@langgraph-js/sdk 1.1.2 → 1.1.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/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/LangGraphClient.js +9 -4
- package/dist/ui-store/createChatStore.d.ts +1 -1
- package/dist/ui-store/createChatStore.js +3 -3
- package/index.html +12 -12
- package/package.json +1 -1
- package/src/LangGraphClient.ts +469 -465
- package/src/SpendTime.ts +29 -29
- package/src/ToolManager.ts +100 -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 +20 -20
- package/src/ui-store/createChatStore.ts +153 -153
- package/src/ui-store/index.ts +2 -2
- package/test/testResponse.json +5418 -5418
- package/tsconfig.json +112 -112
- package/ui/index.ts +182 -182
- package/ui/tool.ts +55 -55
- package/.env +0 -0
package/src/SpendTime.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
export class SpendTime {
|
|
2
|
-
private timeCounter = new Map<string, [Date, Date] | [Date]>();
|
|
3
|
-
|
|
4
|
-
start(key: string) {
|
|
5
|
-
this.timeCounter.set(key, [new Date()]);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
end(key: string) {
|
|
9
|
-
this.timeCounter.set(key, [this.timeCounter.get(key)?.[0] || new Date(), new Date()]);
|
|
10
|
-
}
|
|
11
|
-
setSpendTime(key: string) {
|
|
12
|
-
if (this.timeCounter.has(key)) {
|
|
13
|
-
this.end(key);
|
|
14
|
-
} else {
|
|
15
|
-
this.start(key);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
getStartTime(key: string) {
|
|
19
|
-
return this.timeCounter.get(key)?.[0] || new Date();
|
|
20
|
-
}
|
|
21
|
-
getEndTime(key: string) {
|
|
22
|
-
return this.timeCounter.get(key)?.[1] || new Date();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
getSpendTime(key: string) {
|
|
26
|
-
const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()];
|
|
27
|
-
return end.getTime() - start.getTime();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
export class SpendTime {
|
|
2
|
+
private timeCounter = new Map<string, [Date, Date] | [Date]>();
|
|
3
|
+
|
|
4
|
+
start(key: string) {
|
|
5
|
+
this.timeCounter.set(key, [new Date()]);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
end(key: string) {
|
|
9
|
+
this.timeCounter.set(key, [this.timeCounter.get(key)?.[0] || new Date(), new Date()]);
|
|
10
|
+
}
|
|
11
|
+
setSpendTime(key: string) {
|
|
12
|
+
if (this.timeCounter.has(key)) {
|
|
13
|
+
this.end(key);
|
|
14
|
+
} else {
|
|
15
|
+
this.start(key);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
getStartTime(key: string) {
|
|
19
|
+
return this.timeCounter.get(key)?.[0] || new Date();
|
|
20
|
+
}
|
|
21
|
+
getEndTime(key: string) {
|
|
22
|
+
return this.timeCounter.get(key)?.[1] || new Date();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getSpendTime(key: string) {
|
|
26
|
+
const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()];
|
|
27
|
+
return end.getTime() - start.getTime();
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/ToolManager.ts
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import { ToolMessage } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { LangGraphClient } from "./LangGraphClient";
|
|
3
|
-
import { CallToolResult, createJSONDefineTool, UnionTool } from "./tool/createTool";
|
|
4
|
-
|
|
5
|
-
export class ToolManager {
|
|
6
|
-
private tools: Map<string, UnionTool<any>> = new Map();
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 注册一个工具
|
|
10
|
-
* @param tool 要注册的工具
|
|
11
|
-
*/
|
|
12
|
-
bindTool(tool: UnionTool<any>) {
|
|
13
|
-
if (this.tools.has(tool.name)) {
|
|
14
|
-
throw new Error(`Tool with name ${tool.name} already exists`);
|
|
15
|
-
}
|
|
16
|
-
this.tools.set(tool.name, tool);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 注册多个工具
|
|
21
|
-
* @param tools 要注册的工具数组
|
|
22
|
-
*/
|
|
23
|
-
bindTools(tools: UnionTool<any>[]) {
|
|
24
|
-
tools.forEach((tool) => this.bindTool(tool));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 获取所有已注册的工具
|
|
29
|
-
* @returns 工具数组
|
|
30
|
-
*/
|
|
31
|
-
getAllTools(): UnionTool<any>[] {
|
|
32
|
-
return Array.from(this.tools.values());
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 获取指定名称的工具
|
|
37
|
-
* @param name 工具名称
|
|
38
|
-
* @returns 工具实例或 undefined
|
|
39
|
-
*/
|
|
40
|
-
getTool(name: string): UnionTool<any> | undefined {
|
|
41
|
-
return this.tools.get(name);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 移除指定名称的工具
|
|
46
|
-
* @param name 工具名称
|
|
47
|
-
* @returns 是否成功移除
|
|
48
|
-
*/
|
|
49
|
-
removeTool(name: string): boolean {
|
|
50
|
-
return this.tools.delete(name);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 清空所有工具
|
|
55
|
-
*/
|
|
56
|
-
clearTools() {
|
|
57
|
-
this.tools.clear();
|
|
58
|
-
}
|
|
59
|
-
async callTool(name: string, args: any, context: { client: LangGraphClient; message: ToolMessage }) {
|
|
60
|
-
const tool = this.getTool(name);
|
|
61
|
-
if (!tool) {
|
|
62
|
-
throw new Error(`Tool with name ${name} not found`);
|
|
63
|
-
}
|
|
64
|
-
return await tool.execute(args, context);
|
|
65
|
-
}
|
|
66
|
-
toJSON() {
|
|
67
|
-
return Array.from(this.tools.values()).map((i) => createJSONDefineTool(i));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// === 专门为前端设计的异步触发结构
|
|
71
|
-
private waitingMap: Map<string, (value: CallToolResult) => void> = new Map();
|
|
72
|
-
doneWaiting(id: string, value: CallToolResult) {
|
|
73
|
-
if (this.waitingMap.has(id)) {
|
|
74
|
-
this.waitingMap.get(id)!(value);
|
|
75
|
-
this.waitingMap.delete(id);
|
|
76
|
-
return true;
|
|
77
|
-
} else {
|
|
78
|
-
console.warn(`Waiting for tool ${id} not found`);
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
waitForDone(id: string) {
|
|
83
|
-
if (this.waitingMap.has(id)) {
|
|
84
|
-
return this.waitingMap.get(id);
|
|
85
|
-
}
|
|
86
|
-
const promise = new Promise((resolve, reject) => {
|
|
87
|
-
this.waitingMap.set(id, resolve);
|
|
88
|
-
});
|
|
89
|
-
return promise;
|
|
90
|
-
}
|
|
91
|
-
/** 等待用户输入
|
|
92
|
-
* @example
|
|
93
|
-
* // 继续 chat 流
|
|
94
|
-
* client.tools.doneWaiting(message.id!, (e.target as any).value);
|
|
95
|
-
*/
|
|
96
|
-
static waitForUIDone<T>(_: T, context: { client: LangGraphClient; message: ToolMessage }) {
|
|
97
|
-
// console.log(context.message);
|
|
98
|
-
return context.client.tools.waitForDone(context.message.id!);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
1
|
+
import { ToolMessage } from "@langchain/langgraph-sdk";
|
|
2
|
+
import { LangGraphClient } from "./LangGraphClient";
|
|
3
|
+
import { CallToolResult, createJSONDefineTool, UnionTool } from "./tool/createTool";
|
|
4
|
+
|
|
5
|
+
export class ToolManager {
|
|
6
|
+
private tools: Map<string, UnionTool<any>> = new Map();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 注册一个工具
|
|
10
|
+
* @param tool 要注册的工具
|
|
11
|
+
*/
|
|
12
|
+
bindTool(tool: UnionTool<any>) {
|
|
13
|
+
if (this.tools.has(tool.name)) {
|
|
14
|
+
throw new Error(`Tool with name ${tool.name} already exists`);
|
|
15
|
+
}
|
|
16
|
+
this.tools.set(tool.name, tool);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 注册多个工具
|
|
21
|
+
* @param tools 要注册的工具数组
|
|
22
|
+
*/
|
|
23
|
+
bindTools(tools: UnionTool<any>[]) {
|
|
24
|
+
tools.forEach((tool) => this.bindTool(tool));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 获取所有已注册的工具
|
|
29
|
+
* @returns 工具数组
|
|
30
|
+
*/
|
|
31
|
+
getAllTools(): UnionTool<any>[] {
|
|
32
|
+
return Array.from(this.tools.values());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 获取指定名称的工具
|
|
37
|
+
* @param name 工具名称
|
|
38
|
+
* @returns 工具实例或 undefined
|
|
39
|
+
*/
|
|
40
|
+
getTool(name: string): UnionTool<any> | undefined {
|
|
41
|
+
return this.tools.get(name);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 移除指定名称的工具
|
|
46
|
+
* @param name 工具名称
|
|
47
|
+
* @returns 是否成功移除
|
|
48
|
+
*/
|
|
49
|
+
removeTool(name: string): boolean {
|
|
50
|
+
return this.tools.delete(name);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 清空所有工具
|
|
55
|
+
*/
|
|
56
|
+
clearTools() {
|
|
57
|
+
this.tools.clear();
|
|
58
|
+
}
|
|
59
|
+
async callTool(name: string, args: any, context: { client: LangGraphClient; message: ToolMessage }) {
|
|
60
|
+
const tool = this.getTool(name);
|
|
61
|
+
if (!tool) {
|
|
62
|
+
throw new Error(`Tool with name ${name} not found`);
|
|
63
|
+
}
|
|
64
|
+
return await tool.execute(args, context);
|
|
65
|
+
}
|
|
66
|
+
toJSON() {
|
|
67
|
+
return Array.from(this.tools.values()).map((i) => createJSONDefineTool(i));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// === 专门为前端设计的异步触发结构
|
|
71
|
+
private waitingMap: Map<string, (value: CallToolResult) => void> = new Map();
|
|
72
|
+
doneWaiting(id: string, value: CallToolResult) {
|
|
73
|
+
if (this.waitingMap.has(id)) {
|
|
74
|
+
this.waitingMap.get(id)!(value);
|
|
75
|
+
this.waitingMap.delete(id);
|
|
76
|
+
return true;
|
|
77
|
+
} else {
|
|
78
|
+
console.warn(`Waiting for tool ${id} not found`);
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
waitForDone(id: string) {
|
|
83
|
+
if (this.waitingMap.has(id)) {
|
|
84
|
+
return this.waitingMap.get(id);
|
|
85
|
+
}
|
|
86
|
+
const promise = new Promise((resolve, reject) => {
|
|
87
|
+
this.waitingMap.set(id, resolve);
|
|
88
|
+
});
|
|
89
|
+
return promise;
|
|
90
|
+
}
|
|
91
|
+
/** 等待用户输入
|
|
92
|
+
* @example
|
|
93
|
+
* // 继续 chat 流
|
|
94
|
+
* client.tools.doneWaiting(message.id!, (e.target as any).value);
|
|
95
|
+
*/
|
|
96
|
+
static waitForUIDone<T>(_: T, context: { client: LangGraphClient; message: ToolMessage }) {
|
|
97
|
+
// console.log(context.message);
|
|
98
|
+
return context.client.tools.waitForDone(context.message.id!);
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./LangGraphClient";
|
|
2
|
-
export * from "./tool";
|
|
3
|
-
export * from "@langchain/langgraph-sdk";
|
|
4
|
-
export * from "./ui-store";
|
|
5
|
-
export * from "./ToolManager";
|
|
1
|
+
export * from "./LangGraphClient";
|
|
2
|
+
export * from "./tool";
|
|
3
|
+
export * from "@langchain/langgraph-sdk";
|
|
4
|
+
export * from "./ui-store";
|
|
5
|
+
export * from "./ToolManager";
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import { Message } from "@langchain/langgraph-sdk";
|
|
2
|
-
/**
|
|
3
|
-
* copy and modify from copilotkit
|
|
4
|
-
* https://github.com/copilotkit/copilotkit
|
|
5
|
-
*
|
|
6
|
-
* MIT License
|
|
7
|
-
*/
|
|
8
|
-
type TypeMap = {
|
|
9
|
-
string: string;
|
|
10
|
-
number: number;
|
|
11
|
-
boolean: boolean;
|
|
12
|
-
object: object;
|
|
13
|
-
"string[]": string[];
|
|
14
|
-
"number[]": number[];
|
|
15
|
-
"boolean[]": boolean[];
|
|
16
|
-
"object[]": object[];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
type AbstractParameter = {
|
|
20
|
-
name: string;
|
|
21
|
-
type?: keyof TypeMap;
|
|
22
|
-
description?: string;
|
|
23
|
-
required?: boolean;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
interface StringParameter extends AbstractParameter {
|
|
27
|
-
type: "string";
|
|
28
|
-
enum?: string[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface ObjectParameter extends AbstractParameter {
|
|
32
|
-
type: "object";
|
|
33
|
-
attributes?: Parameter[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface ObjectArrayParameter extends AbstractParameter {
|
|
37
|
-
type: "object[]";
|
|
38
|
-
attributes?: Parameter[];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type SpecialParameters = StringParameter | ObjectParameter | ObjectArrayParameter;
|
|
42
|
-
interface BaseParameter extends AbstractParameter {
|
|
43
|
-
type?: Exclude<AbstractParameter["type"], SpecialParameters["type"]>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type Parameter = BaseParameter | SpecialParameters;
|
|
47
|
-
|
|
48
|
-
type OptionalParameterType<P extends AbstractParameter> = P["required"] extends false ? undefined : never;
|
|
49
|
-
|
|
50
|
-
type StringParameterType<P> = P extends StringParameter ? (P extends { enum?: Array<infer E> } ? E : string) : never;
|
|
51
|
-
|
|
52
|
-
type ObjectParameterType<P> = P extends ObjectParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes> : object) : never;
|
|
53
|
-
|
|
54
|
-
type ObjectArrayParameterType<P> = P extends ObjectArrayParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes>[] : any[]) : never;
|
|
55
|
-
|
|
56
|
-
type MappedTypeOrString<T> = T extends keyof TypeMap ? TypeMap[T] : string;
|
|
57
|
-
type BaseParameterType<P extends AbstractParameter> = P extends { type: infer T } ? (T extends BaseParameter["type"] ? MappedTypeOrString<T> : never) : string;
|
|
58
|
-
|
|
59
|
-
export type MappedParameterTypes<T extends Parameter[] | [] = []> = T extends []
|
|
60
|
-
? Record<string, any>
|
|
61
|
-
: {
|
|
62
|
-
[P in T[number] as P["name"]]: OptionalParameterType<P> | StringParameterType<P> | ObjectParameterType<P> | ObjectArrayParameterType<P> | BaseParameterType<P>;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export type Action<T extends Parameter[] | [] = []> = {
|
|
66
|
-
name: string;
|
|
67
|
-
description?: string;
|
|
68
|
-
parameters?: T;
|
|
69
|
-
handler?: T extends [] ? () => any | Promise<any> : (args: MappedParameterTypes<T>, context?: any) => any | Promise<any>;
|
|
70
|
-
returnDirect?: boolean;
|
|
71
|
-
callbackMessage?: () => Message[];
|
|
72
|
-
};
|
|
1
|
+
import { Message } from "@langchain/langgraph-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* copy and modify from copilotkit
|
|
4
|
+
* https://github.com/copilotkit/copilotkit
|
|
5
|
+
*
|
|
6
|
+
* MIT License
|
|
7
|
+
*/
|
|
8
|
+
type TypeMap = {
|
|
9
|
+
string: string;
|
|
10
|
+
number: number;
|
|
11
|
+
boolean: boolean;
|
|
12
|
+
object: object;
|
|
13
|
+
"string[]": string[];
|
|
14
|
+
"number[]": number[];
|
|
15
|
+
"boolean[]": boolean[];
|
|
16
|
+
"object[]": object[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type AbstractParameter = {
|
|
20
|
+
name: string;
|
|
21
|
+
type?: keyof TypeMap;
|
|
22
|
+
description?: string;
|
|
23
|
+
required?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
interface StringParameter extends AbstractParameter {
|
|
27
|
+
type: "string";
|
|
28
|
+
enum?: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface ObjectParameter extends AbstractParameter {
|
|
32
|
+
type: "object";
|
|
33
|
+
attributes?: Parameter[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ObjectArrayParameter extends AbstractParameter {
|
|
37
|
+
type: "object[]";
|
|
38
|
+
attributes?: Parameter[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type SpecialParameters = StringParameter | ObjectParameter | ObjectArrayParameter;
|
|
42
|
+
interface BaseParameter extends AbstractParameter {
|
|
43
|
+
type?: Exclude<AbstractParameter["type"], SpecialParameters["type"]>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type Parameter = BaseParameter | SpecialParameters;
|
|
47
|
+
|
|
48
|
+
type OptionalParameterType<P extends AbstractParameter> = P["required"] extends false ? undefined : never;
|
|
49
|
+
|
|
50
|
+
type StringParameterType<P> = P extends StringParameter ? (P extends { enum?: Array<infer E> } ? E : string) : never;
|
|
51
|
+
|
|
52
|
+
type ObjectParameterType<P> = P extends ObjectParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes> : object) : never;
|
|
53
|
+
|
|
54
|
+
type ObjectArrayParameterType<P> = P extends ObjectArrayParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes>[] : any[]) : never;
|
|
55
|
+
|
|
56
|
+
type MappedTypeOrString<T> = T extends keyof TypeMap ? TypeMap[T] : string;
|
|
57
|
+
type BaseParameterType<P extends AbstractParameter> = P extends { type: infer T } ? (T extends BaseParameter["type"] ? MappedTypeOrString<T> : never) : string;
|
|
58
|
+
|
|
59
|
+
export type MappedParameterTypes<T extends Parameter[] | [] = []> = T extends []
|
|
60
|
+
? Record<string, any>
|
|
61
|
+
: {
|
|
62
|
+
[P in T[number] as P["name"]]: OptionalParameterType<P> | StringParameterType<P> | ObjectParameterType<P> | ObjectArrayParameterType<P> | BaseParameterType<P>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type Action<T extends Parameter[] | [] = []> = {
|
|
66
|
+
name: string;
|
|
67
|
+
description?: string;
|
|
68
|
+
parameters?: T;
|
|
69
|
+
handler?: T extends [] ? () => any | Promise<any> : (args: MappedParameterTypes<T>, context?: any) => any | Promise<any>;
|
|
70
|
+
returnDirect?: boolean;
|
|
71
|
+
callbackMessage?: () => Message[];
|
|
72
|
+
};
|
package/src/tool/createTool.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils";
|
|
2
|
-
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
3
|
-
import { Action, Parameter } from "./copilotkit-actions";
|
|
4
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
-
import { Message } from "@langchain/langgraph-sdk";
|
|
6
|
-
|
|
7
|
-
export interface UnionTool<Args extends ZodRawShape> {
|
|
8
|
-
name: string;
|
|
9
|
-
description: string;
|
|
10
|
-
parameters: Args;
|
|
11
|
-
/** 是否直接返回工具结果,而不是通过消息返回 */
|
|
12
|
-
returnDirect?: boolean;
|
|
13
|
-
execute: ToolCallback<Args>;
|
|
14
|
-
/** 工具执行成功后触发的附加消息 */
|
|
15
|
-
callbackMessage?: (result: CallToolResult) => Message[];
|
|
16
|
-
}
|
|
17
|
-
export type ToolCallback<Args extends ZodRawShape> = (args: z.objectOutputType<Args, ZodTypeAny>, context?: any) => CallToolResult | Promise<CallToolResult>;
|
|
18
|
-
|
|
19
|
-
export type CallToolResult = string | { type: "text"; text: string }[];
|
|
20
|
-
|
|
21
|
-
/** 用于格式校验 */
|
|
22
|
-
export const createTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
23
|
-
return tool;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/** 提供一种兼容 copilotkit 的定义方式,简化定义形式
|
|
27
|
-
* 来自 copilotkit 的 frontend action
|
|
28
|
-
*/
|
|
29
|
-
export const createFETool = <const T extends Parameter[], Args extends ZodRawShape>(tool: Action<T>): UnionTool<Args> => {
|
|
30
|
-
return {
|
|
31
|
-
name: tool.name,
|
|
32
|
-
description: tool.description || "",
|
|
33
|
-
parameters: convertJsonSchemaToZodRawShape(actionParametersToJsonSchema(tool.parameters || [])) as any,
|
|
34
|
-
returnDirect: tool.returnDirect,
|
|
35
|
-
callbackMessage: tool.callbackMessage,
|
|
36
|
-
async execute(args, context) {
|
|
37
|
-
try {
|
|
38
|
-
const result = await tool.handler?.(args, context);
|
|
39
|
-
if (typeof result === "string") {
|
|
40
|
-
return [{ type: "text", text: result }];
|
|
41
|
-
}
|
|
42
|
-
return [{ type: "text", text: JSON.stringify(result) }];
|
|
43
|
-
} catch (error) {
|
|
44
|
-
return [{ type: "text", text: `Error: ${error}` }];
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
///======= UnionTool 到 各种工具的辅助函数
|
|
51
|
-
export const createJSONDefineTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
52
|
-
return {
|
|
53
|
-
name: tool.name,
|
|
54
|
-
description: tool.description,
|
|
55
|
-
parameters: zodToJsonSchema(z.object(tool.parameters)),
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const createMCPTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
60
|
-
return [
|
|
61
|
-
tool.name,
|
|
62
|
-
tool.description,
|
|
63
|
-
tool.parameters,
|
|
64
|
-
async (args: z.objectOutputType<Args, ZodTypeAny>) => {
|
|
65
|
-
try {
|
|
66
|
-
const result = await tool.execute(args);
|
|
67
|
-
if (typeof result === "string") {
|
|
68
|
-
return { content: [{ type: "text", text: result }] };
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
content: result,
|
|
72
|
-
};
|
|
73
|
-
} catch (error) {
|
|
74
|
-
return { content: [{ type: "text", text: `Error: ${error}` }], isError: true };
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
];
|
|
78
|
-
};
|
|
1
|
+
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils";
|
|
2
|
+
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
3
|
+
import { Action, Parameter } from "./copilotkit-actions";
|
|
4
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
+
import { Message } from "@langchain/langgraph-sdk";
|
|
6
|
+
|
|
7
|
+
export interface UnionTool<Args extends ZodRawShape> {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
parameters: Args;
|
|
11
|
+
/** 是否直接返回工具结果,而不是通过消息返回 */
|
|
12
|
+
returnDirect?: boolean;
|
|
13
|
+
execute: ToolCallback<Args>;
|
|
14
|
+
/** 工具执行成功后触发的附加消息 */
|
|
15
|
+
callbackMessage?: (result: CallToolResult) => Message[];
|
|
16
|
+
}
|
|
17
|
+
export type ToolCallback<Args extends ZodRawShape> = (args: z.objectOutputType<Args, ZodTypeAny>, context?: any) => CallToolResult | Promise<CallToolResult>;
|
|
18
|
+
|
|
19
|
+
export type CallToolResult = string | { type: "text"; text: string }[];
|
|
20
|
+
|
|
21
|
+
/** 用于格式校验 */
|
|
22
|
+
export const createTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
23
|
+
return tool;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** 提供一种兼容 copilotkit 的定义方式,简化定义形式
|
|
27
|
+
* 来自 copilotkit 的 frontend action
|
|
28
|
+
*/
|
|
29
|
+
export const createFETool = <const T extends Parameter[], Args extends ZodRawShape>(tool: Action<T>): UnionTool<Args> => {
|
|
30
|
+
return {
|
|
31
|
+
name: tool.name,
|
|
32
|
+
description: tool.description || "",
|
|
33
|
+
parameters: convertJsonSchemaToZodRawShape(actionParametersToJsonSchema(tool.parameters || [])) as any,
|
|
34
|
+
returnDirect: tool.returnDirect,
|
|
35
|
+
callbackMessage: tool.callbackMessage,
|
|
36
|
+
async execute(args, context) {
|
|
37
|
+
try {
|
|
38
|
+
const result = await tool.handler?.(args, context);
|
|
39
|
+
if (typeof result === "string") {
|
|
40
|
+
return [{ type: "text", text: result }];
|
|
41
|
+
}
|
|
42
|
+
return [{ type: "text", text: JSON.stringify(result) }];
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return [{ type: "text", text: `Error: ${error}` }];
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
///======= UnionTool 到 各种工具的辅助函数
|
|
51
|
+
export const createJSONDefineTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
52
|
+
return {
|
|
53
|
+
name: tool.name,
|
|
54
|
+
description: tool.description,
|
|
55
|
+
parameters: zodToJsonSchema(z.object(tool.parameters)),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const createMCPTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
60
|
+
return [
|
|
61
|
+
tool.name,
|
|
62
|
+
tool.description,
|
|
63
|
+
tool.parameters,
|
|
64
|
+
async (args: z.objectOutputType<Args, ZodTypeAny>) => {
|
|
65
|
+
try {
|
|
66
|
+
const result = await tool.execute(args);
|
|
67
|
+
if (typeof result === "string") {
|
|
68
|
+
return { content: [{ type: "text", text: result }] };
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
content: result,
|
|
72
|
+
};
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return { content: [{ type: "text", text: `Error: ${error}` }], isError: true };
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
};
|
package/src/tool/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./createTool";
|
|
2
|
-
export * from "./copilotkit-actions";
|
|
1
|
+
export * from "./createTool";
|
|
2
|
+
export * from "./copilotkit-actions";
|