@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,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;
|
|
@@ -40,6 +58,10 @@ export interface LangGraphClientConfig {
|
|
|
40
58
|
timeoutMs?: number;
|
|
41
59
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
42
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* @zh StreamingMessageType 类用于判断消息的类型。
|
|
63
|
+
* @en The StreamingMessageType class is used to determine the type of a message.
|
|
64
|
+
*/
|
|
43
65
|
export declare class StreamingMessageType {
|
|
44
66
|
static isUser(m: Message): m is HumanMessage;
|
|
45
67
|
static isTool(m: Message): m is ToolMessage;
|
|
@@ -51,6 +73,10 @@ type StreamingUpdateEvent = {
|
|
|
51
73
|
data: any;
|
|
52
74
|
};
|
|
53
75
|
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
76
|
+
/**
|
|
77
|
+
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
78
|
+
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
79
|
+
*/
|
|
54
80
|
export declare class LangGraphClient extends Client {
|
|
55
81
|
private currentAssistant;
|
|
56
82
|
private currentThread;
|
|
@@ -60,43 +86,103 @@ export declare class LangGraphClient extends Client {
|
|
|
60
86
|
constructor(config: LangGraphClientConfig);
|
|
61
87
|
availableAssistants: Assistant[];
|
|
62
88
|
private listAssistants;
|
|
89
|
+
/**
|
|
90
|
+
* @zh 初始化 Assistant。
|
|
91
|
+
* @en Initializes the Assistant.
|
|
92
|
+
*/
|
|
63
93
|
initAssistant(agentName: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* @zh 创建一个新的 Thread。
|
|
96
|
+
* @en Creates a new Thread.
|
|
97
|
+
*/
|
|
64
98
|
createThread({ threadId, }?: {
|
|
65
99
|
threadId?: string;
|
|
66
100
|
}): Promise<Thread<import("@langchain/langgraph-sdk").DefaultValues>>;
|
|
101
|
+
/**
|
|
102
|
+
* @zh 列出所有的 Thread。
|
|
103
|
+
* @en Lists all Threads.
|
|
104
|
+
*/
|
|
67
105
|
listThreads<T>(): Promise<Thread<T>[]>;
|
|
68
|
-
/**
|
|
106
|
+
/**
|
|
107
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
108
|
+
* @en Resets the Thread data from history.
|
|
109
|
+
*/
|
|
69
110
|
resetThread(agent: string, threadId: string): Promise<void>;
|
|
70
111
|
streamingMessage: RenderMessage[];
|
|
71
112
|
/** 图发过来的更新信息 */
|
|
72
113
|
graphMessages: RenderMessage[];
|
|
73
114
|
cloneMessage(message: Message): Message;
|
|
74
115
|
private replaceMessageWithValuesMessage;
|
|
75
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
118
|
+
* @en Messages used for streaming rendering in the UI.
|
|
119
|
+
*/
|
|
76
120
|
get renderMessage(): RenderMessage[];
|
|
77
|
-
|
|
78
|
-
|
|
121
|
+
/**
|
|
122
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
123
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
124
|
+
*/
|
|
125
|
+
private attachInfoForMessage;
|
|
126
|
+
/**
|
|
127
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
128
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
129
|
+
*/
|
|
130
|
+
private composeToolMessages;
|
|
131
|
+
/**
|
|
132
|
+
* @zh 获取 Token 计数器信息。
|
|
133
|
+
* @en Gets the Token counter information.
|
|
134
|
+
*/
|
|
79
135
|
get tokenCounter(): {
|
|
80
136
|
total_tokens: number;
|
|
81
137
|
input_tokens: number;
|
|
82
138
|
output_tokens: number;
|
|
83
139
|
};
|
|
140
|
+
/**
|
|
141
|
+
* @zh 注册流式更新的回调函数。
|
|
142
|
+
* @en Registers a callback function for streaming updates.
|
|
143
|
+
*/
|
|
84
144
|
onStreamingUpdate(callback: StreamingUpdateCallback): () => void;
|
|
85
145
|
private emitStreamingUpdate;
|
|
86
146
|
graphState: any;
|
|
87
147
|
currentRun?: {
|
|
88
148
|
run_id: string;
|
|
89
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* @zh 取消当前的 Run。
|
|
152
|
+
* @en Cancels the current Run.
|
|
153
|
+
*/
|
|
90
154
|
cancelRun(): void;
|
|
155
|
+
/**
|
|
156
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
157
|
+
* @en Sends a message to the LangGraph backend.
|
|
158
|
+
*/
|
|
91
159
|
sendMessage(input: string | Message[], { extraParams, _debug, command }?: SendMessageOptions): Promise<any[]>;
|
|
92
160
|
private runFETool;
|
|
93
161
|
private callFETool;
|
|
94
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* @zh 继续被前端工具中断的流程。
|
|
164
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
165
|
+
*/
|
|
95
166
|
resume(result: CallToolResult): Promise<any[]>;
|
|
96
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* @zh 标记前端工具等待已完成。
|
|
169
|
+
* @en Marks the frontend tool waiting as completed.
|
|
170
|
+
*/
|
|
97
171
|
doneFEToolWaiting(id: string, result: CallToolResult): void;
|
|
172
|
+
/**
|
|
173
|
+
* @zh 获取当前的 Thread。
|
|
174
|
+
* @en Gets the current Thread.
|
|
175
|
+
*/
|
|
98
176
|
getCurrentThread(): Thread<import("@langchain/langgraph-sdk").DefaultValues> | null;
|
|
177
|
+
/**
|
|
178
|
+
* @zh 获取当前的 Assistant。
|
|
179
|
+
* @en Gets the current Assistant.
|
|
180
|
+
*/
|
|
99
181
|
getCurrentAssistant(): Assistant | null;
|
|
182
|
+
/**
|
|
183
|
+
* @zh 重置客户端状态。
|
|
184
|
+
* @en Resets the client state.
|
|
185
|
+
*/
|
|
100
186
|
reset(): Promise<void>;
|
|
101
187
|
}
|
|
102
188
|
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,6 +45,10 @@ 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();
|
|
@@ -56,6 +68,10 @@ export class LangGraphClient extends Client {
|
|
|
56
68
|
throw error;
|
|
57
69
|
}
|
|
58
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* @zh 创建一个新的 Thread。
|
|
73
|
+
* @en Creates a new Thread.
|
|
74
|
+
*/
|
|
59
75
|
async createThread({ threadId, } = {}) {
|
|
60
76
|
try {
|
|
61
77
|
this.currentThread = await this.threads.create({
|
|
@@ -68,12 +84,19 @@ export class LangGraphClient extends Client {
|
|
|
68
84
|
throw error;
|
|
69
85
|
}
|
|
70
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* @zh 列出所有的 Thread。
|
|
89
|
+
* @en Lists all Threads.
|
|
90
|
+
*/
|
|
71
91
|
async listThreads() {
|
|
72
92
|
return this.threads.search({
|
|
73
93
|
sortOrder: "desc",
|
|
74
94
|
});
|
|
75
95
|
}
|
|
76
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* @zh 从历史中恢复 Thread 数据。
|
|
98
|
+
* @en Resets the Thread data from history.
|
|
99
|
+
*/
|
|
77
100
|
async resetThread(agent, threadId) {
|
|
78
101
|
await this.initAssistant(agent);
|
|
79
102
|
this.currentThread = await this.threads.get(threadId);
|
|
@@ -104,7 +127,10 @@ export class LangGraphClient extends Client {
|
|
|
104
127
|
}
|
|
105
128
|
return message;
|
|
106
129
|
}
|
|
107
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* @zh 用于 UI 中的流式渲染中的消息。
|
|
132
|
+
* @en Messages used for streaming rendering in the UI.
|
|
133
|
+
*/
|
|
108
134
|
get renderMessage() {
|
|
109
135
|
var _a;
|
|
110
136
|
const previousMessage = new Map();
|
|
@@ -128,12 +154,12 @@ export class LangGraphClient extends Client {
|
|
|
128
154
|
/** @ts-ignore */
|
|
129
155
|
const tool_calls = ((_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.length) ? m.tool_calls : m.tool_call_chunks;
|
|
130
156
|
const new_tool_calls = tool_calls.map((tool, index) => {
|
|
131
|
-
var _a;
|
|
157
|
+
var _a, _b, _c, _d;
|
|
132
158
|
return this.replaceMessageWithValuesMessage({
|
|
133
159
|
type: "tool",
|
|
134
160
|
additional_kwargs: {},
|
|
135
161
|
/** @ts-ignore */
|
|
136
|
-
tool_input: (_a = m.additional_kwargs) === null || _a === void 0 ? void 0 : _a.tool_calls[index].function.arguments,
|
|
162
|
+
tool_input: (_d = (_c = (_b = (_a = m.additional_kwargs) === null || _a === void 0 ? void 0 : _a.tool_calls) === null || _b === void 0 ? void 0 : _b[index]) === null || _c === void 0 ? void 0 : _c.function) === null || _d === void 0 ? void 0 : _d.arguments,
|
|
137
163
|
id: tool.id,
|
|
138
164
|
name: tool.name,
|
|
139
165
|
response_metadata: {},
|
|
@@ -157,6 +183,10 @@ export class LangGraphClient extends Client {
|
|
|
157
183
|
}
|
|
158
184
|
return this.attachInfoForMessage(this.composeToolMessages(result));
|
|
159
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
188
|
+
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
189
|
+
*/
|
|
160
190
|
attachInfoForMessage(result) {
|
|
161
191
|
var _a, _b, _c;
|
|
162
192
|
let lastMessage = null;
|
|
@@ -182,6 +212,10 @@ export class LangGraphClient extends Client {
|
|
|
182
212
|
}
|
|
183
213
|
return result;
|
|
184
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
217
|
+
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
218
|
+
*/
|
|
185
219
|
composeToolMessages(messages) {
|
|
186
220
|
var _a;
|
|
187
221
|
const result = [];
|
|
@@ -219,6 +253,10 @@ export class LangGraphClient extends Client {
|
|
|
219
253
|
}
|
|
220
254
|
return result;
|
|
221
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* @zh 获取 Token 计数器信息。
|
|
258
|
+
* @en Gets the Token counter information.
|
|
259
|
+
*/
|
|
222
260
|
get tokenCounter() {
|
|
223
261
|
return this.graphMessages.reduce((acc, message) => {
|
|
224
262
|
var _a, _b, _c, _d, _e;
|
|
@@ -240,6 +278,10 @@ export class LangGraphClient extends Client {
|
|
|
240
278
|
output_tokens: 0,
|
|
241
279
|
});
|
|
242
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* @zh 注册流式更新的回调函数。
|
|
283
|
+
* @en Registers a callback function for streaming updates.
|
|
284
|
+
*/
|
|
243
285
|
onStreamingUpdate(callback) {
|
|
244
286
|
this.streamingCallbacks.add(callback);
|
|
245
287
|
return () => {
|
|
@@ -249,12 +291,20 @@ export class LangGraphClient extends Client {
|
|
|
249
291
|
emitStreamingUpdate(event) {
|
|
250
292
|
this.streamingCallbacks.forEach((callback) => callback(event));
|
|
251
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* @zh 取消当前的 Run。
|
|
296
|
+
* @en Cancels the current Run.
|
|
297
|
+
*/
|
|
252
298
|
cancelRun() {
|
|
253
299
|
var _a, _b;
|
|
254
300
|
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
301
|
this.runs.cancel(this.currentThread.thread_id, this.currentRun.run_id);
|
|
256
302
|
}
|
|
257
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* @zh 发送消息到 LangGraph 后端。
|
|
306
|
+
* @en Sends a message to the LangGraph backend.
|
|
307
|
+
*/
|
|
258
308
|
async sendMessage(input, { extraParams, _debug, command } = {}) {
|
|
259
309
|
if (!this.currentAssistant) {
|
|
260
310
|
throw new Error("Thread or Assistant not initialized");
|
|
@@ -366,7 +416,10 @@ export class LangGraphClient extends Client {
|
|
|
366
416
|
const result = await this.tools.callTool(message.name, args, { client: that, message });
|
|
367
417
|
return this.resume(result);
|
|
368
418
|
}
|
|
369
|
-
/**
|
|
419
|
+
/**
|
|
420
|
+
* @zh 继续被前端工具中断的流程。
|
|
421
|
+
* @en Resumes a process interrupted by a frontend tool.
|
|
422
|
+
*/
|
|
370
423
|
resume(result) {
|
|
371
424
|
return this.sendMessage([], {
|
|
372
425
|
command: {
|
|
@@ -374,7 +427,10 @@ export class LangGraphClient extends Client {
|
|
|
374
427
|
},
|
|
375
428
|
});
|
|
376
429
|
}
|
|
377
|
-
/**
|
|
430
|
+
/**
|
|
431
|
+
* @zh 标记前端工具等待已完成。
|
|
432
|
+
* @en Marks the frontend tool waiting as completed.
|
|
433
|
+
*/
|
|
378
434
|
doneFEToolWaiting(id, result) {
|
|
379
435
|
var _a;
|
|
380
436
|
const done = this.tools.doneWaiting(id, result);
|
|
@@ -382,12 +438,24 @@ export class LangGraphClient extends Client {
|
|
|
382
438
|
this.resume(result);
|
|
383
439
|
}
|
|
384
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* @zh 获取当前的 Thread。
|
|
443
|
+
* @en Gets the current Thread.
|
|
444
|
+
*/
|
|
385
445
|
getCurrentThread() {
|
|
386
446
|
return this.currentThread;
|
|
387
447
|
}
|
|
448
|
+
/**
|
|
449
|
+
* @zh 获取当前的 Assistant。
|
|
450
|
+
* @en Gets the current Assistant.
|
|
451
|
+
*/
|
|
388
452
|
getCurrentAssistant() {
|
|
389
453
|
return this.currentAssistant;
|
|
390
454
|
}
|
|
455
|
+
/**
|
|
456
|
+
* @zh 重置客户端状态。
|
|
457
|
+
* @en Resets the client state.
|
|
458
|
+
*/
|
|
391
459
|
async reset() {
|
|
392
460
|
var _a;
|
|
393
461
|
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);
|
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";
|