@cloudflare/ai-chat 0.10.2 → 0.11.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
@@ -254,11 +254,11 @@ Streams automatically resume on disconnect/reconnect. No configuration needed.
254
254
 
255
255
  When a client disconnects mid-stream, chunks are buffered in SQLite. On reconnect, the client receives all buffered chunks and continues receiving the live stream.
256
256
 
257
- This handles browser disconnects, navigation, and React cleanup while the Durable Object keeps running. To recover after the Durable Object itself is evicted during a model call, opt in to `chatRecovery`.
257
+ This handles browser disconnects, navigation, and React cleanup while the Durable Object keeps running. Durable chat recovery separately handles the Durable Object itself being evicted during a model call.
258
258
 
259
259
  ## Durable Chat Recovery
260
260
 
261
- `AIChatAgent` defaults `chatRecovery` to `false`. Enable it when you want chat turns to survive Worker deploys, Durable Object eviction, or process restarts:
261
+ Every `AIChatAgent` turn runs in a durable recovery fiber, so turns can survive Worker deploys, Durable Object eviction, or process restarts. Assign a `chatRecovery` object only when you need to tune budgets or terminal behavior:
262
262
 
263
263
  ```typescript
264
264
  import type {
@@ -281,7 +281,9 @@ export class ChatAgent extends AIChatAgent<Env> {
281
281
  }
282
282
  ```
283
283
 
284
- See [`docs/agents/chat-agents.md`](../../docs/agents/chat-agents.md#stream-recovery) for provider-specific recovery strategies and observability events.
284
+ `chatRecovery = false` is no longer supported. Previously compiled JavaScript that still supplies `false` receives the default durable configuration. If automatic continuation is unsafe, return `{ continue: false }` from `onChatRecovery()`; use durable state for cancellation or side-effect guards, and configure recovery budgets for cost control.
285
+
286
+ See [`docs/agents/chat-agents.md`](../../docs/agents/chat-agents.md#stream-recovery) for migration guidance, provider-specific recovery strategies, and observability events.
285
287
 
286
288
  Generic client stream abort/cleanup is local-only by default: the server turn continues and can be resumed later. An explicit `stop()` still cancels the server turn:
287
289
 
@@ -466,11 +468,11 @@ Extends `Agent` from the `agents` package.
466
468
  | Property / Method | Type | Description |
467
469
  | ------------------------------------ | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
468
470
  | `messages` | `ChatMessage[]` | Current conversation messages (loaded from SQLite) |
469
- | `chatRecovery` | `ChatRecoveryConfig` | Opt-in Durable Object eviction recovery for chat turns. Default: `false` |
471
+ | `chatRecovery` | `ChatRecoveryConfig` | Always-on durable recovery configuration. Assign an object to tune budgets and terminal behavior |
470
472
  | `maxPersistedMessages` | `number \| undefined` | Max messages to keep in SQLite. Default: unlimited |
471
473
  | `messageConcurrency` | `MessageConcurrency` | Concurrency strategy for `sendMessage()` submits. Default: `"queue"` |
472
474
  | `onChatMessage(onFinish?, options?)` | Override | Handle incoming chat messages. Return a `Response`. `onFinish` is optional. |
473
- | `onChatRecovery(ctx)` | Override | Customize recovery after a chat turn is interrupted while `chatRecovery` is enabled |
475
+ | `onChatRecovery(ctx)` | Override | Customize recovery after a chat turn is interrupted |
474
476
  | `onChatResponse(result)` | Override | Called after a chat turn completes. `result` has `message`, `requestId`, `status`, `continuation` |
475
477
  | `persistMessages(messages)` | `Promise<void>` | Manually persist messages (usually automatic) |
476
478
  | `saveMessages(messages, options?)` | `Promise<SaveMessagesResult>` | Persist messages and trigger `onChatMessage`. Accepts array or function. `options.signal` cancels the turn externally. |
package/dist/index.d.ts CHANGED
@@ -166,9 +166,9 @@ declare class AIChatAgent<
166
166
  */
167
167
  private _turnQueue;
168
168
  /**
169
- * When true, chat turns are wrapped in `runFiber` for durable execution.
170
- * Enables `onChatRecovery` hook and `this.stash()` during streaming.
171
- * Set to `true` in subclasses to enable durable streaming.
169
+ * Durable chat recovery configuration. Every chat turn runs in `runFiber`,
170
+ * enabling `onChatRecovery` and `this.stash()` during streaming. Assign an
171
+ * object to tune recovery budgets and terminal behavior.
172
172
  *
173
173
  * Assign this as a class field or in the constructor — NOT in `onStart()`.
174
174
  * On every wake the SDK evaluates recovery budgets (and may seal an
@@ -180,10 +180,9 @@ declare class AIChatAgent<
180
180
  /**
181
181
  * Inactivity watchdog for the live model/transport stream, in milliseconds.
182
182
  * If more than this many ms elapse between stream chunks, the turn is aborted
183
- * and — when {@link chatRecovery} is enabled — routed into bounded recovery
184
- * (the same continuation machinery as a deploy/eviction interruption, #1626)
185
- * rather than parking forever on a hung provider. With recovery disabled, a
186
- * stall instead surfaces as a terminal stream error (kills the spinner).
183
+ * and routed into bounded recovery (the same continuation machinery as a
184
+ * deploy or eviction interruption, #1626) rather than parking forever on a
185
+ * hung provider.
187
186
  *
188
187
  * Default `0` disables the watchdog (opt-in), matching `@cloudflare/think`.
189
188
  * A value such as `60_000` (60s) is a reasonable starting point; tune it
@@ -575,8 +574,8 @@ declare class AIChatAgent<
575
574
  * framework can't repair "inside" it the way Think does; it repairs
576
575
  * `this.messages` at each point right before handing control to
577
576
  * `onChatMessage` instead. This reaches the cases the recovery-only repair
578
- * missed: a mixed client+server orphan whose client replay drives an
579
- * auto-continuation, and any agent running with `chatRecovery` disabled.
577
+ * missed, including a mixed client+server orphan whose client replay drives
578
+ * an auto-continuation.
580
579
  *
581
580
  * Scope: repair only ever flips a DEAD SERVER orphan (an interrupted tool with
582
581
  * no settled result whose `execute()` died with the evicted isolate). A part
@@ -1295,9 +1294,7 @@ declare class AIChatAgent<
1295
1294
  * Unlike Think there is no durable-submission layer to complete here, so the
1296
1295
  * schedule payload carries no `recoveredRequestId`.
1297
1296
  *
1298
- * Returns `"disabled"` when chat recovery is off (the caller then surfaces the
1299
- * stall as a terminal stream error — the watchdog's "kill the spinner"
1300
- * guarantee), `"exhausted"` when the budget was spent (terminal UX already
1297
+ * Returns `"exhausted"` when the budget was spent (terminal UX already
1301
1298
  * delivered), or `"scheduled"` when a continuation was queued.
1302
1299
  */
1303
1300
  private _routeStallToBoundedRecovery;
package/dist/index.js CHANGED
@@ -146,7 +146,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
146
146
  this._interactionApplyTail = Promise.resolve();
147
147
  this._approvalPersistedMessageId = null;
148
148
  this._turnQueue = new TurnQueue();
149
- this.chatRecovery = false;
149
+ this.chatRecovery = true;
150
150
  this.chatStreamStallTimeoutMs = 0;
151
151
  this._mergeQueuedUserStartIndexByEpoch = /* @__PURE__ */ new Map();
152
152
  this._submitConcurrency = new SubmitConcurrencyController({ defaultDebounceMs: AIChatAgent.MESSAGE_DEBOUNCE_MS });
@@ -372,8 +372,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
372
372
  this._settlePreStreamTurn(chatMessageId);
373
373
  }
374
374
  };
375
- if (this.chatRecovery) await this._runChatRecoveryFiber(chatMessageId, false, chatTurnBody);
376
- else await chatTurnBody();
375
+ await this._runChatRecoveryFiber(chatMessageId, false, chatTurnBody);
377
376
  });
378
377
  });
379
378
  }, {
@@ -796,8 +795,8 @@ var AIChatAgent = class AIChatAgent extends Agent {
796
795
  * framework can't repair "inside" it the way Think does; it repairs
797
796
  * `this.messages` at each point right before handing control to
798
797
  * `onChatMessage` instead. This reaches the cases the recovery-only repair
799
- * missed: a mixed client+server orphan whose client replay drives an
800
- * auto-continuation, and any agent running with `chatRecovery` disabled.
798
+ * missed, including a mixed client+server orphan whose client replay drives
799
+ * an auto-continuation.
801
800
  *
802
801
  * Scope: repair only ever flips a DEAD SERVER orphan (an interrupted tool with
803
802
  * no settled result whose `execute()` died with the evicted isolate). A part
@@ -1372,8 +1371,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
1372
1371
  this._abortRegistry.remove(requestId);
1373
1372
  }
1374
1373
  };
1375
- if (this.chatRecovery) await this._runChatRecoveryFiber(requestId, true, autoContinuationBody);
1376
- else await autoContinuationBody();
1374
+ await this._runChatRecoveryFiber(requestId, true, autoContinuationBody);
1377
1375
  });
1378
1376
  });
1379
1377
  } finally {
@@ -1416,8 +1414,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
1416
1414
  });
1417
1415
  if (response) status = await this._reply(requestId, response, [], { chatMessageId: requestId });
1418
1416
  };
1419
- if (this.chatRecovery) await this._runChatRecoveryFiber(requestId, false, programmaticBody);
1420
- else await programmaticBody();
1417
+ await this._runChatRecoveryFiber(requestId, false, programmaticBody);
1421
1418
  } finally {
1422
1419
  if (abortSignal?.aborted) wasAborted = true;
1423
1420
  detachExternal();
@@ -2268,8 +2265,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
2268
2265
  });
2269
2266
  });
2270
2267
  };
2271
- if (this.chatRecovery) await this._runChatRecoveryFiber(requestId, true, turnBody);
2272
- else await turnBody();
2268
+ await this._runChatRecoveryFiber(requestId, true, turnBody);
2273
2269
  }, { epoch });
2274
2270
  if (this._turnQueue.generation !== epoch && status === "completed") status = "skipped";
2275
2271
  else if (wasAborted && status === "completed") status = "aborted";
@@ -2656,13 +2652,10 @@ var AIChatAgent = class AIChatAgent extends Agent {
2656
2652
  * Unlike Think there is no durable-submission layer to complete here, so the
2657
2653
  * schedule payload carries no `recoveredRequestId`.
2658
2654
  *
2659
- * Returns `"disabled"` when chat recovery is off (the caller then surfaces the
2660
- * stall as a terminal stream error — the watchdog's "kill the spinner"
2661
- * guarantee), `"exhausted"` when the budget was spent (terminal UX already
2655
+ * Returns `"exhausted"` when the budget was spent (terminal UX already
2662
2656
  * delivered), or `"scheduled"` when a continuation was queued.
2663
2657
  */
2664
2658
  async _routeStallToBoundedRecovery(input) {
2665
- if (!this._resolveChatRecoveryConfig().enabled) return "disabled";
2666
2659
  const recoveryRootRequestId = this._activeChatRecoveryRootRequestId ?? input.requestId;
2667
2660
  const latestUserMessageId = [...this.messages].reverse().find((m) => m.role === "user")?.id ?? null;
2668
2661
  const { incident, config, exhausted } = await this._beginChatRecoveryIncident({