From c4170fa9bab120901e879af59f73511ebb2cf63d Mon Sep 17 00:00:00 2001 From: yorkhellen Date: Thu, 30 Apr 2026 15:25:13 +0800 Subject: [PATCH] feat: Add sender_id to LLM runtime context --- nanobot/agent/context.py | 7 +++++-- nanobot/agent/loop.py | 2 ++ nanobot/agent/memory.py | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index 22cb14b5..ccd1b882 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -83,12 +83,14 @@ class ContextBuilder: @staticmethod def _build_runtime_context( channel: str | None, chat_id: str | None, timezone: str | None = None, - session_summary: str | None = None, + session_summary: str | None = None, sender_id: str | None = None, ) -> str: """Build untrusted runtime metadata block for injection before the user message.""" lines = [f"Current Time: {current_time_str(timezone)}"] if channel and chat_id: lines += [f"Channel: {channel}", f"Chat ID: {chat_id}"] + if sender_id: + lines += [f"Sender ID: {sender_id}"] if session_summary: lines += ["", "[Resumed Session]", session_summary] return ContextBuilder._RUNTIME_CONTEXT_TAG + "\n" + "\n".join(lines) + "\n" + ContextBuilder._RUNTIME_CONTEXT_END @@ -138,9 +140,10 @@ class ContextBuilder: chat_id: str | None = None, current_role: str = "user", session_summary: str | None = None, + sender_id: str | None = None, ) -> list[dict[str, Any]]: """Build the complete message list for an LLM call.""" - runtime_ctx = self._build_runtime_context(channel, chat_id, self.timezone, session_summary=session_summary) + runtime_ctx = self._build_runtime_context(channel, chat_id, self.timezone, session_summary=session_summary, sender_id=sender_id) user_content = self._build_user_content(current_message, media) # Merge runtime context and user content into a single user message diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index e463ff37..d9dd4585 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -929,6 +929,7 @@ class AgentLoop: chat_id=chat_id, session_summary=pending, current_role=current_role, + sender_id=msg.sender_id, ) final_content, _, all_msgs, stop_reason, _ = await self._run_agent_loop( messages, session=session, channel=channel, chat_id=chat_id, @@ -1024,6 +1025,7 @@ class AgentLoop: media=msg.media if msg.media else None, channel=msg.channel, chat_id=self._runtime_chat_id(msg), + sender_id=msg.sender_id, ) async def _bus_progress( diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index 756449cb..85cc5ab4 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -518,6 +518,7 @@ class Consolidator: channel=channel, chat_id=chat_id, session_summary=session_summary, + sender_id=None, ) return estimate_prompt_tokens_chain( self.provider,