/** * `claudeup models list | use | status | off` * * The user-facing half of per-subagent model routing. Everything it prints is * derived from `services/models-manager.ts`; this file owns wording and exit * codes only. * * Exit codes: 1 for an unknown preset or a config that fails validation, 0 * otherwise. An unknown preset LISTS the valid names rather than near-matching * one — `fable-lead` and `fable-advisor` differ by which model leads, and * silently picking the closer string would route every subagent differently * from what was asked. */ import { BUILT_IN_PRESETS, DEFAULT_PRESET, presetLabel, presetNames, } from "../data/models-presets.js"; import { loadMateCatalog, stalenessNote, unknownBindings, } from "../services/mate-catalog.js"; import { GRADES, type ModelsConfig, type ModelsState, boundModel, isMate, mateEffort, matesInUse, sameRouting, } from "../services/models-core.js"; import { applyModelPreset, clearModels, configPath, readModelsConfig, readModelsStatus, } from "../services/models-manager.js"; import { ensureManifest } from "./bootstrap.js"; const HEADLINE: Record = { off: "off — no .claude/models.json, subagents inherit the session model", on: "on", invalid: "invalid — nothing is routed", unhooked: "unhooked — the config is there, but nothing runs it", }; /** * `opus (medium)`, or `opus` when no effort is set. * * A mate prints the SLOT and what it is bound to — `mate1 → grok-4.6` — because both are * answers to different questions the reader has: which role this tier plays, and which model * plays it. The slot alone was all that could be printed before the `mates` block existed, * and a bare `kangaroo` in a column of `opus (xhigh)` read as a model id nobody recognises. * * An effort on a mate is qualified `(declared)`, for the reason spelled out at * `DECLARED_SUFFIX`: claudeup writes it for claudish and nothing applies it yet, so printing * a bare `(high)` beside a tier where `(high)` IS in force would claim they do the same * thing. */ function describeSpec( config: ModelsConfig, spec: { model: string; effort?: string }, ): string { if (isMate(spec.model)) { const bound = boundModel(config, spec.model); const effort = spec.effort ?? mateEffort(config, spec.model); const target = bound ?? "unbound"; const note = effort ? `${effort} declared, via claudish` : "via claudish"; return `${spec.model} → ${target} (${note})`; } return spec.effort ? `${spec.model} (${spec.effort})` : spec.model; } /** * A preset headed by what a person would call it, with the id beside it. * * Both, deliberately: the label is what you choose between ("Opus with Fable help" says which * model leads and which helps), and the id is what you TYPE into `models use` and what lands * in the committed config. Printing only the label would leave the command unguessable. */ function printPreset(preset: ModelsConfig, active: boolean): void { const mark = active ? "●" : " "; const isDefault = preset.preset === DEFAULT_PRESET ? " (default)" : ""; console.log( `${mark} ${presetLabel(preset.preset)} — ${preset.preset}${isDefault}`, ); console.log(` main ${describeSpec(preset, preset.main)}`); for (const grade of GRADES) { console.log( ` ${grade.padEnd(10)} ${describeSpec(preset, preset.grades[grade])}`, ); } } async function runList(projectPath: string): Promise { const { config } = await readModelsConfig(projectPath); // Has the project diverged from the built-in it NAMES? // // The test used to be "does it name a preset the built-ins do not", which misses the // ordinary case: `claudeup models use opus-lead` writes that name, and every hand edit // afterwards leaves it in place. So a config with a `mates` block was reported as the // built-in `opus-lead`, and the `●` sat on a preset that is not what runs. Compared by // CONTENT, shared with the TUI through `sameRouting` so the two cannot disagree about // which routing is in force. const builtInNamed = BUILT_IN_PRESETS.find( (preset) => preset.preset === config?.preset, ); const diverged = config !== null && (!builtInNamed || !sameRouting(config, builtInNamed)); console.log("\nModel tiers:\n"); for (const preset of BUILT_IN_PRESETS) { printPreset(preset, !diverged && config?.preset === preset.preset); console.log(); } if (config && diverged) { // The project's own config gets its tiers PRINTED, not just its name. // // It is the routing actually in force, and it is the only config here that can name a // mate — no built-in does — so printing the name alone hid the one line a reader came // for. The built-ins above are shown in full; showing the active one in less detail // than the ones it replaced was backwards. printPreset(config, true); console.log(" (this project's own, not a built-in)\n"); } // Which agents leave Claude Code entirely. // // `models list` prints tiers, never agents, so a mate assigned per-agent — the ordinary // way to use one — appears nowhere else in this command's output. It is also the one // routing decision here that claudeup does not carry out itself, which makes it exactly // the thing worth naming rather than leaving to be discovered in the file. const mated = Object.entries(config?.agents ?? {}).filter( ([, assignment]) => typeof assignment !== "string" && isMate(assignment.model), ); if (config && mated.length > 0) { console.log("Routed outside Claude Code, through claudish:\n"); for (const [agent, assignment] of mated) { const slot = typeof assignment === "string" ? assignment : assignment.model; // The SLOT and the model it names. The slot is what the file says and what the // reader greps for; the bound id is what actually runs. Printing only the slot was // the CLI's version of the screen's `mate1 mate1 —` — a role where a model belongs. const bound = isMate(slot) ? boundModel(config, slot) : null; const target = bound ?? "unbound — add it to the mates block"; console.log(` ${agent.padEnd(28)} ${slot.padEnd(10)} ${target}`); } console.log(); // The staleness advisory, on the same terms as everywhere else: ADVISORY, and silent // when the catalogue could not be read. `unknownBindings` returns nothing for an empty // catalogue, so a machine without claudish prints no note rather than flagging every // binding it cannot check. const bound = matesInUse(config) .map((mate) => boundModel(config, mate)) .filter((id): id is string => id !== null); const note = stalenessNote(unknownBindings(bound, await loadMateCatalog())); if (note) console.log(`⚠ ${note}\n`); } console.log( "Tiers name what a subagent is FOR: `smart` takes the judgement calls, `cheap`", ); console.log( "the searching. Which model serves each is the preset's call. Apply one with\n`claudeup models use `.\n", ); return 0; } async function runStatus(projectPath: string): Promise { const status = await readModelsStatus(projectPath); console.log(`\nModel tiers: ${HEADLINE[status.state]}`); if (status.preset) console.log( `Preset: ${presetLabel(status.preset)} (${status.preset})`, ); console.log(`Config: ${configPath(projectPath)}`); if (status.drift.length > 0) { console.log(status.state === "invalid" ? "\nErrors:" : "\nDrift:"); for (const line of status.drift) console.log(` ✗ ${line}`); if (status.state === "unhooked") console.log("\n Fix: claudeup doctor --fix"); } for (const warning of status.warnings) console.log(`\n⚠ ${warning}`); console.log(); return status.state === "invalid" ? 1 : 0; } async function runUse(projectPath: string, name?: string): Promise { if (!name) { console.error("Usage: claudeup models use "); console.error(`Available: ${presetNames().join(", ")}`); return 1; } if (!presetNames().includes(name)) { // Never near-match. See the file header. console.error(`Unknown preset "${name}".`); console.error(`Available: ${presetNames().join(", ")}`); return 1; } let result: Awaited>; try { result = await applyModelPreset({ projectPath, preset: name, ensureManifest: (p) => ensureManifest(p), }); } catch (error) { console.error(error instanceof Error ? error.message : String(error)); return 1; } console.log(`\n✓ Applied preset "${name}".`); console.log(` ${configPath(projectPath)}`); console.log(` ${result.settingsPath}`); for (const [key, value] of Object.entries(result.wrote)) { console.log(` ${key} = ${JSON.stringify(value)}`); } console.log( ` hook: ${result.hook === "registered" ? "registered at user scope" : "already registered"}`, ); if (result.recordedInManifest && result.profile) { console.log( `\nRecorded in .claude/profiles.json under profiles.${result.profile}.`, ); console.log("Commit .claude/profiles.json to share this with the team."); } else { console.log( "\nNo active profile — written straight to .claude/models.json, which is", ); console.log( "generated and gitignored. Run `claudeup install` to adopt a profile if the", ); console.log("team should share this routing."); } console.log(); return 0; } async function runOff(projectPath: string): Promise { await clearModels(projectPath); console.log( "\n✓ Model tiers off. Subagents inherit the session model again.", ); console.log( " The hook stays registered — it is a no-op without a models.json.\n", ); return 0; } function printUsage(): void { console.log(`Usage: claudeup models list Show the built-in tier presets; ● marks the active one use Apply a preset to this project and register the hook status What is routed right now, and any drift off Turn model tiers off (removes the config, keeps the hook) Available presets: ${presetNames().join(", ")}`); } /** * `projectPath` defaults to the cwd, which is what the router passes. It is a * parameter so tests can drive a temp project without moving the process — * `cli/profile.ts` takes the same shape for the same reason. */ export async function runModelsCommand( args: string[], projectPath: string = process.cwd(), ): Promise { const [command, ...rest] = args; switch (command) { case undefined: case "status": return runStatus(projectPath); case "list": return runList(projectPath); case "use": return runUse(projectPath, rest[0]); case "off": return runOff(projectPath); default: console.error(`Unknown models command "${command}".`); printUsage(); return 1; } }