refactor(loop): extract _assemble_outbound

This commit is contained in:
chengyongru 2026-05-09 15:31:28 +08:00 committed by Xubin Ren
parent 0124c94d19
commit 88cf8db164

View File

@ -1289,12 +1289,28 @@ class AgentLoop:
) )
) )
# When follow-up messages were injected mid-turn, a later natural return self._assemble_outbound(
# language reply may address those follow-ups and should not be msg,
# suppressed just because MessageTool was used earlier in the turn. final_content,
# However, if the turn falls back to the empty-final-response all_msgs,
# placeholder, suppress it when the real user-visible output already stop_reason,
# came from MessageTool. had_injections,
generated_media,
on_stream,
)
def _assemble_outbound(
self,
msg: InboundMessage,
final_content: str,
all_msgs: list[dict[str, Any]],
stop_reason: str,
had_injections: bool,
generated_media: list[str],
on_stream: Callable | None,
) -> OutboundMessage | None:
"""Assemble the final outbound message from turn results."""
# MessageTool suppression
if (mt := self.tools.get("message")) and isinstance(mt, MessageTool) and mt._sent_in_turn: if (mt := self.tools.get("message")) and isinstance(mt, MessageTool) and mt._sent_in_turn:
if not had_injections or stop_reason == "empty_final_response": if not had_injections or stop_reason == "empty_final_response":
return None return None
@ -1303,17 +1319,18 @@ class AgentLoop:
logger.info("Response to {}:{}: {}", msg.channel, msg.sender_id, preview) logger.info("Response to {}:{}: {}", msg.channel, msg.sender_id, preview)
meta = dict(msg.metadata or {}) meta = dict(msg.metadata or {})
final_content, buttons = ask_user_outbound( content, buttons = ask_user_outbound(
final_content, final_content,
ask_user_options_from_messages(all_msgs) if stop_reason == "ask_user" else [], ask_user_options_from_messages(all_msgs) if stop_reason == "ask_user" else [],
msg.channel, msg.channel,
) )
if on_stream is not None and stop_reason not in {"ask_user", "error", "tool_error"}: if on_stream is not None and stop_reason not in {"ask_user", "error", "tool_error"}:
meta["_streamed"] = True meta["_streamed"] = True
return OutboundMessage( return OutboundMessage(
channel=msg.channel, channel=msg.channel,
chat_id=msg.chat_id, chat_id=msg.chat_id,
content=final_content, content=content,
media=generated_media, media=generated_media,
metadata=meta, metadata=meta,
buttons=buttons, buttons=buttons,