AQIT 0.1.0
Loading...
Searching...
No Matches
commands_cli.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2"""aquin commands list | show | clear"""
3
4from __future__ import annotations
5
6import json
7import sys
8from typing import Any
9
10from aquin.command_log import clear_records, list_records, load_record
11
12
13def _parse_flag(args: list[str], name: str) -> str | None:
14 for i, a in enumerate(args):
15 if a == name and i + 1 < len(args):
16 return args[i + 1]
17 return None
18
19
20def cmd_commands_list(args: list[str]) -> None:
21 last_raw = _parse_flag(args, "--last")
22 since = _parse_flag(args, "--since")
23 category = _parse_flag(args, "--category")
24 model_id = _parse_flag(args, "--model")
25 command = _parse_flag(args, "--command")
26
27 last = int(last_raw) if last_raw else None
28 rows = list_records(last=last, since=since, category=category, model_id=model_id, command=command)
29 if not rows:
30 print("No tracked commands.")
31 return
32
33 for row in rows:
34 ok = "✓" if row.get("exit_ok", True) else "✗"
35 ts = str(row.get("ts", ""))[:19].replace("T", " ")
36 print(
37 f"{ok} {row['id'][:8]} {ts} [{row.get('category', '?')}] "
38 f"{row.get('summary', row.get('command', ''))}"
39 )
40
41
42def cmd_commands_show(args: list[str]) -> None:
43 if not args or args[0] in ("--help", "-h"):
44 print("Usage: aquin commands show <id>")
45 sys.exit(0 if args and args[0] in ("--help", "-h") else 1)
47 record_id = args[0]
48 rec = load_record(record_id)
49 if rec is None:
50 matches = [r for r in list_records() if str(r.get("id", "")).startswith(record_id)]
51 if len(matches) == 1:
52 rec = load_record(str(matches[0]["id"]))
53 elif len(matches) > 1:
54 print(f"Ambiguous id prefix '{record_id}' — {len(matches)} matches.")
55 sys.exit(1)
56 if rec is None:
57 print(f"Not found: {record_id}")
58 sys.exit(1)
59 print(json.dumps(rec, indent=2, default=str))
60
61
62def cmd_commands_clear(args: list[str]) -> None:
63 before = _parse_flag(args, "--before")
64 n = clear_records(before=before)
65 print(f"Cleared {n} local command record(s).")
67
68def cmd_commands(args: list[str]) -> None:
69 if not args or args[0] in ("--help", "-h", "help"):
70 print("Usage: aquin commands <subcommand>")
71 print("")
72 print(" list [--last N] [--since 2h] [--category inspection] [--model X]")
73 print(" show <id>")
74 print(" clear [--before 7d]")
75 return
76
77 sub = args[0]
78 rest = args[1:]
79 if sub == "list":
80 cmd_commands_list(rest)
81 elif sub == "show":
82 cmd_commands_show(rest)
83 elif sub == "send":
84 print("'aquin commands send' was removed (web CLI inbox no longer exists).")
85 print("Tracked runs stay local: aquin commands list | show")
86 sys.exit(1)
87 elif sub == "clear":
88 cmd_commands_clear(rest)
89 else:
90 print(f"Unknown subcommand: {sub}")
91 print("Use: aquin commands list | show | clear")
92 sys.exit(1)