|
|
@ -1,6 +1,6 @@
|
|
|
|
import threading
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
from typing import Any, Callable, Dict, List
|
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.live import Live
|
|
|
|
from rich.live import Live
|
|
|
@ -150,6 +150,8 @@ class Formatter:
|
|
|
|
streaming_response,
|
|
|
|
streaming_response,
|
|
|
|
title: str = "🤖 Agent Streaming Response",
|
|
|
|
title: str = "🤖 Agent Streaming Response",
|
|
|
|
style: str = "bold cyan",
|
|
|
|
style: str = "bold cyan",
|
|
|
|
|
|
|
|
collect_chunks: bool = False,
|
|
|
|
|
|
|
|
on_chunk_callback: Optional[Callable] = None,
|
|
|
|
) -> str:
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Display real-time streaming response using Rich Live and Panel.
|
|
|
|
Display real-time streaming response using Rich Live and Panel.
|
|
|
@ -159,6 +161,8 @@ class Formatter:
|
|
|
|
streaming_response: The streaming response generator from LiteLLM.
|
|
|
|
streaming_response: The streaming response generator from LiteLLM.
|
|
|
|
title (str): Title of the panel.
|
|
|
|
title (str): Title of the panel.
|
|
|
|
style (str): Style for the panel border.
|
|
|
|
style (str): Style for the panel border.
|
|
|
|
|
|
|
|
collect_chunks (bool): Whether to collect individual chunks for conversation saving.
|
|
|
|
|
|
|
|
on_chunk_callback (Optional[Callable]): Callback function to call for each chunk.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
str: The complete accumulated response text.
|
|
|
|
str: The complete accumulated response text.
|
|
|
@ -187,6 +191,7 @@ class Formatter:
|
|
|
|
# Create a Text object for streaming content
|
|
|
|
# Create a Text object for streaming content
|
|
|
|
streaming_text = Text()
|
|
|
|
streaming_text = Text()
|
|
|
|
complete_response = ""
|
|
|
|
complete_response = ""
|
|
|
|
|
|
|
|
chunks_collected = []
|
|
|
|
|
|
|
|
|
|
|
|
# TRUE streaming with Rich's automatic text wrapping
|
|
|
|
# TRUE streaming with Rich's automatic text wrapping
|
|
|
|
with Live(
|
|
|
|
with Live(
|
|
|
@ -202,6 +207,14 @@ class Formatter:
|
|
|
|
streaming_text.append(chunk, style="white")
|
|
|
|
streaming_text.append(chunk, style="white")
|
|
|
|
complete_response += chunk
|
|
|
|
complete_response += chunk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Collect chunks if requested
|
|
|
|
|
|
|
|
if collect_chunks:
|
|
|
|
|
|
|
|
chunks_collected.append(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Call chunk callback if provided
|
|
|
|
|
|
|
|
if on_chunk_callback:
|
|
|
|
|
|
|
|
on_chunk_callback(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
# Update display with new text - Rich handles all wrapping automatically
|
|
|
|
# Update display with new text - Rich handles all wrapping automatically
|
|
|
|
live.update(create_streaming_panel(streaming_text, is_complete=False))
|
|
|
|
live.update(create_streaming_panel(streaming_text, is_complete=False))
|
|
|
|
|
|
|
|
|
|
|
|