feat: Add sender_id to LLM runtime context

This commit is contained in:
yorkhellen 2026-04-30 15:25:13 +08:00 committed by Xubin Ren
parent 1040124ede
commit c4170fa9ba
3 changed files with 8 additions and 2 deletions

View File

@ -83,12 +83,14 @@ class ContextBuilder:
@staticmethod @staticmethod
def _build_runtime_context( def _build_runtime_context(
channel: str | None, chat_id: str | None, timezone: str | None = None, 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: ) -> str:
"""Build untrusted runtime metadata block for injection before the user message.""" """Build untrusted runtime metadata block for injection before the user message."""
lines = [f"Current Time: {current_time_str(timezone)}"] lines = [f"Current Time: {current_time_str(timezone)}"]
if channel and chat_id: if channel and chat_id:
lines += [f"Channel: {channel}", f"Chat ID: {chat_id}"] lines += [f"Channel: {channel}", f"Chat ID: {chat_id}"]
if sender_id:
lines += [f"Sender ID: {sender_id}"]
if session_summary: if session_summary:
lines += ["", "[Resumed Session]", session_summary] lines += ["", "[Resumed Session]", session_summary]
return ContextBuilder._RUNTIME_CONTEXT_TAG + "\n" + "\n".join(lines) + "\n" + ContextBuilder._RUNTIME_CONTEXT_END 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, chat_id: str | None = None,
current_role: str = "user", current_role: str = "user",
session_summary: str | None = None, session_summary: str | None = None,
sender_id: str | None = None,
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""Build the complete message list for an LLM call.""" """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) user_content = self._build_user_content(current_message, media)
# Merge runtime context and user content into a single user message # Merge runtime context and user content into a single user message

View File

@ -929,6 +929,7 @@ class AgentLoop:
chat_id=chat_id, chat_id=chat_id,
session_summary=pending, session_summary=pending,
current_role=current_role, current_role=current_role,
sender_id=msg.sender_id,
) )
final_content, _, all_msgs, stop_reason, _ = await self._run_agent_loop( final_content, _, all_msgs, stop_reason, _ = await self._run_agent_loop(
messages, session=session, channel=channel, chat_id=chat_id, messages, session=session, channel=channel, chat_id=chat_id,
@ -1024,6 +1025,7 @@ class AgentLoop:
media=msg.media if msg.media else None, media=msg.media if msg.media else None,
channel=msg.channel, channel=msg.channel,
chat_id=self._runtime_chat_id(msg), chat_id=self._runtime_chat_id(msg),
sender_id=msg.sender_id,
) )
async def _bus_progress( async def _bus_progress(

View File

@ -518,6 +518,7 @@ class Consolidator:
channel=channel, channel=channel,
chat_id=chat_id, chat_id=chat_id,
session_summary=session_summary, session_summary=session_summary,
sender_id=None,
) )
return estimate_prompt_tokens_chain( return estimate_prompt_tokens_chain(
self.provider, self.provider,