@cloudflare/ai-chat 0.8.5 → 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/dist/react.js CHANGED
@@ -1122,9 +1122,17 @@ function useAgentChat(options) {
1122
1122
  case "cf_agent_chat_recovering":
1123
1123
  setIsRecovering(Boolean(data.recovering));
1124
1124
  break;
1125
- case "cf_agent_chat_messages":
1126
- setMessages(preserveProtectedStreamingAssistant(data.messages));
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);
1127
1134
  break;
1135
+ }
1128
1136
  case "cf_agent_message_updated":
1129
1137
  setMessages((prevMessages) => {
1130
1138
  const updatedMessage = data.message;
@@ -1185,6 +1193,17 @@ function useAgentChat(options) {
1185
1193
  const chunkData = JSON.parse(data.body);
1186
1194
  if (chunkData.type === "start" && typeof chunkData.messageId === "string") {
1187
1195
  localResponseIds.set(data.id, chunkData.messageId);
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
+ }
1188
1207
  if (data.replay && !data.continuation && !resumingToolContinuationRef.current && observedToolContinuationRequestIdRef.current !== data.id) {
1189
1208
  pendingReplayResumeRequestIdsRef.current.delete(data.id);
1190
1209
  resetMatchingHydratedAssistantForReplay(chunkData.messageId);