@langgraph-js/sdk 1.10.1 → 1.10.3

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 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): string | undefined;
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
@@ -66,11 +84,12 @@ export declare class TestLangGraphChat {
66
84
  checkAllTask(messages: readonly RenderMessage[], options?: {
67
85
  skipLengthCheck?: boolean;
68
86
  }): void;
87
+ private readited;
69
88
  /**
70
89
  * @zh 准备测试环境,初始化客户端连接
71
90
  * @en Prepare test environment, initialize client connection
72
91
  */
73
- ready(): Promise<import("./LangGraphClient.js").LangGraphClient>;
92
+ ready(): Promise<import("./LangGraphClient.js").LangGraphClient> | undefined;
74
93
  /**
75
94
  * @zh 模拟人类输入消息并等待测试任务完成,这是测试的核心方法
76
95
  * @en Simulate human input and wait for test tasks to complete, this is the core test method
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 `${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;
@@ -26,7 +46,9 @@ export class TestLangGraphChat {
26
46
  this.lastLength = 0;
27
47
  /** 待处理的测试任务列表 */
28
48
  this.processFunc = [];
49
+ this.readited = false;
29
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();
30
52
  options.tools && this.addTools(options.tools);
31
53
  const renderMessages = this.store.data.renderMessages;
32
54
  // 订阅消息变化,自动检查任务完成状态
@@ -68,6 +90,22 @@ export class TestLangGraphChat {
68
90
  });
69
91
  this.store.mutations.setTools(tools);
70
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
+ }
71
109
  /**
72
110
  * @zh 检查所有待处理的测试任务,只有在消息数量发生变化时才执行检查
73
111
  * @en Check all pending test tasks, only executes when message count changes
@@ -83,8 +121,9 @@ export class TestLangGraphChat {
83
121
  !task.success && task.runTask(options.skipLengthCheck ? messages : messages.slice(0, -1));
84
122
  }
85
123
  // 调试模式下打印最新消息
86
- if (this.debug) {
87
- console.log(messages[messages.length - (options.skipLengthCheck ? 1 : 2)]);
124
+ const item = messages[messages.length - (options.skipLengthCheck ? 1 : 2)];
125
+ if (this.debug && item) {
126
+ this.logger.logMessage(item);
88
127
  }
89
128
  }
90
129
  /**
@@ -92,6 +131,10 @@ export class TestLangGraphChat {
92
131
  * @en Prepare test environment, initialize client connection
93
132
  */
94
133
  ready() {
134
+ if (this.readited) {
135
+ return;
136
+ }
137
+ this.readited = true;
95
138
  return this.store.mutations.initClient();
96
139
  }
97
140
  /**
@@ -121,7 +164,9 @@ export class TestLangGraphChat {
121
164
  content: text,
122
165
  },
123
166
  ])
124
- .then(() => {
167
+ .then(async () => {
168
+ // messages 有 10 ms 的 debounce,我们需要稍等一下
169
+ await new Promise((resolve) => setTimeout(resolve, 20));
125
170
  this.checkAllTask(this.getMessages(), {
126
171
  skipLengthCheck: true,
127
172
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "1.10.1",
3
+ "version": "1.10.3",
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",
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 `${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
 
@@ -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,16 +151,22 @@ export class TestLangGraphChat {
112
151
  }
113
152
 
114
153
  // 调试模式下打印最新消息
115
- if (this.debug) {
116
- console.log(messages[messages.length - (options.skipLengthCheck ? 1 : 2)]);
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
 
160
+ private readited = false;
120
161
  /**
121
162
  * @zh 准备测试环境,初始化客户端连接
122
163
  * @en Prepare test environment, initialize client connection
123
164
  */
124
165
  ready() {
166
+ if (this.readited) {
167
+ return;
168
+ }
169
+ this.readited = true;
125
170
  return this.store.mutations.initClient();
126
171
  }
127
172
 
@@ -152,7 +197,9 @@ export class TestLangGraphChat {
152
197
  content: text,
153
198
  },
154
199
  ])
155
- .then(() => {
200
+ .then(async () => {
201
+ // messages 有 10 ms 的 debounce,我们需要稍等一下
202
+ await new Promise((resolve) => setTimeout(resolve, 20));
156
203
  this.checkAllTask(this.getMessages(), {
157
204
  skipLengthCheck: true,
158
205
  });
@@ -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
+ });
@@ -0,0 +1,3 @@
1
+ export default {
2
+ test: {},
3
+ };