@langgraph-js/sdk 1.10.2 → 1.10.4
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/TestKit.d.ts +18 -0
- package/dist/TestKit.js +43 -5
- package/dist/ui-store/createChatStore.js +2 -0
- package/package.json +1 -1
- package/src/TestKit.ts +49 -9
- package/src/ui-store/createChatStore.ts +1 -0
- package/test/TestKit.test.ts +24 -0
- package/vitest.confit.ts +3 -0
package/dist/TestKit.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ interface TestTask {
|
|
|
14
14
|
/** 任务失败时的回调函数 */
|
|
15
15
|
fail: () => void;
|
|
16
16
|
}
|
|
17
|
+
export declare class TestLogger {
|
|
18
|
+
info(message: string): void;
|
|
19
|
+
logMessage(message: RenderMessage): void;
|
|
20
|
+
}
|
|
17
21
|
/**
|
|
18
22
|
* @zh LangGraph 测试工具,可以配合 vitest 等常用框架进行测试
|
|
19
23
|
* @en LangGraph test tool, can be used with vitest and other common frameworks for testing
|
|
@@ -35,6 +39,8 @@ export declare class TestLangGraphChat {
|
|
|
35
39
|
private lastLength;
|
|
36
40
|
/** 待处理的测试任务列表 */
|
|
37
41
|
protected processFunc: TestTask[];
|
|
42
|
+
/** 自定义日志器 */
|
|
43
|
+
private logger;
|
|
38
44
|
/**
|
|
39
45
|
* @zh 构造函数,初始化测试环境
|
|
40
46
|
* @en Constructor, initialize test environment
|
|
@@ -42,6 +48,7 @@ export declare class TestLangGraphChat {
|
|
|
42
48
|
constructor(store: ReturnType<typeof createChatStore>, options: {
|
|
43
49
|
debug?: boolean;
|
|
44
50
|
tools?: UnionTool<any, any, any>[];
|
|
51
|
+
logger?: TestLogger;
|
|
45
52
|
});
|
|
46
53
|
/**
|
|
47
54
|
* @zh 获取当前所有渲染消息
|
|
@@ -59,6 +66,17 @@ export declare class TestLangGraphChat {
|
|
|
59
66
|
* ```
|
|
60
67
|
*/
|
|
61
68
|
addTools(tools: UnionTool<any, any, any>[]): void;
|
|
69
|
+
/**
|
|
70
|
+
* @zh 设置额外参数
|
|
71
|
+
* @en Set extra states to LangGraph
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* testChat.setExtraParams({
|
|
76
|
+
* extraParam: "value",
|
|
77
|
+
* });
|
|
78
|
+
*/
|
|
79
|
+
setExtraParams(extraParams: Record<string, any>): void;
|
|
62
80
|
/**
|
|
63
81
|
* @zh 检查所有待处理的测试任务,只有在消息数量发生变化时才执行检查
|
|
64
82
|
* @en Check all pending test tasks, only executes when message count changes
|
package/dist/TestKit.js
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { ToolRenderData } from "./tool/ToolUI.js";
|
|
2
|
+
export class TestLogger {
|
|
3
|
+
info(message) {
|
|
4
|
+
console.log(message);
|
|
5
|
+
}
|
|
6
|
+
logMessage(message) {
|
|
7
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
8
|
+
const emoji = message.type === "ai" ? "🤖" : message.type === "human" ? "👤" : "🔧";
|
|
9
|
+
const header = `${emoji} ${message.type} | ${(_a = message.name) !== null && _a !== void 0 ? _a : "null"} | ${message.id}`;
|
|
10
|
+
if (message.type === "tool") {
|
|
11
|
+
return console.log(`${header}
|
|
12
|
+
🔧 Input: ${(_c = (_b = message.tool_input) === null || _b === void 0 ? void 0 : _b.slice(0, 100)) !== null && _c !== void 0 ? _c : ""}
|
|
13
|
+
💬 Output: ${(_e = (_d = message.content) === null || _d === void 0 ? void 0 : _d.slice(0, 100)) !== null && _e !== void 0 ? _e : ""}
|
|
14
|
+
`);
|
|
15
|
+
}
|
|
16
|
+
console.log(`---
|
|
17
|
+
${header}
|
|
18
|
+
💬 Output: ${(_g = (_f = message.content) === null || _f === void 0 ? void 0 : _f.slice(0, 100)) !== null && _g !== void 0 ? _g : ""}
|
|
19
|
+
`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
2
22
|
/**
|
|
3
23
|
* @zh LangGraph 测试工具,可以配合 vitest 等常用框架进行测试
|
|
4
24
|
* @en LangGraph test tool, can be used with vitest and other common frameworks for testing
|
|
@@ -18,7 +38,7 @@ export class TestLangGraphChat {
|
|
|
18
38
|
* @en Constructor, initialize test environment
|
|
19
39
|
*/
|
|
20
40
|
constructor(store, options) {
|
|
21
|
-
var _a;
|
|
41
|
+
var _a, _b;
|
|
22
42
|
this.store = store;
|
|
23
43
|
/** 是否开启调试模式 */
|
|
24
44
|
this.debug = false;
|
|
@@ -28,6 +48,7 @@ export class TestLangGraphChat {
|
|
|
28
48
|
this.processFunc = [];
|
|
29
49
|
this.readited = false;
|
|
30
50
|
this.debug = (_a = options.debug) !== null && _a !== void 0 ? _a : false;
|
|
51
|
+
this.logger = (_b = options.logger) !== null && _b !== void 0 ? _b : new TestLogger();
|
|
31
52
|
options.tools && this.addTools(options.tools);
|
|
32
53
|
const renderMessages = this.store.data.renderMessages;
|
|
33
54
|
// 订阅消息变化,自动检查任务完成状态
|
|
@@ -62,13 +83,29 @@ export class TestLangGraphChat {
|
|
|
62
83
|
this.checkAllTask(this.getMessages(), {
|
|
63
84
|
skipLengthCheck: true,
|
|
64
85
|
});
|
|
65
|
-
},
|
|
86
|
+
}, 10);
|
|
66
87
|
return oldExecute(...args);
|
|
67
88
|
};
|
|
68
89
|
}
|
|
69
90
|
});
|
|
70
91
|
this.store.mutations.setTools(tools);
|
|
71
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* @zh 设置额外参数
|
|
95
|
+
* @en Set extra states to LangGraph
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* testChat.setExtraParams({
|
|
100
|
+
* extraParam: "value",
|
|
101
|
+
* });
|
|
102
|
+
*/
|
|
103
|
+
setExtraParams(extraParams) {
|
|
104
|
+
const client = this.store.data.client.get();
|
|
105
|
+
if (client) {
|
|
106
|
+
client.extraParams = extraParams;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
72
109
|
/**
|
|
73
110
|
* @zh 检查所有待处理的测试任务,只有在消息数量发生变化时才执行检查
|
|
74
111
|
* @en Check all pending test tasks, only executes when message count changes
|
|
@@ -84,8 +121,9 @@ export class TestLangGraphChat {
|
|
|
84
121
|
!task.success && task.runTask(options.skipLengthCheck ? messages : messages.slice(0, -1));
|
|
85
122
|
}
|
|
86
123
|
// 调试模式下打印最新消息
|
|
87
|
-
|
|
88
|
-
|
|
124
|
+
const item = messages[messages.length - (options.skipLengthCheck ? 1 : 2)];
|
|
125
|
+
if (this.debug && item) {
|
|
126
|
+
this.logger.logMessage(item);
|
|
89
127
|
}
|
|
90
128
|
}
|
|
91
129
|
/**
|
|
@@ -128,7 +166,7 @@ export class TestLangGraphChat {
|
|
|
128
166
|
])
|
|
129
167
|
.then(async () => {
|
|
130
168
|
// messages 有 10 ms 的 debounce,我们需要稍等一下
|
|
131
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
169
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
132
170
|
this.checkAllTask(this.getMessages(), {
|
|
133
171
|
skipLengthCheck: true,
|
|
134
172
|
});
|
|
@@ -110,6 +110,8 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
110
110
|
// await newClient.createThread();
|
|
111
111
|
inChatError.set(null);
|
|
112
112
|
newClient.onStreamingUpdate((event) => {
|
|
113
|
+
var _a;
|
|
114
|
+
currentChatId.set(((_a = newClient.getCurrentThread()) === null || _a === void 0 ? void 0 : _a.thread_id) || null);
|
|
113
115
|
if (event.type === "start")
|
|
114
116
|
loading.set(true);
|
|
115
117
|
if (event.type === "thread" || event.type === "done") {
|
package/package.json
CHANGED
package/src/TestKit.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Message } from "@langchain/langgraph-sdk";
|
|
|
3
3
|
import { CallToolResult, UnionTool } from "./tool/createTool.js";
|
|
4
4
|
import { ToolRenderData } from "./tool/ToolUI.js";
|
|
5
5
|
import { createChatStore } from "./ui-store/createChatStore.js";
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* @zh 测试任务接口
|
|
9
8
|
* @en Test task interface
|
|
@@ -17,6 +16,27 @@ interface TestTask {
|
|
|
17
16
|
fail: () => void;
|
|
18
17
|
}
|
|
19
18
|
|
|
19
|
+
export class TestLogger {
|
|
20
|
+
info(message: string) {
|
|
21
|
+
console.log(message);
|
|
22
|
+
}
|
|
23
|
+
logMessage(message: RenderMessage) {
|
|
24
|
+
const emoji = message.type === "ai" ? "🤖" : message.type === "human" ? "👤" : "🔧";
|
|
25
|
+
|
|
26
|
+
const header = `${emoji} ${message.type} | ${message.name ?? "null"} | ${message.id}`;
|
|
27
|
+
if (message.type === "tool") {
|
|
28
|
+
return console.log(`${header}
|
|
29
|
+
🔧 Input: ${message.tool_input?.slice(0, 100) ?? ""}
|
|
30
|
+
💬 Output: ${message.content?.slice(0, 100) ?? ""}
|
|
31
|
+
`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log(`---
|
|
35
|
+
${header}
|
|
36
|
+
💬 Output: ${message.content?.slice(0, 100) ?? ""}
|
|
37
|
+
`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
20
40
|
/**
|
|
21
41
|
* @zh LangGraph 测试工具,可以配合 vitest 等常用框架进行测试
|
|
22
42
|
* @en LangGraph test tool, can be used with vitest and other common frameworks for testing
|
|
@@ -37,7 +57,8 @@ export class TestLangGraphChat {
|
|
|
37
57
|
private lastLength = 0;
|
|
38
58
|
/** 待处理的测试任务列表 */
|
|
39
59
|
protected processFunc: TestTask[] = [];
|
|
40
|
-
|
|
60
|
+
/** 自定义日志器 */
|
|
61
|
+
private logger: TestLogger;
|
|
41
62
|
/**
|
|
42
63
|
* @zh 构造函数,初始化测试环境
|
|
43
64
|
* @en Constructor, initialize test environment
|
|
@@ -47,9 +68,11 @@ export class TestLangGraphChat {
|
|
|
47
68
|
options: {
|
|
48
69
|
debug?: boolean;
|
|
49
70
|
tools?: UnionTool<any, any, any>[];
|
|
71
|
+
logger?: TestLogger;
|
|
50
72
|
}
|
|
51
73
|
) {
|
|
52
74
|
this.debug = options.debug ?? false;
|
|
75
|
+
this.logger = options.logger ?? new TestLogger();
|
|
53
76
|
options.tools && this.addTools(options.tools);
|
|
54
77
|
const renderMessages = this.store.data.renderMessages;
|
|
55
78
|
|
|
@@ -87,7 +110,7 @@ export class TestLangGraphChat {
|
|
|
87
110
|
this.checkAllTask(this.getMessages(), {
|
|
88
111
|
skipLengthCheck: true,
|
|
89
112
|
});
|
|
90
|
-
},
|
|
113
|
+
}, 10);
|
|
91
114
|
return oldExecute!(...args);
|
|
92
115
|
};
|
|
93
116
|
}
|
|
@@ -95,6 +118,22 @@ export class TestLangGraphChat {
|
|
|
95
118
|
this.store.mutations.setTools(tools);
|
|
96
119
|
}
|
|
97
120
|
|
|
121
|
+
/**
|
|
122
|
+
* @zh 设置额外参数
|
|
123
|
+
* @en Set extra states to LangGraph
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* testChat.setExtraParams({
|
|
128
|
+
* extraParam: "value",
|
|
129
|
+
* });
|
|
130
|
+
*/
|
|
131
|
+
setExtraParams(extraParams: Record<string, any>) {
|
|
132
|
+
const client = this.store.data.client.get();
|
|
133
|
+
if (client) {
|
|
134
|
+
client.extraParams = extraParams;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
98
137
|
/**
|
|
99
138
|
* @zh 检查所有待处理的测试任务,只有在消息数量发生变化时才执行检查
|
|
100
139
|
* @en Check all pending test tasks, only executes when message count changes
|
|
@@ -112,21 +151,22 @@ export class TestLangGraphChat {
|
|
|
112
151
|
}
|
|
113
152
|
|
|
114
153
|
// 调试模式下打印最新消息
|
|
115
|
-
|
|
116
|
-
|
|
154
|
+
const item = messages[messages.length - (options.skipLengthCheck ? 1 : 2)];
|
|
155
|
+
if (this.debug && item) {
|
|
156
|
+
this.logger.logMessage(item);
|
|
117
157
|
}
|
|
118
158
|
}
|
|
119
159
|
|
|
120
|
-
private readited = false
|
|
160
|
+
private readited = false;
|
|
121
161
|
/**
|
|
122
162
|
* @zh 准备测试环境,初始化客户端连接
|
|
123
163
|
* @en Prepare test environment, initialize client connection
|
|
124
164
|
*/
|
|
125
165
|
ready() {
|
|
126
166
|
if (this.readited) {
|
|
127
|
-
return
|
|
167
|
+
return;
|
|
128
168
|
}
|
|
129
|
-
this.readited = true
|
|
169
|
+
this.readited = true;
|
|
130
170
|
return this.store.mutations.initClient();
|
|
131
171
|
}
|
|
132
172
|
|
|
@@ -159,7 +199,7 @@ export class TestLangGraphChat {
|
|
|
159
199
|
])
|
|
160
200
|
.then(async () => {
|
|
161
201
|
// messages 有 10 ms 的 debounce,我们需要稍等一下
|
|
162
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
202
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
163
203
|
this.checkAllTask(this.getMessages(), {
|
|
164
204
|
skipLengthCheck: true,
|
|
165
205
|
});
|
|
@@ -117,6 +117,7 @@ export const createChatStore = (
|
|
|
117
117
|
// await newClient.createThread();
|
|
118
118
|
inChatError.set(null);
|
|
119
119
|
newClient.onStreamingUpdate((event) => {
|
|
120
|
+
currentChatId.set(newClient.getCurrentThread()?.thread_id || null);
|
|
120
121
|
if (event.type === "start") loading.set(true);
|
|
121
122
|
if (event.type === "thread" || event.type === "done") {
|
|
122
123
|
// console.log(event.data);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { TestLangGraphChat, createChatStore } from "../src";
|
|
3
|
+
|
|
4
|
+
test("test", async () => {
|
|
5
|
+
const testChat = new TestLangGraphChat(
|
|
6
|
+
createChatStore(
|
|
7
|
+
"agent",
|
|
8
|
+
{
|
|
9
|
+
defaultHeaders: {
|
|
10
|
+
Authorization: "Bearer 123",
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
{}
|
|
14
|
+
),
|
|
15
|
+
{ debug: true }
|
|
16
|
+
);
|
|
17
|
+
await testChat.ready();
|
|
18
|
+
|
|
19
|
+
await testChat.humanInput("你好", async () => {
|
|
20
|
+
const aiMessage = await testChat.waitFor("ai");
|
|
21
|
+
expect(aiMessage.content).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
expect(1).toBe(1);
|
|
24
|
+
});
|
package/vitest.confit.ts
ADDED