diff --git a/backend/agentpress/processor/llm_response_processor.py b/backend/agentpress/processor/llm_response_processor.py index 0f879f1f..21985a79 100644 --- a/backend/agentpress/processor/llm_response_processor.py +++ b/backend/agentpress/processor/llm_response_processor.py @@ -133,19 +133,11 @@ class LLMResponseProcessor: if self.tool_calls_accumulated: message["tool_calls"] = self.tool_calls_accumulated - if not hasattr(self, '_message_added'): - await self.results_adder.add_initial_response( - self.thread_id, - self.content_buffer, - self.tool_calls_accumulated - ) - self._message_added = True - else: - await self.results_adder.update_response( - self.thread_id, - self.content_buffer, - self.tool_calls_accumulated - ) + await self.results_adder.update_response( + self.thread_id, + self.content_buffer, + self.tool_calls_accumulated + ) # Handle stream completion if chunk.choices[0].finish_reason: diff --git a/backend/agentpress/processor/xml/xml_results_adder.py b/backend/agentpress/processor/xml/xml_results_adder.py index c8835f87..b41ec01c 100644 --- a/backend/agentpress/processor/xml/xml_results_adder.py +++ b/backend/agentpress/processor/xml/xml_results_adder.py @@ -55,15 +55,23 @@ class XMLResultsAdder(ResultsAdderBase): - Creates initial message if none exists - Preserves XML structure in updates """ - if not self.message_added: - await self.add_initial_response(thread_id, content, tool_calls) - return - message = { "role": "assistant", "content": content } - await self.update_message(thread_id, message) + + # Check if an assistant message already exists in the thread + existing_messages = await self.get_messages(thread_id) + assistant_msg = next((msg for msg in reversed(existing_messages) + if msg['role'] == 'assistant'), None) + + if assistant_msg: + # Update existing message + await self.update_message(thread_id, message) + else: + # Add new message + await self.add_message(thread_id, message) + self.message_added = True async def add_tool_result(self, thread_id: str, result: Dict[str, Any]): """Add XML tool result with proper referencing.