flavor-code 1.4.0-beta.1 → 1.4.0-beta.4

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 (159) hide show
  1. package/dist/agent/loop.d.ts +7 -2
  2. package/dist/{app-C5ZQUOKJ.js → app-UWIEIWBQ.js} +7 -7
  3. package/dist/{chunk-FOTMP4ZS.js → chunk-4RR5VVX4.js} +8 -2
  4. package/dist/{chunk-6MNOZ2TX.js → chunk-DCNUBQCH.js} +1376 -641
  5. package/dist/{chunk-ODRWR4KK.js → chunk-IQAZ42TM.js} +1 -1
  6. package/dist/{chunk-URUVFECH.js → chunk-KQMKY7C5.js} +8 -4
  7. package/dist/chunk-LKQTBL3R.js +107 -0
  8. package/dist/{chunk-3WCDZ6ZL.js → chunk-ZJCAQXA7.js} +2 -1
  9. package/dist/{claude-ink-LP32NSX5.js → claude-ink-ECZ6QF3P.js} +2 -2
  10. package/dist/cli-main.js +22 -15
  11. package/dist/cli.js +24 -5
  12. package/dist/config/schema.d.ts +4 -0
  13. package/dist/context/manager.d.ts +9 -0
  14. package/dist/desktop/main.js +1770 -949
  15. package/dist/desktop-renderer/assets/{index-CqRIr-hz.js → index-C0t1nGih.js} +2 -2
  16. package/dist/desktop-renderer/assets/{interactive-terminal-C4o7MNPO.js → interactive-terminal-BJqe96mZ.js} +1 -1
  17. package/dist/desktop-renderer/index.html +1 -1
  18. package/dist/git/service.d.ts +3 -3
  19. package/dist/git/tools.d.ts +1 -1
  20. package/dist/goal/classifier.d.ts +2 -0
  21. package/dist/goal/orchestrator.d.ts +18 -0
  22. package/dist/goal/planner.d.ts +2 -0
  23. package/dist/goal/store.d.ts +2 -0
  24. package/dist/goal/types.d.ts +8 -0
  25. package/dist/harness/journal.d.ts +2 -0
  26. package/dist/harness/local.d.ts +2 -0
  27. package/dist/init/project.d.ts +0 -6
  28. package/dist/jobs/registry.d.ts +7 -1
  29. package/dist/{load-V2UIYYQI.js → load-F4AYLCF6.js} +1 -1
  30. package/dist/loop/orchestrator.d.ts +18 -0
  31. package/dist/loop/types.d.ts +7 -0
  32. package/dist/production.d.ts +17 -0
  33. package/dist/sdk/index.js +5 -5
  34. package/dist/session/tree.d.ts +2 -0
  35. package/dist/tools/lsp.d.ts +13 -10
  36. package/dist/tools/runtime.d.ts +2 -1
  37. package/dist/tools/search.d.ts +1 -0
  38. package/dist/tools/types.d.ts +1 -1
  39. package/dist/ui/session.d.ts +9 -0
  40. package/dist/utils/abort.d.ts +4 -0
  41. package/dist/utils/execFileNoThrow.d.ts +1 -0
  42. package/dist/utils/heap.d.ts +58 -0
  43. package/dist/utils/memory-restart.d.ts +38 -3
  44. package/dist/utils/process-tree.d.ts +2 -0
  45. package/dist/utils/rotation-census.d.ts +36 -0
  46. package/package.json +2 -2
  47. package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +7 -7
  48. package/dist/astgraph/db.mjs +0 -237
  49. package/dist/astgraph/extract.mjs +0 -239
  50. package/dist/astgraph/flavor-plugin.json +0 -25
  51. package/dist/astgraph/grammars.mjs +0 -68
  52. package/dist/astgraph/index.js +0 -301
  53. package/dist/astgraph/indexer.mjs +0 -175
  54. package/dist/astgraph/query.mjs +0 -165
  55. package/dist/astgraph/resolve.mjs +0 -93
  56. package/dist/astgraph/vendor/tree-sitter-javascript.wasm +0 -0
  57. package/dist/astgraph/vendor/tree-sitter-tsx.wasm +0 -0
  58. package/dist/astgraph/vendor/tree-sitter-typescript.wasm +0 -0
  59. package/dist/astgraph/vendor/tree-sitter.js +0 -3980
  60. package/dist/astgraph/vendor/tree-sitter.wasm +0 -0
  61. package/dist/astgraph/vendor/zod/LICENSE +0 -21
  62. package/dist/astgraph/vendor/zod/index.js +0 -4
  63. package/dist/astgraph/vendor/zod/locales/index.js +0 -1
  64. package/dist/astgraph/vendor/zod/locales/package.json +0 -7
  65. package/dist/astgraph/vendor/zod/package.json +0 -135
  66. package/dist/astgraph/vendor/zod/v4/classic/checks.js +0 -1
  67. package/dist/astgraph/vendor/zod/v4/classic/coerce.js +0 -17
  68. package/dist/astgraph/vendor/zod/v4/classic/compat.js +0 -31
  69. package/dist/astgraph/vendor/zod/v4/classic/errors.js +0 -48
  70. package/dist/astgraph/vendor/zod/v4/classic/external.js +0 -20
  71. package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +0 -599
  72. package/dist/astgraph/vendor/zod/v4/classic/index.js +0 -4
  73. package/dist/astgraph/vendor/zod/v4/classic/iso.js +0 -30
  74. package/dist/astgraph/vendor/zod/v4/classic/package.json +0 -7
  75. package/dist/astgraph/vendor/zod/v4/classic/parse.js +0 -15
  76. package/dist/astgraph/vendor/zod/v4/classic/schemas.js +0 -1395
  77. package/dist/astgraph/vendor/zod/v4/core/api.js +0 -1087
  78. package/dist/astgraph/vendor/zod/v4/core/checks.js +0 -575
  79. package/dist/astgraph/vendor/zod/v4/core/core.js +0 -78
  80. package/dist/astgraph/vendor/zod/v4/core/doc.js +0 -35
  81. package/dist/astgraph/vendor/zod/v4/core/errors.js +0 -185
  82. package/dist/astgraph/vendor/zod/v4/core/index.js +0 -16
  83. package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +0 -95
  84. package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +0 -601
  85. package/dist/astgraph/vendor/zod/v4/core/json-schema.js +0 -1
  86. package/dist/astgraph/vendor/zod/v4/core/package.json +0 -7
  87. package/dist/astgraph/vendor/zod/v4/core/parse.js +0 -93
  88. package/dist/astgraph/vendor/zod/v4/core/regexes.js +0 -139
  89. package/dist/astgraph/vendor/zod/v4/core/registries.js +0 -51
  90. package/dist/astgraph/vendor/zod/v4/core/schemas.js +0 -2239
  91. package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +0 -1
  92. package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +0 -448
  93. package/dist/astgraph/vendor/zod/v4/core/util.js +0 -674
  94. package/dist/astgraph/vendor/zod/v4/core/versions.js +0 -5
  95. package/dist/astgraph/vendor/zod/v4/index.js +0 -3
  96. package/dist/astgraph/vendor/zod/v4/locales/ar.js +0 -106
  97. package/dist/astgraph/vendor/zod/v4/locales/az.js +0 -105
  98. package/dist/astgraph/vendor/zod/v4/locales/be.js +0 -156
  99. package/dist/astgraph/vendor/zod/v4/locales/bg.js +0 -120
  100. package/dist/astgraph/vendor/zod/v4/locales/ca.js +0 -107
  101. package/dist/astgraph/vendor/zod/v4/locales/cs.js +0 -111
  102. package/dist/astgraph/vendor/zod/v4/locales/da.js +0 -115
  103. package/dist/astgraph/vendor/zod/v4/locales/de.js +0 -108
  104. package/dist/astgraph/vendor/zod/v4/locales/el.js +0 -109
  105. package/dist/astgraph/vendor/zod/v4/locales/en.js +0 -113
  106. package/dist/astgraph/vendor/zod/v4/locales/eo.js +0 -109
  107. package/dist/astgraph/vendor/zod/v4/locales/es.js +0 -132
  108. package/dist/astgraph/vendor/zod/v4/locales/fa.js +0 -114
  109. package/dist/astgraph/vendor/zod/v4/locales/fi.js +0 -112
  110. package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +0 -107
  111. package/dist/astgraph/vendor/zod/v4/locales/fr.js +0 -125
  112. package/dist/astgraph/vendor/zod/v4/locales/he.js +0 -214
  113. package/dist/astgraph/vendor/zod/v4/locales/hr.js +0 -122
  114. package/dist/astgraph/vendor/zod/v4/locales/hu.js +0 -108
  115. package/dist/astgraph/vendor/zod/v4/locales/hy.js +0 -147
  116. package/dist/astgraph/vendor/zod/v4/locales/id.js +0 -106
  117. package/dist/astgraph/vendor/zod/v4/locales/index.js +0 -52
  118. package/dist/astgraph/vendor/zod/v4/locales/is.js +0 -109
  119. package/dist/astgraph/vendor/zod/v4/locales/it.js +0 -108
  120. package/dist/astgraph/vendor/zod/v4/locales/ja.js +0 -107
  121. package/dist/astgraph/vendor/zod/v4/locales/ka.js +0 -112
  122. package/dist/astgraph/vendor/zod/v4/locales/kh.js +0 -5
  123. package/dist/astgraph/vendor/zod/v4/locales/km.js +0 -110
  124. package/dist/astgraph/vendor/zod/v4/locales/ko.js +0 -111
  125. package/dist/astgraph/vendor/zod/v4/locales/lt.js +0 -203
  126. package/dist/astgraph/vendor/zod/v4/locales/mk.js +0 -109
  127. package/dist/astgraph/vendor/zod/v4/locales/ms.js +0 -107
  128. package/dist/astgraph/vendor/zod/v4/locales/nl.js +0 -110
  129. package/dist/astgraph/vendor/zod/v4/locales/no.js +0 -108
  130. package/dist/astgraph/vendor/zod/v4/locales/ota.js +0 -109
  131. package/dist/astgraph/vendor/zod/v4/locales/package.json +0 -7
  132. package/dist/astgraph/vendor/zod/v4/locales/pl.js +0 -109
  133. package/dist/astgraph/vendor/zod/v4/locales/ps.js +0 -114
  134. package/dist/astgraph/vendor/zod/v4/locales/pt.js +0 -108
  135. package/dist/astgraph/vendor/zod/v4/locales/ro.js +0 -119
  136. package/dist/astgraph/vendor/zod/v4/locales/ru.js +0 -156
  137. package/dist/astgraph/vendor/zod/v4/locales/sl.js +0 -109
  138. package/dist/astgraph/vendor/zod/v4/locales/sv.js +0 -110
  139. package/dist/astgraph/vendor/zod/v4/locales/ta.js +0 -110
  140. package/dist/astgraph/vendor/zod/v4/locales/th.js +0 -110
  141. package/dist/astgraph/vendor/zod/v4/locales/tr.js +0 -105
  142. package/dist/astgraph/vendor/zod/v4/locales/ua.js +0 -5
  143. package/dist/astgraph/vendor/zod/v4/locales/uk.js +0 -108
  144. package/dist/astgraph/vendor/zod/v4/locales/ur.js +0 -110
  145. package/dist/astgraph/vendor/zod/v4/locales/uz.js +0 -110
  146. package/dist/astgraph/vendor/zod/v4/locales/vi.js +0 -108
  147. package/dist/astgraph/vendor/zod/v4/locales/yo.js +0 -107
  148. package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +0 -109
  149. package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +0 -107
  150. package/dist/astgraph/vendor/zod/v4/mini/checks.js +0 -1
  151. package/dist/astgraph/vendor/zod/v4/mini/coerce.js +0 -22
  152. package/dist/astgraph/vendor/zod/v4/mini/external.js +0 -14
  153. package/dist/astgraph/vendor/zod/v4/mini/index.js +0 -3
  154. package/dist/astgraph/vendor/zod/v4/mini/iso.js +0 -34
  155. package/dist/astgraph/vendor/zod/v4/mini/package.json +0 -7
  156. package/dist/astgraph/vendor/zod/v4/mini/parse.js +0 -1
  157. package/dist/astgraph/vendor/zod/v4/mini/schemas.js +0 -961
  158. package/dist/astgraph/vendor/zod/v4/package.json +0 -7
  159. package/dist/chunk-CIVPKHF4.js +0 -53
@@ -1 +0,0 @@
1
- export {};
@@ -1,448 +0,0 @@
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
- };