Entry point for ``clawcodex agent-server`` (fast-path subcommand).
(argv: list[str])
| 57 | |
| 58 | |
| 59 | def run_agent_server_subcommand(argv: list[str]) -> int: |
| 60 | """Entry point for ``clawcodex agent-server`` (fast-path subcommand).""" |
| 61 | parser = argparse.ArgumentParser( |
| 62 | prog="clawcodex agent-server", |
| 63 | description="Run the Direct Connect agent server (TUI-client backend).", |
| 64 | ) |
| 65 | parser.add_argument("--host", default="127.0.0.1", |
| 66 | help="Bind address (default: 127.0.0.1 — loopback only).") |
| 67 | parser.add_argument("--port", type=int, default=0, |
| 68 | help="HTTP port for POST /sessions (default: ephemeral).") |
| 69 | parser.add_argument("--token", default="", |
| 70 | help="Optional bearer token required on POST /sessions.") |
| 71 | parser.add_argument("--provider", default=None, help="Provider name override.") |
| 72 | parser.add_argument("--model", default=None, help="Model override.") |
| 73 | parser.add_argument("--permission-mode", default="default", |
| 74 | dest="permission_mode", |
| 75 | help="default | acceptEdits | bypassPermissions | plan | auto") |
| 76 | parser.add_argument("--workspace", default=None, |
| 77 | help="Workspace root the agent operates in (default: cwd).") |
| 78 | parser.add_argument("--max-turns", type=int, default=20, dest="max_turns") |
| 79 | parser.add_argument( |
| 80 | "--exit-on-parent", action="store_true", dest="exit_on_parent", |
| 81 | help="Exit when stdin reaches EOF — used when a parent TUI spawns this " |
| 82 | "server, so the backend reliably dies if the TUI crashes (hermes route).", |
| 83 | ) |
| 84 | parser.add_argument( |
| 85 | "--stdio", action="store_true", |
| 86 | help="Talk NDJSON over stdin/stdout (one session) instead of HTTP+" |
| 87 | "WebSocket — the default LOCAL transport for the TUI (a pipe can't " |
| 88 | "idle-time-out). stdout is reserved for JSON frames; diagnostics go " |
| 89 | "to stderr; stdin EOF ends the session.", |
| 90 | ) |
| 91 | args = parser.parse_args(argv) |
| 92 | |
| 93 | # In --stdio mode the inbound reader owns stdin, so the EOF watcher (which |
| 94 | # also reads stdin) must NOT run — EOF is detected by the reader instead. |
| 95 | if args.exit_on_parent and not args.stdio: |
| 96 | _exit_when_stdin_closes() |
| 97 | |
| 98 | workspace = str(Path(args.workspace).resolve()) if args.workspace else str(Path.cwd()) |
| 99 | |
| 100 | agent_config = AgentServerConfig( |
| 101 | provider_name=args.provider, |
| 102 | model=args.model, |
| 103 | permission_mode=args.permission_mode, |
| 104 | max_turns=args.max_turns, |
| 105 | ) |
| 106 | |
| 107 | if args.stdio: |
| 108 | try: |
| 109 | return asyncio.run(_serve_stdio(workspace, agent_config)) |
| 110 | except KeyboardInterrupt: |
| 111 | return 0 |
| 112 | |
| 113 | try: |
| 114 | return asyncio.run(_serve(args, workspace, agent_config)) |
| 115 | except KeyboardInterrupt: |
| 116 | print("\nagent-server: shutting down") |
no test coverage detected