|
| 1 | +import { spawn, spawnSync } from 'child_process'; |
| 2 | + |
| 3 | +import { CLIError } from '../errors/base'; |
| 4 | +import { ExitCode } from '../errors/codes'; |
| 5 | +import type { AgentId } from './types'; |
| 6 | + |
| 7 | +type NpmAgentId = Exclude<AgentId, 'grok' | 'hermes'>; |
| 8 | + |
| 9 | +const NPM_PACKAGES: Record<NpmAgentId, string> = { |
| 10 | + 'claude-code': '@anthropic-ai/claude-code@latest', |
| 11 | + codex: '@openai/codex', |
| 12 | + opencode: 'opencode-ai', |
| 13 | + pi: '@earendil-works/pi-coding-agent', |
| 14 | +}; |
| 15 | + |
| 16 | +const AGENT_EXECUTABLES: Record<NpmAgentId, string> = { |
| 17 | + 'claude-code': 'claude', |
| 18 | + codex: 'codex', |
| 19 | + opencode: 'opencode', |
| 20 | + pi: 'pi', |
| 21 | +}; |
| 22 | + |
| 23 | +const SUPPORTED_PLATFORMS = new Set<NodeJS.Platform>(['darwin', 'linux', 'win32']); |
| 24 | +const SUPPORTED_NATIVE_ARCHITECTURES = new Set(['arm64', 'x64']); |
| 25 | + |
| 26 | +export interface AgentInstallCommand { |
| 27 | + executable: string; |
| 28 | + args: string[]; |
| 29 | + display: string; |
| 30 | +} |
| 31 | + |
| 32 | +export interface AgentInstallEnvironment { |
| 33 | + platform?: NodeJS.Platform; |
| 34 | + arch?: string; |
| 35 | + nodeVersion?: string; |
| 36 | + commandExists?: (executable: string) => boolean; |
| 37 | +} |
| 38 | + |
| 39 | +export type AgentInstallRunner = (command: AgentInstallCommand) => Promise<number>; |
| 40 | + |
| 41 | +function defaultCommandExists(executable: string, platform: NodeJS.Platform): boolean { |
| 42 | + const command = platform === 'win32' ? 'where.exe' : executable; |
| 43 | + const args = platform === 'win32' ? [executable] : ['--version']; |
| 44 | + return spawnSync(command, args, { stdio: 'ignore', timeout: 5_000 }).status === 0; |
| 45 | +} |
| 46 | + |
| 47 | +export function getAgentInstallIssue( |
| 48 | + agent: AgentId, |
| 49 | + environment: AgentInstallEnvironment = {}, |
| 50 | +): string | undefined { |
| 51 | + if (agent === 'grok') { |
| 52 | + return 'The official Grok installer also changes Grok configuration; install it manually from ' |
| 53 | + + 'https://docs.x.ai/build/overview.'; |
| 54 | + } |
| 55 | + if (agent === 'hermes') { |
| 56 | + return 'The official Hermes installer also changes shell configuration; install it manually from ' |
| 57 | + + 'https://github.com/NousResearch/hermes-agent.'; |
| 58 | + } |
| 59 | + |
| 60 | + const platform = environment.platform ?? process.platform; |
| 61 | + const arch = environment.arch ?? process.arch; |
| 62 | + const nodeVersion = environment.nodeVersion ?? process.versions.node; |
| 63 | + const commandExists = environment.commandExists |
| 64 | + ?? ((executable: string) => defaultCommandExists(executable, platform)); |
| 65 | + |
| 66 | + if (!SUPPORTED_PLATFORMS.has(platform)) { |
| 67 | + return `Automated installation is not supported on ${platform}.`; |
| 68 | + } |
| 69 | + if (agent !== 'pi' && !SUPPORTED_NATIVE_ARCHITECTURES.has(arch)) { |
| 70 | + return `Automated installation is not supported on ${platform}/${arch}.`; |
| 71 | + } |
| 72 | + if (agent === 'pi') { |
| 73 | + const [major = 0, minor = 0] = nodeVersion.replace(/^v/, '').split('.').map(Number); |
| 74 | + if (major < 22 || (major === 22 && minor < 19)) { |
| 75 | + return `Pi requires Node.js 22.19 or newer (current: ${nodeVersion}).`; |
| 76 | + } |
| 77 | + } |
| 78 | + const requirements = platform === 'win32' ? ['cmd.exe', 'npm'] : ['npm']; |
| 79 | + const missing = requirements.filter(executable => !commandExists(executable)); |
| 80 | + if (missing.length > 0) return `Required command not found on PATH: ${missing.join(', ')}.`; |
| 81 | + return undefined; |
| 82 | +} |
| 83 | + |
| 84 | +function unavailableInstaller(agent: Extract<AgentId, 'grok' | 'hermes'>): never { |
| 85 | + throw new CLIError( |
| 86 | + `${agent} has no configuration-safe automated installer.`, |
| 87 | + ExitCode.GENERAL, |
| 88 | + getAgentInstallIssue(agent), |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export function getAgentInstallCommand( |
| 93 | + agent: AgentId, |
| 94 | + platform: NodeJS.Platform = process.platform, |
| 95 | +): AgentInstallCommand { |
| 96 | + if (agent === 'grok' || agent === 'hermes') return unavailableInstaller(agent); |
| 97 | + |
| 98 | + const args = [ |
| 99 | + 'install', |
| 100 | + '-g', |
| 101 | + ...(agent === 'pi' ? ['--ignore-scripts', '--engine-strict'] : []), |
| 102 | + NPM_PACKAGES[agent], |
| 103 | + ]; |
| 104 | + const display = `npm ${args.join(' ')}`; |
| 105 | + if (platform === 'win32') { |
| 106 | + return { |
| 107 | + executable: 'cmd.exe', |
| 108 | + args: ['/d', '/s', '/c', display], |
| 109 | + display, |
| 110 | + }; |
| 111 | + } |
| 112 | + return { executable: 'npm', args, display }; |
| 113 | +} |
| 114 | + |
| 115 | +export function getAgentVerificationCommand( |
| 116 | + agent: NpmAgentId, |
| 117 | + platform: NodeJS.Platform = process.platform, |
| 118 | +): AgentInstallCommand { |
| 119 | + const display = `${AGENT_EXECUTABLES[agent]} --version`; |
| 120 | + if (platform === 'win32') { |
| 121 | + return { |
| 122 | + executable: 'cmd.exe', |
| 123 | + args: ['/d', '/s', '/c', display], |
| 124 | + display, |
| 125 | + }; |
| 126 | + } |
| 127 | + return { executable: AGENT_EXECUTABLES[agent], args: ['--version'], display }; |
| 128 | +} |
| 129 | + |
| 130 | +async function runAgentInstallCommand(command: AgentInstallCommand): Promise<number> { |
| 131 | + return new Promise((resolvePromise, reject) => { |
| 132 | + const child = spawn(command.executable, command.args, { |
| 133 | + stdio: ['inherit', process.stderr, 'inherit'], |
| 134 | + }); |
| 135 | + child.once('error', reject); |
| 136 | + child.once('exit', code => resolvePromise(code ?? 1)); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +export async function installAgent( |
| 141 | + agent: AgentId, |
| 142 | + options: AgentInstallEnvironment & { runner?: AgentInstallRunner } = {}, |
| 143 | +): Promise<void> { |
| 144 | + const issue = getAgentInstallIssue(agent, options); |
| 145 | + if (issue) { |
| 146 | + throw new CLIError( |
| 147 | + `Cannot install ${agent}: ${issue}`, |
| 148 | + ExitCode.GENERAL, |
| 149 | + 'Leave it unselected and continue with configuration only.', |
| 150 | + ); |
| 151 | + } |
| 152 | + const installableAgent = agent as NpmAgentId; |
| 153 | + const command = getAgentInstallCommand(installableAgent, options.platform); |
| 154 | + const verificationCommand = getAgentVerificationCommand(installableAgent, options.platform); |
| 155 | + const hint = `Run the installer manually:\n${command.display}\nFor npm permission errors, see: ` |
| 156 | + + 'https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally'; |
| 157 | + const runner = options.runner ?? runAgentInstallCommand; |
| 158 | + |
| 159 | + let exitCode: number; |
| 160 | + try { |
| 161 | + exitCode = await runner(command); |
| 162 | + } catch (error) { |
| 163 | + const detail = error instanceof Error ? ` ${error.message}` : ''; |
| 164 | + throw new CLIError( |
| 165 | + `Could not start the installer for ${agent}.${detail}`, |
| 166 | + ExitCode.GENERAL, |
| 167 | + hint, |
| 168 | + ); |
| 169 | + } |
| 170 | + if (exitCode !== 0) { |
| 171 | + throw new CLIError( |
| 172 | + `Failed to install ${agent} (installer exited with code ${exitCode}).`, |
| 173 | + ExitCode.GENERAL, |
| 174 | + hint, |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + let verificationExitCode: number; |
| 179 | + try { |
| 180 | + verificationExitCode = await runner(verificationCommand); |
| 181 | + } catch (error) { |
| 182 | + const detail = error instanceof Error ? ` ${error.message}` : ''; |
| 183 | + throw new CLIError( |
| 184 | + `Installed ${agent}, but could not start ${verificationCommand.display}.${detail}`, |
| 185 | + ExitCode.GENERAL, |
| 186 | + `Open a new terminal and verify the installation manually:\n${verificationCommand.display}`, |
| 187 | + ); |
| 188 | + } |
| 189 | + if (verificationExitCode !== 0) { |
| 190 | + throw new CLIError( |
| 191 | + `Installed ${agent}, but ${verificationCommand.display} exited with code ${verificationExitCode}.`, |
| 192 | + ExitCode.GENERAL, |
| 193 | + `Open a new terminal and verify the installation manually:\n${verificationCommand.display}`, |
| 194 | + ); |
| 195 | + } |
| 196 | +} |
0 commit comments