@alexeiled/claude-router 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,29 @@
1
- import { loadConfig, TIERS } from '../lib/config.mjs';
1
+ import { DEFAULTS, effectiveRoutes, loadConfig, TIERS, tuningOf, withRoutes, withTuning } from '../lib/config.mjs';
2
2
  import { clip } from '../lib/facts-pure.mjs';
3
- import { REASONS } from '../lib/native-display.mjs';
3
+ import { renderBand, switchToast } from '../lib/native-band.mjs';
4
+ import { GATEWAY_CLEANUP, GATEWAY_SETTINGS } from '../lib/native-display.mjs';
4
5
  import { NativeJev } from '../lib/native-jev.mjs';
5
- import { renderPanel } from '../lib/native-panel.mjs';
6
+ import { renderPanel, routeDraftOf } from '../lib/native-panel.mjs';
6
7
  import {
7
8
  chooseRoute,
8
9
  continueRoute,
9
10
  emptyLoop,
11
+ isModelAllowed,
12
+ isSameModel,
10
13
  nativeFacts,
11
14
  observeResponse,
12
15
  prepareLoop,
13
16
  resetHistory,
14
- SNAPSHOTS,
15
17
  } from '../lib/native-router.mjs';
16
18
 
17
19
  const VIEW = { plugin: 'router', key: 'view' };
18
20
  const LOOP = { plugin: 'router', key: 'loops' };
19
21
  const PANE = 'jev-router';
22
+ const PANE_TITLE = 'Router';
23
+ const BAND_DETAIL = 'band:detail';
20
24
  const MODE_PREFIX = 'mode:';
21
- const GATEWAY_SETTINGS = 'v0.8 gateway settings remain · see /router status';
25
+ // Replies kept for the pane's trend and tier strip.
26
+ const HISTORY = 30;
22
27
 
23
28
  function initialView(model) {
24
29
  return {
@@ -45,9 +50,15 @@ function initialView(model) {
45
50
  adviceChoice: null,
46
51
  estimate: null,
47
52
  comparison: null,
53
+ probabilities: null,
48
54
  history: [],
55
+ tiers: [],
49
56
  configPath: null,
50
57
  tuning: null,
58
+ tuningBase: null,
59
+ routeDraft: null,
60
+ tab: 'now',
61
+ help: false,
51
62
  notice: null,
52
63
  };
53
64
  }
@@ -113,41 +124,32 @@ async function apiKeyOf($, options) {
113
124
  : (await $.env.get('TYPESAFE_API_KEY')) || (await $.env.get('CLAUDE_PLUGIN_OPTION_TYPESAFE_API_KEY'));
114
125
  }
115
126
 
116
- async function configureKey($) {
117
- return $.command.run({ command: 'plugin', args: `configure ${$.plugin.name}` });
127
+ // The host refuses $.command.run from a hook the turn is holding, so the secure key dialog opens from a timer.
128
+ function openKeySettings($) {
129
+ $.clock.after(0, () => $.command.run({ command: 'plugin', args: `configure ${$.plugin.name}` }).catch(() => {}));
118
130
  }
119
131
 
120
- function modelLabel(id) {
121
- if (!id) return 'native model';
122
- return id.replace(/^claude-/, '').replace(/-([0-9])-([0-9])/, ' $1.$2');
123
- }
124
-
125
- function bandText(view) {
126
- if (view.phase === 'unavailable') return `Jev Router · ${view.error}`;
127
- if (view.mode === 'manual') return `Jev Router · Manual · ${modelLabel(view.nativeModel)}`;
128
- if (view.pendingPin) return `Jev Router · ${view.pendingPin} pinned for next turn`;
129
- if (view.phase === 'ready') return 'Jev Router · Auto · ready';
130
- if (view.phase === 'choosing') return 'Jev Router · choosing for this turn…';
131
- if (view.error === 'missing-key')
132
- return `Jev Router · key not set · keeping ${modelLabel(view.actualModel ?? view.nativeModel)}`;
133
- if (view.error === 'paused')
134
- return `Jev Router · Jev paused until ${new Date(view.health.pausedUntil).toLocaleTimeString()}`;
135
- if (view.error) return `Jev Router · Jev ${view.error} · keeping ${modelLabel(view.actualModel ?? view.nativeModel)}`;
136
- if (view.reason === 'context-unknown') return 'Jev Router · context unknown · keeping native model';
137
- if (
138
- view.actualModel &&
139
- view.selectedModel &&
140
- view.actualModel !== view.selectedModel &&
141
- !view.actualModel.startsWith(`${view.selectedModel}-`)
142
- ) {
143
- return `Jev Router · ${modelLabel(view.selectedModel)} → ${modelLabel(view.actualModel)} · native fallback`;
132
+ // Writes router.json through `change(previousFile)` once the result validates. Returns the new config or an error
133
+ // line for the pane; a failure leaves the file untouched.
134
+ async function saveConfig($, path, change) {
135
+ try {
136
+ if ((await $.fs.exists(path)) && (await $.fs.stat(path)).isLink)
137
+ return { error: 'Not saved: router.json is a symlink. Edit its maintained source instead.' };
138
+ const previous = (await $.fs.exists(path)) ? JSON.parse(await $.fs.read(path)) : {};
139
+ const file = change(previous);
140
+ const checked = loadConfig({ userFile: file });
141
+ await $.fs.write(path, `${JSON.stringify(file, null, 2)}\n`);
142
+ return { config: { ...checked, nativePath: path } };
143
+ } catch (error) {
144
+ // A JSON parse message quotes the file; the loadConfig messages name the setting only.
145
+ const reason = error instanceof SyntaxError ? 'router.json is not valid JSON' : error.message;
146
+ return { error: `Not saved: ${reason}. router.json is unchanged.` };
144
147
  }
145
- return `Jev Router · ${view.actualModel ? 'last reply' : 'selected'} ${modelLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel)}${view.effort ? ` · ${view.effort}` : ''} · ${REASONS[view.reason] ?? view.reason}`;
146
148
  }
147
149
 
148
150
  function detailText(view) {
149
151
  return [
150
- `Jev Router — ${view.mode === 'auto' ? 'Auto' : 'Manual'}`,
152
+ `Router — ${view.mode === 'auto' ? 'Auto' : 'Manual'}`,
151
153
  `Native model: ${view.nativeModel}`,
152
154
  `Selected: ${view.selectedModel ?? 'not selected'}`,
153
155
  `Observed: ${view.actualModel ?? 'no response yet'}`,
@@ -158,13 +160,7 @@ function detailText(view) {
158
160
  view.pendingPin ? `Next turn pin: ${view.pendingPin}` : 'No next-turn pin.',
159
161
  'Auto enables routing. Manual preserves Claude’s model. Pins serve one turn only.',
160
162
  'Cache lifetime is an estimate. Claude’s cost ledger owns session totals.',
161
- ...(view.error === GATEWAY_SETTINGS
162
- ? [
163
- 'Remove from settings.json, then restart: model jev-router[1m], its modelPicker row,',
164
- 'env.ANTHROPIC_BASE_URL for 127.0.0.1:43170, env.CLAUDE_CODE_GATEWAY_HINT_HEADERS,',
165
- 'and a statusLine that runs the router scripts/statusline.mjs.',
166
- ]
167
- : []),
163
+ ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP : []),
168
164
  ].join('\n');
169
165
  }
170
166
 
@@ -201,15 +197,7 @@ async function contextOf($, loop) {
201
197
  }
202
198
  }
203
199
 
204
- function tuningOf(config) {
205
- return {
206
- timeoutMs: config.jev.timeoutMs,
207
- downgradeVotes: config.policy.downgradeVotes,
208
- horizon: config.policy.downgradeHorizonTurns,
209
- };
210
- }
211
-
212
- function responseMetrics(view, response) {
200
+ function responseMetrics(view, response, tier) {
213
201
  const usage = response.usage;
214
202
  const counters = usage ? [usage.input_tokens, usage.cache_read_input_tokens, usage.cache_creation_input_tokens] : [];
215
203
  const inputTokens =
@@ -220,7 +208,13 @@ function responseMetrics(view, response) {
220
208
  cacheWrite: usage?.cache_creation_input_tokens ?? null,
221
209
  inputTokens,
222
210
  outputTokens: usage?.output_tokens ?? null,
223
- history: Number.isFinite(inputTokens) ? [...(view?.history ?? []), inputTokens].slice(-10) : (view?.history ?? []),
211
+ // Appended together so the Usage trend can color each reading by its tier.
212
+ ...(Number.isFinite(inputTokens)
213
+ ? {
214
+ history: [...(view?.history ?? []), inputTokens].slice(-HISTORY),
215
+ tiers: [...(view?.tiers ?? []), tier ?? null].slice(-HISTORY),
216
+ }
217
+ : {}),
224
218
  };
225
219
  }
226
220
 
@@ -228,7 +222,7 @@ function responseMetrics(view, response) {
228
222
  function isNativeFallback(loop, model) {
229
223
  if (loop.suspended) return true;
230
224
  const known = [loop.engineModel, loop.decision?.model].filter(Boolean);
231
- return known.length > 0 && !known.some((id) => id === model || SNAPSHOTS[id] === model);
225
+ return known.length > 0 && !known.some((id) => isSameModel(id, model));
232
226
  }
233
227
 
234
228
  async function* passMain($, e, next, loop, version, nativeModel, reason, config, runtime) {
@@ -253,7 +247,7 @@ async function* passMain($, e, next, loop, version, nativeModel, reason, config,
253
247
  now: Date.now(),
254
248
  });
255
249
  const written = await $.state.set(ref, observed, { ifVersion: version });
256
- if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response));
250
+ if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response, null));
257
251
  }
258
252
  return response;
259
253
  }
@@ -271,8 +265,8 @@ export function register(on, options) {
271
265
  on('session.start', async ($, e, next) => {
272
266
  await $.command.register({
273
267
  name: 'router',
274
- description: 'Jev routing, next-turn pins and model controls.',
275
- argumentHint: '[auto|off|pin <tier>|status|setup]',
268
+ description: 'Open the Router pane, or switch routing: auto, off, pin <tier>.',
269
+ argumentHint: '[auto|off|pin <tier>]',
276
270
  immediate: true,
277
271
  });
278
272
  const model = await $.session.model();
@@ -296,7 +290,9 @@ export function register(on, options) {
296
290
  error: !supported ? 'requires Claude Code 2.1.289 or newer' : gateway ? GATEWAY_SETTINGS : null,
297
291
  keySet: Boolean(await apiKeyOf($, options)),
298
292
  configPath: config.nativePath,
299
- tuning: tuningOf(config),
293
+ tuning: null,
294
+ tuningBase: null,
295
+ bandDetail: (await $.store.get(BAND_DETAIL).catch(() => false)) === true,
300
296
  });
301
297
  } catch (error) {
302
298
  await updateView($, runtime, {
@@ -335,11 +331,10 @@ export function register(on, options) {
335
331
  }
336
332
  if (action === 'pin')
337
333
  return { text: TIERS.includes(tier) ? await setPin($, runtime, tier) : `Choose ${TIERS.join(', ')}.` };
338
- if (action === 'setup') return configureKey($);
339
334
  const storedView = await readView($, runtime);
340
335
  const view = { ...storedView, mode: await modeOf($, runtime, storedView.mode) };
341
- if (action === 'status' || !(await $.session.surfaces()).length) return { text: detailText(view) };
342
- await $.ui.open({ id: PANE, title: 'Jev Router', focus: true, closeOnEscape: true });
336
+ if (!(await $.session.surfaces()).length) return { text: detailText(view) };
337
+ await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
343
338
  return {};
344
339
  });
345
340
 
@@ -424,6 +419,7 @@ export function register(on, options) {
424
419
  (await modeOf($, runtime)) !== 'auto'
425
420
  )
426
421
  return null;
422
+ const previous = loop.decision;
427
423
  const selected = chooseRoute(cfg, loop, {
428
424
  facts,
429
425
  advice: result.advice,
@@ -437,6 +433,8 @@ export function register(on, options) {
437
433
  const written = await $.state.set(ref, selected, { ifVersion: loopVersion });
438
434
  // Manual, /clear and turn completion abort the controller; one may land during the write.
439
435
  if (!written.isSet || controller.signal.aborted) return null;
436
+ if (previous?.model && previous.model !== selected.decision.model && !selected.decision.pinned)
437
+ $.ui.toast(switchToast(previous, selected.decision, selected.decision.estimate));
440
438
  await updateView($, runtime, {
441
439
  phase: 'routed',
442
440
  selectedModel: selected.decision.model,
@@ -451,6 +449,7 @@ export function register(on, options) {
451
449
  adviceMs:
452
450
  pin || ['missing-key', 'busy', 'paused'].includes(result.error) ? null : Date.now() - adviceStarted,
453
451
  adviceChoice: result.advice?.choice ?? null,
452
+ probabilities: result.advice?.probabilities ?? null,
454
453
  estimate: selected.decision.estimate ?? null,
455
454
  comparison: selected.decision.comparison,
456
455
  });
@@ -505,7 +504,9 @@ export function register(on, options) {
505
504
  });
506
505
  if (response.stopReason === null) observed.suspended = true;
507
506
  const written = await $.state.set(ref, observed, { ifVersion: loopVersion });
508
- if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response));
507
+ // A substituted reply is not the tier's: the strip and trend must not count it as one.
508
+ const tier = isSameModel(request.model, response.usage?.model) ? loop.decision.tier : null;
509
+ if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response, tier));
509
510
  }
510
511
  return response;
511
512
  });
@@ -547,89 +548,154 @@ export function register(on, options) {
547
548
  keySet: runtime.view?.keySet ?? false,
548
549
  health: classifier.snapshot(),
549
550
  configPath: config.nativePath,
550
- tuning: tuningOf(config),
551
551
  phase: runtime.view?.phase === 'unavailable' ? 'unavailable' : 'ready',
552
552
  error: runtime.view?.phase === 'unavailable' ? runtime.view.error : null,
553
553
  };
554
554
  return next(e);
555
555
  });
556
556
 
557
+ // While the classifier runs, the turn's spinner says so; the engine's own word returns once the route is set.
558
+ on('ui.render', { component: 'Spinner' }, async ($, e, next) => {
559
+ if (e.props.message !== null) return next(e);
560
+ const view = await readView($, runtime);
561
+ return view.phase === 'choosing' ? next({ ...e, props: { ...e.props, message: 'Choosing model' } }) : next(e);
562
+ });
563
+
564
+ // Paused or broken routing stays visible in the prompt footer even when the band is collapsed.
565
+ on('ui.render', { component: 'SessionMode' }, async ($, e, next) => {
566
+ const view = await readView($, runtime);
567
+ const label =
568
+ view.phase === 'unavailable'
569
+ ? 'router unavailable'
570
+ : (await modeOf($, runtime, view.mode)) === 'manual'
571
+ ? 'router off'
572
+ : null;
573
+ return label ? next({ ...e, props: { ...e.props, modes: [...e.props.modes, label] } }) : next(e);
574
+ });
575
+
557
576
  on('ui.render', { component: ['AbovePrompt', 'Pane'] }, async ($, e, next) => {
558
577
  if (e.component === 'Pane' && e.requestId !== PANE) return next(e);
559
578
  const storedView = await readView($, runtime);
560
579
  const view = { ...storedView, mode: await modeOf($, runtime, storedView.mode) };
561
580
  const elements = $.ui.resolve(e);
562
- const { Box, Text, Button } = elements;
563
- if (e.component === 'AbovePrompt')
581
+ const { Box } = elements;
582
+ const usage = await $.session.usage().catch(() => null);
583
+ const openPane = () => $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
584
+ if (e.component === 'AbovePrompt') {
585
+ if (e.props.hasSurvey) return next(e);
564
586
  return Box({
565
587
  flexDirection: 'column',
566
588
  children: [
567
589
  await next(e),
568
- Box({
569
- children: [
570
- Text({ color: view.error ? 'yellow' : 'cyan', children: bandText(view) }),
571
- Text({ children: ' ' }),
572
- Button({
573
- key: 'details',
574
- label: 'Router',
575
- plain: true,
576
- onPress: () => $.ui.open({ id: PANE, title: 'Jev Router', focus: true, closeOnEscape: true }),
577
- }),
578
- ],
579
- }),
590
+ renderBand(
591
+ elements,
592
+ config,
593
+ view,
594
+ usage,
595
+ { columns: e.props.bodyColumns, agentId: e.props.view?.agentId },
596
+ {
597
+ open: openPane,
598
+ mode: (mode) => changeMode($, runtime, mode),
599
+ pin: async (tier) => $.ui.toast(await setPin($, runtime, tier)),
600
+ unpin: () => updateView($, runtime, { pendingPin: null }),
601
+ key: () => openKeySettings($),
602
+ toggleDetail: async () => {
603
+ const bandDetail = !view.bandDetail;
604
+ await $.store.set(BAND_DETAIL, bandDetail).catch(() => {});
605
+ await updateView($, runtime, { bandDetail });
606
+ },
607
+ },
608
+ ),
580
609
  ],
581
610
  });
582
- const usage = await $.session.usage().catch(() => null);
583
- return renderPanel(elements, config, view, usage, {
584
- mode: (mode) => changeMode($, runtime, mode),
585
- pin: async (tier) => {
586
- $.ui.log(await setPin($, runtime, tier));
587
- },
588
- key: async () => {
589
- const command = `/plugin configure ${$.plugin.name}`;
590
- const draft = await $.prompt.read();
591
- await updateView($, runtime, {
592
- notice: `Run ${command} to edit the secure key. Your existing prompt is preserved.`,
593
- });
594
- await $.ui.close({ id: PANE });
595
- if (!draft.text) {
596
- const filled = await $.prompt.fill({ text: command });
597
- $.ui.log(
598
- filled.isFilled
599
- ? 'Press Enter to open secure Jev key configuration.'
600
- : `Run ${command} to configure the secure key.`,
611
+ }
612
+ const settings = await $.settings.read().catch(() => ({}));
613
+ const modelOptions = Object.keys(config.models).filter((alias) =>
614
+ isModelAllowed(config.models[alias].id, settings.availableModels, view.nativeModel),
615
+ );
616
+ const editRoute = (tier, change) => {
617
+ const draft = routeDraftOf(config, runtime.view ?? view);
618
+ const current = effectiveRoutes(draft, config).routes[tier];
619
+ return updateView($, runtime, {
620
+ routeDraft: { ...draft, routes: { ...draft.routes, [tier]: change(current) } },
621
+ notice: null,
622
+ });
623
+ };
624
+ const save = async (change, saved) => {
625
+ const result = await saveConfig($, config.nativePath, change);
626
+ if (result.error) return updateView($, runtime, { notice: result.error });
627
+ config = result.config;
628
+ return updateView($, runtime, saved(config));
629
+ };
630
+ return renderPanel(
631
+ elements,
632
+ config,
633
+ view,
634
+ usage,
635
+ {
636
+ mode: (mode) => changeMode($, runtime, mode),
637
+ tab: (tab) => updateView($, runtime, { tab, notice: null }),
638
+ help: () => updateView($, runtime, { help: !view.help }),
639
+ pin: async (tier) => {
640
+ await updateView($, runtime, { notice: await setPin($, runtime, tier) });
641
+ },
642
+ unpin: () => updateView($, runtime, { pendingPin: null, notice: null }),
643
+ key: async () => {
644
+ openKeySettings($);
645
+ await $.ui.close({ id: PANE });
646
+ },
647
+ copyPath: async (press) => {
648
+ await $.ui.copy({ text: config.nativePath, surface: press?.surface });
649
+ await updateView($, runtime, { notice: 'Path copied.' });
650
+ },
651
+ close: () => $.ui.close({ id: PANE }),
652
+ routeModel: (tier, alias) =>
653
+ editRoute(tier, (route) => ({
654
+ model: alias,
655
+ effort: config.models[alias]?.efforts.includes(route.effort) ? route.effort : null,
656
+ })),
657
+ routeEffort: (tier, effort) => editRoute(tier, (route) => ({ model: route.model, effort })),
658
+ baseline: async (tier) => {
659
+ const draft = routeDraftOf(config, runtime.view ?? view);
660
+ await updateView($, runtime, { routeDraft: { ...draft, baselineTier: tier }, notice: null });
661
+ },
662
+ saveRoutes: async () => {
663
+ const draft = (runtime.view ?? view).routeDraft;
664
+ if (!draft) return updateView($, runtime, { notice: 'No route changes to save.' });
665
+ return save(
666
+ (file) => withRoutes(file, draft),
667
+ () => ({ routeDraft: null, notice: 'Saved routes. They apply from the next turn.' }),
601
668
  );
602
- } else $.ui.log(`Run ${command} after finishing your current draft.`);
603
- },
604
- close: () => $.ui.close({ id: PANE }),
605
- tune: async (key, value) => {
606
- await updateView($, runtime, {
607
- tuning: { ...tuningOf(config), ...runtime.view?.tuning, [key]: value },
608
- notice: 'Draft — select Save tuning to apply.',
609
- });
610
- },
611
- save: async () => {
612
- try {
613
- const draft = runtime.view.tuning;
614
- const path = config.nativePath;
615
- if ((await $.fs.exists(path)) && (await $.fs.stat(path)).isLink) {
616
- await updateView($, runtime, { notice: 'Config is a symlink. Edit its maintained source instead.' });
617
- return;
618
- }
619
- const previous = (await $.fs.exists(path)) ? JSON.parse(await $.fs.read(path)) : {};
620
- const file = {
621
- ...previous,
622
- jev: { ...previous.jev, timeoutMs: draft.timeoutMs },
623
- policy: { ...previous.policy, downgradeVotes: draft.downgradeVotes, downgradeHorizonTurns: draft.horizon },
624
- };
625
- const checked = loadConfig({ userFile: file });
626
- await $.fs.write(path, `${JSON.stringify(file, null, 2)}\n`);
627
- config = { ...checked, nativePath: path };
628
- await updateView($, runtime, { notice: 'Saved. New tuning applies to future turns.' });
629
- } catch {
630
- await updateView($, runtime, { notice: 'Could not save valid tuning. Existing configuration is unchanged.' });
631
- }
669
+ },
670
+ discardRoutes: () => updateView($, runtime, { routeDraft: null, notice: null }),
671
+ resetRoutes: () =>
672
+ updateView($, runtime, {
673
+ routeDraft: {
674
+ ...structuredClone({ routes: DEFAULTS.routes, baselineTier: DEFAULTS.baselineTier }),
675
+ base: routeDraftOf(config, {}).base,
676
+ },
677
+ notice: 'Defaults loaded. Save routes to remove your overrides from router.json.',
678
+ }),
679
+ tune: (key, value) =>
680
+ updateView($, runtime, {
681
+ tuning: { ...(runtime.view ?? view).tuning, [key]: value },
682
+ tuningBase: (runtime.view ?? view).tuningBase ?? tuningOf(config),
683
+ notice: null,
684
+ }),
685
+ saveTuning: async () => {
686
+ const current = runtime.view ?? view;
687
+ const base = current.tuningBase ?? tuningOf(config);
688
+ const draft = { ...base, ...current.tuning };
689
+ if (Object.keys(base).every((key) => draft[key] === base[key]))
690
+ return updateView($, runtime, { notice: 'No tuning changes to save.' });
691
+ return save(
692
+ (file) => withTuning(file, draft, base),
693
+ () => ({ tuning: null, tuningBase: null, notice: 'Saved. New tuning applies to future turns.' }),
694
+ );
695
+ },
696
+ resetTuning: () => updateView($, runtime, { tuning: null, tuningBase: null, notice: null }),
632
697
  },
633
- });
698
+ { modelOptions },
699
+ );
634
700
  });
635
701
  }
package/lib/config.mjs CHANGED
@@ -4,12 +4,12 @@ export const TIERS = ['micro', 'low', 'medium', 'high'];
4
4
  export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
5
5
  export const DEFAULTS = {
6
6
  baselineTier: 'low',
7
- // Sonnet 5.5 is the worker: `low` at the effort the user picked, `medium` at xhigh, so an escalation from `low`
8
- // stays on Sonnet's cache instead of a cold write to Opus. `high` is Opus 5.5 at xhigh, the strongest setting this
9
- // router asks for. These are routing defaults, not a claim of equal model quality.
7
+ // Sonnet 5.5 is the worker at `low`, at the effort the user picked. `medium` is Opus 5.5 at medium effort, a balance
8
+ // of speed and cost per task; `high` is Opus 5.5 at xhigh, the strongest setting this router asks for.
9
+ // These are routing defaults, not a claim of equal model quality.
10
10
  routes: {
11
11
  high: { model: 'opus', effort: 'xhigh' },
12
- medium: { model: 'sonnet', effort: 'xhigh' },
12
+ medium: { model: 'opus', effort: 'medium' },
13
13
  low: { model: 'sonnet' },
14
14
  micro: { model: 'haiku' },
15
15
  },
@@ -89,6 +89,77 @@ export function loadConfig({ env = {}, userFile = null } = {}) {
89
89
  return { ...config, apiKey };
90
90
  }
91
91
 
92
+ export const sameRoute = (a, b) => a.model === b.model && (a.effort ?? null) === (b.effort ?? null);
93
+
94
+ // A route draft carries `base`, the routes and baseline it was made from. A tier counts as edited when it differs
95
+ // from its base; everything else follows `current`, so a change saved elsewhere meanwhile is neither shown stale
96
+ // nor written back.
97
+ export function effectiveRoutes(draft, current) {
98
+ const { base } = draft;
99
+ return {
100
+ routes: Object.fromEntries(
101
+ TIERS.map((tier) => [
102
+ tier,
103
+ sameRoute(draft.routes[tier], base.routes[tier]) ? current.routes[tier] : draft.routes[tier],
104
+ ]),
105
+ ),
106
+ baselineTier: draft.baselineTier === base.baselineTier ? current.baselineTier : draft.baselineTier,
107
+ };
108
+ }
109
+
110
+ // The router.json `file` with the tiers and baseline the person edited in `draft`; everything else stays as the
111
+ // file has it now, so an edit made on disk meanwhile survives. Only differences from DEFAULTS are written, so a later
112
+ // change of a default still reaches tiers the user never edited; a route equal to the default is removed.
113
+ export function withRoutes(file, draft) {
114
+ const { base } = draft;
115
+ const { routes: kept = {}, baselineTier: keptBaseline, ...rest } = file;
116
+ const routes = {};
117
+ for (const tier of TIERS) {
118
+ if (sameRoute(draft.routes[tier], base.routes[tier])) {
119
+ // Untouched here: keep the file's own entry verbatim, so an inherited effort stays inherited.
120
+ if (kept[tier]) routes[tier] = kept[tier];
121
+ continue;
122
+ }
123
+ const { model, effort = null } = draft.routes[tier];
124
+ const preset = DEFAULTS.routes[tier];
125
+ if (model === preset.model && effort === (preset.effort ?? null)) continue;
126
+ routes[tier] = effort !== null || preset.effort ? { model, effort } : { model };
127
+ }
128
+ const baseline =
129
+ draft.baselineTier === base.baselineTier
130
+ ? keptBaseline
131
+ : draft.baselineTier === DEFAULTS.baselineTier
132
+ ? undefined
133
+ : draft.baselineTier;
134
+ return {
135
+ ...rest,
136
+ ...(Object.keys(routes).length ? { routes } : {}),
137
+ ...(baseline === undefined ? {} : { baselineTier: baseline }),
138
+ };
139
+ }
140
+
141
+ // The pane's tuning controls and the router.json fields they stand for.
142
+ const TUNING_FIELDS = {
143
+ timeoutMs: ['jev', 'timeoutMs'],
144
+ downgradeVotes: ['policy', 'downgradeVotes'],
145
+ horizon: ['policy', 'downgradeHorizonTurns'],
146
+ cashCapUsd: ['policy', 'cashCapUsd'],
147
+ };
148
+
149
+ export function tuningOf(config) {
150
+ return Object.fromEntries(
151
+ Object.entries(TUNING_FIELDS).map(([key, [section, field]]) => [key, config[section][field]]),
152
+ );
153
+ }
154
+
155
+ // The router.json `file` with only the tuning values the person changed in `draft` against `saved`.
156
+ export function withTuning(file, draft, saved) {
157
+ let next = file;
158
+ for (const [key, [section, field]] of Object.entries(TUNING_FIELDS))
159
+ if (draft[key] !== saved[key]) next = { ...next, [section]: { ...next[section], [field]: draft[key] } };
160
+ return next;
161
+ }
162
+
92
163
  export function rank(tier) {
93
164
  return TIERS.indexOf(tier);
94
165
  }
@@ -158,8 +229,9 @@ function validate(config) {
158
229
  const route = config.routes[tier];
159
230
  if (!route || typeof route.model !== 'string') throw new Error(`routes.${tier}.model is required`);
160
231
  if (!Object.hasOwn(config.models, route.model)) throw new Error(`routes.${tier}.model is not in models`);
161
- if (route.effort !== undefined && !EFFORTS.includes(route.effort))
162
- throw new Error(`routes.${tier}.effort must be one of ${EFFORTS.join(', ')}`);
232
+ // null keeps the session effort; it is how a file overrides a default route that names one.
233
+ if (route.effort != null && !EFFORTS.includes(route.effort))
234
+ throw new Error(`routes.${tier}.effort must be one of ${EFFORTS.join(', ')} or null`);
163
235
  }
164
236
  for (const [alias, model] of Object.entries(config.models)) {
165
237
  for (const field of ['input', 'cacheRead', 'contextWindow']) {