flavor-code 1.2.13 → 1.2.15

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.
Files changed (136) hide show
  1. package/README.md +9 -1
  2. package/README.zh-CN.md +9 -1
  3. package/dist/agent/planner.d.ts +2 -0
  4. package/dist/{app-Z3YWFIBB.js → app-3KW2FTVJ.js} +2 -2
  5. package/dist/astgraph/db.mjs +237 -237
  6. package/dist/astgraph/extract.mjs +239 -239
  7. package/dist/astgraph/flavor-plugin.json +25 -25
  8. package/dist/astgraph/grammars.mjs +68 -68
  9. package/dist/astgraph/index.js +301 -301
  10. package/dist/astgraph/indexer.mjs +175 -175
  11. package/dist/astgraph/query.mjs +165 -165
  12. package/dist/astgraph/resolve.mjs +93 -93
  13. package/dist/astgraph/vendor/tree-sitter.js +3980 -3980
  14. package/dist/astgraph/vendor/zod/LICENSE +21 -21
  15. package/dist/astgraph/vendor/zod/index.js +4 -4
  16. package/dist/astgraph/vendor/zod/locales/index.js +1 -1
  17. package/dist/astgraph/vendor/zod/locales/package.json +7 -7
  18. package/dist/astgraph/vendor/zod/package.json +135 -135
  19. package/dist/astgraph/vendor/zod/v4/classic/checks.js +1 -1
  20. package/dist/astgraph/vendor/zod/v4/classic/coerce.js +17 -17
  21. package/dist/astgraph/vendor/zod/v4/classic/compat.js +31 -31
  22. package/dist/astgraph/vendor/zod/v4/classic/errors.js +48 -48
  23. package/dist/astgraph/vendor/zod/v4/classic/external.js +20 -20
  24. package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +599 -599
  25. package/dist/astgraph/vendor/zod/v4/classic/index.js +4 -4
  26. package/dist/astgraph/vendor/zod/v4/classic/iso.js +30 -30
  27. package/dist/astgraph/vendor/zod/v4/classic/package.json +7 -7
  28. package/dist/astgraph/vendor/zod/v4/classic/parse.js +15 -15
  29. package/dist/astgraph/vendor/zod/v4/classic/schemas.js +1395 -1395
  30. package/dist/astgraph/vendor/zod/v4/core/api.js +1087 -1087
  31. package/dist/astgraph/vendor/zod/v4/core/checks.js +575 -575
  32. package/dist/astgraph/vendor/zod/v4/core/core.js +78 -78
  33. package/dist/astgraph/vendor/zod/v4/core/doc.js +35 -35
  34. package/dist/astgraph/vendor/zod/v4/core/errors.js +185 -185
  35. package/dist/astgraph/vendor/zod/v4/core/index.js +16 -16
  36. package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +95 -95
  37. package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +601 -601
  38. package/dist/astgraph/vendor/zod/v4/core/json-schema.js +1 -1
  39. package/dist/astgraph/vendor/zod/v4/core/package.json +7 -7
  40. package/dist/astgraph/vendor/zod/v4/core/parse.js +93 -93
  41. package/dist/astgraph/vendor/zod/v4/core/regexes.js +139 -139
  42. package/dist/astgraph/vendor/zod/v4/core/registries.js +51 -51
  43. package/dist/astgraph/vendor/zod/v4/core/schemas.js +2239 -2239
  44. package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +1 -1
  45. package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +448 -448
  46. package/dist/astgraph/vendor/zod/v4/core/util.js +674 -674
  47. package/dist/astgraph/vendor/zod/v4/core/versions.js +5 -5
  48. package/dist/astgraph/vendor/zod/v4/index.js +3 -3
  49. package/dist/astgraph/vendor/zod/v4/locales/ar.js +106 -106
  50. package/dist/astgraph/vendor/zod/v4/locales/az.js +105 -105
  51. package/dist/astgraph/vendor/zod/v4/locales/be.js +156 -156
  52. package/dist/astgraph/vendor/zod/v4/locales/bg.js +120 -120
  53. package/dist/astgraph/vendor/zod/v4/locales/ca.js +107 -107
  54. package/dist/astgraph/vendor/zod/v4/locales/cs.js +111 -111
  55. package/dist/astgraph/vendor/zod/v4/locales/da.js +115 -115
  56. package/dist/astgraph/vendor/zod/v4/locales/de.js +108 -108
  57. package/dist/astgraph/vendor/zod/v4/locales/el.js +109 -109
  58. package/dist/astgraph/vendor/zod/v4/locales/en.js +113 -113
  59. package/dist/astgraph/vendor/zod/v4/locales/eo.js +109 -109
  60. package/dist/astgraph/vendor/zod/v4/locales/es.js +132 -132
  61. package/dist/astgraph/vendor/zod/v4/locales/fa.js +114 -114
  62. package/dist/astgraph/vendor/zod/v4/locales/fi.js +112 -112
  63. package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +107 -107
  64. package/dist/astgraph/vendor/zod/v4/locales/fr.js +125 -125
  65. package/dist/astgraph/vendor/zod/v4/locales/he.js +214 -214
  66. package/dist/astgraph/vendor/zod/v4/locales/hr.js +122 -122
  67. package/dist/astgraph/vendor/zod/v4/locales/hu.js +108 -108
  68. package/dist/astgraph/vendor/zod/v4/locales/hy.js +147 -147
  69. package/dist/astgraph/vendor/zod/v4/locales/id.js +106 -106
  70. package/dist/astgraph/vendor/zod/v4/locales/index.js +52 -52
  71. package/dist/astgraph/vendor/zod/v4/locales/is.js +109 -109
  72. package/dist/astgraph/vendor/zod/v4/locales/it.js +108 -108
  73. package/dist/astgraph/vendor/zod/v4/locales/ja.js +107 -107
  74. package/dist/astgraph/vendor/zod/v4/locales/ka.js +112 -112
  75. package/dist/astgraph/vendor/zod/v4/locales/kh.js +5 -5
  76. package/dist/astgraph/vendor/zod/v4/locales/km.js +110 -110
  77. package/dist/astgraph/vendor/zod/v4/locales/ko.js +111 -111
  78. package/dist/astgraph/vendor/zod/v4/locales/lt.js +203 -203
  79. package/dist/astgraph/vendor/zod/v4/locales/mk.js +109 -109
  80. package/dist/astgraph/vendor/zod/v4/locales/ms.js +107 -107
  81. package/dist/astgraph/vendor/zod/v4/locales/nl.js +110 -110
  82. package/dist/astgraph/vendor/zod/v4/locales/no.js +108 -108
  83. package/dist/astgraph/vendor/zod/v4/locales/ota.js +109 -109
  84. package/dist/astgraph/vendor/zod/v4/locales/package.json +7 -7
  85. package/dist/astgraph/vendor/zod/v4/locales/pl.js +109 -109
  86. package/dist/astgraph/vendor/zod/v4/locales/ps.js +114 -114
  87. package/dist/astgraph/vendor/zod/v4/locales/pt.js +108 -108
  88. package/dist/astgraph/vendor/zod/v4/locales/ro.js +119 -119
  89. package/dist/astgraph/vendor/zod/v4/locales/ru.js +156 -156
  90. package/dist/astgraph/vendor/zod/v4/locales/sl.js +109 -109
  91. package/dist/astgraph/vendor/zod/v4/locales/sv.js +110 -110
  92. package/dist/astgraph/vendor/zod/v4/locales/ta.js +110 -110
  93. package/dist/astgraph/vendor/zod/v4/locales/th.js +110 -110
  94. package/dist/astgraph/vendor/zod/v4/locales/tr.js +105 -105
  95. package/dist/astgraph/vendor/zod/v4/locales/ua.js +5 -5
  96. package/dist/astgraph/vendor/zod/v4/locales/uk.js +108 -108
  97. package/dist/astgraph/vendor/zod/v4/locales/ur.js +110 -110
  98. package/dist/astgraph/vendor/zod/v4/locales/uz.js +110 -110
  99. package/dist/astgraph/vendor/zod/v4/locales/vi.js +108 -108
  100. package/dist/astgraph/vendor/zod/v4/locales/yo.js +107 -107
  101. package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +109 -109
  102. package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +107 -107
  103. package/dist/astgraph/vendor/zod/v4/mini/checks.js +1 -1
  104. package/dist/astgraph/vendor/zod/v4/mini/coerce.js +22 -22
  105. package/dist/astgraph/vendor/zod/v4/mini/external.js +14 -14
  106. package/dist/astgraph/vendor/zod/v4/mini/index.js +3 -3
  107. package/dist/astgraph/vendor/zod/v4/mini/iso.js +34 -34
  108. package/dist/astgraph/vendor/zod/v4/mini/package.json +7 -7
  109. package/dist/astgraph/vendor/zod/v4/mini/parse.js +1 -1
  110. package/dist/astgraph/vendor/zod/v4/mini/schemas.js +961 -961
  111. package/dist/astgraph/vendor/zod/v4/package.json +7 -7
  112. package/dist/{chunk-Y5MZDG7I.js → chunk-5TG3E2EJ.js} +3253 -1909
  113. package/dist/{chunk-G6Q2Y2A7.js → chunk-BKARQJQZ.js} +1 -1
  114. package/dist/{chunk-XFCJXRJ2.js → chunk-IG4Z23CL.js} +6 -0
  115. package/dist/cli.js +80 -18
  116. package/dist/config/schema.d.ts +6 -0
  117. package/dist/desktop/main.js +6216 -4807
  118. package/dist/desktop-renderer/assets/{index-CCnSpff0.js → index-BCJjMvbX.js} +1 -1
  119. package/dist/desktop-renderer/index.html +1 -1
  120. package/dist/evolve/loader.d.ts +21 -0
  121. package/dist/evolve/service.d.ts +37 -0
  122. package/dist/evolve/store.d.ts +117 -0
  123. package/dist/git/insights.d.ts +52 -0
  124. package/dist/git/service.d.ts +43 -0
  125. package/dist/git/tools.d.ts +9 -0
  126. package/dist/hooks/types.d.ts +5 -1
  127. package/dist/{load-6ZMEBHSG.js → load-GKA7P6RJ.js} +1 -1
  128. package/dist/plugins/host.d.ts +9 -0
  129. package/dist/plugins/types.d.ts +1 -0
  130. package/dist/sdk/index.js +3 -3
  131. package/dist/session/store.d.ts +1 -0
  132. package/dist/tools/shell.d.ts +13 -0
  133. package/dist/ui/commands.d.ts +11 -2
  134. package/dist/ui/session.d.ts +3 -0
  135. package/package.json +1 -1
  136. package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +303 -1
@@ -1,448 +1,448 @@
1
- import { globalRegistry } from "./registries.js";
2
- // function initializeContext<T extends schemas.$ZodType>(inputs: JSONSchemaGeneratorParams<T>): ToJSONSchemaContext<T> {
3
- // return {
4
- // processor: inputs.processor,
5
- // metadataRegistry: inputs.metadata ?? globalRegistry,
6
- // target: inputs.target ?? "draft-2020-12",
7
- // unrepresentable: inputs.unrepresentable ?? "throw",
8
- // };
9
- // }
10
- export function initializeContext(params) {
11
- // Normalize target: convert old non-hyphenated versions to hyphenated versions
12
- let target = params?.target ?? "draft-2020-12";
13
- if (target === "draft-4")
14
- target = "draft-04";
15
- if (target === "draft-7")
16
- target = "draft-07";
17
- return {
18
- processors: params.processors ?? {},
19
- metadataRegistry: params?.metadata ?? globalRegistry,
20
- target,
21
- unrepresentable: params?.unrepresentable ?? "throw",
22
- override: params?.override ?? (() => { }),
23
- io: params?.io ?? "output",
24
- counter: 0,
25
- seen: new Map(),
26
- cycles: params?.cycles ?? "ref",
27
- reused: params?.reused ?? "inline",
28
- external: params?.external ?? undefined,
29
- };
30
- }
31
- export function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
32
- var _a;
33
- const def = schema._zod.def;
34
- // check for schema in seens
35
- const seen = ctx.seen.get(schema);
36
- if (seen) {
37
- seen.count++;
38
- // check if cycle
39
- const isCycle = _params.schemaPath.includes(schema);
40
- if (isCycle) {
41
- seen.cycle = _params.path;
42
- }
43
- return seen.schema;
44
- }
45
- // initialize
46
- const result = { schema: {}, count: 1, cycle: undefined, path: _params.path };
47
- ctx.seen.set(schema, result);
48
- // custom method overrides default behavior
49
- const overrideSchema = schema._zod.toJSONSchema?.();
50
- if (overrideSchema) {
51
- result.schema = overrideSchema;
52
- }
53
- else {
54
- const params = {
55
- ..._params,
56
- schemaPath: [..._params.schemaPath, schema],
57
- path: _params.path,
58
- };
59
- if (schema._zod.processJSONSchema) {
60
- schema._zod.processJSONSchema(ctx, result.schema, params);
61
- }
62
- else {
63
- const _json = result.schema;
64
- const processor = ctx.processors[def.type];
65
- if (!processor) {
66
- throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
67
- }
68
- processor(schema, ctx, _json, params);
69
- }
70
- const parent = schema._zod.parent;
71
- if (parent) {
72
- // Also set ref if processor didn't (for inheritance)
73
- if (!result.ref)
74
- result.ref = parent;
75
- process(parent, ctx, params);
76
- ctx.seen.get(parent).isParent = true;
77
- }
78
- }
79
- // metadata
80
- const meta = ctx.metadataRegistry.get(schema);
81
- if (meta)
82
- Object.assign(result.schema, meta);
83
- if (ctx.io === "input" && isTransforming(schema)) {
84
- // examples/defaults only apply to output type of pipe
85
- delete result.schema.examples;
86
- delete result.schema.default;
87
- }
88
- // set prefault as default
89
- if (ctx.io === "input" && "_prefault" in result.schema)
90
- (_a = result.schema).default ?? (_a.default = result.schema._prefault);
91
- delete result.schema._prefault;
92
- // pulling fresh from ctx.seen in case it was overwritten
93
- const _result = ctx.seen.get(schema);
94
- return _result.schema;
95
- }
96
- export function extractDefs(ctx, schema
97
- // params: EmitParams
98
- ) {
99
- // iterate over seen map;
100
- const root = ctx.seen.get(schema);
101
- if (!root)
102
- throw new Error("Unprocessed schema. This is a bug in Zod.");
103
- // Track ids to detect duplicates across different schemas
104
- const idToSchema = new Map();
105
- for (const entry of ctx.seen.entries()) {
106
- const id = ctx.metadataRegistry.get(entry[0])?.id;
107
- if (id) {
108
- const existing = idToSchema.get(id);
109
- if (existing && existing !== entry[0]) {
110
- throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
111
- }
112
- idToSchema.set(id, entry[0]);
113
- }
114
- }
115
- // returns a ref to the schema
116
- // defId will be empty if the ref points to an external schema (or #)
117
- const makeURI = (entry) => {
118
- // comparing the seen objects because sometimes
119
- // multiple schemas map to the same seen object.
120
- // e.g. lazy
121
- // external is configured
122
- const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
123
- if (ctx.external) {
124
- const externalId = ctx.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${ctx.counter++}`;
125
- // check if schema is in the external registry
126
- const uriGenerator = ctx.external.uri ?? ((id) => id);
127
- if (externalId) {
128
- return { ref: uriGenerator(externalId) };
129
- }
130
- // otherwise, add to __shared
131
- const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
132
- entry[1].defId = id; // set defId so it will be reused if needed
133
- return { defId: id, ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}` };
134
- }
135
- if (entry[1] === root) {
136
- return { ref: "#" };
137
- }
138
- // self-contained schema
139
- const uriPrefix = `#`;
140
- const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
141
- const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
142
- return { defId, ref: defUriPrefix + defId };
143
- };
144
- // stored cached version in `def` property
145
- // remove all properties, set $ref
146
- const extractToDef = (entry) => {
147
- // if the schema is already a reference, do not extract it
148
- if (entry[1].schema.$ref) {
149
- return;
150
- }
151
- const seen = entry[1];
152
- const { ref, defId } = makeURI(entry);
153
- seen.def = { ...seen.schema };
154
- // defId won't be set if the schema is a reference to an external schema
155
- // or if the schema is the root schema
156
- if (defId)
157
- seen.defId = defId;
158
- // wipe away all properties except $ref
159
- const schema = seen.schema;
160
- for (const key in schema) {
161
- delete schema[key];
162
- }
163
- schema.$ref = ref;
164
- };
165
- // throw on cycles
166
- // break cycles
167
- if (ctx.cycles === "throw") {
168
- for (const entry of ctx.seen.entries()) {
169
- const seen = entry[1];
170
- if (seen.cycle) {
171
- throw new Error("Cycle detected: " +
172
- `#/${seen.cycle?.join("/")}/<root>` +
173
- '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.');
174
- }
175
- }
176
- }
177
- // extract schemas into $defs
178
- for (const entry of ctx.seen.entries()) {
179
- const seen = entry[1];
180
- // convert root schema to # $ref
181
- if (schema === entry[0]) {
182
- extractToDef(entry); // this has special handling for the root schema
183
- continue;
184
- }
185
- // extract schemas that are in the external registry
186
- if (ctx.external) {
187
- const ext = ctx.external.registry.get(entry[0])?.id;
188
- if (schema !== entry[0] && ext) {
189
- extractToDef(entry);
190
- continue;
191
- }
192
- }
193
- // extract schemas with `id` meta
194
- const id = ctx.metadataRegistry.get(entry[0])?.id;
195
- if (id) {
196
- extractToDef(entry);
197
- continue;
198
- }
199
- // break cycles
200
- if (seen.cycle) {
201
- // any
202
- extractToDef(entry);
203
- continue;
204
- }
205
- // extract reused schemas
206
- if (seen.count > 1) {
207
- if (ctx.reused === "ref") {
208
- extractToDef(entry);
209
- // biome-ignore lint:
210
- continue;
211
- }
212
- }
213
- }
214
- }
215
- export function finalize(ctx, schema) {
216
- const root = ctx.seen.get(schema);
217
- if (!root)
218
- throw new Error("Unprocessed schema. This is a bug in Zod.");
219
- // flatten refs - inherit properties from parent schemas
220
- const flattenRef = (zodSchema) => {
221
- const seen = ctx.seen.get(zodSchema);
222
- // already processed
223
- if (seen.ref === null)
224
- return;
225
- const schema = seen.def ?? seen.schema;
226
- const _cached = { ...schema };
227
- const ref = seen.ref;
228
- seen.ref = null; // prevent infinite recursion
229
- if (ref) {
230
- flattenRef(ref);
231
- const refSeen = ctx.seen.get(ref);
232
- const refSchema = refSeen.schema;
233
- // merge referenced schema into current
234
- if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
235
- // older drafts can't combine $ref with other properties
236
- schema.allOf = schema.allOf ?? [];
237
- schema.allOf.push(refSchema);
238
- }
239
- else {
240
- Object.assign(schema, refSchema);
241
- }
242
- // restore child's own properties (child wins)
243
- Object.assign(schema, _cached);
244
- const isParentRef = zodSchema._zod.parent === ref;
245
- // For parent chain, child is a refinement - remove parent-only properties
246
- if (isParentRef) {
247
- for (const key in schema) {
248
- if (key === "$ref" || key === "allOf")
249
- continue;
250
- if (!(key in _cached)) {
251
- delete schema[key];
252
- }
253
- }
254
- }
255
- // When ref was extracted to $defs, remove properties that match the definition
256
- if (refSchema.$ref && refSeen.def) {
257
- for (const key in schema) {
258
- if (key === "$ref" || key === "allOf")
259
- continue;
260
- if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) {
261
- delete schema[key];
262
- }
263
- }
264
- }
265
- }
266
- // If parent was extracted (has $ref), propagate $ref to this schema
267
- // This handles cases like: readonly().meta({id}).describe()
268
- // where processor sets ref to innerType but parent should be referenced
269
- const parent = zodSchema._zod.parent;
270
- if (parent && parent !== ref) {
271
- // Ensure parent is processed first so its def has inherited properties
272
- flattenRef(parent);
273
- const parentSeen = ctx.seen.get(parent);
274
- if (parentSeen?.schema.$ref) {
275
- schema.$ref = parentSeen.schema.$ref;
276
- // De-duplicate with parent's definition
277
- if (parentSeen.def) {
278
- for (const key in schema) {
279
- if (key === "$ref" || key === "allOf")
280
- continue;
281
- if (key in parentSeen.def && JSON.stringify(schema[key]) === JSON.stringify(parentSeen.def[key])) {
282
- delete schema[key];
283
- }
284
- }
285
- }
286
- }
287
- }
288
- // execute overrides
289
- ctx.override({
290
- zodSchema: zodSchema,
291
- jsonSchema: schema,
292
- path: seen.path ?? [],
293
- });
294
- };
295
- for (const entry of [...ctx.seen.entries()].reverse()) {
296
- flattenRef(entry[0]);
297
- }
298
- const result = {};
299
- if (ctx.target === "draft-2020-12") {
300
- result.$schema = "https://json-schema.org/draft/2020-12/schema";
301
- }
302
- else if (ctx.target === "draft-07") {
303
- result.$schema = "http://json-schema.org/draft-07/schema#";
304
- }
305
- else if (ctx.target === "draft-04") {
306
- result.$schema = "http://json-schema.org/draft-04/schema#";
307
- }
308
- else if (ctx.target === "openapi-3.0") {
309
- // OpenAPI 3.0 schema objects should not include a $schema property
310
- }
311
- else {
312
- // Arbitrary string values are allowed but won't have a $schema property set
313
- }
314
- if (ctx.external?.uri) {
315
- const id = ctx.external.registry.get(schema)?.id;
316
- if (!id)
317
- throw new Error("Schema is missing an `id` property");
318
- result.$id = ctx.external.uri(id);
319
- }
320
- Object.assign(result, root.def ?? root.schema);
321
- // The `id` in `.meta()` is a Zod-specific registration tag used to extract
322
- // schemas into $defs — it is not user-facing JSON Schema metadata. Strip it
323
- // from the output body where it would otherwise leak. The id is preserved
324
- // implicitly via the $defs key (and via $ref paths).
325
- const rootMetaId = ctx.metadataRegistry.get(schema)?.id;
326
- if (rootMetaId !== undefined && result.id === rootMetaId)
327
- delete result.id;
328
- // build defs object
329
- const defs = ctx.external?.defs ?? {};
330
- for (const entry of ctx.seen.entries()) {
331
- const seen = entry[1];
332
- if (seen.def && seen.defId) {
333
- if (seen.def.id === seen.defId)
334
- delete seen.def.id;
335
- defs[seen.defId] = seen.def;
336
- }
337
- }
338
- // set definitions in result
339
- if (ctx.external) {
340
- }
341
- else {
342
- if (Object.keys(defs).length > 0) {
343
- if (ctx.target === "draft-2020-12") {
344
- result.$defs = defs;
345
- }
346
- else {
347
- result.definitions = defs;
348
- }
349
- }
350
- }
351
- try {
352
- // this "finalizes" this schema and ensures all cycles are removed
353
- // each call to finalize() is functionally independent
354
- // though the seen map is shared
355
- const finalized = JSON.parse(JSON.stringify(result));
356
- Object.defineProperty(finalized, "~standard", {
357
- value: {
358
- ...schema["~standard"],
359
- jsonSchema: {
360
- input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
361
- output: createStandardJSONSchemaMethod(schema, "output", ctx.processors),
362
- },
363
- },
364
- enumerable: false,
365
- writable: false,
366
- });
367
- return finalized;
368
- }
369
- catch (_err) {
370
- throw new Error("Error converting schema to JSON.");
371
- }
372
- }
373
- function isTransforming(_schema, _ctx) {
374
- const ctx = _ctx ?? { seen: new Set() };
375
- if (ctx.seen.has(_schema))
376
- return false;
377
- ctx.seen.add(_schema);
378
- const def = _schema._zod.def;
379
- if (def.type === "transform")
380
- return true;
381
- if (def.type === "array")
382
- return isTransforming(def.element, ctx);
383
- if (def.type === "set")
384
- return isTransforming(def.valueType, ctx);
385
- if (def.type === "lazy")
386
- return isTransforming(def.getter(), ctx);
387
- if (def.type === "promise" ||
388
- def.type === "optional" ||
389
- def.type === "nonoptional" ||
390
- def.type === "nullable" ||
391
- def.type === "readonly" ||
392
- def.type === "default" ||
393
- def.type === "prefault") {
394
- return isTransforming(def.innerType, ctx);
395
- }
396
- if (def.type === "intersection") {
397
- return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
398
- }
399
- if (def.type === "record" || def.type === "map") {
400
- return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
401
- }
402
- if (def.type === "pipe") {
403
- if (_schema._zod.traits.has("$ZodCodec"))
404
- return true;
405
- return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
406
- }
407
- if (def.type === "object") {
408
- for (const key in def.shape) {
409
- if (isTransforming(def.shape[key], ctx))
410
- return true;
411
- }
412
- return false;
413
- }
414
- if (def.type === "union") {
415
- for (const option of def.options) {
416
- if (isTransforming(option, ctx))
417
- return true;
418
- }
419
- return false;
420
- }
421
- if (def.type === "tuple") {
422
- for (const item of def.items) {
423
- if (isTransforming(item, ctx))
424
- return true;
425
- }
426
- if (def.rest && isTransforming(def.rest, ctx))
427
- return true;
428
- return false;
429
- }
430
- return false;
431
- }
432
- /**
433
- * Creates a toJSONSchema method for a schema instance.
434
- * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
435
- */
436
- export const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
437
- const ctx = initializeContext({ ...params, processors });
438
- process(schema, ctx);
439
- extractDefs(ctx, schema);
440
- return finalize(ctx, schema);
441
- };
442
- export const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
443
- const { libraryOptions, target } = params ?? {};
444
- const ctx = initializeContext({ ...(libraryOptions ?? {}), target, io, processors });
445
- process(schema, ctx);
446
- extractDefs(ctx, schema);
447
- return finalize(ctx, schema);
448
- };
1
+ import { globalRegistry } from "./registries.js";
2
+ // function initializeContext<T extends schemas.$ZodType>(inputs: JSONSchemaGeneratorParams<T>): ToJSONSchemaContext<T> {
3
+ // return {
4
+ // processor: inputs.processor,
5
+ // metadataRegistry: inputs.metadata ?? globalRegistry,
6
+ // target: inputs.target ?? "draft-2020-12",
7
+ // unrepresentable: inputs.unrepresentable ?? "throw",
8
+ // };
9
+ // }
10
+ export function initializeContext(params) {
11
+ // Normalize target: convert old non-hyphenated versions to hyphenated versions
12
+ let target = params?.target ?? "draft-2020-12";
13
+ if (target === "draft-4")
14
+ target = "draft-04";
15
+ if (target === "draft-7")
16
+ target = "draft-07";
17
+ return {
18
+ processors: params.processors ?? {},
19
+ metadataRegistry: params?.metadata ?? globalRegistry,
20
+ target,
21
+ unrepresentable: params?.unrepresentable ?? "throw",
22
+ override: params?.override ?? (() => { }),
23
+ io: params?.io ?? "output",
24
+ counter: 0,
25
+ seen: new Map(),
26
+ cycles: params?.cycles ?? "ref",
27
+ reused: params?.reused ?? "inline",
28
+ external: params?.external ?? undefined,
29
+ };
30
+ }
31
+ export function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
32
+ var _a;
33
+ const def = schema._zod.def;
34
+ // check for schema in seens
35
+ const seen = ctx.seen.get(schema);
36
+ if (seen) {
37
+ seen.count++;
38
+ // check if cycle
39
+ const isCycle = _params.schemaPath.includes(schema);
40
+ if (isCycle) {
41
+ seen.cycle = _params.path;
42
+ }
43
+ return seen.schema;
44
+ }
45
+ // initialize
46
+ const result = { schema: {}, count: 1, cycle: undefined, path: _params.path };
47
+ ctx.seen.set(schema, result);
48
+ // custom method overrides default behavior
49
+ const overrideSchema = schema._zod.toJSONSchema?.();
50
+ if (overrideSchema) {
51
+ result.schema = overrideSchema;
52
+ }
53
+ else {
54
+ const params = {
55
+ ..._params,
56
+ schemaPath: [..._params.schemaPath, schema],
57
+ path: _params.path,
58
+ };
59
+ if (schema._zod.processJSONSchema) {
60
+ schema._zod.processJSONSchema(ctx, result.schema, params);
61
+ }
62
+ else {
63
+ const _json = result.schema;
64
+ const processor = ctx.processors[def.type];
65
+ if (!processor) {
66
+ throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
67
+ }
68
+ processor(schema, ctx, _json, params);
69
+ }
70
+ const parent = schema._zod.parent;
71
+ if (parent) {
72
+ // Also set ref if processor didn't (for inheritance)
73
+ if (!result.ref)
74
+ result.ref = parent;
75
+ process(parent, ctx, params);
76
+ ctx.seen.get(parent).isParent = true;
77
+ }
78
+ }
79
+ // metadata
80
+ const meta = ctx.metadataRegistry.get(schema);
81
+ if (meta)
82
+ Object.assign(result.schema, meta);
83
+ if (ctx.io === "input" && isTransforming(schema)) {
84
+ // examples/defaults only apply to output type of pipe
85
+ delete result.schema.examples;
86
+ delete result.schema.default;
87
+ }
88
+ // set prefault as default
89
+ if (ctx.io === "input" && "_prefault" in result.schema)
90
+ (_a = result.schema).default ?? (_a.default = result.schema._prefault);
91
+ delete result.schema._prefault;
92
+ // pulling fresh from ctx.seen in case it was overwritten
93
+ const _result = ctx.seen.get(schema);
94
+ return _result.schema;
95
+ }
96
+ export function extractDefs(ctx, schema
97
+ // params: EmitParams
98
+ ) {
99
+ // iterate over seen map;
100
+ const root = ctx.seen.get(schema);
101
+ if (!root)
102
+ throw new Error("Unprocessed schema. This is a bug in Zod.");
103
+ // Track ids to detect duplicates across different schemas
104
+ const idToSchema = new Map();
105
+ for (const entry of ctx.seen.entries()) {
106
+ const id = ctx.metadataRegistry.get(entry[0])?.id;
107
+ if (id) {
108
+ const existing = idToSchema.get(id);
109
+ if (existing && existing !== entry[0]) {
110
+ throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
111
+ }
112
+ idToSchema.set(id, entry[0]);
113
+ }
114
+ }
115
+ // returns a ref to the schema
116
+ // defId will be empty if the ref points to an external schema (or #)
117
+ const makeURI = (entry) => {
118
+ // comparing the seen objects because sometimes
119
+ // multiple schemas map to the same seen object.
120
+ // e.g. lazy
121
+ // external is configured
122
+ const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
123
+ if (ctx.external) {
124
+ const externalId = ctx.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${ctx.counter++}`;
125
+ // check if schema is in the external registry
126
+ const uriGenerator = ctx.external.uri ?? ((id) => id);
127
+ if (externalId) {
128
+ return { ref: uriGenerator(externalId) };
129
+ }
130
+ // otherwise, add to __shared
131
+ const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
132
+ entry[1].defId = id; // set defId so it will be reused if needed
133
+ return { defId: id, ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}` };
134
+ }
135
+ if (entry[1] === root) {
136
+ return { ref: "#" };
137
+ }
138
+ // self-contained schema
139
+ const uriPrefix = `#`;
140
+ const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
141
+ const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
142
+ return { defId, ref: defUriPrefix + defId };
143
+ };
144
+ // stored cached version in `def` property
145
+ // remove all properties, set $ref
146
+ const extractToDef = (entry) => {
147
+ // if the schema is already a reference, do not extract it
148
+ if (entry[1].schema.$ref) {
149
+ return;
150
+ }
151
+ const seen = entry[1];
152
+ const { ref, defId } = makeURI(entry);
153
+ seen.def = { ...seen.schema };
154
+ // defId won't be set if the schema is a reference to an external schema
155
+ // or if the schema is the root schema
156
+ if (defId)
157
+ seen.defId = defId;
158
+ // wipe away all properties except $ref
159
+ const schema = seen.schema;
160
+ for (const key in schema) {
161
+ delete schema[key];
162
+ }
163
+ schema.$ref = ref;
164
+ };
165
+ // throw on cycles
166
+ // break cycles
167
+ if (ctx.cycles === "throw") {
168
+ for (const entry of ctx.seen.entries()) {
169
+ const seen = entry[1];
170
+ if (seen.cycle) {
171
+ throw new Error("Cycle detected: " +
172
+ `#/${seen.cycle?.join("/")}/<root>` +
173
+ '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.');
174
+ }
175
+ }
176
+ }
177
+ // extract schemas into $defs
178
+ for (const entry of ctx.seen.entries()) {
179
+ const seen = entry[1];
180
+ // convert root schema to # $ref
181
+ if (schema === entry[0]) {
182
+ extractToDef(entry); // this has special handling for the root schema
183
+ continue;
184
+ }
185
+ // extract schemas that are in the external registry
186
+ if (ctx.external) {
187
+ const ext = ctx.external.registry.get(entry[0])?.id;
188
+ if (schema !== entry[0] && ext) {
189
+ extractToDef(entry);
190
+ continue;
191
+ }
192
+ }
193
+ // extract schemas with `id` meta
194
+ const id = ctx.metadataRegistry.get(entry[0])?.id;
195
+ if (id) {
196
+ extractToDef(entry);
197
+ continue;
198
+ }
199
+ // break cycles
200
+ if (seen.cycle) {
201
+ // any
202
+ extractToDef(entry);
203
+ continue;
204
+ }
205
+ // extract reused schemas
206
+ if (seen.count > 1) {
207
+ if (ctx.reused === "ref") {
208
+ extractToDef(entry);
209
+ // biome-ignore lint:
210
+ continue;
211
+ }
212
+ }
213
+ }
214
+ }
215
+ export function finalize(ctx, schema) {
216
+ const root = ctx.seen.get(schema);
217
+ if (!root)
218
+ throw new Error("Unprocessed schema. This is a bug in Zod.");
219
+ // flatten refs - inherit properties from parent schemas
220
+ const flattenRef = (zodSchema) => {
221
+ const seen = ctx.seen.get(zodSchema);
222
+ // already processed
223
+ if (seen.ref === null)
224
+ return;
225
+ const schema = seen.def ?? seen.schema;
226
+ const _cached = { ...schema };
227
+ const ref = seen.ref;
228
+ seen.ref = null; // prevent infinite recursion
229
+ if (ref) {
230
+ flattenRef(ref);
231
+ const refSeen = ctx.seen.get(ref);
232
+ const refSchema = refSeen.schema;
233
+ // merge referenced schema into current
234
+ if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
235
+ // older drafts can't combine $ref with other properties
236
+ schema.allOf = schema.allOf ?? [];
237
+ schema.allOf.push(refSchema);
238
+ }
239
+ else {
240
+ Object.assign(schema, refSchema);
241
+ }
242
+ // restore child's own properties (child wins)
243
+ Object.assign(schema, _cached);
244
+ const isParentRef = zodSchema._zod.parent === ref;
245
+ // For parent chain, child is a refinement - remove parent-only properties
246
+ if (isParentRef) {
247
+ for (const key in schema) {
248
+ if (key === "$ref" || key === "allOf")
249
+ continue;
250
+ if (!(key in _cached)) {
251
+ delete schema[key];
252
+ }
253
+ }
254
+ }
255
+ // When ref was extracted to $defs, remove properties that match the definition
256
+ if (refSchema.$ref && refSeen.def) {
257
+ for (const key in schema) {
258
+ if (key === "$ref" || key === "allOf")
259
+ continue;
260
+ if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) {
261
+ delete schema[key];
262
+ }
263
+ }
264
+ }
265
+ }
266
+ // If parent was extracted (has $ref), propagate $ref to this schema
267
+ // This handles cases like: readonly().meta({id}).describe()
268
+ // where processor sets ref to innerType but parent should be referenced
269
+ const parent = zodSchema._zod.parent;
270
+ if (parent && parent !== ref) {
271
+ // Ensure parent is processed first so its def has inherited properties
272
+ flattenRef(parent);
273
+ const parentSeen = ctx.seen.get(parent);
274
+ if (parentSeen?.schema.$ref) {
275
+ schema.$ref = parentSeen.schema.$ref;
276
+ // De-duplicate with parent's definition
277
+ if (parentSeen.def) {
278
+ for (const key in schema) {
279
+ if (key === "$ref" || key === "allOf")
280
+ continue;
281
+ if (key in parentSeen.def && JSON.stringify(schema[key]) === JSON.stringify(parentSeen.def[key])) {
282
+ delete schema[key];
283
+ }
284
+ }
285
+ }
286
+ }
287
+ }
288
+ // execute overrides
289
+ ctx.override({
290
+ zodSchema: zodSchema,
291
+ jsonSchema: schema,
292
+ path: seen.path ?? [],
293
+ });
294
+ };
295
+ for (const entry of [...ctx.seen.entries()].reverse()) {
296
+ flattenRef(entry[0]);
297
+ }
298
+ const result = {};
299
+ if (ctx.target === "draft-2020-12") {
300
+ result.$schema = "https://json-schema.org/draft/2020-12/schema";
301
+ }
302
+ else if (ctx.target === "draft-07") {
303
+ result.$schema = "http://json-schema.org/draft-07/schema#";
304
+ }
305
+ else if (ctx.target === "draft-04") {
306
+ result.$schema = "http://json-schema.org/draft-04/schema#";
307
+ }
308
+ else if (ctx.target === "openapi-3.0") {
309
+ // OpenAPI 3.0 schema objects should not include a $schema property
310
+ }
311
+ else {
312
+ // Arbitrary string values are allowed but won't have a $schema property set
313
+ }
314
+ if (ctx.external?.uri) {
315
+ const id = ctx.external.registry.get(schema)?.id;
316
+ if (!id)
317
+ throw new Error("Schema is missing an `id` property");
318
+ result.$id = ctx.external.uri(id);
319
+ }
320
+ Object.assign(result, root.def ?? root.schema);
321
+ // The `id` in `.meta()` is a Zod-specific registration tag used to extract
322
+ // schemas into $defs — it is not user-facing JSON Schema metadata. Strip it
323
+ // from the output body where it would otherwise leak. The id is preserved
324
+ // implicitly via the $defs key (and via $ref paths).
325
+ const rootMetaId = ctx.metadataRegistry.get(schema)?.id;
326
+ if (rootMetaId !== undefined && result.id === rootMetaId)
327
+ delete result.id;
328
+ // build defs object
329
+ const defs = ctx.external?.defs ?? {};
330
+ for (const entry of ctx.seen.entries()) {
331
+ const seen = entry[1];
332
+ if (seen.def && seen.defId) {
333
+ if (seen.def.id === seen.defId)
334
+ delete seen.def.id;
335
+ defs[seen.defId] = seen.def;
336
+ }
337
+ }
338
+ // set definitions in result
339
+ if (ctx.external) {
340
+ }
341
+ else {
342
+ if (Object.keys(defs).length > 0) {
343
+ if (ctx.target === "draft-2020-12") {
344
+ result.$defs = defs;
345
+ }
346
+ else {
347
+ result.definitions = defs;
348
+ }
349
+ }
350
+ }
351
+ try {
352
+ // this "finalizes" this schema and ensures all cycles are removed
353
+ // each call to finalize() is functionally independent
354
+ // though the seen map is shared
355
+ const finalized = JSON.parse(JSON.stringify(result));
356
+ Object.defineProperty(finalized, "~standard", {
357
+ value: {
358
+ ...schema["~standard"],
359
+ jsonSchema: {
360
+ input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
361
+ output: createStandardJSONSchemaMethod(schema, "output", ctx.processors),
362
+ },
363
+ },
364
+ enumerable: false,
365
+ writable: false,
366
+ });
367
+ return finalized;
368
+ }
369
+ catch (_err) {
370
+ throw new Error("Error converting schema to JSON.");
371
+ }
372
+ }
373
+ function isTransforming(_schema, _ctx) {
374
+ const ctx = _ctx ?? { seen: new Set() };
375
+ if (ctx.seen.has(_schema))
376
+ return false;
377
+ ctx.seen.add(_schema);
378
+ const def = _schema._zod.def;
379
+ if (def.type === "transform")
380
+ return true;
381
+ if (def.type === "array")
382
+ return isTransforming(def.element, ctx);
383
+ if (def.type === "set")
384
+ return isTransforming(def.valueType, ctx);
385
+ if (def.type === "lazy")
386
+ return isTransforming(def.getter(), ctx);
387
+ if (def.type === "promise" ||
388
+ def.type === "optional" ||
389
+ def.type === "nonoptional" ||
390
+ def.type === "nullable" ||
391
+ def.type === "readonly" ||
392
+ def.type === "default" ||
393
+ def.type === "prefault") {
394
+ return isTransforming(def.innerType, ctx);
395
+ }
396
+ if (def.type === "intersection") {
397
+ return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
398
+ }
399
+ if (def.type === "record" || def.type === "map") {
400
+ return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
401
+ }
402
+ if (def.type === "pipe") {
403
+ if (_schema._zod.traits.has("$ZodCodec"))
404
+ return true;
405
+ return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
406
+ }
407
+ if (def.type === "object") {
408
+ for (const key in def.shape) {
409
+ if (isTransforming(def.shape[key], ctx))
410
+ return true;
411
+ }
412
+ return false;
413
+ }
414
+ if (def.type === "union") {
415
+ for (const option of def.options) {
416
+ if (isTransforming(option, ctx))
417
+ return true;
418
+ }
419
+ return false;
420
+ }
421
+ if (def.type === "tuple") {
422
+ for (const item of def.items) {
423
+ if (isTransforming(item, ctx))
424
+ return true;
425
+ }
426
+ if (def.rest && isTransforming(def.rest, ctx))
427
+ return true;
428
+ return false;
429
+ }
430
+ return false;
431
+ }
432
+ /**
433
+ * Creates a toJSONSchema method for a schema instance.
434
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
435
+ */
436
+ export const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
437
+ const ctx = initializeContext({ ...params, processors });
438
+ process(schema, ctx);
439
+ extractDefs(ctx, schema);
440
+ return finalize(ctx, schema);
441
+ };
442
+ export const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
443
+ const { libraryOptions, target } = params ?? {};
444
+ const ctx = initializeContext({ ...(libraryOptions ?? {}), target, io, processors });
445
+ process(schema, ctx);
446
+ extractDefs(ctx, schema);
447
+ return finalize(ctx, schema);
448
+ };