-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·122 lines (103 loc) · 3.52 KB
/
cli.py
File metadata and controls
executable file
·122 lines (103 loc) · 3.52 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
#!/usr/bin/env python3
"""
openqueen — CLI entry point.
Usage:
openqueen init Interactive setup wizard
openqueen status Show running tasks and recent logs
openqueen run <task> Run a task.md file directly
openqueen logs [n] Tail last n lines of dispatch log (default 50)
openqueen version Show version
"""
import os
import subprocess
import sys
from pathlib import Path
OQ_HOME = Path(os.environ.get("OPENQUEEN_HOME", str(Path.home() / "openqueen")))
VERSION = "0.1.0"
def cmd_init():
init_script = OQ_HOME / "init.py"
if not init_script.exists():
print(f"Error: {init_script} not found", file=sys.stderr)
sys.exit(1)
os.execv(sys.executable, [sys.executable, str(init_script)])
def cmd_status():
import json
from datetime import datetime
queue_file = OQ_HOME / "QUEUE.json"
logs_dir = OQ_HOME / "logs" / "sessions"
print(f"\nOpenQueen Status (home: {OQ_HOME})\n")
# Active tasks from QUEUE.json
if queue_file.exists():
try:
queue = json.loads(queue_file.read_text())
# QUEUE.json is a list (dispatch.py format)
if isinstance(queue, list):
items = queue
elif isinstance(queue, dict):
items = list(queue.values())
else:
items = []
if items:
print(f" Queued tasks ({len(items)}):")
for item in items:
label = item.get("summary") or item.get("task_name", "?")
print(f" {label}")
else:
print(" No queued tasks")
except Exception:
print(" Could not read queue")
else:
print(" Queue file not found")
# Recent sessions
if logs_dir.exists():
sessions = sorted(logs_dir.glob("*.log"), key=lambda f: f.stat().st_mtime, reverse=True)[:3]
if sessions:
print(f"\n Recent sessions:")
for s in sessions:
mtime = datetime.fromtimestamp(s.stat().st_mtime).strftime("%m-%d %H:%M")
print(f" {mtime} {s.name}")
print()
def cmd_run(task_file: str):
agent = OQ_HOME / "agent.py"
venv_python = OQ_HOME / ".venv" / "bin" / "python3"
python = str(venv_python) if venv_python.exists() else sys.executable
os.execv(python, [python, str(agent), task_file])
def cmd_logs(n: int = 50):
logs_dir = OQ_HOME / "logs" / "sessions"
if not logs_dir.exists():
print("No logs directory found")
sys.exit(1)
sessions = sorted(logs_dir.glob("*.log"), key=lambda f: f.stat().st_mtime, reverse=True)
if not sessions:
print("No session logs found")
sys.exit(1)
latest = sessions[0]
print(f"==> {latest} <==\n")
lines = latest.read_text().splitlines()
for line in lines[-n:]:
print(line)
def main():
args = sys.argv[1:]
if not args or args[0] in ("-h", "--help"):
print(__doc__)
sys.exit(0)
cmd = args[0]
if cmd == "init":
cmd_init()
elif cmd == "status":
cmd_status()
elif cmd == "run":
if len(args) < 2:
print("Usage: openqueen run <task.md>", file=sys.stderr)
sys.exit(1)
cmd_run(args[1])
elif cmd == "logs":
n = int(args[1]) if len(args) > 1 else 50
cmd_logs(n)
elif cmd == "version":
print(f"openqueen {VERSION}")
else:
print(f"Unknown command: {cmd}\n{__doc__}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()