@alexeiled/claude-router 1.14.0 → 1.16.0
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.
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +61 -0
- package/README.md +4 -1
- package/docs/architecture.md +17 -3
- package/docs/configuration.md +6 -2
- package/docs/evaluation.md +15 -11
- package/docs/native-router.md +2 -0
- package/docs/router-band.svg +30 -8
- package/docs/router-pane-now.svg +133 -131
- package/docs/router-pane-policy.svg +47 -43
- package/docs/routing-modes.md +143 -0
- package/docs/user-guide.md +17 -2
- package/hooks/native-router.mjs +428 -36
- package/lib/activity-stats.mjs +33 -11
- package/lib/activity.mjs +12 -6
- package/lib/band.mjs +8 -1
- package/lib/config.mjs +72 -17
- package/lib/cost.mjs +3 -0
- package/lib/decisions.mjs +252 -0
- package/lib/display.mjs +46 -5
- package/lib/facts.mjs +125 -4
- package/lib/modes.mjs +98 -0
- package/lib/panel.mjs +41 -3
- package/lib/policy.mjs +7 -4
- package/lib/route.mjs +84 -26
- package/lib/savings.mjs +17 -0
- package/lib/usage.mjs +31 -19
- package/package.json +2 -1
- package/types/index.d.ts +15 -2
package/lib/display.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { acceptActivity } from './activity.mjs';
|
|
2
|
-
import { readStore, storeSummary } from './activity-stats.mjs';
|
|
2
|
+
import { readMetrics, readStore, storeSummary, UNPRICED_CAUSES } from './activity-stats.mjs';
|
|
3
3
|
import {
|
|
4
4
|
activeClassifier,
|
|
5
5
|
CLASSIFIER_OPTIONS,
|
|
@@ -7,8 +7,10 @@ import {
|
|
|
7
7
|
resolveRoute,
|
|
8
8
|
routeSpec,
|
|
9
9
|
sameRoute,
|
|
10
|
+
TIERS,
|
|
10
11
|
} from './config.mjs';
|
|
11
12
|
import { ratesAt } from './cost.mjs';
|
|
13
|
+
import { applyMode, modeLabel, modeRoute } from './modes.mjs';
|
|
12
14
|
import { PROBE_RESULTS } from './probe-results.mjs';
|
|
13
15
|
import { cellForModel } from './route.mjs';
|
|
14
16
|
import { emptyTotals, readout } from './savings.mjs';
|
|
@@ -21,6 +23,39 @@ const MODEL_COLOR = { haiku: '#c3e88d', sonnet: '#f78c6c', opus: '#c792ea', fabl
|
|
|
21
23
|
export const modelColor = (id) =>
|
|
22
24
|
Object.entries(MODEL_COLOR).find(([family]) => String(id ?? '').includes(family))?.[1] ?? null;
|
|
23
25
|
|
|
26
|
+
// The config as the view's routing mode routes it: the route texts name what runs, not the configured route. A view
|
|
27
|
+
// from before 1.15 has no mode and reads as balanced.
|
|
28
|
+
export const DEFAULT_ROUTING_MODE = { mode: 'balanced', source: 'default' };
|
|
29
|
+
export const routingModeOf = (view) => view.routingMode ?? DEFAULT_ROUTING_MODE;
|
|
30
|
+
export const shownConfig = (config, view) => applyMode(config, routingModeOf(view));
|
|
31
|
+
// `cost (session)`; the status band and the Now tab show it while routing is on.
|
|
32
|
+
export const routingModeText = (view) => modeLabel(routingModeOf(view));
|
|
33
|
+
|
|
34
|
+
// What the routing mode changed on the cell the band shows, against its configured route: 'effort only' when the mode
|
|
35
|
+
// kept the model and moved the effort. The cost estimates price the same tokens on each route, so an effort change
|
|
36
|
+
// shows no difference there; real output length and billing can differ. Null in balanced, without a tier, or when the
|
|
37
|
+
// mode moved the model or nothing.
|
|
38
|
+
export function modeLever(config, view) {
|
|
39
|
+
const { mode } = routingModeOf(view);
|
|
40
|
+
if (mode === 'balanced' || !TIERS.includes(view.tier)) return null;
|
|
41
|
+
const activity = config.activityRouting === 'on' ? (view.activity ?? null) : null;
|
|
42
|
+
const configured = resolveRoute(config, view.tier, activity);
|
|
43
|
+
const routed = modeRoute(config, mode, view.tier, activity);
|
|
44
|
+
return configured.model === routed.model && !sameRoute(configured, routed) ? 'effort only' : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The routed replies the usage record left out, by cause (UNPRICED_CAUSES), as one line, or null when there are none.
|
|
48
|
+
const UNPRICED_LABEL = { usage: 'no usage', model: 'no price', ttl: 'cache lifetime unknown', error: 'error' };
|
|
49
|
+
export function unpricedLine(metrics) {
|
|
50
|
+
const { unpriced } = readMetrics(metrics);
|
|
51
|
+
const total = UNPRICED_CAUSES.reduce((n, cause) => n + unpriced[cause], 0);
|
|
52
|
+
if (!total) return null;
|
|
53
|
+
const causes = UNPRICED_CAUSES.filter((cause) => unpriced[cause] > 0).map(
|
|
54
|
+
(cause) => `${UNPRICED_LABEL[cause]} ${unpriced[cause]}`,
|
|
55
|
+
);
|
|
56
|
+
return `Unpriced ${total} routed repl${total === 1 ? 'y' : 'ies'} left out of usage: ${causes.join(' · ')}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
24
59
|
// Why routing starts off: the session's model is no tier's model. Set at session start, read by the band.
|
|
25
60
|
export const NOT_A_TIER_REASON = 'not a routing tier';
|
|
26
61
|
|
|
@@ -65,7 +100,7 @@ export const REASONS = {
|
|
|
65
100
|
downgrade: 'enough support for a cheaper model',
|
|
66
101
|
'upgrade-pending': 'waiting before upgrading',
|
|
67
102
|
'downgrade-pending': 'switching is not justified yet',
|
|
68
|
-
'downgrade-held': 'a cheaper model waits for a
|
|
103
|
+
'downgrade-held': 'a cheaper model waits for a checkpoint on this task',
|
|
69
104
|
hold: 'staying after an escalation',
|
|
70
105
|
escalation: 'repeated tool failures need a stronger model',
|
|
71
106
|
'cash-gate': 'estimated cold cache write exceeds the cap',
|
|
@@ -135,7 +170,7 @@ export const SHORT_REASONS = {
|
|
|
135
170
|
downgrade: '↓ downgrade',
|
|
136
171
|
'upgrade-pending': '… waiting to go up',
|
|
137
172
|
'downgrade-pending': '… waiting to go down',
|
|
138
|
-
'downgrade-held': '… down after a
|
|
173
|
+
'downgrade-held': '… down after a checkpoint',
|
|
139
174
|
hold: '= holding',
|
|
140
175
|
escalation: '↑ tool errors',
|
|
141
176
|
'cash-gate': '= cash cap',
|
|
@@ -191,7 +226,8 @@ export function activityReading(view) {
|
|
|
191
226
|
}
|
|
192
227
|
|
|
193
228
|
// The route the turn's cell resolves to, against the tier's base route. Null outside `on`, or before a turn has a tier.
|
|
194
|
-
function activityRouteText(
|
|
229
|
+
function activityRouteText(configured, view) {
|
|
230
|
+
const config = shownConfig(configured, view);
|
|
195
231
|
if (config.activityRouting !== 'on' || !view.tier) return null;
|
|
196
232
|
const cell = `${view.tier}${view.activity ? ` + ${view.activity}` : ''}`;
|
|
197
233
|
const route = resolveRoute(config, view.tier, view.activity ?? null);
|
|
@@ -207,7 +243,8 @@ const STAYED = ['ready', 'same-tier', 'no-advice', 'uncertain', 'downgrade-held'
|
|
|
207
243
|
// The Now tab's Why. In `on`, an activity move, or a stay on an override cell, says the cell plainly: "coding at low
|
|
208
244
|
// runs on Sonnet 5.5 · high", or for a move to no activity "low runs on its base route, Haiku 5.5 · high".
|
|
209
245
|
// Every other decision keeps its reason: a refusal, a hold or a tier move says more than the cell.
|
|
210
|
-
export function whyText(
|
|
246
|
+
export function whyText(configured, view) {
|
|
247
|
+
const config = shownConfig(configured, view);
|
|
211
248
|
const reason = REASONS[view.reason] ?? view.reason ?? 'ready';
|
|
212
249
|
if (config.activityRouting !== 'on' || !view.tier) return reason;
|
|
213
250
|
const activity = view.activity ?? null;
|
|
@@ -215,6 +252,10 @@ export function whyText(config, view) {
|
|
|
215
252
|
const override = activity !== null && !sameRoute(route, resolveRoute(config, view.tier));
|
|
216
253
|
const moved = view.reason === 'activity-up' || view.reason === 'activity-down';
|
|
217
254
|
if (!moved && !(override && STAYED.includes(view.reason ?? 'ready'))) return reason;
|
|
255
|
+
// A held move keeps the route in use, which a routing mode may have moved off this cell's route: say only why.
|
|
256
|
+
const runs =
|
|
257
|
+
routeSpec(config, route).id === view.selectedModel && (route.effort === null || route.effort === view.effort);
|
|
258
|
+
if (!moved && view.selectedModel && !runs) return reason;
|
|
218
259
|
return activity
|
|
219
260
|
? `${activityVerb(activity)} at ${view.tier} runs on ${routeName(config, route)}`
|
|
220
261
|
: `${view.tier} runs on its base route, ${routeName(config, route)}`;
|
package/lib/facts.mjs
CHANGED
|
@@ -108,6 +108,12 @@ const SEPARATORS = /&&|\|\||[;|&\n]/;
|
|
|
108
108
|
// find can run or delete what it finds.
|
|
109
109
|
const WRITING_FIND = /\s-(?:exec|execdir|ok|okdir|delete)\b/;
|
|
110
110
|
|
|
111
|
+
// A command as the classifiers read it: quoted text blanked, so a `;` or `>` inside a pattern is not shell syntax, and
|
|
112
|
+
// redirects of a stream into another stream or into /dev/null dropped, as they write no file. Every classifier below
|
|
113
|
+
// reads this one form, so they agree on what a command is.
|
|
114
|
+
const bareCommand = (command) =>
|
|
115
|
+
command.replace(/'[^']*'|"[^"]*"/g, '""').replace(/\d?>\s*&\d|\d?>\s*\/dev\/null/g, '');
|
|
116
|
+
|
|
111
117
|
// The index of the last user prompt, a user message that is not a tool result: where the latest turn starts. -1 when
|
|
112
118
|
// there is none.
|
|
113
119
|
export function promptIndex(messages) {
|
|
@@ -117,7 +123,10 @@ export function promptIndex(messages) {
|
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
// The bucket of a finished turn, from the tool calls the assistant made after the user message at `fromIndex`:
|
|
120
|
-
// code, docs, ops, read or talk. First match wins; only names and paths are read, no text is kept.
|
|
126
|
+
// code, docs, shell, ops, check, read or talk. First match wins; only names and paths are read, no text is kept.
|
|
127
|
+
// `shell` is a candidate, not proven code: a command that writes a file's content (a redirect, `tee`, `sed -i`, a
|
|
128
|
+
// patch), which may be code, docs or routine work. `check` is a turn whose only commands that are not reads are
|
|
129
|
+
// checks (`npm test`, `tsc`): a review or a verification. Routine file commands (`rm`, `mv`, `mkdir`) stay `ops`.
|
|
121
130
|
export function observedActivity(messages, fromIndex) {
|
|
122
131
|
const calls = [];
|
|
123
132
|
for (let i = fromIndex + 1; i < messages.length; i += 1)
|
|
@@ -125,10 +134,55 @@ export function observedActivity(messages, fromIndex) {
|
|
|
125
134
|
for (const block of blocks(messages[i].content)) if (block.type === 'tool_use') calls.push(block);
|
|
126
135
|
const edited = calls.filter((c) => FILE_EDIT_TOOLS.has(c.name));
|
|
127
136
|
if (edited.length > 0) return edited.every((c) => DOC_PATH.test(editPath(c))) ? 'docs' : 'code';
|
|
128
|
-
|
|
137
|
+
const commands = calls.filter((c) => c.name === 'Bash').map((c) => commandKind(c.input?.command));
|
|
138
|
+
if (commands.includes('shell')) return 'shell';
|
|
139
|
+
if (commands.includes('ops')) return 'ops';
|
|
140
|
+
if (commands.includes('check')) return 'check';
|
|
129
141
|
return calls.length > 0 ? 'read' : 'talk';
|
|
130
142
|
}
|
|
131
143
|
|
|
144
|
+
// The activity routing reads from a bucket: `shell` and `check` route as `ops`, the commands they are. The finer
|
|
145
|
+
// buckets serve the agreement counts only, so they change no route.
|
|
146
|
+
export const routingBucket = (bucket) => (bucket === 'shell' || bucket === 'check' ? 'ops' : bucket);
|
|
147
|
+
|
|
148
|
+
// One Bash command as `read`, `shell` (writes a file's content), `check` (a checker whose status is its own) or
|
|
149
|
+
// `ops` (anything else that runs).
|
|
150
|
+
function commandKind(command) {
|
|
151
|
+
if (readsOnly(command)) return 'read';
|
|
152
|
+
if (writesFile(command)) return 'shell';
|
|
153
|
+
return verifies(command) && checksOnly(command) ? 'check' : 'ops';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Some command of the pipeline or list runs a check, whatever its status reports: `npm test | tail`, `npm test; true`.
|
|
157
|
+
function runsAnyCheck(command) {
|
|
158
|
+
if (typeof command !== 'string') return false;
|
|
159
|
+
return bareCommand(command)
|
|
160
|
+
.split(SEPARATORS)
|
|
161
|
+
.some((part) => part.trim() !== '' && runsCheck(part.trim().split(/\s+/)));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Every step of an `&&` list is a check or a read: `npm test && git commit` runs something besides the check.
|
|
165
|
+
function checksOnly(command) {
|
|
166
|
+
const bare = bareCommand(command);
|
|
167
|
+
return bare.split('&&').every((step) => runsCheck(step.trim().split(/\s+/)) || readsOnly(step));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// A command that writes a file's content: an output redirect to a file, `tee`, an in-place `sed` or `perl`, `patch`
|
|
171
|
+
// or `git apply`. Quoted text is blanked first, as readsOnly does, so a `>` inside a pattern is not a redirect.
|
|
172
|
+
const SHELL_WRITERS = [
|
|
173
|
+
/(?:^|[^<>&\d])\d?>>?\s*[^\s&|>]/,
|
|
174
|
+
/(?:^|[\s;&|(])tee\b/,
|
|
175
|
+
/(?:^|[\s;&|(])sed\b[^;&|]*\s(?:-[a-zA-Z]*i|--in-place)\b/,
|
|
176
|
+
/(?:^|[\s;&|(])perl\b[^;&|]*\s-[a-zA-Z]*i/,
|
|
177
|
+
/(?:^|[\s;&|(])patch\b/,
|
|
178
|
+
/(?:^|[\s;&|(])git\s+apply\b/,
|
|
179
|
+
];
|
|
180
|
+
function writesFile(command) {
|
|
181
|
+
if (typeof command !== 'string') return false;
|
|
182
|
+
const bare = bareCommand(command);
|
|
183
|
+
return SHELL_WRITERS.some((pattern) => pattern.test(bare));
|
|
184
|
+
}
|
|
185
|
+
|
|
132
186
|
function editPath(call) {
|
|
133
187
|
const path = call.input?.file_path ?? call.input?.notebook_path;
|
|
134
188
|
return typeof path === 'string' ? path : '';
|
|
@@ -139,7 +193,7 @@ function editPath(call) {
|
|
|
139
193
|
// a pattern is not taken for shell syntax. Anything unrecognised counts as running something.
|
|
140
194
|
function readsOnly(command) {
|
|
141
195
|
if (typeof command !== 'string' || /`|\$\(/.test(command)) return false;
|
|
142
|
-
const bare = command
|
|
196
|
+
const bare = bareCommand(command);
|
|
143
197
|
if (/[<>]/.test(bare) || WRITING_FIND.test(bare)) return false;
|
|
144
198
|
return bare.split(SEPARATORS).every((part) => {
|
|
145
199
|
if (!part.trim()) return true;
|
|
@@ -184,7 +238,7 @@ const CHECK_WORD =
|
|
|
184
238
|
|
|
185
239
|
function verifies(command) {
|
|
186
240
|
if (typeof command !== 'string') return false;
|
|
187
|
-
const bare = command
|
|
241
|
+
const bare = bareCommand(command);
|
|
188
242
|
const steps = bare.split('&&');
|
|
189
243
|
if (steps.some((step) => /\|\||[;|&\n]/.test(step))) return false;
|
|
190
244
|
return steps.some((step) => runsCheck(step.trim().split(/\s+/)));
|
|
@@ -229,3 +283,70 @@ export function verifiedSince(messages, fromIndex) {
|
|
|
229
283
|
|
|
230
284
|
// What Claude Code's Bash tool returns for a command it moved to the background.
|
|
231
285
|
const BACKGROUNDED = /running in background|moved to the background/i;
|
|
286
|
+
|
|
287
|
+
const DELEGATING = new Set(['Agent', 'Task']);
|
|
288
|
+
const READ_ONLY_ACTIVITIES = new Set(['explore', 'plan', 'review']);
|
|
289
|
+
|
|
290
|
+
// Positive evidence that the work since message `fromIndex` reached a checkpoint, by the activity the running route
|
|
291
|
+
// serves: the checkpoint a move to a cheaper route on the same task waits for. A passed check (verifiedSince) always
|
|
292
|
+
// counts. Without one:
|
|
293
|
+
// - `ops`: the last command that ran something succeeded, and no edit or delegation happened.
|
|
294
|
+
// - `docs`: the last edit succeeded and touched only doc paths, with no other command writing or running.
|
|
295
|
+
// - `explore`, `plan`, `review`: a turn finished since (a newer prompt), nothing was changed or delegated, and the last
|
|
296
|
+
// tool result was not an error.
|
|
297
|
+
// Any other activity, none, or work outside the activity's shape needs the passed check, and so does work where a check
|
|
298
|
+
// ran without passing last: failed, or in a form whose status is not the check's (`npm test && git push`,
|
|
299
|
+
// `npm test > log`, `npm test | tail`, `; `, `||`, the background). The absence of an edit alone is never evidence:
|
|
300
|
+
// each rule asks for a success it can see.
|
|
301
|
+
export function completedSince(messages, fromIndex, activity) {
|
|
302
|
+
if (verifiedSince(messages, fromIndex)) return true;
|
|
303
|
+
const calls = callsSince(messages, fromIndex);
|
|
304
|
+
if (calls.some(({ block }) => DELEGATING.has(block.name))) return false;
|
|
305
|
+
const lastResult = lastResultSince(messages, fromIndex);
|
|
306
|
+
// A check that ran, in any form, and did not pass last (verifiedSince is false) blocks every shortcut.
|
|
307
|
+
if (calls.some(({ block }) => block.name === 'Bash' && runsAnyCheck(block.input?.command))) return false;
|
|
308
|
+
const edits = calls.filter(({ block }) => FILE_EDIT_TOOLS.has(block.name));
|
|
309
|
+
const runs = calls.filter(
|
|
310
|
+
({ block }) => block.name === 'Bash' && ['ops', 'shell'].includes(commandKind(block.input?.command)),
|
|
311
|
+
);
|
|
312
|
+
if (activity === 'ops') {
|
|
313
|
+
if (edits.length > 0 || runs.length === 0) return false;
|
|
314
|
+
return succeeded(messages, runs.at(-1));
|
|
315
|
+
}
|
|
316
|
+
if (activity === 'docs') {
|
|
317
|
+
if (runs.length > 0 || edits.length === 0) return false;
|
|
318
|
+
if (!edits.every(({ block }) => DOC_PATH.test(editPath(block)))) return false;
|
|
319
|
+
return succeeded(messages, edits.at(-1));
|
|
320
|
+
}
|
|
321
|
+
if (READ_ONLY_ACTIVITIES.has(activity)) {
|
|
322
|
+
if (edits.length > 0 || runs.length > 0) return false;
|
|
323
|
+
return promptIndex(messages) >= fromIndex && fromIndex >= 0 && lastResult?.is_error !== true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function callsSince(messages, fromIndex) {
|
|
329
|
+
const calls = [];
|
|
330
|
+
for (let i = Math.max(0, fromIndex); i < messages.length; i += 1)
|
|
331
|
+
if (messages[i]?.role === 'assistant')
|
|
332
|
+
for (const block of blocks(messages[i].content)) if (block.type === 'tool_use') calls.push({ block, index: i });
|
|
333
|
+
return calls;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function lastResultSince(messages, fromIndex) {
|
|
337
|
+
let last = null;
|
|
338
|
+
for (let i = Math.max(0, fromIndex); i < messages.length; i += 1)
|
|
339
|
+
if (messages[i]?.role === 'user')
|
|
340
|
+
for (const block of blocks(messages[i].content)) if (block.type === 'tool_result') last = block;
|
|
341
|
+
return last;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Whether the call's result came back without an error, and not as a launch into the background.
|
|
345
|
+
function succeeded(messages, call) {
|
|
346
|
+
if (call.block.input?.run_in_background === true) return false;
|
|
347
|
+
for (let i = call.index + 1; i < messages.length; i += 1)
|
|
348
|
+
for (const block of blocks(messages[i]?.content))
|
|
349
|
+
if (block.type === 'tool_result' && block.tool_use_id === call.block.id)
|
|
350
|
+
return !block.is_error && !BACKGROUNDED.test(textOf(blocks(block.content)));
|
|
351
|
+
return false;
|
|
352
|
+
}
|
package/lib/modes.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Routing modes (docs/plans/2026-10-21-routing-modes.md): one setting that trades cost against answer quality. The
|
|
2
|
+
// classifier and the configured routes stay as they are; the mode moves each tier's route by one fixed step. Pure.
|
|
3
|
+
import { ACTIVITIES, DEFAULTS, EFFORTS, ROUTING_MODES, rank, resolveRoute, sameRoute, TIERS } from './config.mjs';
|
|
4
|
+
import { clampEffort } from './cost.mjs';
|
|
5
|
+
|
|
6
|
+
// `wanted` on a model that lists `supported`: itself, else the next higher listed, else the highest lower one.
|
|
7
|
+
export function projectEffort(supported, wanted) {
|
|
8
|
+
if (supported.includes(wanted)) return wanted;
|
|
9
|
+
const at = EFFORTS.indexOf(wanted);
|
|
10
|
+
return (
|
|
11
|
+
EFFORTS.slice(at + 1).find((e) => supported.includes(e)) ??
|
|
12
|
+
EFFORTS.slice(0, at)
|
|
13
|
+
.reverse()
|
|
14
|
+
.find((e) => supported.includes(e)) ??
|
|
15
|
+
null
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The route's effort one level up (`step` 1) or down (-1), on what the model lists; null when it cannot move that
|
|
20
|
+
// way: a null effort (the session's), a model without efforts, or no listed level in that direction. The model's
|
|
21
|
+
// lowest listed level is the floor, so a route never loses its effort.
|
|
22
|
+
function steppedEffort(config, route, step) {
|
|
23
|
+
const supported = config.models[route.model].efforts;
|
|
24
|
+
const from = clampEffort(route.effort, supported);
|
|
25
|
+
if (!from) return null;
|
|
26
|
+
const wanted = EFFORTS[EFFORTS.indexOf(from) + step];
|
|
27
|
+
const effort = wanted ? projectEffort(supported, wanted) : null;
|
|
28
|
+
return effort && Math.sign(EFFORTS.indexOf(effort) - EFFORTS.indexOf(from)) === step ? effort : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The route `mode` runs for tier and activity, always from the configured routes. `cost` takes the tier below (never
|
|
32
|
+
// below `low`) when it runs a model with a lower input price, else one effort level less; `quality` one effort level
|
|
33
|
+
// more, else the tier above. One lever per decision: cost pays mostly for input and cache tokens, which effort does
|
|
34
|
+
// not lower, and quality keeps the model and its cache. `micro` and `balanced` keep the configured route.
|
|
35
|
+
export function modeRoute(config, mode, tier, activity = null) {
|
|
36
|
+
const routeAt = (t) => resolveRoute(config, t, activity);
|
|
37
|
+
const route = routeAt(tier);
|
|
38
|
+
if (mode === 'balanced' || tier === 'micro') return route;
|
|
39
|
+
if (mode === 'cost') {
|
|
40
|
+
const below = routeAt(TIERS[Math.max(rank('low'), rank(tier) - 1)]);
|
|
41
|
+
if (below.model !== route.model && config.models[below.model].input < config.models[route.model].input)
|
|
42
|
+
return below;
|
|
43
|
+
const effort = steppedEffort(config, route, -1);
|
|
44
|
+
return effort ? { model: route.model, effort } : route;
|
|
45
|
+
}
|
|
46
|
+
const effort = steppedEffort(config, route, 1);
|
|
47
|
+
return effort ? { model: route.model, effort } : routeAt(TIERS[Math.min(TIERS.length - 1, rank(tier) + 1)]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The config a turn routes on in `routingMode` ({ mode, source }): every tier's and activity's route as modeRoute
|
|
51
|
+
// gives it, and the mode's policy fields over `policy`. `config` must be the configured one (router.config), never a
|
|
52
|
+
// result of this function: the transform starts from the configured routes, so repeated turns never step twice.
|
|
53
|
+
// `balanced` keeps the routes object as configured.
|
|
54
|
+
export function applyMode(config, routingMode) {
|
|
55
|
+
const { mode } = routingMode;
|
|
56
|
+
const policy = { ...config.policy, ...config.modes[mode]?.policy };
|
|
57
|
+
if (mode === 'balanced') return { ...config, policy, routingMode };
|
|
58
|
+
const routes = Object.fromEntries(TIERS.map((tier) => [tier, modeRoute(config, mode, tier)]));
|
|
59
|
+
const activities = {};
|
|
60
|
+
for (const activity of ACTIVITIES)
|
|
61
|
+
for (const tier of TIERS) {
|
|
62
|
+
const route = modeRoute(config, mode, tier, activity);
|
|
63
|
+
if (!sameRoute(route, routes[tier])) activities[activity] = { ...activities[activity], [tier]: route };
|
|
64
|
+
}
|
|
65
|
+
return { ...config, routes, activities, policy, routingMode };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The active mode and its source: a session override, then the project file, then the profile router.json.
|
|
69
|
+
export function resolveRoutingMode({ session = null, project = null, profile = DEFAULTS.mode } = {}) {
|
|
70
|
+
if (ROUTING_MODES.includes(session)) return { mode: session, source: 'session' };
|
|
71
|
+
if (ROUTING_MODES.includes(project)) return { mode: project, source: 'project' };
|
|
72
|
+
if (profile !== DEFAULTS.mode) return { mode: profile, source: 'profile' };
|
|
73
|
+
return { mode: DEFAULTS.mode, source: 'default' };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// `cost (session)`.
|
|
77
|
+
export const modeLabel = ({ mode, source }) => `${mode} (${source})`;
|
|
78
|
+
|
|
79
|
+
// A project's `.claude/router.json`, which may hold only `mode`: the repo chooses among routes the user configured,
|
|
80
|
+
// never the routes, models or classifier. Returns { mode, error }: a file that does not read is ignored, with a reason
|
|
81
|
+
// that names the key, never its value. No file: { mode: null, error: null }.
|
|
82
|
+
export function readProjectMode(text) {
|
|
83
|
+
if (text === null) return { mode: null, error: null };
|
|
84
|
+
let file;
|
|
85
|
+
try {
|
|
86
|
+
file = JSON.parse(text);
|
|
87
|
+
} catch {
|
|
88
|
+
return { mode: null, error: 'project .claude/router.json is not valid JSON' };
|
|
89
|
+
}
|
|
90
|
+
if (typeof file !== 'object' || file === null || Array.isArray(file))
|
|
91
|
+
return { mode: null, error: 'project .claude/router.json must be an object' };
|
|
92
|
+
const other = Object.keys(file).find((key) => key !== 'mode');
|
|
93
|
+
if (other !== undefined) return { mode: null, error: `project .claude/router.json may only set mode, not ${other}` };
|
|
94
|
+
if (!Object.hasOwn(file, 'mode')) return { mode: null, error: null };
|
|
95
|
+
if (!ROUTING_MODES.includes(file.mode))
|
|
96
|
+
return { mode: null, error: `project .claude/router.json mode must be one of ${ROUTING_MODES.join(', ')}` };
|
|
97
|
+
return { mode: file.mode, error: null };
|
|
98
|
+
}
|
package/lib/panel.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
DEFAULTS,
|
|
8
8
|
effectiveActivities,
|
|
9
9
|
effectiveRoutes,
|
|
10
|
+
ROUTING_MODES,
|
|
10
11
|
resolveRoute,
|
|
11
12
|
sameCell,
|
|
12
13
|
sameRoute,
|
|
@@ -32,11 +33,15 @@ import {
|
|
|
32
33
|
probeOf,
|
|
33
34
|
routeLabel,
|
|
34
35
|
routeName,
|
|
36
|
+
routingModeOf,
|
|
37
|
+
routingModeText,
|
|
35
38
|
savingsColor,
|
|
39
|
+
shownConfig,
|
|
36
40
|
signedUsd,
|
|
37
41
|
sparkline,
|
|
38
42
|
TIER_COLOR,
|
|
39
43
|
unavailableReason,
|
|
44
|
+
unpricedLine,
|
|
40
45
|
usageMetrics,
|
|
41
46
|
usd,
|
|
42
47
|
whyText,
|
|
@@ -279,10 +284,11 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
|
279
284
|
const served = view.actualModel && current && !isSameModel(current, view.actualModel);
|
|
280
285
|
const mode = config.activityRouting;
|
|
281
286
|
const reading = mode === 'off' ? null : activityReading(view);
|
|
282
|
-
// In `on` an override names the tier's own model.
|
|
283
|
-
const
|
|
287
|
+
// In `on` an override names the tier's own model, as the routing mode runs it.
|
|
288
|
+
const shown = shownConfig(config, view);
|
|
289
|
+
const base = view.tier ? resolveRoute(shown, view.tier) : null;
|
|
284
290
|
const override =
|
|
285
|
-
mode === 'on' && view.activity && base && !sameRoute(resolveRoute(
|
|
291
|
+
mode === 'on' && view.activity && base && !sameRoute(resolveRoute(shown, view.tier, view.activity), base);
|
|
286
292
|
const support = auto ? supportText(label, view.estimate) : null;
|
|
287
293
|
const out = [
|
|
288
294
|
ui.line(
|
|
@@ -304,6 +310,12 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
|
304
310
|
: null,
|
|
305
311
|
ui.line('Why ', whyText(config, view)),
|
|
306
312
|
support ? ui.dim(` ${support}`) : null,
|
|
313
|
+
ui.line(
|
|
314
|
+
'Mode ',
|
|
315
|
+
routingModeText(view),
|
|
316
|
+
routingModeOf(view).source === 'session' ? ui.dim(' /router mode default clears it') : null,
|
|
317
|
+
),
|
|
318
|
+
view.projectError ? ui.text(` Ignored: ${view.projectError}`, { color: 'yellow' }) : null,
|
|
307
319
|
served
|
|
308
320
|
? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
|
|
309
321
|
: null,
|
|
@@ -926,6 +938,13 @@ function choice(ui, key, value, format, pick) {
|
|
|
926
938
|
|
|
927
939
|
// The Policy tab: each control inside the sentence it completes, its default named when the value differs. One Save
|
|
928
940
|
// writes it with the Routes draft.
|
|
941
|
+
// What the profile's routing mode does, under its control on the Policy tab.
|
|
942
|
+
const MODE_HINT = {
|
|
943
|
+
cost: 'a cheaper model or one effort level less where a turn allows',
|
|
944
|
+
balanced: 'each tier runs its route as configured',
|
|
945
|
+
quality: 'one effort level more, or the tier above, where a turn allows',
|
|
946
|
+
};
|
|
947
|
+
|
|
929
948
|
function policyTab(ui, config, view, _usage, actions) {
|
|
930
949
|
const changes = routingChanges(config, view);
|
|
931
950
|
const { draft, tuning, policy } = changes;
|
|
@@ -939,9 +958,26 @@ function policyTab(ui, config, view, _usage, actions) {
|
|
|
939
958
|
];
|
|
940
959
|
const { upgradeBase, upgradeSlope } = config.policy;
|
|
941
960
|
const credits = Object.values(config.models).some((model) => model.billing === 'credits');
|
|
961
|
+
const active = routingModeOf(view);
|
|
942
962
|
return [
|
|
943
963
|
ui.line(ui.head('POLICY'), ui.dim(' · how readily the router changes models · edit, then Save')),
|
|
944
964
|
ui.text(' '),
|
|
965
|
+
ui.line(
|
|
966
|
+
ui.cell(13, ui.text('Mode', { bold: true })),
|
|
967
|
+
ui.Select
|
|
968
|
+
? ui.Select({
|
|
969
|
+
key: 'routing-mode',
|
|
970
|
+
value: config.mode,
|
|
971
|
+
options: ROUTING_MODES.map((mode) => ({ value: mode, label: mode })),
|
|
972
|
+
onSelect: (mode) => actions.routingMode(mode),
|
|
973
|
+
})
|
|
974
|
+
: config.mode,
|
|
975
|
+
ui.dim(' saved at once'),
|
|
976
|
+
),
|
|
977
|
+
ui.dim(`${pad}${MODE_HINT[config.mode]}`),
|
|
978
|
+
active.source === 'session' || active.source === 'project'
|
|
979
|
+
? ui.text(`${pad}this session runs ${routingModeText(view)}`, { color: 'yellow' })
|
|
980
|
+
: null,
|
|
945
981
|
ui.line(
|
|
946
982
|
ui.cell(13, ui.text('Start', { bold: true })),
|
|
947
983
|
'at ',
|
|
@@ -1185,6 +1221,8 @@ function storeUsage(ui, store, metrics) {
|
|
|
1185
1221
|
out.push(
|
|
1186
1222
|
ui.text(`Escalations after a cheaper activity move: ${escalations} in ${moves} move${moves === 1 ? '' : 's'}`),
|
|
1187
1223
|
);
|
|
1224
|
+
const unpriced = unpricedLine(metrics);
|
|
1225
|
+
if (unpriced) out.push(ui.text(unpriced));
|
|
1188
1226
|
return out;
|
|
1189
1227
|
}
|
|
1190
1228
|
|
package/lib/policy.mjs
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
import { rank, resolveRoute, routeSpec, TIERS } from './config.mjs';
|
|
3
3
|
import * as nativeCosts from './cost.mjs';
|
|
4
4
|
|
|
5
|
-
// Whether an answer is about the task already running: a review inside a turn (facts.midTurn),
|
|
6
|
-
//
|
|
7
|
-
//
|
|
5
|
+
// Whether an answer is about the task already running: a review inside a turn (facts.midTurn), a turn the engine
|
|
6
|
+
// started from progress on the running work (facts.progress: a task notification, an observer's report), or a new
|
|
7
|
+
// prompt the classifier scores as a continuation. On the same task either way is open, but a cheaper route waits for
|
|
8
|
+
// facts.downAllowed: the dwell since the last model change and a checkpoint the activity can show (completedSince).
|
|
8
9
|
export function sameTaskOf(config, advice, facts) {
|
|
9
|
-
return
|
|
10
|
+
return (
|
|
11
|
+
Boolean(facts.midTurn) || Boolean(facts.progress) || (advice?.continuation ?? 0) >= config.policy.continuationMass
|
|
12
|
+
);
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
export function initialState() {
|