agents 0.14.5 → 0.16.0
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/{agent-tool-types-V25Z_HcX.d.ts → agent-tool-types-NofdbL9X.d.ts} +182 -112
- package/dist/agent-tool-types.d.ts +1 -1
- package/dist/{agent-tools-C-9s151X.d.ts → agent-tools-DLquv-dp.d.ts} +2 -2
- package/dist/agent-tools.d.ts +1 -1
- package/dist/browser/ai.d.ts +126 -7
- package/dist/browser/ai.js +73 -29
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/index.d.ts +81 -69
- package/dist/browser/index.js +3 -2
- package/dist/browser/tanstack-ai.d.ts +13 -7
- package/dist/browser/tanstack-ai.js +18 -19
- package/dist/browser/tanstack-ai.js.map +1 -1
- package/dist/chat/index.d.ts +111 -5
- package/dist/chat/index.js +207 -35
- package/dist/chat/index.js.map +1 -1
- package/dist/chat-sdk/index.d.ts +1 -1
- package/dist/{classPrivateFieldGet2-Beqsfu2Z.js → classPrivateFieldGet2-CZ7QjTXN.js} +5 -5
- package/dist/{classPrivateMethodInitSpec-B5ko1s2R.js → classPrivateMethodInitSpec-D-0__zd9.js} +2 -2
- package/dist/client.d.ts +19 -2
- package/dist/client.js +31 -11
- package/dist/client.js.map +1 -1
- package/dist/{compaction-helpers-BEUILPss.d.ts → compaction-helpers-DVcu5lPN.d.ts} +91 -12
- package/dist/connector-D6yYzYHg.js +1080 -0
- package/dist/connector-D6yYzYHg.js.map +1 -0
- package/dist/connector-DXursxV5.d.ts +340 -0
- package/dist/experimental/memory/session/index.d.ts +75 -12
- package/dist/experimental/memory/session/index.js +226 -21
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/experimental/memory/utils/index.d.ts +2 -2
- package/dist/{index-CPe1OtI0.d.ts → index-B7IbEeze.d.ts} +32 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +116 -45
- package/dist/index.js.map +1 -1
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +262 -487
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +1 -1
- package/dist/react.d.ts +12 -1
- package/dist/react.js +101 -30
- package/dist/react.js.map +1 -1
- package/dist/{retries-CF_HKSlJ.d.ts → retries-CwlpAGet.d.ts} +35 -5
- package/dist/retries.d.ts +9 -5
- package/dist/retries.js +87 -1
- package/dist/retries.js.map +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/skills/index.js +2 -2
- package/dist/sub-routing.d.ts +1 -1
- package/dist/workflows.d.ts +1 -1
- package/package.json +10 -10
- package/dist/shared-4CAYLCTO.d.ts +0 -34
- package/dist/shared-wyII629d.js +0 -432
- package/dist/shared-wyII629d.js.map +0 -1
package/dist/browser/ai.js
CHANGED
|
@@ -1,21 +1,85 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { z } from "zod";
|
|
1
|
+
import { i as DurableBrowserSessionStore, t as BrowserConnector } from "../connector-D6yYzYHg.js";
|
|
2
|
+
import { DynamicWorkerExecutor, createCodemodeRuntime, truncateResult } from "@cloudflare/codemode";
|
|
4
3
|
//#region src/browser/ai.ts
|
|
4
|
+
let didWarnExperimental = false;
|
|
5
|
+
function connectorOptions(options) {
|
|
6
|
+
if (options.cdpUrl) return {
|
|
7
|
+
cdpUrl: options.cdpUrl,
|
|
8
|
+
cdpHeaders: options.cdpHeaders,
|
|
9
|
+
timeout: options.timeout
|
|
10
|
+
};
|
|
11
|
+
if (!options.browser) throw new Error("Either 'browser' (Fetcher binding) or 'cdpUrl' must be provided");
|
|
12
|
+
return {
|
|
13
|
+
browser: options.browser,
|
|
14
|
+
store: options.store ?? new DurableBrowserSessionStore(options.ctx.storage),
|
|
15
|
+
session: options.session,
|
|
16
|
+
timeout: options.timeout
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create the browser codemode runtime: the `browser_execute` tool plus the
|
|
21
|
+
* runtime handle and connector for host-side wiring (approvals, session info,
|
|
22
|
+
* sweeps).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* export class MyAgent extends Agent<Env> {
|
|
27
|
+
* get browser() {
|
|
28
|
+
* return createBrowserRuntime({
|
|
29
|
+
* ctx: this.ctx,
|
|
30
|
+
* browser: this.env.BROWSER,
|
|
31
|
+
* loader: this.env.LOADER,
|
|
32
|
+
* session: { mode: "dynamic" }
|
|
33
|
+
* });
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* @callable()
|
|
37
|
+
* async closeBrowserSession() {
|
|
38
|
+
* await this.browser.connector.closeSession();
|
|
39
|
+
* }
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function createBrowserRuntime(options) {
|
|
44
|
+
if (!didWarnExperimental) {
|
|
45
|
+
didWarnExperimental = true;
|
|
46
|
+
console.warn("[agents/browser] Browser tools are experimental and may change in a future release.");
|
|
47
|
+
}
|
|
48
|
+
const connector = new BrowserConnector(options.ctx, connectorOptions(options));
|
|
49
|
+
const runtime = createCodemodeRuntime({
|
|
50
|
+
ctx: options.ctx,
|
|
51
|
+
executor: new DynamicWorkerExecutor({
|
|
52
|
+
loader: options.loader,
|
|
53
|
+
timeout: options.timeout
|
|
54
|
+
}),
|
|
55
|
+
connectors: [connector],
|
|
56
|
+
name: options.name ?? "browser",
|
|
57
|
+
transformResult: truncateResult
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
runtime,
|
|
61
|
+
connector,
|
|
62
|
+
tools: { browser_execute: runtime.tool() }
|
|
63
|
+
};
|
|
64
|
+
}
|
|
5
65
|
/**
|
|
6
66
|
* Create AI SDK tools for browser automation via CDP code mode.
|
|
7
67
|
*
|
|
8
|
-
* Returns a `ToolSet` with `
|
|
9
|
-
*
|
|
68
|
+
* Returns a `ToolSet` with a single durable `browser_execute` tool backed by
|
|
69
|
+
* a codemode runtime: the model writes TypeScript against the `cdp` connector
|
|
70
|
+
* (`cdp.send`, `cdp.attachToTarget`, `cdp.spec`, …), executions are recorded
|
|
71
|
+
* for abort-and-replay, and browser sessions survive pauses.
|
|
10
72
|
*
|
|
11
73
|
* @example
|
|
12
74
|
* ```ts
|
|
13
75
|
* import { createBrowserTools } from "agents/browser/ai";
|
|
14
76
|
* import { generateText } from "ai";
|
|
15
77
|
*
|
|
78
|
+
* // inside a Durable Object / Agent:
|
|
16
79
|
* const browserTools = createBrowserTools({
|
|
17
|
-
*
|
|
18
|
-
*
|
|
80
|
+
* ctx: this.ctx,
|
|
81
|
+
* browser: this.env.BROWSER,
|
|
82
|
+
* loader: this.env.LOADER,
|
|
19
83
|
* });
|
|
20
84
|
*
|
|
21
85
|
* const result = await generateText({
|
|
@@ -26,29 +90,9 @@ import { z } from "zod";
|
|
|
26
90
|
* ```
|
|
27
91
|
*/
|
|
28
92
|
function createBrowserTools(options) {
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
browser_search: tool({
|
|
32
|
-
description: SEARCH_DESCRIPTION,
|
|
33
|
-
inputSchema: z.object({ code: z.string().describe("JavaScript async arrow function that queries the CDP spec") }),
|
|
34
|
-
execute: async ({ code }) => {
|
|
35
|
-
const result = await handlers.search(code);
|
|
36
|
-
if (result.isError) throw new Error(result.text);
|
|
37
|
-
return result.text;
|
|
38
|
-
}
|
|
39
|
-
}),
|
|
40
|
-
browser_execute: tool({
|
|
41
|
-
description: EXECUTE_DESCRIPTION,
|
|
42
|
-
inputSchema: z.object({ code: z.string().describe("JavaScript async arrow function that uses the cdp helper") }),
|
|
43
|
-
execute: async ({ code }) => {
|
|
44
|
-
const result = await handlers.execute(code);
|
|
45
|
-
if (result.isError) throw new Error(result.text);
|
|
46
|
-
return result.text;
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
};
|
|
93
|
+
return createBrowserRuntime(options).tools;
|
|
50
94
|
}
|
|
51
95
|
//#endregion
|
|
52
|
-
export { createBrowserTools };
|
|
96
|
+
export { createBrowserRuntime, createBrowserTools };
|
|
53
97
|
|
|
54
98
|
//# sourceMappingURL=ai.js.map
|
package/dist/browser/ai.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.js","names":[],"sources":["../../src/browser/ai.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"ai.js","names":[],"sources":["../../src/browser/ai.ts"],"sourcesContent":["import type { ToolSet } from \"ai\";\nimport {\n createCodemodeRuntime,\n DynamicWorkerExecutor,\n truncateResult,\n type CodemodeRuntimeHandle\n} from \"@cloudflare/codemode\";\nimport type { BrowserBinding } from \"./browser-run\";\nimport {\n BrowserConnector,\n type BrowserConnectorOptions,\n type BrowserConnectorSessionOptions\n} from \"./connector\";\nimport {\n DurableBrowserSessionStore,\n type BrowserSessionStore\n} from \"./session-manager\";\n\nexport interface CreateBrowserToolsOptions {\n /**\n * Durable Object state. The codemode runtime that backs the browser tool\n * lives in a facet of this DO, and browser session ids are stored in its\n * storage — so the tool must be created from inside a Durable Object\n * (e.g. an Agent).\n *\n * The worker must export the `CodemodeRuntime` class (the\n * `@cloudflare/codemode/vite` plugin does this automatically, or add\n * `export { CodemodeRuntime } from \"@cloudflare/codemode\"` to your entry).\n */\n ctx: DurableObjectState;\n\n /**\n * WorkerLoader binding for sandboxed code execution.\n *\n * Requires `\"worker_loaders\": [{ \"binding\": \"LOADER\" }]` in wrangler.jsonc.\n */\n loader: WorkerLoader;\n\n /**\n * Browser Rendering binding (Fetcher).\n *\n * This is the primary way to connect — works both locally in\n * `wrangler dev` and when deployed to Cloudflare Workers.\n *\n * Requires `\"browser\": { \"binding\": \"BROWSER\" }` in wrangler.jsonc.\n */\n browser?: BrowserBinding;\n\n /**\n * Optional CDP base URL override (e.g. `http://localhost:9222`).\n *\n * Use when connecting to a manually managed Chrome instance or\n * a remote CDP endpoint behind a tunnel.\n */\n cdpUrl?: string;\n\n /**\n * Headers to send with CDP URL discovery requests.\n * Useful when the CDP endpoint requires authentication\n * (e.g. Cloudflare Access headers).\n */\n cdpHeaders?: Record<string, string>;\n\n /**\n * Browser session lifecycle (binding-backed only). Defaults to one fresh\n * session per codemode execution (`one-shot`).\n */\n session?: BrowserConnectorSessionOptions;\n\n /**\n * Durable store for Browser Run session ids. Defaults to a\n * {@link DurableBrowserSessionStore} over `ctx.storage`.\n */\n store?: BrowserSessionStore;\n\n /**\n * Sandbox execution timeout in milliseconds. Defaults to 30000 (30s).\n * Also used as the per-CDP-command timeout.\n */\n timeout?: number;\n\n /**\n * Codemode runtime name — the durable identity of the tool's executions\n * and snippets. Defaults to `\"browser\"`.\n */\n name?: string;\n}\n\n/**\n * The browser tool's moving parts, for hosts that need more than the tools:\n *\n * - `runtime` — the codemode runtime handle (approve/reject paused runs,\n * `expirePaused`, audit via `executions()`, snippets).\n * - `connector` — host-side session helpers: `sessionInfo()`,\n * `closeSession()`, and `sweep()` for a recurring cleanup task.\n * - `tools` — what `createBrowserTools` returns.\n */\nexport interface BrowserRuntime {\n runtime: CodemodeRuntimeHandle;\n connector: BrowserConnector;\n tools: ToolSet;\n}\n\nlet didWarnExperimental = false;\n\nfunction connectorOptions(\n options: CreateBrowserToolsOptions\n): BrowserConnectorOptions {\n if (options.cdpUrl) {\n return {\n cdpUrl: options.cdpUrl,\n cdpHeaders: options.cdpHeaders,\n timeout: options.timeout\n };\n }\n if (!options.browser) {\n throw new Error(\n \"Either 'browser' (Fetcher binding) or 'cdpUrl' must be provided\"\n );\n }\n return {\n browser: options.browser,\n store: options.store ?? new DurableBrowserSessionStore(options.ctx.storage),\n session: options.session,\n timeout: options.timeout\n };\n}\n\n/**\n * Create the browser codemode runtime: the `browser_execute` tool plus the\n * runtime handle and connector for host-side wiring (approvals, session info,\n * sweeps).\n *\n * @example\n * ```ts\n * export class MyAgent extends Agent<Env> {\n * get browser() {\n * return createBrowserRuntime({\n * ctx: this.ctx,\n * browser: this.env.BROWSER,\n * loader: this.env.LOADER,\n * session: { mode: \"dynamic\" }\n * });\n * }\n *\n * @callable()\n * async closeBrowserSession() {\n * await this.browser.connector.closeSession();\n * }\n * }\n * ```\n */\nexport function createBrowserRuntime(\n options: CreateBrowserToolsOptions\n): BrowserRuntime {\n if (!didWarnExperimental) {\n didWarnExperimental = true;\n console.warn(\n \"[agents/browser] Browser tools are experimental and may change in a future release.\"\n );\n }\n\n const connector = new BrowserConnector(\n options.ctx,\n connectorOptions(options)\n );\n const runtime = createCodemodeRuntime({\n ctx: options.ctx,\n executor: new DynamicWorkerExecutor({\n loader: options.loader,\n timeout: options.timeout\n }),\n connectors: [connector],\n name: options.name ?? \"browser\",\n transformResult: truncateResult\n });\n\n return {\n runtime,\n connector,\n tools: { browser_execute: runtime.tool() }\n };\n}\n\n/**\n * Create AI SDK tools for browser automation via CDP code mode.\n *\n * Returns a `ToolSet` with a single durable `browser_execute` tool backed by\n * a codemode runtime: the model writes TypeScript against the `cdp` connector\n * (`cdp.send`, `cdp.attachToTarget`, `cdp.spec`, …), executions are recorded\n * for abort-and-replay, and browser sessions survive pauses.\n *\n * @example\n * ```ts\n * import { createBrowserTools } from \"agents/browser/ai\";\n * import { generateText } from \"ai\";\n *\n * // inside a Durable Object / Agent:\n * const browserTools = createBrowserTools({\n * ctx: this.ctx,\n * browser: this.env.BROWSER,\n * loader: this.env.LOADER,\n * });\n *\n * const result = await generateText({\n * model,\n * tools: { ...browserTools, ...otherTools },\n * messages,\n * });\n * ```\n */\nexport function createBrowserTools(\n options: CreateBrowserToolsOptions\n): ToolSet {\n return createBrowserRuntime(options).tools;\n}\n"],"mappings":";;;AAuGA,IAAI,sBAAsB;AAE1B,SAAS,iBACP,SACyB;CACzB,IAAI,QAAQ,QACV,OAAO;EACL,QAAQ,QAAQ;EAChB,YAAY,QAAQ;EACpB,SAAS,QAAQ;CACnB;CAEF,IAAI,CAAC,QAAQ,SACX,MAAM,IAAI,MACR,iEACF;CAEF,OAAO;EACL,SAAS,QAAQ;EACjB,OAAO,QAAQ,SAAS,IAAI,2BAA2B,QAAQ,IAAI,OAAO;EAC1E,SAAS,QAAQ;EACjB,SAAS,QAAQ;CACnB;AACF;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,qBACd,SACgB;CAChB,IAAI,CAAC,qBAAqB;EACxB,sBAAsB;EACtB,QAAQ,KACN,qFACF;CACF;CAEA,MAAM,YAAY,IAAI,iBACpB,QAAQ,KACR,iBAAiB,OAAO,CAC1B;CACA,MAAM,UAAU,sBAAsB;EACpC,KAAK,QAAQ;EACb,UAAU,IAAI,sBAAsB;GAClC,QAAQ,QAAQ;GAChB,SAAS,QAAQ;EACnB,CAAC;EACD,YAAY,CAAC,SAAS;EACtB,MAAM,QAAQ,QAAQ;EACtB,iBAAiB;CACnB,CAAC;CAED,OAAO;EACL;EACA;EACA,OAAO,EAAE,iBAAiB,QAAQ,KAAK,EAAE;CAC3C;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,mBACd,SACS;CACT,OAAO,qBAAqB,OAAO,CAAC,CAAC;AACvC"}
|
package/dist/browser/index.d.ts
CHANGED
|
@@ -1,82 +1,94 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
C as CdpSendOptions,
|
|
3
|
+
S as CdpAttachOptions,
|
|
4
|
+
T as connectUrl,
|
|
5
|
+
_ as connectBrowser,
|
|
6
|
+
a as BrowserConnectorSweepResult,
|
|
7
|
+
b as deleteBrowserSession,
|
|
8
|
+
c as BrowserSessionStore,
|
|
9
|
+
d as StoredBrowserSession,
|
|
10
|
+
f as BrowserBinding,
|
|
11
|
+
g as ConnectBrowserOptions,
|
|
12
|
+
h as BrowserTargetInfo,
|
|
13
|
+
i as BrowserConnectorSweepOptions,
|
|
14
|
+
l as DEFAULT_SWEEP_IDLE_MS,
|
|
15
|
+
m as BrowserSessionInfo,
|
|
16
|
+
n as BrowserConnectorOptions,
|
|
17
|
+
o as DEFAULT_EXEC_SWEEP_IDLE_MS,
|
|
18
|
+
p as BrowserRenderingError,
|
|
19
|
+
r as BrowserConnectorSessionOptions,
|
|
20
|
+
s as BrowserSessionLock,
|
|
21
|
+
t as BrowserConnector,
|
|
22
|
+
u as DurableBrowserSessionStore,
|
|
23
|
+
v as connectBrowserSession,
|
|
24
|
+
w as CdpSession,
|
|
25
|
+
x as listBrowserTargets,
|
|
26
|
+
y as createBrowserSession
|
|
27
|
+
} from "../connector-DXursxV5.js";
|
|
28
|
+
import { CodemodeRuntime } from "@cloudflare/codemode";
|
|
8
29
|
|
|
9
|
-
//#region src/browser/
|
|
10
|
-
interface
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
30
|
+
//#region src/browser/spec.d.ts
|
|
31
|
+
interface SearchableCdpSpec {
|
|
32
|
+
domains: Array<{
|
|
33
|
+
name: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
commands: Array<{
|
|
36
|
+
name: string;
|
|
37
|
+
method: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
}>;
|
|
40
|
+
events: Array<{
|
|
41
|
+
name: string;
|
|
42
|
+
event: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
}>;
|
|
45
|
+
types: Array<{
|
|
46
|
+
id: string;
|
|
47
|
+
name: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
}>;
|
|
50
|
+
}>;
|
|
14
51
|
}
|
|
15
|
-
interface
|
|
16
|
-
|
|
17
|
-
|
|
52
|
+
interface CdpSpecSource {
|
|
53
|
+
/** Browser Rendering binding (Fetcher) — used in production */
|
|
54
|
+
browser?: BrowserBinding;
|
|
55
|
+
/** CDP base URL override (e.g. http://localhost:9222) */
|
|
56
|
+
cdpUrl?: string;
|
|
57
|
+
/** Headers to send with CDP URL discovery requests */
|
|
58
|
+
cdpHeaders?: Record<string, string>;
|
|
18
59
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* A CDP session over an open WebSocket. Manages command correlation,
|
|
24
|
-
* timeouts, target sessions, and a debug event ring buffer.
|
|
25
|
-
*
|
|
26
|
-
* Used host-side (not in the sandbox) — the sandbox calls into this
|
|
27
|
-
* via DynamicWorkerExecutor's ToolDispatcher RPC.
|
|
28
|
-
*/
|
|
29
|
-
declare class CdpSession {
|
|
30
|
-
#private;
|
|
31
|
-
constructor(
|
|
32
|
-
socket: WebSocket,
|
|
33
|
-
defaultTimeoutMs?: number,
|
|
34
|
-
dispose?: () => void
|
|
35
|
-
);
|
|
36
|
-
send(
|
|
37
|
-
method: string,
|
|
38
|
-
params?: unknown,
|
|
39
|
-
options?: CdpSendOptions
|
|
40
|
-
): Promise<unknown>;
|
|
41
|
-
attachToTarget(targetId: string, options?: CdpAttachOptions): Promise<string>;
|
|
42
|
-
getDebugLog(limit?: number): DebugEntry[];
|
|
43
|
-
clearDebugLog(): void;
|
|
44
|
-
close(): void;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Connect to a browser via the Browser Rendering binding (Fetcher).
|
|
48
|
-
* Establishes a CDP WebSocket through the binding's fetch interface.
|
|
49
|
-
*/
|
|
50
|
-
declare function connectBrowser(
|
|
51
|
-
browser: Fetcher,
|
|
52
|
-
timeoutMs?: number
|
|
53
|
-
): Promise<CdpSession>;
|
|
54
|
-
/**
|
|
55
|
-
* Connect to a browser via a CDP base URL (e.g. http://localhost:9222).
|
|
56
|
-
* Discovers the WebSocket debugger URL via /json/version,
|
|
57
|
-
* rewrites localhost URLs to the base URL host, and opens the WebSocket.
|
|
58
|
-
*
|
|
59
|
-
* Useful for local development with `chrome --remote-debugging-port=9222`
|
|
60
|
-
* or when connecting through a tunnel.
|
|
61
|
-
*/
|
|
62
|
-
declare function connectUrl(
|
|
63
|
-
baseUrl: string,
|
|
64
|
-
options?: {
|
|
65
|
-
timeoutMs?: number;
|
|
66
|
-
headers?: Record<string, string>;
|
|
67
|
-
}
|
|
68
|
-
): Promise<CdpSession>;
|
|
60
|
+
/** Load the (cached) searchable CDP spec for a browser source. */
|
|
61
|
+
declare function loadCdpSpec(source: CdpSpecSource): Promise<SearchableCdpSpec>;
|
|
69
62
|
//#endregion
|
|
70
63
|
export {
|
|
71
|
-
type
|
|
64
|
+
type BrowserBinding,
|
|
65
|
+
BrowserConnector,
|
|
66
|
+
type BrowserConnectorOptions,
|
|
67
|
+
type BrowserConnectorSessionOptions,
|
|
68
|
+
type BrowserConnectorSweepOptions,
|
|
69
|
+
type BrowserConnectorSweepResult,
|
|
70
|
+
BrowserRenderingError,
|
|
71
|
+
type BrowserSessionInfo,
|
|
72
|
+
type BrowserSessionLock,
|
|
73
|
+
type BrowserSessionStore,
|
|
74
|
+
type BrowserTargetInfo,
|
|
72
75
|
type CdpAttachOptions,
|
|
73
76
|
type CdpSendOptions,
|
|
74
77
|
CdpSession,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type
|
|
78
|
+
type CdpSpecSource,
|
|
79
|
+
CodemodeRuntime,
|
|
80
|
+
type ConnectBrowserOptions,
|
|
81
|
+
DEFAULT_EXEC_SWEEP_IDLE_MS,
|
|
82
|
+
DEFAULT_SWEEP_IDLE_MS,
|
|
83
|
+
DurableBrowserSessionStore,
|
|
84
|
+
type SearchableCdpSpec,
|
|
85
|
+
type StoredBrowserSession,
|
|
78
86
|
connectBrowser,
|
|
87
|
+
connectBrowserSession,
|
|
79
88
|
connectUrl,
|
|
80
|
-
|
|
89
|
+
createBrowserSession,
|
|
90
|
+
deleteBrowserSession,
|
|
91
|
+
listBrowserTargets,
|
|
92
|
+
loadCdpSpec
|
|
81
93
|
};
|
|
82
94
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/browser/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
|
|
1
|
+
import { a as loadCdpSpec, c as connectBrowserSession, d as listBrowserTargets, f as CdpSession, i as DurableBrowserSessionStore, l as createBrowserSession, n as DEFAULT_EXEC_SWEEP_IDLE_MS, o as BrowserRenderingError, p as connectUrl, r as DEFAULT_SWEEP_IDLE_MS, s as connectBrowser, t as BrowserConnector, u as deleteBrowserSession } from "../connector-D6yYzYHg.js";
|
|
2
|
+
import { CodemodeRuntime } from "@cloudflare/codemode";
|
|
3
|
+
export { BrowserConnector, BrowserRenderingError, CdpSession, CodemodeRuntime, DEFAULT_EXEC_SWEEP_IDLE_MS, DEFAULT_SWEEP_IDLE_MS, DurableBrowserSessionStore, connectBrowser, connectBrowserSession, connectUrl, createBrowserSession, deleteBrowserSession, listBrowserTargets, loadCdpSpec };
|
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CreateBrowserToolsOptions } from "./ai.js";
|
|
2
2
|
import { ServerTool } from "@tanstack/ai";
|
|
3
3
|
|
|
4
4
|
//#region src/browser/tanstack-ai.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Create TanStack AI tools for browser automation via CDP code mode.
|
|
7
7
|
*
|
|
8
|
-
* Returns an array
|
|
9
|
-
*
|
|
8
|
+
* Returns an array with a single durable `browser_execute` `ServerTool`
|
|
9
|
+
* backed by the same codemode runtime as `agents/browser/ai` — the model
|
|
10
|
+
* writes TypeScript against the `cdp` connector and browser sessions
|
|
11
|
+
* survive pauses.
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
13
15
|
* import { createBrowserTools } from "agents/browser/tanstack-ai";
|
|
14
16
|
* import { chat } from "@tanstack/ai";
|
|
15
17
|
*
|
|
18
|
+
* // inside a Durable Object / Agent:
|
|
16
19
|
* const browserTools = createBrowserTools({
|
|
17
|
-
*
|
|
18
|
-
*
|
|
20
|
+
* ctx: this.ctx,
|
|
21
|
+
* browser: this.env.BROWSER,
|
|
22
|
+
* loader: this.env.LOADER,
|
|
19
23
|
* });
|
|
20
24
|
*
|
|
21
25
|
* const stream = chat({
|
|
@@ -25,7 +29,9 @@ import { ServerTool } from "@tanstack/ai";
|
|
|
25
29
|
* });
|
|
26
30
|
* ```
|
|
27
31
|
*/
|
|
28
|
-
declare function createBrowserTools(
|
|
32
|
+
declare function createBrowserTools(
|
|
33
|
+
options: CreateBrowserToolsOptions
|
|
34
|
+
): ServerTool[];
|
|
29
35
|
//#endregion
|
|
30
|
-
export { type
|
|
36
|
+
export { type CreateBrowserToolsOptions, createBrowserTools };
|
|
31
37
|
//# sourceMappingURL=tanstack-ai.d.ts.map
|
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBrowserRuntime } from "./ai.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { toolDefinition } from "@tanstack/ai";
|
|
4
4
|
//#region src/browser/tanstack-ai.ts
|
|
5
5
|
/**
|
|
6
6
|
* Create TanStack AI tools for browser automation via CDP code mode.
|
|
7
7
|
*
|
|
8
|
-
* Returns an array
|
|
9
|
-
*
|
|
8
|
+
* Returns an array with a single durable `browser_execute` `ServerTool`
|
|
9
|
+
* backed by the same codemode runtime as `agents/browser/ai` — the model
|
|
10
|
+
* writes TypeScript against the `cdp` connector and browser sessions
|
|
11
|
+
* survive pauses.
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
13
15
|
* import { createBrowserTools } from "agents/browser/tanstack-ai";
|
|
14
16
|
* import { chat } from "@tanstack/ai";
|
|
15
17
|
*
|
|
18
|
+
* // inside a Durable Object / Agent:
|
|
16
19
|
* const browserTools = createBrowserTools({
|
|
17
|
-
*
|
|
18
|
-
*
|
|
20
|
+
* ctx: this.ctx,
|
|
21
|
+
* browser: this.env.BROWSER,
|
|
22
|
+
* loader: this.env.LOADER,
|
|
19
23
|
* });
|
|
20
24
|
*
|
|
21
25
|
* const stream = chat({
|
|
@@ -26,23 +30,18 @@ import { toolDefinition } from "@tanstack/ai";
|
|
|
26
30
|
* ```
|
|
27
31
|
*/
|
|
28
32
|
function createBrowserTools(options) {
|
|
29
|
-
const
|
|
33
|
+
const { tools } = createBrowserRuntime(options);
|
|
34
|
+
const executeTool = tools.browser_execute;
|
|
30
35
|
return [toolDefinition({
|
|
31
|
-
name: "browser_search",
|
|
32
|
-
description: SEARCH_DESCRIPTION,
|
|
33
|
-
inputSchema: z.object({ code: z.string().meta({ description: "JavaScript async arrow function that queries the CDP spec" }) })
|
|
34
|
-
}).server(async ({ code }) => {
|
|
35
|
-
const result = await handlers.search(code);
|
|
36
|
-
if (result.isError) throw new Error(result.text);
|
|
37
|
-
return { text: result.text };
|
|
38
|
-
}), toolDefinition({
|
|
39
36
|
name: "browser_execute",
|
|
40
|
-
description:
|
|
41
|
-
inputSchema: z.object({ code: z.string().meta({ description: "
|
|
37
|
+
description: executeTool.description ?? "",
|
|
38
|
+
inputSchema: z.object({ code: z.string().meta({ description: "TypeScript async arrow function that uses the cdp connector" }) })
|
|
42
39
|
}).server(async ({ code }) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
if (!executeTool.execute) throw new Error("browser_execute tool is not executable");
|
|
41
|
+
return await executeTool.execute({ code }, {
|
|
42
|
+
toolCallId: crypto.randomUUID(),
|
|
43
|
+
messages: []
|
|
44
|
+
});
|
|
46
45
|
})];
|
|
47
46
|
}
|
|
48
47
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tanstack-ai.js","names":[],"sources":["../../src/browser/tanstack-ai.ts"],"sourcesContent":["import { toolDefinition } from \"@tanstack/ai\";\nimport type { ServerTool } from \"@tanstack/ai\";\nimport { z } from \"zod\";\nimport {
|
|
1
|
+
{"version":3,"file":"tanstack-ai.js","names":[],"sources":["../../src/browser/tanstack-ai.ts"],"sourcesContent":["import { toolDefinition } from \"@tanstack/ai\";\nimport type { ServerTool } from \"@tanstack/ai\";\nimport type { ProxyToolOutput } from \"@cloudflare/codemode\";\nimport { z } from \"zod\";\nimport { createBrowserRuntime, type CreateBrowserToolsOptions } from \"./ai\";\n\nexport type { CreateBrowserToolsOptions } from \"./ai\";\n\n/**\n * Create TanStack AI tools for browser automation via CDP code mode.\n *\n * Returns an array with a single durable `browser_execute` `ServerTool`\n * backed by the same codemode runtime as `agents/browser/ai` — the model\n * writes TypeScript against the `cdp` connector and browser sessions\n * survive pauses.\n *\n * @example\n * ```ts\n * import { createBrowserTools } from \"agents/browser/tanstack-ai\";\n * import { chat } from \"@tanstack/ai\";\n *\n * // inside a Durable Object / Agent:\n * const browserTools = createBrowserTools({\n * ctx: this.ctx,\n * browser: this.env.BROWSER,\n * loader: this.env.LOADER,\n * });\n *\n * const stream = chat({\n * adapter: openaiText(\"gpt-4o\"),\n * tools: [...browserTools, ...otherTools],\n * messages,\n * });\n * ```\n */\nexport function createBrowserTools(\n options: CreateBrowserToolsOptions\n): ServerTool[] {\n const { tools } = createBrowserRuntime(options);\n const executeTool = tools.browser_execute;\n\n const execute = toolDefinition({\n name: \"browser_execute\" as const,\n description: executeTool.description ?? \"\",\n inputSchema: z.object({\n code: z.string().meta({\n description:\n \"TypeScript async arrow function that uses the cdp connector\"\n })\n })\n }).server(async ({ code }) => {\n if (!executeTool.execute) {\n throw new Error(\"browser_execute tool is not executable\");\n }\n const result = (await executeTool.execute(\n { code },\n { toolCallId: crypto.randomUUID(), messages: [] }\n )) as ProxyToolOutput;\n return result;\n });\n\n return [execute];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,mBACd,SACc;CACd,MAAM,EAAE,UAAU,qBAAqB,OAAO;CAC9C,MAAM,cAAc,MAAM;CAsB1B,OAAO,CApBS,eAAe;EAC7B,MAAM;EACN,aAAa,YAAY,eAAe;EACxC,aAAa,EAAE,OAAO,EACpB,MAAM,EAAE,OAAO,CAAC,CAAC,KAAK,EACpB,aACE,8DACJ,CAAC,EACH,CAAC;CACH,CAAC,CAAC,CAAC,OAAO,OAAO,EAAE,WAAW;EAC5B,IAAI,CAAC,YAAY,SACf,MAAM,IAAI,MAAM,wCAAwC;EAM1D,OAAO,MAJe,YAAY,QAChC,EAAE,KAAK,GACP;GAAE,YAAY,OAAO,WAAW;GAAG,UAAU,CAAC;EAAE,CAClD;CAEF,CAEc,CAAC;AACjB"}
|
package/dist/chat/index.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ import {
|
|
|
3
3
|
d as AgentToolRunState,
|
|
4
4
|
i as AgentToolEventMessage,
|
|
5
5
|
r as AgentToolEvent
|
|
6
|
-
} from "../agent-tool-types-
|
|
6
|
+
} from "../agent-tool-types-NofdbL9X.js";
|
|
7
7
|
import {
|
|
8
8
|
n as createAgentToolEventState,
|
|
9
9
|
t as applyAgentToolEvent
|
|
10
|
-
} from "../agent-tools-
|
|
10
|
+
} from "../agent-tools-DLquv-dp.js";
|
|
11
11
|
import { JSONSchema7, Tool, ToolSet, UIMessage } from "ai";
|
|
12
12
|
import { Connection } from "agents";
|
|
13
13
|
|
|
@@ -277,17 +277,38 @@ type ClientToolSchema = {
|
|
|
277
277
|
description?: Tool["description"] /** JSON Schema defining the tool's input parameters */;
|
|
278
278
|
parameters?: JSONSchema7;
|
|
279
279
|
};
|
|
280
|
+
/**
|
|
281
|
+
* Executes a client-defined tool and returns its output.
|
|
282
|
+
*
|
|
283
|
+
* Used for the RPC path (e.g. a parent agent delegating to a Think sub-agent)
|
|
284
|
+
* where the caller can run the client tools itself, rather than the
|
|
285
|
+
* browser/WebSocket path where results are sent back asynchronously.
|
|
286
|
+
*/
|
|
287
|
+
type ClientToolExecutor = (call: {
|
|
288
|
+
/** The name of the client tool the model called. */ toolName: string /** The model-generated input for the tool call. */;
|
|
289
|
+
input: unknown /** The AI SDK tool-call id for the invocation. */;
|
|
290
|
+
toolCallId: string;
|
|
291
|
+
}) => unknown | Promise<unknown>;
|
|
280
292
|
/**
|
|
281
293
|
* Converts client tool schemas to AI SDK tool format.
|
|
282
294
|
*
|
|
283
|
-
*
|
|
284
|
-
* the tool call is sent back to the client for execution.
|
|
295
|
+
* By default these tools have no `execute` function — when the AI model calls
|
|
296
|
+
* them, the tool call is sent back to the client for execution.
|
|
297
|
+
*
|
|
298
|
+
* When `options.execute` is provided, each tool is built WITH an `execute` that
|
|
299
|
+
* delegates to it. This is used by the RPC path (e.g. a parent agent driving a
|
|
300
|
+
* Think sub-agent) so the model's client-tool call is resolved inline within
|
|
301
|
+
* the same turn.
|
|
285
302
|
*
|
|
286
303
|
* @param clientTools - Array of tool schemas from the client
|
|
304
|
+
* @param options - Optional `execute` delegate to run the tools inline
|
|
287
305
|
* @returns Record of AI SDK tools that can be spread into your tools object
|
|
288
306
|
*/
|
|
289
307
|
declare function createToolsFromClientSchemas(
|
|
290
|
-
clientTools?: ClientToolSchema[]
|
|
308
|
+
clientTools?: ClientToolSchema[],
|
|
309
|
+
options?: {
|
|
310
|
+
execute?: ClientToolExecutor;
|
|
311
|
+
}
|
|
291
312
|
): ToolSet;
|
|
292
313
|
//#endregion
|
|
293
314
|
//#region src/chat/lifecycle.d.ts
|
|
@@ -672,6 +693,12 @@ declare class ResumableStream {
|
|
|
672
693
|
* DO was evicted.
|
|
673
694
|
*/
|
|
674
695
|
private _isLive;
|
|
696
|
+
/**
|
|
697
|
+
* Whether the active stream is a continuation. Mirrors the durable
|
|
698
|
+
* `is_continuation` column so replay frames can carry the flag without a
|
|
699
|
+
* per-replay query; restored from SQLite after hibernation in restore().
|
|
700
|
+
*/
|
|
701
|
+
private _activeIsContinuation;
|
|
675
702
|
private _chunkBuffer;
|
|
676
703
|
private _chunkBufferBytes;
|
|
677
704
|
private _isFlushingChunks;
|
|
@@ -702,6 +729,7 @@ declare class ResumableStream {
|
|
|
702
729
|
requestId: string,
|
|
703
730
|
options?: {
|
|
704
731
|
messageId?: string;
|
|
732
|
+
continuation?: boolean;
|
|
705
733
|
}
|
|
706
734
|
): string;
|
|
707
735
|
/**
|
|
@@ -768,6 +796,33 @@ declare class ResumableStream {
|
|
|
768
796
|
connection: Connection,
|
|
769
797
|
requestId: string
|
|
770
798
|
): boolean;
|
|
799
|
+
/**
|
|
800
|
+
* Replay the stored chunks of an errored stream for a request, WITHOUT a
|
|
801
|
+
* terminal frame — the caller follows up with the `done: true, error: true`
|
|
802
|
+
* frame carrying the durable terminal record's error text, mirroring what a
|
|
803
|
+
* live client observed (content chunks, then the error). Without this, a
|
|
804
|
+
* client that missed broadcast frames while disconnected has no other
|
|
805
|
+
* channel to the pre-error partial content: the server does not push
|
|
806
|
+
* messages on connect, and {@link replayCompletedChunksByRequestId} only
|
|
807
|
+
* serves `completed` streams (#1575).
|
|
808
|
+
*
|
|
809
|
+
* Returns true when the caller should proceed to send its terminal frame:
|
|
810
|
+
* either no errored stream existed (nothing to replay) or its chunks were
|
|
811
|
+
* replayed successfully. Returns false only when a send failed mid-replay,
|
|
812
|
+
* signalling the caller to skip the terminal frame — the connection is gone
|
|
813
|
+
* and the next reconnect retries the whole sequence.
|
|
814
|
+
*/
|
|
815
|
+
replayErroredChunksByRequestId(
|
|
816
|
+
connection: Connection,
|
|
817
|
+
requestId: string
|
|
818
|
+
): boolean;
|
|
819
|
+
/** Latest stream row for a request with the given terminal status. */
|
|
820
|
+
private _latestStreamForRequest;
|
|
821
|
+
/**
|
|
822
|
+
* Send a finished stream's stored chunks to a connection as replay frames.
|
|
823
|
+
* Returns false if the connection closed mid-replay.
|
|
824
|
+
*/
|
|
825
|
+
private _replayStoredChunks;
|
|
771
826
|
/**
|
|
772
827
|
* Restore active stream state if the agent was restarted during streaming.
|
|
773
828
|
* All streams are restored regardless of age — stale cleanup happens
|
|
@@ -782,7 +837,26 @@ declare class ResumableStream {
|
|
|
782
837
|
* Drop all stream tables (called on destroy).
|
|
783
838
|
*/
|
|
784
839
|
destroy(): void;
|
|
840
|
+
/**
|
|
841
|
+
* Force a sweep of aged stream buffers now, bypassing the lazy interval
|
|
842
|
+
* gate used by {@link _maybeCleanupOldStreams}. Intended to be driven by an
|
|
843
|
+
* alarm so idle/hibernated chat DOs still reclaim buffers even when no
|
|
844
|
+
* further stream ever completes to trigger the lazy path.
|
|
845
|
+
*/
|
|
846
|
+
cleanup(now?: number): void;
|
|
847
|
+
/**
|
|
848
|
+
* True if any stream rows remain at all. Used by alarm-driven cleanup to
|
|
849
|
+
* decide whether to re-arm: once no rows remain there is nothing left to
|
|
850
|
+
* sweep, so the DO can stop waking itself.
|
|
851
|
+
*/
|
|
852
|
+
hasReclaimableStreams(): boolean;
|
|
785
853
|
private _maybeCleanupOldStreams;
|
|
854
|
+
/** Delete completed/errored buffers past the completion grace window, plus
|
|
855
|
+
* abandoned "streaming" rows past the stale-in-flight window. The two use
|
|
856
|
+
* different retentions: a completed buffer is redundant with the persisted
|
|
857
|
+
* message and needs only a brief replay grace, whereas an in-flight buffer
|
|
858
|
+
* must outlive resume/recovery before it is presumed dead. */
|
|
859
|
+
private _sweepOldStreams;
|
|
786
860
|
/**
|
|
787
861
|
* Return the stored chunks for a stream as individual chunk bodies in order,
|
|
788
862
|
* unpacking packed segment rows. The returned `chunk_index` is a running
|
|
@@ -807,6 +881,13 @@ declare class ResumableStream {
|
|
|
807
881
|
}>;
|
|
808
882
|
/** @internal For testing only */
|
|
809
883
|
insertStaleStream(streamId: string, requestId: string, ageMs: number): void;
|
|
884
|
+
/**
|
|
885
|
+
* Append a chunk to a stream dated `ageMs` in the past. Used to exercise the
|
|
886
|
+
* last-activity sweep threshold: a long-running streaming row with a *recent*
|
|
887
|
+
* chunk must survive even when its start time is older than the cutoff.
|
|
888
|
+
* @internal For testing only
|
|
889
|
+
*/
|
|
890
|
+
insertChunkAt(streamId: string, body: string, ageMs: number): void;
|
|
810
891
|
}
|
|
811
892
|
//#endregion
|
|
812
893
|
//#region src/chat/sql-batch.d.ts
|
|
@@ -1083,6 +1164,29 @@ declare function crossMessageToolResultUpdate(
|
|
|
1083
1164
|
errorText?: string,
|
|
1084
1165
|
preliminary?: boolean
|
|
1085
1166
|
): ToolPartUpdate;
|
|
1167
|
+
/**
|
|
1168
|
+
* Build an update descriptor that replaces the output of a *paused durable
|
|
1169
|
+
* execution* tool part (e.g. a codemode runtime tool that paused for
|
|
1170
|
+
* approval).
|
|
1171
|
+
*
|
|
1172
|
+
* A paused execution completes its tool call normally — the part is already
|
|
1173
|
+
* `output-available` with an output of `{ status: "paused", executionId }`.
|
|
1174
|
+
* When the host later approves/rejects the execution, the new outcome
|
|
1175
|
+
* (completed / rejected / paused-again) must replace that output in place.
|
|
1176
|
+
*
|
|
1177
|
+
* Matching is deliberately narrow and idempotent:
|
|
1178
|
+
*
|
|
1179
|
+
* - only `output-available` parts are considered;
|
|
1180
|
+
* - the existing output must be a paused-execution object carrying the same
|
|
1181
|
+
* `executionId` — anything else (already replaced, different execution)
|
|
1182
|
+
* returns the *same part reference*, which callers treat as a no-op signal
|
|
1183
|
+
* (skip persist + broadcast), mirroring {@link crossMessageToolResultUpdate}.
|
|
1184
|
+
*/
|
|
1185
|
+
declare function pausedExecutionUpdate(
|
|
1186
|
+
toolCallId: string,
|
|
1187
|
+
executionId: string,
|
|
1188
|
+
output: unknown
|
|
1189
|
+
): ToolPartUpdate;
|
|
1086
1190
|
/**
|
|
1087
1191
|
* Build an update descriptor for applying a tool approval.
|
|
1088
1192
|
*
|
|
@@ -1278,6 +1382,7 @@ export {
|
|
|
1278
1382
|
type ChatResponseResult,
|
|
1279
1383
|
type ChunkAction,
|
|
1280
1384
|
type ChunkResult,
|
|
1385
|
+
type ClientToolExecutor,
|
|
1281
1386
|
type ClientToolSchema,
|
|
1282
1387
|
type ContinuationConnection,
|
|
1283
1388
|
type ContinuationDeferred,
|
|
@@ -1318,6 +1423,7 @@ export {
|
|
|
1318
1423
|
isReplayChunk,
|
|
1319
1424
|
normalizeToolInput,
|
|
1320
1425
|
parseProtocolMessage,
|
|
1426
|
+
pausedExecutionUpdate,
|
|
1321
1427
|
reconcileMessages,
|
|
1322
1428
|
resolveToolMergeId,
|
|
1323
1429
|
sanitizeMessage,
|