"""Terminal agent: type a command, see the parse + plan, confirm to execute.""" import os, sys, json ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "model")); sys.path.insert(0, os.path.join(ROOT, "executor")) from runtime import CommandModel; import executor as EX m = CommandModel(os.path.join(ROOT, "model")); print(f"union command agent · {len(EX.CATALOG)} actions · platform {EX.platform_name()} · type 'quit' to exit") while True: try: t = input("\n> ").strip() except (EOFError, KeyboardInterrupt): break if not t or t in ("quit", "exit"): break out = m.predict(t); cmd = {k: v for k, v in out.items() if k != "_meta"}; p = EX.plan(cmd, None, t) print(json.dumps(cmd, ensure_ascii=False), f"(conf {out['_meta']['confidence']:.2f}, risk {p.risk})") if not p.supported: print(" ", p.note); continue print(f" [{p.kind}] {p.command}") if p.kind == "py" and p.action in ("unknown", "clarify"): print(" ", EX.run(p, cmd, dry_run=False)["output"]); continue conf = input(f" run it? (y / type '{p.action}' if critical / n) ").strip() ok, why = EX.gate(p, allow_caution=conf.lower() == "y", confirm_text=conf) if not ok and conf.lower() != "y": print(" ", why); continue r = EX.run(p, cmd, dry_run=False); print(f" {r['status']}: {r['output'][:600]}")