@cloudflare/think 0.10.0 → 0.11.1
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/README.md +73 -17
- package/dist/{chat-sdk-Bb93yaZh.js → chat-sdk-D6cUghsQ.js} +33 -8
- package/dist/chat-sdk-D6cUghsQ.js.map +1 -0
- package/dist/cli/index.js +224 -55
- package/dist/cli/index.js.map +1 -1
- package/dist/{codegen-CuPdJRpV.js → codegen-DlA2QQuU.js} +6 -38
- package/dist/codegen-DlA2QQuU.js.map +1 -0
- package/dist/{config-D512sQYg.d.ts → config-CD7hqncK.d.ts} +5 -5
- package/dist/extensions/index.js +1 -1
- package/dist/fetch-By3eIQ-a.d.ts +147 -0
- package/dist/framework/index.d.ts +17 -62
- package/dist/framework/index.js +3 -57
- package/dist/{think-i1Pkgb1R.d.ts → index-CR4-ffm_.d.ts} +1734 -736
- package/dist/{manager-BPndMP_6.js → manager-CSk5htym.js} +7 -7
- package/dist/{manager-BPndMP_6.js.map → manager-CSk5htym.js.map} +1 -1
- package/dist/{manifest-CpB-xngB.d.ts → manifest-DK5HuGga.d.ts} +16 -18
- package/dist/messengers/index.d.ts +59 -55
- package/dist/messengers/index.js +2 -2
- package/dist/messengers/telegram.d.ts +4 -4
- package/dist/messengers/telegram.js +1 -1
- package/dist/react.d.ts +79 -0
- package/dist/react.js +28 -0
- package/dist/react.js.map +1 -0
- package/dist/server-entry.d.ts +1 -1
- package/dist/server-entry.js +1 -1
- package/dist/server-entry.js.map +1 -1
- package/dist/studio/assets/{angular-html-z-TUogET.js → angular-html-D27LthTc.js} +1 -1
- package/dist/studio/assets/{angular-ts-DMGuEVeg.js → angular-ts-DtNL2gQ7.js} +1 -1
- package/dist/studio/assets/{apl-KvrEROqd.js → apl-BgEcDqM0.js} +1 -1
- package/dist/studio/assets/{astro-Bm_woEti.js → astro-CstbRF9e.js} +1 -1
- package/dist/studio/assets/{blade-BdCF3r89.js → blade-CLG-7epG.js} +1 -1
- package/dist/studio/assets/{c-CDqDtkvZ.js → c-CssQqwxO.js} +1 -1
- package/dist/studio/assets/{cobol-CeFM1vZz.js → cobol-CjmzHCjg.js} +1 -1
- package/dist/studio/assets/{coffee-2ed6LBym.js → coffee-B_rqYPkp.js} +1 -1
- package/dist/studio/assets/{cpp-Dj3nX0J4.js → cpp-5YyZJlb7.js} +1 -1
- package/dist/studio/assets/{crystal-BsIYZMQ3.js → crystal-D7uKze0i.js} +1 -1
- package/dist/studio/assets/{css-B48fnuoH.js → css-k9oPgg7G.js} +1 -1
- package/dist/studio/assets/{edge-C7qt1aza.js → edge-CGgsSt0x.js} +1 -1
- package/dist/studio/assets/{elixir-CiTmwoQ3.js → elixir-BX_d5Evo.js} +1 -1
- package/dist/studio/assets/{elm-exDZ-PRY.js → elm-DENxDm5e.js} +1 -1
- package/dist/studio/assets/{erb-3fmfgt-Q.js → erb-DvYOup03.js} +1 -1
- package/dist/studio/assets/{git-rebase-75I-C7zW.js → git-rebase-BsAdTS5A.js} +1 -1
- package/dist/studio/assets/{glimmer-js-JjRzYRv8.js → glimmer-js-e0h98Z67.js} +1 -1
- package/dist/studio/assets/{glimmer-ts-DcC_g6bi.js → glimmer-ts-C9s5Kv8m.js} +1 -1
- package/dist/studio/assets/{glsl-CHo__hpu.js → glsl-CCiyeTRs.js} +1 -1
- package/dist/studio/assets/{graphql-J3cHpSdo.js → graphql-BRtHwsqk.js} +1 -1
- package/dist/studio/assets/{hack-or1FCnPg.js → hack-Dnt0QXHh.js} +1 -1
- package/dist/studio/assets/{haml-uv3vtlo6.js → haml-EXGmzuB1.js} +1 -1
- package/dist/studio/assets/{handlebars--xrOP0sp.js → handlebars-CzyBIDJE.js} +1 -1
- package/dist/studio/assets/{highlighted-body-OFNGDK62-D5UBAw6b.js → highlighted-body-OFNGDK62-CQUFKLNu.js} +1 -1
- package/dist/studio/assets/{html-CWnTqWKh.js → html-BAT8b_VM.js} +1 -1
- package/dist/studio/assets/{html-derivative-DWyLco-z.js → html-derivative-7tkS-r6F.js} +1 -1
- package/dist/studio/assets/{http-BGB8mLqf.js → http-Cgka02Qq.js} +1 -1
- package/dist/studio/assets/{hurl-BWdvIVUG.js → hurl-Cbkh8WEw.js} +1 -1
- package/dist/studio/assets/index-D6n8vpkp.js +390 -0
- package/dist/studio/assets/index-DkGWw6ES.css +2 -0
- package/dist/studio/assets/{java-BqyrPpKF.js → java-DrRKiUno.js} +1 -1
- package/dist/studio/assets/{javascript-D8Aej0xg.js → javascript-9LAnHOxD.js} +1 -1
- package/dist/studio/assets/{jinja-SIORrXov.js → jinja-CqRt4prs.js} +1 -1
- package/dist/studio/assets/{jison-B2T4v51n.js → jison-BX-IOQpJ.js} +1 -1
- package/dist/studio/assets/{json-BFdVRj9r.js → json-dOrE-En8.js} +1 -1
- package/dist/studio/assets/{jsx-w4r1xieS.js → jsx-BSTPfqWX.js} +1 -1
- package/dist/studio/assets/{julia-BFBtDmmM.js → julia-Dvz-BPmf.js} +1 -1
- package/dist/studio/assets/{just-CjAPsU_y.js → just-BHQm0Atq.js} +1 -1
- package/dist/studio/assets/{latex-CghDKqKz.js → latex-CxdhxuXv.js} +1 -1
- package/dist/studio/assets/{liquid-Cp9loVkE.js → liquid-D-hM-Q_F.js} +1 -1
- package/dist/studio/assets/{lua-HNnIDxnY.js → lua-5qtqjLHq.js} +1 -1
- package/dist/studio/assets/{marko-BXT10Apm.js → marko-5FJaM2co.js} +1 -1
- package/dist/studio/assets/{mdc-CaiOz4_A.js → mdc-BhYW7lCG.js} +1 -1
- package/dist/studio/assets/mermaid-GHXKKRXX-CWM2EB-u.js +1 -0
- package/dist/studio/assets/{nginx-DgYZ5SY9.js → nginx-rtAXrRye.js} +1 -1
- package/dist/studio/assets/{nim-CAMVX-MV.js → nim-DQSmaN9U.js} +1 -1
- package/dist/studio/assets/{perl-Dc6Wp7SD.js → perl-B54AdcaE.js} +1 -1
- package/dist/studio/assets/{php-DgLi8LNU.js → php-DpvTxCny.js} +1 -1
- package/dist/studio/assets/{pug-ZHFv4Vgd.js → pug-Cyffw0Ho.js} +1 -1
- package/dist/studio/assets/{qml-DU62cSZp.js → qml-CxRetSkO.js} +1 -1
- package/dist/studio/assets/{r-BwU69RSh.js → r-CXWRxrXc.js} +1 -1
- package/dist/studio/assets/{razor-BUgi_XZM.js → razor-BJzI-zc9.js} +1 -1
- package/dist/studio/assets/{regexp-DuNhqfkW.js → regexp-dqxdfrnE.js} +1 -1
- package/dist/studio/assets/{rst-BB3BeaSy.js → rst-BW7eKYj4.js} +1 -1
- package/dist/studio/assets/{ruby-T0tkEfUZ.js → ruby-D5Npyj8-.js} +1 -1
- package/dist/studio/assets/{sas-CMhZa3S0.js → sas-DZLppjul.js} +1 -1
- package/dist/studio/assets/{scss-DtXjZ3vZ.js → scss-CbqoxyAe.js} +1 -1
- package/dist/studio/assets/{shellscript-BESt-FLs.js → shellscript-CFHqBeXu.js} +1 -1
- package/dist/studio/assets/{shellsession-8irtvYxL.js → shellsession-CbZDA4Bf.js} +1 -1
- package/dist/studio/assets/{soy-CIvhOgLR.js → soy-PImjtV58.js} +1 -1
- package/dist/studio/assets/{sql-obAOXh1G.js → sql-DhGo8urW.js} +1 -1
- package/dist/studio/assets/{stata-D3OCK8xW.js → stata-CjHWbU2v.js} +1 -1
- package/dist/studio/assets/{surrealql-Dk_bguB0.js → surrealql-o8Qr4t56.js} +1 -1
- package/dist/studio/assets/{svelte-C0-KQt-t.js → svelte-DlSIyEV1.js} +1 -1
- package/dist/studio/assets/{templ-lg4nvF9H.js → templ-qizfwU1w.js} +1 -1
- package/dist/studio/assets/{tex-BNcQSX7R.js → tex-Ca7T661H.js} +1 -1
- package/dist/studio/assets/{ts-tags-AgW9jVv0.js → ts-tags-DUmjLkgl.js} +1 -1
- package/dist/studio/assets/{tsx-CL8XyR3d.js → tsx-CfwgIqHW.js} +1 -1
- package/dist/studio/assets/{twig-VkSbsH0Y.js → twig-D7NAYUBY.js} +1 -1
- package/dist/studio/assets/{typescript-BfD1znAG.js → typescript-Bbz9oX-P.js} +1 -1
- package/dist/studio/assets/{vue-html-w3sbjuft.js → vue-html-CcxTb_Jn.js} +1 -1
- package/dist/studio/assets/{vue-gv_2i8Vk.js → vue-vYpz06zQ.js} +1 -1
- package/dist/studio/assets/{vue-vine-C9sgcRlL.js → vue-vine-DW4RqCKF.js} +1 -1
- package/dist/studio/assets/{xml-nNPxMKnG.js → xml-R4LZ_u4f.js} +1 -1
- package/dist/studio/assets/{xsl-C7iHP36x.js → xsl-CS-xzp3L.js} +1 -1
- package/dist/studio/assets/{yaml-Cq3YaG6o.js → yaml-CqDWbYXd.js} +1 -1
- package/dist/studio/index.html +2 -2
- package/dist/think.d.ts +3 -2
- package/dist/think.js +8406 -2
- package/dist/think.js.map +1 -0
- package/dist/tools/extensions.js +1 -1
- package/dist/tools/fetch.d.ts +22 -0
- package/dist/tools/fetch.js +646 -0
- package/dist/tools/fetch.js.map +1 -0
- package/dist/tools/workspace.js +2 -2
- package/dist/tools/workspace.js.map +1 -1
- package/dist/{types-codegen-B6c5KpYH.js → types-codegen-C-9yeFao.js} +9 -33
- package/dist/types-codegen-C-9yeFao.js.map +1 -0
- package/dist/vite.d.ts +5 -5
- package/dist/vite.js +1 -1
- package/dist/workflows.d.ts +1 -1
- package/docs/actions.md +297 -0
- package/docs/channels.md +185 -0
- package/docs/client-tools.md +233 -0
- package/docs/getting-started.md +314 -0
- package/docs/index.md +1324 -0
- package/docs/lifecycle-hooks.md +850 -0
- package/docs/messengers.md +197 -0
- package/docs/programmatic-submissions.md +206 -0
- package/docs/sub-agents.md +524 -0
- package/docs/tools.md +582 -0
- package/docs/workflows.md +217 -0
- package/package.json +50 -17
- package/dist/chat-sdk-Bb93yaZh.js.map +0 -1
- package/dist/codegen-CuPdJRpV.js.map +0 -1
- package/dist/framework/index.js.map +0 -1
- package/dist/studio/assets/index-Cbb3GlZY.js +0 -496
- package/dist/studio/assets/index-DN9_cL01.css +0 -2
- package/dist/studio/assets/mermaid-GHXKKRXX-I2ZcYo7k.js +0 -1
- package/dist/think-BYZXQ-vm.js +0 -6513
- package/dist/think-BYZXQ-vm.js.map +0 -1
- package/dist/types-codegen-B6c5KpYH.js.map +0 -1
package/docs/tools.md
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
# Tools
|
|
2
|
+
|
|
3
|
+
Think provides built-in workspace file tools on every turn, plus integration points for custom tools, code execution, and dynamic extensions.
|
|
4
|
+
|
|
5
|
+
## Tool Merge Order
|
|
6
|
+
|
|
7
|
+
On every turn, Think merges tools from multiple sources. Later sources override earlier ones if names collide:
|
|
8
|
+
|
|
9
|
+
1. **Workspace tools** — `read`, `write`, `edit`, `list`, `find`, `grep`, `delete`, `bash` (built-in)
|
|
10
|
+
2. **`getTools()`** — your custom server-side tools
|
|
11
|
+
3. **Extension tools** — tools from loaded extensions (prefixed by extension name)
|
|
12
|
+
4. **Session tools** — `set_context`, `load_context`, `search_context` (from `configureSession`)
|
|
13
|
+
5. **Skill tools** — `activate_skill`, `read_skill_resource`, `run_skill_script` (from `getSkills()`)
|
|
14
|
+
6. **MCP tools** — from connected MCP servers
|
|
15
|
+
7. **Client tools** — from the browser (see [Client Tools](./client-tools.md))
|
|
16
|
+
|
|
17
|
+
Tools belong to the agent running the turn. For parent-child orchestration,
|
|
18
|
+
use [Agent Tools](https://github.com/cloudflare/agents/blob/main/docs/agents/agent-tools.md) instead of passing one-off tools through
|
|
19
|
+
`chat()`.
|
|
20
|
+
|
|
21
|
+
## Built-in Workspace Tools
|
|
22
|
+
|
|
23
|
+
Every Think agent gets `this.workspace` — a virtual filesystem backed by the Durable Object's SQLite storage. Workspace tools are automatically available to the model with no configuration.
|
|
24
|
+
|
|
25
|
+
| Tool | Description |
|
|
26
|
+
| -------- | --------------------------------------------------------------------------- |
|
|
27
|
+
| `read` | Read text with line numbers; pass images and PDFs to multimodal models |
|
|
28
|
+
| `write` | Write content to a file (creates parent directories) |
|
|
29
|
+
| `edit` | Apply a find-and-replace edit to an existing file (supports fuzzy matching) |
|
|
30
|
+
| `list` | List files and directories in a path |
|
|
31
|
+
| `find` | Find files matching a glob pattern |
|
|
32
|
+
| `grep` | Search file contents by regex or fixed string |
|
|
33
|
+
| `delete` | Delete a file or directory |
|
|
34
|
+
| `bash` | Run a sandboxed Bash script against workspace files |
|
|
35
|
+
|
|
36
|
+
The `bash` tool is enabled by default. It mounts workspace files into a
|
|
37
|
+
`just-bash` virtual filesystem, runs with network access disabled, and writes
|
|
38
|
+
created, updated, and deleted files and empty directories back to the workspace.
|
|
39
|
+
Use it for shell-style workflows that combine multiple file operations; use the
|
|
40
|
+
narrower tools for simple reads, writes, and edits.
|
|
41
|
+
|
|
42
|
+
To keep tool calls bounded, the Bash tool snapshots up to 1,000 workspace files
|
|
43
|
+
by default and skips files larger than 1 MB. Skipped files are reported in the
|
|
44
|
+
tool result and are treated as protected during write-back so the script cannot
|
|
45
|
+
accidentally overwrite or delete content that was not mounted. You can tune
|
|
46
|
+
`maxWorkspaceFiles`, `maxWorkspaceFileBytes`, `maxOutputBytes`, `timeout`, and
|
|
47
|
+
`network` through `workspaceBash`.
|
|
48
|
+
|
|
49
|
+
Disable the default Bash tool for conservative deployments:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
export class MyAgent extends Think<Env> {
|
|
53
|
+
workspaceBash = false;
|
|
54
|
+
|
|
55
|
+
getModel() {
|
|
56
|
+
/* ... */
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### R2 Spillover
|
|
62
|
+
|
|
63
|
+
By default, the workspace stores everything in SQLite. For large files, override `workspace` to add R2 spillover:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { Think } from "@cloudflare/think";
|
|
67
|
+
import { Workspace } from "@cloudflare/shell";
|
|
68
|
+
|
|
69
|
+
export class MyAgent extends Think<Env> {
|
|
70
|
+
override workspace = new Workspace({
|
|
71
|
+
sql: this.ctx.storage.sql,
|
|
72
|
+
r2: this.env.R2,
|
|
73
|
+
name: () => this.name
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
getModel() {
|
|
77
|
+
/* ... */
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This requires an R2 bucket binding in `wrangler.jsonc`:
|
|
83
|
+
|
|
84
|
+
```jsonc
|
|
85
|
+
{
|
|
86
|
+
"r2_buckets": [{ "binding": "R2", "bucket_name": "agent-files" }]
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Custom Tools
|
|
91
|
+
|
|
92
|
+
Override `getTools()` to add your own tools. These are standard AI SDK `tool()` definitions with Zod schemas:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { Think } from "@cloudflare/think";
|
|
96
|
+
import { tool } from "ai";
|
|
97
|
+
import type { ToolSet } from "ai";
|
|
98
|
+
import { z } from "zod";
|
|
99
|
+
|
|
100
|
+
export class MyAgent extends Think<Env> {
|
|
101
|
+
getModel() {
|
|
102
|
+
/* ... */
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getTools(): ToolSet {
|
|
106
|
+
return {
|
|
107
|
+
getWeather: tool({
|
|
108
|
+
description: "Get the current weather for a city",
|
|
109
|
+
inputSchema: z.object({
|
|
110
|
+
city: z.string().describe("City name")
|
|
111
|
+
}),
|
|
112
|
+
execute: async ({ city }) => {
|
|
113
|
+
const res = await fetch(
|
|
114
|
+
`https://api.weather.com/v1/current?q=${city}&key=${this.env.WEATHER_KEY}`
|
|
115
|
+
);
|
|
116
|
+
return res.json();
|
|
117
|
+
}
|
|
118
|
+
}),
|
|
119
|
+
|
|
120
|
+
calculate: tool({
|
|
121
|
+
description: "Perform a math calculation",
|
|
122
|
+
inputSchema: z.object({
|
|
123
|
+
a: z.number(),
|
|
124
|
+
b: z.number(),
|
|
125
|
+
operator: z.enum(["+", "-", "*", "/"])
|
|
126
|
+
}),
|
|
127
|
+
execute: async ({ a, b, operator }) => {
|
|
128
|
+
const ops: Record<string, (x: number, y: number) => number> = {
|
|
129
|
+
"+": (x, y) => x + y,
|
|
130
|
+
"-": (x, y) => x - y,
|
|
131
|
+
"*": (x, y) => x * y,
|
|
132
|
+
"/": (x, y) => x / y
|
|
133
|
+
};
|
|
134
|
+
return { result: ops[operator](a, b) };
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Custom tools are merged with workspace tools automatically. If a custom tool has the same name as a workspace tool, the custom tool wins.
|
|
143
|
+
|
|
144
|
+
### Tool Approval
|
|
145
|
+
|
|
146
|
+
Tools can require user approval before execution using the `needsApproval` option:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
getTools(): ToolSet {
|
|
150
|
+
return {
|
|
151
|
+
deleteFile: tool({
|
|
152
|
+
description: "Delete a file from the system",
|
|
153
|
+
inputSchema: z.object({ path: z.string() }),
|
|
154
|
+
needsApproval: async ({ path }) => path.startsWith("/important/"),
|
|
155
|
+
execute: async ({ path }) => {
|
|
156
|
+
await this.workspace.rm(path);
|
|
157
|
+
return { deleted: path };
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
When `needsApproval` returns `true`, the tool call is sent to the client for approval. The conversation pauses until the client responds with `CF_AGENT_TOOL_APPROVAL`. See [Client Tools](./client-tools.md) for the approval flow.
|
|
165
|
+
|
|
166
|
+
> Inside the [Code Execution Tool](#code-execution-tool)'s sandbox, `needsApproval` behaves differently: it maps to the codemode runtime's durable pause/approve/resume flow, and a function-valued `needsApproval` always requires approval (see [Approvals](#approvals-human-in-the-loop)).
|
|
167
|
+
|
|
168
|
+
### Per-turn Tool Overrides
|
|
169
|
+
|
|
170
|
+
The `beforeTurn` hook can restrict or add tools for a specific turn:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
beforeTurn(ctx: TurnContext) {
|
|
174
|
+
return {
|
|
175
|
+
activeTools: ["read", "write", "getWeather"],
|
|
176
|
+
tools: { emergencyTool: this.createEmergencyTool() }
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
`activeTools` limits which tools the model can call. `tools` adds extra tools for this turn only (merged on top of existing tools).
|
|
182
|
+
|
|
183
|
+
## MCP Tools
|
|
184
|
+
|
|
185
|
+
Think inherits MCP client support from the `Agent` base class. MCP tools from connected servers are automatically merged into every turn.
|
|
186
|
+
|
|
187
|
+
Set `waitForMcpConnections` to ensure MCP servers are connected before the inference loop runs:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
export class MyAgent extends Think<Env> {
|
|
191
|
+
waitForMcpConnections = true; // default 10s timeout
|
|
192
|
+
// or: waitForMcpConnections = { timeout: 5000 };
|
|
193
|
+
|
|
194
|
+
getModel() {
|
|
195
|
+
/* ... */
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Add MCP servers programmatically or via `@callable` methods:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { callable } from "agents";
|
|
204
|
+
|
|
205
|
+
export class MyAgent extends Think<Env> {
|
|
206
|
+
getModel() {
|
|
207
|
+
/* ... */
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@callable()
|
|
211
|
+
async addServer(name: string, url: string) {
|
|
212
|
+
return await this.addMcpServer(name, url);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
@callable()
|
|
216
|
+
async removeServer(serverId: string) {
|
|
217
|
+
await this.removeMcpServer(serverId);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
See [Connecting to MCP Servers](https://github.com/cloudflare/agents/blob/main/docs/agents/mcp-client.md) for full MCP client documentation.
|
|
223
|
+
|
|
224
|
+
## Code Execution Tool
|
|
225
|
+
|
|
226
|
+
Let the LLM write and run TypeScript in a sandboxed Worker, recorded on a durable codemode runtime (abort-and-replay, human approvals, audit trail, reusable snippets). Requires `@cloudflare/codemode` and a `worker_loaders` binding.
|
|
227
|
+
|
|
228
|
+
```sh
|
|
229
|
+
npm install @cloudflare/codemode
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The one-liner infers everything from the agent — `state.*` from `this.workspace`, the executor from `env.LOADER`, and a live browser (`cdp.*`) from `env.BROWSER` if bound:
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
import { Think } from "@cloudflare/think";
|
|
236
|
+
import { createExecuteTool } from "@cloudflare/think/tools/execute";
|
|
237
|
+
|
|
238
|
+
export class MyAgent extends Think<Env> {
|
|
239
|
+
getModel() {
|
|
240
|
+
/* ... */
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
getTools() {
|
|
244
|
+
return {
|
|
245
|
+
execute: createExecuteTool(this)
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Setup checklist:
|
|
252
|
+
|
|
253
|
+
```jsonc
|
|
254
|
+
// wrangler.jsonc
|
|
255
|
+
{
|
|
256
|
+
"worker_loaders": [{ "binding": "LOADER" }],
|
|
257
|
+
"browser": { "binding": "BROWSER" } // optional — enables cdp.*
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
// worker entry — the runtime lives in a Durable Object facet, so the class
|
|
263
|
+
// must be exported (the @cloudflare/codemode/vite plugin does this
|
|
264
|
+
// automatically; the Think framework's generated entry already includes it)
|
|
265
|
+
export { CodemodeRuntime } from "@cloudflare/codemode";
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Each missing piece fails with an error naming the step.
|
|
269
|
+
|
|
270
|
+
Inside the sandbox the model sees typed namespaces plus the platform SDK:
|
|
271
|
+
|
|
272
|
+
- `tools.*` — your AI SDK tools (object args, validated against their schemas). Only tools with an `execute` function are exposed — client-side tools can't run in the sandbox.
|
|
273
|
+
- `state.*` — the workspace filesystem (`state.readFile({ path })`, `state.glob({ pattern })`, `state.planEdits(...)`, …)
|
|
274
|
+
- `cdp.*` — the browser, when a Browser Rendering binding is configured. The execute tool defaults to `session: { mode: "dynamic" }`: sessions are per-execution unless the model promotes one with `cdp.startSession()`.
|
|
275
|
+
- `codemode.search` / `codemode.describe` / `codemode.step` / `codemode.run` — discovery, side-effect boundaries, and saved snippets
|
|
276
|
+
|
|
277
|
+
Pass overrides for anything beyond the defaults — e.g. custom `tools.*` alongside the agent-derived state:
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
execute: createExecuteTool(this, { tools: myDomainTools });
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Or fully explicit options (no agent inference):
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import { createWorkspaceStateBackend } from "@cloudflare/shell";
|
|
287
|
+
|
|
288
|
+
createExecuteTool({
|
|
289
|
+
ctx: this.ctx,
|
|
290
|
+
tools: myDomainTools,
|
|
291
|
+
state: createWorkspaceStateBackend(this.workspace),
|
|
292
|
+
browser: this.env.BROWSER,
|
|
293
|
+
loader: this.env.LOADER
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Approvals (human-in-the-loop)
|
|
298
|
+
|
|
299
|
+
An AI SDK tool with `needsApproval` doesn't run immediately inside the sandbox — calling it **pauses the run durably**. The pause comes back as a normal tool output (`{ status: "paused", executionId, pending }`), the model tells the user what it needs, and the turn ends. Note this differs from the client-side approval flow for plain `getTools()` tools: inside the sandbox a function-valued `needsApproval` can't be evaluated against the call's arguments ahead of time, so it conservatively **always** requires approval. Think ships built-in callables to resolve it:
|
|
300
|
+
|
|
301
|
+
- `approveExecution(executionId)` — resumes the run where it stopped (already-done work is replayed, not re-executed); the outcome replaces the paused output in the transcript and the chat auto-continues.
|
|
302
|
+
- `rejectExecution(executionId, reason?)` — ends the run with `{ status: "rejected", reason }` so the model can adapt.
|
|
303
|
+
- `pendingExecutions()` — pending actions (with full args) for rendering approval UI.
|
|
304
|
+
|
|
305
|
+
> **Render approval cards from `pendingExecutions()`, not the transcript.** The `pending` array in the paused tool output is a _truncated preview_ — args are bounded (~2 KB each) so they don't blow up model context, but the full args (up to 1 MB) are what actually execute on approve. A human approving a gated call must see the authoritative args, so fetch them via `pendingExecutions(executionId)` before enabling the Approve button. `examples/assistant`'s `PausedExecutionCard` shows the pattern.
|
|
306
|
+
|
|
307
|
+
See `examples/assistant` for a working approval card, and `design/think-execute-hitl.md` for the design.
|
|
308
|
+
|
|
309
|
+
### The runtime handle
|
|
310
|
+
|
|
311
|
+
`createExecuteRuntime` returns the moving parts when the host needs more than the tool — and the handle is also assigned to `this.codemode` when created from an agent:
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import { createExecuteRuntime } from "@cloudflare/think/tools/execute";
|
|
315
|
+
|
|
316
|
+
const { runtime, connectors, tool } = createExecuteRuntime(this);
|
|
317
|
+
await runtime.executions(); // audit trail
|
|
318
|
+
await runtime.expirePaused(); // reclaim stale never-approved pauses (call from a scheduled task)
|
|
319
|
+
await runtime.saveSnippet("name", { executionId }); // promote a script for reuse
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Browser Tools
|
|
323
|
+
|
|
324
|
+
Give your agent full access to the Chrome DevTools Protocol (CDP) for web page inspection, scraping, screenshots, and debugging. Requires `@cloudflare/codemode` and a Browser Rendering binding.
|
|
325
|
+
|
|
326
|
+
```sh
|
|
327
|
+
npm install @cloudflare/codemode
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
import { Think } from "@cloudflare/think";
|
|
332
|
+
import { createBrowserTools } from "@cloudflare/think/tools/browser";
|
|
333
|
+
|
|
334
|
+
export class MyAgent extends Think<Env> {
|
|
335
|
+
getModel() {
|
|
336
|
+
/* ... */
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
getTools() {
|
|
340
|
+
return {
|
|
341
|
+
...createBrowserTools({
|
|
342
|
+
ctx: this.ctx,
|
|
343
|
+
browser: this.env.BROWSER,
|
|
344
|
+
loader: this.env.LOADER
|
|
345
|
+
})
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
Add the Browser Rendering and Worker Loader bindings in `wrangler.jsonc`:
|
|
352
|
+
|
|
353
|
+
```jsonc
|
|
354
|
+
{
|
|
355
|
+
"browser": { "binding": "BROWSER" },
|
|
356
|
+
"worker_loaders": [{ "binding": "LOADER" }]
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
This adds the durable CDP tool plus — whenever a `browser` binding is present — the stateless [Quick Action](https://github.com/cloudflare/agents/blob/main/docs/agents/browse-the-web.md#quick-actions-stateless-browsing) tools by default:
|
|
361
|
+
|
|
362
|
+
| Tool | Description |
|
|
363
|
+
| ------------------ | ----------------------------------------------------------------------------------------- |
|
|
364
|
+
| `browser_execute` | Run TypeScript against a live browser over CDP (screenshots, DOM reads, JS evaluation, …) |
|
|
365
|
+
| `browser_markdown` | Read a page (or raw HTML) as Markdown |
|
|
366
|
+
| `browser_extract` | Extract structured data from a page with AI |
|
|
367
|
+
| `browser_links` | List every link on a page |
|
|
368
|
+
| `browser_scrape` | Scrape specific elements by CSS selector |
|
|
369
|
+
|
|
370
|
+
Pass `quickActions: false` to keep only `browser_execute`, or `quickActions: { actions, maxChars, options }` to configure them. They share the `browser` binding, need no Worker Loader, and `ctx` is resolved from the current Agent automatically (you only need to pass it outside an Agent). For the stateless tools _without_ the CDP tool, import `createQuickActionTools` directly from `@cloudflare/think/tools/browser`.
|
|
371
|
+
|
|
372
|
+
`browser_execute` is backed by a codemode runtime with the `cdp` connector: the model writes async arrow functions that run in a sandboxed Worker isolate, with `cdp.send()`, `cdp.attachToTarget()`, `cdp.spec()` (the live, normalized protocol description), session helpers (`cdp.startSession()`, `cdp.sessionInfo()`, `cdp.closeSession()`), and debug-log helpers. Executions are recorded for abort-and-replay, so browser sessions survive approval pauses.
|
|
373
|
+
|
|
374
|
+
By default each execution gets a fresh browser session (`one-shot`), torn down when the run ends. Pass `session: { mode: "dynamic" }` to let the model promote a session with `cdp.startSession()` so later executions continue in the same browser, or `session: { mode: "reuse", key }` for a named long-lived session. Stale sessions are reclaimed by the connector's `sweep()` — call it from a scheduled task (see `createBrowserRuntime` in `agents/browser` for the connector handle).
|
|
375
|
+
|
|
376
|
+
> The simplest setup is the unified execute tool above: `createExecuteTool(this)` already includes `cdp.*` alongside `state.*` and `tools.*` when `env.BROWSER` is bound — one tool, one durable history. Use `createBrowserTools` when you want a separate, browser-only tool.
|
|
377
|
+
|
|
378
|
+
### Combining with Other Tools
|
|
379
|
+
|
|
380
|
+
Browser tools compose naturally with workspace tools, code execution, MCP, and extensions:
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
import { createBrowserTools } from "@cloudflare/think/tools/browser";
|
|
384
|
+
import { createExecuteTool } from "@cloudflare/think/tools/execute";
|
|
385
|
+
|
|
386
|
+
export class ResearchAgent extends Think<Env> {
|
|
387
|
+
getModel() {
|
|
388
|
+
/* ... */
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
getTools() {
|
|
392
|
+
return {
|
|
393
|
+
// Browse the web (separate browser-only tool with its own history)
|
|
394
|
+
...createBrowserTools({
|
|
395
|
+
ctx: this.ctx,
|
|
396
|
+
browser: this.env.BROWSER,
|
|
397
|
+
loader: this.env.LOADER
|
|
398
|
+
}),
|
|
399
|
+
// Run sandboxed code against workspace files
|
|
400
|
+
execute: createExecuteTool(this)
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### Custom CDP Endpoint
|
|
407
|
+
|
|
408
|
+
To connect to a Chrome instance running outside of Browser Rendering (e.g. `chrome --remote-debugging-port=9222`), pass `cdpUrl` instead of `browser`:
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
createBrowserTools({
|
|
412
|
+
ctx: this.ctx,
|
|
413
|
+
cdpUrl: "http://localhost:9222",
|
|
414
|
+
loader: this.env.LOADER
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
See [Browse the Web](https://github.com/cloudflare/agents/blob/main/docs/agents/browse-the-web.md) for the full CDP helper API reference, security model, and limitations.
|
|
419
|
+
|
|
420
|
+
## Extensions
|
|
421
|
+
|
|
422
|
+
Extensions are dynamically loaded sandboxed Workers that add tools at runtime. The LLM can write extension source code, load it, and use the new tools on the next turn.
|
|
423
|
+
|
|
424
|
+
### Setup
|
|
425
|
+
|
|
426
|
+
Extensions require `extensionLoader` (a `worker_loaders` binding) and the `ExtensionManager`:
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
import { Think } from "@cloudflare/think";
|
|
430
|
+
import { ExtensionManager } from "@cloudflare/think/extensions";
|
|
431
|
+
import { createExtensionTools } from "@cloudflare/think/tools/extensions";
|
|
432
|
+
|
|
433
|
+
export class MyAgent extends Think<Env> {
|
|
434
|
+
extensionLoader = this.env.LOADER;
|
|
435
|
+
|
|
436
|
+
getModel() {
|
|
437
|
+
/* ... */
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
When `extensionLoader` is set, Think automatically creates an `ExtensionManager` and loads extensions from `getExtensions()`.
|
|
443
|
+
|
|
444
|
+
### Static Extensions
|
|
445
|
+
|
|
446
|
+
Define extensions that load at startup:
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
export class MyAgent extends Think<Env> {
|
|
450
|
+
extensionLoader = this.env.LOADER;
|
|
451
|
+
|
|
452
|
+
getModel() {
|
|
453
|
+
/* ... */
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
getExtensions() {
|
|
457
|
+
return [
|
|
458
|
+
{
|
|
459
|
+
manifest: {
|
|
460
|
+
name: "math",
|
|
461
|
+
version: "1.0.0",
|
|
462
|
+
permissions: { network: false }
|
|
463
|
+
},
|
|
464
|
+
source: `({
|
|
465
|
+
tools: {
|
|
466
|
+
add: {
|
|
467
|
+
description: "Add two numbers",
|
|
468
|
+
parameters: {
|
|
469
|
+
a: { type: "number" },
|
|
470
|
+
b: { type: "number" }
|
|
471
|
+
},
|
|
472
|
+
execute: async ({ a, b }) => ({ result: a + b })
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
})`
|
|
476
|
+
}
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
Extension tools are namespaced — `math` extension with `add` tool becomes `math_add` in the model's tool set.
|
|
483
|
+
|
|
484
|
+
### LLM-Driven Extensions
|
|
485
|
+
|
|
486
|
+
Give the model `createExtensionTools` so it can load extensions dynamically:
|
|
487
|
+
|
|
488
|
+
```typescript
|
|
489
|
+
import { createExtensionTools } from "@cloudflare/think/tools/extensions";
|
|
490
|
+
|
|
491
|
+
export class MyAgent extends Think<Env> {
|
|
492
|
+
extensionLoader = this.env.LOADER;
|
|
493
|
+
|
|
494
|
+
getModel() {
|
|
495
|
+
/* ... */
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
getTools() {
|
|
499
|
+
return {
|
|
500
|
+
...createExtensionTools({ manager: this.extensionManager! }),
|
|
501
|
+
...this.extensionManager!.getTools()
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
This gives the model two tools:
|
|
508
|
+
|
|
509
|
+
- `load_extension` — load a new extension from JavaScript source
|
|
510
|
+
- `list_extensions` — list currently loaded extensions
|
|
511
|
+
|
|
512
|
+
Loaded extensions persist across DO restarts via `extensionManager.restore()`.
|
|
513
|
+
|
|
514
|
+
### Extension Context Blocks
|
|
515
|
+
|
|
516
|
+
Extensions can declare context blocks in their manifest. These are automatically registered with the Session:
|
|
517
|
+
|
|
518
|
+
```typescript
|
|
519
|
+
getExtensions() {
|
|
520
|
+
return [{
|
|
521
|
+
manifest: {
|
|
522
|
+
name: "notes",
|
|
523
|
+
version: "1.0.0",
|
|
524
|
+
permissions: { network: false },
|
|
525
|
+
context: [
|
|
526
|
+
{ label: "scratchpad", description: "Extension scratch space", maxTokens: 500 }
|
|
527
|
+
]
|
|
528
|
+
},
|
|
529
|
+
source: `({ tools: { /* ... */ } })`
|
|
530
|
+
}];
|
|
531
|
+
}
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
The context block is registered as `notes_scratchpad` (namespaced by extension name).
|
|
535
|
+
|
|
536
|
+
## Workspace Tools for Custom Backends
|
|
537
|
+
|
|
538
|
+
The individual tool factories are exported for use with custom storage backends that implement the operations interfaces:
|
|
539
|
+
|
|
540
|
+
```typescript
|
|
541
|
+
import {
|
|
542
|
+
createReadTool,
|
|
543
|
+
createWriteTool,
|
|
544
|
+
createEditTool,
|
|
545
|
+
createListTool,
|
|
546
|
+
createFindTool,
|
|
547
|
+
createGrepTool,
|
|
548
|
+
createDeleteTool
|
|
549
|
+
} from "@cloudflare/think/tools/workspace";
|
|
550
|
+
import type {
|
|
551
|
+
ReadOperations,
|
|
552
|
+
WriteOperations,
|
|
553
|
+
EditOperations,
|
|
554
|
+
ListOperations,
|
|
555
|
+
FindOperations,
|
|
556
|
+
GrepOperations,
|
|
557
|
+
DeleteOperations
|
|
558
|
+
} from "@cloudflare/think/tools/workspace";
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
Implement the operations interface for your storage backend:
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
const myReadOps: ReadOperations = {
|
|
565
|
+
readFile: async (path) => fetchFromMyStorage(path),
|
|
566
|
+
readFileBytes: async (path) => fetchBytesFromMyStorage(path),
|
|
567
|
+
stat: async (path) => getFileInfo(path)
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
const readTool = createReadTool({ ops: myReadOps });
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
Or create the full set from a Workspace:
|
|
574
|
+
|
|
575
|
+
```typescript
|
|
576
|
+
import { createWorkspaceTools } from "@cloudflare/think/tools/workspace";
|
|
577
|
+
|
|
578
|
+
const tools = createWorkspaceTools(myCustomWorkspace);
|
|
579
|
+
const toolsWithoutBash = createWorkspaceTools(myCustomWorkspace, {
|
|
580
|
+
bash: false
|
|
581
|
+
});
|
|
582
|
+
```
|