Skip to content
TypeScript
7.4 KB
Raw Blame
/**
 * install 커맨드 (PRD §22.5, PRD v2 §W7).
 * 바이너리: 설치 확인 + 힌트. 하네스: cc 플러그인(--plugin-dir) · codex 격리 홈(CODEX_HOME) ·
 * 멀티모델 룰 캐시(work_dir materialize). 한 하네스가 여러 표면을 지원하면 모두 설정.
 * 설치 후 harnessStatus 로 검증. `--check`(전체 상태표) · `all`(베스트에포트 전체).
 * 실패 시 수동 명령 안내로 폴백(치명적이지 않게).
 */
import pc from "picocolors";
import { AGENTS, HARNESSES } from "./config";
import { codexHomePath, ensureHarnessRulesStub, harnessRulesPath, harnessStatus } from "./harness";
import { commandExists, execCapture } from "./runners/util";

const BIN_MAP: Record<string, string> = {
  cc: "claude",
  codex: "codex",
  tb: "tb",
  "terminal-bench": "tb",
  opencode: "opencode",
  openclaw: "openclaw",
  hermes: "hermes",
};

const INSTALL_HINTS: Record<string, string> = {
  claude: "npm i -g @anthropic-ai/claude-code",
  codex: "npm i -g @openai/codex",
  tb: "uv tool install terminal-bench",
  opencode: "npm i -g opencode-ai  (or: curl -fsSL https://opencode.ai/install | bash)",
  openclaw: "npm i -g openclaw",
  hermes: "curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash",
};

// 하네스 → cc 플러그인 marketplace source / plugin id
const CC_HARNESS_SRC: Record<string, { src: string; plugin: string }> = {
  superpowers: { src: "obra/superpowers", plugin: "superpowers" },
  omc: { src: "oh-my-claudecode/marketplace", plugin: "oh-my-claudecode" },
  ecc: { src: "ecc/marketplace", plugin: "ecc" },
  ponytail: { src: "DietrichGebert/ponytail", plugin: "ponytail" },
  mattpocock: { src: "mattpocock/skills", plugin: "mattpocock-skills" },
};
// 하네스 → codex 격리 홈 marketplace source / plugin id
const CODEX_HARNESS_SRC: Record<string, { src: string; plugin: string }> = {
  omx: { src: "oh-my-codex/marketplace", plugin: "oh-my-codex" },
  lazycodex: { src: "lazycodex/marketplace", plugin: "lazycodex" },
  omo: { src: "code-yeongyu/oh-my-openagent", plugin: "oh-my-openagent" },
};
// 룰 캐시 소스(repo) — work_dir 로 materialize 됨(멀티모델 에이전트 + codex 공용 적용 경로)
const MULTIMODEL_HARNESS_SRC: Record<string, { repo: string; install: string }> = {
  omo: { repo: "code-yeongyu/oh-my-openagent", install: "bunx oh-my-openagent install" },
  ponytail: { repo: "DietrichGebert/ponytail", install: "npx ponytail install" },
  mattpocock: { repo: "mattpocock/skills", install: "npx degit mattpocock/skills" },
  superpowers: { repo: "obra/superpowers", install: "npx degit obra/superpowers" },
};

const ALL_HARNESSES = Object.keys(HARNESSES).filter((h) => h !== "none");

async function installCcHarness(c: string): Promise<void> {
  const { src, plugin } = CC_HARNESS_SRC[c]!;
  if (!commandExists("claude")) {
    console.log(pc.yellow("  cc surface: claude not installed — skip (install cc first)"));
    return;
  }
  console.log(`  cc surface: installing plugin '${plugin}'…`);
  const a = await execCapture("claude", ["plugin", "marketplace", "add", src], { timeoutS: 120 });
  const b = await execCapture("claude", ["plugin", "install", plugin], { timeoutS: 120 });
  if (a.code === 0 && b.code === 0) {
    await execCapture("claude", ["plugin", "disable", plugin], { timeoutS: 60 });
    console.log(
      pc.green(`  ✓ cc plugin '${plugin}' installed (globally disabled; per-run --plugin-dir)`),
    );
  } else {
    console.log(pc.yellow("  cc auto-install failed — run manually:"));
    console.log(
      `    claude plugin marketplace add ${src} && claude plugin install ${plugin} && claude plugin disable ${plugin}`,
    );
  }
}

async function installCodexHarness(c: string): Promise<void> {
  const { src, plugin } = CODEX_HARNESS_SRC[c]!;
  const home = codexHomePath(c);
  if (!commandExists("codex")) {
    console.log(pc.yellow("  codex surface: codex not installed — skip"));
    return;
  }
  console.log(`  codex surface: building isolated CODEX_HOME at ${home}…`);
  const env = { CODEX_HOME: home };
  const a = await execCapture("codex", ["plugin", "marketplace", "add", src], {
    timeoutS: 120,
    env,
  });
  const b = await execCapture("codex", ["plugin", "add", plugin], { timeoutS: 120, env });
  if (a.code === 0 && b.code === 0) {
    console.log(pc.green("  ✓ codex isolated home built (global ~/.codex untouched)"));
  } else {
    console.log(pc.yellow("  codex auto-build failed — run manually:"));
    console.log(
      `    CODEX_HOME=${home} codex plugin marketplace add ${src} && CODEX_HOME=${home} codex plugin add ${plugin}`,
    );
  }
}

function installMultiModelRules(c: string): void {
  const { repo, install } = MULTIMODEL_HARNESS_SRC[c]!;
  const dir = harnessRulesPath(c);
  // 오프라인/최소: AGENTS.md 룰 스텁을 캐시에 생성(실 설치 시 repo 내용으로 대체).
  ensureHarnessRulesStub(
    c,
    `# ${c} rules (theseus harness materialization)\n` +
      `# source: ${repo}\n` +
      `# replace this stub with the real rules via: ${install}\n`,
  );
  console.log(pc.green(`  ✓ multimodel rules cache ready at ${dir}`));
  console.log(pc.dim(`    (for full rules: ${install})`));
}

function validate(c: string): void {
  const status = harnessStatus(c);
  if (status === "applied") console.log(pc.green("  ⮑ status: applied ✓"));
  else console.log(pc.yellow(`  ⮑ status: ${status} (falls back to base 'none' until applied)`));
}

export async function installHarness(c: string): Promise<void> {
  console.log(pc.bold(`harness '${c}' (agents: ${HARNESSES[c]?.agents.join(",")})`));
  if (c in CC_HARNESS_SRC) await installCcHarness(c);
  if (c in CODEX_HARNESS_SRC) await installCodexHarness(c);
  if (c in MULTIMODEL_HARNESS_SRC) installMultiModelRules(c);
  validate(c);
}

export function installBinary(c: string): boolean {
  const bin = BIN_MAP[c]!;
  if (commandExists(bin)) {
    console.log(pc.green(`✓ ${bin} already installed`));
    return true;
  }
  console.log(pc.yellow(`✗ ${bin} not found`));
  console.log(`  install: ${INSTALL_HINTS[bin] ?? `(see ${bin} docs)`}`);
  return false;
}

export async function runInstall(component: string): Promise<void> {
  const c = component.toLowerCase();

  if (c === "--check" || c === "check") return runInstallCheck();
  if (c === "all") return runInstallAll();

  if (c in BIN_MAP) {
    installBinary(c);
    return;
  }
  if (c in HARNESSES && c !== "none") {
    await installHarness(c);
    return;
  }

  console.log(pc.red(`unknown component '${component}'`));
  console.log("  binaries: cc, codex, opencode, openclaw, hermes, tb");
  console.log(`  harnesses: ${ALL_HARNESSES.join(", ")}`);
  console.log("  meta: --check (status table), all (install everything)");
}

/** 전체 가용성 상태표(doctor 와 유사하나 install 관점). */
export function runInstallCheck(): void {
  console.log(pc.bold("agents:"));
  for (const a of Object.keys(AGENTS)) {
    const ok = commandExists(AGENTS[a]!.bin);
    console.log(`  ${ok ? pc.green("✓") : pc.red("✗")} ${a} (${AGENTS[a]!.bin})`);
  }
  console.log(pc.bold("harnesses:"));
  for (const h of ALL_HARNESSES) {
    const s = harnessStatus(h);
    const mark = s === "applied" ? pc.green("✓ applied") : pc.yellow(`· ${s}`);
    console.log(`  ${mark}  ${h}`);
  }
}

/** 베스트에포트로 모든 하네스 설치 시도(미설치 바이너리는 건너뜀). */
export async function runInstallAll(): Promise<void> {
  for (const h of ALL_HARNESSES) {
    await installHarness(h);
  }
}