@cloudflare/think 0.22.0 → 0.24.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/README.md +79 -117
- package/dist/extensions/index.d.ts +4 -14
- package/dist/extensions/index.js +2 -15
- package/dist/extensions/index.js.map +1 -1
- package/dist/{index-BFK9bpzz.d.ts → index-DIQ9oY1t.d.ts} +208 -75
- package/dist/{manager-dOcdCX4u.d.ts → manager-CtXhU1fK.d.ts} +9 -2
- package/dist/{manager-BubfmA3q.js → manager-sfWIcYJT.js} +24 -4
- package/dist/manager-sfWIcYJT.js.map +1 -0
- package/dist/messengers/index.d.ts +1 -1
- package/dist/messengers/telegram.d.ts +1 -1
- package/dist/rolldown-runtime-D7D4PA-g.js +13 -0
- package/dist/think.d.ts +2 -2
- package/dist/think.js +135 -88
- package/dist/think.js.map +1 -1
- package/dist/tools/browser.d.ts +1 -1
- package/dist/tools/browser.js +1 -1
- package/dist/tools/browser.js.map +1 -1
- package/dist/tools/execute.d.ts +2 -2
- package/dist/tools/execute.js +2 -2
- package/dist/tools/execute.js.map +1 -1
- package/dist/tools/extensions.d.ts +1 -1
- package/dist/tools/extensions.js +1 -1
- package/dist/workflows.d.ts +1 -1
- package/docs/getting-started.md +3 -3
- package/docs/index.md +100 -95
- package/docs/lifecycle-hooks.md +13 -13
- package/docs/multi-chat.md +8 -7
- package/docs/tools.md +12 -14
- package/docs/workflows.md +1 -1
- package/package.json +6 -6
- package/dist/manager-BubfmA3q.js.map +0 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ import { routeAgentRequest } from "agents";
|
|
|
16
16
|
|
|
17
17
|
export class MyAgent extends Think<Env> {
|
|
18
18
|
getModel() {
|
|
19
|
-
return "@cf/
|
|
19
|
+
return "@cf/deepseek-ai/deepseek-v4.1-flash-1017";
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
getSystemPrompt() {
|
|
@@ -187,17 +187,16 @@ Configure the backends, and the modules they offer, on the `Workspace`:
|
|
|
187
187
|
|
|
188
188
|
```ts
|
|
189
189
|
import { Workspace } from "@cloudflare/computer";
|
|
190
|
-
import {
|
|
190
|
+
import { CodemodeBackend } from "@cloudflare/computer/backends/codemode";
|
|
191
191
|
import { createGitModule } from "@cloudflare/computer/modules/git";
|
|
192
192
|
|
|
193
193
|
export class MyAgent extends Think<Env> {
|
|
194
194
|
override workspace = new Workspace({
|
|
195
195
|
storage: this.ctx.storage,
|
|
196
196
|
backends: [
|
|
197
|
-
new
|
|
198
|
-
id: "js",
|
|
197
|
+
new CodemodeBackend({
|
|
199
198
|
loader: this.env.LOADER,
|
|
200
|
-
modules: { "
|
|
199
|
+
modules: { "codemode:git": createGitModule() }
|
|
201
200
|
})
|
|
202
201
|
]
|
|
203
202
|
});
|
|
@@ -207,24 +206,23 @@ export class MyAgent extends Think<Env> {
|
|
|
207
206
|
To change a built-in tool, return a tool with the same name from `getTools()`.
|
|
208
207
|
To hide tools for a turn, return `activeTools` from `beforeTurn`.
|
|
209
208
|
|
|
210
|
-
Give a backend a `
|
|
211
|
-
import the agent's tools; files come from `node:fs`:
|
|
209
|
+
Give a backend a `codemode:tools` module, and code the model runs with `exec`
|
|
210
|
+
can import the agent's tools; files come from `node:fs`:
|
|
212
211
|
|
|
213
212
|
```ts
|
|
214
213
|
import { createToolBindings } from "@cloudflare/computer/modules/tools";
|
|
215
214
|
import { forAISDKTools } from "@cloudflare/computer/tools/ai-sdk";
|
|
216
215
|
|
|
217
|
-
new
|
|
218
|
-
id: "js",
|
|
216
|
+
new CodemodeBackend({
|
|
219
217
|
loader: this.env.LOADER,
|
|
220
218
|
modules: {
|
|
221
|
-
"
|
|
219
|
+
"codemode:tools": createToolBindings(forAISDKTools(() => this.getTools()))
|
|
222
220
|
}
|
|
223
221
|
});
|
|
224
222
|
```
|
|
225
223
|
|
|
226
224
|
```ts
|
|
227
|
-
import { getWeather } from "
|
|
225
|
+
import { getWeather } from "codemode:tools";
|
|
228
226
|
export default async () => (await getWeather({ city: "Lisbon" })).text;
|
|
229
227
|
```
|
|
230
228
|
|
|
@@ -233,113 +231,79 @@ Every Think agent can hand out its own Workspace: `getWorkspace(stub)` from
|
|
|
233
231
|
|
|
234
232
|
## Agent Skills
|
|
235
233
|
|
|
236
|
-
Think supports
|
|
237
|
-
a
|
|
238
|
-
|
|
234
|
+
Think supports [Agent Skills](https://agentskills.io/). A skill is a directory
|
|
235
|
+
with a `SKILL.md` file. Return skill sources from `getSkills()`; Think adds the
|
|
236
|
+
skill catalog to the prompt and exposes `activate_skill` and
|
|
239
237
|
`read_skill_resource` tools when skills are available.
|
|
240
238
|
|
|
239
|
+
Keep skills as files in the agent's Workspace. Every
|
|
240
|
+
`/workspace/.agents/skills/<name>/SKILL.md` is a skill, and the files beside it
|
|
241
|
+
are its resources:
|
|
242
|
+
|
|
241
243
|
```ts
|
|
242
244
|
import { Think, skills } from "@cloudflare/think";
|
|
243
|
-
import bundledSkills from "agents:skills"; // resolves to ./skills next to this file
|
|
244
245
|
|
|
245
246
|
export class MyAgent extends Think<Env> {
|
|
246
247
|
getSkills() {
|
|
247
248
|
return [
|
|
248
|
-
|
|
249
|
+
skills.workspace(this.workspace),
|
|
249
250
|
skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })
|
|
250
251
|
];
|
|
251
252
|
}
|
|
252
|
-
|
|
253
|
-
getSkillScriptRunner() {
|
|
254
|
-
return skills.runner({
|
|
255
|
-
loader: this.env.LOADER,
|
|
256
|
-
workspaceInstance: this.workspace
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
253
|
}
|
|
260
254
|
```
|
|
261
255
|
|
|
262
|
-
Bundled skills use the Agents Vite plugin. Register it alongside the Cloudflare
|
|
263
|
-
plugin in `vite.config.ts`:
|
|
264
|
-
|
|
265
|
-
```ts
|
|
266
|
-
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
267
|
-
import agents from "agents/vite";
|
|
268
|
-
import { defineConfig } from "vite";
|
|
269
|
-
|
|
270
|
-
export default defineConfig({
|
|
271
|
-
plugins: [agents(), cloudflare()]
|
|
272
|
-
});
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
The `agents:skills` specifier resolves to a `./skills` directory next to the
|
|
276
|
-
importing file; use `agents:skills/<dir>` for a differently named sibling
|
|
277
|
-
directory:
|
|
278
|
-
|
|
279
|
-
```ts
|
|
280
|
-
import bundledSkills from "agents:skills";
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
The import is typed by ambient declarations shipped with `agents` (importing
|
|
284
|
-
`Think`, which pulls in `agents`, is enough; otherwise add
|
|
285
|
-
`/// <reference types="agents/skills-module" />`). Without the Vite plugin,
|
|
286
|
-
construct a source with `skills.fromManifest(...)`. Sources are applied in
|
|
287
|
-
order: the first to register a skill name wins, and duplicate or failing
|
|
288
|
-
sources are skipped with a warning instead of failing the agent.
|
|
289
|
-
|
|
290
|
-
The skills engine itself lives in `agents/skills` (so any agent, including a
|
|
291
|
-
plain `@cloudflare/ai-chat` `onChatMessage`, can build a `SkillRegistry`);
|
|
292
|
-
`@cloudflare/think` re-exports it as `skills` and wires `getSkills()` into the
|
|
293
|
-
turn automatically.
|
|
294
|
-
|
|
295
|
-
Think can also project each resolved `SKILL.md` into the active workspace so
|
|
296
|
-
the agent can read and edit skills as files. This is off by default: skills
|
|
297
|
-
load from their sources and nothing is written to the Workspace. Skills land
|
|
298
|
-
under `/workspace/.agents/skills`. Existing
|
|
299
|
-
workspace edits are preserved and affect later activation. Resources copy into
|
|
300
|
-
the workspace on first use rather than at startup. Think records a durable
|
|
301
|
-
source fingerprint, so unchanged cold wakes do not stat or rewrite every skill
|
|
302
|
-
file.
|
|
303
|
-
|
|
304
|
-
Enable projection, or override its path, with:
|
|
305
|
-
|
|
306
|
-
```ts
|
|
307
|
-
skillWorkspace = {};
|
|
308
|
-
// or, for a custom Computer proxy that presents legacy direct methods:
|
|
309
|
-
skillWorkspace = { root: "/workspace/.agents/skills" };
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
The imported directory should contain one child directory per skill:
|
|
313
|
-
|
|
314
256
|
```text
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
257
|
+
/workspace/.agents/skills/release-notes/SKILL.md
|
|
258
|
+
/workspace/.agents/skills/release-notes/scripts/format-release-notes.js
|
|
259
|
+
/workspace/.agents/skills/release-notes/references/style-guide.md
|
|
318
260
|
```
|
|
319
261
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
262
|
+
Sources are listed again before every turn, so a skill written, edited, or
|
|
263
|
+
deleted in the Workspace takes effect on the next turn. `skills.r2()` reads the
|
|
264
|
+
same layout from a bucket, and `skills.manifest(...)` defines skills in
|
|
265
|
+
code. Sources are applied in order: the first to provide a skill name wins, and
|
|
266
|
+
duplicate or failing sources are skipped with a logged warning.
|
|
267
|
+
|
|
268
|
+
Each `SKILL.md` is checked against the
|
|
269
|
+
[spec](https://agentskills.io/specification) and loaded the way the spec's
|
|
270
|
+
guide for agents recommends: a skill whose `name` breaks the format or does
|
|
271
|
+
not match its directory still loads, with a logged warning. Skills with no
|
|
272
|
+
`description` or unreadable frontmatter are skipped and logged. To check a
|
|
273
|
+
skill before writing it, parse it with `skills.parseSkill()` and require
|
|
274
|
+
`warnings` to be empty. Unknown frontmatter keys are kept in
|
|
275
|
+
`skill.extraFields`.
|
|
276
|
+
|
|
277
|
+
Text resources are returned directly; binary assets are returned as base64.
|
|
278
|
+
`read_skill_resource` accepts `{ name, path }` or a qualified path such as
|
|
279
|
+
`release-notes/references/style-guide.md`. Skills from a Workspace also report
|
|
280
|
+
their directory, so a model with the Workspace `exec` tool can run their
|
|
281
|
+
scripts in place.
|
|
282
|
+
|
|
283
|
+
The parser, sources, and catalog live in `agents/skills` and work without
|
|
284
|
+
Think; `@cloudflare/think` re-exports the module as `skills` and wires
|
|
285
|
+
`getSkills()` into the turn.
|
|
325
286
|
|
|
326
287
|
Skills are on-demand instructions, not always-on system prompt text. The model
|
|
327
288
|
sees the catalog first, then calls `activate_skill` when a user task matches a
|
|
328
289
|
skill description. Use a context block from `configureContext()` for behavior
|
|
329
290
|
that should apply to every turn, especially when the agent also uses skills.
|
|
330
|
-
`getSystemPrompt()`
|
|
331
|
-
|
|
291
|
+
An overridden `getSystemPrompt()` stays at the top of the system prompt, above
|
|
292
|
+
the context blocks and the skills catalog. When skills change mid-conversation,
|
|
293
|
+
the frozen system prompt stays the same; the new catalog reaches the model as a
|
|
294
|
+
reminder until the next compaction or history clear.
|
|
332
295
|
|
|
333
|
-
|
|
334
|
-
enables `run_skill_script`, which can run JavaScript, TypeScript, Python, and
|
|
335
|
-
Bash scripts under `scripts/`.
|
|
296
|
+
### Skill script runner
|
|
336
297
|
|
|
337
|
-
|
|
298
|
+
For skills outside a Workspace (for example in R2), `getSkillScriptRunner()`
|
|
299
|
+
enables `run_skill_script`. It is **experimental**; prefer keeping skills in a
|
|
300
|
+
Workspace and running scripts with `exec`. The runner runs JavaScript, Python,
|
|
301
|
+
and Bash scripts under `scripts/`. A JavaScript script must be one
|
|
302
|
+
self-contained module; compile TypeScript or multi-file scripts before storing
|
|
303
|
+
the skill.
|
|
338
304
|
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
export default async function run(input: unknown, ctx: SkillRunContext) {
|
|
305
|
+
```js
|
|
306
|
+
export default async function run(input, ctx) {
|
|
343
307
|
const guide = ctx.files["references/style-guide.md"]; // bundled text resources
|
|
344
308
|
const summary = await ctx.tools.call("summarize", { input }); // explicit tools
|
|
345
309
|
await ctx.output.writeFile("notes.md", summary); // scratch artifact
|
|
@@ -347,19 +311,19 @@ export default async function run(input: unknown, ctx: SkillRunContext) {
|
|
|
347
311
|
}
|
|
348
312
|
```
|
|
349
313
|
|
|
350
|
-
`ctx` is `{ skill, files, workspace, tools, output }
|
|
351
|
-
text resources by relative path, `ctx.workspace` is
|
|
352
|
-
permission, `ctx.tools` exposes only the tools the
|
|
353
|
-
`ctx.output.writeFile(name, content)` returns scratch
|
|
354
|
-
the workspace. Python and Bash scripts instead use
|
|
355
|
-
`/input.json`, `/context.json`, bundled resources under
|
|
356
|
-
for artifacts (Python supports both
|
|
314
|
+
`ctx` is `{ skill, files, workspace, tools, output }` (`SkillRunContext`):
|
|
315
|
+
`ctx.files` holds bundled text resources by relative path, `ctx.workspace` is
|
|
316
|
+
gated by the workspace permission, `ctx.tools` exposes only the tools the
|
|
317
|
+
runner was given, and `ctx.output.writeFile(name, content)` returns scratch
|
|
318
|
+
artifacts without mutating the workspace. Python and Bash scripts instead use
|
|
319
|
+
the path-based contract: `/input.json`, `/context.json`, bundled resources under
|
|
320
|
+
`/skill`, and `/output` for artifacts (Python supports both
|
|
321
|
+
`def run(input, ctx)` and CLI-style scripts).
|
|
357
322
|
|
|
358
323
|
If `workspaceInstance` is provided, scripts get read-only workspace access by
|
|
359
324
|
default. Workspace writes, tools, and network access are opt-in. Scripts default
|
|
360
|
-
to a 30 second timeout, which can be overridden with `timeout`.
|
|
361
|
-
|
|
362
|
-
Python Dynamic Workers; Bash scripts run through `just-bash`.
|
|
325
|
+
to a 30 second timeout, which can be overridden with `timeout`. Python scripts
|
|
326
|
+
run as Python Dynamic Workers; Bash scripts run through `just-bash`.
|
|
363
327
|
|
|
364
328
|
Script execution requires a Worker Loader binding:
|
|
365
329
|
|
|
@@ -390,7 +354,7 @@ Script execution requires a Worker Loader binding:
|
|
|
390
354
|
| Method / Property | Default | Description |
|
|
391
355
|
| -------------------------- | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
392
356
|
| `getModel()` | throws | Return the `LanguageModel` to use |
|
|
393
|
-
| `getSystemPrompt()` | careful assistant operating prompt |
|
|
357
|
+
| `getSystemPrompt()` | careful assistant operating prompt | Instructions at the top of the system prompt, above context blocks. The default is used only with no context blocks |
|
|
394
358
|
| `getTools()` | `{}` | AI SDK `ToolSet` for the agentic loop |
|
|
395
359
|
| `getMessengers()` | `{}` | Messenger ingress and delivery declarations |
|
|
396
360
|
| `getScheduledTasks()` | `{}` | Code-declared recurring prompts |
|
|
@@ -404,7 +368,6 @@ Script execution requires a Worker Loader binding:
|
|
|
404
368
|
| `hydrationByteBudget` | 32 MiB | Byte budget for startup transcript hydration. Charges each row its full stored size, including the continuation rows a large message is split across |
|
|
405
369
|
| `mediaEviction` | `true` | Media eviction policy: aged media leaves the conversation and is preserved as a Workspace file. `false` keeps aged media in the conversation |
|
|
406
370
|
| `getSkills()` | `[]` | First-class Agent Skills sources |
|
|
407
|
-
| `skillWorkspace` | `false` | Project skills into the Workspace as files; `{}` enables with defaults |
|
|
408
371
|
| `getSkillScriptRunner()` | `null` | Optional runner for `run_skill_script` |
|
|
409
372
|
| `getExtensions()` | `[]` | Sandboxed extension declarations (load order) |
|
|
410
373
|
| `extensionLoader` | `undefined` | `WorkerLoader` binding — enables extensions |
|
|
@@ -1012,8 +975,8 @@ import { Think } from "@cloudflare/think";
|
|
|
1012
975
|
type MyConfig = { modelTier: "fast" | "capable"; systemPrompt: string };
|
|
1013
976
|
|
|
1014
977
|
const MODEL_IDS = {
|
|
1015
|
-
fast: "@cf/
|
|
1016
|
-
capable: "@cf/
|
|
978
|
+
fast: "@cf/qwen/qwen3.8-27b",
|
|
979
|
+
capable: "@cf/deepseek-ai/deepseek-v4.1-flash-1017"
|
|
1017
980
|
} as const;
|
|
1018
981
|
|
|
1019
982
|
export class MyAgent extends Think<Env> {
|
|
@@ -1045,7 +1008,7 @@ For values you want broadcast to connected clients, use `state` / `setState` fro
|
|
|
1045
1008
|
|
|
1046
1009
|
## Code execution tool
|
|
1047
1010
|
|
|
1048
|
-
> **Deprecated.** Use `exec` with `
|
|
1011
|
+
> **Deprecated.** Use `exec` with `codemode:tools` (see [Built-in workspace](#built-in-workspace)).
|
|
1049
1012
|
|
|
1050
1013
|
Let the LLM write and run JavaScript in a sandboxed Worker:
|
|
1051
1014
|
|
|
@@ -1146,15 +1109,14 @@ getTools() {
|
|
|
1146
1109
|
|
|
1147
1110
|
## Runtime dependencies
|
|
1148
1111
|
|
|
1149
|
-
| Package
|
|
1150
|
-
|
|
|
1151
|
-
| `agents`
|
|
1152
|
-
| `ai`
|
|
1153
|
-
| `zod`
|
|
1154
|
-
| `@cloudflare/computer`
|
|
1155
|
-
| `@cloudflare/codemode`
|
|
1156
|
-
| `@
|
|
1157
|
-
| `@chat-adapter/telegram` | Required for Telegram messengers |
|
|
1112
|
+
| Package | Notes |
|
|
1113
|
+
| ------------------------ | --------------------------------------------------------- |
|
|
1114
|
+
| `agents` | Cloudflare Agents SDK peer dependency |
|
|
1115
|
+
| `ai` | Vercel AI SDK v7 peer dependency |
|
|
1116
|
+
| `zod` | Schema validation peer dependency |
|
|
1117
|
+
| `@cloudflare/computer` | Workspace and workspace tools peer dependency |
|
|
1118
|
+
| `@cloudflare/codemode` | Code execution, `createExecuteTool`, and JS skill scripts |
|
|
1119
|
+
| `@chat-adapter/telegram` | Required for Telegram messengers |
|
|
1158
1120
|
|
|
1159
1121
|
## Acknowledgments
|
|
1160
1122
|
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
o as ExtensionToolDescriptor,
|
|
6
6
|
r as ExtensionInfo,
|
|
7
7
|
t as ExtensionManager
|
|
8
|
-
} from "../manager-
|
|
8
|
+
} from "../manager-CtXhU1fK.js";
|
|
9
9
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
10
10
|
|
|
11
11
|
//#region src/extensions/host-bridge.d.ts
|
|
@@ -54,15 +54,14 @@ declare class HostBridgeLoopback extends WorkerEntrypoint<
|
|
|
54
54
|
* ContextProvider / WritableContextProvider interfaces.
|
|
55
55
|
*
|
|
56
56
|
* When an extension declares context blocks in its manifest, Think creates
|
|
57
|
-
* a bridge provider for each block. The bridge delegates `get()
|
|
58
|
-
*
|
|
57
|
+
* a bridge provider for each block. The bridge delegates `get()` and
|
|
58
|
+
* `set()` to the extension Worker's entrypoint via RPC.
|
|
59
59
|
*
|
|
60
60
|
* This allows extensions to contribute context blocks to the Session's
|
|
61
61
|
* system prompt without Session knowing about the extension system.
|
|
62
62
|
*/
|
|
63
63
|
interface ExtensionContextEntrypoint {
|
|
64
64
|
contextGet(label: string): Promise<string | null>;
|
|
65
|
-
contextLoad?(label: string, key: string): Promise<string | null>;
|
|
66
65
|
contextSet?(
|
|
67
66
|
label: string,
|
|
68
67
|
key: string,
|
|
@@ -88,20 +87,12 @@ declare class ExtensionContextBridge {
|
|
|
88
87
|
declare class ExtensionWritableBridge extends ExtensionContextBridge {
|
|
89
88
|
set(content: string): Promise<void>;
|
|
90
89
|
}
|
|
91
|
-
/**
|
|
92
|
-
* A skill provider backed by an extension Worker.
|
|
93
|
-
* Delegates `get()`, `load()`, and optionally `set()` to the extension's RPC methods.
|
|
94
|
-
*/
|
|
95
|
-
declare class ExtensionSkillBridge extends ExtensionContextBridge {
|
|
96
|
-
load(key: string): Promise<string | null>;
|
|
97
|
-
set(key: string, content: string, description?: string): Promise<void>;
|
|
98
|
-
}
|
|
99
90
|
/**
|
|
100
91
|
* Create the appropriate bridge provider based on the context block type.
|
|
101
92
|
*/
|
|
102
93
|
declare function createBridgeProvider(
|
|
103
94
|
label: string,
|
|
104
|
-
type: "readonly" | "writable" | "
|
|
95
|
+
type: "readonly" | "writable" | "searchable",
|
|
105
96
|
entrypoint: ExtensionContextEntrypoint
|
|
106
97
|
): ExtensionContextBridge;
|
|
107
98
|
//#endregion
|
|
@@ -112,7 +103,6 @@ export {
|
|
|
112
103
|
type ExtensionManagerOptions,
|
|
113
104
|
type ExtensionManifest,
|
|
114
105
|
type ExtensionPermissions,
|
|
115
|
-
ExtensionSkillBridge,
|
|
116
106
|
type ExtensionToolDescriptor,
|
|
117
107
|
ExtensionWritableBridge,
|
|
118
108
|
HostBridgeLoopback,
|
package/dist/extensions/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { r as _assertClassBrand } from "../classPrivateFieldSet2-DwGJfC3w.js";
|
|
2
|
-
import { r as _classPrivateMethodInitSpec, t as ExtensionManager } from "../manager-
|
|
2
|
+
import { r as _classPrivateMethodInitSpec, t as ExtensionManager } from "../manager-sfWIcYJT.js";
|
|
3
3
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
4
4
|
//#region src/extensions/host-bridge.ts
|
|
5
5
|
/**
|
|
@@ -122,28 +122,15 @@ var ExtensionWritableBridge = class extends ExtensionContextBridge {
|
|
|
122
122
|
}
|
|
123
123
|
};
|
|
124
124
|
/**
|
|
125
|
-
* A skill provider backed by an extension Worker.
|
|
126
|
-
* Delegates `get()`, `load()`, and optionally `set()` to the extension's RPC methods.
|
|
127
|
-
*/
|
|
128
|
-
var ExtensionSkillBridge = class extends ExtensionContextBridge {
|
|
129
|
-
async load(key) {
|
|
130
|
-
return await this._entrypoint.contextLoad?.(this._label, key) ?? null;
|
|
131
|
-
}
|
|
132
|
-
async set(key, content, description) {
|
|
133
|
-
await this._entrypoint.contextSet?.(this._label, key, content, description);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
125
|
* Create the appropriate bridge provider based on the context block type.
|
|
138
126
|
*/
|
|
139
127
|
function createBridgeProvider(label, type, entrypoint) {
|
|
140
128
|
switch (type) {
|
|
141
|
-
case "skill": return new ExtensionSkillBridge(label, entrypoint);
|
|
142
129
|
case "writable": return new ExtensionWritableBridge(label, entrypoint);
|
|
143
130
|
default: return new ExtensionContextBridge(label, entrypoint);
|
|
144
131
|
}
|
|
145
132
|
}
|
|
146
133
|
//#endregion
|
|
147
|
-
export { ExtensionContextBridge, ExtensionManager,
|
|
134
|
+
export { ExtensionContextBridge, ExtensionManager, ExtensionWritableBridge, HostBridgeLoopback, createBridgeProvider };
|
|
148
135
|
|
|
149
136
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/extensions/host-bridge.ts","../../src/extensions/bridge-provider.ts"],"sourcesContent":["/**\n * HostBridgeLoopback — a WorkerEntrypoint that provides controlled workspace\n * access to extension Workers loaded via WorkerLoader.\n *\n * This is a loopback: the extension worker's `env.host` binding points here,\n * and each method call resolves the parent agent via `ctx.exports`, then\n * delegates to the agent's workspace proxy methods (`_hostReadFile`, etc.).\n *\n * Props carry serializable identifiers (agent class name, agent ID, and\n * permissions) so the binding survives across requests and hibernation.\n *\n * Users must re-export this class from their worker entry point:\n *\n * ```typescript\n * export { HostBridgeLoopback } from \"@cloudflare/think/extensions\";\n * ```\n *\n * @experimental The API surface may change before stabilizing.\n */\n\nimport { WorkerEntrypoint } from \"cloudflare:workers\";\nimport type { ExtensionPermissions } from \"./types\";\n\nexport type HostBridgeLoopbackProps = {\n agentClassName: string;\n agentId: string;\n permissions: ExtensionPermissions;\n /** Namespaced context labels this extension declared (for \"own\" write validation). */\n ownContextLabels?: string[];\n};\n\nexport class HostBridgeLoopback extends WorkerEntrypoint<\n Record<string, unknown>,\n HostBridgeLoopbackProps\n> {\n private _permissions = this.ctx.props.permissions;\n\n private _getAgent() {\n const { agentClassName, agentId } = this.ctx.props;\n // @ts-expect-error — experimental: ctx.exports on WorkerEntrypoint\n const ns = this.ctx.exports[agentClassName] as DurableObjectNamespace;\n return ns.get(ns.idFromString(agentId));\n }\n\n // ── Permission checks ──────────────────────────────────────────\n\n #requireWorkspace(level: \"read\" | \"read-write\"): void {\n const ws = this._permissions.workspace ?? \"none\";\n if (ws === \"none\") {\n throw new Error(\"Extension error: no workspace permission declared\");\n }\n if (level === \"read-write\" && ws !== \"read-write\") {\n throw new Error(\n \"Extension error: workspace write permission required, but only read granted\"\n );\n }\n }\n\n #requireContextRead(label: string): void {\n const ctx = this._permissions.context;\n if (!ctx?.read) {\n throw new Error(\"Extension error: no context read permission declared\");\n }\n if (ctx.read !== \"all\" && !ctx.read.includes(label)) {\n throw new Error(\n `Extension error: no read permission for context label \"${label}\"`\n );\n }\n }\n\n #requireContextWrite(label: string): void {\n const ctx = this._permissions.context;\n if (!ctx?.write) {\n throw new Error(\"Extension error: no context write permission declared\");\n }\n if (ctx.write === \"own\") {\n const owned = this.ctx.props.ownContextLabels ?? [];\n if (!owned.includes(label)) {\n throw new Error(\n `Extension error: label \"${label}\" is not owned by this extension`\n );\n }\n } else if (!ctx.write.includes(label)) {\n throw new Error(\n `Extension error: no write permission for context label \"${label}\"`\n );\n }\n }\n\n #requireMessages(): void {\n if (this._permissions.messages !== \"read\") {\n throw new Error(\"Extension error: no messages read permission declared\");\n }\n }\n\n #requireSendMessage(): void {\n if (!this._permissions.session?.sendMessage) {\n throw new Error(\n \"Extension error: no session.sendMessage permission declared\"\n );\n }\n }\n\n #requireSessionMetadata(): void {\n if (!this._permissions.session?.metadata) {\n throw new Error(\n \"Extension error: no session.metadata permission declared\"\n );\n }\n }\n\n // ── Workspace (existing) ───────────────────────────────────────\n\n async readFile(path: string): Promise<string | null> {\n this.#requireWorkspace(\"read\");\n return (\n this._getAgent() as unknown as {\n _hostReadFile(path: string): Promise<string | null>;\n }\n )._hostReadFile(path);\n }\n\n async writeFile(path: string, content: string): Promise<void> {\n this.#requireWorkspace(\"read-write\");\n return (\n this._getAgent() as unknown as {\n _hostWriteFile(path: string, content: string): Promise<void>;\n }\n )._hostWriteFile(path, content);\n }\n\n async deleteFile(path: string): Promise<boolean> {\n this.#requireWorkspace(\"read-write\");\n return (\n this._getAgent() as unknown as {\n _hostDeleteFile(path: string): Promise<boolean>;\n }\n )._hostDeleteFile(path);\n }\n\n async listFiles(\n dir: string\n ): Promise<\n Array<{ name: string; type: string; size: number; path: string }>\n > {\n this.#requireWorkspace(\"read\");\n return (\n this._getAgent() as unknown as {\n _hostListFiles(\n dir: string\n ): Promise<\n Array<{ name: string; type: string; size: number; path: string }>\n >;\n }\n )._hostListFiles(dir);\n }\n\n // ── Context blocks (new) ───────────────────────────────────────\n\n async getContext(label: string): Promise<string | null> {\n this.#requireContextRead(label);\n return (\n this._getAgent() as unknown as {\n _hostGetContext(label: string): Promise<string | null>;\n }\n )._hostGetContext(label);\n }\n\n async setContext(label: string, content: string): Promise<void> {\n this.#requireContextWrite(label);\n return (\n this._getAgent() as unknown as {\n _hostSetContext(label: string, content: string): Promise<void>;\n }\n )._hostSetContext(label, content);\n }\n\n // ── Messages (new) ────────────────────────────────────────────\n\n async getMessages(\n limit?: number\n ): Promise<Array<{ id: string; role: string; content: string }>> {\n this.#requireMessages();\n return (\n this._getAgent() as unknown as {\n _hostGetMessages(\n limit?: number\n ): Promise<Array<{ id: string; role: string; content: string }>>;\n }\n )._hostGetMessages(limit);\n }\n\n async sendMessage(content: string): Promise<void> {\n this.#requireSendMessage();\n return (\n this._getAgent() as unknown as {\n _hostSendMessage(content: string): Promise<void>;\n }\n )._hostSendMessage(content);\n }\n\n // ── Session metadata (new) ────────────────────────────────────\n\n async getSessionInfo(): Promise<{ messageCount: number }> {\n this.#requireSessionMetadata();\n return (\n this._getAgent() as unknown as {\n _hostGetSessionInfo(): Promise<{ messageCount: number }>;\n }\n )._hostGetSessionInfo();\n }\n}\n","/**\n * Bridge providers that adapt extension Worker RPC into Session's\n * ContextProvider / WritableContextProvider interfaces.\n *\n * When an extension declares context blocks in its manifest, Think creates\n * a bridge provider for each block. The bridge delegates `get()`, `load()`,\n * and `set()` to the extension Worker's entrypoint via RPC.\n *\n * This allows extensions to contribute context blocks to the Session's\n * system prompt without Session knowing about the extension system.\n */\n\ninterface ExtensionContextEntrypoint {\n contextGet(label: string): Promise<string | null>;\n contextLoad?(label: string, key: string): Promise<string | null>;\n contextSet?(\n label: string,\n key: string,\n content: string,\n description?: string\n ): Promise<void>;\n}\n\n/**\n * A readonly context provider backed by an extension Worker.\n * Delegates `get()` to the extension's `contextGet` RPC method.\n */\nexport class ExtensionContextBridge {\n protected _label: string;\n protected _entrypoint: ExtensionContextEntrypoint;\n\n constructor(label: string, entrypoint: ExtensionContextEntrypoint) {\n this._label = label;\n this._entrypoint = entrypoint;\n }\n\n init(_label: string): void {}\n\n async get(): Promise<string | null> {\n return this._entrypoint.contextGet(this._label);\n }\n}\n\n/**\n * A writable context provider backed by an extension Worker.\n * Delegates `get()` and `set()` to the extension's RPC methods.\n */\nexport class ExtensionWritableBridge extends ExtensionContextBridge {\n async set(content: string): Promise<void> {\n await this._entrypoint.contextSet?.(this._label, \"\", content);\n }\n}\n\n/**\n * A skill provider backed by an extension Worker.\n * Delegates `get()`, `load()`, and optionally `set()` to the extension's RPC methods.\n */\nexport class ExtensionSkillBridge extends ExtensionContextBridge {\n async load(key: string): Promise<string | null> {\n return (await this._entrypoint.contextLoad?.(this._label, key)) ?? null;\n }\n\n async set(key: string, content: string, description?: string): Promise<void> {\n await this._entrypoint.contextSet?.(this._label, key, content, description);\n }\n}\n\n/**\n * Create the appropriate bridge provider based on the context block type.\n */\nexport function createBridgeProvider(\n label: string,\n type: \"readonly\" | \"writable\" | \"skill\" | \"searchable\",\n entrypoint: ExtensionContextEntrypoint\n) {\n switch (type) {\n case \"skill\":\n return new ExtensionSkillBridge(label, entrypoint);\n case \"writable\":\n return new ExtensionWritableBridge(label, entrypoint);\n case \"readonly\":\n case \"searchable\":\n default:\n return new ExtensionContextBridge(label, entrypoint);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,qBAAb,cAAwC,iBAGtC;;;;EACA,KAAQ,eAAe,KAAK,IAAI,MAAM;;CAEtC,YAAoB;EAClB,MAAM,EAAE,gBAAgB,YAAY,KAAK,IAAI;EAE7C,MAAM,KAAK,KAAK,IAAI,QAAQ;EAC5B,OAAO,GAAG,IAAI,GAAG,aAAa,OAAO,CAAC;CACxC;CAuEA,MAAM,SAAS,MAAsC;EACnD,kBAAA,2BAAA,MAAA,iBAAA,CAAA,CAAA,KAAA,MAAuB,MAAM;EAC7B,OACE,KAAK,UAAU,CAAC,CAGhB,cAAc,IAAI;CACtB;CAEA,MAAM,UAAU,MAAc,SAAgC;EAC5D,kBAAA,2BAAA,MAAA,iBAAA,CAAA,CAAA,KAAA,MAAuB,YAAY;EACnC,OACE,KAA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AA0C,uBAAuB;CAC/D,MAAM,KAAK,KAAqC;EAC9C,OAAQ,MAAM,KAAK,YAAY,cAAc,KAAK,QAAQ,GAAG,KAAM;CACrE;CAEA,MAAM,IAAI,KAAa,SAAiB,aAAqC;EAC3E,MAAM,KAAK,YAAY,aAAa,KAAK,QAAQ,KAAK,SAAS,WAAW;CAC5E;AACF;;;;AAKA,SAAgB,qBACd,OACA,MACA,YACA;CACA,QAAQ,MAAR;EACE,KAAK,SACH,OAAO,IAAI,qBAAqB,OAAO,UAAU;EACnD,KAAK,YACH,OAAO,IAAI,wBAAwB,OAAO,UAAU;EAGtD,SACE,OAAO,IAAI,uBAAuB,OAAO,UAAU;CACvD;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/extensions/host-bridge.ts","../../src/extensions/bridge-provider.ts"],"sourcesContent":["/**\n * HostBridgeLoopback — a WorkerEntrypoint that provides controlled workspace\n * access to extension Workers loaded via WorkerLoader.\n *\n * This is a loopback: the extension worker's `env.host` binding points here,\n * and each method call resolves the parent agent via `ctx.exports`, then\n * delegates to the agent's workspace proxy methods (`_hostReadFile`, etc.).\n *\n * Props carry serializable identifiers (agent class name, agent ID, and\n * permissions) so the binding survives across requests and hibernation.\n *\n * Users must re-export this class from their worker entry point:\n *\n * ```typescript\n * export { HostBridgeLoopback } from \"@cloudflare/think/extensions\";\n * ```\n *\n * @experimental The API surface may change before stabilizing.\n */\n\nimport { WorkerEntrypoint } from \"cloudflare:workers\";\nimport type { ExtensionPermissions } from \"./types\";\n\nexport type HostBridgeLoopbackProps = {\n agentClassName: string;\n agentId: string;\n permissions: ExtensionPermissions;\n /** Namespaced context labels this extension declared (for \"own\" write validation). */\n ownContextLabels?: string[];\n};\n\nexport class HostBridgeLoopback extends WorkerEntrypoint<\n Record<string, unknown>,\n HostBridgeLoopbackProps\n> {\n private _permissions = this.ctx.props.permissions;\n\n private _getAgent() {\n const { agentClassName, agentId } = this.ctx.props;\n // @ts-expect-error — experimental: ctx.exports on WorkerEntrypoint\n const ns = this.ctx.exports[agentClassName] as DurableObjectNamespace;\n return ns.get(ns.idFromString(agentId));\n }\n\n // ── Permission checks ──────────────────────────────────────────\n\n #requireWorkspace(level: \"read\" | \"read-write\"): void {\n const ws = this._permissions.workspace ?? \"none\";\n if (ws === \"none\") {\n throw new Error(\"Extension error: no workspace permission declared\");\n }\n if (level === \"read-write\" && ws !== \"read-write\") {\n throw new Error(\n \"Extension error: workspace write permission required, but only read granted\"\n );\n }\n }\n\n #requireContextRead(label: string): void {\n const ctx = this._permissions.context;\n if (!ctx?.read) {\n throw new Error(\"Extension error: no context read permission declared\");\n }\n if (ctx.read !== \"all\" && !ctx.read.includes(label)) {\n throw new Error(\n `Extension error: no read permission for context label \"${label}\"`\n );\n }\n }\n\n #requireContextWrite(label: string): void {\n const ctx = this._permissions.context;\n if (!ctx?.write) {\n throw new Error(\"Extension error: no context write permission declared\");\n }\n if (ctx.write === \"own\") {\n const owned = this.ctx.props.ownContextLabels ?? [];\n if (!owned.includes(label)) {\n throw new Error(\n `Extension error: label \"${label}\" is not owned by this extension`\n );\n }\n } else if (!ctx.write.includes(label)) {\n throw new Error(\n `Extension error: no write permission for context label \"${label}\"`\n );\n }\n }\n\n #requireMessages(): void {\n if (this._permissions.messages !== \"read\") {\n throw new Error(\"Extension error: no messages read permission declared\");\n }\n }\n\n #requireSendMessage(): void {\n if (!this._permissions.session?.sendMessage) {\n throw new Error(\n \"Extension error: no session.sendMessage permission declared\"\n );\n }\n }\n\n #requireSessionMetadata(): void {\n if (!this._permissions.session?.metadata) {\n throw new Error(\n \"Extension error: no session.metadata permission declared\"\n );\n }\n }\n\n // ── Workspace (existing) ───────────────────────────────────────\n\n async readFile(path: string): Promise<string | null> {\n this.#requireWorkspace(\"read\");\n return (\n this._getAgent() as unknown as {\n _hostReadFile(path: string): Promise<string | null>;\n }\n )._hostReadFile(path);\n }\n\n async writeFile(path: string, content: string): Promise<void> {\n this.#requireWorkspace(\"read-write\");\n return (\n this._getAgent() as unknown as {\n _hostWriteFile(path: string, content: string): Promise<void>;\n }\n )._hostWriteFile(path, content);\n }\n\n async deleteFile(path: string): Promise<boolean> {\n this.#requireWorkspace(\"read-write\");\n return (\n this._getAgent() as unknown as {\n _hostDeleteFile(path: string): Promise<boolean>;\n }\n )._hostDeleteFile(path);\n }\n\n async listFiles(\n dir: string\n ): Promise<\n Array<{ name: string; type: string; size: number; path: string }>\n > {\n this.#requireWorkspace(\"read\");\n return (\n this._getAgent() as unknown as {\n _hostListFiles(\n dir: string\n ): Promise<\n Array<{ name: string; type: string; size: number; path: string }>\n >;\n }\n )._hostListFiles(dir);\n }\n\n // ── Context blocks (new) ───────────────────────────────────────\n\n async getContext(label: string): Promise<string | null> {\n this.#requireContextRead(label);\n return (\n this._getAgent() as unknown as {\n _hostGetContext(label: string): Promise<string | null>;\n }\n )._hostGetContext(label);\n }\n\n async setContext(label: string, content: string): Promise<void> {\n this.#requireContextWrite(label);\n return (\n this._getAgent() as unknown as {\n _hostSetContext(label: string, content: string): Promise<void>;\n }\n )._hostSetContext(label, content);\n }\n\n // ── Messages (new) ────────────────────────────────────────────\n\n async getMessages(\n limit?: number\n ): Promise<Array<{ id: string; role: string; content: string }>> {\n this.#requireMessages();\n return (\n this._getAgent() as unknown as {\n _hostGetMessages(\n limit?: number\n ): Promise<Array<{ id: string; role: string; content: string }>>;\n }\n )._hostGetMessages(limit);\n }\n\n async sendMessage(content: string): Promise<void> {\n this.#requireSendMessage();\n return (\n this._getAgent() as unknown as {\n _hostSendMessage(content: string): Promise<void>;\n }\n )._hostSendMessage(content);\n }\n\n // ── Session metadata (new) ────────────────────────────────────\n\n async getSessionInfo(): Promise<{ messageCount: number }> {\n this.#requireSessionMetadata();\n return (\n this._getAgent() as unknown as {\n _hostGetSessionInfo(): Promise<{ messageCount: number }>;\n }\n )._hostGetSessionInfo();\n }\n}\n","/**\n * Bridge providers that adapt extension Worker RPC into Session's\n * ContextProvider / WritableContextProvider interfaces.\n *\n * When an extension declares context blocks in its manifest, Think creates\n * a bridge provider for each block. The bridge delegates `get()` and\n * `set()` to the extension Worker's entrypoint via RPC.\n *\n * This allows extensions to contribute context blocks to the Session's\n * system prompt without Session knowing about the extension system.\n */\n\ninterface ExtensionContextEntrypoint {\n contextGet(label: string): Promise<string | null>;\n contextSet?(\n label: string,\n key: string,\n content: string,\n description?: string\n ): Promise<void>;\n}\n\n/**\n * A readonly context provider backed by an extension Worker.\n * Delegates `get()` to the extension's `contextGet` RPC method.\n */\nexport class ExtensionContextBridge {\n protected _label: string;\n protected _entrypoint: ExtensionContextEntrypoint;\n\n constructor(label: string, entrypoint: ExtensionContextEntrypoint) {\n this._label = label;\n this._entrypoint = entrypoint;\n }\n\n init(_label: string): void {}\n\n async get(): Promise<string | null> {\n return this._entrypoint.contextGet(this._label);\n }\n}\n\n/**\n * A writable context provider backed by an extension Worker.\n * Delegates `get()` and `set()` to the extension's RPC methods.\n */\nexport class ExtensionWritableBridge extends ExtensionContextBridge {\n async set(content: string): Promise<void> {\n await this._entrypoint.contextSet?.(this._label, \"\", content);\n }\n}\n\n/**\n * Create the appropriate bridge provider based on the context block type.\n */\nexport function createBridgeProvider(\n label: string,\n type: \"readonly\" | \"writable\" | \"searchable\",\n entrypoint: ExtensionContextEntrypoint\n) {\n switch (type) {\n case \"writable\":\n return new ExtensionWritableBridge(label, entrypoint);\n case \"readonly\":\n case \"searchable\":\n default:\n return new ExtensionContextBridge(label, entrypoint);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA+BA,IAAa,qBAAb,cAAwC,iBAGtC;;;;EACA,KAAQ,eAAe,KAAK,IAAI,MAAM;;CAEtC,YAAoB;EAClB,MAAM,EAAE,gBAAgB,YAAY,KAAK,IAAI;EAE7C,MAAM,KAAK,KAAK,IAAI,QAAQ;EAC5B,OAAO,GAAG,IAAI,GAAG,aAAa,OAAO,CAAC;CACxC;CAuEA,MAAM,SAAS,MAAsC;EACnD,kBAAA,2BAAA,MAAA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