from __future__ import annotations

from app.modules.agent.orchestration.context.execution_context import ExecutionContext
from app.modules.agent.orchestration.processes.v1.simple_llm_workflow import SimpleLlmWorkflow


class ExecuteLlmWorkflowStep:
    def __init__(self, workflow: SimpleLlmWorkflow) -> None:
        self._workflow = workflow

    async def run(self, context: ExecutionContext) -> None:
        request = context.request
        await context.publisher.publish_status(
            request.request_id,
            "task_workflow",
            f"Запускаю workflow {self._workflow.workflow_id}.",
        )
        await context.publisher.publish_status(
            request.request_id,
            "prompt_builder",
            "Формирую prompt payload для LLM.",
        )
        result = await self._workflow.run(context)
        request.answer = str(result.get("answer") or "")
        context.workflow_result = result
        await context.publisher.publish_status(
            request.request_id,
            "llm_process",
            "Ответ от LLM получен.",
            {
                "workflow_id": result.get("workflow_id"),
                "prompt_name": result.get("prompt_name"),
                "answer_length": len(request.answer),
            },
        )
