feat(router): /model <name> asks its scope: project, conversation, everywhere

This project writes model into .claude/settings.local.json and sets a sticky route; This conversation sets the sticky route only; Everywhere runs the native write. Links refused in the project tree, native never runs after an answer.
This commit is contained in:
bchanot committed 2026-10-25 16:21:39 +01:00
1 parent 5ee74bbb81
commit 1fde5bbc76
6 files changed
+804 -11

No files matched your search

+406 -6
View File
@@ -39,11 +39,20 @@ type World = {
gate: Promise<void> | undefined // holds a dialog open until it settles
writeGate: Promise<void> | undefined // holds an fs.write open
failWrite: boolean // fs.write rejects
clockFailsAfterAsk: boolean // clock.now rejects once a dialog opened
onAsk: (() => void) | undefined // called when a dialog opens
onWrite: (() => void) | undefined // called when an fs.write starts
repo: { root: string; remote: string | null } | null | 'fail'
env: string | undefined // HOME as env.get answers it (undefined = unset)
kinds: Map<string, 'file' | 'dir' | 'other'> // per-path stat kind override
dirs: Map<string, {
kind: 'dir' | 'file' | 'other'
isLink: boolean
exists?: false // a dangling link: stat answers, exists does not
}>
// paths fs.exists/fs.stat answer for, with or without file text
brokenStat: Set<string> // paths that exist but whose stat rejects
surfaces: readonly string[] // what session.surfaces answers ([] = headless)
complete: (model: string) => unknown // what a completion resolves to
completions: string[] // every model a completion was asked of
}
@@ -56,6 +65,8 @@ type Fixture = {
answers?: string[]
repo?: World['repo']
noHome?: boolean // HOME unset: env.get answers undefined
surfaces?: readonly string[] // default ['terminal']
clockFailsAfterAsk?: boolean // see World; the rig gets no MockClock
}
/**
@@ -117,18 +128,25 @@ function newWorld(fx: Fixture = {}): World {
gate: undefined,
writeGate: undefined,
failWrite: false,
clockFailsAfterAsk: fx.clockFailsAfterAsk ?? false,
onAsk: undefined,
onWrite: undefined,
repo: fx.repo === undefined ? { root: ROOT, remote: REMOTE } : fx.repo,
env: fx.noHome ? undefined : HOME,
kinds: new Map(),
dirs: new Map(),
brokenStat: new Set(),
surfaces: fx.surfaces ?? ['terminal'],
complete: () => PROBE_OK,
completions: [],
}
}
const LOCAL_FILE = `${ROOT}/.claude/settings.local.json`
const slot = (path: string): string => {
if (path.endsWith('/routing.json')) return 'routing'
if (path === LOCAL_FILE) return 'local'
// the engine resolves a relative path against its cwd: `undefined/...`
// (a HOME read as unset and not guarded) comes back absolute
const rel = /\/(undefined\/\.claude\/.*)$/.exec(path)
@@ -146,18 +164,23 @@ const routingOf = (w: World): Rec =>
function installWorld(on: On, w: World): void {
on('env.get', () => ({ value: w.env }))
on('fs.exists', (_a, e) => ({
value: w.files.has(slot(e.path)) || e.path === ROOT,
value: w.dirs.get(e.path)?.exists === false ? false :
w.files.has(slot(e.path)) || e.path === ROOT || w.dirs.has(e.path),
}))
on('fs.stat', (_a, e) => {
const text = w.files.get(slot(e.path))
if (text === undefined && e.path !== ROOT) throw new Error('ENOENT')
const known = w.dirs.get(e.path)
if (w.brokenStat.has(e.path)) throw new Error('EIO')
if (text === undefined && e.path !== ROOT && known === undefined) {
throw new Error('ENOENT')
}
return {
value: {
kind: w.kinds.get(e.path) ??
kind: known?.kind ?? w.kinds.get(e.path) ??
(text === undefined ? 'dir' as const : 'file' as const),
size: text?.length ?? 0,
mtimeMs: 0,
isLink: false,
isLink: known?.isLink ?? false,
...(e.resolve ? { realPath: REAL_ROOT } : {}),
},
}
@@ -170,6 +193,7 @@ function installWorld(on: On, w: World): void {
})
installWrite(on, w)
on('session.root', () => ({ value: ROOT }))
on('session.surfaces', () => ({ value: w.surfaces as never }))
on('session.repo', () => {
if (w.repo === 'fail') throw new Error('no repo')
return { value: w.repo === null ? null : {
@@ -180,13 +204,19 @@ function installWorld(on: On, w: World): void {
installDialog(on, w)
}
/** The paths the world's fs.write accepts: see installWrite. */
const worldServes = (w: World, path: string): boolean =>
path === LOCAL_FILE || (slot(path) === 'routing' && w.reads.includes(path))
/**
* The world's fs.write. Loud: a write anywhere but the routing.json the mod
* read fails the test (never a project tree, never the machine override).
* One carve-out: the project's .claude/settings.local.json, which the
* /model scope dialog writes, read or not (a missing file is created).
*/
function installWrite(on: On, w: World): void {
on('fs.write', async (_a, e) => {
expect(slot(e.path) === 'routing' && w.reads.includes(e.path),
expect(worldServes(w, e.path),
`the world never served ${e.path}`).toBe(true)
w.written.push(e.path)
w.onWrite?.()
@@ -217,6 +247,15 @@ function installDialog(on: On, w: World): void {
})
}
/** A clock that rejects once a dialog opened (no MockClock: never advanced). */
function failingClock(on: On, w: World): MockClock {
on('clock.now', () => {
if (w.asked.length > 0) throw new Error('boom')
return { value: 0 }
})
return undefined as unknown as MockClock
}
/**
* Fires session.start so the mod loads its config and registers /route.
* The bottom hooks the router reads are in place before the first `$` call:
@@ -231,7 +270,9 @@ async function boot(
tokens: number | null = 1000,
world: World = newWorld(),
): Promise<MockClock> {
const clock = mock.clock(on)
const clock = world.clockFailsAfterAsk
? failingClock(on, world)
: mock.clock(on)
on('session.model', () => ({ value: model }))
on('classic.StopFailure', () => ({}))
on('classic.PostModelSwitch', () => ({}))
@@ -5918,3 +5959,362 @@ test('keyword cap: a dispatch outside a run keeps main on the session model', as
await dispatch($)
expect(await stepOn($, rig, OPUS)).toEqual({ model: OPUS, effort: 'medium' })
})
// ---- /model <name>: the scope dialog -------------------------------------
const SCOPES = ['This project', 'This conversation', 'Everywhere']
const FABLE_1M = `${FABLE}[1m]`
const NO_SCOPE = 'model: no scope chosen, nothing changed'
type ModelRig = {
w: World
ran: string[] // the args the native /model got
toasts: string[]
rig: Rig
}
/** Boots with a bottom native /model that records its args. */
async function bootModel(
$: Engine,
on: On,
answers: string[],
session: string = OPUS,
fx: Fixture = {},
): Promise<ModelRig> {
const ran: string[] = []
on('command.run', { command: 'model' }, (_a, e) => {
ran.push(e.args)
return { text: 'native' }
})
const toasts = recordToasts(on)
const w = newWorld({ answers, ...fx })
const rig = await bootRig($, on, session, 1000, w)
return { w, ran, toasts, rig }
}
/** Runs `/model <args>` as typed (or from another origin). */
async function modelCmd(
$: Engine,
args: string,
kind: 'composer' | 'sdk' = 'composer',
): Promise<string> {
const out = await $.command.run({
command: 'model',
args,
origin: { kind },
presentation: { isFullscreen: false, columns: 80 },
})
return out.text ?? ''
}
const localOf = (w: World): Rec =>
JSON.parse(w.files.get('local') ?? '{}') as Rec
test('model command: asks the three scopes, the full id, native not run', async (
$, on) => {
const m = await bootModel($, on, ['Later', 'Later'])
await modelCmd($, 'fable')
await modelCmd($, 'claude-fable-5-1[1m]')
expect(m.w.asked.map(q => q.options)).toEqual([SCOPES, SCOPES])
expect(m.w.asked.map(q => q.header)).toEqual(['model-router', 'model-router'])
const [plain, long] = m.w.asked as [Asked, Asked]
expect(plain.text.endsWith(`Switch main to ${FABLE} where?`)).toBe(true)
expect(long.text.endsWith(`Switch main to ${FABLE_1M} where?`)).toBe(true)
expect(m.ran).toEqual([])
})
test('model command: This project writes the full id and keeps the keys', async (
$, on) => {
const m = await bootModel($, on, ['This project'])
m.w.files.set('local', JSON.stringify({ permissions: { allow: ['Bash'] } }))
m.w.dirs.set(`${ROOT}/.claude`, { kind: 'dir', isLink: false })
const out = await modelCmd($, 'fable')
expect(localOf(m.w)).toEqual({
permissions: { allow: ['Bash'] }, model: FABLE,
})
expect(m.w.files.get('local')).toBe(
JSON.stringify(localOf(m.w), null, 2) + '\n')
expect(out).toContain(`${FABLE} written to .claude/settings.local.json`)
expect(out).toContain('remove its "model" key to undo')
expect(out).not.toContain('file created')
expect(m.toasts).toEqual([])
expect(m.ran).toEqual([])
expect((await stepOn($, m.rig, OPUS)).model).toBe(FABLE)
})
test('model command: This project keeps [1m] verbatim in the file', async (
$, on) => {
const m = await bootModel($, on, ['This project', 'This project'])
await modelCmd($, 'fable[1m]')
expect(localOf(m.w).model).toBe(FABLE_1M)
await modelCmd($, 'claude-fable-5-1[1m]')
expect(localOf(m.w).model).toBe(FABLE_1M)
expect((await stepOn($, m.rig, OPUS)).model).toBe(FABLE)
})
test('model command: the [1m] note shows only for a token or session [1m]', async (
$, on) => {
const m = await bootModel($, on, ['This project', 'This project'])
expect(await modelCmd($, 'fable')).not.toContain('[1m] follows')
expect(await modelCmd($, 'fable[1m]')).toContain(
"[1m] follows the session's variant")
expect(m.ran).toEqual([])
})
test('model command: a [1m] session adds the note to a plain token', async (
$, on) => {
await bootModel($, on, ['This project'], `${OPUS}[1m]`)
expect(await modelCmd($, 'fable')).toContain('[1m] follows')
})
test('model command: This project creates the file when absent', async (
$, on) => {
const m = await bootModel($, on, ['This project'])
const out = await modelCmd($, 'opus')
expect(localOf(m.w)).toEqual({ model: OPUS })
expect(out).toContain('file created: keep it gitignored')
expect(m.toasts).toEqual([])
})
const BAD_TEXT: [string, string, string][] = [
['null', 'null', 'not an object'],
['an array', '[]', 'not an object'],
['a string', '"x"', 'not an object'],
['broken JSON', '{nope', 'not JSON'],
['over the size cap', JSON.stringify({ pad: 'x'.repeat(70000) }),
'too large'],
]
for (const [name, text, cause] of BAD_TEXT) {
test(`model command: This project refuses a file: ${name}`, async (
$, on) => {
const m = await bootModel($, on, ['This project'])
m.w.files.set('local', text)
expect(await modelCmd($, 'fable')).toContain(`not written (${cause})`)
expect(m.w.files.get('local')).toBe(text)
expect(m.w.written).toEqual([])
expect(m.toasts).toEqual([`settings.local.json not written: ${cause}`])
expect(mainLine(await route($, 'show'))).toContain('user custom')
})
}
const refusals: [string, (w: World) => void, string][] = [
['.claude link', w => w.dirs.set(`${ROOT}/.claude`,
{ kind: 'dir', isLink: true }), '.claude is a link'],
['.claude file', w => w.dirs.set(`${ROOT}/.claude`,
{ kind: 'file', isLink: false }), '.claude is not a directory'],
['settings link', w => {
w.files.set('local', '{}')
w.dirs.set(LOCAL_FILE, { kind: 'file', isLink: true })
}, 'settings.local.json is a link'],
['.claude dangling link', w => w.dirs.set(`${ROOT}/.claude`,
{ kind: 'other', isLink: true, exists: false }), '.claude is a link'],
['settings dangling link', w => w.dirs.set(LOCAL_FILE,
{ kind: 'other', isLink: true, exists: false }),
'settings.local.json is a link'],
['settings dir', w => {
w.files.set('local', '{}')
w.dirs.set(LOCAL_FILE, { kind: 'dir', isLink: false })
}, 'not a regular file'],
['stat error', w => {
w.files.set('local', '{}')
w.brokenStat.add(LOCAL_FILE)
}, 'fs error HooksError'],
]
for (const [name, setup, cause] of refusals) {
test(`model command: This project refuses: ${name}`, async ($, on) => {
const m = await bootModel($, on, ['This project'])
setup(m.w)
const before = m.w.files.get('local')
const out = await modelCmd($, 'fable')
expect(out).toContain(`not written (${cause})`)
expect(m.w.files.get('local')).toBe(before)
expect(m.w.written).toEqual([])
expect(m.toasts).toEqual([`settings.local.json not written: ${cause}`])
expect(mainLine(await route($, 'show'))).toContain('user custom')
expect(m.ran).toEqual([])
})
}
test('model command: a failing write reports an fs error, route set', async (
$, on) => {
const m = await bootModel($, on, ['This project'])
m.w.failWrite = true
const out = await modelCmd($, 'fable')
expect(out).toContain('not written (fs error')
expect(m.toasts).toHaveLength(1)
expect(out).not.toContain('EACCES')
expect(mainLine(await route($, 'show'))).toContain('user custom')
})
test('model command: This conversation sets the route, writes nothing', async (
$, on) => {
const m = await bootModel($, on, ['This conversation'])
await route($, 'effort=max')
const out = await modelCmd($, 'fable')
expect(out).toContain(`sticky route ${FABLE} for this conversation`)
expect(m.w.written).toEqual([])
expect(m.w.files.has('local')).toBe(false)
expect(m.ran).toEqual([])
const line = mainLine(await route($, 'show'))
expect(line).toContain('user custom')
expect(line).toContain('effort max')
expect((await stepOn($, m.rig, OPUS)).model).toBe(FABLE)
})
test('model command: the local scopes drop the run slot', async ($, on) => {
on('command.run', { command: 'model' }, () => ({ text: 'native' }))
await bootRun($, on, OPUS, newWorld({ answers: ['This conversation'] }))
await loadSkill($, 'feat')
await endTurn($)
expect(await modelCmd($, 'sonnet')).toContain('sticky route')
expect(await route($, 'clear')).not.toContain('run slot dropped')
})
test('model command: Everywhere runs native, args unchanged', async (
$, on) => {
const m = await bootModel($, on, ['Everywhere'])
expect(await modelCmd($, 'opus')).toBe('native')
expect(m.ran).toEqual(['opus'])
expect(m.w.written).toEqual([])
})
test('model command: Everywhere drops an earlier sticky model', async (
$, on) => {
const m = await bootModel($, on, ['This conversation', 'Everywhere'])
await modelCmd($, 'fable')
expect(mainLine(await route($, 'show'))).toContain('user custom')
await modelCmd($, 'opus')
expect(mainLine(await route($, 'show'))).toContain('session defaults')
expect(m.ran).toEqual(['opus'])
})
test('model command: Everywhere drops a sticky tier too, keeps effort', async (
$, on) => {
const m = await bootModel($, on, ['Everywhere'])
await route($, 'plan')
await modelCmd($, 'opus')
const line = mainLine(await route($, 'show'))
expect(line).toContain('effort xhigh')
expect(line).not.toContain(FABLE)
expect(m.ran).toEqual(['opus'])
})
test('model command: no dialog for a call native owns', async ($, on) => {
const m = await bootModel($, on, [])
const args = ['', 'fable opus', 'default', 'opusplan', 'claude-opus-5-6',
'haiku[1m]', 'whatever']
for (const a of args) expect(await modelCmd($, a)).toBe('native')
expect(m.ran).toEqual(args)
expect(m.w.asked).toEqual([])
})
test('model command: no dialog from another origin', async ($, on) => {
const m = await bootModel($, on, [])
expect(await modelCmd($, 'fable', 'sdk')).toBe('native')
expect(m.w.asked).toEqual([])
})
test('model command: no dialog when the router is off', async ($, on) => {
const m = await bootModel($, on, [])
await route($, 'off')
expect(await modelCmd($, 'fable')).toBe('native')
expect(m.w.asked).toEqual([])
})
test('model command: no dialog without a surface', async ($, on) => {
const m = await bootModel($, on, [], OPUS, { surfaces: [] })
expect(await modelCmd($, 'fable')).toBe('native')
expect(m.w.asked).toEqual([])
})
test('model command: a second /model while one is open goes native', async (
$, on) => {
const m = await bootModel($, on, ['This conversation'])
const held = deferred()
const open = deferred()
m.w.gate = held.gate
m.w.onAsk = open.release
const first = modelCmd($, 'fable')
await open.gate
expect(await modelCmd($, 'opus')).toBe('native')
held.release()
expect(await first).toContain('sticky route')
expect(m.w.asked).toHaveLength(1)
})
test('model command: the question notes a project pin only when set', async (
$, on) => {
const m = await bootModel($, on, ['Later', 'Later'])
await modelCmd($, 'fable')
m.w.files.set('local', JSON.stringify({ model: OPUS }))
await modelCmd($, 'fable')
const [bare, pinned] = m.w.asked as [Asked, Asked]
expect(bare.text).not.toContain('pins')
const lines = pinned.text.split('\n')
const note = lines[lines.length - 2] ?? ''
expect(note).toContain(
`this project pins ${OPUS} in .claude/settings.local.json`)
expect(note).toContain('the project pin wins')
expect(lines[lines.length - 1]).toBe(`Switch main to ${FABLE} where?`)
})
test('model command: Esc or free text cancels, nothing changes', async (
$, on) => {
const m = await bootModel($, on, [])
expect(await modelCmd($, 'fable')).toBe(NO_SCOPE)
m.w.answers.push('maybe')
expect(await modelCmd($, 'fable')).toBe(NO_SCOPE)
expect(m.ran).toEqual([])
expect(m.w.written).toEqual([])
expect(mainLine(await route($, 'show'))).toContain('session defaults')
})
test('model command: Everywhere: native runs once, its throw is reported, not re-run', async (
$, on) => {
const ran: string[] = []
on('command.run', { command: 'model' }, (_a, e) => {
ran.push(e.args)
throw new Error('boom')
})
await bootRig($, on, OPUS, 1000, newWorld({ answers: ['Everywhere'] }))
const out = await modelCmd($, 'opus')
expect(out).toContain('model failed (')
expect(out).toContain('/route show')
expect(out).not.toContain('boom')
expect(ran).toEqual(['opus'])
})
for (const scope of ['This conversation', 'This project']) {
test(`model command: ${scope}: a throw after the answer never reaches native`, async (
$, on) => {
// the dialog is answered, then the status text reads the clock
const m = await bootModel($, on, [scope], OPUS, {
clockFailsAfterAsk: true,
})
const out = await modelCmd($, 'fable')
expect(out).toContain('model failed (')
expect(out).toContain('/route show')
expect(out).not.toContain('boom')
expect(m.ran).toEqual([])
})
}
test('writes: kit: only the project settings.local.json is carved out', {
plugins: [PROBE],
}, async ($, on) => {
const m = await bootModel($, on, [])
const out = await $.command.run({
command: 'probe',
args: LOCAL_FILE,
origin: { kind: 'composer' },
presentation: { isFullscreen: false, columns: 80 },
})
expect(out.text).toBe('written')
for (const path of [`${ROOT}/.claude/settings.json`,
`${LOCAL_FILE}.bak`, `${ROOT}/other/settings.local.json`, HOME_FILE]) {
expect(worldServes(m.w, path), path).toBe(false)
}
expect(worldServes(m.w, LOCAL_FILE)).toBe(true)
})
+251 -1
View File
@@ -7,7 +7,10 @@
// DEFAULT_CONFIG; ~/.claude/model-router.json overrides both per machine.
// One writer per concern: the Agent tool's own params are never rewritten,
// the spawn hook sets an agent's model once, turn.step sets efforts.
import type { EngineInterface, On, Register, TurnStepInput } from 'claude-code'
import type {
CommandRunInput, CommandRunResult, EngineInterface, Next, On, Register,
TurnStepInput,
} from 'claude-code'
type Api = EngineInterface
type Level = 'low' | 'medium' | 'high' | 'xhigh' | 'max'
@@ -190,6 +193,15 @@ const KEEP = 'Keep'
const CHANGE = 'Change'
const EVERYWHERE = 'Everywhere'
const THIS_PROJECT = 'This project only'
// `/model <name>` scope dialog. PROJECT_SCOPE differs from THIS_PROJECT, the
// first-use label: both are fixed by their own contracts.
const PROJECT_SCOPE = 'This project'
const CONVERSATION = 'This conversation'
const MODEL_SCOPES: readonly string[] = [
PROJECT_SCOPE, CONVERSATION, EVERYWHERE,
]
const NO_SCOPE = 'model: no scope chosen, nothing changed'
const LOCAL_SETTINGS = '.claude/settings.local.json'
const CHOICE_ALIASES: readonly string[] = ['fable', 'opus', 'sonnet', 'haiku']
const MAX_DESC = 140 // chars of a skill or agent description in a question
const MAX_ABOUT = 120 // chars of a phase `about`
@@ -2054,6 +2066,238 @@ async function handleCommand($: Api, st: State, args: string): Promise<string> {
}
}
// ---- /model <name>: the scope of a switch ---------------------------------
// A typed `/model <name>` of a model the table knows asks where it applies.
// Everywhere is the native command (user settings.json); the two others set
// a sticky model route, This project also pins `model` in the project's
// settings.local.json. Fail-open to native only before an answer.
type ModelNext = Next<'command.run'>
type Local = { data: Rec; created: boolean }
type Unreadable = { cause: string }
/** An alias or a full id of the table; haiku has no `[1m]` variant. */
function knownModel(cfg: Config, token: string): boolean {
const bare = token.replace(ONE_M, '')
const id = hasKey(cfg.models, bare) ? cfg.models[bare] ?? bare : bare
if (!Object.values(cfg.models).includes(id)) return false
return !(ONE_M.test(token) && id.startsWith(HAIKU))
}
/** The id a token stands for, `[1m]` kept; never `canonical` (it strips). */
function fullId(cfg: Config, token: string): string {
const bare = token.replace(ONE_M, '')
if (!hasKey(cfg.models, bare)) return token
return `${cfg.models[bare] ?? bare}${ONE_M.test(token) ? '[1m]' : ''}`
}
/** A user switch by hand: sticky model, the run slot dropped, effort kept. */
function stickyModel($: Api, st: State, bare: string): void {
const prev = st.userMain
const effort = prev?.route.effort
st.userMain = {
phase: 'custom',
route: { model: bare, ...(effort === undefined ? {} : { effort }) },
source: 'user',
...(prev?.explicitEffort ? { explicitEffort: true } : {}),
}
st.runMain = null
refresh($, st)
}
/** Everywhere: a sticky model or tier would route main back after native. */
function dropStickyModel(st: State): void {
if (st.userMain === null) return
const effort = st.userMain.route.effort
st.userMain = effort === undefined
? null
: { ...st.userMain, phase: 'custom', route: { effort } }
}
async function localPath($: Api): Promise<string | undefined> {
try {
return `${await $.session.root()}/${LOCAL_SETTINGS}`
} catch {
return undefined
}
}
/** The file's text; null when absent (`stat` rejects and `exists` false). */
async function localText(
$: Api,
path: string,
): Promise<string | null | Unreadable> {
try {
let info
try {
info = await $.fs.stat(path)
} catch (err) {
if (!(await $.fs.exists(path))) return null
return { cause: `fs error ${errWord(err)}` }
}
if (info.isLink) return { cause: 'settings.local.json is a link' }
if (info.kind !== 'file') return { cause: 'not a regular file' }
if (info.size > MAX_CONFIG_BYTES) return { cause: 'too large' }
const text = await $.fs.read(path)
return text.length > MAX_CONFIG_BYTES ? { cause: 'too large' } : text
} catch (err) {
return { cause: `fs error ${errWord(err)}` }
}
}
/** The local settings as an object, or a fixed cause word; never throws. */
async function readLocalSettings(
$: Api,
path: string,
): Promise<Local | string> {
const text = await localText($, path)
if (text === null) return { data: {}, created: true }
if (typeof text !== 'string') return text.cause
let data: unknown
try {
data = JSON.parse(text)
} catch {
return 'not JSON'
}
return isRecord(data) ? { data, created: false } : 'not an object'
}
/** The project's `.claude`: absent is fine, a link or a file is not. */
async function claudeDirCause($: Api, path: string): Promise<string | null> {
const dir = path.slice(0, path.lastIndexOf('/'))
try {
let info
try {
info = await $.fs.stat(dir)
} catch (err) {
if (!(await $.fs.exists(dir))) return null
return `fs error ${errWord(err)}`
}
if (info.isLink) return '.claude is a link'
return info.kind === 'dir' ? null : '.claude is not a directory'
} catch (err) {
return `fs error ${errWord(err)}`
}
}
/** 'written', 'created', or a fixed cause word (never an error message). */
async function writeLocalModel($: Api, id: string): Promise<string> {
const path = await localPath($)
if (path === undefined) return 'project root unknown'
const bad = await claudeDirCause($, path)
if (bad !== null) return bad
const local = await readLocalSettings($, path)
if (typeof local === 'string') return local
local.data.model = id
try {
await $.fs.write(path, `${JSON.stringify(local.data, null, 2)}\n`)
} catch (err) {
return `fs error ${errWord(err)}`
}
return local.created ? 'created' : 'written'
}
/** The question: the id, the three scopes, the project pin if any. */
async function scopeQuestion($: Api, id: string): Promise<string> {
const path = await localPath($)
const local = path === undefined ? '' : await readLocalSettings($, path)
const pin = typeof local === 'string' ? undefined : local.data.model
const note = typeof pin === 'string'
? `Note: this project pins ${clean(pin, 60)} in ${LOCAL_SETTINGS}: ` +
'at launch here, the project pin wins over Everywhere\n'
: ''
return `${PROJECT_SCOPE}: ${LOCAL_SETTINGS} of this repo (wins at ` +
`launch) + route now · ${CONVERSATION}: route now, nothing written · ` +
`${EVERYWHERE}: user settings.json, every project\n${note}` +
`Switch main to ${id} where?`
}
/** The route cannot hold `[1m]`: the session's variant is what carries. */
function variantNote(st: State, token: string, bareFull: string): string {
const long = ONE_M.test(token) || ONE_M.test(st.sessionModel)
return long && !bareFull.startsWith(HAIKU)
? " ([1m] follows the session's variant)"
: ''
}
function writtenText(id: string, wrote: string): string {
if (wrote !== 'written' && wrote !== 'created') {
return `model: ${LOCAL_SETTINGS} not written (${wrote})`
}
return `model: ${id} written to ${LOCAL_SETTINGS} (remove its "model" key ` +
`to undo)${wrote === 'created' ? ', file created: keep it gitignored' : ''}`
}
/** This project (file + route) or This conversation (route only). */
async function applyLocalScope(
$: Api,
st: State,
token: string,
toFile: boolean,
): Promise<string> {
const id = fullId(st.cfg, token)
const wrote = toFile ? await writeLocalModel($, id) : undefined
const bare = token.replace(ONE_M, '')
stickyModel($, st, bare)
if (wrote !== undefined && wrote !== 'written' && wrote !== 'created') {
$.ui.toast(`settings.local.json not written: ${wrote}`)
}
const route = `sticky route ${fullId(st.cfg, bare)}`
const head = wrote === undefined
? `model: ${route} for this conversation`
: `${writtenText(id, wrote)}; ${route} set`
const note = variantNote(st, token, fullId(st.cfg, bare))
return `${head}${note}\n${await show($, st)}`
}
/** After an answer native never runs on a failure: a text says what to do. */
async function answerScope(
$: Api,
st: State,
e: CommandRunInput,
next: ModelNext,
answer: string,
): Promise<CommandRunResult> {
try {
if (answer === EVERYWHERE) {
dropStickyModel(st)
return await next(e)
}
const text = await applyLocalScope(
$, st, e.args.trim(), answer === PROJECT_SCOPE)
return { text }
} catch (err) {
warnOnce(st, $, 'command.run model', errWord(err))
return { text: `model failed (${errWord(err)}); the file write or the ` +
`sticky route may have landed: check /route show and ${LOCAL_SETTINGS}` }
}
}
async function handleModelCommand(
$: Api,
st: State,
e: CommandRunInput,
next: ModelNext,
): Promise<CommandRunResult> {
await ensureConfig($, st)
const tokens = e.args.trim().split(/\s+/)
const token = tokens[0] ?? ''
if (st.off || e.origin.kind !== 'composer' || tokens.length !== 1 ||
!knownModel(st.cfg, token) ||
(await $.session.surfaces()).length === 0 || st.asking !== null) {
return next(e)
}
st.asking = `model:${token}`
try {
const question = await scopeQuestion($, fullId(st.cfg, token))
const answer = await askOr($, question, MODEL_SCOPES)
if (!MODEL_SCOPES.includes(answer)) return { text: NO_SCOPE }
return await answerScope($, st, e, next, answer)
} finally {
st.asking = null
}
}
// ---- first use: one dialog per row, the answer kept in routing.json -----
// The first time a row routes (a typed skill, an agent spawn, a phase
// declared through the route tool) the user confirms it once, for every
@@ -3756,6 +4000,12 @@ function registerCwd(on: On, st: State): void {
}
function registerCommandHook(on: On, st: State): void {
on('command.run', { command: 'model' }, async ($, e, next) =>
handleModelCommand($, st, e, next)
).catch(($, e, next) => {
warnOnce(st, $, 'command.run model', next.error.kind)
return next(e)
})
on('command.run', { command: 'route' }, async ($, e) => {
if (e.origin.kind !== 'composer') {
return { text: 'route: user-only command' }