@cloudflare/ai-chat 0.7.0 → 0.7.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/react.js CHANGED
@@ -576,6 +576,13 @@ function moveMessageToEnd(messages, messageId) {
576
576
  result.push(msg);
577
577
  return result;
578
578
  }
579
+ function prependMissingHydratedMessages(hydratedMessages, currentMessages) {
580
+ if (currentMessages.length === 0) return hydratedMessages;
581
+ const currentMessageIds = new Set(currentMessages.map((message) => message.id));
582
+ const missingHydratedMessages = hydratedMessages.filter((message) => !currentMessageIds.has(message.id));
583
+ if (missingHydratedMessages.length === 0) return currentMessages;
584
+ return [...missingHydratedMessages, ...currentMessages];
585
+ }
579
586
  /**
580
587
  * React hook for building AI chat interfaces using an Agent
581
588
  * @param options Chat options including the agent connection
@@ -622,7 +629,6 @@ function useAgentChat(options) {
622
629
  const agentPathChanged = Array.isArray(agent.path) && previousAgentAddressKeyRef.current !== null && previousAgentAddressKeyRef.current !== agentAddressKey;
623
630
  if (stableChatIdRef.current === null) stableChatIdRef.current = resolvedInitialMessagesCacheKey ?? fallbackChatId;
624
631
  else if (previousAgentRef.current !== agent || agentPathChanged) stableChatIdRef.current = resolvedInitialMessagesCacheKey ?? fallbackChatId;
625
- else if (resolvedInitialMessagesCacheKey && stableChatIdRef.current === fallbackChatId) stableChatIdRef.current = resolvedInitialMessagesCacheKey;
626
632
  previousAgentRef.current = agent;
627
633
  previousAgentAddressKeyRef.current = agentAddressKey;
628
634
  const agentRef = useRef(agent);
@@ -767,14 +773,9 @@ function useAgentChat(options) {
767
773
  useEffect(() => {
768
774
  if (!initialMessagesPromise) return;
769
775
  if (seededInitialMessagesKeyRef.current === initialMessagesCacheKey) return;
770
- if (chatMessages.length > 0) {
771
- markInitialMessagesSeeded();
772
- return;
773
- }
774
776
  markInitialMessagesSeeded();
775
- setMessages(initialMessagesRef.current);
777
+ setMessages((prevMessages) => prependMissingHydratedMessages(initialMessagesRef.current, prevMessages));
776
778
  }, [
777
- chatMessages.length,
778
779
  initialMessagesCacheKey,
779
780
  initialMessagesPromise,
780
781
  markInitialMessagesSeeded,