-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.py
More file actions
351 lines (295 loc) · 11.4 KB
/
tui.py
File metadata and controls
351 lines (295 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Terminal User Interface for Jarvis using Rich."""
from datetime import datetime
from typing import List, Dict, Any, Optional
from rich.console import Console, Group
from rich.layout import Layout
from rich.panel import Panel
from rich.live import Live
from rich.text import Text
from rich.table import Table
from rich.align import Align
from rich import box
from loguru import logger
class JarvisTUI:
"""Terminal UI for displaying chat history and actions."""
def __init__(self):
self.console = Console()
self.chat_history: List[Dict[str, Any]] = []
self.actions_log: List[Dict[str, Any]] = []
self.current_status = "Initializing..."
self.current_language = "en"
self.live = None
def _make_header(self) -> Panel:
"""Create the header panel."""
header_text = Text()
header_text.append("🤖 ", style="bold cyan")
header_text.append("JARVIS", style="bold white")
header_text.append(" - Local Voice Assistant", style="dim white")
info = Text()
info.append(f" Language: ", style="dim")
info.append(f"{self.current_language.upper()}", style="bold yellow")
info.append(f" | Status: ", style="dim")
info.append(self.current_status, style="bold green")
content = Group(
Align.center(header_text),
Align.center(info)
)
return Panel(
content,
box=box.DOUBLE,
style="cyan",
padding=(0, 1)
)
def _make_chat_panel(self) -> Panel:
"""Create the chat history panel."""
if not self.chat_history:
content = Text("No conversation yet...", style="dim italic")
else:
content = Group()
# Show last 10 messages
messages = self.chat_history[-10:]
for msg in messages:
role = msg.get("role", "unknown")
text = msg.get("content", "")
timestamp = msg.get("timestamp", "")
if role == "user":
line = Text()
line.append(f"[{timestamp}] ", style="dim")
line.append("👤 You: ", style="bold blue")
line.append(text, style="white")
content.renderables.append(line)
elif role == "assistant":
line = Text()
line.append(f"[{timestamp}] ", style="dim")
line.append("🤖 Jarvis: ", style="bold green")
line.append(text, style="white")
content.renderables.append(line)
elif role == "system":
line = Text()
line.append(f"[{timestamp}] ", style="dim")
line.append("⚙️ ", style="yellow")
line.append(text, style="yellow italic")
content.renderables.append(line)
# Add spacing between messages
content.renderables.append(Text(""))
return Panel(
content,
title="💬 Conversation",
border_style="blue",
padding=(1, 2),
height=20
)
def _make_actions_panel(self) -> Panel:
"""Create the actions log panel."""
if not self.actions_log:
content = Text("No actions performed yet...", style="dim italic")
else:
table = Table(
show_header=True,
header_style="bold magenta",
box=box.SIMPLE,
padding=(0, 1)
)
table.add_column("Time", style="dim", width=8)
table.add_column("Action", style="cyan", width=20)
table.add_column("Details", style="white")
# Show last 8 actions
for action in self.actions_log[-8:]:
timestamp = action.get("timestamp", "")
action_type = action.get("action", "")
details = action.get("details", "")
status = action.get("status", "")
# Truncate long details
if len(details) > 50:
details = details[:47] + "..."
# Color code by status
if status == "success":
action_style = "green"
elif status == "error":
action_style = "red"
else:
action_style = "yellow"
table.add_row(
timestamp,
Text(action_type, style=action_style),
details
)
content = table
return Panel(
content,
title="⚡ Actions & Tools",
border_style="magenta",
padding=(1, 1),
height=12
)
def _make_help_panel(self) -> Panel:
"""Create the help/commands panel."""
help_text = Text()
help_text.append("Commands: ", style="bold")
help_text.append("exit/quit/goodbye", style="cyan")
help_text.append(" | ", style="dim")
help_text.append("switch to french/english", style="cyan")
help_text.append(" | ", style="dim")
help_text.append("Ctrl+C to stop", style="red")
return Panel(
Align.center(help_text),
border_style="dim",
padding=(0, 1)
)
def _make_layout(self) -> Layout:
"""Create the main layout."""
layout = Layout()
layout.split_column(
Layout(name="header", size=5),
Layout(name="body"),
Layout(name="help", size=3)
)
layout["body"].split_row(
Layout(name="chat", ratio=2),
Layout(name="actions", ratio=1)
)
return layout
def _update_layout(self, layout: Layout):
"""Update the layout with current data."""
layout["header"].update(self._make_header())
layout["chat"].update(self._make_chat_panel())
layout["actions"].update(self._make_actions_panel())
layout["help"].update(self._make_help_panel())
def start(self):
"""Start the live TUI."""
layout = self._make_layout()
self._update_layout(layout)
self.live = Live(
layout,
console=self.console,
refresh_per_second=4,
screen=True
)
self.live.start()
def stop(self):
"""Stop the live TUI."""
if self.live:
self.live.stop()
def refresh(self):
"""Manually refresh the display."""
if self.live:
layout = self._make_layout()
self._update_layout(layout)
self.live.update(layout)
def update_status(self, status: str):
"""Update the current status."""
self.current_status = status
self.refresh()
def update_language(self, language: str):
"""Update the current language."""
self.current_language = language
self.refresh()
def add_user_message(self, message: str):
"""Add a user message to chat history."""
timestamp = datetime.now().strftime("%H:%M:%S")
self.chat_history.append({
"role": "user",
"content": message,
"timestamp": timestamp
})
self.refresh()
def add_assistant_message(self, message: str):
"""Add an assistant message to chat history."""
timestamp = datetime.now().strftime("%H:%M:%S")
self.chat_history.append({
"role": "assistant",
"content": message,
"timestamp": timestamp
})
self.refresh()
def add_system_message(self, message: str):
"""Add a system message to chat history."""
timestamp = datetime.now().strftime("%H:%M:%S")
self.chat_history.append({
"role": "system",
"content": message,
"timestamp": timestamp
})
self.refresh()
def add_action(self, action_type: str, details: str, status: str = "info"):
"""
Add an action to the actions log.
Args:
action_type: Type of action (e.g., "File Operation", "Web Fetch")
details: Action details
status: Status - "success", "error", or "info"
"""
timestamp = datetime.now().strftime("%H:%M:%S")
self.actions_log.append({
"action": action_type,
"details": details,
"status": status,
"timestamp": timestamp
})
self.refresh()
def clear_history(self):
"""Clear chat history."""
self.chat_history = []
self.refresh()
def clear_actions(self):
"""Clear actions log."""
self.actions_log = []
self.refresh()
def prompt_confirmation(self, action_description: str, item: str) -> tuple[bool, bool]:
"""
Prompt user for action confirmation.
Args:
action_description: Description of the action
item: Item to potentially whitelist
Returns:
Tuple of (execute_action, add_to_whitelist)
"""
if self.live:
self.live.stop()
self.console.print()
self.console.print(Panel(
f"[bold yellow]⚠️ Confirmation Required[/bold yellow]\n\n"
f"Action: [cyan]{action_description}[/cyan]\n\n"
f"Do you want to proceed?",
border_style="yellow",
padding=(1, 2)
))
self.console.print("[cyan]Options:[/cyan]")
self.console.print(" [green]y[/green] - Execute this action")
self.console.print(" [green]a[/green] - Execute and [bold]add to whitelist[/bold]")
self.console.print(" [red]n[/red] - Cancel this action")
self.console.print()
while True:
choice = self.console.input("[bold]Your choice[/bold] [dim](y/a/n)[/dim]: ").lower().strip()
if choice == 'y':
result = (True, False)
break
elif choice == 'a':
result = (True, True)
self.console.print(f"[green]✓[/green] Added to whitelist: {item}")
break
elif choice == 'n':
result = (False, False)
self.console.print("[red]✗[/red] Action cancelled")
break
else:
self.console.print("[red]Invalid choice. Please enter y, a, or n.[/red]")
self.console.print()
if self.live:
self.live.start()
return result
def show_welcome(self):
"""Show welcome screen."""
self.console.clear()
welcome = Text()
welcome.append("\n\n")
welcome.append(" 🤖 ", style="bold cyan")
welcome.append("JARVIS", style="bold white")
welcome.append(" - Local Voice Assistant\n\n", style="dim white")
welcome.append(" Starting up...\n", style="yellow")
self.console.print(Panel(
Align.center(welcome),
box=box.DOUBLE,
border_style="cyan",
padding=(2, 4)
))