fix:set subagent result message role = assistant

This commit is contained in:
zhangxiaoyu.york
2026-03-16 23:39:03 +08:00
committed by Xubin Ren
parent 20e3eb8fce
commit f72ceb7a3c
2 changed files with 5 additions and 1 deletions

View File

@@ -125,6 +125,7 @@ Reply directly with text for conversations. Only use the 'message' tool to send
media: list[str] | None = None, media: list[str] | None = None,
channel: str | None = None, channel: str | None = None,
chat_id: str | None = None, chat_id: str | None = None,
current_role: str = "user",
) -> 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) runtime_ctx = self._build_runtime_context(channel, chat_id)
@@ -140,7 +141,7 @@ Reply directly with text for conversations. Only use the 'message' tool to send
return [ return [
{"role": "system", "content": self.build_system_prompt(skill_names)}, {"role": "system", "content": self.build_system_prompt(skill_names)},
*history, *history,
{"role": "user", "content": merged}, {"role": current_role, "content": merged},
] ]
def _build_user_content(self, text: str, media: list[str] | None) -> str | list[dict[str, Any]]: def _build_user_content(self, text: str, media: list[str] | None) -> str | list[dict[str, Any]]:

View File

@@ -370,9 +370,12 @@ class AgentLoop:
await self.memory_consolidator.maybe_consolidate_by_tokens(session) await self.memory_consolidator.maybe_consolidate_by_tokens(session)
self._set_tool_context(channel, chat_id, msg.metadata.get("message_id")) self._set_tool_context(channel, chat_id, msg.metadata.get("message_id"))
history = session.get_history(max_messages=0) history = session.get_history(max_messages=0)
# Subagent results should be assistant role, other system messages use user role
current_role = "assistant" if msg.sender_id == "subagent" else "user"
messages = self.context.build_messages( messages = self.context.build_messages(
history=history, history=history,
current_message=msg.content, channel=channel, chat_id=chat_id, current_message=msg.content, channel=channel, chat_id=chat_id,
current_role=current_role,
) )
final_content, _, all_msgs = await self._run_agent_loop(messages) final_content, _, all_msgs = await self._run_agent_loop(messages)
self._save_turn(session, all_msgs, 1 + len(history)) self._save_turn(session, all_msgs, 1 + len(history))