From a69485c288f4a7368458812df844286dc21a2290 Mon Sep 17 00:00:00 2001 From: harshalmore31 Date: Sat, 5 Jul 2025 00:33:19 +0530 Subject: [PATCH] enhance streaming response handling by collecting chunks for detailed conversation history; add callback for real-time chunk logging --- swarms/structs/agent.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index c243d412..030517f1 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -2562,14 +2562,28 @@ class Agent: chunks.append(content) complete_response = "".join(chunks) else: - # Use the streaming panel to display the response + # Collect chunks for conversation saving + collected_chunks = [] + + def on_chunk_received(chunk: str): + """Callback to collect chunks as they arrive""" + collected_chunks.append(chunk) + # Optional: Save each chunk to conversation in real-time + # This creates a more detailed conversation history + if self.verbose: + logger.debug( + f"Streaming chunk received: {chunk[:50]}..." + ) + + # Use the streaming panel to display and collect the response complete_response = formatter.print_streaming_panel( streaming_response, title=f"🤖 Agent: {self.agent_name} Loops: {current_loop}", - style=None, + style=None, # Use random color like non-streaming approach collect_chunks=True, + on_chunk_callback=on_chunk_received, ) - + # Restore original stream setting self.llm.stream = original_stream