from __future__ import annotations

from app.modules.agent.orchestration.adapters.llm_chat_adapter import LlmChatAdapter
from app.modules.agent.orchestration.context.execution_context import ExecutionContext


class RunLlmStep:
    def __init__(self, llm: LlmChatAdapter) -> None:
        self._llm = llm

    async def run(self, context: ExecutionContext) -> None:
        request = context.request
        context.trace_logger.log_step(request.request_id, "run_llm", "started")
        await context.publisher.publish_status(
            request.request_id,
            "llm_process",
            "Отправляю запрос пользователя в LLM.",
        )
        answer = await self._llm.generate(request.message, request.request_id)
        request.answer = answer
        await context.publisher.publish_status(
            request.request_id,
            "llm_process",
            "Ответ от LLM получен.",
        )
        context.trace_logger.log_step(request.request_id, "run_llm", "completed", {"answer_length": len(answer)})
