fix(heartbeat): route unified sessions to last channel

This commit is contained in:
yu-xin-c authored and Xubin Ren committed 2026-07-27 00:12:44 +08:00
1 parent be43a54570
commit a7a6c26eab
5 files changed
+137 -1

No files matched your search

+30
View File
@@ -2,7 +2,11 @@
from __future__ import annotations
from collections.abc import Mapping, MutableMapping
from typing import Any
UNIFIED_SESSION_KEY = "unified:default"
LAST_CHANNEL_METADATA_KEY = "last_channel"
def session_key_for_channel(channel: str, chat_id: str, *, unified_session: bool = False) -> str:
@@ -10,3 +14,29 @@ def session_key_for_channel(channel: str, chat_id: str, *, unified_session: bool
if unified_session:
return UNIFIED_SESSION_KEY
return f"{channel}:{chat_id}"
def remember_last_channel(
metadata: MutableMapping[str, Any],
channel: str,
chat_id: str,
) -> None:
"""Persist the latest concrete delivery route in session metadata."""
if not channel or not chat_id:
return
metadata[LAST_CHANNEL_METADATA_KEY] = f"{channel}:{chat_id}"
def last_channel_from_metadata(
metadata: Mapping[str, Any] | None,
) -> tuple[str, str] | None:
"""Return a concrete delivery route from persisted session metadata."""
if not isinstance(metadata, Mapping):
return None
route = metadata.get(LAST_CHANNEL_METADATA_KEY)
if not isinstance(route, str) or ":" not in route:
return None
channel, chat_id = route.split(":", 1)
if not channel or not chat_id:
return None
return channel, chat_id