@alexeiled/claude-router 1.5.1 → 1.6.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.
- package/.claude-plugin/plugin.json +2 -2
- package/CHANGELOG.md +41 -0
- package/README.md +12 -10
- package/docs/architecture.md +59 -17
- package/docs/configuration.md +86 -32
- package/docs/evaluation.md +56 -1
- package/docs/native-router.md +38 -28
- package/docs/router-band.svg +83 -42
- package/docs/router-pane-now.svg +97 -71
- package/docs/router-pane-routing.svg +103 -32
- package/docs/user-guide.md +75 -27
- package/hooks/native-router.mjs +215 -33
- package/lib/activity-stats.mjs +245 -0
- package/lib/activity.mjs +77 -0
- package/lib/band.mjs +32 -11
- package/lib/classifier-apis.mjs +133 -69
- package/lib/classifier-client.mjs +63 -28
- package/lib/classifier-contract.mjs +48 -0
- package/lib/config.mjs +138 -4
- package/lib/cost.mjs +23 -11
- package/lib/display.mjs +100 -4
- package/lib/facts.mjs +48 -0
- package/lib/panel.mjs +323 -29
- package/lib/policy.mjs +45 -26
- package/lib/route.mjs +169 -55
- package/lib/view.mjs +35 -4
- package/package.json +1 -1
- package/types/index.d.ts +88 -1
package/lib/route.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { chooseActivity, compareRoutes, lateralMove, sameRequest } from './activity.mjs';
|
|
2
|
+
import { ACTIVITIES, rank, resolveRoute, routeSpec, TIERS } from './config.mjs';
|
|
2
3
|
import * as costs from './cost.mjs';
|
|
3
4
|
import { clip, extractFacts } from './facts.mjs';
|
|
4
5
|
import { CONTEXT_FILL, decide, fitTier, initialState } from './policy.mjs';
|
|
@@ -22,14 +23,26 @@ export function isNativeFallback(loop, model) {
|
|
|
22
23
|
return known.length > 0 && !known.some((id) => isSameModel(id, model));
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
// The tier and activity whose route runs model `id`, or null when no route names it. Base routes first, baseline
|
|
27
|
+
// first; activity overrides only when they apply, with `activityRouting: on`.
|
|
28
|
+
export function cellForModel(config, id) {
|
|
26
29
|
const order = [config.baselineTier, ...TIERS.filter((tier) => tier !== config.baselineTier)];
|
|
27
|
-
|
|
30
|
+
const runs = (tier, activity) => isSameModel(routeSpec(config, resolveRoute(config, tier, activity)).id, id);
|
|
31
|
+
const tier = order.find((t) => runs(t, null));
|
|
32
|
+
if (tier) return { tier, activity: null };
|
|
33
|
+
if (config.activityRouting !== 'on') return null;
|
|
34
|
+
for (const t of order)
|
|
35
|
+
for (const activity of ACTIVITIES)
|
|
36
|
+
if (config.activities[activity]?.[t] && runs(t, activity)) return { tier: t, activity };
|
|
37
|
+
return null;
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
export function emptyLoop(config, model) {
|
|
41
|
+
const cell = cellForModel(config, model);
|
|
31
42
|
return {
|
|
32
|
-
lastRoute:
|
|
43
|
+
lastRoute: cell?.tier ?? config.baselineTier,
|
|
44
|
+
lastActivity: cell?.activity ?? null,
|
|
45
|
+
would: null,
|
|
33
46
|
state: initialState(),
|
|
34
47
|
models: {},
|
|
35
48
|
resolutions: {},
|
|
@@ -43,6 +56,8 @@ export function emptyLoop(config, model) {
|
|
|
43
56
|
};
|
|
44
57
|
}
|
|
45
58
|
|
|
59
|
+
const freshState = (state) => ({ ...state, votes: [], holdUntilTurn: 0, escalatedSignature: null });
|
|
60
|
+
|
|
46
61
|
export function resetHistory(loop) {
|
|
47
62
|
return {
|
|
48
63
|
...loop,
|
|
@@ -50,7 +65,8 @@ export function resetHistory(loop) {
|
|
|
50
65
|
models: {},
|
|
51
66
|
resolutions: {},
|
|
52
67
|
historyMeasured: false,
|
|
53
|
-
state:
|
|
68
|
+
state: freshState(loop.state),
|
|
69
|
+
would: loop.would ? { ...loop.would, state: freshState(loop.would.state) } : null,
|
|
54
70
|
// The active turn keeps its route, pin and fallback suspension; the next turn decides afresh.
|
|
55
71
|
ineligible: [],
|
|
56
72
|
};
|
|
@@ -64,7 +80,8 @@ export function prepareLoop(loop, messageCount) {
|
|
|
64
80
|
export function nativeFacts(config, loop, { messages, prompt, effort, turnId, contextTokens }) {
|
|
65
81
|
const facts = extractFacts({ messages, output_config: { effort } }, loop, config.context);
|
|
66
82
|
if (typeof prompt === 'string') facts.prompt = clip(prompt, config.context.maxTextChars);
|
|
67
|
-
|
|
83
|
+
// A loop saved without the field counts as measured, keeping the vote streak.
|
|
84
|
+
return { ...facts, pin: null, contextTokens, turnKey: turnId, historyMeasured: loop.historyMeasured !== false };
|
|
68
85
|
}
|
|
69
86
|
|
|
70
87
|
export function isModelAllowed(id, available, nativeModel) {
|
|
@@ -87,93 +104,190 @@ function economicConfig(config, loop) {
|
|
|
87
104
|
return { ...config, models };
|
|
88
105
|
}
|
|
89
106
|
|
|
90
|
-
|
|
107
|
+
// `activityRouting` decides which activity the applied route may use: none in `off`, none in `shadow`, which runs the
|
|
108
|
+
// decision a second time as `on` for `decision.wouldRoute` only, and the chosen one in `on`. Shadow keeps its own cell
|
|
109
|
+
// and policy state in `loop.would`, so each would-route continues from the previous one, as `on` would. Its limit: the
|
|
110
|
+
// would-route never runs, so its cache is never observed and an unobserved incumbent is priced as `inputBounds`
|
|
111
|
+
// prices any unknown cache.
|
|
112
|
+
export function chooseRoute(config, loop, input) {
|
|
113
|
+
const { pin, facts } = input;
|
|
114
|
+
const applied = routeTurn(config, loop, input);
|
|
115
|
+
let wouldRoute = null;
|
|
116
|
+
let would = null;
|
|
117
|
+
if (config.activityRouting === 'shadow') {
|
|
118
|
+
const from = loop.would ?? {
|
|
119
|
+
lastRoute: loop.lastRoute,
|
|
120
|
+
lastActivity: loop.lastActivity ?? null,
|
|
121
|
+
state: loop.state,
|
|
122
|
+
};
|
|
123
|
+
const run = routeTurn(
|
|
124
|
+
{ ...config, activityRouting: 'on' },
|
|
125
|
+
{ ...loop, ...from },
|
|
126
|
+
{
|
|
127
|
+
...input,
|
|
128
|
+
facts: { ...facts, lastRoute: from.lastRoute },
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
const { activity, tier, model, effort, reason } = run;
|
|
132
|
+
// What the would-route's next request costs against the applied one's; a route priced against itself would
|
|
133
|
+
// still give a spread, so only a different request is priced.
|
|
134
|
+
const priced =
|
|
135
|
+
input.contextKnown &&
|
|
136
|
+
loop.lastRequest &&
|
|
137
|
+
run.tier &&
|
|
138
|
+
applied.tier &&
|
|
139
|
+
(run.model !== applied.model || run.effort !== applied.effort);
|
|
140
|
+
const difference = priced
|
|
141
|
+
? costs.nextTurnRange(
|
|
142
|
+
economicConfig(config, loop),
|
|
143
|
+
resolveRoute(config, run.tier, run.activity),
|
|
144
|
+
resolveRoute(config, applied.tier, applied.activity ?? null),
|
|
145
|
+
facts,
|
|
146
|
+
input.now,
|
|
147
|
+
)
|
|
148
|
+
: null;
|
|
149
|
+
wouldRoute = { activity, tier, model, effort, reason, difference };
|
|
150
|
+
const stays = pin || !run.tier;
|
|
151
|
+
would = {
|
|
152
|
+
lastRoute: stays ? from.lastRoute : run.tier,
|
|
153
|
+
lastActivity: stays ? from.lastActivity : run.activity,
|
|
154
|
+
state: run.state,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const kept = pin || !applied.tier;
|
|
158
|
+
return {
|
|
159
|
+
...loop,
|
|
160
|
+
state: applied.state,
|
|
161
|
+
lastRoute: kept ? loop.lastRoute : applied.tier,
|
|
162
|
+
lastActivity: kept ? activityOf(config, loop) : applied.activity,
|
|
163
|
+
would,
|
|
164
|
+
turnId: facts.turnKey,
|
|
165
|
+
decision: { ...applied, wouldRoute },
|
|
166
|
+
suspended: false,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// The activity of the route running now. Outside `on` there is none, even one a turn in `on` left in the loop before
|
|
171
|
+
// the mode changed: `off` and the applied `shadow` decision are the 1.5 decision.
|
|
172
|
+
const activityOf = (config, loop) => (config.activityRouting === 'on' ? (loop.lastActivity ?? null) : null);
|
|
173
|
+
|
|
174
|
+
// One decision (plan §5.2) for the cell `{ tier, activity }` it lands on. Outside `on` the activity is null throughout.
|
|
175
|
+
function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown, availableModels, now }) {
|
|
91
176
|
const projected = economicConfig(config, loop);
|
|
177
|
+
const routeOf = (cell) => resolveRoute(config, cell.tier, cell.activity);
|
|
178
|
+
const specOf = (cell) => routeSpec(config, routeOf(cell));
|
|
179
|
+
const last = { tier: loop.lastRoute, activity: activityOf(config, loop) };
|
|
180
|
+
const tier0 = TIERS.includes(facts.lastRoute) ? facts.lastRoute : config.baselineTier;
|
|
181
|
+
const incumbent = { tier: tier0, activity: last.activity, route: resolveRoute(config, tier0, last.activity) };
|
|
182
|
+
const activity =
|
|
183
|
+
pin || config.activityRouting !== 'on' ? null : chooseActivity(config, advice, incumbent, facts.effort);
|
|
92
184
|
let result =
|
|
93
185
|
pin && TIERS.includes(pin)
|
|
94
|
-
? { tier: pin, reason: 'pinned', state: { ...loop.state, turn: loop.state.turn + 1 } }
|
|
95
|
-
:
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
now,
|
|
102
|
-
costs,
|
|
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;
|
|
108
|
-
const fits = (tier) => {
|
|
109
|
-
const model = routeModel(config, tier);
|
|
186
|
+
? { tier: pin, activity: null, reason: 'pinned', state: { ...loop.state, turn: loop.state.turn + 1 } }
|
|
187
|
+
: decideCell({ config, projected, facts, advice, state: loop.state, now, incumbent, activity });
|
|
188
|
+
// Without a reliable reading of the context, a route with a smaller window than the last one might overflow.
|
|
189
|
+
const shrinksUnmeasured = (cell) =>
|
|
190
|
+
(!contextKnown || !loop.historyMeasured) && specOf(cell).contextWindow < specOf(last).contextWindow;
|
|
191
|
+
const fits = (cell) => {
|
|
192
|
+
const model = specOf(cell);
|
|
110
193
|
return (
|
|
111
|
-
!shrinksUnmeasured(
|
|
194
|
+
!shrinksUnmeasured(cell) &&
|
|
112
195
|
isModelAllowed(model.id, availableModels, nativeModel) &&
|
|
113
196
|
!loop.ineligible.includes(model.id) &&
|
|
114
197
|
(!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * CONTEXT_FILL)
|
|
115
198
|
);
|
|
116
199
|
};
|
|
117
200
|
if (contextKnown) {
|
|
118
|
-
const
|
|
201
|
+
const routeFor = (tier) => resolveRoute(config, tier, result.activity);
|
|
202
|
+
const fitted = fitTier(config, result.tier, costs.nextContextTokens(facts), routeFor);
|
|
119
203
|
if (fitted !== result.tier) result = { ...result, tier: fitted, reason: 'context-fit' };
|
|
120
204
|
}
|
|
121
|
-
if (shrinksUnmeasured(result
|
|
122
|
-
if (!fits(result
|
|
123
|
-
const
|
|
124
|
-
|
|
205
|
+
if (shrinksUnmeasured(result)) result = { ...result, ...last, reason: 'context-unknown' };
|
|
206
|
+
if (!fits(result)) {
|
|
207
|
+
const native = cellForModel(config, nativeModel);
|
|
208
|
+
const cells = [last, native, ...TIERS.map((tier) => ({ tier, activity }))];
|
|
209
|
+
const fallback = cells.find((cell) => cell && fits(cell)) ?? { tier: null, activity: null };
|
|
210
|
+
result = { ...result, ...fallback, reason: 'model-unavailable' };
|
|
125
211
|
}
|
|
126
|
-
const
|
|
127
|
-
const
|
|
212
|
+
const route = result.tier ? routeOf(result) : null;
|
|
213
|
+
const model = route ? routeSpec(config, route).id : nativeModel;
|
|
214
|
+
const effort = route ? costs.routeEffort(config, route, facts.effort) : facts.effort;
|
|
128
215
|
let comparison = null;
|
|
129
216
|
const candidate = pin ?? advice?.choice;
|
|
130
217
|
if (contextKnown && loop.lastRequest && TIERS.includes(candidate) && candidate !== loop.lastRoute) {
|
|
131
|
-
const
|
|
132
|
-
const
|
|
133
|
-
const incumbent = costs.inputBounds(projected, loop.lastRoute, tokens, facts, now);
|
|
134
|
-
const forecast = costs.shadowEconomics(projected, candidate, loop.lastRoute, facts, now);
|
|
135
|
-
// Without both output prices, compare input only, as shadowEconomics does.
|
|
136
|
-
const output = forecast
|
|
137
|
-
? ((routeModel(projected, candidate).output - routeModel(projected, loop.lastRoute).output) *
|
|
138
|
-
loop.lastRequest.outputTokens) /
|
|
139
|
-
1e6
|
|
140
|
-
: 0;
|
|
218
|
+
const [to, from] = [resolveRoute(config, candidate, activity), routeOf(last)];
|
|
219
|
+
const forecast = costs.shadowEconomics(projected, to, from, facts, now);
|
|
141
220
|
comparison = {
|
|
142
221
|
candidate,
|
|
143
222
|
incumbent: loop.lastRoute,
|
|
144
|
-
|
|
145
|
-
maxUsd: proposed.max - incumbent.min + output,
|
|
223
|
+
...costs.nextTurnRange(projected, to, from, facts, now),
|
|
146
224
|
paybackTurns: forecast?.paybackTurns ?? null,
|
|
147
225
|
outputTokens: loop.lastRequest.outputTokens,
|
|
148
226
|
};
|
|
149
227
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
228
|
+
return { ...result, model, effort, comparison, pinned: Boolean(pin), requestedPin: pin ?? null };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Steps 3-6 of plan §5.2: the tier as `decide` picks it, every candidate priced at its route for `activity` and the
|
|
232
|
+
// incumbent at the route it runs; then, when the tier stays and the route would change, the lateral rule. Equal routes
|
|
233
|
+
// never switch: a label alone decides nothing.
|
|
234
|
+
function decideCell({ config, projected, facts, advice, state, now, incumbent, activity }) {
|
|
235
|
+
const routeFor = (tier) => resolveRoute(config, tier, activity);
|
|
236
|
+
// With an activity, failures move to the lowest tier whose route is stronger, or another request that does not
|
|
237
|
+
// order against it; with none, one tier up as before.
|
|
238
|
+
const escalates = (t) => {
|
|
239
|
+
const order = compareRoutes(config, routeFor(t), incumbent.route, facts.effort);
|
|
240
|
+
return order > 0 || (order === null && !sameRequest(config, routeFor(t), incumbent.route, facts.effort));
|
|
158
241
|
};
|
|
242
|
+
const escalateTo = activity === null ? undefined : (tier) => TIERS.slice(rank(tier) + 1).find(escalates) ?? null;
|
|
243
|
+
const step = decide({
|
|
244
|
+
config: projected,
|
|
245
|
+
facts,
|
|
246
|
+
advice,
|
|
247
|
+
state,
|
|
248
|
+
baseline: config.baselineTier,
|
|
249
|
+
now,
|
|
250
|
+
costs,
|
|
251
|
+
routeFor,
|
|
252
|
+
incumbentRoute: incumbent.route,
|
|
253
|
+
escalateTo,
|
|
254
|
+
});
|
|
255
|
+
const route = routeFor(step.tier);
|
|
256
|
+
if (step.tier !== incumbent.tier || sameRequest(config, route, incumbent.route, facts.effort))
|
|
257
|
+
return { ...step, activity };
|
|
258
|
+
const lateral = lateralMove({
|
|
259
|
+
config: projected,
|
|
260
|
+
from: incumbent.route,
|
|
261
|
+
to: route,
|
|
262
|
+
// Returning to the base route needs no evidence.
|
|
263
|
+
mass: activity === null ? 1 : (advice?.activity?.probabilities?.[activity] ?? 0),
|
|
264
|
+
hold: step.reason === 'hold',
|
|
265
|
+
facts,
|
|
266
|
+
now,
|
|
267
|
+
costs,
|
|
268
|
+
});
|
|
269
|
+
const activityOf = lateral.move ? activity : incumbent.activity;
|
|
270
|
+
return { ...step, activity: activityOf, reason: lateral.reason, estimate: lateral.estimate };
|
|
159
271
|
}
|
|
160
272
|
|
|
273
|
+
// A tool continuation keeps the turn's route and activity; only the context or the model list can move it.
|
|
161
274
|
export function continueRoute(config, loop, { nativeModel, contextTokens, contextKnown, availableModels, effort }) {
|
|
162
275
|
if (!loop.decision) return loop;
|
|
163
276
|
let decision = loop.decision;
|
|
164
277
|
if (contextKnown && decision.tier) {
|
|
165
|
-
const
|
|
278
|
+
const routeOf = (tier) => resolveRoute(config, tier, decision.activity ?? null);
|
|
279
|
+
const fitted = fitTier(config, decision.tier, contextTokens, routeOf);
|
|
166
280
|
if (fitted !== decision.tier)
|
|
167
281
|
decision = {
|
|
168
282
|
...decision,
|
|
169
283
|
tier: fitted,
|
|
170
|
-
model:
|
|
171
|
-
effort: costs.routeEffort(config, fitted, effort),
|
|
284
|
+
model: routeSpec(config, routeOf(fitted)).id,
|
|
285
|
+
effort: costs.routeEffort(config, routeOf(fitted), effort),
|
|
172
286
|
reason: 'context-fit',
|
|
173
287
|
};
|
|
174
288
|
}
|
|
175
289
|
if (!isModelAllowed(decision.model, availableModels, nativeModel) || loop.ineligible.includes(decision.model)) {
|
|
176
|
-
decision = { ...decision, tier: null, model: nativeModel, effort, reason: 'model-unavailable' };
|
|
290
|
+
decision = { ...decision, tier: null, activity: null, model: nativeModel, effort, reason: 'model-unavailable' };
|
|
177
291
|
}
|
|
178
292
|
return { ...loop, decision };
|
|
179
293
|
}
|
package/lib/view.mjs
CHANGED
|
@@ -30,8 +30,15 @@ export function initialView(model) {
|
|
|
30
30
|
estimate: null,
|
|
31
31
|
comparison: null,
|
|
32
32
|
probabilities: null,
|
|
33
|
+
activity: null,
|
|
34
|
+
activityChoice: null,
|
|
35
|
+
activityProbabilities: null,
|
|
36
|
+
wouldRoute: null,
|
|
33
37
|
history: [],
|
|
34
38
|
tiers: [],
|
|
39
|
+
activities: [],
|
|
40
|
+
activityStats: null,
|
|
41
|
+
activityStore: null,
|
|
35
42
|
configPath: null,
|
|
36
43
|
tuning: null,
|
|
37
44
|
tuningBase: null,
|
|
@@ -46,10 +53,33 @@ export function initialView(model) {
|
|
|
46
53
|
// Failures and a pause belong to the classifier that earned them.
|
|
47
54
|
export const healthOf = (client, id) => ({ ...client.snapshot(), classifier: id });
|
|
48
55
|
// The last classification's readings, cleared when another classifier takes over so no label claims them.
|
|
49
|
-
export const CLEARED_READINGS = {
|
|
56
|
+
export const CLEARED_READINGS = {
|
|
57
|
+
adviceMs: null,
|
|
58
|
+
adviceChoice: null,
|
|
59
|
+
probabilities: null,
|
|
60
|
+
estimate: null,
|
|
61
|
+
activityChoice: null,
|
|
62
|
+
activityProbabilities: null,
|
|
63
|
+
wouldRoute: null,
|
|
64
|
+
};
|
|
50
65
|
|
|
51
|
-
// The view patch for a
|
|
52
|
-
|
|
66
|
+
// The view patch for a tool continuation's route. In `on` the activity label is the route's, which a fallback to the
|
|
67
|
+
// native model drops; in `shadow` it stays the classifier's.
|
|
68
|
+
export function continuationView(config, decision, context) {
|
|
69
|
+
return {
|
|
70
|
+
selectedModel: decision.model,
|
|
71
|
+
effort: decision.effort,
|
|
72
|
+
tier: decision.tier,
|
|
73
|
+
reason: decision.reason,
|
|
74
|
+
contextTokens: context.tokens,
|
|
75
|
+
contextKnown: context.known,
|
|
76
|
+
...(config.activityRouting === 'on' ? { activity: decision.activity ?? null } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The view patch for a main reply's usage. `tier` is the routed tier that served it, else null; `activity` the
|
|
81
|
+
// turn's activity as the view labels it, else null.
|
|
82
|
+
export function responseMetrics(view, response, tier, activity = null) {
|
|
53
83
|
const usage = response.usage;
|
|
54
84
|
const counters = usage ? [usage.input_tokens, usage.cache_read_input_tokens, usage.cache_creation_input_tokens] : [];
|
|
55
85
|
const inputTokens =
|
|
@@ -60,11 +90,12 @@ export function responseMetrics(view, response, tier) {
|
|
|
60
90
|
cacheWrite: usage?.cache_creation_input_tokens ?? null,
|
|
61
91
|
inputTokens,
|
|
62
92
|
outputTokens: usage?.output_tokens ?? null,
|
|
63
|
-
// Appended together so the Usage trend can color each reading by its tier.
|
|
93
|
+
// Appended together so the Usage trend can color each reading by its tier, and REPLIES letter it by activity.
|
|
64
94
|
...(Number.isFinite(inputTokens)
|
|
65
95
|
? {
|
|
66
96
|
history: [...(view?.history ?? []), inputTokens].slice(-HISTORY),
|
|
67
97
|
tiers: [...(view?.tiers ?? []), tier ?? null].slice(-HISTORY),
|
|
98
|
+
activities: [...(view?.activities ?? []), activity ?? null].slice(-HISTORY),
|
|
68
99
|
}
|
|
69
100
|
: {}),
|
|
70
101
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Native Claude Code Mod: model and effort routing for the main conversation, advised by Jev, Clef, Clef Flash, OpenAI or Ollama.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
declare module 'claude-code' {
|
|
2
2
|
type RouterTier = 'micro' | 'low' | 'medium' | 'high';
|
|
3
3
|
type RouterCacheState = 'fresh' | 'unknown';
|
|
4
|
+
// What kind of work a turn does (ACTIVITIES in lib/config.mjs); 'uncertain' only as a classifier answer.
|
|
5
|
+
type RouterActivity = 'code' | 'debug' | 'explore' | 'plan' | 'review' | 'ops' | 'docs';
|
|
6
|
+
type RouterActivityAnswer = RouterActivity | 'uncertain';
|
|
7
|
+
type RouterActivityMode = 'off' | 'shadow' | 'on';
|
|
8
|
+
// A finished turn's bucket, read from its tool calls.
|
|
9
|
+
type RouterObserved = 'code' | 'docs' | 'ops' | 'read' | 'talk';
|
|
10
|
+
// Classifier output. `activity` is null when not asked, or missing, malformed or failed.
|
|
11
|
+
interface RouterAdvice {
|
|
12
|
+
choice: RouterTier | 'uncertain';
|
|
13
|
+
confidence: number;
|
|
14
|
+
probabilities: Record<RouterTier | 'uncertain', number>;
|
|
15
|
+
continuation: number | null;
|
|
16
|
+
activity?: {
|
|
17
|
+
choice: RouterActivityAnswer;
|
|
18
|
+
probabilities: Record<RouterActivityAnswer, number>;
|
|
19
|
+
} | null;
|
|
20
|
+
}
|
|
21
|
+
// A next-request cost difference in USD at configured list prices; negative is cheaper.
|
|
22
|
+
interface RouterUsdRange {
|
|
23
|
+
minUsd: number;
|
|
24
|
+
maxUsd: number;
|
|
25
|
+
}
|
|
26
|
+
// What `on` would do, computed in `shadow`. `model` is a model id, as `decision.model`. `difference` prices its next
|
|
27
|
+
// request against the applied route's; null when the routes match or there is no measured request yet.
|
|
28
|
+
interface RouterWouldRoute {
|
|
29
|
+
activity: RouterActivity | null;
|
|
30
|
+
tier: RouterTier | null;
|
|
31
|
+
model: string;
|
|
32
|
+
effort: string | number | null;
|
|
33
|
+
reason: string;
|
|
34
|
+
difference: RouterUsdRange | null;
|
|
35
|
+
}
|
|
36
|
+
// Shadow turns, those `on` would route differently, and of those the ones with a `difference`, summed.
|
|
37
|
+
interface RouterActivityShadow extends RouterUsdRange {
|
|
38
|
+
differs: number;
|
|
39
|
+
turns: number;
|
|
40
|
+
estimated: number;
|
|
41
|
+
}
|
|
42
|
+
interface RouterActivityCounts {
|
|
43
|
+
turns: number;
|
|
44
|
+
requests: number;
|
|
45
|
+
inputTokens: number;
|
|
46
|
+
outputTokens: number;
|
|
47
|
+
}
|
|
48
|
+
// Per-session activity stats; 'none' counts turns without an applied or accepted activity.
|
|
49
|
+
interface RouterActivitySession {
|
|
50
|
+
byActivity: Partial<Record<RouterActivity | 'none', RouterActivityCounts>>;
|
|
51
|
+
switches: { tier: number; activity: number };
|
|
52
|
+
agreement: { matched: number; total: number };
|
|
53
|
+
shadow: RouterActivityShadow;
|
|
54
|
+
}
|
|
55
|
+
// Cross-session counts under store key 'activity:stats:v1'. `runs` buckets run lengths 1, 2, 3, 4, 5+.
|
|
56
|
+
interface RouterActivityStore {
|
|
57
|
+
version: 1;
|
|
58
|
+
confusion: Partial<Record<RouterActivityAnswer | 'none', Partial<Record<RouterObserved, number>>>>;
|
|
59
|
+
runs: Partial<Record<RouterActivity, [number, number, number, number, number]>>;
|
|
60
|
+
lateral: { taken: number; refused: number };
|
|
61
|
+
// A 1.6.0 store has `differs` and `turns` only; readStore fills the rest with zeros.
|
|
62
|
+
shadow: RouterActivityShadow;
|
|
63
|
+
}
|
|
4
64
|
interface RouterComparison {
|
|
5
65
|
candidate: RouterTier;
|
|
6
66
|
incumbent: RouterTier;
|
|
@@ -24,9 +84,19 @@ declare module 'claude-code' {
|
|
|
24
84
|
horizon: number;
|
|
25
85
|
cashCapUsd: number;
|
|
26
86
|
}
|
|
87
|
+
// A model alias and the effort it runs at: what Claude Code receives and what owns a cache.
|
|
88
|
+
interface RouterRoute {
|
|
89
|
+
model: string;
|
|
90
|
+
effort?: string | null;
|
|
91
|
+
}
|
|
27
92
|
interface RouterRoutes {
|
|
28
|
-
routes: Record<RouterTier,
|
|
93
|
+
routes: Record<RouterTier, RouterRoute>;
|
|
29
94
|
baselineTier: RouterTier;
|
|
95
|
+
// The Routing tab's activity edits; a draft without them edits none. A null cell is a removed override.
|
|
96
|
+
activities?: Partial<
|
|
97
|
+
Record<RouterActivity, Partial<Record<RouterTier, { model?: string; effort?: string | null } | null>>>
|
|
98
|
+
>;
|
|
99
|
+
activityRouting?: RouterActivityMode;
|
|
30
100
|
}
|
|
31
101
|
interface RouterView {
|
|
32
102
|
phase: 'ready' | 'choosing' | 'routed' | 'manual' | 'unavailable';
|
|
@@ -54,8 +124,18 @@ declare module 'claude-code' {
|
|
|
54
124
|
estimate?: RouterEstimate | null;
|
|
55
125
|
comparison?: RouterComparison | null;
|
|
56
126
|
probabilities?: Partial<Record<RouterTier | 'uncertain', number>> | null;
|
|
127
|
+
// The turn's activity: the applied one in 'on', the classifier's accepted label in 'shadow'.
|
|
128
|
+
activity?: RouterActivity | null;
|
|
129
|
+
activityChoice?: RouterActivityAnswer | null;
|
|
130
|
+
activityProbabilities?: Partial<Record<RouterActivityAnswer, number>> | null;
|
|
131
|
+
wouldRoute?: RouterWouldRoute | null;
|
|
57
132
|
history?: number[];
|
|
58
133
|
tiers?: (RouterTier | null)[];
|
|
134
|
+
// Aligned with `history` and `tiers`.
|
|
135
|
+
activities?: (RouterActivity | null)[];
|
|
136
|
+
activityStats?: RouterActivitySession | null;
|
|
137
|
+
// The counts kept across sessions as last read or written.
|
|
138
|
+
activityStore?: RouterActivityStore | null;
|
|
59
139
|
configPath?: string | null;
|
|
60
140
|
tuning?: Partial<RouterTuning> | null;
|
|
61
141
|
tuningBase?: RouterTuning | null;
|
|
@@ -75,6 +155,10 @@ declare module 'claude-code' {
|
|
|
75
155
|
}
|
|
76
156
|
interface RouterLoop {
|
|
77
157
|
lastRoute: RouterTier;
|
|
158
|
+
// The activity of the route running now; absent in loops saved before 1.6.
|
|
159
|
+
lastActivity?: RouterActivity | null;
|
|
160
|
+
// Shadow mode only: the cell and policy state `on` would run with, so each would-route continues the last one.
|
|
161
|
+
would?: { lastRoute: RouterTier; lastActivity: RouterActivity | null; state: RouterPolicyState } | null;
|
|
78
162
|
state: RouterPolicyState;
|
|
79
163
|
models: Record<string, { lastAt: number; prefixTokens: number }>;
|
|
80
164
|
resolutions: Record<string, string>;
|
|
@@ -98,6 +182,9 @@ declare module 'claude-code' {
|
|
|
98
182
|
effort: string | number | null;
|
|
99
183
|
estimate?: RouterEstimate | null;
|
|
100
184
|
comparison?: RouterComparison | null;
|
|
185
|
+
// The applied activity; `wouldRoute` is set only in shadow mode.
|
|
186
|
+
activity?: RouterActivity | null;
|
|
187
|
+
wouldRoute?: RouterWouldRoute | null;
|
|
101
188
|
pinned: boolean;
|
|
102
189
|
requestedPin: RouterTier | null;
|
|
103
190
|
} | null;
|