Files
Auto-Trader/agent/cli/commands/show.py
T
Haozhe Wu b5a44a1e56 refactor(cli): split agent/cli.py into agent/cli/ package
Restructures the 3216-line agent/cli.py into a focused package
while preserving every existing surface verbatim.

Legacy compatibility:
- _legacy.py shim keeps every subcommand (serve, run, hypothesis,
  alpha, memory, provider, swarm, sessions, ...) reachable through
  the same CLI flags and console-script entry.
- Public symbols (cmd_*, _INIT_ENV_PATH, Confirm, AGENT_DIR, ...)
  re-exported through cli.<name>; `from cli import *` exposes 76
  symbols (was 1).
- Package-level cli.RUNS_DIR / cli.SESSIONS_DIR monkeypatches now
  propagate into _legacy before any cmd_* runs.

New scaffolding:
- agent/cli/main.py interactive REPL front door
- agent/cli/commands/ slash router + handlers (15 commands)
- agent/cli/components/ reusable Rich UI building blocks
- agent/cli/{theme,intro,onboard,input,completer,stream}.py
- agent/cli/__main__.py restores `python -m cli`
- agent/cli/_version.py reads importlib.metadata.version() so
  --version output and the banner stay aligned with pyproject.toml

Production wiring:
- New FastAPI middleware in api_server.py serves
  frontend/dist/index.html on browser navigation to /runs/{id}
  and /correlation, regex-narrowed so /runs/{id}/code and
  /runs/{id}/pine API routes stay reachable.
- frontend/vite.config.ts dev proxy mirrors the same narrowing.
- scripts/dev backend starts via package-safe
  `python -c 'import cli; cli.main(...)'`.

Chat-gate narrowed: chat --help / chat -h / chat --max-iter <bad> /
chat extra correctly reach legacy argparse instead of being
swallowed by the new REPL.

Tests: 2271 passed, 43 skipped (focused CLI/SPA test files:
test_cli_interactive.py 67 + test_spa_deep_link.py 13 + existing
CLI memory/init/hypothesis 48 = 128).
2026-05-22 00:00:47 +08:00

87 lines
2.5 KiB
Python

"""``/show``, ``/skill``, ``/pine`` — read-only inspect commands.
All three already exist as fully-featured ``cmd_*`` callables in
``cli._legacy``; this module is a thin shim so the slash router can
dispatch by command name without importing the legacy module top-level
(which is heavy — it pulls in providers, swarm, etc.).
"""
from __future__ import annotations
from typing import Any
from rich.console import Console
from rich.text import Text
from cli.theme import get_console
def _resolve_console() -> Console:
"""Return the shared CLI console."""
return get_console()
def cmd_show(ctx: Any = None, *args: str) -> int: # noqa: ARG001
"""``/show <run_id>`` — replay a prior run via the legacy handler."""
console = _resolve_console()
if not args:
console.print(Text("Usage: /show <run_id>", style="bold red"))
return 1
try:
from cli._legacy import cmd_show as legacy_show
legacy_show(args[0])
return 0
except Exception as exc: # noqa: BLE001
console.print(Text(f"/show failed: {exc}", style="bold red"))
return 1
def cmd_skill(ctx: Any = None, *args: str) -> int: # noqa: ARG001
"""``/skill`` — list bundled + user skills (legacy ``cmd_skills``)."""
console = _resolve_console()
try:
from cli._legacy import cmd_skills
cmd_skills()
return 0
except Exception as exc: # noqa: BLE001
console.print(Text(f"/skill failed: {exc}", style="bold red"))
return 1
def cmd_pine(ctx: Any = None, *args: str) -> int: # noqa: ARG001
"""``/pine <run_id>`` — emit Pine Script for a prior backtest."""
console = _resolve_console()
if not args:
console.print(Text("Usage: /pine <run_id>", style="bold red"))
return 1
try:
from cli._legacy import cmd_pine as legacy_pine
legacy_pine(args[0])
return 0
except Exception as exc: # noqa: BLE001
console.print(Text(f"/pine failed: {exc}", style="bold red"))
return 1
_DISPATCH = {
"show": cmd_show,
"skill": cmd_skill,
"pine": cmd_pine,
}
def run(ctx: Any = None, command: str = "show", *args: str) -> int:
"""Dispatch ``run("show", run_id)`` / ``run("skill")`` etc."""
handler = _DISPATCH.get(command)
if handler is None:
console = _resolve_console()
console.print(Text(f"Unknown show command: /{command}", style="bold red"))
return 1
return handler(ctx, *args)
__all__ = ["run", "cmd_show", "cmd_skill", "cmd_pine"]