from __future__ import annotations

import asyncio

from app.modules.agent.orchestration.messaging.client_message_publisher import ClientMessagePublisher


def build_progress_callback(loop: asyncio.AbstractEventLoop, publisher: ClientMessagePublisher, request_id: str):
    def progress_cb(stage: str, message: str, kind: str = "task_progress", meta: dict | None = None) -> None:
        payload = dict(meta or {})
        payload.setdefault("kind", kind)
        asyncio.run_coroutine_threadsafe(
            publisher.publish_status(request_id, stage, message, payload),
            loop,
        )

    return progress_cb
