@langgraph-js/sdk 1.1.5 → 1.1.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.
@@ -1,153 +1,167 @@
1
- import { atom } from "nanostores";
2
- import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient";
3
- import { Message, Thread } from "@langchain/langgraph-sdk";
4
- export const formatTime = (date: Date) => {
5
- return date.toLocaleTimeString("en-US");
6
- };
7
-
8
- export const formatTokens = (tokens: number) => {
9
- return tokens.toLocaleString("en");
10
- };
11
- export const getMessageContent = (content: any) => {
12
- if (typeof content === "string") return content;
13
- if (Array.isArray(content)) {
14
- return content
15
- .map((item) => {
16
- if (typeof item === "string") return item;
17
- if (item.type === "text") return item.text;
18
- if (item.type === "image_url") return `[图片]`;
19
- return JSON.stringify(item);
20
- })
21
- .join("");
22
- }
23
- return JSON.stringify(content);
24
- };
25
- export const createChatStore = (
26
- initClientName: string,
27
- config: LangGraphClientConfig,
28
- context: {
29
- onInit?: (client: LangGraphClient) => void;
30
- } = {}
31
- ) => {
32
- const client = atom<LangGraphClient | null>(null);
33
- const renderMessages = atom<RenderMessage[]>([]);
34
- const userInput = atom<string>("");
35
- const loading = atom<boolean>(false);
36
- const collapsedTools = atom<string[]>([]);
37
- const inChatError = atom<string | null>(null);
38
- const showHistory = atom<boolean>(true);
39
- const currentAgent = atom<string>(initClientName);
40
- const currentChatId = atom<string | null>(null);
41
-
42
- const initClient = async () => {
43
- const newClient = new LangGraphClient(config);
44
- await newClient.initAssistant(currentAgent.get());
45
- // 不再需要创建,sendMessage 会自动创建
46
- // await newClient.createThread();
47
-
48
- newClient.onStreamingUpdate((event) => {
49
- if (event.type === "thread" || event.type === "done") {
50
- // console.log(event.data);
51
- // 创建新会话时,需要自动刷新历史面板
52
- return refreshHistoryList();
53
- }
54
- if (event.type === "error") {
55
- loading.set(false);
56
- inChatError.set(event.data?.message || "发生错误");
57
- }
58
- // console.log(newClient.renderMessage);
59
- renderMessages.set(newClient.renderMessage);
60
- });
61
- context.onInit?.(newClient);
62
- // newClient.tools.bindTools([fileTool, askUserTool]);
63
- newClient.graphState = {};
64
- client.set(newClient);
65
- };
66
-
67
- const sendMessage = async (message?: Message[], extraData?: SendMessageOptions) => {
68
- if ((!userInput.get().trim() && !message?.length) || loading.get() || !client.get()) return;
69
-
70
- loading.set(true);
71
- inChatError.set(null);
72
-
73
- await client.get()?.sendMessage(message || userInput.get(), extraData);
74
-
75
- userInput.set("");
76
- loading.set(false);
77
- };
78
-
79
- const stopGeneration = () => {
80
- client.get()?.cancelRun();
81
- };
82
-
83
- const toggleToolCollapse = (toolId: string) => {
84
- const prev = collapsedTools.get();
85
- collapsedTools.set(prev.includes(toolId) ? prev.filter((id) => id !== toolId) : [...prev, toolId]);
86
- };
87
-
88
- const toggleHistoryVisible = () => {
89
- showHistory.set(!showHistory.get());
90
- };
91
- const historyList = atom<Thread<{ messages: Message[] }>[]>([]);
92
- const refreshHistoryList = async () => {
93
- if (!client.get()) return;
94
- try {
95
- const response = await client.get()?.listThreads<{ messages: Message[] }>();
96
- historyList.set(response || []);
97
- } catch (error) {
98
- console.error("Failed to fetch threads:", error);
99
- }
100
- };
101
-
102
- const addToHistory = (thread: Thread<{ messages: Message[] }>) => {
103
- const prev = historyList.get();
104
- historyList.set([thread, ...prev]);
105
- };
106
-
107
- return {
108
- data: {
109
- client,
110
- renderMessages,
111
- userInput,
112
- loading,
113
- inChatError,
114
- currentAgent,
115
- collapsedTools,
116
- showHistory,
117
- historyList,
118
- currentChatId,
119
- },
120
- mutations: {
121
- initClient,
122
- sendMessage,
123
- stopGeneration,
124
- toggleToolCollapse,
125
- toggleHistoryVisible,
126
- refreshHistoryList,
127
- addToHistory,
128
- setUserInput(input: string) {
129
- userInput.set(input);
130
- },
131
- setCurrentAgent(agent: string) {
132
- currentAgent.set(agent);
133
- return initClient().then(() => {
134
- refreshHistoryList();
135
- });
136
- },
137
- createNewChat() {
138
- client.get()?.reset();
139
- },
140
- toHistoryChat(
141
- thread: Thread<{
142
- messages: Message[];
143
- }>
144
- ) {
145
- client.get()?.resetThread(thread.metadata?.graph_id as string, thread.thread_id);
146
- },
147
- async deleteHistoryChat(thread: Thread<{ messages: Message[] }>) {
148
- await client.get()?.threads.delete(thread.thread_id);
149
- await refreshHistoryList();
150
- },
151
- },
152
- };
153
- };
1
+ import { atom } from "nanostores";
2
+ import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient";
3
+ import { Message, Thread } from "@langchain/langgraph-sdk";
4
+ export const formatTime = (date: Date) => {
5
+ return date.toLocaleTimeString("en-US");
6
+ };
7
+
8
+ export const formatTokens = (tokens: number) => {
9
+ return tokens.toLocaleString("en");
10
+ };
11
+ export const getMessageContent = (content: any) => {
12
+ if (typeof content === "string") return content;
13
+ if (Array.isArray(content)) {
14
+ return content
15
+ .map((item) => {
16
+ if (typeof item === "string") return item;
17
+ if (item.type === "text") return item.text;
18
+ if (item.type === "image_url") return `[图片]`;
19
+ return JSON.stringify(item);
20
+ })
21
+ .join("");
22
+ }
23
+ return JSON.stringify(content);
24
+ };
25
+ export const getHistoryContent = (thread: Thread) => {
26
+ const content = (thread?.values as any)?.messages?.[0]?.content;
27
+ if (content && Array.isArray(content)) {
28
+ return content.map((item: any) => {
29
+ if (item.type === "text") {
30
+ return item.text;
31
+ }
32
+ });
33
+ } else if (typeof content === "string") {
34
+ return content;
35
+ } else {
36
+ return "";
37
+ }
38
+ };
39
+ export const createChatStore = (
40
+ initClientName: string,
41
+ config: LangGraphClientConfig,
42
+ context: {
43
+ onInit?: (client: LangGraphClient) => void;
44
+ } = {}
45
+ ) => {
46
+ const client = atom<LangGraphClient | null>(null);
47
+ const renderMessages = atom<RenderMessage[]>([]);
48
+ const userInput = atom<string>("");
49
+ const loading = atom<boolean>(false);
50
+ const collapsedTools = atom<string[]>([]);
51
+ const inChatError = atom<string | null>(null);
52
+ const showHistory = atom<boolean>(true);
53
+ const currentAgent = atom<string>(initClientName);
54
+ const currentChatId = atom<string | null>(null);
55
+
56
+ const initClient = async () => {
57
+ const newClient = new LangGraphClient(config);
58
+ await newClient.initAssistant(currentAgent.get());
59
+ // 不再需要创建,sendMessage 会自动创建
60
+ // await newClient.createThread();
61
+
62
+ newClient.onStreamingUpdate((event) => {
63
+ if (event.type === "thread" || event.type === "done") {
64
+ // console.log(event.data);
65
+ // 创建新会话时,需要自动刷新历史面板
66
+ return refreshHistoryList();
67
+ }
68
+ if (event.type === "error") {
69
+ loading.set(false);
70
+ inChatError.set(event.data?.message || "发生错误");
71
+ }
72
+ // console.log(newClient.renderMessage);
73
+ renderMessages.set(newClient.renderMessage);
74
+ });
75
+ context.onInit?.(newClient);
76
+ // newClient.tools.bindTools([fileTool, askUserTool]);
77
+ newClient.graphState = {};
78
+ client.set(newClient);
79
+ };
80
+
81
+ const sendMessage = async (message?: Message[], extraData?: SendMessageOptions) => {
82
+ if ((!userInput.get().trim() && !message?.length) || loading.get() || !client.get()) return;
83
+
84
+ loading.set(true);
85
+ inChatError.set(null);
86
+
87
+ await client.get()?.sendMessage(message || userInput.get(), extraData);
88
+
89
+ userInput.set("");
90
+ loading.set(false);
91
+ };
92
+
93
+ const stopGeneration = () => {
94
+ client.get()?.cancelRun();
95
+ };
96
+
97
+ const toggleToolCollapse = (toolId: string) => {
98
+ const prev = collapsedTools.get();
99
+ collapsedTools.set(prev.includes(toolId) ? prev.filter((id) => id !== toolId) : [...prev, toolId]);
100
+ };
101
+
102
+ const toggleHistoryVisible = () => {
103
+ showHistory.set(!showHistory.get());
104
+ };
105
+ const historyList = atom<Thread<{ messages: Message[] }>[]>([]);
106
+ const refreshHistoryList = async () => {
107
+ if (!client.get()) return;
108
+ try {
109
+ const response = await client.get()?.listThreads<{ messages: Message[] }>();
110
+ historyList.set(response || []);
111
+ } catch (error) {
112
+ console.error("Failed to fetch threads:", error);
113
+ }
114
+ };
115
+
116
+ const addToHistory = (thread: Thread<{ messages: Message[] }>) => {
117
+ const prev = historyList.get();
118
+ historyList.set([thread, ...prev]);
119
+ };
120
+
121
+ return {
122
+ data: {
123
+ client,
124
+ renderMessages,
125
+ userInput,
126
+ loading,
127
+ inChatError,
128
+ currentAgent,
129
+ collapsedTools,
130
+ showHistory,
131
+ historyList,
132
+ currentChatId,
133
+ },
134
+ mutations: {
135
+ initClient,
136
+ sendMessage,
137
+ stopGeneration,
138
+ toggleToolCollapse,
139
+ toggleHistoryVisible,
140
+ refreshHistoryList,
141
+ addToHistory,
142
+ setUserInput(input: string) {
143
+ userInput.set(input);
144
+ },
145
+ setCurrentAgent(agent: string) {
146
+ currentAgent.set(agent);
147
+ return initClient().then(() => {
148
+ refreshHistoryList();
149
+ });
150
+ },
151
+ createNewChat() {
152
+ client.get()?.reset();
153
+ },
154
+ toHistoryChat(
155
+ thread: Thread<{
156
+ messages: Message[];
157
+ }>
158
+ ) {
159
+ client.get()?.resetThread(thread.metadata?.graph_id as string, thread.thread_id);
160
+ },
161
+ async deleteHistoryChat(thread: Thread<{ messages: Message[] }>) {
162
+ await client.get()?.threads.delete(thread.thread_id);
163
+ await refreshHistoryList();
164
+ },
165
+ },
166
+ };
167
+ };
@@ -1,2 +1,2 @@
1
- export * from "./createChatStore";
2
- export * from "./UnionStore";
1
+ export * from "./createChatStore";
2
+ export * from "./UnionStore";