@fluidframework/tree-agent 2.74.0 → 2.81.0
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/CHANGELOG.md +99 -0
- package/api-report/tree-agent.alpha.api.md +195 -20
- package/dist/alpha.d.ts +31 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/methodBinding.d.ts +54 -10
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js.map +1 -1
- package/dist/propertyBinding.d.ts +52 -2
- package/dist/propertyBinding.d.ts.map +1 -1
- package/dist/propertyBinding.js +28 -3
- package/dist/propertyBinding.js.map +1 -1
- package/dist/renderSchemaTypeScript.d.ts.map +1 -1
- package/dist/renderSchemaTypeScript.js +24 -9
- package/dist/renderSchemaTypeScript.js.map +1 -1
- package/dist/renderTypeFactoryTypeScript.d.ts +13 -0
- package/dist/renderTypeFactoryTypeScript.d.ts.map +1 -0
- package/dist/renderTypeFactoryTypeScript.js +290 -0
- package/dist/renderTypeFactoryTypeScript.js.map +1 -0
- package/dist/subtree.d.ts.map +1 -1
- package/dist/subtree.js +4 -4
- package/dist/subtree.js.map +1 -1
- package/dist/treeAgentTypes.d.ts +430 -0
- package/dist/treeAgentTypes.d.ts.map +1 -0
- package/dist/treeAgentTypes.js +227 -0
- package/dist/treeAgentTypes.js.map +1 -0
- package/dist/utils.d.ts +0 -4
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +2 -9
- package/dist/utils.js.map +1 -1
- package/eslint.config.mts +4 -4
- package/lib/alpha.d.ts +31 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/methodBinding.d.ts +54 -10
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/propertyBinding.d.ts +52 -2
- package/lib/propertyBinding.d.ts.map +1 -1
- package/lib/propertyBinding.js +28 -3
- package/lib/propertyBinding.js.map +1 -1
- package/lib/renderSchemaTypeScript.d.ts.map +1 -1
- package/lib/renderSchemaTypeScript.js +24 -9
- package/lib/renderSchemaTypeScript.js.map +1 -1
- package/lib/renderTypeFactoryTypeScript.d.ts +13 -0
- package/lib/renderTypeFactoryTypeScript.d.ts.map +1 -0
- package/lib/renderTypeFactoryTypeScript.js +285 -0
- package/lib/renderTypeFactoryTypeScript.js.map +1 -0
- package/lib/subtree.d.ts.map +1 -1
- package/lib/subtree.js +4 -4
- package/lib/subtree.js.map +1 -1
- package/lib/treeAgentTypes.d.ts +430 -0
- package/lib/treeAgentTypes.d.ts.map +1 -0
- package/lib/treeAgentTypes.js +223 -0
- package/lib/treeAgentTypes.js.map +1 -0
- package/lib/utils.d.ts +0 -4
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +2 -8
- package/lib/utils.js.map +1 -1
- package/package.json +17 -17
- package/src/index.ts +36 -0
- package/src/methodBinding.ts +94 -15
- package/src/propertyBinding.ts +66 -9
- package/src/renderSchemaTypeScript.ts +32 -10
- package/src/renderTypeFactoryTypeScript.ts +339 -0
- package/src/subtree.ts +6 -5
- package/src/treeAgentTypes.ts +611 -0
- package/src/utils.ts +2 -9
- package/.eslintrc.cjs +0 -48
package/lib/propertyBinding.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import { instanceOf } from "./renderZodTypeScript.js";
|
|
6
|
+
import { isTypeFactoryType } from "./treeAgentTypes.js";
|
|
6
7
|
/**
|
|
7
8
|
* A symbol used to expose properties to the LLM.
|
|
8
9
|
* @alpha
|
|
@@ -13,7 +14,23 @@ export const exposePropertiesSymbol = Symbol.for("@fluidframework/tree-agent/exp
|
|
|
13
14
|
* @alpha
|
|
14
15
|
*/
|
|
15
16
|
export class PropertyDef {
|
|
16
|
-
constructor(
|
|
17
|
+
constructor(
|
|
18
|
+
/**
|
|
19
|
+
* The name of the property.
|
|
20
|
+
*/
|
|
21
|
+
name,
|
|
22
|
+
/**
|
|
23
|
+
* Optional description of the property.
|
|
24
|
+
*/
|
|
25
|
+
description,
|
|
26
|
+
/**
|
|
27
|
+
* The schema defining the property's type (either Zod or TypeFactory).
|
|
28
|
+
*/
|
|
29
|
+
schema,
|
|
30
|
+
/**
|
|
31
|
+
* Whether the property is readonly.
|
|
32
|
+
*/
|
|
33
|
+
readOnly) {
|
|
17
34
|
this.name = name;
|
|
18
35
|
this.description = description;
|
|
19
36
|
this.schema = schema;
|
|
@@ -26,11 +43,19 @@ class ExposedPropertiesI {
|
|
|
26
43
|
this.properties = {};
|
|
27
44
|
this.referencedTypes = new Set();
|
|
28
45
|
}
|
|
29
|
-
exposeProperty(schema, name,
|
|
46
|
+
exposeProperty(schema, name, defOrType) {
|
|
30
47
|
if (schema !== this.schemaClass) {
|
|
31
48
|
throw new Error('Must expose properties on the "this" schema class');
|
|
32
49
|
}
|
|
33
|
-
|
|
50
|
+
// Handle TypeFactoryType (simple case - type passed directly)
|
|
51
|
+
if (isTypeFactoryType(defOrType)) {
|
|
52
|
+
this.properties[name] = new PropertyDef(name, undefined, defOrType, false);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// Handle object with schema property (works for both Zod and TypeFactory)
|
|
56
|
+
const def = defOrType;
|
|
57
|
+
this.properties[name] = new PropertyDef(name, def.description, def.schema, def.readOnly === true);
|
|
58
|
+
}
|
|
34
59
|
}
|
|
35
60
|
instanceOf(schema) {
|
|
36
61
|
this.referencedTypes.add(schema);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propertyBinding.js","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAkB,MAAM,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0EAA0E;YAC1E,MAAM,GAAG,GAAG,SAIX,CAAC;YACF,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,WAAW,CACtC,IAAI,EACJ,GAAG,CAAC,WAAW,EACf,GAAG,CAAC,MAAM,EACV,GAAG,CAAC,QAAQ,KAAK,IAAI,CACrB,CAAC;QACH,CAAC;IACF,CAAC;IAEM,UAAU,CAChB,MAAS;QAET,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjC,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,oBAAoB,CAAC,WAA2B;QAI7D,MAAM,OAAO,GAAG,IAAI,kBAAkB,CAAC,WAAW,CAAC,CAAC;QACpD,MAAM,WAAW,GAAG,WAA4C,CAAC;QACjE,IAAI,WAAW,CAAC,sBAAsB,CAAC,KAAK,SAAS,EAAE,CAAC;YACvD,WAAW,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO;YACN,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,eAAe,EAAE,OAAO,CAAC,eAAe;SACxC,CAAC;IACH,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,WAA2B;IAI/D,OAAO,kBAAkB,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;AAC7D,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-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from \"zod\";\n\nimport type { BindableSchema, Ctor } from \"./methodBinding.js\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\nimport type { TypeFactoryType } from \"./treeAgentTypes.js\";\nimport { isTypeFactoryType } from \"./treeAgentTypes.js\";\n\n/**\n * A symbol used to expose properties to the LLM.\n * @alpha\n */\nexport const exposePropertiesSymbol: unique symbol = Symbol.for(\n\t\"@fluidframework/tree-agent/exposeProperties\",\n);\n\n/**\n * Set of property keys from `T` that are not methods.\n * @alpha\n */\nexport type ExposableKeys<T> = {\n\t[K in keyof T]?: T[K] extends (...args: any[]) => any ? never : K;\n}[keyof T];\n\n/**\n * Type-level equality test used as a helper to evaluate readonly keys.\n * - If X and Y are the same type, it evaluates to A.\n * - If X and Y are different, it evaluates to B.\n * @alpha\n */\nexport type IfEquals<X, Y, A = true, B = false> = (<T>() => T extends X ? 1 : 2) extends <\n\tT,\n>() => T extends Y ? 1 : 2\n\t? A\n\t: B;\n\n/**\n * Produces a union of keys of `T` which are readonly.\n * @alpha\n */\nexport type ReadonlyKeys<T> = {\n\t// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\n\t[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>;\n}[keyof T];\n\n/**\n * Type to enforce `readOnly: true` for readonly properties.\n * @alpha\n */\nexport type ReadOnlyRequirement<TObj, K extends keyof TObj> = {\n\t[P in K]-?: P extends ReadonlyKeys<TObj> ? { readOnly: true } : { readOnly?: false };\n}[K];\n\n/**\n * Emits compile-time error when there is a type mismatch.\n * @alpha\n */\nexport type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]\n\t? unknown\n\t: {\n\t\t\t__error__: \"Zod schema value type does not match the property's declared type\";\n\t\t\texpected: Expected;\n\t\t\treceived: Received;\n\t\t};\n\n/**\n * A property definition class that describes the structure of the property\n * @alpha\n */\nexport class PropertyDef {\n\tpublic constructor(\n\t\t/**\n\t\t * The name of the property.\n\t\t */\n\t\tpublic readonly name: string,\n\t\t/**\n\t\t * Optional description of the property.\n\t\t */\n\t\tpublic readonly description: string | undefined,\n\t\t/**\n\t\t * The schema defining the property's type (either Zod or TypeFactory).\n\t\t */\n\t\tpublic readonly schema: ZodTypeAny | TypeFactoryType,\n\t\t/**\n\t\t * Whether the property is readonly.\n\t\t */\n\t\tpublic readonly readOnly: boolean,\n\t) {}\n}\n\n/**\n * An interface for exposing properties of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedProperties {\n\t/**\n\t * Expose a property with Zod type checking.\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void;\n\n\t/**\n\t * Expose a property with type factory type and metadata.\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TypeFactoryType; description?: string; readOnly?: boolean },\n\t): void;\n\n\t/**\n\t * Expose a property with type factory type (simple form).\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t>(schema: S, name: K, tfType: TypeFactoryType): void;\n\n\t/**\n\t * Create a Zod type that references a SharedTree schema class.\n\t */\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * An interface that SharedTree schema classes should implement to expose their properties to the LLM.\n *\n * @remarks\n * The `getExposedProperties` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedProperties {\n\t/**\n\t * Static method that exposes properties of this schema class to an agent.\n\t */\n\t[exposePropertiesSymbol]?(properties: ExposedProperties): void;\n}\n\nclass ExposedPropertiesI implements ExposedProperties {\n\tprivate readonly properties: Record<string, PropertyDef> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic exposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdefOrType:\n\t\t\t| ({ schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>)\n\t\t\t| TypeFactoryType,\n\t): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose properties on the \"this\" schema class');\n\t\t}\n\n\t\t// Handle TypeFactoryType (simple case - type passed directly)\n\t\tif (isTypeFactoryType(defOrType)) {\n\t\t\tthis.properties[name] = new PropertyDef(name, undefined, defOrType, false);\n\t\t} else {\n\t\t\t// Handle object with schema property (works for both Zod and TypeFactory)\n\t\t\tconst def = defOrType as {\n\t\t\t\tschema: TZ | TypeFactoryType;\n\t\t\t\tdescription?: string;\n\t\t\t\treadOnly?: boolean;\n\t\t\t};\n\t\t\tthis.properties[name] = new PropertyDef(\n\t\t\t\tname,\n\t\t\t\tdef.description,\n\t\t\t\tdef.schema,\n\t\t\t\tdef.readOnly === true,\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedProperties(schemaClass: BindableSchema): {\n\t\tproperties: Record<string, PropertyDef>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposed = new ExposedPropertiesI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedProperties;\n\t\tif (extractable[exposePropertiesSymbol] !== undefined) {\n\t\t\textractable[exposePropertiesSymbol](exposed);\n\t\t}\n\t\treturn {\n\t\t\tproperties: exposed.properties,\n\t\t\treferencedTypes: exposed.referencedTypes,\n\t\t};\n\t}\n}\n\n/**\n * Get the exposed properties of a schema class.\n * @param schemaClass - The schema class to extract properties from.\n * @returns A record of property names and their corresponding Zod types.\n */\nexport function getExposedProperties(schemaClass: BindableSchema): {\n\tproperties: Record<string, PropertyDef>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedPropertiesI.getExposedProperties(schemaClass);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderSchemaTypeScript.d.ts","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAIX,gBAAgB,EAGhB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"renderSchemaTypeScript.d.ts","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAIX,gBAAgB,EAGhB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,oBAAoB,CAAC;AAuC1E;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAClD,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GAC1C,4BAA4B,CAoV9B"}
|
|
@@ -7,8 +7,10 @@ import { FieldKind, NodeKind, ValueSchema } from "@fluidframework/tree/internal"
|
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
import { getExposedMethods } from "./methodBinding.js";
|
|
9
9
|
import { getExposedProperties } from "./propertyBinding.js";
|
|
10
|
-
import {
|
|
10
|
+
import { instanceOfsTypeFactory, renderTypeFactoryTypeScript, } from "./renderTypeFactoryTypeScript.js";
|
|
11
11
|
import { instanceOfs, renderZodTypeScript } from "./renderZodTypeScript.js";
|
|
12
|
+
import { isTypeFactoryType } from "./treeAgentTypes.js";
|
|
13
|
+
import { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from "./utils.js";
|
|
12
14
|
var TypePrecedence;
|
|
13
15
|
(function (TypePrecedence) {
|
|
14
16
|
TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
|
|
@@ -187,7 +189,9 @@ export function renderSchemaTypeScript(definitions, bindableSchemas) {
|
|
|
187
189
|
lines.push(`// ${note}`);
|
|
188
190
|
}
|
|
189
191
|
}
|
|
190
|
-
|
|
192
|
+
const methodString = formatMethod(name, method);
|
|
193
|
+
const methodLines = methodString.split("\n");
|
|
194
|
+
lines.push(...methodLines);
|
|
191
195
|
}
|
|
192
196
|
if (lines.length > 0) {
|
|
193
197
|
hasHelperMethods = true;
|
|
@@ -323,7 +327,11 @@ function renderPropertyLines(properties) {
|
|
|
323
327
|
}
|
|
324
328
|
}
|
|
325
329
|
const modifier = property.readOnly ? "readonly " : "";
|
|
326
|
-
|
|
330
|
+
const typeString = renderType(property.schema, 0);
|
|
331
|
+
const propertyLine = `${modifier}${name}: ${typeString};`;
|
|
332
|
+
// Split multi-line type strings and add to lines array
|
|
333
|
+
const propertyLines = propertyLine.split("\n");
|
|
334
|
+
lines.push(...propertyLines);
|
|
327
335
|
}
|
|
328
336
|
return lines;
|
|
329
337
|
}
|
|
@@ -331,13 +339,13 @@ function formatMethod(name, method) {
|
|
|
331
339
|
const args = [];
|
|
332
340
|
for (const [argName, argType] of method.args) {
|
|
333
341
|
const { innerType, optional } = unwrapOptional(argType);
|
|
334
|
-
const renderedType =
|
|
342
|
+
const renderedType = renderType(innerType, 0);
|
|
335
343
|
args.push(`${argName}${optional ? "?" : ""}: ${renderedType}`);
|
|
336
344
|
}
|
|
337
345
|
if (method.rest !== null) {
|
|
338
|
-
args.push(`...rest: ${
|
|
346
|
+
args.push(`...rest: ${renderType(method.rest, 0)}[]`);
|
|
339
347
|
}
|
|
340
|
-
return `${name}(${args.join(", ")}): ${
|
|
348
|
+
return `${name}(${args.join(", ")}): ${renderType(method.returns, 0)};`;
|
|
341
349
|
}
|
|
342
350
|
function renderLeaf(leafKind) {
|
|
343
351
|
switch (leafKind) {
|
|
@@ -365,6 +373,11 @@ function formatExpression(expression, minPrecedence = 2 /* TypePrecedence.Object
|
|
|
365
373
|
* Detects optional zod wrappers so argument lists can keep TypeScript optional markers in sync.
|
|
366
374
|
*/
|
|
367
375
|
function unwrapOptional(type) {
|
|
376
|
+
// Handle type factory optional type
|
|
377
|
+
if (isTypeFactoryType(type) && type._kind === "optional") {
|
|
378
|
+
return { innerType: type.innerType, optional: true };
|
|
379
|
+
}
|
|
380
|
+
// Handle Zod optional type
|
|
368
381
|
if (type instanceof z.ZodOptional) {
|
|
369
382
|
const inner = type.unwrap();
|
|
370
383
|
return { innerType: inner, optional: true };
|
|
@@ -387,9 +400,11 @@ function ensureNoMemberConflicts(definition, fieldNames, methods, properties) {
|
|
|
387
400
|
}
|
|
388
401
|
}
|
|
389
402
|
/**
|
|
390
|
-
*
|
|
403
|
+
* Dispatches to the correct renderer based on whether the type is Zod or type factory.
|
|
391
404
|
*/
|
|
392
|
-
function
|
|
393
|
-
return
|
|
405
|
+
function renderType(type, indentLevel = 0) {
|
|
406
|
+
return isTypeFactoryType(type)
|
|
407
|
+
? renderTypeFactoryTypeScript(type, getFriendlyName, instanceOfsTypeFactory, indentLevel)
|
|
408
|
+
: renderZodTypeScript(type, getFriendlyName, instanceOfs);
|
|
394
409
|
}
|
|
395
410
|
//# sourceMappingURL=renderSchemaTypeScript.js.map
|
|
@@ -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C,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"]}
|
|
1
|
+
{"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,CAAC,CAAC,WAAW,EAAE,CAAC;QACnC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAkB,CAAC;QAC5C,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,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,UAAU,CAAC,IAAoC,EAAE,cAAsB,CAAC;IAChF,OAAO,iBAAiB,CAAC,IAAI,CAAC;QAC7B,CAAC,CAAC,2BAA2B,CAAC,IAAI,EAAE,eAAe,EAAE,sBAAsB,EAAE,WAAW,CAAC;QACzF,CAAC,CAAC,mBAAmB,CAAC,IAAI,EAAE,eAAe,EAAE,WAAW,CAAC,CAAC;AAC5D,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 {\n\tinstanceOfsTypeFactory,\n\trenderTypeFactoryTypeScript,\n} from \"./renderTypeFactoryTypeScript.js\";\nimport { instanceOfs, renderZodTypeScript } from \"./renderZodTypeScript.js\";\nimport type { TypeFactoryOptional, TypeFactoryType } from \"./treeAgentTypes.js\";\nimport { isTypeFactoryType } from \"./treeAgentTypes.js\";\nimport { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from \"./utils.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\tconst methodString = formatMethod(name, method);\n\t\t\tconst methodLines = methodString.split(\"\\n\");\n\t\t\tlines.push(...methodLines);\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\tconst typeString = renderType(property.schema, 0);\n\t\tconst propertyLine = `${modifier}${name}: ${typeString};`;\n\t\t// Split multi-line type strings and add to lines array\n\t\tconst propertyLines = propertyLine.split(\"\\n\");\n\t\tlines.push(...propertyLines);\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 = renderType(innerType, 0);\n\t\targs.push(`${argName}${optional ? \"?\" : \"\"}: ${renderedType}`);\n\t}\n\tif (method.rest !== null) {\n\t\targs.push(`...rest: ${renderType(method.rest, 0)}[]`);\n\t}\n\treturn `${name}(${args.join(\", \")}): ${renderType(method.returns, 0)};`;\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 | TypeFactoryType): {\n\tinnerType: z.ZodTypeAny | TypeFactoryType;\n\toptional: boolean;\n} {\n\t// Handle type factory optional type\n\tif (isTypeFactoryType(type) && type._kind === \"optional\") {\n\t\treturn { innerType: (type as TypeFactoryOptional).innerType, optional: true };\n\t}\n\t// Handle Zod optional type\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 * Dispatches to the correct renderer based on whether the type is Zod or type factory.\n */\nfunction renderType(type: z.ZodTypeAny | TypeFactoryType, indentLevel: number = 0): string {\n\treturn isTypeFactoryType(type)\n\t\t? renderTypeFactoryTypeScript(type, getFriendlyName, instanceOfsTypeFactory, indentLevel)\n\t\t: renderZodTypeScript(type, getFriendlyName, instanceOfs);\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { ObjectNodeSchema, TreeNodeSchema } from "@fluidframework/tree/alpha";
|
|
6
|
+
import type { TypeFactoryType } from "./treeAgentTypes.js";
|
|
7
|
+
export { instanceOfsTypeFactory } from "./treeAgentTypes.js";
|
|
8
|
+
/**
|
|
9
|
+
* Converts type factory type definitions into TypeScript declaration text.
|
|
10
|
+
* @alpha
|
|
11
|
+
*/
|
|
12
|
+
export declare function renderTypeFactoryTypeScript(typeFactoryType: TypeFactoryType, getFriendlyName: (schema: TreeNodeSchema) => string, instanceOfLookup: WeakMap<TypeFactoryType, ObjectNodeSchema>, initialIndent?: number): string;
|
|
13
|
+
//# sourceMappingURL=renderTypeFactoryTypeScript.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderTypeFactoryTypeScript.d.ts","sourceRoot":"","sources":["../src/renderTypeFactoryTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAEnF,OAAO,KAAK,EACX,eAAe,EAaf,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE7D;;;GAGG;AACH,wBAAgB,2BAA2B,CAC1C,eAAe,EAAE,eAAe,EAChC,eAAe,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,MAAM,EACnD,gBAAgB,EAAE,OAAO,CAAC,eAAe,EAAE,gBAAgB,CAAC,EAC5D,aAAa,GAAE,MAAU,GACvB,MAAM,CA2RR"}
|
|
@@ -0,0 +1,285 @@
|
|
|
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
|
+
export { instanceOfsTypeFactory } from "./treeAgentTypes.js";
|
|
7
|
+
/**
|
|
8
|
+
* Converts type factory type definitions into TypeScript declaration text.
|
|
9
|
+
* @alpha
|
|
10
|
+
*/
|
|
11
|
+
export function renderTypeFactoryTypeScript(typeFactoryType, getFriendlyName, instanceOfLookup, initialIndent = 0) {
|
|
12
|
+
let result = "";
|
|
13
|
+
let startOfLine = false;
|
|
14
|
+
let indent = initialIndent;
|
|
15
|
+
appendType(typeFactoryType, 0 /* TypePrecedence.Union */);
|
|
16
|
+
return result;
|
|
17
|
+
function appendType(type, minPrecedence = 2 /* TypePrecedence.Object */) {
|
|
18
|
+
const shouldParenthesize = getTypePrecedence(type) < minPrecedence;
|
|
19
|
+
if (shouldParenthesize) {
|
|
20
|
+
append("(");
|
|
21
|
+
}
|
|
22
|
+
appendTypeDefinition(type);
|
|
23
|
+
if (shouldParenthesize) {
|
|
24
|
+
append(")");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function append(s) {
|
|
28
|
+
if (startOfLine) {
|
|
29
|
+
result += " ".repeat(indent);
|
|
30
|
+
startOfLine = false;
|
|
31
|
+
}
|
|
32
|
+
result += s;
|
|
33
|
+
}
|
|
34
|
+
function appendNewLine() {
|
|
35
|
+
append("\n");
|
|
36
|
+
startOfLine = true;
|
|
37
|
+
}
|
|
38
|
+
function appendTypeDefinition(type) {
|
|
39
|
+
switch (type._kind) {
|
|
40
|
+
case "string": {
|
|
41
|
+
append("string");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
case "number": {
|
|
45
|
+
append("number");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
case "boolean": {
|
|
49
|
+
append("boolean");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
case "date": {
|
|
53
|
+
append("Date");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
case "void": {
|
|
57
|
+
append("void");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
case "undefined": {
|
|
61
|
+
append("undefined");
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
case "null": {
|
|
65
|
+
append("null");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
case "unknown": {
|
|
69
|
+
append("unknown");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
case "array": {
|
|
73
|
+
appendArrayType(type);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
case "promise": {
|
|
77
|
+
appendPromiseType(type);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
case "object": {
|
|
81
|
+
appendObjectType(type);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
case "union": {
|
|
85
|
+
appendUnionTypes(type.options, 0 /* TypePrecedence.Union */);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
case "intersection": {
|
|
89
|
+
appendIntersectionTypes(type.types, 1 /* TypePrecedence.Intersection */);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
case "tuple": {
|
|
93
|
+
appendTupleType(type);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
case "record": {
|
|
97
|
+
appendRecordType(type);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
case "map": {
|
|
101
|
+
appendMapType(type);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
case "literal": {
|
|
105
|
+
appendLiteral(type.value);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
case "optional": {
|
|
109
|
+
appendUnionTypes([type.innerType, { _kind: "undefined" }], 0 /* TypePrecedence.Union */);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
case "readonly": {
|
|
113
|
+
appendReadonlyType(type);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
case "function": {
|
|
117
|
+
appendFunctionType(type);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
case "instanceof": {
|
|
121
|
+
const schema = instanceOfLookup.get(type);
|
|
122
|
+
if (schema === undefined) {
|
|
123
|
+
throw new UsageError("instanceof type not found in lookup - this typically indicates the type was not created via typeFactory.instanceOf");
|
|
124
|
+
}
|
|
125
|
+
append(getFriendlyName(schema));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
default: {
|
|
129
|
+
throw new UsageError(`Unsupported type when formatting helper types: ${String(type._kind ?? "unknown")}. Expected one of: string, number, boolean, void, undefined, null, unknown, array, object, union, tuple, record, map, literal, optional, readonly, instanceof.`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function appendArrayType(arrayType) {
|
|
134
|
+
appendType(arrayType.element, 2 /* TypePrecedence.Object */);
|
|
135
|
+
append("[]");
|
|
136
|
+
}
|
|
137
|
+
function appendPromiseType(promiseType) {
|
|
138
|
+
append("Promise<");
|
|
139
|
+
appendType(promiseType.innerType, 0 /* TypePrecedence.Union */);
|
|
140
|
+
append(">");
|
|
141
|
+
}
|
|
142
|
+
function appendObjectType(objectType) {
|
|
143
|
+
append("{");
|
|
144
|
+
appendNewLine();
|
|
145
|
+
indent++;
|
|
146
|
+
for (const [name, propertyType] of Object.entries(objectType.shape)) {
|
|
147
|
+
append(name);
|
|
148
|
+
if (propertyType._kind === "optional") {
|
|
149
|
+
append("?");
|
|
150
|
+
append(": ");
|
|
151
|
+
appendType(propertyType.innerType);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
append(": ");
|
|
155
|
+
appendType(propertyType);
|
|
156
|
+
}
|
|
157
|
+
append(";");
|
|
158
|
+
appendNewLine();
|
|
159
|
+
}
|
|
160
|
+
indent--;
|
|
161
|
+
append("}");
|
|
162
|
+
}
|
|
163
|
+
function appendUnionTypes(types, minPrecedence) {
|
|
164
|
+
let first = true;
|
|
165
|
+
for (const innerType of types) {
|
|
166
|
+
if (!first) {
|
|
167
|
+
append(" | ");
|
|
168
|
+
}
|
|
169
|
+
appendType(innerType, minPrecedence);
|
|
170
|
+
first = false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function appendIntersectionTypes(types, minPrecedence) {
|
|
174
|
+
let first = true;
|
|
175
|
+
for (const innerType of types) {
|
|
176
|
+
if (!first) {
|
|
177
|
+
append(" & ");
|
|
178
|
+
}
|
|
179
|
+
appendType(innerType, minPrecedence);
|
|
180
|
+
first = false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function appendTupleType(tupleType) {
|
|
184
|
+
append("[");
|
|
185
|
+
let first = true;
|
|
186
|
+
for (const innerType of tupleType.items) {
|
|
187
|
+
if (!first) {
|
|
188
|
+
append(", ");
|
|
189
|
+
}
|
|
190
|
+
if (innerType._kind === "optional") {
|
|
191
|
+
appendType(innerType.innerType, 2 /* TypePrecedence.Object */);
|
|
192
|
+
append("?");
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
appendType(innerType);
|
|
196
|
+
}
|
|
197
|
+
first = false;
|
|
198
|
+
}
|
|
199
|
+
if (tupleType.rest !== undefined) {
|
|
200
|
+
if (!first) {
|
|
201
|
+
append(", ");
|
|
202
|
+
}
|
|
203
|
+
append("...");
|
|
204
|
+
appendType(tupleType.rest, 2 /* TypePrecedence.Object */);
|
|
205
|
+
append("[]");
|
|
206
|
+
}
|
|
207
|
+
append("]");
|
|
208
|
+
}
|
|
209
|
+
function appendRecordType(recordType) {
|
|
210
|
+
append("Record<");
|
|
211
|
+
appendType(recordType.keyType, 0 /* TypePrecedence.Union */);
|
|
212
|
+
append(", ");
|
|
213
|
+
appendType(recordType.valueType, 0 /* TypePrecedence.Union */);
|
|
214
|
+
append(">");
|
|
215
|
+
}
|
|
216
|
+
function appendMapType(mapType) {
|
|
217
|
+
append("Map<");
|
|
218
|
+
appendType(mapType.keyType, 0 /* TypePrecedence.Union */);
|
|
219
|
+
append(", ");
|
|
220
|
+
appendType(mapType.valueType, 0 /* TypePrecedence.Union */);
|
|
221
|
+
append(">");
|
|
222
|
+
}
|
|
223
|
+
function appendLiteral(value) {
|
|
224
|
+
append(JSON.stringify(value));
|
|
225
|
+
}
|
|
226
|
+
function appendReadonlyType(readonlyType) {
|
|
227
|
+
append("Readonly<");
|
|
228
|
+
appendType(readonlyType.innerType);
|
|
229
|
+
append(">");
|
|
230
|
+
}
|
|
231
|
+
function appendFunctionType(functionType) {
|
|
232
|
+
append("(");
|
|
233
|
+
let first = true;
|
|
234
|
+
for (const param of functionType.parameters) {
|
|
235
|
+
if (!first) {
|
|
236
|
+
append(", ");
|
|
237
|
+
}
|
|
238
|
+
const [name, type] = param;
|
|
239
|
+
append(name);
|
|
240
|
+
if (type._kind === "optional") {
|
|
241
|
+
append("?");
|
|
242
|
+
append(": ");
|
|
243
|
+
appendType(type.innerType, 0 /* TypePrecedence.Union */);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
append(": ");
|
|
247
|
+
appendType(type, 0 /* TypePrecedence.Union */);
|
|
248
|
+
}
|
|
249
|
+
first = false;
|
|
250
|
+
}
|
|
251
|
+
if (functionType.restParameter !== undefined) {
|
|
252
|
+
if (!first) {
|
|
253
|
+
append(", ");
|
|
254
|
+
}
|
|
255
|
+
const [name, type] = functionType.restParameter;
|
|
256
|
+
append("...");
|
|
257
|
+
append(name);
|
|
258
|
+
append(": ");
|
|
259
|
+
appendType(type, 2 /* TypePrecedence.Object */);
|
|
260
|
+
append("[]");
|
|
261
|
+
}
|
|
262
|
+
append(") => ");
|
|
263
|
+
appendType(functionType.returnType, 0 /* TypePrecedence.Union */);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
var TypePrecedence;
|
|
267
|
+
(function (TypePrecedence) {
|
|
268
|
+
TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
|
|
269
|
+
TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
|
|
270
|
+
TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
|
|
271
|
+
})(TypePrecedence || (TypePrecedence = {}));
|
|
272
|
+
function getTypePrecedence(type) {
|
|
273
|
+
switch (type._kind) {
|
|
274
|
+
case "union": {
|
|
275
|
+
return 0 /* TypePrecedence.Union */;
|
|
276
|
+
}
|
|
277
|
+
case "intersection": {
|
|
278
|
+
return 1 /* TypePrecedence.Intersection */;
|
|
279
|
+
}
|
|
280
|
+
default: {
|
|
281
|
+
return 2 /* TypePrecedence.Object */;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=renderTypeFactoryTypeScript.js.map
|