/** 대화형 CLI에도 명시적 argv와 같은 실행 정책을 적용한다. docs/specs/interactive-execution.md v1.1.0 */ const paths = require('../config/cli-paths'); const { shellTokens, quoteShellArgument } = require('../config/claude-execution-config'); function prepareLaunch({ cliCommand, cliArguments = [], model, profile, permissionBypass = false, maxTurns, cwd, env = process.env, terminalReplyLanguage }) { const tokens = shellTokens(cliCommand).map(token => token.value); if (!tokens.length) throw new Error('CLI command is required'); if (!Array.isArray(cliArguments) || !cliArguments.every(arg => typeof arg === 'string')) throw new Error('Invalid CLI arguments'); // 워크플로우와 프로젝트 터미널이 동일한 OMC bin 해석을 사용한다. if (cliArguments.some(arg => arg.includes('{{omcBinPath}}'))) { const bin = require('../services/omc-runtime').resolveOmcBin(); cliArguments = cliArguments.map(arg => arg.replaceAll('{{omcBinPath}}', bin)); } let command = tokens[0], args = [...tokens.slice(1), ...cliArguments]; let cli = paths.normalizeExecutableName(command); // 베어(bare) 명령어로 지정된 Antigravity(`antigravity` -> `agy`), Hermes(`~/.hermes/hermes-agent/venv/bin/hermes`), // Cursor(`cursor` -> `cursor-agent`) 등을 실제 설치 바이너리 경로로 해석한다. const catalogClis = ['antigravity', 'agy', 'hermes', 'hermes-agent', 'cursor', 'cursor-agent', 'aider', 'goose', 'qwen', 'codex', 'gemini', 'opencode', 'codeagent', 'pi', 'mimo', 'mimocode', 'gjc', 'ollama']; if (!/[\\/]/.test(command) && catalogClis.includes(cli)) { const resolvedBin = require('../herd/herd-cli-catalog').resolveCliBinaryPath(cli, env); if (resolvedBin) { command = resolvedBin; cli = cli === 'agy' ? 'antigravity' : cli === 'hermes-agent' ? 'hermes' : cli === 'cursor-agent' ? 'cursor' : cli; } else if (cli === 'antigravity') { command = 'agy'; } else if (cli === 'cursor') { command = 'cursor-agent'; } } if ((cli === 'hermes' || cli === 'hermes-agent') && require('../herd/herd-cli-catalog').shouldPrependHermesTui(args)) { args.unshift('--tui'); } // 호출자가 명시한 터미널 snapshot만 사용한다. 공유 실행기에서는 전역 설정을 읽지 않는다. const replyLanguage = terminalReplyLanguage && ['claude', 'opencode', 'codeagent'].includes(cli) ? require('./agent-reply-language').restore(terminalReplyLanguage, []) : undefined; if (model) { if (!paths.isModelFormatValid(cli, model)) throw new Error('Invalid model format for ' + cli); if (cli === 'hermes' || cli === 'hermes-agent') { if (!args.includes('-m')) args.push('-m', model); } else { args.push('--model', model); } } if (cli === 'claude') { if (permissionBypass && !args.includes('--dangerously-skip-permissions')) args.push('--dangerously-skip-permissions'); if (maxTurns !== undefined) { if (!Number.isSafeInteger(maxTurns) || maxTurns <= 0) throw new Error('Invalid CLI max turns'); args.push('--max-turns', String(maxTurns)); } } else if ((cli === 'antigravity' || cli === 'agy') && permissionBypass && !args.includes('--dangerously-skip-permissions')) { args.push('--dangerously-skip-permissions'); } else if ((cli === 'hermes' || cli === 'hermes-agent') && permissionBypass && !args.includes('--yolo')) { args.push('--yolo'); } const allowBypass = args.includes('--dangerously-skip-permissions') || args.includes('--yolo'); const selected = ['claude', 'opencode', 'codeagent'].includes(cli) ? require('../config/profile-execution-config').resolveProfile(cwd, profile) : profile; const rawArgs = [...args], rawEnv = { ...env }; const executionEnv = require('./execution-environment').getExecutionEnvironment(env, { cliCommand: quoteShellArgument(command), directArgs: args, cwd, profile: selected }); const runtimeCommand = command; if (process.platform === 'win32' && !/\.exe$/i.test(command)) { args = ['-NoLogo', '-NoProfile', '-Command', '& ' + [command, ...args].map(arg => quoteShellArgument(arg, true)).join(' ')]; command = 'powershell.exe'; } // 단순 미리보기/터미널 준비만으로 문맥 lease를 만들지 않는다. 공통 대화형 실행기가 // 실제 세션을 소유할 때 획득하며, 매 턴 자원 내용을 다시 수집해 재사용 여부를 정한다. const profilePreparation = { acquire: () => require('./cli-profile-scope').acquire({ command: runtimeCommand, profile: selected, cwd, ...(replyLanguage?.defaultLocale !== 'auto' && replyLanguage ? { terminalReplyLanguage: replyLanguage } : {}) }), ready: (...values) => require('./cli-profile-scope').ready(...values), prepare(contextId, headroomOwnerToken) { const nextArgs = [...rawArgs]; const nextEnv = require('./execution-environment').getExecutionEnvironment(rawEnv, { cliCommand: quoteShellArgument(runtimeCommand), directArgs: nextArgs, cwd, profile: selected, profileContextId: contextId, headroomOwnerToken }); return { env: nextEnv, args: process.platform === 'win32' && !/\.exe$/i.test(runtimeCommand) ? ['-NoLogo', '-NoProfile', '-Command', '& ' + [runtimeCommand, ...nextArgs].map(arg => quoteShellArgument(arg, true)).join(' ')] : nextArgs }; } }; return { command, runtimeCommand, args, env: executionEnv, cwd, allowBypass, cli, profilePreparation }; } module.exports = { prepareLaunch };