Fixed bug in ctrl+c handling during response streaming
This commit is contained in:
61
oai.py
61
oai.py
@@ -4985,26 +4985,59 @@ 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:
|
||||||
for chunk in response:
|
try:
|
||||||
if hasattr(chunk, 'error') and chunk.error:
|
for chunk in response:
|
||||||
console.print(f"\n[bold red]Stream error: {chunk.error.message}[/]")
|
if hasattr(chunk, 'error') and chunk.error:
|
||||||
app_logger.error(f"Stream error: {chunk.error.message}")
|
console.print(f"\n[bold red]Stream error: {chunk.error.message}[/]")
|
||||||
break
|
app_logger.error(f"Stream error: {chunk.error.message}")
|
||||||
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
|
break
|
||||||
content_chunk = chunk.choices[0].delta.content
|
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
|
||||||
full_response += content_chunk
|
content_chunk = chunk.choices[0].delta.content
|
||||||
md = Markdown(full_response)
|
full_response += content_chunk
|
||||||
live.update(md)
|
md = Markdown(full_response)
|
||||||
|
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}")
|
||||||
|
|
||||||
console.print("")
|
if not stream_interrupted:
|
||||||
|
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="")
|
||||||
|
|||||||
Reference in New Issue
Block a user