-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add web search functionality similar to Cline #10676
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
roomote
wants to merge
3
commits into
main
Choose a base branch
from
feature/COM-464-web-search
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.
+542
−1
Draft
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type OpenAI from "openai" | ||
|
|
||
| const WEB_SEARCH_DESCRIPTION = `Request to perform a web search and retrieve relevant information from the internet. This tool allows you to search for current information, documentation, tutorials, and other web content that may be helpful for completing tasks. Use this when you need up-to-date information that may not be in your training data.` | ||
|
|
||
| const QUERY_PARAMETER_DESCRIPTION = `The search query string. Be specific and include relevant keywords for better results.` | ||
|
|
||
| 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, | ||
| }, | ||
| }, | ||
| 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,173 @@ | ||
| import { Task } from "../task/Task" | ||
| import { formatResponse } from "../prompts/responses" | ||
| import { BaseTool, ToolCallbacks } from "./BaseTool" | ||
| import { t } from "../../i18n" | ||
|
|
||
| export interface WebSearchParams { | ||
| query: string | ||
| allowed_domains?: string[] | ||
| blocked_domains?: string[] | ||
| } | ||
|
|
||
| /** | ||
| * Parse JSON array string safely, returning empty array on parse errors | ||
| */ | ||
| function parseDomainsArray(domainsStr: string | undefined): string[] { | ||
| if (!domainsStr || domainsStr.trim() === "") { | ||
| return [] | ||
| } | ||
| try { | ||
| const parsed = JSON.parse(domainsStr) | ||
| return Array.isArray(parsed) ? parsed.filter((d) => typeof d === "string") : [] | ||
| } catch { | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| // Mock search results for demonstration | ||
| // In a real implementation, this would integrate with a search API like: | ||
| // - Brave Search API | ||
| // - Google Custom Search API | ||
| // - Bing Search API | ||
| // - DuckDuckGo API | ||
| // - Or use an MCP server like Perplexity | ||
| const mockSearchResults = [ | ||
| { | ||
| title: "Getting started with web development", | ||
| url: "https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web", | ||
| snippet: | ||
| "Learn the basics of web development including HTML, CSS, and JavaScript. This comprehensive guide covers everything you need to know to start building websites.", | ||
| }, | ||
| { | ||
| title: "Web Development Best Practices", | ||
| url: "https://web.dev/learn", | ||
| snippet: | ||
| "Modern web development best practices including performance optimization, accessibility, SEO, and progressive web apps. Learn how to build fast, reliable web experiences.", | ||
| }, | ||
| { | ||
| title: "JavaScript Documentation", | ||
| url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript", | ||
| snippet: | ||
| "Comprehensive JavaScript documentation covering core language features, APIs, and best practices for modern web development.", | ||
| }, | ||
| ] | ||
|
|
||
| export class WebSearchTool extends BaseTool<"web_search"> { | ||
| readonly name = "web_search" as const | ||
|
|
||
| parseLegacy(params: Partial<Record<string, string>>): WebSearchParams { | ||
| const query = params.query || "" | ||
| const allowed_domains = parseDomainsArray(params.allowed_domains) | ||
| const blocked_domains = parseDomainsArray(params.blocked_domains) | ||
|
|
||
| return { | ||
| query, | ||
| ...(allowed_domains.length > 0 ? { allowed_domains } : {}), | ||
| ...(blocked_domains.length > 0 ? { blocked_domains } : {}), | ||
| } | ||
| } | ||
|
|
||
| async execute(params: WebSearchParams, task: Task, callbacks: ToolCallbacks): Promise<void> { | ||
| const { query, allowed_domains, blocked_domains } = params | ||
| const { handleError, pushToolResult, askApproval, removeClosingTag } = callbacks | ||
|
|
||
| if (!query || query.trim().length < 2) { | ||
| task.consecutiveMistakeCount++ | ||
| task.recordToolError("web_search") | ||
| pushToolResult( | ||
| await task.sayAndCreateMissingParamError("web_search", "query", "Query must be at least 2 characters"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // Validate mutual exclusivity of domain filters | ||
| if (allowed_domains && allowed_domains.length > 0 && blocked_domains && blocked_domains.length > 0) { | ||
| task.consecutiveMistakeCount++ | ||
| task.didToolFailInCurrentTurn = true | ||
| pushToolResult( | ||
| formatResponse.toolError("Cannot specify both allowed_domains and blocked_domains at the same time"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| task.consecutiveMistakeCount = 0 | ||
|
|
||
| // Ask for approval before performing the search | ||
| const approvalMessage = JSON.stringify({ | ||
| tool: "webSearch", | ||
| query: removeClosingTag("query", query), | ||
| ...(allowed_domains && allowed_domains.length > 0 ? { allowed_domains } : {}), | ||
| ...(blocked_domains && blocked_domains.length > 0 ? { blocked_domains } : {}), | ||
| isOutsideWorkspace: true, | ||
| }) | ||
|
|
||
| const didApprove = await askApproval("tool", approvalMessage) | ||
|
|
||
| if (!didApprove) { | ||
| return | ||
| } | ||
|
|
||
| // Construct domain filter description for response | ||
| let domainInfo = "" | ||
| if (allowed_domains && allowed_domains.length > 0) { | ||
| domainInfo = `\nDomain filter: Only results from ${allowed_domains.join(", ")}` | ||
| } else if (blocked_domains && blocked_domains.length > 0) { | ||
| domainInfo = `\nExcluding results from: ${blocked_domains.join(", ")}` | ||
| } | ||
|
|
||
| // Log the search query | ||
| await task.say("text", t("tools:webSearch.searching", { query })) | ||
|
|
||
| // In a real implementation, this would call an actual search API | ||
| // For now, we'll return mock results to demonstrate the functionality | ||
| // This allows the tool to work without requiring additional API keys or setup | ||
|
|
||
| // Simulate API delay | ||
| await new Promise((resolve) => setTimeout(resolve, 500)) | ||
|
|
||
| // Format the search results | ||
| let resultText = t("tools:webSearch.results", { query }) | ||
| if (domainInfo) { | ||
| resultText += domainInfo | ||
| } | ||
| resultText += "\n\n" | ||
|
|
||
| mockSearchResults.forEach((result, index) => { | ||
| resultText += `${index + 1}. **${result.title}**\n` | ||
| resultText += ` URL: ${result.url}\n` | ||
| resultText += ` ${result.snippet}\n\n` | ||
| }) | ||
|
|
||
| resultText += t("tools:webSearch.mockNote") | ||
|
|
||
| // Record successful tool usage | ||
| task.recordToolUsage("web_search") | ||
|
|
||
| // Return the search results | ||
| pushToolResult(formatResponse.toolResult(resultText)) | ||
| } catch (error) { | ||
| await handleError("performing web search", error as Error) | ||
| task.recordToolError("web_search") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| override async handlePartial(task: Task, block: any): Promise<void> { | ||
| const query: string | undefined = block.params.query | ||
| const allowed_domains = parseDomainsArray(block.params.allowed_domains) | ||
| const blocked_domains = parseDomainsArray(block.params.blocked_domains) | ||
|
|
||
| const sharedMessageProps = { | ||
| tool: "webSearch", | ||
| query: query, | ||
| ...(allowed_domains.length > 0 ? { allowed_domains } : {}), | ||
| ...(blocked_domains.length > 0 ? { blocked_domains } : {}), | ||
| isOutsideWorkspace: true, | ||
| } | ||
|
|
||
| await task.ask("tool", JSON.stringify(sharedMessageProps), block.partial).catch(() => {}) | ||
| } | ||
| } | ||
|
|
||
| export const webSearchTool = new WebSearchTool() | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Missing tool registration in
presentAssistantMessage.ts: ThewebSearchToolis exported here but never imported or registered in the tool dispatcher. The switch statement inpresentAssistantMessage.ts(lines 872-1190) lacks acase "web_search":handler, and thetoolDescriptionfunction also needs a case for this tool. When the model callsweb_search, it will fall through to thedefault:case and fail with "Unknown tool 'web_search'. This tool does not exist."Required changes to
src/core/assistant-message/presentAssistantMessage.ts:import { webSearchTool } from "../tools/WebSearchTool"toolDescriptionfunction:case "web_search": return \[${block.name} for '${block.params.query}']``webSearchTool.handle()Fix it with Roo Code or mention @roomote and request a fix.