@cloudflare/think 0.21.0 → 0.23.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 +71 -120
- package/dist/extensions/index.d.ts +4 -14
- package/dist/extensions/index.js +1 -14
- package/dist/extensions/index.js.map +1 -1
- package/dist/{fetch-C7s1mpgg.d.ts → fetch-CU2fC7O6.d.ts} +1 -30
- package/dist/{fetch-9f1p8UrI.js → fetch-DO3uQ9py.js} +2 -47
- package/dist/fetch-DO3uQ9py.js.map +1 -0
- package/dist/{index-Cjq_e9F2.d.ts → index-DDnny6kt.d.ts} +189 -73
- package/dist/{manager-dOcdCX4u.d.ts → manager-rze95KCk.d.ts} +1 -2
- 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 +3 -3
- package/dist/think.js +85 -72
- package/dist/think.js.map +1 -1
- package/dist/tools/extensions.d.ts +1 -1
- package/dist/tools/fetch.d.ts +1 -1
- package/dist/tools/fetch.js +1 -1
- package/dist/workflows.d.ts +1 -1
- package/docs/channels.md +1 -5
- package/docs/getting-started.md +1 -1
- package/docs/index.md +98 -94
- package/docs/lifecycle-hooks.md +14 -16
- package/docs/multi-chat.md +12 -13
- package/docs/tools.md +2 -19
- package/package.json +7 -8
- package/dist/fetch-9f1p8UrI.js.map +0 -1
package/dist/tools/fetch.d.ts
CHANGED
package/dist/tools/fetch.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as createFetchTools } from "../fetch-
|
|
1
|
+
import { t as createFetchTools } from "../fetch-DO3uQ9py.js";
|
|
2
2
|
export { createFetchTools };
|
package/dist/workflows.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Tn as ThinkSubmissionStatus, gn as Think } from "./index-DDnny6kt.js";
|
|
2
2
|
import { ZodObject, z } from "zod";
|
|
3
3
|
import { WorkflowEvent, WorkflowSleepDuration } from "cloudflare:workers";
|
|
4
4
|
import {
|
package/docs/channels.md
CHANGED
|
@@ -42,11 +42,7 @@ export class Assistant extends Think<Env> {
|
|
|
42
42
|
maxTurns: 3
|
|
43
43
|
},
|
|
44
44
|
// A messenger channel (Chat SDK webhook).
|
|
45
|
-
telegram: messengerChannel(
|
|
46
|
-
telegram({
|
|
47
|
-
/* adapter config */
|
|
48
|
-
})
|
|
49
|
-
)
|
|
45
|
+
telegram: messengerChannel(telegram({/* adapter config */}))
|
|
50
46
|
};
|
|
51
47
|
}
|
|
52
48
|
}
|
package/docs/getting-started.md
CHANGED
|
@@ -231,7 +231,7 @@ export class MyAgent extends Think<Env> {
|
|
|
231
231
|
|
|
232
232
|
Now the model sees a `MEMORY` section in its system prompt and gets a `set_context` tool to update it. A block declared without a `provider` is auto-wired to durable per-agent SQLite, so facts written to memory survive DO hibernation and restarts.
|
|
233
233
|
|
|
234
|
-
When you declare context blocks, the system prompt is built from those blocks
|
|
234
|
+
When you declare context blocks, the system prompt is built from those blocks, with an overridden `getSystemPrompt()` at the top. The `"soul"` block above acts as the system identity: it is read-only and always appears first. The `"memory"` block is writable, and the model updates it when it learns something useful. The frozen system prompt is always persisted, so a cold wake reuses the exact prompt string the model already cached.
|
|
235
235
|
|
|
236
236
|
The assembled blocks are available as `this.context` once the Lifecycle has started. See the [Context documentation](https://github.com/cloudflare/agents/blob/main/docs/agents/context.md) for providers, tools, and frozen-prompt behavior, and the [Sessions documentation](https://github.com/cloudflare/agents/blob/main/docs/agents/sessions.md) for compaction, search, and multi-session support.
|
|
237
237
|
|
package/docs/index.md
CHANGED
|
@@ -397,7 +397,7 @@ path.
|
|
|
397
397
|
| `getModel()` | throws | Return a model id `string` (resolved via the bundled `workers-ai-provider` off `getAIBinding()` — a `@cf/...` id hits Workers AI, a `"provider/model"` slug routes through AI Gateway) or a `LanguageModel` |
|
|
398
398
|
| `getGateway(model)` | `undefined` | AI Gateway options (`id`, `metadata`, cache settings) for a string model — see [AI Gateway options](#ai-gateway-options) |
|
|
399
399
|
| `getAIBinding()` | `this.env.AI` | Workers AI binding used to resolve string models from `getModel()` |
|
|
400
|
-
| `getSystemPrompt()` | `"You are a helpful assistant."` |
|
|
400
|
+
| `getSystemPrompt()` | `"You are a helpful assistant."` | Instructions at the top of the system prompt, above context blocks. The default is used only with no context blocks |
|
|
401
401
|
| `getTools()` | `{}` | AI SDK `ToolSet` for the agentic loop |
|
|
402
402
|
| `getScheduledTasks()` | `{}` | Code-declared recurring prompts or handlers |
|
|
403
403
|
| `getScheduledTasksScope()` | `"root"` | Which instances arm the declared tasks — `"root"` (top-level agent only) or `"all"` (sub-agents too) |
|
|
@@ -414,7 +414,6 @@ path.
|
|
|
414
414
|
| `hydrationByteBudget` | 32 MiB | Byte budget for startup transcript hydration. Charges each row its stored bytes plus the attachment bytes it re-inflates |
|
|
415
415
|
| `mediaEviction` | `true` | Media eviction policy: aged media leaves the conversation and is preserved as a Workspace file. `false` keeps aged media in the conversation |
|
|
416
416
|
| `getSkills()` | `[]` | Return Agent Skills sources for on-demand skill activation |
|
|
417
|
-
| `skillWorkspace` | `false` | Project skills into the Workspace as files; `{}` enables with defaults |
|
|
418
417
|
| `getSkillScriptRunner()` | `null` | Enable the optional `run_skill_script` tool |
|
|
419
418
|
| `fetchTools` | `false` | Opt-in allowlisted, read-only HTTP fetch tools (`fetch_url` + per-binding `fetch_<name>`). Set to a config object; see [Fetch tool](#fetch-tool) |
|
|
420
419
|
| `messageConcurrency` | `"queue"` | How overlapping submits behave — see [Client Tools](./client-tools.md) |
|
|
@@ -472,84 +471,72 @@ See [Tools](./tools.md#mcp-tools) for MCP setup and alternative exposure paths.
|
|
|
472
471
|
|
|
473
472
|
## Agent Skills
|
|
474
473
|
|
|
475
|
-
Think supports [Agent Skills](https://agentskills.io/)
|
|
476
|
-
instructions. A skill
|
|
477
|
-
Think adds
|
|
478
|
-
when a
|
|
474
|
+
Think supports [Agent Skills](https://agentskills.io/): on-demand
|
|
475
|
+
instructions, references, and scripts. A skill is a directory with a
|
|
476
|
+
`SKILL.md` file. Think adds each skill's name and description to the system
|
|
477
|
+
prompt and gives the model tools to load a skill when a task matches it.
|
|
479
478
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
```typescript
|
|
484
|
-
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
485
|
-
import agents from "agents/vite";
|
|
486
|
-
import { defineConfig } from "vite";
|
|
487
|
-
|
|
488
|
-
export default defineConfig({
|
|
489
|
-
plugins: [agents(), cloudflare()]
|
|
490
|
-
});
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
Then import the bundled skill manifest:
|
|
479
|
+
The simplest place to keep skills is the agent's Workspace. Every
|
|
480
|
+
`/workspace/.agents/skills/<name>/SKILL.md` is a skill, and the files beside it
|
|
481
|
+
are its resources:
|
|
494
482
|
|
|
495
483
|
```typescript
|
|
496
484
|
import { Think, skills } from "@cloudflare/think";
|
|
497
|
-
import bundledSkills from "agents:skills"; // resolves to ./skills next to this file
|
|
498
485
|
|
|
499
486
|
export class MyAgent extends Think<Env> {
|
|
500
487
|
getSkills() {
|
|
501
|
-
return [
|
|
502
|
-
bundledSkills,
|
|
503
|
-
skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })
|
|
504
|
-
];
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
getSkillScriptRunner() {
|
|
508
|
-
return skills.runner({
|
|
509
|
-
loader: this.env.LOADER,
|
|
510
|
-
workspaceInstance: this.workspace
|
|
511
|
-
});
|
|
488
|
+
return [skills.workspace(this.workspace)];
|
|
512
489
|
}
|
|
513
490
|
}
|
|
514
491
|
```
|
|
515
492
|
|
|
516
|
-
`agents:skills` resolves to a `./skills` directory next to the importing file;
|
|
517
|
-
use `agents:skills/<dir>` to point at a differently named sibling directory.
|
|
518
|
-
The `agents:skills` import is typed by ambient declarations that ship with
|
|
519
|
-
`agents`, so importing `Think` in the same file brings the type into scope (for
|
|
520
|
-
a file that imports only the specifier, add
|
|
521
|
-
`/// <reference types="agents/skills-module" />`). If you are not using the
|
|
522
|
-
Agents Vite plugin, build a source with `skills.fromManifest(...)` instead.
|
|
523
|
-
|
|
524
|
-
The skills engine lives in `agents/skills` and is framework-agnostic, so any
|
|
525
|
-
agent (including a plain `@cloudflare/ai-chat` `onChatMessage`) can build a
|
|
526
|
-
`SkillRegistry`; `@cloudflare/think` re-exports it as `skills` and wires
|
|
527
|
-
`getSkills()` into the turn automatically.
|
|
528
|
-
|
|
529
|
-
Think can also project each resolved `SKILL.md` into the active workspace so
|
|
530
|
-
the agent can read and edit skills as files. This is off by default: skills
|
|
531
|
-
load from their sources and nothing is written to the Workspace. Set
|
|
532
|
-
`skillWorkspace = {}` to enable it. Skills land under `/workspace/.agents/skills`.
|
|
533
|
-
Existing workspace edits are preserved and
|
|
534
|
-
affect later activation. Resources copy into the workspace when first requested
|
|
535
|
-
rather than at startup. A durable source fingerprint lets unchanged cold wakes
|
|
536
|
-
attach to the existing projection without statting or rewriting every file.
|
|
537
|
-
|
|
538
|
-
Set `skillWorkspace = { root: "/some/other/dir" }` to project skills somewhere
|
|
539
|
-
else.
|
|
540
|
-
|
|
541
|
-
Sources are applied in order; the first source to register a skill name wins,
|
|
542
|
-
and later duplicates (or a source that fails to load) are skipped with a logged
|
|
543
|
-
warning rather than failing the agent.
|
|
544
|
-
|
|
545
|
-
The imported directory should contain one child directory per skill:
|
|
546
|
-
|
|
547
493
|
```text
|
|
548
|
-
agents/
|
|
549
|
-
agents/
|
|
550
|
-
agents/
|
|
494
|
+
/workspace/.agents/skills/release-notes/SKILL.md
|
|
495
|
+
/workspace/.agents/skills/release-notes/scripts/format-release-notes.js
|
|
496
|
+
/workspace/.agents/skills/release-notes/references/style-guide.md
|
|
551
497
|
```
|
|
552
498
|
|
|
499
|
+
Think lists every source before each turn, so a skill written, edited, or
|
|
500
|
+
deleted in the Workspace (by you, a user, or the agent itself) takes effect on
|
|
501
|
+
the next turn. Pass `{ root }` to read from another directory. Skill scripts
|
|
502
|
+
are Workspace files too: give the Workspace a backend and the model runs them
|
|
503
|
+
with the `exec` tool.
|
|
504
|
+
|
|
505
|
+
Other sources:
|
|
506
|
+
|
|
507
|
+
| Source | Reads skills from |
|
|
508
|
+
| --------------------------------------- | ------------------------------------------------------ |
|
|
509
|
+
| `skills.workspace(workspace, { root })` | A `@cloudflare/computer` Workspace |
|
|
510
|
+
| `skills.r2(bucket, { prefix })` | An R2 bucket with the same layout under `prefix` |
|
|
511
|
+
| `skills.manifest({ id, skills })` | Skills defined in code, for tests and small fixed sets |
|
|
512
|
+
|
|
513
|
+
Sources are applied in order. The first source to provide a skill name wins,
|
|
514
|
+
and later duplicates are skipped with a logged warning. `skills.r2()` reuses
|
|
515
|
+
a bucket listing for 60 seconds by default; set `refreshIntervalMs` to change
|
|
516
|
+
that.
|
|
517
|
+
|
|
518
|
+
Every `SKILL.md` is checked against the
|
|
519
|
+
[spec](https://agentskills.io/specification) and loaded the way the spec's
|
|
520
|
+
[guide for agents](https://agentskills.io/client-implementation/adding-skills-support)
|
|
521
|
+
recommends. A skill that breaks a rule, such as a `name` that is not 1-64
|
|
522
|
+
lowercase letters, digits, and hyphens, does not match its directory, or a
|
|
523
|
+
`description` over 1024 characters, still loads, and Think logs a warning for
|
|
524
|
+
each broken rule. A skill with no `description`, a `name` that is not a safe
|
|
525
|
+
single path segment, or frontmatter that is not YAML is skipped and logged.
|
|
526
|
+
Frontmatter whose values contain an unquoted `: ` is read again with those
|
|
527
|
+
values quoted. To check a skill before you write it, parse it with
|
|
528
|
+
`skills.parseSkill(markdown, { directory })` and require `warnings` to be
|
|
529
|
+
empty. Frontmatter keys the spec does not define are kept in
|
|
530
|
+
`skill.extraFields`. A skill with `disable-model-invocation: true` is not
|
|
531
|
+
shown to the model.
|
|
532
|
+
|
|
533
|
+
The parser, sources, and catalog live in `agents/skills` and work without
|
|
534
|
+
Think: `skills.parseSkill()` parses a `SKILL.md`, `skills.loadSkillCatalog()`
|
|
535
|
+
merges sources, and `createSkillTools()` from `agents/skills/tools/ai-sdk`
|
|
536
|
+
builds the AI SDK tools.
|
|
537
|
+
`@cloudflare/think` re-exports the module as `skills` and wires `getSkills()`
|
|
538
|
+
into every turn.
|
|
539
|
+
|
|
553
540
|
When skills are available, Think exposes:
|
|
554
541
|
|
|
555
542
|
| Tool | Purpose |
|
|
@@ -563,13 +550,23 @@ Skills are not always-on system prompt text. Use a context block from
|
|
|
563
550
|
for task-specific procedures, references, scripts, templates, and assets that
|
|
564
551
|
should be loaded only when relevant.
|
|
565
552
|
|
|
566
|
-
Skills register a context block of their own
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
553
|
+
Skills register a context block of their own. An overridden `getSystemPrompt()`
|
|
554
|
+
stays at the top of the system prompt, above every context block, so adding
|
|
555
|
+
skills never removes it.
|
|
556
|
+
|
|
557
|
+
The skills block is a `whenChanged: "remind"` block. When a skill is added,
|
|
558
|
+
edited, or removed mid-conversation, the frozen system prompt stays the same,
|
|
559
|
+
so the model provider's prompt cache stays warm. The new catalog goes to the
|
|
560
|
+
model as a reminder after the cached prefix on the next turn, and Think folds
|
|
561
|
+
it into the system prompt at the next rebuild: compaction, or clearing the
|
|
562
|
+
history.
|
|
571
563
|
|
|
572
|
-
|
|
564
|
+
### Skill script runner
|
|
565
|
+
|
|
566
|
+
For skills that live outside a Workspace, such as in R2, `skills.runner()` runs
|
|
567
|
+
bundled scripts through `run_skill_script`. It is experimental; prefer keeping
|
|
568
|
+
skills in a Workspace and running scripts with `exec`. It needs a Worker Loader
|
|
569
|
+
binding:
|
|
573
570
|
|
|
574
571
|
```jsonc
|
|
575
572
|
{
|
|
@@ -577,17 +574,25 @@ Script execution is opt-in and requires a Worker Loader binding:
|
|
|
577
574
|
}
|
|
578
575
|
```
|
|
579
576
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
577
|
+
```typescript
|
|
578
|
+
getSkillScriptRunner() {
|
|
579
|
+
return skills.runner({ loader: this.env.LOADER });
|
|
580
|
+
}
|
|
581
|
+
```
|
|
584
582
|
|
|
585
|
-
|
|
583
|
+
Outside Think, import it from `agents/skills/runner`. It is a separate entry
|
|
584
|
+
point because it loads `just-bash` and `@cloudflare/codemode`.
|
|
586
585
|
|
|
587
|
-
|
|
588
|
-
|
|
586
|
+
The runner runs JavaScript, Python, and Bash scripts under `scripts/`. Python
|
|
587
|
+
runs as Python Dynamic Workers and Bash runs through `just-bash`. There is no
|
|
588
|
+
in-Worker bundler: a JavaScript script must be one self-contained module, so
|
|
589
|
+
compile TypeScript or multi-file scripts (for example with esbuild) before
|
|
590
|
+
storing the skill.
|
|
591
|
+
|
|
592
|
+
JavaScript scripts are function-style:
|
|
589
593
|
|
|
590
|
-
|
|
594
|
+
```javascript
|
|
595
|
+
export default async function run(input, ctx) {
|
|
591
596
|
const guide = ctx.files["references/style-guide.md"]; // bundled text resources
|
|
592
597
|
const docs = await ctx.workspace.readFile("README.md"); // gated by permission
|
|
593
598
|
const summary = await ctx.tools.call("summarize", { input }); // explicit tools
|
|
@@ -596,13 +601,13 @@ export default async function run(input: unknown, ctx: SkillRunContext) {
|
|
|
596
601
|
}
|
|
597
602
|
```
|
|
598
603
|
|
|
599
|
-
`ctx` is `{ skill, files, workspace, tools, output }
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
`ctx.output.writeFile(name, content)` returns
|
|
603
|
-
(it does not mutate the workspace). Python and
|
|
604
|
-
instead: `/input.json`, `/context.json`,
|
|
605
|
-
`/output` for artifacts.
|
|
604
|
+
`ctx` is `{ skill, files, workspace, tools, output }` (typed as
|
|
605
|
+
`SkillRunContext`). `ctx.files` holds bundled text resources by relative path,
|
|
606
|
+
`ctx.workspace` is gated by the workspace permission, `ctx.tools` only exposes
|
|
607
|
+
tools the runner was given, and `ctx.output.writeFile(name, content)` returns
|
|
608
|
+
scratch artifacts to the model (it does not mutate the workspace). Python and
|
|
609
|
+
Bash use the path-based contract instead: `/input.json`, `/context.json`,
|
|
610
|
+
bundled resources under `/skill`, and `/output` for artifacts.
|
|
606
611
|
|
|
607
612
|
Passing `workspaceInstance` gives scripts read-only workspace access by default.
|
|
608
613
|
Network access, tools, and workspace writes are opt-in. The default timeout is
|
|
@@ -1028,14 +1033,13 @@ Think's workspace is a `Workspace` from [`@cloudflare/computer`](https://github.
|
|
|
1028
1033
|
- Install `@cloudflare/computer`; it is a peer dependency. Import `Workspace` from it rather than from `@cloudflare/think`.
|
|
1029
1034
|
- `workspace` is typed `ThinkWorkspace`, which needs only `workspace.fs`. `WorkspaceLike` is gone.
|
|
1030
1035
|
- The default Workspace lives on the agent's own storage and has no execution backends. Override `workspace` with `new Workspace({ storage: this.ctx.storage, ... })` to add backends, mounts, or git.
|
|
1031
|
-
-
|
|
1032
|
-
- Think writes its own files under `/workspace`: evicted media under `/workspace/attachments/evicted/`, fetched responses under `/workspace/fetched/`, and skills under `/workspace/.agents/skills`.
|
|
1036
|
+
- Think writes its own files under `/workspace`: evicted media under `/workspace/attachments/evicted/` and fetched responses under `/workspace/fetched/`. `skills.workspace()` reads skills from `/workspace/.agents/skills`.
|
|
1033
1037
|
- The execute tool's `state` option and `state.*` namespace are removed. Pass `createAITools({ workspace: this.workspace })` as `tools` to give sandboxed code the workspace.
|
|
1034
1038
|
- The workspace tools are `@cloudflare/computer`'s `createAITools`: `read`, `ls`, `find`, `grep`, `write`, `edit`, and `delete`, plus `exec` when the Workspace has backends. `list` is now `ls`. `exec` offers every backend the Workspace has.
|
|
1035
1039
|
- `workspaceBash`, `createWorkspaceTools`, and `@cloudflare/think/tools/workspace` are removed, with no replacement. There is no in-process `bash` tool: add a backend to the Workspace to get `exec`. To change a built-in tool, return a same-name tool from `getTools()`; to hide tools for a turn, return `activeTools` from `beforeTurn`.
|
|
1036
1040
|
- Every Think agent has `__getWorkspaceStub()`, so `getWorkspace(stub)` and the Worker shell backend can reach a Workspace it owns.
|
|
1037
1041
|
- Image and PDF reads stored by the old `read` tool replay as an error asking the model to read the file again.
|
|
1038
|
-
- Agent Skills (`agents/skills`) accept only a Computer Workspace, for both
|
|
1042
|
+
- Agent Skills (`agents/skills`) accept only a Computer Workspace, for both `skills.workspace()` and `skills.runner({ workspaceInstance })`. Relative paths in skill scripts resolve against `/workspace`.
|
|
1039
1043
|
|
|
1040
1044
|
Files a Shell Workspace stored are not moved. [Migrating to `@cloudflare/computer`](../shell/migrating-to-computer.md) documents both storage schemas and a copy script.
|
|
1041
1045
|
|
|
@@ -1046,7 +1050,7 @@ Peer dependencies you provide:
|
|
|
1046
1050
|
| Package | Required | Notes |
|
|
1047
1051
|
| ------------------------ | -------- | -------------------------------- |
|
|
1048
1052
|
| `agents` | yes | Cloudflare Agents SDK |
|
|
1049
|
-
| `ai` | yes | Vercel AI SDK
|
|
1053
|
+
| `ai` | yes | Vercel AI SDK v7 |
|
|
1050
1054
|
| `zod` | yes | Schema validation (v4) |
|
|
1051
1055
|
| `@cloudflare/computer` | yes | Workspace and workspace tools |
|
|
1052
1056
|
| `@chat-adapter/telegram` | optional | Required for Telegram messengers |
|
|
@@ -1057,9 +1061,9 @@ Bundled with `@cloudflare/think`:
|
|
|
1057
1061
|
| ---------------------- | ---------------------------------------- |
|
|
1058
1062
|
| `@cloudflare/codemode` | Code execution for `createExecuteTool()` |
|
|
1059
1063
|
|
|
1060
|
-
The Agent Skills
|
|
1061
|
-
[`agents/skills`](https://github.com/cloudflare/agents/blob/main/packages/agents/AGENTS.md) (so
|
|
1062
|
-
|
|
1064
|
+
The Agent Skills parser, sources, and script runner live in
|
|
1065
|
+
[`agents/skills`](https://github.com/cloudflare/agents/blob/main/packages/agents/AGENTS.md) (so the script runner pulls
|
|
1066
|
+
`just-bash` through `agents`, not Think).
|
|
1063
1067
|
|
|
1064
1068
|
## Docs
|
|
1065
1069
|
|
package/docs/lifecycle-hooks.md
CHANGED
|
@@ -126,7 +126,7 @@ export class MyAgent extends Think<Env> {
|
|
|
126
126
|
}
|
|
127
127
|
```
|
|
128
128
|
|
|
129
|
-
A block declared without a `provider` is auto-wired to durable per-agent SQLite, so `memory` above is writable through the `set_context` tool with no extra wiring. When context blocks are configured, Think builds the system prompt from those blocks
|
|
129
|
+
A block declared without a `provider` is auto-wired to durable per-agent SQLite, so `memory` above is writable through the `set_context` tool with no extra wiring. When context blocks are configured, Think builds the system prompt from an overridden `getSystemPrompt()` followed by those blocks. Think's default `getSystemPrompt()` is only used when there are no blocks. The frozen system prompt is always persisted, so a cold wake reuses the exact prompt string the model already cached.
|
|
130
130
|
|
|
131
131
|
The assembled blocks are available as `this.context` once the Lifecycle has started. See the [Context documentation](https://github.com/cloudflare/agents/blob/main/docs/agents/context.md) for providers, tools, and frozen-prompt behavior.
|
|
132
132
|
|
|
@@ -142,18 +142,18 @@ beforeTurn(ctx: TurnContext): TurnConfig | void | Promise<TurnConfig | void>
|
|
|
142
142
|
|
|
143
143
|
### TurnContext
|
|
144
144
|
|
|
145
|
-
| Field | Type | Description
|
|
146
|
-
| -------------- | ------------------------------- |
|
|
147
|
-
| `system` | `string` | Assembled system prompt (
|
|
148
|
-
| `messages` | `ModelMessage[]` | Assembled model messages (truncated)
|
|
149
|
-
| `tools` | `ToolSet` | Merged tool set (workspace + getTools + context + MCP + client + caller)
|
|
150
|
-
| `model` | `LanguageModel` | The resolved model (a string from `getModel()` is already resolved here)
|
|
151
|
-
| `continuation` | `boolean` | Whether this is a continuation turn (auto-continue after tool result)
|
|
152
|
-
| `body` | `Record<string, unknown>` | Custom body fields from the client request
|
|
153
|
-
| `requestId` | `string \| undefined` | The request this turn runs for. See [Turn identity](#turn-identity)
|
|
154
|
-
| `trigger` | `TurnTrigger \| undefined` | What admitted the turn: `"ws-chat"`, `"rpc"`, `"submission"`, and so on
|
|
155
|
-
| `abortSignal` | `AbortSignal \| undefined` | Aborts when the turn is cancelled. Pass it to I/O the hook awaits
|
|
156
|
-
| `messenger` | `MessengerContext \| undefined` | The messenger thread this turn answers, fixed when the turn was admitted
|
|
145
|
+
| Field | Type | Description |
|
|
146
|
+
| -------------- | ------------------------------- | --------------------------------------------------------------------------- |
|
|
147
|
+
| `system` | `string` | Assembled system prompt (`getSystemPrompt()` override, then context blocks) |
|
|
148
|
+
| `messages` | `ModelMessage[]` | Assembled model messages (truncated) |
|
|
149
|
+
| `tools` | `ToolSet` | Merged tool set (workspace + getTools + context + MCP + client + caller) |
|
|
150
|
+
| `model` | `LanguageModel` | The resolved model (a string from `getModel()` is already resolved here) |
|
|
151
|
+
| `continuation` | `boolean` | Whether this is a continuation turn (auto-continue after tool result) |
|
|
152
|
+
| `body` | `Record<string, unknown>` | Custom body fields from the client request |
|
|
153
|
+
| `requestId` | `string \| undefined` | The request this turn runs for. See [Turn identity](#turn-identity) |
|
|
154
|
+
| `trigger` | `TurnTrigger \| undefined` | What admitted the turn: `"ws-chat"`, `"rpc"`, `"submission"`, and so on |
|
|
155
|
+
| `abortSignal` | `AbortSignal \| undefined` | Aborts when the turn is cancelled. Pass it to I/O the hook awaits |
|
|
156
|
+
| `messenger` | `MessengerContext \| undefined` | The messenger thread this turn answers, fixed when the turn was admitted |
|
|
157
157
|
|
|
158
158
|
### Turn identity
|
|
159
159
|
|
|
@@ -917,9 +917,7 @@ Extensions can subscribe to `beforeTurn`, `beforeToolCall`, `afterToolCall`, `on
|
|
|
917
917
|
```js
|
|
918
918
|
// extension source
|
|
919
919
|
({
|
|
920
|
-
tools: {
|
|
921
|
-
/* ... */
|
|
922
|
-
},
|
|
920
|
+
tools: {/* ... */},
|
|
923
921
|
hooks: {
|
|
924
922
|
beforeTurn: async (snapshot, host) => {
|
|
925
923
|
/* may return TurnConfig */
|
package/docs/multi-chat.md
CHANGED
|
@@ -19,10 +19,11 @@ one Durable Object would serialize all of a user's turns on one single-threaded
|
|
|
19
19
|
object, so each chat gets its own.
|
|
20
20
|
|
|
21
21
|
This guide walks through the pattern as implemented in
|
|
22
|
-
|
|
23
|
-
the
|
|
22
|
+
the assistant example, a reference Think app that has since been removed from
|
|
23
|
+
the repository ([last version](https://github.com/cloudflare/agents/tree/786d30e305baebc5fbf5ad5ed80461f88a728468/examples/assistant)). The code below is taken from that
|
|
24
|
+
example.
|
|
24
25
|
|
|
25
|
-
> **Topology note.**
|
|
26
|
+
> **Topology note.** The assistant example hosts each chat as a facet (a
|
|
26
27
|
> [dynamic agent](https://github.com/cloudflare/agents/blob/main/docs/agents/sub-agents.md))
|
|
27
28
|
> of the directory, using `subAgent()`, `hasSubAgent()`, `listSubAgents()`,
|
|
28
29
|
> and `deleteSubAgent()`. Those methods are aliases for `this.dynamicAgents`.
|
|
@@ -280,7 +281,7 @@ export class AssistantDirectory extends Think<Env, DirectoryState> {
|
|
|
280
281
|
### Connect from React
|
|
281
282
|
|
|
282
283
|
The client opens one connection to the directory for the sidebar and one to the
|
|
283
|
-
active chat.
|
|
284
|
+
active chat. The assistant example wraps the directory connection in a local
|
|
284
285
|
`useChats()` hook (`src/use-chats.ts`):
|
|
285
286
|
|
|
286
287
|
```typescript
|
|
@@ -349,21 +350,19 @@ export class AssistantDirectory extends Think<Env, DirectoryState> {
|
|
|
349
350
|
|
|
350
351
|
Every Think agent has a `__getWorkspaceStub()` method, which `getWorkspace()`
|
|
351
352
|
from `@cloudflare/computer` calls over Durable Object RPC. Each chat replaces
|
|
352
|
-
Think's default workspace with a client for the directory's
|
|
353
|
-
defers `getWorkspace()` until a turn first touches a file, and assigning it as a
|
|
354
|
-
class field means Think never creates a per-chat `Workspace`:
|
|
353
|
+
Think's default workspace with a client for the directory's:
|
|
355
354
|
|
|
356
355
|
```typescript
|
|
357
356
|
import { getWorkspace } from "@cloudflare/computer";
|
|
358
|
-
import { lazyWorkspace } from "@cloudflare/think";
|
|
359
357
|
|
|
360
358
|
export class MyAssistant extends Think<Env> {
|
|
361
|
-
override workspace =
|
|
362
|
-
getWorkspace(await this.parentAgent(AssistantDirectory))
|
|
363
|
-
);
|
|
359
|
+
override workspace = getWorkspace(() => this.parentAgent(AssistantDirectory));
|
|
364
360
|
}
|
|
365
361
|
```
|
|
366
362
|
|
|
363
|
+
The client reaches the directory on the first file call, so a chat that never
|
|
364
|
+
touches a file never contacts it.
|
|
365
|
+
|
|
367
366
|
Think's workspace tools, the fetch tool's saved responses, and skill script
|
|
368
367
|
runners all go through it.
|
|
369
368
|
|
|
@@ -565,7 +564,7 @@ See [Scheduled Tasks](./index.md#scheduled-tasks) for the schedule syntax.
|
|
|
565
564
|
The example has no directory-level search index.
|
|
566
565
|
- **No packaged helpers.** There is no `Chats` base class and `useChats()` is not
|
|
567
566
|
exported from any package. The pattern is documented through
|
|
568
|
-
|
|
567
|
+
the assistant example until the shape settles.
|
|
569
568
|
|
|
570
569
|
## Related
|
|
571
570
|
|
|
@@ -574,4 +573,4 @@ See [Scheduled Tasks](./index.md#scheduled-tasks) for the schedule syntax.
|
|
|
574
573
|
- [Lifecycle Hooks](./lifecycle-hooks.md) — `beforeTurn` and `onChatResponse`
|
|
575
574
|
- [Dynamic agents](https://github.com/cloudflare/agents/blob/main/docs/agents/sub-agents.md) — `onBeforeSubAgent`, `parentAgent()`, `useAgent({ sub })`, and routing
|
|
576
575
|
- [Routing to independent Agents](https://github.com/cloudflare/agents/blob/main/docs/agents/routing.md#routing-to-independent-agents) — `RoutedAgents` for top-level chat Durable Objects
|
|
577
|
-
- [
|
|
576
|
+
- [Assistant example (last version)](https://github.com/cloudflare/agents/tree/786d30e305baebc5fbf5ad5ed80461f88a728468/examples/assistant) — the full reference app
|
package/docs/tools.md
CHANGED
|
@@ -104,23 +104,6 @@ Tools that need approval, and tools that run in the browser, are not exported: n
|
|
|
104
104
|
|
|
105
105
|
`exec` offers every backend the Workspace has, and each backend describes itself to the model. To change a built-in tool, return a tool with the same name from `getTools()`; it replaces the built-in. To hide tools for a turn, return `activeTools` from `beforeTurn`.
|
|
106
106
|
|
|
107
|
-
### A shared workspace
|
|
108
|
-
|
|
109
|
-
To work in a Workspace another Durable Object owns, wrap `getWorkspace` in `lazyWorkspace`. The owner is only contacted when a turn first touches a file:
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
import { getWorkspace } from "@cloudflare/computer";
|
|
113
|
-
import { Think, lazyWorkspace } from "@cloudflare/think";
|
|
114
|
-
|
|
115
|
-
export class Chat extends Think<Env> {
|
|
116
|
-
override workspace = lazyWorkspace(async () =>
|
|
117
|
-
getWorkspace(await this.parentAgent(Directory))
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Every Think agent can hand out its Workspace this way: `getWorkspace(stub)` calls the agent's `__getWorkspaceStub()` method over RPC. A lazy Workspace gets the file tools but no `exec`.
|
|
123
|
-
|
|
124
107
|
## Custom Tools
|
|
125
108
|
|
|
126
109
|
Override `getTools()` to add your own tools. These are standard AI SDK `tool()` definitions with Zod schemas:
|
|
@@ -351,9 +334,9 @@ Either way, the text and reasoning the model wrote after the paused output (its
|
|
|
351
334
|
|
|
352
335
|
- `pendingExecutions()` — pending actions (with full args) for rendering approval UI.
|
|
353
336
|
|
|
354
|
-
> **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
|
|
337
|
+
> **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. The `PausedExecutionCard` in the [assistant example](https://github.com/cloudflare/agents/tree/786d30e305baebc5fbf5ad5ed80461f88a728468/examples/assistant/src) shows the pattern.
|
|
355
338
|
|
|
356
|
-
See `
|
|
339
|
+
See `design/think-execute-hitl.md` for the design.
|
|
357
340
|
|
|
358
341
|
### The runtime handle
|
|
359
342
|
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"tools"
|
|
10
10
|
],
|
|
11
11
|
"type": "module",
|
|
12
|
-
"version": "0.
|
|
12
|
+
"version": "0.23.0",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"repository": {
|
|
15
15
|
"directory": "packages/think",
|
|
@@ -23,31 +23,30 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@ai-sdk/anthropic": "^4.0.0",
|
|
25
25
|
"@ai-sdk/openai": "^4.0.0",
|
|
26
|
-
"@cloudflare/codemode": "^0.5.
|
|
26
|
+
"@cloudflare/codemode": "^0.5.6",
|
|
27
27
|
"chat": "^4.31.0",
|
|
28
28
|
"workers-ai-provider": "^4.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@ai-sdk/react": "^4.0.0",
|
|
32
32
|
"@chat-adapter/telegram": "^4.31.0",
|
|
33
|
-
"@cloudflare/
|
|
34
|
-
"@cloudflare/computer": "^0.5.1",
|
|
33
|
+
"@cloudflare/computer": "^0.8.0",
|
|
35
34
|
"@testing-library/react": "^16.3.2",
|
|
36
35
|
"@types/react": "^19.2.17",
|
|
37
36
|
"@types/react-dom": "^19.2.3",
|
|
38
37
|
"@vitejs/plugin-react": "^6.0.3",
|
|
39
|
-
"agents": "^0.
|
|
38
|
+
"agents": "^0.32.0",
|
|
40
39
|
"ai": "^7.0.0",
|
|
41
40
|
"jsdom": "^29.1.1",
|
|
42
41
|
"react": "^19.2.7",
|
|
43
42
|
"react-dom": "^19.2.7",
|
|
44
|
-
"vite": "^8.1.
|
|
45
|
-
"zod": "^4.
|
|
43
|
+
"vite": "^8.1.6",
|
|
44
|
+
"zod": "^4.6.7"
|
|
46
45
|
},
|
|
47
46
|
"peerDependencies": {
|
|
48
47
|
"@ai-sdk/react": "^4.0.0",
|
|
49
48
|
"@chat-adapter/telegram": "^4.29.0",
|
|
50
|
-
"@cloudflare/computer": "^0.
|
|
49
|
+
"@cloudflare/computer": "^0.8.0",
|
|
51
50
|
"agents": ">=0.28.0 <1.0.0",
|
|
52
51
|
"ai": "^7.0.0",
|
|
53
52
|
"react": "^19.0.0",
|