Skip to content
TypeScript
2.8 KB
Raw Blame
/**
 * 조합 확장 & 유효성 (PRD §5).
 * expand(models, agents, harnesses) → { valid, excluded }.
 * silent drop 금지 — 비호환은 excludeReason 과 함께 excluded 에 기록.
 */
import { AGENTS, HARNESSES, agentFamilies, modelFamily } from "./config";
import type { Combo } from "./models";

export interface ExpandResult {
  valid: Combo[];
  excluded: Combo[];
}

/** comboId = `${agent}+${model}+${harness}` */
export function comboId(agent: string, model: string, harness: string): string {
  return `${agent}+${model}+${harness}`;
}

/** harness 는 마지막 세그먼트 — 모델 id 에 '+' 가 있어도 안전(rsplit). */
export function parseComboId(id: string): { agent: string; model: string; harness: string } {
  const lastPlus = id.lastIndexOf("+");
  const firstPlus = id.indexOf("+");
  const agent = id.slice(0, firstPlus);
  const harness = id.slice(lastPlus + 1);
  const model = id.slice(firstPlus + 1, lastPlus);
  return { agent, model, harness };
}

function checkCombo(agent: string, model: string, harness: string): string | null {
  if (!AGENTS[agent]) return `unknown agent '${agent}'`;
  const hdef = HARNESSES[harness];
  if (!hdef) return `unknown harness '${harness}'`;

  const aFams = agentFamilies(agent);
  const mFam = modelFamily(model);

  // 1. agent 가 지원하는 family 안에 모델 family 가 있어야 함
  //    (cc→claude, codex→gpt, opencode/openclaw/hermes→any)
  if (!aFams.includes(mFam)) {
    return `agent '${agent}' (${aFams.join("/")}) is incompatible with model '${model}' (${mFam})`;
  }
  // 2. 하네스-에이전트 호환
  if (!hdef.agents.includes(agent)) {
    return `harness '${harness}' is not compatible with agent '${agent}'`;
  }
  return null;
}

export function expand(models: string[], agents: string[], harnesses: string[]): ExpandResult {
  const valid: Combo[] = [];
  const excluded: Combo[] = [];
  const seen = new Set<string>();

  for (const agent of agents) {
    for (const model of models) {
      for (const harness of harnesses) {
        const id = comboId(agent, model, harness);
        if (seen.has(id)) continue;
        seen.add(id);

        const reason = checkCombo(agent, model, harness);
        // 미등록 alias 는 경고지만 치명적 제외는 아님(preflight 가 경고). family 판정은 진행.
        if (reason) {
          excluded.push({
            comboId: id,
            agent,
            model,
            harness,
            valid: false,
            excludeReason: reason,
          });
        } else {
          // 미등록 alias 는 제외하지 않음(preflight 가 경고) — 유효로 통과시킨다.
          valid.push({ comboId: id, agent, model, harness, valid: true, excludeReason: null });
        }
      }
    }
  }
  return { valid, excluded };
}