{"version":3,"sources":["/Users/shyun/comcom/ain-enterprise/ain-adk/dist/cjs/chunk-AQH6UZZK.cjs","../../src/services/workflow-response-composer.service.ts"],"names":["content"],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACA;ACEA,IAAM,4BAAA,EACL,iLAAA;AAED,IAAM,2BAAA,EACL,mTAAA;AAEM,IAAM,yBAAA,EAAN,MAA+B;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EAER,WAAA,CACC,WAAA,EACA,qBAAA,EAAuB,IAAI,2CAAA,CAAqB,CAAA,EAChD,qBAAA,EAAuB,IAAI,2CAAA,CAAqB,CAAA,EAC/C;AACD,IAAA,IAAA,CAAK,YAAA,EAAc,WAAA;AACnB,IAAA,IAAA,CAAK,qBAAA,EAAuB,oBAAA;AAC5B,IAAA,IAAA,CAAK,qBAAA,EAAuB,oBAAA;AAAA,EAC7B;AAAA,EAEA,MAAA,CAAO,mBAAA,CACN,KAAA,EACA,WAAA,EACA,eAAA,EAA0C,CAAC,CAAA,EACmB;AAC9D,IAAA,GAAA,CAAI,KAAA,CAAM,KAAA,IAAS,SAAA,EAAW;AAC7B,MAAA,MAAM,MAAA,mBAAQ,KAAA,CAAM,KAAA,UAAS,GAAA;AAC7B,MAAA,MAAMA,SAAAA,EAAU,CAAA,EAAA;AAAkC;AAAA;AACnC,MAAA;AACR,MAAA;AACS,QAAA;AACH,QAAA;AACZA,QAAAA;AACD,MAAA;AACD,IAAA;AAEmB,IAAA;AACD,MAAA;AAChB,QAAA;AACA,QAAA;AACD,MAAA;AACO,MAAA;AACS,QAAA;AACH,QAAA;AACH,QAAA;AACM,QAAA;AAChB,MAAA;AACD,IAAA;AAEmB,IAAA;AACD,MAAA;AAChB,QAAA;AACA,QAAA;AACA,QAAA;AACD,MAAA;AACO,MAAA;AACS,QAAA;AACH,QAAA;AACH,QAAA;AACM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ACH,IAAA;AACN,MAAA;AAEL,QAAA;AACA;AAA2E;AAC/E,MAAA;AACD,IAAA;AAES,IAAA;AAEL,MAAA;AACA;AAA8B;AAAA;AAAA,+EAAA;AAClC,IAAA;AAEgB,IAAA;AACjB,EAAA;AAGC,EAAA;AAIgB,IAAA;AACE,IAAA;AACV,MAAA;AACR,IAAA;AACmB,IAAA;AACb,MAAA;AACN,IAAA;AACkB,IAAA;AACnB,EAAA;AAEQ,EAAA;AAII,IAAA;AACF,MAAA;AACT,IAAA;AAEM,IAAA;AACC,IAAA;AAAuB,MAAA;AAE9B,IAAA;AACD,EAAA;AAEQ,EAAA;AAGA,IAAA;AAGY,MAAA;AACd;AAA4C;AAE9B,MAAA;AAAsB;AAAmC;AAErE,IAAA;AACR,EAAA;AACD;AD5EwB;AACA;AACA;AACA","file":"/Users/shyun/comcom/ain-enterprise/ain-adk/dist/cjs/chunk-AQH6UZZK.cjs","sourcesContent":[null,"import type { ModelModule } from \"@/modules\";\nimport { WorkflowGraphService } from \"@/services/workflow-graph.service.js\";\nimport { WorkflowTableService } from \"@/services/workflow-table.service.js\";\nimport type {\n\tWorkflowGraphBlock,\n\tWorkflowRenderedBlock,\n\tWorkflowResponseBlock,\n\tWorkflowTableBlock,\n\tWorkflowTaskResult,\n\tWorkflowTextBlock,\n} from \"@/types/memory.js\";\nimport type { StreamEvent } from \"@/types/stream.js\";\nimport { loggers } from \"@/utils/logger.js\";\nimport { serializeTaskResults } from \"@/utils/workflow-task-results.js\";\n\nconst GENERATE_TEXT_SYSTEM_PROMPT =\n\t\"Generate ONLY the new text described in Instructions. Task results and already-rendered blocks are reference context — never restate, echo, or repeat them in your output.\";\n\nconst PRESENT_TEXT_SYSTEM_PROMPT =\n\t\"Present the provided source content as directed by Instructions. The source content is your material: reorganize and format it exactly the way Instructions ask. Do not add facts or numbers that are not in the source content, and keep numbers and units exactly as written unless Instructions say otherwise.\";\n\nexport class WorkflowResponseComposer {\n\tprivate modelModule: ModelModule;\n\tprivate workflowGraphService: WorkflowGraphService;\n\tprivate workflowTableService: WorkflowTableService;\n\n\tconstructor(\n\t\tmodelModule: ModelModule,\n\t\tworkflowTableService = new WorkflowTableService(),\n\t\tworkflowGraphService = new WorkflowGraphService(),\n\t) {\n\t\tthis.modelModule = modelModule;\n\t\tthis.workflowTableService = workflowTableService;\n\t\tthis.workflowGraphService = workflowGraphService;\n\t}\n\n\tasync *renderResponseBlock(\n\t\tblock: WorkflowResponseBlock,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t\trenderedBlocks: WorkflowRenderedBlock[] = [],\n\t): AsyncGenerator<StreamEvent, WorkflowRenderedBlock, unknown> {\n\t\tif (block.type === \"heading\") {\n\t\t\tconst level = block.level ?? 2;\n\t\t\tconst content = `${\"#\".repeat(level)} ${block.text}\\n\\n`;\n\t\t\tyield { event: \"text_chunk\", data: { delta: content } };\n\t\t\treturn {\n\t\t\t\tblockId: block.blockId,\n\t\t\t\ttype: block.type,\n\t\t\t\tcontent,\n\t\t\t};\n\t\t}\n\n\t\tif (block.type === \"table\") {\n\t\t\tconst rendered = yield* this.renderDeterministicTableBlock(\n\t\t\t\tblock,\n\t\t\t\ttaskResults,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\tblockId: block.blockId,\n\t\t\t\ttype: block.type,\n\t\t\t\tcontent: rendered.content,\n\t\t\t\tdata: rendered.data,\n\t\t\t};\n\t\t}\n\n\t\tif (block.type === \"graph\") {\n\t\t\tconst rendered = yield* this.renderDeterministicGraphBlock(\n\t\t\t\tblock,\n\t\t\t\ttaskResults,\n\t\t\t\trenderedBlocks,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\tblockId: block.blockId,\n\t\t\t\ttype: block.type,\n\t\t\t\tcontent: rendered.content,\n\t\t\t\tdata: rendered.data,\n\t\t\t};\n\t\t}\n\n\t\tif (\n\t\t\tblock.mode === \"present\" &&\n\t\t\t!this.hasPresentableSource(block, taskResults, renderedBlocks)\n\t\t) {\n\t\t\tloggers.agent.warn(\n\t\t\t\t\"Present-mode text block has no source content; rendering empty block\",\n\t\t\t\t{ blockId: block.blockId },\n\t\t\t);\n\t\t\treturn {\n\t\t\t\tblockId: block.blockId,\n\t\t\t\ttype: block.type,\n\t\t\t\tcontent: \"\",\n\t\t\t};\n\t\t}\n\n\t\tconst content = yield* this.renderGeneratedTextBlock(\n\t\t\tblock,\n\t\t\ttaskResults,\n\t\t\trenderedBlocks,\n\t\t\tblock.mode === \"present\"\n\t\t\t\t? PRESENT_TEXT_SYSTEM_PROMPT\n\t\t\t\t: GENERATE_TEXT_SYSTEM_PROMPT,\n\t\t);\n\n\t\tconst finalContent = content.endsWith(\"\\n\\n\") ? content : `${content}\\n\\n`;\n\t\tif (finalContent !== content) {\n\t\t\tyield { event: \"text_chunk\", data: { delta: \"\\n\\n\" } };\n\t\t}\n\n\t\treturn {\n\t\t\tblockId: block.blockId,\n\t\t\ttype: block.type,\n\t\t\tcontent: finalContent,\n\t\t};\n\t}\n\n\tprivate async *renderDeterministicTableBlock(\n\t\tblock: WorkflowTableBlock,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t): AsyncGenerator<\n\t\tStreamEvent,\n\t\tReturnType<WorkflowTableService[\"renderTable\"]>,\n\t\tunknown\n\t> {\n\t\tconst model = this.modelModule.getModel();\n\t\tconst modelOptions = this.modelModule.getModelOptions();\n\t\tconst sourceResults = this.getSourceTaskResults(block, taskResults);\n\t\tconst messages = model.generateMessages({\n\t\t\tquery: this.workflowTableService.buildExtractionPrompt(\n\t\t\t\tblock,\n\t\t\t\tserializeTaskResults(sourceResults),\n\t\t\t),\n\t\t\tsystemPrompt:\n\t\t\t\t\"Extract only the requested table source values as valid JSON. Return only JSON.\",\n\t\t});\n\t\tconst response = await model.fetch(messages, modelOptions);\n\t\tconst rawContent = response.content || \"{}\";\n\t\tconst rendered = this.workflowTableService.renderTable(block, rawContent);\n\t\tyield { event: \"text_chunk\", data: { delta: rendered.content } };\n\t\treturn rendered;\n\t}\n\n\tprivate async *renderDeterministicGraphBlock(\n\t\tblock: WorkflowGraphBlock,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t): AsyncGenerator<\n\t\tStreamEvent,\n\t\tReturnType<WorkflowGraphService[\"renderGraph\"]>,\n\t\tunknown\n\t> {\n\t\tconst model = this.modelModule.getModel();\n\t\tconst modelOptions = this.modelModule.getModelOptions();\n\t\tconst sourceResults = this.getSourceTaskResults(block, taskResults);\n\t\tconst sourceBlocks = this.getSourceRenderedBlocks(block, renderedBlocks);\n\t\tconst messages = model.generateMessages({\n\t\t\tquery: this.workflowGraphService.buildExtractionPrompt(\n\t\t\t\tblock,\n\t\t\t\tserializeTaskResults(sourceResults),\n\t\t\t\tthis.serializeRenderedBlocks(sourceBlocks),\n\t\t\t),\n\t\t\tsystemPrompt:\n\t\t\t\t\"Extract only the requested graph source values as valid JSON. Return only JSON.\",\n\t\t});\n\t\tconst response = await model.fetch(messages, modelOptions);\n\t\tconst rawContent = response.content || \"{}\";\n\t\tconst rendered = this.workflowGraphService.renderGraph(block, rawContent);\n\t\tyield { event: \"text_chunk\", data: { delta: rendered.content } };\n\t\treturn rendered;\n\t}\n\n\tprivate async *renderGeneratedTextBlock(\n\t\tblock: WorkflowTextBlock,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t\tsystemPrompt: string,\n\t): AsyncGenerator<StreamEvent, string, unknown> {\n\t\tconst model = this.modelModule.getModel();\n\t\tconst modelOptions = this.modelModule.getModelOptions();\n\t\tconst sourceResults = this.getSourceTaskResults(block, taskResults);\n\t\tconst sourceBlocks = this.getSourceRenderedBlocks(block, renderedBlocks);\n\t\tconst messages = model.generateMessages({\n\t\t\tquery: this.buildBlockPrompt(block, sourceResults, sourceBlocks),\n\t\t\tsystemPrompt,\n\t\t});\n\t\tconst stream = await model.fetchStreamWithContextMessage(\n\t\t\tmessages,\n\t\t\t[],\n\t\t\tmodelOptions,\n\t\t);\n\n\t\tlet content = \"\";\n\t\tfor await (const chunk of stream) {\n\t\t\tif (chunk.delta?.content) {\n\t\t\t\tcontent += chunk.delta.content;\n\t\t\t\tyield {\n\t\t\t\t\tevent: \"text_chunk\",\n\t\t\t\t\tdata: { delta: chunk.delta.content },\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\treturn content;\n\t}\n\n\tprivate getSourceTaskResults(\n\t\tblock: Exclude<WorkflowResponseBlock, { type: \"heading\" }>,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t): WorkflowTaskResult[] {\n\t\tif (!block.sourceTaskIds || block.sourceTaskIds.length === 0) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn block.sourceTaskIds\n\t\t\t.map((taskId) => taskResults[taskId])\n\t\t\t.filter((result): result is WorkflowTaskResult => Boolean(result));\n\t}\n\n\tprivate buildBlockPrompt(\n\t\tblock: WorkflowTextBlock,\n\t\ttaskResults: WorkflowTaskResult[],\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t): string {\n\t\tconst present = block.mode === \"present\";\n\t\tconst sections: string[] = [];\n\n\t\tif (taskResults.length > 0) {\n\t\t\tsections.push(\n\t\t\t\tpresent\n\t\t\t\t\t? `Source content (task results):\\n${serializeTaskResults(taskResults)}`\n\t\t\t\t\t: `Task results (reference context — do not restate):\\n${serializeTaskResults(taskResults)}`,\n\t\t\t);\n\t\t}\n\n\t\tconst blocksText = this.serializeRenderedBlocks(renderedBlocks);\n\t\tif (blocksText) {\n\t\t\tsections.push(\n\t\t\t\tpresent\n\t\t\t\t\t? `Source content (rendered blocks):\\n${blocksText}`\n\t\t\t\t\t: `Already-rendered blocks (reference context — do not repeat):\\n${blocksText}`,\n\t\t\t);\n\t\t}\n\n\t\tsections.push(\n\t\t\tpresent\n\t\t\t\t? `Instructions:\\n${block.prompt}`\n\t\t\t\t: `Instructions:\\n${block.prompt}\\n\\nOutput only the new text described above. Do not include any reference context.`,\n\t\t);\n\n\t\treturn sections.join(\"\\n\\n\");\n\t}\n\n\tprivate hasPresentableSource(\n\t\tblock: WorkflowTextBlock,\n\t\ttaskResults: Record<string, WorkflowTaskResult>,\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t): boolean {\n\t\tconst results = this.getSourceTaskResults(block, taskResults);\n\t\tif (results.some((result) => result.content?.trim())) {\n\t\t\treturn true;\n\t\t}\n\t\tconst blocksText = this.serializeRenderedBlocks(\n\t\t\tthis.getSourceRenderedBlocks(block, renderedBlocks),\n\t\t);\n\t\treturn blocksText.length > 0;\n\t}\n\n\tprivate getSourceRenderedBlocks(\n\t\tblock: WorkflowTextBlock | WorkflowGraphBlock,\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t): WorkflowRenderedBlock[] {\n\t\tif (!block.sourceBlockIds || block.sourceBlockIds.length === 0) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst sourceBlockIds = new Set(block.sourceBlockIds);\n\t\treturn renderedBlocks.filter((renderedBlock) =>\n\t\t\tsourceBlockIds.has(renderedBlock.blockId),\n\t\t);\n\t}\n\n\tprivate serializeRenderedBlocks(\n\t\trenderedBlocks: WorkflowRenderedBlock[],\n\t): string {\n\t\treturn renderedBlocks\n\t\t\t.filter((block) => block.type === \"table\" || block.type === \"graph\")\n\t\t\t.map((block) => {\n\t\t\t\tconst dataText = block.data\n\t\t\t\t\t? `\\nStructured data:\\n${JSON.stringify(block.data, null, 2)}`\n\t\t\t\t\t: \"\";\n\t\t\t\treturn `[${block.blockId}] ${block.type}\\nContent:\\n${block.content.trim()}${dataText}`;\n\t\t\t})\n\t\t\t.join(\"\\n\\n---\\n\\n\");\n\t}\n}\n"]}