@cloudflare/ai-chat 0.3.1 → 0.3.2
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/experimental/forever.d.ts +1 -1
- package/dist/index.d.ts +623 -9
- package/dist/index.js +201 -682
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +7 -16
- package/dist/react.js +28 -86
- package/dist/react.js.map +1 -1
- package/package.json +4 -4
- package/dist/index-BZiT3pOq.d.ts +0 -901
- package/dist/message-builder-BAgcFJMf.js +0 -244
- package/dist/message-builder-BAgcFJMf.js.map +0 -1
package/dist/react.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ClientToolSchema,
|
|
3
|
+
ClientToolSchema as ClientToolSchema$1
|
|
4
|
+
} from "agents/chat";
|
|
2
5
|
import { UseChatOptions, useChat } from "@ai-sdk/react";
|
|
6
|
+
import { ChatInit, JSONSchema7, Tool, UIMessage } from "ai";
|
|
3
7
|
import { useAgent } from "agents/react";
|
|
4
8
|
|
|
5
9
|
//#region src/react.d.ts
|
|
@@ -37,19 +41,6 @@ type AITool<Input = unknown, Output = unknown> = {
|
|
|
37
41
|
*/
|
|
38
42
|
execute?: (input: Input) => Output | Promise<Output>;
|
|
39
43
|
};
|
|
40
|
-
/**
|
|
41
|
-
* Schema for a client tool sent to the server.
|
|
42
|
-
* This is the wire format — what gets sent in the request body.
|
|
43
|
-
* Must match the server-side ClientToolSchema type.
|
|
44
|
-
*
|
|
45
|
-
* Most apps do not need this directly — it is used internally when the
|
|
46
|
-
* `tools` option on `useAgentChat` contains tools with `execute` functions.
|
|
47
|
-
*/
|
|
48
|
-
type ClientToolSchema = {
|
|
49
|
-
/** Unique name for the tool */ name: string /** Human-readable description of what the tool does */;
|
|
50
|
-
description?: Tool["description"] /** JSON Schema defining the tool's input parameters */;
|
|
51
|
-
parameters?: JSONSchema7;
|
|
52
|
-
};
|
|
53
44
|
/**
|
|
54
45
|
* Extracts tool schemas from tools that have client-side execute functions.
|
|
55
46
|
* These schemas are automatically sent to the server with each request.
|
|
@@ -62,7 +53,7 @@ type ClientToolSchema = {
|
|
|
62
53
|
*/
|
|
63
54
|
declare function extractClientToolSchemas(
|
|
64
55
|
tools?: Record<string, AITool<unknown, unknown>>
|
|
65
|
-
): ClientToolSchema[] | undefined;
|
|
56
|
+
): ClientToolSchema$1[] | undefined;
|
|
66
57
|
type GetInitialMessagesOptions = {
|
|
67
58
|
agent: string;
|
|
68
59
|
name: string;
|
|
@@ -295,7 +286,7 @@ declare function useAgentChat<
|
|
|
295
286
|
//#endregion
|
|
296
287
|
export {
|
|
297
288
|
AITool,
|
|
298
|
-
ClientToolSchema,
|
|
289
|
+
type ClientToolSchema,
|
|
299
290
|
JSONSchemaType,
|
|
300
291
|
OnToolCallCallback,
|
|
301
292
|
PrepareSendMessagesRequestOptions,
|
package/dist/react.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MessageType } from "./types.js";
|
|
2
|
-
import {
|
|
3
|
-
import { getToolName, isToolUIPart } from "ai";
|
|
2
|
+
import { broadcastTransition } from "agents/chat";
|
|
4
3
|
import { nanoid } from "nanoid";
|
|
5
4
|
import { useChat } from "@ai-sdk/react";
|
|
5
|
+
import { getToolName, isToolUIPart } from "ai";
|
|
6
6
|
import { use, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
7
7
|
//#region src/ws-chat-transport.ts
|
|
8
8
|
/**
|
|
@@ -722,42 +722,8 @@ function useAgentChat(options) {
|
|
|
722
722
|
sendToolOutputToServer,
|
|
723
723
|
addToolResult
|
|
724
724
|
]);
|
|
725
|
-
|
|
726
|
-
* Contains the request ID, accumulated message parts, metadata, and a unique message ID.
|
|
727
|
-
* Used for both resumed streams and real-time broadcasts from other tabs.
|
|
728
|
-
* Metadata is captured from start/finish/message-metadata stream chunks
|
|
729
|
-
* so that it's included when the partial message is flushed to React state.
|
|
730
|
-
*/
|
|
731
|
-
const activeStreamRef = useRef(null);
|
|
725
|
+
const streamStateRef = useRef({ status: "idle" });
|
|
732
726
|
const [isServerStreaming, setIsServerStreaming] = useState(false);
|
|
733
|
-
/**
|
|
734
|
-
* Flush the active stream's accumulated parts into React state.
|
|
735
|
-
* Extracted as a helper so it can be called both during live streaming
|
|
736
|
-
* (per-chunk) and after replay completes (once, at done).
|
|
737
|
-
*/
|
|
738
|
-
const flushActiveStreamToMessages = useCallback((activeMsg) => {
|
|
739
|
-
setMessages((prevMessages) => {
|
|
740
|
-
let existingIdx = prevMessages.findIndex((m) => m.id === activeMsg.messageId);
|
|
741
|
-
if (existingIdx < 0 && activeMsg.continuation) {
|
|
742
|
-
for (let i = prevMessages.length - 1; i >= 0; i--) if (prevMessages[i].role === "assistant") {
|
|
743
|
-
existingIdx = i;
|
|
744
|
-
break;
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
const partialMessage = {
|
|
748
|
-
id: existingIdx >= 0 ? prevMessages[existingIdx].id : activeMsg.messageId,
|
|
749
|
-
role: "assistant",
|
|
750
|
-
parts: [...activeMsg.parts],
|
|
751
|
-
...activeMsg.metadata != null && { metadata: activeMsg.metadata }
|
|
752
|
-
};
|
|
753
|
-
if (existingIdx >= 0) {
|
|
754
|
-
const updated = [...prevMessages];
|
|
755
|
-
updated[existingIdx] = partialMessage;
|
|
756
|
-
return updated;
|
|
757
|
-
}
|
|
758
|
-
return [...prevMessages, partialMessage];
|
|
759
|
-
});
|
|
760
|
-
}, [setMessages]);
|
|
761
727
|
useEffect(() => {
|
|
762
728
|
const localResponseIds = localResponseMessageIdsRef.current;
|
|
763
729
|
/**
|
|
@@ -776,6 +742,8 @@ function useAgentChat(options) {
|
|
|
776
742
|
case MessageType.CF_AGENT_CHAT_CLEAR:
|
|
777
743
|
localResponseIds.clear();
|
|
778
744
|
protectedStreamingAssistantRef.current = null;
|
|
745
|
+
streamStateRef.current = broadcastTransition(streamStateRef.current, { type: "clear" }).state;
|
|
746
|
+
setIsServerStreaming(false);
|
|
779
747
|
setMessages([]);
|
|
780
748
|
break;
|
|
781
749
|
case MessageType.CF_AGENT_CHAT_MESSAGES:
|
|
@@ -807,13 +775,11 @@ function useAgentChat(options) {
|
|
|
807
775
|
if (!resume && !customTransport.isAwaitingResume()) return;
|
|
808
776
|
if (customTransport.handleStreamResuming(data)) return;
|
|
809
777
|
if (localRequestIdsRef.current.has(data.id)) return;
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
messageId: nanoid()
|
|
814
|
-
|
|
815
|
-
parts: []
|
|
816
|
-
};
|
|
778
|
+
streamStateRef.current = broadcastTransition(streamStateRef.current, {
|
|
779
|
+
type: "resume-fallback",
|
|
780
|
+
streamId: data.id,
|
|
781
|
+
messageId: nanoid()
|
|
782
|
+
}).state;
|
|
817
783
|
setIsServerStreaming(true);
|
|
818
784
|
agentRef.current.send(JSON.stringify({
|
|
819
785
|
type: MessageType.CF_AGENT_STREAM_RESUME_ACK,
|
|
@@ -833,51 +799,28 @@ function useAgentChat(options) {
|
|
|
833
799
|
}
|
|
834
800
|
return;
|
|
835
801
|
}
|
|
836
|
-
|
|
837
|
-
if (!activeStreamRef.current || activeStreamRef.current.id !== data.id) {
|
|
838
|
-
let messageId = nanoid();
|
|
839
|
-
let existingParts = [];
|
|
840
|
-
let existingMetadata;
|
|
841
|
-
if (isContinuation) {
|
|
842
|
-
const currentMessages = messagesRef.current;
|
|
843
|
-
for (let i = currentMessages.length - 1; i >= 0; i--) if (currentMessages[i].role === "assistant") {
|
|
844
|
-
messageId = currentMessages[i].id;
|
|
845
|
-
existingParts = [...currentMessages[i].parts];
|
|
846
|
-
if (currentMessages[i].metadata != null) existingMetadata = { ...currentMessages[i].metadata };
|
|
847
|
-
break;
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
activeStreamRef.current = {
|
|
851
|
-
id: data.id,
|
|
852
|
-
messageId,
|
|
853
|
-
continuation: isContinuation,
|
|
854
|
-
parts: existingParts,
|
|
855
|
-
metadata: existingMetadata
|
|
856
|
-
};
|
|
857
|
-
setIsServerStreaming(true);
|
|
858
|
-
}
|
|
859
|
-
const activeMsg = activeStreamRef.current;
|
|
860
|
-
const isReplay = data.replay === true;
|
|
802
|
+
let chunkData;
|
|
861
803
|
if (data.body?.trim()) try {
|
|
862
|
-
|
|
863
|
-
const handled = applyChunkToParts(activeMsg.parts, chunkData);
|
|
804
|
+
chunkData = JSON.parse(data.body);
|
|
864
805
|
if (typeof chunkData.type === "string" && chunkData.type.startsWith("data-") && onDataRef.current) onDataRef.current(chunkData);
|
|
865
|
-
if (!handled && (chunkData.type === "start" || chunkData.type === "finish" || chunkData.type === "message-metadata")) {
|
|
866
|
-
if (chunkData.messageId != null && chunkData.type === "start" && !isContinuation) activeMsg.messageId = chunkData.messageId;
|
|
867
|
-
if (chunkData.messageMetadata != null) activeMsg.metadata = activeMsg.metadata ? {
|
|
868
|
-
...activeMsg.metadata,
|
|
869
|
-
...chunkData.messageMetadata
|
|
870
|
-
} : { ...chunkData.messageMetadata };
|
|
871
|
-
}
|
|
872
|
-
if (!isReplay) flushActiveStreamToMessages(activeMsg);
|
|
873
806
|
} catch (parseError) {
|
|
874
807
|
console.warn("[useAgentChat] Failed to parse stream chunk:", parseError instanceof Error ? parseError.message : parseError, "body:", data.body?.slice(0, 100));
|
|
875
808
|
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
809
|
+
const result = broadcastTransition(streamStateRef.current, {
|
|
810
|
+
type: "response",
|
|
811
|
+
streamId: data.id,
|
|
812
|
+
messageId: nanoid(),
|
|
813
|
+
chunkData,
|
|
814
|
+
done: data.done,
|
|
815
|
+
error: data.error,
|
|
816
|
+
replay: data.replay,
|
|
817
|
+
replayComplete: data.replayComplete,
|
|
818
|
+
continuation: data.continuation,
|
|
819
|
+
currentMessages: data.continuation ? messagesRef.current : void 0
|
|
820
|
+
});
|
|
821
|
+
streamStateRef.current = result.state;
|
|
822
|
+
if (result.messagesUpdate) setMessages(result.messagesUpdate);
|
|
823
|
+
setIsServerStreaming(result.isStreaming);
|
|
881
824
|
break;
|
|
882
825
|
}
|
|
883
826
|
}
|
|
@@ -885,7 +828,7 @@ function useAgentChat(options) {
|
|
|
885
828
|
agent.addEventListener("message", onAgentMessage);
|
|
886
829
|
return () => {
|
|
887
830
|
agent.removeEventListener("message", onAgentMessage);
|
|
888
|
-
|
|
831
|
+
streamStateRef.current = { status: "idle" };
|
|
889
832
|
setIsServerStreaming(false);
|
|
890
833
|
protectedStreamingAssistantRef.current = null;
|
|
891
834
|
localResponseIds.clear();
|
|
@@ -894,7 +837,6 @@ function useAgentChat(options) {
|
|
|
894
837
|
agent,
|
|
895
838
|
setMessages,
|
|
896
839
|
resume,
|
|
897
|
-
flushActiveStreamToMessages,
|
|
898
840
|
customTransport,
|
|
899
841
|
preserveProtectedStreamingAssistant,
|
|
900
842
|
restoreProtectedStreamingAssistant
|