Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/types/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const toolNames = [
"update_todo_list",
"run_slash_command",
"generate_image",
"web_search",
"custom_tool",
] as const

Expand Down
24 changes: 24 additions & 0 deletions src/core/assistant-message/NativeToolCallParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,20 @@ export class NativeToolCallParser {
}
break

case "web_search":
if (partialArgs.query !== undefined) {
nativeArgs = {
query: partialArgs.query,
allowed_domains: Array.isArray(partialArgs.allowed_domains)
? partialArgs.allowed_domains
: undefined,
blocked_domains: Array.isArray(partialArgs.blocked_domains)
? partialArgs.blocked_domains
: undefined,
}
}
break

case "codebase_search":
if (partialArgs.query !== undefined) {
nativeArgs = {
Expand Down Expand Up @@ -697,6 +711,16 @@ export class NativeToolCallParser {
}
break

case "web_search":
if (args.query !== undefined) {
nativeArgs = {
query: args.query,
allowed_domains: Array.isArray(args.allowed_domains) ? args.allowed_domains : undefined,
blocked_domains: Array.isArray(args.blocked_domains) ? args.blocked_domains : undefined,
} as any as NativeArgsFor<TName>
}
break

case "codebase_search":
if (args.query !== undefined) {
nativeArgs = {
Expand Down
2 changes: 2 additions & 0 deletions src/core/prompts/tools/native-tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import edit_file from "./edit_file"
import searchFiles from "./search_files"
import switchMode from "./switch_mode"
import updateTodoList from "./update_todo_list"
import webSearch from "./web_search"
import writeToFile from "./write_to_file"

export { getMcpServerTools } from "./mcp_server"
Expand Down Expand Up @@ -73,6 +74,7 @@ export function getNativeTools(options: NativeToolsOptions = {}): OpenAI.Chat.Ch
searchFiles,
switchMode,
updateTodoList,
webSearch,
writeToFile,
] satisfies OpenAI.Chat.ChatCompletionTool[]
}
Expand Down
25 changes: 25 additions & 0 deletions src/core/prompts/tools/native-tools/web_search.ts
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
173 changes: 173 additions & 0 deletions src/core/tools/WebSearchTool.ts
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()
Copy link
Contributor Author

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: The webSearchTool is exported here but never imported or registered in the tool dispatcher. The switch statement in presentAssistantMessage.ts (lines 872-1190) lacks a case "web_search": handler, and the toolDescription function also needs a case for this tool. When the model calls web_search, it will fall through to the default: case and fail with "Unknown tool 'web_search'. This tool does not exist."

Required changes to src/core/assistant-message/presentAssistantMessage.ts:

  1. Add import: import { webSearchTool } from "../tools/WebSearchTool"
  2. Add case in toolDescription function: case "web_search": return \[${block.name} for '${block.params.query}']``
  3. Add case in switch statement to call webSearchTool.handle()

Fix it with Roo Code or mention @roomote and request a fix.

Loading
Loading