@cloudflare/think 0.19.0 → 0.20.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 CHANGED
@@ -364,9 +364,11 @@ Script execution requires a Worker Loader binding:
364
364
  | `getTools()` | `{}` | AI SDK `ToolSet` for the agentic loop |
365
365
  | `getMessengers()` | `{}` | Messenger ingress and delivery declarations |
366
366
  | `getScheduledTasks()` | `{}` | Code-declared recurring prompts |
367
+ | `getScheduledTasksScope()` | `"root"` | Which instances arm declared tasks — `"root"` or `"all"` (sub-agents too) |
367
368
  | `getDefaultTimezone()` | `undefined` | Default timezone for wall-clock schedules |
368
369
  | `maxSteps` | `10` | Max tool-call rounds per turn (property) |
369
370
  | `sendReasoning` | `true` | Send reasoning chunks to chat clients |
371
+ | `messageMetadata` | `undefined` | Default writer for server-authored assistant-message metadata (override per turn via `TurnConfig`) |
370
372
  | `configureSession()` | identity | Configure the default session handle: compaction and search |
371
373
  | `configureContext()` | `[]` | Declare prompt context blocks. See [Session and context](#session-and-context) |
372
374
  | `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 |
@@ -545,6 +547,14 @@ work such as creating a Workflow run or writing a run ledger. Delivery is
545
547
  at-least-once; use `idempotencyKey` or `occurrenceKey` for your own durable
546
548
  idempotency.
547
549
 
550
+ Declared tasks are armed on the **root agent only**. Because
551
+ `getScheduledTasks()` is normally a static declaration, it returns the same
552
+ tasks on every instance of the class, so arming it on sub-agents as well would
553
+ dispatch each occurrence once per live sub-agent on top of the root. Override
554
+ `getScheduledTasksScope()` to return `"all"` when a class genuinely declares
555
+ different tasks per sub-agent — each sub-agent then owns an independent
556
+ schedule.
557
+
548
558
  Static declarations reconcile on startup. If `getScheduledTasks()` reads
549
559
  product-owned data that can change while the Durable Object is live, call
550
560
  `internal_reconcileScheduledTasks()` after updating that data. During
@@ -601,6 +611,8 @@ The AI SDK-derived contexts spread the SDK's own types at the top level — no i
601
611
 
602
612
  `TurnConfig` also accepts `sendReasoning` to override whether reasoning chunks are emitted for the current UI message stream. The instance-level `sendReasoning` property defaults to `true`; return `{ sendReasoning: false }` from `beforeTurn` to hide reasoning for a single turn, for example on internal continuation turns.
603
613
 
614
+ `TurnConfig.messageMetadata` writes server-authored metadata onto the assistant message a turn persists — the same AI SDK `messageMetadata` callback base `AIChatAgent` + `streamText` accept, now forwarded through Think. It is called with each stream part; return a JSON-serializable object (typically from the `start` and/or `finish` part) and each return is shallow-merged into the message's metadata. An auto-continuation is its own turn: `beforeTurn` runs again with `ctx.continuation: true`, and the continuation persists as a separate assistant message with its own metadata. Its return value is broadcast to clients and persisted, so it must not carry server-only secrets. Set the instance-level `messageMetadata` property for turn-independent metadata (e.g. stamping a `createdAt` timestamp on every assistant message); return `messageMetadata` from `beforeTurn` to override it for a single turn. Because it is a function, configure it from a Think subclass — sandboxed extension hooks cannot provide it over RPC.
615
+
604
616
  `TurnConfig` also accepts stable AI SDK `streamText` call settings such as `maxOutputTokens`, `temperature`, `stopSequences`, `seed`, `maxRetries`, `timeout`, and `headers`. Use them to tune model behavior per turn, for example disabling retries or adding a chunk timeout during recovery flows.
605
617
 
606
618
  `TurnConfig.stopWhen` accepts AI SDK stop conditions such as `hasToolCall("finalAnswer")` for ending a turn early. Think composes these with its own `maxSteps` bound, so a custom condition can stop before the cap without removing the safety limit. Because stop conditions are functions, return `stopWhen` from a Think subclass's `beforeTurn`; sandboxed extension hooks cannot provide it over RPC.
@@ -726,6 +738,7 @@ interface TurnConfig {
726
738
  maxSteps?: number; // override maxSteps for this turn
727
739
  stopWhen?: StopCondition | StopCondition[]; // additional early-exit conditions
728
740
  sendReasoning?: boolean; // send reasoning chunks for this turn
741
+ messageMetadata?: MessageMetadataCallback; // write assistant-message metadata for this turn
729
742
  maxOutputTokens?: number;
730
743
  temperature?: number;
731
744
  topP?: number;
@@ -968,12 +981,19 @@ Tools belong to the child agent; define them with `getTools()` or use
968
981
  `configure()` and `getConfig()` persist a JSON-serializable config blob in SQLite — useful for private server-side settings that should survive hibernation and restarts. Pass the config shape as a method generic for typed call sites:
969
982
 
970
983
  ```ts
984
+ import { Think } from "@cloudflare/think";
985
+
971
986
  type MyConfig = { modelTier: "fast" | "capable"; systemPrompt: string };
972
987
 
988
+ const MODEL_IDS = {
989
+ fast: "@cf/meta/llama-3.1-8b-instruct",
990
+ capable: "@cf/moonshotai/kimi-k2.7-code"
991
+ } as const;
992
+
973
993
  export class MyAgent extends Think<Env> {
974
994
  getModel() {
975
995
  const tier = this.getConfig<MyConfig>()?.modelTier ?? "fast";
976
- return createWorkersAI({ binding: this.env.AI })(MODEL_IDS[tier]);
996
+ return MODEL_IDS[tier];
977
997
  }
978
998
  }
979
999
  ```