@fluidframework/tree-agent 2.74.0-365691 → 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.
- 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,314 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
|
+
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
8
|
+
import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree/alpha";
|
|
9
|
+
import { ObjectNodeSchema } from "@fluidframework/tree/alpha";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Converts Zod schema definitions into TypeScript declaration text.
|
|
14
|
+
*/
|
|
15
|
+
export function renderZodTypeScript(
|
|
16
|
+
zodType: z.ZodTypeAny,
|
|
17
|
+
getFriendlyName: (schema: TreeNodeSchema) => string,
|
|
18
|
+
instanceOfLookup: WeakMap<z.ZodTypeAny, ObjectNodeSchema>,
|
|
19
|
+
): string {
|
|
20
|
+
let result = "";
|
|
21
|
+
let startOfLine = true;
|
|
22
|
+
let indent = 0;
|
|
23
|
+
|
|
24
|
+
appendType(zodType);
|
|
25
|
+
return result;
|
|
26
|
+
|
|
27
|
+
function appendType(type: z.ZodTypeAny, minPrecedence = TypePrecedence.Object): void {
|
|
28
|
+
const shouldParenthesize = getTypePrecendece(type) < minPrecedence;
|
|
29
|
+
if (shouldParenthesize) {
|
|
30
|
+
append("(");
|
|
31
|
+
}
|
|
32
|
+
appendTypeDefinition(type);
|
|
33
|
+
if (shouldParenthesize) {
|
|
34
|
+
append(")");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function append(s: string): void {
|
|
39
|
+
if (startOfLine) {
|
|
40
|
+
result += " ".repeat(indent);
|
|
41
|
+
startOfLine = false;
|
|
42
|
+
}
|
|
43
|
+
result += s;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function appendNewLine(): void {
|
|
47
|
+
append("\n");
|
|
48
|
+
startOfLine = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function appendTypeDefinition(type: z.ZodTypeAny): void {
|
|
52
|
+
switch (getTypeKind(type)) {
|
|
53
|
+
case z.ZodFirstPartyTypeKind.ZodString: {
|
|
54
|
+
append("string");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
case z.ZodFirstPartyTypeKind.ZodNumber: {
|
|
58
|
+
append("number");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
case z.ZodFirstPartyTypeKind.ZodBoolean: {
|
|
62
|
+
append("boolean");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
case z.ZodFirstPartyTypeKind.ZodDate: {
|
|
66
|
+
append("Date");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
case z.ZodFirstPartyTypeKind.ZodUndefined: {
|
|
70
|
+
append("undefined");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
case z.ZodFirstPartyTypeKind.ZodNull: {
|
|
74
|
+
append("null");
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
case z.ZodFirstPartyTypeKind.ZodUnknown: {
|
|
78
|
+
append("unknown");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
case z.ZodFirstPartyTypeKind.ZodArray: {
|
|
82
|
+
appendArrayType(type);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case z.ZodFirstPartyTypeKind.ZodObject: {
|
|
86
|
+
appendObjectType(type);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
case z.ZodFirstPartyTypeKind.ZodUnion: {
|
|
90
|
+
appendUnionOrIntersectionTypes(
|
|
91
|
+
(type._def as z.ZodUnionDef).options,
|
|
92
|
+
TypePrecedence.Union,
|
|
93
|
+
);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
97
|
+
appendUnionOrIntersectionTypes(
|
|
98
|
+
[...(type._def as z.ZodDiscriminatedUnionDef<string>).options.values()],
|
|
99
|
+
TypePrecedence.Union,
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
104
|
+
appendUnionOrIntersectionTypes(
|
|
105
|
+
[
|
|
106
|
+
(type._def as z.ZodIntersectionDef).left,
|
|
107
|
+
(type._def as z.ZodIntersectionDef).right,
|
|
108
|
+
],
|
|
109
|
+
TypePrecedence.Intersection,
|
|
110
|
+
);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
case z.ZodFirstPartyTypeKind.ZodTuple: {
|
|
114
|
+
appendTupleType(type);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
case z.ZodFirstPartyTypeKind.ZodRecord: {
|
|
118
|
+
appendRecordType(type);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
case z.ZodFirstPartyTypeKind.ZodMap: {
|
|
122
|
+
appendMapType(type);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
case z.ZodFirstPartyTypeKind.ZodLiteral: {
|
|
126
|
+
appendLiteral((type._def as z.ZodLiteralDef).value);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
case z.ZodFirstPartyTypeKind.ZodEnum: {
|
|
130
|
+
append(
|
|
131
|
+
(type._def as z.ZodEnumDef).values.map((value) => JSON.stringify(value)).join(" | "),
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
case z.ZodFirstPartyTypeKind.ZodOptional: {
|
|
136
|
+
appendUnionOrIntersectionTypes(
|
|
137
|
+
[(type._def as z.ZodOptionalDef).innerType, z.undefined()],
|
|
138
|
+
TypePrecedence.Union,
|
|
139
|
+
);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
case z.ZodFirstPartyTypeKind.ZodReadonly: {
|
|
143
|
+
appendReadonlyType(type);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
case z.ZodFirstPartyTypeKind.ZodEffects: {
|
|
147
|
+
const schema = instanceOfLookup.get(type);
|
|
148
|
+
if (schema === undefined) {
|
|
149
|
+
throw new UsageError(
|
|
150
|
+
`Unsupported zod effects type when formatting helper types: ${getTypeKind(type)}`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
append(getFriendlyName(schema));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
case z.ZodFirstPartyTypeKind.ZodVoid: {
|
|
157
|
+
append("void");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
case z.ZodFirstPartyTypeKind.ZodLazy: {
|
|
161
|
+
appendType((type._def as z.ZodLazyDef).getter());
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
default: {
|
|
165
|
+
throw new UsageError(
|
|
166
|
+
`Unsupported type when formatting helper types: ${getTypeKind(type)}`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function appendArrayType(arrayType: z.ZodTypeAny): void {
|
|
173
|
+
appendType((arrayType._def as z.ZodArrayDef).type, TypePrecedence.Object);
|
|
174
|
+
append("[]");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function appendObjectType(objectType: z.ZodTypeAny): void {
|
|
178
|
+
append("{");
|
|
179
|
+
appendNewLine();
|
|
180
|
+
indent++;
|
|
181
|
+
for (const [name, entry] of Object.entries((objectType._def as z.ZodObjectDef).shape())) {
|
|
182
|
+
let propertyType = entry;
|
|
183
|
+
append(name);
|
|
184
|
+
if (getTypeKind(propertyType) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
185
|
+
append("?");
|
|
186
|
+
propertyType = (propertyType._def as z.ZodOptionalDef).innerType;
|
|
187
|
+
}
|
|
188
|
+
append(": ");
|
|
189
|
+
appendType(propertyType);
|
|
190
|
+
append(";");
|
|
191
|
+
appendNewLine();
|
|
192
|
+
}
|
|
193
|
+
indent--;
|
|
194
|
+
append("}");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function appendUnionOrIntersectionTypes(
|
|
198
|
+
types: readonly z.ZodTypeAny[],
|
|
199
|
+
minPrecedence: TypePrecedence,
|
|
200
|
+
): void {
|
|
201
|
+
let first = true;
|
|
202
|
+
for (const innerType of types) {
|
|
203
|
+
if (!first) {
|
|
204
|
+
append(minPrecedence === TypePrecedence.Intersection ? " & " : " | ");
|
|
205
|
+
}
|
|
206
|
+
appendType(innerType, minPrecedence);
|
|
207
|
+
first = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function appendTupleType(tupleType: z.ZodTypeAny): void {
|
|
212
|
+
append("[");
|
|
213
|
+
let first = true;
|
|
214
|
+
for (const innerType of (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType>)
|
|
215
|
+
.items) {
|
|
216
|
+
if (!first) {
|
|
217
|
+
append(", ");
|
|
218
|
+
}
|
|
219
|
+
if (getTypeKind(innerType) === z.ZodFirstPartyTypeKind.ZodOptional) {
|
|
220
|
+
appendType((innerType._def as z.ZodOptionalDef).innerType, TypePrecedence.Object);
|
|
221
|
+
append("?");
|
|
222
|
+
} else {
|
|
223
|
+
appendType(innerType);
|
|
224
|
+
}
|
|
225
|
+
first = false;
|
|
226
|
+
}
|
|
227
|
+
const rest = (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType | null>).rest;
|
|
228
|
+
if (rest !== null) {
|
|
229
|
+
if (!first) {
|
|
230
|
+
append(", ");
|
|
231
|
+
}
|
|
232
|
+
append("...");
|
|
233
|
+
appendType(rest, TypePrecedence.Object);
|
|
234
|
+
append("[]");
|
|
235
|
+
}
|
|
236
|
+
append("]");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function appendRecordType(recordType: z.ZodTypeAny): void {
|
|
240
|
+
append("Record<");
|
|
241
|
+
appendType((recordType._def as z.ZodRecordDef).keyType);
|
|
242
|
+
append(", ");
|
|
243
|
+
appendType((recordType._def as z.ZodRecordDef).valueType);
|
|
244
|
+
append(">");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function appendMapType(mapType: z.ZodTypeAny): void {
|
|
248
|
+
append("Map<");
|
|
249
|
+
appendType((mapType._def as z.ZodMapDef).keyType);
|
|
250
|
+
append(", ");
|
|
251
|
+
appendType((mapType._def as z.ZodMapDef).valueType);
|
|
252
|
+
append(">");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function appendLiteral(value: unknown): void {
|
|
256
|
+
append(
|
|
257
|
+
typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
|
258
|
+
? JSON.stringify(value)
|
|
259
|
+
: "any",
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function appendReadonlyType(readonlyType: z.ZodTypeAny): void {
|
|
264
|
+
append("Readonly<");
|
|
265
|
+
appendType((readonlyType._def as z.ZodReadonlyDef).innerType);
|
|
266
|
+
append(">");
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function getTypeKind(type: z.ZodType): z.ZodFirstPartyTypeKind {
|
|
271
|
+
return (type._def as z.ZodTypeDef & { typeName: z.ZodFirstPartyTypeKind }).typeName;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const enum TypePrecedence {
|
|
275
|
+
Union = 0,
|
|
276
|
+
Intersection = 1,
|
|
277
|
+
Object = 2,
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function getTypePrecendece(type: z.ZodType): TypePrecedence {
|
|
281
|
+
switch (getTypeKind(type)) {
|
|
282
|
+
case z.ZodFirstPartyTypeKind.ZodEnum:
|
|
283
|
+
case z.ZodFirstPartyTypeKind.ZodUnion:
|
|
284
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
|
285
|
+
return TypePrecedence.Union;
|
|
286
|
+
}
|
|
287
|
+
case z.ZodFirstPartyTypeKind.ZodIntersection: {
|
|
288
|
+
return TypePrecedence.Intersection;
|
|
289
|
+
}
|
|
290
|
+
default: {
|
|
291
|
+
return TypePrecedence.Object;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Create a Zod schema for a SharedTree schema class.
|
|
298
|
+
* @alpha
|
|
299
|
+
*/
|
|
300
|
+
export function instanceOf<T extends TreeNodeSchemaClass>(
|
|
301
|
+
schema: T,
|
|
302
|
+
): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>> {
|
|
303
|
+
if (!(schema instanceof ObjectNodeSchema)) {
|
|
304
|
+
throw new UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);
|
|
305
|
+
}
|
|
306
|
+
const effect = z.instanceof(schema);
|
|
307
|
+
instanceOfs.set(effect, schema);
|
|
308
|
+
return effect;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* A lookup from Zod instanceOf schemas to their corresponding ObjectNodeSchema.
|
|
313
|
+
*/
|
|
314
|
+
export const instanceOfs = new WeakMap<z.ZodTypeAny, ObjectNodeSchema>();
|