@alexeiled/claude-router 1.3.0 → 1.3.2
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 +20 -0
- package/README.md +2 -2
- package/docs/architecture.md +17 -13
- package/docs/configuration.md +3 -3
- package/docs/evaluation.md +1 -1
- package/docs/native-router.md +1 -1
- package/docs/router-pane-now.svg +1 -1
- package/docs/router-pane-routing.svg +4 -4
- package/docs/user-guide.md +1 -1
- package/hooks/native-router.mjs +423 -519
- package/lib/{native-band.mjs → band.mjs} +22 -28
- package/lib/{native-jev.mjs → classifier-client.mjs} +8 -8
- package/lib/{jev-contract.mjs → classifier-contract.mjs} +2 -2
- package/lib/config-file.mjs +62 -0
- package/lib/config.mjs +21 -8
- package/lib/{native-cost.mjs → cost.mjs} +6 -6
- package/lib/{native-display.mjs → display.mjs} +42 -14
- package/lib/{native-panel.mjs → panel.mjs} +15 -23
- package/lib/policy.mjs +6 -6
- package/lib/{native-router.mjs → route.mjs} +30 -31
- package/lib/view.mjs +71 -0
- package/package.json +6 -4
- /package/lib/{facts-pure.mjs → facts.mjs} +0 -0
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { activeClassifier, TIERS } from './config.mjs';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
classifierLabel,
|
|
4
|
+
classifierStatus,
|
|
5
|
+
classifierWarns,
|
|
6
|
+
GATEWAY_SETTINGS,
|
|
7
|
+
modelName,
|
|
8
|
+
percent,
|
|
9
|
+
routeLabel,
|
|
10
|
+
SHORT_REASONS,
|
|
11
|
+
switchCount,
|
|
12
|
+
TIER_COLOR,
|
|
13
|
+
usageMetrics,
|
|
14
|
+
} from './display.mjs';
|
|
15
|
+
import { isSameModel } from './route.mjs';
|
|
4
16
|
|
|
5
17
|
const BARS = '▂▄▆█';
|
|
6
18
|
const SEPARATOR = ' · ';
|
|
7
19
|
// Space the band keeps for the trailing Router button.
|
|
8
20
|
const ROUTER_BUTTON = 8;
|
|
9
21
|
const STRIP = 20;
|
|
10
|
-
// Classifier failures worth a warning; busy and cancelled are routine and pass silently.
|
|
11
|
-
const WARNED = new Set([
|
|
12
|
-
'missing-key',
|
|
13
|
-
'missing-account',
|
|
14
|
-
'paused',
|
|
15
|
-
'timeout',
|
|
16
|
-
'auth',
|
|
17
|
-
'http',
|
|
18
|
-
'unreachable',
|
|
19
|
-
'policy',
|
|
20
|
-
'malformed',
|
|
21
|
-
]);
|
|
22
22
|
|
|
23
23
|
const part = (text, style = {}) => ({ text, style });
|
|
24
24
|
const button = (props) => ({ button: props });
|
|
@@ -56,23 +56,18 @@ function meter(tier, dim = false) {
|
|
|
56
56
|
return [...BARS].map((bar, i) => part(bar, i <= lit ? { color: TIER_COLOR[tier] } : { dimColor: true }));
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
const id = view.actualModel ?? view.selectedModel ?? view.nativeModel;
|
|
61
|
-
const effort = view.effort === null || view.effort === undefined ? '' : ` · ${view.effort}`;
|
|
62
|
-
return `${modelName(id)}${effort}`;
|
|
63
|
-
}
|
|
59
|
+
const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
|
|
64
60
|
|
|
65
61
|
function served(view) {
|
|
66
62
|
const { actualModel: actual, selectedModel: selected } = view;
|
|
67
|
-
return Boolean(actual && selected &&
|
|
63
|
+
return Boolean(actual && selected && !isSameModel(selected, actual));
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
function supportText(config, estimate) {
|
|
71
67
|
if (!Number.isFinite(estimate?.threshold)) return null;
|
|
72
68
|
const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
|
|
73
69
|
if (!Number.isFinite(mass)) return null;
|
|
74
|
-
|
|
75
|
-
return `${classifierLabel(config)} ${pct(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${pct(estimate.threshold)}`;
|
|
70
|
+
return `${classifierLabel(config)} ${percent(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${percent(estimate.threshold)}`;
|
|
76
71
|
}
|
|
77
72
|
|
|
78
73
|
export function bandSegments(config, view, usage, actions) {
|
|
@@ -130,7 +125,7 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
130
125
|
);
|
|
131
126
|
// The warning leads and the route yields: when the band is narrow, what to fix matters more than the model kept,
|
|
132
127
|
// and a long classifier label ("Clef Flash: no account ID") must not push the warning out.
|
|
133
|
-
if (
|
|
128
|
+
if (classifierWarns(view.error))
|
|
134
129
|
return [
|
|
135
130
|
segment(0, part(`⚠ ${classifierStatus(config, view.error)}`, { color: 'yellow' })),
|
|
136
131
|
view.error === 'missing-key' || view.error === 'missing-account'
|
|
@@ -141,7 +136,7 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
141
136
|
segment(3, part('keeping model', { color: 'yellow' })),
|
|
142
137
|
].filter(Boolean);
|
|
143
138
|
const metrics = usageMetrics(config, view, usage);
|
|
144
|
-
const
|
|
139
|
+
const reading = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
|
|
145
140
|
const support = supportText(config, view.estimate);
|
|
146
141
|
return [
|
|
147
142
|
main,
|
|
@@ -150,7 +145,7 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
150
145
|
support ? segment(4, part(support, { dimColor: true })) : null,
|
|
151
146
|
metrics.contextBar.percent === null && metrics.cacheBar.percent === null
|
|
152
147
|
? null
|
|
153
|
-
: segment(3, part('ctx '),
|
|
148
|
+
: segment(3, part('ctx '), reading(metrics.contextBar), part(' · cache '), reading(metrics.cacheBar)),
|
|
154
149
|
].filter(Boolean);
|
|
155
150
|
}
|
|
156
151
|
|
|
@@ -158,7 +153,7 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
158
153
|
function detailRow(config, view, usage) {
|
|
159
154
|
const tiers = (view.tiers ?? []).slice(-STRIP);
|
|
160
155
|
if (!tiers.length) return [part('no replies yet', { dimColor: true })];
|
|
161
|
-
const switches =
|
|
156
|
+
const switches = switchCount(tiers);
|
|
162
157
|
const metrics = usageMetrics(config, view, usage);
|
|
163
158
|
return [
|
|
164
159
|
part('replies ', { dimColor: true }),
|
|
@@ -216,8 +211,7 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
|
|
|
216
211
|
}
|
|
217
212
|
|
|
218
213
|
export function switchToast(config, previous, next, estimate) {
|
|
219
|
-
const label = (decision) =>
|
|
220
|
-
`${modelName(decision.model)}${decision.effort === null || decision.effort === undefined ? '' : ` · ${decision.effort}`}`;
|
|
214
|
+
const label = (decision) => routeLabel(decision.model, decision.effort);
|
|
221
215
|
const support = supportText(config, estimate);
|
|
222
216
|
return `${label(previous)} → ${label(next)} — ${SHORT_REASONS[next.reason]?.replace(/^\W+\s*/, '') ?? next.reason}${support ? ` (${support})` : ''}`;
|
|
223
217
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { activeClassifier } from './config.mjs';
|
|
2
1
|
import {
|
|
3
2
|
buildRequest,
|
|
3
|
+
FAILURES_TO_PAUSE,
|
|
4
4
|
isTransientStatus,
|
|
5
|
-
|
|
6
|
-
JEV_PAUSE_MS,
|
|
5
|
+
PAUSE_MS,
|
|
7
6
|
parseAnswers,
|
|
8
7
|
RETRY_DELAY_MS,
|
|
9
8
|
retryDelayMs,
|
|
10
|
-
} from './
|
|
9
|
+
} from './classifier-contract.mjs';
|
|
10
|
+
import { activeClassifier } from './config.mjs';
|
|
11
11
|
|
|
12
12
|
class AdviceError extends Error {
|
|
13
13
|
constructor(code) {
|
|
@@ -17,7 +17,7 @@ class AdviceError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// Owned by one Mod activation. It survives history resets; the host cancels its work on unload.
|
|
20
|
-
export class
|
|
20
|
+
export class ClassifierClient {
|
|
21
21
|
constructor({ now = Date.now, health = null } = {}) {
|
|
22
22
|
this.now = now;
|
|
23
23
|
this.pending = null;
|
|
@@ -27,7 +27,7 @@ export class NativeJev {
|
|
|
27
27
|
|
|
28
28
|
restore(health) {
|
|
29
29
|
this.failures =
|
|
30
|
-
Number.isInteger(health?.failures) && health.failures >= 0 ? Math.min(health.failures,
|
|
30
|
+
Number.isInteger(health?.failures) && health.failures >= 0 ? Math.min(health.failures, FAILURES_TO_PAUSE) : 0;
|
|
31
31
|
this.pausedUntil = Number.isFinite(health?.pausedUntil) ? health.pausedUntil : 0;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -50,8 +50,8 @@ export class NativeJev {
|
|
|
50
50
|
} catch (error) {
|
|
51
51
|
const code = signal?.aborted ? 'cancelled' : error instanceof AdviceError ? error.code : 'unreachable';
|
|
52
52
|
if (code !== 'cancelled' && code !== 'policy') {
|
|
53
|
-
this.failures = Math.min(this.failures + 1,
|
|
54
|
-
if (this.failures >=
|
|
53
|
+
this.failures = Math.min(this.failures + 1, FAILURES_TO_PAUSE);
|
|
54
|
+
if (this.failures >= FAILURES_TO_PAUSE) this.pausedUntil = this.now() + PAUSE_MS;
|
|
55
55
|
}
|
|
56
56
|
return { advice: null, error: code };
|
|
57
57
|
} finally {
|
|
@@ -34,8 +34,8 @@ const ROUTE_INSTRUCTIONS = {
|
|
|
34
34
|
const CONTINUATION_INSTRUCTIONS =
|
|
35
35
|
'Is `currentRequest.text` a continuation of the task in `recentDialogue` (for example "continue", "yes", "now fix the tests"), rather than a new task?';
|
|
36
36
|
export const RETRY_DELAY_MS = 100;
|
|
37
|
-
export const
|
|
38
|
-
export const
|
|
37
|
+
export const FAILURES_TO_PAUSE = 3;
|
|
38
|
+
export const PAUSE_MS = 60_000;
|
|
39
39
|
const TRANSIENT = new Set([408, 429, 500, 502, 503, 504]);
|
|
40
40
|
|
|
41
41
|
export function isTransientStatus(status) {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// router.json edits the pane writes: the rewrite, its validation and what Undo restores. Pure: the hook reads and
|
|
2
|
+
// writes the file.
|
|
3
|
+
import { loadConfig } from './config.mjs';
|
|
4
|
+
|
|
5
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
6
|
+
|
|
7
|
+
// The router.json text after `change(previousFile)` and the config it loads, from the file's text or null when there
|
|
8
|
+
// is none. Throws when the old text is not JSON or the result does not validate.
|
|
9
|
+
export function rewrittenConfig(text, change) {
|
|
10
|
+
const file = change(text === null ? {} : JSON.parse(text));
|
|
11
|
+
return { config: loadConfig({ userFile: file }), text: `${JSON.stringify(file, null, 2)}\n` };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// The pane line for a write that failed.
|
|
15
|
+
export function notSaved(error) {
|
|
16
|
+
// A JSON parse message quotes the file; the loadConfig messages name the setting only.
|
|
17
|
+
const reason = error instanceof SyntaxError ? 'router.json is not valid JSON' : error.message;
|
|
18
|
+
return `Not saved: ${reason}. router.json is unchanged.`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The leaves where router.json `after` differs from `before`, each with its value in `before`, or no `value` where it
|
|
22
|
+
// had none. An object present on one side only is walked as empty on the other, so a sibling stays out of it.
|
|
23
|
+
export function changedLeaves(before, after, path = []) {
|
|
24
|
+
const out = [];
|
|
25
|
+
for (const key of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
26
|
+
const [a, b] = [before[key], after[key]];
|
|
27
|
+
if ((isRecord(a) || a === undefined) && (isRecord(b) || b === undefined) && (isRecord(a) || isRecord(b)))
|
|
28
|
+
out.push(...changedLeaves(a ?? {}, b ?? {}, [...path, key]));
|
|
29
|
+
else if (JSON.stringify(a) !== JSON.stringify(b))
|
|
30
|
+
out.push(Object.hasOwn(before, key) ? { path: [...path, key], value: a } : { path: [...path, key] });
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The router.json `file` with each leaf of one pane write put back as it was before it: a value written again
|
|
36
|
+
// verbatim, a leaf that was absent deleted along with any object that leaves empty. Leaves the write did not touch,
|
|
37
|
+
// such as an edit made on disk since, stay as they are.
|
|
38
|
+
export function restored(file, leaves) {
|
|
39
|
+
const next = structuredClone(file);
|
|
40
|
+
for (const { path, ...leaf } of leaves) {
|
|
41
|
+
const parents = path.slice(0, -1);
|
|
42
|
+
const key = path.at(-1);
|
|
43
|
+
if (Object.hasOwn(leaf, 'value')) {
|
|
44
|
+
let node = next;
|
|
45
|
+
for (const name of parents) {
|
|
46
|
+
if (!isRecord(node[name])) node[name] = {};
|
|
47
|
+
node = node[name];
|
|
48
|
+
}
|
|
49
|
+
node[key] = leaf.value;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const chain = [next];
|
|
53
|
+
for (const name of parents) {
|
|
54
|
+
if (!isRecord(chain.at(-1)[name])) break;
|
|
55
|
+
chain.push(chain.at(-1)[name]);
|
|
56
|
+
}
|
|
57
|
+
if (chain.length !== path.length) continue;
|
|
58
|
+
delete chain.at(-1)[key];
|
|
59
|
+
for (let i = chain.length - 1; i > 0 && !Object.keys(chain[i]).length; i -= 1) delete chain[i - 1][path[i - 1]];
|
|
60
|
+
}
|
|
61
|
+
return next;
|
|
62
|
+
}
|
package/lib/config.mjs
CHANGED
|
@@ -6,15 +6,17 @@ export const DEFAULTS = {
|
|
|
6
6
|
baselineTier: 'low',
|
|
7
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
8
|
// of speed and cost per task; `high` is Opus 5.5 at xhigh, the strongest setting this router asks for.
|
|
9
|
+
// `micro` is Haiku 5.5 at low effort: Claude Code owns the thinking field, so low effort is the least thinking the router can ask for.
|
|
9
10
|
// These are routing defaults, not a claim of equal model quality.
|
|
10
11
|
routes: {
|
|
11
12
|
high: { model: 'opus', effort: 'xhigh' },
|
|
12
13
|
medium: { model: 'opus', effort: 'medium' },
|
|
13
14
|
low: { model: 'sonnet' },
|
|
14
|
-
micro: { model: 'haiku' },
|
|
15
|
+
micro: { model: 'haiku', effort: 'low' },
|
|
15
16
|
},
|
|
16
17
|
// `id` selects the native model. List prices in USD per million tokens; `cacheRead` is absolute,
|
|
17
|
-
// not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x).
|
|
18
|
+
// not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). Haiku 5.5 lists the rates for
|
|
19
|
+
// prompts up to 100k tokens; above that Anthropic charges 5x on input, output and cache read, so estimates run low there. A price change must also change
|
|
18
20
|
// test/fixtures/list-prices.json, with its source and date. `output` feeds the shadow estimate and the downgrade tax.
|
|
19
21
|
// `efforts` lists what the model accepts; an empty list means no effort field and no adaptive thinking. A change
|
|
20
22
|
// must also change test/fixtures/effort-support.json, from a new probe against the real API.
|
|
@@ -38,13 +40,13 @@ export const DEFAULTS = {
|
|
|
38
40
|
efforts: EFFORTS,
|
|
39
41
|
},
|
|
40
42
|
haiku: {
|
|
41
|
-
id: 'claude-haiku-
|
|
42
|
-
input: 1,
|
|
43
|
-
output: 5,
|
|
44
|
-
cacheRead: 0.
|
|
45
|
-
contextWindow:
|
|
43
|
+
id: 'claude-haiku-5-5',
|
|
44
|
+
input: 0.1,
|
|
45
|
+
output: 0.5,
|
|
46
|
+
cacheRead: 0.01,
|
|
47
|
+
contextWindow: 1_000_000,
|
|
46
48
|
billing: 'plan',
|
|
47
|
-
efforts:
|
|
49
|
+
efforts: EFFORTS,
|
|
48
50
|
},
|
|
49
51
|
},
|
|
50
52
|
cache: {
|
|
@@ -232,6 +234,17 @@ export function rank(tier) {
|
|
|
232
234
|
return TIERS.indexOf(tier);
|
|
233
235
|
}
|
|
234
236
|
|
|
237
|
+
// The model entry a tier routes to.
|
|
238
|
+
export const routeModel = (config, tier) => config.models[config.routes[tier].model];
|
|
239
|
+
|
|
240
|
+
// Whether a Claude Code version, such as 2.1.289 or a 2.1.290-beta build, runs the router: 2.1.289 or newer.
|
|
241
|
+
export function supportedVersion(version) {
|
|
242
|
+
const parts = /^(\d+)\.(\d+)\.(\d+)(?:$|-)/.exec(version ?? '');
|
|
243
|
+
if (!parts) return false;
|
|
244
|
+
const [major, minor, patch] = parts.slice(1).map(Number);
|
|
245
|
+
return major > 2 || (major === 2 && (minor > 1 || (minor === 1 && patch >= 289)));
|
|
246
|
+
}
|
|
247
|
+
|
|
235
248
|
function merge(base, override) {
|
|
236
249
|
if (Array.isArray(base) || typeof base !== 'object' || base === null) return override;
|
|
237
250
|
const out = { ...base };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EFFORTS } from './config.mjs';
|
|
1
|
+
import { EFFORTS, routeModel } from './config.mjs';
|
|
2
2
|
|
|
3
3
|
export function cacheKey(modelId, effort) {
|
|
4
4
|
return effort ? `${modelId}@${effort}` : modelId;
|
|
@@ -18,7 +18,7 @@ export function nextContextTokens(facts) {
|
|
|
18
18
|
|
|
19
19
|
export function routeEffort(config, tier, sentEffort) {
|
|
20
20
|
const route = config.routes[tier];
|
|
21
|
-
const model = config
|
|
21
|
+
const model = routeModel(config, tier);
|
|
22
22
|
if (model.efforts.length === 0) return null;
|
|
23
23
|
if (route.effort) return clampEffort(route.effort, model.efforts);
|
|
24
24
|
if (typeof sentEffort === 'number' && Number.isFinite(sentEffort)) return sentEffort;
|
|
@@ -26,7 +26,7 @@ export function routeEffort(config, tier, sentEffort) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function routeCacheKey(config, tier, sentEffort) {
|
|
29
|
-
return cacheKey(config
|
|
29
|
+
return cacheKey(routeModel(config, tier).id, routeEffort(config, tier, sentEffort));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export function isWarm(state, now, cache) {
|
|
@@ -41,7 +41,7 @@ export function cacheState(state, now, cache) {
|
|
|
41
41
|
|
|
42
42
|
// Bounds are scenarios, never an independent ledger. Unknown incumbent cache can still be served.
|
|
43
43
|
export function inputBounds(config, tier, tokens, facts, now) {
|
|
44
|
-
const model = config
|
|
44
|
+
const model = routeModel(config, tier);
|
|
45
45
|
const state = facts.models[routeCacheKey(config, tier, facts.effort)];
|
|
46
46
|
const prefix = Math.min(state?.prefixTokens ?? tokens, tokens);
|
|
47
47
|
const fresh = isWarm(state, now, config.cache);
|
|
@@ -64,8 +64,8 @@ export function switchingTaxUsd(config, candidate, incumbent, facts, now) {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export function shadowEconomics(config, candidate, incumbent, facts, now) {
|
|
67
|
-
const proposed = config
|
|
68
|
-
const current = config
|
|
67
|
+
const proposed = routeModel(config, candidate);
|
|
68
|
+
const current = routeModel(config, incumbent);
|
|
69
69
|
if (!(proposed.output > 0) || !(current.output > 0)) return null;
|
|
70
70
|
const tokens = nextContextTokens(facts);
|
|
71
71
|
const outputTokens = facts.lastRequest?.outputTokens ?? 0;
|
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
import { activeClassifier, CLASSIFIER_OPTIONS, endpointSettings } from './config.mjs';
|
|
2
|
-
import { tierForModel } from './
|
|
1
|
+
import { activeClassifier, CLASSIFIER_OPTIONS, endpointSettings, routeModel } from './config.mjs';
|
|
2
|
+
import { tierForModel } from './route.mjs';
|
|
3
|
+
|
|
4
|
+
// Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
|
|
5
|
+
export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
|
|
6
|
+
|
|
7
|
+
export function modelName(id) {
|
|
8
|
+
if (!id) return 'no response yet';
|
|
9
|
+
return id
|
|
10
|
+
.replace(/^claude-/, '')
|
|
11
|
+
.replace(/-([0-9])-([0-9])(-\d{8})?$/, ' $1.$2')
|
|
12
|
+
.replace(/^./, (c) => c.toUpperCase());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// "Opus 5.5 · high"; a model with no effort reading shows its name alone.
|
|
16
|
+
export const routeLabel = (model, effort) =>
|
|
17
|
+
`${modelName(model)}${effort === null || effort === undefined ? '' : ` · ${effort}`}`;
|
|
18
|
+
|
|
19
|
+
// Changes of tier between consecutive replies; a reply routing did not choose breaks no run.
|
|
20
|
+
export const switchCount = (tiers) => tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
|
|
21
|
+
|
|
22
|
+
export const percent = (value) => `${Math.round(value * 100)}%`;
|
|
3
23
|
|
|
4
24
|
export const REASONS = {
|
|
5
25
|
ready: 'ready for the next turn',
|
|
@@ -51,20 +71,28 @@ export function classifierStatus(config, error) {
|
|
|
51
71
|
const label = activeClassifier(config).label;
|
|
52
72
|
return error === 'missing-key' || error === 'missing-account'
|
|
53
73
|
? `${label}: ${missingText(config, config.classifier, error)}`
|
|
54
|
-
: `${label} ${CLASSIFIER_ERRORS[error] ?? error}`;
|
|
74
|
+
: `${label} ${CLASSIFIER_ERRORS[error]?.text ?? error}`;
|
|
55
75
|
}
|
|
56
76
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
77
|
+
// Every classifier error: its words after the label (missing credentials name the setting instead), whether the band
|
|
78
|
+
// warns (busy and cancelled are routine and pass silently), and whether the wait counts as a timed reading (a request
|
|
79
|
+
// that was never sent does not).
|
|
80
|
+
const CLASSIFIER_ERRORS = {
|
|
81
|
+
'missing-key': { text: null, warn: true, timed: false },
|
|
82
|
+
'missing-account': { text: null, warn: true, timed: false },
|
|
83
|
+
paused: { text: 'paused after failures', warn: true, timed: false },
|
|
84
|
+
busy: { text: 'busy', warn: false, timed: false },
|
|
85
|
+
timeout: { text: 'timed out', warn: true, timed: true },
|
|
86
|
+
auth: { text: 'rejected the key', warn: true, timed: true },
|
|
87
|
+
http: { text: 'request failed', warn: true, timed: true },
|
|
88
|
+
unreachable: { text: 'unreachable', warn: true, timed: true },
|
|
89
|
+
policy: { text: 'blocked by network policy', warn: true, timed: true },
|
|
90
|
+
malformed: { text: 'sent a bad answer', warn: true, timed: true },
|
|
91
|
+
cancelled: { text: 'cancelled', warn: false, timed: true },
|
|
67
92
|
};
|
|
93
|
+
export const classifierWarns = (error) => CLASSIFIER_ERRORS[error]?.warn === true;
|
|
94
|
+
// No error, or one outside the table, times the wait.
|
|
95
|
+
export const classifierTimed = (error) => CLASSIFIER_ERRORS[error]?.timed !== false;
|
|
68
96
|
// One or two words for the band; REASONS holds the sentence the pane shows.
|
|
69
97
|
export const SHORT_REASONS = {
|
|
70
98
|
ready: 'ready',
|
|
@@ -116,7 +144,7 @@ export function bar(value, maximum, width = 16) {
|
|
|
116
144
|
export function usageMetrics(config, view, usage) {
|
|
117
145
|
const model = view.actualModel ?? view.selectedModel ?? view.nativeModel;
|
|
118
146
|
const tier = tierForModel(config, model);
|
|
119
|
-
const spec = tier ? config
|
|
147
|
+
const spec = tier ? routeModel(config, tier) : null;
|
|
120
148
|
const window = spec?.contextWindow ?? null;
|
|
121
149
|
const observed = Number.isFinite(usage?.context?.tokens) ? usage.context.tokens : null;
|
|
122
150
|
const candidates = [view.contextTokens, observed === null ? null : observed + (view.outputTokens ?? 0)].filter(
|
|
@@ -9,37 +9,33 @@ import {
|
|
|
9
9
|
GATEWAY_SETTINGS,
|
|
10
10
|
missingCredentials,
|
|
11
11
|
missingText,
|
|
12
|
+
modelName,
|
|
13
|
+
percent,
|
|
12
14
|
REASONS,
|
|
15
|
+
routeLabel,
|
|
13
16
|
sparkline,
|
|
17
|
+
switchCount,
|
|
18
|
+
TIER_COLOR,
|
|
14
19
|
usageMetrics,
|
|
15
|
-
} from './
|
|
20
|
+
} from './display.mjs';
|
|
21
|
+
import { isSameModel } from './route.mjs';
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
const TABS = [
|
|
18
24
|
['now', '1', 'Now'],
|
|
19
25
|
['routing', '2', 'Routing'],
|
|
20
26
|
['classifier', '3', 'Classifier'],
|
|
21
27
|
['usage', '4', 'Usage'],
|
|
22
28
|
];
|
|
23
|
-
|
|
29
|
+
const TUNING_CHOICES = {
|
|
24
30
|
timeoutMs: [500, 1000, 1500, 3000],
|
|
25
31
|
downgradeVotes: [1, 2, 3],
|
|
26
32
|
horizon: [1, 3, 5, 10],
|
|
27
33
|
cashCapUsd: [0.5, 1, 2, 5],
|
|
28
34
|
};
|
|
29
|
-
// Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
|
|
30
|
-
export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
|
|
31
35
|
const LADDER = [...TIERS].reverse();
|
|
32
36
|
const SESSION_EFFORT = 'session';
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
if (!id) return 'no response yet';
|
|
36
|
-
return id
|
|
37
|
-
.replace(/^claude-/, '')
|
|
38
|
-
.replace(/-([0-9])-([0-9])(-\d{8})?$/, ' $1.$2')
|
|
39
|
-
.replace(/^./, (c) => c.toUpperCase());
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function routeName(config, route) {
|
|
38
|
+
function routeName(config, route) {
|
|
43
39
|
const model = config.models[route.model];
|
|
44
40
|
return `${modelName(model.id)}${model.efforts.length ? ` · ${route.effort ?? SESSION_EFFORT}` : ''}`;
|
|
45
41
|
}
|
|
@@ -53,7 +49,6 @@ export function routeDraftOf(config, view) {
|
|
|
53
49
|
|
|
54
50
|
const dollars = (value) => (Number.isFinite(value) ? `$${value.toFixed(3)}` : 'not reported');
|
|
55
51
|
const difference = (value) => `${value < 0 ? '−' : '+'}$${Math.abs(value).toFixed(3)}`;
|
|
56
|
-
const percent = (value) => `${Math.round(value * 100)}%`;
|
|
57
52
|
const hostOf = (endpoint) => endpoint.replace(/^https?:\/\//, '').split('/')[0];
|
|
58
53
|
const windowSize = (tokens) => (tokens >= 1e6 ? `${tokens / 1e6}M` : `${Math.round(tokens / 1e3)}K`);
|
|
59
54
|
const contextSuffix = (metrics) =>
|
|
@@ -167,14 +162,9 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
167
162
|
const current = view.selectedModel ?? view.nativeModel;
|
|
168
163
|
const label = classifierLabel(config);
|
|
169
164
|
const missing = missingCredentials(view, config.classifier);
|
|
170
|
-
const served =
|
|
171
|
-
view.actualModel && current && view.actualModel !== current && !view.actualModel.startsWith(`${current}-`);
|
|
165
|
+
const served = view.actualModel && current && !isSameModel(current, view.actualModel);
|
|
172
166
|
const out = [
|
|
173
|
-
ui.line(
|
|
174
|
-
'Now ',
|
|
175
|
-
view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null,
|
|
176
|
-
`${modelName(current)}${view.effort === null || view.effort === undefined ? '' : ` · ${view.effort}`}`,
|
|
177
|
-
),
|
|
167
|
+
ui.line('Now ', view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null, routeLabel(current, view.effort)),
|
|
178
168
|
ui.line('Why ', REASONS[view.reason] ?? view.reason ?? 'ready'),
|
|
179
169
|
served
|
|
180
170
|
? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
|
|
@@ -247,7 +237,7 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
247
237
|
function replies(ui, view) {
|
|
248
238
|
const tiers = view.tiers ?? [];
|
|
249
239
|
if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
|
|
250
|
-
const switches =
|
|
240
|
+
const switches = switchCount(tiers);
|
|
251
241
|
return [
|
|
252
242
|
ui.head(`REPLIES · last ${tiers.length}`),
|
|
253
243
|
ui.line(
|
|
@@ -259,6 +249,8 @@ function replies(ui, view) {
|
|
|
259
249
|
];
|
|
260
250
|
}
|
|
261
251
|
|
|
252
|
+
// Rows: [key, label, format, hint, router.json field]. Typed as tuples so tsc keeps each position's type.
|
|
253
|
+
/** @type {[string, string, (value: number) => string, string, string][]} */
|
|
262
254
|
const POLICY_ROWS = [
|
|
263
255
|
['downgradeVotes', 'Votes to go down', String, 'agreeing turns before cheaper', 'policy.downgradeVotes'],
|
|
264
256
|
['horizon', 'Payback horizon', (v) => `${v} turns`, 'a switch repays its cache', 'policy.downgradeHorizonTurns'],
|
package/lib/policy.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Switching policy v0 (docs/architecture.md): stickiness, escalation floor, cost-gated votes.
|
|
2
|
-
import { rank, TIERS } from './config.mjs';
|
|
3
|
-
import * as nativeCosts from './
|
|
2
|
+
import { rank, routeModel, TIERS } from './config.mjs';
|
|
3
|
+
import * as nativeCosts from './cost.mjs';
|
|
4
4
|
|
|
5
5
|
export function initialState() {
|
|
6
6
|
return { turn: 0, votes: [], holdUntilTurn: 0, escalatedSignature: null };
|
|
@@ -65,12 +65,12 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
|
|
|
65
65
|
|
|
66
66
|
// Share of a model's window the next request may fill. The estimate leaves out the new prompt and tool
|
|
67
67
|
// results, so a model drops out before the conversation reaches its limit.
|
|
68
|
-
const CONTEXT_FILL = 0.8;
|
|
68
|
+
export const CONTEXT_FILL = 0.8;
|
|
69
69
|
|
|
70
70
|
// The lowest tier at or above `tier` whose model fits `tokens` of context. Routes need not grow in window
|
|
71
71
|
// with rank, so the search falls back to any tier that fits, then to the largest window, keeping `tier` on a tie.
|
|
72
72
|
export function fitTier(config, tier, tokens) {
|
|
73
|
-
const windowOf = (t) => config
|
|
73
|
+
const windowOf = (t) => routeModel(config, t).contextWindow;
|
|
74
74
|
const fits = (t) => tokens <= windowOf(t) * CONTEXT_FILL;
|
|
75
75
|
if (fits(tier)) return tier;
|
|
76
76
|
return (
|
|
@@ -88,9 +88,9 @@ function cashGate(config, tier, facts, now, costs) {
|
|
|
88
88
|
if (config.models[alias].billing !== 'credits') return { blocked: false };
|
|
89
89
|
if (costs.isWarm(facts.models[costs.routeCacheKey(config, tier, facts.effort)], now, config.cache))
|
|
90
90
|
return { blocked: false };
|
|
91
|
-
const cold = costs.coldWriteUsd(config, alias, costs.nextContextTokens(facts)
|
|
91
|
+
const cold = costs.coldWriteUsd(config, alias, costs.nextContextTokens(facts));
|
|
92
92
|
if (cold <= config.policy.cashCapUsd) return { blocked: false };
|
|
93
|
-
const fallback = [...TIERS].reverse().find((t) => config
|
|
93
|
+
const fallback = [...TIERS].reverse().find((t) => routeModel(config, t).billing === 'plan');
|
|
94
94
|
const estimate = { coldUsd: cold, cap: config.policy.cashCapUsd, cache: cacheOf(config, tier, facts, now, costs) };
|
|
95
95
|
return { blocked: true, fallback, estimate };
|
|
96
96
|
}
|