@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
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
import { Client, Thread, Message, Assistant, HumanMessage, ToolMessage, Command } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { ToolManager } from "./ToolManager";
|
|
3
|
-
import { CallToolResult } from "./tool";
|
|
4
|
-
|
|
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
|
+
}
|
|
5
23
|
export type RenderMessage = Message & {
|
|
6
24
|
/** 工具入参 ,聚合而来*/
|
|
7
25
|
tool_input?: string;
|
|
@@ -25,6 +43,8 @@ export type RenderMessage = Message & {
|
|
|
25
43
|
spend_time?: number;
|
|
26
44
|
/** 渲染时的唯一 id,聚合而来*/
|
|
27
45
|
unique_id?: string;
|
|
46
|
+
/** 工具调用是否完成 */
|
|
47
|
+
done?: boolean;
|
|
28
48
|
};
|
|
29
49
|
export type SendMessageOptions = {
|
|
30
50
|
extraParams?: Record<string, any>;
|
|
@@ -40,6 +60,10 @@ export interface LangGraphClientConfig {
|
|
|
40
60
|
timeoutMs?: number;
|
|
41
61
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
42
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* @zh StreamingMessageType 类用于判断消息的类型。
|
|
65
|
+
* @en The StreamingMessageType class is used to determine the type of a message.
|
|
66
|
+
*/
|
|
43
67
|
export declare class StreamingMessageType {
|
|
44
68
|
static isUser(m: Message): m is HumanMessage;
|
|
45
69
|
static isTool(m: Message): m is ToolMessage;
|
|
@@ -51,6 +75,10 @@ type StreamingUpdateEvent = {
|
|
|
51
75
|
data: any;
|
|
52
76
|
};
|
|
53
77
|
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
78
|
+
/**
|
|
79
|
+
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
80
|
+
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
81
|
+
*/
|
|
54
82
|
export declare class LangGraphClient extends Client {
|
|
55
83
|
private currentAssistant;
|
|
56
84
|
private currentThread;
|
|
@@ -60,43 +88,103 @@ export declare class LangGraphClient extends Client {
|
|
|
60
88
|
constructor(config: LangGraphClientConfig);
|
|
61
89
|
availableAssistants: Assistant[];
|
|
62
90
|
private listAssistants;
|
|
63
|
-
|
|
91
|
+
/**
|
|
92
|
+
* @zh 初始化 Assistant。
|
|
93
|
+
* @en Initializes the Assistant.
|
|
94
|
+
*/
|
|
95
|
+
initAssistant(agentName?: string): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* @zh 创建一个新的 Thread。
|
|
98
|
+
* @en Creates a new Thread.
|
|
99
|
+
*/
|
|
64
100
|
createThread({ threadId, }?: {
|
|
65
101
|
threadId?: string;
|
|
66
102
|
}): Promise<Thread<import("@langchain/langgraph-sdk").DefaultValues>>;
|
|
103
|
+
/**
|
|
104
|
+
* @zh 列出所有的 Thread。
|
|
105
|
+
* @en Lists all Threads.
|
|
106
|
+
*/
|
|
67
107
|
listThreads<T>(): Promise<Thread<T>[]>;
|
|
68
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
110
|
+
* @en Resets the Thread data from history.
|
|
111
|
+
*/
|
|
69
112
|
resetThread(agent: string, threadId: string): Promise<void>;
|
|
70
113
|
streamingMessage: RenderMessage[];
|
|
71
114
|
/** 图发过来的更新信息 */
|
|
72
115
|
graphMessages: RenderMessage[];
|
|
73
116
|
cloneMessage(message: Message): Message;
|
|
74
117
|
private replaceMessageWithValuesMessage;
|
|
75
|
-
/**
|
|
118
|
+
/**
|
|
119
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
120
|
+
* @en Messages used for streaming rendering in the UI.
|
|
121
|
+
*/
|
|
76
122
|
get renderMessage(): RenderMessage[];
|
|
77
|
-
|
|
78
|
-
|
|
123
|
+
/**
|
|
124
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
125
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
126
|
+
*/
|
|
127
|
+
private attachInfoForMessage;
|
|
128
|
+
/**
|
|
129
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
130
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
131
|
+
*/
|
|
132
|
+
private composeToolMessages;
|
|
133
|
+
/**
|
|
134
|
+
* @zh 获取 Token 计数器信息。
|
|
135
|
+
* @en Gets the Token counter information.
|
|
136
|
+
*/
|
|
79
137
|
get tokenCounter(): {
|
|
80
138
|
total_tokens: number;
|
|
81
139
|
input_tokens: number;
|
|
82
140
|
output_tokens: number;
|
|
83
141
|
};
|
|
142
|
+
/**
|
|
143
|
+
* @zh 注册流式更新的回调函数。
|
|
144
|
+
* @en Registers a callback function for streaming updates.
|
|
145
|
+
*/
|
|
84
146
|
onStreamingUpdate(callback: StreamingUpdateCallback): () => void;
|
|
85
147
|
private emitStreamingUpdate;
|
|
86
148
|
graphState: any;
|
|
87
149
|
currentRun?: {
|
|
88
150
|
run_id: string;
|
|
89
151
|
};
|
|
152
|
+
/**
|
|
153
|
+
* @zh 取消当前的 Run。
|
|
154
|
+
* @en Cancels the current Run.
|
|
155
|
+
*/
|
|
90
156
|
cancelRun(): void;
|
|
157
|
+
/**
|
|
158
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
159
|
+
* @en Sends a message to the LangGraph backend.
|
|
160
|
+
*/
|
|
91
161
|
sendMessage(input: string | Message[], { extraParams, _debug, command }?: SendMessageOptions): Promise<any[]>;
|
|
92
162
|
private runFETool;
|
|
93
163
|
private callFETool;
|
|
94
|
-
/**
|
|
164
|
+
/**
|
|
165
|
+
* @zh 继续被前端工具中断的流程。
|
|
166
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
167
|
+
*/
|
|
95
168
|
resume(result: CallToolResult): Promise<any[]>;
|
|
96
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* @zh 标记前端工具等待已完成。
|
|
171
|
+
* @en Marks the frontend tool waiting as completed.
|
|
172
|
+
*/
|
|
97
173
|
doneFEToolWaiting(id: string, result: CallToolResult): void;
|
|
174
|
+
/**
|
|
175
|
+
* @zh 获取当前的 Thread。
|
|
176
|
+
* @en Gets the current Thread.
|
|
177
|
+
*/
|
|
98
178
|
getCurrentThread(): Thread<import("@langchain/langgraph-sdk").DefaultValues> | null;
|
|
179
|
+
/**
|
|
180
|
+
* @zh 获取当前的 Assistant。
|
|
181
|
+
* @en Gets the current Assistant.
|
|
182
|
+
*/
|
|
99
183
|
getCurrentAssistant(): Assistant | null;
|
|
184
|
+
/**
|
|
185
|
+
* @zh 重置客户端状态。
|
|
186
|
+
* @en Resets the client state.
|
|
187
|
+
*/
|
|
100
188
|
reset(): Promise<void>;
|
|
101
189
|
}
|
|
102
190
|
export {};
|
package/dist/LangGraphClient.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Client } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { ToolManager } from "./ToolManager";
|
|
2
|
+
import { ToolManager } from "./ToolManager.js";
|
|
3
|
+
/**
|
|
4
|
+
* @zh StreamingMessageType 类用于判断消息的类型。
|
|
5
|
+
* @en The StreamingMessageType class is used to determine the type of a message.
|
|
6
|
+
*/
|
|
3
7
|
export class StreamingMessageType {
|
|
4
8
|
static isUser(m) {
|
|
5
9
|
return m.type === "human";
|
|
@@ -16,6 +20,10 @@ export class StreamingMessageType {
|
|
|
16
20
|
return m.type === "ai" && (((_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.length) || ((_b = m.tool_call_chunks) === null || _b === void 0 ? void 0 : _b.length));
|
|
17
21
|
}
|
|
18
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
25
|
+
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
26
|
+
*/
|
|
19
27
|
export class LangGraphClient extends Client {
|
|
20
28
|
constructor(config) {
|
|
21
29
|
super(config);
|
|
@@ -37,14 +45,24 @@ export class LangGraphClient extends Client {
|
|
|
37
45
|
limit: 100,
|
|
38
46
|
});
|
|
39
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* @zh 初始化 Assistant。
|
|
50
|
+
* @en Initializes the Assistant.
|
|
51
|
+
*/
|
|
40
52
|
async initAssistant(agentName) {
|
|
41
53
|
try {
|
|
42
54
|
const assistants = await this.listAssistants();
|
|
43
55
|
this.availableAssistants = assistants;
|
|
44
56
|
if (assistants.length > 0) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
if (agentName) {
|
|
58
|
+
console.log("agentName", agentName);
|
|
59
|
+
this.currentAssistant = assistants.find((assistant) => assistant.graph_id === agentName) || null;
|
|
60
|
+
if (!this.currentAssistant) {
|
|
61
|
+
throw new Error("Agent not found: " + agentName);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this.currentAssistant = assistants[0];
|
|
48
66
|
}
|
|
49
67
|
}
|
|
50
68
|
else {
|
|
@@ -56,6 +74,10 @@ export class LangGraphClient extends Client {
|
|
|
56
74
|
throw error;
|
|
57
75
|
}
|
|
58
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* @zh 创建一个新的 Thread。
|
|
79
|
+
* @en Creates a new Thread.
|
|
80
|
+
*/
|
|
59
81
|
async createThread({ threadId, } = {}) {
|
|
60
82
|
try {
|
|
61
83
|
this.currentThread = await this.threads.create({
|
|
@@ -68,12 +90,19 @@ export class LangGraphClient extends Client {
|
|
|
68
90
|
throw error;
|
|
69
91
|
}
|
|
70
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* @zh 列出所有的 Thread。
|
|
95
|
+
* @en Lists all Threads.
|
|
96
|
+
*/
|
|
71
97
|
async listThreads() {
|
|
72
98
|
return this.threads.search({
|
|
73
99
|
sortOrder: "desc",
|
|
74
100
|
});
|
|
75
101
|
}
|
|
76
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
104
|
+
* @en Resets the Thread data from history.
|
|
105
|
+
*/
|
|
77
106
|
async resetThread(agent, threadId) {
|
|
78
107
|
await this.initAssistant(agent);
|
|
79
108
|
this.currentThread = await this.threads.get(threadId);
|
|
@@ -104,7 +133,10 @@ export class LangGraphClient extends Client {
|
|
|
104
133
|
}
|
|
105
134
|
return message;
|
|
106
135
|
}
|
|
107
|
-
/**
|
|
136
|
+
/**
|
|
137
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
138
|
+
* @en Messages used for streaming rendering in the UI.
|
|
139
|
+
*/
|
|
108
140
|
get renderMessage() {
|
|
109
141
|
var _a;
|
|
110
142
|
const previousMessage = new Map();
|
|
@@ -157,6 +189,10 @@ export class LangGraphClient extends Client {
|
|
|
157
189
|
}
|
|
158
190
|
return this.attachInfoForMessage(this.composeToolMessages(result));
|
|
159
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
194
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
195
|
+
*/
|
|
160
196
|
attachInfoForMessage(result) {
|
|
161
197
|
var _a, _b, _c;
|
|
162
198
|
let lastMessage = null;
|
|
@@ -182,6 +218,10 @@ export class LangGraphClient extends Client {
|
|
|
182
218
|
}
|
|
183
219
|
return result;
|
|
184
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
223
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
224
|
+
*/
|
|
185
225
|
composeToolMessages(messages) {
|
|
186
226
|
var _a;
|
|
187
227
|
const result = [];
|
|
@@ -204,8 +244,10 @@ export class LangGraphClient extends Client {
|
|
|
204
244
|
message.tool_input = typeof assistantToolMessage.args !== "string" ? JSON.stringify(assistantToolMessage.args) : assistantToolMessage.args;
|
|
205
245
|
if (message.additional_kwargs) {
|
|
206
246
|
message.additional_kwargs.done = true;
|
|
247
|
+
message.done = true;
|
|
207
248
|
}
|
|
208
249
|
else {
|
|
250
|
+
message.done = true;
|
|
209
251
|
message.additional_kwargs = {
|
|
210
252
|
done: true,
|
|
211
253
|
};
|
|
@@ -219,6 +261,10 @@ export class LangGraphClient extends Client {
|
|
|
219
261
|
}
|
|
220
262
|
return result;
|
|
221
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* @zh 获取 Token 计数器信息。
|
|
266
|
+
* @en Gets the Token counter information.
|
|
267
|
+
*/
|
|
222
268
|
get tokenCounter() {
|
|
223
269
|
return this.graphMessages.reduce((acc, message) => {
|
|
224
270
|
var _a, _b, _c, _d, _e;
|
|
@@ -240,6 +286,10 @@ export class LangGraphClient extends Client {
|
|
|
240
286
|
output_tokens: 0,
|
|
241
287
|
});
|
|
242
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* @zh 注册流式更新的回调函数。
|
|
291
|
+
* @en Registers a callback function for streaming updates.
|
|
292
|
+
*/
|
|
243
293
|
onStreamingUpdate(callback) {
|
|
244
294
|
this.streamingCallbacks.add(callback);
|
|
245
295
|
return () => {
|
|
@@ -249,12 +299,20 @@ export class LangGraphClient extends Client {
|
|
|
249
299
|
emitStreamingUpdate(event) {
|
|
250
300
|
this.streamingCallbacks.forEach((callback) => callback(event));
|
|
251
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* @zh 取消当前的 Run。
|
|
304
|
+
* @en Cancels the current Run.
|
|
305
|
+
*/
|
|
252
306
|
cancelRun() {
|
|
253
307
|
var _a, _b;
|
|
254
308
|
if (((_a = this.currentThread) === null || _a === void 0 ? void 0 : _a.thread_id) && ((_b = this.currentRun) === null || _b === void 0 ? void 0 : _b.run_id)) {
|
|
255
309
|
this.runs.cancel(this.currentThread.thread_id, this.currentRun.run_id);
|
|
256
310
|
}
|
|
257
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
314
|
+
* @en Sends a message to the LangGraph backend.
|
|
315
|
+
*/
|
|
258
316
|
async sendMessage(input, { extraParams, _debug, command } = {}) {
|
|
259
317
|
if (!this.currentAssistant) {
|
|
260
318
|
throw new Error("Thread or Assistant not initialized");
|
|
@@ -366,7 +424,10 @@ export class LangGraphClient extends Client {
|
|
|
366
424
|
const result = await this.tools.callTool(message.name, args, { client: that, message });
|
|
367
425
|
return this.resume(result);
|
|
368
426
|
}
|
|
369
|
-
/**
|
|
427
|
+
/**
|
|
428
|
+
* @zh 继续被前端工具中断的流程。
|
|
429
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
430
|
+
*/
|
|
370
431
|
resume(result) {
|
|
371
432
|
return this.sendMessage([], {
|
|
372
433
|
command: {
|
|
@@ -374,7 +435,10 @@ export class LangGraphClient extends Client {
|
|
|
374
435
|
},
|
|
375
436
|
});
|
|
376
437
|
}
|
|
377
|
-
/**
|
|
438
|
+
/**
|
|
439
|
+
* @zh 标记前端工具等待已完成。
|
|
440
|
+
* @en Marks the frontend tool waiting as completed.
|
|
441
|
+
*/
|
|
378
442
|
doneFEToolWaiting(id, result) {
|
|
379
443
|
var _a;
|
|
380
444
|
const done = this.tools.doneWaiting(id, result);
|
|
@@ -382,12 +446,24 @@ export class LangGraphClient extends Client {
|
|
|
382
446
|
this.resume(result);
|
|
383
447
|
}
|
|
384
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* @zh 获取当前的 Thread。
|
|
451
|
+
* @en Gets the current Thread.
|
|
452
|
+
*/
|
|
385
453
|
getCurrentThread() {
|
|
386
454
|
return this.currentThread;
|
|
387
455
|
}
|
|
456
|
+
/**
|
|
457
|
+
* @zh 获取当前的 Assistant。
|
|
458
|
+
* @en Gets the current Assistant.
|
|
459
|
+
*/
|
|
388
460
|
getCurrentAssistant() {
|
|
389
461
|
return this.currentAssistant;
|
|
390
462
|
}
|
|
463
|
+
/**
|
|
464
|
+
* @zh 重置客户端状态。
|
|
465
|
+
* @en Resets the client state.
|
|
466
|
+
*/
|
|
391
467
|
async reset() {
|
|
392
468
|
var _a;
|
|
393
469
|
await this.initAssistant((_a = this.currentAssistant) === null || _a === void 0 ? void 0 : _a.name);
|
package/dist/SpendTime.d.ts
CHANGED
|
@@ -1,9 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zh SpendTime 类用于计算和记录操作的耗时。
|
|
3
|
+
* @en The SpendTime class is used to calculate and record the time spent on operations.
|
|
4
|
+
*/
|
|
1
5
|
export declare class SpendTime {
|
|
2
6
|
private timeCounter;
|
|
7
|
+
/**
|
|
8
|
+
* @zh 开始计时。
|
|
9
|
+
* @en Starts timing.
|
|
10
|
+
*/
|
|
3
11
|
start(key: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* @zh 结束计时。
|
|
14
|
+
* @en Ends timing.
|
|
15
|
+
*/
|
|
4
16
|
end(key: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* @zh 设置或更新指定键的耗时记录。如果键已存在,则更新结束时间;否则,开始新的计时。
|
|
19
|
+
* @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.
|
|
20
|
+
*/
|
|
5
21
|
setSpendTime(key: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* @zh 获取指定键的开始时间。
|
|
24
|
+
* @en Gets the start time for the specified key.
|
|
25
|
+
*/
|
|
6
26
|
getStartTime(key: string): Date;
|
|
27
|
+
/**
|
|
28
|
+
* @zh 获取指定键的结束时间。
|
|
29
|
+
* @en Gets the end time for the specified key.
|
|
30
|
+
*/
|
|
7
31
|
getEndTime(key: string): Date;
|
|
32
|
+
/**
|
|
33
|
+
* @zh 获取指定键的耗时(毫秒)。
|
|
34
|
+
* @en Gets the time spent (in milliseconds) for the specified key.
|
|
35
|
+
*/
|
|
8
36
|
getSpendTime(key: string): number;
|
|
9
37
|
}
|
package/dist/SpendTime.js
CHANGED
|
@@ -1,14 +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
|
constructor() {
|
|
3
7
|
this.timeCounter = new Map();
|
|
4
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* @zh 开始计时。
|
|
11
|
+
* @en Starts timing.
|
|
12
|
+
*/
|
|
5
13
|
start(key) {
|
|
6
14
|
this.timeCounter.set(key, [new Date()]);
|
|
7
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* @zh 结束计时。
|
|
18
|
+
* @en Ends timing.
|
|
19
|
+
*/
|
|
8
20
|
end(key) {
|
|
9
21
|
var _a;
|
|
10
22
|
this.timeCounter.set(key, [((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[0]) || new Date(), new Date()]);
|
|
11
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
|
+
*/
|
|
12
28
|
setSpendTime(key) {
|
|
13
29
|
if (this.timeCounter.has(key)) {
|
|
14
30
|
this.end(key);
|
|
@@ -17,14 +33,26 @@ export class SpendTime {
|
|
|
17
33
|
this.start(key);
|
|
18
34
|
}
|
|
19
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* @zh 获取指定键的开始时间。
|
|
38
|
+
* @en Gets the start time for the specified key.
|
|
39
|
+
*/
|
|
20
40
|
getStartTime(key) {
|
|
21
41
|
var _a;
|
|
22
42
|
return ((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[0]) || new Date();
|
|
23
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* @zh 获取指定键的结束时间。
|
|
46
|
+
* @en Gets the end time for the specified key.
|
|
47
|
+
*/
|
|
24
48
|
getEndTime(key) {
|
|
25
49
|
var _a;
|
|
26
50
|
return ((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[1]) || new Date();
|
|
27
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* @zh 获取指定键的耗时(毫秒)。
|
|
54
|
+
* @en Gets the time spent (in milliseconds) for the specified key.
|
|
55
|
+
*/
|
|
28
56
|
getSpendTime(key) {
|
|
29
57
|
const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()];
|
|
30
58
|
return end.getTime() - start.getTime();
|
package/dist/ToolManager.d.ts
CHANGED
|
@@ -1,43 +1,54 @@
|
|
|
1
1
|
import { ToolMessage } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { LangGraphClient } from "./LangGraphClient";
|
|
3
|
-
import { CallToolResult, UnionTool } from "./tool/createTool";
|
|
2
|
+
import { LangGraphClient } from "./LangGraphClient.js";
|
|
3
|
+
import { CallToolResult, UnionTool } from "./tool/createTool.js";
|
|
4
|
+
/**
|
|
5
|
+
* @zh ToolManager 类用于管理和执行工具。
|
|
6
|
+
* @en The ToolManager class is used to manage and execute tools.
|
|
7
|
+
*/
|
|
4
8
|
export declare class ToolManager {
|
|
5
9
|
private tools;
|
|
6
10
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
11
|
+
* @zh 注册一个工具。
|
|
12
|
+
* @en Registers a tool.
|
|
9
13
|
*/
|
|
10
14
|
bindTool(tool: UnionTool<any>): void;
|
|
11
15
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
16
|
+
* @zh 注册多个工具。
|
|
17
|
+
* @en Registers multiple tools.
|
|
14
18
|
*/
|
|
15
19
|
bindTools(tools: UnionTool<any>[]): void;
|
|
16
20
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
21
|
+
* @zh 获取所有已注册的工具。
|
|
22
|
+
* @en Gets all registered tools.
|
|
19
23
|
*/
|
|
20
24
|
getAllTools(): UnionTool<any>[];
|
|
21
25
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
24
|
-
* @returns 工具实例或 undefined
|
|
26
|
+
* @zh 获取指定名称的工具。
|
|
27
|
+
* @en Gets the tool with the specified name.
|
|
25
28
|
*/
|
|
26
29
|
getTool(name: string): UnionTool<any> | undefined;
|
|
27
30
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @returns 是否成功移除
|
|
31
|
+
* @zh 移除指定名称的工具。
|
|
32
|
+
* @en Removes the tool with the specified name.
|
|
31
33
|
*/
|
|
32
34
|
removeTool(name: string): boolean;
|
|
33
35
|
/**
|
|
34
|
-
*
|
|
36
|
+
* @zh 清空所有工具。
|
|
37
|
+
* @en Clears all tools.
|
|
35
38
|
*/
|
|
36
39
|
clearTools(): void;
|
|
40
|
+
/**
|
|
41
|
+
* @zh 调用指定名称的工具。
|
|
42
|
+
* @en Calls the tool with the specified name.
|
|
43
|
+
*/
|
|
37
44
|
callTool(name: string, args: any, context: {
|
|
38
45
|
client: LangGraphClient;
|
|
39
46
|
message: ToolMessage;
|
|
40
47
|
}): Promise<CallToolResult>;
|
|
48
|
+
/**
|
|
49
|
+
* @zh 将所有工具转换为 JSON 定义格式。
|
|
50
|
+
* @en Converts all tools to JSON definition format.
|
|
51
|
+
*/
|
|
41
52
|
toJSON(): {
|
|
42
53
|
name: string;
|
|
43
54
|
description: string;
|
|
@@ -49,12 +60,19 @@ export declare class ToolManager {
|
|
|
49
60
|
};
|
|
50
61
|
}[];
|
|
51
62
|
private waitingMap;
|
|
63
|
+
/**
|
|
64
|
+
* @zh 标记指定 ID 的工具等待已完成,并传递结果。
|
|
65
|
+
* @en Marks the tool waiting with the specified ID as completed and passes the result.
|
|
66
|
+
*/
|
|
52
67
|
doneWaiting(id: string, value: CallToolResult): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* @zh 等待指定 ID 的工具完成。
|
|
70
|
+
* @en Waits for the tool with the specified ID to complete.
|
|
71
|
+
*/
|
|
53
72
|
waitForDone(id: string): Promise<unknown> | ((value: CallToolResult) => void) | undefined;
|
|
54
|
-
/**
|
|
55
|
-
* @
|
|
56
|
-
*
|
|
57
|
-
* client.tools.doneWaiting(message.id!, (e.target as any).value);
|
|
73
|
+
/**
|
|
74
|
+
* @zh 一个静态方法,用于在前端等待用户界面操作完成。
|
|
75
|
+
* @en A static method used in the frontend to wait for user interface operations to complete.
|
|
58
76
|
*/
|
|
59
77
|
static waitForUIDone<T>(_: T, context: {
|
|
60
78
|
client: LangGraphClient;
|
package/dist/ToolManager.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { createJSONDefineTool } from "./tool/createTool";
|
|
1
|
+
import { createJSONDefineTool } from "./tool/createTool.js";
|
|
2
|
+
/**
|
|
3
|
+
* @zh ToolManager 类用于管理和执行工具。
|
|
4
|
+
* @en The ToolManager class is used to manage and execute tools.
|
|
5
|
+
*/
|
|
2
6
|
export class ToolManager {
|
|
3
7
|
constructor() {
|
|
4
8
|
this.tools = new Map();
|
|
@@ -6,8 +10,8 @@ export class ToolManager {
|
|
|
6
10
|
this.waitingMap = new Map();
|
|
7
11
|
}
|
|
8
12
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
13
|
+
* @zh 注册一个工具。
|
|
14
|
+
* @en Registers a tool.
|
|
11
15
|
*/
|
|
12
16
|
bindTool(tool) {
|
|
13
17
|
if (this.tools.has(tool.name)) {
|
|
@@ -16,41 +20,44 @@ export class ToolManager {
|
|
|
16
20
|
this.tools.set(tool.name, tool);
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @
|
|
23
|
+
* @zh 注册多个工具。
|
|
24
|
+
* @en Registers multiple tools.
|
|
21
25
|
*/
|
|
22
26
|
bindTools(tools) {
|
|
23
27
|
tools.forEach((tool) => this.bindTool(tool));
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
30
|
+
* @zh 获取所有已注册的工具。
|
|
31
|
+
* @en Gets all registered tools.
|
|
28
32
|
*/
|
|
29
33
|
getAllTools() {
|
|
30
34
|
return Array.from(this.tools.values());
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
35
|
-
* @returns 工具实例或 undefined
|
|
37
|
+
* @zh 获取指定名称的工具。
|
|
38
|
+
* @en Gets the tool with the specified name.
|
|
36
39
|
*/
|
|
37
40
|
getTool(name) {
|
|
38
41
|
return this.tools.get(name);
|
|
39
42
|
}
|
|
40
43
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @
|
|
43
|
-
* @returns 是否成功移除
|
|
44
|
+
* @zh 移除指定名称的工具。
|
|
45
|
+
* @en Removes the tool with the specified name.
|
|
44
46
|
*/
|
|
45
47
|
removeTool(name) {
|
|
46
48
|
return this.tools.delete(name);
|
|
47
49
|
}
|
|
48
50
|
/**
|
|
49
|
-
*
|
|
51
|
+
* @zh 清空所有工具。
|
|
52
|
+
* @en Clears all tools.
|
|
50
53
|
*/
|
|
51
54
|
clearTools() {
|
|
52
55
|
this.tools.clear();
|
|
53
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* @zh 调用指定名称的工具。
|
|
59
|
+
* @en Calls the tool with the specified name.
|
|
60
|
+
*/
|
|
54
61
|
async callTool(name, args, context) {
|
|
55
62
|
const tool = this.getTool(name);
|
|
56
63
|
if (!tool) {
|
|
@@ -58,9 +65,17 @@ export class ToolManager {
|
|
|
58
65
|
}
|
|
59
66
|
return await tool.execute(args, context);
|
|
60
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* @zh 将所有工具转换为 JSON 定义格式。
|
|
70
|
+
* @en Converts all tools to JSON definition format.
|
|
71
|
+
*/
|
|
61
72
|
toJSON() {
|
|
62
73
|
return Array.from(this.tools.values()).map((i) => createJSONDefineTool(i));
|
|
63
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* @zh 标记指定 ID 的工具等待已完成,并传递结果。
|
|
77
|
+
* @en Marks the tool waiting with the specified ID as completed and passes the result.
|
|
78
|
+
*/
|
|
64
79
|
doneWaiting(id, value) {
|
|
65
80
|
if (this.waitingMap.has(id)) {
|
|
66
81
|
this.waitingMap.get(id)(value);
|
|
@@ -72,6 +87,10 @@ export class ToolManager {
|
|
|
72
87
|
return false;
|
|
73
88
|
}
|
|
74
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* @zh 等待指定 ID 的工具完成。
|
|
92
|
+
* @en Waits for the tool with the specified ID to complete.
|
|
93
|
+
*/
|
|
75
94
|
waitForDone(id) {
|
|
76
95
|
if (this.waitingMap.has(id)) {
|
|
77
96
|
return this.waitingMap.get(id);
|
|
@@ -81,10 +100,9 @@ export class ToolManager {
|
|
|
81
100
|
});
|
|
82
101
|
return promise;
|
|
83
102
|
}
|
|
84
|
-
/**
|
|
85
|
-
* @
|
|
86
|
-
*
|
|
87
|
-
* client.tools.doneWaiting(message.id!, (e.target as any).value);
|
|
103
|
+
/**
|
|
104
|
+
* @zh 一个静态方法,用于在前端等待用户界面操作完成。
|
|
105
|
+
* @en A static method used in the frontend to wait for user interface operations to complete.
|
|
88
106
|
*/
|
|
89
107
|
static waitForUIDone(_, context) {
|
|
90
108
|
// console.log(context.message);
|