UNPKG

agents

Version:

A home for your AI agents

109 lines (108 loc) • 3.53 kB
import { i as WebSearchError, m as webSearchStructuredOutput, n as createWebSearchToolCore, u as MAX_WEB_SEARCH_QUERY_LENGTH } from "../../../tool-CmAGY6Fo.js"; import { arr, int, obj, opt, str } from "@earendil-works/pi-schema"; //#region src/web/search/tools/pi.ts /** * The model's input schema. The description tells the model the host's * `limit`; a larger value is not rejected (which would cost a validation * round trip) but capped by the core. pi already coerces `"5"` to `5`. */ function webSearchParameters(maxLimit) { return obj({ query: str({ minLength: 1, maxLength: MAX_WEB_SEARCH_QUERY_LENGTH, description: "What to search for." }), limit: opt(int({ minimum: 1, description: `How many results to return (at most ${maxLimit}).` })) }, { open: true }); } /** * What a code mode script's `tools.web_search()` resolves to. A failed * search rejects with the text the model would read. */ function webSearchOutputSchema() { return obj({ query: str({ description: "The query as the provider saw it." }), items: arr(obj({ title: str(), url: str({ description: "Read it with `web_fetch`." }), description: opt(str({ description: "A snippet of the page." })), lastModifiedDate: opt(str({ description: "ISO 8601." })) }, { open: true })) }, { open: true }); } /** * Create a pi tool that searches the web through Cloudflare's Web Search * API. Install it on a registry like any other pi tool. * * The model chooses the query and (within the host's `limit`) how many * results; the host chooses the gateway, provider, and billing. The tool is * `replay: "safe"`: a search interrupted mid-call (say, by an eviction) runs * again when the session recovers, which is a second billed search. * Completed results are stored and not searched again. * * Scripts in pi's code mode get the results as an object, * `{ query, items: [{ title, url, description, lastModifiedDate }] }`; a * failed search rejects with the text the model would read. * * @example * ```ts * import { createRegistry, Harness } from "@earendil-works/pi-durable"; * import { webSearchTool } from "agents/websearch/pi"; * * // In PiHarness's factory, so each object gets its own registry. * harness: ({ storage, context, onWake }) => { * const registry = createRegistry(); * registry.install({ * name: "tools", * tools: [webSearchTool({ binding: this.env.AI, provider: "exa" })] * }); * return Harness.open(storage, { models, registry, onWake }, context); * } * ``` */ function webSearchTool(options) { const core = createWebSearchToolCore(options); return { name: core.name, description: core.description, parameters: webSearchParameters(core.limit), structuredOutputSchema: webSearchOutputSchema(), replay: "safe", async execute(input, _api, context) { const run = await core.run(input, { signal: context.abortSignal }); if (run.ok) return { output: [{ type: "text", text: run.text }], structuredOutput: webSearchStructuredOutput(run.output, { maxDescriptionChars: options.maxDescriptionChars }), details: { ok: true, output: run.output } }; return { output: [{ type: "text", text: run.text }], isError: true, details: { ok: false, message: run.error.message, status: run.error.status, code: run.error.code, retryable: run.error.retryable, requestId: run.error.requestId } }; } }; } //#endregion export { WebSearchError, webSearchTool }; //# sourceMappingURL=pi.js.map