Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import { runWeb } from "./web-errors";import { readBrowserPage, browserProgress } from "./browser-read";import type { BrowserLeaseHooks } from "./browser-session";import { tool } from "ai";import { z } from "zod";import type { BrowserBinding } from "agents/browser";import type { ToolActivityDescriptor } from "./tool-activity";import { readWebUrl } from "./web-read";import { searchWeb } from "./web-search";
export const webActivityDescriptors: Record<string, ToolActivityDescriptor> = { browser_read: { kind: "browser", label: "Read rendered webpage", outputSummary: () => "Rendered webpage read", outcome: (output) => (output as { ok?: boolean })?.ok === true ? "succeeded" : "failed", progress: (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value < browserProgress.length ? { text: browserProgress[value], completed: value, total: browserProgress.length - 1, } : undefined, }, web_search: { kind: "web", label: "Search the web", outputSummary: () => "Web search finished", outcome: (output) => (output as { ok?: boolean })?.ok === true ? "succeeded" : "failed", }, read_url: { kind: "web", label: "Read webpage", outputSummary: () => "Webpage reading finished", outcome: (output) => (output as { ok?: boolean })?.ok === true ? "succeeded" : "failed", },};export function createWebTools( browser: BrowserBinding, ai: Ai, keepAlive: (promise: Promise<unknown>) => void, browserOptions?: { lease: () => BrowserLeaseHooks; progress: (id: string, stage: number) => void; },) { return { browser_read: tool({ description: "Render a public HTTP(S) webpage with JavaScript when read_url lacks the needed content. Returns bounded visible text, exact final source URL, and up to 20 links for follow-up calls. Optionally wait for a CSS selector within a 30-second total deadline. Each call uses a fresh browser; no login, clicks, or persistent session.", inputSchema: z .object({ url: z.string().min(1).max(4096), waitForSelector: z.string().trim().min(1).max(300).optional(), }) .strict(), execute: (input, { abortSignal, toolCallId }) => runWeb( readBrowserPage( browser, input, abortSignal, keepAlive, browserOptions?.lease(), (stage) => browserOptions?.progress(toolCallId, stage), ), abortSignal, ), }), web_search: tool({ description: "Search the public web for up to 5 sources using OpenAI through Cloudflare AI Gateway. Returns provider citation URLs and a separately labeled AI-generated summary, not read page content or verbatim excerpts. Requires AI Gateway credits; never treat failures as no results.", inputSchema: z .object({ query: z.string().trim().min(1).max(500), limit: z.number().int().min(1).max(5).default(5), }) .strict(), execute: ({ query, limit }, { abortSignal }) => runWeb(searchWeb(ai, query, limit), abortSignal), }), read_url: tool({ description: "Read one public HTTP(S) URL as bounded HTML text, Markdown or plain text. Returns source metadata and content for citation; does not execute JavaScript or read PDFs.", inputSchema: z.object({ url: z.string().min(1).max(4096) }).strict(), execute: ({ url }, options) => runWeb(readWebUrl(url, options), options.abortSignal), }), };}