| 1 | """Entry point for the installed ViMax application.""" |
| 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | import shutil |
| 6 | import subprocess |
| 7 | import sys |
| 8 | from importlib.metadata import version |
| 9 | from pathlib import Path |
| 10 | |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parent |
| 13 | APP = ROOT / "vimax_package" / "app" |
| 14 | if not APP.is_dir(): |
| 15 | APP = ROOT.parent |
| 16 | |
| 17 | |
| 18 | def init_workspace(workspace: Path) -> None: |
| 19 | config = workspace / "configs" / "agent.local.yaml" |
| 20 | config.parent.mkdir(parents=True, exist_ok=True) |
| 21 | example = (APP / "configs" / "agent.example.yaml").read_bytes() |
| 22 | try: |
| 23 | fd = os.open(config, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) |
| 24 | except FileExistsError: |
| 25 | print(f"Using existing {config}") |
| 26 | return |
| 27 | with os.fdopen(fd, "wb") as stream: |
| 28 | stream.write(example) |
| 29 | print(f"Created {config}") |
| 30 | |
| 31 | |
| 32 | def npm_command() -> str: |
| 33 | npm = shutil.which("npm.cmd" if os.name == "nt" else "npm") |
| 34 | if not npm: |
| 35 | raise RuntimeError("Install Node.js and npm to use the ViMax TUI or Web UI.") |
| 36 | return npm |
| 37 | |
| 38 | |
| 39 | def client_path(workspace: Path, client: str) -> Path: |
| 40 | return workspace / ".vimax" / "clients" / version("vimax") / ("ui" if client == "tui" else "web") |
| 41 | |
| 42 | |
| 43 | def install_client(workspace: Path, client: str) -> None: |
| 44 | npm = npm_command() |
| 45 | destination = client_path(workspace, client) |
| 46 | shutil.copytree( |
| 47 | APP / destination.name, |
| 48 | destination, |
| 49 | dirs_exist_ok=True, |
| 50 | ignore=shutil.ignore_patterns("node_modules", "dist"), |
| 51 | ) |
| 52 | subprocess.run([npm, "ci", "--no-audit", "--no-fund"], cwd=destination, check=True) |
| 53 | if client == "web": |
| 54 | subprocess.run([npm, "run", "build"], cwd=destination, check=True) |
| 55 | |
| 56 | |
| 57 | def main() -> int: |
| 58 | parser = argparse.ArgumentParser(prog="vimax") |
| 59 | parser.add_argument("--version", action="version", version=f"ViMax {version('vimax')}") |
| 60 | commands = parser.add_subparsers(dest="command", required=True) |
| 61 | commands.add_parser("init", help="Create the workspace's provider configuration") |
| 62 | setup = commands.add_parser("ui-install", help="Install the Node.js dependencies for a client") |
| 63 | setup.add_argument("client", choices=("tui", "web")) |
| 64 | commands.add_parser("agent", add_help=False, help="Run the Python agent") |
| 65 | mcp = commands.add_parser("mcp", help="Serve ViMax tools over local MCP stdio") |
| 66 | mcp.add_argument("--workspace", type=Path, default=None, help="ViMax workspace (defaults to VIMAX_WORKSPACE_ROOT or the current directory)") |
| 67 | commands.add_parser("tui", add_help=False, help="Run the terminal UI") |
| 68 | commands.add_parser("web", help="Serve the built Web UI") |
| 69 | args, extra = parser.parse_known_args() |
| 70 | if extra and args.command not in ("agent", "tui"): |
| 71 | parser.error(f"unrecognized arguments: {' '.join(extra)}") |
| 72 | |
| 73 | workspace = Path.cwd() |
| 74 | try: |
| 75 | if args.command == "init": |
| 76 | init_workspace(workspace) |
| 77 | elif args.command == "ui-install": |
| 78 | install_client(workspace, args.client) |
| 79 | elif args.command == "agent": |
| 80 | return subprocess.call([sys.executable, str(APP / "main_agent.py"), *extra]) |
| 81 | elif args.command == "mcp": |
| 82 | workspace = args.workspace or Path(os.environ.get("VIMAX_WORKSPACE_ROOT", ".")) |
| 83 | return subprocess.call([sys.executable, str(APP / "main_mcp.py")], cwd=workspace.resolve()) |
| 84 | else: |
| 85 | destination = client_path(workspace, args.command) |
| 86 | if args.command == "tui" and extra[:1] in (["-h"], ["--help"]): |
| 87 | print("Usage: vimax tui [new | resume [SESSION_ID] | --new-session | --session SESSION_ID]") |
| 88 | return 0 |
| 89 | marker = destination / ("node_modules/tsx/package.json" if args.command == "tui" else "dist/index.html") |
| 90 | if not marker.is_file(): |
| 91 | raise RuntimeError(f"Run 'vimax ui-install {args.command}' in this workspace first.") |
| 92 | env = { |
| 93 | **os.environ, |
| 94 | "VIMAX_WORKSPACE_ROOT": str(workspace), |
| 95 | "VIMAX_AGENT_ENTRY": str(APP / "main_agent.py"), |
| 96 | "VIMAX_LOGO_PATH": str(APP / "assets" / "vimax.png"), |
| 97 | "VIMAX_PYTHON_CMD": sys.executable, |
| 98 | } |
| 99 | command = [npm_command(), "run", "start"] |
| 100 | if args.command == "tui": |
| 101 | if extra[:1] == ["new"]: |
| 102 | extra[0] = "--new-session" |
| 103 | elif extra[:1] in (["resume"], ["old"]): |
| 104 | extra = extra[1:] |
| 105 | if extra and not extra[0].startswith("-"): |
| 106 | extra.insert(0, "--session") |
| 107 | command = [npm_command(), "run", "tui", "--", *extra] |
| 108 | return subprocess.call(command, cwd=destination, env=env) |
| 109 | except subprocess.CalledProcessError as exc: |
| 110 | return exc.returncode |
| 111 | except (OSError, RuntimeError) as exc: |
| 112 | parser.exit(1, f"vimax: {exc}\n") |
| 113 | except KeyboardInterrupt: |
| 114 | return 130 |
| 115 | return 0 |
| 116 |