@fluidframework/tree-agent 2.73.0 → 2.74.0-368706

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 (68) hide show
  1. package/api-report/tree-agent.alpha.api.md +1 -3
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +2 -0
  4. package/dist/agent.js.map +1 -1
  5. package/dist/methodBinding.d.ts +7 -4
  6. package/dist/methodBinding.d.ts.map +1 -1
  7. package/dist/methodBinding.js +14 -3
  8. package/dist/methodBinding.js.map +1 -1
  9. package/dist/prompt.d.ts.map +1 -1
  10. package/dist/prompt.js +2 -12
  11. package/dist/prompt.js.map +1 -1
  12. package/dist/propertyBinding.d.ts.map +1 -1
  13. package/dist/propertyBinding.js +2 -2
  14. package/dist/propertyBinding.js.map +1 -1
  15. package/dist/renderSchemaTypeScript.d.ts +18 -0
  16. package/dist/renderSchemaTypeScript.d.ts.map +1 -0
  17. package/dist/renderSchemaTypeScript.js +399 -0
  18. package/dist/renderSchemaTypeScript.js.map +1 -0
  19. package/dist/renderZodTypeScript.d.ts +21 -0
  20. package/dist/renderZodTypeScript.d.ts.map +1 -0
  21. package/dist/renderZodTypeScript.js +272 -0
  22. package/dist/renderZodTypeScript.js.map +1 -0
  23. package/dist/typeGeneration.d.ts +3 -5
  24. package/dist/typeGeneration.d.ts.map +1 -1
  25. package/dist/typeGeneration.js +21 -254
  26. package/dist/typeGeneration.js.map +1 -1
  27. package/dist/utils.d.ts +10 -27
  28. package/dist/utils.d.ts.map +1 -1
  29. package/dist/utils.js +20 -364
  30. package/dist/utils.js.map +1 -1
  31. package/lib/agent.d.ts.map +1 -1
  32. package/lib/agent.js +2 -0
  33. package/lib/agent.js.map +1 -1
  34. package/lib/methodBinding.d.ts +7 -4
  35. package/lib/methodBinding.d.ts.map +1 -1
  36. package/lib/methodBinding.js +11 -1
  37. package/lib/methodBinding.js.map +1 -1
  38. package/lib/prompt.d.ts.map +1 -1
  39. package/lib/prompt.js +3 -13
  40. package/lib/prompt.js.map +1 -1
  41. package/lib/propertyBinding.d.ts.map +1 -1
  42. package/lib/propertyBinding.js +1 -1
  43. package/lib/propertyBinding.js.map +1 -1
  44. package/lib/renderSchemaTypeScript.d.ts +18 -0
  45. package/lib/renderSchemaTypeScript.d.ts.map +1 -0
  46. package/lib/renderSchemaTypeScript.js +395 -0
  47. package/lib/renderSchemaTypeScript.js.map +1 -0
  48. package/lib/renderZodTypeScript.d.ts +21 -0
  49. package/lib/renderZodTypeScript.d.ts.map +1 -0
  50. package/lib/renderZodTypeScript.js +267 -0
  51. package/lib/renderZodTypeScript.js.map +1 -0
  52. package/lib/typeGeneration.d.ts +3 -5
  53. package/lib/typeGeneration.d.ts.map +1 -1
  54. package/lib/typeGeneration.js +24 -257
  55. package/lib/typeGeneration.js.map +1 -1
  56. package/lib/utils.d.ts +10 -27
  57. package/lib/utils.d.ts.map +1 -1
  58. package/lib/utils.js +20 -362
  59. package/lib/utils.js.map +1 -1
  60. package/package.json +10 -10
  61. package/src/agent.ts +2 -0
  62. package/src/methodBinding.ts +17 -5
  63. package/src/prompt.ts +6 -22
  64. package/src/propertyBinding.ts +2 -1
  65. package/src/renderSchemaTypeScript.ts +523 -0
  66. package/src/renderZodTypeScript.ts +314 -0
  67. package/src/typeGeneration.ts +32 -414
  68. package/src/utils.ts +20 -414
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.instanceOfs = exports.instanceOf = exports.renderZodTypeScript = void 0;
8
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
9
+ const internal_1 = require("@fluidframework/telemetry-utils/internal");
10
+ const alpha_1 = require("@fluidframework/tree/alpha");
11
+ const zod_1 = require("zod");
12
+ /**
13
+ * Converts Zod schema definitions into TypeScript declaration text.
14
+ */
15
+ function renderZodTypeScript(zodType, getFriendlyName, instanceOfLookup) {
16
+ let result = "";
17
+ let startOfLine = true;
18
+ let indent = 0;
19
+ appendType(zodType);
20
+ return result;
21
+ function appendType(type, minPrecedence = 2 /* TypePrecedence.Object */) {
22
+ const shouldParenthesize = getTypePrecendece(type) < minPrecedence;
23
+ if (shouldParenthesize) {
24
+ append("(");
25
+ }
26
+ appendTypeDefinition(type);
27
+ if (shouldParenthesize) {
28
+ append(")");
29
+ }
30
+ }
31
+ function append(s) {
32
+ if (startOfLine) {
33
+ result += " ".repeat(indent);
34
+ startOfLine = false;
35
+ }
36
+ result += s;
37
+ }
38
+ function appendNewLine() {
39
+ append("\n");
40
+ startOfLine = true;
41
+ }
42
+ function appendTypeDefinition(type) {
43
+ switch (getTypeKind(type)) {
44
+ case zod_1.z.ZodFirstPartyTypeKind.ZodString: {
45
+ append("string");
46
+ return;
47
+ }
48
+ case zod_1.z.ZodFirstPartyTypeKind.ZodNumber: {
49
+ append("number");
50
+ return;
51
+ }
52
+ case zod_1.z.ZodFirstPartyTypeKind.ZodBoolean: {
53
+ append("boolean");
54
+ return;
55
+ }
56
+ case zod_1.z.ZodFirstPartyTypeKind.ZodDate: {
57
+ append("Date");
58
+ return;
59
+ }
60
+ case zod_1.z.ZodFirstPartyTypeKind.ZodUndefined: {
61
+ append("undefined");
62
+ return;
63
+ }
64
+ case zod_1.z.ZodFirstPartyTypeKind.ZodNull: {
65
+ append("null");
66
+ return;
67
+ }
68
+ case zod_1.z.ZodFirstPartyTypeKind.ZodUnknown: {
69
+ append("unknown");
70
+ return;
71
+ }
72
+ case zod_1.z.ZodFirstPartyTypeKind.ZodArray: {
73
+ appendArrayType(type);
74
+ return;
75
+ }
76
+ case zod_1.z.ZodFirstPartyTypeKind.ZodObject: {
77
+ appendObjectType(type);
78
+ return;
79
+ }
80
+ case zod_1.z.ZodFirstPartyTypeKind.ZodUnion: {
81
+ appendUnionOrIntersectionTypes(type._def.options, 0 /* TypePrecedence.Union */);
82
+ return;
83
+ }
84
+ case zod_1.z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
85
+ appendUnionOrIntersectionTypes([...type._def.options.values()], 0 /* TypePrecedence.Union */);
86
+ return;
87
+ }
88
+ case zod_1.z.ZodFirstPartyTypeKind.ZodIntersection: {
89
+ appendUnionOrIntersectionTypes([
90
+ type._def.left,
91
+ type._def.right,
92
+ ], 1 /* TypePrecedence.Intersection */);
93
+ return;
94
+ }
95
+ case zod_1.z.ZodFirstPartyTypeKind.ZodTuple: {
96
+ appendTupleType(type);
97
+ return;
98
+ }
99
+ case zod_1.z.ZodFirstPartyTypeKind.ZodRecord: {
100
+ appendRecordType(type);
101
+ return;
102
+ }
103
+ case zod_1.z.ZodFirstPartyTypeKind.ZodMap: {
104
+ appendMapType(type);
105
+ return;
106
+ }
107
+ case zod_1.z.ZodFirstPartyTypeKind.ZodLiteral: {
108
+ appendLiteral(type._def.value);
109
+ return;
110
+ }
111
+ case zod_1.z.ZodFirstPartyTypeKind.ZodEnum: {
112
+ append(type._def.values.map((value) => JSON.stringify(value)).join(" | "));
113
+ return;
114
+ }
115
+ case zod_1.z.ZodFirstPartyTypeKind.ZodOptional: {
116
+ appendUnionOrIntersectionTypes([type._def.innerType, zod_1.z.undefined()], 0 /* TypePrecedence.Union */);
117
+ return;
118
+ }
119
+ case zod_1.z.ZodFirstPartyTypeKind.ZodReadonly: {
120
+ appendReadonlyType(type);
121
+ return;
122
+ }
123
+ case zod_1.z.ZodFirstPartyTypeKind.ZodEffects: {
124
+ const schema = instanceOfLookup.get(type);
125
+ if (schema === undefined) {
126
+ throw new internal_1.UsageError(`Unsupported zod effects type when formatting helper types: ${getTypeKind(type)}`);
127
+ }
128
+ append(getFriendlyName(schema));
129
+ return;
130
+ }
131
+ case zod_1.z.ZodFirstPartyTypeKind.ZodVoid: {
132
+ append("void");
133
+ return;
134
+ }
135
+ case zod_1.z.ZodFirstPartyTypeKind.ZodLazy: {
136
+ appendType(type._def.getter());
137
+ return;
138
+ }
139
+ default: {
140
+ throw new internal_1.UsageError(`Unsupported type when formatting helper types: ${getTypeKind(type)}`);
141
+ }
142
+ }
143
+ }
144
+ function appendArrayType(arrayType) {
145
+ appendType(arrayType._def.type, 2 /* TypePrecedence.Object */);
146
+ append("[]");
147
+ }
148
+ function appendObjectType(objectType) {
149
+ append("{");
150
+ appendNewLine();
151
+ indent++;
152
+ for (const [name, entry] of Object.entries(objectType._def.shape())) {
153
+ let propertyType = entry;
154
+ append(name);
155
+ if (getTypeKind(propertyType) === zod_1.z.ZodFirstPartyTypeKind.ZodOptional) {
156
+ append("?");
157
+ propertyType = propertyType._def.innerType;
158
+ }
159
+ append(": ");
160
+ appendType(propertyType);
161
+ append(";");
162
+ appendNewLine();
163
+ }
164
+ indent--;
165
+ append("}");
166
+ }
167
+ function appendUnionOrIntersectionTypes(types, minPrecedence) {
168
+ let first = true;
169
+ for (const innerType of types) {
170
+ if (!first) {
171
+ append(minPrecedence === 1 /* TypePrecedence.Intersection */ ? " & " : " | ");
172
+ }
173
+ appendType(innerType, minPrecedence);
174
+ first = false;
175
+ }
176
+ }
177
+ function appendTupleType(tupleType) {
178
+ append("[");
179
+ let first = true;
180
+ for (const innerType of tupleType._def
181
+ .items) {
182
+ if (!first) {
183
+ append(", ");
184
+ }
185
+ if (getTypeKind(innerType) === zod_1.z.ZodFirstPartyTypeKind.ZodOptional) {
186
+ appendType(innerType._def.innerType, 2 /* TypePrecedence.Object */);
187
+ append("?");
188
+ }
189
+ else {
190
+ appendType(innerType);
191
+ }
192
+ first = false;
193
+ }
194
+ const rest = tupleType._def.rest;
195
+ if (rest !== null) {
196
+ if (!first) {
197
+ append(", ");
198
+ }
199
+ append("...");
200
+ appendType(rest, 2 /* TypePrecedence.Object */);
201
+ append("[]");
202
+ }
203
+ append("]");
204
+ }
205
+ function appendRecordType(recordType) {
206
+ append("Record<");
207
+ appendType(recordType._def.keyType);
208
+ append(", ");
209
+ appendType(recordType._def.valueType);
210
+ append(">");
211
+ }
212
+ function appendMapType(mapType) {
213
+ append("Map<");
214
+ appendType(mapType._def.keyType);
215
+ append(", ");
216
+ appendType(mapType._def.valueType);
217
+ append(">");
218
+ }
219
+ function appendLiteral(value) {
220
+ append(typeof value === "string" || typeof value === "number" || typeof value === "boolean"
221
+ ? JSON.stringify(value)
222
+ : "any");
223
+ }
224
+ function appendReadonlyType(readonlyType) {
225
+ append("Readonly<");
226
+ appendType(readonlyType._def.innerType);
227
+ append(">");
228
+ }
229
+ }
230
+ exports.renderZodTypeScript = renderZodTypeScript;
231
+ function getTypeKind(type) {
232
+ return type._def.typeName;
233
+ }
234
+ var TypePrecedence;
235
+ (function (TypePrecedence) {
236
+ TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
237
+ TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
238
+ TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
239
+ })(TypePrecedence || (TypePrecedence = {}));
240
+ function getTypePrecendece(type) {
241
+ switch (getTypeKind(type)) {
242
+ case zod_1.z.ZodFirstPartyTypeKind.ZodEnum:
243
+ case zod_1.z.ZodFirstPartyTypeKind.ZodUnion:
244
+ case zod_1.z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
245
+ return 0 /* TypePrecedence.Union */;
246
+ }
247
+ case zod_1.z.ZodFirstPartyTypeKind.ZodIntersection: {
248
+ return 1 /* TypePrecedence.Intersection */;
249
+ }
250
+ default: {
251
+ return 2 /* TypePrecedence.Object */;
252
+ }
253
+ }
254
+ }
255
+ /**
256
+ * Create a Zod schema for a SharedTree schema class.
257
+ * @alpha
258
+ */
259
+ function instanceOf(schema) {
260
+ if (!(schema instanceof alpha_1.ObjectNodeSchema)) {
261
+ throw new internal_1.UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);
262
+ }
263
+ const effect = zod_1.z.instanceof(schema);
264
+ exports.instanceOfs.set(effect, schema);
265
+ return effect;
266
+ }
267
+ exports.instanceOf = instanceOf;
268
+ /**
269
+ * A lookup from Zod instanceOf schemas to their corresponding ObjectNodeSchema.
270
+ */
271
+ exports.instanceOfs = new WeakMap();
272
+ //# sourceMappingURL=renderZodTypeScript.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderZodTypeScript.js","sourceRoot":"","sources":["../src/renderZodTypeScript.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,0DAA0D;AAC1D,uEAAsE;AAEtE,sDAA8D;AAC9D,6BAAwB;AAExB;;GAEG;AACH,SAAgB,mBAAmB,CAClC,OAAqB,EACrB,eAAmD,EACnD,gBAAyD;IAEzD,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,WAAW,GAAG,IAAI,CAAC;IACvB,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,UAAU,CAAC,OAAO,CAAC,CAAC;IACpB,OAAO,MAAM,CAAC;IAEd,SAAS,UAAU,CAAC,IAAkB,EAAE,aAAa,gCAAwB;QAC5E,MAAM,kBAAkB,GAAG,iBAAiB,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC;QACnE,IAAI,kBAAkB,EAAE,CAAC;YACxB,MAAM,CAAC,GAAG,CAAC,CAAC;QACb,CAAC;QACD,oBAAoB,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,kBAAkB,EAAE,CAAC;YACxB,MAAM,CAAC,GAAG,CAAC,CAAC;QACb,CAAC;IACF,CAAC;IAED,SAAS,MAAM,CAAC,CAAS;QACxB,IAAI,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAChC,WAAW,GAAG,KAAK,CAAC;QACrB,CAAC;QACD,MAAM,IAAI,CAAC,CAAC;IACb,CAAC;IAED,SAAS,aAAa;QACrB,MAAM,CAAC,IAAI,CAAC,CAAC;QACb,WAAW,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,SAAS,oBAAoB,CAAC,IAAkB;QAC/C,QAAQ,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,K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,CAAC,MAAM,YAAY,wBAAgB,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,qBAAU,CAAC,GAAG,MAAM,CAAC,UAAU,2CAA2C,CAAC,CAAC;IACvF,CAAC;IACD,MAAM,MAAM,GAAG,OAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACpC,mBAAW,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,MAAM,CAAC;AACf,CAAC;AATD,gCASC;AAED;;GAEG;AACU,QAAA,WAAW,GAAG,IAAI,OAAO,EAAkC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-unsafe-argument */\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree/alpha\";\nimport { ObjectNodeSchema } from \"@fluidframework/tree/alpha\";\nimport { z } from \"zod\";\n\n/**\n * Converts Zod schema definitions into TypeScript declaration text.\n */\nexport function renderZodTypeScript(\n\tzodType: z.ZodTypeAny,\n\tgetFriendlyName: (schema: TreeNodeSchema) => string,\n\tinstanceOfLookup: WeakMap<z.ZodTypeAny, ObjectNodeSchema>,\n): string {\n\tlet result = \"\";\n\tlet startOfLine = true;\n\tlet indent = 0;\n\n\tappendType(zodType);\n\treturn result;\n\n\tfunction appendType(type: z.ZodTypeAny, minPrecedence = TypePrecedence.Object): void {\n\t\tconst shouldParenthesize = getTypePrecendece(type) < minPrecedence;\n\t\tif (shouldParenthesize) {\n\t\t\tappend(\"(\");\n\t\t}\n\t\tappendTypeDefinition(type);\n\t\tif (shouldParenthesize) {\n\t\t\tappend(\")\");\n\t\t}\n\t}\n\n\tfunction append(s: string): void {\n\t\tif (startOfLine) {\n\t\t\tresult += \" \".repeat(indent);\n\t\t\tstartOfLine = false;\n\t\t}\n\t\tresult += s;\n\t}\n\n\tfunction appendNewLine(): void {\n\t\tappend(\"\\n\");\n\t\tstartOfLine = true;\n\t}\n\n\tfunction appendTypeDefinition(type: z.ZodTypeAny): void {\n\t\tswitch (getTypeKind(type)) {\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodString: {\n\t\t\t\tappend(\"string\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodNumber: {\n\t\t\t\tappend(\"number\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodBoolean: {\n\t\t\t\tappend(\"boolean\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodDate: {\n\t\t\t\tappend(\"Date\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodUndefined: {\n\t\t\t\tappend(\"undefined\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodNull: {\n\t\t\t\tappend(\"null\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodUnknown: {\n\t\t\t\tappend(\"unknown\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodArray: {\n\t\t\t\tappendArrayType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodObject: {\n\t\t\t\tappendObjectType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodUnion: {\n\t\t\t\tappendUnionOrIntersectionTypes(\n\t\t\t\t\t(type._def as z.ZodUnionDef).options,\n\t\t\t\t\tTypePrecedence.Union,\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {\n\t\t\t\tappendUnionOrIntersectionTypes(\n\t\t\t\t\t[...(type._def as z.ZodDiscriminatedUnionDef<string>).options.values()],\n\t\t\t\t\tTypePrecedence.Union,\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodIntersection: {\n\t\t\t\tappendUnionOrIntersectionTypes(\n\t\t\t\t\t[\n\t\t\t\t\t\t(type._def as z.ZodIntersectionDef).left,\n\t\t\t\t\t\t(type._def as z.ZodIntersectionDef).right,\n\t\t\t\t\t],\n\t\t\t\t\tTypePrecedence.Intersection,\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodTuple: {\n\t\t\t\tappendTupleType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodRecord: {\n\t\t\t\tappendRecordType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodMap: {\n\t\t\t\tappendMapType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodLiteral: {\n\t\t\t\tappendLiteral((type._def as z.ZodLiteralDef).value);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodEnum: {\n\t\t\t\tappend(\n\t\t\t\t\t(type._def as z.ZodEnumDef).values.map((value) => JSON.stringify(value)).join(\" | \"),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodOptional: {\n\t\t\t\tappendUnionOrIntersectionTypes(\n\t\t\t\t\t[(type._def as z.ZodOptionalDef).innerType, z.undefined()],\n\t\t\t\t\tTypePrecedence.Union,\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodReadonly: {\n\t\t\t\tappendReadonlyType(type);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodEffects: {\n\t\t\t\tconst schema = instanceOfLookup.get(type);\n\t\t\t\tif (schema === undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`Unsupported zod effects type when formatting helper types: ${getTypeKind(type)}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tappend(getFriendlyName(schema));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodVoid: {\n\t\t\t\tappend(\"void\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcase z.ZodFirstPartyTypeKind.ZodLazy: {\n\t\t\t\tappendType((type._def as z.ZodLazyDef).getter());\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new UsageError(\n\t\t\t\t\t`Unsupported type when formatting helper types: ${getTypeKind(type)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction appendArrayType(arrayType: z.ZodTypeAny): void {\n\t\tappendType((arrayType._def as z.ZodArrayDef).type, TypePrecedence.Object);\n\t\tappend(\"[]\");\n\t}\n\n\tfunction appendObjectType(objectType: z.ZodTypeAny): void {\n\t\tappend(\"{\");\n\t\tappendNewLine();\n\t\tindent++;\n\t\tfor (const [name, entry] of Object.entries((objectType._def as z.ZodObjectDef).shape())) {\n\t\t\tlet propertyType = entry;\n\t\t\tappend(name);\n\t\t\tif (getTypeKind(propertyType) === z.ZodFirstPartyTypeKind.ZodOptional) {\n\t\t\t\tappend(\"?\");\n\t\t\t\tpropertyType = (propertyType._def as z.ZodOptionalDef).innerType;\n\t\t\t}\n\t\t\tappend(\": \");\n\t\t\tappendType(propertyType);\n\t\t\tappend(\";\");\n\t\t\tappendNewLine();\n\t\t}\n\t\tindent--;\n\t\tappend(\"}\");\n\t}\n\n\tfunction appendUnionOrIntersectionTypes(\n\t\ttypes: readonly z.ZodTypeAny[],\n\t\tminPrecedence: TypePrecedence,\n\t): void {\n\t\tlet first = true;\n\t\tfor (const innerType of types) {\n\t\t\tif (!first) {\n\t\t\t\tappend(minPrecedence === TypePrecedence.Intersection ? \" & \" : \" | \");\n\t\t\t}\n\t\t\tappendType(innerType, minPrecedence);\n\t\t\tfirst = false;\n\t\t}\n\t}\n\n\tfunction appendTupleType(tupleType: z.ZodTypeAny): void {\n\t\tappend(\"[\");\n\t\tlet first = true;\n\t\tfor (const innerType of (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType>)\n\t\t\t.items) {\n\t\t\tif (!first) {\n\t\t\t\tappend(\", \");\n\t\t\t}\n\t\t\tif (getTypeKind(innerType) === z.ZodFirstPartyTypeKind.ZodOptional) {\n\t\t\t\tappendType((innerType._def as z.ZodOptionalDef).innerType, TypePrecedence.Object);\n\t\t\t\tappend(\"?\");\n\t\t\t} else {\n\t\t\t\tappendType(innerType);\n\t\t\t}\n\t\t\tfirst = false;\n\t\t}\n\t\tconst rest = (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType | null>).rest;\n\t\tif (rest !== null) {\n\t\t\tif (!first) {\n\t\t\t\tappend(\", \");\n\t\t\t}\n\t\t\tappend(\"...\");\n\t\t\tappendType(rest, TypePrecedence.Object);\n\t\t\tappend(\"[]\");\n\t\t}\n\t\tappend(\"]\");\n\t}\n\n\tfunction appendRecordType(recordType: z.ZodTypeAny): void {\n\t\tappend(\"Record<\");\n\t\tappendType((recordType._def as z.ZodRecordDef).keyType);\n\t\tappend(\", \");\n\t\tappendType((recordType._def as z.ZodRecordDef).valueType);\n\t\tappend(\">\");\n\t}\n\n\tfunction appendMapType(mapType: z.ZodTypeAny): void {\n\t\tappend(\"Map<\");\n\t\tappendType((mapType._def as z.ZodMapDef).keyType);\n\t\tappend(\", \");\n\t\tappendType((mapType._def as z.ZodMapDef).valueType);\n\t\tappend(\">\");\n\t}\n\n\tfunction appendLiteral(value: unknown): void {\n\t\tappend(\n\t\t\ttypeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\"\n\t\t\t\t? JSON.stringify(value)\n\t\t\t\t: \"any\",\n\t\t);\n\t}\n\n\tfunction appendReadonlyType(readonlyType: z.ZodTypeAny): void {\n\t\tappend(\"Readonly<\");\n\t\tappendType((readonlyType._def as z.ZodReadonlyDef).innerType);\n\t\tappend(\">\");\n\t}\n}\n\nfunction getTypeKind(type: z.ZodType): z.ZodFirstPartyTypeKind {\n\treturn (type._def as z.ZodTypeDef & { typeName: z.ZodFirstPartyTypeKind }).typeName;\n}\n\nconst enum TypePrecedence {\n\tUnion = 0,\n\tIntersection = 1,\n\tObject = 2,\n}\n\nfunction getTypePrecendece(type: z.ZodType): TypePrecedence {\n\tswitch (getTypeKind(type)) {\n\t\tcase z.ZodFirstPartyTypeKind.ZodEnum:\n\t\tcase z.ZodFirstPartyTypeKind.ZodUnion:\n\t\tcase z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {\n\t\t\treturn TypePrecedence.Union;\n\t\t}\n\t\tcase z.ZodFirstPartyTypeKind.ZodIntersection: {\n\t\t\treturn TypePrecedence.Intersection;\n\t\t}\n\t\tdefault: {\n\t\t\treturn TypePrecedence.Object;\n\t\t}\n\t}\n}\n\n/**\n * Create a Zod schema for a SharedTree schema class.\n * @alpha\n */\nexport function instanceOf<T extends TreeNodeSchemaClass>(\n\tschema: T,\n): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>> {\n\tif (!(schema instanceof ObjectNodeSchema)) {\n\t\tthrow new UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);\n\t}\n\tconst effect = z.instanceof(schema);\n\tinstanceOfs.set(effect, schema);\n\treturn effect;\n}\n\n/**\n * A lookup from Zod instanceOf schemas to their corresponding ObjectNodeSchema.\n */\nexport const instanceOfs = new WeakMap<z.ZodTypeAny, ObjectNodeSchema>();\n"]}
@@ -3,11 +3,9 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import type { ImplicitFieldSchema, SimpleTreeSchema } from "@fluidframework/tree/internal";
6
- import { type ZodTypeAny } from "zod";
6
+ import { type SchemaTypeScriptRenderResult } from "./renderSchemaTypeScript.js";
7
7
  /**
8
- * TODO
8
+ * Generates TypeScript declarations for the schemas reachable from the provided root field schema.
9
9
  */
10
- export declare function generateEditTypesForPrompt(rootSchema: ImplicitFieldSchema, schema: SimpleTreeSchema): {
11
- domainTypes: Record<string, ZodTypeAny>;
12
- };
10
+ export declare function generateEditTypesForPrompt(rootSchema: ImplicitFieldSchema, schema: SimpleTreeSchema): SchemaTypeScriptRenderResult;
13
11
  //# sourceMappingURL=typeGeneration.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,KAAK,EACX,mBAAmB,EAGnB,gBAAgB,EAEhB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAK,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AAkCzC;;GAEG;AACH,wBAAgB,0BAA0B,CACzC,UAAU,EAAE,mBAAmB,EAC/B,MAAM,EAAE,gBAAgB,GACtB;IACF,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACxC,CAqCA"}
1
+ {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAK3F,OAAO,EAEN,KAAK,4BAA4B,EACjC,MAAM,6BAA6B,CAAC;AAKrC;;GAEG;AACH,wBAAgB,0BAA0B,CACzC,UAAU,EAAE,mBAAmB,EAC/B,MAAM,EAAE,gBAAgB,GACtB,4BAA4B,CAI9B"}
@@ -5,274 +5,41 @@
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.generateEditTypesForPrompt = void 0;
8
- const internal_1 = require("@fluidframework/core-utils/internal");
9
- const internal_2 = require("@fluidframework/telemetry-utils/internal");
10
- const internal_3 = require("@fluidframework/tree/internal");
11
- const zod_1 = require("zod");
8
+ const internal_1 = require("@fluidframework/tree/internal");
12
9
  const methodBinding_js_1 = require("./methodBinding.js");
13
10
  const propertyBinding_js_1 = require("./propertyBinding.js");
11
+ const renderSchemaTypeScript_js_1 = require("./renderSchemaTypeScript.js");
14
12
  const utils_js_1 = require("./utils.js");
15
- /**
16
- *
17
- * TODO: Add a prompt suggestion API!
18
- *
19
- * TODO: Handle rate limit errors.
20
- *
21
- * TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt
22
- *
23
- * TODO make the Ids be "Vector-2" instead of "Vector2" (or else it gets weird when you have a type called "Vector2")
24
- */
25
- /**
26
- * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM
27
- */
28
13
  const promptSchemaCache = new WeakMap();
29
14
  /**
30
- * TODO
15
+ * Generates TypeScript declarations for the schemas reachable from the provided root field schema.
31
16
  */
32
17
  function generateEditTypesForPrompt(rootSchema, schema) {
33
- return (0, utils_js_1.getOrCreate)(promptSchemaCache, schema, () => {
34
- const allSchemas = new Set();
35
- const objectTypeSchemas = new Set();
36
- (0, internal_3.walkFieldSchema)(rootSchema, {
37
- node: (n) => {
38
- allSchemas.add(n);
39
- if (n instanceof internal_3.ObjectNodeSchema ||
40
- n instanceof internal_3.MapNodeSchema ||
41
- n instanceof internal_3.ArrayNodeSchema ||
42
- n instanceof internal_3.RecordNodeSchema) {
43
- objectTypeSchemas.add(n);
44
- const exposedMethods = (0, methodBinding_js_1.getExposedMethods)(n);
45
- for (const t of exposedMethods.referencedTypes) {
46
- allSchemas.add(t);
47
- objectTypeSchemas.add(t);
48
- }
49
- const exposedProperties = (0, propertyBinding_js_1.getExposedProperties)(n);
50
- for (const t of exposedProperties.referencedTypes) {
51
- allSchemas.add(t);
52
- objectTypeSchemas.add(t);
53
- }
54
- }
55
- },
56
- });
57
- const nodeSchemas = [...objectTypeSchemas.values()];
58
- const bindableSchemas = new Map(nodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]));
59
- return generateEditTypes([...allSchemas.values()].map((s) => (0, internal_3.getSimpleSchema)(s)), new Map(), bindableSchemas);
60
- });
18
+ return (0, utils_js_1.getOrCreate)(promptSchemaCache, schema, () => buildPromptSchemaDescription(rootSchema));
61
19
  }
62
20
  exports.generateEditTypesForPrompt = generateEditTypesForPrompt;
63
- /**
64
- * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}
65
- * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.
66
- *
67
- * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.
68
- *
69
- * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.
70
- */
71
- function generateEditTypes(schemas, objectCache, bindableSchemas) {
72
- const domainTypes = {};
73
- for (const schema of schemas) {
74
- for (const name of schema.definitions.keys()) {
75
- // If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate
76
- domainTypes[name] = getOrCreateType(schema.definitions, name, objectCache, bindableSchemas);
77
- }
78
- }
79
- return {
80
- domainTypes,
81
- };
82
- }
83
- function getBoundMembersForBindable(bindableSchema) {
84
- const memberTypes = [];
85
- const methods = (0, methodBinding_js_1.getExposedMethods)(bindableSchema);
86
- for (const [name, method] of Object.entries(methods.methods)) {
87
- const zodFunction = zod_1.z.instanceof(methodBinding_js_1.FunctionWrapper);
88
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
89
- zodFunction.method = method;
90
- memberTypes.push([name, zodFunction]);
91
- }
92
- const props = (0, propertyBinding_js_1.getExposedProperties)(bindableSchema);
93
- for (const [name, prop] of Object.entries(props.properties)) {
94
- const schema = prop.schema;
95
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
96
- schema.property = prop;
97
- memberTypes.push([name, prop.schema]);
98
- }
99
- return {
100
- members: memberTypes,
101
- referencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),
102
- };
103
- }
104
- function getBoundMembers(definition, bindableSchemas) {
105
- const bindableSchema = bindableSchemas.get(definition) ?? (0, utils_js_1.fail)("unknown definition");
106
- return getBoundMembersForBindable(bindableSchema).members;
107
- }
108
- function addBindingIntersectionIfNeeded(typeString, zodTypeBound, definition, simpleNodeSchema, bindableSchemas) {
109
- let zodType = zodTypeBound;
110
- let description = simpleNodeSchema.metadata?.description ?? "";
111
- const boundMembers = getBoundMembers(definition, bindableSchemas);
112
- const methods = {};
113
- const properties = {};
114
- let hasProperties = false;
115
- let hasMethods = false;
116
- if (boundMembers.length > 0) {
117
- for (const [name, zodMember] of boundMembers) {
118
- const kind =
119
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
120
- zodMember.method === undefined
121
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
122
- zodMember.property === undefined
123
- ? "Unknown"
124
- : "Property"
125
- : "Method";
126
- if (kind === "Unknown") {
127
- throw new internal_2.UsageError(`Unrecognized bound member ${name} in schema ${definition}`);
128
- }
129
- if (kind === "Method") {
130
- if (methods[name] !== undefined) {
131
- throw new internal_2.UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
132
- }
133
- methods[name] = zodMember;
134
- }
135
- if (kind === "Property") {
136
- if (properties[name] !== undefined) {
137
- throw new internal_2.UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
138
- }
139
- properties[name] = zodMember;
140
- }
141
- }
142
- hasMethods = Object.keys(methods).length > 0 ? true : false;
143
- hasProperties = Object.keys(properties).length > 0 ? true : false;
144
- if (hasMethods) {
145
- zodType = zod_1.z.intersection(zodType, zod_1.z.object(methods));
146
- }
147
- if (hasProperties) {
148
- zodType = zod_1.z.intersection(zodType, zod_1.z.object(properties));
149
- }
150
- }
151
- let note = "";
152
- if (hasMethods && hasProperties) {
153
- note = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;
154
- }
155
- else if (hasMethods) {
156
- note = `Note: this ${typeString} has custom user-defined methods directly on it.`;
157
- }
158
- else if (hasProperties) {
159
- note = `Note: this ${typeString} has custom user-defined properties directly on it.`;
160
- }
161
- if (note !== "") {
162
- description = description === "" ? note : `${description} - ${note}`;
163
- }
164
- return zodType.describe(description);
165
- }
166
- function getOrCreateType(definitionMap, definition, objectCache, bindableSchemas) {
167
- const simpleNodeSchema = definitionMap.get(definition) ?? (0, utils_js_1.fail)("Unexpected definition");
168
- return (0, utils_js_1.getOrCreate)(objectCache, simpleNodeSchema, () => {
169
- // Handle recursive types: temporarily create a zod "lazy" type that can be referenced by a recursive call to getOrCreateType.
170
- let type;
171
- objectCache.set(simpleNodeSchema, zod_1.z.lazy(() => type ?? (0, utils_js_1.fail)("Recursive type used before creation")));
172
- switch (simpleNodeSchema.kind) {
173
- case internal_3.NodeKind.Object: {
174
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
175
- const properties = Object.fromEntries([...simpleNodeSchema.fields]
176
- .map(([key, field]) => {
177
- return [
178
- key,
179
- getOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),
180
- ];
181
- })
182
- .filter(([, value]) => value !== undefined));
183
- for (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {
184
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
185
- const kind = zodMember.method === undefined ? "Property" : "Method";
186
- if (properties[name] !== undefined) {
187
- throw new internal_2.UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
21
+ function buildPromptSchemaDescription(rootSchema) {
22
+ const bindableSchemas = new Map();
23
+ (0, internal_1.walkFieldSchema)(rootSchema, {
24
+ node: (node) => {
25
+ if ((0, methodBinding_js_1.isBindableSchema)(node)) {
26
+ bindableSchemas.set(node.identifier, node);
27
+ const exposedMethods = (0, methodBinding_js_1.getExposedMethods)(node);
28
+ for (const referenced of exposedMethods.referencedTypes) {
29
+ if ((0, methodBinding_js_1.isBindableSchema)(referenced)) {
30
+ bindableSchemas.set(referenced.identifier, referenced);
188
31
  }
189
- properties[name] = zodMember;
190
32
  }
191
- return (type = zod_1.z
192
- .object(properties)
193
- .describe(simpleNodeSchema.metadata?.description ?? ""));
194
- }
195
- case internal_3.NodeKind.Map: {
196
- const zodType = zod_1.z.map(zod_1.z.string(), getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
197
- return (type = addBindingIntersectionIfNeeded("map", zodType, definition, simpleNodeSchema, bindableSchemas));
198
- }
199
- case internal_3.NodeKind.Record: {
200
- const zodType = zod_1.z.record(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
201
- return (type = addBindingIntersectionIfNeeded("record", zodType, definition, simpleNodeSchema, bindableSchemas));
202
- }
203
- case internal_3.NodeKind.Array: {
204
- const zodType = zod_1.z.array(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
205
- return (type = addBindingIntersectionIfNeeded("array", zodType, definition, simpleNodeSchema, bindableSchemas));
206
- }
207
- case internal_3.NodeKind.Leaf: {
208
- switch (simpleNodeSchema.leafKind) {
209
- case internal_3.ValueSchema.Boolean: {
210
- return (type = zod_1.z.boolean());
211
- }
212
- case internal_3.ValueSchema.Number: {
213
- return (type = zod_1.z.number());
214
- }
215
- case internal_3.ValueSchema.String: {
216
- return (type = zod_1.z.string());
217
- }
218
- case internal_3.ValueSchema.Null: {
219
- return (type = zod_1.z.null());
220
- }
221
- default: {
222
- throw new Error(`Unsupported leaf kind ${internal_3.NodeKind[simpleNodeSchema.leafKind]}.`);
33
+ const exposedProperties = (0, propertyBinding_js_1.getExposedProperties)(node);
34
+ for (const referenced of exposedProperties.referencedTypes) {
35
+ if ((0, methodBinding_js_1.isBindableSchema)(referenced)) {
36
+ bindableSchemas.set(referenced.identifier, referenced);
223
37
  }
224
38
  }
225
39
  }
226
- default: {
227
- return (0, internal_1.unreachableCase)(simpleNodeSchema, "Unknown node kind");
228
- }
229
- }
40
+ },
230
41
  });
231
- }
232
- function getOrCreateTypeForField(definitionMap, fieldSchema, objectCache, bindableSchemas) {
233
- const customMetadata = fieldSchema.metadata.custom;
234
- const getDefault = customMetadata?.[utils_js_1.llmDefault];
235
- if (getDefault !== undefined) {
236
- if (typeof getDefault !== "function") {
237
- throw new internal_2.UsageError(`Expected value of ${utils_js_1.llmDefault.description} property to be a function, but got ${typeof getDefault}`);
238
- }
239
- if (fieldSchema.kind !== internal_3.FieldKind.Optional) {
240
- throw new internal_2.UsageError(`The ${utils_js_1.llmDefault.description} property is only permitted on optional fields.`);
241
- }
242
- }
243
- const field = getTypeForAllowedTypes(definitionMap, new Set(fieldSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas).describe(getDefault === undefined
244
- ? (fieldSchema.metadata?.description ?? "")
245
- : "Do not populate this field. It will be automatically supplied by the system after insertion.");
246
- switch (fieldSchema.kind) {
247
- case internal_3.FieldKind.Required: {
248
- return field;
249
- }
250
- case internal_3.FieldKind.Optional: {
251
- return field.optional();
252
- }
253
- case internal_3.FieldKind.Identifier: {
254
- return field
255
- .optional()
256
- .describe("This is an ID automatically generated by the system. Do not supply it when constructing a new object.");
257
- }
258
- default: {
259
- throw new Error(`Unsupported field kind ${internal_3.NodeKind[fieldSchema.kind]}.`);
260
- }
261
- }
262
- }
263
- function getTypeForAllowedTypes(definitionMap, allowedTypes, objectCache, bindableSchemas) {
264
- const single = (0, utils_js_1.tryGetSingleton)(allowedTypes);
265
- if (single === undefined) {
266
- const types = [
267
- ...(0, utils_js_1.mapIterable)(allowedTypes, (name) => {
268
- return getOrCreateType(definitionMap, name, objectCache, bindableSchemas);
269
- }),
270
- ];
271
- (0, internal_1.assert)((0, utils_js_1.hasAtLeastTwo)(types), 0xa7e /* Expected at least two types */);
272
- return zod_1.z.union(types);
273
- }
274
- else {
275
- return getOrCreateType(definitionMap, single, objectCache, bindableSchemas);
276
- }
42
+ const simpleTreeSchema = (0, internal_1.getSimpleSchema)(rootSchema);
43
+ return (0, renderSchemaTypeScript_js_1.renderSchemaTypeScript)(simpleTreeSchema.definitions, bindableSchemas);
277
44
  }
278
45
  //# sourceMappingURL=typeGeneration.js.map