@cloudflare/ai-chat 0.4.5 → 0.4.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/index.d.ts +11 -0
- package/dist/index.js +56 -34
- package/dist/index.js.map +1 -1
- package/dist/react.js +24 -24
- package/dist/react.js.map +1 -1
- package/package.json +2 -2
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./types.js";
|
|
2
2
|
import { broadcastTransition } from "agents/chat";
|
|
3
3
|
import { nanoid } from "nanoid";
|
|
4
4
|
import { useChat } from "@ai-sdk/react";
|
|
@@ -99,7 +99,7 @@ var WebSocketChatTransport = class {
|
|
|
99
99
|
try {
|
|
100
100
|
agent.send(JSON.stringify({
|
|
101
101
|
id: requestId,
|
|
102
|
-
type:
|
|
102
|
+
type: "cf_agent_chat_request_cancel"
|
|
103
103
|
}));
|
|
104
104
|
} catch {}
|
|
105
105
|
finish(() => streamController.error(abortError), true);
|
|
@@ -111,7 +111,7 @@ var WebSocketChatTransport = class {
|
|
|
111
111
|
const onMessage = (event) => {
|
|
112
112
|
try {
|
|
113
113
|
const data = JSON.parse(event.data);
|
|
114
|
-
if (data.type !==
|
|
114
|
+
if (data.type !== "cf_agent_use_chat_response") return;
|
|
115
115
|
if (data.id !== requestId) return;
|
|
116
116
|
if (data.error) {
|
|
117
117
|
finish(() => controller.error(new Error(data.body || "Stream error")));
|
|
@@ -140,7 +140,7 @@ var WebSocketChatTransport = class {
|
|
|
140
140
|
method: "POST",
|
|
141
141
|
body: bodyPayload
|
|
142
142
|
},
|
|
143
|
-
type:
|
|
143
|
+
type: "cf_agent_use_chat_request"
|
|
144
144
|
}));
|
|
145
145
|
return stream;
|
|
146
146
|
}
|
|
@@ -166,13 +166,13 @@ var WebSocketChatTransport = class {
|
|
|
166
166
|
const requestId = data.id;
|
|
167
167
|
activeIds?.add(requestId);
|
|
168
168
|
this.agent.send(JSON.stringify({
|
|
169
|
-
type:
|
|
169
|
+
type: "cf_agent_stream_resume_ack",
|
|
170
170
|
id: requestId
|
|
171
171
|
}));
|
|
172
172
|
done(this._createResumeStream(requestId));
|
|
173
173
|
};
|
|
174
174
|
try {
|
|
175
|
-
this.agent.send(JSON.stringify({ type:
|
|
175
|
+
this.agent.send(JSON.stringify({ type: "cf_agent_stream_resume_request" }));
|
|
176
176
|
} catch {}
|
|
177
177
|
timeout = setTimeout(() => done(null), 5e3);
|
|
178
178
|
});
|
|
@@ -222,7 +222,7 @@ var WebSocketChatTransport = class {
|
|
|
222
222
|
}
|
|
223
223
|
try {
|
|
224
224
|
agent.send(JSON.stringify({
|
|
225
|
-
type:
|
|
225
|
+
type: "cf_agent_chat_request_cancel",
|
|
226
226
|
id: requestId
|
|
227
227
|
}));
|
|
228
228
|
} catch {}
|
|
@@ -245,7 +245,7 @@ var WebSocketChatTransport = class {
|
|
|
245
245
|
clearHandshakeResolvers(onResume, onResumeNone);
|
|
246
246
|
if (timeout) clearTimeout(timeout);
|
|
247
247
|
agent.send(JSON.stringify({
|
|
248
|
-
type:
|
|
248
|
+
type: "cf_agent_stream_resume_ack",
|
|
249
249
|
id: requestId
|
|
250
250
|
}));
|
|
251
251
|
};
|
|
@@ -257,7 +257,7 @@ var WebSocketChatTransport = class {
|
|
|
257
257
|
const onMessage = (event) => {
|
|
258
258
|
try {
|
|
259
259
|
const data = JSON.parse(event.data);
|
|
260
|
-
if (data.type !==
|
|
260
|
+
if (data.type !== "cf_agent_use_chat_response" || requestId == null || data.id !== requestId) return;
|
|
261
261
|
if (data.error) {
|
|
262
262
|
finish(() => controller.error(new Error(data.body || "Stream error")), onResume, onResumeNone);
|
|
263
263
|
return;
|
|
@@ -271,7 +271,7 @@ var WebSocketChatTransport = class {
|
|
|
271
271
|
};
|
|
272
272
|
agent.addEventListener("message", onMessage, { signal: streamController.signal });
|
|
273
273
|
try {
|
|
274
|
-
agent.send(JSON.stringify({ type:
|
|
274
|
+
agent.send(JSON.stringify({ type: "cf_agent_stream_resume_request" }));
|
|
275
275
|
} catch {
|
|
276
276
|
finish(() => controller.close());
|
|
277
277
|
}
|
|
@@ -304,7 +304,7 @@ var WebSocketChatTransport = class {
|
|
|
304
304
|
const onMessage = (event) => {
|
|
305
305
|
try {
|
|
306
306
|
const data = JSON.parse(event.data);
|
|
307
|
-
if (data.type !==
|
|
307
|
+
if (data.type !== "cf_agent_use_chat_response") return;
|
|
308
308
|
if (data.id !== requestId) return;
|
|
309
309
|
if (data.error) {
|
|
310
310
|
finish(() => controller.error(new Error(data.body || "Stream error")));
|
|
@@ -727,7 +727,7 @@ function useAgentChat(options) {
|
|
|
727
727
|
if (toolResults.length > 0) {
|
|
728
728
|
const clientToolSchemas = extractClientToolSchemas(currentTools);
|
|
729
729
|
for (const result of toolResults) agentRef.current.send(JSON.stringify({
|
|
730
|
-
type:
|
|
730
|
+
type: "cf_agent_tool_result",
|
|
731
731
|
toolCallId: result.toolCallId,
|
|
732
732
|
toolName: result.toolName,
|
|
733
733
|
output: result.output,
|
|
@@ -765,7 +765,7 @@ function useAgentChat(options) {
|
|
|
765
765
|
const sendToolOutputToServer = useCallback((toolCallId, toolName, output, state, errorText) => {
|
|
766
766
|
const shouldAutoContinue = state === "output-error" ? false : autoContinueAfterToolResult;
|
|
767
767
|
agentRef.current.send(JSON.stringify({
|
|
768
|
-
type:
|
|
768
|
+
type: "cf_agent_tool_result",
|
|
769
769
|
toolCallId,
|
|
770
770
|
toolName,
|
|
771
771
|
output,
|
|
@@ -779,7 +779,7 @@ function useAgentChat(options) {
|
|
|
779
779
|
}, [autoContinueAfterToolResult, startToolContinuation]);
|
|
780
780
|
const sendToolApprovalToServer = useCallback((toolCallId, approved) => {
|
|
781
781
|
agentRef.current.send(JSON.stringify({
|
|
782
|
-
type:
|
|
782
|
+
type: "cf_agent_tool_approval",
|
|
783
783
|
toolCallId,
|
|
784
784
|
approved,
|
|
785
785
|
autoContinue: autoContinueAfterToolResult
|
|
@@ -835,17 +835,17 @@ function useAgentChat(options) {
|
|
|
835
835
|
return;
|
|
836
836
|
}
|
|
837
837
|
switch (data.type) {
|
|
838
|
-
case
|
|
838
|
+
case "cf_agent_chat_clear":
|
|
839
839
|
localResponseIds.clear();
|
|
840
840
|
protectedStreamingAssistantRef.current = null;
|
|
841
841
|
streamStateRef.current = broadcastTransition(streamStateRef.current, { type: "clear" }).state;
|
|
842
842
|
setIsServerStreaming(false);
|
|
843
843
|
setMessages([]);
|
|
844
844
|
break;
|
|
845
|
-
case
|
|
845
|
+
case "cf_agent_chat_messages":
|
|
846
846
|
setMessages(preserveProtectedStreamingAssistant(data.messages));
|
|
847
847
|
break;
|
|
848
|
-
case
|
|
848
|
+
case "cf_agent_message_updated":
|
|
849
849
|
setMessages((prevMessages) => {
|
|
850
850
|
const updatedMessage = data.message;
|
|
851
851
|
let idx = prevMessages.findIndex((m) => m.id === updatedMessage.id);
|
|
@@ -864,10 +864,10 @@ function useAgentChat(options) {
|
|
|
864
864
|
return prevMessages;
|
|
865
865
|
});
|
|
866
866
|
break;
|
|
867
|
-
case
|
|
867
|
+
case "cf_agent_stream_resume_none":
|
|
868
868
|
customTransport.handleStreamResumeNone();
|
|
869
869
|
break;
|
|
870
|
-
case
|
|
870
|
+
case "cf_agent_stream_resuming":
|
|
871
871
|
if (!resume && !customTransport.isAwaitingResume()) return;
|
|
872
872
|
if (customTransport.handleStreamResuming(data)) return;
|
|
873
873
|
if (localRequestIdsRef.current.has(data.id)) return;
|
|
@@ -878,11 +878,11 @@ function useAgentChat(options) {
|
|
|
878
878
|
}).state;
|
|
879
879
|
setIsServerStreaming(true);
|
|
880
880
|
agentRef.current.send(JSON.stringify({
|
|
881
|
-
type:
|
|
881
|
+
type: "cf_agent_stream_resume_ack",
|
|
882
882
|
id: data.id
|
|
883
883
|
}));
|
|
884
884
|
break;
|
|
885
|
-
case
|
|
885
|
+
case "cf_agent_use_chat_response": {
|
|
886
886
|
if (localRequestIdsRef.current.has(data.id)) {
|
|
887
887
|
if (data.body?.trim()) try {
|
|
888
888
|
const chunkData = JSON.parse(data.body);
|
|
@@ -942,7 +942,7 @@ function useAgentChat(options) {
|
|
|
942
942
|
const toolName = "tool" in args ? args.tool : "";
|
|
943
943
|
const output = "output" in args ? args.output : void 0;
|
|
944
944
|
agentRef.current.send(JSON.stringify({
|
|
945
|
-
type:
|
|
945
|
+
type: "cf_agent_tool_result",
|
|
946
946
|
toolCallId,
|
|
947
947
|
toolName,
|
|
948
948
|
output,
|
|
@@ -1039,7 +1039,7 @@ function useAgentChat(options) {
|
|
|
1039
1039
|
processedToolCalls.current.clear();
|
|
1040
1040
|
localResponseMessageIdsRef.current.clear();
|
|
1041
1041
|
protectedStreamingAssistantRef.current = null;
|
|
1042
|
-
agent.send(JSON.stringify({ type:
|
|
1042
|
+
agent.send(JSON.stringify({ type: "cf_agent_chat_clear" }));
|
|
1043
1043
|
},
|
|
1044
1044
|
setMessages: (messagesOrUpdater) => {
|
|
1045
1045
|
let resolvedMessages;
|
|
@@ -1048,7 +1048,7 @@ function useAgentChat(options) {
|
|
|
1048
1048
|
setMessages(resolvedMessages);
|
|
1049
1049
|
agent.send(JSON.stringify({
|
|
1050
1050
|
messages: resolvedMessages,
|
|
1051
|
-
type:
|
|
1051
|
+
type: "cf_agent_chat_messages"
|
|
1052
1052
|
}));
|
|
1053
1053
|
}
|
|
1054
1054
|
};
|