@langgraph-js/sdk 1.1.5 → 1.1.8
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 +95 -9
- package/dist/LangGraphClient.js +75 -7
- 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 +42 -1
- package/dist/ui-store/createChatStore.js +93 -7
- package/dist/ui-store/index.d.ts +2 -2
- package/dist/ui-store/index.js +2 -2
- package/package.json +2 -4
- package/src/LangGraphClient.ts +96 -11
- 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 +94 -6
- package/src/ui-store/index.ts +2 -2
- package/tsconfig.json +2 -2
- package/index.html +0 -12
- package/ui/index.ts +0 -182
- package/ui/tool.ts +0 -55
|
@@ -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,8 +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
|
+
*/
|
|
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
|
+
*/
|
|
6
27
|
export declare const createChatStore: (initClientName: string, config: LangGraphClientConfig, context?: {
|
|
7
28
|
onInit?: (client: LangGraphClient) => void;
|
|
8
29
|
}) => {
|
|
@@ -30,12 +51,32 @@ export declare const createChatStore: (initClientName: string, config: LangGraph
|
|
|
30
51
|
addToHistory: (thread: Thread<{
|
|
31
52
|
messages: Message[];
|
|
32
53
|
}>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* @zh 设置用户输入内容。
|
|
56
|
+
* @en Sets the user input content.
|
|
57
|
+
*/
|
|
33
58
|
setUserInput(input: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
61
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
62
|
+
*/
|
|
34
63
|
setCurrentAgent(agent: string): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* @zh 创建一个新的聊天会话。
|
|
66
|
+
* @en Creates a new chat session.
|
|
67
|
+
*/
|
|
35
68
|
createNewChat(): void;
|
|
69
|
+
/**
|
|
70
|
+
* @zh 切换到指定的历史聊天会话。
|
|
71
|
+
* @en Switches to the specified historical chat session.
|
|
72
|
+
*/
|
|
36
73
|
toHistoryChat(thread: Thread<{
|
|
37
74
|
messages: Message[];
|
|
38
75
|
}>): void;
|
|
76
|
+
/**
|
|
77
|
+
* @zh 删除指定的历史聊天会话。
|
|
78
|
+
* @en Deletes the specified historical chat session.
|
|
79
|
+
*/
|
|
39
80
|
deleteHistoryChat(thread: Thread<{
|
|
40
81
|
messages: Message[];
|
|
41
82
|
}>): Promise<void>;
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { atom } from "nanostores";
|
|
2
|
-
import { LangGraphClient } from "../LangGraphClient";
|
|
2
|
+
import { LangGraphClient } from "../LangGraphClient.js";
|
|
3
|
+
/**
|
|
4
|
+
* @zh 格式化日期对象为时间字符串。
|
|
5
|
+
* @en Formats a Date object into a time string.
|
|
6
|
+
*/
|
|
3
7
|
export const formatTime = (date) => {
|
|
4
8
|
return date.toLocaleTimeString("en-US");
|
|
5
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* @zh 格式化数字为带千位分隔符的字符串。
|
|
12
|
+
* @en Formats a number into a string with thousand separators.
|
|
13
|
+
*/
|
|
6
14
|
export const formatTokens = (tokens) => {
|
|
7
15
|
return tokens.toLocaleString("en");
|
|
8
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* @zh 获取消息内容的文本表示,处理不同类型的消息内容。
|
|
19
|
+
* @en Gets the text representation of message content, handling different types of message content.
|
|
20
|
+
*/
|
|
9
21
|
export const getMessageContent = (content) => {
|
|
10
22
|
if (typeof content === "string")
|
|
11
23
|
return content;
|
|
@@ -24,6 +36,31 @@ export const getMessageContent = (content) => {
|
|
|
24
36
|
}
|
|
25
37
|
return JSON.stringify(content);
|
|
26
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* @zh 获取历史记录中 Thread 内容的文本表示。
|
|
41
|
+
* @en Gets the text representation of Thread content in history.
|
|
42
|
+
*/
|
|
43
|
+
export const getHistoryContent = (thread) => {
|
|
44
|
+
var _a, _b, _c;
|
|
45
|
+
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;
|
|
46
|
+
if (content && Array.isArray(content)) {
|
|
47
|
+
return content.map((item) => {
|
|
48
|
+
if (item.type === "text") {
|
|
49
|
+
return item.text;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else if (typeof content === "string") {
|
|
54
|
+
return content;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* @zh 创建一个用于聊天界面的状态管理器 (store)。
|
|
62
|
+
* @en Creates a state manager (store) for the chat interface.
|
|
63
|
+
*/
|
|
27
64
|
export const createChatStore = (initClientName, config, context = {}) => {
|
|
28
65
|
const client = atom(null);
|
|
29
66
|
const renderMessages = atom([]);
|
|
@@ -34,14 +71,18 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
34
71
|
const showHistory = atom(true);
|
|
35
72
|
const currentAgent = atom(initClientName);
|
|
36
73
|
const currentChatId = atom(null);
|
|
37
|
-
|
|
74
|
+
/**
|
|
75
|
+
* @zh 初始化 LangGraph 客户端。
|
|
76
|
+
* @en Initializes the LangGraph client.
|
|
77
|
+
*/
|
|
78
|
+
async function initClient() {
|
|
38
79
|
var _a;
|
|
39
80
|
const newClient = new LangGraphClient(config);
|
|
40
81
|
await newClient.initAssistant(currentAgent.get());
|
|
41
82
|
// 不再需要创建,sendMessage 会自动创建
|
|
42
83
|
// await newClient.createThread();
|
|
84
|
+
inChatError.set(null);
|
|
43
85
|
newClient.onStreamingUpdate((event) => {
|
|
44
|
-
var _a;
|
|
45
86
|
if (event.type === "thread" || event.type === "done") {
|
|
46
87
|
// console.log(event.data);
|
|
47
88
|
// 创建新会话时,需要自动刷新历史面板
|
|
@@ -49,16 +90,20 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
49
90
|
}
|
|
50
91
|
if (event.type === "error") {
|
|
51
92
|
loading.set(false);
|
|
52
|
-
inChatError.set(
|
|
93
|
+
inChatError.set(event.data);
|
|
53
94
|
}
|
|
54
95
|
// console.log(newClient.renderMessage);
|
|
55
96
|
renderMessages.set(newClient.renderMessage);
|
|
56
97
|
});
|
|
57
98
|
(_a = context.onInit) === null || _a === void 0 ? void 0 : _a.call(context, newClient);
|
|
58
|
-
// newClient.tools.bindTools([fileTool, askUserTool]);
|
|
59
99
|
newClient.graphState = {};
|
|
60
100
|
client.set(newClient);
|
|
61
|
-
}
|
|
101
|
+
}
|
|
102
|
+
;
|
|
103
|
+
/**
|
|
104
|
+
* @zh 发送消息。
|
|
105
|
+
* @en Sends a message.
|
|
106
|
+
*/
|
|
62
107
|
const sendMessage = async (message, extraData) => {
|
|
63
108
|
var _a;
|
|
64
109
|
if ((!userInput.get().trim() && !(message === null || message === void 0 ? void 0 : message.length)) || loading.get() || !client.get())
|
|
@@ -69,21 +114,37 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
69
114
|
userInput.set("");
|
|
70
115
|
loading.set(false);
|
|
71
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* @zh 停止当前的消息生成。
|
|
119
|
+
* @en Stops the current message generation.
|
|
120
|
+
*/
|
|
72
121
|
const stopGeneration = () => {
|
|
73
122
|
var _a;
|
|
74
123
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.cancelRun();
|
|
75
124
|
};
|
|
125
|
+
/**
|
|
126
|
+
* @zh 切换工具消息的折叠状态。
|
|
127
|
+
* @en Toggles the collapsed state of a tool message.
|
|
128
|
+
*/
|
|
76
129
|
const toggleToolCollapse = (toolId) => {
|
|
77
130
|
const prev = collapsedTools.get();
|
|
78
131
|
collapsedTools.set(prev.includes(toolId) ? prev.filter((id) => id !== toolId) : [...prev, toolId]);
|
|
79
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* @zh 切换历史记录面板的可见性。
|
|
135
|
+
* @en Toggles the visibility of the history panel.
|
|
136
|
+
*/
|
|
80
137
|
const toggleHistoryVisible = () => {
|
|
81
138
|
showHistory.set(!showHistory.get());
|
|
82
139
|
};
|
|
83
140
|
const historyList = atom([]);
|
|
141
|
+
/**
|
|
142
|
+
* @zh 刷新历史记录列表。
|
|
143
|
+
* @en Refreshes the history list.
|
|
144
|
+
*/
|
|
84
145
|
const refreshHistoryList = async () => {
|
|
85
146
|
var _a;
|
|
86
|
-
if (!client.get())
|
|
147
|
+
if (!client.get() || !showHistory.get())
|
|
87
148
|
return;
|
|
88
149
|
try {
|
|
89
150
|
const response = await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.listThreads());
|
|
@@ -93,6 +154,10 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
93
154
|
console.error("Failed to fetch threads:", error);
|
|
94
155
|
}
|
|
95
156
|
};
|
|
157
|
+
/**
|
|
158
|
+
* @zh 将一个 Thread 添加到历史记录列表的开头。
|
|
159
|
+
* @en Adds a Thread to the beginning of the history list.
|
|
160
|
+
*/
|
|
96
161
|
const addToHistory = (thread) => {
|
|
97
162
|
const prev = historyList.get();
|
|
98
163
|
historyList.set([thread, ...prev]);
|
|
@@ -118,23 +183,44 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
118
183
|
toggleHistoryVisible,
|
|
119
184
|
refreshHistoryList,
|
|
120
185
|
addToHistory,
|
|
186
|
+
/**
|
|
187
|
+
* @zh 设置用户输入内容。
|
|
188
|
+
* @en Sets the user input content.
|
|
189
|
+
*/
|
|
121
190
|
setUserInput(input) {
|
|
122
191
|
userInput.set(input);
|
|
123
192
|
},
|
|
193
|
+
/**
|
|
194
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
195
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
196
|
+
*/
|
|
124
197
|
setCurrentAgent(agent) {
|
|
125
198
|
currentAgent.set(agent);
|
|
126
199
|
return initClient().then(() => {
|
|
127
200
|
refreshHistoryList();
|
|
128
201
|
});
|
|
129
202
|
},
|
|
203
|
+
/**
|
|
204
|
+
* @zh 创建一个新的聊天会话。
|
|
205
|
+
* @en Creates a new chat session.
|
|
206
|
+
*/
|
|
130
207
|
createNewChat() {
|
|
131
208
|
var _a;
|
|
132
209
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.reset();
|
|
210
|
+
inChatError.set(null);
|
|
133
211
|
},
|
|
212
|
+
/**
|
|
213
|
+
* @zh 切换到指定的历史聊天会话。
|
|
214
|
+
* @en Switches to the specified historical chat session.
|
|
215
|
+
*/
|
|
134
216
|
toHistoryChat(thread) {
|
|
135
217
|
var _a, _b;
|
|
136
218
|
(_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);
|
|
137
219
|
},
|
|
220
|
+
/**
|
|
221
|
+
* @zh 删除指定的历史聊天会话。
|
|
222
|
+
* @en Deletes the specified historical chat session.
|
|
223
|
+
*/
|
|
138
224
|
async deleteHistoryChat(thread) {
|
|
139
225
|
var _a;
|
|
140
226
|
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";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langgraph-js/sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
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",
|
|
@@ -34,9 +34,7 @@
|
|
|
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;
|
|
@@ -40,6 +57,10 @@ export interface LangGraphClientConfig {
|
|
|
40
57
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
41
58
|
}
|
|
42
59
|
|
|
60
|
+
/**
|
|
61
|
+
* @zh StreamingMessageType 类用于判断消息的类型。
|
|
62
|
+
* @en The StreamingMessageType class is used to determine the type of a message.
|
|
63
|
+
*/
|
|
43
64
|
export class StreamingMessageType {
|
|
44
65
|
static isUser(m: Message) {
|
|
45
66
|
return m.type === "human";
|
|
@@ -63,6 +84,10 @@ type StreamingUpdateEvent = {
|
|
|
63
84
|
|
|
64
85
|
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
65
86
|
|
|
87
|
+
/**
|
|
88
|
+
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
89
|
+
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
90
|
+
*/
|
|
66
91
|
export class LangGraphClient extends Client {
|
|
67
92
|
private currentAssistant: Assistant | null = null;
|
|
68
93
|
private currentThread: Thread | null = null;
|
|
@@ -81,6 +106,10 @@ export class LangGraphClient extends Client {
|
|
|
81
106
|
limit: 100,
|
|
82
107
|
});
|
|
83
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* @zh 初始化 Assistant。
|
|
111
|
+
* @en Initializes the Assistant.
|
|
112
|
+
*/
|
|
84
113
|
async initAssistant(agentName: string) {
|
|
85
114
|
try {
|
|
86
115
|
const assistants = await this.listAssistants();
|
|
@@ -99,6 +128,10 @@ export class LangGraphClient extends Client {
|
|
|
99
128
|
}
|
|
100
129
|
}
|
|
101
130
|
|
|
131
|
+
/**
|
|
132
|
+
* @zh 创建一个新的 Thread。
|
|
133
|
+
* @en Creates a new Thread.
|
|
134
|
+
*/
|
|
102
135
|
async createThread({
|
|
103
136
|
threadId,
|
|
104
137
|
}: {
|
|
@@ -114,12 +147,19 @@ export class LangGraphClient extends Client {
|
|
|
114
147
|
throw error;
|
|
115
148
|
}
|
|
116
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* @zh 列出所有的 Thread。
|
|
152
|
+
* @en Lists all Threads.
|
|
153
|
+
*/
|
|
117
154
|
async listThreads<T>() {
|
|
118
155
|
return this.threads.search<T>({
|
|
119
156
|
sortOrder: "desc",
|
|
120
157
|
});
|
|
121
158
|
}
|
|
122
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
161
|
+
* @en Resets the Thread data from history.
|
|
162
|
+
*/
|
|
123
163
|
async resetThread(agent: string, threadId: string) {
|
|
124
164
|
await this.initAssistant(agent);
|
|
125
165
|
this.currentThread = await this.threads.get(threadId);
|
|
@@ -155,7 +195,10 @@ export class LangGraphClient extends Client {
|
|
|
155
195
|
return message;
|
|
156
196
|
}
|
|
157
197
|
|
|
158
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
200
|
+
* @en Messages used for streaming rendering in the UI.
|
|
201
|
+
*/
|
|
159
202
|
get renderMessage() {
|
|
160
203
|
const previousMessage = new Map<string, Message>();
|
|
161
204
|
const result: Message[] = [];
|
|
@@ -188,7 +231,7 @@ export class LangGraphClient extends Client {
|
|
|
188
231
|
type: "tool",
|
|
189
232
|
additional_kwargs: {},
|
|
190
233
|
/** @ts-ignore */
|
|
191
|
-
tool_input: m.additional_kwargs?.tool_calls[index]
|
|
234
|
+
tool_input: m.additional_kwargs?.tool_calls?.[index]?.function?.arguments,
|
|
192
235
|
id: tool.id,
|
|
193
236
|
name: tool.name,
|
|
194
237
|
response_metadata: {},
|
|
@@ -214,7 +257,11 @@ export class LangGraphClient extends Client {
|
|
|
214
257
|
|
|
215
258
|
return this.attachInfoForMessage(this.composeToolMessages(result as RenderMessage[]));
|
|
216
259
|
}
|
|
217
|
-
|
|
260
|
+
/**
|
|
261
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
262
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
263
|
+
*/
|
|
264
|
+
private attachInfoForMessage(result: RenderMessage[]) {
|
|
218
265
|
let lastMessage: RenderMessage | null = null;
|
|
219
266
|
for (const message of result) {
|
|
220
267
|
const createTime = message.response_metadata?.create_time || "";
|
|
@@ -241,7 +288,11 @@ export class LangGraphClient extends Client {
|
|
|
241
288
|
}
|
|
242
289
|
return result;
|
|
243
290
|
}
|
|
244
|
-
|
|
291
|
+
/**
|
|
292
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
293
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
294
|
+
*/
|
|
295
|
+
private composeToolMessages(messages: RenderMessage[]): RenderMessage[] {
|
|
245
296
|
const result: RenderMessage[] = [];
|
|
246
297
|
const assistantToolMessages = new Map<string, { args: string }>();
|
|
247
298
|
const toolParentMessage = new Map<string, RenderMessage>();
|
|
@@ -275,6 +326,10 @@ export class LangGraphClient extends Client {
|
|
|
275
326
|
}
|
|
276
327
|
return result;
|
|
277
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* @zh 获取 Token 计数器信息。
|
|
331
|
+
* @en Gets the Token counter information.
|
|
332
|
+
*/
|
|
278
333
|
get tokenCounter() {
|
|
279
334
|
return this.graphMessages.reduce(
|
|
280
335
|
(acc, message) => {
|
|
@@ -302,6 +357,10 @@ export class LangGraphClient extends Client {
|
|
|
302
357
|
}
|
|
303
358
|
);
|
|
304
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* @zh 注册流式更新的回调函数。
|
|
362
|
+
* @en Registers a callback function for streaming updates.
|
|
363
|
+
*/
|
|
305
364
|
onStreamingUpdate(callback: StreamingUpdateCallback) {
|
|
306
365
|
this.streamingCallbacks.add(callback);
|
|
307
366
|
return () => {
|
|
@@ -314,11 +373,19 @@ export class LangGraphClient extends Client {
|
|
|
314
373
|
}
|
|
315
374
|
graphState: any = {};
|
|
316
375
|
currentRun?: { run_id: string };
|
|
376
|
+
/**
|
|
377
|
+
* @zh 取消当前的 Run。
|
|
378
|
+
* @en Cancels the current Run.
|
|
379
|
+
*/
|
|
317
380
|
cancelRun() {
|
|
318
381
|
if (this.currentThread?.thread_id && this.currentRun?.run_id) {
|
|
319
382
|
this.runs.cancel(this.currentThread!.thread_id, this.currentRun.run_id);
|
|
320
383
|
}
|
|
321
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
387
|
+
* @en Sends a message to the LangGraph backend.
|
|
388
|
+
*/
|
|
322
389
|
async sendMessage(input: string | Message[], { extraParams, _debug, command }: SendMessageOptions = {}) {
|
|
323
390
|
if (!this.currentAssistant) {
|
|
324
391
|
throw new Error("Thread or Assistant not initialized");
|
|
@@ -428,7 +495,10 @@ export class LangGraphClient extends Client {
|
|
|
428
495
|
const result = await this.tools.callTool(message.name!, args, { client: that, message });
|
|
429
496
|
return this.resume(result);
|
|
430
497
|
}
|
|
431
|
-
/**
|
|
498
|
+
/**
|
|
499
|
+
* @zh 继续被前端工具中断的流程。
|
|
500
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
501
|
+
*/
|
|
432
502
|
resume(result: CallToolResult) {
|
|
433
503
|
return this.sendMessage([], {
|
|
434
504
|
command: {
|
|
@@ -436,7 +506,10 @@ export class LangGraphClient extends Client {
|
|
|
436
506
|
},
|
|
437
507
|
});
|
|
438
508
|
}
|
|
439
|
-
/**
|
|
509
|
+
/**
|
|
510
|
+
* @zh 标记前端工具等待已完成。
|
|
511
|
+
* @en Marks the frontend tool waiting as completed.
|
|
512
|
+
*/
|
|
440
513
|
doneFEToolWaiting(id: string, result: CallToolResult) {
|
|
441
514
|
const done = this.tools.doneWaiting(id, result);
|
|
442
515
|
if (!done && this.currentThread?.status === "interrupted") {
|
|
@@ -444,14 +517,26 @@ export class LangGraphClient extends Client {
|
|
|
444
517
|
}
|
|
445
518
|
}
|
|
446
519
|
|
|
520
|
+
/**
|
|
521
|
+
* @zh 获取当前的 Thread。
|
|
522
|
+
* @en Gets the current Thread.
|
|
523
|
+
*/
|
|
447
524
|
getCurrentThread() {
|
|
448
525
|
return this.currentThread;
|
|
449
526
|
}
|
|
450
527
|
|
|
528
|
+
/**
|
|
529
|
+
* @zh 获取当前的 Assistant。
|
|
530
|
+
* @en Gets the current Assistant.
|
|
531
|
+
*/
|
|
451
532
|
getCurrentAssistant() {
|
|
452
533
|
return this.currentAssistant;
|
|
453
534
|
}
|
|
454
535
|
|
|
536
|
+
/**
|
|
537
|
+
* @zh 重置客户端状态。
|
|
538
|
+
* @en Resets the client state.
|
|
539
|
+
*/
|
|
455
540
|
async reset() {
|
|
456
541
|
await this.initAssistant(this.currentAssistant?.name!);
|
|
457
542
|
this.currentThread = null;
|
package/src/SpendTime.ts
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zh SpendTime 类用于计算和记录操作的耗时。
|
|
3
|
+
* @en The SpendTime class is used to calculate and record the time spent on operations.
|
|
4
|
+
*/
|
|
1
5
|
export class SpendTime {
|
|
2
6
|
private timeCounter = new Map<string, [Date, Date] | [Date]>();
|
|
3
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @zh 开始计时。
|
|
10
|
+
* @en Starts timing.
|
|
11
|
+
*/
|
|
4
12
|
start(key: string) {
|
|
5
13
|
this.timeCounter.set(key, [new Date()]);
|
|
6
14
|
}
|
|
7
15
|
|
|
16
|
+
/**
|
|
17
|
+
* @zh 结束计时。
|
|
18
|
+
* @en Ends timing.
|
|
19
|
+
*/
|
|
8
20
|
end(key: string) {
|
|
9
21
|
this.timeCounter.set(key, [this.timeCounter.get(key)?.[0] || new Date(), new Date()]);
|
|
10
22
|
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @zh 设置或更新指定键的耗时记录。如果键已存在,则更新结束时间;否则,开始新的计时。
|
|
26
|
+
* @en Sets or updates the time spent record for the specified key. If the key already exists, updates the end time; otherwise, starts a new timing.
|
|
27
|
+
*/
|
|
11
28
|
setSpendTime(key: string) {
|
|
12
29
|
if (this.timeCounter.has(key)) {
|
|
13
30
|
this.end(key);
|
|
@@ -15,13 +32,27 @@ export class SpendTime {
|
|
|
15
32
|
this.start(key);
|
|
16
33
|
}
|
|
17
34
|
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @zh 获取指定键的开始时间。
|
|
38
|
+
* @en Gets the start time for the specified key.
|
|
39
|
+
*/
|
|
18
40
|
getStartTime(key: string) {
|
|
19
41
|
return this.timeCounter.get(key)?.[0] || new Date();
|
|
20
42
|
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @zh 获取指定键的结束时间。
|
|
46
|
+
* @en Gets the end time for the specified key.
|
|
47
|
+
*/
|
|
21
48
|
getEndTime(key: string) {
|
|
22
49
|
return this.timeCounter.get(key)?.[1] || new Date();
|
|
23
50
|
}
|
|
24
51
|
|
|
52
|
+
/**
|
|
53
|
+
* @zh 获取指定键的耗时(毫秒)。
|
|
54
|
+
* @en Gets the time spent (in milliseconds) for the specified key.
|
|
55
|
+
*/
|
|
25
56
|
getSpendTime(key: string) {
|
|
26
57
|
const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()];
|
|
27
58
|
return end.getTime() - start.getTime();
|