From 0f23b209ad5d9f6bb46d796c543b52df33606250 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Tue, 3 Mar 2026 11:38:14 +0800 Subject: [PATCH] fix: adjust the context of restart loading --- agent/chat/service.py | 20 +++++++++++++++++++- bridge/agent_initializer.py | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/agent/chat/service.py b/agent/chat/service.py index b0d49fb..ebb1a6d 100644 --- a/agent/chat/service.py +++ b/agent/chat/service.py @@ -68,9 +68,24 @@ class ChatService: # a new segment; collect tool results until turn_end. state.pending_tool_results = [] - elif event_type == "tool_execution_end": + elif event_type == "tool_execution_start": + # Notify the client that a tool is about to run (with its input args) tool_name = data.get("tool_name", "") arguments = data.get("arguments", {}) + # Cache arguments keyed by tool_call_id so tool_execution_end can include them + tool_call_id = data.get("tool_call_id", tool_name) + state.pending_tool_arguments[tool_call_id] = arguments + send_chunk_fn({ + "chunk_type": "tool_start", + "tool": tool_name, + "arguments": arguments, + }) + + elif event_type == "tool_execution_end": + tool_name = data.get("tool_name", "") + tool_call_id = data.get("tool_call_id", tool_name) + # Retrieve cached arguments from the matching tool_execution_start event + arguments = state.pending_tool_arguments.pop(tool_call_id, data.get("arguments", {})) result = data.get("result", "") status = data.get("status", "unknown") execution_time = data.get("execution_time", 0) @@ -167,3 +182,6 @@ class _StreamState: # None means we are not accumulating tool results right now. # A list means we are in the middle of a tool-execution phase. self.pending_tool_results: Optional[list] = None + # Maps tool_call_id -> arguments captured from tool_execution_start, + # so that tool_execution_end can attach the correct input args. + self.pending_tool_arguments: dict = {} diff --git a/bridge/agent_initializer.py b/bridge/agent_initializer.py index 8e5a241..32ff84c 100644 --- a/bridge/agent_initializer.py +++ b/bridge/agent_initializer.py @@ -147,7 +147,7 @@ class AgentInitializer: from agent.memory import get_conversation_store store = get_conversation_store() max_turns = conf().get("agent_max_context_turns", 20) - restore_turns = max(6, max_turns // 5) + restore_turns = max(3, max_turns // 6) saved = store.load_messages(session_id, max_turns=restore_turns) if saved: filtered = self._filter_text_only_messages(saved)