import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { CONFIG_DIR_NAME, getAgentDir } from "@selesai/code"; export const SECTIONS = ["priority", "fallback", "cheap"] as const; export type SectionName = (typeof SECTIONS)[number]; export const SECTION_LABELS: Record = { priority: "Priority", fallback: "Fallback", cheap: "Cheap", }; export const EFFORTS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const; export type Effort = (typeof EFFORTS)[number]; export interface RosterRow { difficulty: number; provider: string; model: string; effort: Effort; concurrency: number; } export interface Roster { version: 1; sections: Record; } export function globalRosterPath(agentDir = getAgentDir()): string { return join(agentDir, "implement-models.json"); } export function projectRosterPath(cwd: string): string { return join(cwd, CONFIG_DIR_NAME, "implement-models.json"); } export function emptyRoster(): Roster { return { version: 1, sections: { priority: [], fallback: [], cheap: [] } }; } function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } export function validateRoster(value: unknown): { ok: true; roster: Roster } | { ok: false; error: string } { if (!isRecord(value) || value.version !== 1) return { ok: false, error: "Roster version must be 1." }; if (!isRecord(value.sections)) return { ok: false, error: "Roster sections must be an object." }; const roster = emptyRoster(); for (const section of SECTIONS) { const rows = value.sections[section]; if (rows === undefined) continue; if (!Array.isArray(rows)) return { ok: false, error: `${section}: expected a section array.` }; for (const [index, row] of rows.entries()) { const error = (message: string) => ({ ok: false as const, error: `${section} row ${index}: ${message}` }); if (!isRecord(row)) return error("expected a row object."); if (typeof row.difficulty !== "number" || !Number.isFinite(row.difficulty) || row.difficulty < 1 || row.difficulty > 7) { return error("difficulty must be a finite number in [1, 7]."); } if (typeof row.concurrency !== "number" || !Number.isInteger(row.concurrency) || row.concurrency < 1) { return error("concurrency must be an integer >= 1."); } if (typeof row.effort !== "string" || !EFFORTS.includes(row.effort as Effort)) { return error(`effort must be one of ${EFFORTS.join(", ")}.`); } if (typeof row.provider !== "string" || !row.provider.trim()) return error("provider must be a non-empty string."); if (typeof row.model !== "string" || !row.model.trim()) return error("model must be a non-empty string."); roster.sections[section].push({ difficulty: row.difficulty, provider: row.provider, model: row.model, effort: row.effort as Effort, concurrency: row.concurrency, }); } } if (!SECTIONS.some((section) => roster.sections[section].length > 0)) { return { ok: false, error: "Roster must contain at least one row across priority, fallback, and cheap." }; } return { ok: true, roster }; } export type RosterLoadResult = | { roster: Roster; path: string; scope: "project" | "global" } | { error: string; path: string; scope: "project" | "global" }; export function loadRoster({ cwd, trusted, agentDir }: { cwd: string; trusted: boolean; agentDir?: string }): RosterLoadResult | undefined { const candidates: { path: string; scope: "project" | "global" }[] = []; if (trusted) candidates.push({ path: projectRosterPath(cwd), scope: "project" }); candidates.push({ path: globalRosterPath(agentDir), scope: "global" }); for (const candidate of candidates) { let contents: string; try { contents = readFileSync(candidate.path, "utf8"); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") continue; return { ...candidate, error: error instanceof Error ? error.message : String(error) }; } try { const result = validateRoster(JSON.parse(contents) as unknown); return result.ok ? { ...candidate, roster: result.roster } : { ...candidate, error: result.error }; } catch (error) { return { ...candidate, error: error instanceof Error ? error.message : String(error) }; } } return undefined; } export function saveRoster(path: string, roster: Roster): void { const result = validateRoster(roster); if (!result.ok) throw new Error(result.error); mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, `${JSON.stringify(result.roster, null, 2)}\n`, "utf8"); } export function formatRow(row: RosterRow): string { return `${row.difficulty.toFixed(1)} | ${row.provider} | ${row.model} | ${row.effort} | ${row.concurrency}`; }