|
1 | 1 | # @cloudflare/think |
2 | 2 |
|
| 3 | +## 0.5.1 |
| 4 | + |
| 5 | +### Patch Changes |
| 6 | + |
| 7 | +- [#1443](https://github.com/cloudflare/agents/pull/1443) [`e7d225b`](https://github.com/cloudflare/agents/commit/e7d225b72a743a2cf1491ebf73f06580c668e560) Thanks [@threepointone](https://github.com/threepointone)! - Fix sub-agent WebSockets on deployed Workers by keeping the browser WebSocket owned by the parent Agent and forwarding connect/message/close events to child facets over RPC. |
| 8 | + |
| 9 | + Fix resumed chat streams so a partially hydrated assistant response is rebuilt from replay chunks instead of rendering replayed text as a second assistant text part. |
| 10 | + |
| 11 | + Fix a resume ACK race where drill-in chat connections could miss the terminal stream frame if the helper completed between the resume notification and client acknowledgement. |
| 12 | + |
| 13 | +- [#1435](https://github.com/cloudflare/agents/pull/1435) [`b197faf`](https://github.com/cloudflare/agents/commit/b197faf0ca79d9e921d2f80c5fcafe4899995d11) Thanks [@threepointone](https://github.com/threepointone)! - Add multimodal-aware workspace reads for images and PDFs while keeping persisted tool results compact. |
| 14 | + |
3 | 15 | ## 0.5.0 |
4 | 16 |
|
5 | 17 | ### Minor Changes |
|
31 | 43 |
|
32 | 44 | ```typescript |
33 | 45 | const result = await this.saveMessages(messages, { |
34 | | - signal: controller.signal |
| 46 | + signal: controller.signal, |
35 | 47 | }); |
36 | 48 | if (result.status === "aborted") { |
37 | 49 | // Inference loop terminated mid-stream; partial chunks persisted. |
38 | 50 | } |
39 | 51 | ``` |
40 | 52 |
|
41 | 53 | The signal is linked to Think's per-turn `AbortController` for the duration of the call. When it aborts: |
| 54 | + |
42 | 55 | - the inference loop's signal aborts (the same path `chat-request-cancel` takes); |
43 | 56 | - partial chunks already streamed are persisted to the resumable stream; |
44 | 57 | - `saveMessages` resolves with `{ status: "aborted" }`; |
|
51 | 64 | `SaveMessagesResult.status` now includes `"aborted"` alongside `"completed"` and `"skipped"`. Existing callers that only switch on `"completed"` are unaffected. |
52 | 65 |
|
53 | 66 | **Limitations.** |
| 67 | + |
54 | 68 | - `AbortSignal` cannot cross Durable Object RPC. Construct the controller inside the DO that calls `saveMessages`. To bridge a parent's intent into a child DO, return a `ReadableStream` from the child whose `cancel` callback aborts a per-turn controller — `examples/agents-as-tools` shows the canonical pattern. |
55 | 69 | - The signal lives in memory only. If the DO hibernates mid-turn and `chatRecovery` is enabled, the recovered turn calls `continueLastTurn()` internally without the original signal — an abort fired after restart has no effect on the recovered turn. |
56 | 70 |
|
|
65 | 79 | This extracts the `latest`/`merge`/`drop`/`debounce` admission state machine into a `SubmitConcurrencyController` exported from `agents/chat`. `AIChatAgent` semantics (including merge persistence) are preserved. `Think` now picks up the same pending-enqueue protection, so an overlapping submit is still detected while an accepted request is between admission and turn queue registration. |
66 | 80 |
|
67 | 81 | Additional fixes: |
| 82 | + |
68 | 83 | - `Think` now captures the turn generation immediately after admission and threads it into `_turnQueue.enqueue`, so a clear that lands between admission and queue registration cannot run a stale turn. |
69 | 84 | - Pending-enqueue tracking is now bound to a release function tied to the controller's reset epoch, so a release from a pre-reset submit can no longer erase a post-reset submit's marker and let a third submit slip through as non-overlapping. |
70 | 85 | - Debounce cancellation correctly resolves all in-flight waiters instead of overwriting a single timer slot. |
71 | 86 |
|
72 | 87 | - [#1394](https://github.com/cloudflare/agents/pull/1394) [`a0a0d17`](https://github.com/cloudflare/agents/commit/a0a0d179a862547715b0dd2e38d37065f24eabe5) Thanks [@threepointone](https://github.com/threepointone)! - think: add `beforeStep` lifecycle hook and `output` passthrough on `TurnConfig`. |
| 88 | + |
73 | 89 | - **`beforeStep(ctx)`** — new lifecycle hook called before each AI SDK step in the agentic loop, wired to `streamText({ prepareStep })`. Receives a `PrepareStepContext` (the AI SDK's `PrepareStepFunction` parameter — `steps`, `stepNumber`, `model`, `messages`, `experimental_context`) and may return a `StepConfig` (`PrepareStepResult`) to override `model`, `toolChoice`, `activeTools`, `system`, `messages`, `experimental_context`, or `providerOptions` for the current step. Use `beforeTurn` for turn-wide assembly and `beforeStep` when the decision depends on the step number or previous step results. Resolves [#1363](https://github.com/cloudflare/agents/issues/1363). |
74 | 90 | - **`TurnConfig.output`** — new optional field on `TurnConfig` forwarded to `streamText`. Accepts the AI SDK's structured-output spec (e.g. `Output.object({ schema })`, `Output.text()`) so a single agent can keep tools enabled on intermediate turns and return schema-validated structured output on a designated turn — without losing tools at model construction. Combine with `activeTools: []` for providers that strip tools when `responseFormat: "json"` is active (e.g. `workers-ai-provider`). Resolves [#1383](https://github.com/cloudflare/agents/issues/1383). |
75 | 91 | - New re-exports from `@cloudflare/think`: `PrepareStepFunction`, `PrepareStepResult`, `PrepareStepContext`, `StepConfig`. |
|
167 | 183 | ### `subAgent()` cross-DO I/O fix |
168 | 184 |
|
169 | 185 | Three issues in the facet initialization path caused `"Cannot perform I/O on behalf of a different Durable Object"` errors when spawning sub-agents in production: |
| 186 | + |
170 | 187 | - `subAgent()` constructed a `Request` in the parent DO and passed it to the child via `stub.fetch()`. The `Request` carried native I/O tied to the parent isolate, which the child rejected. |
171 | 188 | - The facet flag was set _after_ the first `onStart()` ran, so `broadcastMcpServers()` fired with `_isFacet === false` on the initial boot. |
172 | 189 | - `_broadcastProtocol()`, the inherited `broadcast()`, and `_workflow_broadcast()` iterated the connection registry without an `_isFacet` guard, letting broadcasts reach into the parent DO's WebSocket registry from a child isolate. |
|
176 | 193 | ### `"experimental"` compatibility flag no longer required |
177 | 194 |
|
178 | 195 | `ctx.facets`, `ctx.exports`, and `env.LOADER` (Worker Loader) have graduated out of the `"experimental"` compatibility flag in workerd. `agents` and `@cloudflare/think` no longer require it: |
| 196 | + |
179 | 197 | - `subAgent()` / `abortSubAgent()` / `deleteSubAgent()` — the `@experimental` JSDoc tag and runtime error messages no longer reference the flag. The runtime guards on `ctx.facets` / `ctx.exports` stay in place and now nudge users toward updating `compatibility_date` instead. |
180 | 198 | - `Think` — the `@experimental` JSDoc tag no longer references the flag. |
181 | 199 |
|
182 | 200 | No code change is required; remove `"experimental"` from your `compatibility_flags` in `wrangler.jsonc` if it was only there for these features. |
183 | 201 |
|
184 | 202 | - [#1332](https://github.com/cloudflare/agents/pull/1332) [`7cb8acf`](https://github.com/cloudflare/agents/commit/7cb8acff8281a30bc17980e506ab5582f3cb1c72) Thanks [@threepointone](https://github.com/threepointone)! - Expose `createdAt` on fiber and chat recovery contexts so apps can suppress continuations for stale, interrupted turns. |
| 203 | + |
185 | 204 | - `FiberRecoveryContext` (from `agents`) gains `createdAt: number` — epoch milliseconds when `runFiber` started, read from the `cf_agents_runs` row that was already tracked internally. |
186 | 205 | - `ChatRecoveryContext` (from `@cloudflare/ai-chat` and `@cloudflare/think`) gains the same `createdAt` field, threaded through from the underlying fiber. |
187 | 206 |
|
|
247 | 266 | - [#1270](https://github.com/cloudflare/agents/pull/1270) [`87b4512`](https://github.com/cloudflare/agents/commit/87b4512985e47de659bf970a65a6d1951f5855fe) Thanks [@threepointone](https://github.com/threepointone)! - Wire Session into Think as the storage layer, achieving full feature parity with AIChatAgent plus Session-backed advantages. |
248 | 267 |
|
249 | 268 | **Think (`@cloudflare/think`):** |
| 269 | + |
250 | 270 | - Session integration: `this.messages` backed by `session.getHistory()`, tree-structured messages, context blocks, compaction, FTS5 search |
251 | 271 | - `configureSession()` override for context blocks, compaction, search, skills (sync or async) |
252 | 272 | - `assembleContext()` returns `{ system, messages }` with context block composition |
|
265 | 285 | - Constructor wraps `onStart` — subclasses never need `super.onStart()` |
266 | 286 |
|
267 | 287 | **agents (`agents/chat`):** |
| 288 | + |
268 | 289 | - Extract `AbortRegistry`, `applyToolUpdate` + builders, `parseProtocolMessage` into shared `agents/chat` layer |
269 | 290 | - Add `applyChunkToParts` export for fiber recovery |
270 | 291 |
|
271 | 292 | **AIChatAgent (`@cloudflare/ai-chat`):** |
| 293 | + |
272 | 294 | - Refactor to use shared `AbortRegistry` from `agents/chat` |
273 | 295 | - Add `continuation` flag to `OnChatMessageOptions` |
274 | 296 | - Export `getAgentMessages()` and tool part helpers |
|
283 | 305 | `Think` now extends `Agent` directly (no mixin). Fiber support is inherited from the base class. |
284 | 306 |
|
285 | 307 | **Breaking (experimental APIs only):** |
| 308 | + |
286 | 309 | - Removed `withFibers` mixin (`agents/experimental/forever`) |
287 | 310 | - Removed `withDurableChat` mixin (`@cloudflare/ai-chat/experimental/forever`) |
288 | 311 | - Removed `./experimental/forever` export from both packages |
|
0 commit comments