File size: 5,479 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/** ๋Œ€ํ™”ํ˜• 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 };