|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Peaky Peek CLI - zero-friction onboarding and demo commands.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def run_seed() -> int: |
| 14 | + """Run the demo seed script.""" |
| 15 | + script_path = Path(__file__).parent.parent / "scripts" / "seed_demo_sessions.py" |
| 16 | + if not script_path.exists(): |
| 17 | + print(f"Error: seed script not found at {script_path}", file=sys.stderr) |
| 18 | + return 1 |
| 19 | + |
| 20 | + print("Seeding demo sessions...") |
| 21 | + result = subprocess.run([sys.executable, str(script_path)]) |
| 22 | + return result.returncode |
| 23 | + |
| 24 | + |
| 25 | +def run_serve() -> int: |
| 26 | + """Start the development server.""" |
| 27 | + print("Starting server on http://localhost:8000") |
| 28 | + print("Press Ctrl+C to stop") |
| 29 | + try: |
| 30 | + subprocess.run( |
| 31 | + [sys.executable, "-m", "uvicorn", "api.main:app", "--reload", "--port", "8000"], |
| 32 | + check=True, |
| 33 | + ) |
| 34 | + except KeyboardInterrupt: |
| 35 | + print("\nServer stopped") |
| 36 | + return 0 |
| 37 | + except subprocess.CalledProcessError as e: |
| 38 | + print(f"Error starting server: {e}", file=sys.stderr) |
| 39 | + return 1 |
| 40 | + except FileNotFoundError: |
| 41 | + print("Error: uvicorn not found. Install with: pip install uvicorn", file=sys.stderr) |
| 42 | + return 1 |
| 43 | + |
| 44 | + |
| 45 | +def run_demo() -> int: |
| 46 | + """Seed demo data and start server + frontend.""" |
| 47 | + # Step 1: Seed the data |
| 48 | + seed_result = run_seed() |
| 49 | + if seed_result != 0: |
| 50 | + return seed_result |
| 51 | + |
| 52 | + # Step 2: Start server in background |
| 53 | + print("\nStarting server in background...") |
| 54 | + try: |
| 55 | + server_process = subprocess.Popen( |
| 56 | + [sys.executable, "-m", "uvicorn", "api.main:app", "--reload", "--port", "8000"], |
| 57 | + stdout=subprocess.DEVNULL, |
| 58 | + stderr=subprocess.DEVNULL, |
| 59 | + ) |
| 60 | + except (FileNotFoundError, OSError) as e: |
| 61 | + print(f"Error starting server: {e}", file=sys.stderr) |
| 62 | + return 1 |
| 63 | + |
| 64 | + # Step 3: Start frontend in background |
| 65 | + print("Starting frontend in background...") |
| 66 | + frontend_dir = Path(__file__).parent.parent / "frontend" |
| 67 | + try: |
| 68 | + frontend_process = subprocess.Popen( |
| 69 | + ["npm", "run", "dev"], |
| 70 | + cwd=str(frontend_dir), |
| 71 | + env={**os.environ, "API_PORT": "8000"}, |
| 72 | + stdout=subprocess.DEVNULL, |
| 73 | + stderr=subprocess.DEVNULL, |
| 74 | + ) |
| 75 | + except (FileNotFoundError, OSError) as e: |
| 76 | + print(f"Error starting frontend: {e}", file=sys.stderr) |
| 77 | + server_process.terminate() |
| 78 | + return 1 |
| 79 | + |
| 80 | + # Step 4: Print instructions |
| 81 | + print("\n" + "=" * 60) |
| 82 | + print(" Peaky Peek demo is running!") |
| 83 | + print("=" * 60) |
| 84 | + print("\n Server: http://localhost:8000") |
| 85 | + print(" Frontend: http://localhost:5173") |
| 86 | + print("\n Press Ctrl+C to stop both processes") |
| 87 | + print("\n Open http://localhost:5173 to explore demo sessions") |
| 88 | + print("=" * 60 + "\n") |
| 89 | + |
| 90 | + # Wait for interrupt |
| 91 | + try: |
| 92 | + server_process.wait() |
| 93 | + frontend_process.wait() |
| 94 | + except KeyboardInterrupt: |
| 95 | + print("\nStopping server and frontend...") |
| 96 | + server_process.terminate() |
| 97 | + frontend_process.terminate() |
| 98 | + server_process.wait() |
| 99 | + frontend_process.wait() |
| 100 | + print("Done") |
| 101 | + |
| 102 | + return 0 |
| 103 | + |
| 104 | + |
| 105 | +def main() -> int: |
| 106 | + """CLI entry point.""" |
| 107 | + parser = argparse.ArgumentParser( |
| 108 | + prog="peaky-peek", |
| 109 | + description="Peaky Peek - AI agent debugger CLI", |
| 110 | + ) |
| 111 | + subparsers = parser.add_subparsers(dest="command", help="Available commands") |
| 112 | + |
| 113 | + subparsers.add_parser("demo", help="Seed demo data and launch server + frontend") |
| 114 | + subparsers.add_parser("seed", help="Seed benchmark demo sessions") |
| 115 | + subparsers.add_parser("serve", help="Start the development server") |
| 116 | + |
| 117 | + args = parser.parse_args() |
| 118 | + |
| 119 | + if args.command == "demo": |
| 120 | + return run_demo() |
| 121 | + elif args.command == "seed": |
| 122 | + return run_seed() |
| 123 | + elif args.command == "serve": |
| 124 | + return run_serve() |
| 125 | + else: |
| 126 | + parser.print_help() |
| 127 | + return 0 |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + sys.exit(main()) |
0 commit comments