// The web as agent tools (release 27 D7): `web_search` and `web_fetch`, read // only, through the process's pacer. For the local model; a Claude run uses // the API's own server-side search and may take `web_fetch` from here. import type { AgentTool } from "../report/draft/agentTool"; import type { WebPacer } from "./pacer"; import { formatPage, webFetch, WEB_FETCH_DEFAULT_CHARS, WEB_FETCH_MAX_CHARS } from "./fetchPage"; import { formatSearchResults, webSearch, WEB_SEARCH_DEFAULT_RESULTS, WEB_SEARCH_MAX_RESULTS } from "./search"; export type WebToolsOptions = { pacer?: WebPacer; // The search endpoint (default DuckDuckGo's HTML page). searchUrl?: string; // Tests only: let a loopback fake server through web_fetch's fence. allowPrivate?: boolean; resolve?: (host: string) => Promise; }; export function webTools(opts: WebToolsOptions = {}): AgentTool[] { return [ { name: "web_search", description: "Search the web (DuckDuckGo): titles, URLs and snippets. Read only, paced (one request at a time, a few " + "seconds apart). For context — a web page is never cited as what someone said.", inputSchema: { type: "object", properties: { query: { type: "string", description: "What to search for." }, n: { type: "integer", description: `How many results (default ${WEB_SEARCH_DEFAULT_RESULTS}, at most ${WEB_SEARCH_MAX_RESULTS}).` }, }, required: ["query"], additionalProperties: false, }, run: async (input: { query?: unknown; n?: unknown }, ctx) => formatSearchResults( await webSearch(String(input?.query ?? ""), { ...(typeof input?.n === "number" ? { n: input.n } : {}), ...(opts.searchUrl ? { searchUrl: opts.searchUrl } : {}), ...(opts.pacer ? { pacer: opts.pacer } : {}), ...(ctx?.signal ? { signal: ctx.signal } : {}), }), ), }, { name: "web_fetch", description: "Read one web page (http or https, public hosts only) as plain text, cut to max_chars. Read only, paced. " + "For context — a web page is never cited as what someone said.", inputSchema: { type: "object", properties: { url: { type: "string", description: "The page's URL." }, max_chars: { type: "integer", description: `Characters of text (default ${WEB_FETCH_DEFAULT_CHARS}, at most ${WEB_FETCH_MAX_CHARS}).` }, }, required: ["url"], additionalProperties: false, }, run: async (input: { url?: unknown; max_chars?: unknown }, ctx) => formatPage( await webFetch(String(input?.url ?? ""), { ...(typeof input?.max_chars === "number" ? { maxChars: input.max_chars } : {}), ...(opts.pacer ? { pacer: opts.pacer } : {}), ...(opts.allowPrivate ? { allowPrivate: true } : {}), ...(opts.resolve ? { resolve: opts.resolve } : {}), ...(ctx?.signal ? { signal: ctx.signal } : {}), }), ), }, ]; }