mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-22 05:22:09 +00:00
fix(telegram): preserve DM topic routing via reply fallback
This commit is contained in:
parent
28b5bd7e93
commit
b3239572f0
6 changed files with 1331 additions and 152 deletions
|
|
@ -40,6 +40,52 @@ def _platform_name(platform) -> str:
|
|||
return str(value or "").lower()
|
||||
|
||||
|
||||
def _thread_metadata_for_source(source, reply_to_message_id: str | None = None) -> dict | None:
|
||||
"""Build platform-aware thread metadata for adapter sends.
|
||||
|
||||
Most platforms route threaded sends with a generic ``thread_id`` metadata
|
||||
value. Telegram private-chat topics created through Hermes' DM-topic helper
|
||||
are exposed in updates as ``message_thread_id`` plus a reply anchor, but
|
||||
outbound sends only render in the correct Telegram lane when the adapter
|
||||
supplies both ``message_thread_id`` and ``reply_to_message_id``. Mark those
|
||||
lanes so the Telegram adapter can avoid the known-bad partial routes.
|
||||
"""
|
||||
thread_id = getattr(source, "thread_id", None)
|
||||
if thread_id is None:
|
||||
return None
|
||||
metadata = {"thread_id": thread_id}
|
||||
if _platform_name(getattr(source, "platform", None)) == "telegram" and getattr(source, "chat_type", None) == "dm":
|
||||
metadata["telegram_dm_topic_reply_fallback"] = True
|
||||
anchor = reply_to_message_id or getattr(source, "message_id", None)
|
||||
if anchor is not None:
|
||||
metadata["telegram_reply_to_message_id"] = str(anchor)
|
||||
return metadata
|
||||
|
||||
|
||||
def _reply_anchor_for_event(event) -> str | None:
|
||||
"""Return reply_to id for platforms that need reply semantics.
|
||||
|
||||
Telegram forum/supergroup topics should be routed by topic metadata, not by
|
||||
replying to the triggering message. Hermes-created Telegram private-chat
|
||||
topic lanes are different: Bot API sends reject their ``message_thread_id``
|
||||
and do not route with ``direct_messages_topic_id``. Those lanes only remain
|
||||
visible when sent with both the private topic thread id and a reply to the
|
||||
triggering user message.
|
||||
"""
|
||||
source = getattr(event, "source", None)
|
||||
platform = _platform_name(getattr(source, "platform", None))
|
||||
thread_id = getattr(source, "thread_id", None)
|
||||
if platform == "telegram" and thread_id and getattr(source, "chat_type", None) == "dm":
|
||||
# Reply to the triggering user message. Replying to Telegram's earlier
|
||||
# topic seed/anchor can render the bot response outside the active lane.
|
||||
return getattr(event, "message_id", None) or getattr(event, "reply_to_message_id", None)
|
||||
if platform == "telegram" and thread_id:
|
||||
return None
|
||||
if platform == "feishu" and thread_id and getattr(event, "reply_to_message_id", None):
|
||||
return getattr(event, "reply_to_message_id", None)
|
||||
return getattr(event, "message_id", None)
|
||||
|
||||
|
||||
def should_send_media_as_audio(platform, ext: str, is_voice: bool = False) -> bool:
|
||||
"""Return True when a media file should use the platform's audio sender.
|
||||
|
||||
|
|
@ -1719,7 +1765,7 @@ class BasePlatformAdapter(ABC):
|
|||
"""
|
||||
# Fallback: send URL as text (subclasses override for native images)
|
||||
text = f"{caption}\n{image_url}" if caption else image_url
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to)
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
|
||||
|
||||
async def send_animation(
|
||||
self,
|
||||
|
|
@ -1798,6 +1844,7 @@ class BasePlatformAdapter(ABC):
|
|||
audio_path: str,
|
||||
caption: Optional[str] = None,
|
||||
reply_to: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
**kwargs,
|
||||
) -> SendResult:
|
||||
"""
|
||||
|
|
@ -1810,7 +1857,7 @@ class BasePlatformAdapter(ABC):
|
|||
text = f"🔊 Audio: {audio_path}"
|
||||
if caption:
|
||||
text = f"{caption}\n{text}"
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to)
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
|
||||
|
||||
async def play_tts(
|
||||
self,
|
||||
|
|
@ -1832,6 +1879,7 @@ class BasePlatformAdapter(ABC):
|
|||
video_path: str,
|
||||
caption: Optional[str] = None,
|
||||
reply_to: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
**kwargs,
|
||||
) -> SendResult:
|
||||
"""
|
||||
|
|
@ -1843,7 +1891,7 @@ class BasePlatformAdapter(ABC):
|
|||
text = f"🎬 Video: {video_path}"
|
||||
if caption:
|
||||
text = f"{caption}\n{text}"
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to)
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
|
||||
|
||||
async def send_document(
|
||||
self,
|
||||
|
|
@ -1852,6 +1900,7 @@ class BasePlatformAdapter(ABC):
|
|||
caption: Optional[str] = None,
|
||||
file_name: Optional[str] = None,
|
||||
reply_to: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
**kwargs,
|
||||
) -> SendResult:
|
||||
"""
|
||||
|
|
@ -1863,7 +1912,7 @@ class BasePlatformAdapter(ABC):
|
|||
text = f"📎 File: {file_path}"
|
||||
if caption:
|
||||
text = f"{caption}\n{text}"
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to)
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
|
||||
|
||||
async def send_image_file(
|
||||
self,
|
||||
|
|
@ -1871,6 +1920,7 @@ class BasePlatformAdapter(ABC):
|
|||
image_path: str,
|
||||
caption: Optional[str] = None,
|
||||
reply_to: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
**kwargs,
|
||||
) -> SendResult:
|
||||
"""
|
||||
|
|
@ -1883,7 +1933,7 @@ class BasePlatformAdapter(ABC):
|
|||
text = f"🖼️ Image: {image_path}"
|
||||
if caption:
|
||||
text = f"{caption}\n{text}"
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to)
|
||||
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
|
||||
|
||||
@staticmethod
|
||||
def extract_media(content: str) -> Tuple[List[Tuple[str, bool]], str]:
|
||||
|
|
@ -2558,7 +2608,7 @@ class BasePlatformAdapter(ABC):
|
|||
current_guard = self._active_sessions.get(session_key)
|
||||
command_guard = asyncio.Event()
|
||||
self._active_sessions[session_key] = command_guard
|
||||
thread_meta = {"thread_id": event.source.thread_id} if event.source.thread_id else None
|
||||
thread_meta = _thread_metadata_for_source(event.source, _reply_anchor_for_event(event))
|
||||
|
||||
try:
|
||||
response = await self._message_handler(event)
|
||||
|
|
@ -2579,13 +2629,7 @@ class BasePlatformAdapter(ABC):
|
|||
_r = await self._send_with_retry(
|
||||
chat_id=event.source.chat_id,
|
||||
content=_text,
|
||||
reply_to=(
|
||||
event.reply_to_message_id
|
||||
if event.source.platform == Platform.FEISHU
|
||||
and event.source.thread_id
|
||||
and event.reply_to_message_id
|
||||
else event.message_id
|
||||
),
|
||||
reply_to=_reply_anchor_for_event(event),
|
||||
metadata=thread_meta,
|
||||
)
|
||||
if _eph_ttl > 0 and _r.success and _r.message_id:
|
||||
|
|
@ -2678,20 +2722,14 @@ class BasePlatformAdapter(ABC):
|
|||
self.name, cmd, session_key,
|
||||
)
|
||||
try:
|
||||
_thread_meta = {"thread_id": event.source.thread_id} if event.source.thread_id else None
|
||||
_thread_meta = _thread_metadata_for_source(event.source, _reply_anchor_for_event(event))
|
||||
response = await self._message_handler(event)
|
||||
_text, _eph_ttl = self._unwrap_ephemeral(response)
|
||||
if _text:
|
||||
_r = await self._send_with_retry(
|
||||
chat_id=event.source.chat_id,
|
||||
content=_text,
|
||||
reply_to=(
|
||||
event.reply_to_message_id
|
||||
if event.source.platform == Platform.FEISHU
|
||||
and event.source.thread_id
|
||||
and event.reply_to_message_id
|
||||
else event.message_id
|
||||
),
|
||||
reply_to=_reply_anchor_for_event(event),
|
||||
metadata=_thread_meta,
|
||||
)
|
||||
if _eph_ttl > 0 and _r.success and _r.message_id:
|
||||
|
|
@ -2783,7 +2821,7 @@ class BasePlatformAdapter(ABC):
|
|||
self._active_sessions[session_key] = interrupt_event
|
||||
|
||||
# Start continuous typing indicator (refreshes every 2 seconds)
|
||||
_thread_metadata = {"thread_id": event.source.thread_id} if event.source.thread_id else None
|
||||
_thread_metadata = _thread_metadata_for_source(event.source, _reply_anchor_for_event(event))
|
||||
_keep_typing_kwargs = {"metadata": _thread_metadata}
|
||||
try:
|
||||
_keep_typing_sig = inspect.signature(self._keep_typing)
|
||||
|
|
@ -2911,11 +2949,7 @@ class BasePlatformAdapter(ABC):
|
|||
# Send the text portion
|
||||
if text_content:
|
||||
logger.info("[%s] Sending response (%d chars) to %s", self.name, len(text_content), event.source.chat_id)
|
||||
_reply_anchor = (
|
||||
event.reply_to_message_id
|
||||
if event.source.platform == Platform.FEISHU and event.source.thread_id and event.reply_to_message_id
|
||||
else event.message_id
|
||||
)
|
||||
_reply_anchor = _reply_anchor_for_event(event)
|
||||
result = await self._send_with_retry(
|
||||
chat_id=event.source.chat_id,
|
||||
content=text_content,
|
||||
|
|
@ -3108,7 +3142,7 @@ class BasePlatformAdapter(ABC):
|
|||
try:
|
||||
error_type = type(e).__name__
|
||||
error_detail = str(e)[:300] if str(e) else "no details available"
|
||||
_thread_metadata = {"thread_id": event.source.thread_id} if event.source.thread_id else None
|
||||
_thread_metadata = _thread_metadata_for_source(event.source, _reply_anchor_for_event(event))
|
||||
await self.send(
|
||||
chat_id=event.source.chat_id,
|
||||
content=(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue