@langgraph-js/sdk 1.1.6 → 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/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/LangGraphClient.d.ts +95 -9
- package/dist/LangGraphClient.js +73 -5
- 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 +76 -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 +554 -469
- package/src/SpendTime.ts +60 -29
- package/src/ToolManager.ts +123 -100
- package/src/index.ts +5 -5
- package/src/tool/copilotkit-actions.ts +72 -72
- package/src/tool/createTool.ts +78 -78
- package/src/tool/index.ts +2 -2
- package/src/tool/utils.ts +158 -158
- package/src/ui-store/UnionStore.ts +29 -20
- package/src/ui-store/createChatStore.ts +241 -167
- package/src/ui-store/index.ts +2 -2
- package/test/testResponse.json +5418 -5418
- package/tsconfig.json +112 -112
- package/.env +0 -0
- package/index.html +0 -12
- package/ui/index.ts +0 -182
- package/ui/tool.ts +0 -55
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";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
2
|
-
import { Action, Parameter } from "./copilotkit-actions";
|
|
2
|
+
import { Action, Parameter } from "./copilotkit-actions.js";
|
|
3
3
|
import { Message } from "@langchain/langgraph-sdk";
|
|
4
4
|
export interface UnionTool<Args extends ZodRawShape> {
|
|
5
5
|
name: string;
|
package/dist/tool/createTool.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils";
|
|
1
|
+
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
4
|
/** 用于格式校验 */
|
package/dist/tool/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createTool";
|
|
2
|
-
export * from "./copilotkit-actions";
|
|
1
|
+
export * from "./createTool.js";
|
|
2
|
+
export * from "./copilotkit-actions.js";
|
package/dist/tool/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createTool";
|
|
2
|
-
export * from "./copilotkit-actions";
|
|
1
|
+
export * from "./createTool.js";
|
|
2
|
+
export * from "./copilotkit-actions.js";
|
package/dist/tool/utils.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { PreinitializedWritableAtom, StoreValue } from "nanostores";
|
|
2
|
+
/**
|
|
3
|
+
* @zh UnionStore 类型用于合并 store 的 data 和 mutations,使其可以直接访问。
|
|
4
|
+
* @en The UnionStore type is used to merge the data and mutations of a store, allowing direct access.
|
|
5
|
+
*/
|
|
2
6
|
export type UnionStore<T extends {
|
|
3
7
|
data: Record<string, PreinitializedWritableAtom<any>>;
|
|
4
8
|
mutations: Record<string, any>;
|
|
5
9
|
}> = {
|
|
6
10
|
[k in keyof T["data"]]: StoreValue<T["data"][k]>;
|
|
7
11
|
} & T["mutations"];
|
|
12
|
+
/**
|
|
13
|
+
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
14
|
+
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
15
|
+
*/
|
|
8
16
|
export declare const useUnionStore: <T extends {
|
|
9
17
|
data: Record<string, any>;
|
|
10
18
|
mutations: Record<string, any>;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
3
|
+
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
4
|
+
*/
|
|
1
5
|
export const useUnionStore = (store, useStore) => {
|
|
2
6
|
const data = Object.fromEntries(Object.entries(store.data).map(([key, value]) => {
|
|
3
7
|
return [key, useStore(value)];
|
|
@@ -1,9 +1,29 @@
|
|
|
1
|
-
import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient";
|
|
1
|
+
import { LangGraphClient, LangGraphClientConfig, RenderMessage, SendMessageOptions } from "../LangGraphClient.js";
|
|
2
2
|
import { Message, Thread } from "@langchain/langgraph-sdk";
|
|
3
|
+
/**
|
|
4
|
+
* @zh 格式化日期对象为时间字符串。
|
|
5
|
+
* @en Formats a Date object into a time string.
|
|
6
|
+
*/
|
|
3
7
|
export declare const formatTime: (date: Date) => string;
|
|
8
|
+
/**
|
|
9
|
+
* @zh 格式化数字为带千位分隔符的字符串。
|
|
10
|
+
* @en Formats a number into a string with thousand separators.
|
|
11
|
+
*/
|
|
4
12
|
export declare const formatTokens: (tokens: number) => string;
|
|
13
|
+
/**
|
|
14
|
+
* @zh 获取消息内容的文本表示,处理不同类型的消息内容。
|
|
15
|
+
* @en Gets the text representation of message content, handling different types of message content.
|
|
16
|
+
*/
|
|
5
17
|
export declare const getMessageContent: (content: any) => string;
|
|
18
|
+
/**
|
|
19
|
+
* @zh 获取历史记录中 Thread 内容的文本表示。
|
|
20
|
+
* @en Gets the text representation of Thread content in history.
|
|
21
|
+
*/
|
|
6
22
|
export declare const getHistoryContent: (thread: Thread) => string | any[];
|
|
23
|
+
/**
|
|
24
|
+
* @zh 创建一个用于聊天界面的状态管理器 (store)。
|
|
25
|
+
* @en Creates a state manager (store) for the chat interface.
|
|
26
|
+
*/
|
|
7
27
|
export declare const createChatStore: (initClientName: string, config: LangGraphClientConfig, context?: {
|
|
8
28
|
onInit?: (client: LangGraphClient) => void;
|
|
9
29
|
}) => {
|
|
@@ -31,12 +51,32 @@ export declare const createChatStore: (initClientName: string, config: LangGraph
|
|
|
31
51
|
addToHistory: (thread: Thread<{
|
|
32
52
|
messages: Message[];
|
|
33
53
|
}>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* @zh 设置用户输入内容。
|
|
56
|
+
* @en Sets the user input content.
|
|
57
|
+
*/
|
|
34
58
|
setUserInput(input: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
61
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
62
|
+
*/
|
|
35
63
|
setCurrentAgent(agent: string): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* @zh 创建一个新的聊天会话。
|
|
66
|
+
* @en Creates a new chat session.
|
|
67
|
+
*/
|
|
36
68
|
createNewChat(): void;
|
|
69
|
+
/**
|
|
70
|
+
* @zh 切换到指定的历史聊天会话。
|
|
71
|
+
* @en Switches to the specified historical chat session.
|
|
72
|
+
*/
|
|
37
73
|
toHistoryChat(thread: Thread<{
|
|
38
74
|
messages: Message[];
|
|
39
75
|
}>): void;
|
|
76
|
+
/**
|
|
77
|
+
* @zh 删除指定的历史聊天会话。
|
|
78
|
+
* @en Deletes the specified historical chat session.
|
|
79
|
+
*/
|
|
40
80
|
deleteHistoryChat(thread: Thread<{
|
|
41
81
|
messages: Message[];
|
|
42
82
|
}>): Promise<void>;
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { atom } from "nanostores";
|
|
2
|
-
import { LangGraphClient } from "../LangGraphClient";
|
|
2
|
+
import { LangGraphClient } from "../LangGraphClient.js";
|
|
3
|
+
/**
|
|
4
|
+
* @zh 格式化日期对象为时间字符串。
|
|
5
|
+
* @en Formats a Date object into a time string.
|
|
6
|
+
*/
|
|
3
7
|
export const formatTime = (date) => {
|
|
4
8
|
return date.toLocaleTimeString("en-US");
|
|
5
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* @zh 格式化数字为带千位分隔符的字符串。
|
|
12
|
+
* @en Formats a number into a string with thousand separators.
|
|
13
|
+
*/
|
|
6
14
|
export const formatTokens = (tokens) => {
|
|
7
15
|
return tokens.toLocaleString("en");
|
|
8
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* @zh 获取消息内容的文本表示,处理不同类型的消息内容。
|
|
19
|
+
* @en Gets the text representation of message content, handling different types of message content.
|
|
20
|
+
*/
|
|
9
21
|
export const getMessageContent = (content) => {
|
|
10
22
|
if (typeof content === "string")
|
|
11
23
|
return content;
|
|
@@ -24,6 +36,10 @@ export const getMessageContent = (content) => {
|
|
|
24
36
|
}
|
|
25
37
|
return JSON.stringify(content);
|
|
26
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* @zh 获取历史记录中 Thread 内容的文本表示。
|
|
41
|
+
* @en Gets the text representation of Thread content in history.
|
|
42
|
+
*/
|
|
27
43
|
export const getHistoryContent = (thread) => {
|
|
28
44
|
var _a, _b, _c;
|
|
29
45
|
const content = (_c = (_b = (_a = thread === null || thread === void 0 ? void 0 : thread.values) === null || _a === void 0 ? void 0 : _a.messages) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.content;
|
|
@@ -41,6 +57,10 @@ export const getHistoryContent = (thread) => {
|
|
|
41
57
|
return "";
|
|
42
58
|
}
|
|
43
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* @zh 创建一个用于聊天界面的状态管理器 (store)。
|
|
62
|
+
* @en Creates a state manager (store) for the chat interface.
|
|
63
|
+
*/
|
|
44
64
|
export const createChatStore = (initClientName, config, context = {}) => {
|
|
45
65
|
const client = atom(null);
|
|
46
66
|
const renderMessages = atom([]);
|
|
@@ -51,14 +71,18 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
51
71
|
const showHistory = atom(true);
|
|
52
72
|
const currentAgent = atom(initClientName);
|
|
53
73
|
const currentChatId = atom(null);
|
|
54
|
-
|
|
74
|
+
/**
|
|
75
|
+
* @zh 初始化 LangGraph 客户端。
|
|
76
|
+
* @en Initializes the LangGraph client.
|
|
77
|
+
*/
|
|
78
|
+
async function initClient() {
|
|
55
79
|
var _a;
|
|
56
80
|
const newClient = new LangGraphClient(config);
|
|
57
81
|
await newClient.initAssistant(currentAgent.get());
|
|
58
82
|
// 不再需要创建,sendMessage 会自动创建
|
|
59
83
|
// await newClient.createThread();
|
|
84
|
+
inChatError.set(null);
|
|
60
85
|
newClient.onStreamingUpdate((event) => {
|
|
61
|
-
var _a;
|
|
62
86
|
if (event.type === "thread" || event.type === "done") {
|
|
63
87
|
// console.log(event.data);
|
|
64
88
|
// 创建新会话时,需要自动刷新历史面板
|
|
@@ -66,16 +90,20 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
66
90
|
}
|
|
67
91
|
if (event.type === "error") {
|
|
68
92
|
loading.set(false);
|
|
69
|
-
inChatError.set(
|
|
93
|
+
inChatError.set(event.data);
|
|
70
94
|
}
|
|
71
95
|
// console.log(newClient.renderMessage);
|
|
72
96
|
renderMessages.set(newClient.renderMessage);
|
|
73
97
|
});
|
|
74
98
|
(_a = context.onInit) === null || _a === void 0 ? void 0 : _a.call(context, newClient);
|
|
75
|
-
// newClient.tools.bindTools([fileTool, askUserTool]);
|
|
76
99
|
newClient.graphState = {};
|
|
77
100
|
client.set(newClient);
|
|
78
|
-
}
|
|
101
|
+
}
|
|
102
|
+
;
|
|
103
|
+
/**
|
|
104
|
+
* @zh 发送消息。
|
|
105
|
+
* @en Sends a message.
|
|
106
|
+
*/
|
|
79
107
|
const sendMessage = async (message, extraData) => {
|
|
80
108
|
var _a;
|
|
81
109
|
if ((!userInput.get().trim() && !(message === null || message === void 0 ? void 0 : message.length)) || loading.get() || !client.get())
|
|
@@ -86,21 +114,37 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
86
114
|
userInput.set("");
|
|
87
115
|
loading.set(false);
|
|
88
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* @zh 停止当前的消息生成。
|
|
119
|
+
* @en Stops the current message generation.
|
|
120
|
+
*/
|
|
89
121
|
const stopGeneration = () => {
|
|
90
122
|
var _a;
|
|
91
123
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.cancelRun();
|
|
92
124
|
};
|
|
125
|
+
/**
|
|
126
|
+
* @zh 切换工具消息的折叠状态。
|
|
127
|
+
* @en Toggles the collapsed state of a tool message.
|
|
128
|
+
*/
|
|
93
129
|
const toggleToolCollapse = (toolId) => {
|
|
94
130
|
const prev = collapsedTools.get();
|
|
95
131
|
collapsedTools.set(prev.includes(toolId) ? prev.filter((id) => id !== toolId) : [...prev, toolId]);
|
|
96
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* @zh 切换历史记录面板的可见性。
|
|
135
|
+
* @en Toggles the visibility of the history panel.
|
|
136
|
+
*/
|
|
97
137
|
const toggleHistoryVisible = () => {
|
|
98
138
|
showHistory.set(!showHistory.get());
|
|
99
139
|
};
|
|
100
140
|
const historyList = atom([]);
|
|
141
|
+
/**
|
|
142
|
+
* @zh 刷新历史记录列表。
|
|
143
|
+
* @en Refreshes the history list.
|
|
144
|
+
*/
|
|
101
145
|
const refreshHistoryList = async () => {
|
|
102
146
|
var _a;
|
|
103
|
-
if (!client.get())
|
|
147
|
+
if (!client.get() || !showHistory.get())
|
|
104
148
|
return;
|
|
105
149
|
try {
|
|
106
150
|
const response = await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.listThreads());
|
|
@@ -110,6 +154,10 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
110
154
|
console.error("Failed to fetch threads:", error);
|
|
111
155
|
}
|
|
112
156
|
};
|
|
157
|
+
/**
|
|
158
|
+
* @zh 将一个 Thread 添加到历史记录列表的开头。
|
|
159
|
+
* @en Adds a Thread to the beginning of the history list.
|
|
160
|
+
*/
|
|
113
161
|
const addToHistory = (thread) => {
|
|
114
162
|
const prev = historyList.get();
|
|
115
163
|
historyList.set([thread, ...prev]);
|
|
@@ -135,23 +183,44 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
135
183
|
toggleHistoryVisible,
|
|
136
184
|
refreshHistoryList,
|
|
137
185
|
addToHistory,
|
|
186
|
+
/**
|
|
187
|
+
* @zh 设置用户输入内容。
|
|
188
|
+
* @en Sets the user input content.
|
|
189
|
+
*/
|
|
138
190
|
setUserInput(input) {
|
|
139
191
|
userInput.set(input);
|
|
140
192
|
},
|
|
193
|
+
/**
|
|
194
|
+
* @zh 设置当前的 Agent 并重新初始化客户端。
|
|
195
|
+
* @en Sets the current Agent and reinitializes the client.
|
|
196
|
+
*/
|
|
141
197
|
setCurrentAgent(agent) {
|
|
142
198
|
currentAgent.set(agent);
|
|
143
199
|
return initClient().then(() => {
|
|
144
200
|
refreshHistoryList();
|
|
145
201
|
});
|
|
146
202
|
},
|
|
203
|
+
/**
|
|
204
|
+
* @zh 创建一个新的聊天会话。
|
|
205
|
+
* @en Creates a new chat session.
|
|
206
|
+
*/
|
|
147
207
|
createNewChat() {
|
|
148
208
|
var _a;
|
|
149
209
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.reset();
|
|
210
|
+
inChatError.set(null);
|
|
150
211
|
},
|
|
212
|
+
/**
|
|
213
|
+
* @zh 切换到指定的历史聊天会话。
|
|
214
|
+
* @en Switches to the specified historical chat session.
|
|
215
|
+
*/
|
|
151
216
|
toHistoryChat(thread) {
|
|
152
217
|
var _a, _b;
|
|
153
218
|
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.resetThread((_b = thread.metadata) === null || _b === void 0 ? void 0 : _b.graph_id, thread.thread_id);
|
|
154
219
|
},
|
|
220
|
+
/**
|
|
221
|
+
* @zh 删除指定的历史聊天会话。
|
|
222
|
+
* @en Deletes the specified historical chat session.
|
|
223
|
+
*/
|
|
155
224
|
async deleteHistoryChat(thread) {
|
|
156
225
|
var _a;
|
|
157
226
|
await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.threads.delete(thread.thread_id));
|
package/dist/ui-store/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createChatStore";
|
|
2
|
-
export * from "./UnionStore";
|
|
1
|
+
export * from "./createChatStore.js";
|
|
2
|
+
export * from "./UnionStore.js";
|
package/dist/ui-store/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createChatStore";
|
|
2
|
-
export * from "./UnionStore";
|
|
1
|
+
export * from "./createChatStore.js";
|
|
2
|
+
export * from "./UnionStore.js";
|