from __future__ import annotations

from app.modules.agent.orchestration.adapters.task_runtime_adapter import TaskRuntimeAdapter
from app.modules.agent.orchestration.context.execution_context import ExecutionContext


class RunTaskWorkflowStep:
    def __init__(self, runtime: TaskRuntimeAdapter) -> None:
        self._runtime = runtime

    async def run(self, context: ExecutionContext) -> None:
        request = context.request
        context.trace_logger.log_step(request.request_id, "task_runtime", "started")
        await context.publisher.publish_status(
            request.request_id,
            "orchestrator",
            "Запускаю pipeline v2: intent router, rag retrieval и task workflow.",
            {"process_version": request.process_version},
        )
        result = await self._runtime.run(context)
        request.answer = result.answer or ""
        context.trace_logger.log_step(
            request.request_id,
            "task_runtime",
            "completed",
            {
                "result_type": getattr(result.result_type, "value", str(result.result_type)),
                "meta": getattr(result, "meta", {}) or {},
            },
        )
