@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,30 @@
|
|
|
1
|
-
import { TIERS } from './config.mjs';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { decide, fitTier, initialState } from './policy.mjs';
|
|
1
|
+
import { routeModel, TIERS } from './config.mjs';
|
|
2
|
+
import * as costs from './cost.mjs';
|
|
3
|
+
import { clip, extractFacts } from './facts.mjs';
|
|
4
|
+
import { CONTEXT_FILL, decide, fitTier, initialState } from './policy.mjs';
|
|
5
5
|
|
|
6
6
|
// Exact observed API snapshot, not a rule that merges different dated versions.
|
|
7
|
-
|
|
7
|
+
const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
|
|
8
|
+
|
|
9
|
+
// A request this much smaller than the last means the history was compacted or rewound: the cached prefixes the
|
|
10
|
+
// observations describe are gone, so they are dropped rather than priced as warm.
|
|
11
|
+
const HISTORY_SHRINK = 0.8;
|
|
8
12
|
|
|
9
13
|
// Whether the engine served the requested model, allowing for a dated snapshot of it.
|
|
10
14
|
export function isSameModel(requested, served) {
|
|
11
15
|
return requested === served || SNAPSHOTS[requested] === served;
|
|
12
16
|
}
|
|
13
17
|
|
|
18
|
+
// The engine may echo our own routed model on a continuation; any third model is its fallback.
|
|
19
|
+
export function isNativeFallback(loop, model) {
|
|
20
|
+
if (loop.suspended) return true;
|
|
21
|
+
const known = [loop.engineModel, loop.decision?.model].filter(Boolean);
|
|
22
|
+
return known.length > 0 && !known.some((id) => isSameModel(id, model));
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
export function tierForModel(config, id) {
|
|
15
26
|
const order = [config.baselineTier, ...TIERS.filter((tier) => tier !== config.baselineTier)];
|
|
16
|
-
return (
|
|
17
|
-
order.find((tier) => {
|
|
18
|
-
const model = config.models[config.routes[tier].model];
|
|
19
|
-
return isSameModel(model.id, id);
|
|
20
|
-
}) ?? null
|
|
21
|
-
);
|
|
27
|
+
return order.find((tier) => isSameModel(routeModel(config, tier).id, id)) ?? null;
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
export function emptyLoop(config, model) {
|
|
@@ -95,35 +101,29 @@ export function chooseRoute(config, loop, { facts, advice, pin, nativeModel, con
|
|
|
95
101
|
now,
|
|
96
102
|
costs,
|
|
97
103
|
});
|
|
104
|
+
// Without a reliable reading of the context, a tier with a smaller window than the last route's might overflow.
|
|
105
|
+
const shrinksUnmeasured = (tier) =>
|
|
106
|
+
(!contextKnown || !loop.historyMeasured) &&
|
|
107
|
+
routeModel(config, tier).contextWindow < routeModel(config, loop.lastRoute).contextWindow;
|
|
98
108
|
const fits = (tier) => {
|
|
99
|
-
const model = config
|
|
100
|
-
if (
|
|
101
|
-
(!contextKnown || !loop.historyMeasured) &&
|
|
102
|
-
model.contextWindow < config.models[config.routes[loop.lastRoute].model].contextWindow
|
|
103
|
-
)
|
|
104
|
-
return false;
|
|
109
|
+
const model = routeModel(config, tier);
|
|
105
110
|
return (
|
|
111
|
+
!shrinksUnmeasured(tier) &&
|
|
106
112
|
isModelAllowed(model.id, availableModels, nativeModel) &&
|
|
107
113
|
!loop.ineligible.includes(model.id) &&
|
|
108
|
-
(!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow *
|
|
114
|
+
(!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * CONTEXT_FILL)
|
|
109
115
|
);
|
|
110
116
|
};
|
|
111
117
|
if (contextKnown) {
|
|
112
118
|
const fitted = fitTier(config, result.tier, costs.nextContextTokens(facts));
|
|
113
119
|
if (fitted !== result.tier) result = { ...result, tier: fitted, reason: 'context-fit' };
|
|
114
120
|
}
|
|
115
|
-
if (
|
|
116
|
-
(!contextKnown || !loop.historyMeasured) &&
|
|
117
|
-
config.models[config.routes[result.tier].model].contextWindow <
|
|
118
|
-
config.models[config.routes[loop.lastRoute].model].contextWindow
|
|
119
|
-
) {
|
|
120
|
-
result = { ...result, tier: loop.lastRoute, reason: 'context-unknown' };
|
|
121
|
-
}
|
|
121
|
+
if (shrinksUnmeasured(result.tier)) result = { ...result, tier: loop.lastRoute, reason: 'context-unknown' };
|
|
122
122
|
if (!fits(result.tier)) {
|
|
123
123
|
const fallback = [loop.lastRoute, tierForModel(config, nativeModel), ...TIERS].find((tier) => tier && fits(tier));
|
|
124
124
|
result = { ...result, tier: fallback ?? null, reason: 'model-unavailable' };
|
|
125
125
|
}
|
|
126
|
-
const model = result.tier ? config
|
|
126
|
+
const model = result.tier ? routeModel(config, result.tier).id : nativeModel;
|
|
127
127
|
const effort = result.tier ? costs.routeEffort(config, result.tier, facts.effort) : facts.effort;
|
|
128
128
|
let comparison = null;
|
|
129
129
|
const candidate = pin ?? advice?.choice;
|
|
@@ -134,8 +134,7 @@ export function chooseRoute(config, loop, { facts, advice, pin, nativeModel, con
|
|
|
134
134
|
const forecast = costs.shadowEconomics(projected, candidate, loop.lastRoute, facts, now);
|
|
135
135
|
// Without both output prices, compare input only, as shadowEconomics does.
|
|
136
136
|
const output = forecast
|
|
137
|
-
? ((projected.
|
|
138
|
-
projected.models[projected.routes[loop.lastRoute].model].output) *
|
|
137
|
+
? ((routeModel(projected, candidate).output - routeModel(projected, loop.lastRoute).output) *
|
|
139
138
|
loop.lastRequest.outputTokens) /
|
|
140
139
|
1e6
|
|
141
140
|
: 0;
|
|
@@ -168,7 +167,7 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
|
|
|
168
167
|
decision = {
|
|
169
168
|
...decision,
|
|
170
169
|
tier: fitted,
|
|
171
|
-
model: config
|
|
170
|
+
model: routeModel(config, fitted).id,
|
|
172
171
|
effort: costs.routeEffort(config, fitted, effort),
|
|
173
172
|
reason: 'context-fit',
|
|
174
173
|
};
|
|
@@ -179,7 +178,7 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
|
|
|
179
178
|
return { ...loop, decision };
|
|
180
179
|
}
|
|
181
180
|
|
|
182
|
-
export function observeResponse(
|
|
181
|
+
export function observeResponse(loop, { usage, requestedModel, effort, stopReason, now }) {
|
|
183
182
|
let next = loop;
|
|
184
183
|
if (stopReason === 'model_context_window_exceeded') {
|
|
185
184
|
next = { ...next, ineligible: [...new Set([...next.ineligible, requestedModel])] };
|
|
@@ -194,7 +193,7 @@ export function observeResponse(_config, loop, { usage, requestedModel, effort,
|
|
|
194
193
|
const resolutions = sameModel ? { ...next.resolutions, [requestedModel]: usage.model } : { ...next.resolutions };
|
|
195
194
|
if (!sameModel) delete resolutions[requestedModel];
|
|
196
195
|
let models = next.models;
|
|
197
|
-
if (next.lastRequest && tokens < next.lastRequest.tokens *
|
|
196
|
+
if (next.lastRequest && tokens < next.lastRequest.tokens * HISTORY_SHRINK) models = {};
|
|
198
197
|
// A substituted model's effort is not reported, so do not credit its estimated cache.
|
|
199
198
|
if (sameModel)
|
|
200
199
|
models = {
|
package/lib/view.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The view the band and pane draw from: its initial shape and the patches built from pure inputs. Pure: the hook
|
|
2
|
+
// writes the view to $.state.
|
|
3
|
+
|
|
4
|
+
// Replies kept for the pane's trend and tier strip.
|
|
5
|
+
const HISTORY = 30;
|
|
6
|
+
|
|
7
|
+
export function initialView(model) {
|
|
8
|
+
return {
|
|
9
|
+
phase: 'ready',
|
|
10
|
+
activeTurnId: null,
|
|
11
|
+
mode: 'auto',
|
|
12
|
+
nativeModel: model,
|
|
13
|
+
selectedModel: null,
|
|
14
|
+
actualModel: null,
|
|
15
|
+
tier: null,
|
|
16
|
+
effort: null,
|
|
17
|
+
reason: 'ready',
|
|
18
|
+
error: null,
|
|
19
|
+
pendingPin: null,
|
|
20
|
+
credentials: null,
|
|
21
|
+
contextTokens: null,
|
|
22
|
+
contextKnown: false,
|
|
23
|
+
cacheRead: null,
|
|
24
|
+
cacheWrite: null,
|
|
25
|
+
health: { failures: 0, pausedUntil: 0 },
|
|
26
|
+
inputTokens: null,
|
|
27
|
+
outputTokens: null,
|
|
28
|
+
adviceMs: null,
|
|
29
|
+
adviceChoice: null,
|
|
30
|
+
estimate: null,
|
|
31
|
+
comparison: null,
|
|
32
|
+
probabilities: null,
|
|
33
|
+
history: [],
|
|
34
|
+
tiers: [],
|
|
35
|
+
configPath: null,
|
|
36
|
+
tuning: null,
|
|
37
|
+
tuningBase: null,
|
|
38
|
+
routeDraft: null,
|
|
39
|
+
lastWrite: null,
|
|
40
|
+
tab: 'now',
|
|
41
|
+
help: false,
|
|
42
|
+
notice: null,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Failures and a pause belong to the classifier that earned them.
|
|
47
|
+
export const healthOf = (client, id) => ({ ...client.snapshot(), classifier: id });
|
|
48
|
+
// The last classification's readings, cleared when another classifier takes over so no label claims them.
|
|
49
|
+
export const CLEARED_READINGS = { adviceMs: null, adviceChoice: null, probabilities: null, estimate: null };
|
|
50
|
+
|
|
51
|
+
// The view patch for a main reply's usage. `tier` is the routed tier that served it, else null.
|
|
52
|
+
export function responseMetrics(view, response, tier) {
|
|
53
|
+
const usage = response.usage;
|
|
54
|
+
const counters = usage ? [usage.input_tokens, usage.cache_read_input_tokens, usage.cache_creation_input_tokens] : [];
|
|
55
|
+
const inputTokens =
|
|
56
|
+
counters.length && counters.every(Number.isFinite) ? counters.reduce((sum, n) => sum + n, 0) : null;
|
|
57
|
+
return {
|
|
58
|
+
actualModel: usage?.model ?? null,
|
|
59
|
+
cacheRead: usage?.cache_read_input_tokens ?? null,
|
|
60
|
+
cacheWrite: usage?.cache_creation_input_tokens ?? null,
|
|
61
|
+
inputTokens,
|
|
62
|
+
outputTokens: usage?.output_tokens ?? null,
|
|
63
|
+
// Appended together so the Usage trend can color each reading by its tier.
|
|
64
|
+
...(Number.isFinite(inputTokens)
|
|
65
|
+
? {
|
|
66
|
+
history: [...(view?.history ?? []), inputTokens].slice(-HISTORY),
|
|
67
|
+
tiers: [...(view?.tiers ?? []), tier ?? null].slice(-HISTORY),
|
|
68
|
+
}
|
|
69
|
+
: {}),
|
|
70
|
+
};
|
|
71
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Native Claude Code Mod: model and effort routing for the main conversation, advised by Jev, Clef or Clef Flash.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|
|
@@ -28,8 +28,9 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"test": "node --test --test-timeout=8000 test/*.test.mjs",
|
|
32
|
-
"check": "biome check lib scripts test hooks types .claude-plugin package.json package-lock.json biome.json experiments/mod-router",
|
|
31
|
+
"test": "node --test --test-timeout=8000 test/*.test.mjs experiments/mod-router/scripts/*.test.mjs",
|
|
32
|
+
"check": "biome check lib scripts test hooks types .claude-plugin package.json package-lock.json biome.json tsconfig.json experiments/mod-router",
|
|
33
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
33
34
|
"validate": "claude plugin validate .claude-plugin/plugin.json && claude plugin validate .claude-plugin/marketplace.json",
|
|
34
35
|
"test:plugin": "bash scripts/plugin-test.sh",
|
|
35
36
|
"format": "biome format --write .",
|
|
@@ -38,6 +39,7 @@
|
|
|
38
39
|
"prepublishOnly": "npm run check && npm test"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@biomejs/biome": "2.5.14"
|
|
42
|
+
"@biomejs/biome": "2.5.14",
|
|
43
|
+
"typescript": "7.0.2"
|
|
42
44
|
}
|
|
43
45
|
}
|
|
File without changes
|