ag-common 0.0.863 → 0.0.864
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/dist/api/helpers/zod.d.ts +3 -1
- package/dist/api/helpers/zod.js +40 -19
- package/package.json +1 -1
|
@@ -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"),
|
package/dist/api/helpers/zod.js
CHANGED
|
@@ -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 (
|
|
39
|
-
|
|
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 (
|
|
43
|
-
|
|
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 (
|
|
61
|
+
if (typeName === 'ZodString') {
|
|
48
62
|
return `z.string()${describeChain}`;
|
|
49
63
|
}
|
|
50
|
-
if (
|
|
64
|
+
if (typeName === 'ZodNumber') {
|
|
51
65
|
return `z.number()${describeChain}`;
|
|
52
66
|
}
|
|
53
|
-
if (
|
|
67
|
+
if (typeName === 'ZodBoolean') {
|
|
54
68
|
return `z.boolean()${describeChain}`;
|
|
55
69
|
}
|
|
56
|
-
if (
|
|
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 (
|
|
62
|
-
|
|
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 (
|
|
82
|
+
if (typeName === 'ZodObject') {
|
|
66
83
|
// Nested object - recurse
|
|
67
84
|
return zodSchemaToString(zodType);
|
|
68
85
|
}
|
|
69
|
-
if (
|
|
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 (
|
|
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 (
|
|
79
|
-
|
|
80
|
-
|
|
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 (
|
|
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
|
-
|
|
108
|
+
throw new Error(`Unsupported Zod type: ${typeName}`);
|
|
88
109
|
}
|
package/package.json
CHANGED