@alexeiled/claude-router 1.11.0 → 1.13.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 +34 -0
- package/README.md +1 -1
- package/docs/activity-routing.md +3 -3
- package/docs/architecture.md +4 -4
- package/docs/evaluation.md +7 -7
- package/docs/native-router.md +6 -6
- package/docs/router-pane-now.svg +111 -75
- package/docs/router-pane-usage-whatif.svg +128 -0
- package/docs/router-pane-usage.svg +100 -87
- package/docs/user-guide.md +47 -21
- package/hooks/native-router.mjs +170 -45
- package/lib/display.mjs +5 -9
- package/lib/panel.mjs +719 -436
- package/lib/savings.mjs +8 -23
- package/lib/usage.mjs +399 -0
- package/lib/view.mjs +61 -11
- package/package.json +1 -1
- package/types/index.d.ts +45 -11
package/hooks/native-router.mjs
CHANGED
|
@@ -72,16 +72,14 @@ import {
|
|
|
72
72
|
} from '../lib/route.mjs';
|
|
73
73
|
import {
|
|
74
74
|
addReplies,
|
|
75
|
-
addReply,
|
|
76
75
|
afterReset,
|
|
77
76
|
compareModels,
|
|
78
77
|
emptySavingsStore,
|
|
79
|
-
emptyTotals,
|
|
80
78
|
mergeSavingsStores,
|
|
81
79
|
modelAlias,
|
|
82
80
|
modelEntry,
|
|
81
|
+
modelSpec,
|
|
83
82
|
readSavingsStore,
|
|
84
|
-
recordSavingsStore,
|
|
85
83
|
SAVINGS_PREFIX,
|
|
86
84
|
SAVINGS_RESET_KEY,
|
|
87
85
|
sessionCacheTtl,
|
|
@@ -106,7 +104,26 @@ import {
|
|
|
106
104
|
subagentsAfterReset,
|
|
107
105
|
typeRoute,
|
|
108
106
|
} from '../lib/subagents.mjs';
|
|
109
|
-
import {
|
|
107
|
+
import {
|
|
108
|
+
emptyUsage,
|
|
109
|
+
mergeUsage,
|
|
110
|
+
PERIOD_KEY,
|
|
111
|
+
PERIODS,
|
|
112
|
+
prunedUsage,
|
|
113
|
+
readUsage,
|
|
114
|
+
recordUsage,
|
|
115
|
+
replyEntry,
|
|
116
|
+
USAGE_PREFIX,
|
|
117
|
+
usageAfterReset,
|
|
118
|
+
} from '../lib/usage.mjs';
|
|
119
|
+
import {
|
|
120
|
+
CLEARED_READINGS,
|
|
121
|
+
continuationView,
|
|
122
|
+
healthOf,
|
|
123
|
+
initialView,
|
|
124
|
+
responseMetrics,
|
|
125
|
+
switchView,
|
|
126
|
+
} from '../lib/view.mjs';
|
|
110
127
|
|
|
111
128
|
// The engine follows $ only into functions declared in this file, never across an import: every helper that takes $
|
|
112
129
|
// lives here, and the pure parts live in lib/.
|
|
@@ -121,9 +138,21 @@ const isSet = (value) => /^(1|true|yes|on)$/i.test(String(value ?? '').trim());
|
|
|
121
138
|
|
|
122
139
|
// The counts kept across sessions: each session writes only its own record of a kind, `prefix` and its session id,
|
|
123
140
|
// because $.store has no atomic update; the view's `field` shows every session's record since the last reset added
|
|
124
|
-
// up. One marker, SAVINGS_RESET_KEY, resets them all.
|
|
125
|
-
// as another session's record until a reset deletes
|
|
141
|
+
// up, and `ownField`, where a kind has one, this session's alone. One marker, SAVINGS_RESET_KEY, resets them all.
|
|
142
|
+
// `legacy` is the key all sessions shared before 1.10.3: it counts as another session's record until a reset deletes
|
|
143
|
+
// it. Another session's records that `prune` names are deleted when read. `savings` is no longer written: its records
|
|
144
|
+
// are the undated totals from before `usage`, which the All period adds.
|
|
126
145
|
const LEDGERS = {
|
|
146
|
+
usage: {
|
|
147
|
+
prefix: USAGE_PREFIX,
|
|
148
|
+
field: 'usageStore',
|
|
149
|
+
ownField: 'usageSession',
|
|
150
|
+
read: readUsage,
|
|
151
|
+
afterReset: usageAfterReset,
|
|
152
|
+
merge: mergeUsage,
|
|
153
|
+
empty: emptyUsage,
|
|
154
|
+
prune: prunedUsage,
|
|
155
|
+
},
|
|
127
156
|
savings: {
|
|
128
157
|
prefix: SAVINGS_PREFIX,
|
|
129
158
|
field: 'savingsStore',
|
|
@@ -187,8 +216,8 @@ function createRouter(options) {
|
|
|
187
216
|
afterDown: null,
|
|
188
217
|
// Counts every switch to routing off; a turn compares it with its own start to see routing went off meanwhile.
|
|
189
218
|
offSwitches: 0,
|
|
190
|
-
// Each turn's
|
|
191
|
-
|
|
219
|
+
// Each turn's routed replies (replyEntry) until turn.complete adds them to the session's usage record.
|
|
220
|
+
turnUsage: new Map(),
|
|
192
221
|
// The counts kept across sessions, by LEDGERS kind: this session's record (`own`, `{ sessionId, record }`), held
|
|
193
222
|
// here so a refresh never reads it back from the store while a write of it is pending, and the sum of every other
|
|
194
223
|
// session's (`others`), read on refresh. The view shows their sum.
|
|
@@ -251,6 +280,12 @@ async function startView($, router, model) {
|
|
|
251
280
|
return fresh && mode === 'manual' ? { mode, reason: NOT_A_TIER_REASON } : { mode };
|
|
252
281
|
}
|
|
253
282
|
|
|
283
|
+
// The Usage tab's period as last chosen in any session; Session when none was or the store cannot be read.
|
|
284
|
+
async function periodOf($) {
|
|
285
|
+
const saved = await $.store.get(PERIOD_KEY).catch(() => null);
|
|
286
|
+
return PERIODS.includes(saved) ? saved : 'session';
|
|
287
|
+
}
|
|
288
|
+
|
|
254
289
|
async function rememberMode($, mode) {
|
|
255
290
|
try {
|
|
256
291
|
await $.store.set(`${MODE_PREFIX}${await $.session.id()}`, mode);
|
|
@@ -288,10 +323,36 @@ async function ownRecord($, router, kind) {
|
|
|
288
323
|
|
|
289
324
|
// Every other session's record of `kind` since the last reset, added up and held. `keys` is one $.store.keys() listing.
|
|
290
325
|
async function readOthers($, router, kind, { sessionId, resetAt, keys }) {
|
|
291
|
-
const { prefix, legacy, merge } = LEDGERS[kind];
|
|
326
|
+
const { prefix, legacy, merge, prune } = LEDGERS[kind];
|
|
292
327
|
const own = `${prefix}${sessionId}`;
|
|
293
328
|
const names = keys.filter((key) => (key.startsWith(prefix) && key !== own) || key === legacy);
|
|
294
|
-
|
|
329
|
+
let values = await Promise.all(names.map((key) => $.store.get(key)));
|
|
330
|
+
if (prune) {
|
|
331
|
+
// An expired record holds nothing a period shows, a capped one only the oldest days; a session still running
|
|
332
|
+
// writes its own again at its next turn.
|
|
333
|
+
const gone = prune(
|
|
334
|
+
names.map((key, i) => [key, values[i]]),
|
|
335
|
+
Date.now(),
|
|
336
|
+
);
|
|
337
|
+
// Read again just before deleting: a session that wrote its record since keeps it, and it counts as written. The
|
|
338
|
+
// window between the read and the delete stays, as $.store has no conditional delete; it needs that session's write
|
|
339
|
+
// inside it. A record whose delete failed still counts.
|
|
340
|
+
const deleted = new Set();
|
|
341
|
+
// Every check settles before the merge, a failed read or delete included.
|
|
342
|
+
await Promise.allSettled(
|
|
343
|
+
[...gone].map(async (key) => {
|
|
344
|
+
const i = names.indexOf(key);
|
|
345
|
+
const current = await $.store.get(key);
|
|
346
|
+
if (JSON.stringify(current) !== JSON.stringify(values[i])) values[i] = current;
|
|
347
|
+
else {
|
|
348
|
+
await $.store.delete(key);
|
|
349
|
+
deleted.add(key);
|
|
350
|
+
}
|
|
351
|
+
}),
|
|
352
|
+
);
|
|
353
|
+
values = values.filter((_, i) => !deleted.has(names[i]));
|
|
354
|
+
}
|
|
355
|
+
const others = merge(values, resetAt);
|
|
295
356
|
router.ledgers[kind] = { ...router.ledgers[kind], others, othersResetAt: resetAt ?? null };
|
|
296
357
|
}
|
|
297
358
|
|
|
@@ -301,7 +362,10 @@ function ledgerView(router, kinds = LEDGER_KINDS) {
|
|
|
301
362
|
const patch = {};
|
|
302
363
|
for (const kind of kinds) {
|
|
303
364
|
const { others, own } = router.ledgers[kind];
|
|
304
|
-
|
|
365
|
+
const { field, ownField, merge, empty } = LEDGERS[kind];
|
|
366
|
+
if (!others) continue;
|
|
367
|
+
patch[field] = merge([others, own?.record]);
|
|
368
|
+
if (ownField) patch[ownField] = own?.record ?? empty();
|
|
305
369
|
}
|
|
306
370
|
return patch;
|
|
307
371
|
}
|
|
@@ -348,7 +412,7 @@ async function storeView($, router) {
|
|
|
348
412
|
// A notice answers the last press in the pane, so the pane opens without one; the last write keeps its Undo in the
|
|
349
413
|
// status bar.
|
|
350
414
|
async function openPane($, router) {
|
|
351
|
-
await updateView($, router, { notice: null, ...(await storeView($, router)) });
|
|
415
|
+
await updateView($, router, { notice: null, confirmDelete: false, ...(await storeView($, router)) });
|
|
352
416
|
await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
|
|
353
417
|
}
|
|
354
418
|
|
|
@@ -569,7 +633,7 @@ async function* passMain($, e, next, loop, version, nativeModel, reason, router)
|
|
|
569
633
|
const compared = await compareYours($, router, loop, response, e.effort ?? null, nativeModel);
|
|
570
634
|
if (compared) observed.yoursBy = compared.states;
|
|
571
635
|
const written = await $.state.set(ref, observed, { ifVersion: version });
|
|
572
|
-
if (written.isSet) await updateView($, router, responseMetrics(router.view, response, null));
|
|
636
|
+
if (written.isSet) await updateView($, router, responseMetrics(router.view, response, null, Date.now()));
|
|
573
637
|
}
|
|
574
638
|
return response;
|
|
575
639
|
}
|
|
@@ -662,6 +726,7 @@ async function decideTurn($, router, e, signal, step) {
|
|
|
662
726
|
routes: {},
|
|
663
727
|
});
|
|
664
728
|
await updateView($, router, {
|
|
729
|
+
...(activity.switched ? switchView(previous, selected.decision, activity.label, Date.now()) : {}),
|
|
665
730
|
phase: 'routed',
|
|
666
731
|
selectedModel: selected.decision.model,
|
|
667
732
|
actualModel: null,
|
|
@@ -715,6 +780,9 @@ async function transcript($) {
|
|
|
715
780
|
}
|
|
716
781
|
}
|
|
717
782
|
|
|
783
|
+
// The activity a route runs for, as the view labels it: only activity routing `on` applies one.
|
|
784
|
+
const activityLabel = (cfg, decision) => (cfg.activityRouting === 'on' ? (decision.activity ?? null) : null);
|
|
785
|
+
|
|
718
786
|
// A tool continuation whose turn shows stronger work than its route was chosen for moves up once (observedMove). Only
|
|
719
787
|
// tool calls after the last applied review count (`loop.observedFrom`): the review judged the ones before. The
|
|
720
788
|
// transcript is read only when some activity's route at this tier is stronger than the running one.
|
|
@@ -730,7 +798,11 @@ async function moveOnObserved($, router, cfg, loop, e, context) {
|
|
|
730
798
|
// Replies from here on are labelled with the moved activity; the classifier's answer stays as recorded.
|
|
731
799
|
const turn = router.turnActivities.get(e.turnId);
|
|
732
800
|
if (turn) router.turnActivities.set(e.turnId, { ...turn, label: moved.decision.activity });
|
|
733
|
-
await updateView($, router, {
|
|
801
|
+
await updateView($, router, {
|
|
802
|
+
estimate: moved.decision.estimate ?? null,
|
|
803
|
+
comparison: null,
|
|
804
|
+
...switchView(loop.decision, moved.decision, activityLabel(cfg, moved.decision), Date.now()),
|
|
805
|
+
});
|
|
734
806
|
return moved;
|
|
735
807
|
}
|
|
736
808
|
|
|
@@ -763,6 +835,11 @@ async function reviewMidTurn($, router, cfg, loop, e, { context, nativeModel, av
|
|
|
763
835
|
$.ui.toast(switchToast(cfg, previous, selected.decision, selected.decision.estimate));
|
|
764
836
|
const turn = router.turnActivities.get(e.turnId);
|
|
765
837
|
if (turn) router.turnActivities.set(e.turnId, { ...turn, label: selected.decision.activity ?? null });
|
|
838
|
+
await updateView(
|
|
839
|
+
$,
|
|
840
|
+
router,
|
|
841
|
+
switchView(previous, selected.decision, activityLabel(cfg, selected.decision), Date.now()),
|
|
842
|
+
);
|
|
766
843
|
}
|
|
767
844
|
// The pane shows the readings behind the route; a review by a classifier the pane no longer labels keeps them off.
|
|
768
845
|
if (cfg.classifier === router.config.classifier)
|
|
@@ -813,17 +890,10 @@ async function reviewMidTurn($, router, cfg, loop, e, { context, nativeModel, av
|
|
|
813
890
|
return { ...loop, reviewedAt: loop.requests ?? 0 };
|
|
814
891
|
}
|
|
815
892
|
|
|
816
|
-
// A routed reply's metrics in the view,
|
|
817
|
-
// turn's activity record with its list price (`routedUsd`, null when unpriced) and, when its tier served it, its route.
|
|
893
|
+
// A routed reply's metrics in the view, with the tier that served it, and added to the turn's activity record with its list price (`routedUsd`, null when unpriced) and, when its tier served it, its route.
|
|
818
894
|
async function publishReply($, router, turnId, response, tier, route, routedUsd) {
|
|
819
895
|
const turn = router.turnActivities.get(turnId);
|
|
820
|
-
const metrics = responseMetrics(
|
|
821
|
-
router.view,
|
|
822
|
-
response,
|
|
823
|
-
tier,
|
|
824
|
-
tier ? (turn?.label ?? null) : null,
|
|
825
|
-
tier ? route : null,
|
|
826
|
-
);
|
|
896
|
+
const metrics = responseMetrics(router.view, response, tier, Date.now());
|
|
827
897
|
await updateView($, router, metrics);
|
|
828
898
|
if (turn)
|
|
829
899
|
router.turnActivities.set(turnId, {
|
|
@@ -840,12 +910,14 @@ async function publishReply($, router, turnId, response, tier, route, routedUsd)
|
|
|
840
910
|
// A routed reply written to the loop at `loopVersion`, then published: the view's metrics and the routing-vs-your-model
|
|
841
911
|
// totals. A lost write publishes nothing.
|
|
842
912
|
async function observeRouted($, router, { turnId, cfg, loop, loopVersion, request, response, nativeModel }) {
|
|
913
|
+
// When the reply arrived: the awaits below can cross midnight or the hour it is kept under.
|
|
914
|
+
const at = Date.now();
|
|
843
915
|
const observed = observeResponse(cfg, loop, {
|
|
844
916
|
usage: response.usage,
|
|
845
917
|
requestedModel: request.model,
|
|
846
918
|
effort: request.effort ?? null,
|
|
847
919
|
stopReason: response.stopReason,
|
|
848
|
-
now:
|
|
920
|
+
now: at,
|
|
849
921
|
});
|
|
850
922
|
if (response.stopReason === null) observed.suspended = true;
|
|
851
923
|
const compared = await compareYours($, router, loop, response, request.effort ?? null, nativeModel, cfg);
|
|
@@ -856,7 +928,20 @@ async function observeRouted($, router, { turnId, cfg, loop, loopVersion, reques
|
|
|
856
928
|
const tier = isSameModel(request.model, response.usage?.model) ? loop.decision.tier : null;
|
|
857
929
|
const route = `${request.model}@${request.effort ?? 'session'}`;
|
|
858
930
|
await publishReply($, router, turnId, response, tier, route, compared?.reply?.routedUsd ?? null);
|
|
859
|
-
|
|
931
|
+
// Kept by model id, under the model that served it: a substitute's cost is not the requested model's, and a models
|
|
932
|
+
// key can name another model later.
|
|
933
|
+
const idOf = (model) => modelSpec(cfg, model)?.id ?? model;
|
|
934
|
+
if (compared)
|
|
935
|
+
await addSavings($, router, {
|
|
936
|
+
turnId,
|
|
937
|
+
compared,
|
|
938
|
+
tier,
|
|
939
|
+
route: `${idOf(response.usage?.model ?? request.model)}@${request.effort ?? 'session'}`,
|
|
940
|
+
yours: idOf(nativeModel),
|
|
941
|
+
byId: Object.fromEntries(Object.entries(compared.replies).map(([alias, r]) => [cfg.models[alias].id, r])),
|
|
942
|
+
usage: response.usage,
|
|
943
|
+
at,
|
|
944
|
+
});
|
|
860
945
|
}
|
|
861
946
|
|
|
862
947
|
// The main conversation's prompt-cache lifetime by Claude Code's rule (sessionCacheTtl), or null when a reading
|
|
@@ -899,22 +984,35 @@ async function compareYours($, router, loop, response, servedEffort, nativeModel
|
|
|
899
984
|
}
|
|
900
985
|
}
|
|
901
986
|
|
|
902
|
-
// A routed reply's prices added to each model's session totals in the view, and
|
|
903
|
-
// the turn's
|
|
904
|
-
|
|
987
|
+
// A routed reply's prices added to each model's session totals in the view, and the reply, priced against your model
|
|
988
|
+
// now, to the turn's replies for the usage record. `route` is the served model's `id@effort`, `yours` your model's id,
|
|
989
|
+
// `byId` the reply priced against each model as yours, keyed by model id; `at` when the reply arrived (ms).
|
|
990
|
+
async function addSavings($, router, { turnId, compared, tier, route, yours, byId, usage, at }) {
|
|
905
991
|
try {
|
|
906
992
|
const view = router.view ?? (await readView($, router));
|
|
907
|
-
if (compared.reply)
|
|
908
|
-
|
|
909
|
-
|
|
993
|
+
if (compared.reply) {
|
|
994
|
+
const turn = router.turnActivities.get(turnId);
|
|
995
|
+
const entry = replyEntry({
|
|
996
|
+
at,
|
|
997
|
+
activity: tier ? (turn?.label ?? null) : null,
|
|
998
|
+
tier,
|
|
999
|
+
route,
|
|
1000
|
+
yours,
|
|
1001
|
+
reply: compared.reply,
|
|
1002
|
+
replies: byId,
|
|
1003
|
+
usage,
|
|
1004
|
+
});
|
|
1005
|
+
router.turnUsage.set(turnId, [...(router.turnUsage.get(turnId) ?? []), entry]);
|
|
1006
|
+
}
|
|
1007
|
+
await updateView($, router, { savingsBy: addReplies(view.savingsBy ?? {}, compared.replies) });
|
|
910
1008
|
} catch {}
|
|
911
1009
|
}
|
|
912
1010
|
|
|
913
|
-
// A finished turn's
|
|
914
|
-
async function
|
|
1011
|
+
// A finished turn's replies, and the switch that started it, added to this session's usage record.
|
|
1012
|
+
async function flushUsage($, router, entries, switched) {
|
|
915
1013
|
try {
|
|
916
|
-
if (await addToLedger($, router, '
|
|
917
|
-
await updateView($, router, ledgerView(router, ['
|
|
1014
|
+
if (await addToLedger($, router, 'usage', (record, now) => recordUsage(record, entries, { switched, now })))
|
|
1015
|
+
await updateView($, router, ledgerView(router, ['usage']));
|
|
918
1016
|
} catch {}
|
|
919
1017
|
}
|
|
920
1018
|
|
|
@@ -1239,9 +1337,15 @@ function paneActions($, router, view) {
|
|
|
1239
1337
|
() => ({ ...patch, lastWrite: { label, leaves }, notice: `Saved: ${label}. Applies from the next turn.` }),
|
|
1240
1338
|
);
|
|
1241
1339
|
};
|
|
1340
|
+
// The Usage tab's period, kept across sessions; a failed save keeps it for this session.
|
|
1341
|
+
const showPeriod = async (period, patch = {}) => {
|
|
1342
|
+
if (!PERIODS.includes(period)) return;
|
|
1343
|
+
await $.store.set(PERIOD_KEY, period).catch(() => {});
|
|
1344
|
+
await updateView($, router, { usagePeriod: period, usageOpen: null, ...patch });
|
|
1345
|
+
};
|
|
1242
1346
|
return {
|
|
1243
1347
|
mode: (mode) => changeMode($, router, mode),
|
|
1244
|
-
tab: (tab) => updateView($, router, { tab, notice: null }),
|
|
1348
|
+
tab: (tab) => updateView($, router, { tab, notice: null, confirmDelete: false }),
|
|
1245
1349
|
help: () => updateView($, router, { help: !(router.view ?? view).help }),
|
|
1246
1350
|
pin: async (tier) => {
|
|
1247
1351
|
await updateView($, router, { notice: await setPin($, router, tier) });
|
|
@@ -1297,6 +1401,15 @@ function paneActions($, router, view) {
|
|
|
1297
1401
|
removeActivity: (activity, tier) => editCell(activity, tier, 'remove'),
|
|
1298
1402
|
selectCell: (cell) => updateView($, router, { routeCell: cell, notice: null }),
|
|
1299
1403
|
usageDetail: () => updateView($, router, { usageDetail: !(router.view ?? view).usageDetail }),
|
|
1404
|
+
period: (period) => showPeriod(period),
|
|
1405
|
+
// The Now tab's Usage links: the Usage tab on the period the line sums.
|
|
1406
|
+
usageFor: (period) => showPeriod(period, { tab: 'usage', notice: null, confirmDelete: false }),
|
|
1407
|
+
usageGroup: (group) => updateView($, router, { usageGroup: group, usageOpen: null }),
|
|
1408
|
+
usageCompare: () => updateView($, router, { usageCompare: !(router.view ?? view).usageCompare }),
|
|
1409
|
+
usageOpen: (row) => updateView($, router, { usageOpen: row }),
|
|
1410
|
+
// An insight's link: the Routes tab with the grid cell it names open.
|
|
1411
|
+
openRoute: (cell) => updateView($, router, { tab: 'routes', routeCell: cell, notice: null }),
|
|
1412
|
+
confirmDelete: (asked) => updateView($, router, { confirmDelete: asked }),
|
|
1300
1413
|
// `/router activities <mode>`: the mode alone, saved at once like a classifier press, with Undo.
|
|
1301
1414
|
activityRouting: (mode) => {
|
|
1302
1415
|
const from = router.config.activityRouting;
|
|
@@ -1305,12 +1418,13 @@ function paneActions($, router, view) {
|
|
|
1305
1418
|
withActivities(file, { base: { activityRouting: from }, activityRouting: mode }),
|
|
1306
1419
|
);
|
|
1307
1420
|
},
|
|
1308
|
-
//
|
|
1421
|
+
// Delete history: every record kept across sessions (usage, the undated savings, activity stats and metrics,
|
|
1422
|
+
// subagents) and this session's counts.
|
|
1309
1423
|
resetStats: async () => {
|
|
1310
1424
|
router.statsResets += 1;
|
|
1311
1425
|
router.run = null;
|
|
1312
1426
|
router.afterDown = null;
|
|
1313
|
-
router.
|
|
1427
|
+
router.turnUsage.clear();
|
|
1314
1428
|
for (const [id, agent] of router.agents) router.agents.set(id, flushedAgent(agent));
|
|
1315
1429
|
// Every session's records. The marker is written first, so a session still holding its record from before the
|
|
1316
1430
|
// reset drops it, unless that record's first count has the reset's millisecond or the clock moved back across
|
|
@@ -1327,13 +1441,21 @@ function paneActions($, router, view) {
|
|
|
1327
1441
|
return true;
|
|
1328
1442
|
};
|
|
1329
1443
|
const cleared = await clearRecords().catch(() => false);
|
|
1444
|
+
// The store kept the history: this session's records start over, and its next write replaces the stored one.
|
|
1445
|
+
if (!cleared)
|
|
1446
|
+
for (const kind of LEDGER_KINDS) {
|
|
1447
|
+
const held = router.ledgers[kind];
|
|
1448
|
+
if (held.own) held.own = { ...held.own, record: LEDGERS[kind].empty() };
|
|
1449
|
+
}
|
|
1330
1450
|
await updateView($, router, {
|
|
1331
1451
|
activityStats: null,
|
|
1332
1452
|
savingsBy: {},
|
|
1333
|
-
|
|
1453
|
+
confirmDelete: false,
|
|
1454
|
+
usageOpen: null,
|
|
1455
|
+
...ledgerView(router),
|
|
1334
1456
|
notice: cleared
|
|
1335
|
-
? '
|
|
1336
|
-
: 'This session’s
|
|
1457
|
+
? 'History deleted: usage, activity and subagent records of every session.'
|
|
1458
|
+
: 'This session’s counts cleared. The saved history could not be deleted.',
|
|
1337
1459
|
});
|
|
1338
1460
|
},
|
|
1339
1461
|
discardRouting: () => updateView($, router, { routeDraft: null, tuning: null, tuningBase: null, notice: null }),
|
|
@@ -1398,7 +1520,7 @@ async function endSession($, router) {
|
|
|
1398
1520
|
router.turnActivities.clear();
|
|
1399
1521
|
for (const review of router.reviews.values()) review.controller.abort();
|
|
1400
1522
|
router.reviews.clear();
|
|
1401
|
-
router.
|
|
1523
|
+
router.turnUsage.clear();
|
|
1402
1524
|
const run = router.run;
|
|
1403
1525
|
router.run = null;
|
|
1404
1526
|
router.afterDown = null;
|
|
@@ -1419,6 +1541,8 @@ async function endSession($, router) {
|
|
|
1419
1541
|
activityMetrics: router.view?.activityMetrics ?? null,
|
|
1420
1542
|
savingsStore: router.view?.savingsStore ?? null,
|
|
1421
1543
|
subagentStore: router.view?.subagentStore ?? null,
|
|
1544
|
+
usageStore: router.view?.usageStore ?? null,
|
|
1545
|
+
usagePeriod: router.view?.usagePeriod ?? 'session',
|
|
1422
1546
|
health: healthOf(clientOf(router, router.config.classifier), router.config.classifier),
|
|
1423
1547
|
configPath: router.config.nativePath,
|
|
1424
1548
|
phase: router.view?.phase === 'unavailable' ? 'unavailable' : 'ready',
|
|
@@ -1465,6 +1589,7 @@ export function register(on, options) {
|
|
|
1465
1589
|
routeDraft: null,
|
|
1466
1590
|
lastWrite: null,
|
|
1467
1591
|
bandDetail: (await $.store.get(BAND_DETAIL).catch(() => false)) === true,
|
|
1592
|
+
usagePeriod: await periodOf($),
|
|
1468
1593
|
...(await storeView($, router)),
|
|
1469
1594
|
});
|
|
1470
1595
|
} catch (error) {
|
|
@@ -1612,9 +1737,9 @@ export function register(on, options) {
|
|
|
1612
1737
|
if (turn?.mode === 'off') {
|
|
1613
1738
|
if ((await $.session.id()) === turn.sessionId) router.afterDown = advanceDown(router.afterDown, turn).after;
|
|
1614
1739
|
} else if (turn) await recordActivity($, router, turn);
|
|
1615
|
-
const
|
|
1616
|
-
router.
|
|
1617
|
-
if (
|
|
1740
|
+
const entries = router.turnUsage.get(e.turnId);
|
|
1741
|
+
router.turnUsage.delete(e.turnId);
|
|
1742
|
+
if (entries) await flushUsage($, router, entries, turn?.switched ?? null);
|
|
1618
1743
|
return next(e);
|
|
1619
1744
|
});
|
|
1620
1745
|
|
package/lib/display.mjs
CHANGED
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
import { ratesAt } from './cost.mjs';
|
|
12
12
|
import { PROBE_RESULTS } from './probe-results.mjs';
|
|
13
13
|
import { cellForModel } from './route.mjs';
|
|
14
|
-
import { emptyTotals, readout
|
|
14
|
+
import { emptyTotals, readout } from './savings.mjs';
|
|
15
|
+
import { allTime } from './usage.mjs';
|
|
15
16
|
|
|
16
17
|
// Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
|
|
17
18
|
export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
|
|
@@ -37,11 +38,6 @@ export function routeName(config, route) {
|
|
|
37
38
|
return `${modelName(model.id)}${model.efforts.length ? ` · ${route.effort ?? 'session'}` : ''}`;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
// Changes of route between consecutive replies, from the (model, effort) each request went out with, so a move inside
|
|
41
|
-
// a tier counts and a later change of settings does not rewrite the count. A reply routing did not choose breaks no run.
|
|
42
|
-
export const switchCount = (routes) =>
|
|
43
|
-
routes.slice(1).filter((route, i) => route && routes[i] && route !== routes[i]).length;
|
|
44
|
-
|
|
45
41
|
export const percent = (value) => `${Math.round(value * 100)}%`;
|
|
46
42
|
|
|
47
43
|
// Two shares that are compared, such as a classifier's support and the bar it must clear: whole percents, or as many
|
|
@@ -299,12 +295,12 @@ export function strongerReplies(totals) {
|
|
|
299
295
|
export function savingsLine(view) {
|
|
300
296
|
const label = `vs your model (${yourModel(view)})`;
|
|
301
297
|
const session = readout(view.savings ?? emptyTotals());
|
|
302
|
-
const
|
|
303
|
-
const across = readout(
|
|
298
|
+
const kept = allTime(view.usageStore, view.savingsStore);
|
|
299
|
+
const across = readout(kept.totals);
|
|
304
300
|
if (!session.replies) {
|
|
305
301
|
const saved = across.early
|
|
306
302
|
? ''
|
|
307
|
-
: `; ${savingsAmount(across)} since ${sinceDate(
|
|
303
|
+
: `; ${savingsAmount(across)} since ${sinceDate(kept.since)}, each session compared with its own model`;
|
|
308
304
|
return `${label}: no routed replies this session${saved}`;
|
|
309
305
|
}
|
|
310
306
|
if (session.early) return `${label}: too early, ${replyCount(session.replies, 'routed replies')}`;
|