From 5f354ef6ac7d74f65b08682cf259576c2ba2980a Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Tue, 7 Apr 2026 18:06:40 +0200 Subject: [PATCH] fix: capture agent text from on_chat_model_end for non-streaming LLM calls --- .../app/agents/autocomplete/autocomplete_agent.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/surfsense_backend/app/agents/autocomplete/autocomplete_agent.py b/surfsense_backend/app/agents/autocomplete/autocomplete_agent.py index 36b5bc086..84f6a064d 100644 --- a/surfsense_backend/app/agents/autocomplete/autocomplete_agent.py +++ b/surfsense_backend/app/agents/autocomplete/autocomplete_agent.py @@ -275,7 +275,7 @@ async def create_autocomplete_agent( # --------------------------------------------------------------------------- -# JSON suggestion parsing (robust fallback) +# JSON suggestion parsing (with fallback) # --------------------------------------------------------------------------- @@ -383,7 +383,6 @@ async def stream_autocomplete_agent( input_data, config=config, version="v2" ): event_type = event.get("event", "") - if event_type == "on_chat_model_stream": if active_tool_depth > 0: continue @@ -395,6 +394,17 @@ async def stream_autocomplete_agent( if content and isinstance(content, str): text_buffer.append(content) + elif event_type == "on_chat_model_end": + if active_tool_depth > 0: + continue + if "surfsense:internal" in event.get("tags", []): + continue + output = event.get("data", {}).get("output") + if output and hasattr(output, "content"): + content = output.content + if content and isinstance(content, str) and not text_buffer: + text_buffer.append(content) + elif event_type == "on_tool_start": active_tool_depth += 1 tool_name = event.get("name", "unknown_tool")