UNPKG

agents

Version:

A home for your AI agents

88 lines (87 loc) • 2.62 kB
import { i as WebSearchError, n as createWebSearchToolCore, u as MAX_WEB_SEARCH_QUERY_LENGTH } from "../../../tool-DkT_DJBm.js"; import { 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 }); } /** * 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. * * @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), 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 }], 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