|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import path from "node:path"; |
| 3 | +import { formatErrorMessage } from "../infra/errors.js"; |
| 4 | +import { attachChildProcessBridge } from "../process/child-process-bridge.js"; |
| 5 | +import { TUI_SETUP_AUTH_SOURCE_CONFIG, TUI_SETUP_AUTH_SOURCE_ENV } from "./setup-launch-env.js"; |
| 6 | +import type { TuiOptions } from "./tui.js"; |
| 7 | + |
| 8 | +type TuiLaunchOptions = { |
| 9 | + authSource?: "config"; |
| 10 | + gatewayUrl?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +function appendOption(args: string[], flag: string, value: string | number | undefined): void { |
| 14 | + if (value === undefined) { |
| 15 | + return; |
| 16 | + } |
| 17 | + args.push(flag, String(value)); |
| 18 | +} |
| 19 | + |
| 20 | +function filterTuiExecArgv(execArgv: readonly string[]): string[] { |
| 21 | + const filtered: string[] = []; |
| 22 | + for (let index = 0; index < execArgv.length; index += 1) { |
| 23 | + const arg = execArgv[index] ?? ""; |
| 24 | + if ( |
| 25 | + arg === "--inspect" || |
| 26 | + arg.startsWith("--inspect=") || |
| 27 | + arg === "--inspect-brk" || |
| 28 | + arg.startsWith("--inspect-brk=") || |
| 29 | + arg === "--inspect-wait" || |
| 30 | + arg.startsWith("--inspect-wait=") |
| 31 | + ) { |
| 32 | + const next = execArgv[index + 1]; |
| 33 | + if (!arg.includes("=") && typeof next === "string" && !next.startsWith("-")) { |
| 34 | + index += 1; |
| 35 | + } |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (arg === "--inspect-port") { |
| 39 | + const next = execArgv[index + 1]; |
| 40 | + if (typeof next === "string" && !next.startsWith("-")) { |
| 41 | + index += 1; |
| 42 | + } |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (arg.startsWith("--inspect-port=")) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + filtered.push(arg); |
| 49 | + } |
| 50 | + return filtered; |
| 51 | +} |
| 52 | + |
| 53 | +function buildCurrentCliEntryArgs(): string[] { |
| 54 | + const entry = process.argv[1]?.trim(); |
| 55 | + if (!entry) { |
| 56 | + throw new Error("unable to relaunch TUI: current CLI entry path is unavailable"); |
| 57 | + } |
| 58 | + return path.isAbsolute(entry) ? [entry] : []; |
| 59 | +} |
| 60 | + |
| 61 | +function buildTuiCliArgs(opts: TuiOptions): string[] { |
| 62 | + const args = [...filterTuiExecArgv(process.execArgv), ...buildCurrentCliEntryArgs(), "tui"]; |
| 63 | + appendOption(args, "--url", opts.url); |
| 64 | + appendOption(args, "--token", opts.token); |
| 65 | + appendOption(args, "--password", opts.password); |
| 66 | + appendOption(args, "--session", opts.session); |
| 67 | + appendOption(args, "--thinking", opts.thinking); |
| 68 | + appendOption(args, "--message", opts.message); |
| 69 | + appendOption(args, "--timeout-ms", opts.timeoutMs); |
| 70 | + appendOption(args, "--history-limit", opts.historyLimit); |
| 71 | + if (opts.deliver) { |
| 72 | + args.push("--deliver"); |
| 73 | + } |
| 74 | + return args; |
| 75 | +} |
| 76 | + |
| 77 | +export async function launchTuiCli( |
| 78 | + opts: TuiOptions, |
| 79 | + launchOptions: TuiLaunchOptions = {}, |
| 80 | +): Promise<void> { |
| 81 | + const args = buildTuiCliArgs(opts); |
| 82 | + const env = |
| 83 | + launchOptions.gatewayUrl || launchOptions.authSource |
| 84 | + ? { |
| 85 | + ...process.env, |
| 86 | + ...(launchOptions.gatewayUrl ? { OPENCLAW_GATEWAY_URL: launchOptions.gatewayUrl } : {}), |
| 87 | + ...(launchOptions.authSource === "config" |
| 88 | + ? { [TUI_SETUP_AUTH_SOURCE_ENV]: TUI_SETUP_AUTH_SOURCE_CONFIG } |
| 89 | + : {}), |
| 90 | + } |
| 91 | + : process.env; |
| 92 | + const stdinWasPaused = |
| 93 | + typeof process.stdin.isPaused === "function" ? process.stdin.isPaused() : false; |
| 94 | + |
| 95 | + process.stdin.pause(); |
| 96 | + |
| 97 | + await new Promise<void>((resolve, reject) => { |
| 98 | + const child = spawn(process.execPath, args, { |
| 99 | + stdio: "inherit", |
| 100 | + env, |
| 101 | + }); |
| 102 | + const { detach } = attachChildProcessBridge(child); |
| 103 | + |
| 104 | + child.once("error", (error) => { |
| 105 | + detach(); |
| 106 | + reject(new Error(`failed to launch TUI: ${formatErrorMessage(error)}`)); |
| 107 | + }); |
| 108 | + |
| 109 | + child.once("exit", (code, signal) => { |
| 110 | + detach(); |
| 111 | + if (signal) { |
| 112 | + reject(new Error(`TUI exited from signal ${signal}`)); |
| 113 | + return; |
| 114 | + } |
| 115 | + if ((code ?? 0) !== 0) { |
| 116 | + reject(new Error(`TUI exited with code ${code ?? 1}`)); |
| 117 | + return; |
| 118 | + } |
| 119 | + resolve(); |
| 120 | + }); |
| 121 | + }).finally(() => { |
| 122 | + if (!stdinWasPaused) { |
| 123 | + process.stdin.resume(); |
| 124 | + } |
| 125 | + }); |
| 126 | +} |
0 commit comments