@langgraph-js/sdk 1.10.2 → 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
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;
@@ -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
  // 订阅消息变化,自动检查任务完成状态
@@ -69,6 +90,22 @@ export class TestLangGraphChat {
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
- if (this.debug) {
88
- 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);
89
127
  }
90
128
  }
91
129
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "1.10.2",
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,21 +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
 
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
 
@@ -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
+ };