refactor(gateway): reuse looks_like_telegram_private_chat_id helper

The handoff seed path inlined its own int(chat_id) > 0 private-chat
check; delivery.py already had the identical heuristic. Promote it to
a public name and reuse it from both sites instead of duplicating.
This commit is contained in:
teknium1 2026-07-01 00:38:10 -07:00 committed by Teknium
parent 8341b72122
commit 8d3c450126
3 changed files with 17 additions and 12 deletions

View file

@ -1245,13 +1245,13 @@ def _deliver_result(job: dict, content: str, adapters=None, loop=None) -> Option
DeliveryRouter,
DeliveryTarget,
_looks_like_int,
_looks_like_telegram_private_chat_id,
looks_like_telegram_private_chat_id,
)
is_private_dm_topic = (
platform == Platform.TELEGRAM
and thread_id is not None
and _looks_like_telegram_private_chat_id(str(chat_id))
and looks_like_telegram_private_chat_id(str(chat_id))
and _looks_like_int(str(thread_id))
)
if is_private_dm_topic:

View file

@ -59,7 +59,14 @@ from .session import SessionSource
from .dead_targets import DeadTargetRegistry
def _looks_like_telegram_private_chat_id(chat_id: Optional[str]) -> bool:
def looks_like_telegram_private_chat_id(chat_id: Optional[str]) -> bool:
"""True when ``chat_id`` is a positive int — Telegram's private-chat shape.
Telegram private chats use positive chat IDs; groups/channels/supergroups
use negative IDs. This is the single source of truth for that heuristic,
reused by the handoff seed path in ``gateway/run.py`` so handoff-created
DM topics key the same way as inbound DM-topic messages.
"""
if chat_id is None:
return False
try:
@ -467,7 +474,7 @@ class DeliveryRouter:
target_thread_id = target.thread_id
is_named_telegram_private_topic = (
target.platform == Platform.TELEGRAM
and _looks_like_telegram_private_chat_id(target.chat_id)
and looks_like_telegram_private_chat_id(target.chat_id)
and not _looks_like_int(target_thread_id)
and "thread_id" not in send_metadata
and "message_thread_id" not in send_metadata
@ -490,7 +497,7 @@ class DeliveryRouter:
send_metadata["telegram_dm_topic_created_for_send"] = True
elif (
target.platform == Platform.TELEGRAM
and _looks_like_telegram_private_chat_id(target.chat_id)
and looks_like_telegram_private_chat_id(target.chat_id)
and "thread_id" not in send_metadata
and "message_thread_id" not in send_metadata
and not has_explicit_direct_topic

View file

@ -1676,7 +1676,7 @@ from gateway.session import (
build_session_key,
is_shared_multi_user_session,
)
from gateway.delivery import DeliveryRouter
from gateway.delivery import DeliveryRouter, looks_like_telegram_private_chat_id
from gateway.authz_mixin import GatewayAuthorizationMixin
from gateway.kanban_watchers import GatewayKanbanWatchersMixin
from gateway.slash_commands import GatewaySlashCommandsMixin
@ -6823,12 +6823,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
# handoff turn binds a generic `thread` session key while real replies
# arrive on a `dm` session key.
home_chat_id = str(home.chat_id)
is_telegram_private_chat = False
if platform == Platform.TELEGRAM:
try:
is_telegram_private_chat = int(home_chat_id) > 0
except (TypeError, ValueError):
is_telegram_private_chat = False
is_telegram_private_chat = (
platform == Platform.TELEGRAM
and looks_like_telegram_private_chat_id(home_chat_id)
)
if new_thread_id and not is_telegram_private_chat:
dest_chat_type = "thread"