From ce52070fcf5642b3143d66f257fd8af0c660e314 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Sat, 9 May 2026 15:28:28 +0800 Subject: [PATCH] refactor(loop): extract _persist_user_message_early --- nanobot/agent/loop.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 8943a06dd..69fb23c40 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -610,6 +610,27 @@ class AgentLoop: return _on_retry_wait + def _persist_user_message_early( + self, + msg: InboundMessage, + session: Session, + pending_ask_id: str | None, + ) -> bool: + """Persist the triggering user message before the turn starts. + + Returns True if the message was persisted. + """ + media_paths = [p for p in (msg.media or []) if isinstance(p, str) and p] + has_text = isinstance(msg.content, str) and msg.content.strip() + if not pending_ask_id and (has_text or media_paths): + extra: dict[str, Any] = {"media": list(media_paths)} if media_paths else {} + text = msg.content if isinstance(msg.content, str) else "" + session.add_message("user", text, **extra) + self._mark_pending_user_turn(session) + self.sessions.save(session) + return True + return False + async def _dispatch_command_inline( self, msg: InboundMessage, @@ -1217,16 +1238,7 @@ class AgentLoop: # doesn't silently lose the prompt on recovery. ``media`` rides along # as raw on-disk paths — sanitized image blocks are stripped from # JSONL, and webui replay needs the paths to mint signed URLs. - user_persisted_early = False - media_paths = [p for p in (msg.media or []) if isinstance(p, str) and p] - has_text = isinstance(msg.content, str) and msg.content.strip() - if not pending_ask_id and (has_text or media_paths): - extra: dict[str, Any] = {"media": list(media_paths)} if media_paths else {} - text = msg.content if isinstance(msg.content, str) else "" - session.add_message("user", text, **extra) - self._mark_pending_user_turn(session) - self.sessions.save(session) - user_persisted_early = True + user_persisted_early = self._persist_user_message_early(msg, session, pending_ask_id) final_content, _, all_msgs, stop_reason, had_injections = await self._run_agent_loop( initial_messages,