ag-common 0.0.863 → 0.0.865

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,8 +1,10 @@
1
- import { z } from 'zod';
1
+ import type { z } from 'zod';
2
2
  /**
3
3
  * Convert a Zod object schema to its code representation for use in LLM prompts.
4
4
  * Outputs actual Zod code like: z.object({ field: z.string().nullable().describe("...") })
5
5
  *
6
+ * Uses _def.typeName instead of instanceof checks to work across different Zod module instances.
7
+ *
6
8
  * @example
7
9
  * const schema = z.object({
8
10
  * name: z.string().describe("The user's name"),
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.zodSchemaToString = zodSchemaToString;
4
- const zod_1 = require("zod");
5
4
  /**
6
5
  * Convert a Zod object schema to its code representation for use in LLM prompts.
7
6
  * Outputs actual Zod code like: z.object({ field: z.string().nullable().describe("...") })
8
7
  *
8
+ * Uses _def.typeName instead of instanceof checks to work across different Zod module instances.
9
+ *
9
10
  * @example
10
11
  * const schema = z.object({
11
12
  * name: z.string().describe("The user's name"),
@@ -26,6 +27,14 @@ function zodSchemaToString(schema) {
26
27
  }
27
28
  return `z.object({\n${fields.join(',\n')}\n})`;
28
29
  }
30
+ /**
31
+ * Get the type name from a Zod type's internal definition.
32
+ * This works across different Zod module instances unlike instanceof.
33
+ */
34
+ function getTypeName(zodType) {
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ return zodType._def.typeName;
37
+ }
29
38
  /**
30
39
  * Recursively convert a Zod type to its code representation
31
40
  */
@@ -34,55 +43,67 @@ function zodTypeToCode(zodType) {
34
43
  const describeChain = description
35
44
  ? `.describe(${JSON.stringify(description)})`
36
45
  : '';
46
+ const typeName = getTypeName(zodType);
37
47
  // Handle wrapper types first (they wrap inner types)
38
- if (zodType instanceof zod_1.z.ZodOptional) {
39
- const inner = zodTypeToCode(zodType.unwrap()).replace(/\.describe\([^)]+\)$/, '');
48
+ if (typeName === 'ZodOptional') {
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ const innerType = zodType._def.innerType;
51
+ const inner = zodTypeToCode(innerType).replace(/\.describe\([^)]+\)$/, '');
40
52
  return `${inner}.optional()${describeChain}`;
41
53
  }
42
- if (zodType instanceof zod_1.z.ZodNullable) {
43
- const inner = zodTypeToCode(zodType.unwrap()).replace(/\.describe\([^)]+\)$/, '');
54
+ if (typeName === 'ZodNullable') {
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ const innerType = zodType._def.innerType;
57
+ const inner = zodTypeToCode(innerType).replace(/\.describe\([^)]+\)$/, '');
44
58
  return `${inner}.nullable()${describeChain}`;
45
59
  }
46
60
  // Handle base types
47
- if (zodType instanceof zod_1.z.ZodString) {
61
+ if (typeName === 'ZodString') {
48
62
  return `z.string()${describeChain}`;
49
63
  }
50
- if (zodType instanceof zod_1.z.ZodNumber) {
64
+ if (typeName === 'ZodNumber') {
51
65
  return `z.number()${describeChain}`;
52
66
  }
53
- if (zodType instanceof zod_1.z.ZodBoolean) {
67
+ if (typeName === 'ZodBoolean') {
54
68
  return `z.boolean()${describeChain}`;
55
69
  }
56
- if (zodType instanceof zod_1.z.ZodEnum) {
70
+ if (typeName === 'ZodEnum') {
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
72
  const values = zodType._def.values;
58
73
  const enumStr = values.map((v) => JSON.stringify(v)).join(', ');
59
74
  return `z.enum([${enumStr}])${describeChain}`;
60
75
  }
61
- if (zodType instanceof zod_1.z.ZodArray) {
62
- const innerType = zodTypeToCode(zodType.element);
76
+ if (typeName === 'ZodArray') {
77
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
+ const elementType = zodType._def.type;
79
+ const innerType = zodTypeToCode(elementType);
63
80
  return `z.array(${innerType})${describeChain}`;
64
81
  }
65
- if (zodType instanceof zod_1.z.ZodObject) {
82
+ if (typeName === 'ZodObject') {
66
83
  // Nested object - recurse
67
84
  return zodSchemaToString(zodType);
68
85
  }
69
- if (zodType instanceof zod_1.z.ZodLiteral) {
86
+ if (typeName === 'ZodLiteral') {
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
88
  const value = zodType._def.value;
71
89
  return `z.literal(${JSON.stringify(value)})${describeChain}`;
72
90
  }
73
- if (zodType instanceof zod_1.z.ZodUnion) {
91
+ if (typeName === 'ZodUnion') {
92
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
93
  const options = zodType._def.options;
75
94
  const optionsStr = options.map((opt) => zodTypeToCode(opt)).join(', ');
76
95
  return `z.union([${optionsStr}])${describeChain}`;
77
96
  }
78
- if (zodType instanceof zod_1.z.ZodRecord) {
79
- const valueType = zodTypeToCode(zodType._def.valueType);
80
- return `z.record(${valueType})${describeChain}`;
97
+ if (typeName === 'ZodRecord') {
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ const valueType = zodType._def.valueType;
100
+ return `z.record(${zodTypeToCode(valueType)})${describeChain}`;
81
101
  }
82
- if (zodType instanceof zod_1.z.ZodTuple) {
102
+ if (typeName === 'ZodTuple') {
103
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
104
  const items = zodType._def.items;
84
105
  const itemsStr = items.map((item) => zodTypeToCode(item)).join(', ');
85
106
  return `z.tuple([${itemsStr}])${describeChain}`;
86
107
  }
87
- return `z.unknown()${describeChain}`;
108
+ throw new Error(`Unsupported Zod type: ${typeName}`);
88
109
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.863",
2
+ "version": "0.0.865",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "cross-env": "^10.1.0",
18
18
  "eslint": "^9.39.2",
19
19
  "eslint-config-e7npm": "^0.1.31",
20
- "lucide-react": "^0.562.0",
20
+ "lucide-react": "^0.563.0",
21
21
  "react": "^19.2.3",
22
22
  "react-dom": "^19.2.3",
23
23
  "tailwind-merge": "^3.4.0",