agents
Version:
A home for your AI agents
47 lines (46 loc) • 1.7 kB
JavaScript
import { a as WebFetchError, r as toolFailure, t as createWebFetchToolCore } from "../../../tool-CxKsvxjm.js";
import { t as webFetchInputSchema } from "../../../input-schema-0uUu392-.js";
//#region src/web/fetch/tools/ai-sdk.ts
/**
* Create an AI SDK tool that reads a URL as Markdown, JSON, or text. The
* tool's output is the window the model read plus the page's metadata
* (status, headers, redirects, total length), so it stays small in stored
* messages; `toModelOutput` renders it for the model. A failed fetch throws a `WebFetchError`, 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 detail.
*
* `toModelOutput` only applies to earlier turns when the tools are passed to
* `convertToModelMessages(messages, { tools })`; otherwise the AI SDK sends
* the output back to the model as JSON, without the header or the
* untrusted-content wrapper. `AiSdkHarness` passes them.
*
* @example
* ```ts
* import { webFetchTool } from "agents/webfetch/ai-sdk";
*
* const result = streamText({
* model,
* tools: { web_fetch: webFetchTool({ binding: env.AI }) },
* messages
* });
* ```
*/
function webFetchTool(options) {
const core = createWebFetchToolCore(options);
return {
description: core.description,
inputSchema: webFetchInputSchema(),
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 { WebFetchError, webFetchTool };
//# sourceMappingURL=ai-sdk.js.map