@alexeiled/claude-router 1.6.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +32 -0
- package/README.md +14 -4
- package/docs/activity-routing.md +254 -0
- package/docs/architecture.md +9 -3
- package/docs/configuration.md +56 -44
- package/docs/evaluation.md +32 -5
- package/docs/native-router.md +16 -13
- package/docs/router-band.svg +78 -80
- package/docs/router-pane-classifier.svg +7 -7
- package/docs/router-pane-now.svg +9 -9
- package/docs/router-pane-routing.svg +73 -87
- package/docs/router-pane-usage.svg +123 -0
- package/docs/user-guide.md +77 -53
- package/hooks/native-router.mjs +215 -36
- package/lib/activity-stats.mjs +1 -1
- package/lib/band.mjs +82 -55
- package/lib/config.mjs +49 -32
- package/lib/cost.mjs +21 -6
- package/lib/display.mjs +68 -4
- package/lib/panel.mjs +350 -64
- package/lib/route.mjs +8 -2
- package/lib/savings.mjs +237 -0
- package/lib/view.mjs +9 -1
- package/package.json +1 -1
- package/types/index.d.ts +32 -0
package/hooks/native-router.mjs
CHANGED
|
@@ -38,7 +38,9 @@ import {
|
|
|
38
38
|
GATEWAY_SETTINGS,
|
|
39
39
|
missingCredentials,
|
|
40
40
|
NOT_A_TIER_REASON,
|
|
41
|
+
savingsLine,
|
|
41
42
|
storeAgreementLine,
|
|
43
|
+
unavailableReason,
|
|
42
44
|
} from '../lib/display.mjs';
|
|
43
45
|
import { clip, observedActivity, promptIndex } from '../lib/facts.mjs';
|
|
44
46
|
import { renderPanel, routeDraftOf, routingChanges } from '../lib/panel.mjs';
|
|
@@ -55,6 +57,19 @@ import {
|
|
|
55
57
|
prepareLoop,
|
|
56
58
|
resetHistory,
|
|
57
59
|
} from '../lib/route.mjs';
|
|
60
|
+
import {
|
|
61
|
+
addReply,
|
|
62
|
+
afterReset,
|
|
63
|
+
compareReply,
|
|
64
|
+
emptySavingsStore,
|
|
65
|
+
emptyTotals,
|
|
66
|
+
mergeSavingsStores,
|
|
67
|
+
readSavingsStore,
|
|
68
|
+
recordSavingsStore,
|
|
69
|
+
SAVINGS_PREFIX,
|
|
70
|
+
SAVINGS_RESET_KEY,
|
|
71
|
+
sessionCacheTtl,
|
|
72
|
+
} from '../lib/savings.mjs';
|
|
58
73
|
import { CLEARED_READINGS, continuationView, healthOf, initialView, responseMetrics } from '../lib/view.mjs';
|
|
59
74
|
|
|
60
75
|
// The engine follows $ only into functions declared in this file, never across an import: every helper that takes $
|
|
@@ -88,6 +103,13 @@ function createRouter(options) {
|
|
|
88
103
|
// one activity, flushed to the store when it ends.
|
|
89
104
|
turnActivities: new Map(),
|
|
90
105
|
run: null,
|
|
106
|
+
// Each turn's routing-vs-your-model totals until turn.complete adds them to the store.
|
|
107
|
+
turnSavings: new Map(),
|
|
108
|
+
// The totals kept across sessions: this session's record (`{ sessionId, record }`), held here so a refresh never
|
|
109
|
+
// reads it back from the store while a write of it is pending, and the sum of every other session's, read on
|
|
110
|
+
// refresh. The view shows their sum.
|
|
111
|
+
savingsOwn: null,
|
|
112
|
+
savingsOthers: null,
|
|
91
113
|
// Bumped by Reset activity stats, so a store write that read the counts before the reset skips.
|
|
92
114
|
statsResets: 0,
|
|
93
115
|
};
|
|
@@ -118,7 +140,8 @@ async function modeOf($, router, fallback = 'auto') {
|
|
|
118
140
|
}
|
|
119
141
|
}
|
|
120
142
|
|
|
121
|
-
// A saved preference wins; a fresh session starts with routing on for a model some
|
|
143
|
+
// A saved preference wins; a fresh session starts with routing on for a model some route runs (a tier's, or in `on` an
|
|
144
|
+
// activity override's), and off for any other.
|
|
122
145
|
async function startMode($, router, model) {
|
|
123
146
|
const mode = await modeOf($, router, cellForModel(router.config, model) === null ? 'manual' : 'auto');
|
|
124
147
|
router.modes.set(await $.session.id(), mode);
|
|
@@ -147,20 +170,50 @@ async function updateView($, router, patch) {
|
|
|
147
170
|
await $.state.set(VIEW, router.view);
|
|
148
171
|
}
|
|
149
172
|
|
|
173
|
+
// This session's record kept across sessions, read from the store once per session, and the last reset (ms) from
|
|
174
|
+
// any session, read each time: a Reset in another session empties a record held from before it.
|
|
175
|
+
async function ownSavings($, router) {
|
|
176
|
+
const sessionId = await $.session.id();
|
|
177
|
+
const resetAt = await $.store.get(SAVINGS_RESET_KEY);
|
|
178
|
+
if (router.savingsOwn?.sessionId !== sessionId) {
|
|
179
|
+
const record = readSavingsStore(await $.store.get(`${SAVINGS_PREFIX}${sessionId}`));
|
|
180
|
+
// Another call may have read it meanwhile and a turn added to it: that one stands.
|
|
181
|
+
if (router.savingsOwn?.sessionId !== sessionId) router.savingsOwn = { sessionId, record };
|
|
182
|
+
}
|
|
183
|
+
router.savingsOwn.record = afterReset(router.savingsOwn.record, resetAt);
|
|
184
|
+
return { own: router.savingsOwn, resetAt };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Every other session's record since the last reset, added up.
|
|
188
|
+
async function otherSavings($, sessionId, resetAt) {
|
|
189
|
+
const own = `${SAVINGS_PREFIX}${sessionId}`;
|
|
190
|
+
const keys = (await $.store.keys()).filter((key) => key.startsWith(SAVINGS_PREFIX) && key !== own);
|
|
191
|
+
return mergeSavingsStores(await Promise.all(keys.map((key) => $.store.get(key))), resetAt);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The view's totals kept across sessions, from what the router holds; nothing until a read succeeded.
|
|
195
|
+
const savedView = (router) =>
|
|
196
|
+
router.savingsOthers ? { savingsStore: mergeSavingsStores([router.savingsOthers, router.savingsOwn?.record]) } : {};
|
|
197
|
+
|
|
150
198
|
// 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
|
-
|
|
199
|
+
// may have added to them. A failed read keeps what the view has; it never breaks a turn or a render. The saved
|
|
200
|
+
// totals are taken from the router after the reads, so a turn that finished meanwhile counts once.
|
|
201
|
+
async function storeView($, router) {
|
|
202
|
+
const out = {};
|
|
153
203
|
try {
|
|
154
|
-
|
|
155
|
-
} catch {
|
|
156
|
-
|
|
157
|
-
|
|
204
|
+
out.activityStore = readStore(await $.store.get(STORE_KEY));
|
|
205
|
+
} catch {}
|
|
206
|
+
try {
|
|
207
|
+
const { own, resetAt } = await ownSavings($, router);
|
|
208
|
+
router.savingsOthers = await otherSavings($, own.sessionId, resetAt);
|
|
209
|
+
} catch {}
|
|
210
|
+
return out;
|
|
158
211
|
}
|
|
159
212
|
|
|
160
213
|
// A notice answers the last press in the pane, so the pane opens without one; the last write keeps its Undo in the
|
|
161
214
|
// status bar.
|
|
162
215
|
async function openPane($, router) {
|
|
163
|
-
await updateView($, router, { notice: null, ...(await storeView(
|
|
216
|
+
await updateView($, router, { notice: null, ...(await storeView($, router)), ...savedView(router) });
|
|
164
217
|
await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
|
|
165
218
|
}
|
|
166
219
|
|
|
@@ -180,7 +233,7 @@ async function changeMode($, router, mode) {
|
|
|
180
233
|
|
|
181
234
|
async function setPin($, router, tier) {
|
|
182
235
|
const view = router.view ?? (await readView($, router));
|
|
183
|
-
if (view.phase === 'unavailable') return
|
|
236
|
+
if (view.phase === 'unavailable') return unavailableText(view);
|
|
184
237
|
const mode = await modeOf($, router, view.mode);
|
|
185
238
|
if (mode === 'manual') return 'Routing is off. Turn routing on before pinning a turn.';
|
|
186
239
|
await updateView($, router, { pendingPin: tier });
|
|
@@ -191,6 +244,8 @@ async function setPin($, router, tier) {
|
|
|
191
244
|
async function routerCommand($, router, args) {
|
|
192
245
|
const [action, value] = args.trim().split(/\s+/);
|
|
193
246
|
if (action === 'auto' || action === 'off') {
|
|
247
|
+
const view = router.view ?? (await readView($, router));
|
|
248
|
+
if (view.phase === 'unavailable') return { text: unavailableText(view) };
|
|
194
249
|
await changeMode($, router, action === 'auto' ? 'auto' : 'manual');
|
|
195
250
|
return { text: action === 'auto' ? 'Routing on.' : 'Routing off: Claude’s model is kept.' };
|
|
196
251
|
}
|
|
@@ -212,7 +267,7 @@ async function routerCommand($, router, args) {
|
|
|
212
267
|
// `/router activities <mode>`: the pane's write, so the file keeps the rest and Undo in the pane restores it.
|
|
213
268
|
async function setActivityRouting($, router, mode) {
|
|
214
269
|
const view = router.view ?? (await readView($, router));
|
|
215
|
-
if (view.phase === 'unavailable') return
|
|
270
|
+
if (view.phase === 'unavailable') return unavailableText(view);
|
|
216
271
|
if (mode === router.config.activityRouting) return `Activity routing is already ${mode}.`;
|
|
217
272
|
await paneActions($, router, view).activityRouting(mode);
|
|
218
273
|
return router.view.notice;
|
|
@@ -291,7 +346,15 @@ async function saveConfig($, path, change) {
|
|
|
291
346
|
}
|
|
292
347
|
}
|
|
293
348
|
|
|
349
|
+
const unavailableText = (view) => `Routing unavailable: ${unavailableReason(view)}. Claude’s model is kept.`;
|
|
350
|
+
|
|
294
351
|
function detailText(config, view) {
|
|
352
|
+
if (view.phase === 'unavailable')
|
|
353
|
+
return [
|
|
354
|
+
unavailableText(view),
|
|
355
|
+
`Model: ${view.nativeModel}`,
|
|
356
|
+
...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP : []),
|
|
357
|
+
].join('\n');
|
|
295
358
|
const missing = missingCredentials(config, view, config.classifier);
|
|
296
359
|
return [
|
|
297
360
|
`Router — routing ${view.mode === 'auto' ? 'on' : 'off'}`,
|
|
@@ -301,13 +364,13 @@ function detailText(config, view) {
|
|
|
301
364
|
`Reason: ${view.reason}`,
|
|
302
365
|
...activityDetailLines(config, view),
|
|
303
366
|
storeAgreementLine(view),
|
|
367
|
+
savingsLine(view),
|
|
304
368
|
view.error || missing ? classifierStatus(config, view.error ?? missing) : `${activeClassifier(config).label} ready`,
|
|
305
369
|
`Context: ${view.contextKnown ? `${view.contextTokens} tokens (estimate)` : 'unknown'}`,
|
|
306
370
|
`Observed cache: ${view.cacheRead ?? 'unknown'} read, ${view.cacheWrite ?? 'unknown'} written tokens`,
|
|
307
371
|
view.pendingPin ? `Next turn pin: ${view.pendingPin}` : 'No next-turn pin.',
|
|
308
372
|
'Routing on picks a model for each turn. Routing off keeps Claude’s model. Pins serve one turn only.',
|
|
309
373
|
'Cache lifetime is an estimate. Claude’s cost ledger owns session totals.',
|
|
310
|
-
...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP : []),
|
|
311
374
|
].join('\n');
|
|
312
375
|
}
|
|
313
376
|
|
|
@@ -345,6 +408,9 @@ async function* passMain($, e, next, loop, version, nativeModel, reason, router)
|
|
|
345
408
|
stopReason: response.stopReason,
|
|
346
409
|
now: Date.now(),
|
|
347
410
|
});
|
|
411
|
+
// Routing chose nothing here, so the reply is not counted; your model's cache still follows it.
|
|
412
|
+
const compared = await compareYours($, router, loop, response, e.effort ?? null, nativeModel);
|
|
413
|
+
if (compared) observed.yours = compared.state;
|
|
348
414
|
const written = await $.state.set(ref, observed, { ifVersion: version });
|
|
349
415
|
if (written.isSet) await updateView($, router, responseMetrics(router.view, response, null));
|
|
350
416
|
}
|
|
@@ -452,9 +518,15 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
452
518
|
|
|
453
519
|
// A routed reply's metrics in the view, labelled with the turn's activity when its tier served it, and added to the
|
|
454
520
|
// turn's activity record.
|
|
455
|
-
async function publishReply($, router, turnId, response, tier) {
|
|
521
|
+
async function publishReply($, router, turnId, response, tier, route) {
|
|
456
522
|
const turn = router.turnActivities.get(turnId);
|
|
457
|
-
const metrics = responseMetrics(
|
|
523
|
+
const metrics = responseMetrics(
|
|
524
|
+
router.view,
|
|
525
|
+
response,
|
|
526
|
+
tier,
|
|
527
|
+
tier ? (turn?.label ?? null) : null,
|
|
528
|
+
tier ? route : null,
|
|
529
|
+
);
|
|
458
530
|
await updateView($, router, metrics);
|
|
459
531
|
if (turn)
|
|
460
532
|
router.turnActivities.set(turnId, {
|
|
@@ -465,6 +537,91 @@ async function publishReply($, router, turnId, response, tier) {
|
|
|
465
537
|
});
|
|
466
538
|
}
|
|
467
539
|
|
|
540
|
+
// A routed reply written to the loop at `loopVersion`, then published: the view's metrics and the routing-vs-your-model
|
|
541
|
+
// totals. A lost write publishes nothing.
|
|
542
|
+
async function observeRouted($, router, { turnId, cfg, loop, loopVersion, request, response, nativeModel }) {
|
|
543
|
+
const observed = observeResponse(loop, {
|
|
544
|
+
usage: response.usage,
|
|
545
|
+
requestedModel: request.model,
|
|
546
|
+
effort: request.effort ?? null,
|
|
547
|
+
stopReason: response.stopReason,
|
|
548
|
+
now: Date.now(),
|
|
549
|
+
});
|
|
550
|
+
if (response.stopReason === null) observed.suspended = true;
|
|
551
|
+
const compared = await compareYours($, router, loop, response, request.effort ?? null, nativeModel, cfg);
|
|
552
|
+
if (compared) observed.yours = compared.state;
|
|
553
|
+
const written = await $.state.set({ ...LOOP, id: 'main' }, observed, { ifVersion: loopVersion });
|
|
554
|
+
if (!written.isSet) return;
|
|
555
|
+
// A substituted reply is not the tier's: the strip and trend must not count it as one.
|
|
556
|
+
const tier = isSameModel(request.model, response.usage?.model) ? loop.decision.tier : null;
|
|
557
|
+
await publishReply($, router, turnId, response, tier, `${request.model}@${request.effort ?? 'session'}`);
|
|
558
|
+
if (compared?.reply) await addSavings($, router, turnId, compared.reply, tier);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// The main conversation's prompt-cache lifetime by Claude Code's rule (sessionCacheTtl), or null when a reading
|
|
562
|
+
// fails. $.env.get takes literal names only. A plan subscriber is one whose replies report plan rate limits; a usage
|
|
563
|
+
// without them is not one.
|
|
564
|
+
async function cacheTtlOf($) {
|
|
565
|
+
try {
|
|
566
|
+
const settings = await $.settings.read();
|
|
567
|
+
const usage = await $.session.usage();
|
|
568
|
+
return sessionCacheTtl({
|
|
569
|
+
force5m: await $.env.get('FORCE_PROMPT_CACHING_5M'),
|
|
570
|
+
envTtl: await $.env.get('CLAUDE_CODE_PROMPT_CACHE_TTL'),
|
|
571
|
+
settingTtl: settings.promptCacheTtl,
|
|
572
|
+
enable1h: await $.env.get('ENABLE_PROMPT_CACHING_1H'),
|
|
573
|
+
subscriber: (usage?.rateLimits ?? []).some((limit) => limit.kind === 'five_hour' || limit.kind === 'seven_day'),
|
|
574
|
+
});
|
|
575
|
+
} catch {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// A main reply against your model (compareReply): the loop's next `yours` state and the reply's prices, or null. The
|
|
581
|
+
// readout never breaks a turn. A reply whose cache lifetime cannot be read still moves your model's cache and is not
|
|
582
|
+
// counted; any other failure leaves the loop's state and the totals as they were.
|
|
583
|
+
async function compareYours($, router, loop, response, servedEffort, nativeModel, cfg = router.config) {
|
|
584
|
+
try {
|
|
585
|
+
if (!response.usage?.model) return null;
|
|
586
|
+
const view = router.view ?? (await readView($, router));
|
|
587
|
+
return compareReply(cfg, loop.yours ?? null, {
|
|
588
|
+
usage: response.usage,
|
|
589
|
+
served: { model: response.usage.model, effort: servedEffort },
|
|
590
|
+
yours: { model: nativeModel, effort: view.nativeEffort ?? null },
|
|
591
|
+
ttl: await cacheTtlOf($),
|
|
592
|
+
now: Date.now(),
|
|
593
|
+
});
|
|
594
|
+
} catch {
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// A routed reply's prices added to the session totals in the view and to the turn's totals for the store.
|
|
600
|
+
async function addSavings($, router, turnId, reply, tier) {
|
|
601
|
+
try {
|
|
602
|
+
const view = router.view ?? (await readView($, router));
|
|
603
|
+
router.turnSavings.set(turnId, addReply(router.turnSavings.get(turnId) ?? emptyTotals(), reply, tier));
|
|
604
|
+
await updateView($, router, { savings: addReply(view.savings ?? emptyTotals(), reply, tier) });
|
|
605
|
+
} catch {}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// A finished turn's totals added to this session's record kept across sessions, with the guard recordActivity uses:
|
|
609
|
+
// totals read before a reset are not written back. The record is added to in memory before the write, so a refresh
|
|
610
|
+
// during the write counts the turn once.
|
|
611
|
+
async function flushSavings($, router, totals) {
|
|
612
|
+
try {
|
|
613
|
+
const resets = router.statsResets;
|
|
614
|
+
const { own, resetAt } = await ownSavings($, router);
|
|
615
|
+
// The others held from before a reset in another session are read again.
|
|
616
|
+
if (!router.savingsOthers || afterReset(router.savingsOthers, resetAt) !== router.savingsOthers)
|
|
617
|
+
router.savingsOthers = await otherSavings($, own.sessionId, resetAt);
|
|
618
|
+
if (router.statsResets !== resets) return;
|
|
619
|
+
own.record = recordSavingsStore(own.record, totals, Date.now());
|
|
620
|
+
await $.store.set(`${SAVINGS_PREFIX}${own.sessionId}`, own.record);
|
|
621
|
+
if (router.statsResets === resets) await updateView($, router, savedView(router));
|
|
622
|
+
} catch {}
|
|
623
|
+
}
|
|
624
|
+
|
|
468
625
|
// A finished turn's activity stats: the session counts in the view and the counts kept across sessions. Stats never
|
|
469
626
|
// break a turn: a failed read or write loses this turn's counts only.
|
|
470
627
|
async function recordActivity($, router, turn) {
|
|
@@ -671,17 +828,39 @@ function paneActions($, router, view) {
|
|
|
671
828
|
withActivities(file, { base: { activityRouting: from }, activityRouting: mode }),
|
|
672
829
|
);
|
|
673
830
|
},
|
|
674
|
-
|
|
831
|
+
// One reset for both readouts: activity stats and routing vs your model, this session's and the saved ones.
|
|
832
|
+
resetStats: async () => {
|
|
675
833
|
router.statsResets += 1;
|
|
676
834
|
router.run = null;
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
835
|
+
router.turnSavings.clear();
|
|
836
|
+
const clear = (key, value) =>
|
|
837
|
+
$.store
|
|
838
|
+
.set(key, value)
|
|
839
|
+
.then(() => true)
|
|
840
|
+
.catch(() => false);
|
|
841
|
+
const clearSavings = async () => {
|
|
842
|
+
// Written first, so a session still holding its record from before the reset drops it, unless that record's
|
|
843
|
+
// first reply has the reset's millisecond or the clock moved back across the reset.
|
|
844
|
+
await $.store.set(SAVINGS_RESET_KEY, Date.now());
|
|
845
|
+
const keys = (await $.store.keys()).filter((key) => key.startsWith(SAVINGS_PREFIX));
|
|
846
|
+
await Promise.all(keys.map((key) => $.store.delete(key)));
|
|
847
|
+
router.savingsOwn = null;
|
|
848
|
+
router.savingsOthers = emptySavingsStore();
|
|
849
|
+
return true;
|
|
850
|
+
};
|
|
851
|
+
const [activity, savings] = await Promise.all([
|
|
852
|
+
clear(STORE_KEY, emptyStore()),
|
|
853
|
+
clearSavings().catch(() => false),
|
|
854
|
+
]);
|
|
681
855
|
await updateView($, router, {
|
|
682
856
|
activityStats: null,
|
|
683
|
-
|
|
684
|
-
|
|
857
|
+
savings: null,
|
|
858
|
+
...(activity ? { activityStore: emptyStore() } : {}),
|
|
859
|
+
...(savings ? { savingsStore: emptySavingsStore() } : {}),
|
|
860
|
+
notice:
|
|
861
|
+
activity && savings
|
|
862
|
+
? 'Stats reset: activity and routing vs your model, this session and saved.'
|
|
863
|
+
: 'This session’s stats reset. Saved stats could not be cleared.',
|
|
685
864
|
});
|
|
686
865
|
},
|
|
687
866
|
discardRouting: () => updateView($, router, { routeDraft: null, tuning: null, tuningBase: null, notice: null }),
|
|
@@ -773,7 +952,8 @@ export function register(on, options) {
|
|
|
773
952
|
routeDraft: null,
|
|
774
953
|
lastWrite: null,
|
|
775
954
|
bandDetail: (await $.store.get(BAND_DETAIL).catch(() => false)) === true,
|
|
776
|
-
...(await storeView(
|
|
955
|
+
...(await storeView($, router)),
|
|
956
|
+
...savedView(router),
|
|
777
957
|
});
|
|
778
958
|
} catch (error) {
|
|
779
959
|
await updateView($, router, {
|
|
@@ -823,6 +1003,7 @@ export function register(on, options) {
|
|
|
823
1003
|
const saved = await rememberMode($, mode);
|
|
824
1004
|
await updateView($, router, {
|
|
825
1005
|
mode,
|
|
1006
|
+
nativeEffort: e.effort ?? null,
|
|
826
1007
|
...(!saved ? { notice: 'Resume preference could not be saved. Current mode remains active.' } : {}),
|
|
827
1008
|
});
|
|
828
1009
|
}
|
|
@@ -879,20 +1060,8 @@ export function register(on, options) {
|
|
|
879
1060
|
if (loop.decision.effort === null) delete request.effort;
|
|
880
1061
|
else request.effort = loop.decision.effort;
|
|
881
1062
|
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
|
-
}
|
|
1063
|
+
if (!next.signal.aborted && (await $.session.id()) === sessionId)
|
|
1064
|
+
await observeRouted($, router, { turnId: e.turnId, cfg, loop, loopVersion, request, response, nativeModel });
|
|
896
1065
|
return response;
|
|
897
1066
|
});
|
|
898
1067
|
|
|
@@ -911,6 +1080,9 @@ export function register(on, options) {
|
|
|
911
1080
|
const turn = router.turnActivities.get(e.turnId);
|
|
912
1081
|
router.turnActivities.delete(e.turnId);
|
|
913
1082
|
if (turn && turn.mode !== 'off') await recordActivity($, router, turn);
|
|
1083
|
+
const savings = router.turnSavings.get(e.turnId);
|
|
1084
|
+
router.turnSavings.delete(e.turnId);
|
|
1085
|
+
if (savings) await flushSavings($, router, savings);
|
|
914
1086
|
return next(e);
|
|
915
1087
|
});
|
|
916
1088
|
|
|
@@ -932,15 +1104,19 @@ export function register(on, options) {
|
|
|
932
1104
|
router.prompts.clear();
|
|
933
1105
|
router.turnConfigs.clear();
|
|
934
1106
|
router.turnActivities.clear();
|
|
1107
|
+
router.turnSavings.clear();
|
|
935
1108
|
const run = router.run;
|
|
936
1109
|
router.run = null;
|
|
937
1110
|
if (run) await flushRun($, run);
|
|
938
1111
|
router.view = {
|
|
939
1112
|
...initialView(router.view?.nativeModel ?? ''),
|
|
1113
|
+
// The effort carries over /clear until the next turn reads it again, so "your model" keeps its effort.
|
|
1114
|
+
nativeEffort: router.view?.nativeEffort ?? null,
|
|
940
1115
|
mode: 'auto',
|
|
941
1116
|
credentials: router.view?.credentials ?? null,
|
|
942
1117
|
// Counts kept across sessions: the next session starts from them.
|
|
943
1118
|
activityStore: router.view?.activityStore ?? null,
|
|
1119
|
+
savingsStore: router.view?.savingsStore ?? null,
|
|
944
1120
|
health: healthOf(clientOf(router, router.config.classifier), router.config.classifier),
|
|
945
1121
|
configPath: router.config.nativePath,
|
|
946
1122
|
phase: router.view?.phase === 'unavailable' ? 'unavailable' : 'ready',
|
|
@@ -1007,6 +1183,9 @@ export function register(on, options) {
|
|
|
1007
1183
|
const modelOptions = Object.keys(router.config.models).filter((alias) =>
|
|
1008
1184
|
isModelAllowed(router.config.models[alias].id, settings.availableModels, view.nativeModel),
|
|
1009
1185
|
);
|
|
1010
|
-
return renderPanel(elements, router.config, view, usage, paneActions($, router, view), {
|
|
1186
|
+
return renderPanel(elements, router.config, view, usage, paneActions($, router, view), {
|
|
1187
|
+
modelOptions,
|
|
1188
|
+
columns: e.props.bodyColumns,
|
|
1189
|
+
});
|
|
1011
1190
|
});
|
|
1012
1191
|
}
|
package/lib/activity-stats.mjs
CHANGED
|
@@ -208,7 +208,7 @@ export function turnActivity(config, advice, previous, decision) {
|
|
|
208
208
|
};
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
// The activities plan §11
|
|
211
|
+
// The activities whose agreement plan §11 tracks: the costly mistake is a cheap route on a coding turn.
|
|
212
212
|
const FOCUS = ['code', 'ops', 'explore'];
|
|
213
213
|
|
|
214
214
|
// The counts kept across sessions as the Usage tab and /router show them. Agreement covers the rows that name an
|
package/lib/band.mjs
CHANGED
|
@@ -3,21 +3,27 @@ import {
|
|
|
3
3
|
classifierLabel,
|
|
4
4
|
classifierStatus,
|
|
5
5
|
classifierWarns,
|
|
6
|
-
|
|
6
|
+
differenceShare,
|
|
7
7
|
modelName,
|
|
8
8
|
NOT_A_TIER_REASON,
|
|
9
9
|
percent,
|
|
10
10
|
reasonActivity,
|
|
11
11
|
routeLabel,
|
|
12
|
+
SAVINGS_COLOR,
|
|
12
13
|
shortReason,
|
|
14
|
+
signedUsd,
|
|
13
15
|
switchCount,
|
|
14
16
|
TIER_COLOR,
|
|
17
|
+
unavailableReason,
|
|
15
18
|
usageMetrics,
|
|
16
19
|
} from './display.mjs';
|
|
17
20
|
import { isSameModel } from './route.mjs';
|
|
21
|
+
import { emptyTotals, readout } from './savings.mjs';
|
|
18
22
|
|
|
19
23
|
const BARS = '▂▄▆█';
|
|
20
24
|
const SEPARATOR = ' · ';
|
|
25
|
+
// The hover row's controls sit apart without the dot: they are buttons, not readings.
|
|
26
|
+
const GAP = ' ';
|
|
21
27
|
// Space the band keeps for the trailing Router button.
|
|
22
28
|
const ROUTER_BUTTON = 8;
|
|
23
29
|
const STRIP = 20;
|
|
@@ -33,15 +39,15 @@ const button = (props) => ({ button: props });
|
|
|
33
39
|
// drop from inside a segment it shares no separator with.
|
|
34
40
|
const segment = (priority, ...parts) => ({ priority, parts: parts.flat().filter(Boolean) });
|
|
35
41
|
const partWidth = (p) => (p.button ? p.button.label.length + (p.button.plain ? 0 : 4) : p.text.length);
|
|
36
|
-
const lineWidth = (segments) =>
|
|
37
|
-
segments.reduce((sum, s, i) => sum + (i ?
|
|
42
|
+
const lineWidth = (segments, separator) =>
|
|
43
|
+
segments.reduce((sum, s, i) => sum + (i ? separator.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
|
|
38
44
|
|
|
39
45
|
// Drops the highest-priority segments and parts, later ones first on a tie, until the line fits `columns`. If the
|
|
40
46
|
// priority-0 segments alone are too wide, the later ones go too, and the first is cut with an ellipsis: Ink would
|
|
41
47
|
// otherwise shrink every Text in the row and garble it.
|
|
42
|
-
export function fitSegments(segments, columns) {
|
|
48
|
+
export function fitSegments(segments, columns, separator = SEPARATOR) {
|
|
43
49
|
const kept = [...segments];
|
|
44
|
-
while (lineWidth(kept) > columns) {
|
|
50
|
+
while (lineWidth(kept, separator) > columns) {
|
|
45
51
|
let drop = null;
|
|
46
52
|
kept.forEach((s, i) => {
|
|
47
53
|
s.parts.forEach((p, j) => {
|
|
@@ -56,7 +62,7 @@ export function fitSegments(segments, columns) {
|
|
|
56
62
|
else if (kept.length > 1) kept.pop();
|
|
57
63
|
else break;
|
|
58
64
|
}
|
|
59
|
-
const overflow = lineWidth(kept) - columns;
|
|
65
|
+
const overflow = lineWidth(kept, separator) - columns;
|
|
60
66
|
if (overflow > 0 && kept.length) {
|
|
61
67
|
const parts = [...kept[0].parts];
|
|
62
68
|
const last = parts.findLastIndex((p) => !p.button && p.text.length > overflow);
|
|
@@ -75,10 +81,6 @@ function meter(tier, dim = false) {
|
|
|
75
81
|
// The classifier is working: every bar in the accent colour.
|
|
76
82
|
const pendingMeter = () => [...BARS].map((bar) => part(bar, { color: COLOR.accent }));
|
|
77
83
|
|
|
78
|
-
// Turns routing off: the one control in Auto that pauses it.
|
|
79
|
-
const routingOffButton = (actions, variant) =>
|
|
80
|
-
button({ key: 'band-manual-main', label: 'Routing off', variant, onPress: () => actions.mode('manual') });
|
|
81
|
-
|
|
82
84
|
const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
|
|
83
85
|
|
|
84
86
|
// The turn's activity between the tier and the route, in neutral ink so the tier keeps its color; dim and marked in
|
|
@@ -105,31 +107,30 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
105
107
|
if (view.phase === 'unavailable')
|
|
106
108
|
return [
|
|
107
109
|
segment(0, part('✕ Routing unavailable', { color: 'red' })),
|
|
108
|
-
segment(
|
|
109
|
-
2,
|
|
110
|
-
part(view.error === GATEWAY_SETTINGS ? 'v0.8 gateway settings remain' : (view.error ?? 'unknown error'), {
|
|
111
|
-
color: 'red',
|
|
112
|
-
}),
|
|
113
|
-
),
|
|
110
|
+
segment(2, part(unavailableReason(view), { color: 'red' })),
|
|
114
111
|
segment(1, button({ key: 'band-fix', label: 'Fix', onPress: actions.open })),
|
|
115
112
|
];
|
|
113
|
+
// The state and Turn on lead in the first segment, which never drops, and a cut keeps buttons whole: the band's one
|
|
114
|
+
// way back on stays at any width. The model and why go first, then the word Routing.
|
|
116
115
|
if (view.mode === 'manual')
|
|
117
116
|
return [
|
|
118
117
|
segment(
|
|
119
118
|
0,
|
|
120
119
|
part('○ ', { dimColor: true }),
|
|
121
|
-
part('Routing '),
|
|
120
|
+
{ ...part('Routing '), priority: 1 },
|
|
122
121
|
part('off', { backgroundColor: COLOR.off, color: COLOR.muted }),
|
|
123
|
-
part(
|
|
124
|
-
|
|
122
|
+
part(' '),
|
|
123
|
+
button({ key: 'band-auto', label: 'Turn on', variant: 'primary', onPress: () => actions.mode('auto') }),
|
|
125
124
|
),
|
|
126
|
-
view.reason === NOT_A_TIER_REASON
|
|
127
|
-
? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
|
|
128
|
-
: null,
|
|
129
125
|
segment(
|
|
130
126
|
1,
|
|
131
|
-
|
|
127
|
+
{ ...part('every turn uses '), priority: 3 },
|
|
128
|
+
part(modelName(view.nativeModel)),
|
|
129
|
+
view.reason === 'model selected manually' ? { ...part(' (/model)', { dimColor: true }), priority: 2 } : null,
|
|
132
130
|
),
|
|
131
|
+
view.reason === NOT_A_TIER_REASON
|
|
132
|
+
? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
|
|
133
|
+
: null,
|
|
133
134
|
].filter(Boolean);
|
|
134
135
|
if (view.phase === 'choosing')
|
|
135
136
|
return [
|
|
@@ -151,11 +152,7 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
151
152
|
)
|
|
152
153
|
: null;
|
|
153
154
|
if (!view.tier && view.phase === 'ready' && !view.actualModel)
|
|
154
|
-
return [
|
|
155
|
-
segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })),
|
|
156
|
-
segment(1, routingOffButton(actions, 'secondary')),
|
|
157
|
-
pin,
|
|
158
|
-
].filter(Boolean);
|
|
155
|
+
return [segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })), pin].filter(Boolean);
|
|
159
156
|
const main = segment(
|
|
160
157
|
0,
|
|
161
158
|
view.tier ? [...meter(view.tier), pill(view.tier, TIER_COLOR[view.tier])] : part('○ '),
|
|
@@ -180,7 +177,6 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
180
177
|
const support = supportText(config, view.estimate);
|
|
181
178
|
return [
|
|
182
179
|
main,
|
|
183
|
-
segment(1, routingOffButton(actions, 'secondary')),
|
|
184
180
|
pin,
|
|
185
181
|
segment(1, part(shortReason(view.reason, reasonActivity(config, view)), { dimColor: true })),
|
|
186
182
|
support ? segment(4, part(support, { dimColor: true })) : null,
|
|
@@ -190,13 +186,34 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
190
186
|
].filter(Boolean);
|
|
191
187
|
}
|
|
192
188
|
|
|
193
|
-
// The
|
|
194
|
-
|
|
189
|
+
// The session against your model, once it is past "too early": "vs your model −22% (−$1.84, list prices)", or the
|
|
190
|
+
// extra with what bought it, "+$2.48 (stronger models, list prices)". Null before that.
|
|
191
|
+
export function savingsParts(view) {
|
|
192
|
+
const r = readout(view.savings ?? emptyTotals());
|
|
193
|
+
if (r.early) return null;
|
|
194
|
+
const share = differenceShare(r);
|
|
195
|
+
const [shown, note] =
|
|
196
|
+
r.differenceUsd < 0
|
|
197
|
+
? [share ?? signedUsd(r.differenceUsd), `${signedUsd(r.differenceUsd)}, list prices`]
|
|
198
|
+
: r.differenceUsd > 0
|
|
199
|
+
? [
|
|
200
|
+
signedUsd(r.differenceUsd),
|
|
201
|
+
`${r.strongerUsd >= r.switchUsd ? 'stronger models' : 'switch cache writes'}, list prices`,
|
|
202
|
+
]
|
|
203
|
+
: ['$0.00', 'list prices'];
|
|
204
|
+
const color = r.differenceUsd < 0 ? SAVINGS_COLOR.less : r.differenceUsd > 0 ? SAVINGS_COLOR.more : undefined;
|
|
205
|
+
return [part('vs your model '), part(shown, color ? { color } : {}), part(` (${note})`, { dimColor: true })];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The second row of the detailed band: recent replies by tier, the session's switch economics, and the session
|
|
209
|
+
// against your model when the row has room for it.
|
|
210
|
+
function detailRow(config, view, usage, columns) {
|
|
195
211
|
const tiers = (view.tiers ?? []).slice(-STRIP);
|
|
196
212
|
if (!tiers.length) return [part('no replies yet', { dimColor: true })];
|
|
197
|
-
const switches = switchCount(tiers);
|
|
213
|
+
const switches = switchCount((view.routes ?? []).slice(-tiers.length));
|
|
198
214
|
const metrics = usageMetrics(config, view, usage);
|
|
199
|
-
|
|
215
|
+
const savings = savingsParts(view);
|
|
216
|
+
const row = [
|
|
200
217
|
part('replies ', { dimColor: true }),
|
|
201
218
|
...tiers.map((tier) => (tier ? part('█', { color: TIER_COLOR[tier] }) : part('·', { dimColor: true }))),
|
|
202
219
|
part(
|
|
@@ -206,6 +223,35 @@ function detailRow(config, view, usage) {
|
|
|
206
223
|
{ dimColor: true },
|
|
207
224
|
),
|
|
208
225
|
];
|
|
226
|
+
const tail = savings ? [part(SEPARATOR, { dimColor: true }), ...savings] : [];
|
|
227
|
+
const width = (parts) => parts.reduce((n, p) => n + partWidth(p), 0);
|
|
228
|
+
return width(row) + width(tail) <= columns ? [...row, ...tail] : row;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The row revealed under the pointer: pins and the mode control while routing is on, and the one- or two-row choice.
|
|
232
|
+
// No digit hotkeys, since a bare digit in an empty prompt would press them. When the row is narrow, the routing on
|
|
233
|
+
// label goes first, then 2 rows, then the pin label, then the pins. Turn off stays, the band's one way to turn
|
|
234
|
+
// routing off, and so does 1 row, the only way back from a second row too wide for the band.
|
|
235
|
+
function hoverSegments(view, actions, routing) {
|
|
236
|
+
const pins = TIERS.flatMap((tier, i) => [
|
|
237
|
+
i ? part(' ') : null,
|
|
238
|
+
button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
|
|
239
|
+
]);
|
|
240
|
+
return [
|
|
241
|
+
routing ? segment(1, { ...part('pin next turn ', { dimColor: true }), priority: 2 }, pins) : null,
|
|
242
|
+
// The state, then what a press does. The state goes first: the meter on the row above already says routing is on.
|
|
243
|
+
routing
|
|
244
|
+
? segment(
|
|
245
|
+
0,
|
|
246
|
+
{ ...part('routing on ', { dimColor: true }), priority: 4 },
|
|
247
|
+
button({ key: 'band-manual', label: 'Turn off', onPress: () => actions.mode('manual') }),
|
|
248
|
+
)
|
|
249
|
+
: null,
|
|
250
|
+
segment(
|
|
251
|
+
view.bandDetail ? 0 : 3,
|
|
252
|
+
button({ key: 'band-detail', label: view.bandDetail ? '1 row' : '2 rows', onPress: actions.toggleDetail }),
|
|
253
|
+
),
|
|
254
|
+
].filter(Boolean);
|
|
209
255
|
}
|
|
210
256
|
|
|
211
257
|
export function renderBand({ Box, Text, Button }, config, view, usage, { columns, agentId }, actions) {
|
|
@@ -216,36 +262,17 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
|
|
|
216
262
|
const line = kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
|
|
217
263
|
line.push(part(' '), button({ key: 'details', label: 'Router', plain: true, onPress: actions.open }));
|
|
218
264
|
const routing = view.mode === 'auto' && view.phase !== 'unavailable';
|
|
265
|
+
const hover = fitSegments(hoverSegments(view, actions, routing), columns, GAP);
|
|
219
266
|
return Box({
|
|
220
267
|
key: 'router-band',
|
|
221
268
|
flexDirection: 'column',
|
|
222
269
|
children: [
|
|
223
270
|
row(line),
|
|
224
|
-
view.bandDetail && routing ? row(detailRow(config, view, usage)) : null,
|
|
225
|
-
// Revealed while the pointer is over the band; no digit hotkeys, since a bare digit in an
|
|
226
|
-
// empty prompt would press them.
|
|
271
|
+
view.bandDetail && routing ? row(detailRow(config, view, usage, columns)) : null,
|
|
227
272
|
Box({
|
|
228
273
|
display: 'none',
|
|
229
274
|
hover: { display: 'flex' },
|
|
230
|
-
children: [
|
|
231
|
-
Text({ dimColor: true, children: routing ? 'pin next turn ' : '' }),
|
|
232
|
-
...(routing
|
|
233
|
-
? TIERS.flatMap((tier) => [
|
|
234
|
-
Button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
|
|
235
|
-
Text({ children: ' ' }),
|
|
236
|
-
])
|
|
237
|
-
: []),
|
|
238
|
-
Text({ children: routing ? ' ' : '' }),
|
|
239
|
-
routing
|
|
240
|
-
? Button({ key: 'band-manual', label: 'Routing off', onPress: () => actions.mode('manual') })
|
|
241
|
-
: Button({ key: 'band-auto-row', label: 'Routing on', onPress: () => actions.mode('auto') }),
|
|
242
|
-
Text({ children: ' ' }),
|
|
243
|
-
Button({
|
|
244
|
-
key: 'band-detail',
|
|
245
|
-
label: view.bandDetail ? '1 row' : '2 rows',
|
|
246
|
-
onPress: actions.toggleDetail,
|
|
247
|
-
}),
|
|
248
|
-
],
|
|
275
|
+
children: hover.flatMap((s, i) => (i ? [part(GAP), ...s.parts] : s.parts)).map(draw),
|
|
249
276
|
}),
|
|
250
277
|
],
|
|
251
278
|
});
|