diff --git a/api/core/agent/cot_agent_runner.py b/api/core/agent/cot_agent_runner.py index 141ba2c252d95d..20d924ddc708b4 100644 --- a/api/core/agent/cot_agent_runner.py +++ b/api/core/agent/cot_agent_runner.py @@ -379,7 +379,7 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom organize historic prompt messages """ result: list[PromptMessage] = [] - scratchpad: list[AgentScratchpadUnit] = [] + scratchpads: list[AgentScratchpadUnit] = [] current_scratchpad: AgentScratchpadUnit = None self.history_prompt_messages = AgentHistoryPromptTransform( @@ -391,13 +391,15 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom for message in self.history_prompt_messages: if isinstance(message, AssistantPromptMessage): - current_scratchpad = AgentScratchpadUnit( - agent_response=message.content, - thought=message.content or 'I am thinking about how to help you', - action_str='', - action=None, - observation=None, - ) + if not current_scratchpad: + current_scratchpad = AgentScratchpadUnit( + agent_response=message.content, + thought=message.content or 'I am thinking about how to help you', + action_str='', + action=None, + observation=None, + ) + scratchpads.append(current_scratchpad) if message.tool_calls: try: current_scratchpad.action = AgentScratchpadUnit.Action( @@ -409,24 +411,23 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom ) except: pass - - scratchpad.append(current_scratchpad) elif isinstance(message, ToolPromptMessage): if current_scratchpad: current_scratchpad.observation = message.content elif isinstance(message, UserPromptMessage): - result.append(message) - - if scratchpad: + if scratchpads: result.append(AssistantPromptMessage( - content=self._format_assistant_message(scratchpad) + content=self._format_assistant_message(scratchpads) )) + scratchpads = [] + current_scratchpad = None + + result.append(message) - scratchpad = [] - if scratchpad: + if scratchpads: result.append(AssistantPromptMessage( - content=self._format_assistant_message(scratchpad) + content=self._format_assistant_message(scratchpads) )) return result \ No newline at end of file