Upload jarvis/ui.py
Browse files- jarvis/ui.py +103 -0
jarvis/ui.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Terminal chat interface for J.A.R.V.I.S. using Rich."""
|
| 2 |
+
import sys
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from rich.console import Console
|
| 6 |
+
from rich.panel import Panel
|
| 7 |
+
from rich.text import Text
|
| 8 |
+
from rich.prompt import Prompt
|
| 9 |
+
from rich.markdown import Markdown
|
| 10 |
+
|
| 11 |
+
from jarvis.config import Config
|
| 12 |
+
from jarvis.memory import Memory
|
| 13 |
+
from jarvis.llm import get_backend, BaseBackend
|
| 14 |
+
from jarvis.pipeline import run_pipeline
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TerminalUI:
|
| 18 |
+
def __init__(self, config: Config, memory: Memory, backend: BaseBackend):
|
| 19 |
+
self.console = Console()
|
| 20 |
+
self.config = config
|
| 21 |
+
self.memory = memory
|
| 22 |
+
self.backend = backend
|
| 23 |
+
self.persona_name = config.persona.name
|
| 24 |
+
self.user_name = config.persona.user_name
|
| 25 |
+
|
| 26 |
+
def _header(self):
|
| 27 |
+
self.console.print()
|
| 28 |
+
self.console.print(
|
| 29 |
+
Panel.fit(
|
| 30 |
+
f"[bold cyan]{self.persona_name}[/bold cyan] — Alvin OS v0.1.0 (MVP)\n"
|
| 31 |
+
f"[dim]Type 'exit', 'quit', or press Ctrl-C to leave.[/dim]",
|
| 32 |
+
title="[bold green]Personal AI Assistant[/bold green]",
|
| 33 |
+
border_style="cyan",
|
| 34 |
+
)
|
| 35 |
+
)
|
| 36 |
+
self.console.print()
|
| 37 |
+
|
| 38 |
+
def _print_user(self, text: str):
|
| 39 |
+
self.console.print(
|
| 40 |
+
Panel(
|
| 41 |
+
text,
|
| 42 |
+
title=f"[bold yellow]{self.user_name}[/bold yellow]",
|
| 43 |
+
border_style="yellow",
|
| 44 |
+
width=min(80, self.console.width - 4),
|
| 45 |
+
)
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def _print_jarvis(self, text: str, metadata: Optional[dict] = None):
|
| 49 |
+
# Render markdown in the response
|
| 50 |
+
md = Markdown(text)
|
| 51 |
+
meta_text = ""
|
| 52 |
+
if metadata:
|
| 53 |
+
meta_text = (
|
| 54 |
+
f"\n[dim]Contract: {metadata.get('contract')} | "
|
| 55 |
+
f"Dial: {metadata.get('dial')} | "
|
| 56 |
+
f"Mix: {metadata.get('archetype_mix')}[/dim]"
|
| 57 |
+
)
|
| 58 |
+
self.console.print(
|
| 59 |
+
Panel(
|
| 60 |
+
md,
|
| 61 |
+
title=f"[bold cyan]{self.persona_name}[/bold cyan]",
|
| 62 |
+
subtitle=meta_text,
|
| 63 |
+
border_style="cyan",
|
| 64 |
+
width=min(80, self.console.width - 4),
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def _print_system(self, text: str):
|
| 69 |
+
self.console.print(f"[dim italic]{text}[/dim italic]")
|
| 70 |
+
|
| 71 |
+
def run(self):
|
| 72 |
+
self._header()
|
| 73 |
+
try:
|
| 74 |
+
while True:
|
| 75 |
+
user_input = Prompt.ask(f"[bold yellow]{self.user_name}[/bold yellow]")
|
| 76 |
+
user_input = user_input.strip()
|
| 77 |
+
if not user_input:
|
| 78 |
+
continue
|
| 79 |
+
if user_input.lower() in ("exit", "quit", "q"):
|
| 80 |
+
self._print_system("Shutting down. Goodbye, sir.")
|
| 81 |
+
break
|
| 82 |
+
|
| 83 |
+
self._print_user(user_input)
|
| 84 |
+
self._print_system("Processing...")
|
| 85 |
+
|
| 86 |
+
result = run_pipeline(
|
| 87 |
+
user_message=user_input,
|
| 88 |
+
config=self.config,
|
| 89 |
+
memory=self.memory,
|
| 90 |
+
backend=self.backend,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
self._print_jarvis(result["response"], metadata={
|
| 94 |
+
"contract": result["contract"],
|
| 95 |
+
"dial": result["dial"],
|
| 96 |
+
"archetype_mix": result["archetype_mix"],
|
| 97 |
+
})
|
| 98 |
+
self.console.print()
|
| 99 |
+
|
| 100 |
+
except KeyboardInterrupt:
|
| 101 |
+
self.console.print()
|
| 102 |
+
self._print_system("Interrupted. Goodbye, sir.")
|
| 103 |
+
sys.exit(0)
|