import logging from typing import Any, Dict, Optional from sqlmodel import Session from services.topic_service import _has_topic_mcp_server, _topic_publish_internal from .publisher import build_topic_publish_payload def publish_runtime_topic_packet( engine: Any, bot_id: str, packet: Dict[str, Any], source_channel: str, persisted_message_id: Optional[int], logger: logging.Logger, ) -> None: packet_type = str(packet.get("type") or "").strip().upper() if packet_type not in {"ASSISTANT_MESSAGE", "BUS_EVENT"} or not persisted_message_id: return if not _has_topic_mcp_server(bot_id): return topic_payload = build_topic_publish_payload( bot_id, {**packet, "channel": source_channel}, persisted_message_id, ) if not topic_payload: return try: with Session(engine) as session: _topic_publish_internal(session, bot_id, topic_payload) except Exception: logger.exception("topic auto publish failed for bot %s packet %s", bot_id, packet_type)