@fluidframework/ai-collab 2.52.0 → 2.53.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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # @fluidframework/ai-collab
2
2
 
3
+ ## 2.53.0
4
+
5
+ Dependency updates only.
6
+
3
7
  ## 2.52.0
4
8
 
5
9
  Dependency updates only.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/explicit-strategy/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,uEAAsE;AACtE,4DAKuC;AACvC,sDAAsD;AACtD,4CAAuD;AAKvD,+BAAoC;AACpC,6BAAwB;AAUxB,+DAAuD;AAEvD,qDAe0B;AAC1B,qDAA+C;AAC/C,+DAM+B;AAC/B,2DAA+D;AAC/D,yCAAkC;AA8DlC;;;;;;;;;GASG;AACI,KAAK,UAAU,iBAAiB,CACtC,OAAiC;IAEjC,MAAM,WAAW,GAAG,IAAI,4BAAW,EAAE,CAAC;IACtC,MAAM,OAAO,GAAY,EAAE,CAAC;IAC5B,MAAM,KAAK,GAAW,EAAE,CAAC;IAEzB,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,oBAAoB,GAAG,CAAC,CAAC;IAE7B,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,eAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEpE,MAAM,UAAU,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC;IAEvD,MAAM,eAAe,GAAG,IAAA,SAAM,GAAE,CAAC;IAEjC,MAAM,oBAAoB,GAAG,IAAA,SAAM,GAAE,CAAC;IACtC,OAAO,CAAC,oBAAoB,EAAE,CAAC;QAC9B,GAAG,IAAA,mCAAkB,EAAC,yBAAyB,EAAE,eAAe,CAAC;QACjE,aAAa,EAAE,oCAAmB,CA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gB;QAC/C,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,QAAQ;QACvC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,EAAE,GAAG,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI;YACnB,UAAU,EAAE;gBACX,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,aAAa;gBACxC,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,iBAAiB;aAChD;SACD,CAAC;KAC6B,CAAC,CAAC;IAElC,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC5D,UAAU,CAAC,WAAW,IAAI,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;QACtD,UAAU,CAAC,YAAY,IAAI,MAAM,CAAC,KAAK,EAAE,iBAAiB,CAAC;IAC5D,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;AACxD,CAAC;AAED,MAAM,uBAAwB,SAAQ,KAAK;CAAG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tgetSimpleSchema,\n\tTree,\n\ttype SimpleTreeSchema,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport type {\n\tChatCompletionCreateParams,\n\t// eslint-disable-next-line import/no-internal-modules\n} from \"openai/resources/index.mjs\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { z } from \"zod\";\n\nimport type {\n\tDebugEventLogHandler,\n\tOpenAiClientOptions,\n\tTokenLimits,\n\tTokenUsage,\n} from \"../aiCollabApi.js\";\nimport type { Diff } from \"../diffTypes.js\";\n\nimport { applyAgentEdit } from \"./agentEditReducer.js\";\nimport type { EditWrapper, TreeEdit } from \"./agentEditTypes.js\";\nimport {\n\ttype ApplyEditFailure,\n\ttype ApplyEditSuccess,\n\ttype GenerateTreeEditCompleted,\n\ttype GenerateTreeEditStarted,\n\ttype FinalReviewCompleted,\n\ttype FinalReviewStarted,\n\ttype LlmApiCallDebugEvent,\n\ttype PlanningPromptCompleted,\n\ttype CoreEventLoopStarted,\n\ttype CoreEventLoopCompleted,\n\tgenerateDebugEvent,\n\ttype PlanningPromptStarted,\n\ttype EventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\nimport { IdGenerator } from \"./idGenerator.js\";\nimport {\n\tgetEditingSystemPrompt,\n\tgetPlanningSystemPrompt,\n\tgetReviewSystemPrompt,\n\ttoDecoratedJson,\n\ttype EditLog,\n} from \"./promptGeneration.js\";\nimport { generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n// TODO: Create a proper index file and move the logic of this file to a new location\nexport type {\n\tApplyEditFailure,\n\tApplyEditSuccess,\n\tCoreEventLoopCompleted,\n\tCoreEventLoopStarted,\n\tFinalReviewCompleted,\n\tFinalReviewStarted,\n\tGenerateTreeEditCompleted,\n\tGenerateTreeEditStarted,\n\tLlmApiCallDebugEvent,\n\tPlanningPromptCompleted,\n\tPlanningPromptStarted,\n\tLlmTreeEdit,\n\tEventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\n\n/**\n * {@link generateTreeEdits} options.\n *\n * @internal\n */\nexport interface GenerateTreeEditsOptions {\n\topenAI: OpenAiClientOptions;\n\ttreeNode: TreeNode;\n\tprompt: {\n\t\tsystemRoleContext: string;\n\t\tuserAsk: string;\n\t};\n\tlimiters?: {\n\t\tabortController?: AbortController;\n\t\tmaxSequentialErrors?: number;\n\t\tmaxModelCalls?: number;\n\t\ttokenLimits?: TokenLimits;\n\t};\n\tfinalReviewStep?: boolean;\n\tvalidator?: (newContent: TreeNode) => void;\n\tdebugEventLogHandler?: DebugEventLogHandler;\n\tplanningStep?: boolean;\n}\n\ninterface GenerateTreeEditsSuccessResponse {\n\tstatus: \"success\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\ninterface GenerateTreeEditsErrorResponse {\n\tstatus: \"failure\" | \"partial-failure\";\n\terrorMessage:\n\t\t| \"tokenLimitExceeded\"\n\t\t| \"tooManyErrors\"\n\t\t| \"tooManyModelCalls\"\n\t\t| \"aborted\"\n\t\t| \"unexpectedError\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\n/**\n * Prompts the provided LLM client to generate valid tree edits.\n * Applies those edits to the provided tree branch before returning.\n *\n * @remarks\n * - Optional root nodes are not supported\n * - Primitive root nodes are not supported\n *\n * @internal\n */\nexport async function generateTreeEdits(\n\toptions: GenerateTreeEditsOptions,\n): Promise<GenerateTreeEditsSuccessResponse | GenerateTreeEditsErrorResponse> {\n\tconst idGenerator = new IdGenerator();\n\tconst editLog: EditLog = [];\n\tconst diffs: Diff[] = [];\n\n\tlet editCount = 0;\n\tlet sequentialErrorCount = 0;\n\n\tconst simpleSchema = getSimpleSchema(Tree.schema(options.treeNode));\n\n\tconst tokensUsed = { inputTokens: 0, outputTokens: 0 };\n\n\tconst debugLogTraceId = uuidv4();\n\n\tconst coreEventFlowTraceId = uuidv4();\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_STARTED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"STARTED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t} satisfies CoreEventLoopStarted);\n\n\ttry {\n\t\tfor await (const generateEditResult of generateEdits(\n\t\t\toptions,\n\t\t\tsimpleSchema,\n\t\t\tidGenerator,\n\t\t\teditLog,\n\t\t\toptions.limiters?.tokenLimits,\n\t\t\ttokensUsed,\n\t\t\toptions.debugEventLogHandler && {\n\t\t\t\teventLogHandler: options.debugEventLogHandler,\n\t\t\t\ttraceId: debugLogTraceId,\n\t\t\t},\n\t\t)) {\n\t\t\ttry {\n\t\t\t\tconst result = applyAgentEdit(\n\t\t\t\t\tgenerateEditResult.edit,\n\t\t\t\t\tidGenerator,\n\t\t\t\t\tsimpleSchema.definitions,\n\t\t\t\t\toptions.validator,\n\t\t\t\t);\n\t\t\t\teditLog.push({ edit: { ...result.edit } });\n\t\t\t\tdiffs.push(result.diff);\n\t\t\t\tsequentialErrorCount = 0;\n\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_SUCCESS\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t} satisfies ApplyEditSuccess);\n\t\t\t} catch (error: unknown) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_FAILURE\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t\terrorMessage: (error as Error)?.message,\n\t\t\t\t\tsequentialErrorCount,\n\t\t\t\t} satisfies ApplyEditFailure);\n\n\t\t\t\tif (error instanceof UsageError) {\n\t\t\t\t\tsequentialErrorCount += 1;\n\t\t\t\t\teditLog.push({ edit: generateEditResult.edit, error: error.message });\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet shouldExitEarly = false;\n\t\t\tconst completionResponse: GenerateTreeEditsErrorResponse = {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"unexpectedError\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\n\t\t\tif (options.limiters?.abortController?.signal.aborted === true) {\n\t\t\t\tcompletionResponse.errorMessage = \"aborted\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\tsequentialErrorCount >\n\t\t\t\t(options.limiters?.maxSequentialErrors ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyErrors\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\t++editCount >= (options.limiters?.maxModelCalls ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyModelCalls\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t}\n\n\t\t\tif (shouldExitEarly) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\t\t\tstatus: \"failure\",\n\t\t\t\t\tfailureReason: completionResponse.errorMessage,\n\t\t\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\t\t} satisfies CoreEventLoopCompleted);\n\n\t\t\t\treturn completionResponse;\n\t\t\t}\n\t\t}\n\t} catch (error: unknown) {\n\t\toptions.debugEventLogHandler?.({\n\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\tstatus: \"failure\",\n\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\tfailureReason:\n\t\t\t\terror instanceof TokenLimitExceededError ? \"tokenLimitExceeded\" : \"unexpectedError\",\n\t\t\terrorMessage: (error as Error)?.message,\n\t\t} satisfies CoreEventLoopCompleted);\n\n\t\tif (error instanceof TokenLimitExceededError) {\n\t\t\treturn {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"tokenLimitExceeded\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\t\t}\n\t\tthrow error;\n\t}\n\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"COMPLETED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\tstatus: \"success\",\n\t} satisfies CoreEventLoopCompleted);\n\n\treturn {\n\t\tstatus: \"success\",\n\t\ttokensUsed,\n\t\tdiffs,\n\t};\n}\n\ninterface ReviewResult {\n\tgoalAccomplished: \"yes\" | \"no\";\n}\n\n/**\n * Generates a single {@link TreeEdit} from an LLM.\n *\n * @remarks\n * The design of this async generator function is such that which each iteration of this functions values,\n * an LLM will be prompted to generate the next value (a {@link TreeEdit}) based on the users ask.\n * Once the LLM believes it has completed the user's ask, it will no longer return an edit and as a result\n * this generator will no longer yield a next value.\n */\nasync function* generateEdits(\n\toptions: GenerateTreeEditsOptions,\n\tsimpleSchema: SimpleTreeSchema,\n\tidGenerator: IdGenerator,\n\teditLog: EditLog,\n\ttokenLimits: TokenLimits | undefined,\n\ttokensUsed: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t},\n): AsyncGenerator<{ edit: TreeEdit; eventFlowTraceId: string }> {\n\tconst [types, rootTypeName] = generateGenericEditTypes(simpleSchema, true);\n\n\tlet plan: string | undefined;\n\tif (options.planningStep !== undefined) {\n\t\tconst planningPrompt = getPlanningSystemPrompt(\n\t\t\toptions.treeNode,\n\t\t\toptions.prompt.userAsk,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst generatePlanningPromptEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t} satisfies PlanningPromptStarted);\n\n\t\tplan = await getStringFromLlm(\n\t\t\tplanningPrompt,\n\t\t\toptions.openAI,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\tisLlmResponseValid: plan !== undefined,\n\t\t\tllmGeneratedPlan: plan,\n\t\t} satisfies PlanningPromptCompleted);\n\t}\n\n\tconst originalDecoratedJson =\n\t\t(options.finalReviewStep ?? false)\n\t\t\t? toDecoratedJson(idGenerator, options.treeNode)\n\t\t\t: undefined;\n\t// reviewed is implicitly true if finalReviewStep is false\n\tlet hasReviewed = (options.finalReviewStep ?? false) ? false : true;\n\tasync function getNextEdit(): Promise<\n\t\t{ edit: TreeEdit; eventFlowTraceId: string } | undefined\n\t> {\n\t\tconst systemPrompt = getEditingSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\teditLog,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t\tplan,\n\t\t);\n\n\t\tconst schema = types[rootTypeName] ?? fail(\"Root type not found.\");\n\n\t\tconst generateTreeEditEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies GenerateTreeEditStarted);\n\n\t\tconst wrapper = await getStructuredOutputFromLlm<EditWrapper>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\t\"A JSON object that represents an edit to a JSON tree.\",\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tisLlmResponseValid: wrapper?.edit !== undefined,\n\t\t\tllmGeneratedEdit: wrapper?.edit as Record<string, unknown> | null,\n\t\t} satisfies GenerateTreeEditCompleted);\n\n\t\tif (wrapper === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (wrapper.edit === null) {\n\t\t\tif ((options.finalReviewStep ?? false) && !hasReviewed) {\n\t\t\t\tconst reviewResult = await reviewGoal();\n\t\t\t\tif (reviewResult === undefined) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\thasReviewed = true;\n\t\t\t\tif (reviewResult.goalAccomplished === \"yes\") {\n\t\t\t\t\treturn undefined;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\t\teditLog.length = 0;\n\t\t\t\t\treturn getNextEdit();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\treturn { edit: wrapper.edit, eventFlowTraceId: generateTreeEditEventFlowId };\n\t\t}\n\t}\n\n\tasync function reviewGoal(): Promise<ReviewResult | undefined> {\n\t\tconst systemPrompt = getReviewSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\toriginalDecoratedJson ?? fail(\"Original decorated tree not provided.\"),\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst schema = z.object({\n\t\t\tgoalAccomplished: z\n\t\t\t\t.enum([\"yes\", \"no\"])\n\t\t\t\t.describe('Whether the user\\'s goal was met in the \"after\" tree.'),\n\t\t});\n\n\t\tconst finalReviewEventFlowTraceId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies FinalReviewStarted);\n\n\t\t// TODO: In the future, when using structured output isn't guarenteed, we will\n\t\t// need to add a custom type guard to ensure that output is in the right shape.\n\t\tconst output = await getStructuredOutputFromLlm<ReviewResult>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\tundefined,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tisLlmResponseValid: output !== undefined,\n\t\t\tdidLlmAccomplishGoal: output?.goalAccomplished,\n\t\t} satisfies FinalReviewCompleted);\n\n\t\treturn output;\n\t}\n\n\tlet edit = await getNextEdit();\n\twhile (edit !== undefined) {\n\t\tyield edit;\n\t\tif (tokensUsed.inputTokens > (tokenLimits?.inputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Input token limit exceeded.\");\n\t\t}\n\t\tif (tokensUsed.outputTokens > (tokenLimits?.outputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Output token limit exceeded.\");\n\t\t}\n\t\tedit = await getNextEdit();\n\t}\n}\n\n/**\n * Calls the LLM to generate a structured output response based on the provided prompt.\n */\nasync function getStructuredOutputFromLlm<T>(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\tstructuredOutputSchema: Zod.ZodTypeAny,\n\tdescription?: string,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<T | undefined> {\n\tconst response_format = zodResponseFormat(structuredOutputSchema, \"SharedTreeAI\", {\n\t\tdescription,\n\t});\n\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t\tresponse_format,\n\t};\n\n\tconst result = await openAi.client.beta.chat.completions.parse(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\t// TODO: fix types so this isn't null and doesn't need a cast\n\t// The type should be derived from the zod schema\n\t// TODO: Determine why this value would be undefined.\n\treturn result.choices[0]?.message.parsed as T | undefined;\n}\n\n/**\n * Calls the LLM to generate a response based on the provided prompt.\n */\nasync function getStringFromLlm(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<string | undefined> {\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t};\n\n\tconst result = await openAi.client.chat.completions.create(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\treturn result.choices[0]?.message.content ?? undefined;\n}\n\nclass TokenLimitExceededError extends Error {}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/explicit-strategy/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,uEAAsE;AACtE,4DAKuC;AACvC,sDAAsD;AACtD,4CAAuD;AAKvD,+BAAoC;AACpC,6BAAyC;AAUzC,+DAAuD;AAEvD,qDAe0B;AAC1B,qDAA+C;AAC/C,+DAM+B;AAC/B,2DAA+D;AAC/D,yCAAkC;AA8DlC;;;;;;;;;GASG;AACI,KAAK,UAAU,iBAAiB,CACtC,OAAiC;IAEjC,MAAM,WAAW,GAAG,IAAI,4BAAW,EAAE,CAAC;IACtC,MAAM,OAAO,GAAY,EAAE,CAAC;IAC5B,MAAM,KAAK,GAAW,EAAE,CAAC;IAEzB,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,oBAAoB,GAAG,CAAC,CAAC;IAE7B,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,eAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEpE,MAAM,UAAU,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,YAA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,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAEjE,YAAY,EAAE,eAAe,EAAE,CAAC;QAC/B,GAAG,IAAA,mCAAkB,EAAC,cAAc,EAAE,YAAY,CAAC,OAAO,CAAC;QAC3D,uBAAuB,EAAE,YAAY,CAAC,uBAAuB;QAC7D,gBAAgB,EAAE,YAAY,CAAC,gBAAgB;QAC/C,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,QAAQ;QACvC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,EAAE,GAAG,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI;YACnB,UAAU,EAAE;gBACX,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,aAAa;gBACxC,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,iBAAiB;aAChD;SACD,CAAC;KAC6B,CAAC,CAAC;IAElC,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC5D,UAAU,CAAC,WAAW,IAAI,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;QACtD,UAAU,CAAC,YAAY,IAAI,MAAM,CAAC,KAAK,EAAE,iBAAiB,CAAC;IAC5D,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;AACxD,CAAC;AAED,MAAM,uBAAwB,SAAQ,KAAK;CAAG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tgetSimpleSchema,\n\tTree,\n\ttype SimpleTreeSchema,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport type {\n\tChatCompletionCreateParams,\n\t// eslint-disable-next-line import/no-internal-modules\n} from \"openai/resources/index.mjs\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type {\n\tDebugEventLogHandler,\n\tOpenAiClientOptions,\n\tTokenLimits,\n\tTokenUsage,\n} from \"../aiCollabApi.js\";\nimport type { Diff } from \"../diffTypes.js\";\n\nimport { applyAgentEdit } from \"./agentEditReducer.js\";\nimport type { EditWrapper, TreeEdit } from \"./agentEditTypes.js\";\nimport {\n\ttype ApplyEditFailure,\n\ttype ApplyEditSuccess,\n\ttype GenerateTreeEditCompleted,\n\ttype GenerateTreeEditStarted,\n\ttype FinalReviewCompleted,\n\ttype FinalReviewStarted,\n\ttype LlmApiCallDebugEvent,\n\ttype PlanningPromptCompleted,\n\ttype CoreEventLoopStarted,\n\ttype CoreEventLoopCompleted,\n\tgenerateDebugEvent,\n\ttype PlanningPromptStarted,\n\ttype EventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\nimport { IdGenerator } from \"./idGenerator.js\";\nimport {\n\tgetEditingSystemPrompt,\n\tgetPlanningSystemPrompt,\n\tgetReviewSystemPrompt,\n\ttoDecoratedJson,\n\ttype EditLog,\n} from \"./promptGeneration.js\";\nimport { generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n// TODO: Create a proper index file and move the logic of this file to a new location\nexport type {\n\tApplyEditFailure,\n\tApplyEditSuccess,\n\tCoreEventLoopCompleted,\n\tCoreEventLoopStarted,\n\tFinalReviewCompleted,\n\tFinalReviewStarted,\n\tGenerateTreeEditCompleted,\n\tGenerateTreeEditStarted,\n\tLlmApiCallDebugEvent,\n\tPlanningPromptCompleted,\n\tPlanningPromptStarted,\n\tLlmTreeEdit,\n\tEventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\n\n/**\n * {@link generateTreeEdits} options.\n *\n * @internal\n */\nexport interface GenerateTreeEditsOptions {\n\topenAI: OpenAiClientOptions;\n\ttreeNode: TreeNode;\n\tprompt: {\n\t\tsystemRoleContext: string;\n\t\tuserAsk: string;\n\t};\n\tlimiters?: {\n\t\tabortController?: AbortController;\n\t\tmaxSequentialErrors?: number;\n\t\tmaxModelCalls?: number;\n\t\ttokenLimits?: TokenLimits;\n\t};\n\tfinalReviewStep?: boolean;\n\tvalidator?: (newContent: TreeNode) => void;\n\tdebugEventLogHandler?: DebugEventLogHandler;\n\tplanningStep?: boolean;\n}\n\ninterface GenerateTreeEditsSuccessResponse {\n\tstatus: \"success\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\ninterface GenerateTreeEditsErrorResponse {\n\tstatus: \"failure\" | \"partial-failure\";\n\terrorMessage:\n\t\t| \"tokenLimitExceeded\"\n\t\t| \"tooManyErrors\"\n\t\t| \"tooManyModelCalls\"\n\t\t| \"aborted\"\n\t\t| \"unexpectedError\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\n/**\n * Prompts the provided LLM client to generate valid tree edits.\n * Applies those edits to the provided tree branch before returning.\n *\n * @remarks\n * - Optional root nodes are not supported\n * - Primitive root nodes are not supported\n *\n * @internal\n */\nexport async function generateTreeEdits(\n\toptions: GenerateTreeEditsOptions,\n): Promise<GenerateTreeEditsSuccessResponse | GenerateTreeEditsErrorResponse> {\n\tconst idGenerator = new IdGenerator();\n\tconst editLog: EditLog = [];\n\tconst diffs: Diff[] = [];\n\n\tlet editCount = 0;\n\tlet sequentialErrorCount = 0;\n\n\tconst simpleSchema = getSimpleSchema(Tree.schema(options.treeNode));\n\n\tconst tokensUsed = { inputTokens: 0, outputTokens: 0 };\n\n\tconst debugLogTraceId = uuidv4();\n\n\tconst coreEventFlowTraceId = uuidv4();\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_STARTED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"STARTED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t} satisfies CoreEventLoopStarted);\n\n\ttry {\n\t\tfor await (const generateEditResult of generateEdits(\n\t\t\toptions,\n\t\t\tsimpleSchema,\n\t\t\tidGenerator,\n\t\t\teditLog,\n\t\t\toptions.limiters?.tokenLimits,\n\t\t\ttokensUsed,\n\t\t\toptions.debugEventLogHandler && {\n\t\t\t\teventLogHandler: options.debugEventLogHandler,\n\t\t\t\ttraceId: debugLogTraceId,\n\t\t\t},\n\t\t)) {\n\t\t\ttry {\n\t\t\t\tconst result = applyAgentEdit(\n\t\t\t\t\tgenerateEditResult.edit,\n\t\t\t\t\tidGenerator,\n\t\t\t\t\tsimpleSchema.definitions,\n\t\t\t\t\toptions.validator,\n\t\t\t\t);\n\t\t\t\teditLog.push({ edit: { ...result.edit } });\n\t\t\t\tdiffs.push(result.diff);\n\t\t\t\tsequentialErrorCount = 0;\n\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_SUCCESS\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t} satisfies ApplyEditSuccess);\n\t\t\t} catch (error: unknown) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_FAILURE\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t\terrorMessage: (error as Error)?.message,\n\t\t\t\t\tsequentialErrorCount,\n\t\t\t\t} satisfies ApplyEditFailure);\n\n\t\t\t\tif (error instanceof UsageError) {\n\t\t\t\t\tsequentialErrorCount += 1;\n\t\t\t\t\teditLog.push({ edit: generateEditResult.edit, error: error.message });\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet shouldExitEarly = false;\n\t\t\tconst completionResponse: GenerateTreeEditsErrorResponse = {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"unexpectedError\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\n\t\t\tif (options.limiters?.abortController?.signal.aborted === true) {\n\t\t\t\tcompletionResponse.errorMessage = \"aborted\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\tsequentialErrorCount >\n\t\t\t\t(options.limiters?.maxSequentialErrors ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyErrors\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\t++editCount >= (options.limiters?.maxModelCalls ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyModelCalls\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t}\n\n\t\t\tif (shouldExitEarly) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\t\t\tstatus: \"failure\",\n\t\t\t\t\tfailureReason: completionResponse.errorMessage,\n\t\t\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\t\t} satisfies CoreEventLoopCompleted);\n\n\t\t\t\treturn completionResponse;\n\t\t\t}\n\t\t}\n\t} catch (error: unknown) {\n\t\toptions.debugEventLogHandler?.({\n\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\tstatus: \"failure\",\n\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\tfailureReason:\n\t\t\t\terror instanceof TokenLimitExceededError ? \"tokenLimitExceeded\" : \"unexpectedError\",\n\t\t\terrorMessage: (error as Error)?.message,\n\t\t} satisfies CoreEventLoopCompleted);\n\n\t\tif (error instanceof TokenLimitExceededError) {\n\t\t\treturn {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"tokenLimitExceeded\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\t\t}\n\t\tthrow error;\n\t}\n\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"COMPLETED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\tstatus: \"success\",\n\t} satisfies CoreEventLoopCompleted);\n\n\treturn {\n\t\tstatus: \"success\",\n\t\ttokensUsed,\n\t\tdiffs,\n\t};\n}\n\ninterface ReviewResult {\n\tgoalAccomplished: \"yes\" | \"no\";\n}\n\n/**\n * Generates a single {@link TreeEdit} from an LLM.\n *\n * @remarks\n * The design of this async generator function is such that which each iteration of this functions values,\n * an LLM will be prompted to generate the next value (a {@link TreeEdit}) based on the users ask.\n * Once the LLM believes it has completed the user's ask, it will no longer return an edit and as a result\n * this generator will no longer yield a next value.\n */\nasync function* generateEdits(\n\toptions: GenerateTreeEditsOptions,\n\tsimpleSchema: SimpleTreeSchema,\n\tidGenerator: IdGenerator,\n\teditLog: EditLog,\n\ttokenLimits: TokenLimits | undefined,\n\ttokensUsed: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t},\n): AsyncGenerator<{ edit: TreeEdit; eventFlowTraceId: string }> {\n\tconst [types, rootTypeName] = generateGenericEditTypes(simpleSchema, true);\n\n\tlet plan: string | undefined;\n\tif (options.planningStep !== undefined) {\n\t\tconst planningPrompt = getPlanningSystemPrompt(\n\t\t\toptions.treeNode,\n\t\t\toptions.prompt.userAsk,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst generatePlanningPromptEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t} satisfies PlanningPromptStarted);\n\n\t\tplan = await getStringFromLlm(\n\t\t\tplanningPrompt,\n\t\t\toptions.openAI,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\tisLlmResponseValid: plan !== undefined,\n\t\t\tllmGeneratedPlan: plan,\n\t\t} satisfies PlanningPromptCompleted);\n\t}\n\n\tconst originalDecoratedJson =\n\t\t(options.finalReviewStep ?? false)\n\t\t\t? toDecoratedJson(idGenerator, options.treeNode)\n\t\t\t: undefined;\n\t// reviewed is implicitly true if finalReviewStep is false\n\tlet hasReviewed = (options.finalReviewStep ?? false) ? false : true;\n\tasync function getNextEdit(): Promise<\n\t\t{ edit: TreeEdit; eventFlowTraceId: string } | undefined\n\t> {\n\t\tconst systemPrompt = getEditingSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\teditLog,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t\tplan,\n\t\t);\n\n\t\tconst schema = types[rootTypeName] ?? fail(\"Root type not found.\");\n\n\t\tconst generateTreeEditEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies GenerateTreeEditStarted);\n\n\t\tconst wrapper = await getStructuredOutputFromLlm<EditWrapper>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\t\"A JSON object that represents an edit to a JSON tree.\",\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tisLlmResponseValid: wrapper?.edit !== undefined,\n\t\t\tllmGeneratedEdit: wrapper?.edit as Record<string, unknown> | null,\n\t\t} satisfies GenerateTreeEditCompleted);\n\n\t\tif (wrapper === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (wrapper.edit === null) {\n\t\t\tif ((options.finalReviewStep ?? false) && !hasReviewed) {\n\t\t\t\tconst reviewResult = await reviewGoal();\n\t\t\t\tif (reviewResult === undefined) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\thasReviewed = true;\n\t\t\t\tif (reviewResult.goalAccomplished === \"yes\") {\n\t\t\t\t\treturn undefined;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\t\teditLog.length = 0;\n\t\t\t\t\treturn getNextEdit();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\treturn { edit: wrapper.edit, eventFlowTraceId: generateTreeEditEventFlowId };\n\t\t}\n\t}\n\n\tasync function reviewGoal(): Promise<ReviewResult | undefined> {\n\t\tconst systemPrompt = getReviewSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\toriginalDecoratedJson ?? fail(\"Original decorated tree not provided.\"),\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst schema = z.object({\n\t\t\tgoalAccomplished: z\n\t\t\t\t.enum([\"yes\", \"no\"])\n\t\t\t\t.describe('Whether the user\\'s goal was met in the \"after\" tree.'),\n\t\t});\n\n\t\tconst finalReviewEventFlowTraceId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies FinalReviewStarted);\n\n\t\t// TODO: In the future, when using structured output isn't guarenteed, we will\n\t\t// need to add a custom type guard to ensure that output is in the right shape.\n\t\tconst output = await getStructuredOutputFromLlm<ReviewResult>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\tundefined,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tisLlmResponseValid: output !== undefined,\n\t\t\tdidLlmAccomplishGoal: output?.goalAccomplished,\n\t\t} satisfies FinalReviewCompleted);\n\n\t\treturn output;\n\t}\n\n\tlet edit = await getNextEdit();\n\twhile (edit !== undefined) {\n\t\tyield edit;\n\t\tif (tokensUsed.inputTokens > (tokenLimits?.inputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Input token limit exceeded.\");\n\t\t}\n\t\tif (tokensUsed.outputTokens > (tokenLimits?.outputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Output token limit exceeded.\");\n\t\t}\n\t\tedit = await getNextEdit();\n\t}\n}\n\n/**\n * Calls the LLM to generate a structured output response based on the provided prompt.\n */\nasync function getStructuredOutputFromLlm<T>(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\tstructuredOutputSchema: ZodTypeAny,\n\tdescription?: string,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<T | undefined> {\n\tconst response_format = zodResponseFormat(structuredOutputSchema, \"SharedTreeAI\", {\n\t\tdescription,\n\t});\n\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t\tresponse_format,\n\t};\n\n\tconst result = await openAi.client.beta.chat.completions.parse(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\t// TODO: fix types so this isn't null and doesn't need a cast\n\t// The type should be derived from the zod schema\n\t// TODO: Determine why this value would be undefined.\n\treturn result.choices[0]?.message.parsed as T | undefined;\n}\n\n/**\n * Calls the LLM to generate a response based on the provided prompt.\n */\nasync function getStringFromLlm(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<string | undefined> {\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t};\n\n\tconst result = await openAi.client.chat.completions.create(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\treturn result.choices[0]?.message.content ?? undefined;\n}\n\nclass TokenLimitExceededError extends Error {}\n"]}
@@ -3,6 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import type { SimpleTreeSchema, TreeNode } from "@fluidframework/tree/internal";
6
+ import { type ZodTypeAny } from "zod";
6
7
  /**
7
8
  * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}
8
9
  * 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.
@@ -11,7 +12,7 @@ import type { SimpleTreeSchema, TreeNode } from "@fluidframework/tree/internal";
11
12
  *
12
13
  * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.
13
14
  */
14
- export declare function generateGenericEditTypes(schema: SimpleTreeSchema, generateDomainTypes: boolean): [Record<string, Zod.ZodTypeAny>, root: string];
15
+ export declare function generateGenericEditTypes(schema: SimpleTreeSchema, generateDomainTypes: boolean): [Record<string, ZodTypeAny>, root: string];
15
16
  /**
16
17
  * Determines if the provided {@link TreeNode} contains an array schema.
17
18
  */
@@ -1 +1 @@
1
- {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAGX,gBAAgB,EAChB,QAAQ,EACR,MAAM,+BAA+B,CAAC;AA+EvC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,gBAAgB,EACxB,mBAAmB,EAAE,OAAO,GAC1B,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CA8GhD;AA6LD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAUlE"}
1
+ {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAGX,gBAAgB,EAChB,QAAQ,EACR,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAK,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AA8EzC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,gBAAgB,EACxB,mBAAmB,EAAE,OAAO,GAC1B,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CA8G5C;AA6LD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAUlE"}
@@ -252,7 +252,7 @@ function getTypeForAllowedTypes(definitionMap, typeMap, insertSet, modifyFieldSe
252
252
  return getOrCreateType(definitionMap, typeMap, insertSet, modifyFieldSet, modifyTypeSet, name);
253
253
  }),
254
254
  ];
255
- (0, internal_1.assert)(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
255
+ (0, internal_1.assert)(hasAtLeastTwo(types), 0xc1b /* Expected at least two types */);
256
256
  return zod_1.z.union(types);
257
257
  }
258
258
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,4DAMuC;AAOvC,6BAAwB;AAExB,2DAA6D;AAC7D,yCAA4D;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,OAAC;KACpB,MAAM,CAAC;IACP,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,+BAAW,qCAAqC,CACtG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,OAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC;IAC7B,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,+BAAW,iLAAiL,CACvM;IACF,KAAK,EAAE,OAAC;SACN,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;SACzB,QAAQ,CACR,qGAAqG,CACrG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,UAAU,GAAG,OAAC;KAClB,MAAM,CAAC;IACP,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,YAAY,CAAC,CAAC;IAC5B,QAAQ,EAAE,OAAC;SACT,MAAM,EAAE;SACR,QAAQ,CACR,WAAW,+BAAW,8IAA8I,CACpK;IACF,KAAK,EAAE,OAAC,CAAC,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P,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;AAED,SAAS,eAAe,CAAI,GAAmB;IAC9C,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,aAAa,CAAI,KAAU;IACnC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;GAEG;AACH,SAAgB,0BAA0B,CAAC,IAAc;IACxD,MAAM,MAAM,GAAG,eAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,MAAM,CAAC,CAAC;IAC7C,KAAK,MAAM,CAAC,EAAE,UAAU,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;QACvD,IAAI,UAAU,CAAC,IAAI,KAAK,mBAAQ,CAAC,KAAK,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAVD,gEAUC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport {\n\tFieldKind,\n\tgetSimpleSchema,\n\tNodeKind,\n\tTree,\n\tValueSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNode,\n} from \"@fluidframework/tree/internal\";\nimport { z } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z\n\t.object({\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a specific object node in the tree, identified by the target object's Id.\",\n\t);\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, Zod.ZodTypeAny>, root: string] {\n\treturn getOrCreate(cache, schema, () => {\n\t\tconst insertSet = new Set<string>();\n\t\tconst modifyFieldSet = new Set<string>();\n\t\tconst modifyTypeSet = new Set<string>();\n\t\tconst typeMap = new Map<string, Zod.ZodTypeAny>();\n\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): Zod.ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst doesSchemaHaveArray = insertSet.size > 0;\n\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\n\t\tconst typeRecord: Record<string, Zod.ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tModify: modify,\n\t\t};\n\n\t\ttypeRecord.Modify = modify;\n\n\t\tif (doesSchemaHaveArray) {\n\t\t\ttypeRecord.ObjectPlace = objectPlace;\n\t\t\ttypeRecord.ArrayPlace = arrayPlace;\n\t\t\ttypeRecord.Range = range;\n\t\t\ttypeRecord.Insert = insert;\n\t\t\ttypeRecord.Remove = remove;\n\t\t\ttypeRecord.Move = move;\n\t\t}\n\n\t\tconst editTypes = doesSchemaHaveArray\n\t\t\t? ([insert, remove, move, modify, z.null()] as const)\n\t\t\t: ([modify, z.null()] as const);\n\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\ttypeRecord.EditWrapper = editWrapper;\n\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): Zod.ZodTypeAny {\n\treturn getOrCreate(typeMap, definition, () => {\n\t\tconst nodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\t\tswitch (nodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\tfor (const [key, field] of 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers) {\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\t[...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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): Zod.ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tname,\n\t\t\t\t);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\n\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Determines if the provided {@link TreeNode} contains an array schema.\n */\nexport function doesNodeContainArraySchema(node: TreeNode): boolean {\n\tconst schema = Tree.schema(node);\n\tconst simpleSchema = getSimpleSchema(schema);\n\tfor (const [, nodeSchema] of simpleSchema.definitions) {\n\t\tif (nodeSchema.kind === NodeKind.Array) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"]}
1
+ {"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,4DAMuC;AAOvC,6BAAyC;AAEzC,2DAA6D;AAC7D,yCAA4D;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,OAAC;KACpB,MAAM,CAAC;IACP,MAAM,EAAE,OAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,+BAAW,qCAAqC,CACtG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,OAAC;KACnB,MAAM,CAAC;IACP,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CA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MAAM,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,MAAM,CAAC,CAAC;IAC7C,KAAK,MAAM,CAAC,EAAE,UAAU,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;QACvD,IAAI,UAAU,CAAC,IAAI,KAAK,mBAAQ,CAAC,KAAK,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAVD,gEAUC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport {\n\tFieldKind,\n\tgetSimpleSchema,\n\tNodeKind,\n\tTree,\n\tValueSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNode,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z\n\t.object({\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a specific object node in the tree, identified by the target object's Id.\",\n\t);\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, 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, ZodTypeAny>();\n\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst doesSchemaHaveArray = insertSet.size > 0;\n\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\n\t\tconst typeRecord: Record<string, ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tModify: modify,\n\t\t};\n\n\t\ttypeRecord.Modify = modify;\n\n\t\tif (doesSchemaHaveArray) {\n\t\t\ttypeRecord.ObjectPlace = objectPlace;\n\t\t\ttypeRecord.ArrayPlace = arrayPlace;\n\t\t\ttypeRecord.Range = range;\n\t\t\ttypeRecord.Insert = insert;\n\t\t\ttypeRecord.Remove = remove;\n\t\t\ttypeRecord.Move = move;\n\t\t}\n\n\t\tconst editTypes = doesSchemaHaveArray\n\t\t\t? ([insert, remove, move, modify, z.null()] as const)\n\t\t\t: ([modify, z.null()] as const);\n\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\ttypeRecord.EditWrapper = editWrapper;\n\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): 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 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers) {\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\t[...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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tfieldSchema: SimpleFieldSchema,\n): 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(\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), 0xc1b /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\n\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Determines if the provided {@link TreeNode} contains an array schema.\n */\nexport function doesNodeContainArraySchema(node: TreeNode): boolean {\n\tconst schema = Tree.schema(node);\n\tconst simpleSchema = getSimpleSchema(schema);\n\tfor (const [, nodeSchema] of simpleSchema.definitions) {\n\t\tif (nodeSchema.kind === NodeKind.Array) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"]}
@@ -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AE,YAAY,CAAC,gBAAgB;QAC/C,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,QAAQ;QACvC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,EAAE,GAAG,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI;YACnB,UAAU,EAAE;gBACX,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,aAAa;gBACxC,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,iBAAiB;aAChD;SACD,CAAC;KAC6B,CAAC,CAAC;IAElC,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC5D,UAAU,CAAC,WAAW,IAAI,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;QACtD,UAAU,CAAC,YAAY,IAAI,MAAM,CAAC,KAAK,EAAE,iBAAiB,CAAC;IAC5D,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;AACxD,CAAC;AAED,MAAM,uBAAwB,SAAQ,KAAK;CAAG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tgetSimpleSchema,\n\tTree,\n\ttype SimpleTreeSchema,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport type {\n\tChatCompletionCreateParams,\n\t// eslint-disable-next-line import/no-internal-modules\n} from \"openai/resources/index.mjs\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { z } from \"zod\";\n\nimport type {\n\tDebugEventLogHandler,\n\tOpenAiClientOptions,\n\tTokenLimits,\n\tTokenUsage,\n} from \"../aiCollabApi.js\";\nimport type { Diff } from \"../diffTypes.js\";\n\nimport { applyAgentEdit } from \"./agentEditReducer.js\";\nimport type { EditWrapper, TreeEdit } from \"./agentEditTypes.js\";\nimport {\n\ttype ApplyEditFailure,\n\ttype ApplyEditSuccess,\n\ttype GenerateTreeEditCompleted,\n\ttype GenerateTreeEditStarted,\n\ttype FinalReviewCompleted,\n\ttype FinalReviewStarted,\n\ttype LlmApiCallDebugEvent,\n\ttype PlanningPromptCompleted,\n\ttype CoreEventLoopStarted,\n\ttype CoreEventLoopCompleted,\n\tgenerateDebugEvent,\n\ttype PlanningPromptStarted,\n\ttype EventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\nimport { IdGenerator } from \"./idGenerator.js\";\nimport {\n\tgetEditingSystemPrompt,\n\tgetPlanningSystemPrompt,\n\tgetReviewSystemPrompt,\n\ttoDecoratedJson,\n\ttype EditLog,\n} from \"./promptGeneration.js\";\nimport { generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n// TODO: Create a proper index file and move the logic of this file to a new location\nexport type {\n\tApplyEditFailure,\n\tApplyEditSuccess,\n\tCoreEventLoopCompleted,\n\tCoreEventLoopStarted,\n\tFinalReviewCompleted,\n\tFinalReviewStarted,\n\tGenerateTreeEditCompleted,\n\tGenerateTreeEditStarted,\n\tLlmApiCallDebugEvent,\n\tPlanningPromptCompleted,\n\tPlanningPromptStarted,\n\tLlmTreeEdit,\n\tEventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\n\n/**\n * {@link generateTreeEdits} options.\n *\n * @internal\n */\nexport interface GenerateTreeEditsOptions {\n\topenAI: OpenAiClientOptions;\n\ttreeNode: TreeNode;\n\tprompt: {\n\t\tsystemRoleContext: string;\n\t\tuserAsk: string;\n\t};\n\tlimiters?: {\n\t\tabortController?: AbortController;\n\t\tmaxSequentialErrors?: number;\n\t\tmaxModelCalls?: number;\n\t\ttokenLimits?: TokenLimits;\n\t};\n\tfinalReviewStep?: boolean;\n\tvalidator?: (newContent: TreeNode) => void;\n\tdebugEventLogHandler?: DebugEventLogHandler;\n\tplanningStep?: boolean;\n}\n\ninterface GenerateTreeEditsSuccessResponse {\n\tstatus: \"success\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\ninterface GenerateTreeEditsErrorResponse {\n\tstatus: \"failure\" | \"partial-failure\";\n\terrorMessage:\n\t\t| \"tokenLimitExceeded\"\n\t\t| \"tooManyErrors\"\n\t\t| \"tooManyModelCalls\"\n\t\t| \"aborted\"\n\t\t| \"unexpectedError\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\n/**\n * Prompts the provided LLM client to generate valid tree edits.\n * Applies those edits to the provided tree branch before returning.\n *\n * @remarks\n * - Optional root nodes are not supported\n * - Primitive root nodes are not supported\n *\n * @internal\n */\nexport async function generateTreeEdits(\n\toptions: GenerateTreeEditsOptions,\n): Promise<GenerateTreeEditsSuccessResponse | GenerateTreeEditsErrorResponse> {\n\tconst idGenerator = new IdGenerator();\n\tconst editLog: EditLog = [];\n\tconst diffs: Diff[] = [];\n\n\tlet editCount = 0;\n\tlet sequentialErrorCount = 0;\n\n\tconst simpleSchema = getSimpleSchema(Tree.schema(options.treeNode));\n\n\tconst tokensUsed = { inputTokens: 0, outputTokens: 0 };\n\n\tconst debugLogTraceId = uuidv4();\n\n\tconst coreEventFlowTraceId = uuidv4();\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_STARTED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"STARTED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t} satisfies CoreEventLoopStarted);\n\n\ttry {\n\t\tfor await (const generateEditResult of generateEdits(\n\t\t\toptions,\n\t\t\tsimpleSchema,\n\t\t\tidGenerator,\n\t\t\teditLog,\n\t\t\toptions.limiters?.tokenLimits,\n\t\t\ttokensUsed,\n\t\t\toptions.debugEventLogHandler && {\n\t\t\t\teventLogHandler: options.debugEventLogHandler,\n\t\t\t\ttraceId: debugLogTraceId,\n\t\t\t},\n\t\t)) {\n\t\t\ttry {\n\t\t\t\tconst result = applyAgentEdit(\n\t\t\t\t\tgenerateEditResult.edit,\n\t\t\t\t\tidGenerator,\n\t\t\t\t\tsimpleSchema.definitions,\n\t\t\t\t\toptions.validator,\n\t\t\t\t);\n\t\t\t\teditLog.push({ edit: { ...result.edit } });\n\t\t\t\tdiffs.push(result.diff);\n\t\t\t\tsequentialErrorCount = 0;\n\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_SUCCESS\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t} satisfies ApplyEditSuccess);\n\t\t\t} catch (error: unknown) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_FAILURE\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t\terrorMessage: (error as Error)?.message,\n\t\t\t\t\tsequentialErrorCount,\n\t\t\t\t} satisfies ApplyEditFailure);\n\n\t\t\t\tif (error instanceof UsageError) {\n\t\t\t\t\tsequentialErrorCount += 1;\n\t\t\t\t\teditLog.push({ edit: generateEditResult.edit, error: error.message });\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet shouldExitEarly = false;\n\t\t\tconst completionResponse: GenerateTreeEditsErrorResponse = {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"unexpectedError\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\n\t\t\tif (options.limiters?.abortController?.signal.aborted === true) {\n\t\t\t\tcompletionResponse.errorMessage = \"aborted\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\tsequentialErrorCount >\n\t\t\t\t(options.limiters?.maxSequentialErrors ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyErrors\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\t++editCount >= (options.limiters?.maxModelCalls ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyModelCalls\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t}\n\n\t\t\tif (shouldExitEarly) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\t\t\tstatus: \"failure\",\n\t\t\t\t\tfailureReason: completionResponse.errorMessage,\n\t\t\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\t\t} satisfies CoreEventLoopCompleted);\n\n\t\t\t\treturn completionResponse;\n\t\t\t}\n\t\t}\n\t} catch (error: unknown) {\n\t\toptions.debugEventLogHandler?.({\n\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\tstatus: \"failure\",\n\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\tfailureReason:\n\t\t\t\terror instanceof TokenLimitExceededError ? \"tokenLimitExceeded\" : \"unexpectedError\",\n\t\t\terrorMessage: (error as Error)?.message,\n\t\t} satisfies CoreEventLoopCompleted);\n\n\t\tif (error instanceof TokenLimitExceededError) {\n\t\t\treturn {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"tokenLimitExceeded\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\t\t}\n\t\tthrow error;\n\t}\n\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"COMPLETED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\tstatus: \"success\",\n\t} satisfies CoreEventLoopCompleted);\n\n\treturn {\n\t\tstatus: \"success\",\n\t\ttokensUsed,\n\t\tdiffs,\n\t};\n}\n\ninterface ReviewResult {\n\tgoalAccomplished: \"yes\" | \"no\";\n}\n\n/**\n * Generates a single {@link TreeEdit} from an LLM.\n *\n * @remarks\n * The design of this async generator function is such that which each iteration of this functions values,\n * an LLM will be prompted to generate the next value (a {@link TreeEdit}) based on the users ask.\n * Once the LLM believes it has completed the user's ask, it will no longer return an edit and as a result\n * this generator will no longer yield a next value.\n */\nasync function* generateEdits(\n\toptions: GenerateTreeEditsOptions,\n\tsimpleSchema: SimpleTreeSchema,\n\tidGenerator: IdGenerator,\n\teditLog: EditLog,\n\ttokenLimits: TokenLimits | undefined,\n\ttokensUsed: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t},\n): AsyncGenerator<{ edit: TreeEdit; eventFlowTraceId: string }> {\n\tconst [types, rootTypeName] = generateGenericEditTypes(simpleSchema, true);\n\n\tlet plan: string | undefined;\n\tif (options.planningStep !== undefined) {\n\t\tconst planningPrompt = getPlanningSystemPrompt(\n\t\t\toptions.treeNode,\n\t\t\toptions.prompt.userAsk,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst generatePlanningPromptEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t} satisfies PlanningPromptStarted);\n\n\t\tplan = await getStringFromLlm(\n\t\t\tplanningPrompt,\n\t\t\toptions.openAI,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\tisLlmResponseValid: plan !== undefined,\n\t\t\tllmGeneratedPlan: plan,\n\t\t} satisfies PlanningPromptCompleted);\n\t}\n\n\tconst originalDecoratedJson =\n\t\t(options.finalReviewStep ?? false)\n\t\t\t? toDecoratedJson(idGenerator, options.treeNode)\n\t\t\t: undefined;\n\t// reviewed is implicitly true if finalReviewStep is false\n\tlet hasReviewed = (options.finalReviewStep ?? false) ? false : true;\n\tasync function getNextEdit(): Promise<\n\t\t{ edit: TreeEdit; eventFlowTraceId: string } | undefined\n\t> {\n\t\tconst systemPrompt = getEditingSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\teditLog,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t\tplan,\n\t\t);\n\n\t\tconst schema = types[rootTypeName] ?? fail(\"Root type not found.\");\n\n\t\tconst generateTreeEditEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies GenerateTreeEditStarted);\n\n\t\tconst wrapper = await getStructuredOutputFromLlm<EditWrapper>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\t\"A JSON object that represents an edit to a JSON tree.\",\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tisLlmResponseValid: wrapper?.edit !== undefined,\n\t\t\tllmGeneratedEdit: wrapper?.edit as Record<string, unknown> | null,\n\t\t} satisfies GenerateTreeEditCompleted);\n\n\t\tif (wrapper === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (wrapper.edit === null) {\n\t\t\tif ((options.finalReviewStep ?? false) && !hasReviewed) {\n\t\t\t\tconst reviewResult = await reviewGoal();\n\t\t\t\tif (reviewResult === undefined) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\thasReviewed = true;\n\t\t\t\tif (reviewResult.goalAccomplished === \"yes\") {\n\t\t\t\t\treturn undefined;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\t\teditLog.length = 0;\n\t\t\t\t\treturn getNextEdit();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\treturn { edit: wrapper.edit, eventFlowTraceId: generateTreeEditEventFlowId };\n\t\t}\n\t}\n\n\tasync function reviewGoal(): Promise<ReviewResult | undefined> {\n\t\tconst systemPrompt = getReviewSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\toriginalDecoratedJson ?? fail(\"Original decorated tree not provided.\"),\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst schema = z.object({\n\t\t\tgoalAccomplished: z\n\t\t\t\t.enum([\"yes\", \"no\"])\n\t\t\t\t.describe('Whether the user\\'s goal was met in the \"after\" tree.'),\n\t\t});\n\n\t\tconst finalReviewEventFlowTraceId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies FinalReviewStarted);\n\n\t\t// TODO: In the future, when using structured output isn't guarenteed, we will\n\t\t// need to add a custom type guard to ensure that output is in the right shape.\n\t\tconst output = await getStructuredOutputFromLlm<ReviewResult>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\tundefined,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tisLlmResponseValid: output !== undefined,\n\t\t\tdidLlmAccomplishGoal: output?.goalAccomplished,\n\t\t} satisfies FinalReviewCompleted);\n\n\t\treturn output;\n\t}\n\n\tlet edit = await getNextEdit();\n\twhile (edit !== undefined) {\n\t\tyield edit;\n\t\tif (tokensUsed.inputTokens > (tokenLimits?.inputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Input token limit exceeded.\");\n\t\t}\n\t\tif (tokensUsed.outputTokens > (tokenLimits?.outputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Output token limit exceeded.\");\n\t\t}\n\t\tedit = await getNextEdit();\n\t}\n}\n\n/**\n * Calls the LLM to generate a structured output response based on the provided prompt.\n */\nasync function getStructuredOutputFromLlm<T>(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\tstructuredOutputSchema: Zod.ZodTypeAny,\n\tdescription?: string,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<T | undefined> {\n\tconst response_format = zodResponseFormat(structuredOutputSchema, \"SharedTreeAI\", {\n\t\tdescription,\n\t});\n\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t\tresponse_format,\n\t};\n\n\tconst result = await openAi.client.beta.chat.completions.parse(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\t// TODO: fix types so this isn't null and doesn't need a cast\n\t// The type should be derived from the zod schema\n\t// TODO: Determine why this value would be undefined.\n\treturn result.choices[0]?.message.parsed as T | undefined;\n}\n\n/**\n * Calls the LLM to generate a response based on the provided prompt.\n */\nasync function getStringFromLlm(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<string | undefined> {\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t};\n\n\tconst result = await openAi.client.chat.completions.create(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\treturn result.choices[0]?.message.content ?? undefined;\n}\n\nclass TokenLimitExceededError extends Error {}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/explicit-strategy/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AACtE,OAAO,EACN,eAAe,EACf,IAAI,GAGJ,MAAM,+BAA+B,CAAC;AACvC,sDAAsD;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAKvD,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,CAAC,EAAmB,MAAM,KAAK,CAAC;AAUzC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAWN,kBAAkB,EAGlB,mBAAmB,GACnB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EACN,sBAAsB,EACtB,uBAAuB,EACvB,qBAAqB,EACrB,eAAe,GAEf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE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AwB,SAAQ,KAAK;CAAG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tgetSimpleSchema,\n\tTree,\n\ttype SimpleTreeSchema,\n\ttype TreeNode,\n} from \"@fluidframework/tree/internal\";\n// eslint-disable-next-line import/no-internal-modules\nimport { zodResponseFormat } from \"openai/helpers/zod\";\nimport type {\n\tChatCompletionCreateParams,\n\t// eslint-disable-next-line import/no-internal-modules\n} from \"openai/resources/index.mjs\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type {\n\tDebugEventLogHandler,\n\tOpenAiClientOptions,\n\tTokenLimits,\n\tTokenUsage,\n} from \"../aiCollabApi.js\";\nimport type { Diff } from \"../diffTypes.js\";\n\nimport { applyAgentEdit } from \"./agentEditReducer.js\";\nimport type { EditWrapper, TreeEdit } from \"./agentEditTypes.js\";\nimport {\n\ttype ApplyEditFailure,\n\ttype ApplyEditSuccess,\n\ttype GenerateTreeEditCompleted,\n\ttype GenerateTreeEditStarted,\n\ttype FinalReviewCompleted,\n\ttype FinalReviewStarted,\n\ttype LlmApiCallDebugEvent,\n\ttype PlanningPromptCompleted,\n\ttype CoreEventLoopStarted,\n\ttype CoreEventLoopCompleted,\n\tgenerateDebugEvent,\n\ttype PlanningPromptStarted,\n\ttype EventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\nimport { IdGenerator } from \"./idGenerator.js\";\nimport {\n\tgetEditingSystemPrompt,\n\tgetPlanningSystemPrompt,\n\tgetReviewSystemPrompt,\n\ttoDecoratedJson,\n\ttype EditLog,\n} from \"./promptGeneration.js\";\nimport { generateGenericEditTypes } from \"./typeGeneration.js\";\nimport { fail } from \"./utils.js\";\n\n// TODO: Create a proper index file and move the logic of this file to a new location\nexport type {\n\tApplyEditFailure,\n\tApplyEditSuccess,\n\tCoreEventLoopCompleted,\n\tCoreEventLoopStarted,\n\tFinalReviewCompleted,\n\tFinalReviewStarted,\n\tGenerateTreeEditCompleted,\n\tGenerateTreeEditStarted,\n\tLlmApiCallDebugEvent,\n\tPlanningPromptCompleted,\n\tPlanningPromptStarted,\n\tLlmTreeEdit,\n\tEventFlowDebugName,\n\tEventFlowDebugNames,\n} from \"./debugEvents.js\";\n\n/**\n * {@link generateTreeEdits} options.\n *\n * @internal\n */\nexport interface GenerateTreeEditsOptions {\n\topenAI: OpenAiClientOptions;\n\ttreeNode: TreeNode;\n\tprompt: {\n\t\tsystemRoleContext: string;\n\t\tuserAsk: string;\n\t};\n\tlimiters?: {\n\t\tabortController?: AbortController;\n\t\tmaxSequentialErrors?: number;\n\t\tmaxModelCalls?: number;\n\t\ttokenLimits?: TokenLimits;\n\t};\n\tfinalReviewStep?: boolean;\n\tvalidator?: (newContent: TreeNode) => void;\n\tdebugEventLogHandler?: DebugEventLogHandler;\n\tplanningStep?: boolean;\n}\n\ninterface GenerateTreeEditsSuccessResponse {\n\tstatus: \"success\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\ninterface GenerateTreeEditsErrorResponse {\n\tstatus: \"failure\" | \"partial-failure\";\n\terrorMessage:\n\t\t| \"tokenLimitExceeded\"\n\t\t| \"tooManyErrors\"\n\t\t| \"tooManyModelCalls\"\n\t\t| \"aborted\"\n\t\t| \"unexpectedError\";\n\ttokensUsed: TokenUsage;\n\treadonly diffs: readonly Diff[];\n}\n\n/**\n * Prompts the provided LLM client to generate valid tree edits.\n * Applies those edits to the provided tree branch before returning.\n *\n * @remarks\n * - Optional root nodes are not supported\n * - Primitive root nodes are not supported\n *\n * @internal\n */\nexport async function generateTreeEdits(\n\toptions: GenerateTreeEditsOptions,\n): Promise<GenerateTreeEditsSuccessResponse | GenerateTreeEditsErrorResponse> {\n\tconst idGenerator = new IdGenerator();\n\tconst editLog: EditLog = [];\n\tconst diffs: Diff[] = [];\n\n\tlet editCount = 0;\n\tlet sequentialErrorCount = 0;\n\n\tconst simpleSchema = getSimpleSchema(Tree.schema(options.treeNode));\n\n\tconst tokensUsed = { inputTokens: 0, outputTokens: 0 };\n\n\tconst debugLogTraceId = uuidv4();\n\n\tconst coreEventFlowTraceId = uuidv4();\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_STARTED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"STARTED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t} satisfies CoreEventLoopStarted);\n\n\ttry {\n\t\tfor await (const generateEditResult of generateEdits(\n\t\t\toptions,\n\t\t\tsimpleSchema,\n\t\t\tidGenerator,\n\t\t\teditLog,\n\t\t\toptions.limiters?.tokenLimits,\n\t\t\ttokensUsed,\n\t\t\toptions.debugEventLogHandler && {\n\t\t\t\teventLogHandler: options.debugEventLogHandler,\n\t\t\t\ttraceId: debugLogTraceId,\n\t\t\t},\n\t\t)) {\n\t\t\ttry {\n\t\t\t\tconst result = applyAgentEdit(\n\t\t\t\t\tgenerateEditResult.edit,\n\t\t\t\t\tidGenerator,\n\t\t\t\t\tsimpleSchema.definitions,\n\t\t\t\t\toptions.validator,\n\t\t\t\t);\n\t\t\t\teditLog.push({ edit: { ...result.edit } });\n\t\t\t\tdiffs.push(result.diff);\n\t\t\t\tsequentialErrorCount = 0;\n\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_SUCCESS\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t} satisfies ApplyEditSuccess);\n\t\t\t} catch (error: unknown) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"APPLIED_EDIT_FAILURE\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\t\teventFlowStatus: \"IN_PROGRESS\",\n\t\t\t\t\teventFlowTraceId: generateEditResult.eventFlowTraceId,\n\t\t\t\t\tedit: generateEditResult.edit as unknown as Record<string, unknown>,\n\t\t\t\t\terrorMessage: (error as Error)?.message,\n\t\t\t\t\tsequentialErrorCount,\n\t\t\t\t} satisfies ApplyEditFailure);\n\n\t\t\t\tif (error instanceof UsageError) {\n\t\t\t\t\tsequentialErrorCount += 1;\n\t\t\t\t\teditLog.push({ edit: generateEditResult.edit, error: error.message });\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet shouldExitEarly = false;\n\t\t\tconst completionResponse: GenerateTreeEditsErrorResponse = {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"unexpectedError\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\n\t\t\tif (options.limiters?.abortController?.signal.aborted === true) {\n\t\t\t\tcompletionResponse.errorMessage = \"aborted\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\tsequentialErrorCount >\n\t\t\t\t(options.limiters?.maxSequentialErrors ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyErrors\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t} else if (\n\t\t\t\t++editCount >= (options.limiters?.maxModelCalls ?? Number.POSITIVE_INFINITY)\n\t\t\t) {\n\t\t\t\tcompletionResponse.errorMessage = \"tooManyModelCalls\";\n\t\t\t\tshouldExitEarly = true;\n\t\t\t}\n\n\t\t\tif (shouldExitEarly) {\n\t\t\t\toptions.debugEventLogHandler?.({\n\t\t\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\t\t\tstatus: \"failure\",\n\t\t\t\t\tfailureReason: completionResponse.errorMessage,\n\t\t\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\t\t} satisfies CoreEventLoopCompleted);\n\n\t\t\t\treturn completionResponse;\n\t\t\t}\n\t\t}\n\t} catch (error: unknown) {\n\t\toptions.debugEventLogHandler?.({\n\t\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\tstatus: \"failure\",\n\t\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\t\tfailureReason:\n\t\t\t\terror instanceof TokenLimitExceededError ? \"tokenLimitExceeded\" : \"unexpectedError\",\n\t\t\terrorMessage: (error as Error)?.message,\n\t\t} satisfies CoreEventLoopCompleted);\n\n\t\tif (error instanceof TokenLimitExceededError) {\n\t\t\treturn {\n\t\t\t\tstatus:\n\t\t\t\t\teditCount > 0 && sequentialErrorCount < editCount ? \"partial-failure\" : \"failure\",\n\t\t\t\terrorMessage: \"tokenLimitExceeded\",\n\t\t\t\ttokensUsed,\n\t\t\t\tdiffs,\n\t\t\t};\n\t\t}\n\t\tthrow error;\n\t}\n\n\toptions.debugEventLogHandler?.({\n\t\t...generateDebugEvent(\"CORE_EVENT_LOOP_COMPLETED\", debugLogTraceId),\n\t\teventFlowName: EventFlowDebugNames.CORE_EVENT_LOOP,\n\t\teventFlowStatus: \"COMPLETED\",\n\t\teventFlowTraceId: coreEventFlowTraceId,\n\t\tstatus: \"success\",\n\t} satisfies CoreEventLoopCompleted);\n\n\treturn {\n\t\tstatus: \"success\",\n\t\ttokensUsed,\n\t\tdiffs,\n\t};\n}\n\ninterface ReviewResult {\n\tgoalAccomplished: \"yes\" | \"no\";\n}\n\n/**\n * Generates a single {@link TreeEdit} from an LLM.\n *\n * @remarks\n * The design of this async generator function is such that which each iteration of this functions values,\n * an LLM will be prompted to generate the next value (a {@link TreeEdit}) based on the users ask.\n * Once the LLM believes it has completed the user's ask, it will no longer return an edit and as a result\n * this generator will no longer yield a next value.\n */\nasync function* generateEdits(\n\toptions: GenerateTreeEditsOptions,\n\tsimpleSchema: SimpleTreeSchema,\n\tidGenerator: IdGenerator,\n\teditLog: EditLog,\n\ttokenLimits: TokenLimits | undefined,\n\ttokensUsed: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t},\n): AsyncGenerator<{ edit: TreeEdit; eventFlowTraceId: string }> {\n\tconst [types, rootTypeName] = generateGenericEditTypes(simpleSchema, true);\n\n\tlet plan: string | undefined;\n\tif (options.planningStep !== undefined) {\n\t\tconst planningPrompt = getPlanningSystemPrompt(\n\t\t\toptions.treeNode,\n\t\t\toptions.prompt.userAsk,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst generatePlanningPromptEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t} satisfies PlanningPromptStarted);\n\n\t\tplan = await getStringFromLlm(\n\t\t\tplanningPrompt,\n\t\t\toptions.openAI,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_PLANNING_PROMPT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_PLANNING_PROMPT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generatePlanningPromptEventFlowId,\n\t\t\tisLlmResponseValid: plan !== undefined,\n\t\t\tllmGeneratedPlan: plan,\n\t\t} satisfies PlanningPromptCompleted);\n\t}\n\n\tconst originalDecoratedJson =\n\t\t(options.finalReviewStep ?? false)\n\t\t\t? toDecoratedJson(idGenerator, options.treeNode)\n\t\t\t: undefined;\n\t// reviewed is implicitly true if finalReviewStep is false\n\tlet hasReviewed = (options.finalReviewStep ?? false) ? false : true;\n\tasync function getNextEdit(): Promise<\n\t\t{ edit: TreeEdit; eventFlowTraceId: string } | undefined\n\t> {\n\t\tconst systemPrompt = getEditingSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\teditLog,\n\t\t\toptions.prompt.systemRoleContext,\n\t\t\tplan,\n\t\t);\n\n\t\tconst schema = types[rootTypeName] ?? fail(\"Root type not found.\");\n\n\t\tconst generateTreeEditEventFlowId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies GenerateTreeEditStarted);\n\n\t\tconst wrapper = await getStructuredOutputFromLlm<EditWrapper>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\t\"A JSON object that represents an edit to a JSON tree.\",\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"GENERATE_TREE_EDIT_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.GENERATE_AND_APPLY_TREE_EDIT,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: generateTreeEditEventFlowId,\n\t\t\tisLlmResponseValid: wrapper?.edit !== undefined,\n\t\t\tllmGeneratedEdit: wrapper?.edit as Record<string, unknown> | null,\n\t\t} satisfies GenerateTreeEditCompleted);\n\n\t\tif (wrapper === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (wrapper.edit === null) {\n\t\t\tif ((options.finalReviewStep ?? false) && !hasReviewed) {\n\t\t\t\tconst reviewResult = await reviewGoal();\n\t\t\t\tif (reviewResult === undefined) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\thasReviewed = true;\n\t\t\t\tif (reviewResult.goalAccomplished === \"yes\") {\n\t\t\t\t\treturn undefined;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line require-atomic-updates\n\t\t\t\t\teditLog.length = 0;\n\t\t\t\t\treturn getNextEdit();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\treturn { edit: wrapper.edit, eventFlowTraceId: generateTreeEditEventFlowId };\n\t\t}\n\t}\n\n\tasync function reviewGoal(): Promise<ReviewResult | undefined> {\n\t\tconst systemPrompt = getReviewSystemPrompt(\n\t\t\toptions.prompt.userAsk,\n\t\t\tidGenerator,\n\t\t\toptions.treeNode,\n\t\t\toriginalDecoratedJson ?? fail(\"Original decorated tree not provided.\"),\n\t\t\toptions.prompt.systemRoleContext,\n\t\t);\n\n\t\tconst schema = z.object({\n\t\t\tgoalAccomplished: z\n\t\t\t\t.enum([\"yes\", \"no\"])\n\t\t\t\t.describe('Whether the user\\'s goal was met in the \"after\" tree.'),\n\t\t});\n\n\t\tconst finalReviewEventFlowTraceId = uuidv4();\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_STARTED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"STARTED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tllmPrompt: systemPrompt,\n\t\t} satisfies FinalReviewStarted);\n\n\t\t// TODO: In the future, when using structured output isn't guarenteed, we will\n\t\t// need to add a custom type guard to ensure that output is in the right shape.\n\t\tconst output = await getStructuredOutputFromLlm<ReviewResult>(\n\t\t\tsystemPrompt,\n\t\t\toptions.openAI,\n\t\t\tschema,\n\t\t\tundefined,\n\t\t\ttokensUsed,\n\t\t\tdebugOptions && {\n\t\t\t\t...debugOptions,\n\t\t\t\ttriggeringEventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\t},\n\t\t);\n\n\t\tdebugOptions?.eventLogHandler?.({\n\t\t\t...generateDebugEvent(\"FINAL_REVIEW_COMPLETED\", debugOptions.traceId),\n\t\t\teventFlowName: EventFlowDebugNames.FINAL_REVIEW,\n\t\t\teventFlowStatus: \"COMPLETED\",\n\t\t\teventFlowTraceId: finalReviewEventFlowTraceId,\n\t\t\tisLlmResponseValid: output !== undefined,\n\t\t\tdidLlmAccomplishGoal: output?.goalAccomplished,\n\t\t} satisfies FinalReviewCompleted);\n\n\t\treturn output;\n\t}\n\n\tlet edit = await getNextEdit();\n\twhile (edit !== undefined) {\n\t\tyield edit;\n\t\tif (tokensUsed.inputTokens > (tokenLimits?.inputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Input token limit exceeded.\");\n\t\t}\n\t\tif (tokensUsed.outputTokens > (tokenLimits?.outputTokens ?? Number.POSITIVE_INFINITY)) {\n\t\t\tthrow new TokenLimitExceededError(\"Output token limit exceeded.\");\n\t\t}\n\t\tedit = await getNextEdit();\n\t}\n}\n\n/**\n * Calls the LLM to generate a structured output response based on the provided prompt.\n */\nasync function getStructuredOutputFromLlm<T>(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\tstructuredOutputSchema: ZodTypeAny,\n\tdescription?: string,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<T | undefined> {\n\tconst response_format = zodResponseFormat(structuredOutputSchema, \"SharedTreeAI\", {\n\t\tdescription,\n\t});\n\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t\tresponse_format,\n\t};\n\n\tconst result = await openAi.client.beta.chat.completions.parse(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\t// TODO: fix types so this isn't null and doesn't need a cast\n\t// The type should be derived from the zod schema\n\t// TODO: Determine why this value would be undefined.\n\treturn result.choices[0]?.message.parsed as T | undefined;\n}\n\n/**\n * Calls the LLM to generate a response based on the provided prompt.\n */\nasync function getStringFromLlm(\n\tprompt: string,\n\topenAi: OpenAiClientOptions,\n\ttokensUsed?: TokenUsage,\n\tdebugOptions?: {\n\t\teventLogHandler: DebugEventLogHandler;\n\t\ttraceId: string;\n\t\ttriggeringEventFlowName: EventFlowDebugName;\n\t\teventFlowTraceId: string;\n\t},\n): Promise<string | undefined> {\n\tconst body: ChatCompletionCreateParams = {\n\t\tmessages: [{ role: \"system\", content: prompt }],\n\t\tmodel: openAi.modelName ?? \"gpt-4o\",\n\t};\n\n\tconst result = await openAi.client.chat.completions.create(body);\n\n\tdebugOptions?.eventLogHandler?.({\n\t\t...generateDebugEvent(\"LLM_API_CALL\", debugOptions.traceId),\n\t\ttriggeringEventFlowName: debugOptions.triggeringEventFlowName,\n\t\teventFlowTraceId: debugOptions.eventFlowTraceId,\n\t\tmodelName: openAi.modelName ?? \"gpt-4o\",\n\t\trequestParams: body,\n\t\tresponse: { ...result },\n\t\t...(result.usage && {\n\t\t\ttokenUsage: {\n\t\t\t\tpromptTokens: result.usage.prompt_tokens,\n\t\t\t\tcompletionTokens: result.usage.completion_tokens,\n\t\t\t},\n\t\t}),\n\t} satisfies LlmApiCallDebugEvent);\n\n\tif (result.usage !== undefined && tokensUsed !== undefined) {\n\t\ttokensUsed.inputTokens += result.usage?.prompt_tokens;\n\t\ttokensUsed.outputTokens += result.usage?.completion_tokens;\n\t}\n\n\treturn result.choices[0]?.message.content ?? undefined;\n}\n\nclass TokenLimitExceededError extends Error {}\n"]}
@@ -3,6 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import type { SimpleTreeSchema, TreeNode } from "@fluidframework/tree/internal";
6
+ import { type ZodTypeAny } from "zod";
6
7
  /**
7
8
  * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}
8
9
  * 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.
@@ -11,7 +12,7 @@ import type { SimpleTreeSchema, TreeNode } from "@fluidframework/tree/internal";
11
12
  *
12
13
  * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.
13
14
  */
14
- export declare function generateGenericEditTypes(schema: SimpleTreeSchema, generateDomainTypes: boolean): [Record<string, Zod.ZodTypeAny>, root: string];
15
+ export declare function generateGenericEditTypes(schema: SimpleTreeSchema, generateDomainTypes: boolean): [Record<string, ZodTypeAny>, root: string];
15
16
  /**
16
17
  * Determines if the provided {@link TreeNode} contains an array schema.
17
18
  */
@@ -1 +1 @@
1
- {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAGX,gBAAgB,EAChB,QAAQ,EACR,MAAM,+BAA+B,CAAC;AA+EvC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,gBAAgB,EACxB,mBAAmB,EAAE,OAAO,GAC1B,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CA8GhD;AA6LD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAUlE"}
1
+ {"version":3,"file":"typeGeneration.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/typeGeneration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAGX,gBAAgB,EAChB,QAAQ,EACR,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAK,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AA8EzC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,gBAAgB,EACxB,mBAAmB,EAAE,OAAO,GAC1B,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CA8G5C;AA6LD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAUlE"}
@@ -248,7 +248,7 @@ function getTypeForAllowedTypes(definitionMap, typeMap, insertSet, modifyFieldSe
248
248
  return getOrCreateType(definitionMap, typeMap, insertSet, modifyFieldSet, modifyTypeSet, name);
249
249
  }),
250
250
  ];
251
- assert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
251
+ assert(hasAtLeastTwo(types), 0xc1b /* Expected at least two types */);
252
252
  return z.union(types);
253
253
  }
254
254
  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,EACN,SAAS,EACT,eAAe,EACf,QAAQ,EACR,IAAI,EACJ,WAAW,GACX,MAAM,+BAA+B,CAAC;AAOvC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D;;;GAGG;AACH,MAAM,YAAY,GAAG,CAAC;KACpB,MAAM,CAAC;IACP,MAAM,EAAE,CAAC;SACP,MAAM,EAAE;SACR,QAAQ,CACR,sDAAsD,WAAW,qCAAqC,CACtG;CACF,CAAC;KACD,QAAQ,CACR,wFAAwF,CACxF,CAAC;AACH;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC;KACnB,MAAM,CAAC;IACP,IAAI,EA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AAC,CAAC;IAC7C,KAAK,MAAM,CAAC,EAAE,UAAU,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;QACvD,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,CAAC,KAAK,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,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\tFieldKind,\n\tgetSimpleSchema,\n\tNodeKind,\n\tTree,\n\tValueSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNode,\n} from \"@fluidframework/tree/internal\";\nimport { z } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z\n\t.object({\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a specific object node in the tree, identified by the target object's Id.\",\n\t);\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, Zod.ZodTypeAny>, root: string] {\n\treturn getOrCreate(cache, schema, () => {\n\t\tconst insertSet = new Set<string>();\n\t\tconst modifyFieldSet = new Set<string>();\n\t\tconst modifyTypeSet = new Set<string>();\n\t\tconst typeMap = new Map<string, Zod.ZodTypeAny>();\n\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): Zod.ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst doesSchemaHaveArray = insertSet.size > 0;\n\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\n\t\tconst typeRecord: Record<string, Zod.ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tModify: modify,\n\t\t};\n\n\t\ttypeRecord.Modify = modify;\n\n\t\tif (doesSchemaHaveArray) {\n\t\t\ttypeRecord.ObjectPlace = objectPlace;\n\t\t\ttypeRecord.ArrayPlace = arrayPlace;\n\t\t\ttypeRecord.Range = range;\n\t\t\ttypeRecord.Insert = insert;\n\t\t\ttypeRecord.Remove = remove;\n\t\t\ttypeRecord.Move = move;\n\t\t}\n\n\t\tconst editTypes = doesSchemaHaveArray\n\t\t\t? ([insert, remove, move, modify, z.null()] as const)\n\t\t\t: ([modify, z.null()] as const);\n\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\ttypeRecord.EditWrapper = editWrapper;\n\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): Zod.ZodTypeAny {\n\treturn getOrCreate(typeMap, definition, () => {\n\t\tconst nodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\t\tswitch (nodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\tfor (const [key, field] of 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers) {\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\t[...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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, Zod.ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): Zod.ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(\n\t\t\t\t\tdefinitionMap,\n\t\t\t\t\ttypeMap,\n\t\t\t\t\tinsertSet,\n\t\t\t\t\tmodifyFieldSet,\n\t\t\t\t\tmodifyTypeSet,\n\t\t\t\t\tname,\n\t\t\t\t);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\n\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Determines if the provided {@link TreeNode} contains an array schema.\n */\nexport function doesNodeContainArraySchema(node: TreeNode): boolean {\n\tconst schema = Tree.schema(node);\n\tconst simpleSchema = getSimpleSchema(schema);\n\tfor (const [, nodeSchema] of simpleSchema.definitions) {\n\t\tif (nodeSchema.kind === NodeKind.Array) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"]}
1
+ {"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../../src/explicit-strategy/typeGene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,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;AAED,SAAS,eAAe,CAAI,GAAmB;IAC9C,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,aAAa,CAAI,KAAU;IACnC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,0BAA0B,CAAC,IAAc;IACxD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,YAAY,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IAC7C,KAAK,MAAM,CAAC,EAAE,UAAU,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;QACvD,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,CAAC,KAAK,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,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\tFieldKind,\n\tgetSimpleSchema,\n\tNodeKind,\n\tTree,\n\tValueSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNode,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport { objectIdKey, typeField } from \"./agentEditTypes.js\";\nimport { fail, getOrCreate, mapIterable } from \"./utils.js\";\n\n/**\n * Zod Object type used to represent & validate the ObjectTarget type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectTarget = z\n\t.object({\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id of the object (as specified by the object's ${objectIdKey} property) that is being referenced`,\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a specific object node in the tree, identified by the target object's Id.\",\n\t);\n/**\n * Zod Object type used to represent & validate the ObjectPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst objectPlace = z\n\t.object({\n\t\ttype: z.enum([\"objectPlace\"]),\n\t\ttarget: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the object that the new/moved object should be placed relative to. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tplace: z\n\t\t\t.enum([\"before\", \"after\"])\n\t\t\t.describe(\n\t\t\t\t\"Where the new/moved object will be relative to the target object - either just before or just after\",\n\t\t\t),\n\t})\n\t.describe(\n\t\t\"A pointer to a location either just before or just after an object that is in an array\",\n\t);\n/**\n * Zod Object type used to represent & validate the ArrayPlace type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst arrayPlace = z\n\t.object({\n\t\ttype: z.enum([\"arrayPlace\"]),\n\t\tparentId: z\n\t\t\t.string()\n\t\t\t.describe(\n\t\t\t\t`The id (${objectIdKey}) of the parent object of the array. This must be the id of an object that already existed in the tree content that was originally supplied.`,\n\t\t\t),\n\t\tfield: z.string().describe(\"The key of the array to insert into\"),\n\t\tlocation: z\n\t\t\t.enum([\"start\", \"end\"])\n\t\t\t.describe(\"Where to insert into the array - either the start or the end\"),\n\t})\n\t.describe(\n\t\t`either the \"start\" or \"end\" of an array, as specified by a \"parent\" ObjectTarget and a \"field\" name under which the array is stored (useful for prepending or appending)`,\n\t);\n/**\n * Zod Object type used to represent & validate the Range type within a {@link TreeEdit}.\n * @remarks this is used as a component with {@link generateGenericEditTypes} to produce the final zod validation objects.\n */\nconst range = z\n\t.object({\n\t\tfrom: objectPlace,\n\t\tto: objectPlace,\n\t})\n\t.describe(\n\t\t'A range of objects in the same array specified by a \"from\" and \"to\" Place. The \"to\" and \"from\" objects MUST be in the same array.',\n\t);\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEditTypes>>();\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nexport function generateGenericEditTypes(\n\tschema: SimpleTreeSchema,\n\tgenerateDomainTypes: boolean,\n): [Record<string, 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, ZodTypeAny>();\n\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\tgetOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\ttypeMap,\n\t\t\t\tinsertSet,\n\t\t\t\tmodifyFieldSet,\n\t\t\t\tmodifyTypeSet,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\t\tfunction getType(allowedTypes: ReadonlySet<string>): ZodTypeAny {\n\t\t\tswitch (allowedTypes.size) {\n\t\t\t\tcase 0: {\n\t\t\t\t\treturn z.never();\n\t\t\t\t}\n\t\t\t\tcase 1: {\n\t\t\t\t\treturn (\n\t\t\t\t\t\ttypeMap.get(tryGetSingleton(allowedTypes) ?? fail(\"Expected singleton\")) ??\n\t\t\t\t\t\tfail(\"Unknown type\")\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tconst types = Array.from(\n\t\t\t\t\t\tallowedTypes,\n\t\t\t\t\t\t(name) => typeMap.get(name) ?? fail(\"Unknown type\"),\n\t\t\t\t\t);\n\t\t\t\t\tassert(hasAtLeastTwo(types), 0xa7d /* Expected at least two types */);\n\t\t\t\t\treturn z.union(types);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst doesSchemaHaveArray = insertSet.size > 0;\n\n\t\tconst modify = z\n\t\t\t.object({\n\t\t\t\ttype: z.enum([\"modify\"]),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\ttarget: objectTarget,\n\t\t\t\tfield: z.enum([...modifyFieldSet] as [string, ...string[]]), // Modify with appropriate fields\n\t\t\t\tmodification: generateDomainTypes\n\t\t\t\t\t? getType(modifyTypeSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t})\n\t\t\t.describe(\"Sets a field on a specific ObjectTarget.\");\n\n\t\tconst remove = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"remove\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t})\n\t\t\t.describe(\"Deletes an object or Range of objects from the tree.\");\n\n\t\tconst insert = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"insert\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tcontent: generateDomainTypes\n\t\t\t\t\t? getType(insertSet)\n\t\t\t\t\t: z.any().describe(\"Domain-specific content here\"),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Inserts a new object at a specific Place or ArrayPlace.\");\n\n\t\tconst move = z\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"move\"),\n\t\t\t\texplanation: z.string().describe(editDescription),\n\t\t\t\tsource: z.union([objectTarget, range]),\n\t\t\t\tdestination: z.union([arrayPlace, objectPlace]),\n\t\t\t})\n\t\t\t.describe(\"Moves an object or Range of objects to a new Place or ArrayPlace.\");\n\n\t\tconst typeRecord: Record<string, ZodTypeAny> = {\n\t\t\tObjectTarget: objectTarget,\n\t\t\tModify: modify,\n\t\t};\n\n\t\ttypeRecord.Modify = modify;\n\n\t\tif (doesSchemaHaveArray) {\n\t\t\ttypeRecord.ObjectPlace = objectPlace;\n\t\t\ttypeRecord.ArrayPlace = arrayPlace;\n\t\t\ttypeRecord.Range = range;\n\t\t\ttypeRecord.Insert = insert;\n\t\t\ttypeRecord.Remove = remove;\n\t\t\ttypeRecord.Move = move;\n\t\t}\n\n\t\tconst editTypes = doesSchemaHaveArray\n\t\t\t? ([insert, remove, move, modify, z.null()] as const)\n\t\t\t: ([modify, z.null()] as const);\n\n\t\tconst editWrapper = z.object({\n\t\t\tedit: z\n\t\t\t\t.union(editTypes)\n\t\t\t\t.describe(\"The next edit to apply to the tree, or null if the task is complete.\"),\n\t\t});\n\t\ttypeRecord.EditWrapper = editWrapper;\n\n\t\treturn [typeRecord, \"EditWrapper\"];\n\t});\n}\nconst editDescription =\n\t\"A description of what this edit is meant to accomplish in human readable English\";\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tdefinition: string,\n): 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 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers) {\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\t[...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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\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, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tfieldSchema: SimpleFieldSchema,\n): 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.allowedTypesIdentifiers,\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.allowedTypesIdentifiers,\n\t\t\t\t),\n\t\t\t]);\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn undefined;\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\ttypeMap: Map<string, ZodTypeAny>,\n\tinsertSet: Set<string>,\n\tmodifyFieldSet: Set<string>,\n\tmodifyTypeSet: Set<string>,\n\tallowedTypes: ReadonlySet<string>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(\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), 0xc1b /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(\n\t\t\tdefinitionMap,\n\t\t\ttypeMap,\n\t\t\tinsertSet,\n\t\t\tmodifyFieldSet,\n\t\t\tmodifyTypeSet,\n\t\t\tsingle,\n\t\t);\n\t}\n}\n\nfunction tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\nfunction hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Determines if the provided {@link TreeNode} contains an array schema.\n */\nexport function doesNodeContainArraySchema(node: TreeNode): boolean {\n\tconst schema = Tree.schema(node);\n\tconst simpleSchema = getSimpleSchema(schema);\n\tfor (const [, nodeSchema] of simpleSchema.definitions) {\n\t\tif (nodeSchema.kind === NodeKind.Array) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/ai-collab",
3
- "version": "2.52.0",
3
+ "version": "2.53.0",
4
4
  "description": "Experimental package to simplify integrating AI into Fluid-based applications",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -69,27 +69,27 @@
69
69
  "temp-directory": "nyc/.nyc_output"
70
70
  },
71
71
  "dependencies": {
72
- "@fluidframework/core-utils": "~2.52.0",
73
- "@fluidframework/runtime-utils": "~2.52.0",
74
- "@fluidframework/telemetry-utils": "~2.52.0",
75
- "@fluidframework/tree": "~2.52.0",
72
+ "@fluidframework/core-utils": "~2.53.0",
73
+ "@fluidframework/runtime-utils": "~2.53.0",
74
+ "@fluidframework/telemetry-utils": "~2.53.0",
75
+ "@fluidframework/tree": "~2.53.0",
76
76
  "fastest-levenshtein": "^1.0.16",
77
77
  "openai": "^4.67.3",
78
78
  "typechat": "^0.1.1",
79
79
  "uuid": "^11.1.0",
80
- "zod": "^3.23.8"
80
+ "zod": "^3.25.32"
81
81
  },
82
82
  "devDependencies": {
83
83
  "@arethetypeswrong/cli": "^0.17.1",
84
84
  "@biomejs/biome": "~1.9.3",
85
- "@fluid-internal/mocha-test-setup": "~2.52.0",
85
+ "@fluid-internal/mocha-test-setup": "~2.53.0",
86
86
  "@fluid-tools/build-cli": "^0.57.0",
87
87
  "@fluidframework/build-common": "^2.0.3",
88
88
  "@fluidframework/build-tools": "^0.57.0",
89
89
  "@fluidframework/eslint-config-fluid": "^5.7.4",
90
- "@fluidframework/id-compressor": "~2.52.0",
91
- "@fluidframework/runtime-utils": "~2.52.0",
92
- "@fluidframework/test-runtime-utils": "~2.52.0",
90
+ "@fluidframework/id-compressor": "~2.53.0",
91
+ "@fluidframework/runtime-utils": "~2.53.0",
92
+ "@fluidframework/test-runtime-utils": "~2.53.0",
93
93
  "@microsoft/api-extractor": "7.52.8",
94
94
  "@types/mocha": "^10.0.10",
95
95
  "@types/node": "^18.19.0",
@@ -17,7 +17,7 @@ import type {
17
17
  // eslint-disable-next-line import/no-internal-modules
18
18
  } from "openai/resources/index.mjs";
19
19
  import { v4 as uuidv4 } from "uuid";
20
- import { z } from "zod";
20
+ import { z, type ZodTypeAny } from "zod";
21
21
 
22
22
  import type {
23
23
  DebugEventLogHandler,
@@ -487,7 +487,7 @@ async function* generateEdits(
487
487
  async function getStructuredOutputFromLlm<T>(
488
488
  prompt: string,
489
489
  openAi: OpenAiClientOptions,
490
- structuredOutputSchema: Zod.ZodTypeAny,
490
+ structuredOutputSchema: ZodTypeAny,
491
491
  description?: string,
492
492
  tokensUsed?: TokenUsage,
493
493
  debugOptions?: {
@@ -17,7 +17,7 @@ import type {
17
17
  SimpleTreeSchema,
18
18
  TreeNode,
19
19
  } from "@fluidframework/tree/internal";
20
- import { z } from "zod";
20
+ import { z, type ZodTypeAny } from "zod";
21
21
 
22
22
  import { objectIdKey, typeField } from "./agentEditTypes.js";
23
23
  import { fail, getOrCreate, mapIterable } from "./utils.js";
@@ -106,12 +106,12 @@ const cache = new WeakMap<SimpleTreeSchema, ReturnType<typeof generateGenericEdi
106
106
  export function generateGenericEditTypes(
107
107
  schema: SimpleTreeSchema,
108
108
  generateDomainTypes: boolean,
109
- ): [Record<string, Zod.ZodTypeAny>, root: string] {
109
+ ): [Record<string, ZodTypeAny>, root: string] {
110
110
  return getOrCreate(cache, schema, () => {
111
111
  const insertSet = new Set<string>();
112
112
  const modifyFieldSet = new Set<string>();
113
113
  const modifyTypeSet = new Set<string>();
114
- const typeMap = new Map<string, Zod.ZodTypeAny>();
114
+ const typeMap = new Map<string, ZodTypeAny>();
115
115
 
116
116
  for (const name of schema.definitions.keys()) {
117
117
  getOrCreateType(
@@ -123,7 +123,7 @@ export function generateGenericEditTypes(
123
123
  name,
124
124
  );
125
125
  }
126
- function getType(allowedTypes: ReadonlySet<string>): Zod.ZodTypeAny {
126
+ function getType(allowedTypes: ReadonlySet<string>): ZodTypeAny {
127
127
  switch (allowedTypes.size) {
128
128
  case 0: {
129
129
  return z.never();
@@ -187,7 +187,7 @@ export function generateGenericEditTypes(
187
187
  })
188
188
  .describe("Moves an object or Range of objects to a new Place or ArrayPlace.");
189
189
 
190
- const typeRecord: Record<string, Zod.ZodTypeAny> = {
190
+ const typeRecord: Record<string, ZodTypeAny> = {
191
191
  ObjectTarget: objectTarget,
192
192
  Modify: modify,
193
193
  };
@@ -221,12 +221,12 @@ const editDescription =
221
221
  "A description of what this edit is meant to accomplish in human readable English";
222
222
  function getOrCreateType(
223
223
  definitionMap: ReadonlyMap<string, SimpleNodeSchema>,
224
- typeMap: Map<string, Zod.ZodTypeAny>,
224
+ typeMap: Map<string, ZodTypeAny>,
225
225
  insertSet: Set<string>,
226
226
  modifyFieldSet: Set<string>,
227
227
  modifyTypeSet: Set<string>,
228
228
  definition: string,
229
- ): Zod.ZodTypeAny {
229
+ ): ZodTypeAny {
230
230
  return getOrCreate(typeMap, definition, () => {
231
231
  const nodeSchema = definitionMap.get(definition) ?? fail("Unexpected definition");
232
232
  switch (nodeSchema.kind) {
@@ -318,12 +318,12 @@ function getOrCreateType(
318
318
  }
319
319
  function getOrCreateTypeForField(
320
320
  definitionMap: ReadonlyMap<string, SimpleNodeSchema>,
321
- typeMap: Map<string, Zod.ZodTypeAny>,
321
+ typeMap: Map<string, ZodTypeAny>,
322
322
  insertSet: Set<string>,
323
323
  modifyFieldSet: Set<string>,
324
324
  modifyTypeSet: Set<string>,
325
325
  fieldSchema: SimpleFieldSchema,
326
- ): Zod.ZodTypeAny | undefined {
326
+ ): ZodTypeAny | undefined {
327
327
  switch (fieldSchema.kind) {
328
328
  case FieldKind.Required: {
329
329
  return getTypeForAllowedTypes(
@@ -359,12 +359,12 @@ function getOrCreateTypeForField(
359
359
 
360
360
  function getTypeForAllowedTypes(
361
361
  definitionMap: ReadonlyMap<string, SimpleNodeSchema>,
362
- typeMap: Map<string, Zod.ZodTypeAny>,
362
+ typeMap: Map<string, ZodTypeAny>,
363
363
  insertSet: Set<string>,
364
364
  modifyFieldSet: Set<string>,
365
365
  modifyTypeSet: Set<string>,
366
366
  allowedTypes: ReadonlySet<string>,
367
- ): Zod.ZodTypeAny {
367
+ ): ZodTypeAny {
368
368
  const single = tryGetSingleton(allowedTypes);
369
369
  if (single === undefined) {
370
370
  const types = [
@@ -379,7 +379,7 @@ function getTypeForAllowedTypes(
379
379
  );
380
380
  }),
381
381
  ];
382
- assert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);
382
+ assert(hasAtLeastTwo(types), 0xc1b /* Expected at least two types */);
383
383
  return z.union(types);
384
384
  } else {
385
385
  return getOrCreateType(