@fluidframework/ai-collab 2.11.0 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/explicit-strategy/agentEditReducer.d.ts.map +1 -1
- package/dist/explicit-strategy/agentEditReducer.js +6 -5
- package/dist/explicit-strategy/agentEditReducer.js.map +1 -1
- package/dist/explicit-strategy/promptGeneration.d.ts.map +1 -1
- package/dist/explicit-strategy/promptGeneration.js +8 -7
- package/dist/explicit-strategy/promptGeneration.js.map +1 -1
- package/dist/explicit-strategy/typeGeneration.d.ts +5 -1
- package/dist/explicit-strategy/typeGeneration.d.ts.map +1 -1
- package/dist/explicit-strategy/typeGeneration.js +54 -32
- package/dist/explicit-strategy/typeGeneration.js.map +1 -1
- package/lib/explicit-strategy/agentEditReducer.d.ts.map +1 -1
- package/lib/explicit-strategy/agentEditReducer.js +7 -6
- package/lib/explicit-strategy/agentEditReducer.js.map +1 -1
- package/lib/explicit-strategy/promptGeneration.d.ts.map +1 -1
- package/lib/explicit-strategy/promptGeneration.js +9 -8
- package/lib/explicit-strategy/promptGeneration.js.map +1 -1
- package/lib/explicit-strategy/typeGeneration.d.ts +5 -1
- package/lib/explicit-strategy/typeGeneration.d.ts.map +1 -1
- package/lib/explicit-strategy/typeGeneration.js +53 -32
- package/lib/explicit-strategy/typeGeneration.js.map +1 -1
- package/package.json +12 -13
- package/src/explicit-strategy/agentEditReducer.ts +8 -10
- package/src/explicit-strategy/promptGeneration.ts +11 -8
- package/src/explicit-strategy/typeGeneration.ts +84 -37
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import { assert } from "@fluidframework/core-utils/internal";
|
|
6
|
-
import { FieldKind, NodeKind, ValueSchema } from "@fluidframework/tree/internal";
|
|
6
|
+
import { FieldKind, getSimpleSchema, NodeKind, Tree, ValueSchema, } from "@fluidframework/tree/internal";
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
import { objectIdKey, typeField } from "./agentEditTypes.js";
|
|
9
9
|
import { fail, getOrCreate, mapIterable } from "./utils.js";
|
|
@@ -11,11 +11,13 @@ import { fail, getOrCreate, mapIterable } from "./utils.js";
|
|
|
11
11
|
* Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.
|
|
12
12
|
* @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.
|
|
13
13
|
*/
|
|
14
|
-
const objectTarget = z
|
|
14
|
+
const objectTarget = z
|
|
15
|
+
.object({
|
|
15
16
|
target: z
|
|
16
17
|
.string()
|
|
17
18
|
.describe(`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`),
|
|
18
|
-
})
|
|
19
|
+
})
|
|
20
|
+
.describe("A pointer to a specific object node in the tree, identified by the target object's Id.");
|
|
19
21
|
/**
|
|
20
22
|
* Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.
|
|
21
23
|
* @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.
|
|
@@ -94,16 +96,18 @@ export function generateGenericEditTypes(schema, generateDomainTypes) {
|
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
}
|
|
97
|
-
const
|
|
99
|
+
const doesSchemaHaveArray = insertSet.size > 0;
|
|
100
|
+
const modify = z
|
|
98
101
|
.object({
|
|
99
|
-
type: z.
|
|
102
|
+
type: z.enum(["modify"]),
|
|
100
103
|
explanation: z.string().describe(editDescription),
|
|
101
|
-
|
|
102
|
-
|
|
104
|
+
target: objectTarget,
|
|
105
|
+
field: z.enum([...modifyFieldSet]), // Modify with appropriate fields
|
|
106
|
+
modification: generateDomainTypes
|
|
107
|
+
? getType(modifyTypeSet)
|
|
103
108
|
: z.any().describe("Domain-specific content here"),
|
|
104
|
-
destination: z.union([arrayPlace, objectPlace]),
|
|
105
109
|
})
|
|
106
|
-
.describe("
|
|
110
|
+
.describe("Sets a field on a specific ObjectTarget.");
|
|
107
111
|
const remove = z
|
|
108
112
|
.object({
|
|
109
113
|
type: z.literal("remove"),
|
|
@@ -111,6 +115,16 @@ export function generateGenericEditTypes(schema, generateDomainTypes) {
|
|
|
111
115
|
source: z.union([objectTarget, range]),
|
|
112
116
|
})
|
|
113
117
|
.describe("Deletes an object or Range of objects from the tree.");
|
|
118
|
+
const insert = z
|
|
119
|
+
.object({
|
|
120
|
+
type: z.literal("insert"),
|
|
121
|
+
explanation: z.string().describe(editDescription),
|
|
122
|
+
content: generateDomainTypes
|
|
123
|
+
? getType(insertSet)
|
|
124
|
+
: z.any().describe("Domain-specific content here"),
|
|
125
|
+
destination: z.union([arrayPlace, objectPlace]),
|
|
126
|
+
})
|
|
127
|
+
.describe("Inserts a new object at a specific Place or ArrayPlace.");
|
|
114
128
|
const move = z
|
|
115
129
|
.object({
|
|
116
130
|
type: z.literal("move"),
|
|
@@ -119,34 +133,28 @@ export function generateGenericEditTypes(schema, generateDomainTypes) {
|
|
|
119
133
|
destination: z.union([arrayPlace, objectPlace]),
|
|
120
134
|
})
|
|
121
135
|
.describe("Moves an object or Range of objects to a new Place or ArrayPlace.");
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
.
|
|
133
|
-
|
|
136
|
+
const typeRecord = {
|
|
137
|
+
ObjectTarget: objectTarget,
|
|
138
|
+
Modify: modify,
|
|
139
|
+
};
|
|
140
|
+
typeRecord.Modify = modify;
|
|
141
|
+
if (doesSchemaHaveArray) {
|
|
142
|
+
typeRecord.ObjectPlace = objectPlace;
|
|
143
|
+
typeRecord.ArrayPlace = arrayPlace;
|
|
144
|
+
typeRecord.Range = range;
|
|
145
|
+
typeRecord.Insert = insert;
|
|
146
|
+
typeRecord.Remove = remove;
|
|
147
|
+
typeRecord.Move = move;
|
|
148
|
+
}
|
|
149
|
+
const editTypes = doesSchemaHaveArray
|
|
150
|
+
? [insert, remove, move, modify, z.null()]
|
|
151
|
+
: [modify, z.null()];
|
|
134
152
|
const editWrapper = z.object({
|
|
135
153
|
edit: z
|
|
136
154
|
.union(editTypes)
|
|
137
155
|
.describe("The next edit to apply to the tree, or null if the task is complete."),
|
|
138
156
|
});
|
|
139
|
-
|
|
140
|
-
ObjectTarget: objectTarget,
|
|
141
|
-
ObjectPlace: objectPlace,
|
|
142
|
-
ArrayPlace: arrayPlace,
|
|
143
|
-
Range: range,
|
|
144
|
-
Insert: insert,
|
|
145
|
-
Remove: remove,
|
|
146
|
-
Move: move,
|
|
147
|
-
Modify: modify,
|
|
148
|
-
EditWrapper: editWrapper,
|
|
149
|
-
};
|
|
157
|
+
typeRecord.EditWrapper = editWrapper;
|
|
150
158
|
return [typeRecord, "EditWrapper"];
|
|
151
159
|
});
|
|
152
160
|
}
|
|
@@ -257,4 +265,17 @@ function tryGetSingleton(set) {
|
|
|
257
265
|
function hasAtLeastTwo(array) {
|
|
258
266
|
return array.length >= 2;
|
|
259
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Determines if the provided {@link TreeNode} contains an array schema.
|
|
270
|
+
*/
|
|
271
|
+
export function doesNodeContainArraySchema(node) {
|
|
272
|
+
const schema = Tree.schema(node);
|
|
273
|
+
const simpleSchema = getSimpleSchema(schema);
|
|
274
|
+
for (const [, nodeSchema] of simpleSchema.definitions) {
|
|
275
|
+
if (nodeSchema.kind === NodeKind.Array) {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
260
281
|
//# sourceMappingURL=typeGeneration.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,SAAS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ation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport { FieldKind, NodeKind, ValueSchema } from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z.object({\n\ttarget: z\n\t\t.string()\n\t\t.describe(\n\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t),\n});\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\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 cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\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 */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, Zod.ZodTypeAny>, root: string] {\n\treturn getOrCreate(cache, schema, () => {\n\t\tconst insertSet = new Set<string>();\n\t\tconst modifyFieldSet = new Set<string>();\n\t\tconst modifyTypeSet = new Set<string>();\n\t\tconst typeMap = new Map<string, Zod.ZodTypeAny>();\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): Zod.ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\t\tconst editTypes = [insert, remove, move, modify, z.null()] as const;\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\tconst typeRecord: Record<string, Zod.ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tObjectPlace: objectPlace,\n\t\t\tArrayPlace: arrayPlace,\n\t\t\tRange: range,\n\t\t\tInsert: insert,\n\t\t\tRemove: remove,\n\t\t\tMove: move,\n\t\t\tModify: modify,\n\t\t\tEditWrapper: editWrapper,\n\t\t};\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): Zod.ZodTypeAny {\n\treturn getOrCreate(typeMap, definition, () => {\n\t\tconst nodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\t\tswitch (nodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\tfor (const [key, field] of Object.entries(nodeSchema.fields)) {\n\t\t\t\t\t// TODO: Remove when AI better\n\t\t\t\t\tif (\n\t\t\t\t\t\tArray.from(\n\t\t\t\t\t\t\tfield.allowedTypes,\n\t\t\t\t\t\t\t(n) => definitionMap.get(n) ?? fail(\"Unknown definition\"),\n\t\t\t\t\t\t).some((n) => n.kind === NodeKind.Array)\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tmodifyFieldSet.add(key);\n\t\t\t\t\tfor (const type of field.allowedTypes) {\n\t\t\t\t\t\tmodifyTypeSet.add(type);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties = Object.fromEntries(\n\t\t\t\t\tObject.entries(nodeSchema.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(\n\t\t\t\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\t\t\t\ttypeMap,\n\t\t\t\t\t\t\t\t\tinsertSet,\n\t\t\t\t\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t),\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\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\t\t\tproperties[typeField] = z.enum([definition]);\n\t\t\t\treturn z.object(properties);\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tfor (const [name] of Array.from(\n\t\t\t\t\tnodeSchema.allowedTypes,\n\t\t\t\t\t(n): [string, SimpleNodeSchema] => [\n\t\t\t\t\t\tn,\n\t\t\t\t\t\tdefinitionMap.get(n) ?? fail(\"Unknown definition\"),\n\t\t\t\t\t],\n\t\t\t\t).filter(\n\t\t\t\t\t([_, schema]) => schema.kind === NodeKind.Object || schema.kind === NodeKind.Leaf,\n\t\t\t\t)) {\n\t\t\t\t\tinsertSet.add(name);\n\t\t\t\t}\n\t\t\t\treturn z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\ttypeMap,\n\t\t\t\t\t\tinsertSet,\n\t\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\t\tnodeSchema.allowedTypes,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (nodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn z.boolean();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn z.number();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn z.string();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn 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[nodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new Error(`Unsupported node kind ${NodeKind[nodeSchema.kind]}.`);\n\t\t\t}\n\t\t}\n\t});\n}\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tfieldSchema: SimpleFieldSchema,\n): Zod.ZodTypeAny | undefined {\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn getTypeForAllowedTypes(\n\t\t\t\tdefinitionMap,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tfieldSchema.allowedTypes,\n\t\t\t);\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn z.union([\n\t\t\t\tz.null(),\n\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tfieldSchema.allowedTypes,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): Zod.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(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tname,\n\t\t\t\t);\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(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EACN,SAAS,EACT,eAAe,EACf,QAAQ,EACR,IAAI,EACJ,WAAW,GACX,MAAM,+BAA+B,CAAC;AAOvC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,CAAC;KACpB,MAAM,CAAC;IACP,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,WAAW,qCAAqC,CACtG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC;IAC7B,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,WAAW,iLAAiL,CACvM;IACF,KAAK,EAAE,CAAC;SACN,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;SACzB,QAAQ,CACR,qGAAqG,CACrG;CACF,CAAC;KACD,QAAQ,CACR,w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*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport {\n\tFieldKind,\n\tgetSimpleSchema,\n\tNodeKind,\n\tTree,\n\tValueSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNode,\n} from \"@fluidframework/tree/internal\";\nimport { z } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z\n\t.object({\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a specific object node in the tree, identified by the target object's Id.\",\n\t);\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\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 cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\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 */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, Zod.ZodTypeAny>, root: string] {\n\treturn getOrCreate(cache, schema, () => {\n\t\tconst insertSet = new Set<string>();\n\t\tconst modifyFieldSet = new Set<string>();\n\t\tconst modifyTypeSet = new Set<string>();\n\t\tconst typeMap = new Map<string, Zod.ZodTypeAny>();\n\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): Zod.ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst doesSchemaHaveArray = insertSet.size > 0;\n\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\n\t\tconst typeRecord: Record<string, Zod.ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tModify: modify,\n\t\t};\n\n\t\ttypeRecord.Modify = modify;\n\n\t\tif (doesSchemaHaveArray) {\n\t\t\ttypeRecord.ObjectPlace = objectPlace;\n\t\t\ttypeRecord.ArrayPlace = arrayPlace;\n\t\t\ttypeRecord.Range = range;\n\t\t\ttypeRecord.Insert = insert;\n\t\t\ttypeRecord.Remove = remove;\n\t\t\ttypeRecord.Move = move;\n\t\t}\n\n\t\tconst editTypes = doesSchemaHaveArray\n\t\t\t? ([insert, remove, move, modify, z.null()] as const)\n\t\t\t: ([modify, z.null()] as const);\n\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\ttypeRecord.EditWrapper = editWrapper;\n\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): Zod.ZodTypeAny {\n\treturn getOrCreate(typeMap, definition, () => {\n\t\tconst nodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\t\tswitch (nodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\tfor (const [key, field] of Object.entries(nodeSchema.fields)) {\n\t\t\t\t\t// TODO: Remove when AI better\n\t\t\t\t\tif (\n\t\t\t\t\t\tArray.from(\n\t\t\t\t\t\t\tfield.allowedTypes,\n\t\t\t\t\t\t\t(n) => definitionMap.get(n) ?? fail(\"Unknown definition\"),\n\t\t\t\t\t\t).some((n) => n.kind === NodeKind.Array)\n\t\t\t\t\t) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tmodifyFieldSet.add(key);\n\t\t\t\t\tfor (const type of field.allowedTypes) {\n\t\t\t\t\t\tmodifyTypeSet.add(type);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties = Object.fromEntries(\n\t\t\t\t\tObject.entries(nodeSchema.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(\n\t\t\t\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\t\t\t\ttypeMap,\n\t\t\t\t\t\t\t\t\tinsertSet,\n\t\t\t\t\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t),\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\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\t\t\tproperties[typeField] = z.enum([definition]);\n\t\t\t\treturn z.object(properties);\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tfor (const [name] of Array.from(\n\t\t\t\t\tnodeSchema.allowedTypes,\n\t\t\t\t\t(n): [string, SimpleNodeSchema] => [\n\t\t\t\t\t\tn,\n\t\t\t\t\t\tdefinitionMap.get(n) ?? fail(\"Unknown definition\"),\n\t\t\t\t\t],\n\t\t\t\t).filter(\n\t\t\t\t\t([_, schema]) => schema.kind === NodeKind.Object || schema.kind === NodeKind.Leaf,\n\t\t\t\t)) {\n\t\t\t\t\tinsertSet.add(name);\n\t\t\t\t}\n\t\t\t\treturn z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\ttypeMap,\n\t\t\t\t\t\tinsertSet,\n\t\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\t\tnodeSchema.allowedTypes,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (nodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn z.boolean();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn z.number();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn z.string();\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn 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[nodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new Error(`Unsupported node kind ${NodeKind[nodeSchema.kind]}.`);\n\t\t\t}\n\t\t}\n\t});\n}\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tfieldSchema: SimpleFieldSchema,\n): Zod.ZodTypeAny | undefined {\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn getTypeForAllowedTypes(\n\t\t\t\tdefinitionMap,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tfieldSchema.allowedTypes,\n\t\t\t);\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn z.union([\n\t\t\t\tz.null(),\n\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tfieldSchema.allowedTypes,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\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\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): Zod.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(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tname,\n\t\t\t\t);\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(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\n\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Determines if the provided {@link TreeNode} contains an array schema.\n */\nexport function doesNodeContainArraySchema(node: TreeNode): boolean {\n\tconst schema = Tree.schema(node);\n\tconst simpleSchema = getSimpleSchema(schema);\n\tfor (const [, nodeSchema] of simpleSchema.definitions) {\n\t\tif (nodeSchema.kind === NodeKind.Array) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/ai-collab",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "Experimental package to simplify integrating AI into Fluid-based applications",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -69,27 +69,27 @@
|
|
|
69
69
|
"temp-directory": "nyc/.nyc_output"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@fluidframework/core-utils": "~2.
|
|
73
|
-
"@fluidframework/runtime-utils": "~2.
|
|
74
|
-
"@fluidframework/telemetry-utils": "~2.
|
|
75
|
-
"@fluidframework/tree": "~2.
|
|
72
|
+
"@fluidframework/core-utils": "~2.13.0",
|
|
73
|
+
"@fluidframework/runtime-utils": "~2.13.0",
|
|
74
|
+
"@fluidframework/telemetry-utils": "~2.13.0",
|
|
75
|
+
"@fluidframework/tree": "~2.13.0",
|
|
76
76
|
"openai": "^4.67.3",
|
|
77
77
|
"typechat": "^0.1.1",
|
|
78
78
|
"zod": "^3.23.8"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@arethetypeswrong/cli": "^0.
|
|
81
|
+
"@arethetypeswrong/cli": "^0.17.1",
|
|
82
82
|
"@biomejs/biome": "~1.9.3",
|
|
83
|
-
"@fluid-internal/mocha-test-setup": "~2.
|
|
83
|
+
"@fluid-internal/mocha-test-setup": "~2.13.0",
|
|
84
84
|
"@fluid-tools/build-cli": "^0.51.0",
|
|
85
85
|
"@fluidframework/build-common": "^2.0.3",
|
|
86
86
|
"@fluidframework/build-tools": "^0.51.0",
|
|
87
87
|
"@fluidframework/eslint-config-fluid": "^5.6.0",
|
|
88
|
-
"@fluidframework/id-compressor": "~2.
|
|
89
|
-
"@fluidframework/runtime-utils": "~2.
|
|
90
|
-
"@fluidframework/test-runtime-utils": "~2.
|
|
88
|
+
"@fluidframework/id-compressor": "~2.13.0",
|
|
89
|
+
"@fluidframework/runtime-utils": "~2.13.0",
|
|
90
|
+
"@fluidframework/test-runtime-utils": "~2.13.0",
|
|
91
91
|
"@microsoft/api-extractor": "7.47.8",
|
|
92
|
-
"@types/mocha": "^
|
|
92
|
+
"@types/mocha": "^10.0.10",
|
|
93
93
|
"@types/node": "^18.19.0",
|
|
94
94
|
"c8": "^8.0.1",
|
|
95
95
|
"concurrently": "^8.2.1",
|
|
@@ -98,7 +98,6 @@
|
|
|
98
98
|
"eslint": "~8.55.0",
|
|
99
99
|
"eslint-config-prettier": "~9.0.0",
|
|
100
100
|
"mocha": "^10.2.0",
|
|
101
|
-
"mocha-json-output-reporter": "^2.0.1",
|
|
102
101
|
"mocha-multi-reporters": "^1.5.1",
|
|
103
102
|
"prettier": "~3.0.3",
|
|
104
103
|
"rimraf": "^4.4.0",
|
|
@@ -133,7 +132,7 @@
|
|
|
133
132
|
"build:test": "npm run build:test:esm && npm run build:test:cjs",
|
|
134
133
|
"build:test:cjs": "fluid-tsc commonjs --project ./src/test/tsconfig.cjs.json",
|
|
135
134
|
"build:test:esm": "tsc --project ./src/test/tsconfig.json",
|
|
136
|
-
"check:are-the-types-wrong": "
|
|
135
|
+
"check:are-the-types-wrong": "attw --pack . --profile node16",
|
|
137
136
|
"check:biome": "biome check .",
|
|
138
137
|
"check:exports": "concurrently \"npm:check:exports:*\"",
|
|
139
138
|
"check:exports:bundle-release-tags": "api-extractor run --config api-extractor/api-extractor-lint-bundle.json",
|
|
@@ -18,12 +18,8 @@ import {
|
|
|
18
18
|
FieldSchema,
|
|
19
19
|
normalizeAllowedTypes,
|
|
20
20
|
type ImplicitFieldSchema,
|
|
21
|
-
booleanSchema,
|
|
22
|
-
handleSchema,
|
|
23
|
-
nullSchema,
|
|
24
|
-
numberSchema,
|
|
25
|
-
stringSchema,
|
|
26
21
|
type IterableTreeArrayContent,
|
|
22
|
+
SchemaFactory,
|
|
27
23
|
} from "@fluidframework/tree/internal";
|
|
28
24
|
|
|
29
25
|
import {
|
|
@@ -66,25 +62,27 @@ function populateDefaults(
|
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
function getSchemaIdentifier(content: TreeEditValue): string | undefined {
|
|
65
|
+
const sf = new SchemaFactory(undefined);
|
|
66
|
+
|
|
69
67
|
switch (typeof content) {
|
|
70
68
|
case "boolean": {
|
|
71
|
-
return
|
|
69
|
+
return sf.boolean.identifier;
|
|
72
70
|
}
|
|
73
71
|
case "number": {
|
|
74
|
-
return
|
|
72
|
+
return sf.number.identifier;
|
|
75
73
|
}
|
|
76
74
|
case "string": {
|
|
77
|
-
return
|
|
75
|
+
return sf.string.identifier;
|
|
78
76
|
}
|
|
79
77
|
case "object": {
|
|
80
78
|
if (content === null) {
|
|
81
|
-
return
|
|
79
|
+
return sf.null.identifier;
|
|
82
80
|
}
|
|
83
81
|
if (Array.isArray(content)) {
|
|
84
82
|
throw new UsageError("Arrays are not currently supported in this context");
|
|
85
83
|
}
|
|
86
84
|
if (isFluidHandle(content)) {
|
|
87
|
-
return
|
|
85
|
+
return sf.handle.identifier;
|
|
88
86
|
}
|
|
89
87
|
return content[typeField];
|
|
90
88
|
}
|
|
@@ -22,7 +22,7 @@ import { createZodJsonValidator } from "typechat/zod";
|
|
|
22
22
|
|
|
23
23
|
import { objectIdKey, type TreeEdit } from "./agentEditTypes.js";
|
|
24
24
|
import type { IdGenerator } from "./idGenerator.js";
|
|
25
|
-
import { generateGenericEditTypes } from "./typeGeneration.js";
|
|
25
|
+
import { doesNodeContainArraySchema, generateGenericEditTypes } from "./typeGeneration.js";
|
|
26
26
|
import { fail } from "./utils.js";
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -82,6 +82,8 @@ export function getPlanningSystemPrompt(
|
|
|
82
82
|
The other agent follows this guidance: ${systemRoleContext}`
|
|
83
83
|
}`;
|
|
84
84
|
|
|
85
|
+
const editOptions = `modifying ${doesNodeContainArraySchema(treeNode) ? "as well as inserting, removing, or moving" : ""} elements in the tree`;
|
|
86
|
+
|
|
85
87
|
const systemPrompt = `
|
|
86
88
|
${role}
|
|
87
89
|
The application state tree is a JSON object with the following schema: ${promptFriendlySchema}
|
|
@@ -89,7 +91,7 @@ export function getPlanningSystemPrompt(
|
|
|
89
91
|
The user requested that I accomplish the following goal:
|
|
90
92
|
"${userPrompt}"
|
|
91
93
|
I've made a plan to accomplish this goal by doing a sequence of edits to the tree.
|
|
92
|
-
Edits can include
|
|
94
|
+
Edits can include ${editOptions}.
|
|
93
95
|
Here is my plan:`;
|
|
94
96
|
|
|
95
97
|
return systemPrompt;
|
|
@@ -130,20 +132,21 @@ export function getEditingSystemPrompt(
|
|
|
130
132
|
const role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${
|
|
131
133
|
appGuidance === undefined
|
|
132
134
|
? ""
|
|
133
|
-
:
|
|
134
|
-
The application that owns the JSON tree has the following guidance about your role: ${appGuidance}`
|
|
135
|
+
: `\nThe application that owns the JSON tree has the following guidance about your role: "${appGuidance}".`
|
|
135
136
|
}`;
|
|
136
137
|
|
|
137
138
|
const treeSchemaString = createZodJsonValidator(
|
|
138
139
|
...generateGenericEditTypes(getSimpleSchema(schema), false),
|
|
139
140
|
).getSchemaText();
|
|
140
141
|
|
|
142
|
+
const topLevelEditWrapperDescription = doesNodeContainArraySchema(treeNode)
|
|
143
|
+
? `contains one of the following interfaces: "Modify", null or an array node only edit: "Insert", "Remove", "Move"`
|
|
144
|
+
: `contains the interface "Modify" or null`;
|
|
145
|
+
|
|
141
146
|
// TODO: security: user prompt in system prompt
|
|
142
147
|
const systemPrompt = `
|
|
143
|
-
${role}
|
|
144
|
-
|
|
145
|
-
The top level object you produce is an "EditWrapper" object which contains one of "Insert", "Modify", "Remove", "Move", or null.
|
|
146
|
-
${treeSchemaString}
|
|
148
|
+
${role}\nEdits are JSON objects that conform to the schema described below. The top-level object you produce for a given edit is an "EditWrapper" object which ${topLevelEditWrapperDescription}.
|
|
149
|
+
\nHere are the schema definitions for an edit:\n${treeSchemaString}\n
|
|
147
150
|
The tree is a JSON object with the following schema: ${promptFriendlySchema}
|
|
148
151
|
${plan === undefined ? "" : `You have made a plan to accomplish the user's goal. The plan is: "${plan}". You will perform one or more edits that correspond to that plan to accomplish the goal.`}
|
|
149
152
|
${
|
|
@@ -4,11 +4,18 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { assert } from "@fluidframework/core-utils/internal";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
FieldKind,
|
|
9
|
+
getSimpleSchema,
|
|
10
|
+
NodeKind,
|
|
11
|
+
Tree,
|
|
12
|
+
ValueSchema,
|
|
13
|
+
} from "@fluidframework/tree/internal";
|
|
8
14
|
import type {
|
|
9
15
|
SimpleFieldSchema,
|
|
10
16
|
SimpleNodeSchema,
|
|
11
17
|
SimpleTreeSchema,
|
|
18
|
+
TreeNode,
|
|
12
19
|
} from "@fluidframework/tree/internal";
|
|
13
20
|
import { z } from "zod";
|
|
14
21
|
|
|
@@ -19,13 +26,17 @@ import { fail, getOrCreate, mapIterable } from "./utils.js";
|
|
|
19
26
|
* Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.
|
|
20
27
|
* @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.
|
|
21
28
|
*/
|
|
22
|
-
const objectTarget = z
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
const objectTarget = z
|
|
30
|
+
.object({
|
|
31
|
+
target: z
|
|
32
|
+
.string()
|
|
33
|
+
.describe(
|
|
34
|
+
`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,
|
|
35
|
+
),
|
|
36
|
+
})
|
|
37
|
+
.describe(
|
|
38
|
+
"A pointer to a specific object node in the tree, identified by the target object's Id.",
|
|
39
|
+
);
|
|
29
40
|
/**
|
|
30
41
|
* Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.
|
|
31
42
|
* @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.
|
|
@@ -101,6 +112,7 @@ export function generateGenericEditTypes(
|
|
|
101
112
|
const modifyFieldSet = new Set<string>();
|
|
102
113
|
const modifyTypeSet = new Set<string>();
|
|
103
114
|
const typeMap = new Map<string, Zod.ZodTypeAny>();
|
|
115
|
+
|
|
104
116
|
for (const name of schema.definitions.keys()) {
|
|
105
117
|
getOrCreateType(
|
|
106
118
|
schema.definitions,
|
|
@@ -132,16 +144,21 @@ export function generateGenericEditTypes(
|
|
|
132
144
|
}
|
|
133
145
|
}
|
|
134
146
|
}
|
|
135
|
-
|
|
147
|
+
|
|
148
|
+
const doesSchemaHaveArray = insertSet.size > 0;
|
|
149
|
+
|
|
150
|
+
const modify = z
|
|
136
151
|
.object({
|
|
137
|
-
type: z.
|
|
152
|
+
type: z.enum(["modify"]),
|
|
138
153
|
explanation: z.string().describe(editDescription),
|
|
139
|
-
|
|
140
|
-
|
|
154
|
+
target: objectTarget,
|
|
155
|
+
field: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields
|
|
156
|
+
modification: generateDomainTypes
|
|
157
|
+
? getType(modifyTypeSet)
|
|
141
158
|
: z.any().describe("Domain-specific content here"),
|
|
142
|
-
destination: z.union([arrayPlace, objectPlace]),
|
|
143
159
|
})
|
|
144
|
-
.describe("
|
|
160
|
+
.describe("Sets a field on a specific ObjectTarget.");
|
|
161
|
+
|
|
145
162
|
const remove = z
|
|
146
163
|
.object({
|
|
147
164
|
type: z.literal("remove"),
|
|
@@ -149,6 +166,18 @@ export function generateGenericEditTypes(
|
|
|
149
166
|
source: z.union([objectTarget, range]),
|
|
150
167
|
})
|
|
151
168
|
.describe("Deletes an object or Range of objects from the tree.");
|
|
169
|
+
|
|
170
|
+
const insert = z
|
|
171
|
+
.object({
|
|
172
|
+
type: z.literal("insert"),
|
|
173
|
+
explanation: z.string().describe(editDescription),
|
|
174
|
+
content: generateDomainTypes
|
|
175
|
+
? getType(insertSet)
|
|
176
|
+
: z.any().describe("Domain-specific content here"),
|
|
177
|
+
destination: z.union([arrayPlace, objectPlace]),
|
|
178
|
+
})
|
|
179
|
+
.describe("Inserts a new object at a specific Place or ArrayPlace.");
|
|
180
|
+
|
|
152
181
|
const move = z
|
|
153
182
|
.object({
|
|
154
183
|
type: z.literal("move"),
|
|
@@ -157,34 +186,34 @@ export function generateGenericEditTypes(
|
|
|
157
186
|
destination: z.union([arrayPlace, objectPlace]),
|
|
158
187
|
})
|
|
159
188
|
.describe("Moves an object or Range of objects to a new Place or ArrayPlace.");
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.
|
|
171
|
-
|
|
189
|
+
|
|
190
|
+
const typeRecord: Record<string, Zod.ZodTypeAny> = {
|
|
191
|
+
ObjectTarget: objectTarget,
|
|
192
|
+
Modify: modify,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
typeRecord.Modify = modify;
|
|
196
|
+
|
|
197
|
+
if (doesSchemaHaveArray) {
|
|
198
|
+
typeRecord.ObjectPlace = objectPlace;
|
|
199
|
+
typeRecord.ArrayPlace = arrayPlace;
|
|
200
|
+
typeRecord.Range = range;
|
|
201
|
+
typeRecord.Insert = insert;
|
|
202
|
+
typeRecord.Remove = remove;
|
|
203
|
+
typeRecord.Move = move;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const editTypes = doesSchemaHaveArray
|
|
207
|
+
? ([insert, remove, move, modify, z.null()] as const)
|
|
208
|
+
: ([modify, z.null()] as const);
|
|
209
|
+
|
|
172
210
|
const editWrapper = z.object({
|
|
173
211
|
edit: z
|
|
174
212
|
.union(editTypes)
|
|
175
213
|
.describe("The next edit to apply to the tree, or null if the task is complete."),
|
|
176
214
|
});
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
ObjectPlace: objectPlace,
|
|
180
|
-
ArrayPlace: arrayPlace,
|
|
181
|
-
Range: range,
|
|
182
|
-
Insert: insert,
|
|
183
|
-
Remove: remove,
|
|
184
|
-
Move: move,
|
|
185
|
-
Modify: modify,
|
|
186
|
-
EditWrapper: editWrapper,
|
|
187
|
-
};
|
|
215
|
+
typeRecord.EditWrapper = editWrapper;
|
|
216
|
+
|
|
188
217
|
return [typeRecord, "EditWrapper"];
|
|
189
218
|
});
|
|
190
219
|
}
|
|
@@ -327,6 +356,7 @@ function getOrCreateTypeForField(
|
|
|
327
356
|
}
|
|
328
357
|
}
|
|
329
358
|
}
|
|
359
|
+
|
|
330
360
|
function getTypeForAllowedTypes(
|
|
331
361
|
definitionMap: ReadonlyMap<string, SimpleNodeSchema>,
|
|
332
362
|
typeMap: Map<string, Zod.ZodTypeAny>,
|
|
@@ -362,6 +392,7 @@ function getTypeForAllowedTypes(
|
|
|
362
392
|
);
|
|
363
393
|
}
|
|
364
394
|
}
|
|
395
|
+
|
|
365
396
|
function tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {
|
|
366
397
|
if (set.size === 1) {
|
|
367
398
|
for (const item of set) {
|
|
@@ -369,6 +400,22 @@ function tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {
|
|
|
369
400
|
}
|
|
370
401
|
}
|
|
371
402
|
}
|
|
403
|
+
|
|
372
404
|
function hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {
|
|
373
405
|
return array.length >= 2;
|
|
374
406
|
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Determines if the provided {@link TreeNode} contains an array schema.
|
|
410
|
+
*/
|
|
411
|
+
export function doesNodeContainArraySchema(node: TreeNode): boolean {
|
|
412
|
+
const schema = Tree.schema(node);
|
|
413
|
+
const simpleSchema = getSimpleSchema(schema);
|
|
414
|
+
for (const [, nodeSchema] of simpleSchema.definitions) {
|
|
415
|
+
if (nodeSchema.kind === NodeKind.Array) {
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return false;
|
|
421
|
+
}
|