@cloudflare/ai-chat 0.8.4 → 0.8.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/README.md +3 -3
- package/dist/index.d.ts +63 -4
- package/dist/index.js +123 -22
- package/dist/index.js.map +1 -1
- package/dist/react.js +46 -4
- package/dist/react.js.map +1 -1
- package/package.json +5 -5
package/dist/react.js
CHANGED
|
@@ -687,6 +687,17 @@ function useAgentChat(options) {
|
|
|
687
687
|
const localRequestIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
688
688
|
const pendingReplayResumeRequestIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
689
689
|
const replayHydratedAssistantMessageIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
690
|
+
/**
|
|
691
|
+
* Request ids this socket already ACKed via the fallback resume path.
|
|
692
|
+
* The server sends CF_AGENT_STREAM_RESUMING for the same request from
|
|
693
|
+
* both onConnect and its CF_AGENT_STREAM_RESUME_REQUEST handler (#1733).
|
|
694
|
+
* The transport-handled path dedupes the second notify via
|
|
695
|
+
* localRequestIdsRef, but the fallback path used to ACK both — triggering
|
|
696
|
+
* a second full-buffer replay that duplicated streamed parts. Entries are
|
|
697
|
+
* dropped when the turn completes; the whole set resets when the socket
|
|
698
|
+
* closes, since a new connection legitimately needs a fresh ACK+replay.
|
|
699
|
+
*/
|
|
700
|
+
const fallbackAckedResumeRequestIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
690
701
|
const customTransportRef = useRef(null);
|
|
691
702
|
if (customTransportRef.current === null) customTransportRef.current = new WebSocketChatTransport({
|
|
692
703
|
agent: agentRef.current,
|
|
@@ -907,6 +918,7 @@ function useAgentChat(options) {
|
|
|
907
918
|
processedToolCalls.current.clear();
|
|
908
919
|
localResponseMessageIdsRef.current.clear();
|
|
909
920
|
pendingReplayResumeRequestIdsRef.current.clear();
|
|
921
|
+
fallbackAckedResumeRequestIdsRef.current.clear();
|
|
910
922
|
replayHydratedAssistantMessageIdsRef.current.clear();
|
|
911
923
|
protectedStreamingAssistantRef.current = null;
|
|
912
924
|
}, [
|
|
@@ -1110,9 +1122,17 @@ function useAgentChat(options) {
|
|
|
1110
1122
|
case "cf_agent_chat_recovering":
|
|
1111
1123
|
setIsRecovering(Boolean(data.recovering));
|
|
1112
1124
|
break;
|
|
1113
|
-
case "cf_agent_chat_messages":
|
|
1114
|
-
|
|
1125
|
+
case "cf_agent_chat_messages": {
|
|
1126
|
+
let next = preserveProtectedStreamingAssistant(data.messages);
|
|
1127
|
+
const observed = streamStateRef.current;
|
|
1128
|
+
if (observed.status === "observing" && observed.accumulator.parts.length > 0) {
|
|
1129
|
+
const snapshotIdx = next.findIndex((m) => m.id === observed.accumulator.messageId);
|
|
1130
|
+
const snapshotParts = snapshotIdx >= 0 ? next[snapshotIdx].parts.length : 0;
|
|
1131
|
+
if (observed.accumulator.parts.length >= snapshotParts) next = observed.accumulator.mergeInto(next);
|
|
1132
|
+
}
|
|
1133
|
+
setMessages(next);
|
|
1115
1134
|
break;
|
|
1135
|
+
}
|
|
1116
1136
|
case "cf_agent_message_updated":
|
|
1117
1137
|
setMessages((prevMessages) => {
|
|
1118
1138
|
const updatedMessage = data.message;
|
|
@@ -1143,6 +1163,7 @@ function useAgentChat(options) {
|
|
|
1143
1163
|
if (!resumingToolContinuationRef.current) pendingReplayResumeRequestIdsRef.current.add(data.id);
|
|
1144
1164
|
if (customTransport.handleStreamResuming(data)) return;
|
|
1145
1165
|
if (localRequestIdsRef.current.has(data.id)) return;
|
|
1166
|
+
if (fallbackAckedResumeRequestIdsRef.current.has(data.id)) return;
|
|
1146
1167
|
if (isEarlyToolContinuation) {
|
|
1147
1168
|
pendingToolContinuationRef.current = false;
|
|
1148
1169
|
observedToolContinuationRequestIdRef.current = data.id;
|
|
@@ -1159,6 +1180,7 @@ function useAgentChat(options) {
|
|
|
1159
1180
|
customTransport.observeServerTurn(data.id);
|
|
1160
1181
|
setIsServerStreaming(true);
|
|
1161
1182
|
setIsRecovering(false);
|
|
1183
|
+
fallbackAckedResumeRequestIdsRef.current.add(data.id);
|
|
1162
1184
|
agentRef.current.send(JSON.stringify({
|
|
1163
1185
|
type: "cf_agent_stream_resume_ack",
|
|
1164
1186
|
id: data.id
|
|
@@ -1171,7 +1193,18 @@ function useAgentChat(options) {
|
|
|
1171
1193
|
const chunkData = JSON.parse(data.body);
|
|
1172
1194
|
if (chunkData.type === "start" && typeof chunkData.messageId === "string") {
|
|
1173
1195
|
localResponseIds.set(data.id, chunkData.messageId);
|
|
1174
|
-
if (data.
|
|
1196
|
+
if (!data.continuation) {
|
|
1197
|
+
if (protectedStreamingAssistantRef.current?.assistantId !== chunkData.messageId) {
|
|
1198
|
+
const msgs = messagesRef.current;
|
|
1199
|
+
const idx = msgs.findIndex((m) => m.id === chunkData.messageId);
|
|
1200
|
+
const anchorMessageId = idx >= 0 ? msgs[idx - 1]?.id ?? null : msgs[msgs.length - 1]?.id ?? null;
|
|
1201
|
+
protectedStreamingAssistantRef.current = {
|
|
1202
|
+
assistantId: chunkData.messageId,
|
|
1203
|
+
anchorMessageId
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
if (data.replay && !data.continuation && !resumingToolContinuationRef.current && observedToolContinuationRequestIdRef.current !== data.id) {
|
|
1175
1208
|
pendingReplayResumeRequestIdsRef.current.delete(data.id);
|
|
1176
1209
|
resetMatchingHydratedAssistantForReplay(chunkData.messageId);
|
|
1177
1210
|
}
|
|
@@ -1187,6 +1220,7 @@ function useAgentChat(options) {
|
|
|
1187
1220
|
restoreProtectedStreamingAssistant(localResponseIds.get(data.id));
|
|
1188
1221
|
localResponseIds.delete(data.id);
|
|
1189
1222
|
localRequestIdsRef.current.delete(data.id);
|
|
1223
|
+
fallbackAckedResumeRequestIdsRef.current.delete(data.id);
|
|
1190
1224
|
}
|
|
1191
1225
|
return;
|
|
1192
1226
|
}
|
|
@@ -1194,7 +1228,7 @@ function useAgentChat(options) {
|
|
|
1194
1228
|
if (data.replay && streamStateRef.current.status !== "observing" && !pendingReplayResumeRequestIdsRef.current.has(data.id)) return;
|
|
1195
1229
|
if (data.body?.trim()) try {
|
|
1196
1230
|
chunkData = JSON.parse(data.body);
|
|
1197
|
-
if (data.replay &&
|
|
1231
|
+
if (data.replay && !data.continuation && !resumingToolContinuationRef.current && observedToolContinuationRequestIdRef.current !== data.id && typeof chunkData.messageId === "string" && chunkData.type === "start") {
|
|
1198
1232
|
pendingReplayResumeRequestIdsRef.current.delete(data.id);
|
|
1199
1233
|
resetMatchingHydratedAssistantForReplay(chunkData.messageId);
|
|
1200
1234
|
}
|
|
@@ -1205,6 +1239,7 @@ function useAgentChat(options) {
|
|
|
1205
1239
|
if (data.done || data.replayComplete) pendingReplayResumeRequestIdsRef.current.delete(data.id);
|
|
1206
1240
|
if (data.done) {
|
|
1207
1241
|
customTransport.handleServerTurnCompleted(data.id);
|
|
1242
|
+
fallbackAckedResumeRequestIdsRef.current.delete(data.id);
|
|
1208
1243
|
setIsRecovering(false);
|
|
1209
1244
|
}
|
|
1210
1245
|
const completedObservedToolContinuation = data.done && observedToolContinuationRequestIdRef.current === data.id;
|
|
@@ -1228,9 +1263,16 @@ function useAgentChat(options) {
|
|
|
1228
1263
|
}
|
|
1229
1264
|
}
|
|
1230
1265
|
}
|
|
1266
|
+
const fallbackAckedResumeRequestIds = fallbackAckedResumeRequestIdsRef.current;
|
|
1267
|
+
function onAgentClose() {
|
|
1268
|
+
fallbackAckedResumeRequestIds.clear();
|
|
1269
|
+
}
|
|
1231
1270
|
agent.addEventListener("message", onAgentMessage);
|
|
1271
|
+
agent.addEventListener("close", onAgentClose);
|
|
1232
1272
|
return () => {
|
|
1233
1273
|
agent.removeEventListener("message", onAgentMessage);
|
|
1274
|
+
agent.removeEventListener("close", onAgentClose);
|
|
1275
|
+
fallbackAckedResumeRequestIds.clear();
|
|
1234
1276
|
streamStateRef.current = { status: "idle" };
|
|
1235
1277
|
setIsServerStreaming(false);
|
|
1236
1278
|
setIsRecovering(false);
|