Fixed bug in ctrl+c handling during response streaming

This commit is contained in:
2026-01-07 08:01:33 +01:00
parent 2e7c49bf68
commit d4f1a1c6a4

39
oai.py
View File

@@ -4985,9 +4985,12 @@ All queries are read-only. INSERT/UPDATE/DELETE are not allowed."""
# PROCESS FINAL RESPONSE # PROCESS FINAL RESPONSE
# ======================================================================== # ========================================================================
full_response = "" full_response = ""
stream_interrupted = False
if is_streaming: if is_streaming:
try: try:
with Live("", console=console, refresh_per_second=10, auto_refresh=True) as live: with Live("", console=console, refresh_per_second=10, auto_refresh=True) as live:
try:
for chunk in response: for chunk in response:
if hasattr(chunk, 'error') and chunk.error: if hasattr(chunk, 'error') and chunk.error:
console.print(f"\n[bold red]Stream error: {chunk.error.message}[/]") console.print(f"\n[bold red]Stream error: {chunk.error.message}[/]")
@@ -4998,13 +5001,43 @@ All queries are read-only. INSERT/UPDATE/DELETE are not allowed."""
full_response += content_chunk full_response += content_chunk
md = Markdown(full_response) md = Markdown(full_response)
live.update(md) live.update(md)
except KeyboardInterrupt:
stream_interrupted = True
console.print("\n[bold yellow]⚠️ Streaming interrupted![/]")
app_logger.info("Streaming interrupted by user (Ctrl+C)")
except Exception as stream_error:
stream_interrupted = True
console.print(f"\n[bold red]Stream error: {stream_error}[/]")
app_logger.error(f"Stream processing error: {stream_error}")
if not stream_interrupted:
console.print("") console.print("")
except KeyboardInterrupt: except KeyboardInterrupt:
console.print("\n[bold yellow]Streaming cancelled![/]") # Outer interrupt handler (in case inner misses it)
app_logger.info("Streaming cancelled by user") stream_interrupted = True
continue console.print("\n[bold yellow]⚠️ Streaming interrupted![/]")
app_logger.info("Streaming interrupted by user (outer)")
except Exception as e:
stream_interrupted = True
console.print(f"\n[bold red]Error during streaming: {e}[/]")
app_logger.error(f"Streaming error: {e}")
# If stream was interrupted, skip processing and continue to next prompt
if stream_interrupted:
if full_response:
console.print(f"\n[dim yellow]Partial response received ({len(full_response)} chars). Discarding...[/]")
console.print("[dim blue]💡 Ready for next prompt[/]\n")
app_logger.info("Stream cleanup completed, returning to prompt")
# Force close the response if possible
try:
if hasattr(response, 'close'):
response.close()
except:
pass
continue # Now it's safe to continue
else: else:
full_response = response.choices[0].message.content if response.choices else "" full_response = response.choices[0].message.content if response.choices else ""
console.print(f"\r{' ' * 50}\r", end="") console.print(f"\r{' ' * 50}\r", end="")