@fluidframework/tree-agent 2.74.0-365691 → 2.74.0-370705
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/methodBinding.d.ts +6 -1
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js +14 -3
- package/dist/methodBinding.js.map +1 -1
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +2 -12
- package/dist/prompt.js.map +1 -1
- package/dist/propertyBinding.js +2 -2
- package/dist/propertyBinding.js.map +1 -1
- package/dist/renderSchemaTypeScript.d.ts +18 -0
- package/dist/renderSchemaTypeScript.d.ts.map +1 -0
- package/dist/renderSchemaTypeScript.js +399 -0
- package/dist/renderSchemaTypeScript.js.map +1 -0
- package/dist/renderZodTypeScript.d.ts +21 -0
- package/dist/renderZodTypeScript.d.ts.map +1 -0
- package/dist/renderZodTypeScript.js +272 -0
- package/dist/renderZodTypeScript.js.map +1 -0
- package/dist/typeGeneration.d.ts +3 -5
- package/dist/typeGeneration.d.ts.map +1 -1
- package/dist/typeGeneration.js +21 -254
- package/dist/typeGeneration.js.map +1 -1
- package/dist/utils.d.ts +10 -27
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +18 -362
- package/dist/utils.js.map +1 -1
- package/lib/methodBinding.d.ts +6 -1
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js +11 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +3 -13
- package/lib/prompt.js.map +1 -1
- package/lib/propertyBinding.js +1 -1
- package/lib/propertyBinding.js.map +1 -1
- package/lib/renderSchemaTypeScript.d.ts +18 -0
- package/lib/renderSchemaTypeScript.d.ts.map +1 -0
- package/lib/renderSchemaTypeScript.js +395 -0
- package/lib/renderSchemaTypeScript.js.map +1 -0
- package/lib/renderZodTypeScript.d.ts +21 -0
- package/lib/renderZodTypeScript.d.ts.map +1 -0
- package/lib/renderZodTypeScript.js +267 -0
- package/lib/renderZodTypeScript.js.map +1 -0
- package/lib/typeGeneration.d.ts +3 -5
- package/lib/typeGeneration.d.ts.map +1 -1
- package/lib/typeGeneration.js +24 -257
- package/lib/typeGeneration.js.map +1 -1
- package/lib/utils.d.ts +10 -27
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +18 -360
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/methodBinding.ts +15 -3
- package/src/prompt.ts +6 -22
- package/src/propertyBinding.ts +1 -1
- package/src/renderSchemaTypeScript.ts +523 -0
- package/src/renderZodTypeScript.ts +314 -0
- package/src/typeGeneration.ts +32 -414
- package/src/utils.ts +18 -412
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
6
|
+
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
7
|
+
import { ObjectNodeSchema } from "@fluidframework/tree/alpha";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
/**
|
|
10
|
+
* Converts Zod schema definitions into TypeScript declaration text.
|
|
11
|
+
*/
|
|
12
|
+
export function renderZodTypeScript(zodType, getFriendlyName, instanceOfLookup) {
|
|
13
|
+
let result = "";
|
|
14
|
+
let startOfLine = true;
|
|
15
|
+
let indent = 0;
|
|
16
|
+
appendType(zodType);
|
|
17
|
+
return result;
|
|
18
|
+
function appendType(type, minPrecedence = 2 /* TypePrecedence.Object */) {
|
|
19
|
+
const shouldParenthesize = getTypePrecendece(type) < minPrecedence;
|
|
20
|
+
if (shouldParenthesize) {
|
|
21
|
+
append("(");
|
|
22
|
+
}
|
|
23
|
+
appendTypeDefinition(type);
|
|
24
|
+
if (shouldParenthesize) {
|
|
25
|
+
append(")");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function append(s) {
|
|
29
|
+
if (startOfLine) {
|
|
30
|
+
result += " ".repeat(indent);
|
|
31
|
+
startOfLine = false;
|
|
32
|
+
}
|
|
33
|
+
result += s;
|
|
34
|
+
}
|
|
35
|
+
function appendNewLine() {
|
|
36
|
+
append("\n");
|
|
37
|
+
startOfLine = true;
|
|
38
|
+
}
|
|
39
|
+
function appendTypeDefinition(type) {
|
|
40
|
+
switch (getTypeKind(type)) {
|
|
41
|
+
case z.ZodFirstPartyTypeKind.ZodString: {
|
|
42
|
+
append("string");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
case z.ZodFirstPartyTypeKind.ZodNumber: {
|
|
46
|
+
append("number");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
case z.ZodFirstPartyTypeKind.ZodBoolean: {
|
|
50
|
+
append("boolean");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
case z.ZodFirstPartyTypeKind.ZodDate: {
|
|
54
|
+
append("Date");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
case z.ZodFirstPartyTypeKind.ZodUndefined: {
|
|
58
|
+
append("undefined");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
case z.ZodFirstPartyTypeKind.ZodNull: {
|
|
62
|
+
append("null");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
case z.ZodFirstPartyTypeKind.ZodUnknown: {
|
|
66
|
+
append("unknown");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
case z.ZodFirstPartyTypeKind.ZodArray: {
|
|
70
|
+
appendArrayType(type);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
case z.ZodFirstPartyTypeKind.ZodObject: {
|
|
74
|
+
appendObjectType(type);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
case z.ZodFirstPartyTypeKind.ZodUnion: {
|
|
78
|
+
appendUnionOrIntersectionTypes(type._def.options, 0 /* TypePrecedence.Union */);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
82
|
+
appendUnionOrIntersectionTypes([...type._def.options.values()], 0 /* TypePrecedence.Union */);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
86
|
+
appendUnionOrIntersectionTypes([
|
|
87
|
+
type._def.left,
|
|
88
|
+
type._def.right,
|
|
89
|
+
], 1 /* TypePrecedence.Intersection */);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
case z.ZodFirstPartyTypeKind.ZodTuple: {
|
|
93
|
+
appendTupleType(type);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
case z.ZodFirstPartyTypeKind.ZodRecord: {
|
|
97
|
+
appendRecordType(type);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
case z.ZodFirstPartyTypeKind.ZodMap: {
|
|
101
|
+
appendMapType(type);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
case z.ZodFirstPartyTypeKind.ZodLiteral: {
|
|
105
|
+
appendLiteral(type._def.value);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
case z.ZodFirstPartyTypeKind.ZodEnum: {
|
|
109
|
+
append(type._def.values.map((value) => JSON.stringify(value)).join(" | "));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
case z.ZodFirstPartyTypeKind.ZodOptional: {
|
|
113
|
+
appendUnionOrIntersectionTypes([type._def.innerType, z.undefined()], 0 /* TypePrecedence.Union */);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
case z.ZodFirstPartyTypeKind.ZodReadonly: {
|
|
117
|
+
appendReadonlyType(type);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
case z.ZodFirstPartyTypeKind.ZodEffects: {
|
|
121
|
+
const schema = instanceOfLookup.get(type);
|
|
122
|
+
if (schema === undefined) {
|
|
123
|
+
throw new UsageError(`Unsupported zod effects type when formatting helper types: ${getTypeKind(type)}`);
|
|
124
|
+
}
|
|
125
|
+
append(getFriendlyName(schema));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
case z.ZodFirstPartyTypeKind.ZodVoid: {
|
|
129
|
+
append("void");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
case z.ZodFirstPartyTypeKind.ZodLazy: {
|
|
133
|
+
appendType(type._def.getter());
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
default: {
|
|
137
|
+
throw new UsageError(`Unsupported type when formatting helper types: ${getTypeKind(type)}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function appendArrayType(arrayType) {
|
|
142
|
+
appendType(arrayType._def.type, 2 /* TypePrecedence.Object */);
|
|
143
|
+
append("[]");
|
|
144
|
+
}
|
|
145
|
+
function appendObjectType(objectType) {
|
|
146
|
+
append("{");
|
|
147
|
+
appendNewLine();
|
|
148
|
+
indent++;
|
|
149
|
+
for (const [name, entry] of Object.entries(objectType._def.shape())) {
|
|
150
|
+
let propertyType = entry;
|
|
151
|
+
append(name);
|
|
152
|
+
if (getTypeKind(propertyType) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
153
|
+
append("?");
|
|
154
|
+
propertyType = propertyType._def.innerType;
|
|
155
|
+
}
|
|
156
|
+
append(": ");
|
|
157
|
+
appendType(propertyType);
|
|
158
|
+
append(";");
|
|
159
|
+
appendNewLine();
|
|
160
|
+
}
|
|
161
|
+
indent--;
|
|
162
|
+
append("}");
|
|
163
|
+
}
|
|
164
|
+
function appendUnionOrIntersectionTypes(types, minPrecedence) {
|
|
165
|
+
let first = true;
|
|
166
|
+
for (const innerType of types) {
|
|
167
|
+
if (!first) {
|
|
168
|
+
append(minPrecedence === 1 /* TypePrecedence.Intersection */ ? " & " : " | ");
|
|
169
|
+
}
|
|
170
|
+
appendType(innerType, minPrecedence);
|
|
171
|
+
first = false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function appendTupleType(tupleType) {
|
|
175
|
+
append("[");
|
|
176
|
+
let first = true;
|
|
177
|
+
for (const innerType of tupleType._def
|
|
178
|
+
.items) {
|
|
179
|
+
if (!first) {
|
|
180
|
+
append(", ");
|
|
181
|
+
}
|
|
182
|
+
if (getTypeKind(innerType) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
183
|
+
appendType(innerType._def.innerType, 2 /* TypePrecedence.Object */);
|
|
184
|
+
append("?");
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
appendType(innerType);
|
|
188
|
+
}
|
|
189
|
+
first = false;
|
|
190
|
+
}
|
|
191
|
+
const rest = tupleType._def.rest;
|
|
192
|
+
if (rest !== null) {
|
|
193
|
+
if (!first) {
|
|
194
|
+
append(", ");
|
|
195
|
+
}
|
|
196
|
+
append("...");
|
|
197
|
+
appendType(rest, 2 /* TypePrecedence.Object */);
|
|
198
|
+
append("[]");
|
|
199
|
+
}
|
|
200
|
+
append("]");
|
|
201
|
+
}
|
|
202
|
+
function appendRecordType(recordType) {
|
|
203
|
+
append("Record<");
|
|
204
|
+
appendType(recordType._def.keyType);
|
|
205
|
+
append(", ");
|
|
206
|
+
appendType(recordType._def.valueType);
|
|
207
|
+
append(">");
|
|
208
|
+
}
|
|
209
|
+
function appendMapType(mapType) {
|
|
210
|
+
append("Map<");
|
|
211
|
+
appendType(mapType._def.keyType);
|
|
212
|
+
append(", ");
|
|
213
|
+
appendType(mapType._def.valueType);
|
|
214
|
+
append(">");
|
|
215
|
+
}
|
|
216
|
+
function appendLiteral(value) {
|
|
217
|
+
append(typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
|
218
|
+
? JSON.stringify(value)
|
|
219
|
+
: "any");
|
|
220
|
+
}
|
|
221
|
+
function appendReadonlyType(readonlyType) {
|
|
222
|
+
append("Readonly<");
|
|
223
|
+
appendType(readonlyType._def.innerType);
|
|
224
|
+
append(">");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function getTypeKind(type) {
|
|
228
|
+
return type._def.typeName;
|
|
229
|
+
}
|
|
230
|
+
var TypePrecedence;
|
|
231
|
+
(function (TypePrecedence) {
|
|
232
|
+
TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
|
|
233
|
+
TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
|
|
234
|
+
TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
|
|
235
|
+
})(TypePrecedence || (TypePrecedence = {}));
|
|
236
|
+
function getTypePrecendece(type) {
|
|
237
|
+
switch (getTypeKind(type)) {
|
|
238
|
+
case z.ZodFirstPartyTypeKind.ZodEnum:
|
|
239
|
+
case z.ZodFirstPartyTypeKind.ZodUnion:
|
|
240
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
241
|
+
return 0 /* TypePrecedence.Union */;
|
|
242
|
+
}
|
|
243
|
+
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
244
|
+
return 1 /* TypePrecedence.Intersection */;
|
|
245
|
+
}
|
|
246
|
+
default: {
|
|
247
|
+
return 2 /* TypePrecedence.Object */;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Create a Zod schema for a SharedTree schema class.
|
|
253
|
+
* @alpha
|
|
254
|
+
*/
|
|
255
|
+
export function instanceOf(schema) {
|
|
256
|
+
if (!(schema instanceof ObjectNodeSchema)) {
|
|
257
|
+
throw new UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);
|
|
258
|
+
}
|
|
259
|
+
const effect = z.instanceof(schema);
|
|
260
|
+
instanceOfs.set(effect, schema);
|
|
261
|
+
return effect;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* A lookup from Zod instanceOf schemas to their corresponding ObjectNodeSchema.
|
|
265
|
+
*/
|
|
266
|
+
export const instanceOfs = new WeakMap();
|
|
267
|
+
//# 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,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AAEtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,UAAU,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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UAAU,UAAU,CACzB,MAAS;IAET,IAAI,CAAC,CAAC,MAAM,YAAY,gBAAgB,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,UAAU,CAAC,GAAG,MAAM,CAAC,UAAU,2CAA2C,CAAC,CAAC;IACvF,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACpC,WAAW,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,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"]}
|
package/lib/typeGeneration.d.ts
CHANGED
|
@@ -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
|
|
6
|
+
import { type SchemaTypeScriptRenderResult } from "./renderSchemaTypeScript.js";
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
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;
|
|
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"}
|
package/lib/typeGeneration.js
CHANGED
|
@@ -2,273 +2,40 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { ArrayNodeSchema, FieldKind, getSimpleSchema, MapNodeSchema, NodeKind, ObjectNodeSchema, RecordNodeSchema, ValueSchema, walkFieldSchema, } from "@fluidframework/tree/internal";
|
|
8
|
-
import { z } from "zod";
|
|
9
|
-
import { FunctionWrapper, getExposedMethods } from "./methodBinding.js";
|
|
5
|
+
import { getSimpleSchema, walkFieldSchema } from "@fluidframework/tree/internal";
|
|
6
|
+
import { getExposedMethods, isBindableSchema } from "./methodBinding.js";
|
|
10
7
|
import { getExposedProperties } from "./propertyBinding.js";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
* TODO: Add a prompt suggestion API!
|
|
15
|
-
*
|
|
16
|
-
* TODO: Handle rate limit errors.
|
|
17
|
-
*
|
|
18
|
-
* TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt
|
|
19
|
-
*
|
|
20
|
-
* TODO make the Ids be "Vector-2" instead of "Vector2" (or else it gets weird when you have a type called "Vector2")
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* 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
|
|
24
|
-
*/
|
|
8
|
+
import { renderSchemaTypeScript, } from "./renderSchemaTypeScript.js";
|
|
9
|
+
import { getOrCreate } from "./utils.js";
|
|
25
10
|
const promptSchemaCache = new WeakMap();
|
|
26
11
|
/**
|
|
27
|
-
*
|
|
12
|
+
* Generates TypeScript declarations for the schemas reachable from the provided root field schema.
|
|
28
13
|
*/
|
|
29
14
|
export function generateEditTypesForPrompt(rootSchema, schema) {
|
|
30
|
-
return getOrCreate(promptSchemaCache, schema, () =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
for (const t of exposedMethods.referencedTypes) {
|
|
43
|
-
allSchemas.add(t);
|
|
44
|
-
objectTypeSchemas.add(t);
|
|
15
|
+
return getOrCreate(promptSchemaCache, schema, () => buildPromptSchemaDescription(rootSchema));
|
|
16
|
+
}
|
|
17
|
+
function buildPromptSchemaDescription(rootSchema) {
|
|
18
|
+
const bindableSchemas = new Map();
|
|
19
|
+
walkFieldSchema(rootSchema, {
|
|
20
|
+
node: (node) => {
|
|
21
|
+
if (isBindableSchema(node)) {
|
|
22
|
+
bindableSchemas.set(node.identifier, node);
|
|
23
|
+
const exposedMethods = getExposedMethods(node);
|
|
24
|
+
for (const referenced of exposedMethods.referencedTypes) {
|
|
25
|
+
if (isBindableSchema(referenced)) {
|
|
26
|
+
bindableSchemas.set(referenced.identifier, referenced);
|
|
45
27
|
}
|
|
46
|
-
const exposedProperties = getExposedProperties(n);
|
|
47
|
-
for (const t of exposedProperties.referencedTypes) {
|
|
48
|
-
allSchemas.add(t);
|
|
49
|
-
objectTypeSchemas.add(t);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
const nodeSchemas = [...objectTypeSchemas.values()];
|
|
55
|
-
const bindableSchemas = new Map(nodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]));
|
|
56
|
-
return generateEditTypes([...allSchemas.values()].map((s) => getSimpleSchema(s)), new Map(), bindableSchemas);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}
|
|
61
|
-
* 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.
|
|
62
|
-
*
|
|
63
|
-
* @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.
|
|
64
|
-
*
|
|
65
|
-
* @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.
|
|
66
|
-
*/
|
|
67
|
-
function generateEditTypes(schemas, objectCache, bindableSchemas) {
|
|
68
|
-
const domainTypes = {};
|
|
69
|
-
for (const schema of schemas) {
|
|
70
|
-
for (const name of schema.definitions.keys()) {
|
|
71
|
-
// If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate
|
|
72
|
-
domainTypes[name] = getOrCreateType(schema.definitions, name, objectCache, bindableSchemas);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
domainTypes,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
function getBoundMembersForBindable(bindableSchema) {
|
|
80
|
-
const memberTypes = [];
|
|
81
|
-
const methods = getExposedMethods(bindableSchema);
|
|
82
|
-
for (const [name, method] of Object.entries(methods.methods)) {
|
|
83
|
-
const zodFunction = z.instanceof(FunctionWrapper);
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
85
|
-
zodFunction.method = method;
|
|
86
|
-
memberTypes.push([name, zodFunction]);
|
|
87
|
-
}
|
|
88
|
-
const props = getExposedProperties(bindableSchema);
|
|
89
|
-
for (const [name, prop] of Object.entries(props.properties)) {
|
|
90
|
-
const schema = prop.schema;
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
92
|
-
schema.property = prop;
|
|
93
|
-
memberTypes.push([name, prop.schema]);
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
members: memberTypes,
|
|
97
|
-
referencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function getBoundMembers(definition, bindableSchemas) {
|
|
101
|
-
const bindableSchema = bindableSchemas.get(definition) ?? fail("unknown definition");
|
|
102
|
-
return getBoundMembersForBindable(bindableSchema).members;
|
|
103
|
-
}
|
|
104
|
-
function addBindingIntersectionIfNeeded(typeString, zodTypeBound, definition, simpleNodeSchema, bindableSchemas) {
|
|
105
|
-
let zodType = zodTypeBound;
|
|
106
|
-
let description = simpleNodeSchema.metadata?.description ?? "";
|
|
107
|
-
const boundMembers = getBoundMembers(definition, bindableSchemas);
|
|
108
|
-
const methods = {};
|
|
109
|
-
const properties = {};
|
|
110
|
-
let hasProperties = false;
|
|
111
|
-
let hasMethods = false;
|
|
112
|
-
if (boundMembers.length > 0) {
|
|
113
|
-
for (const [name, zodMember] of boundMembers) {
|
|
114
|
-
const kind =
|
|
115
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
116
|
-
zodMember.method === undefined
|
|
117
|
-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
118
|
-
zodMember.property === undefined
|
|
119
|
-
? "Unknown"
|
|
120
|
-
: "Property"
|
|
121
|
-
: "Method";
|
|
122
|
-
if (kind === "Unknown") {
|
|
123
|
-
throw new UsageError(`Unrecognized bound member ${name} in schema ${definition}`);
|
|
124
|
-
}
|
|
125
|
-
if (kind === "Method") {
|
|
126
|
-
if (methods[name] !== undefined) {
|
|
127
|
-
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
128
|
-
}
|
|
129
|
-
methods[name] = zodMember;
|
|
130
|
-
}
|
|
131
|
-
if (kind === "Property") {
|
|
132
|
-
if (properties[name] !== undefined) {
|
|
133
|
-
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
134
28
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
hasProperties = Object.keys(properties).length > 0 ? true : false;
|
|
140
|
-
if (hasMethods) {
|
|
141
|
-
zodType = z.intersection(zodType, z.object(methods));
|
|
142
|
-
}
|
|
143
|
-
if (hasProperties) {
|
|
144
|
-
zodType = z.intersection(zodType, z.object(properties));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
let note = "";
|
|
148
|
-
if (hasMethods && hasProperties) {
|
|
149
|
-
note = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;
|
|
150
|
-
}
|
|
151
|
-
else if (hasMethods) {
|
|
152
|
-
note = `Note: this ${typeString} has custom user-defined methods directly on it.`;
|
|
153
|
-
}
|
|
154
|
-
else if (hasProperties) {
|
|
155
|
-
note = `Note: this ${typeString} has custom user-defined properties directly on it.`;
|
|
156
|
-
}
|
|
157
|
-
if (note !== "") {
|
|
158
|
-
description = description === "" ? note : `${description} - ${note}`;
|
|
159
|
-
}
|
|
160
|
-
return zodType.describe(description);
|
|
161
|
-
}
|
|
162
|
-
function getOrCreateType(definitionMap, definition, objectCache, bindableSchemas) {
|
|
163
|
-
const simpleNodeSchema = definitionMap.get(definition) ?? fail("Unexpected definition");
|
|
164
|
-
return getOrCreate(objectCache, simpleNodeSchema, () => {
|
|
165
|
-
// Handle recursive types: temporarily create a zod "lazy" type that can be referenced by a recursive call to getOrCreateType.
|
|
166
|
-
let type;
|
|
167
|
-
objectCache.set(simpleNodeSchema, z.lazy(() => type ?? fail("Recursive type used before creation")));
|
|
168
|
-
switch (simpleNodeSchema.kind) {
|
|
169
|
-
case NodeKind.Object: {
|
|
170
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
171
|
-
const properties = Object.fromEntries([...simpleNodeSchema.fields]
|
|
172
|
-
.map(([key, field]) => {
|
|
173
|
-
return [
|
|
174
|
-
key,
|
|
175
|
-
getOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),
|
|
176
|
-
];
|
|
177
|
-
})
|
|
178
|
-
.filter(([, value]) => value !== undefined));
|
|
179
|
-
for (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {
|
|
180
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
181
|
-
const kind = zodMember.method === undefined ? "Property" : "Method";
|
|
182
|
-
if (properties[name] !== undefined) {
|
|
183
|
-
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
29
|
+
const exposedProperties = getExposedProperties(node);
|
|
30
|
+
for (const referenced of exposedProperties.referencedTypes) {
|
|
31
|
+
if (isBindableSchema(referenced)) {
|
|
32
|
+
bindableSchemas.set(referenced.identifier, referenced);
|
|
184
33
|
}
|
|
185
|
-
properties[name] = zodMember;
|
|
186
34
|
}
|
|
187
|
-
return (type = z
|
|
188
|
-
.object(properties)
|
|
189
|
-
.describe(simpleNodeSchema.metadata?.description ?? ""));
|
|
190
|
-
}
|
|
191
|
-
case NodeKind.Map: {
|
|
192
|
-
const zodType = z.map(z.string(), getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
193
|
-
return (type = addBindingIntersectionIfNeeded("map", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
194
|
-
}
|
|
195
|
-
case NodeKind.Record: {
|
|
196
|
-
const zodType = z.record(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
197
|
-
return (type = addBindingIntersectionIfNeeded("record", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
198
35
|
}
|
|
199
|
-
|
|
200
|
-
const zodType = z.array(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
201
|
-
return (type = addBindingIntersectionIfNeeded("array", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
202
|
-
}
|
|
203
|
-
case NodeKind.Leaf: {
|
|
204
|
-
switch (simpleNodeSchema.leafKind) {
|
|
205
|
-
case ValueSchema.Boolean: {
|
|
206
|
-
return (type = z.boolean());
|
|
207
|
-
}
|
|
208
|
-
case ValueSchema.Number: {
|
|
209
|
-
return (type = z.number());
|
|
210
|
-
}
|
|
211
|
-
case ValueSchema.String: {
|
|
212
|
-
return (type = z.string());
|
|
213
|
-
}
|
|
214
|
-
case ValueSchema.Null: {
|
|
215
|
-
return (type = z.null());
|
|
216
|
-
}
|
|
217
|
-
default: {
|
|
218
|
-
throw new Error(`Unsupported leaf kind ${NodeKind[simpleNodeSchema.leafKind]}.`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
default: {
|
|
223
|
-
return unreachableCase(simpleNodeSchema, "Unknown node kind");
|
|
224
|
-
}
|
|
225
|
-
}
|
|
36
|
+
},
|
|
226
37
|
});
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const customMetadata = fieldSchema.metadata.custom;
|
|
230
|
-
const getDefault = customMetadata?.[llmDefault];
|
|
231
|
-
if (getDefault !== undefined) {
|
|
232
|
-
if (typeof getDefault !== "function") {
|
|
233
|
-
throw new UsageError(`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`);
|
|
234
|
-
}
|
|
235
|
-
if (fieldSchema.kind !== FieldKind.Optional) {
|
|
236
|
-
throw new UsageError(`The ${llmDefault.description} property is only permitted on optional fields.`);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
const field = getTypeForAllowedTypes(definitionMap, new Set(fieldSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas).describe(getDefault === undefined
|
|
240
|
-
? (fieldSchema.metadata?.description ?? "")
|
|
241
|
-
: "Do not populate this field. It will be automatically supplied by the system after insertion.");
|
|
242
|
-
switch (fieldSchema.kind) {
|
|
243
|
-
case FieldKind.Required: {
|
|
244
|
-
return field;
|
|
245
|
-
}
|
|
246
|
-
case FieldKind.Optional: {
|
|
247
|
-
return field.optional();
|
|
248
|
-
}
|
|
249
|
-
case FieldKind.Identifier: {
|
|
250
|
-
return field
|
|
251
|
-
.optional()
|
|
252
|
-
.describe("This is an ID automatically generated by the system. Do not supply it when constructing a new object.");
|
|
253
|
-
}
|
|
254
|
-
default: {
|
|
255
|
-
throw new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
function getTypeForAllowedTypes(definitionMap, allowedTypes, objectCache, bindableSchemas) {
|
|
260
|
-
const single = tryGetSingleton(allowedTypes);
|
|
261
|
-
if (single === undefined) {
|
|
262
|
-
const types = [
|
|
263
|
-
...mapIterable(allowedTypes, (name) => {
|
|
264
|
-
return getOrCreateType(definitionMap, name, objectCache, bindableSchemas);
|
|
265
|
-
}),
|
|
266
|
-
];
|
|
267
|
-
assert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
|
|
268
|
-
return z.union(types);
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
return getOrCreateType(definitionMap, single, objectCache, bindableSchemas);
|
|
272
|
-
}
|
|
38
|
+
const simpleTreeSchema = getSimpleSchema(rootSchema);
|
|
39
|
+
return renderSchemaTypeScript(simpleTreeSchema.definitions, bindableSchemas);
|
|
273
40
|
}
|
|
274
41
|
//# sourceMappingURL=typeGeneration.js.map
|