@saber2pr/ai-agent 0.0.11 → 0.0.12

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.
@@ -1,2 +1,2 @@
1
1
  import { z } from "zod";
2
- export declare function jsonSchemaToZod(parameters: any): z.ZodObject<{}, z.core.$loose>;
2
+ export declare function jsonSchemaToZod(parameters: any): z.ZodObject<any>;
@@ -2,16 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jsonSchemaToZod = jsonSchemaToZod;
4
4
  const zod_1 = require("zod");
5
- // 将 JSON Schema 转换为 Zod Schema 的简易转换器
6
5
  function jsonSchemaToZod(parameters) {
7
6
  var _a;
8
- if (!parameters || !parameters.properties) {
9
- return zod_1.z.object({}).loose();
7
+ if (!parameters || typeof parameters !== 'object') {
8
+ return zod_1.z.object({}).passthrough();
10
9
  }
11
- const obj = {};
12
- const properties = parameters.properties;
13
- for (const key in properties) {
14
- const prop = properties[key];
10
+ // 辅助函数:递归转换单个属性
11
+ function convertProp(prop) {
15
12
  let schema = zod_1.z.any();
16
13
  if (prop.type === "string") {
17
14
  schema = zod_1.z.string();
@@ -22,12 +19,39 @@ function jsonSchemaToZod(parameters) {
22
19
  else if (prop.type === "boolean") {
23
20
  schema = zod_1.z.boolean();
24
21
  }
22
+ else if (prop.type === "array") {
23
+ // 递归处理数组项
24
+ if (prop.items) {
25
+ schema = zod_1.z.array(convertProp(prop.items));
26
+ }
27
+ else {
28
+ schema = zod_1.z.array(zod_1.z.any());
29
+ }
30
+ }
31
+ else if (prop.type === "object") {
32
+ // 递归处理嵌套对象
33
+ const nestedObj = {};
34
+ if (prop.properties) {
35
+ for (const key in prop.properties) {
36
+ nestedObj[key] = convertProp(prop.properties[key]);
37
+ }
38
+ }
39
+ schema = zod_1.z.object(nestedObj).passthrough();
40
+ }
25
41
  if (prop.description) {
26
42
  schema = schema.describe(prop.description);
27
43
  }
28
- // 默认都设为 optional 以防 Agent 报错,除非在 JSON Schema 的 required 数组中
44
+ return schema;
45
+ }
46
+ const obj = {};
47
+ const properties = parameters.properties || {};
48
+ for (const key in properties) {
49
+ const prop = properties[key];
50
+ let schema = convertProp(prop);
51
+ // 处理必填项
29
52
  const isRequired = (_a = parameters.required) === null || _a === void 0 ? void 0 : _a.includes(key);
30
53
  obj[key] = isRequired ? schema : schema.optional();
31
54
  }
32
- return zod_1.z.object(obj).loose();
55
+ // 使用 passthrough 或 loose 以增加容错性
56
+ return zod_1.z.object(obj).passthrough();
33
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saber2pr/ai-agent",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "AI Assistant CLI.",
5
5
  "author": "saber2pr",
6
6
  "license": "ISC",