diff --git a/gateway/scripts/chat_bridge.py b/gateway/scripts/chat_bridge.py index 94f6658..978eea3 100644 --- a/gateway/scripts/chat_bridge.py +++ b/gateway/scripts/chat_bridge.py @@ -17,6 +17,41 @@ os.environ["NO_PROXY"] = "*" import requests _TZ = timezone(timedelta(hours=8)) +# ── Dynamic TUI session tracking ── +# When xxm is chatting with hmo in TUI, record the active session ID. +# When the bot processes a coregroup message, it injects TUI context +# so the LLM knows what hmo and xxm were discussing in the TUI. +# Auto-expires after 1 hour (matching Hermes' state_meta pattern). +_ACTIVE_SESSION_FILE = os.path.join( + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), + "temp", ".active_tui_session.json") + + +def mark_active_tui_session(session_id: str): + """Record the active TUI session ID + timestamp.""" + try: + d = os.path.dirname(_ACTIVE_SESSION_FILE) + if d: os.makedirs(d, exist_ok=True) + with open(_ACTIVE_SESSION_FILE, "w") as f: + json.dump({"session_id": session_id, "timestamp": int(time.time())}, f) + except Exception: + pass + + +def get_active_tui_session(max_age: int = 3600) -> str | None: + """Return active TUI session ID if recent enough (< 1 hour).""" + try: + if not os.path.exists(_ACTIVE_SESSION_FILE): + return None + with open(_ACTIVE_SESSION_FILE, "r") as f: + data = json.load(f) + age = time.time() - data.get("timestamp", 0) + if age < max_age: + return data.get("session_id") + except Exception: + pass + return None + # ── Logging ── _LOG_FILE = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), @@ -650,6 +685,23 @@ class SessionBridge: if recent_ctx: sys_prompt += f"\n\n最近对话:\n{recent_ctx}" + # Dynamic session switching: inject TUI context for coregroup messages + if "[群聊" in message: + tui_sid = get_active_tui_session() + if tui_sid: + try: + from session_router import extract_session_context + tui_ctx = extract_session_context(tui_sid, limit=50) + if tui_ctx: + sys_prompt += ( + "\n\n【你的 TUI 工作台最近对话】" + f"\n(老莫在 TUI 和你讨论这些时,转发了群聊消息," + f"请结合下面的 TUI 对话理解他在群里的意图。" + f"群聊和 TUI 是同一个人,两份上下文关联看)\n{tui_ctx}" + ) + except Exception: + pass + # Build messages array messages = [ {"role": "system", "content": sys_prompt}, diff --git a/gateway/scripts/session_router.py b/gateway/scripts/session_router.py index 915f686..87f6526 100644 --- a/gateway/scripts/session_router.py +++ b/gateway/scripts/session_router.py @@ -620,5 +620,16 @@ class SessionRouter: tagged = f"{prefix}{message}" history = [{"role": "user", "content": tagged}] + # Dynamic context switching: record active TUI session + # so bot can inject TUI context when replying to coregroup + if channel != "xmpp": + try: + sid = self._resolve_session(key, allow_create=False) + if sid: + from chat_bridge import mark_active_tui_session + mark_active_tui_session(sid) + except Exception: + pass + # 4. Run LLM command loop return self._llm_loop(key, history)