-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui_textual.py
More file actions
486 lines (404 loc) · 17.8 KB
/
Copy pathtui_textual.py
File metadata and controls
486 lines (404 loc) · 17.8 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python3
"""
Eling Textual TUI — split-pane conversation UI with permanent input bar,
elapsed timer, and real Eling agent integration via run_turn().
Usage:
python tui_textual.py
"""
from __future__ import annotations
import os
import sys
import time
from datetime import datetime
from pathlib import Path
# ── Rich ──────────────────────────────────────────────────────────
from rich.markdown import Markdown
from rich.panel import Panel
from rich.text import Text
from rich.style import Style as RichStyle
# ── Textual ───────────────────────────────────────────────────────
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Header, Input, RichLog, Static, Button
from textual.binding import Binding
from textual import work
# ── Path setup ────────────────────────────────────────────────────
_HERE = Path(__file__).parent
sys.path.insert(0, str(_HERE))
# ── Theme palette ────────────────────────────────────────────────
ACCENT = "#60a5fa"
MIDBLUE = "#3b82f6"
LIGHTBLUE = "#93c5fd"
MUTEDBLUE = "#bfdbfe"
DEEPBLUE = "#1e3a5f"
TEXT = "#f0f9ff"
BG = "#0f172a"
GREEN = "#4ade80"
RED = "#f87171"
YELLOW = "#fbbf24"
def _fmt_time(seconds: float) -> str:
hours, rem = divmod(int(seconds), 3600)
mins, secs = divmod(rem, 60)
if hours:
return f"{hours}h {mins}m {secs}s"
elif mins:
return f"{mins}m {secs}s"
else:
return f"{secs}s"
def _fmt_ts() -> str:
return datetime.now().strftime("%H:%M:%S")
# ═══════════════════════════════════════════════════════════════════
# Textual TUI — drop-in replacement for original ElingTUI
# ═══════════════════════════════════════════════════════════════════
class ElingTextualTUI(App):
"""Split-pane conversation TUI with permanent input bar, elapsed timer,
and real Eling agent integration.
Implements the same interface (``assistant()``, ``user_input()``,
``reasoning()``, ``tool_call()``, …) as the original ``ElingTUI``
from ``tui.py``, so it can be passed to ``run_turn()``.
"""
# ── CSS ────────────────────────────────────────────────────────
CSS = f"""
Screen {{
background: {BG};
}}
#app-container {{
layout: grid;
grid-size: 1;
grid-rows: auto 1fr auto auto;
height: 100%;
}}
#conversation {{
background: {BG};
border: none;
margin: 0 1;
overflow-y: auto;
scrollbar-color: {MIDBLUE} {DEEPBLUE};
scrollbar-size-vertical: 1;
}}
#input-bar {{
background: {DEEPBLUE};
height: auto;
padding: 0 1;
border-top: solid {MIDBLUE};
min-height: 3;
}}
#status-bar {{
background: {DEEPBLUE};
height: 1;
padding: 0 2;
}}
#input-field {{
background: #1e3a5f;
color: {TEXT};
border: none;
width: 1fr;
height: 3;
padding: 0 1;
}}
#input-field:focus {{
border: none;
}}
#timer-label {{
color: {LIGHTBLUE};
padding: 0 1;
}}
#count-label {{
color: {MUTEDBLUE};
padding: 0 1;
}}
#clear-btn {{
background: {MIDBLUE};
color: {TEXT};
min-width: 8;
height: 3;
margin: 0 1;
}}
#input-label {{
color: {LIGHTBLUE};
padding: 0 1;
height: 3;
content-align: center middle;
}}
#thinking-bar {{
background: #1e293b;
height: 1;
padding: 0 2;
dock: bottom;
}}
"""
BINDINGS = [
Binding("ctrl+c", "quit", "Quit", priority=True),
Binding("ctrl+l", "clear_view", "Clear"),
Binding("ctrl+d", "quit", "Quit"),
]
# ── State ──────────────────────────────────────────────────────
_last_input_time: float
_msg_count: int
_thinking: bool
_session_start: float
_conversation_history: list
_processing: bool
# Agent components (initialized in _init_agent)
_memory_store = None
_skills_lib = None
_provider = None
_plugin_callables = None
_plugin_schemas = None
_mcp_manager = None
_config = None
_agent_ready: bool = False
def __init__(self):
super().__init__()
self._last_input_time = time.time()
self._msg_count = 0
self._thinking = False
self._session_start = time.time()
self._conversation_history = []
self._processing = False
self._agent_ready = False
# ── Compose UI ─────────────────────────────────────────────────
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield RichLog(id="conversation", highlight=True, markup=True, wrap=True)
with Horizontal(id="input-bar"):
yield Input(id="input-field", placeholder="❯ Type your message…")
yield Button("Clear", id="clear-btn", variant="primary")
yield Static(id="status-bar")
def on_mount(self) -> None:
self.query_one("#input-field", Input).focus()
self.set_interval(1, self._update_status_bar)
self._update_status_bar()
self._init_agent()
def _update_status_bar(self) -> None:
try:
elapsed = time.time() - self._last_input_time
timer = _fmt_time(elapsed)
status = self.query_one("#status-bar", Static)
thinking = " 🤖 Thinking…" if self._thinking else ""
status.update(f"💬 {self._msg_count} ⏱ {timer}{thinking}")
except Exception:
pass # Gracefully handle shutdown races
# ── Agent initialization ─────────────────────────────────────────
def _init_agent(self) -> None:
"""Load config, models, memory, plugins — runs in worker thread."""
self._run_init_worker()
@work(thread=True, exit_on_error=False)
def _run_init_worker(self) -> None:
try:
from agent import load_config
from memory import MemoryStore
from skills import SkillLibrary
from provider import ZenProvider
from mcp_client import MCPManager
from plugins import load_plugins
config = load_config()
# Resolve relative paths
_cfg_dir = os.path.dirname(os.path.abspath(config.get("_config_path", "config.json")))
for _key in ("memory_db", "skills_db"):
_val = config.get(_key)
if _val and not os.path.isabs(_val):
config[_key] = os.path.join(_cfg_dir, _val)
zen_api_key = os.environ.get("ZEN_API_KEY") or config.get("zen_api_key", "")
if not zen_api_key or zen_api_key == "REPLACE_WITH_YOUR_ZEN_KEY":
self._safe_warning(
"⚠️ No ZEN_API_KEY set.\nSet ZEN_API_KEY env var or configure zen_api_key in config.json")
self._agent_ready = True
return
memory_store = MemoryStore(config.get("memory_db", "agent_memory.db"))
skills_lib = SkillLibrary(config.get("skills_db", "agent_skills.db"))
provider = ZenProvider(
api_key=zen_api_key,
base_url=config.get("zen_base_url", "https://api.zenprovider.com/v1"),
model=config.get("zen_model", "gpt-4o"),
)
# Load plugins
plugin_callables, plugin_schemas = load_plugins()
# MCP
mcp_servers = {k: v for k, v in config.get("mcp_servers", {}).items() if not k.startswith("_")}
mcp_manager = MCPManager(mcp_servers)
mcp_manager._ensure_connected()
self._config = config
self._memory_store = memory_store
self._skills_lib = skills_lib
self._provider = provider
self._plugin_callables = plugin_callables
self._plugin_schemas = plugin_schemas
self._mcp_manager = mcp_manager
self._agent_ready = True
self.call_from_thread(self._load_history)
except Exception as e:
import traceback
tb = traceback.format_exc()
self._safe_warning(
f"⚠️ Agent init failed: {e}\n\n{tb[-500:]}")
def _safe_warning(self, msg: str) -> None:
"""Show warning from a worker thread if the app is still running."""
try:
self.call_from_thread(self._show_warning, msg)
except Exception:
pass # App may have been closed during init
def _show_warning(self, msg: str) -> None:
log = self.query_one("#conversation", RichLog)
log.write(Panel(Text(msg, style=f"bold {YELLOW}"), style=RichStyle(color=YELLOW)))
# ── History persistence ─────────────────────────────────────────
def _load_history(self) -> None:
if not self._memory_store:
return
try:
entries = self._memory_store.recent(n=20)
for entry in reversed(entries):
if entry.user_input:
self._append_msg("user", entry.user_input, entry.timestamp)
if entry.agent_output:
self._append_msg("agent", entry.agent_output, entry.timestamp)
except Exception:
pass
# ── Message display ─────────────────────────────────────────────
def _append_msg(self, role: str, text: str, ts: str | None = None) -> None:
stamp = ts or _fmt_ts()
log = self.query_one("#conversation", RichLog)
if role == "user":
panel = Panel(
Text(f"🧑 {text}", style=f"bold {LIGHTBLUE}"),
style=RichStyle(color=MUTEDBLUE),
subtitle=Text(f" {stamp} ", style=f"italic {MUTEDBLUE}"),
subtitle_align="right",
)
else:
panel = Panel(
Markdown(text),
style=RichStyle(color=TEXT),
subtitle=Text(f" {stamp} ", style=f"italic {MUTEDBLUE}"),
subtitle_align="right",
)
log.write(panel)
self._msg_count += 1
def user_input(self, text: str) -> None:
"""Called by run_turn — display user message."""
self.call_from_thread(self._append_msg, "user", text)
def assistant(self, content: str) -> None:
"""Called by run_turn — display agent response."""
self.call_from_thread(self._append_msg, "agent", content)
def reasoning(self, text: str) -> None:
"""Called by run_turn — show chain-of-thought."""
stamp = _fmt_ts()
log = self.query_one("#conversation", RichLog)
panel = Panel(
Text(f"🤔 {text}", style=f"italic {YELLOW}"),
style=RichStyle(color=YELLOW),
subtitle=Text(f" {stamp} ", style=f"italic {MUTEDBLUE}"),
subtitle_align="right",
)
log.write(panel)
def tool_call(self, name: str, args_preview: str = "",
tool_call_id: str = "") -> None:
"""Called by run_turn — show tool call."""
log = self.query_one("#conversation", RichLog)
preview = args_preview[:120] + "…" if len(args_preview) > 120 else args_preview
log.write(Text(f" 🔧 {name}({preview})", style=f"bold {MIDBLUE}"))
def tool_start(self, name: str, args_preview: str = "") -> None:
self.tool_call(name, args_preview)
def tool_end(self, name: str, result: str = "", duration: float = 0,
ok: bool = True) -> None:
log = self.query_one("#conversation", RichLog)
status = "✅" if ok else "❌"
result_preview = result[:200].replace("\n", " ") + ("…" if len(result) > 200 else "")
log.write(Text(f" {status} {name} ({duration:.1f}s): {result_preview}",
style=RichStyle(color=GREEN if ok else RED)))
def tool_batch(self, results: list[dict]) -> None:
log = self.query_one("#conversation", RichLog)
for r in results:
name = r.get("name", r.get("tool_call_id", "?"))
ok = r.get("ok", True)
dur = r.get("duration", 0)
status = "✅" if ok else "❌"
log.write(Text(f" {status} {name} ({dur:.1f}s)",
style=RichStyle(color=GREEN if ok else RED)))
def recall_header(self) -> None:
log = self.query_one("#conversation", RichLog)
log.write(Text("\n📚 Context retrieved:", style=f"bold {LIGHTBLUE}"))
def context_hit(self, source: str, snippet: str, score: float) -> None:
log = self.query_one("#conversation", RichLog)
snippet_short = snippet[:100].replace("\n", " ") + ("…" if len(snippet) > 100 else "")
log.write(Text(f" [{source}] ({score:.2f}) {snippet_short}",
style=RichStyle(color=MUTEDBLUE)))
def separator(self) -> None:
log = self.query_one("#conversation", RichLog)
log.write(Text("─" * 40, style=RichStyle(color=MIDBLUE)))
def turn_start(self, query: str, **kwargs) -> None:
self._thinking = True
self._update_status_bar()
def turn_end(self) -> None:
self._thinking = False
self._update_status_bar()
def clear_screen(self) -> None:
log = self.query_one("#conversation", RichLog)
log.clear()
def set_verbose_tool_output(self, verbose: bool) -> None:
pass # Not needed in Textual version
def learned_skill(self, name: str) -> None:
log = self.query_one("#conversation", RichLog)
log.write(Text(f" 🧠 Learned skill: {name}", style=f"bold {GREEN}"))
# ── Input handling ──────────────────────────────────────────────
def on_input_submitted(self, event: Input.Submitted) -> None:
user_text = event.value.strip()
if not user_text or self._processing:
return
event.input.value = ""
self._last_input_time = time.time()
self._processing = True
# Show user message immediately
self._append_msg("user", user_text)
# Run agent turn in worker
self._run_turn_worker(user_text)
@work(thread=True, exit_on_error=False)
def _run_turn_worker(self, user_text: str) -> None:
if not self._agent_ready or not self._provider:
self.call_from_thread(self._append_msg, "agent",
"*Agent not ready. Check ZEN_API_KEY configuration.*")
self._processing = False
return
from agent import run_turn
self.call_from_thread(self.turn_start, user_text)
try:
response, history = run_turn(
provider=self._provider,
user_input=user_text,
memory_store=self._memory_store,
skills_lib=self._skills_lib,
plugin_callables=self._plugin_callables,
plugin_schemas=self._plugin_schemas,
mcp_manager=self._mcp_manager,
config=self._config,
tui=self,
conversation_history=self._conversation_history,
)
self._conversation_history = history
self.call_from_thread(self.assistant, response)
except Exception as exc:
self.call_from_thread(self._append_msg, "agent",
f"*Error: {exc}*")
self.call_from_thread(self.turn_end)
self._processing = False
# ── Button / Key handlers ───────────────────────────────────────
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "clear-btn":
self.clear_screen()
def action_clear_view(self) -> None:
self.clear_screen()
# ── Quit ────────────────────────────────────────────────────────
def on_exit(self) -> None:
if self._mcp_manager:
try:
self._mcp_manager.stop_all()
except Exception:
pass
# ═══════════════════════════════════════════════════════════════════
# Entry point
# ═══════════════════════════════════════════════════════════════════
def main():
app = ElingTextualTUI()
app.run()
if __name__ == "__main__":
main()