-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
783 lines (697 loc) · 29.1 KB
/
Copy pathagent.py
File metadata and controls
783 lines (697 loc) · 29.1 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#!/usr/bin/env python3
"""
Eling — a small autonomous agent framework with local memory,
auto-learning skill library, plugin loader, MCP client, and
OpenCode Zen as the model provider.
"""
import argparse
import concurrent.futures
import json
import logging
import os
import re
import requests
import shutil
import subprocess
import sys
import time
from memory import MemoryStore
from skills import SkillLibrary
from provider import ZenProvider
from mcp_client import MCPManager
from plugins import load_plugins
from rich.logging import RichHandler
logging.basicConfig(
level=logging.WARNING,
format="%(message)s",
handlers=[RichHandler(rich_tracebacks=True, show_path=False, show_time=False)],
)
log = logging.getLogger("eling")
BASE_SYSTEM_PROMPT = (
"You are Eling, a helpful autonomous assistant running locally on "
"the user's device. You have access to tools (local plugins and MCP "
"servers, including a long-term memory server if configured). Use "
"tools when they help; otherwise answer directly. Be concise."
)
def load_config(path: str = "config.json") -> dict:
if not os.path.exists(path):
script_dir = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(script_dir, "config.json")
if not os.path.exists(path):
home_cfg = os.path.join(os.path.expanduser("~"), "eling-agent", "config.json")
if os.path.exists(home_cfg):
path = home_cfg
if not os.path.exists(path):
# Bootstrap from example if available
example = path.replace("config.json", "config.example.json")
if os.path.exists(example):
shutil.copy2(example, path)
print(f"Created {path} from {example} — edit to add your API key.")
else:
print(
"ERROR: No config.json found. Copy config.example.json to "
"config.json and set your zen_api_key (or set ZEN_API_KEY env var)."
)
sys.exit(1)
with open(path) as f:
cfg = json.load(f)
cfg["_config_path"] = path
return cfg
def build_system_prompt(
skills_hits: list,
memory_hits: list,
) -> str:
"""Build the full system prompt by appending retrieved skills and memories."""
parts = [BASE_SYSTEM_PROMPT]
if skills_hits:
parts.append("\n\n## Retrieved Skills")
parts.append(
"The following skills may be relevant to the current query. "
"Use their bodies as guidance."
)
for skill, score in skills_hits:
parts.append(
f"\n### {skill.name} (uses={skill.uses}, "
f"successes={skill.successes}, relevance={score:.3f})"
)
parts.append(skill.body)
if memory_hits:
parts.append("\n\n## Relevant Past Episodes")
parts.append("Past exchanges that may be relevant to the current situation:")
for entry, score in memory_hits:
parts.append(
f"\n--- (relevance={score:.3f}) ---\n"
f"User: {entry.user_input}\n"
f"Assistant: {entry.agent_output}\n"
f"Outcome: {entry.outcome}"
)
return "\n".join(parts)
# ── Auto Ruff + Auto Pytest —──────────────────────────────────────────
PY_FILE_RE = re.compile(r'(?:^|\s)(/[^\s]*\.py|[a-zA-Z0-9_./-]+\.py)')
def _extract_py_files(tool_results: list[dict]) -> set[str]:
"""Extract .py file paths from tool result content."""
files: set[str] = set()
for tr in tool_results:
text = tr.get("content", "")
for m in PY_FILE_RE.finditer(text):
candidate = m.group(1).rstrip(".,;:!?)'\"")
if os.path.isfile(candidate):
files.add(candidate)
return files
def _auto_ruff_check(tool_results: list[dict], tui=None) -> None:
"""Scan tool results for .py file references and run ruff check.
Called after each tool round — catches syntax/quality issues
immediately after code is created or edited.
"""
files = _extract_py_files(tool_results)
if not files:
return
if tui:
tui.console.print(
f" [dim {tui.MUTEDBLUE}]⏳ ruff check {' '.join(sorted(files))}[/]"
)
try:
result = subprocess.run(
["ruff", "check", *sorted(files), "--output-format=concise"],
capture_output=True, text=True, timeout=15,
)
if result.returncode == 0:
if tui:
tui.console.print(f" [bold {tui.GREEN}]✓ ruff[/] [dim {tui.MUTEDBLUE}]clean[/]")
else:
# Auto-fix with safe fixes
if tui:
tui.console.print(f" [dim {tui.MUTEDBLUE}] ↻ ruff --fix ...[/]")
subprocess.run(
["ruff", "check", *sorted(files), "--fix", "--quiet"],
capture_output=True, timeout=15,
)
# Re-check
recheck = subprocess.run(
["ruff", "check", *sorted(files), "--output-format=concise"],
capture_output=True, text=True, timeout=15,
)
if recheck.returncode == 0:
if tui:
tui.console.print(f" [bold {tui.GREEN}]✓ ruff[/] [dim {tui.MUTEDBLUE}]fixed and clean[/]")
else:
lines = recheck.stdout.strip().splitlines()
snippet = "\n".join(lines[:5])
if len(lines) > 5:
snippet += f"\n [dim {tui.MUTEDBLUE}]... and {len(lines)-5} more[/]"
if tui:
tui.console.print(f" [bold {tui.RED}]⚠ ruff (unfixable)[/]")
tui.console.print(snippet)
else:
print(f"ruff: {len(lines)} issue(s) remaining")
except FileNotFoundError:
pass # ruff not installed
except subprocess.TimeoutExpired:
if tui:
tui.console.print(f" [dim {tui.MUTEDBLUE}]⚠ ruff timed out[/]")
def _auto_pytest(tool_results: list[dict], tui=None) -> str | None:
"""Scan tool results for test files and auto-run pytest on them.
Runs after each tool round when the model creates or edits test files.
- If explicit test_*.py files are found, run pytest on those.
- If source files are modified, look for matching tests/test_<name>.py.
- Silently skips when no test files match (no noise).
Returns a markdown summary of failures (for the model to fix in the
next round) or None if all passed / nothing to run.
"""
all_files = _extract_py_files(tool_results)
if not all_files:
return
test_targets: set[str] = set()
for f in all_files:
rel = os.path.relpath(f)
# Direct test file match
if "/test_" in rel or rel.startswith("test_") or "/tests/" in rel:
test_targets.add(f)
else:
# Source file → look for matching test
basename = os.path.splitext(os.path.basename(f))[0]
candidate = os.path.join(os.path.dirname(f), "tests", f"test_{basename}.py")
if os.path.isfile(candidate):
test_targets.add(candidate)
# Also check project-root tests/
root_test = os.path.join(
os.path.dirname(os.path.dirname(f)), "tests", f"test_{basename}.py"
)
if root_test != candidate and os.path.isfile(root_test):
test_targets.add(root_test)
if not test_targets:
return None
if tui:
labels = [os.path.relpath(t) for t in sorted(test_targets)]
tui.console.print(
f" [dim {tui.MUTEDBLUE}]⏳ pytest {' '.join(labels)}[/]"
)
try:
result = subprocess.run(
["python3", "-m", "pytest", *sorted(test_targets), "-x", "--tb=short"],
capture_output=True, text=True, timeout=120,
)
if result.returncode == 0:
if tui:
last_line = (result.stdout.strip().splitlines() or ["all passed"])[-1]
tui.console.print(f" [bold {tui.GREEN}]✓ pytest[/] [dim {tui.MUTEDBLUE}]{last_line}[/]")
return None
else:
# Build a compact failure summary for the model
lines = result.stdout.strip().splitlines()
failure_lines = [line for line in lines if "FAILED" in line or "ERROR" in line or "assert" in line]
summary = "\n".join(failure_lines[-15:]) if failure_lines else result.stdout.strip()[:800]
if tui:
tui.console.print(f" [bold {tui.RED}]✗ pytest[/]")
for line in (failure_lines or lines)[:5]:
tui.console.print(f" {line}")
return f"*Auto-pytest found failures:*\n```\n{summary}\n```\n*Fix the test(s) above and re-run.*"
except FileNotFoundError:
return None # pytest not installed
except subprocess.TimeoutExpired:
if tui:
tui.console.print(f" [dim {tui.MUTEDBLUE}]⚠ pytest timed out (120s)[/]")
return "*Auto-pytest timed out (120s).*"
def run_tool_calls(
tool_calls: list,
plugin_callables: dict,
mcp_manager: MCPManager,
tui=None,
) -> list[dict]:
"""Execute all tool calls from one model turn concurrently."""
if not tool_calls:
return []
cpu_count = os.cpu_count() or 4
max_workers = min(cpu_count, len(tool_calls))
def _execute_one(tc: dict) -> dict:
func_name = tc["function"]["name"]
arguments_raw = tc["function"].get("arguments", "{}")
if isinstance(arguments_raw, str):
try:
arguments = json.loads(arguments_raw)
except json.JSONDecodeError:
arguments = {}
else:
arguments = arguments_raw
tool_call_id = tc.get("id", "")
if tui:
args_preview = ""
if isinstance(arguments, dict):
items = []
for k, v in arguments.items():
items.append(f"{k}={str(v)}")
args_preview = ", ".join(items)
tui.tool_start(func_name, args_preview)
start_time = time.monotonic()
ok = True
if func_name in plugin_callables:
try:
result = plugin_callables[func_name](**arguments)
if not isinstance(result, str):
result = json.dumps(result, ensure_ascii=False)
except Exception as exc:
result = f"(plugin error: {exc})"
ok = False
elif func_name.startswith("mcp__"):
try:
mcp_result = mcp_manager.call(func_name, arguments)
result = json.dumps(mcp_result, ensure_ascii=False, default=str)
except Exception as exc:
result = f"(mcp error: {exc})"
ok = False
else:
result = f"(unknown tool: {func_name})"
ok = False
duration = time.monotonic() - start_time
if tui:
tui.tool_end(func_name, result, duration, ok)
return {
"role": "tool",
"tool_call_id": tool_call_id,
"content": result,
}
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as pool:
return list(pool.map(_execute_one, tool_calls))
def learn_from_exchange(
provider: ZenProvider,
user_input: str,
agent_output: str,
skills_lib: SkillLibrary,
tui=None,
):
"""
Make one extra call to the model asking whether to learn a skill
from this exchange. If the model says learn=true, upsert into the
skill library.
"""
_max = 2000
truncated_input = user_input[:_max] + ("..." if len(user_input) > _max else "")
truncated_output = agent_output[:_max] + ("..." if len(agent_output) > _max else "")
messages = [
{
"role": "system",
"content": (
"You extract reusable skill patterns from conversations.\n\n"
"A skill is worth learning when the assistant solved a real problem — "
"wrote code, debugged, explained a technique, or performed multi-step work.\n\n"
"Examples:\n"
'- {"learn": true, "name": "live-elapsed-timer", "trigger": "add time counter", "body": "1) Record start time 2) Spawn daemon thread updating display every 0.5s 3) Show elapsed seconds 4) Join on exit"}\n'
'- {"learn": true, "name": "system-health-check", "trigger": "check system health", "body": "Run top -bn1, free -h, df -h, uptime to get system metrics"}\n\n'
"Respond STRICT JSON only. No markdown, no extra text.\n"
'If learn is false: {"learn": false}'
),
},
{
"role": "user",
"content": f"User query: {truncated_input}\n\nAssistant response: {truncated_output}",
},
]
try:
resp = provider.chat(messages, max_tokens=500, temperature=0.3)
content = resp.get("content", "")
if not content:
return
lines = content.strip().splitlines()
filtered = [line for line in lines if not line.startswith("```")]
content = "\n".join(filtered).strip()
data = json.loads(content)
if data.get("learn") and data.get("name"):
name = data["name"].strip()
body = (data.get("body") or agent_output[:1000]).strip()
trigger = (data.get("trigger") or user_input[:200]).strip()
# Quality heuristic: require meaningful body content
if len(body) < 50:
log.debug("Skill '%s' body too short (%d chars), skipping", name, len(body))
return
# Quality heuristic: reject overly generic names
generic_names = {"fix", "debug", "help", "solve", "patch", "workaround"}
if name.lower() in generic_names:
log.debug("Skill name '%s' too generic, skipping", name)
return
skills_lib.upsert(
name=name,
trigger=trigger,
body=body,
)
log.info("Learned new skill: %s", name)
if tui:
tui.learned_skill(name)
except Exception as exc:
log.debug("Learn-from-exchange skipped: %s", exc)
def run_turn(
provider: ZenProvider,
user_input: str,
memory_store: MemoryStore,
skills_lib: SkillLibrary,
plugin_callables: dict,
plugin_schemas: list,
mcp_manager: MCPManager,
config: dict,
tui=None,
*,
conversation_history: list | None = None,
) -> tuple[str, list]:
"""Process one turn: retrieve context, loop tool rounds, log, learn.
Returns (response_text, conversation_history) so history can be
passed to the next turn for continuity.
"""
history = list(conversation_history) if conversation_history else []
max_tool_rounds = config.get("max_tool_rounds", 200)
max_turn_duration = config.get("max_turn_duration", 300) # wall-clock timeout (seconds)
_turn_start = time.monotonic()
# Retrieve relevant context
skills_hits = skills_lib.relevant(user_input, k=3)
memory_hits = memory_store.relevant(user_input, k=5)
# Display retrieved context
if tui and (skills_hits or memory_hits):
tui.recall_header()
for skill, score in skills_hits:
tui.context_hit(f"skill:{skill.name}", skill.body, score)
for entry, score in memory_hits:
tui.context_hit("memory", entry.user_input, score)
tui.console.print()
system_prompt = build_system_prompt(skills_hits, memory_hits)
# Build messages: system + history (last 10 exchanges) + new user input
messages = [{"role": "system", "content": system_prompt}]
# Keep last 20 history entries (10 user/assistant pairs) to stay within token limits
messages.extend(history[-20:])
messages.append({"role": "user", "content": user_input})
all_tool_schemas = list(plugin_schemas)
all_tool_schemas.extend(mcp_manager.openai_tools())
final_content = ""
for round_num in range(max_tool_rounds):
# Wall-clock timeout check
elapsed = time.monotonic() - _turn_start
if elapsed > max_turn_duration:
if tui:
tui.console.print(f" [{tui.MIDBLUE}]⏰ Turn timed out ({elapsed:.0f}s > {max_turn_duration}s)[/]")
break
try:
resp = provider.chat(
messages,
tools=all_tool_schemas if all_tool_schemas else None,
max_tokens=16384,
temperature=0.4,
)
except requests.exceptions.RequestException as exc:
error_msg = f"*Model request failed after retries: {exc}*"
if tui:
tui.console.print(f" [bold {tui.RED}]✗ {error_msg}[/]")
# Final attempt: hand the error back as the response
final_content = error_msg
break
content = resp.get("content") or ""
tool_calls = resp.get("tool_calls")
# Show model's chain-of-thought reasoning if present
reasoning = resp.get("reasoning_content") or ""
if reasoning and tui and config.get("show_reasoning", True):
tui.reasoning(reasoning)
# If content is empty but we have reasoning (e.g. max_tokens still too low),
# use the reasoning as fallback so the agent can still respond
if not content and reasoning:
content = f"*[reasoning overshadows response — increase max_tokens for cleaner output]*\n\n{reasoning}"
assistant_msg = {"role": "assistant", "content": content}
if tool_calls:
assistant_msg["tool_calls"] = tool_calls
messages.append(assistant_msg)
if not tool_calls:
final_content = content
break
tool_results = run_tool_calls(
tool_calls, plugin_callables, mcp_manager, tui,
)
_auto_ruff_check(tool_results, tui)
pytest_fail = _auto_pytest(tool_results, tui)
if pytest_fail:
tool_results.append({
"role": "tool",
"content": pytest_fail,
"tool_call_id": "_auto_pytest",
})
messages.extend(tool_results)
if not final_content and messages:
for msg in reversed(messages):
if msg.get("role") == "assistant" and msg.get("content"):
final_content = msg["content"]
break
# If we still have no content or the last round ended on tool_calls,
# make one final provider call asking for a summary
if not final_content or (
next((m for m in reversed(messages) if m["role"] == "assistant"), {}).get("tool_calls")
):
if tui:
tui.reasoning("⏰ Tool rounds exhausted — requesting summary from model...")
messages.append({
"role": "user",
"content": (
"Tool rounds exhausted. "
"Please provide a summary of what you've discovered so far. "
"Be concise."
)
})
final_resp = provider.chat(messages, temperature=0.3)
summary = final_resp.get("content") or ""
if summary:
final_content = summary
memory_store.add(
user_input=user_input,
agent_output=final_content,
outcome="completed" if final_content else "failed",
)
for skill, _score in skills_hits:
skills_lib.record_use(skill.name, success=bool(final_content))
learn_from_exchange(provider, user_input, final_content, skills_lib, tui)
# Update conversation history with this turn
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": final_content})
# Keep only last 20 exchanges (40 messages) max
if len(history) > 40:
history = history[-40:]
return final_content, history
def _count_mcp_daemons() -> int:
"""Count running MCP daemon processes (brain, continuum, termux, etc.)."""
try:
r = subprocess.run(
["ps", "aux"],
capture_output=True, text=True, timeout=5,
)
count = 0
for line in r.stdout.split("\n"):
if "mcp" in line.lower() and "grep" not in line:
if any(name in line for name in ("brain-mcp", "continuum-mcp", "termux-mcp")):
count += 1
return count
except Exception:
return 0
def main():
_start_time = time.time()
parser = argparse.ArgumentParser(description="Eling — autonomous agent")
parser.add_argument(
"query", nargs="*",
help="Optional query (one-shot mode). If omitted, runs interactive REPL.",
)
parser.add_argument(
"--compact", action="store_true",
help="Compact display mode (no banner, minimal output)",
)
args = parser.parse_args()
config = load_config()
# Resolve relative DB paths based on config file location
_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":
print(
"ERROR: Set zen_api_key in config.json or ZEN_API_KEY env var.\n"
"Get a key at https://opencode.ai/zen"
)
sys.exit(1)
provider = ZenProvider(
api_key=zen_api_key,
model=config.get("zen_model", "zen/default"),
base_url=config.get("zen_base_url", "https://opencode.ai/zen/v1"),
)
# Clean up stale WAL/SHM files from previous uncheckpointed closes
_mem_path = config.get("memory_db", "agent_memory.db")
for _ext in ("-wal", "-shm"):
_orphan = _mem_path + _ext
if os.path.exists(_orphan):
try:
os.remove(_orphan)
except OSError:
pass
memory_store = MemoryStore(_mem_path)
skills_lib = SkillLibrary(config.get("skills_db", "agent_skills.db"))
# Prune stale skills on startup
try:
pruned = skills_lib.prune_unused(days=30)
if pruned:
log.info("Pruned %d unused skill(s) older than 30 days", pruned)
except Exception as exc:
log.debug("Skill prune (unused) skipped: %s", exc)
try:
low_pruned = skills_lib.prune_low_performer()
if low_pruned:
log.info("Pruned %d low-performing skill(s)", low_pruned)
except Exception as exc:
log.debug("Skill prune (low-performer) skipped: %s", exc)
# ── Memory pruning on startup ──────────────────────────────────
try:
dup_pruned = memory_store.prune_duplicates()
if dup_pruned:
log.info("Pruned %d duplicate memory entries", dup_pruned)
except Exception as exc:
log.debug("Duplicate pruning skipped: %s", exc)
try:
old_pruned = memory_store.prune_old(keep=1000)
if old_pruned:
log.info("Pruned %d old memory entries (keeping %d)", old_pruned, 1000)
except Exception as exc:
log.debug("Old-memory pruning skipped: %s", exc)
plugin_callables, plugin_schemas = load_plugins()
mcp_servers = {
k: v for k, v in config.get("mcp_servers", {}).items() if not k.startswith("_")
}
mcp_manager = MCPManager(mcp_servers)
# ── Initialise TUI ──────────────────────────────────────────────
if not args.compact:
from tui import ElingTUI
tui = ElingTUI(session_start=_start_time, theme=config.get("theme"))
# Apply verbose_tool_output config
if not config.get("verbose_tool_output", True):
tui.set_verbose_tool_output(False)
tui.clear_screen()
else:
print("\033[3J\033[2J\033[H", end="")
tui = None
# Count skills/memories for banner
skill_count = len(skills_lib.relevant("", k=9999))
mem_count = memory_store.count()
if tui:
tui.banner(
skills=skill_count,
memories=mem_count,
plugins=len(plugin_schemas),
mcp=len(mcp_manager.connections) + _count_mcp_daemons(),
model=config.get("zen_model", ""),
theme=tui._theme_name,
)
tui.console.print(
f"[dim {tui.MUTEDBLUE}]Type your query or 'exit' to quit.[/]"
)
tui.console.print()
else:
log.info(
"Eling ready — %d plugins, %d MCP server(s), %d skills, %d memories",
len(plugin_schemas), len(mcp_manager.connections) + _count_mcp_daemons(),
skill_count, mem_count,
)
try:
conversation_history: list = []
if args.query:
query_text = " ".join(args.query)
if tui:
tui.turn_start(query_text)
else:
print(f"\n> {query_text}")
response, conversation_history = run_turn(
provider, query_text,
memory_store, skills_lib,
plugin_callables, plugin_schemas,
mcp_manager, config, tui,
conversation_history=conversation_history,
)
if tui:
tui.assistant(response)
tui.turn_end()
else:
print(response)
else:
while True:
try:
if tui:
user_input = tui.input_prompt().strip()
else:
user_input = input("\n> ").strip()
except (EOFError, KeyboardInterrupt):
if tui:
tui.clear_screen()
tui.console.print("[dim]Goodbye![/]")
else:
print("\033[3J\033[2J\033[H", end="")
print("Goodbye!")
break
if not user_input:
continue
if user_input.strip() == "/new":
if tui:
tui.clear_screen()
else:
print("\033[3J\033[2J\033[H", end="")
ts = time.strftime("%Y%m%d_%H%M%S")
new_dir = os.path.join(os.path.expanduser("~"), "eling-workspace", f"session-{ts}")
os.makedirs(new_dir, exist_ok=True)
for fname in ("config.json", config.get("memory_db", "agent_memory.db"), config.get("skills_db", "agent_skills.db")):
src = os.path.abspath(fname)
dst = os.path.join(new_dir, os.path.basename(fname))
if os.path.exists(src) and not os.path.exists(dst):
shutil.copy2(src, dst)
if tui:
tui.console.print(f"[bold {tui.MIDBLUE}]\u267b Restarting in {new_dir}...[/]")
mcp_manager.stop_all()
memory_store.close()
skills_lib.close()
os.chdir(new_dir)
os.execv(sys.executable, [sys.executable] + sys.argv)
if user_input.lower() in ("exit", "quit", "/exit"):
if tui:
tui.clear_screen()
tui.console.print("[dim]Goodbye![/]")
else:
print("\033[3J\033[2J\033[H", end="")
break
if tui:
tui.turn_start(user_input, show_input=False)
else:
print(f"\n> {user_input}")
try:
if tui:
with tui.thinking():
response, conversation_history = run_turn(
provider, user_input,
memory_store, skills_lib,
plugin_callables, plugin_schemas,
mcp_manager, config, tui,
conversation_history=conversation_history,
)
else:
response, conversation_history = run_turn(
provider, user_input,
memory_store, skills_lib,
plugin_callables, plugin_schemas,
mcp_manager, config, tui,
conversation_history=conversation_history,
)
except KeyboardInterrupt:
print()
if tui:
tui.console.print(f" [{tui.MIDBLUE}]⏹ Interrupted[/]")
else:
print("Interrupted.")
response = ""
continue
if tui:
tui.assistant(response)
tui.turn_end()
else:
print(response)
finally:
log.info("Shutting down...")
mcp_manager.stop_all()
memory_store.close()
skills_lib.close()
if __name__ == "__main__":
main()