@fluidframework/tree-agent 2.74.0-365691 → 2.74.0-368706
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/methodBinding.d.ts +6 -1
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js +14 -3
- package/dist/methodBinding.js.map +1 -1
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +2 -12
- package/dist/prompt.js.map +1 -1
- package/dist/propertyBinding.js +2 -2
- package/dist/propertyBinding.js.map +1 -1
- package/dist/renderSchemaTypeScript.d.ts +18 -0
- package/dist/renderSchemaTypeScript.d.ts.map +1 -0
- package/dist/renderSchemaTypeScript.js +399 -0
- package/dist/renderSchemaTypeScript.js.map +1 -0
- package/dist/renderZodTypeScript.d.ts +21 -0
- package/dist/renderZodTypeScript.d.ts.map +1 -0
- package/dist/renderZodTypeScript.js +272 -0
- package/dist/renderZodTypeScript.js.map +1 -0
- package/dist/typeGeneration.d.ts +3 -5
- package/dist/typeGeneration.d.ts.map +1 -1
- package/dist/typeGeneration.js +21 -254
- package/dist/typeGeneration.js.map +1 -1
- package/dist/utils.d.ts +10 -27
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +18 -362
- package/dist/utils.js.map +1 -1
- package/lib/methodBinding.d.ts +6 -1
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js +11 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +3 -13
- package/lib/prompt.js.map +1 -1
- package/lib/propertyBinding.js +1 -1
- package/lib/propertyBinding.js.map +1 -1
- package/lib/renderSchemaTypeScript.d.ts +18 -0
- package/lib/renderSchemaTypeScript.d.ts.map +1 -0
- package/lib/renderSchemaTypeScript.js +395 -0
- package/lib/renderSchemaTypeScript.js.map +1 -0
- package/lib/renderZodTypeScript.d.ts +21 -0
- package/lib/renderZodTypeScript.d.ts.map +1 -0
- package/lib/renderZodTypeScript.js +267 -0
- package/lib/renderZodTypeScript.js.map +1 -0
- package/lib/typeGeneration.d.ts +3 -5
- package/lib/typeGeneration.d.ts.map +1 -1
- package/lib/typeGeneration.js +24 -257
- package/lib/typeGeneration.js.map +1 -1
- package/lib/utils.d.ts +10 -27
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +18 -360
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/methodBinding.ts +15 -3
- package/src/prompt.ts +6 -22
- package/src/propertyBinding.ts +1 -1
- package/src/renderSchemaTypeScript.ts +523 -0
- package/src/renderZodTypeScript.ts +314 -0
- package/src/typeGeneration.ts +32 -414
- package/src/utils.ts +18 -412
package/dist/methodBinding.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import type {
|
|
5
|
+
import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
|
|
6
|
+
import { NodeKind } from "@fluidframework/tree";
|
|
6
7
|
import type { z } from "zod";
|
|
7
8
|
/**
|
|
8
9
|
* A utility type that extracts the method keys from a given type.
|
|
@@ -21,6 +22,10 @@ export type Ctor<T = any> = new (...args: any[]) => T;
|
|
|
21
22
|
* @alpha
|
|
22
23
|
*/
|
|
23
24
|
export type BindableSchema = TreeNodeSchema<string, NodeKind.Object> | TreeNodeSchema<string, NodeKind.Record> | TreeNodeSchema<string, NodeKind.Array> | TreeNodeSchema<string, NodeKind.Map>;
|
|
25
|
+
/**
|
|
26
|
+
* A type guard to check if a schema is {@link BindableSchema | bindable}.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isBindableSchema(schema: TreeNodeSchema): schema is BindableSchema;
|
|
24
29
|
/**
|
|
25
30
|
* Get the exposed methods of a schema class.
|
|
26
31
|
* @param schemaClass - The schema class to extract methods from.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"methodBinding.d.ts","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"methodBinding.d.ts","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAG7B;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;KAC1B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GAAG,CAAC,GAAG,KAAK;CAChE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEtD;;;GAGG;AACH,MAAM,MAAM,cAAc,GACvB,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GACvC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GACvC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,GACtC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;AAExC;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,IAAI,cAAc,CAOjF;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,cAAc,GAAG;IAC/D,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACzC,eAAe,EAAE,GAAG,CAAC,cAAc,CAA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EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,IAAI,CAAC;IAElD;;;;OAIG;IACH,UAAU,CAAC,CAAC,SAAS,mBAAmB,EACvC,MAAM,EAAE,CAAC,GACP,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;CAC7D;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,EAAE,OAAO,MAAsB,CAAC;AAEhE;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,eAAe;IAC/B,CAAC,mBAAmB,CAAC,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;CACrD"}
|
package/dist/methodBinding.js
CHANGED
|
@@ -4,8 +4,19 @@
|
|
|
4
4
|
* Licensed under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.exposeMethodsSymbol = exports.buildFunc = exports.FunctionWrapper = exports.getExposedMethods = void 0;
|
|
8
|
-
const
|
|
7
|
+
exports.exposeMethodsSymbol = exports.buildFunc = exports.FunctionWrapper = exports.getExposedMethods = exports.isBindableSchema = void 0;
|
|
8
|
+
const tree_1 = require("@fluidframework/tree");
|
|
9
|
+
const renderZodTypeScript_js_1 = require("./renderZodTypeScript.js");
|
|
10
|
+
/**
|
|
11
|
+
* A type guard to check if a schema is {@link BindableSchema | bindable}.
|
|
12
|
+
*/
|
|
13
|
+
function isBindableSchema(schema) {
|
|
14
|
+
return (schema.kind === tree_1.NodeKind.Object ||
|
|
15
|
+
schema.kind === tree_1.NodeKind.Record ||
|
|
16
|
+
schema.kind === tree_1.NodeKind.Array ||
|
|
17
|
+
schema.kind === tree_1.NodeKind.Map);
|
|
18
|
+
}
|
|
19
|
+
exports.isBindableSchema = isBindableSchema;
|
|
9
20
|
/**
|
|
10
21
|
* Get the exposed methods of a schema class.
|
|
11
22
|
* @param schemaClass - The schema class to extract methods from.
|
|
@@ -64,7 +75,7 @@ class ExposedMethodsI {
|
|
|
64
75
|
}
|
|
65
76
|
instanceOf(schema) {
|
|
66
77
|
this.referencedTypes.add(schema);
|
|
67
|
-
return (0,
|
|
78
|
+
return (0, renderZodTypeScript_js_1.instanceOf)(schema);
|
|
68
79
|
}
|
|
69
80
|
static getExposedMethods(schemaClass) {
|
|
70
81
|
const exposedMethods = new ExposedMethodsI(schemaClass);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"methodBinding.js","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"methodBinding.js","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAIH,+CAAgD;AAEhD,qEAAsD;AA0BtD;;GAEG;AACH,SAAgB,gBAAgB,CAAC,MAAsB;IACtD,OAAO,CACN,MAAM,CAAC,IAAI,KAAK,eAAQ,CAAC,MAAM;QAC/B,MAAM,CAAC,IAAI,KAAK,eAAQ,CAAC,MAAM;QAC/B,MAAM,CAAC,IAAI,KAAK,eAAQ,CAAC,KAAK;QAC9B,MAAM,CAAC,IAAI,KAAK,eAAQ,CAAC,GAAG,CAC5B,CAAC;AACH,CAAC;AAPD,4CAOC;AAED;;;;GAIG;AACH,SAAgB,iBAAiB,CAAC,WAA2B;IAI5D,OAAO,eAAe,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;AACvD,CAAC;AALD,8CAKC;AAuBD;;GAEG;AACH,MAAa,eAAe;IAG3B,YACiB,IAAY,EACZ,WAA+B,EAC/B,IAAoB;IACpC,kDAAkD;IAClC,IAAyB,EACzB,OAAqB;QALrB,SAAI,GAAJ,IAAI,CAAQ;QACZ,gBAAW,GAAX,WAAW,CAAoB;QAC/B,SAAI,GAAJ,IAAI,CAAgB;QAEpB,SAAI,GAAJ,IAAI,CAAqB;QACzB,YAAO,GAAP,OAAO,CAAc;IAC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;YACpD,WAAW,CAAC,2BAAmB,CAAC,CAAC,cAAc,CAAC,CAAC;QAClD,CAAC;QACD,OAAO;YACN,OAAO,EAAE,cAAc,CAAC,OAAO;YAC/B,eAAe,EAAE,cAAc,CAAC,eAAe;SAC/C,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport { NodeKind } from \"@fluidframework/tree\";\nimport type { z } from \"zod\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\n\n/**\n * A utility type that extracts the method keys from a given type.\n * @alpha\n */\nexport type MethodKeys<T> = {\n\t[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;\n};\n\n/**\n * A type that represents a constructor function.\n * @alpha\n */\nexport type Ctor<T = any> = new (...args: any[]) => T;\n\n/**\n * A type that represents an object schema class.\n * @alpha\n */\nexport type BindableSchema =\n\t| TreeNodeSchema<string, NodeKind.Object>\n\t| TreeNodeSchema<string, NodeKind.Record>\n\t| TreeNodeSchema<string, NodeKind.Array>\n\t| TreeNodeSchema<string, NodeKind.Map>;\n\n/**\n * A type guard to check if a schema is {@link BindableSchema | bindable}.\n */\nexport function isBindableSchema(schema: TreeNodeSchema): schema is BindableSchema {\n\treturn (\n\t\tschema.kind === NodeKind.Object ||\n\t\tschema.kind === NodeKind.Record ||\n\t\tschema.kind === NodeKind.Array ||\n\t\tschema.kind === NodeKind.Map\n\t);\n}\n\n/**\n * Get the exposed methods of a schema class.\n * @param schemaClass - The schema class to extract methods from.\n * @returns A record of method names and their corresponding Zod types.\n */\nexport function getExposedMethods(schemaClass: BindableSchema): {\n\tmethods: Record<string, FunctionWrapper>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedMethodsI.getExposedMethods(schemaClass);\n}\n\n/**\n * A type that represents a function argument.\n * @alpha\n */\nexport type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string, type: T];\n\n/**\n * A function definition interface that describes the structure of a function.\n * @alpha\n */\nexport interface FunctionDef<\n\tArgs extends readonly Arg[],\n\tReturn extends z.ZodTypeAny,\n\tRest extends z.ZodTypeAny | null = null,\n> {\n\tdescription?: string;\n\targs: Args;\n\trest?: Rest;\n\treturns: Return;\n}\n\n/**\n * A class that implements the FunctionDef interface.\n */\nexport class FunctionWrapper\n\timplements FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>\n{\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly description: string | undefined,\n\t\tpublic readonly args: readonly Arg[],\n\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\tpublic readonly rest: z.ZodTypeAny | null,\n\t\tpublic readonly returns: z.ZodTypeAny,\n\t) {}\n}\n\n/**\n * A utility type that extracts the argument types from a function definition.\n * @alpha\n */\nexport type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Single extends Arg]\n\t? [Single[1]]\n\t: T extends readonly [infer Head extends Arg, ...infer Tail extends readonly Arg[]]\n\t\t? [Head[1], ...ArgsTuple<Tail>]\n\t\t: never;\n\n/**\n * A utility function to build a function definition.\n * @alpha\n */\nexport function buildFunc<\n\tconst Return extends z.ZodTypeAny,\n\tconst Args extends readonly Arg[],\n\tconst Rest extends z.ZodTypeAny | null = null,\n>(\n\tdef: { description?: string; returns: Return; rest?: Rest },\n\t...args: Args\n): FunctionDef<Args, Return, Rest> {\n\treturn {\n\t\tdescription: def.description,\n\t\treturns: def.returns,\n\t\targs,\n\t\trest: def.rest,\n\t};\n}\n\n/**\n * A utility type that infers the return type of a function definition.\n * @alpha\n */\nexport type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Rest>\n\t? z.infer<z.ZodFunction<z.ZodTuple<ArgsTuple<Args>, Rest>, Return>>\n\t: never;\n\n/**\n * An interface for exposing methods of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedMethods {\n\texpose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,\n\t\tZ extends FunctionDef<any, any, any>,\n\t>(schema: S, methodName: K, zodFunction: Z): void;\n\n\t/**\n\t * Create a Zod schema for a SharedTree schema class.\n\t * @remarks\n\t * Use it to \"wrap\" schema types that are referenced as arguments or return types when exposing methods (with {@link ExposedMethods.expose}).\n\t */\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * A symbol used to expose methods to the LLM.\n * @alpha\n */\nexport const exposeMethodsSymbol: unique symbol = Symbol(\"run\");\n\n/**\n * An interface that SharedTree schema classes should implement to expose their methods to the LLM.\n *\n * @remarks\n * The `getExposedMethods` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedMethods {\n\t[exposeMethodsSymbol](methods: ExposedMethods): void;\n}\n\nclass ExposedMethodsI implements ExposedMethods {\n\tprivate readonly methods: Record<string, FunctionWrapper> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic expose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,\n\t\tZ extends FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>,\n\t>(schema: S, methodName: K, functionDef: Z): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose methods on the \"this\" object');\n\t\t}\n\t\tthis.methods[methodName] = new FunctionWrapper(\n\t\t\tmethodName,\n\t\t\tfunctionDef.description,\n\t\t\tfunctionDef.args,\n\t\t\t// eslint-disable-next-line unicorn/no-null\n\t\t\tfunctionDef.rest ?? null,\n\t\t\tfunctionDef.returns,\n\t\t);\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedMethods(schemaClass: BindableSchema): {\n\t\tmethods: Record<string, FunctionWrapper>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposedMethods = new ExposedMethodsI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedMethods;\n\t\tif (extractable[exposeMethodsSymbol] !== undefined) {\n\t\t\textractable[exposeMethodsSymbol](exposedMethods);\n\t\t}\n\t\treturn {\n\t\t\tmethods: exposedMethods.methods,\n\t\t\treferencedTypes: exposedMethods.referencedTypes,\n\t\t};\n\t}\n}\n"]}
|
package/dist/prompt.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,mBAAmB,EAAe,MAAM,sBAAsB,CAAC;AAC7E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,mBAAmB,EAAe,MAAM,sBAAsB,CAAC;AAC7E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAI5C;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE;IAC/B,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC,CAAC;IAChE,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,GAAG,MAAM,CAgRT;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,aAAa,CAAC,mBAAmB,CAAC,GAAG,MAAM,CA8C9E"}
|
package/dist/prompt.js
CHANGED
|
@@ -49,18 +49,8 @@ function getPrompt(args) {
|
|
|
49
49
|
// No default
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
const { domainTypes } = (0, typeGeneration_js_1.generateEditTypesForPrompt)(schema, (0, alpha_1.getSimpleSchema)(schema));
|
|
53
|
-
for (const [key, value] of Object.entries(domainTypes)) {
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
55
|
-
delete domainTypes[key];
|
|
56
|
-
if ((0, utils_js_1.isNamedSchema)(key)) {
|
|
57
|
-
const friendlyKey = (0, utils_js_1.unqualifySchema)(key);
|
|
58
|
-
domainTypes[friendlyKey] = value;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
52
|
const stringified = stringifyTree(field);
|
|
62
|
-
const
|
|
63
|
-
const typescriptSchemaTypes = (0, utils_js_1.getZodSchemaAsTypeScript)(domainTypes, details);
|
|
53
|
+
const { schemaText: typescriptSchemaTypes, hasHelperMethods } = (0, typeGeneration_js_1.generateEditTypesForPrompt)(schema, (0, alpha_1.getSimpleSchema)(schema));
|
|
64
54
|
const exampleTypeName = nodeTypeUnion === undefined
|
|
65
55
|
? undefined
|
|
66
56
|
: nodeTypeUnion
|
|
@@ -154,7 +144,7 @@ function getPrompt(args) {
|
|
|
154
144
|
key(child: TreeData): string | number;
|
|
155
145
|
}
|
|
156
146
|
\`\`\``;
|
|
157
|
-
const helperMethodExplanation =
|
|
147
|
+
const helperMethodExplanation = hasHelperMethods
|
|
158
148
|
? `Manipulating the data using the APIs described below is allowed, but when possible ALWAYS prefer to use any application helper methods exposed on the schema TypeScript types if the goal can be accomplished that way.
|
|
159
149
|
It will often not be possible to fully accomplish the goal using those helpers. When this is the case, mutate the objects as normal, taking into account the following guidance.`
|
|
160
150
|
: "";
|
package/dist/prompt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA0D;AAC1D,+CAAgE;AAGhE,sDAA6D;AAC7D,4DAAqE;AAGrE,2DAAiE;AACjE,yCAQoB;AAEpB;;GAEG;AACH,SAAgB,SAAS,CAAC,IAIzB;IACA,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IACpD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAClC,MAAM,kBAAkB,GAAG,WAAW,CAAC;IACvC,MAAM,gBAAgB,GAAG,SAAS,CAAC;IACnC,6IAA6I;IAC7I,MAAM,SAAS,GAAG,CAAC,GAAG,IAAA,+BAAoB,EAAC,MAAM,CAAC,CAAC,cAAc,CAAC,CAAC;IACnE,MAAM,aAAa,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,0BAAe,EAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;IAChF,IAAI,aAAiC,CAAC;IACtC,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,iBAAqC,CAAC;IAC1C,KAAK,MAAM,CAAC,IAAI,IAAA,sBAAW,EAAC,MAAM,CAAC,EAAE,CAAC;QACrC,IAAI,CAAC,CAAC,IAAI,KAAK,eAAQ,CAAC,IA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyDxF,CAAC;AACH,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { oob } from \"@fluidframework/core-utils/internal\";\nimport { NodeKind, Tree, TreeNode } from \"@fluidframework/tree\";\nimport type { ImplicitFieldSchema, TreeMapNode } from \"@fluidframework/tree\";\nimport type { ReadableField } from \"@fluidframework/tree/alpha\";\nimport { getSimpleSchema } from \"@fluidframework/tree/alpha\";\nimport { normalizeFieldSchema } from \"@fluidframework/tree/internal\";\n\nimport type { Subtree } from \"./subtree.js\";\nimport { generateEditTypesForPrompt } from \"./typeGeneration.js\";\nimport {\n\tgetFriendlyName,\n\tgetZodSchemaAsTypeScript,\n\tisNamedSchema,\n\tcommunize,\n\tunqualifySchema,\n\ttype SchemaDetails,\n\tfindSchemas,\n} from \"./utils.js\";\n\n/**\n * Produces a \"system\" prompt for the tree agent, based on the provided subtree.\n */\nexport function getPrompt(args: {\n\tsubtree: Pick<Subtree<ImplicitFieldSchema>, \"schema\" | \"field\">;\n\teditToolName: string | undefined;\n\tdomainHints?: string;\n}): string {\n\tconst { subtree, editToolName, domainHints } = args;\n\tconst { field, schema } = subtree;\n\tconst arrayInterfaceName = \"TreeArray\";\n\tconst mapInterfaceName = \"TreeMap\";\n\t// Inspect the schema to determine what kinds of nodes are possible - this will affect how much information we need to include in the prompt.\n\tconst rootTypes = [...normalizeFieldSchema(schema).allowedTypeSet];\n\tconst rootTypeUnion = `${rootTypes.map((t) => getFriendlyName(t)).join(\" | \")}`;\n\tlet nodeTypeUnion: string | undefined;\n\tlet hasArrays = false;\n\tlet hasMaps = false;\n\tlet exampleObjectName: string | undefined;\n\tfor (const s of findSchemas(schema)) {\n\t\tif (s.kind !== NodeKind.Leaf) {\n\t\t\tnodeTypeUnion =\n\t\t\t\tnodeTypeUnion === undefined\n\t\t\t\t\t? getFriendlyName(s)\n\t\t\t\t\t: `${nodeTypeUnion} | ${getFriendlyName(s)}`;\n\t\t}\n\n\t\tswitch (s.kind) {\n\t\t\tcase NodeKind.Array: {\n\t\t\t\thasArrays = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\thasMaps = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase NodeKind.Object: {\n\t\t\t\texampleObjectName ??= getFriendlyName(s);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// No default\n\t\t}\n\t}\n\n\tconst { domainTypes } = generateEditTypesForPrompt(schema, getSimpleSchema(schema));\n\tfor (const [key, value] of Object.entries(domainTypes)) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n\t\tdelete domainTypes[key];\n\t\tif (isNamedSchema(key)) {\n\t\t\tconst friendlyKey = unqualifySchema(key);\n\t\t\tdomainTypes[friendlyKey] = value;\n\t\t}\n\t}\n\n\tconst stringified = stringifyTree(field);\n\tconst details: SchemaDetails = { hasHelperMethods: false };\n\tconst typescriptSchemaTypes = getZodSchemaAsTypeScript(domainTypes, details);\n\tconst exampleTypeName =\n\t\tnodeTypeUnion === undefined\n\t\t\t? undefined\n\t\t\t: nodeTypeUnion\n\t\t\t\t\t.split(\"|\")\n\t\t\t\t\t.map((part) => part.trim())\n\t\t\t\t\t.find((part) => part.length > 0);\n\n\tconst createDocs =\n\t\texampleObjectName === undefined\n\t\t\t? \"\"\n\t\t\t: `\\n\t/**\n\t * A collection of builder functions for creating new tree nodes.\n\t * @remarks\n\t * Each property on this object is named after a type in the tree schema.\n\t * Call the corresponding function to create a new node of that type.\n\t * Always use these builder functions when creating new nodes rather than plain JavaScript objects.\n\t *\n\t * For example:\n\t *\n\t * \\`\\`\\`javascript\n\t * // This creates a new ${exampleObjectName} object:\n\t * const ${communize(exampleObjectName)} = context.create.${exampleObjectName}({ ...properties });\n\t * // Don't do this:\n\t * // const ${communize(exampleObjectName)} = { ...properties };\n\t * \\`\\`\\`\n\t */\n\tcreate: Record<string, <T extends TreeData>(input: T) => T>;\\n`;\n\n\tconst isDocs =\n\t\texampleTypeName === undefined\n\t\t\t? \"\"\n\t\t\t: `\\n\t/**\n\t * A collection of type-guard functions for data in the tree.\n\t * @remarks\n\t * Each property on this object is named after a type in the tree schema.\n\t * Call the corresponding function to check if a node is of that specific type.\n\t * This is useful when working with nodes that could be one of multiple types.\n\t *\n\t * ${`Example: Check if a node is a ${exampleTypeName} with \\`if (context.is.${exampleTypeName}(node)) {}\\``}\n\t */\n\tis: Record<string, <T extends TreeData>(data: unknown) => data is T>;\n\n\t/**\n\t * Checks if the provided data is an array.\n\t * @remarks\n\t * DO NOT use \\`Array.isArray\\` to check if tree data is an array - use this function instead.\n\t *\n\t * This function will also work for native JavaScript arrays.\n\t *\n\t * ${`Example: \\`if (context.isArray(node)) {}\\``}\n\t */\n\tisArray(data: any): boolean;\n\n\t/**\n\t * Checks if the provided data is a map.\n\t * @remarks\n\t * DO NOT use \\`instanceof Map\\` to check if tree data is a map - use this function instead.\n\t *\n\t * This function will also work for native JavaScript Map instances.\n\t *\n\t * ${`Example: \\`if (context.isMap(node)) {}\\``}\n\t */\n\tisMap(data: any): boolean;\\n`;\n\n\tconst context = `\\`\\`\\`typescript\n\t${nodeTypeUnion === undefined ? \"\" : `type TreeData = ${nodeTypeUnion};\\n\\n`}\t/**\n\t * An object available to generated code which provides read and write access to the tree as well as utilities for creating and inspecting data in the tree.\n\t * @remarks This object is available as a variable named \\`context\\` in the scope of the generated JavaScript snippet.\n\t */\n\tinterface Context<TSchema extends ImplicitFieldSchema> {\n\t/**\n\t * The root of the tree that can be read or mutated.\n\t * @remarks\n\t * You can read properties and navigate through the tree starting from this root.\n\t * You can also assign a new value to this property to replace the entire tree, as long as the new value is one of the types allowed at the root.\n\t *\n\t * Example: Read the current root with \\`const currentRoot = context.root;\\`\n\t *${rootTypes.length > 0 ? ` Example: Replace the entire root with \\`context.root = context.create.${getFriendlyName(rootTypes[0] ?? oob())}({ });\\`\\n\t *` : \"\"}/\n\troot: ReadableField<TSchema>;\n\t${createDocs}\n\t${isDocs}\n\t/**\n\t * Returns the parent object/array/map of the given object/array/map, if there is one.\n\t * @returns The parent node, or \\`undefined\\` if the node is the root or is not in the tree.\n\t * @remarks\n\t * Example: Get the parent with \\`const parent = context.parent(child);\\`\n\t */\n\tparent(child: TreeData): TreeData | undefined;\n\n\t/**\n\t * Returns the property key or index of the given object/array/map within its parent.\n\t * @returns A string key if the child is in an object or map, or a numeric index if the child is in an array.\n\t *\n\t * Example: \\`const key = context.key(child);\\`\n\t */\n\tkey(child: TreeData): string | number;\n}\n\\`\\`\\``;\n\n\tconst helperMethodExplanation = details.hasHelperMethods\n\t\t? `Manipulating the data using the APIs described below is allowed, but when possible ALWAYS prefer to use any application helper methods exposed on the schema TypeScript types if the goal can be accomplished that way.\nIt will often not be possible to fully accomplish the goal using those helpers. When this is the case, mutate the objects as normal, taking into account the following guidance.`\n\t\t: \"\";\n\n\tconst reinsertionExplanation = `Once non-primitive data has been removed from the tree (e.g. replaced via assignment, or removed from an array), that data cannot be re-inserted into the tree.\nInstead, it must be deep cloned and recreated.\n${\n\texampleObjectName === undefined\n\t\t? \"\"\n\t\t: `For example:\n\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst ${communize(exampleObjectName)} = parent.${communize(exampleObjectName)};\nparent.${communize(exampleObjectName)} = undefined;\n// \\`${communize(exampleObjectName)}\\` cannot be directly re-inserted into the tree - this will throw an error:\n// parent.${communize(exampleObjectName)} = ${communize(exampleObjectName)}; // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\nparent.${communize(exampleObjectName)} = context.create.${exampleObjectName}({ /*... deep clone all properties from \\`${communize(exampleObjectName)}\\` */ });\n\\`\\`\\`${\n\t\t\t\thasArrays\n\t\t\t\t\t? `\\n\\nThe same applies when using arrays:\\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst item = arrayOf${exampleObjectName}[0];\narrayOf${exampleObjectName}.removeAt(0);\n// \\`item\\` cannot be directly re-inserted into the tree - this will throw an error:\narrayOf${exampleObjectName}.insertAt(0, item); // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\narrayOf${exampleObjectName}.insertAt(0, context.create.${exampleObjectName}({ /*... deep clone all properties from \\`item\\` */ }));\n\\`\\`\\``\n\t\t\t\t\t: \"\"\n\t\t\t}${\n\t\t\t\thasMaps\n\t\t\t\t\t? `\\n\\nThe same applies when using maps:\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst value = mapOf${exampleObjectName}.get(\"someKey\");\nmapOf${exampleObjectName}.delete(\"someKey\");\n// \\`value\\` cannot be directly re-inserted into the tree - this will throw an error:\nmapOf${exampleObjectName}.set(\"someKey\", value); // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\nmapOf${exampleObjectName}.set(\"someKey\", context.create.${exampleObjectName}({ /*... deep clone all properties from \\`value\\` */ }));\n\\`\\`\\``\n\t\t\t\t\t: \"\"\n\t\t\t}`\n}`;\n\n\tconst arrayEditing = `#### Editing Arrays\n\nThe arrays in the tree are somewhat different than normal JavaScript \\`Array\\`s.\nRead-only operations are generally the same - you can create them, read via index, and call non-mutating methods like \\`concat\\`, \\`map\\`, \\`filter\\`, \\`find\\`, \\`forEach\\`, \\`indexOf\\`, \\`slice\\`, \\`join\\`, etc.\nHowever, write operations (e.g. index assignment, \\`push\\`, \\`pop\\`, \\`splice\\`, etc.) are not supported.\nInstead, you must use the methods on the following interface to mutate the array:\n\n\\`\\`\\`typescript\n${getTreeArrayNodeDocumentation(arrayInterfaceName)}\n\\`\\`\\`\n\nWhen possible, ensure that the edits preserve the identity of objects already in the tree.\nFor example, prefer \\`array.moveToIndex\\` over \\`array.removeAt\\` + \\`array.insertAt\\` and prefer \\`array.moveRangeToIndex\\` over \\`array.removeRange\\` + \\`array.insertAt\\`.\n\n`;\n\n\tconst mapEditing = `#### Editing Maps\n\nThe maps in the tree are somewhat different than normal JavaScript \\`Map\\`s.\nMap keys are always strings.\nRead-only operations are generally the same - you can create them, read via \\`get\\`, and call non-mutating methods like \\`has\\`, \\`forEach\\`, \\`entries\\`, \\`keys\\`, \\`values\\`, etc. (note the subtle differences around return values and iteration order).\nHowever, write operations (e.g. \\`set\\`, \\`delete\\`, etc.) are not supported.\nInstead, you must use the methods on the following interface to mutate the map:\n\n\\`\\`\\`typescript\n${getTreeMapNodeDocumentation(mapInterfaceName)}\n\\`\\`\\`\n\n`;\n\n\tconst editing = `If the user asks you to edit the tree, you should author a snippet of JavaScript code to accomplish the user-specified goal, following the instructions for editing detailed below.\nYou must use the \"${editToolName}\" tool to run the generated code.\nAfter editing the tree, review the latest state of the tree to see if it satisfies the user's request.\nIf it does not, or if you receive an error, you may try again with a different approach.\nOnce the tree is in the desired state, you should inform the user that the request has been completed.\n\n### Editing\n\nIf the user asks you to edit the document, you will write a snippet of JavaScript code that mutates the data in-place to achieve the user's goal.\nThe snippet may be synchronous or asynchronous (i.e. it may \\`await\\` functions if necessary).\nThe snippet has a \\`context\\` variable in its scope.\nThis \\`context\\` variable holds the current state of the tree in the \\`root\\` property.\nYou may mutate any part of this tree as necessary, taking into account the caveats around${hasArrays ? ` arrays${hasMaps ? \" and\" : \"\"}` : \"\"}${hasMaps ? \" maps\" : \"\"} detailed below.\nYou may also set the \\`root\\` property of the context to be an entirely new value as long as it is one of the types allowed at the root of the tree (\\`${rootTypeUnion}\\`).\nYou should also use the \\`context\\` object to create new data to insert into the tree, using the builder functions available on the \\`create\\` property.\nThere are other additional helper functions available on the \\`context\\` object to help you analyze the tree.\nHere is the definition of the \\`Context\\` interface:\n${context}\n${helperMethodExplanation}\n${hasArrays ? arrayEditing : \"\"}${hasMaps ? mapEditing : \"\"}#### Additional Notes\n\nBefore outputting the edit function, you should check that it is valid according to both the application tree's schema and any restrictions of the editing APIs described above.\n\n${reinsertionExplanation}\n\nFinally, double check that the edits would accomplish the user's request (if it is possible).\n\n`;\n\n\tconst prompt = `You are a helpful assistant collaborating with the user on a document. The document state is a JSON tree, and you are able to analyze and edit it.\nThe JSON tree adheres to the following Typescript schema:\n\n\\`\\`\\`typescript\n${typescriptSchemaTypes}\n\\`\\`\\`\n\nIf the user asks you a question about the tree, you should inspect the state of the tree and answer the question.\nWhen answering such a question, DO NOT answer with information that is not part of the document unless requested to do so.\n\n${editToolName === undefined ? \"\" : editing}### Application data\n\n${\n\tdomainHints === undefined\n\t\t? \"\"\n\t\t: `\\nThe application supplied the following additional instructions: ${domainHints}`\n}\nThe current state of \\`context.root\\` (a \\`${field === undefined ? \"undefined\" : getFriendlyName(Tree.schema(field))}\\`) is:\n\n\\`\\`\\`JSON\n${stringified}\n\\`\\`\\``;\n\treturn prompt;\n}\n\n/**\n * Serializes tree data e.g. to include in a prompt or message.\n * @remarks This includes some extra metadata to make it easier to understand the structure of the tree.\n */\nexport function stringifyTree(tree: ReadableField<ImplicitFieldSchema>): string {\n\tconst typeReplacementKey = \"_e944da5a5fd04ea2b8b2eb6109e089ed\";\n\tconst indexReplacementKey = \"_27bb216b474d45e6aaee14d1ec267b96\";\n\tconst mapReplacementKey = \"_a0d98d22a1c644539f07828d3f064d71\";\n\tconst stringified = JSON.stringify(\n\t\ttree,\n\t\t(_, node: unknown) => {\n\t\t\tif (node instanceof TreeNode) {\n\t\t\t\tconst key = Tree.key(node);\n\t\t\t\tconst index = typeof key === \"number\" ? key : undefined;\n\t\t\t\tconst schema = Tree.schema(node);\n\t\t\t\tswitch (schema.kind) {\n\t\t\t\t\tcase NodeKind.Object: {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t[typeReplacementKey]: getFriendlyName(schema),\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t...node,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tcase NodeKind.Map: {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t[mapReplacementKey]: \"\",\n\t\t\t\t\t\t\t...Object.fromEntries(node as TreeMapNode),\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\treturn {\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t...node,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn node;\n\t\t},\n\t\t2,\n\t);\n\n\treturn stringified\n\t\t.replace(new RegExp(`\"${typeReplacementKey}\":`, \"g\"), `// Type:`)\n\t\t.replace(new RegExp(`\"${indexReplacementKey}\":`, \"g\"), `// Index:`)\n\t\t.replace(\n\t\t\tnew RegExp(`\"${mapReplacementKey}\": \"\"`, \"g\"),\n\t\t\t`// Note: This is a map that has been serialized to JSON. It is not a key-value object/record but is being printed as such.`,\n\t\t);\n}\n\n/**\n * Retrieves the documentation for the `TreeArrayNode` interface to feed to the LLM.\n * @remarks The documentation has been simplified in various ways to make it easier for the LLM to understand.\n * @privateRemarks TODO: How do we keep this in sync with the actual `TreeArrayNode` docs if/when those docs change?\n */\nfunction getTreeArrayNodeDocumentation(typeName: string): string {\n\treturn `/** A special type of array which implements 'readonly T[]' (i.e. it supports all read-only JS array methods) and provides custom array mutation APIs. */\nexport interface ${typeName}<T> extends ReadonlyArray<T> {\n\t/**\n\t * Inserts new item(s) at a specified location.\n\t * @param index - The index at which to insert \\`value\\`.\n\t * @param value - The content to insert.\n\t * @throws Throws if \\`index\\` is not in the range [0, \\`array.length\\`).\n\t */\n\tinsertAt(index: number, ...value: readonly T[]): void;\n\n\t/**\n\t * Removes the item at the specified location.\n\t * @param index - The index at which to remove the item.\n\t * @throws Throws if \\`index\\` is not in the range [0, \\`array.length\\`).\n\t */\n\tremoveAt(index: number): void;\n\n\t/**\n\t * Removes all items between the specified indices.\n\t * @param start - The starting index of the range to remove (inclusive). Defaults to the start of the array.\n\t * @param end - The ending index of the range to remove (exclusive). Defaults to \\`array.length\\`.\n\t * @throws Throws if \\`start\\` is not in the range [0, \\`array.length\\`].\n\t * @throws Throws if \\`end\\` is less than \\`start\\`.\n\t * If \\`end\\` is not supplied or is greater than the length of the array, all items after \\`start\\` are removed.\n\t *\n\t * @remarks\n\t * The default values for start and end are computed when this is called,\n\t * and thus the behavior is the same as providing them explicitly, even with respect to merge resolution with concurrent edits.\n\t * For example, two concurrent transactions both emptying the array with \\`node.removeRange()\\` then inserting an item,\n\t * will merge to result in the array having both inserted items.\n\t */\n\tremoveRange(start?: number, end?: number): void;\n\n\t/**\n\t * Moves the specified item to the desired location in the array.\n\t *\n\t * WARNING - This API is easily misused.\n\t * Please read the documentation for the \\`destinationGap\\` parameter carefully.\n\t *\n\t * @param destinationGap - The location *between* existing items that the moved item should be moved to.\n\t *\n\t * WARNING - \\`destinationGap\\` describes a location between existing items *prior to applying the move operation*.\n\t *\n\t * For example, if the array contains items \\`[A, B, C]\\` before the move, the \\`destinationGap\\` must be one of the following:\n\t *\n\t * - \\`0\\` (between the start of the array and \\`A\\`'s original position)\n\t * - \\`1\\` (between \\`A\\`'s original position and \\`B\\`'s original position)\n\t * - \\`2\\` (between \\`B\\`'s original position and \\`C\\`'s original position)\n\t * - \\`3\\` (between \\`C\\`'s original position and the end of the array)\n\t *\n\t * So moving \\`A\\` between \\`B\\` and \\`C\\` would require \\`destinationGap\\` to be \\`2\\`.\n\t *\n\t * This interpretation of \\`destinationGap\\` makes it easy to specify the desired destination relative to a sibling item that is not being moved,\n\t * or relative to the start or end of the array:\n\t *\n\t * - Move to the start of the array: \\`array.moveToIndex(0, ...)\\` (see also \\`moveToStart\\`)\n\t * - Move to before some item X: \\`array.moveToIndex(indexOfX, ...)\\`\n\t * - Move to after some item X: \\`array.moveToIndex(indexOfX + 1\\`, ...)\n\t * - Move to the end of the array: \\`array.moveToIndex(array.length, ...)\\` (see also \\`moveToEnd\\`)\n\t *\n\t * This interpretation of \\`destinationGap\\` does however make it less obvious how to move an item relative to its current position:\n\t *\n\t * - Move item B before its predecessor: \\`array.moveToIndex(indexOfB - 1, ...)\\`\n\t * - Move item B after its successor: \\`array.moveToIndex(indexOfB + 2, ...)\\`\n\t *\n\t * Notice the asymmetry between \\`-1\\` and \\`+2\\` in the above examples.\n\t * In such scenarios, it can often be easier to approach such edits by swapping adjacent items:\n\t * If items A and B are adjacent, such that A precedes B,\n\t * then they can be swapped with \\`array.moveToIndex(indexOfA, indexOfB)\\`.\n\t *\n\t * @param sourceIndex - The index of the item to move.\n\t * @param source - The optional source array to move the item out of (defaults to this array).\n\t * @throws Throws if any of the source index is not in the range [0, \\`array.length\\`),\n\t * or if the index is not in the range [0, \\`array.length\\`].\n\t */\n\tmoveToIndex(destinationGap: number, sourceIndex: number, source?: ${typeName}<T>): void;\n\n\t/**\n\t * Moves the specified items to the desired location within the array.\n\t *\n\t * WARNING - This API is easily misused.\n\t * Please read the documentation for the \\`destinationGap\\` parameter carefully.\n\t *\n\t * @param destinationGap - The location *between* existing items that the moved item should be moved to.\n\t *\n\t * WARNING - \\`destinationGap\\` describes a location between existing items *prior to applying the move operation*.\n\t *\n\t * For example, if the array contains items \\`[A, B, C]\\` before the move, the \\`destinationGap\\` must be one of the following:\n\t *\n\t * - \\`0\\` (between the start of the array and \\`A\\`'s original position)\n\t * - \\`1\\` (between \\`A\\`'s original position and \\`B\\`'s original position)\n\t * - \\`2\\` (between \\`B\\`'s original position and \\`C\\`'s original position)\n\t * - \\`3\\` (between \\`C\\`'s original position and the end of the array)\n\t *\n\t * So moving \\`A\\` between \\`B\\` and \\`C\\` would require \\`destinationGap\\` to be \\`2\\`.\n\t *\n\t * This interpretation of \\`destinationGap\\` makes it easy to specify the desired destination relative to a sibling item that is not being moved,\n\t * or relative to the start or end of the array:\n\t *\n\t * - Move to the start of the array: \\`array.moveToIndex(0, ...)\\` (see also \\`moveToStart\\`)\n\t * - Move to before some item X: \\`array.moveToIndex(indexOfX, ...)\\`\n\t * - Move to after some item X: \\`array.moveToIndex(indexOfX + 1\\`, ...)\n\t * - Move to the end of the array: \\`array.moveToIndex(array.length, ...)\\` (see also \\`moveToEnd\\`)\n\t *\n\t * This interpretation of \\`destinationGap\\` does however make it less obvious how to move an item relative to its current position:\n\t *\n\t * - Move item B before its predecessor: \\`array.moveToIndex(indexOfB - 1, ...)\\`\n\t * - Move item B after its successor: \\`array.moveToIndex(indexOfB + 2, ...)\\`\n\t *\n\t * Notice the asymmetry between \\`-1\\` and \\`+2\\` in the above examples.\n\t * In such scenarios, it can often be easier to approach such edits by swapping adjacent items:\n\t * If items A and B are adjacent, such that A precedes B,\n\t * then they can be swapped with \\`array.moveToIndex(indexOfA, indexOfB)\\`.\n\t *\n\t * @param sourceStart - The starting index of the range to move (inclusive).\n\t * @param sourceEnd - The ending index of the range to move (exclusive)\n\t * @param source - The optional source array to move items out of (defaults to this array).\n\t * @throws Throws if the types of any of the items being moved are not allowed in the destination array,\n\t * if any of the input indices are not in the range [0, \\`array.length\\`], or if \\`sourceStart\\` is greater than \\`sourceEnd\\`.\n\t */\n\tmoveRangeToIndex(\n\t\tdestinationGap: number,\n\t\tsourceStart: number,\n\t\tsourceEnd: number,\n\t\tsource?: ${typeName}<T>,\n\t): void;\n}`;\n}\n\n/**\n * Retrieves the documentation for the `TreeMapNode` interface to feed to the LLM.\n * @remarks The documentation has been simplified in various ways to make it easier for the LLM to understand.\n * @privateRemarks TODO: How do we keep this in sync with the actual `TreeMapNode` docs if/when those docs change?\n */\nfunction getTreeMapNodeDocumentation(typeName: string): string {\n\treturn `/**\n * A map of string keys to tree objects.\n */\nexport interface ${typeName}<T> extends ReadonlyMap<string, T> {\n\t/**\n\t * Adds or updates an entry in the map with a specified \\`key\\` and a \\`value\\`.\n\t *\n\t * @param key - The key of the element to add to the map.\n\t * @param value - The value of the element to add to the map.\n\t *\n\t * @remarks\n\t * Setting the value at a key to \\`undefined\\` is equivalent to calling {@link ${typeName}.delete} with that key.\n\t */\n\tset(key: string, value: T | undefined): void;\n\n\t/**\n\t * Removes the specified element from this map by its \\`key\\`.\n\t *\n\t * @remarks\n\t * Note: unlike JavaScript's Map API, this method does not return a flag indicating whether or not the value was\n\t * deleted.\n\t *\n\t * @param key - The key of the element to remove from the map.\n\t */\n\tdelete(key: string): void;\n\n\t/**\n\t * Returns an iterable of keys in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the keys returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tkeys(): IterableIterator<string>;\n\n\t/**\n\t * Returns an iterable of values in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the values returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tvalues(): IterableIterator<T>;\n\n\t/**\n\t * Returns an iterable of key/value pairs for every entry in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the entries returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tentries(): IterableIterator<[string, T]>;\n\n\t/**\n\t * Executes the provided function once per each key/value pair in this map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order in which the function is called with respect to the map's entries.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tforEach(\n\t\tcallbackfn: (\n\t\t\tvalue: T,\n\t\t\tkey: string,\n\t\t\tmap: ReadonlyMap<string, T>,\n\t\t) => void,\n\t\tthisArg?: any,\n\t): void;\n}`;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA0D;AAC1D,+CAAgE;AAGhE,sDAA6D;AAC7D,4DAAqE;AAGrE,2DAAiE;AACjE,yCAAqE;AAErE;;GAEG;AACH,SAAgB,SAAS,CAAC,IAIzB;IACA,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IACpD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,C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AED;;;;GAIG;AACH,SAAS,2BAA2B,CAAC,QAAgB;IACpD,OAAO;;;mBAGW,QAAQ;;;;;;;;kFAQuD,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyDxF,CAAC;AACH,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { oob } from \"@fluidframework/core-utils/internal\";\nimport { NodeKind, Tree, TreeNode } from \"@fluidframework/tree\";\nimport type { ImplicitFieldSchema, TreeMapNode } from \"@fluidframework/tree\";\nimport type { ReadableField } from \"@fluidframework/tree/alpha\";\nimport { getSimpleSchema } from \"@fluidframework/tree/alpha\";\nimport { normalizeFieldSchema } from \"@fluidframework/tree/internal\";\n\nimport type { Subtree } from \"./subtree.js\";\nimport { generateEditTypesForPrompt } from \"./typeGeneration.js\";\nimport { getFriendlyName, communize, findSchemas } from \"./utils.js\";\n\n/**\n * Produces a \"system\" prompt for the tree agent, based on the provided subtree.\n */\nexport function getPrompt(args: {\n\tsubtree: Pick<Subtree<ImplicitFieldSchema>, \"schema\" | \"field\">;\n\teditToolName: string | undefined;\n\tdomainHints?: string;\n}): string {\n\tconst { subtree, editToolName, domainHints } = args;\n\tconst { field, schema } = subtree;\n\tconst arrayInterfaceName = \"TreeArray\";\n\tconst mapInterfaceName = \"TreeMap\";\n\t// Inspect the schema to determine what kinds of nodes are possible - this will affect how much information we need to include in the prompt.\n\tconst rootTypes = [...normalizeFieldSchema(schema).allowedTypeSet];\n\tconst rootTypeUnion = `${rootTypes.map((t) => getFriendlyName(t)).join(\" | \")}`;\n\tlet nodeTypeUnion: string | undefined;\n\tlet hasArrays = false;\n\tlet hasMaps = false;\n\tlet exampleObjectName: string | undefined;\n\tfor (const s of findSchemas(schema)) {\n\t\tif (s.kind !== NodeKind.Leaf) {\n\t\t\tnodeTypeUnion =\n\t\t\t\tnodeTypeUnion === undefined\n\t\t\t\t\t? getFriendlyName(s)\n\t\t\t\t\t: `${nodeTypeUnion} | ${getFriendlyName(s)}`;\n\t\t}\n\n\t\tswitch (s.kind) {\n\t\t\tcase NodeKind.Array: {\n\t\t\t\thasArrays = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\thasMaps = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase NodeKind.Object: {\n\t\t\t\texampleObjectName ??= getFriendlyName(s);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// No default\n\t\t}\n\t}\n\n\tconst stringified = stringifyTree(field);\n\tconst { schemaText: typescriptSchemaTypes, hasHelperMethods } = generateEditTypesForPrompt(\n\t\tschema,\n\t\tgetSimpleSchema(schema),\n\t);\n\tconst exampleTypeName =\n\t\tnodeTypeUnion === undefined\n\t\t\t? undefined\n\t\t\t: nodeTypeUnion\n\t\t\t\t\t.split(\"|\")\n\t\t\t\t\t.map((part) => part.trim())\n\t\t\t\t\t.find((part) => part.length > 0);\n\n\tconst createDocs =\n\t\texampleObjectName === undefined\n\t\t\t? \"\"\n\t\t\t: `\\n\t/**\n\t * A collection of builder functions for creating new tree nodes.\n\t * @remarks\n\t * Each property on this object is named after a type in the tree schema.\n\t * Call the corresponding function to create a new node of that type.\n\t * Always use these builder functions when creating new nodes rather than plain JavaScript objects.\n\t *\n\t * For example:\n\t *\n\t * \\`\\`\\`javascript\n\t * // This creates a new ${exampleObjectName} object:\n\t * const ${communize(exampleObjectName)} = context.create.${exampleObjectName}({ ...properties });\n\t * // Don't do this:\n\t * // const ${communize(exampleObjectName)} = { ...properties };\n\t * \\`\\`\\`\n\t */\n\tcreate: Record<string, <T extends TreeData>(input: T) => T>;\\n`;\n\n\tconst isDocs =\n\t\texampleTypeName === undefined\n\t\t\t? \"\"\n\t\t\t: `\\n\t/**\n\t * A collection of type-guard functions for data in the tree.\n\t * @remarks\n\t * Each property on this object is named after a type in the tree schema.\n\t * Call the corresponding function to check if a node is of that specific type.\n\t * This is useful when working with nodes that could be one of multiple types.\n\t *\n\t * ${`Example: Check if a node is a ${exampleTypeName} with \\`if (context.is.${exampleTypeName}(node)) {}\\``}\n\t */\n\tis: Record<string, <T extends TreeData>(data: unknown) => data is T>;\n\n\t/**\n\t * Checks if the provided data is an array.\n\t * @remarks\n\t * DO NOT use \\`Array.isArray\\` to check if tree data is an array - use this function instead.\n\t *\n\t * This function will also work for native JavaScript arrays.\n\t *\n\t * ${`Example: \\`if (context.isArray(node)) {}\\``}\n\t */\n\tisArray(data: any): boolean;\n\n\t/**\n\t * Checks if the provided data is a map.\n\t * @remarks\n\t * DO NOT use \\`instanceof Map\\` to check if tree data is a map - use this function instead.\n\t *\n\t * This function will also work for native JavaScript Map instances.\n\t *\n\t * ${`Example: \\`if (context.isMap(node)) {}\\``}\n\t */\n\tisMap(data: any): boolean;\\n`;\n\n\tconst context = `\\`\\`\\`typescript\n\t${nodeTypeUnion === undefined ? \"\" : `type TreeData = ${nodeTypeUnion};\\n\\n`}\t/**\n\t * An object available to generated code which provides read and write access to the tree as well as utilities for creating and inspecting data in the tree.\n\t * @remarks This object is available as a variable named \\`context\\` in the scope of the generated JavaScript snippet.\n\t */\n\tinterface Context<TSchema extends ImplicitFieldSchema> {\n\t/**\n\t * The root of the tree that can be read or mutated.\n\t * @remarks\n\t * You can read properties and navigate through the tree starting from this root.\n\t * You can also assign a new value to this property to replace the entire tree, as long as the new value is one of the types allowed at the root.\n\t *\n\t * Example: Read the current root with \\`const currentRoot = context.root;\\`\n\t *${rootTypes.length > 0 ? ` Example: Replace the entire root with \\`context.root = context.create.${getFriendlyName(rootTypes[0] ?? oob())}({ });\\`\\n\t *` : \"\"}/\n\troot: ReadableField<TSchema>;\n\t${createDocs}\n\t${isDocs}\n\t/**\n\t * Returns the parent object/array/map of the given object/array/map, if there is one.\n\t * @returns The parent node, or \\`undefined\\` if the node is the root or is not in the tree.\n\t * @remarks\n\t * Example: Get the parent with \\`const parent = context.parent(child);\\`\n\t */\n\tparent(child: TreeData): TreeData | undefined;\n\n\t/**\n\t * Returns the property key or index of the given object/array/map within its parent.\n\t * @returns A string key if the child is in an object or map, or a numeric index if the child is in an array.\n\t *\n\t * Example: \\`const key = context.key(child);\\`\n\t */\n\tkey(child: TreeData): string | number;\n}\n\\`\\`\\``;\n\n\tconst helperMethodExplanation = hasHelperMethods\n\t\t? `Manipulating the data using the APIs described below is allowed, but when possible ALWAYS prefer to use any application helper methods exposed on the schema TypeScript types if the goal can be accomplished that way.\nIt will often not be possible to fully accomplish the goal using those helpers. When this is the case, mutate the objects as normal, taking into account the following guidance.`\n\t\t: \"\";\n\n\tconst reinsertionExplanation = `Once non-primitive data has been removed from the tree (e.g. replaced via assignment, or removed from an array), that data cannot be re-inserted into the tree.\nInstead, it must be deep cloned and recreated.\n${\n\texampleObjectName === undefined\n\t\t? \"\"\n\t\t: `For example:\n\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst ${communize(exampleObjectName)} = parent.${communize(exampleObjectName)};\nparent.${communize(exampleObjectName)} = undefined;\n// \\`${communize(exampleObjectName)}\\` cannot be directly re-inserted into the tree - this will throw an error:\n// parent.${communize(exampleObjectName)} = ${communize(exampleObjectName)}; // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\nparent.${communize(exampleObjectName)} = context.create.${exampleObjectName}({ /*... deep clone all properties from \\`${communize(exampleObjectName)}\\` */ });\n\\`\\`\\`${\n\t\t\t\thasArrays\n\t\t\t\t\t? `\\n\\nThe same applies when using arrays:\\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst item = arrayOf${exampleObjectName}[0];\narrayOf${exampleObjectName}.removeAt(0);\n// \\`item\\` cannot be directly re-inserted into the tree - this will throw an error:\narrayOf${exampleObjectName}.insertAt(0, item); // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\narrayOf${exampleObjectName}.insertAt(0, context.create.${exampleObjectName}({ /*... deep clone all properties from \\`item\\` */ }));\n\\`\\`\\``\n\t\t\t\t\t: \"\"\n\t\t\t}${\n\t\t\t\thasMaps\n\t\t\t\t\t? `\\n\\nThe same applies when using maps:\n\\`\\`\\`javascript\n// Data is removed from the tree:\nconst value = mapOf${exampleObjectName}.get(\"someKey\");\nmapOf${exampleObjectName}.delete(\"someKey\");\n// \\`value\\` cannot be directly re-inserted into the tree - this will throw an error:\nmapOf${exampleObjectName}.set(\"someKey\", value); // ❌ A node may not be inserted into the tree more than once\n// Instead, it must be deep cloned and recreated before insertion:\nmapOf${exampleObjectName}.set(\"someKey\", context.create.${exampleObjectName}({ /*... deep clone all properties from \\`value\\` */ }));\n\\`\\`\\``\n\t\t\t\t\t: \"\"\n\t\t\t}`\n}`;\n\n\tconst arrayEditing = `#### Editing Arrays\n\nThe arrays in the tree are somewhat different than normal JavaScript \\`Array\\`s.\nRead-only operations are generally the same - you can create them, read via index, and call non-mutating methods like \\`concat\\`, \\`map\\`, \\`filter\\`, \\`find\\`, \\`forEach\\`, \\`indexOf\\`, \\`slice\\`, \\`join\\`, etc.\nHowever, write operations (e.g. index assignment, \\`push\\`, \\`pop\\`, \\`splice\\`, etc.) are not supported.\nInstead, you must use the methods on the following interface to mutate the array:\n\n\\`\\`\\`typescript\n${getTreeArrayNodeDocumentation(arrayInterfaceName)}\n\\`\\`\\`\n\nWhen possible, ensure that the edits preserve the identity of objects already in the tree.\nFor example, prefer \\`array.moveToIndex\\` over \\`array.removeAt\\` + \\`array.insertAt\\` and prefer \\`array.moveRangeToIndex\\` over \\`array.removeRange\\` + \\`array.insertAt\\`.\n\n`;\n\n\tconst mapEditing = `#### Editing Maps\n\nThe maps in the tree are somewhat different than normal JavaScript \\`Map\\`s.\nMap keys are always strings.\nRead-only operations are generally the same - you can create them, read via \\`get\\`, and call non-mutating methods like \\`has\\`, \\`forEach\\`, \\`entries\\`, \\`keys\\`, \\`values\\`, etc. (note the subtle differences around return values and iteration order).\nHowever, write operations (e.g. \\`set\\`, \\`delete\\`, etc.) are not supported.\nInstead, you must use the methods on the following interface to mutate the map:\n\n\\`\\`\\`typescript\n${getTreeMapNodeDocumentation(mapInterfaceName)}\n\\`\\`\\`\n\n`;\n\n\tconst editing = `If the user asks you to edit the tree, you should author a snippet of JavaScript code to accomplish the user-specified goal, following the instructions for editing detailed below.\nYou must use the \"${editToolName}\" tool to run the generated code.\nAfter editing the tree, review the latest state of the tree to see if it satisfies the user's request.\nIf it does not, or if you receive an error, you may try again with a different approach.\nOnce the tree is in the desired state, you should inform the user that the request has been completed.\n\n### Editing\n\nIf the user asks you to edit the document, you will write a snippet of JavaScript code that mutates the data in-place to achieve the user's goal.\nThe snippet may be synchronous or asynchronous (i.e. it may \\`await\\` functions if necessary).\nThe snippet has a \\`context\\` variable in its scope.\nThis \\`context\\` variable holds the current state of the tree in the \\`root\\` property.\nYou may mutate any part of this tree as necessary, taking into account the caveats around${hasArrays ? ` arrays${hasMaps ? \" and\" : \"\"}` : \"\"}${hasMaps ? \" maps\" : \"\"} detailed below.\nYou may also set the \\`root\\` property of the context to be an entirely new value as long as it is one of the types allowed at the root of the tree (\\`${rootTypeUnion}\\`).\nYou should also use the \\`context\\` object to create new data to insert into the tree, using the builder functions available on the \\`create\\` property.\nThere are other additional helper functions available on the \\`context\\` object to help you analyze the tree.\nHere is the definition of the \\`Context\\` interface:\n${context}\n${helperMethodExplanation}\n${hasArrays ? arrayEditing : \"\"}${hasMaps ? mapEditing : \"\"}#### Additional Notes\n\nBefore outputting the edit function, you should check that it is valid according to both the application tree's schema and any restrictions of the editing APIs described above.\n\n${reinsertionExplanation}\n\nFinally, double check that the edits would accomplish the user's request (if it is possible).\n\n`;\n\n\tconst prompt = `You are a helpful assistant collaborating with the user on a document. The document state is a JSON tree, and you are able to analyze and edit it.\nThe JSON tree adheres to the following Typescript schema:\n\n\\`\\`\\`typescript\n${typescriptSchemaTypes}\n\\`\\`\\`\n\nIf the user asks you a question about the tree, you should inspect the state of the tree and answer the question.\nWhen answering such a question, DO NOT answer with information that is not part of the document unless requested to do so.\n\n${editToolName === undefined ? \"\" : editing}### Application data\n\n${\n\tdomainHints === undefined\n\t\t? \"\"\n\t\t: `\\nThe application supplied the following additional instructions: ${domainHints}`\n}\nThe current state of \\`context.root\\` (a \\`${field === undefined ? \"undefined\" : getFriendlyName(Tree.schema(field))}\\`) is:\n\n\\`\\`\\`JSON\n${stringified}\n\\`\\`\\``;\n\treturn prompt;\n}\n\n/**\n * Serializes tree data e.g. to include in a prompt or message.\n * @remarks This includes some extra metadata to make it easier to understand the structure of the tree.\n */\nexport function stringifyTree(tree: ReadableField<ImplicitFieldSchema>): string {\n\tconst typeReplacementKey = \"_e944da5a5fd04ea2b8b2eb6109e089ed\";\n\tconst indexReplacementKey = \"_27bb216b474d45e6aaee14d1ec267b96\";\n\tconst mapReplacementKey = \"_a0d98d22a1c644539f07828d3f064d71\";\n\tconst stringified = JSON.stringify(\n\t\ttree,\n\t\t(_, node: unknown) => {\n\t\t\tif (node instanceof TreeNode) {\n\t\t\t\tconst key = Tree.key(node);\n\t\t\t\tconst index = typeof key === \"number\" ? key : undefined;\n\t\t\t\tconst schema = Tree.schema(node);\n\t\t\t\tswitch (schema.kind) {\n\t\t\t\t\tcase NodeKind.Object: {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t[typeReplacementKey]: getFriendlyName(schema),\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t...node,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tcase NodeKind.Map: {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t[mapReplacementKey]: \"\",\n\t\t\t\t\t\t\t...Object.fromEntries(node as TreeMapNode),\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\treturn {\n\t\t\t\t\t\t\t[indexReplacementKey]: index,\n\t\t\t\t\t\t\t...node,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn node;\n\t\t},\n\t\t2,\n\t);\n\n\treturn stringified\n\t\t.replace(new RegExp(`\"${typeReplacementKey}\":`, \"g\"), `// Type:`)\n\t\t.replace(new RegExp(`\"${indexReplacementKey}\":`, \"g\"), `// Index:`)\n\t\t.replace(\n\t\t\tnew RegExp(`\"${mapReplacementKey}\": \"\"`, \"g\"),\n\t\t\t`// Note: This is a map that has been serialized to JSON. It is not a key-value object/record but is being printed as such.`,\n\t\t);\n}\n\n/**\n * Retrieves the documentation for the `TreeArrayNode` interface to feed to the LLM.\n * @remarks The documentation has been simplified in various ways to make it easier for the LLM to understand.\n * @privateRemarks TODO: How do we keep this in sync with the actual `TreeArrayNode` docs if/when those docs change?\n */\nfunction getTreeArrayNodeDocumentation(typeName: string): string {\n\treturn `/** A special type of array which implements 'readonly T[]' (i.e. it supports all read-only JS array methods) and provides custom array mutation APIs. */\nexport interface ${typeName}<T> extends ReadonlyArray<T> {\n\t/**\n\t * Inserts new item(s) at a specified location.\n\t * @param index - The index at which to insert \\`value\\`.\n\t * @param value - The content to insert.\n\t * @throws Throws if \\`index\\` is not in the range [0, \\`array.length\\`).\n\t */\n\tinsertAt(index: number, ...value: readonly T[]): void;\n\n\t/**\n\t * Removes the item at the specified location.\n\t * @param index - The index at which to remove the item.\n\t * @throws Throws if \\`index\\` is not in the range [0, \\`array.length\\`).\n\t */\n\tremoveAt(index: number): void;\n\n\t/**\n\t * Removes all items between the specified indices.\n\t * @param start - The starting index of the range to remove (inclusive). Defaults to the start of the array.\n\t * @param end - The ending index of the range to remove (exclusive). Defaults to \\`array.length\\`.\n\t * @throws Throws if \\`start\\` is not in the range [0, \\`array.length\\`].\n\t * @throws Throws if \\`end\\` is less than \\`start\\`.\n\t * If \\`end\\` is not supplied or is greater than the length of the array, all items after \\`start\\` are removed.\n\t *\n\t * @remarks\n\t * The default values for start and end are computed when this is called,\n\t * and thus the behavior is the same as providing them explicitly, even with respect to merge resolution with concurrent edits.\n\t * For example, two concurrent transactions both emptying the array with \\`node.removeRange()\\` then inserting an item,\n\t * will merge to result in the array having both inserted items.\n\t */\n\tremoveRange(start?: number, end?: number): void;\n\n\t/**\n\t * Moves the specified item to the desired location in the array.\n\t *\n\t * WARNING - This API is easily misused.\n\t * Please read the documentation for the \\`destinationGap\\` parameter carefully.\n\t *\n\t * @param destinationGap - The location *between* existing items that the moved item should be moved to.\n\t *\n\t * WARNING - \\`destinationGap\\` describes a location between existing items *prior to applying the move operation*.\n\t *\n\t * For example, if the array contains items \\`[A, B, C]\\` before the move, the \\`destinationGap\\` must be one of the following:\n\t *\n\t * - \\`0\\` (between the start of the array and \\`A\\`'s original position)\n\t * - \\`1\\` (between \\`A\\`'s original position and \\`B\\`'s original position)\n\t * - \\`2\\` (between \\`B\\`'s original position and \\`C\\`'s original position)\n\t * - \\`3\\` (between \\`C\\`'s original position and the end of the array)\n\t *\n\t * So moving \\`A\\` between \\`B\\` and \\`C\\` would require \\`destinationGap\\` to be \\`2\\`.\n\t *\n\t * This interpretation of \\`destinationGap\\` makes it easy to specify the desired destination relative to a sibling item that is not being moved,\n\t * or relative to the start or end of the array:\n\t *\n\t * - Move to the start of the array: \\`array.moveToIndex(0, ...)\\` (see also \\`moveToStart\\`)\n\t * - Move to before some item X: \\`array.moveToIndex(indexOfX, ...)\\`\n\t * - Move to after some item X: \\`array.moveToIndex(indexOfX + 1\\`, ...)\n\t * - Move to the end of the array: \\`array.moveToIndex(array.length, ...)\\` (see also \\`moveToEnd\\`)\n\t *\n\t * This interpretation of \\`destinationGap\\` does however make it less obvious how to move an item relative to its current position:\n\t *\n\t * - Move item B before its predecessor: \\`array.moveToIndex(indexOfB - 1, ...)\\`\n\t * - Move item B after its successor: \\`array.moveToIndex(indexOfB + 2, ...)\\`\n\t *\n\t * Notice the asymmetry between \\`-1\\` and \\`+2\\` in the above examples.\n\t * In such scenarios, it can often be easier to approach such edits by swapping adjacent items:\n\t * If items A and B are adjacent, such that A precedes B,\n\t * then they can be swapped with \\`array.moveToIndex(indexOfA, indexOfB)\\`.\n\t *\n\t * @param sourceIndex - The index of the item to move.\n\t * @param source - The optional source array to move the item out of (defaults to this array).\n\t * @throws Throws if any of the source index is not in the range [0, \\`array.length\\`),\n\t * or if the index is not in the range [0, \\`array.length\\`].\n\t */\n\tmoveToIndex(destinationGap: number, sourceIndex: number, source?: ${typeName}<T>): void;\n\n\t/**\n\t * Moves the specified items to the desired location within the array.\n\t *\n\t * WARNING - This API is easily misused.\n\t * Please read the documentation for the \\`destinationGap\\` parameter carefully.\n\t *\n\t * @param destinationGap - The location *between* existing items that the moved item should be moved to.\n\t *\n\t * WARNING - \\`destinationGap\\` describes a location between existing items *prior to applying the move operation*.\n\t *\n\t * For example, if the array contains items \\`[A, B, C]\\` before the move, the \\`destinationGap\\` must be one of the following:\n\t *\n\t * - \\`0\\` (between the start of the array and \\`A\\`'s original position)\n\t * - \\`1\\` (between \\`A\\`'s original position and \\`B\\`'s original position)\n\t * - \\`2\\` (between \\`B\\`'s original position and \\`C\\`'s original position)\n\t * - \\`3\\` (between \\`C\\`'s original position and the end of the array)\n\t *\n\t * So moving \\`A\\` between \\`B\\` and \\`C\\` would require \\`destinationGap\\` to be \\`2\\`.\n\t *\n\t * This interpretation of \\`destinationGap\\` makes it easy to specify the desired destination relative to a sibling item that is not being moved,\n\t * or relative to the start or end of the array:\n\t *\n\t * - Move to the start of the array: \\`array.moveToIndex(0, ...)\\` (see also \\`moveToStart\\`)\n\t * - Move to before some item X: \\`array.moveToIndex(indexOfX, ...)\\`\n\t * - Move to after some item X: \\`array.moveToIndex(indexOfX + 1\\`, ...)\n\t * - Move to the end of the array: \\`array.moveToIndex(array.length, ...)\\` (see also \\`moveToEnd\\`)\n\t *\n\t * This interpretation of \\`destinationGap\\` does however make it less obvious how to move an item relative to its current position:\n\t *\n\t * - Move item B before its predecessor: \\`array.moveToIndex(indexOfB - 1, ...)\\`\n\t * - Move item B after its successor: \\`array.moveToIndex(indexOfB + 2, ...)\\`\n\t *\n\t * Notice the asymmetry between \\`-1\\` and \\`+2\\` in the above examples.\n\t * In such scenarios, it can often be easier to approach such edits by swapping adjacent items:\n\t * If items A and B are adjacent, such that A precedes B,\n\t * then they can be swapped with \\`array.moveToIndex(indexOfA, indexOfB)\\`.\n\t *\n\t * @param sourceStart - The starting index of the range to move (inclusive).\n\t * @param sourceEnd - The ending index of the range to move (exclusive)\n\t * @param source - The optional source array to move items out of (defaults to this array).\n\t * @throws Throws if the types of any of the items being moved are not allowed in the destination array,\n\t * if any of the input indices are not in the range [0, \\`array.length\\`], or if \\`sourceStart\\` is greater than \\`sourceEnd\\`.\n\t */\n\tmoveRangeToIndex(\n\t\tdestinationGap: number,\n\t\tsourceStart: number,\n\t\tsourceEnd: number,\n\t\tsource?: ${typeName}<T>,\n\t): void;\n}`;\n}\n\n/**\n * Retrieves the documentation for the `TreeMapNode` interface to feed to the LLM.\n * @remarks The documentation has been simplified in various ways to make it easier for the LLM to understand.\n * @privateRemarks TODO: How do we keep this in sync with the actual `TreeMapNode` docs if/when those docs change?\n */\nfunction getTreeMapNodeDocumentation(typeName: string): string {\n\treturn `/**\n * A map of string keys to tree objects.\n */\nexport interface ${typeName}<T> extends ReadonlyMap<string, T> {\n\t/**\n\t * Adds or updates an entry in the map with a specified \\`key\\` and a \\`value\\`.\n\t *\n\t * @param key - The key of the element to add to the map.\n\t * @param value - The value of the element to add to the map.\n\t *\n\t * @remarks\n\t * Setting the value at a key to \\`undefined\\` is equivalent to calling {@link ${typeName}.delete} with that key.\n\t */\n\tset(key: string, value: T | undefined): void;\n\n\t/**\n\t * Removes the specified element from this map by its \\`key\\`.\n\t *\n\t * @remarks\n\t * Note: unlike JavaScript's Map API, this method does not return a flag indicating whether or not the value was\n\t * deleted.\n\t *\n\t * @param key - The key of the element to remove from the map.\n\t */\n\tdelete(key: string): void;\n\n\t/**\n\t * Returns an iterable of keys in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the keys returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tkeys(): IterableIterator<string>;\n\n\t/**\n\t * Returns an iterable of values in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the values returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tvalues(): IterableIterator<T>;\n\n\t/**\n\t * Returns an iterable of key/value pairs for every entry in the map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order of the entries returned.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tentries(): IterableIterator<[string, T]>;\n\n\t/**\n\t * Executes the provided function once per each key/value pair in this map.\n\t *\n\t * @remarks\n\t * Note: no guarantees are made regarding the order in which the function is called with respect to the map's entries.\n\t * If your usage scenario depends on consistent ordering, you will need to sort these yourself.\n\t */\n\tforEach(\n\t\tcallbackfn: (\n\t\t\tvalue: T,\n\t\t\tkey: string,\n\t\t\tmap: ReadonlyMap<string, T>,\n\t\t) => void,\n\t\tthisArg?: any,\n\t): void;\n}`;\n}\n"]}
|
package/dist/propertyBinding.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.getExposedProperties = exports.PropertyDef = exports.exposePropertiesSymbol = void 0;
|
|
8
|
-
const
|
|
8
|
+
const renderZodTypeScript_js_1 = require("./renderZodTypeScript.js");
|
|
9
9
|
/**
|
|
10
10
|
* A symbol used to expose properties to the LLM.
|
|
11
11
|
* @alpha
|
|
@@ -38,7 +38,7 @@ class ExposedPropertiesI {
|
|
|
38
38
|
}
|
|
39
39
|
instanceOf(schema) {
|
|
40
40
|
this.referencedTypes.add(schema);
|
|
41
|
-
return (0,
|
|
41
|
+
return (0, renderZodTypeScript_js_1.instanceOf)(schema);
|
|
42
42
|
}
|
|
43
43
|
static getExposedProperties(schemaClass) {
|
|
44
44
|
const exposed = new ExposedPropertiesI(schemaClass);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propertyBinding.js","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAOH,
|
|
1
|
+
{"version":3,"file":"propertyBinding.js","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAOH,qEAAsD;AAEtD;;;GAGG;AACU,QAAA,sBAAsB,GAAkB,MAAM,CAAC,GAAG,CAC9D,6CAA6C,CAC7C,CAAC;AAmDF;;;GAGG;AACH,MAAa,WAAW;IACvB,YACiB,IAAY,EACZ,WAA+B,EAC/B,MAAkB,EAClB,QAAiB;QAHjB,SAAI,GAAJ,IAAI,CAAQ;QACZ,gBAAW,GAAX,WAAW,CAAoB;QAC/B,WAAM,GAAN,MAAM,CAAY;QAClB,aAAQ,GAAR,QAAQ,CAAS;IAC/B,CAAC;CACJ;AAPD,kCAOC;AAyCD,MAAM,kBAAkB;IAIvB,YAAoC,WAA2B;QAA3B,gBAAW,GAAX,WAAW,CAAgB;QAH9C,eAAU,GAAgC,EAAE,CAAC;QAC7C,oBAAe,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEK,CAAC;IAE5D,cAAc,CAKpB,MAAS,EACT,IAAO,EACP,GACmD;QAEnD,IAAI,MAAM,KAAK,IAAI,CAAC,WAAW,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;QACtE,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,WAAW,CACtC,IAAI,EACJ,GAAG,CAAC,WAAW,EACf,GAAG,CAAC,MAAM,EACV,GAAG,CAAC,QAAQ,KAAK,IAAI,CACrB,CAAC;IACH,CAAC;IAEM,UAAU,CAChB,MAAS;QAET,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjC,OAAO,IAAA,mCAAU,EAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,oBAAoB,CAAC,WAA2B;QAI7D,MAAM,OAAO,GAAG,IAAI,kBAAkB,CAAC,WAAW,CAAC,CAAC;QACpD,MAAM,WAAW,GAAG,WAA4C,CAAC;QACjE,IAAI,WAAW,CAAC,8BAAsB,CAAC,KAAK,SAAS,EAAE,CAAC;YACvD,WAAW,CAAC,8BAAsB,CAAC,CAAC,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO;YACN,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,eAAe,EAAE,OAAO,CAAC,eAAe;SACxC,CAAC;IACH,CAAC;CACD;AAED;;;;GAIG;AACH,SAAgB,oBAAoB,CAAC,WAA2B;IAI/D,OAAO,kBAAkB,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;AAC7D,CAAC;AALD,oDAKC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from \"zod\";\n\nimport type { BindableSchema, Ctor } from \"./methodBinding.js\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\n\n/**\n * A symbol used to expose properties to the LLM.\n * @alpha\n */\nexport const exposePropertiesSymbol: unique symbol = Symbol.for(\n\t\"@fluidframework/tree-agent/exposeProperties\",\n);\n\n/**\n * Set of property keys from `T` that are not methods.\n * @alpha\n */\nexport type ExposableKeys<T> = {\n\t[K in keyof T]?: T[K] extends (...args: any[]) => any ? never : K;\n}[keyof T];\n\n/**\n * Type-level equality test used as a helper to evaluate readonly keys.\n * - If X and Y are the same type, it evaluates to A.\n * - If X and Y are different, it evaluates to B.\n * @alpha\n */\nexport type IfEquals<X, Y, A = true, B = false> = (<T>() => T extends X ? 1 : 2) extends <\n\tT,\n>() => T extends Y ? 1 : 2\n\t? A\n\t: B;\n\n/**\n * Produces a union of keys of `T` which are readonly.\n * @alpha\n */\nexport type ReadonlyKeys<T> = {\n\t// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\n\t[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>;\n}[keyof T];\n\n/**\n * Type to enforce `readOnly: true` for readonly properties.\n * @alpha\n */\nexport type ReadOnlyRequirement<TObj, K extends keyof TObj> = {\n\t[P in K]-?: P extends ReadonlyKeys<TObj> ? { readOnly: true } : { readOnly?: false };\n}[K];\n\n/**\n * Emits compile-time error when there is a type mismatch.\n * @alpha\n */\nexport type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]\n\t? unknown\n\t: {\n\t\t\t__error__: \"Zod schema value type does not match the property's declared type\";\n\t\t\texpected: Expected;\n\t\t\treceived: Received;\n\t\t};\n\n/**\n * A property definition class that describes the structure of the property\n * @alpha\n */\nexport class PropertyDef {\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly description: string | undefined,\n\t\tpublic readonly schema: ZodTypeAny,\n\t\tpublic readonly readOnly: boolean,\n\t) {}\n}\n\n/**\n * An interface for exposing properties of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedProperties {\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void;\n\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * An interface that SharedTree schema classes should implement to expose their properties to the LLM.\n *\n * @remarks\n * The `getExposedProperties` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedProperties {\n\t[exposePropertiesSymbol]?(properties: ExposedProperties): void;\n}\n\nclass ExposedPropertiesI implements ExposedProperties {\n\tprivate readonly properties: Record<string, PropertyDef> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic exposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose properties on the \"this\" schema class');\n\t\t}\n\t\tthis.properties[name] = new PropertyDef(\n\t\t\tname,\n\t\t\tdef.description,\n\t\t\tdef.schema,\n\t\t\tdef.readOnly === true,\n\t\t);\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedProperties(schemaClass: BindableSchema): {\n\t\tproperties: Record<string, PropertyDef>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposed = new ExposedPropertiesI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedProperties;\n\t\tif (extractable[exposePropertiesSymbol] !== undefined) {\n\t\t\textractable[exposePropertiesSymbol](exposed);\n\t\t}\n\t\treturn {\n\t\t\tproperties: exposed.properties,\n\t\t\treferencedTypes: exposed.referencedTypes,\n\t\t};\n\t}\n}\n\n/**\n * Get the exposed properties of a schema class.\n * @param schemaClass - The schema class to extract properties from.\n * @returns A record of property names and their corresponding Zod types.\n */\nexport function getExposedProperties(schemaClass: BindableSchema): {\n\tproperties: Record<string, PropertyDef>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedPropertiesI.getExposedProperties(schemaClass);\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { SimpleNodeSchema } from "@fluidframework/tree/internal";
|
|
6
|
+
import type { BindableSchema } from "./methodBinding.js";
|
|
7
|
+
/**
|
|
8
|
+
* Output of rendering schema metadata into TypeScript declaration text.
|
|
9
|
+
*/
|
|
10
|
+
export interface SchemaTypeScriptRenderResult {
|
|
11
|
+
schemaText: string;
|
|
12
|
+
hasHelperMethods: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Converts schema metadata into TypeScript declarations suitable for prompt inclusion.
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderSchemaTypeScript(definitions: ReadonlyMap<string, SimpleNodeSchema>, bindableSchemas: Map<string, BindableSchema>): SchemaTypeScriptRenderResult;
|
|
18
|
+
//# sourceMappingURL=renderSchemaTypeScript.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSchemaTypeScript.d.ts","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAIX,gBAAgB,EAGhB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,oBAAoB,CAAC;AAiC1E;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAClD,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GAC1C,4BAA4B,CAkV9B"}
|