3030 AgentLoggingCallback ,
3131 RagPipeline ,
3232)
33- from cairo_coder .core .types import Message , Role
33+ from cairo_coder .core .types import Message , Role , StreamEventType
3434from cairo_coder .dspy .document_retriever import SourceFilteredPgVectorRM
3535from cairo_coder .dspy .suggestion_program import SuggestionGeneration
3636from cairo_coder .utils .logging import setup_logging
@@ -401,7 +401,7 @@ async def _handle_chat_completion(
401401 ) from e
402402
403403 async def _stream_chat_completion (
404- self , agent , query : str , history : list [Message ], mcp_mode : bool
404+ self , agent : RagPipeline , query : str , history : list [Message ], mcp_mode : bool
405405 ) -> AsyncGenerator [str , None ]:
406406 """Stream chat completion response - replicates TypeScript streaming."""
407407 response_id = str (uuid .uuid4 ())
@@ -425,14 +425,14 @@ async def _stream_chat_completion(
425425 async for event in agent .aforward_streaming (
426426 query = query , chat_history = history , mcp_mode = mcp_mode
427427 ):
428- if event .type == "sources" :
428+ if event .type == StreamEventType . SOURCES :
429429 # Emit sources event for clients to display
430430 sources_chunk = {
431431 "type" : "sources" ,
432432 "data" : event .data ,
433433 }
434434 yield f"data: { json .dumps (sources_chunk )} \n \n "
435- elif event .type == "response" :
435+ elif event .type == StreamEventType . RESPONSE :
436436 content_buffer += event .data
437437
438438 # Send content chunk
@@ -446,7 +446,14 @@ async def _stream_chat_completion(
446446 ],
447447 }
448448 yield f"data: { json .dumps (chunk )} \n \n "
449- elif event .type == "error" :
449+ elif event .type == StreamEventType .FINAL_RESPONSE :
450+ # Emit an explicit final response event for clients
451+ final_event = {
452+ "type" : "final_response" ,
453+ "data" : event .data ,
454+ }
455+ yield f"data: { json .dumps (final_event )} \n \n "
456+ elif event .type == StreamEventType .ERROR :
450457 # Emit an error as a final delta and stop
451458 error_chunk = {
452459 "id" : response_id ,
@@ -463,7 +470,7 @@ async def _stream_chat_completion(
463470 }
464471 yield f"data: { json .dumps (error_chunk )} \n \n "
465472 break
466- elif event .type == "end" :
473+ elif event .type == StreamEventType . END :
467474 break
468475 rt .end (outputs = {"output" : content_buffer })
469476
0 commit comments