@langgraph-js/sdk 1.6.4 → 1.7.0
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/.env +0 -0
- package/.turbo/turbo-build.log +5 -0
- package/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/LangGraphClient.d.ts +2 -0
- package/dist/LangGraphClient.js +11 -0
- package/dist/server/createState.d.ts +13 -0
- package/dist/server/createState.js +20 -0
- package/dist/server/feTools.d.ts +16 -0
- package/dist/server/feTools.js +37 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +3 -0
- package/dist/server/interrupt/index.d.ts +23 -0
- package/dist/server/interrupt/index.js +36 -0
- package/dist/server/swarm/handoff.d.ts +11 -0
- package/dist/server/swarm/handoff.js +84 -0
- package/dist/server/swarm/keepState.d.ts +6 -0
- package/dist/server/swarm/keepState.js +21 -0
- package/dist/server/tools/index.d.ts +1 -0
- package/dist/server/tools/index.js +1 -0
- package/dist/server/tools/sequential-thinking.d.ts +52 -0
- package/dist/server/tools/sequential-thinking.js +69 -0
- package/dist/server/utils.d.ts +3 -0
- package/dist/server/utils.js +24 -0
- package/dist/ui-store/createChatStore.d.ts +1 -0
- package/dist/ui-store/createChatStore.js +4 -0
- package/package.json +1 -1
- package/src/LangGraphClient.ts +657 -646
- package/src/SpendTime.ts +60 -60
- package/src/ToolManager.ts +131 -131
- package/src/index.ts +5 -5
- package/src/tool/ToolUI.ts +41 -41
- package/src/tool/copilotkit-actions.ts +72 -72
- package/src/tool/createTool.ts +102 -102
- package/src/tool/index.ts +3 -3
- package/src/tool/utils.ts +158 -158
- package/src/ui-store/UnionStore.ts +29 -29
- package/src/ui-store/createChatStore.ts +298 -294
- package/src/ui-store/index.ts +2 -2
- package/src/ui-store/rafDebounce.ts +29 -29
- package/test/testResponse.json +5418 -5418
- package/tsconfig.json +112 -112
package/src/tool/createTool.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils.js";
|
|
2
|
-
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
3
|
-
import { Action, Parameter } from "./copilotkit-actions.js";
|
|
4
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
-
import { Message } from "@langchain/langgraph-sdk";
|
|
6
|
-
import { ToolRenderData } from "./ToolUI.js";
|
|
7
|
-
|
|
8
|
-
export interface UnionTool<Args extends ZodRawShape, Child extends Object = Object> {
|
|
9
|
-
name: string;
|
|
10
|
-
description: string;
|
|
11
|
-
parameters: Args;
|
|
12
|
-
/** 是否直接返回工具结果,而不是通过消息返回 */
|
|
13
|
-
returnDirect?: boolean;
|
|
14
|
-
execute: ToolCallback<Args>;
|
|
15
|
-
/** 工具执行成功后触发的附加消息 */
|
|
16
|
-
callbackMessage?: (result: CallToolResult) => Message[];
|
|
17
|
-
render?: <D>(tool: ToolRenderData<D>) => Child;
|
|
18
|
-
onlyRender?: boolean;
|
|
19
|
-
/** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
|
|
20
|
-
allowAgent?: string[];
|
|
21
|
-
}
|
|
22
|
-
export type ToolCallback<Args extends ZodRawShape> = (args: z.objectOutputType<Args, ZodTypeAny>, context?: any) => CallToolResult | Promise<CallToolResult>;
|
|
23
|
-
|
|
24
|
-
export type CallToolResult = string | { type: "text"; text: string }[];
|
|
25
|
-
|
|
26
|
-
/** 用于格式校验 */
|
|
27
|
-
export const createTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
28
|
-
return tool;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** 提供一种兼容 copilotkit 的定义方式,简化定义形式
|
|
32
|
-
* 来自 copilotkit 的 frontend action
|
|
33
|
-
*/
|
|
34
|
-
export const createFETool = <const T extends Parameter[], Args extends ZodRawShape>(
|
|
35
|
-
tool: Action<T> & {
|
|
36
|
-
allowAgent?: string[];
|
|
37
|
-
}
|
|
38
|
-
): UnionTool<Args> => {
|
|
39
|
-
return {
|
|
40
|
-
name: tool.name,
|
|
41
|
-
description: tool.description || "",
|
|
42
|
-
parameters: convertJsonSchemaToZodRawShape(actionParametersToJsonSchema(tool.parameters || [])) as any,
|
|
43
|
-
returnDirect: tool.returnDirect,
|
|
44
|
-
callbackMessage: tool.callbackMessage,
|
|
45
|
-
allowAgent: tool.allowAgent,
|
|
46
|
-
async execute(args, context) {
|
|
47
|
-
try {
|
|
48
|
-
const result = await tool.handler?.(args, context);
|
|
49
|
-
if (typeof result === "string") {
|
|
50
|
-
return [{ type: "text", text: result }];
|
|
51
|
-
}
|
|
52
|
-
return [{ type: "text", text: JSON.stringify(result) }];
|
|
53
|
-
} catch (error) {
|
|
54
|
-
return [{ type: "text", text: `Error: ${error}` }];
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
///======= UnionTool 到 各种工具的辅助函数
|
|
61
|
-
export const createJSONDefineTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
62
|
-
return {
|
|
63
|
-
name: tool.name,
|
|
64
|
-
description: tool.description,
|
|
65
|
-
parameters: zodToJsonSchema(z.object(tool.parameters)),
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
export const createMCPTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
70
|
-
return [
|
|
71
|
-
tool.name,
|
|
72
|
-
tool.description,
|
|
73
|
-
tool.parameters,
|
|
74
|
-
async (args: z.objectOutputType<Args, ZodTypeAny>) => {
|
|
75
|
-
try {
|
|
76
|
-
const result = await tool.execute(args);
|
|
77
|
-
if (typeof result === "string") {
|
|
78
|
-
return { content: [{ type: "text", text: result }] };
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
content: result,
|
|
82
|
-
};
|
|
83
|
-
} catch (error) {
|
|
84
|
-
return { content: [{ type: "text", text: `Error: ${error}` }], isError: true };
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
];
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const createToolUI = <Args extends Parameter[] | [] = [], Child extends Object = {}>(
|
|
91
|
-
tool: Action<Args> & {
|
|
92
|
-
allowAgent?: string[];
|
|
93
|
-
render?: (tool: ToolRenderData<any>) => Child;
|
|
94
|
-
onlyRender?: boolean;
|
|
95
|
-
}
|
|
96
|
-
) => {
|
|
97
|
-
return {
|
|
98
|
-
...createFETool(tool),
|
|
99
|
-
render: tool.render,
|
|
100
|
-
onlyRender: tool.onlyRender,
|
|
101
|
-
};
|
|
102
|
-
};
|
|
1
|
+
import { actionParametersToJsonSchema, convertJsonSchemaToZodRawShape } from "./utils.js";
|
|
2
|
+
import { z, ZodRawShape, ZodTypeAny } from "zod";
|
|
3
|
+
import { Action, Parameter } from "./copilotkit-actions.js";
|
|
4
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
+
import { Message } from "@langchain/langgraph-sdk";
|
|
6
|
+
import { ToolRenderData } from "./ToolUI.js";
|
|
7
|
+
|
|
8
|
+
export interface UnionTool<Args extends ZodRawShape, Child extends Object = Object> {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
parameters: Args;
|
|
12
|
+
/** 是否直接返回工具结果,而不是通过消息返回 */
|
|
13
|
+
returnDirect?: boolean;
|
|
14
|
+
execute: ToolCallback<Args>;
|
|
15
|
+
/** 工具执行成功后触发的附加消息 */
|
|
16
|
+
callbackMessage?: (result: CallToolResult) => Message[];
|
|
17
|
+
render?: <D>(tool: ToolRenderData<D>) => Child;
|
|
18
|
+
onlyRender?: boolean;
|
|
19
|
+
/** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
|
|
20
|
+
allowAgent?: string[];
|
|
21
|
+
}
|
|
22
|
+
export type ToolCallback<Args extends ZodRawShape> = (args: z.objectOutputType<Args, ZodTypeAny>, context?: any) => CallToolResult | Promise<CallToolResult>;
|
|
23
|
+
|
|
24
|
+
export type CallToolResult = string | { type: "text"; text: string }[];
|
|
25
|
+
|
|
26
|
+
/** 用于格式校验 */
|
|
27
|
+
export const createTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
28
|
+
return tool;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** 提供一种兼容 copilotkit 的定义方式,简化定义形式
|
|
32
|
+
* 来自 copilotkit 的 frontend action
|
|
33
|
+
*/
|
|
34
|
+
export const createFETool = <const T extends Parameter[], Args extends ZodRawShape>(
|
|
35
|
+
tool: Action<T> & {
|
|
36
|
+
allowAgent?: string[];
|
|
37
|
+
}
|
|
38
|
+
): UnionTool<Args> => {
|
|
39
|
+
return {
|
|
40
|
+
name: tool.name,
|
|
41
|
+
description: tool.description || "",
|
|
42
|
+
parameters: convertJsonSchemaToZodRawShape(actionParametersToJsonSchema(tool.parameters || [])) as any,
|
|
43
|
+
returnDirect: tool.returnDirect,
|
|
44
|
+
callbackMessage: tool.callbackMessage,
|
|
45
|
+
allowAgent: tool.allowAgent,
|
|
46
|
+
async execute(args, context) {
|
|
47
|
+
try {
|
|
48
|
+
const result = await tool.handler?.(args, context);
|
|
49
|
+
if (typeof result === "string") {
|
|
50
|
+
return [{ type: "text", text: result }];
|
|
51
|
+
}
|
|
52
|
+
return [{ type: "text", text: JSON.stringify(result) }];
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return [{ type: "text", text: `Error: ${error}` }];
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
///======= UnionTool 到 各种工具的辅助函数
|
|
61
|
+
export const createJSONDefineTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
62
|
+
return {
|
|
63
|
+
name: tool.name,
|
|
64
|
+
description: tool.description,
|
|
65
|
+
parameters: zodToJsonSchema(z.object(tool.parameters)),
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const createMCPTool = <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
70
|
+
return [
|
|
71
|
+
tool.name,
|
|
72
|
+
tool.description,
|
|
73
|
+
tool.parameters,
|
|
74
|
+
async (args: z.objectOutputType<Args, ZodTypeAny>) => {
|
|
75
|
+
try {
|
|
76
|
+
const result = await tool.execute(args);
|
|
77
|
+
if (typeof result === "string") {
|
|
78
|
+
return { content: [{ type: "text", text: result }] };
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
content: result,
|
|
82
|
+
};
|
|
83
|
+
} catch (error) {
|
|
84
|
+
return { content: [{ type: "text", text: `Error: ${error}` }], isError: true };
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const createToolUI = <Args extends Parameter[] | [] = [], Child extends Object = {}>(
|
|
91
|
+
tool: Action<Args> & {
|
|
92
|
+
allowAgent?: string[];
|
|
93
|
+
render?: (tool: ToolRenderData<any>) => Child;
|
|
94
|
+
onlyRender?: boolean;
|
|
95
|
+
}
|
|
96
|
+
) => {
|
|
97
|
+
return {
|
|
98
|
+
...createFETool(tool),
|
|
99
|
+
render: tool.render,
|
|
100
|
+
onlyRender: tool.onlyRender,
|
|
101
|
+
};
|
|
102
|
+
};
|
package/src/tool/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./createTool.js";
|
|
2
|
-
export * from "./copilotkit-actions.js";
|
|
3
|
-
export * from "./ToolUI.js";
|
|
1
|
+
export * from "./createTool.js";
|
|
2
|
+
export * from "./copilotkit-actions.js";
|
|
3
|
+
export * from "./ToolUI.js";
|
package/src/tool/utils.ts
CHANGED
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* copy and modify from copilotkit
|
|
3
|
-
* https://github.com/copilotkit/copilotkit
|
|
4
|
-
*
|
|
5
|
-
* MIT License
|
|
6
|
-
*/
|
|
7
|
-
import { z, ZodRawShape } from "zod";
|
|
8
|
-
import { Parameter } from "./copilotkit-actions.js";
|
|
9
|
-
|
|
10
|
-
export type JSONSchemaString = {
|
|
11
|
-
type: "string";
|
|
12
|
-
description?: string;
|
|
13
|
-
enum?: string[];
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type JSONSchemaNumber = {
|
|
17
|
-
type: "number";
|
|
18
|
-
description?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type JSONSchemaBoolean = {
|
|
22
|
-
type: "boolean";
|
|
23
|
-
description?: string;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type JSONSchemaObject = {
|
|
27
|
-
type: "object";
|
|
28
|
-
properties?: Record<string, JSONSchema>;
|
|
29
|
-
required?: string[];
|
|
30
|
-
description?: string;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export type JSONSchemaArray = {
|
|
34
|
-
type: "array";
|
|
35
|
-
items: JSONSchema;
|
|
36
|
-
description?: string;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
|
|
40
|
-
|
|
41
|
-
export function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {
|
|
42
|
-
// Create the parameters object based on the argumentAnnotations
|
|
43
|
-
let parameters: { [key: string]: any } = {};
|
|
44
|
-
for (let parameter of actionParameters || []) {
|
|
45
|
-
parameters[parameter.name] = convertAttribute(parameter);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let requiredParameterNames: string[] = [];
|
|
49
|
-
for (let arg of actionParameters || []) {
|
|
50
|
-
if (arg.required !== false) {
|
|
51
|
-
requiredParameterNames.push(arg.name);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Create the ChatCompletionFunctions object
|
|
56
|
-
return {
|
|
57
|
-
type: "object",
|
|
58
|
-
properties: parameters,
|
|
59
|
-
required: requiredParameterNames,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function convertAttribute(attribute: Parameter): JSONSchema {
|
|
64
|
-
switch (attribute.type) {
|
|
65
|
-
case "string":
|
|
66
|
-
return {
|
|
67
|
-
type: "string",
|
|
68
|
-
description: attribute.description,
|
|
69
|
-
...(attribute.enum && { enum: attribute.enum }),
|
|
70
|
-
};
|
|
71
|
-
case "number":
|
|
72
|
-
case "boolean":
|
|
73
|
-
return {
|
|
74
|
-
type: attribute.type,
|
|
75
|
-
description: attribute.description,
|
|
76
|
-
};
|
|
77
|
-
case "object":
|
|
78
|
-
case "object[]":
|
|
79
|
-
const properties = attribute.attributes?.reduce(
|
|
80
|
-
(acc, attr) => {
|
|
81
|
-
acc[attr.name] = convertAttribute(attr);
|
|
82
|
-
return acc;
|
|
83
|
-
},
|
|
84
|
-
{} as Record<string, any>
|
|
85
|
-
);
|
|
86
|
-
const required = attribute.attributes?.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
87
|
-
if (attribute.type === "object[]") {
|
|
88
|
-
return {
|
|
89
|
-
type: "array",
|
|
90
|
-
items: {
|
|
91
|
-
type: "object",
|
|
92
|
-
...(properties && { properties }),
|
|
93
|
-
...(required && required.length > 0 && { required }),
|
|
94
|
-
},
|
|
95
|
-
description: attribute.description,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
return {
|
|
99
|
-
type: "object",
|
|
100
|
-
description: attribute.description,
|
|
101
|
-
...(properties && { properties }),
|
|
102
|
-
...(required && required.length > 0 && { required }),
|
|
103
|
-
};
|
|
104
|
-
default:
|
|
105
|
-
// Handle arrays of primitive types and undefined attribute.type
|
|
106
|
-
if (attribute.type?.endsWith("[]")) {
|
|
107
|
-
const itemType = attribute.type.slice(0, -2);
|
|
108
|
-
return {
|
|
109
|
-
type: "array",
|
|
110
|
-
items: { type: itemType as any },
|
|
111
|
-
description: attribute.description,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
// Fallback for undefined type or any other unexpected type
|
|
115
|
-
return {
|
|
116
|
-
type: "string",
|
|
117
|
-
description: attribute.description,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function convertJsonSchemaToZodRawShape(jsonSchema: any): ZodRawShape {
|
|
123
|
-
const spec: { [key: string]: z.ZodSchema } = {};
|
|
124
|
-
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
125
|
-
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
126
|
-
}
|
|
127
|
-
return spec;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
|
|
131
|
-
if (jsonSchema.type === "object") {
|
|
132
|
-
const spec: { [key: string]: z.ZodSchema } = {};
|
|
133
|
-
|
|
134
|
-
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
135
|
-
return !required ? z.object(spec).optional() : z.object(spec);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
139
|
-
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
140
|
-
}
|
|
141
|
-
let schema = z.object(spec).describe(jsonSchema.description);
|
|
142
|
-
return required ? schema : schema.optional();
|
|
143
|
-
} else if (jsonSchema.type === "string") {
|
|
144
|
-
let schema = z.string().describe(jsonSchema.description);
|
|
145
|
-
return required ? schema : schema.optional();
|
|
146
|
-
} else if (jsonSchema.type === "number") {
|
|
147
|
-
let schema = z.number().describe(jsonSchema.description);
|
|
148
|
-
return required ? schema : schema.optional();
|
|
149
|
-
} else if (jsonSchema.type === "boolean") {
|
|
150
|
-
let schema = z.boolean().describe(jsonSchema.description);
|
|
151
|
-
return required ? schema : schema.optional();
|
|
152
|
-
} else if (jsonSchema.type === "array") {
|
|
153
|
-
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
154
|
-
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
155
|
-
return required ? schema : schema.optional();
|
|
156
|
-
}
|
|
157
|
-
throw new Error("Invalid JSON schema");
|
|
158
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* copy and modify from copilotkit
|
|
3
|
+
* https://github.com/copilotkit/copilotkit
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*/
|
|
7
|
+
import { z, ZodRawShape } from "zod";
|
|
8
|
+
import { Parameter } from "./copilotkit-actions.js";
|
|
9
|
+
|
|
10
|
+
export type JSONSchemaString = {
|
|
11
|
+
type: "string";
|
|
12
|
+
description?: string;
|
|
13
|
+
enum?: string[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type JSONSchemaNumber = {
|
|
17
|
+
type: "number";
|
|
18
|
+
description?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type JSONSchemaBoolean = {
|
|
22
|
+
type: "boolean";
|
|
23
|
+
description?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type JSONSchemaObject = {
|
|
27
|
+
type: "object";
|
|
28
|
+
properties?: Record<string, JSONSchema>;
|
|
29
|
+
required?: string[];
|
|
30
|
+
description?: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type JSONSchemaArray = {
|
|
34
|
+
type: "array";
|
|
35
|
+
items: JSONSchema;
|
|
36
|
+
description?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
|
|
40
|
+
|
|
41
|
+
export function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {
|
|
42
|
+
// Create the parameters object based on the argumentAnnotations
|
|
43
|
+
let parameters: { [key: string]: any } = {};
|
|
44
|
+
for (let parameter of actionParameters || []) {
|
|
45
|
+
parameters[parameter.name] = convertAttribute(parameter);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let requiredParameterNames: string[] = [];
|
|
49
|
+
for (let arg of actionParameters || []) {
|
|
50
|
+
if (arg.required !== false) {
|
|
51
|
+
requiredParameterNames.push(arg.name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Create the ChatCompletionFunctions object
|
|
56
|
+
return {
|
|
57
|
+
type: "object",
|
|
58
|
+
properties: parameters,
|
|
59
|
+
required: requiredParameterNames,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function convertAttribute(attribute: Parameter): JSONSchema {
|
|
64
|
+
switch (attribute.type) {
|
|
65
|
+
case "string":
|
|
66
|
+
return {
|
|
67
|
+
type: "string",
|
|
68
|
+
description: attribute.description,
|
|
69
|
+
...(attribute.enum && { enum: attribute.enum }),
|
|
70
|
+
};
|
|
71
|
+
case "number":
|
|
72
|
+
case "boolean":
|
|
73
|
+
return {
|
|
74
|
+
type: attribute.type,
|
|
75
|
+
description: attribute.description,
|
|
76
|
+
};
|
|
77
|
+
case "object":
|
|
78
|
+
case "object[]":
|
|
79
|
+
const properties = attribute.attributes?.reduce(
|
|
80
|
+
(acc, attr) => {
|
|
81
|
+
acc[attr.name] = convertAttribute(attr);
|
|
82
|
+
return acc;
|
|
83
|
+
},
|
|
84
|
+
{} as Record<string, any>
|
|
85
|
+
);
|
|
86
|
+
const required = attribute.attributes?.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
87
|
+
if (attribute.type === "object[]") {
|
|
88
|
+
return {
|
|
89
|
+
type: "array",
|
|
90
|
+
items: {
|
|
91
|
+
type: "object",
|
|
92
|
+
...(properties && { properties }),
|
|
93
|
+
...(required && required.length > 0 && { required }),
|
|
94
|
+
},
|
|
95
|
+
description: attribute.description,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
type: "object",
|
|
100
|
+
description: attribute.description,
|
|
101
|
+
...(properties && { properties }),
|
|
102
|
+
...(required && required.length > 0 && { required }),
|
|
103
|
+
};
|
|
104
|
+
default:
|
|
105
|
+
// Handle arrays of primitive types and undefined attribute.type
|
|
106
|
+
if (attribute.type?.endsWith("[]")) {
|
|
107
|
+
const itemType = attribute.type.slice(0, -2);
|
|
108
|
+
return {
|
|
109
|
+
type: "array",
|
|
110
|
+
items: { type: itemType as any },
|
|
111
|
+
description: attribute.description,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// Fallback for undefined type or any other unexpected type
|
|
115
|
+
return {
|
|
116
|
+
type: "string",
|
|
117
|
+
description: attribute.description,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function convertJsonSchemaToZodRawShape(jsonSchema: any): ZodRawShape {
|
|
123
|
+
const spec: { [key: string]: z.ZodSchema } = {};
|
|
124
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
125
|
+
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
126
|
+
}
|
|
127
|
+
return spec;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
|
|
131
|
+
if (jsonSchema.type === "object") {
|
|
132
|
+
const spec: { [key: string]: z.ZodSchema } = {};
|
|
133
|
+
|
|
134
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
135
|
+
return !required ? z.object(spec).optional() : z.object(spec);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
139
|
+
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
140
|
+
}
|
|
141
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
142
|
+
return required ? schema : schema.optional();
|
|
143
|
+
} else if (jsonSchema.type === "string") {
|
|
144
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
145
|
+
return required ? schema : schema.optional();
|
|
146
|
+
} else if (jsonSchema.type === "number") {
|
|
147
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
148
|
+
return required ? schema : schema.optional();
|
|
149
|
+
} else if (jsonSchema.type === "boolean") {
|
|
150
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
151
|
+
return required ? schema : schema.optional();
|
|
152
|
+
} else if (jsonSchema.type === "array") {
|
|
153
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
154
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
155
|
+
return required ? schema : schema.optional();
|
|
156
|
+
}
|
|
157
|
+
throw new Error("Invalid JSON schema");
|
|
158
|
+
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { PreinitializedWritableAtom, StoreValue } from "nanostores";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @zh UnionStore 类型用于合并 store 的 data 和 mutations,使其可以直接访问。
|
|
5
|
-
* @en The UnionStore type is used to merge the data and mutations of a store, allowing direct access.
|
|
6
|
-
*/
|
|
7
|
-
export type UnionStore<T extends { data: Record<string, PreinitializedWritableAtom<any>>; mutations: Record<string, any> }> = {
|
|
8
|
-
[k in keyof T["data"]]: StoreValue<T["data"][k]>;
|
|
9
|
-
} & T["mutations"];
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
13
|
-
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
14
|
-
*/
|
|
15
|
-
export const useUnionStore = <T extends { data: Record<string, any>; mutations: Record<string, any> }>(
|
|
16
|
-
store: T,
|
|
17
|
-
useStore: (store: PreinitializedWritableAtom<any>) => StoreValue<T["data"][keyof T["data"]]>
|
|
18
|
-
): UnionStore<T> => {
|
|
19
|
-
const data: any = Object.fromEntries(
|
|
20
|
-
Object.entries(store.data as any).map(([key, value]) => {
|
|
21
|
-
return [key, useStore(value as any)];
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
...data,
|
|
27
|
-
...store.mutations,
|
|
28
|
-
};
|
|
29
|
-
};
|
|
1
|
+
import { PreinitializedWritableAtom, StoreValue } from "nanostores";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @zh UnionStore 类型用于合并 store 的 data 和 mutations,使其可以直接访问。
|
|
5
|
+
* @en The UnionStore type is used to merge the data and mutations of a store, allowing direct access.
|
|
6
|
+
*/
|
|
7
|
+
export type UnionStore<T extends { data: Record<string, PreinitializedWritableAtom<any>>; mutations: Record<string, any> }> = {
|
|
8
|
+
[k in keyof T["data"]]: StoreValue<T["data"][k]>;
|
|
9
|
+
} & T["mutations"];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @zh useUnionStore Hook 用于将 nanostores 的 store 结构转换为更易于在 UI 组件中使用的扁平结构。
|
|
13
|
+
* @en The useUnionStore Hook is used to transform the nanostores store structure into a flatter structure that is easier to use in UI components.
|
|
14
|
+
*/
|
|
15
|
+
export const useUnionStore = <T extends { data: Record<string, any>; mutations: Record<string, any> }>(
|
|
16
|
+
store: T,
|
|
17
|
+
useStore: (store: PreinitializedWritableAtom<any>) => StoreValue<T["data"][keyof T["data"]]>
|
|
18
|
+
): UnionStore<T> => {
|
|
19
|
+
const data: any = Object.fromEntries(
|
|
20
|
+
Object.entries(store.data as any).map(([key, value]) => {
|
|
21
|
+
return [key, useStore(value as any)];
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
...data,
|
|
27
|
+
...store.mutations,
|
|
28
|
+
};
|
|
29
|
+
};
|