@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,4DAAiF;AAMjF,6BAAwB;AAExB,2DAA6D;AAC7D,yCAA4D;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAA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AAG,CAAC,CAAC;QAC1E,CAAC;IACF,CAAC;AACF,CAAC;AACD,SAAS,sBAAsB,CAC9B,aAAoD,EACpD,OAAoC,EACpC,SAAsB,EACtB,cAA2B,EAC3B,aAA0B,EAC1B,YAAiC;IAEjC,MAAM,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG;YACb,GAAG,IAAA,sBAAW,EAAC,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE;gBACrC,OAAO,eAAe,CACrB,aAAa,EACb,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,IAAI,CACJ,CAAC;YACH,CAAC,CAAC;SACF,CAAC;QACF,IAAA,iBAAM,EAAC,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,OAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;SAAM,CAAC;QACP,OAAO,eAAe,CACrB,aAAa,EACb,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,MAAM,CACN,CAAC;IACH,CAAC;AACF,CAAC;AACD,SAAS,eAAe,CAAI,GAAmB;IAC9C,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC;AACD,SAAS,aAAa,CAAI,KAAU;IACnC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["/*!\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 { 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,kEAA6D;AAC7D,4DAMuC;AAOvC,6BAAwB;AAExB,2DAA6D;AAC7D,yCAA4D;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,OAAC;KACpB,MAAM,CAAC;IACP,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,+BAAW,qCAAqC,CACtG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,OAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC;IAC7B,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,+BAAW,iLAAiL,CACvM;IACF,KAAK,EAAE,OAAC;SACN,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;SACzB,QAAQ,CACR,qGAAqG,CACrG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,UAAU,GAAG,OAAC;KAClB,MAAM,CAAC;IACP,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,YAAY,CAAC,CAAC;IAC5B,QAAQ,EAAE,OAAC;SACT,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,+BAAW,8IAA8I,CACpK;IACF,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,qCAAqC,CAAC;IACjE,QAAQ,EAAE,OAAC;SACT,IAAI,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SACtB,QAAQ,CAAC,8DAA8D,CAAC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EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,OAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;SAAM,CAAC;QACP,OAAO,eAAe,CACrB,aAAa,EACb,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,MAAM,CACN,CAAC;IACH,CAAC;AACF,CAAC;AAED,SAAS,eAAe,CAAI,GAAmB;IAC9C,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,aAAa,CAAI,KAAU;IACnC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;GAEG;AACH,SAAgB,0BAA0B,CAAC,IAAc;IACxD,MAAM,MAAM,GAAG,eAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,MAAM,CAAC,CAAC;IAC7C,KAAK,MAAM,CAAC,EAAE,UAAU,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;QACvD,IAAI,UAAU,CAAC,IAAI,KAAK,mBAAQ,CAAC,KAAK,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAVD,gEAUC","sourcesContent":["/*!\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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentEditReducer.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAKN,KAAK,QAAQ,EAEb,KAAK,gBAAgB,
|
|
1
|
+
{"version":3,"file":"agentEditReducer.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAKN,KAAK,QAAQ,EAEb,KAAK,gBAAgB,EAOrB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACN,KAAK,QAAQ,EASb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AA+DpD;;GAEG;AACH,wBAAgB,cAAc,CAC7B,QAAQ,EAAE,QAAQ,EAClB,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACpD,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,GAClC,QAAQ,CAyMV"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { assert } from "@fluidframework/core-utils/internal";
|
|
6
6
|
import { isFluidHandle } from "@fluidframework/runtime-utils";
|
|
7
7
|
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
8
|
-
import { Tree, NodeKind, FieldKind, FieldSchema, normalizeAllowedTypes,
|
|
8
|
+
import { Tree, NodeKind, FieldKind, FieldSchema, normalizeAllowedTypes, SchemaFactory, } from "@fluidframework/tree/internal";
|
|
9
9
|
import { typeField, } from "./agentEditTypes.js";
|
|
10
10
|
import { toDecoratedJson } from "./promptGeneration.js";
|
|
11
11
|
import { fail } from "./utils.js";
|
|
@@ -27,25 +27,26 @@ function populateDefaults(json, definitionMap) {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
function getSchemaIdentifier(content) {
|
|
30
|
+
const sf = new SchemaFactory(undefined);
|
|
30
31
|
switch (typeof content) {
|
|
31
32
|
case "boolean": {
|
|
32
|
-
return
|
|
33
|
+
return sf.boolean.identifier;
|
|
33
34
|
}
|
|
34
35
|
case "number": {
|
|
35
|
-
return
|
|
36
|
+
return sf.number.identifier;
|
|
36
37
|
}
|
|
37
38
|
case "string": {
|
|
38
|
-
return
|
|
39
|
+
return sf.string.identifier;
|
|
39
40
|
}
|
|
40
41
|
case "object": {
|
|
41
42
|
if (content === null) {
|
|
42
|
-
return
|
|
43
|
+
return sf.null.identifier;
|
|
43
44
|
}
|
|
44
45
|
if (Array.isArray(content)) {
|
|
45
46
|
throw new UsageError("Arrays are not currently supported in this context");
|
|
46
47
|
}
|
|
47
48
|
if (isFluidHandle(content)) {
|
|
48
|
-
return
|
|
49
|
+
return sf.handle.identifier;
|
|
49
50
|
}
|
|
50
51
|
return content[typeField];
|
|
51
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentEditRe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QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,SAAS,EACxD,CAAC;gBACF,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/C,CAAC;YAED,wBAAwB;YACxB,IAAI,QAAQ,CAAC,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;gBACjD,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;oBACpE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;gBACpD,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,SAAS,EAAE,CAAC;oBACtE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBACtD,CAAC;YACF,CAAC;YACD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,UAAU,CAAC,iBAAiB,gBAAgB,kBAAkB,CAAC,CAAC;YAC3E,CAAC;YACD,MAAM;QACP,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACT,MAAM;QACP,CAAC;IACF,CAAC;AACF,CAAC;AAOD,SAAS,mBAAmB,CAAC,IAAc;IAC1C,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,gBAAgB,GAAG,cAA6D,CAAC;IACvF,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC;AAC7C,CAAC","sourcesContent":["/*!\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 { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tTree,\n\tNodeKind,\n\ttype ImplicitAllowedTypes,\n\ttype TreeArrayNode,\n\ttype TreeNode,\n\ttype TreeNodeSchema,\n\ttype SimpleNodeSchema,\n\tFieldKind,\n\tFieldSchema,\n\tnormalizeAllowedTypes,\n\ttype ImplicitFieldSchema,\n\tbooleanSchema,\n\thandleSchema,\n\tnullSchema,\n\tnumberSchema,\n\tstringSchema,\n\ttype IterableTreeArrayContent,\n} from \"@fluidframework/tree/internal\";\n\nimport {\n\ttype TreeEdit,\n\ttype ObjectTarget,\n\ttype Selection,\n\ttype Range,\n\ttype ObjectPlace,\n\ttype ArrayPlace,\n\ttype TreeEditObject,\n\ttype TreeEditValue,\n\ttypeField,\n} from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport type { JsonValue } from \"./jsonTypes.js\";\nimport { toDecoratedJson } from \"./promptGeneration.js\";\nimport { fail } from \"./utils.js\";\n\nfunction populateDefaults(\n\tjson: JsonValue,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n): void {\n\tif (typeof json === \"object\") {\n\t\tif (json === null) {\n\t\t\treturn;\n\t\t}\n\t\tif (Array.isArray(json)) {\n\t\t\tfor (const element of json) {\n\t\t\t\tpopulateDefaults(element, definitionMap);\n\t\t\t}\n\t\t} else {\n\t\t\tassert(\n\t\t\t\ttypeof json[typeField] === \"string\",\n\t\t\t\t0xa73 /* The typeField must be present in new JSON content */,\n\t\t\t);\n\t\t\tconst nodeSchema = definitionMap.get(json[typeField]);\n\t\t\tassert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);\n\t\t}\n\t}\n}\n\nfunction getSchemaIdentifier(content: TreeEditValue): string | undefined {\n\tswitch (typeof content) {\n\t\tcase \"boolean\": {\n\t\t\treturn booleanSchema.identifier;\n\t\t}\n\t\tcase \"number\": {\n\t\t\treturn numberSchema.identifier;\n\t\t}\n\t\tcase \"string\": {\n\t\t\treturn stringSchema.identifier;\n\t\t}\n\t\tcase \"object\": {\n\t\t\tif (content === null) {\n\t\t\t\treturn nullSchema.identifier;\n\t\t\t}\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\tthrow new UsageError(\"Arrays are not currently supported in this context\");\n\t\t\t}\n\t\t\tif (isFluidHandle(content)) {\n\t\t\t\treturn handleSchema.identifier;\n\t\t\t}\n\t\t\treturn content[typeField];\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new UsageError(\"Unsupported content type\");\n\t\t}\n\t}\n}\n\nfunction contentWithIds(content: TreeNode, idGenerator: IdGenerator): TreeEditObject {\n\treturn JSON.parse(toDecoratedJson(idGenerator, content)) as TreeEditObject;\n}\n\n/**\n * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.\n */\nexport function applyAgentEdit(\n\ttreeEdit: TreeEdit,\n\tidGenerator: IdGenerator,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tvalidator?: (edit: TreeNode) => void,\n): TreeEdit {\n\tassertObjectIdsExist(treeEdit, idGenerator);\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tconst { array, index } = getPlaceInfo(treeEdit.destination, idGenerator);\n\n\t\t\tconst parentNodeSchema = Tree.schema(array);\n\t\t\tpopulateDefaults(treeEdit.content, definitionMap);\n\n\t\t\tconst schemaIdentifier = getSchemaIdentifier(treeEdit.content);\n\n\t\t\t// We assume that the parentNode for inserts edits are guaranteed to be an arrayNode.\n\t\t\tconst allowedTypes = [\n\t\t\t\t...normalizeAllowedTypes(parentNodeSchema.info as ImplicitAllowedTypes),\n\t\t\t];\n\n\t\t\tfor (const allowedType of allowedTypes.values()) {\n\t\t\t\tif (allowedType.identifier === schemaIdentifier && typeof allowedType === \"function\") {\n\t\t\t\t\tconst simpleNodeSchema = allowedType as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\t\tconst insertNode = new simpleNodeSchema(treeEdit.content);\n\t\t\t\t\tvalidator?.(insertNode);\n\t\t\t\t\tarray.insertAt(index, insertNode as unknown as IterableTreeArrayContent<never>);\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tcontent: contentWithIds(insertNode, idGenerator),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tfail(\"inserted node must be of an allowed type\");\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tconst source = treeEdit.source;\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst parentNode = Tree.parent(node);\n\t\t\t\t// Case for deleting rootNode\n\t\t\t\tif (parentNode === undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"The root is required, and cannot be removed. Please use modify edit instead.\",\n\t\t\t\t\t);\n\t\t\t\t} else if (Tree.schema(parentNode).kind === NodeKind.Array) {\n\t\t\t\t\tconst nodeIndex = Tree.key(node) as number;\n\t\t\t\t\t(parentNode as TreeArrayNode).removeAt(nodeIndex);\n\t\t\t\t} else {\n\t\t\t\t\tconst fieldKey = Tree.key(node);\n\t\t\t\t\tconst parentSchema = Tree.schema(parentNode);\n\t\t\t\t\tconst fieldSchema =\n\t\t\t\t\t\t(parentSchema.info as Record<string, ImplicitFieldSchema>)[fieldKey] ??\n\t\t\t\t\t\tfail(\"Expected field schema\");\n\t\t\t\t\tif (fieldSchema instanceof FieldSchema && fieldSchema.kind === FieldKind.Optional) {\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(parentNode as any)[fieldKey] = undefined;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${fieldKey} is required, and cannot be removed. Please use modify edit instead.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);\n\t\t\t\tarray.removeRange(startIndex, endIndex);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tconst node = getNodeFromTarget(treeEdit.target, idGenerator);\n\t\t\tconst { treeNodeSchema } = getSimpleNodeSchema(node);\n\n\t\t\tconst fieldSchema =\n\t\t\t\t(treeNodeSchema.info as Record<string, ImplicitFieldSchema>)[treeEdit.field] ??\n\t\t\t\tfail(\"Expected field schema\");\n\n\t\t\tconst modification = treeEdit.modification;\n\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access\n\t\t\tconst schemaIdentifier = (modification as any)[typeField];\n\n\t\t\tlet insertedObject: TreeNode | undefined;\n\t\t\t// if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.\n\t\t\tif (isPrimitive(modification)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t}\n\t\t\t// If the fieldSchema is a function we can grab the constructor and make an instance of that node.\n\t\t\telse if (typeof fieldSchema === \"function\") {\n\t\t\t\tconst simpleSchema = fieldSchema as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\tpopulateDefaults(modification, definitionMap);\n\t\t\t\tconst constructedModification = new simpleSchema(modification);\n\t\t\t\tvalidator?.(constructedModification);\n\t\t\t\tinsertedObject = constructedModification;\n\n\t\t\t\tif (Array.isArray(modification)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst field = (node as any)[treeEdit.field] as TreeArrayNode;\n\t\t\t\t\tassert(Array.isArray(field), 0xa75 /* the field must be an array node */);\n\t\t\t\t\tassert(\n\t\t\t\t\t\tArray.isArray(constructedModification),\n\t\t\t\t\t\t0xa76 /* the modification must be an array node */,\n\t\t\t\t\t);\n\t\t\t\t\tfield.removeRange(0);\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.\n\t\t\telse if (fieldSchema instanceof FieldSchema) {\n\t\t\t\tif (fieldSchema.kind === FieldKind.Optional && modification === undefined) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = undefined;\n\t\t\t\t} else {\n\t\t\t\t\tfor (const allowedType of fieldSchema.allowedTypeSet.values()) {\n\t\t\t\t\t\tif (allowedType.identifier === schemaIdentifier) {\n\t\t\t\t\t\t\tif (typeof allowedType === \"function\") {\n\t\t\t\t\t\t\t\tconst simpleSchema = allowedType as unknown as new (\n\t\t\t\t\t\t\t\t\tdummy: unknown,\n\t\t\t\t\t\t\t\t) => TreeNode;\n\t\t\t\t\t\t\t\tconst constructedObject = new simpleSchema(modification);\n\t\t\t\t\t\t\t\tinsertedObject = constructedObject;\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = constructedObject;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn insertedObject === undefined\n\t\t\t\t? treeEdit\n\t\t\t\t: {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tmodification: contentWithIds(insertedObject, idGenerator),\n\t\t\t\t\t};\n\t\t}\n\t\tcase \"move\": {\n\t\t\t// TODO: need to add schema check for valid moves\n\t\t\tconst source = treeEdit.source;\n\t\t\tconst destination = treeEdit.destination;\n\t\t\tconst { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(\n\t\t\t\tdestination,\n\t\t\t\tidGenerator,\n\t\t\t);\n\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst sourceNode = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst sourceIndex = Tree.key(sourceNode) as number;\n\t\t\t\tconst sourceArrayNode = Tree.parent(sourceNode) as TreeArrayNode;\n\t\t\t\tconst sourceArraySchema = Tree.schema(sourceArrayNode);\n\t\t\t\tif (sourceArraySchema.kind !== NodeKind.Array) {\n\t\t\t\t\tthrow new UsageError(\"the source node must be within an arrayNode\");\n\t\t\t\t}\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tconst nodeToMove = sourceArrayNode.at(sourceIndex);\n\t\t\t\tassert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);\n\t\t\t\tif (isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\tsourceIndex,\n\t\t\t\t\t\tsourceIndex + 1,\n\t\t\t\t\t\tsourceArrayNode,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst {\n\t\t\t\t\tarray,\n\t\t\t\t\tstartIndex: sourceStartIndex,\n\t\t\t\t\tendIndex: sourceEndIndex,\n\t\t\t\t} = getRangeInfo(source, idGenerator);\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tfor (let i = sourceStartIndex; i < sourceEndIndex; i++) {\n\t\t\t\t\tconst nodeToMove = array.at(i);\n\t\t\t\t\tassert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);\n\t\t\t\t\tif (!isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\tdestinationIndex,\n\t\t\t\t\tsourceStartIndex,\n\t\t\t\t\tsourceEndIndex,\n\t\t\t\t\tarray,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tdefault: {\n\t\t\tfail(\"invalid tree edit\");\n\t\t}\n\t}\n}\n\nfunction isNodeAllowedType(node: TreeNode, allowedTypes: TreeNodeSchema[]): boolean {\n\tfor (const allowedType of allowedTypes) {\n\t\tif (Tree.is(node, allowedType)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction isPrimitive(content: unknown): boolean {\n\treturn (\n\t\ttypeof content === \"number\" ||\n\t\ttypeof content === \"string\" ||\n\t\ttypeof content === \"boolean\" ||\n\t\tcontent === undefined ||\n\t\tcontent === null\n\t);\n}\n\nfunction isObjectTarget(selection: Selection): selection is ObjectTarget {\n\treturn Object.keys(selection).length === 1 && \"target\" in selection;\n}\n\nfunction isRange(selection: Selection): selection is Range {\n\treturn \"from\" in selection && \"to\" in selection;\n}\n\ninterface RangeInfo {\n\tarray: TreeArrayNode;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\nfunction getRangeInfo(range: Range, idGenerator: IdGenerator): RangeInfo {\n\tconst { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);\n\tconst { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);\n\n\tif (arrayFrom !== arrayTo) {\n\t\tthrow new UsageError(\n\t\t\t'The \"from\" node and \"to\" nodes of the range must be in the same parent array.',\n\t\t);\n\t}\n\n\treturn { array: arrayFrom, startIndex, endIndex };\n}\n\nfunction getPlaceInfo(\n\tplace: ObjectPlace | ArrayPlace,\n\tidGenerator: IdGenerator,\n): {\n\tarray: TreeArrayNode;\n\tindex: number;\n} {\n\tif (place.type === \"arrayPlace\") {\n\t\tconst parent = idGenerator.getNode(place.parentId) ?? fail(\"Expected parent node\");\n\t\tconst child = (parent as unknown as Record<string, unknown>)[place.field];\n\t\tif (child === undefined) {\n\t\t\tthrow new UsageError(`No child under field field`);\n\t\t}\n\t\tconst schema = Tree.schema(child as TreeNode);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: child as TreeArrayNode,\n\t\t\tindex: place.location === \"start\" ? 0 : (child as TreeArrayNode).length,\n\t\t};\n\t} else {\n\t\tconst node = getNodeFromTarget(place, idGenerator);\n\t\tconst nodeIndex = Tree.key(node);\n\t\tconst parent = Tree.parent(node);\n\t\tif (parent === undefined) {\n\t\t\tthrow new UsageError(\"TODO: root node target not supported\");\n\t\t}\n\t\tconst schema = Tree.schema(parent);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: parent as unknown as TreeArrayNode,\n\t\t\tindex: place.place === \"before\" ? (nodeIndex as number) : (nodeIndex as number) + 1,\n\t\t};\n\t}\n}\n\n/**\n * Returns the target node with the matching internal objectId using the provided {@link ObjectTarget}\n */\nfunction getNodeFromTarget(target: ObjectTarget, idGenerator: IdGenerator): TreeNode {\n\tconst node = idGenerator.getNode(target.target);\n\tassert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);\n\treturn node;\n}\n\n/**\n * Checks that the objectIds of the Tree Nodes within the givin the {@link TreeEdit} exist within the given {@link IdGenerator}\n *\n * @throws An {@link UsageError} if the objectIdKey does not exist in the {@link IdGenerator}\n */\nfunction assertObjectIdsExist(treeEdit: TreeEdit, idGenerator: IdGenerator): void {\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.target} does not exist`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.parentId} does not exist`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tthrow new UsageError(`objectIdKeys [${missingObjectIds}] does not exist`);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.source.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tif (idGenerator.getNode(treeEdit.target.target) === undefined) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.target.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"move\": {\n\t\t\tconst invalidObjectIds: string[] = [];\n\t\t\t// check the source\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tinvalidObjectIds.push(...missingObjectIds);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tinvalidObjectIds.push(treeEdit.source.target);\n\t\t\t}\n\n\t\t\t// check the destination\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.target);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.parentId);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (invalidObjectIds.length > 0) {\n\t\t\t\tthrow new UsageError(`objectIdKeys [${invalidObjectIds}] does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\ninterface SchemaInfo {\n\ttreeNodeSchema: TreeNodeSchema;\n\tsimpleNodeSchema: new (dummy: unknown) => TreeNode;\n}\n\nfunction getSimpleNodeSchema(node: TreeNode): SchemaInfo {\n\tconst treeNodeSchema = Tree.schema(node);\n\tconst simpleNodeSchema = treeNodeSchema as unknown as new (dummy: unknown) => TreeNode;\n\treturn { treeNodeSchema, simpleNodeSchema };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"agentEditReducer.js","source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AAc,CAAC,QAAQ,CAAC,MAAM,CAAC;gBAC/B,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,SAAS,EACxD,CAAC;gBACF,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/C,CAAC;YAED,wBAAwB;YACxB,IAAI,QAAQ,CAAC,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;gBACjD,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;oBACpE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;gBACpD,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,SAAS,EAAE,CAAC;oBACtE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBACtD,CAAC;YACF,CAAC;YACD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,UAAU,CAAC,iBAAiB,gBAAgB,kBAAkB,CAAC,CAAC;YAC3E,CAAC;YACD,MAAM;QACP,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACT,MAAM;QACP,CAAC;IACF,CAAC;AACF,CAAC;AAOD,SAAS,mBAAmB,CAAC,IAAc;IAC1C,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,gBAAgB,GAAG,cAA6D,CAAC;IACvF,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC;AAC7C,CAAC","sourcesContent":["/*!\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 { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tTree,\n\tNodeKind,\n\ttype ImplicitAllowedTypes,\n\ttype TreeArrayNode,\n\ttype TreeNode,\n\ttype TreeNodeSchema,\n\ttype SimpleNodeSchema,\n\tFieldKind,\n\tFieldSchema,\n\tnormalizeAllowedTypes,\n\ttype ImplicitFieldSchema,\n\ttype IterableTreeArrayContent,\n\tSchemaFactory,\n} from \"@fluidframework/tree/internal\";\n\nimport {\n\ttype TreeEdit,\n\ttype ObjectTarget,\n\ttype Selection,\n\ttype Range,\n\ttype ObjectPlace,\n\ttype ArrayPlace,\n\ttype TreeEditObject,\n\ttype TreeEditValue,\n\ttypeField,\n} from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport type { JsonValue } from \"./jsonTypes.js\";\nimport { toDecoratedJson } from \"./promptGeneration.js\";\nimport { fail } from \"./utils.js\";\n\nfunction populateDefaults(\n\tjson: JsonValue,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n): void {\n\tif (typeof json === \"object\") {\n\t\tif (json === null) {\n\t\t\treturn;\n\t\t}\n\t\tif (Array.isArray(json)) {\n\t\t\tfor (const element of json) {\n\t\t\t\tpopulateDefaults(element, definitionMap);\n\t\t\t}\n\t\t} else {\n\t\t\tassert(\n\t\t\t\ttypeof json[typeField] === \"string\",\n\t\t\t\t0xa73 /* The typeField must be present in new JSON content */,\n\t\t\t);\n\t\t\tconst nodeSchema = definitionMap.get(json[typeField]);\n\t\t\tassert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);\n\t\t}\n\t}\n}\n\nfunction getSchemaIdentifier(content: TreeEditValue): string | undefined {\n\tconst sf = new SchemaFactory(undefined);\n\n\tswitch (typeof content) {\n\t\tcase \"boolean\": {\n\t\t\treturn sf.boolean.identifier;\n\t\t}\n\t\tcase \"number\": {\n\t\t\treturn sf.number.identifier;\n\t\t}\n\t\tcase \"string\": {\n\t\t\treturn sf.string.identifier;\n\t\t}\n\t\tcase \"object\": {\n\t\t\tif (content === null) {\n\t\t\t\treturn sf.null.identifier;\n\t\t\t}\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\tthrow new UsageError(\"Arrays are not currently supported in this context\");\n\t\t\t}\n\t\t\tif (isFluidHandle(content)) {\n\t\t\t\treturn sf.handle.identifier;\n\t\t\t}\n\t\t\treturn content[typeField];\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new UsageError(\"Unsupported content type\");\n\t\t}\n\t}\n}\n\nfunction contentWithIds(content: TreeNode, idGenerator: IdGenerator): TreeEditObject {\n\treturn JSON.parse(toDecoratedJson(idGenerator, content)) as TreeEditObject;\n}\n\n/**\n * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.\n */\nexport function applyAgentEdit(\n\ttreeEdit: TreeEdit,\n\tidGenerator: IdGenerator,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tvalidator?: (edit: TreeNode) => void,\n): TreeEdit {\n\tassertObjectIdsExist(treeEdit, idGenerator);\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tconst { array, index } = getPlaceInfo(treeEdit.destination, idGenerator);\n\n\t\t\tconst parentNodeSchema = Tree.schema(array);\n\t\t\tpopulateDefaults(treeEdit.content, definitionMap);\n\n\t\t\tconst schemaIdentifier = getSchemaIdentifier(treeEdit.content);\n\n\t\t\t// We assume that the parentNode for inserts edits are guaranteed to be an arrayNode.\n\t\t\tconst allowedTypes = [\n\t\t\t\t...normalizeAllowedTypes(parentNodeSchema.info as ImplicitAllowedTypes),\n\t\t\t];\n\n\t\t\tfor (const allowedType of allowedTypes.values()) {\n\t\t\t\tif (allowedType.identifier === schemaIdentifier && typeof allowedType === \"function\") {\n\t\t\t\t\tconst simpleNodeSchema = allowedType as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\t\tconst insertNode = new simpleNodeSchema(treeEdit.content);\n\t\t\t\t\tvalidator?.(insertNode);\n\t\t\t\t\tarray.insertAt(index, insertNode as unknown as IterableTreeArrayContent<never>);\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tcontent: contentWithIds(insertNode, idGenerator),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tfail(\"inserted node must be of an allowed type\");\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tconst source = treeEdit.source;\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst parentNode = Tree.parent(node);\n\t\t\t\t// Case for deleting rootNode\n\t\t\t\tif (parentNode === undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"The root is required, and cannot be removed. Please use modify edit instead.\",\n\t\t\t\t\t);\n\t\t\t\t} else if (Tree.schema(parentNode).kind === NodeKind.Array) {\n\t\t\t\t\tconst nodeIndex = Tree.key(node) as number;\n\t\t\t\t\t(parentNode as TreeArrayNode).removeAt(nodeIndex);\n\t\t\t\t} else {\n\t\t\t\t\tconst fieldKey = Tree.key(node);\n\t\t\t\t\tconst parentSchema = Tree.schema(parentNode);\n\t\t\t\t\tconst fieldSchema =\n\t\t\t\t\t\t(parentSchema.info as Record<string, ImplicitFieldSchema>)[fieldKey] ??\n\t\t\t\t\t\tfail(\"Expected field schema\");\n\t\t\t\t\tif (fieldSchema instanceof FieldSchema && fieldSchema.kind === FieldKind.Optional) {\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(parentNode as any)[fieldKey] = undefined;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${fieldKey} is required, and cannot be removed. Please use modify edit instead.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);\n\t\t\t\tarray.removeRange(startIndex, endIndex);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tconst node = getNodeFromTarget(treeEdit.target, idGenerator);\n\t\t\tconst { treeNodeSchema } = getSimpleNodeSchema(node);\n\n\t\t\tconst fieldSchema =\n\t\t\t\t(treeNodeSchema.info as Record<string, ImplicitFieldSchema>)[treeEdit.field] ??\n\t\t\t\tfail(\"Expected field schema\");\n\n\t\t\tconst modification = treeEdit.modification;\n\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access\n\t\t\tconst schemaIdentifier = (modification as any)[typeField];\n\n\t\t\tlet insertedObject: TreeNode | undefined;\n\t\t\t// if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.\n\t\t\tif (isPrimitive(modification)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t}\n\t\t\t// If the fieldSchema is a function we can grab the constructor and make an instance of that node.\n\t\t\telse if (typeof fieldSchema === \"function\") {\n\t\t\t\tconst simpleSchema = fieldSchema as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\tpopulateDefaults(modification, definitionMap);\n\t\t\t\tconst constructedModification = new simpleSchema(modification);\n\t\t\t\tvalidator?.(constructedModification);\n\t\t\t\tinsertedObject = constructedModification;\n\n\t\t\t\tif (Array.isArray(modification)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst field = (node as any)[treeEdit.field] as TreeArrayNode;\n\t\t\t\t\tassert(Array.isArray(field), 0xa75 /* the field must be an array node */);\n\t\t\t\t\tassert(\n\t\t\t\t\t\tArray.isArray(constructedModification),\n\t\t\t\t\t\t0xa76 /* the modification must be an array node */,\n\t\t\t\t\t);\n\t\t\t\t\tfield.removeRange(0);\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.\n\t\t\telse if (fieldSchema instanceof FieldSchema) {\n\t\t\t\tif (fieldSchema.kind === FieldKind.Optional && modification === undefined) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = undefined;\n\t\t\t\t} else {\n\t\t\t\t\tfor (const allowedType of fieldSchema.allowedTypeSet.values()) {\n\t\t\t\t\t\tif (allowedType.identifier === schemaIdentifier) {\n\t\t\t\t\t\t\tif (typeof allowedType === \"function\") {\n\t\t\t\t\t\t\t\tconst simpleSchema = allowedType as unknown as new (\n\t\t\t\t\t\t\t\t\tdummy: unknown,\n\t\t\t\t\t\t\t\t) => TreeNode;\n\t\t\t\t\t\t\t\tconst constructedObject = new simpleSchema(modification);\n\t\t\t\t\t\t\t\tinsertedObject = constructedObject;\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = constructedObject;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn insertedObject === undefined\n\t\t\t\t? treeEdit\n\t\t\t\t: {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tmodification: contentWithIds(insertedObject, idGenerator),\n\t\t\t\t\t};\n\t\t}\n\t\tcase \"move\": {\n\t\t\t// TODO: need to add schema check for valid moves\n\t\t\tconst source = treeEdit.source;\n\t\t\tconst destination = treeEdit.destination;\n\t\t\tconst { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(\n\t\t\t\tdestination,\n\t\t\t\tidGenerator,\n\t\t\t);\n\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst sourceNode = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst sourceIndex = Tree.key(sourceNode) as number;\n\t\t\t\tconst sourceArrayNode = Tree.parent(sourceNode) as TreeArrayNode;\n\t\t\t\tconst sourceArraySchema = Tree.schema(sourceArrayNode);\n\t\t\t\tif (sourceArraySchema.kind !== NodeKind.Array) {\n\t\t\t\t\tthrow new UsageError(\"the source node must be within an arrayNode\");\n\t\t\t\t}\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tconst nodeToMove = sourceArrayNode.at(sourceIndex);\n\t\t\t\tassert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);\n\t\t\t\tif (isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\tsourceIndex,\n\t\t\t\t\t\tsourceIndex + 1,\n\t\t\t\t\t\tsourceArrayNode,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst {\n\t\t\t\t\tarray,\n\t\t\t\t\tstartIndex: sourceStartIndex,\n\t\t\t\t\tendIndex: sourceEndIndex,\n\t\t\t\t} = getRangeInfo(source, idGenerator);\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tfor (let i = sourceStartIndex; i < sourceEndIndex; i++) {\n\t\t\t\t\tconst nodeToMove = array.at(i);\n\t\t\t\t\tassert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);\n\t\t\t\t\tif (!isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\tdestinationIndex,\n\t\t\t\t\tsourceStartIndex,\n\t\t\t\t\tsourceEndIndex,\n\t\t\t\t\tarray,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tdefault: {\n\t\t\tfail(\"invalid tree edit\");\n\t\t}\n\t}\n}\n\nfunction isNodeAllowedType(node: TreeNode, allowedTypes: TreeNodeSchema[]): boolean {\n\tfor (const allowedType of allowedTypes) {\n\t\tif (Tree.is(node, allowedType)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction isPrimitive(content: unknown): boolean {\n\treturn (\n\t\ttypeof content === \"number\" ||\n\t\ttypeof content === \"string\" ||\n\t\ttypeof content === \"boolean\" ||\n\t\tcontent === undefined ||\n\t\tcontent === null\n\t);\n}\n\nfunction isObjectTarget(selection: Selection): selection is ObjectTarget {\n\treturn Object.keys(selection).length === 1 && \"target\" in selection;\n}\n\nfunction isRange(selection: Selection): selection is Range {\n\treturn \"from\" in selection && \"to\" in selection;\n}\n\ninterface RangeInfo {\n\tarray: TreeArrayNode;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\nfunction getRangeInfo(range: Range, idGenerator: IdGenerator): RangeInfo {\n\tconst { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);\n\tconst { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);\n\n\tif (arrayFrom !== arrayTo) {\n\t\tthrow new UsageError(\n\t\t\t'The \"from\" node and \"to\" nodes of the range must be in the same parent array.',\n\t\t);\n\t}\n\n\treturn { array: arrayFrom, startIndex, endIndex };\n}\n\nfunction getPlaceInfo(\n\tplace: ObjectPlace | ArrayPlace,\n\tidGenerator: IdGenerator,\n): {\n\tarray: TreeArrayNode;\n\tindex: number;\n} {\n\tif (place.type === \"arrayPlace\") {\n\t\tconst parent = idGenerator.getNode(place.parentId) ?? fail(\"Expected parent node\");\n\t\tconst child = (parent as unknown as Record<string, unknown>)[place.field];\n\t\tif (child === undefined) {\n\t\t\tthrow new UsageError(`No child under field field`);\n\t\t}\n\t\tconst schema = Tree.schema(child as TreeNode);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: child as TreeArrayNode,\n\t\t\tindex: place.location === \"start\" ? 0 : (child as TreeArrayNode).length,\n\t\t};\n\t} else {\n\t\tconst node = getNodeFromTarget(place, idGenerator);\n\t\tconst nodeIndex = Tree.key(node);\n\t\tconst parent = Tree.parent(node);\n\t\tif (parent === undefined) {\n\t\t\tthrow new UsageError(\"TODO: root node target not supported\");\n\t\t}\n\t\tconst schema = Tree.schema(parent);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: parent as unknown as TreeArrayNode,\n\t\t\tindex: place.place === \"before\" ? (nodeIndex as number) : (nodeIndex as number) + 1,\n\t\t};\n\t}\n}\n\n/**\n * Returns the target node with the matching internal objectId using the provided {@link ObjectTarget}\n */\nfunction getNodeFromTarget(target: ObjectTarget, idGenerator: IdGenerator): TreeNode {\n\tconst node = idGenerator.getNode(target.target);\n\tassert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);\n\treturn node;\n}\n\n/**\n * Checks that the objectIds of the Tree Nodes within the givin the {@link TreeEdit} exist within the given {@link IdGenerator}\n *\n * @throws An {@link UsageError} if the objectIdKey does not exist in the {@link IdGenerator}\n */\nfunction assertObjectIdsExist(treeEdit: TreeEdit, idGenerator: IdGenerator): void {\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.target} does not exist`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.parentId} does not exist`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tthrow new UsageError(`objectIdKeys [${missingObjectIds}] does not exist`);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.source.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tif (idGenerator.getNode(treeEdit.target.target) === undefined) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.target.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"move\": {\n\t\t\tconst invalidObjectIds: string[] = [];\n\t\t\t// check the source\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tinvalidObjectIds.push(...missingObjectIds);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tinvalidObjectIds.push(treeEdit.source.target);\n\t\t\t}\n\n\t\t\t// check the destination\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.target);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.parentId);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (invalidObjectIds.length > 0) {\n\t\t\t\tthrow new UsageError(`objectIdKeys [${invalidObjectIds}] does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\ninterface SchemaInfo {\n\ttreeNodeSchema: TreeNodeSchema;\n\tsimpleNodeSchema: new (dummy: unknown) => TreeNode;\n}\n\nfunction getSimpleNodeSchema(node: TreeNode): SchemaInfo {\n\tconst treeNodeSchema = Tree.schema(node);\n\tconst simpleNodeSchema = treeNodeSchema as unknown as new (dummy: unknown) => TreeNode;\n\treturn { treeNodeSchema, simpleNodeSchema };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promptGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/promptGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAEN,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,EAK/B,KAAK,cAAc,EAGnB,KAAK,QAAQ,EACb,MAAM,+BAA+B,CAAC;AAIvC,OAAO,EAAe,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIpD;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,EAAE,CAAC;AAEJ;;GAEG;AACH,wBAAgB,eAAe,CAC9B,WAAW,EAAE,WAAW,EACxB,IAAI,EAAE,0BAA0B,CAAC,mBAAmB,CAAC,GACnD,MAAM,CAsBR;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,MAAM,EAClB,iBAAiB,CAAC,EAAE,MAAM,GACxB,MAAM,
|
|
1
|
+
{"version":3,"file":"promptGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/promptGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAEN,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,EAK/B,KAAK,cAAc,EAGnB,KAAK,QAAQ,EACb,MAAM,+BAA+B,CAAC;AAIvC,OAAO,EAAe,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIpD;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,EAAE,CAAC;AAEJ;;GAEG;AACH,wBAAgB,eAAe,CAC9B,WAAW,EAAE,WAAW,EACxB,IAAI,EAAE,0BAA0B,CAAC,mBAAmB,CAAC,GACnD,MAAM,CAsBR;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,MAAM,EAClB,iBAAiB,CAAC,EAAE,MAAM,GACxB,MAAM,CAwBR;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUlE;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CACrC,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,OAAO,EACZ,WAAW,CAAC,EAAE,MAAM,EACpB,IAAI,CAAC,EAAE,MAAM,GACX,MAAM,CAsCR;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACpC,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,QAAQ,EAClB,qBAAqB,EAAE,MAAM,EAC7B,WAAW,CAAC,EAAE,MAAM,GAClB,MAAM,CAwBR;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,UAAU,EAAE,cAAc,GAAG,MAAM,CA6B9E;AAgDD;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAOhE"}
|
|
@@ -7,7 +7,7 @@ import { NodeKind, getJsonSchema, getSimpleSchema, Tree, } from "@fluidframework
|
|
|
7
7
|
// eslint-disable-next-line import/no-internal-modules
|
|
8
8
|
import { createZodJsonValidator } from "typechat/zod";
|
|
9
9
|
import { objectIdKey } from "./agentEditTypes.js";
|
|
10
|
-
import { generateGenericEditTypes } from "./typeGeneration.js";
|
|
10
|
+
import { doesNodeContainArraySchema, generateGenericEditTypes } from "./typeGeneration.js";
|
|
11
11
|
import { fail } from "./utils.js";
|
|
12
12
|
/**
|
|
13
13
|
* TBD
|
|
@@ -42,6 +42,7 @@ export function getPlanningSystemPrompt(treeNode, userPrompt, systemRoleContext)
|
|
|
42
42
|
? ""
|
|
43
43
|
: `
|
|
44
44
|
The other agent follows this guidance: ${systemRoleContext}`}`;
|
|
45
|
+
const editOptions = `modifying ${doesNodeContainArraySchema(treeNode) ? "as well as inserting, removing, or moving" : ""} elements in the tree`;
|
|
45
46
|
const systemPrompt = `
|
|
46
47
|
${role}
|
|
47
48
|
The application state tree is a JSON object with the following schema: ${promptFriendlySchema}
|
|
@@ -49,7 +50,7 @@ export function getPlanningSystemPrompt(treeNode, userPrompt, systemRoleContext)
|
|
|
49
50
|
The user requested that I accomplish the following goal:
|
|
50
51
|
"${userPrompt}"
|
|
51
52
|
I've made a plan to accomplish this goal by doing a sequence of edits to the tree.
|
|
52
|
-
Edits can include
|
|
53
|
+
Edits can include ${editOptions}.
|
|
53
54
|
Here is my plan:`;
|
|
54
55
|
return systemPrompt;
|
|
55
56
|
}
|
|
@@ -77,15 +78,15 @@ export function getEditingSystemPrompt(userPrompt, idGenerator, treeNode, log, a
|
|
|
77
78
|
const decoratedTreeJson = toDecoratedJson(idGenerator, treeNode);
|
|
78
79
|
const role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${appGuidance === undefined
|
|
79
80
|
? ""
|
|
80
|
-
:
|
|
81
|
-
The application that owns the JSON tree has the following guidance about your role: ${appGuidance}`}`;
|
|
81
|
+
: `\nThe application that owns the JSON tree has the following guidance about your role: "${appGuidance}".`}`;
|
|
82
82
|
const treeSchemaString = createZodJsonValidator(...generateGenericEditTypes(getSimpleSchema(schema), false)).getSchemaText();
|
|
83
|
+
const topLevelEditWrapperDescription = doesNodeContainArraySchema(treeNode)
|
|
84
|
+
? `contains one of the following interfaces: "Modify", null or an array node only edit: "Insert", "Remove", "Move"`
|
|
85
|
+
: `contains the interface "Modify" or null`;
|
|
83
86
|
// TODO: security: user prompt in system prompt
|
|
84
87
|
const systemPrompt = `
|
|
85
|
-
${role}
|
|
86
|
-
|
|
87
|
-
The top level object you produce is an "EditWrapper" object which contains one of "Insert", "Modify", "Remove", "Move", or null.
|
|
88
|
-
${treeSchemaString}
|
|
88
|
+
${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}.
|
|
89
|
+
\nHere are the schema definitions for an edit:\n${treeSchemaString}\n
|
|
89
90
|
The tree is a JSON object with the following schema: ${promptFriendlySchema}
|
|
90
91
|
${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.`}
|
|
91
92
|
${log.length === 0
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promptGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/promptGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EACN,QAAQ,EAGR,aAAa,EAKb,eAAe,EACf,IAAI,GAEJ,MAAM,+BAA+B,CAAC;AACvC,sDAAsD;AACtD,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAiB,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAWlC;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,WAAwB,EACxB,IAAqD;IAErD,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,WAAW,GAAW,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE;QAC7D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,IA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,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAkB;IACvD,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC3C,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,cAAc;QACd,OAAO,UAAU,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;AACnB,CAAC","sourcesContent":["/*!\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\tNodeKind,\n\ttype ImplicitFieldSchema,\n\ttype TreeFieldFromImplicitField,\n\tgetJsonSchema,\n\ttype JsonFieldSchema,\n\ttype JsonNodeSchema,\n\ttype JsonSchemaRef,\n\ttype JsonTreeSchema,\n\tgetSimpleSchema,\n\tTree,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { createZodJsonValidator } from \"typechat/zod\";\n\nimport { objectIdKey, type TreeEdit } from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport { generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n/**\n * A log of edits that have been made to a tree.\n * @remarks This is primarily used to help an LLM keep track of the active changes it has made.\n */\nexport type EditLog = {\n\tedit: TreeEdit;\n\terror?: string;\n}[];\n\n/**\n * TBD\n */\nexport function toDecoratedJson(\n\tidGenerator: IdGenerator,\n\troot: TreeFieldFromImplicitField<ImplicitFieldSchema>,\n): string {\n\tidGenerator.assignIds(root);\n\tconst stringified: string = JSON.stringify(root, (_, value) => {\n\t\tif (typeof value === \"object\" && !Array.isArray(value) && value !== null) {\n\t\t\t// TODO: SharedTree Team needs to either publish TreeNode as a class to use .instanceof() or a typeguard.\n\t\t\t// Uncomment this assertion back once we have a typeguard ready.\n\t\t\t// assert(isTreeNode(node), \"Non-TreeNode value in tree.\");\n\t\t\tconst objId =\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tidGenerator.getId(value) ?? fail(\"ID of new node should have been assigned.\");\n\t\t\tassert(\n\t\t\t\t!Object.prototype.hasOwnProperty.call(value, objectIdKey),\n\t\t\t\t0xa7b /* Collision of object id property. */,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\t[objectIdKey]: objId,\n\t\t\t\t...value,\n\t\t\t} as unknown;\n\t\t}\n\t\treturn value as unknown;\n\t});\n\treturn stringified;\n}\n\n/**\n * Generates a prompt designed to make an LLM produce a plan to edit the SharedTree to accomplish a user-specified goal.\n */\nexport function getPlanningSystemPrompt(\n\ttreeNode: TreeNode,\n\tuserPrompt: string,\n\tsystemRoleContext?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst role = `I'm an agent who makes plans for another agent to achieve a user-specified goal to update the state of an application.${\n\t\tsystemRoleContext === undefined\n\t\t\t? \"\"\n\t\t\t: `\n\t\t\tThe other agent follows this guidance: ${systemRoleContext}`\n\t}`;\n\n\tconst systemPrompt = `\n\t${role}\n\tThe application state tree is a JSON object with the following schema: ${promptFriendlySchema}\n\tThe current state is: ${JSON.stringify(treeNode)}.\n\tThe user requested that I accomplish the following goal:\n\t\"${userPrompt}\"\n\tI've made a plan to accomplish this goal by doing a sequence of edits to the tree.\n\tEdits can include setting the root, inserting, modifying, removing, or moving elements in the tree.\n\tHere is my plan:`;\n\n\treturn systemPrompt;\n}\n\n/**\n * Generates a prompt that provides a history of the edits an LLM has made to a SharedTree as well as any errors that occured from attemping to apply each respsecitve edit to the tree.\n */\nexport function createEditListHistoryPrompt(edits: EditLog): string {\n\treturn edits\n\t\t.map((edit, index) => {\n\t\t\tconst error =\n\t\t\t\tedit.error === undefined\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: ` This edit produced an error, and was discarded. The error message was: \"${edit.error}\"`;\n\t\t\treturn `${index + 1}. ${JSON.stringify(edit.edit)}${error}`;\n\t\t})\n\t\t.join(\"\\n\");\n}\n\n/**\n * Generates the main prompt of this explicit strategy.\n * This prompt is designed to give an LLM instructions on how it can modify a SharedTree using specific types of {@link TreeEdit}'s\n * and provides with both a serialized version of the current state of the provided tree node as well as the interfaces that compromise said tree nodes data.\n */\nexport function getEditingSystemPrompt(\n\tuserPrompt: string,\n\tidGenerator: IdGenerator,\n\ttreeNode: TreeNode,\n\tlog: EditLog,\n\tappGuidance?: string,\n\tplan?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst decoratedTreeJson = toDecoratedJson(idGenerator, treeNode);\n\n\tconst role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${\n\t\tappGuidance === undefined\n\t\t\t? \"\"\n\t\t\t: `\n\t\t\tThe application that owns the JSON tree has the following guidance about your role: ${appGuidance}`\n\t}`;\n\n\tconst treeSchemaString = createZodJsonValidator(\n\t\t...generateGenericEditTypes(getSimpleSchema(schema), false),\n\t).getSchemaText();\n\n\t// TODO: security: user prompt in system prompt\n\tconst systemPrompt = `\n\t${role}\n\tEdits are JSON objects that conform to the following schema.\n\tThe top level object you produce is an \"EditWrapper\" object which contains one of \"Insert\", \"Modify\", \"Remove\", \"Move\", or null.\n\t${treeSchemaString}\n\tThe tree is a JSON object with the following schema: ${promptFriendlySchema}\n\t${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.`}\n\t${\n\t\tlog.length === 0\n\t\t\t? \"\"\n\t\t\t: `You have already performed the following edits:\n\t\t\t${createEditListHistoryPrompt(log)}\n\t\t\tThis means that the current state of the tree reflects these changes.`\n\t}\n\tThe current state of the tree is: ${decoratedTreeJson}.\n\t${log.length > 0 ? \"Before you made the above edits t\" : \"T\"}he user requested you accomplish the following goal:\n\t\"${userPrompt}\"\n\tIf the goal is now completed or is impossible, you should return null.\n\tOtherwise, you should create an edit that makes progress towards the goal. It should have an English description (\"explanation\") of which edit to perform (specifying one of the allowed edit types).`;\n\treturn systemPrompt;\n}\n\n/**\n * Generates a prompt designed to make an LLM review the edits it created and applied to a SharedTree based\n * on a user-specified goal. This prompt is designed to give the LLM's ability to correct for mistakes and improve the accuracy/fidelity of its final set of tree edits\n */\nexport function getReviewSystemPrompt(\n\tuserPrompt: string,\n\tidGenerator: IdGenerator,\n\ttreeNode: TreeNode,\n\toriginalDecoratedJson: string,\n\tappGuidance?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst decoratedTreeJson = toDecoratedJson(idGenerator, treeNode);\n\n\tconst role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${\n\t\tappGuidance === undefined\n\t\t\t? \"\"\n\t\t\t: `\n\t\t\tThe application that owns the JSON tree has the following guidance: ${appGuidance}`\n\t}`;\n\n\t// TODO: security: user prompt in system prompt\n\tconst systemPrompt = `\n\t${role}\n\tYou have performed a number of actions already to accomplish a user request.\n\tYou must review the resulting state to determine if the actions you performed successfully accomplished the user's goal.\n\tThe tree is a JSON object with the following schema: ${promptFriendlySchema}\n\tThe state of the tree BEFORE changes was: ${originalDecoratedJson}.\n\tThe state of the tree AFTER changes is: ${decoratedTreeJson}.\n\tThe user requested that the following goal should be accomplished:\n\t${userPrompt}\n\tWas the goal accomplished?`;\n\treturn systemPrompt;\n}\n\n/**\n * Converts a fully-qualified SharedTree schema name to a single-word name for use in textual TypeScript-style types.\n *\n * @remarks\n * - TODO: Determine what to do with user-provided names that include periods (e.g. \"Foo.Bar\").\n * - TODO: Should probably ensure name starts with an uppercase character.\n */\nexport function getPromptFriendlyTreeSchema(jsonSchema: JsonTreeSchema): string {\n\tlet stringifiedSchema = \"\";\n\tfor (const [name, def] of Object.entries(jsonSchema.$defs)) {\n\t\tif (def.type !== \"object\" || def._treeNodeSchemaKind === NodeKind.Map) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tlet stringifiedEntry = `interface ${getFriendlySchemaName(name)} {`;\n\n\t\tfor (const [fieldName, fieldSchema] of Object.entries(def.properties)) {\n\t\t\tlet typeString: string;\n\t\t\tif (isJsonSchemaRef(fieldSchema)) {\n\t\t\t\tconst nextFieldName = fieldSchema.$ref;\n\t\t\t\tconst nextDef = getDef(jsonSchema.$defs, nextFieldName);\n\t\t\t\ttypeString = `${getTypeString(jsonSchema.$defs, [nextFieldName, nextDef])}`;\n\t\t\t} else {\n\t\t\t\ttypeString = `${getAnyOfTypeString(jsonSchema.$defs, fieldSchema.anyOf, true)}`;\n\t\t\t}\n\t\t\tif (def.required && !def.required.includes(fieldName)) {\n\t\t\t\ttypeString = `${typeString} | undefined`;\n\t\t\t}\n\t\t\tstringifiedEntry += ` ${fieldName}: ${typeString};`;\n\t\t}\n\n\t\tstringifiedEntry += \" }\";\n\n\t\tstringifiedSchema += (stringifiedSchema === \"\" ? \"\" : \" \") + stringifiedEntry;\n\t}\n\treturn stringifiedSchema;\n}\n\nfunction getTypeString(\n\tdefs: Record<string, JsonNodeSchema>,\n\t[name, currentDef]: [string, JsonNodeSchema],\n): string {\n\tconst { _treeNodeSchemaKind } = currentDef;\n\tif (_treeNodeSchemaKind === NodeKind.Leaf) {\n\t\treturn currentDef.type;\n\t}\n\tif (_treeNodeSchemaKind === NodeKind.Object) {\n\t\treturn getFriendlySchemaName(name);\n\t}\n\tif (_treeNodeSchemaKind === NodeKind.Array) {\n\t\tconst items = currentDef.items;\n\t\tconst innerType = isJsonSchemaRef(items)\n\t\t\t? getTypeString(defs, [items.$ref, getDef(defs, items.$ref)])\n\t\t\t: getAnyOfTypeString(defs, items.anyOf);\n\t\treturn `${innerType}[]`;\n\t}\n\tfail(\"Non-object, non-leaf, non-array schema type.\");\n}\n\nfunction getAnyOfTypeString(\n\tdefs: Record<string, JsonNodeSchema>,\n\trefList: JsonSchemaRef[],\n\ttopLevel = false,\n): string {\n\tconst typeNames: string[] = [];\n\tfor (const ref of refList) {\n\t\ttypeNames.push(getTypeString(defs, [ref.$ref, getDef(defs, ref.$ref)]));\n\t}\n\tconst typeString = typeNames.join(\" | \");\n\treturn topLevel ? typeString : `(${typeString})`;\n}\n\nfunction isJsonSchemaRef(field: JsonFieldSchema): field is JsonSchemaRef {\n\treturn (field as JsonSchemaRef).$ref !== undefined;\n}\n\nfunction getDef(defs: Record<string, JsonNodeSchema>, ref: string): JsonNodeSchema {\n\t// strip the \"#/$defs/\" prefix\n\tconst strippedRef = ref.slice(8);\n\tconst nextDef = defs[strippedRef];\n\tassert(nextDef !== undefined, 0xa7c /* Ref not found. */);\n\treturn nextDef;\n}\n\n/**\n * TBD\n */\nexport function getFriendlySchemaName(schemaName: string): string {\n\tconst matches = schemaName.match(/[^.]+$/);\n\tif (matches === null) {\n\t\t// empty scope\n\t\treturn schemaName;\n\t}\n\treturn matches[0];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"promptGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/promptGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EACN,QAAQ,EAGR,aAAa,EAKb,eAAe,EACf,IAAI,GAEJ,MAAM,+BAA+B,CAAC;AACvC,sDAAsD;AACtD,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAiB,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC3F,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAWlC;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,WAAwB,EACxB,IAAqD;IAErD,WAAW,CAAC,S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U,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACzC,OAAO,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,UAAU,GAAG,CAAC;AAClD,CAAC;AAED,SAAS,eAAe,CAAC,KAAsB;IAC9C,OAAQ,KAAuB,CAAC,IAAI,KAAK,SAAS,CAAC;AACpD,CAAC;AAED,SAAS,MAAM,CAAC,IAAoC,EAAE,GAAW;IAChE,8BAA8B;IAC9B,MAAM,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjC,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC;IAClC,MAAM,CAAC,OAAO,KAAK,SAAS,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAAC;IAC1D,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAkB;IACvD,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC3C,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,cAAc;QACd,OAAO,UAAU,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;AACnB,CAAC","sourcesContent":["/*!\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\tNodeKind,\n\ttype ImplicitFieldSchema,\n\ttype TreeFieldFromImplicitField,\n\tgetJsonSchema,\n\ttype JsonFieldSchema,\n\ttype JsonNodeSchema,\n\ttype JsonSchemaRef,\n\ttype JsonTreeSchema,\n\tgetSimpleSchema,\n\tTree,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { createZodJsonValidator } from \"typechat/zod\";\n\nimport { objectIdKey, type TreeEdit } from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport { doesNodeContainArraySchema, generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n/**\n * A log of edits that have been made to a tree.\n * @remarks This is primarily used to help an LLM keep track of the active changes it has made.\n */\nexport type EditLog = {\n\tedit: TreeEdit;\n\terror?: string;\n}[];\n\n/**\n * TBD\n */\nexport function toDecoratedJson(\n\tidGenerator: IdGenerator,\n\troot: TreeFieldFromImplicitField<ImplicitFieldSchema>,\n): string {\n\tidGenerator.assignIds(root);\n\tconst stringified: string = JSON.stringify(root, (_, value) => {\n\t\tif (typeof value === \"object\" && !Array.isArray(value) && value !== null) {\n\t\t\t// TODO: SharedTree Team needs to either publish TreeNode as a class to use .instanceof() or a typeguard.\n\t\t\t// Uncomment this assertion back once we have a typeguard ready.\n\t\t\t// assert(isTreeNode(node), \"Non-TreeNode value in tree.\");\n\t\t\tconst objId =\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tidGenerator.getId(value) ?? fail(\"ID of new node should have been assigned.\");\n\t\t\tassert(\n\t\t\t\t!Object.prototype.hasOwnProperty.call(value, objectIdKey),\n\t\t\t\t0xa7b /* Collision of object id property. */,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\t[objectIdKey]: objId,\n\t\t\t\t...value,\n\t\t\t} as unknown;\n\t\t}\n\t\treturn value as unknown;\n\t});\n\treturn stringified;\n}\n\n/**\n * Generates a prompt designed to make an LLM produce a plan to edit the SharedTree to accomplish a user-specified goal.\n */\nexport function getPlanningSystemPrompt(\n\ttreeNode: TreeNode,\n\tuserPrompt: string,\n\tsystemRoleContext?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst role = `I'm an agent who makes plans for another agent to achieve a user-specified goal to update the state of an application.${\n\t\tsystemRoleContext === undefined\n\t\t\t? \"\"\n\t\t\t: `\n\t\t\tThe other agent follows this guidance: ${systemRoleContext}`\n\t}`;\n\n\tconst editOptions = `modifying ${doesNodeContainArraySchema(treeNode) ? \"as well as inserting, removing, or moving\" : \"\"} elements in the tree`;\n\n\tconst systemPrompt = `\n\t${role}\n\tThe application state tree is a JSON object with the following schema: ${promptFriendlySchema}\n\tThe current state is: ${JSON.stringify(treeNode)}.\n\tThe user requested that I accomplish the following goal:\n\t\"${userPrompt}\"\n\tI've made a plan to accomplish this goal by doing a sequence of edits to the tree.\n\tEdits can include ${editOptions}.\n\tHere is my plan:`;\n\n\treturn systemPrompt;\n}\n\n/**\n * Generates a prompt that provides a history of the edits an LLM has made to a SharedTree as well as any errors that occured from attemping to apply each respsecitve edit to the tree.\n */\nexport function createEditListHistoryPrompt(edits: EditLog): string {\n\treturn edits\n\t\t.map((edit, index) => {\n\t\t\tconst error =\n\t\t\t\tedit.error === undefined\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: ` This edit produced an error, and was discarded. The error message was: \"${edit.error}\"`;\n\t\t\treturn `${index + 1}. ${JSON.stringify(edit.edit)}${error}`;\n\t\t})\n\t\t.join(\"\\n\");\n}\n\n/**\n * Generates the main prompt of this explicit strategy.\n * This prompt is designed to give an LLM instructions on how it can modify a SharedTree using specific types of {@link TreeEdit}'s\n * and provides with both a serialized version of the current state of the provided tree node as well as the interfaces that compromise said tree nodes data.\n */\nexport function getEditingSystemPrompt(\n\tuserPrompt: string,\n\tidGenerator: IdGenerator,\n\ttreeNode: TreeNode,\n\tlog: EditLog,\n\tappGuidance?: string,\n\tplan?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst decoratedTreeJson = toDecoratedJson(idGenerator, treeNode);\n\n\tconst role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${\n\t\tappGuidance === undefined\n\t\t\t? \"\"\n\t\t\t: `\\nThe application that owns the JSON tree has the following guidance about your role: \"${appGuidance}\".`\n\t}`;\n\n\tconst treeSchemaString = createZodJsonValidator(\n\t\t...generateGenericEditTypes(getSimpleSchema(schema), false),\n\t).getSchemaText();\n\n\tconst topLevelEditWrapperDescription = doesNodeContainArraySchema(treeNode)\n\t\t? `contains one of the following interfaces: \"Modify\", null or an array node only edit: \"Insert\", \"Remove\", \"Move\"`\n\t\t: `contains the interface \"Modify\" or null`;\n\n\t// TODO: security: user prompt in system prompt\n\tconst systemPrompt = `\n\t${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}.\n\t\\nHere are the schema definitions for an edit:\\n${treeSchemaString}\\n\n\tThe tree is a JSON object with the following schema: ${promptFriendlySchema}\n\t${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.`}\n\t${\n\t\tlog.length === 0\n\t\t\t? \"\"\n\t\t\t: `You have already performed the following edits:\n\t\t\t${createEditListHistoryPrompt(log)}\n\t\t\tThis means that the current state of the tree reflects these changes.`\n\t}\n\tThe current state of the tree is: ${decoratedTreeJson}.\n\t${log.length > 0 ? \"Before you made the above edits t\" : \"T\"}he user requested you accomplish the following goal:\n\t\"${userPrompt}\"\n\tIf the goal is now completed or is impossible, you should return null.\n\tOtherwise, you should create an edit that makes progress towards the goal. It should have an English description (\"explanation\") of which edit to perform (specifying one of the allowed edit types).`;\n\treturn systemPrompt;\n}\n\n/**\n * Generates a prompt designed to make an LLM review the edits it created and applied to a SharedTree based\n * on a user-specified goal. This prompt is designed to give the LLM's ability to correct for mistakes and improve the accuracy/fidelity of its final set of tree edits\n */\nexport function getReviewSystemPrompt(\n\tuserPrompt: string,\n\tidGenerator: IdGenerator,\n\ttreeNode: TreeNode,\n\toriginalDecoratedJson: string,\n\tappGuidance?: string,\n): string {\n\tconst schema = Tree.schema(treeNode);\n\tconst promptFriendlySchema = getPromptFriendlyTreeSchema(getJsonSchema(schema));\n\tconst decoratedTreeJson = toDecoratedJson(idGenerator, treeNode);\n\n\tconst role = `You are a collaborative agent who interacts with a JSON tree by performing edits to achieve a user-specified goal.${\n\t\tappGuidance === undefined\n\t\t\t? \"\"\n\t\t\t: `\n\t\t\tThe application that owns the JSON tree has the following guidance: ${appGuidance}`\n\t}`;\n\n\t// TODO: security: user prompt in system prompt\n\tconst systemPrompt = `\n\t${role}\n\tYou have performed a number of actions already to accomplish a user request.\n\tYou must review the resulting state to determine if the actions you performed successfully accomplished the user's goal.\n\tThe tree is a JSON object with the following schema: ${promptFriendlySchema}\n\tThe state of the tree BEFORE changes was: ${originalDecoratedJson}.\n\tThe state of the tree AFTER changes is: ${decoratedTreeJson}.\n\tThe user requested that the following goal should be accomplished:\n\t${userPrompt}\n\tWas the goal accomplished?`;\n\treturn systemPrompt;\n}\n\n/**\n * Converts a fully-qualified SharedTree schema name to a single-word name for use in textual TypeScript-style types.\n *\n * @remarks\n * - TODO: Determine what to do with user-provided names that include periods (e.g. \"Foo.Bar\").\n * - TODO: Should probably ensure name starts with an uppercase character.\n */\nexport function getPromptFriendlyTreeSchema(jsonSchema: JsonTreeSchema): string {\n\tlet stringifiedSchema = \"\";\n\tfor (const [name, def] of Object.entries(jsonSchema.$defs)) {\n\t\tif (def.type !== \"object\" || def._treeNodeSchemaKind === NodeKind.Map) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tlet stringifiedEntry = `interface ${getFriendlySchemaName(name)} {`;\n\n\t\tfor (const [fieldName, fieldSchema] of Object.entries(def.properties)) {\n\t\t\tlet typeString: string;\n\t\t\tif (isJsonSchemaRef(fieldSchema)) {\n\t\t\t\tconst nextFieldName = fieldSchema.$ref;\n\t\t\t\tconst nextDef = getDef(jsonSchema.$defs, nextFieldName);\n\t\t\t\ttypeString = `${getTypeString(jsonSchema.$defs, [nextFieldName, nextDef])}`;\n\t\t\t} else {\n\t\t\t\ttypeString = `${getAnyOfTypeString(jsonSchema.$defs, fieldSchema.anyOf, true)}`;\n\t\t\t}\n\t\t\tif (def.required && !def.required.includes(fieldName)) {\n\t\t\t\ttypeString = `${typeString} | undefined`;\n\t\t\t}\n\t\t\tstringifiedEntry += ` ${fieldName}: ${typeString};`;\n\t\t}\n\n\t\tstringifiedEntry += \" }\";\n\n\t\tstringifiedSchema += (stringifiedSchema === \"\" ? \"\" : \" \") + stringifiedEntry;\n\t}\n\treturn stringifiedSchema;\n}\n\nfunction getTypeString(\n\tdefs: Record<string, JsonNodeSchema>,\n\t[name, currentDef]: [string, JsonNodeSchema],\n): string {\n\tconst { _treeNodeSchemaKind } = currentDef;\n\tif (_treeNodeSchemaKind === NodeKind.Leaf) {\n\t\treturn currentDef.type;\n\t}\n\tif (_treeNodeSchemaKind === NodeKind.Object) {\n\t\treturn getFriendlySchemaName(name);\n\t}\n\tif (_treeNodeSchemaKind === NodeKind.Array) {\n\t\tconst items = currentDef.items;\n\t\tconst innerType = isJsonSchemaRef(items)\n\t\t\t? getTypeString(defs, [items.$ref, getDef(defs, items.$ref)])\n\t\t\t: getAnyOfTypeString(defs, items.anyOf);\n\t\treturn `${innerType}[]`;\n\t}\n\tfail(\"Non-object, non-leaf, non-array schema type.\");\n}\n\nfunction getAnyOfTypeString(\n\tdefs: Record<string, JsonNodeSchema>,\n\trefList: JsonSchemaRef[],\n\ttopLevel = false,\n): string {\n\tconst typeNames: string[] = [];\n\tfor (const ref of refList) {\n\t\ttypeNames.push(getTypeString(defs, [ref.$ref, getDef(defs, ref.$ref)]));\n\t}\n\tconst typeString = typeNames.join(\" | \");\n\treturn topLevel ? typeString : `(${typeString})`;\n}\n\nfunction isJsonSchemaRef(field: JsonFieldSchema): field is JsonSchemaRef {\n\treturn (field as JsonSchemaRef).$ref !== undefined;\n}\n\nfunction getDef(defs: Record<string, JsonNodeSchema>, ref: string): JsonNodeSchema {\n\t// strip the \"#/$defs/\" prefix\n\tconst strippedRef = ref.slice(8);\n\tconst nextDef = defs[strippedRef];\n\tassert(nextDef !== undefined, 0xa7c /* Ref not found. */);\n\treturn nextDef;\n}\n\n/**\n * TBD\n */\nexport function getFriendlySchemaName(schemaName: string): string {\n\tconst matches = schemaName.match(/[^.]+$/);\n\tif (matches === null) {\n\t\t// empty scope\n\t\treturn schemaName;\n\t}\n\treturn matches[0];\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import type { SimpleTreeSchema } from "@fluidframework/tree/internal";
|
|
5
|
+
import type { SimpleTreeSchema, TreeNode } from "@fluidframework/tree/internal";
|
|
6
6
|
/**
|
|
7
7
|
* Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}
|
|
8
8
|
* 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.
|
|
@@ -12,4 +12,8 @@ import type { SimpleTreeSchema } from "@fluidframework/tree/internal";
|
|
|
12
12
|
* @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.
|
|
13
13
|
*/
|
|
14
14
|
export declare function generateGenericEditTypes(schema: SimpleTreeSchema, generateDomainTypes: boolean): [Record<string, Zod.ZodTypeAny>, root: string];
|
|
15
|
+
/**
|
|
16
|
+
* Determines if the provided {@link TreeNode} contains an array schema.
|
|
17
|
+
*/
|
|
18
|
+
export declare function doesNodeContainArraySchema(node: TreeNode): boolean;
|
|
15
19
|
//# sourceMappingURL=typeGeneration.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAGX,gBAAgB,EAChB,QAAQ,EACR,MAAM,+BAA+B,CAAC;AA+EvC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,gBAAgB,EACxB,mBAAmB,EAAE,OAAO,GAC1B,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CA8GhD;AA6LD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAUlE"}
|