-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Implement web_search tool from cline/cline #11075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/rework-web-search-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−1
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type OpenAI from "openai" | ||
|
|
||
| const WEB_SEARCH_DESCRIPTION = `Performs a web search and returns relevant results with titles and URLs. | ||
|
|
||
| Use this tool when you need to search the web for information. The search returns a list of results with titles and URLs that can help you find up-to-date information from the internet. | ||
|
|
||
| Important notes: | ||
| - If an MCP-provided web search tool is available, prefer using that tool instead, as it may have fewer restrictions | ||
| - You can optionally filter results by allowed or blocked domains | ||
| - You may provide either allowed_domains OR blocked_domains, but NOT both | ||
| - This tool is read-only and does not modify any files` | ||
|
|
||
| const QUERY_PARAMETER_DESCRIPTION = `The search query to use. Must be at least 2 characters.` | ||
|
|
||
| const ALLOWED_DOMAINS_PARAMETER_DESCRIPTION = `Optional array of domains to restrict results to. Only results from these domains will be returned. Cannot be used with blocked_domains.` | ||
|
|
||
| const BLOCKED_DOMAINS_PARAMETER_DESCRIPTION = `Optional array of domains to exclude from results. Results from these domains will be filtered out. Cannot be used with allowed_domains.` | ||
|
|
||
| export default { | ||
| type: "function", | ||
| function: { | ||
| name: "web_search", | ||
| description: WEB_SEARCH_DESCRIPTION, | ||
| strict: false, | ||
| parameters: { | ||
| type: "object", | ||
| properties: { | ||
| query: { | ||
| type: "string", | ||
| description: QUERY_PARAMETER_DESCRIPTION, | ||
| }, | ||
| allowed_domains: { | ||
| type: ["array", "null"], | ||
| description: ALLOWED_DOMAINS_PARAMETER_DESCRIPTION, | ||
| items: { | ||
| type: "string", | ||
| }, | ||
| }, | ||
| blocked_domains: { | ||
| type: ["array", "null"], | ||
| description: BLOCKED_DOMAINS_PARAMETER_DESCRIPTION, | ||
| items: { | ||
| type: "string", | ||
| }, | ||
| }, | ||
| }, | ||
| required: ["query"], | ||
| additionalProperties: false, | ||
| }, | ||
| }, | ||
| } satisfies OpenAI.Chat.ChatCompletionTool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,120 @@ | ||||||
| import { type ClineSayTool } from "@roo-code/types" | ||||||
|
|
||||||
| import { Task } from "../task/Task" | ||||||
| import { formatResponse } from "../prompts/responses" | ||||||
| import type { ToolUse } from "../../shared/tools" | ||||||
|
|
||||||
| import { BaseTool, ToolCallbacks } from "./BaseTool" | ||||||
|
|
||||||
| interface WebSearchParams { | ||||||
| query: string | ||||||
| allowed_domains?: string[] | ||||||
| blocked_domains?: string[] | ||||||
| } | ||||||
|
|
||||||
| export class WebSearchTool extends BaseTool<"web_search"> { | ||||||
| readonly name = "web_search" as const | ||||||
|
|
||||||
| async execute(params: WebSearchParams, task: Task, callbacks: ToolCallbacks): Promise<void> { | ||||||
| const { handleError, pushToolResult, askApproval } = callbacks | ||||||
| const { query, allowed_domains, blocked_domains } = params | ||||||
|
|
||||||
| try { | ||||||
| // Validate required parameters | ||||||
| if (!query || query.trim().length < 2) { | ||||||
| task.consecutiveMistakeCount++ | ||||||
| task.recordToolError("web_search") | ||||||
| task.didToolFailInCurrentTurn = true | ||||||
| pushToolResult(await task.sayAndCreateMissingParamError("web_search", "query")) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Validate mutual exclusivity of domain filters | ||||||
| if (allowed_domains && allowed_domains.length > 0 && blocked_domains && blocked_domains.length > 0) { | ||||||
| task.consecutiveMistakeCount++ | ||||||
| task.recordToolError("web_search") | ||||||
| task.didToolFailInCurrentTurn = true | ||||||
| pushToolResult(formatResponse.toolError("Cannot specify both allowed_domains and blocked_domains")) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| task.consecutiveMistakeCount = 0 | ||||||
|
|
||||||
| // Create message for approval | ||||||
| const completeMessage = JSON.stringify({ | ||||||
| tool: "webSearch", | ||||||
| path: query, | ||||||
| content: `Searching for: ${query}`, | ||||||
| operationIsLocatedInWorkspace: false, | ||||||
| } satisfies ClineSayTool) | ||||||
|
|
||||||
| const didApprove = await askApproval("tool", completeMessage) | ||||||
|
|
||||||
| if (!didApprove) { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Get CloudService and perform search | ||||||
| const provider = task.providerRef.deref() | ||||||
| const cloudService = provider?.getCloudService() | ||||||
|
|
||||||
| if (!cloudService) { | ||||||
| pushToolResult(formatResponse.toolError("Cloud service not available")) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const cloudAPI = cloudService.cloudAPI | ||||||
| if (!cloudAPI) { | ||||||
| pushToolResult(formatResponse.toolError("Cloud API not available")) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Execute the actual search | ||||||
| const options: { allowed_domains?: string[]; blocked_domains?: string[] } = {} | ||||||
| if (allowed_domains && allowed_domains.length > 0) { | ||||||
| options.allowed_domains = allowed_domains | ||||||
| } | ||||||
| if (blocked_domains && blocked_domains.length > 0) { | ||||||
| options.blocked_domains = blocked_domains | ||||||
| } | ||||||
|
|
||||||
| const searchResult = await cloudAPI.webSearch(query, options) | ||||||
|
|
||||||
| // Format results for display | ||||||
| const results = searchResult.results || [] | ||||||
| const resultCount = results.length | ||||||
|
|
||||||
| let resultText = `Search completed (${resultCount} results found)` | ||||||
| if (results.length > 0) { | ||||||
| resultText += ":\n\n" | ||||||
| results.forEach((result: { title: string; url: string }, index: number) => { | ||||||
| resultText += `${index + 1}. ${result.title}\n ${result.url}\n\n` | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| pushToolResult(formatResponse.toolResult(resultText)) | ||||||
| } catch (error) { | ||||||
| await handleError( | ||||||
| "web search", | ||||||
| error instanceof Error ? error : new Error(`Error performing web search: ${String(error)}`), | ||||||
| ) | ||||||
| } finally { | ||||||
| this.resetPartialState() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| override async handlePartial(task: Task, block: ToolUse<"web_search">): Promise<void> { | ||||||
| const query: string | undefined = block.params.query | ||||||
| const sharedMessageProps: ClineSayTool = { | ||||||
| tool: "webSearch", | ||||||
| path: query ?? "", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as above: using
Suggested change
Fix it with Roo Code or mention @roomote and request a fix. |
||||||
| content: `Searching for: ${query ?? ""}`, | ||||||
| operationIsLocatedInWorkspace: false, | ||||||
| } | ||||||
|
|
||||||
| const partialMessage = JSON.stringify(sharedMessageProps) | ||||||
| await task.ask("tool", partialMessage, block.partial).catch(() => {}) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export const webSearchTool = new WebSearchTool() | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ClineSayToolinterface has a dedicatedqueryproperty for search queries (see line 824 invscode-extension-host.ts), but this code usespathinstead. Usingpathis semantically incorrect since it typically represents file paths in this interface. This could cause confusion in the UI layer when displaying tool approval dialogs.Fix it with Roo Code or mention @roomote and request a fix.