@cloudflare/ai-chat 0.7.1 → 0.8.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 +36 -5
- package/dist/index.d.ts +194 -14
- package/dist/index.js +745 -108
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +10 -0
- package/dist/react.js +75 -13
- package/dist/react.js.map +1 -1
- package/dist/types.d.ts +20 -1
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
package/dist/react.d.ts
CHANGED
|
@@ -401,6 +401,16 @@ declare function useAgentChat<
|
|
|
401
401
|
* Use this for showing a universal streaming indicator.
|
|
402
402
|
*/
|
|
403
403
|
isStreaming: boolean;
|
|
404
|
+
/**
|
|
405
|
+
* `true` while a durable chat turn is being recovered (interrupted by a
|
|
406
|
+
* deploy/eviction or a stream-stall watchdog abort and now resuming, #1620).
|
|
407
|
+
* Distinct from `isStreaming` — a recovering turn isn't producing tokens yet,
|
|
408
|
+
* so a client can show a "recovering…" hint instead of looking frozen. Most
|
|
409
|
+
* UIs treat `isStreaming || isRecovering` as "busy". Driven by the server's
|
|
410
|
+
* `CF_AGENT_CHAT_RECOVERING` frames (also replayed on connect for
|
|
411
|
+
* `@cloudflare/think`); cleared automatically on the next stream or terminal.
|
|
412
|
+
*/
|
|
413
|
+
isRecovering: boolean;
|
|
404
414
|
/**
|
|
405
415
|
* `true` when the current `status`/`isServerStreaming` activity is
|
|
406
416
|
* driven by a server-pushed tool continuation (i.e. the server is
|
package/dist/react.js
CHANGED
|
@@ -727,28 +727,57 @@ function useAgentChat(options) {
|
|
|
727
727
|
const statusRef = useRef(status);
|
|
728
728
|
statusRef.current = status;
|
|
729
729
|
const resumingToolContinuationRef = useRef(false);
|
|
730
|
+
const pendingToolContinuationRef = useRef(false);
|
|
731
|
+
const observedToolContinuationRequestIdRef = useRef(null);
|
|
732
|
+
const continuationLaunchTimerRef = useRef(null);
|
|
730
733
|
const continuationGenerationRef = useRef(0);
|
|
731
734
|
const [isToolContinuation, setIsToolContinuation] = useState(false);
|
|
732
735
|
const resetToolContinuation = useCallback(() => {
|
|
733
736
|
continuationGenerationRef.current++;
|
|
737
|
+
pendingToolContinuationRef.current = false;
|
|
734
738
|
resumingToolContinuationRef.current = false;
|
|
739
|
+
observedToolContinuationRequestIdRef.current = null;
|
|
740
|
+
if (continuationLaunchTimerRef.current) {
|
|
741
|
+
clearTimeout(continuationLaunchTimerRef.current);
|
|
742
|
+
continuationLaunchTimerRef.current = null;
|
|
743
|
+
}
|
|
735
744
|
setIsToolContinuation(false);
|
|
736
745
|
}, []);
|
|
746
|
+
const scheduleToolContinuationLaunch = useCallback(() => {
|
|
747
|
+
if (!pendingToolContinuationRef.current || statusRef.current !== "ready" || continuationLaunchTimerRef.current) return;
|
|
748
|
+
continuationLaunchTimerRef.current = setTimeout(() => {
|
|
749
|
+
continuationLaunchTimerRef.current = null;
|
|
750
|
+
if (!pendingToolContinuationRef.current || statusRef.current !== "ready") return;
|
|
751
|
+
pendingToolContinuationRef.current = false;
|
|
752
|
+
const myGeneration = continuationGenerationRef.current;
|
|
753
|
+
customTransport.expectToolContinuation();
|
|
754
|
+
resumeStream().catch((error) => {
|
|
755
|
+
console.error("[useAgentChat] Tool continuation resume failed:", error);
|
|
756
|
+
}).finally(() => {
|
|
757
|
+
if (continuationGenerationRef.current !== myGeneration) return;
|
|
758
|
+
resumingToolContinuationRef.current = false;
|
|
759
|
+
setIsToolContinuation(false);
|
|
760
|
+
});
|
|
761
|
+
}, 0);
|
|
762
|
+
}, [customTransport, resumeStream]);
|
|
737
763
|
const startToolContinuation = useCallback(() => {
|
|
738
764
|
if (!autoContinueAfterToolResult || resumingToolContinuationRef.current) return;
|
|
739
|
-
|
|
765
|
+
++continuationGenerationRef.current;
|
|
740
766
|
resumingToolContinuationRef.current = true;
|
|
767
|
+
pendingToolContinuationRef.current = true;
|
|
741
768
|
setIsToolContinuation(true);
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
769
|
+
scheduleToolContinuationLaunch();
|
|
770
|
+
}, [autoContinueAfterToolResult, scheduleToolContinuationLaunch]);
|
|
771
|
+
useEffect(() => {
|
|
772
|
+
if (status === "error" && pendingToolContinuationRef.current) {
|
|
773
|
+
resetToolContinuation();
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
scheduleToolContinuationLaunch();
|
|
748
777
|
}, [
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
778
|
+
resetToolContinuation,
|
|
779
|
+
scheduleToolContinuationLaunch,
|
|
780
|
+
status
|
|
752
781
|
]);
|
|
753
782
|
const stopWithToolContinuationAbort = useCallback(async () => {
|
|
754
783
|
try {
|
|
@@ -1030,6 +1059,7 @@ function useAgentChat(options) {
|
|
|
1030
1059
|
]);
|
|
1031
1060
|
const streamStateRef = useRef({ status: "idle" });
|
|
1032
1061
|
const [isServerStreaming, setIsServerStreaming] = useState(false);
|
|
1062
|
+
const [isRecovering, setIsRecovering] = useState(false);
|
|
1033
1063
|
useEffect(() => {
|
|
1034
1064
|
const localResponseIds = localResponseMessageIdsRef.current;
|
|
1035
1065
|
/**
|
|
@@ -1048,8 +1078,12 @@ function useAgentChat(options) {
|
|
|
1048
1078
|
case "cf_agent_chat_clear":
|
|
1049
1079
|
streamStateRef.current = broadcastTransition(streamStateRef.current, { type: "clear" }).state;
|
|
1050
1080
|
setIsServerStreaming(false);
|
|
1081
|
+
setIsRecovering(false);
|
|
1051
1082
|
resetLocalChatState();
|
|
1052
1083
|
break;
|
|
1084
|
+
case "cf_agent_chat_recovering":
|
|
1085
|
+
setIsRecovering(Boolean(data.recovering));
|
|
1086
|
+
break;
|
|
1053
1087
|
case "cf_agent_chat_messages":
|
|
1054
1088
|
setMessages(preserveProtectedStreamingAssistant(data.messages));
|
|
1055
1089
|
break;
|
|
@@ -1075,11 +1109,22 @@ function useAgentChat(options) {
|
|
|
1075
1109
|
case "cf_agent_stream_resume_none":
|
|
1076
1110
|
customTransport.handleStreamResumeNone();
|
|
1077
1111
|
break;
|
|
1078
|
-
case "cf_agent_stream_resuming":
|
|
1079
|
-
|
|
1112
|
+
case "cf_agent_stream_resuming": {
|
|
1113
|
+
const isEarlyToolContinuation = resumingToolContinuationRef.current && !customTransport.isAwaitingResume();
|
|
1114
|
+
if (!resume && !customTransport.isAwaitingResume()) {
|
|
1115
|
+
if (!isEarlyToolContinuation) return;
|
|
1116
|
+
}
|
|
1080
1117
|
if (!resumingToolContinuationRef.current) pendingReplayResumeRequestIdsRef.current.add(data.id);
|
|
1081
1118
|
if (customTransport.handleStreamResuming(data)) return;
|
|
1082
1119
|
if (localRequestIdsRef.current.has(data.id)) return;
|
|
1120
|
+
if (isEarlyToolContinuation) {
|
|
1121
|
+
pendingToolContinuationRef.current = false;
|
|
1122
|
+
observedToolContinuationRequestIdRef.current = data.id;
|
|
1123
|
+
if (continuationLaunchTimerRef.current) {
|
|
1124
|
+
clearTimeout(continuationLaunchTimerRef.current);
|
|
1125
|
+
continuationLaunchTimerRef.current = null;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1083
1128
|
streamStateRef.current = broadcastTransition(streamStateRef.current, {
|
|
1084
1129
|
type: "resume-fallback",
|
|
1085
1130
|
streamId: data.id,
|
|
@@ -1087,11 +1132,13 @@ function useAgentChat(options) {
|
|
|
1087
1132
|
}).state;
|
|
1088
1133
|
customTransport.observeServerTurn(data.id);
|
|
1089
1134
|
setIsServerStreaming(true);
|
|
1135
|
+
setIsRecovering(false);
|
|
1090
1136
|
agentRef.current.send(JSON.stringify({
|
|
1091
1137
|
type: "cf_agent_stream_resume_ack",
|
|
1092
1138
|
id: data.id
|
|
1093
1139
|
}));
|
|
1094
1140
|
break;
|
|
1141
|
+
}
|
|
1095
1142
|
case "cf_agent_use_chat_response": {
|
|
1096
1143
|
if (localRequestIdsRef.current.has(data.id)) {
|
|
1097
1144
|
if (data.body?.trim()) try {
|
|
@@ -1130,7 +1177,11 @@ function useAgentChat(options) {
|
|
|
1130
1177
|
console.warn("[useAgentChat] Failed to parse stream chunk:", parseError instanceof Error ? parseError.message : parseError, "body:", data.body?.slice(0, 100));
|
|
1131
1178
|
}
|
|
1132
1179
|
if (data.done || data.replayComplete) pendingReplayResumeRequestIdsRef.current.delete(data.id);
|
|
1133
|
-
if (data.done)
|
|
1180
|
+
if (data.done) {
|
|
1181
|
+
customTransport.handleServerTurnCompleted(data.id);
|
|
1182
|
+
setIsRecovering(false);
|
|
1183
|
+
}
|
|
1184
|
+
const completedObservedToolContinuation = data.done && observedToolContinuationRequestIdRef.current === data.id;
|
|
1134
1185
|
const result = broadcastTransition(streamStateRef.current, {
|
|
1135
1186
|
type: "response",
|
|
1136
1187
|
streamId: data.id,
|
|
@@ -1146,6 +1197,7 @@ function useAgentChat(options) {
|
|
|
1146
1197
|
streamStateRef.current = result.state;
|
|
1147
1198
|
if (result.messagesUpdate) setMessages(result.messagesUpdate);
|
|
1148
1199
|
setIsServerStreaming(result.isStreaming);
|
|
1200
|
+
if (completedObservedToolContinuation) resetToolContinuation();
|
|
1149
1201
|
break;
|
|
1150
1202
|
}
|
|
1151
1203
|
}
|
|
@@ -1155,6 +1207,7 @@ function useAgentChat(options) {
|
|
|
1155
1207
|
agent.removeEventListener("message", onAgentMessage);
|
|
1156
1208
|
streamStateRef.current = { status: "idle" };
|
|
1157
1209
|
setIsServerStreaming(false);
|
|
1210
|
+
setIsRecovering(false);
|
|
1158
1211
|
protectedStreamingAssistantRef.current = null;
|
|
1159
1212
|
localResponseIds.clear();
|
|
1160
1213
|
};
|
|
@@ -1164,6 +1217,7 @@ function useAgentChat(options) {
|
|
|
1164
1217
|
resume,
|
|
1165
1218
|
customTransport,
|
|
1166
1219
|
preserveProtectedStreamingAssistant,
|
|
1220
|
+
resetToolContinuation,
|
|
1167
1221
|
resetMatchingHydratedAssistantForReplay,
|
|
1168
1222
|
restoreProtectedStreamingAssistant,
|
|
1169
1223
|
resetLocalChatState
|
|
@@ -1273,6 +1327,14 @@ function useAgentChat(options) {
|
|
|
1273
1327
|
messages: messagesWithToolResults,
|
|
1274
1328
|
isServerStreaming: effectiveIsServerStreaming,
|
|
1275
1329
|
isStreaming,
|
|
1330
|
+
/**
|
|
1331
|
+
* True while a durable chat turn is being recovered (interrupted by a
|
|
1332
|
+
* deploy/eviction or a stream-stall watchdog abort and now resuming, #1620).
|
|
1333
|
+
* Distinct from `isStreaming` — a recovering turn isn't producing tokens
|
|
1334
|
+
* yet. Render a "recovering…" hint; most UIs treat `isStreaming ||
|
|
1335
|
+
* isRecovering` as "busy". Cleared automatically on the next stream/terminal.
|
|
1336
|
+
*/
|
|
1337
|
+
isRecovering,
|
|
1276
1338
|
isToolContinuation,
|
|
1277
1339
|
sendMessage: sendMessageWithStreamingProtection,
|
|
1278
1340
|
stop: stopWithToolContinuationAbort,
|