@cloudflare/ai-chat 0.4.6 → 0.5.0
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 +12 -12
- package/dist/index.d.ts +37 -75
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +45 -9
- package/dist/react.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/dist/react.d.ts
CHANGED
|
@@ -157,7 +157,7 @@ declare function getAgentMessages<M extends UIMessage = UIMessage>(
|
|
|
157
157
|
type GetInitialMessagesOptions = {
|
|
158
158
|
agent: string;
|
|
159
159
|
name: string;
|
|
160
|
-
url
|
|
160
|
+
url?: string;
|
|
161
161
|
};
|
|
162
162
|
type UseChatParams<M extends UIMessage = UIMessage> = ChatInit<M> &
|
|
163
163
|
UseChatOptions<M>;
|
package/dist/react.js
CHANGED
|
@@ -513,13 +513,23 @@ function useAgentChat(options) {
|
|
|
513
513
|
onToolCallRef.current = onToolCall;
|
|
514
514
|
const onDataRef = useRef(onData);
|
|
515
515
|
onDataRef.current = onData;
|
|
516
|
-
const
|
|
517
|
-
agentUrl
|
|
518
|
-
|
|
519
|
-
const
|
|
516
|
+
const rawHttpUrl = agent.getHttpUrl();
|
|
517
|
+
const agentUrl = rawHttpUrl ? new URL(rawHttpUrl) : null;
|
|
518
|
+
if (agentUrl) agentUrl.searchParams.delete("_pk");
|
|
519
|
+
const agentUrlString = agentUrl?.toString() ?? null;
|
|
520
|
+
const agentIdentityKey = `${agent.agent ?? ""}|${agent.name ?? ""}`;
|
|
521
|
+
const resolvedInitialMessagesCacheKey = agentUrl ? `${agentUrl.origin}${agentUrl.pathname}|${agentIdentityKey}` : null;
|
|
522
|
+
const initialMessagesCacheKey = agentIdentityKey;
|
|
523
|
+
const stableChatIdRef = useRef(null);
|
|
524
|
+
const previousChatIdentityKeyRef = useRef(null);
|
|
525
|
+
const fallbackChatId = agentIdentityKey;
|
|
526
|
+
if (stableChatIdRef.current === null || previousChatIdentityKeyRef.current !== agentIdentityKey) stableChatIdRef.current = resolvedInitialMessagesCacheKey ?? fallbackChatId;
|
|
527
|
+
else if (resolvedInitialMessagesCacheKey && stableChatIdRef.current !== fallbackChatId && stableChatIdRef.current !== resolvedInitialMessagesCacheKey) stableChatIdRef.current = resolvedInitialMessagesCacheKey;
|
|
528
|
+
previousChatIdentityKeyRef.current = agentIdentityKey;
|
|
520
529
|
const agentRef = useRef(agent);
|
|
521
530
|
agentRef.current = agent;
|
|
522
531
|
async function defaultGetInitialMessagesFetch({ url }) {
|
|
532
|
+
if (!url) return [];
|
|
523
533
|
const getMessagesUrl = new URL(url);
|
|
524
534
|
getMessagesUrl.pathname += "/get-messages";
|
|
525
535
|
const response = await fetch(getMessagesUrl.toString(), {
|
|
@@ -546,10 +556,10 @@ function useAgentChat(options) {
|
|
|
546
556
|
requestCache.set(cacheKey, promise);
|
|
547
557
|
return promise;
|
|
548
558
|
}
|
|
549
|
-
const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
|
|
559
|
+
const initialMessagesPromise = !(getInitialMessages === null ? false : getInitialMessages ? true : !!agentUrlString) ? null : doGetInitialMessages({
|
|
550
560
|
agent: agent.agent,
|
|
551
561
|
name: agent.name,
|
|
552
|
-
url: agentUrlString
|
|
562
|
+
url: agentUrlString ?? void 0
|
|
553
563
|
}, initialMessagesCacheKey);
|
|
554
564
|
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : optionsInitialMessages ?? [];
|
|
555
565
|
useEffect(() => {
|
|
@@ -601,7 +611,7 @@ function useAgentChat(options) {
|
|
|
601
611
|
onData,
|
|
602
612
|
messages: initialMessages,
|
|
603
613
|
transport: customTransport,
|
|
604
|
-
id:
|
|
614
|
+
id: stableChatIdRef.current,
|
|
605
615
|
resume
|
|
606
616
|
});
|
|
607
617
|
const { messages: chatMessages, setMessages, addToolResult, addToolApprovalResponse, sendMessage, resumeStream, status, stop } = useChatHelpers;
|
|
@@ -633,13 +643,35 @@ function useAgentChat(options) {
|
|
|
633
643
|
const [clientToolResults, setClientToolResults] = useState(/* @__PURE__ */ new Map());
|
|
634
644
|
const messagesRef = useRef(chatMessages);
|
|
635
645
|
messagesRef.current = chatMessages;
|
|
646
|
+
const initialMessagesRef = useRef(initialMessages);
|
|
647
|
+
initialMessagesRef.current = initialMessages;
|
|
648
|
+
const seededInitialMessagesKeyRef = useRef(null);
|
|
649
|
+
const markInitialMessagesSeeded = useCallback(() => {
|
|
650
|
+
seededInitialMessagesKeyRef.current = initialMessagesCacheKey;
|
|
651
|
+
}, [initialMessagesCacheKey]);
|
|
652
|
+
useEffect(() => {
|
|
653
|
+
if (!initialMessagesPromise) return;
|
|
654
|
+
if (seededInitialMessagesKeyRef.current === initialMessagesCacheKey) return;
|
|
655
|
+
if (chatMessages.length > 0) {
|
|
656
|
+
markInitialMessagesSeeded();
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
markInitialMessagesSeeded();
|
|
660
|
+
setMessages(initialMessagesRef.current);
|
|
661
|
+
}, [
|
|
662
|
+
chatMessages.length,
|
|
663
|
+
initialMessagesCacheKey,
|
|
664
|
+
initialMessagesPromise,
|
|
665
|
+
markInitialMessagesSeeded,
|
|
666
|
+
setMessages
|
|
667
|
+
]);
|
|
636
668
|
const localResponseMessageIdsRef = useRef(/* @__PURE__ */ new Map());
|
|
637
669
|
const protectedStreamingAssistantRef = useRef(null);
|
|
638
670
|
const preserveProtectedStreamingAssistant = useCallback((messages) => {
|
|
639
671
|
const protection = protectedStreamingAssistantRef.current;
|
|
640
|
-
if (!protection) return messages;
|
|
672
|
+
if (!protection) return [...messages];
|
|
641
673
|
const protectedAssistant = messagesRef.current.find((message) => message.id === protection.assistantId) ?? messages.find((message) => message.id === protection.assistantId);
|
|
642
|
-
if (!protectedAssistant) return messages;
|
|
674
|
+
if (!protectedAssistant) return [...messages];
|
|
643
675
|
return [...messages.filter((message) => message.id !== protection.assistantId), protectedAssistant];
|
|
644
676
|
}, []);
|
|
645
677
|
const protectStreamingAssistantTail = useCallback(() => {
|
|
@@ -840,6 +872,7 @@ function useAgentChat(options) {
|
|
|
840
872
|
protectedStreamingAssistantRef.current = null;
|
|
841
873
|
streamStateRef.current = broadcastTransition(streamStateRef.current, { type: "clear" }).state;
|
|
842
874
|
setIsServerStreaming(false);
|
|
875
|
+
markInitialMessagesSeeded();
|
|
843
876
|
setMessages([]);
|
|
844
877
|
break;
|
|
845
878
|
case "cf_agent_chat_messages":
|
|
@@ -934,6 +967,7 @@ function useAgentChat(options) {
|
|
|
934
967
|
setMessages,
|
|
935
968
|
resume,
|
|
936
969
|
customTransport,
|
|
970
|
+
markInitialMessagesSeeded,
|
|
937
971
|
preserveProtectedStreamingAssistant,
|
|
938
972
|
restoreProtectedStreamingAssistant
|
|
939
973
|
]);
|
|
@@ -1033,6 +1067,7 @@ function useAgentChat(options) {
|
|
|
1033
1067
|
addToolResult: addToolResultAndSendMessage,
|
|
1034
1068
|
addToolApprovalResponse: addToolApprovalResponseAndNotifyServer,
|
|
1035
1069
|
clearHistory: () => {
|
|
1070
|
+
markInitialMessagesSeeded();
|
|
1036
1071
|
setMessages([]);
|
|
1037
1072
|
setClientToolResults(/* @__PURE__ */ new Map());
|
|
1038
1073
|
resumingToolContinuationRef.current = false;
|
|
@@ -1045,6 +1080,7 @@ function useAgentChat(options) {
|
|
|
1045
1080
|
let resolvedMessages;
|
|
1046
1081
|
if (typeof messagesOrUpdater === "function") resolvedMessages = messagesOrUpdater(messagesRef.current);
|
|
1047
1082
|
else resolvedMessages = messagesOrUpdater;
|
|
1083
|
+
if (resolvedMessages.length === 0) markInitialMessagesSeeded();
|
|
1048
1084
|
setMessages(resolvedMessages);
|
|
1049
1085
|
agent.send(JSON.stringify({
|
|
1050
1086
|
messages: resolvedMessages,
|