@alexeiled/claude-router 1.6.1 → 1.7.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 +1 -1
- package/CHANGELOG.md +53 -0
- package/README.md +14 -4
- package/docs/activity-routing.md +254 -0
- package/docs/architecture.md +11 -5
- package/docs/configuration.md +56 -44
- package/docs/evaluation.md +34 -7
- package/docs/native-router.md +17 -14
- package/docs/router-band.svg +78 -80
- package/docs/router-pane-classifier.svg +46 -41
- package/docs/router-pane-now.svg +9 -9
- package/docs/router-pane-routing.svg +73 -87
- package/docs/router-pane-usage.svg +134 -0
- package/docs/user-guide.md +81 -57
- package/hooks/native-router.mjs +316 -44
- package/lib/activity-stats.mjs +165 -11
- package/lib/band.mjs +82 -55
- package/lib/config.mjs +49 -32
- package/lib/cost.mjs +21 -6
- package/lib/display.mjs +101 -4
- package/lib/panel.mjs +409 -77
- package/lib/probe-results.mjs +40 -0
- package/lib/route.mjs +34 -10
- package/lib/savings.mjs +238 -0
- package/lib/view.mjs +10 -1
- package/package.json +2 -1
- package/types/index.d.ts +55 -0
package/hooks/native-router.mjs
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
advanceDown,
|
|
2
3
|
advanceRun,
|
|
4
|
+
emptyMetrics,
|
|
3
5
|
emptySession,
|
|
4
6
|
emptyStore,
|
|
7
|
+
METRICS_PREFIX,
|
|
8
|
+
mergeMetrics,
|
|
9
|
+
metricsAfterReset,
|
|
10
|
+
readMetrics,
|
|
5
11
|
readStore,
|
|
12
|
+
recordMetrics,
|
|
6
13
|
recordStore,
|
|
7
14
|
recordTurn,
|
|
8
15
|
STORE_KEY,
|
|
@@ -38,7 +45,9 @@ import {
|
|
|
38
45
|
GATEWAY_SETTINGS,
|
|
39
46
|
missingCredentials,
|
|
40
47
|
NOT_A_TIER_REASON,
|
|
48
|
+
savingsLine,
|
|
41
49
|
storeAgreementLine,
|
|
50
|
+
unavailableReason,
|
|
42
51
|
} from '../lib/display.mjs';
|
|
43
52
|
import { clip, observedActivity, promptIndex } from '../lib/facts.mjs';
|
|
44
53
|
import { renderPanel, routeDraftOf, routingChanges } from '../lib/panel.mjs';
|
|
@@ -55,6 +64,19 @@ import {
|
|
|
55
64
|
prepareLoop,
|
|
56
65
|
resetHistory,
|
|
57
66
|
} from '../lib/route.mjs';
|
|
67
|
+
import {
|
|
68
|
+
addReply,
|
|
69
|
+
afterReset,
|
|
70
|
+
compareReply,
|
|
71
|
+
emptySavingsStore,
|
|
72
|
+
emptyTotals,
|
|
73
|
+
mergeSavingsStores,
|
|
74
|
+
readSavingsStore,
|
|
75
|
+
recordSavingsStore,
|
|
76
|
+
SAVINGS_PREFIX,
|
|
77
|
+
SAVINGS_RESET_KEY,
|
|
78
|
+
sessionCacheTtl,
|
|
79
|
+
} from '../lib/savings.mjs';
|
|
58
80
|
import { CLEARED_READINGS, continuationView, healthOf, initialView, responseMetrics } from '../lib/view.mjs';
|
|
59
81
|
|
|
60
82
|
// The engine follows $ only into functions declared in this file, never across an import: every helper that takes $
|
|
@@ -88,6 +110,18 @@ function createRouter(options) {
|
|
|
88
110
|
// one activity, flushed to the store when it ends.
|
|
89
111
|
turnActivities: new Map(),
|
|
90
112
|
run: null,
|
|
113
|
+
// The route a cheaper activity move went to, while the turns stay on it (advanceDown); else null.
|
|
114
|
+
afterDown: null,
|
|
115
|
+
// Each turn's routing-vs-your-model totals until turn.complete adds them to the store.
|
|
116
|
+
turnSavings: new Map(),
|
|
117
|
+
// The totals kept across sessions: this session's record (`{ sessionId, record }`), held here so a refresh never
|
|
118
|
+
// reads it back from the store while a write of it is pending, and the sum of every other session's, read on
|
|
119
|
+
// refresh. The view shows their sum.
|
|
120
|
+
savingsOwn: null,
|
|
121
|
+
savingsOthers: null,
|
|
122
|
+
// The activity metrics kept across sessions, held the same way: this session's record and the others' sum.
|
|
123
|
+
metricsOwn: null,
|
|
124
|
+
metricsOthers: null,
|
|
91
125
|
// Bumped by Reset activity stats, so a store write that read the counts before the reset skips.
|
|
92
126
|
statsResets: 0,
|
|
93
127
|
};
|
|
@@ -118,7 +152,8 @@ async function modeOf($, router, fallback = 'auto') {
|
|
|
118
152
|
}
|
|
119
153
|
}
|
|
120
154
|
|
|
121
|
-
// A saved preference wins; a fresh session starts with routing on for a model some
|
|
155
|
+
// A saved preference wins; a fresh session starts with routing on for a model some route runs (a tier's, or in `on` an
|
|
156
|
+
// activity override's), and off for any other.
|
|
122
157
|
async function startMode($, router, model) {
|
|
123
158
|
const mode = await modeOf($, router, cellForModel(router.config, model) === null ? 'manual' : 'auto');
|
|
124
159
|
router.modes.set(await $.session.id(), mode);
|
|
@@ -147,26 +182,84 @@ async function updateView($, router, patch) {
|
|
|
147
182
|
await $.state.set(VIEW, router.view);
|
|
148
183
|
}
|
|
149
184
|
|
|
185
|
+
// This session's record kept across sessions, read from the store once per session, and the last reset (ms) from
|
|
186
|
+
// any session, read each time: a Reset in another session empties a record held from before it.
|
|
187
|
+
async function ownSavings($, router) {
|
|
188
|
+
const sessionId = await $.session.id();
|
|
189
|
+
const resetAt = await $.store.get(SAVINGS_RESET_KEY);
|
|
190
|
+
if (router.savingsOwn?.sessionId !== sessionId) {
|
|
191
|
+
const record = readSavingsStore(await $.store.get(`${SAVINGS_PREFIX}${sessionId}`));
|
|
192
|
+
// Another call may have read it meanwhile and a turn added to it: that one stands.
|
|
193
|
+
if (router.savingsOwn?.sessionId !== sessionId) router.savingsOwn = { sessionId, record };
|
|
194
|
+
}
|
|
195
|
+
router.savingsOwn.record = afterReset(router.savingsOwn.record, resetAt);
|
|
196
|
+
return { own: router.savingsOwn, resetAt };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Every other session's record since the last reset, added up.
|
|
200
|
+
async function otherSavings($, sessionId, resetAt) {
|
|
201
|
+
const own = `${SAVINGS_PREFIX}${sessionId}`;
|
|
202
|
+
const keys = (await $.store.keys()).filter((key) => key.startsWith(SAVINGS_PREFIX) && key !== own);
|
|
203
|
+
return mergeSavingsStores(await Promise.all(keys.map((key) => $.store.get(key))), resetAt);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// This session's metrics record and the last reset, as ownSavings: Reset stats clears both with one marker.
|
|
207
|
+
async function ownMetrics($, router) {
|
|
208
|
+
const sessionId = await $.session.id();
|
|
209
|
+
const resetAt = await $.store.get(SAVINGS_RESET_KEY);
|
|
210
|
+
if (router.metricsOwn?.sessionId !== sessionId) {
|
|
211
|
+
const record = readMetrics(await $.store.get(`${METRICS_PREFIX}${sessionId}`));
|
|
212
|
+
if (router.metricsOwn?.sessionId !== sessionId) router.metricsOwn = { sessionId, record };
|
|
213
|
+
}
|
|
214
|
+
router.metricsOwn.record = metricsAfterReset(router.metricsOwn.record, resetAt);
|
|
215
|
+
return { own: router.metricsOwn, resetAt };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Every other session's metrics since the last reset, added up.
|
|
219
|
+
async function otherMetrics($, sessionId, resetAt) {
|
|
220
|
+
const own = `${METRICS_PREFIX}${sessionId}`;
|
|
221
|
+
const keys = (await $.store.keys()).filter((key) => key.startsWith(METRICS_PREFIX) && key !== own);
|
|
222
|
+
return mergeMetrics(await Promise.all(keys.map((key) => $.store.get(key))), resetAt);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const metricsView = (router) => mergeMetrics([router.metricsOthers, router.metricsOwn?.record]);
|
|
226
|
+
|
|
227
|
+
// The view's totals kept across sessions, from what the router holds; nothing until a read succeeded.
|
|
228
|
+
const savedView = (router) =>
|
|
229
|
+
router.savingsOthers ? { savingsStore: mergeSavingsStores([router.savingsOthers, router.savingsOwn?.record]) } : {};
|
|
230
|
+
|
|
150
231
|
// The counts kept across sessions for the view, read when the session starts and the pane opens: another session
|
|
151
|
-
// may have added to them. A failed read keeps what the view has; it never breaks a turn or a render.
|
|
152
|
-
|
|
232
|
+
// may have added to them. A failed read keeps what the view has; it never breaks a turn or a render. The saved
|
|
233
|
+
// totals are taken from the router after the reads, so a turn that finished meanwhile counts once.
|
|
234
|
+
async function storeView($, router) {
|
|
235
|
+
const out = {};
|
|
153
236
|
try {
|
|
154
|
-
|
|
155
|
-
} catch {
|
|
156
|
-
|
|
157
|
-
|
|
237
|
+
out.activityStore = readStore(await $.store.get(STORE_KEY));
|
|
238
|
+
} catch {}
|
|
239
|
+
try {
|
|
240
|
+
const { own, resetAt } = await ownMetrics($, router);
|
|
241
|
+
router.metricsOthers = await otherMetrics($, own.sessionId, resetAt);
|
|
242
|
+
out.activityMetrics = metricsView(router);
|
|
243
|
+
} catch {}
|
|
244
|
+
try {
|
|
245
|
+
const { own, resetAt } = await ownSavings($, router);
|
|
246
|
+
router.savingsOthers = await otherSavings($, own.sessionId, resetAt);
|
|
247
|
+
} catch {}
|
|
248
|
+
return out;
|
|
158
249
|
}
|
|
159
250
|
|
|
160
251
|
// A notice answers the last press in the pane, so the pane opens without one; the last write keeps its Undo in the
|
|
161
252
|
// status bar.
|
|
162
253
|
async function openPane($, router) {
|
|
163
|
-
await updateView($, router, { notice: null, ...(await storeView(
|
|
254
|
+
await updateView($, router, { notice: null, ...(await storeView($, router)), ...savedView(router) });
|
|
164
255
|
await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
|
|
165
256
|
}
|
|
166
257
|
|
|
167
258
|
async function changeMode($, router, mode) {
|
|
168
259
|
const view = router.view ?? (await readView($, router));
|
|
169
260
|
for (const controller of router.controllers) controller.abort();
|
|
261
|
+
// Turns with routing off run on Claude's model, off the cheaper move's route: the escalation watch ends.
|
|
262
|
+
if (mode === 'manual') router.afterDown = null;
|
|
170
263
|
router.modes.set(await $.session.id(), mode);
|
|
171
264
|
const saved = await rememberMode($, mode);
|
|
172
265
|
await updateView($, router, {
|
|
@@ -180,7 +273,7 @@ async function changeMode($, router, mode) {
|
|
|
180
273
|
|
|
181
274
|
async function setPin($, router, tier) {
|
|
182
275
|
const view = router.view ?? (await readView($, router));
|
|
183
|
-
if (view.phase === 'unavailable') return
|
|
276
|
+
if (view.phase === 'unavailable') return unavailableText(view);
|
|
184
277
|
const mode = await modeOf($, router, view.mode);
|
|
185
278
|
if (mode === 'manual') return 'Routing is off. Turn routing on before pinning a turn.';
|
|
186
279
|
await updateView($, router, { pendingPin: tier });
|
|
@@ -191,6 +284,8 @@ async function setPin($, router, tier) {
|
|
|
191
284
|
async function routerCommand($, router, args) {
|
|
192
285
|
const [action, value] = args.trim().split(/\s+/);
|
|
193
286
|
if (action === 'auto' || action === 'off') {
|
|
287
|
+
const view = router.view ?? (await readView($, router));
|
|
288
|
+
if (view.phase === 'unavailable') return { text: unavailableText(view) };
|
|
194
289
|
await changeMode($, router, action === 'auto' ? 'auto' : 'manual');
|
|
195
290
|
return { text: action === 'auto' ? 'Routing on.' : 'Routing off: Claude’s model is kept.' };
|
|
196
291
|
}
|
|
@@ -212,7 +307,7 @@ async function routerCommand($, router, args) {
|
|
|
212
307
|
// `/router activities <mode>`: the pane's write, so the file keeps the rest and Undo in the pane restores it.
|
|
213
308
|
async function setActivityRouting($, router, mode) {
|
|
214
309
|
const view = router.view ?? (await readView($, router));
|
|
215
|
-
if (view.phase === 'unavailable') return
|
|
310
|
+
if (view.phase === 'unavailable') return unavailableText(view);
|
|
216
311
|
if (mode === router.config.activityRouting) return `Activity routing is already ${mode}.`;
|
|
217
312
|
await paneActions($, router, view).activityRouting(mode);
|
|
218
313
|
return router.view.notice;
|
|
@@ -291,7 +386,15 @@ async function saveConfig($, path, change) {
|
|
|
291
386
|
}
|
|
292
387
|
}
|
|
293
388
|
|
|
389
|
+
const unavailableText = (view) => `Routing unavailable: ${unavailableReason(view)}. Claude’s model is kept.`;
|
|
390
|
+
|
|
294
391
|
function detailText(config, view) {
|
|
392
|
+
if (view.phase === 'unavailable')
|
|
393
|
+
return [
|
|
394
|
+
unavailableText(view),
|
|
395
|
+
`Model: ${view.nativeModel}`,
|
|
396
|
+
...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP : []),
|
|
397
|
+
].join('\n');
|
|
295
398
|
const missing = missingCredentials(config, view, config.classifier);
|
|
296
399
|
return [
|
|
297
400
|
`Router — routing ${view.mode === 'auto' ? 'on' : 'off'}`,
|
|
@@ -301,13 +404,13 @@ function detailText(config, view) {
|
|
|
301
404
|
`Reason: ${view.reason}`,
|
|
302
405
|
...activityDetailLines(config, view),
|
|
303
406
|
storeAgreementLine(view),
|
|
407
|
+
savingsLine(view),
|
|
304
408
|
view.error || missing ? classifierStatus(config, view.error ?? missing) : `${activeClassifier(config).label} ready`,
|
|
305
409
|
`Context: ${view.contextKnown ? `${view.contextTokens} tokens (estimate)` : 'unknown'}`,
|
|
306
410
|
`Observed cache: ${view.cacheRead ?? 'unknown'} read, ${view.cacheWrite ?? 'unknown'} written tokens`,
|
|
307
411
|
view.pendingPin ? `Next turn pin: ${view.pendingPin}` : 'No next-turn pin.',
|
|
308
412
|
'Routing on picks a model for each turn. Routing off keeps Claude’s model. Pins serve one turn only.',
|
|
309
413
|
'Cache lifetime is an estimate. Claude’s cost ledger owns session totals.',
|
|
310
|
-
...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP : []),
|
|
311
414
|
].join('\n');
|
|
312
415
|
}
|
|
313
416
|
|
|
@@ -345,6 +448,9 @@ async function* passMain($, e, next, loop, version, nativeModel, reason, router)
|
|
|
345
448
|
stopReason: response.stopReason,
|
|
346
449
|
now: Date.now(),
|
|
347
450
|
});
|
|
451
|
+
// Routing chose nothing here, so the reply is not counted; your model's cache still follows it.
|
|
452
|
+
const compared = await compareYours($, router, loop, response, e.effort ?? null, nativeModel);
|
|
453
|
+
if (compared) observed.yours = compared.state;
|
|
348
454
|
const written = await $.state.set(ref, observed, { ifVersion: version });
|
|
349
455
|
if (written.isSet) await updateView($, router, responseMetrics(router.view, response, null));
|
|
350
456
|
}
|
|
@@ -395,6 +501,8 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
395
501
|
turns: facts.turns,
|
|
396
502
|
signal: controller.signal,
|
|
397
503
|
});
|
|
504
|
+
// The wait for advice alone, comparable with the activity probe's p95; the routing after it is not the classifier's.
|
|
505
|
+
const adviceMs = pin || !classifierTimed(result.error) ? null : Date.now() - adviceStarted;
|
|
398
506
|
if (controller.signal.aborted || (await $.session.id()) !== sessionId || (await modeOf($, router)) !== 'auto')
|
|
399
507
|
return null;
|
|
400
508
|
const previous = loop.decision;
|
|
@@ -414,7 +522,22 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
414
522
|
if (previous?.model && previous.model !== selected.decision.model && !selected.decision.pinned)
|
|
415
523
|
$.ui.toast(switchToast(cfg, previous, selected.decision, selected.decision.estimate));
|
|
416
524
|
const activity = turnActivity(cfg, result.advice, previous, selected.decision);
|
|
417
|
-
router.turnActivities.set(e.turnId, {
|
|
525
|
+
router.turnActivities.set(e.turnId, {
|
|
526
|
+
...activity,
|
|
527
|
+
reason: selected.decision.reason,
|
|
528
|
+
escalated: Boolean(selected.decision.escalated),
|
|
529
|
+
pinned: selected.decision.pinned,
|
|
530
|
+
route: `${selected.decision.model}@${selected.decision.effort ?? 'session'}`,
|
|
531
|
+
classifier: cfg.classifier,
|
|
532
|
+
adviceMs,
|
|
533
|
+
sessionId,
|
|
534
|
+
requests: 0,
|
|
535
|
+
inputTokens: 0,
|
|
536
|
+
outputTokens: 0,
|
|
537
|
+
routedUsd: null,
|
|
538
|
+
pricedRequests: 0,
|
|
539
|
+
routes: {},
|
|
540
|
+
});
|
|
418
541
|
await updateView($, router, {
|
|
419
542
|
phase: 'routed',
|
|
420
543
|
selectedModel: selected.decision.model,
|
|
@@ -432,7 +555,7 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
432
555
|
? {
|
|
433
556
|
error: result.error,
|
|
434
557
|
health: healthOf(clientOf(router, cfg.classifier), cfg.classifier),
|
|
435
|
-
adviceMs
|
|
558
|
+
adviceMs,
|
|
436
559
|
adviceChoice: result.advice?.choice ?? null,
|
|
437
560
|
probabilities: result.advice?.probabilities ?? null,
|
|
438
561
|
estimate: selected.decision.estimate ?? null,
|
|
@@ -451,10 +574,16 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
451
574
|
}
|
|
452
575
|
|
|
453
576
|
// A routed reply's metrics in the view, labelled with the turn's activity when its tier served it, and added to the
|
|
454
|
-
// turn's activity record.
|
|
455
|
-
async function publishReply($, router, turnId, response, tier) {
|
|
577
|
+
// turn's activity record with its list price (`routedUsd`, null when unpriced) and, when its tier served it, its route.
|
|
578
|
+
async function publishReply($, router, turnId, response, tier, route, routedUsd) {
|
|
456
579
|
const turn = router.turnActivities.get(turnId);
|
|
457
|
-
const metrics = responseMetrics(
|
|
580
|
+
const metrics = responseMetrics(
|
|
581
|
+
router.view,
|
|
582
|
+
response,
|
|
583
|
+
tier,
|
|
584
|
+
tier ? (turn?.label ?? null) : null,
|
|
585
|
+
tier ? route : null,
|
|
586
|
+
);
|
|
458
587
|
await updateView($, router, metrics);
|
|
459
588
|
if (turn)
|
|
460
589
|
router.turnActivities.set(turnId, {
|
|
@@ -462,11 +591,101 @@ async function publishReply($, router, turnId, response, tier) {
|
|
|
462
591
|
requests: turn.requests + 1,
|
|
463
592
|
inputTokens: turn.inputTokens + (metrics.inputTokens ?? 0),
|
|
464
593
|
outputTokens: turn.outputTokens + (metrics.outputTokens ?? 0),
|
|
594
|
+
routedUsd: Number.isFinite(routedUsd) ? (turn.routedUsd ?? 0) + routedUsd : turn.routedUsd,
|
|
595
|
+
pricedRequests: turn.pricedRequests + (Number.isFinite(routedUsd) ? 1 : 0),
|
|
596
|
+
routes: tier ? { ...turn.routes, [route]: (turn.routes[route] ?? 0) + 1 } : turn.routes,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// A routed reply written to the loop at `loopVersion`, then published: the view's metrics and the routing-vs-your-model
|
|
601
|
+
// totals. A lost write publishes nothing.
|
|
602
|
+
async function observeRouted($, router, { turnId, cfg, loop, loopVersion, request, response, nativeModel }) {
|
|
603
|
+
const observed = observeResponse(loop, {
|
|
604
|
+
usage: response.usage,
|
|
605
|
+
requestedModel: request.model,
|
|
606
|
+
effort: request.effort ?? null,
|
|
607
|
+
stopReason: response.stopReason,
|
|
608
|
+
now: Date.now(),
|
|
609
|
+
});
|
|
610
|
+
if (response.stopReason === null) observed.suspended = true;
|
|
611
|
+
const compared = await compareYours($, router, loop, response, request.effort ?? null, nativeModel, cfg);
|
|
612
|
+
if (compared) observed.yours = compared.state;
|
|
613
|
+
const written = await $.state.set({ ...LOOP, id: 'main' }, observed, { ifVersion: loopVersion });
|
|
614
|
+
if (!written.isSet) return;
|
|
615
|
+
// A substituted reply is not the tier's: the strip and trend must not count it as one.
|
|
616
|
+
const tier = isSameModel(request.model, response.usage?.model) ? loop.decision.tier : null;
|
|
617
|
+
const route = `${request.model}@${request.effort ?? 'session'}`;
|
|
618
|
+
await publishReply($, router, turnId, response, tier, route, compared?.reply?.routedUsd ?? null);
|
|
619
|
+
if (compared?.reply) await addSavings($, router, turnId, compared.reply, tier);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// The main conversation's prompt-cache lifetime by Claude Code's rule (sessionCacheTtl), or null when a reading
|
|
623
|
+
// fails. $.env.get takes literal names only. A plan subscriber is one whose replies report plan rate limits; a usage
|
|
624
|
+
// without them is not one.
|
|
625
|
+
async function cacheTtlOf($) {
|
|
626
|
+
try {
|
|
627
|
+
const settings = await $.settings.read();
|
|
628
|
+
const usage = await $.session.usage();
|
|
629
|
+
return sessionCacheTtl({
|
|
630
|
+
force5m: await $.env.get('FORCE_PROMPT_CACHING_5M'),
|
|
631
|
+
envTtl: await $.env.get('CLAUDE_CODE_PROMPT_CACHE_TTL'),
|
|
632
|
+
settingTtl: settings.promptCacheTtl,
|
|
633
|
+
enable1h: await $.env.get('ENABLE_PROMPT_CACHING_1H'),
|
|
634
|
+
subscriber: (usage?.rateLimits ?? []).some((limit) => limit.kind === 'five_hour' || limit.kind === 'seven_day'),
|
|
635
|
+
});
|
|
636
|
+
} catch {
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// A main reply against your model (compareReply): the loop's next `yours` state and the reply's prices, or null. The
|
|
642
|
+
// readout never breaks a turn. A reply whose cache lifetime cannot be read still moves your model's cache and is not
|
|
643
|
+
// counted; any other failure leaves the loop's state and the totals as they were.
|
|
644
|
+
async function compareYours($, router, loop, response, servedEffort, nativeModel, cfg = router.config) {
|
|
645
|
+
try {
|
|
646
|
+
if (!response.usage?.model) return null;
|
|
647
|
+
const view = router.view ?? (await readView($, router));
|
|
648
|
+
return compareReply(cfg, loop.yours ?? null, {
|
|
649
|
+
usage: response.usage,
|
|
650
|
+
served: { model: response.usage.model, effort: servedEffort },
|
|
651
|
+
yours: { model: nativeModel, effort: view.nativeEffort ?? null },
|
|
652
|
+
ttl: await cacheTtlOf($),
|
|
653
|
+
now: Date.now(),
|
|
465
654
|
});
|
|
655
|
+
} catch {
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// A routed reply's prices added to the session totals in the view and to the turn's totals for the store.
|
|
661
|
+
async function addSavings($, router, turnId, reply, tier) {
|
|
662
|
+
try {
|
|
663
|
+
const view = router.view ?? (await readView($, router));
|
|
664
|
+
router.turnSavings.set(turnId, addReply(router.turnSavings.get(turnId) ?? emptyTotals(), reply, tier));
|
|
665
|
+
await updateView($, router, { savings: addReply(view.savings ?? emptyTotals(), reply, tier) });
|
|
666
|
+
} catch {}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// A finished turn's totals added to this session's record kept across sessions, with the guard recordActivity uses:
|
|
670
|
+
// totals read before a reset are not written back. The record is added to in memory before the write, so a refresh
|
|
671
|
+
// during the write counts the turn once.
|
|
672
|
+
async function flushSavings($, router, totals) {
|
|
673
|
+
try {
|
|
674
|
+
const resets = router.statsResets;
|
|
675
|
+
const { own, resetAt } = await ownSavings($, router);
|
|
676
|
+
// The others held from before a reset in another session are read again.
|
|
677
|
+
if (!router.savingsOthers || afterReset(router.savingsOthers, resetAt) !== router.savingsOthers)
|
|
678
|
+
router.savingsOthers = await otherSavings($, own.sessionId, resetAt);
|
|
679
|
+
if (router.statsResets !== resets) return;
|
|
680
|
+
own.record = recordSavingsStore(own.record, totals, Date.now());
|
|
681
|
+
await $.store.set(`${SAVINGS_PREFIX}${own.sessionId}`, own.record);
|
|
682
|
+
if (router.statsResets === resets) await updateView($, router, savedView(router));
|
|
683
|
+
} catch {}
|
|
466
684
|
}
|
|
467
685
|
|
|
468
686
|
// A finished turn's activity stats: the session counts in the view and the counts kept across sessions. Stats never
|
|
469
|
-
// break a turn: a failed read or write loses this turn's counts
|
|
687
|
+
// break a turn: a failed read or write loses this turn's counts. Readings already in the session's metrics record stay
|
|
688
|
+
// in memory after a failed write and go out with the next turn that adds a reading.
|
|
470
689
|
async function recordActivity($, router, turn) {
|
|
471
690
|
try {
|
|
472
691
|
if ((await $.session.id()) !== turn.sessionId) return;
|
|
@@ -480,6 +699,8 @@ async function recordActivity($, router, turn) {
|
|
|
480
699
|
// Advanced before any further await, so a session change meanwhile cannot carry this run into the new session.
|
|
481
700
|
const { run, ended } = advanceRun(router.run, turn.answer);
|
|
482
701
|
router.run = run;
|
|
702
|
+
const down = advanceDown(router.afterDown, turn);
|
|
703
|
+
router.afterDown = down.after;
|
|
483
704
|
await updateView($, router, {
|
|
484
705
|
activityStats: recordTurn(view.activityStats ?? emptySession(), {
|
|
485
706
|
activity: turn.label,
|
|
@@ -488,8 +709,12 @@ async function recordActivity($, router, turn) {
|
|
|
488
709
|
requests: turn.requests,
|
|
489
710
|
inputTokens: turn.inputTokens,
|
|
490
711
|
outputTokens: turn.outputTokens,
|
|
712
|
+
routedUsd: turn.routedUsd,
|
|
713
|
+
pricedRequests: turn.pricedRequests,
|
|
714
|
+
routes: turn.routes,
|
|
491
715
|
tierSwitches: turn.switched === 'tier' ? 1 : 0,
|
|
492
716
|
activitySwitches: turn.switched === 'activity' ? 1 : 0,
|
|
717
|
+
lateral: turn.lateral,
|
|
493
718
|
wouldDiffer: turn.wouldDiffer,
|
|
494
719
|
wouldUsd: turn.wouldUsd,
|
|
495
720
|
shadow: turn.mode === 'shadow',
|
|
@@ -497,6 +722,10 @@ async function recordActivity($, router, turn) {
|
|
|
497
722
|
});
|
|
498
723
|
const resets = router.statsResets;
|
|
499
724
|
const store = readStore(await $.store.get(STORE_KEY));
|
|
725
|
+
const { own, resetAt } = await ownMetrics($, router);
|
|
726
|
+
// The others held from before a reset in another session are read again.
|
|
727
|
+
if (!router.metricsOthers || metricsAfterReset(router.metricsOthers, resetAt) !== router.metricsOthers)
|
|
728
|
+
router.metricsOthers = await otherMetrics($, own.sessionId, resetAt);
|
|
500
729
|
// Writing counts read before a reset would bring them back; this turn's are lost instead.
|
|
501
730
|
if (router.statsResets !== resets) return;
|
|
502
731
|
const written = recordStore(store, {
|
|
@@ -507,9 +736,23 @@ async function recordActivity($, router, turn) {
|
|
|
507
736
|
wouldDiffer: turn.wouldDiffer,
|
|
508
737
|
wouldUsd: turn.wouldUsd,
|
|
509
738
|
});
|
|
510
|
-
|
|
739
|
+
const measured = recordMetrics(own.record, {
|
|
740
|
+
classifier: turn.classifier,
|
|
741
|
+
adviceMs: turn.adviceMs,
|
|
742
|
+
downMove: down.event,
|
|
743
|
+
now: Date.now(),
|
|
744
|
+
});
|
|
745
|
+
// Added to in memory before the write, so a refresh during the write counts the turn once.
|
|
746
|
+
const changed = measured !== own.record;
|
|
747
|
+
own.record = measured;
|
|
748
|
+
// Both writes start before any reset can run between them.
|
|
749
|
+
await Promise.all([
|
|
750
|
+
$.store.set(STORE_KEY, written),
|
|
751
|
+
changed ? $.store.set(`${METRICS_PREFIX}${own.sessionId}`, measured) : null,
|
|
752
|
+
]);
|
|
511
753
|
// A reset during the write emptied the view; these counts are older than it.
|
|
512
|
-
if (router.statsResets === resets)
|
|
754
|
+
if (router.statsResets === resets)
|
|
755
|
+
await updateView($, router, { activityStore: written, activityMetrics: metricsView(router) });
|
|
513
756
|
} catch {}
|
|
514
757
|
}
|
|
515
758
|
|
|
@@ -671,17 +914,42 @@ function paneActions($, router, view) {
|
|
|
671
914
|
withActivities(file, { base: { activityRouting: from }, activityRouting: mode }),
|
|
672
915
|
);
|
|
673
916
|
},
|
|
674
|
-
|
|
917
|
+
// One reset for both readouts: activity stats and routing vs your model, this session's and the saved ones.
|
|
918
|
+
resetStats: async () => {
|
|
675
919
|
router.statsResets += 1;
|
|
676
920
|
router.run = null;
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
921
|
+
router.afterDown = null;
|
|
922
|
+
router.turnSavings.clear();
|
|
923
|
+
const clear = (key, value) =>
|
|
924
|
+
$.store
|
|
925
|
+
.set(key, value)
|
|
926
|
+
.then(() => true)
|
|
927
|
+
.catch(() => false);
|
|
928
|
+
// The per-session records: savings and activity metrics.
|
|
929
|
+
const clearRecords = async () => {
|
|
930
|
+
// Written first, so a session still holding its record from before the reset drops it, unless that record's
|
|
931
|
+
// first reading has the reset's millisecond or the clock moved back across the reset.
|
|
932
|
+
await $.store.set(SAVINGS_RESET_KEY, Date.now());
|
|
933
|
+
const keys = (await $.store.keys()).filter(
|
|
934
|
+
(key) => key.startsWith(SAVINGS_PREFIX) || key.startsWith(METRICS_PREFIX),
|
|
935
|
+
);
|
|
936
|
+
await Promise.all(keys.map((key) => $.store.delete(key)));
|
|
937
|
+
router.savingsOwn = null;
|
|
938
|
+
router.savingsOthers = emptySavingsStore();
|
|
939
|
+
router.metricsOwn = null;
|
|
940
|
+
router.metricsOthers = emptyMetrics();
|
|
941
|
+
return true;
|
|
942
|
+
};
|
|
943
|
+
const [stats, records] = await Promise.all([clear(STORE_KEY, emptyStore()), clearRecords().catch(() => false)]);
|
|
681
944
|
await updateView($, router, {
|
|
682
945
|
activityStats: null,
|
|
683
|
-
|
|
684
|
-
|
|
946
|
+
savings: null,
|
|
947
|
+
...(stats ? { activityStore: emptyStore() } : {}),
|
|
948
|
+
...(records ? { activityMetrics: emptyMetrics(), savingsStore: emptySavingsStore() } : {}),
|
|
949
|
+
notice:
|
|
950
|
+
stats && records
|
|
951
|
+
? 'Stats reset: activity and routing vs your model, this session and saved.'
|
|
952
|
+
: 'This session’s stats reset. Saved stats could not be cleared.',
|
|
685
953
|
});
|
|
686
954
|
},
|
|
687
955
|
discardRouting: () => updateView($, router, { routeDraft: null, tuning: null, tuningBase: null, notice: null }),
|
|
@@ -773,7 +1041,8 @@ export function register(on, options) {
|
|
|
773
1041
|
routeDraft: null,
|
|
774
1042
|
lastWrite: null,
|
|
775
1043
|
bandDetail: (await $.store.get(BAND_DETAIL).catch(() => false)) === true,
|
|
776
|
-
...(await storeView(
|
|
1044
|
+
...(await storeView($, router)),
|
|
1045
|
+
...savedView(router),
|
|
777
1046
|
});
|
|
778
1047
|
} catch (error) {
|
|
779
1048
|
await updateView($, router, {
|
|
@@ -823,6 +1092,7 @@ export function register(on, options) {
|
|
|
823
1092
|
const saved = await rememberMode($, mode);
|
|
824
1093
|
await updateView($, router, {
|
|
825
1094
|
mode,
|
|
1095
|
+
nativeEffort: e.effort ?? null,
|
|
826
1096
|
...(!saved ? { notice: 'Resume preference could not be saved. Current mode remains active.' } : {}),
|
|
827
1097
|
});
|
|
828
1098
|
}
|
|
@@ -879,20 +1149,8 @@ export function register(on, options) {
|
|
|
879
1149
|
if (loop.decision.effort === null) delete request.effort;
|
|
880
1150
|
else request.effort = loop.decision.effort;
|
|
881
1151
|
const response = yield* next(request);
|
|
882
|
-
if (!next.signal.aborted && (await $.session.id()) === sessionId)
|
|
883
|
-
|
|
884
|
-
usage: response.usage,
|
|
885
|
-
requestedModel: request.model,
|
|
886
|
-
effort: request.effort ?? null,
|
|
887
|
-
stopReason: response.stopReason,
|
|
888
|
-
now: Date.now(),
|
|
889
|
-
});
|
|
890
|
-
if (response.stopReason === null) observed.suspended = true;
|
|
891
|
-
const written = await $.state.set(ref, observed, { ifVersion: loopVersion });
|
|
892
|
-
// A substituted reply is not the tier's: the strip and trend must not count it as one.
|
|
893
|
-
const tier = isSameModel(request.model, response.usage?.model) ? loop.decision.tier : null;
|
|
894
|
-
if (written.isSet) await publishReply($, router, e.turnId, response, tier);
|
|
895
|
-
}
|
|
1152
|
+
if (!next.signal.aborted && (await $.session.id()) === sessionId)
|
|
1153
|
+
await observeRouted($, router, { turnId: e.turnId, cfg, loop, loopVersion, request, response, nativeModel });
|
|
896
1154
|
return response;
|
|
897
1155
|
});
|
|
898
1156
|
|
|
@@ -907,10 +1165,15 @@ export function register(on, options) {
|
|
|
907
1165
|
}
|
|
908
1166
|
router.prompts.delete(e.turnId);
|
|
909
1167
|
for (const key of router.decisions.keys()) if (key.includes(`:${e.turnId}:`)) router.decisions.delete(key);
|
|
910
|
-
// `off` asks nothing, so it records nothing.
|
|
1168
|
+
// `off` asks nothing, so it records nothing; it only ends the watch after a cheaper activity move.
|
|
911
1169
|
const turn = router.turnActivities.get(e.turnId);
|
|
912
1170
|
router.turnActivities.delete(e.turnId);
|
|
913
|
-
if (turn
|
|
1171
|
+
if (turn?.mode === 'off') {
|
|
1172
|
+
if ((await $.session.id()) === turn.sessionId) router.afterDown = advanceDown(router.afterDown, turn).after;
|
|
1173
|
+
} else if (turn) await recordActivity($, router, turn);
|
|
1174
|
+
const savings = router.turnSavings.get(e.turnId);
|
|
1175
|
+
router.turnSavings.delete(e.turnId);
|
|
1176
|
+
if (savings) await flushSavings($, router, savings);
|
|
914
1177
|
return next(e);
|
|
915
1178
|
});
|
|
916
1179
|
|
|
@@ -932,15 +1195,21 @@ export function register(on, options) {
|
|
|
932
1195
|
router.prompts.clear();
|
|
933
1196
|
router.turnConfigs.clear();
|
|
934
1197
|
router.turnActivities.clear();
|
|
1198
|
+
router.turnSavings.clear();
|
|
935
1199
|
const run = router.run;
|
|
936
1200
|
router.run = null;
|
|
1201
|
+
router.afterDown = null;
|
|
937
1202
|
if (run) await flushRun($, run);
|
|
938
1203
|
router.view = {
|
|
939
1204
|
...initialView(router.view?.nativeModel ?? ''),
|
|
1205
|
+
// The effort carries over /clear until the next turn reads it again, so "your model" keeps its effort.
|
|
1206
|
+
nativeEffort: router.view?.nativeEffort ?? null,
|
|
940
1207
|
mode: 'auto',
|
|
941
1208
|
credentials: router.view?.credentials ?? null,
|
|
942
1209
|
// Counts kept across sessions: the next session starts from them.
|
|
943
1210
|
activityStore: router.view?.activityStore ?? null,
|
|
1211
|
+
activityMetrics: router.view?.activityMetrics ?? null,
|
|
1212
|
+
savingsStore: router.view?.savingsStore ?? null,
|
|
944
1213
|
health: healthOf(clientOf(router, router.config.classifier), router.config.classifier),
|
|
945
1214
|
configPath: router.config.nativePath,
|
|
946
1215
|
phase: router.view?.phase === 'unavailable' ? 'unavailable' : 'ready',
|
|
@@ -1007,6 +1276,9 @@ export function register(on, options) {
|
|
|
1007
1276
|
const modelOptions = Object.keys(router.config.models).filter((alias) =>
|
|
1008
1277
|
isModelAllowed(router.config.models[alias].id, settings.availableModels, view.nativeModel),
|
|
1009
1278
|
);
|
|
1010
|
-
return renderPanel(elements, router.config, view, usage, paneActions($, router, view), {
|
|
1279
|
+
return renderPanel(elements, router.config, view, usage, paneActions($, router, view), {
|
|
1280
|
+
modelOptions,
|
|
1281
|
+
columns: e.props.bodyColumns,
|
|
1282
|
+
});
|
|
1011
1283
|
});
|
|
1012
1284
|
}
|