import { h as WorkspaceLike } from "../workspace-BZXdZcuw.js"; import { Tool, ToolSet } from "ai"; import { StateBackend } from "@cloudflare/shell"; import { CodemodeConnector, CodemodeRuntimeHandle, Executor } from "@cloudflare/codemode"; import { BrowserBinding, BrowserConnectorSessionOptions } from "agents/browser"; //#region src/tools/execute.d.ts /** * The minimum agent surface for the `createExecuteTool(this)` one-liner. * Any Think agent satisfies it. The agent must be a Durable Object — its * `ctx` hosts the codemode runtime facet, and its `env` provides the * LOADER/BROWSER bindings. (`ctx` and `env` are not declared here because * they are `protected` on the DO base class in some type configurations, * which would break structural assignability of `this`.) */ interface ExecuteToolAgent { workspace?: WorkspaceLike; /** Set by `createExecuteRuntime(agent)` so callables can reach the runtime. */ codemode?: CodemodeRuntimeHandle; } interface CreateExecuteToolOptions { /** * Durable Object state. The codemode runtime that backs the execute tool * lives in a facet of this DO — the tool must be created from inside a * Durable Object (e.g. a Think agent: pass `this.ctx`). */ ctx: DurableObjectState; /** * AI SDK tools exposed inside the sandbox as `tools.*`. Tools with * `needsApproval` get the runtime's durable pause/approve/resume flow: * calling one pauses the execution until `approveExecution` / * `rejectExecution`. A function-valued `needsApproval` can't be evaluated * against sandbox arguments ahead of time, so it conservatively always * requires approval. Tools without an `execute` function (client-side / * provider-executed) are skipped — the sandbox can't call them. */ tools?: ToolSet; /** * StateBackend exposed as `state.*` inside the sandbox — the full * filesystem API (readFile, writeFile, glob, searchFiles, replaceInFiles, * planEdits, …). Every method takes a single object argument. * * @example * ```ts * import { createWorkspaceStateBackend } from "@cloudflare/shell"; * state: createWorkspaceStateBackend(this.workspace) * ``` */ state?: StateBackend; /** * Browser Rendering binding. When provided, the sandbox gets the `cdp.*` * connector (cdp.send, cdp.attachToTarget, cdp.spec, …) — a live browser * over the Chrome DevTools Protocol. * * Requires `"browser": { "binding": "BROWSER" }` in wrangler.jsonc. */ browser?: BrowserBinding; /** * Browser session lifecycle (only with `browser`). Defaults to `dynamic`: * one-shot sessions the model can promote with `cdp.startSession()`. */ session?: BrowserConnectorSessionOptions; /** * Additional connectors for the sandbox beyond `tools`, `state`, and `cdp`. * Each adds its own named namespace. */ connectors?: CodemodeConnector[]; /** * The executor that runs the generated code. If not provided, a * `DynamicWorkerExecutor` is created from `loader`. */ executor?: Executor; /** * WorkerLoader binding for creating a `DynamicWorkerExecutor`. * * Requires `"worker_loaders": [{ "binding": "LOADER" }]` in wrangler.jsonc. */ loader?: WorkerLoader; /** * Timeout in milliseconds for code execution. Defaults to 30000 (30s). * Only used when `loader` is provided (ignored if `executor` is given). */ timeout?: number; /** * Controls outbound network access from sandboxed code. * - `null` (default): fetch() and connect() throw — sandbox is fully isolated. * - `undefined`: inherits parent Worker's network access. * - A `Fetcher`: all outbound requests route through this handler. * * Only used when `loader` is provided (ignored if `executor` is given). */ globalOutbound?: Fetcher | null; /** * Custom tool description. Replaces the generated default entirely — the * default explains the codemode workflow and lists each configured * namespace (`tools.*`, `state.*`, `cdp.*`) with a usage hint. */ description?: string; /** * Codemode runtime name — the durable identity of the tool's executions * and snippets. Defaults to `"execute"`. Adding or removing connectors * does NOT change the identity, so histories survive configuration * changes. */ name?: string; } /** * The execute tool's moving parts, for hosts that need more than the tool: * * - `runtime` — the codemode runtime handle (approve/reject paused runs, * `expirePaused`, audit via `executions()`, snippets). * - `connectors` — the connector set backing the runtime (e.g. the * `BrowserConnector` for host-side `sessionInfo()` / `closeSession()` / * `sweep()`). * - `tool` — what `createExecuteTool` returns. */ interface ExecuteRuntime { runtime: CodemodeRuntimeHandle; connectors: CodemodeConnector[]; tool: Tool; } /** * Build the codemode runtime behind the execute tool, returning the runtime * handle and connectors alongside the tool. Use this instead of * {@link createExecuteTool} when the host needs approvals, the audit trail, * snippets, or browser session management. * * When called with an agent, the runtime handle is also assigned to * `agent.codemode` so callables (and Think's built-in approval flow) can * reach it. */ declare function createExecuteRuntime( source: CreateExecuteToolOptions | ExecuteToolAgent, overrides?: Partial> ): ExecuteRuntime; /** * Truncate the `pending[].args` of a paused execution output for transcript / * model consumption. The full args stay on the runtime facet (used by the * actual resume); only the model-facing copy is bounded. Non-paused outputs * pass through unchanged. */ declare function truncatePausedExecutionOutput(output: unknown): unknown; /** * Create a code execution tool that lets the LLM write and run TypeScript * against your tools, the workspace filesystem, and (optionally) a live * browser — all inside a sandboxed Worker, recorded on a durable codemode * runtime (abort-and-replay, approvals, snippets). * * The model sees typed namespaces: `tools.*` for your AI SDK tools, * `state.*` for the filesystem (object args: `state.readFile({ path })`), * and `cdp.*` for the browser. * * Setup checklist: * * - `"worker_loaders": [{ "binding": "LOADER" }]` in wrangler.jsonc * - `"browser": { "binding": "BROWSER" }` in wrangler.jsonc (for `cdp.*`) * - export the runtime class from your worker entry: * `export { CodemodeRuntime } from "@cloudflare/codemode"` * (the `@cloudflare/codemode/vite` plugin does this automatically) * * @example One-liner — defaults from the agent * ```ts * getTools() { * return { * // state.* from this.workspace, cdp.* if env.BROWSER is bound, * // executor from env.LOADER * execute: createExecuteTool(this) * }; * } * ``` * * @example Agent defaults plus overrides (e.g. custom tools.*) * ```ts * execute: createExecuteTool(this, { tools: myDomainTools }) * ``` * * @example Explicit options * ```ts * execute: createExecuteTool({ * ctx: this.ctx, * tools: myDomainTools, // tools.* * state: createWorkspaceStateBackend(this.workspace), // state.* * browser: this.env.BROWSER, // cdp.* * loader: this.env.LOADER * }) * ``` * * Use {@link createExecuteRuntime} to also get the runtime handle * (approvals, audit, snippets) and the connector set. */ declare function createExecuteTool( source: CreateExecuteToolOptions | ExecuteToolAgent, overrides?: Partial> ): Tool; //#endregion export { CreateExecuteToolOptions, ExecuteRuntime, ExecuteToolAgent, createExecuteRuntime, createExecuteTool, truncatePausedExecutionOutput }; //# sourceMappingURL=execute.d.ts.map