Skip to content

Your lightweight open-source personal agent.

feat: Add sender_id to LLM runtime context

yorkhellenc4170fa9bab1
diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py
index 22cb14b57..ccd1b882e 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 e463ff373..d9dd4585c 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 756449cbd..85cc5ab4a 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,