@langgraph-js/sdk 1.1.6 → 1.1.9
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/LangGraphClient.d.ts +98 -10
- package/dist/LangGraphClient.js +84 -8
- package/dist/SpendTime.d.ts +28 -0
- package/dist/SpendTime.js +28 -0
- package/dist/ToolManager.d.ts +37 -19
- package/dist/ToolManager.js +36 -18
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/tool/createTool.d.ts +1 -1
- package/dist/tool/createTool.js +1 -1
- package/dist/tool/index.d.ts +2 -2
- package/dist/tool/index.js +2 -2
- package/dist/tool/utils.d.ts +1 -1
- package/dist/ui-store/UnionStore.d.ts +8 -0
- package/dist/ui-store/UnionStore.js +4 -0
- package/dist/ui-store/createChatStore.d.ts +41 -1
- package/dist/ui-store/createChatStore.js +84 -9
- package/dist/ui-store/index.d.ts +2 -2
- package/dist/ui-store/index.js +2 -2
- package/dist/ui-store/rafDebounce.d.ts +6 -0
- package/dist/ui-store/rafDebounce.js +26 -0
- package/package.json +3 -5
- package/src/LangGraphClient.ts +108 -14
- package/src/SpendTime.ts +31 -0
- package/src/ToolManager.ts +42 -19
- package/src/index.ts +4 -4
- package/src/tool/createTool.ts +2 -2
- package/src/tool/index.ts +2 -2
- package/src/tool/utils.ts +1 -1
- package/src/ui-store/UnionStore.ts +10 -1
- package/src/ui-store/createChatStore.ts +91 -9
- package/src/ui-store/index.ts +2 -2
- package/src/ui-store/rafDebounce.ts +29 -0
- package/tsconfig.json +2 -2
- package/index.html +0 -12
- package/ui/index.ts +0 -182
- package/ui/tool.ts +0 -55
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./LangGraphClient";
|
|
2
|
-
export * from "./tool";
|
|
1
|
+
export * from "./LangGraphClient.js";
|
|
2
|
+
export * from "./tool/index.js";
|
|
3
3
|
export * from "@langchain/langgraph-sdk";
|
|
4
|
-
export * from "./ui-store";
|
|
5
|
-
export * from "./ToolManager";
|
|
4
|
+
export * from "./ui-store/index.js";
|
|
5
|
+
export * from "./ToolManager.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./LangGraphClient";
|
|
2
|
-
export * from "./tool";
|
|
1
|
+
export * from "./LangGraphClient.js";
|
|
2
|
+
export * from "./tool/index.js";
|
|
3
3
|
export * from "@langchain/langgraph-sdk";
|
|
4
|
-
export * from "./ui-store";
|
|
5
|
-
export * from "./ToolManager";
|
|
4
|
+
export * from "./ui-store/index.js";
|
|
5
|
+
export * from "./ToolManager.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
2
|
-
import { Action, Parameter } from "./copilotkit-actions";
|
|
2
|
+
import { Action, Parameter } from "./copilotkit-actions.js";
|
|
3
3
|
import { Message } from "@langchain/langgraph-sdk";
|
|
4
4
|
export interface UnionTool<Args extends ZodRawShape> {
|
|
5
5
|
name: string;
|
package/dist/tool/createTool.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils";
|
|
1
|
+
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
4
|
/** 用于格式校验 */
|
package/dist/tool/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createTool";
|
|
2
|
-
export * from "./copilotkit-actions";
|
|
1
|
+
export * from "./createTool.js";
|
|
2
|
+
export * from "./copilotkit-actions.js";
|
package/dist/tool/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createTool";
|
|
2
|
-
export * from "./copilotkit-actions";
|
|
1
|
+
export * from "./createTool.js";
|
|
2
|
+
export * from "./copilotkit-actions.js";
|
package/dist/tool/utils.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { PreinitializedWritableAtom, StoreValue } from "nanostores";
|
|
2
|
+
/**
|
|
3
|
+
* @zh UnionStore 类型用于合并 store 的 data 和 mutations,使其可以直接访问。
|
|
4
|
+
* @en The UnionStore type is used to merge the data and mutations of a store, allowing direct access.
|
|
5
|
+
*/
|
|
2
6
|
export type UnionStore<T extends {
|
|
3
7
|
data: Record<string, PreinitializedWritableAtom<any>>;
|
|
4
8
|
mutations: Record<string, any>;
|
|
5
9
|
}> = {
|
|
6
10
|
[k in keyof T["data"]]: StoreValue<T["data"][k]>;
|
|
7
11
|
} & T["mutations"];
|
|
12
|
+
/**
|
|
13
|
+
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
14
|
+
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
15
|
+
*/
|
|
8
16
|
export declare const useUnionStore: <T extends {
|
|
9
17
|
data: Record<string, any>;
|
|
10
18
|
mutations: Record<string, any>;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
3
|
+
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
4
|
+
*/
|
|
1
5
|
export const useUnionStore = (store, useStore) => {
|
|
2
6
|
const data = Object.fromEntries(Object.entries(store.data).map(([key, value]) => {
|
|
3
7
|
return [key, useStore(value)];
|
|
@@ -1,9 +1,29 @@
|
|
|
1
|
-
import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient";
|
|
1
|
+
import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient.js";
|
|
2
2
|
import { Message, Thread } from "@langchain/langgraph-sdk";
|
|
3
|
+
/**
|
|
4
|
+
* @zh 格式化日期对象为时间字符串。
|
|
5
|
+
* @en Formats a Date object into a time string.
|
|
6
|
+
*/
|
|
3
7
|
export declare const formatTime: (date: Date) => string;
|
|
8
|
+
/**
|
|
9
|
+
* @zh 格式化数字为带千位分隔符的字符串。
|
|
10
|
+
* @en Formats a number into a string with thousand separators.
|
|
11
|
+
*/
|
|
4
12
|
export declare const formatTokens: (tokens: number) => string;
|
|
13
|
+
/**
|
|
14
|
+
* @zh 获取消息内容的文本表示,处理不同类型的消息内容。
|
|
15
|
+
* @en Gets the text representation of message content, handling different types of message content.
|
|
16
|
+
*/
|
|
5
17
|
export declare const getMessageContent: (content: any) => string;
|
|
18
|
+
/**
|
|
19
|
+
* @zh 获取历史记录中 Thread 内容的文本表示。
|
|
20
|
+
* @en Gets the text representation of Thread content in history.
|
|
21
|
+
*/
|
|
6
22
|
export declare const getHistoryContent: (thread: Thread) => string | any[];
|
|
23
|
+
/**
|
|
24
|
+
* @zh 创建一个用于聊天界面的状态管理器 (store)。
|
|
25
|
+
* @en Creates a state manager (store) for the chat interface.
|
|
26
|
+
*/
|
|
7
27
|
export declare const createChatStore: (initClientName: string, config: LangGraphClientConfig, context?: {
|
|
8
28
|
onInit?: (client: LangGraphClient) => void;
|
|
9
29
|
}) => {
|
|
@@ -31,12 +51,32 @@ export declare const createChatStore: (initClientName: string, config: LangGraph
|
|
|
31
51
|
addToHistory: (thread: Thread<{
|
|
32
52
|
messages: Message[];
|
|
33
53
|
}>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* @zh 设置用户输入内容。
|
|
56
|
+
* @en Sets the user input content.
|
|
57
|
+
*/
|
|
34
58
|
setUserInput(input: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
61
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
62
|
+
*/
|
|
35
63
|
setCurrentAgent(agent: string): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* @zh 创建一个新的聊天会话。
|
|
66
|
+
* @en Creates a new chat session.
|
|
67
|
+
*/
|
|
36
68
|
createNewChat(): void;
|
|
69
|
+
/**
|
|
70
|
+
* @zh 切换到指定的历史聊天会话。
|
|
71
|
+
* @en Switches to the specified historical chat session.
|
|
72
|
+
*/
|
|
37
73
|
toHistoryChat(thread: Thread<{
|
|
38
74
|
messages: Message[];
|
|
39
75
|
}>): void;
|
|
76
|
+
/**
|
|
77
|
+
* @zh 删除指定的历史聊天会话。
|
|
78
|
+
* @en Deletes the specified historical chat session.
|
|
79
|
+
*/
|
|
40
80
|
deleteHistoryChat(thread: Thread<{
|
|
41
81
|
messages: Message[];
|
|
42
82
|
}>): Promise<void>;
|
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { atom } from "nanostores";
|
|
2
|
-
import { LangGraphClient } from "../LangGraphClient";
|
|
2
|
+
import { LangGraphClient } from "../LangGraphClient.js";
|
|
3
|
+
import { rafDebounce } from "./rafDebounce.js";
|
|
4
|
+
/**
|
|
5
|
+
* @zh 格式化日期对象为时间字符串。
|
|
6
|
+
* @en Formats a Date object into a time string.
|
|
7
|
+
*/
|
|
3
8
|
export const formatTime = (date) => {
|
|
4
9
|
return date.toLocaleTimeString("en-US");
|
|
5
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* @zh 格式化数字为带千位分隔符的字符串。
|
|
13
|
+
* @en Formats a number into a string with thousand separators.
|
|
14
|
+
*/
|
|
6
15
|
export const formatTokens = (tokens) => {
|
|
7
16
|
return tokens.toLocaleString("en");
|
|
8
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* @zh 获取消息内容的文本表示,处理不同类型的消息内容。
|
|
20
|
+
* @en Gets the text representation of message content, handling different types of message content.
|
|
21
|
+
*/
|
|
9
22
|
export const getMessageContent = (content) => {
|
|
10
23
|
if (typeof content === "string")
|
|
11
24
|
return content;
|
|
@@ -24,6 +37,10 @@ export const getMessageContent = (content) => {
|
|
|
24
37
|
}
|
|
25
38
|
return JSON.stringify(content);
|
|
26
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* @zh 获取历史记录中 Thread 内容的文本表示。
|
|
42
|
+
* @en Gets the text representation of Thread content in history.
|
|
43
|
+
*/
|
|
27
44
|
export const getHistoryContent = (thread) => {
|
|
28
45
|
var _a, _b, _c;
|
|
29
46
|
const content = (_c = (_b = (_a = thread === null || thread === void 0 ? void 0 : thread.values) === null || _a === void 0 ? void 0 : _a.messages) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.content;
|
|
@@ -41,6 +58,10 @@ export const getHistoryContent = (thread) => {
|
|
|
41
58
|
return "";
|
|
42
59
|
}
|
|
43
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* @zh 创建一个用于聊天界面的状态管理器 (store)。
|
|
63
|
+
* @en Creates a state manager (store) for the chat interface.
|
|
64
|
+
*/
|
|
44
65
|
export const createChatStore = (initClientName, config, context = {}) => {
|
|
45
66
|
const client = atom(null);
|
|
46
67
|
const renderMessages = atom([]);
|
|
@@ -51,14 +72,22 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
51
72
|
const showHistory = atom(true);
|
|
52
73
|
const currentAgent = atom(initClientName);
|
|
53
74
|
const currentChatId = atom(null);
|
|
54
|
-
const
|
|
75
|
+
const updateUI = rafDebounce((newClient) => {
|
|
76
|
+
renderMessages.set(newClient.renderMessage);
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* @zh 初始化 LangGraph 客户端。
|
|
80
|
+
* @en Initializes the LangGraph client.
|
|
81
|
+
*/
|
|
82
|
+
async function initClient() {
|
|
55
83
|
var _a;
|
|
56
84
|
const newClient = new LangGraphClient(config);
|
|
57
85
|
await newClient.initAssistant(currentAgent.get());
|
|
86
|
+
currentAgent.set(newClient.getCurrentAssistant().graph_id);
|
|
58
87
|
// 不再需要创建,sendMessage 会自动创建
|
|
59
88
|
// await newClient.createThread();
|
|
89
|
+
inChatError.set(null);
|
|
60
90
|
newClient.onStreamingUpdate((event) => {
|
|
61
|
-
var _a;
|
|
62
91
|
if (event.type === "thread" || event.type === "done") {
|
|
63
92
|
// console.log(event.data);
|
|
64
93
|
// 创建新会话时,需要自动刷新历史面板
|
|
@@ -66,16 +95,19 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
66
95
|
}
|
|
67
96
|
if (event.type === "error") {
|
|
68
97
|
loading.set(false);
|
|
69
|
-
inChatError.set(
|
|
98
|
+
inChatError.set(event.data);
|
|
70
99
|
}
|
|
71
100
|
// console.log(newClient.renderMessage);
|
|
72
|
-
|
|
101
|
+
updateUI(newClient);
|
|
73
102
|
});
|
|
74
103
|
(_a = context.onInit) === null || _a === void 0 ? void 0 : _a.call(context, newClient);
|
|
75
|
-
// newClient.tools.bindTools([fileTool, askUserTool]);
|
|
76
104
|
newClient.graphState = {};
|
|
77
105
|
client.set(newClient);
|
|
78
|
-
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @zh 发送消息。
|
|
109
|
+
* @en Sends a message.
|
|
110
|
+
*/
|
|
79
111
|
const sendMessage = async (message, extraData) => {
|
|
80
112
|
var _a;
|
|
81
113
|
if ((!userInput.get().trim() && !(message === null || message === void 0 ? void 0 : message.length)) || loading.get() || !client.get())
|
|
@@ -86,21 +118,37 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
86
118
|
userInput.set("");
|
|
87
119
|
loading.set(false);
|
|
88
120
|
};
|
|
121
|
+
/**
|
|
122
|
+
* @zh 停止当前的消息生成。
|
|
123
|
+
* @en Stops the current message generation.
|
|
124
|
+
*/
|
|
89
125
|
const stopGeneration = () => {
|
|
90
126
|
var _a;
|
|
91
127
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.cancelRun();
|
|
92
128
|
};
|
|
129
|
+
/**
|
|
130
|
+
* @zh 切换工具消息的折叠状态。
|
|
131
|
+
* @en Toggles the collapsed state of a tool message.
|
|
132
|
+
*/
|
|
93
133
|
const toggleToolCollapse = (toolId) => {
|
|
94
134
|
const prev = collapsedTools.get();
|
|
95
135
|
collapsedTools.set(prev.includes(toolId) ? prev.filter((id) => id !== toolId) : [...prev, toolId]);
|
|
96
136
|
};
|
|
137
|
+
/**
|
|
138
|
+
* @zh 切换历史记录面板的可见性。
|
|
139
|
+
* @en Toggles the visibility of the history panel.
|
|
140
|
+
*/
|
|
97
141
|
const toggleHistoryVisible = () => {
|
|
98
142
|
showHistory.set(!showHistory.get());
|
|
99
143
|
};
|
|
100
144
|
const historyList = atom([]);
|
|
145
|
+
/**
|
|
146
|
+
* @zh 刷新历史记录列表。
|
|
147
|
+
* @en Refreshes the history list.
|
|
148
|
+
*/
|
|
101
149
|
const refreshHistoryList = async () => {
|
|
102
150
|
var _a;
|
|
103
|
-
if (!client.get())
|
|
151
|
+
if (!client.get() || !showHistory.get())
|
|
104
152
|
return;
|
|
105
153
|
try {
|
|
106
154
|
const response = await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.listThreads());
|
|
@@ -110,6 +158,10 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
110
158
|
console.error("Failed to fetch threads:", error);
|
|
111
159
|
}
|
|
112
160
|
};
|
|
161
|
+
/**
|
|
162
|
+
* @zh 将一个 Thread 添加到历史记录列表的开头。
|
|
163
|
+
* @en Adds a Thread to the beginning of the history list.
|
|
164
|
+
*/
|
|
113
165
|
const addToHistory = (thread) => {
|
|
114
166
|
const prev = historyList.get();
|
|
115
167
|
historyList.set([thread, ...prev]);
|
|
@@ -135,23 +187,46 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
135
187
|
toggleHistoryVisible,
|
|
136
188
|
refreshHistoryList,
|
|
137
189
|
addToHistory,
|
|
190
|
+
/**
|
|
191
|
+
* @zh 设置用户输入内容。
|
|
192
|
+
* @en Sets the user input content.
|
|
193
|
+
*/
|
|
138
194
|
setUserInput(input) {
|
|
139
195
|
userInput.set(input);
|
|
140
196
|
},
|
|
197
|
+
/**
|
|
198
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
199
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
200
|
+
*/
|
|
141
201
|
setCurrentAgent(agent) {
|
|
142
202
|
currentAgent.set(agent);
|
|
143
203
|
return initClient().then(() => {
|
|
144
|
-
|
|
204
|
+
if (showHistory.get()) {
|
|
205
|
+
refreshHistoryList();
|
|
206
|
+
}
|
|
145
207
|
});
|
|
146
208
|
},
|
|
209
|
+
/**
|
|
210
|
+
* @zh 创建一个新的聊天会话。
|
|
211
|
+
* @en Creates a new chat session.
|
|
212
|
+
*/
|
|
147
213
|
createNewChat() {
|
|
148
214
|
var _a;
|
|
149
215
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.reset();
|
|
216
|
+
inChatError.set(null);
|
|
150
217
|
},
|
|
218
|
+
/**
|
|
219
|
+
* @zh 切换到指定的历史聊天会话。
|
|
220
|
+
* @en Switches to the specified historical chat session.
|
|
221
|
+
*/
|
|
151
222
|
toHistoryChat(thread) {
|
|
152
223
|
var _a, _b;
|
|
153
224
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.resetThread((_b = thread.metadata) === null || _b === void 0 ? void 0 : _b.graph_id, thread.thread_id);
|
|
154
225
|
},
|
|
226
|
+
/**
|
|
227
|
+
* @zh 删除指定的历史聊天会话。
|
|
228
|
+
* @en Deletes the specified historical chat session.
|
|
229
|
+
*/
|
|
155
230
|
async deleteHistoryChat(thread) {
|
|
156
231
|
var _a;
|
|
157
232
|
await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.threads.delete(thread.thread_id));
|
package/dist/ui-store/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createChatStore";
|
|
2
|
-
export * from "./UnionStore";
|
|
1
|
+
export * from "./createChatStore.js";
|
|
2
|
+
export * from "./UnionStore.js";
|
package/dist/ui-store/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createChatStore";
|
|
2
|
-
export * from "./UnionStore";
|
|
1
|
+
export * from "./createChatStore.js";
|
|
2
|
+
export * from "./UnionStore.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced function that executes once per animation frame
|
|
3
|
+
* @param callback - The function to debounce
|
|
4
|
+
* @returns A function that executes the callback on the next animation frame
|
|
5
|
+
*/
|
|
6
|
+
export declare function rafDebounce<T extends (...args: any[]) => any>(callback: T): (...args: Parameters<T>) => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced function that executes once per animation frame
|
|
3
|
+
* @param callback - The function to debounce
|
|
4
|
+
* @returns A function that executes the callback on the next animation frame
|
|
5
|
+
*/
|
|
6
|
+
export function rafDebounce(callback) {
|
|
7
|
+
let rafId = null;
|
|
8
|
+
let lastArgs = null;
|
|
9
|
+
// Return the debounced function
|
|
10
|
+
return function (...args) {
|
|
11
|
+
// Store the most recent arguments
|
|
12
|
+
lastArgs = args;
|
|
13
|
+
// Cancel any pending animation frame
|
|
14
|
+
if (rafId !== null) {
|
|
15
|
+
cancelAnimationFrame(rafId);
|
|
16
|
+
}
|
|
17
|
+
// Schedule execution on the next animation frame
|
|
18
|
+
rafId = requestAnimationFrame(() => {
|
|
19
|
+
if (lastArgs !== null) {
|
|
20
|
+
callback(...lastArgs);
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
}
|
|
23
|
+
rafId = null;
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langgraph-js/sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"description": "The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -29,14 +29,12 @@
|
|
|
29
29
|
"url": "https://github.com/KonghaYao/YaoAgent/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@langchain/langgraph-sdk": "^0.0.
|
|
32
|
+
"@langchain/langgraph-sdk": "^0.0.76",
|
|
33
33
|
"nanostores": "^1.0.1",
|
|
34
34
|
"zod": "^3.24.3",
|
|
35
35
|
"zod-to-json-schema": "^3.24.3"
|
|
36
36
|
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"vite": "^6.3.4"
|
|
39
|
-
},
|
|
37
|
+
"devDependencies": {},
|
|
40
38
|
"scripts": {
|
|
41
39
|
"build": "tsc",
|
|
42
40
|
"prepublish": "pnpm build"
|
package/src/LangGraphClient.ts
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import { Client, Thread, Message, Assistant, HumanMessage, AIMessage, ToolMessage, Command } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { ToolManager } from "./ToolManager";
|
|
3
|
-
import { CallToolResult } from "./tool";
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { ToolManager } from "./ToolManager.js";
|
|
3
|
+
import { CallToolResult } from "./tool/createTool.js";
|
|
4
|
+
interface AsyncCallerParams {
|
|
5
|
+
/**
|
|
6
|
+
* The maximum number of concurrent calls that can be made.
|
|
7
|
+
* Defaults to `Infinity`, which means no limit.
|
|
8
|
+
*/
|
|
9
|
+
maxConcurrency?: number;
|
|
10
|
+
/**
|
|
11
|
+
* The maximum number of retries that can be made for a single call,
|
|
12
|
+
* with an exponential backoff between each attempt. Defaults to 6.
|
|
13
|
+
*/
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
onFailedResponseHook?: any;
|
|
16
|
+
/**
|
|
17
|
+
* Specify a custom fetch implementation.
|
|
18
|
+
*
|
|
19
|
+
* By default we expect the `fetch` is available in the global scope.
|
|
20
|
+
*/
|
|
21
|
+
fetch?: typeof fetch | ((...args: any[]) => any);
|
|
22
|
+
}
|
|
6
23
|
export type RenderMessage = Message & {
|
|
7
24
|
/** 工具入参 ,聚合而来*/
|
|
8
25
|
tool_input?: string;
|
|
@@ -26,6 +43,8 @@ export type RenderMessage = Message & {
|
|
|
26
43
|
spend_time?: number;
|
|
27
44
|
/** 渲染时的唯一 id,聚合而来*/
|
|
28
45
|
unique_id?: string;
|
|
46
|
+
/** 工具调用是否完成 */
|
|
47
|
+
done?: boolean;
|
|
29
48
|
};
|
|
30
49
|
export type SendMessageOptions = {
|
|
31
50
|
extraParams?: Record<string, any>;
|
|
@@ -40,6 +59,10 @@ export interface LangGraphClientConfig {
|
|
|
40
59
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
41
60
|
}
|
|
42
61
|
|
|
62
|
+
/**
|
|
63
|
+
* @zh StreamingMessageType 类用于判断消息的类型。
|
|
64
|
+
* @en The StreamingMessageType class is used to determine the type of a message.
|
|
65
|
+
*/
|
|
43
66
|
export class StreamingMessageType {
|
|
44
67
|
static isUser(m: Message) {
|
|
45
68
|
return m.type === "human";
|
|
@@ -63,6 +86,10 @@ type StreamingUpdateEvent = {
|
|
|
63
86
|
|
|
64
87
|
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
65
88
|
|
|
89
|
+
/**
|
|
90
|
+
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
91
|
+
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
92
|
+
*/
|
|
66
93
|
export class LangGraphClient extends Client {
|
|
67
94
|
private currentAssistant: Assistant | null = null;
|
|
68
95
|
private currentThread: Thread | null = null;
|
|
@@ -81,14 +108,23 @@ export class LangGraphClient extends Client {
|
|
|
81
108
|
limit: 100,
|
|
82
109
|
});
|
|
83
110
|
}
|
|
84
|
-
|
|
111
|
+
/**
|
|
112
|
+
* @zh 初始化 Assistant。
|
|
113
|
+
* @en Initializes the Assistant.
|
|
114
|
+
*/
|
|
115
|
+
async initAssistant(agentName?: string) {
|
|
85
116
|
try {
|
|
86
117
|
const assistants = await this.listAssistants();
|
|
87
118
|
this.availableAssistants = assistants;
|
|
88
119
|
if (assistants.length > 0) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
120
|
+
if (agentName) {
|
|
121
|
+
console.log("agentName", agentName);
|
|
122
|
+
this.currentAssistant = assistants.find((assistant) => assistant.graph_id === agentName) || null;
|
|
123
|
+
if (!this.currentAssistant) {
|
|
124
|
+
throw new Error("Agent not found: " + agentName);
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
this.currentAssistant = assistants[0];
|
|
92
128
|
}
|
|
93
129
|
} else {
|
|
94
130
|
throw new Error("No assistants found");
|
|
@@ -99,6 +135,10 @@ export class LangGraphClient extends Client {
|
|
|
99
135
|
}
|
|
100
136
|
}
|
|
101
137
|
|
|
138
|
+
/**
|
|
139
|
+
* @zh 创建一个新的 Thread。
|
|
140
|
+
* @en Creates a new Thread.
|
|
141
|
+
*/
|
|
102
142
|
async createThread({
|
|
103
143
|
threadId,
|
|
104
144
|
}: {
|
|
@@ -114,12 +154,19 @@ export class LangGraphClient extends Client {
|
|
|
114
154
|
throw error;
|
|
115
155
|
}
|
|
116
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* @zh 列出所有的 Thread。
|
|
159
|
+
* @en Lists all Threads.
|
|
160
|
+
*/
|
|
117
161
|
async listThreads<T>() {
|
|
118
162
|
return this.threads.search<T>({
|
|
119
163
|
sortOrder: "desc",
|
|
120
164
|
});
|
|
121
165
|
}
|
|
122
|
-
/**
|
|
166
|
+
/**
|
|
167
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
168
|
+
* @en Resets the Thread data from history.
|
|
169
|
+
*/
|
|
123
170
|
async resetThread(agent: string, threadId: string) {
|
|
124
171
|
await this.initAssistant(agent);
|
|
125
172
|
this.currentThread = await this.threads.get(threadId);
|
|
@@ -155,7 +202,10 @@ export class LangGraphClient extends Client {
|
|
|
155
202
|
return message;
|
|
156
203
|
}
|
|
157
204
|
|
|
158
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
207
|
+
* @en Messages used for streaming rendering in the UI.
|
|
208
|
+
*/
|
|
159
209
|
get renderMessage() {
|
|
160
210
|
const previousMessage = new Map<string, Message>();
|
|
161
211
|
const result: Message[] = [];
|
|
@@ -214,7 +264,11 @@ export class LangGraphClient extends Client {
|
|
|
214
264
|
|
|
215
265
|
return this.attachInfoForMessage(this.composeToolMessages(result as RenderMessage[]));
|
|
216
266
|
}
|
|
217
|
-
|
|
267
|
+
/**
|
|
268
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
269
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
270
|
+
*/
|
|
271
|
+
private attachInfoForMessage(result: RenderMessage[]) {
|
|
218
272
|
let lastMessage: RenderMessage | null = null;
|
|
219
273
|
for (const message of result) {
|
|
220
274
|
const createTime = message.response_metadata?.create_time || "";
|
|
@@ -241,7 +295,11 @@ export class LangGraphClient extends Client {
|
|
|
241
295
|
}
|
|
242
296
|
return result;
|
|
243
297
|
}
|
|
244
|
-
|
|
298
|
+
/**
|
|
299
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
300
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
301
|
+
*/
|
|
302
|
+
private composeToolMessages(messages: RenderMessage[]): RenderMessage[] {
|
|
245
303
|
const result: RenderMessage[] = [];
|
|
246
304
|
const assistantToolMessages = new Map<string, { args: string }>();
|
|
247
305
|
const toolParentMessage = new Map<string, RenderMessage>();
|
|
@@ -261,7 +319,9 @@ export class LangGraphClient extends Client {
|
|
|
261
319
|
message.tool_input = typeof assistantToolMessage.args !== "string" ? JSON.stringify(assistantToolMessage.args) : assistantToolMessage.args;
|
|
262
320
|
if (message.additional_kwargs) {
|
|
263
321
|
message.additional_kwargs.done = true;
|
|
322
|
+
message.done = true;
|
|
264
323
|
} else {
|
|
324
|
+
message.done = true;
|
|
265
325
|
message.additional_kwargs = {
|
|
266
326
|
done: true,
|
|
267
327
|
};
|
|
@@ -275,6 +335,10 @@ export class LangGraphClient extends Client {
|
|
|
275
335
|
}
|
|
276
336
|
return result;
|
|
277
337
|
}
|
|
338
|
+
/**
|
|
339
|
+
* @zh 获取 Token 计数器信息。
|
|
340
|
+
* @en Gets the Token counter information.
|
|
341
|
+
*/
|
|
278
342
|
get tokenCounter() {
|
|
279
343
|
return this.graphMessages.reduce(
|
|
280
344
|
(acc, message) => {
|
|
@@ -302,6 +366,10 @@ export class LangGraphClient extends Client {
|
|
|
302
366
|
}
|
|
303
367
|
);
|
|
304
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* @zh 注册流式更新的回调函数。
|
|
371
|
+
* @en Registers a callback function for streaming updates.
|
|
372
|
+
*/
|
|
305
373
|
onStreamingUpdate(callback: StreamingUpdateCallback) {
|
|
306
374
|
this.streamingCallbacks.add(callback);
|
|
307
375
|
return () => {
|
|
@@ -314,11 +382,19 @@ export class LangGraphClient extends Client {
|
|
|
314
382
|
}
|
|
315
383
|
graphState: any = {};
|
|
316
384
|
currentRun?: { run_id: string };
|
|
385
|
+
/**
|
|
386
|
+
* @zh 取消当前的 Run。
|
|
387
|
+
* @en Cancels the current Run.
|
|
388
|
+
*/
|
|
317
389
|
cancelRun() {
|
|
318
390
|
if (this.currentThread?.thread_id && this.currentRun?.run_id) {
|
|
319
391
|
this.runs.cancel(this.currentThread!.thread_id, this.currentRun.run_id);
|
|
320
392
|
}
|
|
321
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
396
|
+
* @en Sends a message to the LangGraph backend.
|
|
397
|
+
*/
|
|
322
398
|
async sendMessage(input: string | Message[], { extraParams, _debug, command }: SendMessageOptions = {}) {
|
|
323
399
|
if (!this.currentAssistant) {
|
|
324
400
|
throw new Error("Thread or Assistant not initialized");
|
|
@@ -428,7 +504,10 @@ export class LangGraphClient extends Client {
|
|
|
428
504
|
const result = await this.tools.callTool(message.name!, args, { client: that, message });
|
|
429
505
|
return this.resume(result);
|
|
430
506
|
}
|
|
431
|
-
/**
|
|
507
|
+
/**
|
|
508
|
+
* @zh 继续被前端工具中断的流程。
|
|
509
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
510
|
+
*/
|
|
432
511
|
resume(result: CallToolResult) {
|
|
433
512
|
return this.sendMessage([], {
|
|
434
513
|
command: {
|
|
@@ -436,7 +515,10 @@ export class LangGraphClient extends Client {
|
|
|
436
515
|
},
|
|
437
516
|
});
|
|
438
517
|
}
|
|
439
|
-
/**
|
|
518
|
+
/**
|
|
519
|
+
* @zh 标记前端工具等待已完成。
|
|
520
|
+
* @en Marks the frontend tool waiting as completed.
|
|
521
|
+
*/
|
|
440
522
|
doneFEToolWaiting(id: string, result: CallToolResult) {
|
|
441
523
|
const done = this.tools.doneWaiting(id, result);
|
|
442
524
|
if (!done && this.currentThread?.status === "interrupted") {
|
|
@@ -444,14 +526,26 @@ export class LangGraphClient extends Client {
|
|
|
444
526
|
}
|
|
445
527
|
}
|
|
446
528
|
|
|
529
|
+
/**
|
|
530
|
+
* @zh 获取当前的 Thread。
|
|
531
|
+
* @en Gets the current Thread.
|
|
532
|
+
*/
|
|
447
533
|
getCurrentThread() {
|
|
448
534
|
return this.currentThread;
|
|
449
535
|
}
|
|
450
536
|
|
|
537
|
+
/**
|
|
538
|
+
* @zh 获取当前的 Assistant。
|
|
539
|
+
* @en Gets the current Assistant.
|
|
540
|
+
*/
|
|
451
541
|
getCurrentAssistant() {
|
|
452
542
|
return this.currentAssistant;
|
|
453
543
|
}
|
|
454
544
|
|
|
545
|
+
/**
|
|
546
|
+
* @zh 重置客户端状态。
|
|
547
|
+
* @en Resets the client state.
|
|
548
|
+
*/
|
|
455
549
|
async reset() {
|
|
456
550
|
await this.initAssistant(this.currentAssistant?.name!);
|
|
457
551
|
this.currentThread = null;
|