@fluidframework/tree-agent 2.72.0 → 2.74.0-365691
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 +4 -0
- package/api-report/tree-agent.alpha.api.md +89 -20
- package/dist/agent.d.ts +5 -31
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +21 -38
- package/dist/agent.js.map +1 -1
- package/dist/alpha.d.ts +12 -3
- package/dist/api.d.ts +38 -20
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/methodBinding.d.ts +1 -3
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js.map +1 -1
- package/dist/prompt.d.ts +2 -2
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +4 -4
- package/dist/prompt.js.map +1 -1
- package/dist/propertyBinding.d.ts +106 -0
- package/dist/propertyBinding.d.ts.map +1 -0
- package/dist/propertyBinding.js +64 -0
- package/dist/propertyBinding.js.map +1 -0
- package/dist/subtree.d.ts +8 -9
- package/dist/subtree.d.ts.map +1 -1
- package/dist/subtree.js +21 -21
- package/dist/subtree.js.map +1 -1
- package/dist/typeGeneration.d.ts.map +1 -1
- package/dist/typeGeneration.js +78 -21
- package/dist/typeGeneration.js.map +1 -1
- package/dist/utils.d.ts +1 -7
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +19 -5
- package/dist/utils.js.map +1 -1
- package/lib/agent.d.ts +5 -31
- package/lib/agent.d.ts.map +1 -1
- package/lib/agent.js +19 -35
- package/lib/agent.js.map +1 -1
- package/lib/alpha.d.ts +12 -3
- package/lib/api.d.ts +38 -20
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +4 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/methodBinding.d.ts +1 -3
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/prompt.d.ts +2 -2
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +4 -4
- package/lib/prompt.js.map +1 -1
- package/lib/propertyBinding.d.ts +106 -0
- package/lib/propertyBinding.d.ts.map +1 -0
- package/lib/propertyBinding.js +59 -0
- package/lib/propertyBinding.js.map +1 -0
- package/lib/subtree.d.ts +8 -9
- package/lib/subtree.d.ts.map +1 -1
- package/lib/subtree.js +21 -21
- package/lib/subtree.js.map +1 -1
- package/lib/typeGeneration.d.ts.map +1 -1
- package/lib/typeGeneration.js +78 -21
- package/lib/typeGeneration.js.map +1 -1
- package/lib/utils.d.ts +1 -7
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +19 -5
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/agent.ts +29 -69
- package/src/api.ts +56 -29
- package/src/index.ts +16 -4
- package/src/methodBinding.ts +2 -2
- package/src/prompt.ts +6 -6
- package/src/propertyBinding.ts +182 -0
- package/src/subtree.ts +26 -29
- package/src/typeGeneration.ts +90 -24
- package/src/utils.ts +23 -17
package/lib/subtree.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subtree.d.ts","sourceRoot":"","sources":["../src/subtree.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"subtree.d.ts","sourceRoot":"","sources":["../src/subtree.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EACX,mBAAmB,EACnB,0BAA0B,EAG1B,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EACX,aAAa,EAEb,eAAe,EACf,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAAY,UAAU,EAAE,MAAM,UAAU,CAAC;AAErD;;GAEG;AACH,qBAAa,OAAO,CAAC,KAAK,SAAS,mBAAmB;aAClB,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;gBAA7B,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;IAMhE,IAAW,MAAM,IAAI,eAAe,CAInC;IAED,IAAW,KAAK,IAAI,aAAa,CAAC,KAAK,CAAC,CAEvC;IAED,IAAW,KAAK,CAAC,KAAK,EAAE,0BAA0B,CAAC,KAAK,CAAC,EAsDxD;IAED,IAAW,MAAM,IAAI,KAAK,CAMzB;IAEM,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC;CAY7B"}
|
package/lib/subtree.js
CHANGED
|
@@ -11,23 +11,23 @@ import { getNodeOnBranch } from "./getNodeOnBranch.js";
|
|
|
11
11
|
* Wraps either a {@link TreeView} or a {@link TreeNode} and provides a common interface over them.
|
|
12
12
|
*/
|
|
13
13
|
export class Subtree {
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
if (
|
|
14
|
+
constructor(viewOrTree) {
|
|
15
|
+
this.viewOrTree = viewOrTree;
|
|
16
|
+
if (viewOrTree instanceof TreeNode && TreeAlpha.branch(viewOrTree) === undefined) {
|
|
17
17
|
throw new UsageError("The provided node must belong to a branch.");
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
get branch() {
|
|
21
|
-
return this.
|
|
22
|
-
? (TreeAlpha.branch(this.
|
|
23
|
-
: this.
|
|
21
|
+
return this.viewOrTree instanceof TreeNode
|
|
22
|
+
? (TreeAlpha.branch(this.viewOrTree) ?? fail("Node cannot be raw."))
|
|
23
|
+
: this.viewOrTree;
|
|
24
24
|
}
|
|
25
25
|
get field() {
|
|
26
|
-
return this.
|
|
26
|
+
return this.viewOrTree instanceof TreeNode ? this.viewOrTree : this.viewOrTree.root;
|
|
27
27
|
}
|
|
28
28
|
set field(value) {
|
|
29
|
-
if (this.
|
|
30
|
-
const parent = Tree.parent(this.
|
|
29
|
+
if (this.viewOrTree instanceof TreeNode) {
|
|
30
|
+
const parent = Tree.parent(this.viewOrTree);
|
|
31
31
|
if (parent === undefined) {
|
|
32
32
|
// In general, this is not a correct cast, but we know that the root of the branch at least allows the type of `value`
|
|
33
33
|
const view = this.branch;
|
|
@@ -37,12 +37,12 @@ export class Subtree {
|
|
|
37
37
|
const schema = Tree.schema(parent);
|
|
38
38
|
switch (schema.kind) {
|
|
39
39
|
case NodeKind.Object: {
|
|
40
|
-
const key = Tree.key(this.
|
|
40
|
+
const key = Tree.key(this.viewOrTree);
|
|
41
41
|
parent[key] = value;
|
|
42
42
|
break;
|
|
43
43
|
}
|
|
44
44
|
case NodeKind.Record: {
|
|
45
|
-
const key = Tree.key(this.
|
|
45
|
+
const key = Tree.key(this.viewOrTree);
|
|
46
46
|
if (value === undefined) {
|
|
47
47
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
48
48
|
delete parent[key];
|
|
@@ -53,7 +53,7 @@ export class Subtree {
|
|
|
53
53
|
break;
|
|
54
54
|
}
|
|
55
55
|
case NodeKind.Map: {
|
|
56
|
-
const key = Tree.key(this.
|
|
56
|
+
const key = Tree.key(this.viewOrTree);
|
|
57
57
|
if (value === undefined) {
|
|
58
58
|
parent.delete(key);
|
|
59
59
|
}
|
|
@@ -63,7 +63,7 @@ export class Subtree {
|
|
|
63
63
|
break;
|
|
64
64
|
}
|
|
65
65
|
case NodeKind.Array: {
|
|
66
|
-
const index = Tree.key(this.
|
|
66
|
+
const index = Tree.key(this.viewOrTree);
|
|
67
67
|
const arrayNode = parent;
|
|
68
68
|
if (value === undefined) {
|
|
69
69
|
arrayNode.removeAt(index);
|
|
@@ -82,23 +82,23 @@ export class Subtree {
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
|
-
this.
|
|
85
|
+
this.viewOrTree.root = value;
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
get schema() {
|
|
89
|
-
return this.
|
|
90
|
-
? Tree.schema(this.
|
|
91
|
-
: this.
|
|
89
|
+
return (this.viewOrTree instanceof TreeNode
|
|
90
|
+
? Tree.schema(this.viewOrTree)
|
|
91
|
+
: this.viewOrTree.schema);
|
|
92
92
|
}
|
|
93
93
|
fork() {
|
|
94
|
-
if (this.
|
|
95
|
-
const branch = TreeAlpha.branch(this.
|
|
96
|
-
const node = getNodeOnBranch(this.
|
|
94
|
+
if (this.viewOrTree instanceof TreeNode) {
|
|
95
|
+
const branch = TreeAlpha.branch(this.viewOrTree) ?? fail("Node cannot be raw.");
|
|
96
|
+
const node = getNodeOnBranch(this.viewOrTree, branch.fork()) ??
|
|
97
97
|
fail("Expected node to be on new fork.");
|
|
98
98
|
return new Subtree(node);
|
|
99
99
|
}
|
|
100
100
|
else {
|
|
101
|
-
return new Subtree(this.
|
|
101
|
+
return new Subtree(this.viewOrTree.fork());
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
package/lib/subtree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subtree.js","sourceRoot":"","sources":["../src/subtree.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"subtree.js","sourceRoot":"","sources":["../src/subtree.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAOhE,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAOvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD;;GAEG;AACH,MAAM,OAAO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ACJ,CAAC;oBACF,CAAC;oBACD,OAAO,CAAC,CAAC,CAAC;wBACT,IAAI,CAAC,sBAAsB,CAAC,CAAC;oBAC9B,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,UAAU,CAAC,IAAI,GAAG,KAAK,CAAC;QAC9B,CAAC;IACF,CAAC;IAED,IAAW,MAAM;QAChB,OAAO,CACN,IAAI,CAAC,UAAU,YAAY,QAAQ;YAClC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC;YAC9B,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAChB,CAAC;IACZ,CAAC;IAEM,IAAI;QACV,IAAI,IAAI,CAAC,UAAU,YAAY,QAAQ,EAAE,CAAC;YACzC,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,IAAI,CAAC,qBAAqB,CAAC,CAAC;YAChF,MAAM,IAAI,GACT,eAAe,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC/C,IAAI,CAAC,kCAAkC,CAAC,CAAC;YAE1C,OAAO,IAAI,OAAO,CAAQ,IAAI,CAAC,CAAC;QACjC,CAAC;aAAM,CAAC;YACP,OAAO,IAAI,OAAO,CAAQ,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;QACnD,CAAC;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { fail } from \"@fluidframework/core-utils/internal\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport { TreeNode, Tree, NodeKind } from \"@fluidframework/tree\";\nimport type {\n\tImplicitFieldSchema,\n\tTreeFieldFromImplicitField,\n\tTreeMapNode,\n\tTreeArrayNode,\n} from \"@fluidframework/tree\";\nimport { TreeAlpha } from \"@fluidframework/tree/alpha\";\nimport type {\n\tReadableField,\n\tTreeRecordNode,\n\tTreeBranchAlpha,\n} from \"@fluidframework/tree/alpha\";\n\nimport { getNodeOnBranch } from \"./getNodeOnBranch.js\";\nimport type { TreeView, ViewOrTree } from \"./api.js\";\n\n/**\n * Wraps either a {@link TreeView} or a {@link TreeNode} and provides a common interface over them.\n */\nexport class Subtree<TRoot extends ImplicitFieldSchema> {\n\tpublic constructor(public readonly viewOrTree: ViewOrTree<TRoot>) {\n\t\tif (viewOrTree instanceof TreeNode && TreeAlpha.branch(viewOrTree) === undefined) {\n\t\t\tthrow new UsageError(\"The provided node must belong to a branch.\");\n\t\t}\n\t}\n\n\tpublic get branch(): TreeBranchAlpha {\n\t\treturn this.viewOrTree instanceof TreeNode\n\t\t\t? (TreeAlpha.branch(this.viewOrTree) ?? fail(\"Node cannot be raw.\"))\n\t\t\t: this.viewOrTree;\n\t}\n\n\tpublic get field(): ReadableField<TRoot> {\n\t\treturn this.viewOrTree instanceof TreeNode ? this.viewOrTree : this.viewOrTree.root;\n\t}\n\n\tpublic set field(value: TreeFieldFromImplicitField<TRoot>) {\n\t\tif (this.viewOrTree instanceof TreeNode) {\n\t\t\tconst parent = Tree.parent(this.viewOrTree);\n\t\t\tif (parent === undefined) {\n\t\t\t\t// In general, this is not a correct cast, but we know that the root of the branch at least allows the type of `value`\n\t\t\t\tconst view = this.branch as TreeView<TRoot>;\n\t\t\t\tview.root = value;\n\t\t\t} else {\n\t\t\t\tconst schema = Tree.schema(parent);\n\t\t\t\tswitch (schema.kind) {\n\t\t\t\t\tcase NodeKind.Object: {\n\t\t\t\t\t\tconst key = Tree.key(this.viewOrTree) as string;\n\t\t\t\t\t\t(parent as unknown as Record<string, unknown>)[key] = value;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase NodeKind.Record: {\n\t\t\t\t\t\tconst key = Tree.key(this.viewOrTree) as string;\n\t\t\t\t\t\tif (value === undefined) {\n\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n\t\t\t\t\t\t\tdelete (parent as TreeRecordNode)[key];\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t(parent as TreeRecordNode)[key] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase NodeKind.Map: {\n\t\t\t\t\t\tconst key = Tree.key(this.viewOrTree) as string;\n\t\t\t\t\t\tif (value === undefined) {\n\t\t\t\t\t\t\t(parent as TreeMapNode).delete(key);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t(parent as TreeMapNode).set(key, value as never);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase NodeKind.Array: {\n\t\t\t\t\t\tconst index = Tree.key(this.viewOrTree) as number;\n\t\t\t\t\t\tconst arrayNode = parent as TreeArrayNode;\n\t\t\t\t\t\tif (value === undefined) {\n\t\t\t\t\t\t\tarrayNode.removeAt(index);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis.branch.runTransaction(() => {\n\t\t\t\t\t\t\t\tarrayNode.removeAt(index);\n\t\t\t\t\t\t\t\tarrayNode.insertAt(index, value as never);\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tfail(\"Unexpected node kind\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthis.viewOrTree.root = value;\n\t\t}\n\t}\n\n\tpublic get schema(): TRoot {\n\t\treturn (\n\t\t\tthis.viewOrTree instanceof TreeNode\n\t\t\t\t? Tree.schema(this.viewOrTree)\n\t\t\t\t: this.viewOrTree.schema\n\t\t) as TRoot;\n\t}\n\n\tpublic fork(): Subtree<TRoot> {\n\t\tif (this.viewOrTree instanceof TreeNode) {\n\t\t\tconst branch = TreeAlpha.branch(this.viewOrTree) ?? fail(\"Node cannot be raw.\");\n\t\t\tconst node =\n\t\t\t\tgetNodeOnBranch(this.viewOrTree, branch.fork()) ??\n\t\t\t\tfail(\"Expected node to be on new fork.\");\n\n\t\t\treturn new Subtree<TRoot>(node);\n\t\t} else {\n\t\t\treturn new Subtree<TRoot>(this.viewOrTree.fork());\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,KAAK,EACX,mBAAmB,EAGnB,gBAAgB,EAEhB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAK,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,OAAO,KAAK,EACX,mBAAmB,EAGnB,gBAAgB,EAEhB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAK,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AAkCzC;;GAEG;AACH,wBAAgB,0BAA0B,CACzC,UAAU,EAAE,mBAAmB,EAC/B,MAAM,EAAE,gBAAgB,GACtB;IACF,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACxC,CAqCA"}
|
package/lib/typeGeneration.js
CHANGED
|
@@ -7,6 +7,7 @@ import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
|
7
7
|
import { ArrayNodeSchema, FieldKind, getSimpleSchema, MapNodeSchema, NodeKind, ObjectNodeSchema, RecordNodeSchema, ValueSchema, walkFieldSchema, } from "@fluidframework/tree/internal";
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import { FunctionWrapper, getExposedMethods } from "./methodBinding.js";
|
|
10
|
+
import { getExposedProperties } from "./propertyBinding.js";
|
|
10
11
|
import { fail, getOrCreate, hasAtLeastTwo, llmDefault, mapIterable, tryGetSingleton, } from "./utils.js";
|
|
11
12
|
/**
|
|
12
13
|
*
|
|
@@ -42,6 +43,11 @@ export function generateEditTypesForPrompt(rootSchema, schema) {
|
|
|
42
43
|
allSchemas.add(t);
|
|
43
44
|
objectTypeSchemas.add(t);
|
|
44
45
|
}
|
|
46
|
+
const exposedProperties = getExposedProperties(n);
|
|
47
|
+
for (const t of exposedProperties.referencedTypes) {
|
|
48
|
+
allSchemas.add(t);
|
|
49
|
+
objectTypeSchemas.add(t);
|
|
50
|
+
}
|
|
45
51
|
}
|
|
46
52
|
},
|
|
47
53
|
});
|
|
@@ -70,36 +76,86 @@ function generateEditTypes(schemas, objectCache, bindableSchemas) {
|
|
|
70
76
|
domainTypes,
|
|
71
77
|
};
|
|
72
78
|
}
|
|
73
|
-
function
|
|
74
|
-
const
|
|
79
|
+
function getBoundMembersForBindable(bindableSchema) {
|
|
80
|
+
const memberTypes = [];
|
|
75
81
|
const methods = getExposedMethods(bindableSchema);
|
|
76
82
|
for (const [name, method] of Object.entries(methods.methods)) {
|
|
77
83
|
const zodFunction = z.instanceof(FunctionWrapper);
|
|
78
84
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
79
85
|
zodFunction.method = method;
|
|
80
|
-
|
|
86
|
+
memberTypes.push([name, zodFunction]);
|
|
87
|
+
}
|
|
88
|
+
const props = getExposedProperties(bindableSchema);
|
|
89
|
+
for (const [name, prop] of Object.entries(props.properties)) {
|
|
90
|
+
const schema = prop.schema;
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
92
|
+
schema.property = prop;
|
|
93
|
+
memberTypes.push([name, prop.schema]);
|
|
81
94
|
}
|
|
82
|
-
return {
|
|
95
|
+
return {
|
|
96
|
+
members: memberTypes,
|
|
97
|
+
referencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),
|
|
98
|
+
};
|
|
83
99
|
}
|
|
84
|
-
function
|
|
85
|
-
const bindableSchema = bindableSchemas.get(definition) ?? fail("
|
|
86
|
-
return
|
|
100
|
+
function getBoundMembers(definition, bindableSchemas) {
|
|
101
|
+
const bindableSchema = bindableSchemas.get(definition) ?? fail("unknown definition");
|
|
102
|
+
return getBoundMembersForBindable(bindableSchema).members;
|
|
87
103
|
}
|
|
88
104
|
function addBindingIntersectionIfNeeded(typeString, zodTypeBound, definition, simpleNodeSchema, bindableSchemas) {
|
|
89
105
|
let zodType = zodTypeBound;
|
|
90
106
|
let description = simpleNodeSchema.metadata?.description ?? "";
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
107
|
+
const boundMembers = getBoundMembers(definition, bindableSchemas);
|
|
108
|
+
const methods = {};
|
|
109
|
+
const properties = {};
|
|
110
|
+
let hasProperties = false;
|
|
111
|
+
let hasMethods = false;
|
|
112
|
+
if (boundMembers.length > 0) {
|
|
113
|
+
for (const [name, zodMember] of boundMembers) {
|
|
114
|
+
const kind =
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
116
|
+
zodMember.method === undefined
|
|
117
|
+
? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
118
|
+
zodMember.property === undefined
|
|
119
|
+
? "Unknown"
|
|
120
|
+
: "Property"
|
|
121
|
+
: "Method";
|
|
122
|
+
if (kind === "Unknown") {
|
|
123
|
+
throw new UsageError(`Unrecognized bound member ${name} in schema ${definition}`);
|
|
124
|
+
}
|
|
125
|
+
if (kind === "Method") {
|
|
126
|
+
if (methods[name] !== undefined) {
|
|
127
|
+
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
128
|
+
}
|
|
129
|
+
methods[name] = zodMember;
|
|
97
130
|
}
|
|
98
|
-
|
|
131
|
+
if (kind === "Property") {
|
|
132
|
+
if (properties[name] !== undefined) {
|
|
133
|
+
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
134
|
+
}
|
|
135
|
+
properties[name] = zodMember;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
hasMethods = Object.keys(methods).length > 0 ? true : false;
|
|
139
|
+
hasProperties = Object.keys(properties).length > 0 ? true : false;
|
|
140
|
+
if (hasMethods) {
|
|
141
|
+
zodType = z.intersection(zodType, z.object(methods));
|
|
142
|
+
}
|
|
143
|
+
if (hasProperties) {
|
|
144
|
+
zodType = z.intersection(zodType, z.object(properties));
|
|
99
145
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
146
|
+
}
|
|
147
|
+
let note = "";
|
|
148
|
+
if (hasMethods && hasProperties) {
|
|
149
|
+
note = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;
|
|
150
|
+
}
|
|
151
|
+
else if (hasMethods) {
|
|
152
|
+
note = `Note: this ${typeString} has custom user-defined methods directly on it.`;
|
|
153
|
+
}
|
|
154
|
+
else if (hasProperties) {
|
|
155
|
+
note = `Note: this ${typeString} has custom user-defined properties directly on it.`;
|
|
156
|
+
}
|
|
157
|
+
if (note !== "") {
|
|
158
|
+
description = description === "" ? note : `${description} - ${note}`;
|
|
103
159
|
}
|
|
104
160
|
return zodType.describe(description);
|
|
105
161
|
}
|
|
@@ -120,12 +176,13 @@ function getOrCreateType(definitionMap, definition, objectCache, bindableSchemas
|
|
|
120
176
|
];
|
|
121
177
|
})
|
|
122
178
|
.filter(([, value]) => value !== undefined));
|
|
123
|
-
|
|
124
|
-
|
|
179
|
+
for (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
181
|
+
const kind = zodMember.method === undefined ? "Property" : "Method";
|
|
125
182
|
if (properties[name] !== undefined) {
|
|
126
|
-
throw new UsageError(
|
|
183
|
+
throw new UsageError(`${kind} ${name} conflicts with field of the same name in schema ${definition}`);
|
|
127
184
|
}
|
|
128
|
-
properties[name] =
|
|
185
|
+
properties[name] = zodMember;
|
|
129
186
|
}
|
|
130
187
|
return (type = z
|
|
131
188
|
.object(properties)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EACN,eAAe,EACf,SAAS,EACT,eAAe,EACf,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,gBAAgB,EAChB,WAAW,EACX,eAAe,GACf,MAAM,+BAA+B,CAAC;AAQvC,OAAO,EAAE,CAAC,EAAmB,MAAM,KAAK,CAAC;AAGzC,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,UAAU,EACV,WAAW,EACX,eAAe,GAEf,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AAEJ;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAIxB,OAAO,WAAW,CAAC,iBAAiB,EAAE,MAAM,EAAE,GAAG,EAAE;QAClD,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC7C,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAkB,CAAC;QACpD,eAAe,CAAC,UAAU,EAAE;YAC3B,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE;gBACX,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAClB,IACC,CAAC,YAAY,gBAAgB;oBAC7B,CAAC,YAAY,aAAa;o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AAC,CAAC,CAAC;YACzB,OAAO,KAAK,CAAC;QACd,CAAC;QACD,KAAK,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;YACzB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;QACD,KAAK,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;YAC3B,OAAO,KAAK;iBACV,QAAQ,EAAE;iBACV,QAAQ,CACR,uGAAuG,CACvG,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,0BAA0B,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1E,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,sBAAsB,CAC9B,aAAoD,EACpD,YAAiC,EACjC,WAAoD,EACpD,eAA4C;IAE5C,MAAM,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG;YACb,GAAG,WAAW,CAAC,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE;gBACrC,OAAO,eAAe,CAAC,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;YAC3E,CAAC,CAAC;SACF,CAAC;QACF,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;SAAM,CAAC;QACP,OAAO,eAAe,CAAC,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;IAC7E,CAAC;AACF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, unreachableCase } from \"@fluidframework/core-utils/internal\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tArrayNodeSchema,\n\tFieldKind,\n\tgetSimpleSchema,\n\tMapNodeSchema,\n\tNodeKind,\n\tObjectNodeSchema,\n\tRecordNodeSchema,\n\tValueSchema,\n\twalkFieldSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tImplicitFieldSchema,\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { FunctionWrapper, getExposedMethods } from \"./methodBinding.js\";\nimport {\n\tfail,\n\tgetOrCreate,\n\thasAtLeastTwo,\n\tllmDefault,\n\tmapIterable,\n\ttryGetSingleton,\n\ttype MapGetSet,\n} from \"./utils.js\";\n\n/**\n *\n * TODO: Add a prompt suggestion API!\n *\n * TODO: Handle rate limit errors.\n *\n * TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt\n *\n * TODO make the Ids be \"Vector-2\" instead of \"Vector2\" (or else it gets weird when you have a type called \"Vector2\")\n */\n\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst promptSchemaCache = new WeakMap<\n\tSimpleTreeSchema,\n\tReturnType<typeof generateEditTypes>\n>();\n\n/**\n * TODO\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\treturn getOrCreate(promptSchemaCache, schema, () => {\n\t\tconst allSchemas = new Set<TreeNodeSchema>();\n\t\tconst objectTypeSchemas = new Set<TreeNodeSchema>();\n\t\twalkFieldSchema(rootSchema, {\n\t\t\tnode: (n) => {\n\t\t\t\tallSchemas.add(n);\n\t\t\t\tif (\n\t\t\t\t\tn instanceof ObjectNodeSchema ||\n\t\t\t\t\tn instanceof MapNodeSchema ||\n\t\t\t\t\tn instanceof ArrayNodeSchema ||\n\t\t\t\t\tn instanceof RecordNodeSchema\n\t\t\t\t) {\n\t\t\t\t\tobjectTypeSchemas.add(n);\n\t\t\t\t\tconst exposedMethods = getExposedMethods(n);\n\t\t\t\t\tfor (const t of exposedMethods.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tconst nodeSchemas = [...objectTypeSchemas.values()] as BindableSchema[];\n\t\tconst bindableSchemas = new Map<string, BindableSchema>(\n\t\t\tnodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]),\n\t\t);\n\t\treturn generateEditTypes(\n\t\t\t[...allSchemas.values()].map((s) => getSimpleSchema(s)),\n\t\t\tnew Map(),\n\t\t\tbindableSchemas,\n\t\t);\n\t});\n}\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nfunction generateEditTypes(\n\tschemas: Iterable<SimpleTreeSchema>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\tconst domainTypes: Record<string, ZodTypeAny> = {};\n\tfor (const schema of schemas) {\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\t// If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate\n\t\t\tdomainTypes[name] = getOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\tname,\n\t\t\t\tobjectCache,\n\t\t\t\tbindableSchemas,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn {\n\t\tdomainTypes,\n\t};\n}\n\nfunction getBoundMethodsForBindable(bindableSchema: BindableSchema): {\n\treferencedTypes: Set<TreeNodeSchema>;\n\tmethods: [string, ZodTypeAny][];\n} {\n\tconst methodTypes: [string, ZodTypeAny][] = [];\n\tconst methods = getExposedMethods(bindableSchema);\n\tfor (const [name, method] of Object.entries(methods.methods)) {\n\t\tconst zodFunction = z.instanceof(FunctionWrapper);\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(zodFunction as any).method = method;\n\t\tmethodTypes.push([name, zodFunction]);\n\t}\n\treturn { methods: methodTypes, referencedTypes: methods.referencedTypes };\n}\n\nfunction getBoundMethods(\n\tdefinition: string,\n\tbindableSchemas: Map<string, BindableSchema>,\n): [string, ZodTypeAny][] {\n\tconst bindableSchema = bindableSchemas.get(definition) ?? fail(\"Unknown definition\");\n\treturn getBoundMethodsForBindable(bindableSchema).methods;\n}\n\nfunction addBindingIntersectionIfNeeded(\n\ttypeString: \"array\" | \"map\" | \"record\",\n\tzodTypeBound: ZodTypeAny,\n\tdefinition: string,\n\tsimpleNodeSchema: SimpleNodeSchema,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tlet zodType = zodTypeBound;\n\tlet description = simpleNodeSchema.metadata?.description ?? \"\";\n\tconst boundMethods = getBoundMethods(definition, bindableSchemas);\n\tif (boundMethods.length > 0) {\n\t\tconst methods: Record<string, z.ZodTypeAny> = {};\n\t\tfor (const [name, zodFunction] of boundMethods) {\n\t\t\tif (methods[name] !== undefined) {\n\t\t\t\tthrow new UsageError(\n\t\t\t\t\t`Method ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tmethods[name] = zodFunction;\n\t\t}\n\t\tzodType = z.intersection(zodType, z.object(methods));\n\t\tconst methodNote = `Note: this ${typeString} has custom user-defined methods directly on it.`;\n\t\tdescription = description === \"\" ? methodNote : `${description} - ${methodNote}`;\n\t}\n\treturn zodType.describe(description);\n}\n\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tdefinition: string,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst simpleNodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\treturn getOrCreate(objectCache, simpleNodeSchema, () => {\n\t\t// Handle recursive types: temporarily create a zod \"lazy\" type that can be referenced by a recursive call to getOrCreateType.\n\t\tlet type: ZodTypeAny | undefined;\n\t\tobjectCache.set(\n\t\t\tsimpleNodeSchema,\n\t\t\tz.lazy(() => type ?? fail(\"Recursive type used before creation\")),\n\t\t);\n\t\tswitch (simpleNodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties: Record<string, z.ZodTypeAny> = Object.fromEntries(\n\t\t\t\t\t[...simpleNodeSchema.fields]\n\t\t\t\t\t\t.map(([key, field]) => {\n\t\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\tgetOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.filter(([, value]) => value !== undefined),\n\t\t\t\t);\n\n\t\t\t\t// Unlike arrays/maps/records, object nodes include methods directly on them rather than using an intersection\n\t\t\t\tfor (const [name, zodFunction] of getBoundMethods(definition, bindableSchemas)) {\n\t\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`Method ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tproperties[name] = zodFunction;\n\t\t\t\t}\n\n\t\t\t\treturn (type = z\n\t\t\t\t\t.object(properties)\n\t\t\t\t\t.describe(simpleNodeSchema.metadata?.description ?? \"\"));\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\tconst zodType = z.map(\n\t\t\t\t\tz.string(),\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"map\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\tconst zodType = z.record(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"record\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tconst zodType = z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"array\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (simpleNodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn (type = z.boolean());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn (type = z.number());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn (type = z.string());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn (type = z.null());\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthrow new Error(`Unsupported leaf kind ${NodeKind[simpleNodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn unreachableCase(simpleNodeSchema, \"Unknown node kind\");\n\t\t\t}\n\t\t}\n\t});\n}\n\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tfieldSchema: SimpleFieldSchema,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst customMetadata = fieldSchema.metadata.custom as\n\t\t| Record<string | symbol, unknown>\n\t\t| undefined;\n\tconst getDefault = customMetadata?.[llmDefault];\n\tif (getDefault !== undefined) {\n\t\tif (typeof getDefault !== \"function\") {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`,\n\t\t\t);\n\t\t}\n\n\t\tif (fieldSchema.kind !== FieldKind.Optional) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`The ${llmDefault.description} property is only permitted on optional fields.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst field = getTypeForAllowedTypes(\n\t\tdefinitionMap,\n\t\tnew Set(fieldSchema.simpleAllowedTypes.keys()),\n\t\tobjectCache,\n\t\tbindableSchemas,\n\t).describe(\n\t\tgetDefault === undefined\n\t\t\t? (fieldSchema.metadata?.description ?? \"\")\n\t\t\t: \"Do not populate this field. It will be automatically supplied by the system after insertion.\",\n\t);\n\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn field;\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn field.optional();\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn field\n\t\t\t\t.optional()\n\t\t\t\t.describe(\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\t);\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tallowedTypes: ReadonlySet<string>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(definitionMap, name, objectCache, bindableSchemas);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(definitionMap, single, objectCache, bindableSchemas);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAA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AAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1E,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,sBAAsB,CAC9B,aAAoD,EACpD,YAAiC,EACjC,WAAoD,EACpD,eAA4C;IAE5C,MAAM,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG;YACb,GAAG,WAAW,CAAC,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE;gBACrC,OAAO,eAAe,CAAC,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;YAC3E,CAAC,CAAC;SACF,CAAC;QACF,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;SAAM,CAAC;QACP,OAAO,eAAe,CAAC,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;IAC7E,CAAC;AACF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, unreachableCase } from \"@fluidframework/core-utils/internal\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tArrayNodeSchema,\n\tFieldKind,\n\tgetSimpleSchema,\n\tMapNodeSchema,\n\tNodeKind,\n\tObjectNodeSchema,\n\tRecordNodeSchema,\n\tValueSchema,\n\twalkFieldSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tImplicitFieldSchema,\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { FunctionWrapper, getExposedMethods } from \"./methodBinding.js\";\nimport { getExposedProperties } from \"./propertyBinding.js\";\nimport {\n\tfail,\n\tgetOrCreate,\n\thasAtLeastTwo,\n\tllmDefault,\n\tmapIterable,\n\ttryGetSingleton,\n\ttype MapGetSet,\n} from \"./utils.js\";\n\n/**\n *\n * TODO: Add a prompt suggestion API!\n *\n * TODO: Handle rate limit errors.\n *\n * TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt\n *\n * TODO make the Ids be \"Vector-2\" instead of \"Vector2\" (or else it gets weird when you have a type called \"Vector2\")\n */\n\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst promptSchemaCache = new WeakMap<\n\tSimpleTreeSchema,\n\tReturnType<typeof generateEditTypes>\n>();\n\n/**\n * TODO\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\treturn getOrCreate(promptSchemaCache, schema, () => {\n\t\tconst allSchemas = new Set<TreeNodeSchema>();\n\t\tconst objectTypeSchemas = new Set<TreeNodeSchema>();\n\t\twalkFieldSchema(rootSchema, {\n\t\t\tnode: (n) => {\n\t\t\t\tallSchemas.add(n);\n\t\t\t\tif (\n\t\t\t\t\tn instanceof ObjectNodeSchema ||\n\t\t\t\t\tn instanceof MapNodeSchema ||\n\t\t\t\t\tn instanceof ArrayNodeSchema ||\n\t\t\t\t\tn instanceof RecordNodeSchema\n\t\t\t\t) {\n\t\t\t\t\tobjectTypeSchemas.add(n);\n\t\t\t\t\tconst exposedMethods = getExposedMethods(n);\n\t\t\t\t\tfor (const t of exposedMethods.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t\tconst exposedProperties = getExposedProperties(n);\n\t\t\t\t\tfor (const t of exposedProperties.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tconst nodeSchemas = [...objectTypeSchemas.values()] as BindableSchema[];\n\t\tconst bindableSchemas = new Map<string, BindableSchema>(\n\t\t\tnodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]),\n\t\t);\n\t\treturn generateEditTypes(\n\t\t\t[...allSchemas.values()].map((s) => getSimpleSchema(s)),\n\t\t\tnew Map(),\n\t\t\tbindableSchemas,\n\t\t);\n\t});\n}\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nfunction generateEditTypes(\n\tschemas: Iterable<SimpleTreeSchema>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\tconst domainTypes: Record<string, ZodTypeAny> = {};\n\tfor (const schema of schemas) {\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\t// If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate\n\t\t\tdomainTypes[name] = getOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\tname,\n\t\t\t\tobjectCache,\n\t\t\t\tbindableSchemas,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn {\n\t\tdomainTypes,\n\t};\n}\n\nfunction getBoundMembersForBindable(bindableSchema: BindableSchema): {\n\treferencedTypes: Set<TreeNodeSchema>;\n\tmembers: [string, ZodTypeAny][];\n} {\n\tconst memberTypes: [string, ZodTypeAny][] = [];\n\n\tconst methods = getExposedMethods(bindableSchema);\n\tfor (const [name, method] of Object.entries(methods.methods)) {\n\t\tconst zodFunction = z.instanceof(FunctionWrapper);\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(zodFunction as any).method = method;\n\t\tmemberTypes.push([name, zodFunction]);\n\t}\n\tconst props = getExposedProperties(bindableSchema);\n\tfor (const [name, prop] of Object.entries(props.properties)) {\n\t\tconst schema = prop.schema;\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(schema as any).property = prop;\n\t\tmemberTypes.push([name, prop.schema]);\n\t}\n\n\treturn {\n\t\tmembers: memberTypes,\n\t\treferencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),\n\t};\n}\n\nfunction getBoundMembers(\n\tdefinition: string,\n\tbindableSchemas: Map<string, BindableSchema>,\n): [string, ZodTypeAny][] {\n\tconst bindableSchema = bindableSchemas.get(definition) ?? fail(\"unknown definition\");\n\treturn getBoundMembersForBindable(bindableSchema).members;\n}\n\nfunction addBindingIntersectionIfNeeded(\n\ttypeString: \"array\" | \"map\" | \"record\",\n\tzodTypeBound: ZodTypeAny,\n\tdefinition: string,\n\tsimpleNodeSchema: SimpleNodeSchema,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tlet zodType = zodTypeBound;\n\tlet description = simpleNodeSchema.metadata?.description ?? \"\";\n\n\tconst boundMembers = getBoundMembers(definition, bindableSchemas);\n\tconst methods: Record<string, z.ZodTypeAny> = {};\n\tconst properties: Record<string, z.ZodTypeAny> = {};\n\n\tlet hasProperties: boolean = false;\n\tlet hasMethods: boolean = false;\n\n\tif (boundMembers.length > 0) {\n\t\tfor (const [name, zodMember] of boundMembers) {\n\t\t\tconst kind =\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(zodMember as any).method === undefined\n\t\t\t\t\t? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(zodMember as any).property === undefined\n\t\t\t\t\t\t? \"Unknown\"\n\t\t\t\t\t\t: \"Property\"\n\t\t\t\t\t: \"Method\";\n\n\t\t\tif (kind === \"Unknown\") {\n\t\t\t\tthrow new UsageError(`Unrecognized bound member ${name} in schema ${definition}`);\n\t\t\t}\n\t\t\tif (kind === \"Method\") {\n\t\t\t\tif (methods[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tmethods[name] = zodMember;\n\t\t\t}\n\t\t\tif (kind === \"Property\") {\n\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tproperties[name] = zodMember;\n\t\t\t}\n\t\t}\n\t\thasMethods = Object.keys(methods).length > 0 ? true : false;\n\t\thasProperties = Object.keys(properties).length > 0 ? true : false;\n\n\t\tif (hasMethods) {\n\t\t\tzodType = z.intersection(zodType, z.object(methods));\n\t\t}\n\t\tif (hasProperties) {\n\t\t\tzodType = z.intersection(zodType, z.object(properties));\n\t\t}\n\t}\n\n\tlet note = \"\";\n\tif (hasMethods && hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;\n\t} else if (hasMethods) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods directly on it.`;\n\t} else if (hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined properties directly on it.`;\n\t}\n\tif (note !== \"\") {\n\t\tdescription = description === \"\" ? note : `${description} - ${note}`;\n\t}\n\n\treturn zodType.describe(description);\n}\n\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tdefinition: string,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst simpleNodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\treturn getOrCreate(objectCache, simpleNodeSchema, () => {\n\t\t// Handle recursive types: temporarily create a zod \"lazy\" type that can be referenced by a recursive call to getOrCreateType.\n\t\tlet type: ZodTypeAny | undefined;\n\t\tobjectCache.set(\n\t\t\tsimpleNodeSchema,\n\t\t\tz.lazy(() => type ?? fail(\"Recursive type used before creation\")),\n\t\t);\n\t\tswitch (simpleNodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties: Record<string, z.ZodTypeAny> = Object.fromEntries(\n\t\t\t\t\t[...simpleNodeSchema.fields]\n\t\t\t\t\t\t.map(([key, field]) => {\n\t\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\tgetOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.filter(([, value]) => value !== undefined),\n\t\t\t\t);\n\n\t\t\t\tfor (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst kind = (zodMember as any).method === undefined ? \"Property\" : \"Method\";\n\t\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tproperties[name] = zodMember;\n\t\t\t\t}\n\n\t\t\t\treturn (type = z\n\t\t\t\t\t.object(properties)\n\t\t\t\t\t.describe(simpleNodeSchema.metadata?.description ?? \"\"));\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\tconst zodType = z.map(\n\t\t\t\t\tz.string(),\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"map\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\tconst zodType = z.record(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"record\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tconst zodType = z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"array\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (simpleNodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn (type = z.boolean());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn (type = z.number());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn (type = z.string());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn (type = z.null());\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthrow new Error(`Unsupported leaf kind ${NodeKind[simpleNodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn unreachableCase(simpleNodeSchema, \"Unknown node kind\");\n\t\t\t}\n\t\t}\n\t});\n}\n\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tfieldSchema: SimpleFieldSchema,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst customMetadata = fieldSchema.metadata.custom as\n\t\t| Record<string | symbol, unknown>\n\t\t| undefined;\n\tconst getDefault = customMetadata?.[llmDefault];\n\tif (getDefault !== undefined) {\n\t\tif (typeof getDefault !== \"function\") {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`,\n\t\t\t);\n\t\t}\n\n\t\tif (fieldSchema.kind !== FieldKind.Optional) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`The ${llmDefault.description} property is only permitted on optional fields.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst field = getTypeForAllowedTypes(\n\t\tdefinitionMap,\n\t\tnew Set(fieldSchema.simpleAllowedTypes.keys()),\n\t\tobjectCache,\n\t\tbindableSchemas,\n\t).describe(\n\t\tgetDefault === undefined\n\t\t\t? (fieldSchema.metadata?.description ?? \"\")\n\t\t\t: \"Do not populate this field. It will be automatically supplied by the system after insertion.\",\n\t);\n\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn field;\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn field.optional();\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn field\n\t\t\t\t.optional()\n\t\t\t\t.describe(\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\t);\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tallowedTypes: ReadonlySet<string>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(definitionMap, name, objectCache, bindableSchemas);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(definitionMap, single, objectCache, bindableSchemas);\n\t}\n}\n"]}
|
package/lib/utils.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import type { ImplicitFieldSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
|
|
6
|
-
import type { InsertableContent,
|
|
6
|
+
import type { InsertableContent, TreeNode, TreeNodeSchema } from "@fluidframework/tree/alpha";
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
/**
|
|
9
9
|
* Subset of Map interface.
|
|
@@ -37,12 +37,6 @@ export declare function mapIterable<T, U>(iterable: Iterable<T>, map: (t: T) =>
|
|
|
37
37
|
* @remarks originally from tree/src/util/utils.ts
|
|
38
38
|
*/
|
|
39
39
|
export declare function getOrCreate<K, V>(map: MapGetSet<K, V>, key: K, defaultValue: (key: K) => V): V;
|
|
40
|
-
/**
|
|
41
|
-
* TODO
|
|
42
|
-
* @alpha
|
|
43
|
-
* @privateRemarks This is a subset of the TreeViewAlpha functionality because if take it wholesale, it causes problems with invariance of the generic parameters.
|
|
44
|
-
*/
|
|
45
|
-
export type TreeView<TRoot extends ImplicitFieldSchema | UnsafeUnknownSchema> = Pick<TreeViewAlpha<TRoot>, "root" | "fork" | "merge" | "rebaseOnto" | "schema" | "events"> & TreeBranchAlpha;
|
|
46
40
|
/**
|
|
47
41
|
* TODO
|
|
48
42
|
*/
|
package/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EACX,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EACX,iBAAiB,EACjB,QAAQ,EACR,cAAc,EAEd,MAAM,4BAA4B,CAAC;AASpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB;;;;GAIG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,EAAE,CAAC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ED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAU/D;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM,CAOhE;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC7B,gBAAgB,EAAE,OAAO,CAAC;CAC1B;AAOD;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,EACjC,OAAO,CAAC,EAAE,aAAa,GACrB,MAAM,CAyTR;AA6CD;;;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;AAID;;;GAGG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,mBAAmB,EAC3B,MAAM,GAAE,CAAC,MAAM,EAAE,cAAc,KAAK,OAAoB,EACxD,OAAO,sBAA4B,GACjC,GAAG,CAAC,cAAc,CAAC,CAUrB;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CASpD"}
|
package/lib/utils.js
CHANGED
|
@@ -11,6 +11,7 @@ import { ArrayNodeSchema, MapNodeSchema, ObjectNodeSchema, RecordNodeSchema, Tre
|
|
|
11
11
|
import { NodeKind, normalizeFieldSchema } from "@fluidframework/tree/internal";
|
|
12
12
|
import { z } from "zod";
|
|
13
13
|
import { FunctionWrapper } from "./methodBinding.js";
|
|
14
|
+
import { PropertyDef } from "./propertyBinding.js";
|
|
14
15
|
/**
|
|
15
16
|
* TBD
|
|
16
17
|
*/
|
|
@@ -127,7 +128,7 @@ export function isNamedSchema(schemaIdentifier) {
|
|
|
127
128
|
if (["string", "number", "boolean", "null", "handle"].includes(unqualifySchema(schemaIdentifier))) {
|
|
128
129
|
return false;
|
|
129
130
|
}
|
|
130
|
-
return
|
|
131
|
+
return /(?:Array|Map|Record)<\["(.*)"]>/.exec(schemaIdentifier) === null;
|
|
131
132
|
}
|
|
132
133
|
/**
|
|
133
134
|
* Returns the unqualified name of a schema (e.g. `"my.scope.MyNode"` returns `"MyNode"`).
|
|
@@ -136,7 +137,7 @@ export function isNamedSchema(schemaIdentifier) {
|
|
|
136
137
|
*/
|
|
137
138
|
export function unqualifySchema(schemaIdentifier) {
|
|
138
139
|
// Get the unqualified name by removing the scope (everything before the last dot).
|
|
139
|
-
const matches =
|
|
140
|
+
const matches = /[^.]+$/.exec(schemaIdentifier);
|
|
140
141
|
if (matches === null) {
|
|
141
142
|
return schemaIdentifier; // Return the original name if it is unscoped.
|
|
142
143
|
}
|
|
@@ -335,6 +336,21 @@ export function getZodSchemaAsTypeScript(schema, details) {
|
|
|
335
336
|
}
|
|
336
337
|
}
|
|
337
338
|
}
|
|
339
|
+
function appendBoundProperties(type) {
|
|
340
|
+
const property = type.property;
|
|
341
|
+
if (!(property instanceof PropertyDef)) {
|
|
342
|
+
if (type.description !== undefined && type.description !== "") {
|
|
343
|
+
append(` // ${type.description}`);
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (property.readOnly === true) {
|
|
348
|
+
append(" // readonly");
|
|
349
|
+
}
|
|
350
|
+
if (property.description !== undefined && property.description !== "") {
|
|
351
|
+
append(` - ${property.description}`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
338
354
|
function appendArrayType(arrayType) {
|
|
339
355
|
appendType(arrayType._def.type, 2 /* TypePrecedence.Object */);
|
|
340
356
|
append("[]");
|
|
@@ -355,9 +371,7 @@ export function getZodSchemaAsTypeScript(schema, details) {
|
|
|
355
371
|
append(": ");
|
|
356
372
|
appendType(type);
|
|
357
373
|
append(";");
|
|
358
|
-
|
|
359
|
-
if (comment !== undefined && comment !== "")
|
|
360
|
-
append(` // ${comment}`);
|
|
374
|
+
appendBoundProperties(type);
|
|
361
375
|
appendNewLine();
|
|
362
376
|
}
|
|
363
377
|
}
|