agents
Version:
A home for your AI agents
46 lines (45 loc) • 1.66 kB
JavaScript
import { i as WebSearchError, n as createWebSearchToolCore, r as toolFailure } from "../../../tool-DkT_DJBm.js";
import { t as webSearchInputSchema } from "../../../input-schema-waggtYrH.js";
//#region src/web/search/tools/ai-sdk.ts
/**
* Create an AI SDK tool that searches the web through Cloudflare's Web
* Search API. The host gets the full response as the tool's output (whole
* descriptions, provider metadata); the model gets a trimmed text rendering
* via `toModelOutput`. A failed search throws a `WebSearchError`, which the
* AI SDK reports to the model as a tool error: its `message` is written for
* the model, and its `cause` is the source's error with the API's detail.
*
* `toModelOutput` only applies to earlier turns when the tools are passed to
* `convertToModelMessages(messages, { tools })`; otherwise the AI SDK sends
* the full response back to the model as JSON. `AiSdkHarness` passes them.
*
* @example
* ```ts
* import { webSearchTool } from "agents/websearch/ai-sdk";
*
* const result = streamText({
* model,
* tools: { web_search: webSearchTool({ binding: env.AI }) },
* messages
* });
* ```
*/
function webSearchTool(options) {
const core = createWebSearchToolCore(options);
return {
description: core.description,
inputSchema: webSearchInputSchema(core.limit),
async execute(input, { abortSignal }) {
const run = await core.run(input, { signal: abortSignal });
if (!run.ok) throw toolFailure(run);
return run.output;
},
toModelOutput: ({ output }) => ({
type: "text",
value: core.render(output)
})
};
}
//#endregion
export { WebSearchError, webSearchTool };
//# sourceMappingURL=ai-sdk.js.map