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")