@fluidframework/ai-collab 2.10.0-307399 → 2.10.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 +4 -0
- package/dist/explicit-strategy/agentEditReducer.js +7 -7
- package/dist/explicit-strategy/agentEditReducer.js.map +1 -1
- package/dist/explicit-strategy/idGenerator.js +1 -1
- package/dist/explicit-strategy/idGenerator.js.map +1 -1
- package/dist/explicit-strategy/promptGeneration.js +2 -2
- package/dist/explicit-strategy/promptGeneration.js.map +1 -1
- package/dist/explicit-strategy/typeGeneration.js +2 -2
- package/dist/explicit-strategy/typeGeneration.js.map +1 -1
- package/lib/explicit-strategy/agentEditReducer.js +7 -7
- package/lib/explicit-strategy/agentEditReducer.js.map +1 -1
- package/lib/explicit-strategy/idGenerator.js +1 -1
- package/lib/explicit-strategy/idGenerator.js.map +1 -1
- package/lib/explicit-strategy/promptGeneration.js +2 -2
- package/lib/explicit-strategy/promptGeneration.js.map +1 -1
- package/lib/explicit-strategy/typeGeneration.js +2 -2
- package/lib/explicit-strategy/typeGeneration.js.map +1 -1
- package/package.json +9 -9
- package/src/explicit-strategy/agentEditReducer.ts +7 -7
- package/src/explicit-strategy/idGenerator.ts +1 -1
- package/src/explicit-strategy/promptGeneration.ts +2 -2
- package/src/explicit-strategy/typeGeneration.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -23,9 +23,9 @@ function populateDefaults(json, definitionMap) {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
|
-
(0, internal_1.assert)(typeof json[agentEditTypes_js_1.typeField] === "string",
|
|
26
|
+
(0, internal_1.assert)(typeof json[agentEditTypes_js_1.typeField] === "string", 0xa73 /* The typeField must be present in new JSON content */);
|
|
27
27
|
const nodeSchema = definitionMap.get(json[agentEditTypes_js_1.typeField]);
|
|
28
|
-
(0, internal_1.assert)(nodeSchema?.kind === internal_3.NodeKind.Object,
|
|
28
|
+
(0, internal_1.assert)(nodeSchema?.kind === internal_3.NodeKind.Object, 0xa74 /* Expected object schema */);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -146,8 +146,8 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
|
|
|
146
146
|
if (Array.isArray(modification)) {
|
|
147
147
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
148
148
|
const field = node[treeEdit.field];
|
|
149
|
-
(0, internal_1.assert)(Array.isArray(field),
|
|
150
|
-
(0, internal_1.assert)(Array.isArray(constructedModification),
|
|
149
|
+
(0, internal_1.assert)(Array.isArray(field), 0xa75 /* the field must be an array node */);
|
|
150
|
+
(0, internal_1.assert)(Array.isArray(constructedModification), 0xa76 /* the modification must be an array node */);
|
|
151
151
|
field.removeRange(0);
|
|
152
152
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
153
153
|
node[treeEdit.field] = constructedModification;
|
|
@@ -206,7 +206,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
|
|
|
206
206
|
...(0, internal_3.normalizeAllowedTypes)(destinationArraySchema.info),
|
|
207
207
|
];
|
|
208
208
|
const nodeToMove = sourceArrayNode.at(sourceIndex);
|
|
209
|
-
(0, internal_1.assert)(nodeToMove !== undefined,
|
|
209
|
+
(0, internal_1.assert)(nodeToMove !== undefined, 0xa77 /* node to move must exist */);
|
|
210
210
|
if (isNodeAllowedType(nodeToMove, allowedTypes)) {
|
|
211
211
|
destinationArrayNode.moveRangeToIndex(destinationIndex, sourceIndex, sourceIndex + 1, sourceArrayNode);
|
|
212
212
|
}
|
|
@@ -222,7 +222,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
|
|
|
222
222
|
];
|
|
223
223
|
for (let i = sourceStartIndex; i < sourceEndIndex; i++) {
|
|
224
224
|
const nodeToMove = array.at(i);
|
|
225
|
-
(0, internal_1.assert)(nodeToMove !== undefined,
|
|
225
|
+
(0, internal_1.assert)(nodeToMove !== undefined, 0xa78 /* node to move must exist */);
|
|
226
226
|
if (!isNodeAllowedType(nodeToMove, allowedTypes)) {
|
|
227
227
|
throw new internal_2.UsageError("Illegal node type in destination array");
|
|
228
228
|
}
|
|
@@ -304,7 +304,7 @@ function getPlaceInfo(place, idGenerator) {
|
|
|
304
304
|
*/
|
|
305
305
|
function getNodeFromTarget(target, idGenerator) {
|
|
306
306
|
const node = idGenerator.getNode(target.target);
|
|
307
|
-
(0, internal_1.assert)(node !== undefined,
|
|
307
|
+
(0, internal_1.assert)(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);
|
|
308
308
|
return node;
|
|
309
309
|
}
|
|
310
310
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentEditReducer.js","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,iEAA8D;AAC9D,uEAAsE;AACtE,4DAkBuC;AAEvC,2DAU6B;AAG7B,+DAAwD;AACxD,yCAAkC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;IAEpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC5B,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAA,iBAAM,EACL,OAAO,IAAI,CAAC,6BAAS,CAAC,KAAK,QAAQ,EACnC,mDAAmD,CACnD,CAAC;YACF,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,6BAAS,CAAC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C,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\t\"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, \"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), \"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\t\"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, \"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, \"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, \"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","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,iEAA8D;AAC9D,uEAAsE;AACtE,4DAkBuC;AAEvC,2DAU6B;AAG7B,+DAAwD;AACxD,yCAAkC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;IAEpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC5B,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAA,iBAAM,EACL,OAAO,IAAI,CAAC,6BAAS,CAAC,KAAK,QAAQ,EACnC,KAAK,CAAC,uDAAuD,CAC7D,CAAC;YACF,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,6BAAS,CAAC,CAAC,CAAC;YACtD,IAAA,iBAAM,EAAC,UAAU,EAAE,IAAI,KAAK,mBAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAClF,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,mBAAmB,CAAC,OAAsB;IAClD,QAAQ,OAAO,OAAO,EAAE,CAAC;QACxB,KAAK,SAAS,CAAC,CAAC,CAAC;YAChB,OAAO,wBAAa,CAAC,UAAU,CAAC;QACjC,CAAC;QACD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AO,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,qBAAU,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,eAAI,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"]}
|
|
@@ -63,7 +63,7 @@ class IdGenerator {
|
|
|
63
63
|
const lastSegment = segments[segments.length - 1] ?? (0, internal_1.oob)();
|
|
64
64
|
const prefix = segments.length > 1 ? segments.slice(0, -1).join(".") : "";
|
|
65
65
|
// Check if the last segment already exists with a different prefix
|
|
66
|
-
(0, internal_1.assert)(!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix,
|
|
66
|
+
(0, internal_1.assert)(!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix, 0xa7a /* Different scopes not supported yet. */);
|
|
67
67
|
this.prefixMap.set(lastSegment, prefix);
|
|
68
68
|
const count = this.idCountMap.get(lastSegment) ?? 1;
|
|
69
69
|
this.idCountMap.set(lastSegment, count + 1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idGenerator.js","sourceRoot":"","sources":["../../src/explicit-strategy/idGenerator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAAkE;AAClE,4DAA+D;AAQ/D;;;GAGG;AACH,MAAa,WAAW;IAMvB;QALiB,eAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;QACvC,cAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;QACtC,gBAAW,GAAG,IAAI,GAAG,EAAoB,CAAC;QAC1C,gBAAW,GAAG,IAAI,GAAG,EAAoB,CAAC;IAErC,CAAC;IAEhB,aAAa,CAAC,IAAc;QAClC,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC9C,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC9B,OAAO,UAAU,CAAC;QACnB,CAAC;QAED,MAAM,MAAM,GAAG,eAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC;QAC5C,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACnC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC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AAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAE1E,mEAAmE;QACnE,IAAA,iBAAM,EACL,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,CAAC,KAAK,MAAM,EAC9E,KAAK,CAAC,yCAAyC,CAC/C,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACpD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QAE5C,OAAO,GAAG,WAAW,GAAG,KAAK,EAAE,CAAC;IACjC,CAAC;CACD;AAvED,kCAuEC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, oob } from \"@fluidframework/core-utils/internal\";\nimport { Tree, NodeKind } from \"@fluidframework/tree/internal\";\nimport type {\n\tTreeNode,\n\tImplicitFieldSchema,\n\tTreeArrayNode,\n\tTreeFieldFromImplicitField,\n} from \"@fluidframework/tree/internal\";\n\n/**\n * Given a tree, generates a set of LLM-friendly, unique IDs for each node in the tree.\n * @remarks The ability to uniquely and stably in the tree is important for the LLM and this library to create and distinguish between different types certain {@link TreeEdit}s.\n */\nexport class IdGenerator {\n\tprivate readonly idCountMap = new Map<string, number>();\n\tprivate readonly prefixMap = new Map<string, string>();\n\tprivate readonly nodeToIdMap = new Map<TreeNode, string>();\n\tprivate readonly idToNodeMap = new Map<string, TreeNode>();\n\n\tpublic constructor() {}\n\n\tpublic getOrCreateId(node: TreeNode): string {\n\t\tconst existingID = this.nodeToIdMap.get(node);\n\t\tif (existingID !== undefined) {\n\t\t\treturn existingID;\n\t\t}\n\n\t\tconst schema = Tree.schema(node).identifier;\n\t\tconst id = this.generateID(schema);\n\t\tthis.nodeToIdMap.set(node, id);\n\t\tthis.idToNodeMap.set(id, node);\n\n\t\treturn id;\n\t}\n\n\tpublic getNode(id: string): TreeNode | undefined {\n\t\treturn this.idToNodeMap.get(id);\n\t}\n\n\tpublic getId(node: TreeNode): string | undefined {\n\t\treturn this.nodeToIdMap.get(node);\n\t}\n\n\tpublic assignIds(node: TreeFieldFromImplicitField<ImplicitFieldSchema>): string | undefined {\n\t\tif (typeof node === \"object\" && node !== null) {\n\t\t\tconst schema = Tree.schema(node as unknown as TreeNode);\n\t\t\tif (schema.kind === NodeKind.Array) {\n\t\t\t\tfor (const element of node as unknown as TreeArrayNode) {\n\t\t\t\t\tthis.assignIds(element);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// TODO: SharedTree Team needs to either publish TreeNode as a class to use .instanceof() or a typeguard.\n\t\t\t\t// Uncomment this assertion back once we have a typeguard ready.\n\t\t\t\t// assert(isTreeNode(node), \"Non-TreeNode value in tree.\");\n\t\t\t\tconst objId = this.getOrCreateId(node as TreeNode);\n\t\t\t\tfor (const key of Object.keys(node)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument\n\t\t\t\t\tthis.assignIds((node as unknown as any)[key]);\n\t\t\t\t}\n\t\t\t\treturn objId;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate generateID(schema: string): string {\n\t\tconst segments = schema.split(\".\");\n\n\t\t// If there's no period, the schema itself is the last segment\n\t\tconst lastSegment = segments[segments.length - 1] ?? oob();\n\t\tconst prefix = segments.length > 1 ? segments.slice(0, -1).join(\".\") : \"\";\n\n\t\t// Check if the last segment already exists with a different prefix\n\t\tassert(\n\t\t\t!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix,\n\t\t\t0xa7a /* Different scopes not supported yet. */,\n\t\t);\n\n\t\tthis.prefixMap.set(lastSegment, prefix);\n\t\tconst count = this.idCountMap.get(lastSegment) ?? 1;\n\t\tthis.idCountMap.set(lastSegment, count + 1);\n\n\t\treturn `${lastSegment}${count}`;\n\t}\n}\n"]}
|
|
@@ -25,7 +25,7 @@ function toDecoratedJson(idGenerator, root) {
|
|
|
25
25
|
const objId =
|
|
26
26
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
27
27
|
idGenerator.getId(value) ?? (0, utils_js_1.fail)("ID of new node should have been assigned.");
|
|
28
|
-
(0, internal_1.assert)(!Object.prototype.hasOwnProperty.call(value, agentEditTypes_js_1.objectIdKey),
|
|
28
|
+
(0, internal_1.assert)(!Object.prototype.hasOwnProperty.call(value, agentEditTypes_js_1.objectIdKey), 0xa7b /* Collision of object id property. */);
|
|
29
29
|
return {
|
|
30
30
|
[agentEditTypes_js_1.objectIdKey]: objId,
|
|
31
31
|
...value,
|
|
@@ -200,7 +200,7 @@ function getDef(defs, ref) {
|
|
|
200
200
|
// strip the "#/$defs/" prefix
|
|
201
201
|
const strippedRef = ref.slice(8);
|
|
202
202
|
const nextDef = defs[strippedRef];
|
|
203
|
-
(0, internal_1.assert)(nextDef !== undefined,
|
|
203
|
+
(0, internal_1.assert)(nextDef !== undefined, 0xa7c /* Ref not found. */);
|
|
204
204
|
return nextDef;
|
|
205
205
|
}
|
|
206
206
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promptGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/promptGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,4DAYuC;AACvC,sDAAsD;AACtD,sCAAsD;AAEtD,2DAAiE;AAEjE,2DAA+D;AAC/D,yCAAkC;AAWlC;;G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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,IAAA,iBAAM,EAAC,OAAO,KAAK,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAChD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,SAAgB,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;AAPD,sDAOC","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\t`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, \"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,kEAA6D;AAC7D,4DAYuC;AACvC,sDAAsD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MAAM,UAAU,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,IAAA,iBAAM,EAAC,OAAO,KAAK,SAAS,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAAC;IAC1D,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,SAAgB,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;AAPD,sDAOC","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"]}
|
|
@@ -92,7 +92,7 @@ function generateGenericEditTypes(schema, generateDomainTypes) {
|
|
|
92
92
|
}
|
|
93
93
|
default: {
|
|
94
94
|
const types = Array.from(allowedTypes, (name) => typeMap.get(name) ?? (0, utils_js_1.fail)("Unknown type"));
|
|
95
|
-
(0, internal_1.assert)(hasAtLeastTwo(types),
|
|
95
|
+
(0, internal_1.assert)(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);
|
|
96
96
|
return zod_1.z.union(types);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
@@ -244,7 +244,7 @@ function getTypeForAllowedTypes(definitionMap, typeMap, insertSet, modifyFieldSe
|
|
|
244
244
|
return getOrCreateType(definitionMap, typeMap, insertSet, modifyFieldSet, modifyTypeSet, name);
|
|
245
245
|
}),
|
|
246
246
|
];
|
|
247
|
-
(0, internal_1.assert)(hasAtLeastTwo(types),
|
|
247
|
+
(0, internal_1.assert)(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
|
|
248
248
|
return zod_1.z.union(types);
|
|
249
249
|
}
|
|
250
250
|
else {
|
|
@@ -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,GAAG,OAAC,CAAC,MAAM,CAAC;IAC7B,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,+BAAW,qCAAqC,CACtG;CACF,CAAC,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,OAA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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,6BAA6B,CAAC,CAAC;QAC5D,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), \"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), \"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,4DAAiF;AAMjF,6BAAwB;AAExB,2DAA6D;AAC7D,yCAA4D;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,OAAC,CAAC,MAAM,CAAC;IAC7B,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,+BA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,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"]}
|
|
@@ -20,9 +20,9 @@ function populateDefaults(json, definitionMap) {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
|
-
assert(typeof json[typeField] === "string",
|
|
23
|
+
assert(typeof json[typeField] === "string", 0xa73 /* The typeField must be present in new JSON content */);
|
|
24
24
|
const nodeSchema = definitionMap.get(json[typeField]);
|
|
25
|
-
assert(nodeSchema?.kind === NodeKind.Object,
|
|
25
|
+
assert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -143,8 +143,8 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
|
|
|
143
143
|
if (Array.isArray(modification)) {
|
|
144
144
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
145
145
|
const field = node[treeEdit.field];
|
|
146
|
-
assert(Array.isArray(field),
|
|
147
|
-
assert(Array.isArray(constructedModification),
|
|
146
|
+
assert(Array.isArray(field), 0xa75 /* the field must be an array node */);
|
|
147
|
+
assert(Array.isArray(constructedModification), 0xa76 /* the modification must be an array node */);
|
|
148
148
|
field.removeRange(0);
|
|
149
149
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
150
150
|
node[treeEdit.field] = constructedModification;
|
|
@@ -203,7 +203,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
|
|
|
203
203
|
...normalizeAllowedTypes(destinationArraySchema.info),
|
|
204
204
|
];
|
|
205
205
|
const nodeToMove = sourceArrayNode.at(sourceIndex);
|
|
206
|
-
assert(nodeToMove !== undefined,
|
|
206
|
+
assert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);
|
|
207
207
|
if (isNodeAllowedType(nodeToMove, allowedTypes)) {
|
|
208
208
|
destinationArrayNode.moveRangeToIndex(destinationIndex, sourceIndex, sourceIndex + 1, sourceArrayNode);
|
|
209
209
|
}
|
|
@@ -219,7 +219,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
|
|
|
219
219
|
];
|
|
220
220
|
for (let i = sourceStartIndex; i < sourceEndIndex; i++) {
|
|
221
221
|
const nodeToMove = array.at(i);
|
|
222
|
-
assert(nodeToMove !== undefined,
|
|
222
|
+
assert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);
|
|
223
223
|
if (!isNodeAllowedType(nodeToMove, allowedTypes)) {
|
|
224
224
|
throw new UsageError("Illegal node type in destination array");
|
|
225
225
|
}
|
|
@@ -300,7 +300,7 @@ function getPlaceInfo(place, idGenerator) {
|
|
|
300
300
|
*/
|
|
301
301
|
function getNodeFromTarget(target, idGenerator) {
|
|
302
302
|
const node = idGenerator.getNode(target.target);
|
|
303
|
-
assert(node !== undefined,
|
|
303
|
+
assert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);
|
|
304
304
|
return node;
|
|
305
305
|
}
|
|
306
306
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentEditReducer.js","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EACN,IAAI,EACJ,QAAQ,EAMR,SAAS,EACT,WAAW,EACX,qBAAqB,EAErB,aAAa,EACb,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,YAAY,GAEZ,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EASN,SAAS,GACT,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;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,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\t\"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, \"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), \"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\t\"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, \"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, \"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, \"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","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EACN,IAAI,EACJ,QAAQ,EAMR,SAAS,EACT,WAAW,EACX,qBAAqB,EAErB,aAAa,EACb,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,YAAY,GAEZ,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EASN,SAAS,GACT,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;IAEpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,OAA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AAC,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"]}
|
|
@@ -60,7 +60,7 @@ export class IdGenerator {
|
|
|
60
60
|
const lastSegment = segments[segments.length - 1] ?? oob();
|
|
61
61
|
const prefix = segments.length > 1 ? segments.slice(0, -1).join(".") : "";
|
|
62
62
|
// Check if the last segment already exists with a different prefix
|
|
63
|
-
assert(!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix,
|
|
63
|
+
assert(!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix, 0xa7a /* Different scopes not supported yet. */);
|
|
64
64
|
this.prefixMap.set(lastSegment, prefix);
|
|
65
65
|
const count = this.idCountMap.get(lastSegment) ?? 1;
|
|
66
66
|
this.idCountMap.set(lastSegment, count + 1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idGenerator.js","sourceRoot":"","sources":["../../src/explicit-strategy/idGenerator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,qCAAqC,CAAC;AAClE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AAQ/D;;;GAGG;AACH,MAAM,OAAO,WAAW;IAMvB;QALiB,eAAU,GAAG,IAAI,GAAG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AG,CAAC,CAAC,CAAC;QAE5C,OAAO,GAAG,WAAW,GAAG,KAAK,EAAE,CAAC;IACjC,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, oob } from \"@fluidframework/core-utils/internal\";\nimport { Tree, NodeKind } from \"@fluidframework/tree/internal\";\nimport type {\n\tTreeNode,\n\tImplicitFieldSchema,\n\tTreeArrayNode,\n\tTreeFieldFromImplicitField,\n} from \"@fluidframework/tree/internal\";\n\n/**\n * Given a tree, generates a set of LLM-friendly, unique IDs for each node in the tree.\n * @remarks The ability to uniquely and stably in the tree is important for the LLM and this library to create and distinguish between different types certain {@link TreeEdit}s.\n */\nexport class IdGenerator {\n\tprivate readonly idCountMap = new Map<string, number>();\n\tprivate readonly prefixMap = new Map<string, string>();\n\tprivate readonly nodeToIdMap = new Map<TreeNode, string>();\n\tprivate readonly idToNodeMap = new Map<string, TreeNode>();\n\n\tpublic constructor() {}\n\n\tpublic getOrCreateId(node: TreeNode): string {\n\t\tconst existingID = this.nodeToIdMap.get(node);\n\t\tif (existingID !== undefined) {\n\t\t\treturn existingID;\n\t\t}\n\n\t\tconst schema = Tree.schema(node).identifier;\n\t\tconst id = this.generateID(schema);\n\t\tthis.nodeToIdMap.set(node, id);\n\t\tthis.idToNodeMap.set(id, node);\n\n\t\treturn id;\n\t}\n\n\tpublic getNode(id: string): TreeNode | undefined {\n\t\treturn this.idToNodeMap.get(id);\n\t}\n\n\tpublic getId(node: TreeNode): string | undefined {\n\t\treturn this.nodeToIdMap.get(node);\n\t}\n\n\tpublic assignIds(node: TreeFieldFromImplicitField<ImplicitFieldSchema>): string | undefined {\n\t\tif (typeof node === \"object\" && node !== null) {\n\t\t\tconst schema = Tree.schema(node as unknown as TreeNode);\n\t\t\tif (schema.kind === NodeKind.Array) {\n\t\t\t\tfor (const element of node as unknown as TreeArrayNode) {\n\t\t\t\t\tthis.assignIds(element);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// TODO: SharedTree Team needs to either publish TreeNode as a class to use .instanceof() or a typeguard.\n\t\t\t\t// Uncomment this assertion back once we have a typeguard ready.\n\t\t\t\t// assert(isTreeNode(node), \"Non-TreeNode value in tree.\");\n\t\t\t\tconst objId = this.getOrCreateId(node as TreeNode);\n\t\t\t\tfor (const key of Object.keys(node)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument\n\t\t\t\t\tthis.assignIds((node as unknown as any)[key]);\n\t\t\t\t}\n\t\t\t\treturn objId;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate generateID(schema: string): string {\n\t\tconst segments = schema.split(\".\");\n\n\t\t// If there's no period, the schema itself is the last segment\n\t\tconst lastSegment = segments[segments.length - 1] ?? oob();\n\t\tconst prefix = segments.length > 1 ? segments.slice(0, -1).join(\".\") : \"\";\n\n\t\t// Check if the last segment already exists with a different prefix\n\t\tassert(\n\t\t\t!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix,\n\t\t\t0xa7a /* Different scopes not supported yet. */,\n\t\t);\n\n\t\tthis.prefixMap.set(lastSegment, prefix);\n\t\tconst count = this.idCountMap.get(lastSegment) ?? 1;\n\t\tthis.idCountMap.set(lastSegment, count + 1);\n\n\t\treturn `${lastSegment}${count}`;\n\t}\n}\n"]}
|
|
@@ -22,7 +22,7 @@ export function toDecoratedJson(idGenerator, root) {
|
|
|
22
22
|
const objId =
|
|
23
23
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
24
24
|
idGenerator.getId(value) ?? fail("ID of new node should have been assigned.");
|
|
25
|
-
assert(!Object.prototype.hasOwnProperty.call(value, objectIdKey),
|
|
25
|
+
assert(!Object.prototype.hasOwnProperty.call(value, objectIdKey), 0xa7b /* Collision of object id property. */);
|
|
26
26
|
return {
|
|
27
27
|
[objectIdKey]: objId,
|
|
28
28
|
...value,
|
|
@@ -191,7 +191,7 @@ function getDef(defs, ref) {
|
|
|
191
191
|
// strip the "#/$defs/" prefix
|
|
192
192
|
const strippedRef = ref.slice(8);
|
|
193
193
|
const nextDef = defs[strippedRef];
|
|
194
|
-
assert(nextDef !== undefined,
|
|
194
|
+
assert(nextDef !== undefined, 0xa7c /* Ref not found. */);
|
|
195
195
|
return nextDef;
|
|
196
196
|
}
|
|
197
197
|
/**
|
|
@@ -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,IAAI,EAAE,CAAC;YAC1E,yGAAyG;YACzG,gEAAgE;YAChE,2DAA2D;YAC3D,MAAM,KAAK;YACV,iEAAiE;YACjE,WAAW,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,IAAI,CA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AAI,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\t`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, \"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,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,KAA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C,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 { 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"]}
|
|
@@ -89,7 +89,7 @@ export function generateGenericEditTypes(schema, generateDomainTypes) {
|
|
|
89
89
|
}
|
|
90
90
|
default: {
|
|
91
91
|
const types = Array.from(allowedTypes, (name) => typeMap.get(name) ?? fail("Unknown type"));
|
|
92
|
-
assert(hasAtLeastTwo(types),
|
|
92
|
+
assert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);
|
|
93
93
|
return z.union(types);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
@@ -240,7 +240,7 @@ function getTypeForAllowedTypes(definitionMap, typeMap, insertSet, modifyFieldSe
|
|
|
240
240
|
return getOrCreateType(definitionMap, typeMap, insertSet, modifyFieldSet, modifyTypeSet, name);
|
|
241
241
|
}),
|
|
242
242
|
];
|
|
243
|
-
assert(hasAtLeastTwo(types),
|
|
243
|
+
assert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
|
|
244
244
|
return z.union(types);
|
|
245
245
|
}
|
|
246
246
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAMjF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IAC7B,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,WAAW,qCAAqC,CACtG;CACF,CAAC,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC;IAC7B,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,WAAW,iLAAiL,CACvM;IACF,KAAK,EAAE,CAAC;SACN,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;SACzB,QAAQ,CACR,qGAAqG,CACrG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,UAAU,GAAG,CAAC;KAClB,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,YAAY,CAAC,CAAC;IAC5B,QAAQ,EAAE,CAAC;SACT,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,WAA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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), \"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), \"Expected at least two types\");\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAMjF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IAC7B,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,WAAW,qCAAqC,CACtG;CACF,CAAC,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC;IAC7B,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;S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T,cAAc,EACd,aAAa,EACb,IAAI,CACJ,CAAC;YACH,CAAC,CAAC;SACF,CAAC;QACF,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACtE,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;SAAM,CAAC;QACP,OAAO,eAAe,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/ai-collab",
|
|
3
|
-
"version": "2.10.0
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "Experimental package to simplify integrating AI into Fluid-based applications",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -69,10 +69,10 @@
|
|
|
69
69
|
"temp-directory": "nyc/.nyc_output"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@fluidframework/core-utils": "2.10.0
|
|
73
|
-
"@fluidframework/runtime-utils": "2.10.0
|
|
74
|
-
"@fluidframework/telemetry-utils": "2.10.0
|
|
75
|
-
"@fluidframework/tree": "2.10.0
|
|
72
|
+
"@fluidframework/core-utils": "~2.10.0",
|
|
73
|
+
"@fluidframework/runtime-utils": "~2.10.0",
|
|
74
|
+
"@fluidframework/telemetry-utils": "~2.10.0",
|
|
75
|
+
"@fluidframework/tree": "~2.10.0",
|
|
76
76
|
"openai": "^4.67.3",
|
|
77
77
|
"typechat": "^0.1.1",
|
|
78
78
|
"zod": "^3.23.8"
|
|
@@ -80,14 +80,14 @@
|
|
|
80
80
|
"devDependencies": {
|
|
81
81
|
"@arethetypeswrong/cli": "^0.16.4",
|
|
82
82
|
"@biomejs/biome": "~1.9.3",
|
|
83
|
-
"@fluid-internal/mocha-test-setup": "2.10.0
|
|
83
|
+
"@fluid-internal/mocha-test-setup": "~2.10.0",
|
|
84
84
|
"@fluid-tools/build-cli": "^0.51.0",
|
|
85
85
|
"@fluidframework/build-common": "^2.0.3",
|
|
86
86
|
"@fluidframework/build-tools": "^0.51.0",
|
|
87
87
|
"@fluidframework/eslint-config-fluid": "^5.4.0",
|
|
88
|
-
"@fluidframework/id-compressor": "2.10.0
|
|
89
|
-
"@fluidframework/runtime-utils": "2.10.0
|
|
90
|
-
"@fluidframework/test-runtime-utils": "2.10.0
|
|
88
|
+
"@fluidframework/id-compressor": "~2.10.0",
|
|
89
|
+
"@fluidframework/runtime-utils": "~2.10.0",
|
|
90
|
+
"@fluidframework/test-runtime-utils": "~2.10.0",
|
|
91
91
|
"@microsoft/api-extractor": "7.47.8",
|
|
92
92
|
"@types/mocha": "^9.1.1",
|
|
93
93
|
"@types/node": "^18.19.0",
|
|
@@ -57,10 +57,10 @@ function populateDefaults(
|
|
|
57
57
|
} else {
|
|
58
58
|
assert(
|
|
59
59
|
typeof json[typeField] === "string",
|
|
60
|
-
|
|
60
|
+
0xa73 /* The typeField must be present in new JSON content */,
|
|
61
61
|
);
|
|
62
62
|
const nodeSchema = definitionMap.get(json[typeField]);
|
|
63
|
-
assert(nodeSchema?.kind === NodeKind.Object,
|
|
63
|
+
assert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -200,10 +200,10 @@ export function applyAgentEdit(
|
|
|
200
200
|
if (Array.isArray(modification)) {
|
|
201
201
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
202
202
|
const field = (node as any)[treeEdit.field] as TreeArrayNode;
|
|
203
|
-
assert(Array.isArray(field),
|
|
203
|
+
assert(Array.isArray(field), 0xa75 /* the field must be an array node */);
|
|
204
204
|
assert(
|
|
205
205
|
Array.isArray(constructedModification),
|
|
206
|
-
|
|
206
|
+
0xa76 /* the modification must be an array node */,
|
|
207
207
|
);
|
|
208
208
|
field.removeRange(0);
|
|
209
209
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
@@ -266,7 +266,7 @@ export function applyAgentEdit(
|
|
|
266
266
|
...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),
|
|
267
267
|
];
|
|
268
268
|
const nodeToMove = sourceArrayNode.at(sourceIndex);
|
|
269
|
-
assert(nodeToMove !== undefined,
|
|
269
|
+
assert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);
|
|
270
270
|
if (isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {
|
|
271
271
|
destinationArrayNode.moveRangeToIndex(
|
|
272
272
|
destinationIndex,
|
|
@@ -289,7 +289,7 @@ export function applyAgentEdit(
|
|
|
289
289
|
];
|
|
290
290
|
for (let i = sourceStartIndex; i < sourceEndIndex; i++) {
|
|
291
291
|
const nodeToMove = array.at(i);
|
|
292
|
-
assert(nodeToMove !== undefined,
|
|
292
|
+
assert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);
|
|
293
293
|
if (!isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {
|
|
294
294
|
throw new UsageError("Illegal node type in destination array");
|
|
295
295
|
}
|
|
@@ -399,7 +399,7 @@ function getPlaceInfo(
|
|
|
399
399
|
*/
|
|
400
400
|
function getNodeFromTarget(target: ObjectTarget, idGenerator: IdGenerator): TreeNode {
|
|
401
401
|
const node = idGenerator.getNode(target.target);
|
|
402
|
-
assert(node !== undefined,
|
|
402
|
+
assert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);
|
|
403
403
|
return node;
|
|
404
404
|
}
|
|
405
405
|
|
|
@@ -78,7 +78,7 @@ export class IdGenerator {
|
|
|
78
78
|
// Check if the last segment already exists with a different prefix
|
|
79
79
|
assert(
|
|
80
80
|
!this.prefixMap.has(lastSegment) || this.prefixMap.get(lastSegment) === prefix,
|
|
81
|
-
|
|
81
|
+
0xa7a /* Different scopes not supported yet. */,
|
|
82
82
|
);
|
|
83
83
|
|
|
84
84
|
this.prefixMap.set(lastSegment, prefix);
|
|
@@ -52,7 +52,7 @@ export function toDecoratedJson(
|
|
|
52
52
|
idGenerator.getId(value) ?? fail("ID of new node should have been assigned.");
|
|
53
53
|
assert(
|
|
54
54
|
!Object.prototype.hasOwnProperty.call(value, objectIdKey),
|
|
55
|
-
|
|
55
|
+
0xa7b /* Collision of object id property. */,
|
|
56
56
|
);
|
|
57
57
|
return {
|
|
58
58
|
[objectIdKey]: objId,
|
|
@@ -277,7 +277,7 @@ function getDef(defs: Record<string, JsonNodeSchema>, ref: string): JsonNodeSche
|
|
|
277
277
|
// strip the "#/$defs/" prefix
|
|
278
278
|
const strippedRef = ref.slice(8);
|
|
279
279
|
const nextDef = defs[strippedRef];
|
|
280
|
-
assert(nextDef !== undefined,
|
|
280
|
+
assert(nextDef !== undefined, 0xa7c /* Ref not found. */);
|
|
281
281
|
return nextDef;
|
|
282
282
|
}
|
|
283
283
|
|
|
@@ -127,7 +127,7 @@ export function generateGenericEditTypes(
|
|
|
127
127
|
allowedTypes,
|
|
128
128
|
(name) => typeMap.get(name) ?? fail("Unknown type"),
|
|
129
129
|
);
|
|
130
|
-
assert(hasAtLeastTwo(types),
|
|
130
|
+
assert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);
|
|
131
131
|
return z.union(types);
|
|
132
132
|
}
|
|
133
133
|
}
|
|
@@ -349,7 +349,7 @@ function getTypeForAllowedTypes(
|
|
|
349
349
|
);
|
|
350
350
|
}),
|
|
351
351
|
];
|
|
352
|
-
assert(hasAtLeastTwo(types),
|
|
352
|
+
assert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
|
|
353
353
|
return z.union(types);
|
|
354
354
|
} else {
|
|
355
355
|
return getOrCreateType(
|