@fluidframework/tree-agent 2.70.0 → 2.72.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/.eslintrc.cjs +2 -2
- package/CHANGELOG.md +8 -1
- package/dist/typeGeneration.js +4 -4
- package/dist/typeGeneration.js.map +1 -1
- package/lib/typeGeneration.js +4 -4
- package/lib/typeGeneration.js.map +1 -1
- package/package.json +15 -15
- package/src/typeGeneration.ts +4 -4
package/.eslintrc.cjs
CHANGED
|
@@ -10,7 +10,7 @@ module.exports = {
|
|
|
10
10
|
},
|
|
11
11
|
rules: {
|
|
12
12
|
// Allow reaching into FluidFramework package paths that end with alpha, beta, legacy, or internal
|
|
13
|
-
"import/no-internal-modules": [
|
|
13
|
+
"import-x/no-internal-modules": [
|
|
14
14
|
"error",
|
|
15
15
|
{
|
|
16
16
|
allow: [
|
|
@@ -30,7 +30,7 @@ module.exports = {
|
|
|
30
30
|
},
|
|
31
31
|
rules: {
|
|
32
32
|
// Test files can import from submodules for testing purposes
|
|
33
|
-
"import/no-internal-modules": [
|
|
33
|
+
"import-x/no-internal-modules": [
|
|
34
34
|
"error",
|
|
35
35
|
{
|
|
36
36
|
allow: [
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @fluidframework/tree-agent
|
|
2
2
|
|
|
3
|
+
## 2.72.0
|
|
4
|
+
|
|
5
|
+
Dependency updates only.
|
|
6
|
+
|
|
7
|
+
## 2.71.0
|
|
8
|
+
|
|
9
|
+
Dependency updates only.
|
|
10
|
+
|
|
3
11
|
## 2.70.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
|
@@ -7,7 +15,6 @@
|
|
|
7
15
|
- A minimal set of branching APIs has been promoted to beta. ([#25744](https://github.com/microsoft/FluidFramework/pull/25744)) [32cc2c75d8](https://github.com/microsoft/FluidFramework/commit/32cc2c75d82c35403caa91e67e81f71baee5d092)
|
|
8
16
|
|
|
9
17
|
The following APIs have been promoted to beta in `@fluidframework/tree`:
|
|
10
|
-
|
|
11
18
|
- `TreeBranch.fork()`
|
|
12
19
|
- `TreeBranch.merge()`
|
|
13
20
|
- `TreeBranch.rebaseOnto()`
|
package/dist/typeGeneration.js
CHANGED
|
@@ -136,15 +136,15 @@ function getOrCreateType(definitionMap, definition, objectCache, bindableSchemas
|
|
|
136
136
|
.describe(simpleNodeSchema.metadata?.description ?? ""));
|
|
137
137
|
}
|
|
138
138
|
case internal_3.NodeKind.Map: {
|
|
139
|
-
const zodType = zod_1.z.map(zod_1.z.string(), getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
139
|
+
const zodType = zod_1.z.map(zod_1.z.string(), getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
140
140
|
return (type = addBindingIntersectionIfNeeded("map", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
141
141
|
}
|
|
142
142
|
case internal_3.NodeKind.Record: {
|
|
143
|
-
const zodType = zod_1.z.record(getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
143
|
+
const zodType = zod_1.z.record(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
144
144
|
return (type = addBindingIntersectionIfNeeded("record", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
145
145
|
}
|
|
146
146
|
case internal_3.NodeKind.Array: {
|
|
147
|
-
const zodType = zod_1.z.array(getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
147
|
+
const zodType = zod_1.z.array(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
148
148
|
return (type = addBindingIntersectionIfNeeded("array", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
149
149
|
}
|
|
150
150
|
case internal_3.NodeKind.Leaf: {
|
|
@@ -183,7 +183,7 @@ function getOrCreateTypeForField(definitionMap, fieldSchema, objectCache, bindab
|
|
|
183
183
|
throw new internal_2.UsageError(`The ${utils_js_1.llmDefault.description} property is only permitted on optional fields.`);
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
-
const field = getTypeForAllowedTypes(definitionMap, fieldSchema.
|
|
186
|
+
const field = getTypeForAllowedTypes(definitionMap, new Set(fieldSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas).describe(getDefault === undefined
|
|
187
187
|
? (fieldSchema.metadata?.description ?? "")
|
|
188
188
|
: "Do not populate this field. It will be automatically supplied by the system after insertion.");
|
|
189
189
|
switch (fieldSchema.kind) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA8E;AAC9E,uEAAsE;AACtE,4DAUuC;AAQvC,6BAAyC;AAGzC,yDAAwE;AACxE,yCAQoB;AAEpB;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AAEJ;;GAEG;AACH,SAAgB,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAIxB,OAAO,IAAA,sBAAW,EAAC,iBAAiB,EAAE,MAAM,EAAE,GAAG,EAAE;QAClD,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC7C,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAkB,CAAC;QACpD,IAAA,0BAAe,EAAC,UAAU,EAAE;YAC3B,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE;gBACX,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAClB,IACC,CAAC,YAAY,2BAAgB;oBAC7B,CAAC,YAAY,wBAAa;oBAC1B,CAAC,YAAY,0BAAe;oBAC5B,CAAC,YAAY,2BAAgB,EAC5B,CAAC;oBACF,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC,CAA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AC,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,mBAAQ,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,IAAA,0BAAe,EAAC,YAAY,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG;YACb,GAAG,IAAA,sBAAW,EAAC,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,IAAA,iBAAM,EAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,OAAC,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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tfieldSchema.allowedTypesIdentifiers,\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,kEAA8E;AAC9E,uEAAsE;AACtE,4DAUuC;AAQvC,6BAAyC;AAGzC,yDAAwE;AACxE,yCAQoB;AAEpB;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AAEJ;;GAEG;AACH,SAAgB,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAIxB,OAAO,IAAA,sBAAW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AC3E,CAAC,CAAC;SACF,CAAC;QACF,IAAA,iBAAM,EAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,OAAC,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"]}
|
package/lib/typeGeneration.js
CHANGED
|
@@ -132,15 +132,15 @@ function getOrCreateType(definitionMap, definition, objectCache, bindableSchemas
|
|
|
132
132
|
.describe(simpleNodeSchema.metadata?.description ?? ""));
|
|
133
133
|
}
|
|
134
134
|
case NodeKind.Map: {
|
|
135
|
-
const zodType = z.map(z.string(), getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
135
|
+
const zodType = z.map(z.string(), getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
136
136
|
return (type = addBindingIntersectionIfNeeded("map", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
137
137
|
}
|
|
138
138
|
case NodeKind.Record: {
|
|
139
|
-
const zodType = z.record(getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
139
|
+
const zodType = z.record(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
140
140
|
return (type = addBindingIntersectionIfNeeded("record", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
141
141
|
}
|
|
142
142
|
case NodeKind.Array: {
|
|
143
|
-
const zodType = z.array(getTypeForAllowedTypes(definitionMap, simpleNodeSchema.
|
|
143
|
+
const zodType = z.array(getTypeForAllowedTypes(definitionMap, new Set(simpleNodeSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas));
|
|
144
144
|
return (type = addBindingIntersectionIfNeeded("array", zodType, definition, simpleNodeSchema, bindableSchemas));
|
|
145
145
|
}
|
|
146
146
|
case NodeKind.Leaf: {
|
|
@@ -179,7 +179,7 @@ function getOrCreateTypeForField(definitionMap, fieldSchema, objectCache, bindab
|
|
|
179
179
|
throw new UsageError(`The ${llmDefault.description} property is only permitted on optional fields.`);
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
|
-
const field = getTypeForAllowedTypes(definitionMap, fieldSchema.
|
|
182
|
+
const field = getTypeForAllowedTypes(definitionMap, new Set(fieldSchema.simpleAllowedTypes.keys()), objectCache, bindableSchemas).describe(getDefault === undefined
|
|
183
183
|
? (fieldSchema.metadata?.description ?? "")
|
|
184
184
|
: "Do not populate this field. It will be automatically supplied by the system after insertion.");
|
|
185
185
|
switch (fieldSchema.kind) {
|
|
@@ -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,CA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esContent":["/*!\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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tsimpleNodeSchema.allowedTypesIdentifiers,\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\tfieldSchema.allowedTypesIdentifiers,\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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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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/tree-agent",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.72.0",
|
|
4
4
|
"description": "Experimental package to simplify integrating AI into Fluid-based applications",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -70,24 +70,24 @@
|
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
72
|
"@anthropic-ai/sdk": "^0.39.0",
|
|
73
|
-
"@fluidframework/core-utils": "~2.
|
|
74
|
-
"@fluidframework/runtime-utils": "~2.
|
|
75
|
-
"@fluidframework/telemetry-utils": "~2.
|
|
76
|
-
"@fluidframework/tree": "~2.
|
|
73
|
+
"@fluidframework/core-utils": "~2.72.0",
|
|
74
|
+
"@fluidframework/runtime-utils": "~2.72.0",
|
|
75
|
+
"@fluidframework/telemetry-utils": "~2.72.0",
|
|
76
|
+
"@fluidframework/tree": "~2.72.0",
|
|
77
77
|
"uuid": "^11.1.0",
|
|
78
78
|
"zod": "^3.25.32"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
81
|
"@arethetypeswrong/cli": "^0.17.1",
|
|
82
82
|
"@biomejs/biome": "~1.9.3",
|
|
83
|
-
"@fluid-internal/mocha-test-setup": "~2.
|
|
84
|
-
"@fluid-tools/build-cli": "^0.
|
|
83
|
+
"@fluid-internal/mocha-test-setup": "~2.72.0",
|
|
84
|
+
"@fluid-tools/build-cli": "^0.60.0",
|
|
85
85
|
"@fluidframework/build-common": "^2.0.3",
|
|
86
|
-
"@fluidframework/build-tools": "^0.
|
|
87
|
-
"@fluidframework/eslint-config-fluid": "
|
|
88
|
-
"@fluidframework/id-compressor": "~2.
|
|
89
|
-
"@fluidframework/runtime-utils": "~2.
|
|
90
|
-
"@fluidframework/test-runtime-utils": "~2.
|
|
86
|
+
"@fluidframework/build-tools": "^0.60.0",
|
|
87
|
+
"@fluidframework/eslint-config-fluid": "~2.72.0",
|
|
88
|
+
"@fluidframework/id-compressor": "~2.72.0",
|
|
89
|
+
"@fluidframework/runtime-utils": "~2.72.0",
|
|
90
|
+
"@fluidframework/test-runtime-utils": "~2.72.0",
|
|
91
91
|
"@langchain/anthropic": "^0.3.24",
|
|
92
92
|
"@langchain/core": "^0.3.78",
|
|
93
93
|
"@langchain/google-genai": "^0.2.16",
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"eslint-config-prettier": "~10.1.8",
|
|
105
105
|
"mocha": "^10.8.2",
|
|
106
106
|
"mocha-multi-reporters": "^1.5.1",
|
|
107
|
-
"prettier": "~3.
|
|
107
|
+
"prettier": "~3.6.2",
|
|
108
108
|
"rimraf": "^4.4.0",
|
|
109
109
|
"ses": "^1.14.0",
|
|
110
110
|
"typescript": "~5.4.5"
|
|
@@ -149,8 +149,8 @@
|
|
|
149
149
|
"check:format": "npm run check:biome",
|
|
150
150
|
"ci:build:docs": "api-extractor run",
|
|
151
151
|
"clean": "rimraf --glob dist lib {alpha,beta,internal,legacy}.d.ts \"**/*.tsbuildinfo\" \"**/*.build.log\" _api-extractor-temp nyc",
|
|
152
|
-
"eslint": "eslint --format stylish src",
|
|
153
|
-
"eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
|
|
152
|
+
"eslint": "eslint --quiet --format stylish src",
|
|
153
|
+
"eslint:fix": "eslint --quiet --format stylish src --fix --fix-type problem,suggestion,layout",
|
|
154
154
|
"format": "npm run format:biome",
|
|
155
155
|
"format:biome": "biome check . --write",
|
|
156
156
|
"lint": "fluid-build . --task lint",
|
package/src/typeGeneration.ts
CHANGED
|
@@ -228,7 +228,7 @@ function getOrCreateType(
|
|
|
228
228
|
z.string(),
|
|
229
229
|
getTypeForAllowedTypes(
|
|
230
230
|
definitionMap,
|
|
231
|
-
simpleNodeSchema.
|
|
231
|
+
new Set(simpleNodeSchema.simpleAllowedTypes.keys()),
|
|
232
232
|
objectCache,
|
|
233
233
|
bindableSchemas,
|
|
234
234
|
),
|
|
@@ -245,7 +245,7 @@ function getOrCreateType(
|
|
|
245
245
|
const zodType = z.record(
|
|
246
246
|
getTypeForAllowedTypes(
|
|
247
247
|
definitionMap,
|
|
248
|
-
simpleNodeSchema.
|
|
248
|
+
new Set(simpleNodeSchema.simpleAllowedTypes.keys()),
|
|
249
249
|
objectCache,
|
|
250
250
|
bindableSchemas,
|
|
251
251
|
),
|
|
@@ -262,7 +262,7 @@ function getOrCreateType(
|
|
|
262
262
|
const zodType = z.array(
|
|
263
263
|
getTypeForAllowedTypes(
|
|
264
264
|
definitionMap,
|
|
265
|
-
simpleNodeSchema.
|
|
265
|
+
new Set(simpleNodeSchema.simpleAllowedTypes.keys()),
|
|
266
266
|
objectCache,
|
|
267
267
|
bindableSchemas,
|
|
268
268
|
),
|
|
@@ -327,7 +327,7 @@ function getOrCreateTypeForField(
|
|
|
327
327
|
|
|
328
328
|
const field = getTypeForAllowedTypes(
|
|
329
329
|
definitionMap,
|
|
330
|
-
fieldSchema.
|
|
330
|
+
new Set(fieldSchema.simpleAllowedTypes.keys()),
|
|
331
331
|
objectCache,
|
|
332
332
|
bindableSchemas,
|
|
333
333
|
).describe(
|