-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.py
More file actions
383 lines (314 loc) · 12.3 KB
/
cli.py
File metadata and controls
383 lines (314 loc) · 12.3 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
"""Mr.Stack CLI — command-line interface for the proactive AI butler."""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from datetime import datetime, timedelta
from pathlib import Path
import typer
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from . import __version__
from .banner import (
BRAND_CYAN,
BRAND_DIM,
BRAND_GREEN,
BRAND_MAGENTA,
COMPACT_LOGO,
print_banner,
print_divider,
status_icon,
)
from .constants import (
BOT_COMMAND,
DATA_DIR,
DB_FILE,
ENV_FILE,
IS_LINUX,
IS_MACOS,
IS_WINDOWS,
LOG_DIR,
MEMORY_DIR,
find_site_packages,
resolve_env_value,
)
from .daemon import (
daemon_install,
daemon_uninstall,
find_bot_pid,
is_running,
start_background,
start_foreground,
stop_bot,
)
console = Console()
app = typer.Typer(
name="mrstack",
help="Mr.Stack — Proactive AI Butler for Claude Code",
no_args_is_help=True,
pretty_exceptions_enable=False,
)
# ── version ────────────────────────────────────────────
def _version_callback(value: bool) -> None:
if value:
print_banner(console, compact=True)
raise typer.Exit()
@app.callback()
def main(
version: bool = typer.Option(
False, "--version", "-v", callback=_version_callback, is_eager=True,
help="Show version and exit.",
),
) -> None:
"""Mr.Stack — Proactive AI Butler for Claude Code."""
# ── init ───────────────────────────────────────────────
@app.command()
def init() -> None:
"""Interactive setup wizard."""
from .wizard import run_wizard
run_wizard()
# ── start ──────────────────────────────────────────────
@app.command()
def start(
background: bool = typer.Option(False, "--bg", "-b", help="Run in background."),
) -> None:
"""Start the bot."""
print_banner(console, compact=True)
if background:
start_background()
_print_quick_status()
else:
start_foreground()
# ── stop ───────────────────────────────────────────────
@app.command()
def stop() -> None:
"""Stop the bot."""
stop_bot()
# ── daemon ─────────────────────────────────────────────
@app.command()
def daemon(
uninstall: bool = typer.Option(False, "--uninstall", "-u", help="Remove daemon."),
) -> None:
"""Install/uninstall as a system daemon (launchd/systemd)."""
if uninstall:
daemon_uninstall()
else:
print_banner(console, compact=True)
daemon_install()
# ── status ─────────────────────────────────────────────
@app.command()
def status() -> None:
"""Show current status."""
print_banner(console, compact=True)
print_divider(console)
pid = find_bot_pid()
running = pid is not None
# Status
if running:
console.print(f" {status_icon(True)} Status [green bold]Running[/] [dim](PID {pid})[/]")
else:
console.print(f" {status_icon(False)} Status [red bold]Stopped[/]")
# Uptime
uptime_str = ""
if running and pid:
try:
import psutil
proc = psutil.Process(pid)
uptime = datetime.now() - datetime.fromtimestamp(proc.create_time())
days = uptime.days
hours, rem = divmod(uptime.seconds, 3600)
mins = rem // 60
parts = []
if days:
parts.append(f"{days}d")
if hours:
parts.append(f"{hours}h")
parts.append(f"{mins}m")
uptime_str = " ".join(parts)
except Exception:
uptime_str = "?"
console.print(f" {status_icon(True)} Uptime [bold]{uptime_str}[/]")
# Memory
memory_count = 0
if MEMORY_DIR.is_dir():
memory_count = sum(1 for _ in MEMORY_DIR.rglob("*.md"))
console.print(f" {status_icon(memory_count > 0)} Memory [bold]{memory_count}[/] entries")
# Last message
last_msg = _get_last_message_time()
console.print(f" {status_icon(last_msg != '—')} Last msg {last_msg}")
# Jarvis
jarvis_enabled = resolve_env_value("ENABLE_JARVIS", "false").lower() == "true"
if jarvis_enabled:
if IS_MACOS:
console.print(f" {status_icon(True)} Jarvis [green bold]ON[/]")
else:
console.print(f" {status_icon(True)} Jarvis [yellow bold]ON[/] [dim](limited)[/]")
else:
console.print(f" {status_icon(False)} Jarvis [dim]OFF[/]")
# Platform
platform_label = "macOS" if IS_MACOS else ("Linux" if IS_LINUX else "Windows")
console.print(f" {status_icon(True)} Platform {platform_label}")
print_divider(console)
# Quick hints
if not running:
console.print(f" [dim]Start: [/][bold]mrstack start[/]")
else:
console.print(f" [dim]Logs: [/][bold]mrstack logs -f[/] [dim]| Stop: [/][bold]mrstack stop[/]")
console.print()
# ── logs ───────────────────────────────────────────────
@app.command()
def logs(
lines: int = typer.Option(50, "--lines", "-n", help="Number of lines."),
follow: bool = typer.Option(False, "--follow", "-f", help="Follow log output."),
) -> None:
"""View recent logs."""
log_file = LOG_DIR / "daemon-stdout.log"
if not log_file.is_file():
log_file = LOG_DIR / "stdout.log"
if not log_file.is_file():
console.print("[yellow]No log files found.[/]")
raise typer.Exit(1)
cmd = ["tail"]
if follow:
cmd.append("-f")
cmd.extend(["-n", str(lines), str(log_file)])
try:
subprocess.run(cmd)
except KeyboardInterrupt:
pass
except FileNotFoundError:
console.print("[red]'tail' command not found.[/]")
content = log_file.read_text()
for line in content.splitlines()[-lines:]:
console.print(line)
# ── config ─────────────────────────────────────────────
@app.command()
def config() -> None:
"""Open configuration file in editor."""
if not ENV_FILE.is_file():
console.print("[red].env not found.[/] Run [bold]mrstack init[/] first.")
raise typer.Exit(1)
editor = os.environ.get("EDITOR", "vim")
subprocess.run([editor, str(ENV_FILE)])
# ── jarvis ─────────────────────────────────────────────
@app.command()
def jarvis(
state: str = typer.Argument(
..., help="on / off", metavar="STATE",
),
) -> None:
"""Toggle Jarvis mode."""
state = state.strip().lower()
if state not in ("on", "off"):
console.print("[red]Usage: mrstack jarvis on|off[/]")
raise typer.Exit(1)
if not ENV_FILE.is_file():
console.print("[red].env not found.[/]")
raise typer.Exit(1)
enable = state == "on"
if enable and not IS_MACOS:
console.print(
"[yellow]Jarvis has limited functionality on this platform.[/]\n"
"[dim] Active app detection and Chrome tab reading are macOS-only.[/]"
)
text = ENV_FILE.read_text()
new_val = "true" if enable else "false"
if "ENABLE_JARVIS=" in text:
import re
text = re.sub(r"ENABLE_JARVIS=\w+", f"ENABLE_JARVIS={new_val}", text)
else:
text += f"\nENABLE_JARVIS={new_val}\n"
ENV_FILE.write_text(text)
if enable:
console.print(f" {status_icon(True)} Jarvis [green bold]ON[/]")
else:
console.print(f" {status_icon(False)} Jarvis [dim]OFF[/]")
if is_running():
console.print(" [dim]Restart for changes to take effect.[/]")
# ── patch ──────────────────────────────────────────────
@app.command()
def patch(
force: bool = typer.Option(False, "--force", "-f", help="Overwrite existing files."),
) -> None:
"""Install/update Mr.Stack modules into claude-code-telegram."""
from .patcher import patch_install
patch_install(force=force)
# ── update ─────────────────────────────────────────────
@app.command()
def update() -> None:
"""Update Mr.Stack to the latest version."""
console.print(f" {status_icon(True)} Updating Mr.Stack...")
if shutil.which("uv"):
subprocess.run(["uv", "tool", "upgrade", "mrstack"], check=True)
elif shutil.which("pipx"):
subprocess.run(["pipx", "upgrade", "mrstack"], check=True)
else:
subprocess.run(["pip", "install", "--upgrade", "mrstack"], check=True)
console.print(f" {status_icon(True)} Re-applying patches...")
from .patcher import patch_install
patch_install(force=True)
console.print(f" [green bold]Update complete.[/]")
# ── version (explicit command) ─────────────────────────
@app.command(name="version")
def version_cmd() -> None:
"""Show version information."""
from .banner import LOGO, STACK_ICON
console.print(STACK_ICON)
console.print(LOGO)
print_divider(console)
console.print(f" {status_icon(True)} Mr.Stack [bold]v{__version__}[/]")
# claude-code-telegram version
try:
from importlib.metadata import version as pkg_version
cct_ver = pkg_version("claude-code-telegram")
console.print(f" {status_icon(True)} claude-code-telegram [bold]v{cct_ver}[/]")
except Exception:
console.print(f" {status_icon(False)} claude-code-telegram [dim]not installed[/]")
# Claude Code version
try:
result = subprocess.run(
["claude", "--version"], capture_output=True, text=True, timeout=5
)
if result.returncode == 0:
console.print(f" {status_icon(True)} Claude Code [bold]{result.stdout.strip()}[/]")
except Exception:
console.print(f" {status_icon(False)} Claude Code [dim]not found[/]")
# Platform
import platform
console.print(f" {status_icon(True)} Platform {platform.system()} {platform.machine()}")
console.print(f" {status_icon(True)} Python {platform.python_version()}")
print_divider(console)
console.print()
# ── Helpers ────────────────────────────────────────────
def _get_last_message_time() -> str:
if DB_FILE.is_file():
try:
import sqlite3
with sqlite3.connect(str(DB_FILE)) as conn:
row = conn.execute(
"SELECT MAX(created_at) FROM messages"
).fetchone()
if row and row[0]:
ts = datetime.fromisoformat(row[0])
delta = datetime.now() - ts
if delta < timedelta(minutes=1):
return "just now"
elif delta < timedelta(hours=1):
return f"{int(delta.total_seconds() // 60)}m ago"
elif delta < timedelta(days=1):
return f"{int(delta.total_seconds() // 3600)}h ago"
else:
return ts.strftime("%Y-%m-%d %H:%M")
except Exception:
pass
return "[dim]—[/]"
def _print_quick_status() -> None:
"""Print a quick 2-line status after start."""
pid = find_bot_pid()
if pid:
jarvis = resolve_env_value("ENABLE_JARVIS", "false").lower() == "true"
j_str = "[green]ON[/]" if jarvis else "[dim]OFF[/]"
console.print(f" {status_icon(True)} Jarvis: {j_str} {status_icon(True)} Logs: [bold]mrstack logs -f[/]")
console.print()