@cloudflare/ai-chat 0.8.4 → 0.8.5

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/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
  }, [
@@ -1143,6 +1155,7 @@ function useAgentChat(options) {
1143
1155
  if (!resumingToolContinuationRef.current) pendingReplayResumeRequestIdsRef.current.add(data.id);
1144
1156
  if (customTransport.handleStreamResuming(data)) return;
1145
1157
  if (localRequestIdsRef.current.has(data.id)) return;
1158
+ if (fallbackAckedResumeRequestIdsRef.current.has(data.id)) return;
1146
1159
  if (isEarlyToolContinuation) {
1147
1160
  pendingToolContinuationRef.current = false;
1148
1161
  observedToolContinuationRequestIdRef.current = data.id;
@@ -1159,6 +1172,7 @@ function useAgentChat(options) {
1159
1172
  customTransport.observeServerTurn(data.id);
1160
1173
  setIsServerStreaming(true);
1161
1174
  setIsRecovering(false);
1175
+ fallbackAckedResumeRequestIdsRef.current.add(data.id);
1162
1176
  agentRef.current.send(JSON.stringify({
1163
1177
  type: "cf_agent_stream_resume_ack",
1164
1178
  id: data.id
@@ -1171,7 +1185,7 @@ function useAgentChat(options) {
1171
1185
  const chunkData = JSON.parse(data.body);
1172
1186
  if (chunkData.type === "start" && typeof chunkData.messageId === "string") {
1173
1187
  localResponseIds.set(data.id, chunkData.messageId);
1174
- if (data.replay && pendingReplayResumeRequestIdsRef.current.has(data.id)) {
1188
+ if (data.replay && !data.continuation && !resumingToolContinuationRef.current && observedToolContinuationRequestIdRef.current !== data.id) {
1175
1189
  pendingReplayResumeRequestIdsRef.current.delete(data.id);
1176
1190
  resetMatchingHydratedAssistantForReplay(chunkData.messageId);
1177
1191
  }
@@ -1187,6 +1201,7 @@ function useAgentChat(options) {
1187
1201
  restoreProtectedStreamingAssistant(localResponseIds.get(data.id));
1188
1202
  localResponseIds.delete(data.id);
1189
1203
  localRequestIdsRef.current.delete(data.id);
1204
+ fallbackAckedResumeRequestIdsRef.current.delete(data.id);
1190
1205
  }
1191
1206
  return;
1192
1207
  }
@@ -1194,7 +1209,7 @@ function useAgentChat(options) {
1194
1209
  if (data.replay && streamStateRef.current.status !== "observing" && !pendingReplayResumeRequestIdsRef.current.has(data.id)) return;
1195
1210
  if (data.body?.trim()) try {
1196
1211
  chunkData = JSON.parse(data.body);
1197
- if (data.replay && pendingReplayResumeRequestIdsRef.current.has(data.id) && typeof chunkData.messageId === "string" && chunkData.type === "start") {
1212
+ if (data.replay && !data.continuation && !resumingToolContinuationRef.current && observedToolContinuationRequestIdRef.current !== data.id && typeof chunkData.messageId === "string" && chunkData.type === "start") {
1198
1213
  pendingReplayResumeRequestIdsRef.current.delete(data.id);
1199
1214
  resetMatchingHydratedAssistantForReplay(chunkData.messageId);
1200
1215
  }
@@ -1205,6 +1220,7 @@ function useAgentChat(options) {
1205
1220
  if (data.done || data.replayComplete) pendingReplayResumeRequestIdsRef.current.delete(data.id);
1206
1221
  if (data.done) {
1207
1222
  customTransport.handleServerTurnCompleted(data.id);
1223
+ fallbackAckedResumeRequestIdsRef.current.delete(data.id);
1208
1224
  setIsRecovering(false);
1209
1225
  }
1210
1226
  const completedObservedToolContinuation = data.done && observedToolContinuationRequestIdRef.current === data.id;
@@ -1228,9 +1244,16 @@ function useAgentChat(options) {
1228
1244
  }
1229
1245
  }
1230
1246
  }
1247
+ const fallbackAckedResumeRequestIds = fallbackAckedResumeRequestIdsRef.current;
1248
+ function onAgentClose() {
1249
+ fallbackAckedResumeRequestIds.clear();
1250
+ }
1231
1251
  agent.addEventListener("message", onAgentMessage);
1252
+ agent.addEventListener("close", onAgentClose);
1232
1253
  return () => {
1233
1254
  agent.removeEventListener("message", onAgentMessage);
1255
+ agent.removeEventListener("close", onAgentClose);
1256
+ fallbackAckedResumeRequestIds.clear();
1234
1257
  streamStateRef.current = { status: "idle" };
1235
1258
  setIsServerStreaming(false);
1236
1259
  setIsRecovering(false);