@cloudflare/ai-chat 0.4.0 → 0.4.1
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/dist/index.d.ts +8 -0
- package/dist/index.js +14 -7
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +26 -5
- package/dist/react.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -178,6 +178,14 @@ declare class AIChatAgent<
|
|
|
178
178
|
private _submitSequence;
|
|
179
179
|
/** Latest overlapping submit-message sequence kept for latest/debounce. */
|
|
180
180
|
private _latestOverlappingSubmitSequence;
|
|
181
|
+
/**
|
|
182
|
+
* Tracks requests that have passed concurrency decision but haven't
|
|
183
|
+
* yet been enqueued into `_turnQueue`. Bridges the gap caused by
|
|
184
|
+
* `await persistMessages()` between the decision and the enqueue,
|
|
185
|
+
* preventing a race where a subsequent message sees `queuedCount()=0`
|
|
186
|
+
* and skips concurrency handling.
|
|
187
|
+
*/
|
|
188
|
+
private _pendingEnqueueCount;
|
|
181
189
|
/** Active debounce timer handle, cleared on resetTurnState. */
|
|
182
190
|
private _activeDebounceTimer;
|
|
183
191
|
/** Resolve callback for the active debounce promise. */
|
package/dist/index.js
CHANGED
|
@@ -170,6 +170,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
170
170
|
this._mergeQueuedUserStartIndexByEpoch = /* @__PURE__ */ new Map();
|
|
171
171
|
this._submitSequence = 0;
|
|
172
172
|
this._latestOverlappingSubmitSequence = 0;
|
|
173
|
+
this._pendingEnqueueCount = 0;
|
|
173
174
|
this._activeDebounceTimer = null;
|
|
174
175
|
this._activeDebounceResolve = null;
|
|
175
176
|
this._pendingResumeConnections = /* @__PURE__ */ new Set();
|
|
@@ -239,12 +240,17 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
239
240
|
this._completeSkippedRequest(connection, chatMessageId);
|
|
240
241
|
return;
|
|
241
242
|
}
|
|
242
|
-
this.
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
243
|
+
this._pendingEnqueueCount++;
|
|
244
|
+
try {
|
|
245
|
+
this._broadcastChatMessage({
|
|
246
|
+
messages: transformedMessages,
|
|
247
|
+
type: MessageType.CF_AGENT_CHAT_MESSAGES
|
|
248
|
+
}, [connection.id]);
|
|
249
|
+
await this.persistMessages(transformedMessages, [connection.id], { _deleteStaleRows: true });
|
|
250
|
+
if (concurrencyDecision.mergeQueuedMessages) await this._mergeQueuedUserMessages(epoch);
|
|
251
|
+
} finally {
|
|
252
|
+
this._pendingEnqueueCount = Math.max(0, this._pendingEnqueueCount - 1);
|
|
253
|
+
}
|
|
248
254
|
return this._runExclusiveChatTurn(chatMessageId, async () => {
|
|
249
255
|
if (this._isSupersededSubmit(concurrencyDecision.submitSequence)) {
|
|
250
256
|
this._completeSkippedRequest(connection, chatMessageId);
|
|
@@ -638,7 +644,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
638
644
|
};
|
|
639
645
|
}
|
|
640
646
|
_getSubmitConcurrencyDecision(trigger) {
|
|
641
|
-
const queuedTurnsInCurrentEpoch = this._turnQueue.queuedCount();
|
|
647
|
+
const queuedTurnsInCurrentEpoch = this._turnQueue.queuedCount() + this._pendingEnqueueCount;
|
|
642
648
|
if (trigger !== "submit-message" || queuedTurnsInCurrentEpoch === 0) return {
|
|
643
649
|
action: "execute",
|
|
644
650
|
submitSequence: null,
|
|
@@ -844,6 +850,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
844
850
|
this._turnQueue.reset();
|
|
845
851
|
this._abortRegistry.destroyAll();
|
|
846
852
|
this._cancelActiveDebounce();
|
|
853
|
+
this._pendingEnqueueCount = 0;
|
|
847
854
|
this._pendingInteractionPromise = null;
|
|
848
855
|
this._continuation.sendResumeNone();
|
|
849
856
|
this._continuation.clearAll();
|