@parcha/pi-browser 0.1.0-beta.34 → 0.1.0-beta.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -17,7 +17,6 @@ export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicFetch, type Pub
17
17
  export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage, type PageRead, type ReadPageOptions } from "./core/read-page.js";
18
18
  export * from "./core/receipts.js";
19
19
  export * from "./core/redact.js";
20
- export * from "./core/sites.js";
21
20
  export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
22
21
  export * from "./core/tools.js";
23
22
  export * from "./core/usage.js";
package/dist/index.js CHANGED
@@ -18,7 +18,6 @@ export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicFetch } from ".
18
18
  export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage } from "./core/read-page.js";
19
19
  export * from "./core/receipts.js";
20
20
  export * from "./core/redact.js";
21
- export * from "./core/sites.js";
22
21
  export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
23
22
  export * from "./core/tools.js";
24
23
  export * from "./core/usage.js";
@@ -3,8 +3,6 @@ import { type CdpSession } from "./cdp-call.js";
3
3
  import { type ProfileStore } from "./profiles.js";
4
4
  export { rewriteConnectUrl, browserbaseNoProxyHosts, applyBrowserbaseNoProxy } from "./browserbase-net.js";
5
5
  export { fileProfileStore, memoryProfileStore, type ProfileStore } from "./profiles.js";
6
- export declare const SEARCH_QUERY_MAX = 200;
7
- export declare const SEARCH_RESULTS_MAX = 25;
8
6
  export type BrowserbaseSdk = {
9
7
  sessions: {
10
8
  create(body: BrowserbaseSessionBody, options?: RequestOptions): Promise<{
@@ -62,41 +60,12 @@ export type BrowserbaseSdk = {
62
60
  id: string;
63
61
  }>;
64
62
  };
65
- fetchAPI: {
66
- create(body: {
67
- url: string;
68
- allowRedirects?: boolean;
69
- proxies?: boolean;
70
- format?: string;
71
- }, options?: RequestOptions): Promise<FetchApiResult>;
72
- };
73
- search: {
74
- web(body: {
75
- query: string;
76
- numResults?: number;
77
- }, options?: RequestOptions): Promise<SearchApiResult>;
78
- };
79
63
  get<T = unknown>(path: string, options?: RequestOptions & {
80
64
  query?: Record<string, unknown>;
81
65
  headers?: Record<string, string>;
82
66
  __binaryResponse?: boolean;
83
67
  }): Promise<T>;
84
68
  };
85
- type FetchApiResult = {
86
- statusCode?: unknown;
87
- contentType?: unknown;
88
- content?: unknown;
89
- } | null | undefined;
90
- type SearchRow = {
91
- url?: unknown;
92
- title?: string | null;
93
- author?: string | null;
94
- publishedDate?: string | null;
95
- published?: string | null;
96
- };
97
- type SearchApiResult = {
98
- results?: SearchRow[];
99
- } | null | undefined;
100
69
  type BrowserbaseSessionBody = {
101
70
  proxies: boolean | Array<Record<string, unknown>>;
102
71
  timeout: number;
@@ -2,16 +2,14 @@
2
2
  // URL (it carries a signing key) is held here, found again by `retrieve` after a restart.
3
3
  import { createReadStream } from "node:fs";
4
4
  import { isRefusal } from "../core/failures.js";
5
- import { fetchUsage, resolvePrices, searchUsage } from "../core/usage.js";
5
+ import { resolvePrices } from "../core/usage.js";
6
6
  import { cdpCall } from "./cdp-call.js";
7
7
  import { applyBrowserbaseNoProxy, bindConnectDial, installWebSocketWrapper, rewriteConnectUrl, unbindConnectDial } from "./browserbase-net.js";
8
8
  import { directFetch } from "./http.js";
9
9
  import { claimWriter, fileProfileStore, freeWriter, isWriter, noteWriter } from "./profiles.js";
10
- import { accountKey, assertTag, browserbaseHoldings, defaultSleep, DEFAULT_CONFIRM, extensionArchive, nonEmpty, releaseAndConfirm, tagValue, untilAborted, uploadOnce } from "./shared.js";
10
+ import { accountKey, assertTag, browserbaseHoldings, defaultSleep, DEFAULT_CONFIRM, extensionArchive, nonEmpty, releaseAndConfirm, tagValue, uploadOnce } from "./shared.js";
11
11
  export { rewriteConnectUrl, browserbaseNoProxyHosts, applyBrowserbaseNoProxy } from "./browserbase-net.js";
12
12
  export { fileProfileStore, memoryProfileStore } from "./profiles.js";
13
- export const SEARCH_QUERY_MAX = 200;
14
- export const SEARCH_RESULTS_MAX = 25;
15
13
  /** Into `downloadPath` (default `downloads`, Browserbase's synced-downloads rule); the socket stays open. */
16
14
  export const setDownloadBehavior = (url, options = {}) => cdpCall(url, "Browser.setDownloadBehavior", { behavior: "allow", downloadPath: options.downloadPath ?? "downloads", eventsEnabled: true }, options.timeoutMs);
17
15
  const CAPS = { timeoutModel: "wall-clock", maxLifetimeS: 21_600, survivesDisconnect: true, releaseIsAsync: true, profiles: true, extension: "uploaded-per-launch", settings: { proxies: true, verified: true, geolocation: true, region: true, context: true },
@@ -292,28 +290,5 @@ export function browserbaseProvider(options = {}) {
292
290
  return response.body;
293
291
  },
294
292
  },
295
- async fetch(request, signal) {
296
- signal?.throwIfAborted();
297
- const result = await untilAborted((async () => (await client()).fetchAPI.create({ url: request.url, proxies: request.proxies, allowRedirects: true, format: request.format }, { signal }))(), signal);
298
- // The Fetch API reports no final URL.
299
- return { finalUrl: null, statusCode: Number(result?.statusCode) || null, contentType: typeof result?.contentType === "string" ? result.contentType : null, content: contentText(result?.content), usage: fetchUsage(prices, request.proxies) };
300
- },
301
- async search(request, signal) {
302
- signal?.throwIfAborted();
303
- // The facade validates strictly (query 1 to 200 characters, 1 to 25 results): clamp, so a long query degrades and
304
- // never throws.
305
- const query = request.query.slice(0, SEARCH_QUERY_MAX);
306
- const numResults = Math.max(1, Math.min(SEARCH_RESULTS_MAX, Math.floor(request.n) || 10));
307
- const result = await untilAborted((async () => (await client()).search.web({ query, numResults }, { signal }))(), signal);
308
- const rows = Array.isArray(result?.results) ? result.results : Array.isArray(result) ? result : [];
309
- return { results: rows.filter((r) => typeof r?.url === "string").map((r) => ({ url: r.url, title: r.title ?? null, author: r.author ?? null, published: r.publishedDate ?? r.published ?? null })), usage: searchUsage(prices) };
310
- },
311
293
  };
312
294
  }
313
- function contentText(content) {
314
- if (typeof content === "string")
315
- return content;
316
- if (Array.isArray(content))
317
- return content.map((c) => (typeof c === "string" ? c : typeof c?.text === "string" ? c.text : JSON.stringify(c))).join("\n");
318
- return content && typeof content === "object" ? JSON.stringify(content) : "";
319
- }
@@ -1,20 +1,6 @@
1
1
  import { EventEmitter } from "node:events";
2
- import type { AttachTarget, BrowserProvider, ProviderCaps, RemoteFile, SearchResult } from "../core/host.js";
2
+ import type { AttachTarget, BrowserProvider, ProviderCaps, RemoteFile } from "../core/host.js";
3
3
  import { type Sentinels } from "./sentinels.js";
4
- export type FakeFetchByFormat = {
5
- markdown?: {
6
- contentType: string;
7
- content: string;
8
- } | {
9
- status: number;
10
- };
11
- raw?: {
12
- contentType: string;
13
- content: string;
14
- } | {
15
- status: number;
16
- };
17
- };
18
4
  /** A vendor SDK's failure: the HTTP status is on the error, never inside the text. */
19
5
  export declare class FakeProviderError extends Error {
20
6
  readonly status: number;
@@ -63,9 +49,6 @@ export type FakeBackendOptions = {
63
49
  caps?: Partial<ProviderCaps>;
64
50
  start?: string;
65
51
  pages?: Record<string, FixturePage>;
66
- /** A URL not listed is a 404; an object answers markdown and raw apart. */
67
- fetches?: Record<string, string | FakeFetchByFormat>;
68
- search?: SearchResult["results"];
69
52
  /** Every ledger row is also appended here as JSONL, so a killed process leaves its calls behind. */
70
53
  logFile?: string;
71
54
  connections?: BrokerConnection[];
@@ -128,7 +111,7 @@ export declare class FakeBackend {
128
111
  private delays;
129
112
  private peak;
130
113
  private echoOn;
131
- readonly opts: Required<Pick<FakeBackendOptions, "start" | "fetches" | "search" | "connections" | "downloads">> & FakeBackendOptions;
114
+ readonly opts: Required<Pick<FakeBackendOptions, "start" | "connections" | "downloads">> & FakeBackendOptions;
132
115
  constructor(options?: FakeBackendOptions);
133
116
  /** The next call of `op` is applied and recorded, then never answered. */
134
117
  hold(op: string): void;
@@ -155,8 +138,6 @@ export declare class FakeBackend {
155
138
  attaches: number;
156
139
  releases: number;
157
140
  releaseNoops: number;
158
- fetches: number;
159
- searches: number;
160
141
  dispatches: number;
161
142
  reads: number;
162
143
  peakLive: number;
@@ -62,8 +62,6 @@ export class FakeBackend {
62
62
  this.opts = {
63
63
  ...options,
64
64
  start: options.start ?? "about:blank",
65
- fetches: options.fetches ?? {},
66
- search: options.search ?? [],
67
65
  downloads: options.downloads ?? {},
68
66
  connections: options.connections ?? [{ name: "demo", startUrl: "https://login.sentinel.test/", fields: [{ name: "username", type: "text" }, { name: "password", type: "password" }, { name: "otp", type: "totp" }], allowedOrigins: ["https://login.sentinel.test"], enrollment: true }],
69
67
  };
@@ -104,7 +102,7 @@ export class FakeBackend {
104
102
  const count = (op) => this.ledger.filter((r) => r.op === op).length;
105
103
  return {
106
104
  creates: count("create"), findByTag: count("findByTag"), status: count("status"), attaches: count("attach"), releases: count("release"), releaseNoops: count("release-noop"),
107
- fetches: count("fetch"), searches: count("search"), dispatches: this.dispatched.filter((d) => d.op === "run").length, reads: this.dispatched.filter((d) => d.op === "read").length,
105
+ dispatches: this.dispatched.filter((d) => d.op === "run").length, reads: this.dispatched.filter((d) => d.op === "read").length,
108
106
  peakLive: this.peak, liveAtEnd: this.liveCount(),
109
107
  };
110
108
  }
@@ -211,26 +209,6 @@ export class FakeBackend {
211
209
  const view = b.sentinels.bbLiveViewUrl;
212
210
  return { fullscreen: view, framed: `${view}&framed=1`, pages: [{ id: "p1", url: s.url, title: b.pages[s.url]?.title ?? "", fullscreen: view }] };
213
211
  },
214
- async fetch(request, signal) {
215
- await b.before("fetch", signal);
216
- b.record("fetch", { url: request.url });
217
- await b.after("fetch");
218
- const entry = b.opts.fetches[request.url];
219
- if (entry === undefined)
220
- throw new FakeProviderError(`fetch ${request.url}: 404`, 404);
221
- if (typeof entry === "string")
222
- return { finalUrl: request.url, statusCode: 200, contentType: "text/markdown", content: entry };
223
- const answer = entry[request.format === "raw" ? "raw" : "markdown"];
224
- if (!answer || "status" in answer)
225
- throw new FakeProviderError(`fetch ${request.url}: ${answer?.status ?? 404}`, answer?.status ?? 404);
226
- return { finalUrl: request.url, statusCode: 200, contentType: answer.contentType, content: answer.content };
227
- },
228
- async search(request, signal) {
229
- await b.before("search", signal);
230
- b.record("search", { query: request.query });
231
- await b.after("search");
232
- return { results: b.opts.search.slice(0, request.n) };
233
- },
234
212
  downloads: {
235
213
  async list(_ref) { return Object.entries(b.opts.downloads).map(([name, body]) => ({ name, sizeBytes: Buffer.byteLength(body), modifiedAt: null })); },
236
214
  async read(_ref, fileName, maxBytes) {
@@ -299,8 +277,6 @@ export function remoteProvider(url, options = {}) {
299
277
  attach: (ref, signal) => call("attach", [ref], signal),
300
278
  release: (ref, signal) => call("release", [ref], signal),
301
279
  liveView: (ref) => call("liveView", [ref]),
302
- fetch: (request, signal) => call("fetch", [request], signal),
303
- search: (request, signal) => call("search", [request], signal),
304
280
  };
305
281
  }
306
282
  /** Counts a fill before the provider is touched and never repeats an unknown one; its failures carry only codes. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/pi-browser",
3
- "version": "0.1.0-beta.34",
3
+ "version": "0.1.0-beta.35",
4
4
  "description": "Your agent gets its own browser, and you can take over and hand it back.",
5
5
  "keywords": [
6
6
  "pi-durable",
@@ -12,7 +12,7 @@
12
12
  "license": "Apache-2.0",
13
13
  "author": "Parcha Labs, Inc.",
14
14
  "engines": {
15
- "node": ">=24.11.0"
15
+ "node": ">=24.22.3"
16
16
  },
17
17
  "exports": {
18
18
  ".": {
@@ -1,33 +0,0 @@
1
- import type { ToolContract } from "./contract.js";
2
- import type { ToolOutput } from "./custody.js";
3
- import type { ChildRunner, SiteInfo, SiteRunPort } from "./host.js";
4
- export declare const MAX_SITE_INSTRUCTIONS = 16000;
5
- /** The site enum and the description render from the listing, which is configuration: every request sends the same
6
- * text and keeps the prompt cache. */
7
- export declare function useWebsiteContract(sites: readonly SiteInfo[]): ToolContract;
8
- /** Only the declared shape crosses back to the model: a port's extra fields, such as capabilities or URLs, are dropped
9
- * here, and an answer with both shapes or neither is no answer. */
10
- export declare function siteAnswer(raw: unknown): {
11
- run: {
12
- id: string;
13
- state: string;
14
- };
15
- } | {
16
- report: string;
17
- } | null;
18
- export declare function useWebsite(args: {
19
- site?: unknown;
20
- request_id?: unknown;
21
- instructions?: unknown;
22
- }, port: SiteRunPort, sites: readonly SiteInfo[], ctx: {
23
- signal: AbortSignal;
24
- runChild: ChildRunner;
25
- }): Promise<ToolOutput>;
26
- export type LocalSite = {
27
- title: string;
28
- description: string;
29
- startUrl: string;
30
- };
31
- /** `use_website` for a plain pi user: each task runs a child agent through the adapter's `runChild` and answers its
32
- * report. A request_id runs once; a failed run may be asked again. */
33
- export declare function localSites(sites: Record<string, LocalSite>): SiteRunPort;
@@ -1,104 +0,0 @@
1
- import { browserFailure } from "./failures.js";
2
- export const MAX_SITE_INSTRUCTIONS = 16_000;
3
- const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4
- const RUN_ID = /^[A-Za-z0-9_.:-]{1,200}$/;
5
- const RUN_STATE = /^[a-z_]{1,40}$/;
6
- /** The site enum and the description render from the listing, which is configuration: every request sends the same
7
- * text and keeps the prompt cache. */
8
- export function useWebsiteContract(sites) {
9
- return {
10
- name: "use_website",
11
- label: "Use a website",
12
- description: [
13
- "Hand a task to an agent that works on one of these websites in its own browser, and get back what it found or did:",
14
- ...sites.map((site) => `- ${site.site}: ${site.title}. ${site.description}`),
15
- "Give a new request_id (a UUID) for each new task. Calling again with the request_id of a finished task answers its result without running it again; a task that failed runs again, and may repeat what it already did on the site.",
16
- "Several calls in one turn run at once, each agent in its own browser.",
17
- ].join("\n"),
18
- parameters: {
19
- type: "object", additionalProperties: false, required: ["site", "request_id", "instructions"],
20
- properties: {
21
- site: { type: "string", enum: sites.map((site) => site.site), description: "The website to use." },
22
- request_id: { type: "string", description: "A UUID you choose for this task." },
23
- instructions: { type: "string", maxLength: MAX_SITE_INSTRUCTIONS, description: "What to do on the website and what to report back." },
24
- },
25
- },
26
- effect: "effect", replay: "safe", executionMode: "parallel", evidence: "none", listedWhen: "sites",
27
- };
28
- }
29
- /** Only the declared shape crosses back to the model: a port's extra fields, such as capabilities or URLs, are dropped
30
- * here, and an answer with both shapes or neither is no answer. */
31
- export function siteAnswer(raw) {
32
- if (!raw || typeof raw !== "object")
33
- return null;
34
- const { run, report } = raw;
35
- if ((run === undefined) === (report === undefined))
36
- return null;
37
- if (report !== undefined)
38
- return typeof report === "string" ? { report } : null;
39
- const { id, state } = (run ?? {});
40
- return typeof id === "string" && RUN_ID.test(id) && typeof state === "string" && RUN_STATE.test(state) ? { run: { id, state } } : null;
41
- }
42
- const siteOutput = (body, isError = false) => ({ content: [{ type: "text", text: JSON.stringify(body) }], ...(isError ? { isError: true } : {}) });
43
- const siteRefusal = (message) => siteOutput(browserFailure("refused", message), true);
44
- export async function useWebsite(args, port, sites, ctx) {
45
- const site = sites.find((known) => known.site === args.site);
46
- if (!site)
47
- return siteRefusal(`site is one of: ${sites.map((known) => known.site).join(", ")}`);
48
- if (typeof args.request_id !== "string" || !UUID.test(args.request_id))
49
- return siteRefusal("request_id is a UUID you choose for this task");
50
- const instructions = typeof args.instructions === "string" ? args.instructions.trim() : "";
51
- if (!instructions || instructions.length > MAX_SITE_INSTRUCTIONS)
52
- return siteRefusal(`instructions say what to do on the site, in at most ${MAX_SITE_INSTRUCTIONS} characters`);
53
- const head = { ok: true, site: site.site, request_id: args.request_id };
54
- let raw;
55
- try {
56
- raw = await port.use({ site: site.site, requestId: args.request_id, instructions }, ctx);
57
- }
58
- catch {
59
- // A port's error text can carry its grant or a URL: none of it is forwarded.
60
- if (ctx.signal.aborted)
61
- return siteOutput({ ok: false, code: "aborted", retryable: true, effect: "possibly_effected", message: "The call was cancelled, and the site's task may already have changed the site. Calling again with the same request_id runs an unfinished task again, and it may repeat what it did." }, true);
62
- return siteOutput({ ok: false, code: "command_failed", retryable: true, effect: "possibly_effected", message: `The ${site.site} task failed, and may already have changed the site. Calling use_website again with the same request_id runs it again, and it may repeat what it did.` }, true);
63
- }
64
- const parsed = siteAnswer(raw);
65
- if (!parsed)
66
- return siteOutput({ ok: false, code: "command_failed", retryable: false, effect: "possibly_effected", message: `The ${site.site} runner answered neither a started task nor a report.` }, true);
67
- if ("report" in parsed)
68
- return siteOutput({ ...head, report: parsed.report });
69
- return siteOutput({ ...head, run: parsed.run, next: "The site's task was started; its result arrives through your host. Do not call use_website again for this request." });
70
- }
71
- const childPrompt = (site, entry, instructions) => [
72
- `You are doing one task on ${entry.title} (${site}) for another agent, in your own browser.`,
73
- `Start at ${entry.startUrl}.`,
74
- `Task: ${instructions}`,
75
- "Use the browser tools. Do not ask questions. Finish with a short report of what you found or did.",
76
- ].join("\n");
77
- /** `use_website` for a plain pi user: each task runs a child agent through the adapter's `runChild` and answers its
78
- * report. A request_id runs once; a failed run may be asked again. */
79
- export function localSites(sites) {
80
- for (const [site, entry] of Object.entries(sites)) {
81
- if (!/^https?:\/\//.test(entry.startUrl))
82
- throw new Error(`site ${site}: startUrl must be an http or https URL`);
83
- }
84
- const runs = new Map();
85
- return {
86
- async list() {
87
- return Object.entries(sites).map(([site, entry]) => ({ site, title: entry.title, description: entry.description }));
88
- },
89
- async use({ site, requestId, instructions }, { signal, runChild }) {
90
- const entry = Object.hasOwn(sites, site) ? sites[site] : undefined;
91
- if (!entry)
92
- throw new Error(`no site named ${site}`);
93
- const key = `${site}\u0000${requestId}`;
94
- let run = runs.get(key);
95
- if (!run) {
96
- run = runChild({ site, requestId, prompt: childPrompt(site, entry, instructions) }, signal);
97
- runs.set(key, run);
98
- run.catch(() => { if (runs.get(key) === run)
99
- runs.delete(key); });
100
- }
101
- return { report: await run };
102
- },
103
- };
104
- }
@@ -1,47 +0,0 @@
1
- import { defineExtension, type ConversationId, type ToolRegistration } from "@earendil-works/pi-durable";
2
- import type { Decisions } from "../core/decisions.js";
3
- import type { EvidenceSink } from "../core/evidence.js";
4
- import type { FetchRequest, FetchResult, PriceTable, SearchRequest, SearchResult } from "../core/host.js";
5
- import { type FetchSource, type RenderedPage, type SearchSource, type WebBackup, type WebToolsOptions } from "../core/web.js";
6
- export type WebExtensionOptions = {
7
- providers: {
8
- /** Names the source in `via` (`<name>_fetch`, `<name>_search`); default "provider". */
9
- name?: string;
10
- fetch?: (request: FetchRequest, signal?: AbortSignal) => Promise<FetchResult>;
11
- search?: (request: SearchRequest, signal?: AbortSignal) => Promise<SearchResult>;
12
- };
13
- /** Searches tried before `providers.search`, in order (see `SearchSource`). */
14
- searchSources?: readonly SearchSource[];
15
- /** Readers tried before `providers.fetch` for a markdown read, in order (see `FetchSource`). */
16
- fetchSources?: readonly FetchSource[];
17
- evidence: EvidenceSink;
18
- /** Per use: evidence label and proxies. Default: label "web", proxies on. */
19
- scope?: (conversationId: ConversationId) => {
20
- label: string;
21
- proxies: boolean;
22
- } | Promise<{
23
- label: string;
24
- proxies: boolean;
25
- }>;
26
- prices?: Partial<PriceTable>;
27
- redact?: {
28
- values: () => readonly string[];
29
- };
30
- /** The host's fallback fetchers, tried in order when the provider cannot fetch a page. */
31
- backups?: ReadonlyArray<WebBackup>;
32
- decisions?: Pick<Decisions, "classifyPage">;
33
- render?: {
34
- enabled: boolean;
35
- read(url: string, signal?: AbortSignal): Promise<RenderedPage>;
36
- };
37
- pdf?: WebToolsOptions["pdf"];
38
- /** How long the screening of a search answer's snippets may take in all (see `WebToolsOptions`). */
39
- snippetScreenMs?: number;
40
- section?: {
41
- addendum?: string;
42
- };
43
- };
44
- export declare function createWebExtension(options: WebExtensionOptions): {
45
- extension: ReturnType<typeof defineExtension>;
46
- tools: readonly ToolRegistration[];
47
- };
@@ -1,41 +0,0 @@
1
- import { defineExtension, defineTool } from "@earendil-works/pi-durable";
2
- import { WEB_TOOLS } from "../core/contract.js";
3
- import { providerKeyValues } from "../core/session-control.js";
4
- import { toolUsage } from "../core/usage.js";
5
- import { createWebTools } from "../core/web.js";
6
- import { forModel } from "./result.js";
7
- export function createWebExtension(options) {
8
- const tools = createWebTools({
9
- name: options.providers.name ?? "provider",
10
- fetch: options.providers.fetch,
11
- search: options.providers.search,
12
- searchSources: options.searchSources,
13
- fetchSources: options.fetchSources,
14
- evidence: options.evidence,
15
- scope: options.scope ?? (() => ({ label: "web", proxies: true })),
16
- prices: options.prices,
17
- redact: { values: () => providerKeyValues(process.env, options.redact?.values) },
18
- backups: options.backups,
19
- classifyPage: options.decisions?.classifyPage,
20
- render: options.render,
21
- pdf: options.pdf,
22
- snippetScreenMs: options.snippetScreenMs,
23
- });
24
- const run = { web_fetch: tools.fetch, web_search: tools.search };
25
- const result = ({ text, isError, details, usd }) => ({ ...forModel([{ type: "text", text }]), isError, details: details, usage: toolUsage(usd).usage });
26
- const registrations = WEB_TOOLS
27
- .filter((tool) => (tool.listedWhen === "fetch" ? options.providers.fetch || options.render?.enabled || options.backups?.length || options.fetchSources?.length : options.providers.search || options.searchSources?.length))
28
- .map((tool) => defineTool({
29
- name: tool.name,
30
- description: tool.description,
31
- parameters: tool.parameters,
32
- replay: tool.replay,
33
- executionMode: tool.executionMode,
34
- execute: async (args, api, context) => result(await run[tool.name](api.conversationId, args, context.abortSignal)),
35
- }));
36
- // Matched by name: another extension's tool registered under the same name would carry the text too.
37
- const names = new Set(registrations.map((tool) => tool.name));
38
- const addendum = options.section?.addendum;
39
- const sections = addendum ? [{ key: "web", render: async (input) => (input.agent.tools.some((tool) => names.has(tool.name)) ? addendum : undefined) }] : [];
40
- return { extension: defineExtension({ name: "web", tools: registrations, sections }), tools: registrations };
41
- }