UNPKG

agents

Version:

A home for your AI agents

58 lines (57 loc) • 2.37 kB
import { t as browserExecuteModelOutput } from "../../tool-helpers-F3_Wy2YX.js"; import { createQuickActionTools } from "../ai.js"; import { t as createBrowserToolCore } from "../../browser-tool-DOqT1oSD.js"; //#region src/browser/tools/ai-sdk.ts /** The AI SDK's per-call `abortSignal`, typed loosely across versions. */ function abortSignalOf(call) { if (typeof call !== "object" || call === null) return void 0; const signal = call.abortSignal; return signal instanceof AbortSignal ? signal : void 0; } /** * Create an AI SDK tool that lets the model drive a persistent browser with * JavaScript and the Chrome DevTools Protocol. * * The browser outlives each run: tabs, cookies, and logins carry over. The * model never starts, closes, or resets it, and `sessionId: "active"` * addresses the tab it last worked in. If the browser had to be replaced * (it expired or was closed), the code still runs in the new browser and the * result's `notice` says so. * * Browser Run only (Chromium). Guardrails, `keepAliveMs`, and recording are * set on the `Browser`'s provider. * * @example * ```ts * import { Browser, browserRun } from "agents/browser"; * import { browserTool } from "agents/browser/ai-sdk"; * * export class MyAgent extends Agent<Env> { * browser = new Browser({ provider: browserRun(this.env.BROWSER) }); * * constructor(ctx: AgentContext, env: Env) { * super(ctx, env); * this.lifecycle.use(this.browser); * } * * async onChatMessage() { * const tools = { * browser: browserTool({ browser: this.browser, loader: this.env.LOADER }) * }; * // …pass `tools` to streamText / generateText * } * } * ``` */ function browserTool(options) { const core = createBrowserToolCore(options, { screenshotHint: "To show a screenshot, return { type: 'browser_screenshot', mediaType, data } with data from Page.captureScreenshot and mediaType 'image/png', or 'image/jpeg' if you captured with format: 'jpeg'. The user sees the image; you get a short text summary." }); return { description: core.description, inputSchema: core.inputSchema, execute: (input, call) => core.execute(input, { signal: abortSignalOf(call) }), toModelOutput: ({ output }) => browserExecuteModelOutput(output, output.notice ? [output.notice] : []) }; } //#endregion export { browserTool, createQuickActionTools }; //# sourceMappingURL=ai-sdk.js.map