Files
sloppy/ai_chat.py
T
2026-10-15 01:05:01 +02:00

642 lines
23 KiB
Python

import asyncio
import json
import logging
import os
import io
from datetime import datetime
from pathlib import Path
from time import monotonic
from dataclasses import dataclass
import httpx
from dotenv import load_dotenv
from textual.app import App, ComposeResult
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.markdown import Markdown
from textual.containers import Container, Horizontal, VerticalScroll
from textual.widgets import Header, Footer, Static, Button
from textual.binding import Binding
from textual.screen import ModalScreen
log = logging.getLogger(__name__)
@dataclass
class AIConfig:
name: str
base_url: str
api_key: str
model: str
system_prompt: str = "You are a helpful assistant."
@dataclass
class ChatMessage:
role: str
content: str
sender: str
class SquareLoadingIndicator(Static):
"""A hollow three-by-three square with a rotating highlight."""
def on_mount(self) -> None:
self.auto_refresh = 0.15
def render(self) -> Text:
active = int(monotonic() / 0.15) % 8
if self.app.animation_level == "none":
active = 0
color = self.rich_style.color
result = Text(no_wrap=True)
for row, positions in enumerate(((0, 1, 2), (7, -1, 3), (6, 5, 4))):
if row:
result.append("\n")
for column, position in enumerate(positions):
if column:
result.append(" ")
result.append(" " if position == -1 else "\u25cf", style=f"{'bold' if position == active else 'dim'} {color.name if color else 'cyan'}")
return result
class MessageBubble(Static):
"""A chat message bubble that supports streaming updates."""
def __init__(self, message: ChatMessage, **kwargs):
super().__init__(**kwargs)
self.message = message
self.border_title = f"[bold]{message.sender}[/bold]"
if message.sender == "AI 1":
self.styles.border = ("round", "blue")
else:
self.styles.border = ("round", "green")
self._content_widget = Static(message.content, classes="bubble-content", markup=False)
self._streaming = False
self._waiting_widget = Horizontal(
SquareLoadingIndicator(),
Static("Waiting for response...", markup=False, classes="waiting-label"),
classes="response-waiting",
)
self._waiting_widget.display = False
self._render_content()
def _render_content(self) -> None:
# Keep the raw source in message.content for subsequent API requests.
content = self.message.content
self._content_widget.update(
Markdown(content, code_theme="monokai")
if self.message.role == "assistant" else content
)
def set_waiting(self, waiting: bool) -> None:
self._waiting_widget.display = waiting
self._content_widget.display = bool(self.message.content.strip()) or not waiting
def compose(self) -> ComposeResult:
yield self._content_widget
yield self._waiting_widget
def set_content(self, content: str) -> None:
"""Set the full content (non-streaming)."""
self.message.content = content
self._render_content()
self.set_waiting(False)
self.refresh()
def append_content(self, delta: str) -> None:
"""Append content for streaming."""
self.message.content += delta
self._render_content()
if self.message.content.strip():
self.set_waiting(False)
self.refresh()
def start_streaming(self) -> None:
"""Mark as streaming."""
self._streaming = True
self.set_waiting(True)
self.refresh()
def end_streaming(self) -> None:
"""Mark streaming as complete."""
self._streaming = False
self.set_waiting(False)
self.refresh()
class ChatContainer(VerticalScroll):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.messages: list[ChatMessage] = []
def add_message(self, message: ChatMessage) -> MessageBubble:
"""Add a new message and return its bubble for streaming."""
bubble = MessageBubble(message)
self.mount(bubble)
self.messages.append(message)
self.refresh()
return bubble
def update_last_message(self, content: str) -> None:
"""Update the last message's content."""
if self.messages:
self.messages[-1].content = content
bubbles = self.query(MessageBubble)
if bubbles:
bubbles.last().set_content(content)
self.refresh()
class ControlsHelp(ModalScreen):
BINDINGS = [Binding("escape,f1", "dismiss", "Close")]
DEFAULT_CSS = """
ControlsHelp { align: center middle; }
#controls-help {
width: 64;
max-width: 95%;
height: auto;
padding: 1 2;
border: round $primary;
background: $surface;
}
"""
def compose(self) -> ComposeResult:
yield Static(
"[bold]Reading controls[/bold]\n\n"
"Mouse wheel / Up / Down: scroll\n"
"Page Up / Page Down: scroll a page\n"
"S / F2: save visible screen or full conversation\n"
"Home: oldest message\n"
"End: latest message and resume following\n\n"
"Scrolling up pauses automatic following.\n\n"
"[bold]Zoom (Windows Terminal)[/bold]\n"
"Ctrl + Plus: larger text\n"
"Ctrl + Minus: smaller text\n"
"Ctrl + 0: reset font size\n\n"
"Font size is controlled by your terminal.\n"
"Other terminals may use different shortcuts.\n\n"
"Escape / F1: close help",
id="controls-help",
)
class ScreenshotOptions(ModalScreen[str | None]):
BINDINGS = [
Binding("escape", "dismiss(None)", "Cancel"),
Binding("f", "dismiss('full')", "Full conversation"),
Binding("v", "dismiss('visible')", "Visible screen"),
]
DEFAULT_CSS = """
ScreenshotOptions { align: center middle; }
#screenshot-options {
width: 58;
max-width: 95%;
height: auto;
padding: 1 2;
border: round $primary;
background: $surface;
}
#screenshot-options Button { width: 100%; margin-top: 1; }
"""
def compose(self) -> ComposeResult:
with Container(id="screenshot-options"):
yield Static("[bold]Save screenshot[/bold]\nSVG images are saved in screenshots/.")
yield Button("Full conversation (F)", id="full", variant="primary")
yield Button("Visible screen (V)", id="visible")
yield Button("Cancel", id="cancel")
def on_button_pressed(self, event: Button.Pressed) -> None:
self.dismiss(None if event.button.id == "cancel" else event.button.id)
class AIChatApp(App):
CSS = """
Screen {
background: $surface;
}
#elapsed-time {
height: 1;
padding: 0 1;
text-align: right;
background: $surface-darken-1;
}
#chat-container {
height: 1fr;
border: solid $primary;
margin: 1;
padding: 1;
}
.bubble-content {
padding: 1;
width: 100%;
}
.response-waiting {
height: 3;
align-vertical: middle;
}
.response-waiting SquareLoadingIndicator {
width: 5;
height: 3;
margin: 0 2 0 1;
color: $primary;
}
.response-waiting .waiting-label {
width: 1fr;
height: 3;
content-align: left middle;
}
MessageBubble {
margin: 1 0;
width: 100%;
min-height: 3;
}
#turn-counter {
height: 1;
padding: 0 1;
background: $surface-darken-1;
text-style: bold;
}
#status-bar {
height: 3;
padding: 0 1;
background: $surface-darken-1;
}
"""
BINDINGS = [
Binding("ctrl+c", "quit", "Quit"),
Binding("f1", "controls_help", "Help / Zoom"),
Binding("s", "screenshot_options", "Screenshot", priority=True),
Binding("f2", "screenshot_options", show=False, priority=True),
Binding("pageup", "chat_scroll('page_up')", "Page up", priority=True),
Binding("pagedown", "chat_scroll('page_down')", "Page down", priority=True),
Binding("home", "chat_scroll('home')", "Oldest", priority=True),
Binding("end", "chat_scroll('end')", "Latest", priority=True),
Binding("up", "chat_scroll('up')", show=False, priority=True),
Binding("down", "chat_scroll('down')", show=False, priority=True),
]
def __init__(
self,
ai1_config: AIConfig,
ai2_config: AIConfig,
initial_prompt: str,
max_turns: int = 20,
):
super().__init__()
if max_turns < 1:
raise ValueError("max_turns must be positive")
self.ai1 = ai1_config
self.ai2 = ai2_config
self.initial_prompt = initial_prompt
self.max_turns = max_turns
self.messages: list[ChatMessage] = []
self.current_turn = 0
self._started_at: float | None = None
self._ended_at: float | None = None
self._is_running = False
self.client = httpx.AsyncClient(timeout=120.0)
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield Container(
Static("Elapsed: 00:00:00", id="elapsed-time", markup=False),
ChatContainer(id="chat-container"),
Static(f"Turns completed: 0 / {self.max_turns}", id="turn-counter", markup=False),
Static(id="status-bar", markup=False),
id="main-container"
)
yield Footer()
async def on_mount(self) -> None:
self.chat_container = self.query_one(ChatContainer)
self.status_bar = self.query_one("#status-bar", Static)
self.turn_counter = self.query_one("#turn-counter", Static)
self.elapsed_time = self.query_one("#elapsed-time", Static)
self.chat_container.focus()
self.chat_container.anchor()
self.run_worker(self.start_conversation(), exclusive=True)
def action_screenshot_options(self) -> None:
if not isinstance(self.screen, ScreenshotOptions):
self.push_screen(ScreenshotOptions(), self._screenshot_selected)
def _screenshot_selected(self, choice: str | None) -> None:
if choice is not None:
# Capture after the modal has been removed from the screen.
self.call_after_refresh(self._save_conversation_screenshot, choice)
def export_conversation_screenshot(self) -> str:
"""Render all messages without the viewport's scrolling/clipping."""
console = Console(
width=max(40, self.size.width), file=io.StringIO(),
record=True, force_terminal=True, color_system="truecolor",
)
for message in self.messages:
content = Markdown(message.content, code_theme="monokai") if message.role == "assistant" else Text(message.content)
console.print(Panel(
content, title=Text(message.sender),
border_style="blue" if message.sender == "AI 1" else "green",
padding=(1, 1),
))
return console.export_svg(title="AI conversation")
def _save_conversation_screenshot(self, choice: str) -> None:
try:
directory = Path("screenshots")
directory.mkdir(exist_ok=True)
filename = f"conversation-{choice}-{datetime.now():%Y%m%d-%H%M%S-%f}.svg"
path = directory / filename
if choice == "full":
path.write_text(self.export_conversation_screenshot(), encoding="utf-8")
else:
self.save_screenshot(filename=filename, path=str(directory))
except (OSError, ValueError) as error:
self.notify(f"Could not save screenshot: {error}", severity="error")
else:
self.notify(f"Screenshot saved: {path.resolve()}", timeout=10)
def action_controls_help(self) -> None:
self.push_screen(ControlsHelp())
def action_chat_scroll(self, direction: str) -> None:
if isinstance(self.screen, (ControlsHelp, ScreenshotOptions)):
return
chat = self.chat_container
if direction == "end":
chat.anchor()
elif direction == "home":
chat.scroll_home(animate=False)
elif direction == "page_up":
chat.scroll_page_up(animate=False)
elif direction == "page_down":
chat.scroll_page_down(animate=False)
elif direction == "up":
chat.scroll_up(animate=False)
elif direction == "down":
chat.scroll_down(animate=False)
async def start_conversation(self) -> None:
self._is_running = True
self._started_at = monotonic()
self._ended_at = None
elapsed_timer = self.set_interval(1, self.update_elapsed_time)
self.update_status(f"Starting conversation... AI 1 ({self.ai1.name}) begins")
self.chat_container.messages = self.messages
self.chat_container.add_message(ChatMessage("user", self.initial_prompt, "System"))
try:
await self.run_conversation_loop()
finally:
self._ended_at = monotonic()
elapsed_timer.stop()
self.update_elapsed_time()
def update_elapsed_time(self) -> None:
if self._started_at is None:
return
end = self._ended_at if self._ended_at is not None else monotonic()
seconds = max(0, int(end - self._started_at))
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
self.elapsed_time.update(f"Elapsed: {hours:02d}:{minutes:02d}:{seconds:02d}")
def history_for(self, sender: str) -> list[dict]:
"""Describe each turn from the responding model's perspective."""
return [
{"role": "assistant" if message.sender == sender else "user",
"content": message.content}
for message in self.messages if message.role != "system"
]
async def run_conversation_loop(self) -> None:
try:
while self.current_turn < self.max_turns and self._is_running:
current_ai = self.ai1 if self.current_turn % 2 == 0 else self.ai2
sender = "AI 1" if self.current_turn % 2 == 0 else "AI 2"
history = self.history_for(sender)
self.update_status(f"Streaming... ({sender}: {current_ai.name})")
bubble = self.chat_container.add_message(ChatMessage("assistant", "", sender))
try:
try:
await self._try_streaming(current_ai, history, bubble)
except Exception as stream_error:
self.update_status(f"Streaming failed, trying non-streaming... ({stream_error})")
bubble.set_waiting(True)
try:
full_response = await self.call_ai(current_ai, history)
bubble.set_content(full_response)
finally:
bubble.set_waiting(False)
except Exception as error:
error_msg = f"Error: {error}"
self.chat_container.add_message(ChatMessage("system", error_msg, "System"))
self.update_status(error_msg)
return
self.current_turn += 1
self.turn_counter.update(
f"Turns completed: {self.current_turn} / {self.max_turns}"
)
if self.current_turn < self.max_turns:
await asyncio.sleep(0.5)
self.update_status("Conversation ended. Press Ctrl+C to quit.")
finally:
self._is_running = False
async def _try_streaming(self, ai: AIConfig, history: list[dict], bubble: MessageBubble) -> str:
"""Stream into a single bubble, reused if a fallback is needed."""
bubble.start_streaming()
full_response = ""
try:
async for chunk in self.stream_ai(ai, history):
full_response += chunk
bubble.append_content(chunk)
await asyncio.sleep(0)
if not full_response.strip():
raise ValueError("Empty response from API")
return full_response
finally:
bubble.end_streaming()
async def call_ai(self, ai: AIConfig, history: list[dict]) -> str:
"""Non-streaming fallback."""
messages = [{"role": "system", "content": ai.system_prompt}] + history
payload = {
"model": ai.model,
"messages": messages,
"temperature": 0.7,
"max_tokens": 2000,
}
headers = {
"Authorization": f"Bearer {ai.api_key}",
"Content-Type": "application/json",
}
url = f"{ai.base_url.rstrip('/')}/chat/completions"
response = await self.client.post(url, json=payload, headers=headers)
if response.status_code != 200:
raise Exception(f"API Error {response.status_code}: {response.text}")
data = response.json()
if "error" in data:
raise Exception(f"API Error: {data['error']}")
content = data["choices"][0]["message"].get("content")
if not isinstance(content, str) or not content.strip():
raise ValueError("Empty response from API")
return content
async def stream_ai(self, ai: AIConfig, history: list[dict]):
"""Stream response from AI using SSE."""
messages = [{"role": "system", "content": ai.system_prompt}] + history
payload = {
"model": ai.model,
"messages": messages,
"temperature": 0.7,
"max_tokens": 2000,
"stream": True,
}
headers = {
"Authorization": f"Bearer {ai.api_key}",
"Content-Type": "application/json",
"Accept": "text/event-stream",
}
url = f"{ai.base_url.rstrip('/')}/chat/completions"
log.debug(f"Streaming request to {url} with model {ai.model}")
async with self.client.stream("POST", url, json=payload, headers=headers) as response:
log.debug(f"Response status: {response.status_code}")
if response.status_code != 200:
error_body = await response.aread()
raise Exception(f"API Error {response.status_code}: {error_body.decode(errors='replace')}")
async for line in response.aiter_lines():
if not line.startswith("data:"):
continue
data = line[5:].lstrip()
if data == "[DONE]":
break
if not data:
continue
chunk = json.loads(data)
if "error" in chunk:
raise ValueError(f"Stream error: {chunk['error']}")
choices = chunk.get("choices", [])
if not choices: # Usage-only events have no choices.
continue
delta = choices[0].get("delta", {}).get("content")
if delta is not None:
if not isinstance(delta, str):
raise ValueError("Invalid stream content")
yield delta
def update_status(self, text: str) -> None:
self.status_bar.update(f"Status: {text}")
async def on_unmount(self) -> None:
await self.client.aclose()
def load_config() -> tuple[AIConfig, AIConfig, str, str]:
load_dotenv()
def get_env(key: str, default: str = "") -> str:
return os.getenv(key, default)
ai1 = AIConfig(
name=get_env("AI1_NAME", "AI 1"),
base_url=get_env("AI1_BASE_URL", ""),
api_key=get_env("AI1_API_KEY", ""),
model=get_env("AI1_MODEL", "gpt-3.5-turbo"),
system_prompt=get_env("AI1_SYSTEM_PROMPT", "You are a helpful assistant."),
)
ai2 = AIConfig(
name=get_env("AI2_NAME", "AI 2"),
base_url=get_env("AI2_BASE_URL", ""),
api_key=get_env("AI2_API_KEY", ""),
model=get_env("AI2_MODEL", "gpt-3.5-turbo"),
system_prompt=get_env("AI2_SYSTEM_PROMPT", "You are a helpful assistant."),
)
prompt = get_env("INITIAL_PROMPT", "")
turns = get_env("MAX_TURNS", "20")
return ai1, ai2, prompt, turns
def main():
import argparse
# Load from .env first
ai1_env, ai2_env, prompt_env, turns_env = load_config()
parser = argparse.ArgumentParser(description="Two AI models conversation TUI with streaming")
parser.add_argument("--ai1-name", default=ai1_env.name, help="Name for AI 1")
parser.add_argument("--ai1-url", default=ai1_env.base_url, help="Base URL for AI 1 (OpenAI-compatible)")
parser.add_argument("--ai1-key", default=ai1_env.api_key, help="API key for AI 1")
parser.add_argument("--ai1-model", default=ai1_env.model, help="Model for AI 1")
parser.add_argument("--ai1-prompt", default=ai1_env.system_prompt, help="System prompt for AI 1")
parser.add_argument("--ai2-name", default=ai2_env.name, help="Name for AI 2")
parser.add_argument("--ai2-url", default=ai2_env.base_url, help="Base URL for AI 2 (OpenAI-compatible)")
parser.add_argument("--ai2-key", default=ai2_env.api_key, help="API key for AI 2")
parser.add_argument("--ai2-model", default=ai2_env.model, help="Model for AI 2")
parser.add_argument("--ai2-prompt", default=ai2_env.system_prompt, help="System prompt for AI 2")
parser.add_argument("--prompt", default=prompt_env, help="Initial prompt for AI 1 to start")
parser.add_argument("--turns", type=int, default=turns_env, help="Maximum conversation turns")
args = parser.parse_args()
if not args.ai1_url or not args.ai1_key:
parser.error("AI1_BASE_URL and AI1_API_KEY required (set in .env or via --ai1-url/--ai1-key)")
if not args.ai2_url or not args.ai2_key:
parser.error("AI2_BASE_URL and AI2_API_KEY required (set in .env or via --ai2-url/--ai2-key)")
if args.turns < 1:
parser.error("--turns / MAX_TURNS must be positive")
if not args.prompt.strip():
parser.error("INITIAL_PROMPT required (set in .env or via --prompt)")
ai1 = AIConfig(
name=args.ai1_name,
base_url=args.ai1_url,
api_key=args.ai1_key,
model=args.ai1_model,
system_prompt=args.ai1_prompt,
)
ai2 = AIConfig(
name=args.ai2_name,
base_url=args.ai2_url,
api_key=args.ai2_key,
model=args.ai2_model,
system_prompt=args.ai2_prompt,
)
app = AIChatApp(ai1, ai2, args.prompt, args.turns)
app.run()
if __name__ == "__main__":
main()