@cloudflare/ai-chat 0.4.5 → 0.4.6
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 +11 -0
- package/dist/index.js +56 -34
- package/dist/index.js.map +1 -1
- package/dist/react.js +24 -24
- package/dist/react.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -367,6 +367,17 @@ declare class AIChatAgent<
|
|
|
367
367
|
private _loadMessagesFromDb;
|
|
368
368
|
private _tryCatchChat;
|
|
369
369
|
private isChatTurnActive;
|
|
370
|
+
/**
|
|
371
|
+
* Wait until the agent is fully idle — both the turn queue is drained
|
|
372
|
+
* AND no submits are in flight between the concurrency decision and
|
|
373
|
+
* `_runExclusiveChatTurn` (mid-`persistMessages` etc.).
|
|
374
|
+
*
|
|
375
|
+
* Just awaiting `_turnQueue.waitForIdle()` would miss submits whose
|
|
376
|
+
* handlers have bumped `_latestOverlappingSubmitSequence` and
|
|
377
|
+
* incremented `_pendingEnqueueCount` but haven't yet reached
|
|
378
|
+
* `_runExclusiveChatTurn` — those would race with anything calling
|
|
379
|
+
* `waitForIdle()` (tests, `waitUntilStable`, recovery code).
|
|
380
|
+
*/
|
|
370
381
|
private waitForIdle;
|
|
371
382
|
private _normalizeMessageConcurrency;
|
|
372
383
|
private _getSubmitConcurrencyDecision;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./types.js";
|
|
2
2
|
import { autoTransformMessages } from "./ai-chat-v5-migration.js";
|
|
3
3
|
import { Agent, __DO_NOT_USE_WILL_BREAK__agentContext } from "agents";
|
|
4
4
|
import { AbortRegistry, ContinuationState, ROW_MAX_BYTES, ResumableStream, TurnQueue, applyChunkToParts, byteLength, createToolsFromClientSchemas, sanitizeMessage } from "agents/chat";
|
|
@@ -190,7 +190,8 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
190
190
|
)`;
|
|
191
191
|
this._restoreRequestContext();
|
|
192
192
|
this._resumableStream = new ResumableStream(this.sql.bind(this));
|
|
193
|
-
|
|
193
|
+
const rawMessages = this._loadMessagesFromDb();
|
|
194
|
+
this.messages = autoTransformMessages(rawMessages);
|
|
194
195
|
this._abortRegistry = new AbortRegistry();
|
|
195
196
|
const _onConnect = this.onConnect.bind(this);
|
|
196
197
|
this.onConnect = async (connection, ctx) => {
|
|
@@ -214,7 +215,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
214
215
|
} catch (_error) {
|
|
215
216
|
return _onMessage(connection, message);
|
|
216
217
|
}
|
|
217
|
-
if (data.type ===
|
|
218
|
+
if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
|
|
218
219
|
const { body } = data.init;
|
|
219
220
|
if (!body) {
|
|
220
221
|
console.warn("[AIChatAgent] Received chat request with empty body, ignoring");
|
|
@@ -244,7 +245,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
244
245
|
try {
|
|
245
246
|
this._broadcastChatMessage({
|
|
246
247
|
messages: transformedMessages,
|
|
247
|
-
type:
|
|
248
|
+
type: "cf_agent_chat_messages"
|
|
248
249
|
}, [connection.id]);
|
|
249
250
|
await this.persistMessages(transformedMessages, [connection.id], { _deleteStaleRows: true });
|
|
250
251
|
if (concurrencyDecision.mergeQueuedMessages) await this._mergeQueuedUserMessages(epoch);
|
|
@@ -308,7 +309,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
308
309
|
body: "No response was generated by the agent.",
|
|
309
310
|
done: true,
|
|
310
311
|
id: chatMessageId,
|
|
311
|
-
type:
|
|
312
|
+
type: "cf_agent_use_chat_response"
|
|
312
313
|
}, [connection.id]);
|
|
313
314
|
}
|
|
314
315
|
} finally {
|
|
@@ -326,7 +327,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
326
327
|
onStale: () => this._completeSkippedRequest(connection, chatMessageId)
|
|
327
328
|
});
|
|
328
329
|
}
|
|
329
|
-
if (data.type ===
|
|
330
|
+
if (data.type === "cf_agent_chat_clear") {
|
|
330
331
|
this.resetTurnState();
|
|
331
332
|
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
332
333
|
this._resumableStream.clearAll();
|
|
@@ -336,28 +337,28 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
336
337
|
this._persistRequestContext();
|
|
337
338
|
this._persistedMessageCache.clear();
|
|
338
339
|
this.messages = [];
|
|
339
|
-
this._broadcastChatMessage({ type:
|
|
340
|
+
this._broadcastChatMessage({ type: "cf_agent_chat_clear" }, [connection.id]);
|
|
340
341
|
this._emit("message:clear");
|
|
341
342
|
return;
|
|
342
343
|
}
|
|
343
|
-
if (data.type ===
|
|
344
|
+
if (data.type === "cf_agent_chat_messages") {
|
|
344
345
|
const transformedMessages = autoTransformMessages(data.messages);
|
|
345
346
|
await this.persistMessages(transformedMessages, [connection.id]);
|
|
346
347
|
return;
|
|
347
348
|
}
|
|
348
|
-
if (data.type ===
|
|
349
|
+
if (data.type === "cf_agent_chat_request_cancel") {
|
|
349
350
|
this._abortRegistry.cancel(data.id);
|
|
350
351
|
this._emit("message:cancel", { requestId: data.id });
|
|
351
352
|
return;
|
|
352
353
|
}
|
|
353
|
-
if (data.type ===
|
|
354
|
-
if (this._resumableStream.hasActiveStream()) if (this._continuation.activeRequestId === this._resumableStream.activeRequestId && this._continuation.activeConnectionId !== null && this._continuation.activeConnectionId !== connection.id) connection.send(JSON.stringify({ type:
|
|
354
|
+
if (data.type === "cf_agent_stream_resume_request") {
|
|
355
|
+
if (this._resumableStream.hasActiveStream()) if (this._continuation.activeRequestId === this._resumableStream.activeRequestId && this._continuation.activeConnectionId !== null && this._continuation.activeConnectionId !== connection.id) connection.send(JSON.stringify({ type: "cf_agent_stream_resume_none" }));
|
|
355
356
|
else this._notifyStreamResuming(connection);
|
|
356
357
|
else if (this._continuation.pending !== null && this._continuation.pending.connectionId === connection.id) this._continuation.awaitingConnections.set(connection.id, connection);
|
|
357
|
-
else connection.send(JSON.stringify({ type:
|
|
358
|
+
else connection.send(JSON.stringify({ type: "cf_agent_stream_resume_none" }));
|
|
358
359
|
return;
|
|
359
360
|
}
|
|
360
|
-
if (data.type ===
|
|
361
|
+
if (data.type === "cf_agent_stream_resume_ack") {
|
|
361
362
|
this._pendingResumeConnections.delete(connection.id);
|
|
362
363
|
if (this._resumableStream.hasActiveStream() && this._resumableStream.activeRequestId === data.id) {
|
|
363
364
|
const orphanedStreamId = this._resumableStream.replayChunks(connection, this._resumableStream.activeRequestId);
|
|
@@ -365,7 +366,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
365
366
|
}
|
|
366
367
|
return;
|
|
367
368
|
}
|
|
368
|
-
if (data.type ===
|
|
369
|
+
if (data.type === "cf_agent_tool_result") {
|
|
369
370
|
const { toolCallId, toolName, output, state, errorText, autoContinue, clientTools } = data;
|
|
370
371
|
if (clientTools?.length) {
|
|
371
372
|
this._lastClientTools = clientTools;
|
|
@@ -384,7 +385,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
384
385
|
if (autoContinue) this._enqueueAutoContinuation(connection, clientTools ?? this._lastClientTools, this._lastBody, "[AIChatAgent] Tool continuation failed:", applyPromise);
|
|
385
386
|
return;
|
|
386
387
|
}
|
|
387
|
-
if (data.type ===
|
|
388
|
+
if (data.type === "cf_agent_tool_approval") {
|
|
388
389
|
const { toolCallId, approved, autoContinue } = data;
|
|
389
390
|
this._emit("tool:approval", {
|
|
390
391
|
toolCallId,
|
|
@@ -453,7 +454,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
453
454
|
if (!this._resumableStream.hasActiveStream()) return;
|
|
454
455
|
this._pendingResumeConnections.add(connection.id);
|
|
455
456
|
connection.send(JSON.stringify({
|
|
456
|
-
type:
|
|
457
|
+
type: "cf_agent_stream_resuming",
|
|
457
458
|
id: this._resumableStream.activeRequestId
|
|
458
459
|
}));
|
|
459
460
|
}
|
|
@@ -599,7 +600,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
599
600
|
body,
|
|
600
601
|
done: false,
|
|
601
602
|
id: event.id,
|
|
602
|
-
type:
|
|
603
|
+
type: "cf_agent_use_chat_response",
|
|
603
604
|
...continuation && { continuation: true }
|
|
604
605
|
});
|
|
605
606
|
}
|
|
@@ -632,8 +633,23 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
632
633
|
isChatTurnActive() {
|
|
633
634
|
return this._turnQueue.isActive;
|
|
634
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
* Wait until the agent is fully idle — both the turn queue is drained
|
|
638
|
+
* AND no submits are in flight between the concurrency decision and
|
|
639
|
+
* `_runExclusiveChatTurn` (mid-`persistMessages` etc.).
|
|
640
|
+
*
|
|
641
|
+
* Just awaiting `_turnQueue.waitForIdle()` would miss submits whose
|
|
642
|
+
* handlers have bumped `_latestOverlappingSubmitSequence` and
|
|
643
|
+
* incremented `_pendingEnqueueCount` but haven't yet reached
|
|
644
|
+
* `_runExclusiveChatTurn` — those would race with anything calling
|
|
645
|
+
* `waitForIdle()` (tests, `waitUntilStable`, recovery code).
|
|
646
|
+
*/
|
|
635
647
|
async waitForIdle() {
|
|
636
|
-
|
|
648
|
+
while (true) {
|
|
649
|
+
await this._turnQueue.waitForIdle();
|
|
650
|
+
if (this._pendingEnqueueCount === 0) return;
|
|
651
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
652
|
+
}
|
|
637
653
|
}
|
|
638
654
|
_normalizeMessageConcurrency() {
|
|
639
655
|
if (typeof this.messageConcurrency === "string") return this.messageConcurrency;
|
|
@@ -796,13 +812,13 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
796
812
|
body: "",
|
|
797
813
|
done: true,
|
|
798
814
|
id: requestId,
|
|
799
|
-
type:
|
|
815
|
+
type: "cf_agent_use_chat_response"
|
|
800
816
|
});
|
|
801
817
|
}
|
|
802
818
|
_rollbackDroppedSubmit(connection) {
|
|
803
819
|
this._sendDirectMessage(connection, {
|
|
804
820
|
messages: this._messagesForClientSync(),
|
|
805
|
-
type:
|
|
821
|
+
type: "cf_agent_chat_messages"
|
|
806
822
|
});
|
|
807
823
|
}
|
|
808
824
|
/** `true` when an assistant message is waiting on a client tool result or approval. */
|
|
@@ -821,7 +837,11 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
821
837
|
async waitUntilStable(options) {
|
|
822
838
|
const deadline = options?.timeout != null ? Date.now() + options.timeout : null;
|
|
823
839
|
while (true) {
|
|
824
|
-
|
|
840
|
+
while (true) {
|
|
841
|
+
if (await this._awaitWithDeadline(this._turnQueue.waitForIdle(), deadline) === TIMED_OUT) return false;
|
|
842
|
+
if (this._pendingEnqueueCount === 0) break;
|
|
843
|
+
if (await this._awaitWithDeadline(new Promise((resolve) => setTimeout(resolve, 5)), deadline) === TIMED_OUT) return false;
|
|
844
|
+
}
|
|
825
845
|
if (!this.hasPendingInteraction()) return true;
|
|
826
846
|
const pending = this._pendingInteractionPromise;
|
|
827
847
|
if (pending) {
|
|
@@ -1333,10 +1353,11 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1333
1353
|
}
|
|
1334
1354
|
}
|
|
1335
1355
|
if (this.maxPersistedMessages != null) this._enforceMaxPersistedMessages();
|
|
1336
|
-
|
|
1356
|
+
const persisted = this._loadMessagesFromDb();
|
|
1357
|
+
this.messages = autoTransformMessages(persisted);
|
|
1337
1358
|
this._broadcastChatMessage({
|
|
1338
1359
|
messages: mergedMessages,
|
|
1339
|
-
type:
|
|
1360
|
+
type: "cf_agent_chat_messages"
|
|
1340
1361
|
}, excludeBroadcastIds);
|
|
1341
1362
|
}
|
|
1342
1363
|
/**
|
|
@@ -1629,7 +1650,8 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1629
1650
|
where id = ${message.id}
|
|
1630
1651
|
`;
|
|
1631
1652
|
this._persistedMessageCache.set(message.id, json);
|
|
1632
|
-
|
|
1653
|
+
const persisted = this._loadMessagesFromDb();
|
|
1654
|
+
this.messages = autoTransformMessages(persisted);
|
|
1633
1655
|
}
|
|
1634
1656
|
}
|
|
1635
1657
|
if (!updated) {
|
|
@@ -1637,13 +1659,13 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1637
1659
|
return false;
|
|
1638
1660
|
}
|
|
1639
1661
|
if (isStreamingMessage) this._broadcastChatMessage({
|
|
1640
|
-
type:
|
|
1662
|
+
type: "cf_agent_message_updated",
|
|
1641
1663
|
message
|
|
1642
1664
|
});
|
|
1643
1665
|
else {
|
|
1644
1666
|
const broadcastMessage = this._findMessageByToolCallId(toolCallId);
|
|
1645
1667
|
if (broadcastMessage) this._broadcastChatMessage({
|
|
1646
|
-
type:
|
|
1668
|
+
type: "cf_agent_message_updated",
|
|
1647
1669
|
message: broadcastMessage
|
|
1648
1670
|
});
|
|
1649
1671
|
}
|
|
@@ -1704,7 +1726,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1704
1726
|
body: "",
|
|
1705
1727
|
done: true,
|
|
1706
1728
|
id,
|
|
1707
|
-
type:
|
|
1729
|
+
type: "cf_agent_use_chat_response",
|
|
1708
1730
|
...continuation && { continuation: true }
|
|
1709
1731
|
});
|
|
1710
1732
|
return "completed";
|
|
@@ -1791,7 +1813,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1791
1813
|
body: data.errorText ?? JSON.stringify(data),
|
|
1792
1814
|
done: false,
|
|
1793
1815
|
id,
|
|
1794
|
-
type:
|
|
1816
|
+
type: "cf_agent_use_chat_response"
|
|
1795
1817
|
});
|
|
1796
1818
|
break;
|
|
1797
1819
|
}
|
|
@@ -1814,7 +1836,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1814
1836
|
body: chunkBody,
|
|
1815
1837
|
done: false,
|
|
1816
1838
|
id,
|
|
1817
|
-
type:
|
|
1839
|
+
type: "cf_agent_use_chat_response",
|
|
1818
1840
|
...continuation && { continuation: true }
|
|
1819
1841
|
});
|
|
1820
1842
|
} catch (_error) {}
|
|
@@ -1826,7 +1848,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1826
1848
|
body: "",
|
|
1827
1849
|
done: true,
|
|
1828
1850
|
id,
|
|
1829
|
-
type:
|
|
1851
|
+
type: "cf_agent_use_chat_response",
|
|
1830
1852
|
...continuation && { continuation: true }
|
|
1831
1853
|
});
|
|
1832
1854
|
return "aborted";
|
|
@@ -1880,7 +1902,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1880
1902
|
body: "",
|
|
1881
1903
|
done: true,
|
|
1882
1904
|
id,
|
|
1883
|
-
type:
|
|
1905
|
+
type: "cf_agent_use_chat_response",
|
|
1884
1906
|
...continuation && { continuation: true }
|
|
1885
1907
|
});
|
|
1886
1908
|
return "completed";
|
|
@@ -1907,7 +1929,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1907
1929
|
body: "",
|
|
1908
1930
|
done: true,
|
|
1909
1931
|
id,
|
|
1910
|
-
type:
|
|
1932
|
+
type: "cf_agent_use_chat_response",
|
|
1911
1933
|
...continuation && { continuation: true }
|
|
1912
1934
|
});
|
|
1913
1935
|
return "aborted";
|
|
@@ -1946,7 +1968,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1946
1968
|
body: "",
|
|
1947
1969
|
done: true,
|
|
1948
1970
|
id,
|
|
1949
|
-
type:
|
|
1971
|
+
type: "cf_agent_use_chat_response",
|
|
1950
1972
|
...continuation && { continuation: true }
|
|
1951
1973
|
});
|
|
1952
1974
|
this._activateDeferredAutoContinuation();
|
|
@@ -1971,7 +1993,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1971
1993
|
done: true,
|
|
1972
1994
|
error: true,
|
|
1973
1995
|
id,
|
|
1974
|
-
type:
|
|
1996
|
+
type: "cf_agent_use_chat_response",
|
|
1975
1997
|
...continuation && { continuation: true }
|
|
1976
1998
|
});
|
|
1977
1999
|
this._emit("message:error", { error: error instanceof Error ? error.message : String(error) });
|