@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,395 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
6
|
+
import { FieldKind, NodeKind, ValueSchema } from "@fluidframework/tree/internal";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { getExposedMethods } from "./methodBinding.js";
|
|
9
|
+
import { getExposedProperties } from "./propertyBinding.js";
|
|
10
|
+
import { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from "./utils.js";
|
|
11
|
+
import { instanceOfs, renderZodTypeScript } from "./renderZodTypeScript.js";
|
|
12
|
+
var TypePrecedence;
|
|
13
|
+
(function (TypePrecedence) {
|
|
14
|
+
TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
|
|
15
|
+
TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
|
|
16
|
+
TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
|
|
17
|
+
})(TypePrecedence || (TypePrecedence = {}));
|
|
18
|
+
/**
|
|
19
|
+
* Converts schema metadata into TypeScript declarations suitable for prompt inclusion.
|
|
20
|
+
*/
|
|
21
|
+
export function renderSchemaTypeScript(definitions, bindableSchemas) {
|
|
22
|
+
const friendlyNames = new Map();
|
|
23
|
+
let hasHelperMethods = false;
|
|
24
|
+
for (const identifier of definitions.keys()) {
|
|
25
|
+
if (isNamedSchema(identifier)) {
|
|
26
|
+
friendlyNames.set(identifier, unqualifySchema(identifier));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const declarations = [];
|
|
30
|
+
for (const [identifier, schema] of definitions) {
|
|
31
|
+
if (!isNamedSchema(identifier)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const friendlyName = friendlyNames.get(identifier) ?? unqualifySchema(identifier);
|
|
35
|
+
const rendered = renderNamedSchema(identifier, friendlyName, schema);
|
|
36
|
+
if (rendered === undefined) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const lines = [];
|
|
40
|
+
if (rendered.description !== undefined && rendered.description !== "") {
|
|
41
|
+
for (const comment of rendered.description.split("\n")) {
|
|
42
|
+
lines.push(`// ${comment}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
lines.push(rendered.declaration);
|
|
46
|
+
declarations.push(lines.join("\n"));
|
|
47
|
+
}
|
|
48
|
+
const schemaText = declarations.join("\n\n");
|
|
49
|
+
return {
|
|
50
|
+
schemaText: schemaText === "" ? "" : `${schemaText}\n`,
|
|
51
|
+
hasHelperMethods,
|
|
52
|
+
};
|
|
53
|
+
function renderNamedSchema(identifier, friendlyName, schema) {
|
|
54
|
+
switch (schema.kind) {
|
|
55
|
+
case NodeKind.Object: {
|
|
56
|
+
return renderObjectDeclaration(identifier, friendlyName, schema);
|
|
57
|
+
}
|
|
58
|
+
case NodeKind.Array: {
|
|
59
|
+
return renderArrayDeclaration(identifier, friendlyName, schema);
|
|
60
|
+
}
|
|
61
|
+
case NodeKind.Map: {
|
|
62
|
+
return renderMapDeclaration(identifier, friendlyName, schema);
|
|
63
|
+
}
|
|
64
|
+
case NodeKind.Record: {
|
|
65
|
+
return renderRecordDeclaration(identifier, friendlyName, schema);
|
|
66
|
+
}
|
|
67
|
+
case NodeKind.Leaf: {
|
|
68
|
+
return {
|
|
69
|
+
declaration: `type ${friendlyName} = ${renderLeaf(schema.leafKind)};`,
|
|
70
|
+
description: schema.metadata?.description,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
default: {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function renderObjectDeclaration(definition, name, schema) {
|
|
79
|
+
const fieldLines = [];
|
|
80
|
+
const fieldNames = new Set();
|
|
81
|
+
for (const [fieldName, fieldSchema] of schema.fields) {
|
|
82
|
+
fieldNames.add(fieldName);
|
|
83
|
+
fieldLines.push(...renderFieldLine(fieldName, fieldSchema));
|
|
84
|
+
}
|
|
85
|
+
const { methods, properties } = getBoundMembers(definition);
|
|
86
|
+
ensureNoMemberConflicts(definition, fieldNames, methods, properties);
|
|
87
|
+
fieldLines.push(...renderPropertyLines(properties));
|
|
88
|
+
fieldLines.push(...renderMethodLines(methods));
|
|
89
|
+
const members = fieldLines.map((line) => ` ${line}`).join("\n");
|
|
90
|
+
const body = members === "" ? "" : `\n${members}`;
|
|
91
|
+
return {
|
|
92
|
+
declaration: `interface ${name} {${body}\n}`,
|
|
93
|
+
description: schema.metadata?.description,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function renderArrayDeclaration(definition, name, schema) {
|
|
97
|
+
const elementTypes = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
98
|
+
const base = `${formatExpression(elementTypes)}[]`;
|
|
99
|
+
const binding = renderBindingIntersection(definition);
|
|
100
|
+
return {
|
|
101
|
+
declaration: `type ${name} = ${base}${binding.suffix};`,
|
|
102
|
+
description: describeBinding(schema.metadata?.description, "array", binding),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function renderMapDeclaration(definition, name, schema) {
|
|
106
|
+
const valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
107
|
+
const base = `Map<string, ${valueType.text}>`;
|
|
108
|
+
const binding = renderBindingIntersection(definition);
|
|
109
|
+
return {
|
|
110
|
+
declaration: `type ${name} = ${base}${binding.suffix};`,
|
|
111
|
+
description: describeBinding(schema.metadata?.description, "map", binding),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function renderRecordDeclaration(definition, name, schema) {
|
|
115
|
+
const valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
116
|
+
const base = `Record<string, ${valueType.text}>`;
|
|
117
|
+
const binding = renderBindingIntersection(definition);
|
|
118
|
+
return {
|
|
119
|
+
declaration: `type ${name} = ${base}${binding.suffix};`,
|
|
120
|
+
description: describeBinding(schema.metadata?.description, "record", binding),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function renderFieldLine(name, field) {
|
|
124
|
+
const { comment, optional, type } = describeField(field);
|
|
125
|
+
const lines = [];
|
|
126
|
+
if (comment !== undefined && comment !== "") {
|
|
127
|
+
for (const note of comment.split("\n")) {
|
|
128
|
+
lines.push(`// ${note}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
lines.push(`${name}${optional ? "?" : ""}: ${type};`);
|
|
132
|
+
return lines;
|
|
133
|
+
}
|
|
134
|
+
function describeField(field) {
|
|
135
|
+
const allowedTypes = renderAllowedTypes(field.simpleAllowedTypes.keys());
|
|
136
|
+
const type = formatExpression(allowedTypes);
|
|
137
|
+
const optional = field.kind !== FieldKind.Required;
|
|
138
|
+
const customMetadata = field.metadata.custom;
|
|
139
|
+
const getDefault = customMetadata?.[llmDefault];
|
|
140
|
+
if (getDefault !== undefined) {
|
|
141
|
+
if (typeof getDefault !== "function") {
|
|
142
|
+
throw new UsageError(`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`);
|
|
143
|
+
}
|
|
144
|
+
if (field.kind !== FieldKind.Optional) {
|
|
145
|
+
throw new UsageError(`The ${llmDefault.description} property is only permitted on optional fields.`);
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
optional,
|
|
149
|
+
type,
|
|
150
|
+
comment: "Do not populate this field. It will be automatically supplied by the system after insertion.",
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (field.kind === FieldKind.Identifier) {
|
|
154
|
+
return {
|
|
155
|
+
optional: true,
|
|
156
|
+
type,
|
|
157
|
+
comment: "This is an ID automatically generated by the system. Do not supply it when constructing a new object.",
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const description = field.metadata?.description;
|
|
161
|
+
return {
|
|
162
|
+
optional,
|
|
163
|
+
type,
|
|
164
|
+
comment: description === undefined || description === "" ? undefined : description,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function renderBindingIntersection(definition) {
|
|
168
|
+
const { methods, properties } = getBoundMembers(definition);
|
|
169
|
+
const propertyLines = renderPropertyLines(properties);
|
|
170
|
+
const methodLines = renderMethodLines(methods);
|
|
171
|
+
if (propertyLines.length === 0 && methodLines.length === 0) {
|
|
172
|
+
return { hasMethods: false, hasProperties: false, suffix: "" };
|
|
173
|
+
}
|
|
174
|
+
const lines = [...propertyLines, ...methodLines].map((line) => ` ${line}`);
|
|
175
|
+
const suffix = ` & {\n${lines.join("\n")}\n}`;
|
|
176
|
+
return {
|
|
177
|
+
hasMethods: methodLines.length > 0,
|
|
178
|
+
hasProperties: propertyLines.length > 0,
|
|
179
|
+
suffix,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function renderMethodLines(methods) {
|
|
183
|
+
const lines = [];
|
|
184
|
+
for (const [name, method] of Object.entries(methods)) {
|
|
185
|
+
if (method.description !== undefined && method.description !== "") {
|
|
186
|
+
for (const note of method.description.split("\n")) {
|
|
187
|
+
lines.push(`// ${note}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
lines.push(formatMethod(name, method));
|
|
191
|
+
}
|
|
192
|
+
if (lines.length > 0) {
|
|
193
|
+
hasHelperMethods = true;
|
|
194
|
+
}
|
|
195
|
+
return lines;
|
|
196
|
+
}
|
|
197
|
+
function getBoundMembers(definition) {
|
|
198
|
+
const schemaClass = bindableSchemas.get(definition);
|
|
199
|
+
if (schemaClass === undefined) {
|
|
200
|
+
return { methods: {}, properties: {} };
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
methods: getExposedMethods(schemaClass).methods,
|
|
204
|
+
properties: getExposedProperties(schemaClass).properties,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function renderAllowedTypes(allowedTypes) {
|
|
208
|
+
const expressions = [];
|
|
209
|
+
for (const identifier of allowedTypes) {
|
|
210
|
+
expressions.push(renderTypeReference(identifier));
|
|
211
|
+
}
|
|
212
|
+
if (expressions.length === 0) {
|
|
213
|
+
return { precedence: 2 /* TypePrecedence.Object */, text: "never" };
|
|
214
|
+
}
|
|
215
|
+
if (expressions.length === 1) {
|
|
216
|
+
return expressions[0] ?? { precedence: 2 /* TypePrecedence.Object */, text: "never" };
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
precedence: 0 /* TypePrecedence.Union */,
|
|
220
|
+
text: expressions
|
|
221
|
+
.map((expr) => formatExpression(expr, 0 /* TypePrecedence.Union */))
|
|
222
|
+
.join(" | "),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function renderTypeReference(identifier) {
|
|
226
|
+
const schema = definitions.get(identifier);
|
|
227
|
+
if (schema === undefined) {
|
|
228
|
+
return {
|
|
229
|
+
precedence: 2 /* TypePrecedence.Object */,
|
|
230
|
+
text: friendlyNames.get(identifier) ?? unqualifySchema(identifier),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (isNamedSchema(identifier)) {
|
|
234
|
+
return {
|
|
235
|
+
precedence: 2 /* TypePrecedence.Object */,
|
|
236
|
+
text: friendlyNames.get(identifier) ?? unqualifySchema(identifier),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return renderInlineSchema(schema);
|
|
240
|
+
}
|
|
241
|
+
function renderInlineSchema(schema) {
|
|
242
|
+
switch (schema.kind) {
|
|
243
|
+
case NodeKind.Object: {
|
|
244
|
+
return renderInlineObject(schema);
|
|
245
|
+
}
|
|
246
|
+
case NodeKind.Array: {
|
|
247
|
+
return renderInlineArray(schema);
|
|
248
|
+
}
|
|
249
|
+
case NodeKind.Map: {
|
|
250
|
+
return renderInlineMap(schema);
|
|
251
|
+
}
|
|
252
|
+
case NodeKind.Record: {
|
|
253
|
+
return renderInlineRecord(schema);
|
|
254
|
+
}
|
|
255
|
+
case NodeKind.Leaf: {
|
|
256
|
+
return { precedence: 2 /* TypePrecedence.Object */, text: renderLeaf(schema.leafKind) };
|
|
257
|
+
}
|
|
258
|
+
default: {
|
|
259
|
+
return { precedence: 2 /* TypePrecedence.Object */, text: "unknown" };
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function renderInlineObject(schema) {
|
|
264
|
+
const fieldLines = [];
|
|
265
|
+
for (const [fieldName, fieldSchema] of schema.fields) {
|
|
266
|
+
fieldLines.push(...renderFieldLine(fieldName, fieldSchema));
|
|
267
|
+
}
|
|
268
|
+
const members = fieldLines.map((line) => ` ${line}`).join("\n");
|
|
269
|
+
const text = members === ""
|
|
270
|
+
? "{\n}"
|
|
271
|
+
: `{
|
|
272
|
+
${members}
|
|
273
|
+
}`;
|
|
274
|
+
return { precedence: 2 /* TypePrecedence.Object */, text };
|
|
275
|
+
}
|
|
276
|
+
function renderInlineArray(schema) {
|
|
277
|
+
const elementTypes = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
278
|
+
return {
|
|
279
|
+
precedence: 2 /* TypePrecedence.Object */,
|
|
280
|
+
text: `${formatExpression(elementTypes)}[]`,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function renderInlineMap(schema) {
|
|
284
|
+
const valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
285
|
+
return {
|
|
286
|
+
precedence: 2 /* TypePrecedence.Object */,
|
|
287
|
+
text: `Map<string, ${valueType.text}>`,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function renderInlineRecord(schema) {
|
|
291
|
+
const valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());
|
|
292
|
+
return {
|
|
293
|
+
precedence: 2 /* TypePrecedence.Object */,
|
|
294
|
+
text: `Record<string, ${valueType.text}>`,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function describeBinding(description, typeLabel, binding) {
|
|
299
|
+
let note = "";
|
|
300
|
+
if (binding.hasMethods && binding.hasProperties) {
|
|
301
|
+
note = `Note: this ${typeLabel} has custom user-defined methods and properties directly on it.`;
|
|
302
|
+
}
|
|
303
|
+
else if (binding.hasMethods) {
|
|
304
|
+
note = `Note: this ${typeLabel} has custom user-defined methods directly on it.`;
|
|
305
|
+
}
|
|
306
|
+
else if (binding.hasProperties) {
|
|
307
|
+
note = `Note: this ${typeLabel} has custom user-defined properties directly on it.`;
|
|
308
|
+
}
|
|
309
|
+
if (note === "") {
|
|
310
|
+
return description === undefined || description === "" ? undefined : description;
|
|
311
|
+
}
|
|
312
|
+
if (description === undefined || description === "") {
|
|
313
|
+
return note;
|
|
314
|
+
}
|
|
315
|
+
return `${description} - ${note}`;
|
|
316
|
+
}
|
|
317
|
+
function renderPropertyLines(properties) {
|
|
318
|
+
const lines = [];
|
|
319
|
+
for (const [name, property] of Object.entries(properties)) {
|
|
320
|
+
if (property.description !== undefined && property.description !== "") {
|
|
321
|
+
for (const note of property.description.split("\n")) {
|
|
322
|
+
lines.push(`// ${note}`);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const modifier = property.readOnly ? "readonly " : "";
|
|
326
|
+
lines.push(`${modifier}${name}: ${renderZodType(property.schema)};`);
|
|
327
|
+
}
|
|
328
|
+
return lines;
|
|
329
|
+
}
|
|
330
|
+
function formatMethod(name, method) {
|
|
331
|
+
const args = [];
|
|
332
|
+
for (const [argName, argType] of method.args) {
|
|
333
|
+
const { innerType, optional } = unwrapOptional(argType);
|
|
334
|
+
const renderedType = renderZodType(innerType);
|
|
335
|
+
args.push(`${argName}${optional ? "?" : ""}: ${renderedType}`);
|
|
336
|
+
}
|
|
337
|
+
if (method.rest !== null) {
|
|
338
|
+
args.push(`...rest: ${renderZodType(method.rest)}[]`);
|
|
339
|
+
}
|
|
340
|
+
return `${name}(${args.join(", ")}): ${renderZodType(method.returns)};`;
|
|
341
|
+
}
|
|
342
|
+
function renderLeaf(leafKind) {
|
|
343
|
+
switch (leafKind) {
|
|
344
|
+
case ValueSchema.Boolean: {
|
|
345
|
+
return "boolean";
|
|
346
|
+
}
|
|
347
|
+
case ValueSchema.Number: {
|
|
348
|
+
return "number";
|
|
349
|
+
}
|
|
350
|
+
case ValueSchema.String: {
|
|
351
|
+
return "string";
|
|
352
|
+
}
|
|
353
|
+
case ValueSchema.Null: {
|
|
354
|
+
return "null";
|
|
355
|
+
}
|
|
356
|
+
default: {
|
|
357
|
+
throw new Error(`Unsupported leaf kind ${NodeKind[leafKind]}.`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function formatExpression(expression, minPrecedence = 2 /* TypePrecedence.Object */) {
|
|
362
|
+
return expression.precedence < minPrecedence ? `(${expression.text})` : expression.text;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Detects optional zod wrappers so argument lists can keep TypeScript optional markers in sync.
|
|
366
|
+
*/
|
|
367
|
+
function unwrapOptional(type) {
|
|
368
|
+
if (type instanceof z.ZodOptional) {
|
|
369
|
+
const inner = type.unwrap();
|
|
370
|
+
return { innerType: inner, optional: true };
|
|
371
|
+
}
|
|
372
|
+
return { innerType: type, optional: false };
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Verifies that helper members do not clobber structural fields and fails fast if they do.
|
|
376
|
+
*/
|
|
377
|
+
function ensureNoMemberConflicts(definition, fieldNames, methods, properties) {
|
|
378
|
+
for (const name of Object.keys(methods)) {
|
|
379
|
+
if (fieldNames.has(name)) {
|
|
380
|
+
throw new UsageError(`Method ${name} conflicts with field of the same name in schema ${definition}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
for (const name of Object.keys(properties)) {
|
|
384
|
+
if (fieldNames.has(name)) {
|
|
385
|
+
throw new UsageError(`Property ${name} conflicts with field of the same name in schema ${definition}`);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Converts schema metadata into TypeScript declarations suitable for prompt inclusion.
|
|
391
|
+
*/
|
|
392
|
+
function renderZodType(type) {
|
|
393
|
+
return renderZodTypeScript(type, getFriendlyName, instanceOfs);
|
|
394
|
+
}
|
|
395
|
+
//# sourceMappingURL=renderSchemaTypeScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSchemaTypeScript.js","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AASjF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAoB,MAAM,sBAAsB,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAuB5E,IAAW,cAIV;AAJD,WAAW,cAAc;IACxB,qDAAS,CAAA;IACT,mEAAgB,CAAA;IAChB,uDAAU,CAAA;AACX,CAAC,EAJU,cAAc,KAAd,cAAc,QAIxB;AAUD;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAC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,EAAE,IAAI,EAAE,CAAC;IAC7C,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAC7C,CAAC;AAED;;GAEG;AACH,SAAS,uBAAuB,CAC/B,UAAkB,EAClB,UAA+B,EAC/B,OAAwC,EACxC,UAAuC;IAEvC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACzC,IAAI,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,UAAU,CACnB,UAAU,IAAI,oDAAoD,UAAU,EAAE,CAC9E,CAAC;QACH,CAAC;IACF,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5C,IAAI,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,UAAU,CACnB,YAAY,IAAI,oDAAoD,UAAU,EAAE,CAChF,CAAC;QACH,CAAC;IACF,CAAC;AACF,CAAC;AAED;;GAEG;AACH,SAAS,aAAa,CAAC,IAAkB;IACxC,OAAO,mBAAmB,CAAC,IAAI,EAAE,eAAe,EAAE,WAAW,CAAC,CAAC;AAChE,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport { FieldKind, NodeKind, ValueSchema } from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleArrayNodeSchema,\n\tSimpleFieldSchema,\n\tSimpleMapNodeSchema,\n\tSimpleNodeSchema,\n\tSimpleObjectNodeSchema,\n\tSimpleRecordNodeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z } from \"zod\";\n\nimport type { BindableSchema, FunctionWrapper } from \"./methodBinding.js\";\nimport { getExposedMethods } from \"./methodBinding.js\";\nimport { getExposedProperties, type PropertyDef } from \"./propertyBinding.js\";\nimport { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from \"./utils.js\";\nimport { instanceOfs, renderZodTypeScript } from \"./renderZodTypeScript.js\";\n\ninterface BoundMembers {\n\tmethods: Record<string, FunctionWrapper>;\n\tproperties: Record<string, PropertyDef>;\n}\n\ninterface RenderResult {\n\tdeclaration: string;\n\tdescription?: string;\n}\n\ninterface BindingIntersectionResult {\n\thasMethods: boolean;\n\thasProperties: boolean;\n\tsuffix: string;\n}\n\ninterface TypeExpression {\n\tprecedence: TypePrecedence;\n\ttext: string;\n}\n\nconst enum TypePrecedence {\n\tUnion = 0,\n\tIntersection = 1,\n\tObject = 2,\n}\n\n/**\n * Output of rendering schema metadata into TypeScript declaration text.\n */\nexport interface SchemaTypeScriptRenderResult {\n\tschemaText: string;\n\thasHelperMethods: boolean;\n}\n\n/**\n * Converts schema metadata into TypeScript declarations suitable for prompt inclusion.\n */\nexport function renderSchemaTypeScript(\n\tdefinitions: ReadonlyMap<string, SimpleNodeSchema>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): SchemaTypeScriptRenderResult {\n\tconst friendlyNames = new Map<string, string>();\n\tlet hasHelperMethods = false;\n\n\tfor (const identifier of definitions.keys()) {\n\t\tif (isNamedSchema(identifier)) {\n\t\t\tfriendlyNames.set(identifier, unqualifySchema(identifier));\n\t\t}\n\t}\n\n\tconst declarations: string[] = [];\n\tfor (const [identifier, schema] of definitions) {\n\t\tif (!isNamedSchema(identifier)) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst friendlyName = friendlyNames.get(identifier) ?? unqualifySchema(identifier);\n\t\tconst rendered = renderNamedSchema(identifier, friendlyName, schema);\n\t\tif (rendered === undefined) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst lines: string[] = [];\n\t\tif (rendered.description !== undefined && rendered.description !== \"\") {\n\t\t\tfor (const comment of rendered.description.split(\"\\n\")) {\n\t\t\t\tlines.push(`// ${comment}`);\n\t\t\t}\n\t\t}\n\t\tlines.push(rendered.declaration);\n\t\tdeclarations.push(lines.join(\"\\n\"));\n\t}\n\n\tconst schemaText = declarations.join(\"\\n\\n\");\n\treturn {\n\t\tschemaText: schemaText === \"\" ? \"\" : `${schemaText}\\n`,\n\t\thasHelperMethods,\n\t};\n\n\tfunction renderNamedSchema(\n\t\tidentifier: string,\n\t\tfriendlyName: string,\n\t\tschema: SimpleNodeSchema,\n\t): RenderResult | undefined {\n\t\tswitch (schema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\treturn renderObjectDeclaration(identifier, friendlyName, schema);\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\treturn renderArrayDeclaration(identifier, friendlyName, schema);\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\treturn renderMapDeclaration(identifier, friendlyName, schema);\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\treturn renderRecordDeclaration(identifier, friendlyName, schema);\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\treturn {\n\t\t\t\t\tdeclaration: `type ${friendlyName} = ${renderLeaf(schema.leafKind)};`,\n\t\t\t\t\tdescription: schema.metadata?.description,\n\t\t\t\t};\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction renderObjectDeclaration(\n\t\tdefinition: string,\n\t\tname: string,\n\t\tschema: SimpleObjectNodeSchema,\n\t): RenderResult {\n\t\tconst fieldLines: string[] = [];\n\t\tconst fieldNames = new Set<string>();\n\n\t\tfor (const [fieldName, fieldSchema] of schema.fields) {\n\t\t\tfieldNames.add(fieldName);\n\t\t\tfieldLines.push(...renderFieldLine(fieldName, fieldSchema));\n\t\t}\n\n\t\tconst { methods, properties } = getBoundMembers(definition);\n\t\tensureNoMemberConflicts(definition, fieldNames, methods, properties);\n\t\tfieldLines.push(...renderPropertyLines(properties));\n\t\tfieldLines.push(...renderMethodLines(methods));\n\n\t\tconst members = fieldLines.map((line) => ` ${line}`).join(\"\\n\");\n\t\tconst body = members === \"\" ? \"\" : `\\n${members}`;\n\t\treturn {\n\t\t\tdeclaration: `interface ${name} {${body}\\n}`,\n\t\t\tdescription: schema.metadata?.description,\n\t\t};\n\t}\n\n\tfunction renderArrayDeclaration(\n\t\tdefinition: string,\n\t\tname: string,\n\t\tschema: SimpleArrayNodeSchema,\n\t): RenderResult {\n\t\tconst elementTypes = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\tconst base = `${formatExpression(elementTypes)}[]`;\n\t\tconst binding = renderBindingIntersection(definition);\n\t\treturn {\n\t\t\tdeclaration: `type ${name} = ${base}${binding.suffix};`,\n\t\t\tdescription: describeBinding(schema.metadata?.description, \"array\", binding),\n\t\t};\n\t}\n\n\tfunction renderMapDeclaration(\n\t\tdefinition: string,\n\t\tname: string,\n\t\tschema: SimpleMapNodeSchema,\n\t): RenderResult {\n\t\tconst valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\tconst base = `Map<string, ${valueType.text}>`;\n\t\tconst binding = renderBindingIntersection(definition);\n\t\treturn {\n\t\t\tdeclaration: `type ${name} = ${base}${binding.suffix};`,\n\t\t\tdescription: describeBinding(schema.metadata?.description, \"map\", binding),\n\t\t};\n\t}\n\n\tfunction renderRecordDeclaration(\n\t\tdefinition: string,\n\t\tname: string,\n\t\tschema: SimpleRecordNodeSchema,\n\t): RenderResult {\n\t\tconst valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\tconst base = `Record<string, ${valueType.text}>`;\n\t\tconst binding = renderBindingIntersection(definition);\n\t\treturn {\n\t\t\tdeclaration: `type ${name} = ${base}${binding.suffix};`,\n\t\t\tdescription: describeBinding(schema.metadata?.description, \"record\", binding),\n\t\t};\n\t}\n\n\tfunction renderFieldLine(name: string, field: SimpleFieldSchema): string[] {\n\t\tconst { comment, optional, type } = describeField(field);\n\t\tconst lines: string[] = [];\n\t\tif (comment !== undefined && comment !== \"\") {\n\t\t\tfor (const note of comment.split(\"\\n\")) {\n\t\t\t\tlines.push(`// ${note}`);\n\t\t\t}\n\t\t}\n\t\tlines.push(`${name}${optional ? \"?\" : \"\"}: ${type};`);\n\t\treturn lines;\n\t}\n\n\tfunction describeField(field: SimpleFieldSchema): {\n\t\tcomment?: string;\n\t\toptional: boolean;\n\t\ttype: string;\n\t} {\n\t\tconst allowedTypes = renderAllowedTypes(field.simpleAllowedTypes.keys());\n\t\tconst type = formatExpression(allowedTypes);\n\t\tconst optional = field.kind !== FieldKind.Required;\n\t\tconst customMetadata = field.metadata.custom as\n\t\t\t| Record<string | symbol, unknown>\n\t\t\t| undefined;\n\t\tconst getDefault = customMetadata?.[llmDefault];\n\n\t\tif (getDefault !== undefined) {\n\t\t\tif (typeof getDefault !== \"function\") {\n\t\t\t\tthrow new UsageError(\n\t\t\t\t\t`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (field.kind !== FieldKind.Optional) {\n\t\t\t\tthrow new UsageError(\n\t\t\t\t\t`The ${llmDefault.description} property is only permitted on optional fields.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\toptional,\n\t\t\t\ttype,\n\t\t\t\tcomment:\n\t\t\t\t\t\"Do not populate this field. It will be automatically supplied by the system after insertion.\",\n\t\t\t};\n\t\t}\n\n\t\tif (field.kind === FieldKind.Identifier) {\n\t\t\treturn {\n\t\t\t\toptional: true,\n\t\t\t\ttype,\n\t\t\t\tcomment:\n\t\t\t\t\t\"This is an ID automatically generated by the system. Do not supply it when constructing a new object.\",\n\t\t\t};\n\t\t}\n\n\t\tconst description = field.metadata?.description;\n\t\treturn {\n\t\t\toptional,\n\t\t\ttype,\n\t\t\tcomment: description === undefined || description === \"\" ? undefined : description,\n\t\t};\n\t}\n\n\tfunction renderBindingIntersection(definition: string): BindingIntersectionResult {\n\t\tconst { methods, properties } = getBoundMembers(definition);\n\t\tconst propertyLines = renderPropertyLines(properties);\n\t\tconst methodLines = renderMethodLines(methods);\n\n\t\tif (propertyLines.length === 0 && methodLines.length === 0) {\n\t\t\treturn { hasMethods: false, hasProperties: false, suffix: \"\" };\n\t\t}\n\n\t\tconst lines = [...propertyLines, ...methodLines].map((line) => ` ${line}`);\n\t\tconst suffix = ` & {\\n${lines.join(\"\\n\")}\\n}`;\n\t\treturn {\n\t\t\thasMethods: methodLines.length > 0,\n\t\t\thasProperties: propertyLines.length > 0,\n\t\t\tsuffix,\n\t\t};\n\t}\n\n\tfunction renderMethodLines(methods: Record<string, FunctionWrapper>): string[] {\n\t\tconst lines: string[] = [];\n\t\tfor (const [name, method] of Object.entries(methods)) {\n\t\t\tif (method.description !== undefined && method.description !== \"\") {\n\t\t\t\tfor (const note of method.description.split(\"\\n\")) {\n\t\t\t\t\tlines.push(`// ${note}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tlines.push(formatMethod(name, method));\n\t\t}\n\t\tif (lines.length > 0) {\n\t\t\thasHelperMethods = true;\n\t\t}\n\t\treturn lines;\n\t}\n\n\tfunction getBoundMembers(definition: string): BoundMembers {\n\t\tconst schemaClass = bindableSchemas.get(definition);\n\t\tif (schemaClass === undefined) {\n\t\t\treturn { methods: {}, properties: {} };\n\t\t}\n\t\treturn {\n\t\t\tmethods: getExposedMethods(schemaClass).methods,\n\t\t\tproperties: getExposedProperties(schemaClass).properties,\n\t\t};\n\t}\n\n\tfunction renderAllowedTypes(allowedTypes: Iterable<string>): TypeExpression {\n\t\tconst expressions: TypeExpression[] = [];\n\t\tfor (const identifier of allowedTypes) {\n\t\t\texpressions.push(renderTypeReference(identifier));\n\t\t}\n\t\tif (expressions.length === 0) {\n\t\t\treturn { precedence: TypePrecedence.Object, text: \"never\" };\n\t\t}\n\t\tif (expressions.length === 1) {\n\t\t\treturn expressions[0] ?? { precedence: TypePrecedence.Object, text: \"never\" };\n\t\t}\n\t\treturn {\n\t\t\tprecedence: TypePrecedence.Union,\n\t\t\ttext: expressions\n\t\t\t\t.map((expr) => formatExpression(expr, TypePrecedence.Union))\n\t\t\t\t.join(\" | \"),\n\t\t};\n\t}\n\n\tfunction renderTypeReference(identifier: string): TypeExpression {\n\t\tconst schema = definitions.get(identifier);\n\t\tif (schema === undefined) {\n\t\t\treturn {\n\t\t\t\tprecedence: TypePrecedence.Object,\n\t\t\t\ttext: friendlyNames.get(identifier) ?? unqualifySchema(identifier),\n\t\t\t};\n\t\t}\n\t\tif (isNamedSchema(identifier)) {\n\t\t\treturn {\n\t\t\t\tprecedence: TypePrecedence.Object,\n\t\t\t\ttext: friendlyNames.get(identifier) ?? unqualifySchema(identifier),\n\t\t\t};\n\t\t}\n\t\treturn renderInlineSchema(schema);\n\t}\n\n\tfunction renderInlineSchema(schema: SimpleNodeSchema): TypeExpression {\n\t\tswitch (schema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\treturn renderInlineObject(schema);\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\treturn renderInlineArray(schema);\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\treturn renderInlineMap(schema);\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\treturn renderInlineRecord(schema);\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\treturn { precedence: TypePrecedence.Object, text: renderLeaf(schema.leafKind) };\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn { precedence: TypePrecedence.Object, text: \"unknown\" };\n\t\t\t}\n\t\t}\n\t}\n\n\tfunction renderInlineObject(schema: SimpleObjectNodeSchema): TypeExpression {\n\t\tconst fieldLines: string[] = [];\n\t\tfor (const [fieldName, fieldSchema] of schema.fields) {\n\t\t\tfieldLines.push(...renderFieldLine(fieldName, fieldSchema));\n\t\t}\n\t\tconst members = fieldLines.map((line) => ` ${line}`).join(\"\\n\");\n\t\tconst text =\n\t\t\tmembers === \"\"\n\t\t\t\t? \"{\\n}\"\n\t\t\t\t: `{\n${members}\n}`;\n\t\treturn { precedence: TypePrecedence.Object, text };\n\t}\n\n\tfunction renderInlineArray(schema: SimpleArrayNodeSchema): TypeExpression {\n\t\tconst elementTypes = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\treturn {\n\t\t\tprecedence: TypePrecedence.Object,\n\t\t\ttext: `${formatExpression(elementTypes)}[]`,\n\t\t};\n\t}\n\n\tfunction renderInlineMap(schema: SimpleMapNodeSchema): TypeExpression {\n\t\tconst valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\treturn {\n\t\t\tprecedence: TypePrecedence.Object,\n\t\t\ttext: `Map<string, ${valueType.text}>`,\n\t\t};\n\t}\n\n\tfunction renderInlineRecord(schema: SimpleRecordNodeSchema): TypeExpression {\n\t\tconst valueType = renderAllowedTypes(schema.simpleAllowedTypes.keys());\n\t\treturn {\n\t\t\tprecedence: TypePrecedence.Object,\n\t\t\ttext: `Record<string, ${valueType.text}>`,\n\t\t};\n\t}\n}\n\nfunction describeBinding(\n\tdescription: string | undefined,\n\ttypeLabel: \"array\" | \"map\" | \"record\",\n\tbinding: BindingIntersectionResult,\n): string | undefined {\n\tlet note = \"\";\n\tif (binding.hasMethods && binding.hasProperties) {\n\t\tnote = `Note: this ${typeLabel} has custom user-defined methods and properties directly on it.`;\n\t} else if (binding.hasMethods) {\n\t\tnote = `Note: this ${typeLabel} has custom user-defined methods directly on it.`;\n\t} else if (binding.hasProperties) {\n\t\tnote = `Note: this ${typeLabel} has custom user-defined properties directly on it.`;\n\t}\n\n\tif (note === \"\") {\n\t\treturn description === undefined || description === \"\" ? undefined : description;\n\t}\n\tif (description === undefined || description === \"\") {\n\t\treturn note;\n\t}\n\treturn `${description} - ${note}`;\n}\n\nfunction renderPropertyLines(properties: Record<string, PropertyDef>): string[] {\n\tconst lines: string[] = [];\n\tfor (const [name, property] of Object.entries(properties)) {\n\t\tif (property.description !== undefined && property.description !== \"\") {\n\t\t\tfor (const note of property.description.split(\"\\n\")) {\n\t\t\t\tlines.push(`// ${note}`);\n\t\t\t}\n\t\t}\n\t\tconst modifier = property.readOnly ? \"readonly \" : \"\";\n\t\tlines.push(`${modifier}${name}: ${renderZodType(property.schema)};`);\n\t}\n\treturn lines;\n}\n\nfunction formatMethod(name: string, method: FunctionWrapper): string {\n\tconst args: string[] = [];\n\tfor (const [argName, argType] of method.args) {\n\t\tconst { innerType, optional } = unwrapOptional(argType);\n\t\tconst renderedType = renderZodType(innerType);\n\t\targs.push(`${argName}${optional ? \"?\" : \"\"}: ${renderedType}`);\n\t}\n\tif (method.rest !== null) {\n\t\targs.push(`...rest: ${renderZodType(method.rest)}[]`);\n\t}\n\treturn `${name}(${args.join(\", \")}): ${renderZodType(method.returns)};`;\n}\n\nfunction renderLeaf(leafKind: ValueSchema): string {\n\tswitch (leafKind) {\n\t\tcase ValueSchema.Boolean: {\n\t\t\treturn \"boolean\";\n\t\t}\n\t\tcase ValueSchema.Number: {\n\t\t\treturn \"number\";\n\t\t}\n\t\tcase ValueSchema.String: {\n\t\t\treturn \"string\";\n\t\t}\n\t\tcase ValueSchema.Null: {\n\t\t\treturn \"null\";\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported leaf kind ${NodeKind[leafKind]}.`);\n\t\t}\n\t}\n}\n\nfunction formatExpression(\n\texpression: TypeExpression,\n\tminPrecedence: TypePrecedence = TypePrecedence.Object,\n): string {\n\treturn expression.precedence < minPrecedence ? `(${expression.text})` : expression.text;\n}\n\n/**\n * Detects optional zod wrappers so argument lists can keep TypeScript optional markers in sync.\n */\nfunction unwrapOptional(type: z.ZodTypeAny): { innerType: z.ZodTypeAny; optional: boolean } {\n\tif (type instanceof z.ZodOptional) {\n\t\tconst inner = type.unwrap() as z.ZodTypeAny;\n\t\treturn { innerType: inner, optional: true };\n\t}\n\treturn { innerType: type, optional: false };\n}\n\n/**\n * Verifies that helper members do not clobber structural fields and fails fast if they do.\n */\nfunction ensureNoMemberConflicts(\n\tdefinition: string,\n\tfieldNames: ReadonlySet<string>,\n\tmethods: Record<string, FunctionWrapper>,\n\tproperties: Record<string, PropertyDef>,\n): void {\n\tfor (const name of Object.keys(methods)) {\n\t\tif (fieldNames.has(name)) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Method ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t);\n\t\t}\n\t}\n\tfor (const name of Object.keys(properties)) {\n\t\tif (fieldNames.has(name)) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Property ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\n/**\n * Converts schema metadata into TypeScript declarations suitable for prompt inclusion.\n */\nfunction renderZodType(type: z.ZodTypeAny): string {\n\treturn renderZodTypeScript(type, getFriendlyName, instanceOfs);\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree/alpha";
|
|
6
|
+
import { ObjectNodeSchema } from "@fluidframework/tree/alpha";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
/**
|
|
9
|
+
* Converts Zod schema definitions into TypeScript declaration text.
|
|
10
|
+
*/
|
|
11
|
+
export declare function renderZodTypeScript(zodType: z.ZodTypeAny, getFriendlyName: (schema: TreeNodeSchema) => string, instanceOfLookup: WeakMap<z.ZodTypeAny, ObjectNodeSchema>): string;
|
|
12
|
+
/**
|
|
13
|
+
* Create a Zod schema for a SharedTree schema class.
|
|
14
|
+
* @alpha
|
|
15
|
+
*/
|
|
16
|
+
export declare function instanceOf<T extends TreeNodeSchemaClass>(schema: T): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;
|
|
17
|
+
/**
|
|
18
|
+
* A lookup from Zod instanceOf schemas to their corresponding ObjectNodeSchema.
|
|
19
|
+
*/
|
|
20
|
+
export declare const instanceOfs: WeakMap<z.ZodTypeAny, ObjectNodeSchema<string, import("@fluidframework/tree/alpha").RestrictiveStringRecord<import("@fluidframework/tree/alpha").ImplicitFieldSchema>, boolean, unknown>>;
|
|
21
|
+
//# sourceMappingURL=renderZodTypeScript.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderZodTypeScript.d.ts","sourceRoot":"","sources":["../src/renderZodTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtF,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,CAAC,CAAC,UAAU,EACrB,eAAe,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,MAAM,EACnD,gBAAgB,EAAE,OAAO,CAAC,CAAC,CAAC,UAAU,EAAE,gBAAgB,CAAC,GACvD,MAAM,CAyPR;AA4BD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,mBAAmB,EACvD,MAAM,EAAE,CAAC,GACP,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAO3D;AAED;;GAEG;AACH,eAAO,MAAM,WAAW,2LAAgD,CAAC"}
|