@alexeiled/claude-router 1.24.0 → 1.25.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 +44 -0
- package/README.md +39 -7
- package/docs/architecture.md +27 -4
- package/docs/configuration.md +23 -14
- package/docs/evaluation.md +3 -3
- package/docs/native-router.md +11 -3
- package/docs/router-band.svg +124 -133
- package/docs/router-pane-classifier.svg +24 -23
- package/docs/router-pane-now.svg +9 -7
- package/docs/router-pane-policy.svg +4 -4
- package/docs/router-pane-routes.svg +8 -8
- package/docs/router-pane-usage-whatif.svg +14 -14
- package/docs/router-pane-usage.svg +20 -20
- package/docs/routing-modes.md +10 -8
- package/docs/user-guide.md +70 -17
- package/hooks/native-router.mjs +333 -80
- package/lib/activity-stats.mjs +23 -4
- package/lib/activity.mjs +7 -5
- package/lib/auto-engine.mjs +13 -6
- package/lib/auto-route.mjs +10 -4
- package/lib/band.mjs +35 -440
- package/lib/classifier-client.mjs +12 -8
- package/lib/config-file.mjs +7 -2
- package/lib/config.mjs +24 -9
- package/lib/cost.mjs +5 -1
- package/lib/costs.mjs +4 -2
- package/lib/decisions.mjs +13 -9
- package/lib/display.mjs +69 -20
- package/lib/facts.mjs +3 -0
- package/lib/modes.mjs +8 -4
- package/lib/panel.mjs +75 -2167
- package/lib/policy.mjs +7 -6
- package/lib/replay-trace.mjs +5 -2
- package/lib/replay.mjs +7 -1
- package/lib/route-line.mjs +46 -0
- package/lib/route.mjs +21 -16
- package/lib/savings.mjs +14 -3
- package/lib/subagents.mjs +41 -11
- package/lib/subcommands.mjs +60 -0
- package/lib/ui/fallback.mjs +22 -0
- package/lib/ui/graphical/band.mjs +256 -0
- package/lib/ui/graphical/charts.mjs +163 -0
- package/lib/ui/graphical/classifier.mjs +153 -0
- package/lib/ui/graphical/frame.mjs +227 -0
- package/lib/ui/graphical/kit.mjs +156 -0
- package/lib/ui/graphical/meter.mjs +25 -0
- package/lib/ui/graphical/now.mjs +353 -0
- package/lib/ui/graphical/policy.mjs +56 -0
- package/lib/ui/graphical/routes.mjs +264 -0
- package/lib/ui/graphical/squares.mjs +29 -0
- package/lib/ui/graphical/usage.mjs +376 -0
- package/lib/ui/model/band.mjs +439 -0
- package/lib/ui/model/classifier.mjs +308 -0
- package/lib/ui/model/draft.mjs +62 -0
- package/lib/ui/model/format.mjs +41 -0
- package/lib/ui/model/frame.mjs +359 -0
- package/lib/ui/model/now.mjs +390 -0
- package/lib/ui/model/policy.mjs +194 -0
- package/lib/ui/model/routes.mjs +392 -0
- package/lib/ui/model/usage-agents.mjs +311 -0
- package/lib/ui/model/usage-breakdown.mjs +237 -0
- package/lib/ui/model/usage-shared.mjs +58 -0
- package/lib/ui/model/usage.mjs +330 -0
- package/lib/ui/terminal/band.mjs +220 -0
- package/lib/ui/terminal/classifier.mjs +142 -0
- package/lib/ui/terminal/frame.mjs +169 -0
- package/lib/ui/terminal/kit.mjs +133 -0
- package/lib/ui/terminal/now.mjs +300 -0
- package/lib/ui/terminal/policy.mjs +52 -0
- package/lib/ui/terminal/routes.mjs +167 -0
- package/lib/ui/terminal/usage.mjs +363 -0
- package/lib/ui/terminal/wrap.mjs +37 -0
- package/lib/ui/tokens.mjs +106 -0
- package/lib/usage.mjs +5 -5
- package/lib/view.mjs +5 -2
- package/package.json +3 -2
- package/types/index.d.ts +15 -0
package/hooks/native-router.mjs
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
} from '../lib/activity-stats.mjs';
|
|
24
24
|
import { advanceDemand, assessCandidates, initialAutoState, selectTarget } from '../lib/auto-engine.mjs';
|
|
25
25
|
import { autoRoute, observeAutoFallback } from '../lib/auto-route.mjs';
|
|
26
|
-
import { renderBand, switchToast } from '../lib/band.mjs';
|
|
26
|
+
import { renderBand, renderRouteLine, switchToast } from '../lib/band.mjs';
|
|
27
27
|
import { ClassifierClient } from '../lib/classifier-client.mjs';
|
|
28
28
|
import { resolveCredentials } from '../lib/classifier-contract.mjs';
|
|
29
29
|
import {
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
effectiveRoutes,
|
|
38
38
|
loadConfig,
|
|
39
39
|
MIGRATION_HINT,
|
|
40
|
+
MIN_CLAUDE_CODE,
|
|
40
41
|
ROUTING_MODES,
|
|
41
42
|
supportedVersion,
|
|
42
43
|
TIERS,
|
|
@@ -76,6 +77,8 @@ import {
|
|
|
76
77
|
GATEWAY_SETTINGS,
|
|
77
78
|
missingCredentials,
|
|
78
79
|
NOT_A_TIER_REASON,
|
|
80
|
+
pinCard,
|
|
81
|
+
pluginIdOf,
|
|
79
82
|
savingsLine,
|
|
80
83
|
storeAgreementLine,
|
|
81
84
|
unavailableReason,
|
|
@@ -93,7 +96,7 @@ import {
|
|
|
93
96
|
stripReminders,
|
|
94
97
|
} from '../lib/facts.mjs';
|
|
95
98
|
import { applyMode, modeLabel, readProjectMode, resolveRoutingMode } from '../lib/modes.mjs';
|
|
96
|
-
import { renderPanel
|
|
99
|
+
import { renderPanel } from '../lib/panel.mjs';
|
|
97
100
|
import { sameTaskOf } from '../lib/policy.mjs';
|
|
98
101
|
import { providerFile, providerName } from '../lib/providers.mjs';
|
|
99
102
|
import {
|
|
@@ -132,6 +135,7 @@ import {
|
|
|
132
135
|
prepareLoop,
|
|
133
136
|
resetHistory,
|
|
134
137
|
} from '../lib/route.mjs';
|
|
138
|
+
import { LINE_MODES, lineId, lineModeOf, lineOf, routeOf, showsLine } from '../lib/route-line.mjs';
|
|
135
139
|
import {
|
|
136
140
|
afterReset,
|
|
137
141
|
compareModels,
|
|
@@ -166,6 +170,10 @@ import {
|
|
|
166
170
|
subagentsAfterReset,
|
|
167
171
|
typeRoute,
|
|
168
172
|
} from '../lib/subagents.mjs';
|
|
173
|
+
import { argumentHint, routerSuggestions } from '../lib/subcommands.mjs';
|
|
174
|
+
import { routeDraftOf, routingChanges } from '../lib/ui/model/draft.mjs';
|
|
175
|
+
import { keyHelpOf } from '../lib/ui/model/frame.mjs';
|
|
176
|
+
import { isGraphical } from '../lib/ui/tokens.mjs';
|
|
169
177
|
import {
|
|
170
178
|
emptyUsage,
|
|
171
179
|
mergeUsage,
|
|
@@ -192,10 +200,17 @@ import {
|
|
|
192
200
|
|
|
193
201
|
const VIEW = { plugin: 'router', key: 'view' };
|
|
194
202
|
const LOOP = { plugin: 'router', key: 'loops' };
|
|
203
|
+
// One record per route line placed, by its `$.ui.place` id: what its `Placed` row draws.
|
|
204
|
+
const LINES = { plugin: 'router', key: 'lines' };
|
|
195
205
|
const PANE = 'jev-router';
|
|
196
206
|
const PANE_TITLE = 'Router';
|
|
197
207
|
const BAND_DETAIL = 'band:detail';
|
|
208
|
+
// Whether and when a turn leaves a route line (lib/route-line.mjs), kept across sessions.
|
|
209
|
+
const ROUTE_LINE = 'route:line';
|
|
198
210
|
const MODE_PREFIX = 'mode:';
|
|
211
|
+
// When the toast for one reason routing cannot run last showed; the reason is appended.
|
|
212
|
+
const UNAVAILABLE_NOTICE_PREFIX = 'notice:unavailable:';
|
|
213
|
+
const UNAVAILABLE_NOTICE_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
199
214
|
// The session's routing mode override (lib/modes.mjs), apart from its auto|manual MODE_PREFIX.
|
|
200
215
|
const ROUTING_MODE_PREFIX = 'routing-mode:';
|
|
201
216
|
const isSet = (value) => /^(1|true|yes|on)$/i.test(String(value ?? '').trim());
|
|
@@ -254,6 +269,20 @@ const LEDGERS = {
|
|
|
254
269
|
};
|
|
255
270
|
const LEDGER_KINDS = Object.keys(LEDGERS);
|
|
256
271
|
|
|
272
|
+
/** @typedef {ReturnType<typeof readTrace> | ReturnType<typeof newTrace>} TraceRecord */
|
|
273
|
+
/**
|
|
274
|
+
* @typedef {object} TraceHold
|
|
275
|
+
* @property {string} sessionId
|
|
276
|
+
* @property {TraceRecord | null} record
|
|
277
|
+
* @property {unknown} persisted
|
|
278
|
+
* @property {number} lostWrites
|
|
279
|
+
* @property {Set<string>} reasons
|
|
280
|
+
* @property {Map<string, any>} turns
|
|
281
|
+
* @property {number} nextTurn
|
|
282
|
+
* @property {string | null} config
|
|
283
|
+
* @property {{ events: object[], excludedEvents?: number } | null} controls
|
|
284
|
+
*/
|
|
285
|
+
|
|
257
286
|
// Everything the hooks change between events, in one object `register()` creates and passes to its helpers. Module
|
|
258
287
|
// variables, not $.state: a reload starts them over, and the view is written through to $.state as it changes.
|
|
259
288
|
function createRouter(options) {
|
|
@@ -284,8 +313,10 @@ function createRouter(options) {
|
|
|
284
313
|
// Each turn's decision rows until turn.complete writes them, with the turn's end, to the session's record, held as
|
|
285
314
|
// `{ sessionId, record }` once read (`decisionRecord`).
|
|
286
315
|
turnDecisions: new Map(),
|
|
316
|
+
// The route the last routed turn ran, to tell whether the next one moved it; null at a session's start.
|
|
317
|
+
lastRoute: null,
|
|
287
318
|
decisionRecord: null,
|
|
288
|
-
trace: null,
|
|
319
|
+
trace: /** @type {TraceHold | null} */ (null),
|
|
289
320
|
traceFreshSession: null,
|
|
290
321
|
traceClearSession: null,
|
|
291
322
|
traceStartedSession: null,
|
|
@@ -293,7 +324,7 @@ function createRouter(options) {
|
|
|
293
324
|
traceOperations: new Map(),
|
|
294
325
|
controllers: new Set(),
|
|
295
326
|
turnControllers: new Map(),
|
|
296
|
-
view: null,
|
|
327
|
+
view: /** @type {Record<string, unknown> | null} */ (null),
|
|
297
328
|
modes: new Map(),
|
|
298
329
|
// Each session's routing mode override (null: none), as read from or written to the store.
|
|
299
330
|
routingModes: new Map(),
|
|
@@ -352,6 +383,7 @@ async function ensureTrace($, router, e, turn, loop, loaded, fresh) {
|
|
|
352
383
|
if (router.trace?.sessionId === sessionId && router.trace.record) return;
|
|
353
384
|
const previous = router.trace?.sessionId === sessionId ? router.trace : null;
|
|
354
385
|
const at = Date.now();
|
|
386
|
+
/** @type {TraceHold} */
|
|
355
387
|
const held = {
|
|
356
388
|
sessionId,
|
|
357
389
|
record: null,
|
|
@@ -534,7 +566,7 @@ async function flushTrace($, router, turnId = null, ending = false) {
|
|
|
534
566
|
try {
|
|
535
567
|
const at = Date.now();
|
|
536
568
|
const keys = await $.store.keys();
|
|
537
|
-
let entries = await Promise.all(keys.map(async (
|
|
569
|
+
let entries = await Promise.all(keys.map(async (name) => [name, await $.store.get(name)]));
|
|
538
570
|
if (entries.some(([, value]) => value === undefined)) throw new Error('store-guard');
|
|
539
571
|
if (traceBytes(Object.fromEntries(entries)) > TRACE_STORE_BYTES) throw new Error('store-guard');
|
|
540
572
|
const resetAt = entries.find(([name]) => name === SAVINGS_RESET_KEY)?.[1];
|
|
@@ -597,9 +629,8 @@ async function flushTrace($, router, turnId = null, ending = false) {
|
|
|
597
629
|
if (ending) held.controls = null;
|
|
598
630
|
} catch (error) {
|
|
599
631
|
held.lostWrites++;
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
);
|
|
632
|
+
const message = /** @type {Error} */ (error).message;
|
|
633
|
+
held.reasons.add(message === 'conflict' ? 'conflict' : message === 'store-guard' ? 'store-guard' : 'lost-write');
|
|
603
634
|
}
|
|
604
635
|
} catch {
|
|
605
636
|
traceFailed(router);
|
|
@@ -641,7 +672,7 @@ async function readView($, router) {
|
|
|
641
672
|
|
|
642
673
|
// Only a saved mode is cached: the engine can draw the band before session.start, and a cached fallback from that
|
|
643
674
|
// render would override the start rule.
|
|
644
|
-
async function modeOf($, router, fallback = 'auto') {
|
|
675
|
+
async function modeOf($, router, fallback = /** @type {string | null} */ ('auto')) {
|
|
645
676
|
const sessionId = await $.session.id();
|
|
646
677
|
if (router.modes.has(sessionId)) return router.modes.get(sessionId);
|
|
647
678
|
try {
|
|
@@ -775,15 +806,17 @@ async function readProject($, profilePath) {
|
|
|
775
806
|
}
|
|
776
807
|
|
|
777
808
|
async function updateView($, router, patch) {
|
|
778
|
-
|
|
779
|
-
|
|
809
|
+
// Read the stored view only when none is held, and merge into whatever is held once it arrives: a patch applied
|
|
810
|
+
// during that read stays.
|
|
811
|
+
const stored = router.view ? null : await readView($, router);
|
|
812
|
+
router.view = { ...(router.view ?? stored), ...patch };
|
|
780
813
|
await $.state.set(VIEW, router.view);
|
|
781
814
|
}
|
|
782
815
|
|
|
783
816
|
// This session's record of `kind`, read from the store once per session, and the last reset (ms) from any session,
|
|
784
817
|
// read each time: a Reset in another session empties a record held from before it.
|
|
785
818
|
async function ownRecord($, router, kind) {
|
|
786
|
-
const { prefix, read, afterReset } = LEDGERS[kind];
|
|
819
|
+
const { prefix, read, afterReset: resetRecord } = LEDGERS[kind];
|
|
787
820
|
const held = router.ledgers[kind];
|
|
788
821
|
const sessionId = await $.session.id();
|
|
789
822
|
const resetAt = await $.store.get(SAVINGS_RESET_KEY);
|
|
@@ -792,7 +825,7 @@ async function ownRecord($, router, kind) {
|
|
|
792
825
|
// Another call may have read it meanwhile and a turn added to it: that one stands.
|
|
793
826
|
if (held.own?.sessionId !== sessionId) held.own = { sessionId, record };
|
|
794
827
|
}
|
|
795
|
-
held.own.record =
|
|
828
|
+
held.own.record = resetRecord(held.own.record, resetAt);
|
|
796
829
|
return { own: held.own, resetAt };
|
|
797
830
|
}
|
|
798
831
|
|
|
@@ -893,7 +926,10 @@ async function storeView($, router) {
|
|
|
893
926
|
// status bar.
|
|
894
927
|
async function openPane($, router) {
|
|
895
928
|
await updateView($, router, { notice: null, confirmDelete: false, ...(await storeView($, router)) });
|
|
896
|
-
await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
|
|
929
|
+
const opened = await $.ui.open({ id: PANE, title: PANE_TITLE, focus: true, closeOnEscape: true });
|
|
930
|
+
// The pane is open but undrawn, as where no surface places panes or the terminal is too narrow for it; saying why beats
|
|
931
|
+
// a press that shows nothing.
|
|
932
|
+
if (opened?.isPlaced === false) $.ui.toast(`The Router pane is not shown: ${opened.reason}`);
|
|
897
933
|
}
|
|
898
934
|
|
|
899
935
|
async function changeMode($, router, mode) {
|
|
@@ -951,32 +987,67 @@ async function setPin($, router, tier) {
|
|
|
951
987
|
return `${tier} pinned for the next turn and its tool continuations.`;
|
|
952
988
|
}
|
|
953
989
|
|
|
954
|
-
//
|
|
955
|
-
//
|
|
990
|
+
// What each word of the SUBCOMMANDS table does: a switch, since the engine reads `$` only where it is passed to a
|
|
991
|
+
// function it can name. A word with no case, or one that answers nothing, leaves the command to open the pane.
|
|
992
|
+
async function runSubcommand($, router, action, value) {
|
|
993
|
+
switch (action) {
|
|
994
|
+
case 'auto':
|
|
995
|
+
return switchRouting($, router, 'auto');
|
|
996
|
+
case 'off':
|
|
997
|
+
return switchRouting($, router, 'manual');
|
|
998
|
+
case 'trace':
|
|
999
|
+
return value === 'export' ? { text: await exportTraces($, router) } : undefined;
|
|
1000
|
+
case 'line':
|
|
1001
|
+
return {
|
|
1002
|
+
text: LINE_MODES.includes(value) ? await setRouteLine($, value) : `Choose line ${LINE_MODES.join(', ')}.`,
|
|
1003
|
+
};
|
|
1004
|
+
case 'decisions':
|
|
1005
|
+
return { text: await decisionsText($, router, value) };
|
|
1006
|
+
case 'pin':
|
|
1007
|
+
return { text: TIERS.includes(value) ? await setPin($, router, value) : `Choose ${TIERS.join(', ')}.` };
|
|
1008
|
+
case 'mode':
|
|
1009
|
+
return {
|
|
1010
|
+
text: [...ROUTING_MODES, 'default'].includes(value)
|
|
1011
|
+
? await setRoutingMode($, router, value)
|
|
1012
|
+
: `Choose mode ${[...ROUTING_MODES, 'default'].join(', ')}.`,
|
|
1013
|
+
};
|
|
1014
|
+
case 'activities':
|
|
1015
|
+
return {
|
|
1016
|
+
text: ACTIVITY_MODES.includes(value)
|
|
1017
|
+
? await setActivityRouting($, router, value)
|
|
1018
|
+
: `Choose activities ${ACTIVITY_MODES.join(', ')}.`,
|
|
1019
|
+
};
|
|
1020
|
+
default:
|
|
1021
|
+
return undefined;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// `/router line <mode>`: a preference like the band's second row, kept across sessions and never in router.json.
|
|
1026
|
+
async function setRouteLine($, mode) {
|
|
1027
|
+
const saved = await $.store.set(ROUTE_LINE, mode).then(
|
|
1028
|
+
() => true,
|
|
1029
|
+
() => false,
|
|
1030
|
+
);
|
|
1031
|
+
const says = {
|
|
1032
|
+
off: 'no route line after a turn',
|
|
1033
|
+
moves: 'a route line after a turn that ran another model or effort than the last',
|
|
1034
|
+
all: 'a route line after every turn',
|
|
1035
|
+
};
|
|
1036
|
+
return `Route line ${mode}: ${says[mode]}.${saved ? '' : ' It could not be saved for later sessions.'}`;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
async function switchRouting($, router, mode) {
|
|
1040
|
+
const view = router.view ?? (await readView($, router));
|
|
1041
|
+
if (view.phase === 'unavailable') return { text: unavailableText(view) };
|
|
1042
|
+
await changeMode($, router, mode);
|
|
1043
|
+
return { text: mode === 'auto' ? 'Routing on.' : 'Routing off: Claude’s model is kept.' };
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// `/router [word [value]]`, the words of SUBCOMMANDS; with none, the pane, or the status text without a surface.
|
|
956
1047
|
async function routerCommand($, router, args) {
|
|
957
1048
|
const [action, value] = args.trim().split(/\s+/);
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
if (view.phase === 'unavailable') return { text: unavailableText(view) };
|
|
961
|
-
await changeMode($, router, action === 'auto' ? 'auto' : 'manual');
|
|
962
|
-
return { text: action === 'auto' ? 'Routing on.' : 'Routing off: Claude’s model is kept.' };
|
|
963
|
-
}
|
|
964
|
-
if (action === 'trace' && value === 'export') return { text: await exportTraces($, router) };
|
|
965
|
-
if (action === 'decisions') return { text: await decisionsText($, router, value) };
|
|
966
|
-
if (action === 'pin')
|
|
967
|
-
return { text: TIERS.includes(value) ? await setPin($, router, value) : `Choose ${TIERS.join(', ')}.` };
|
|
968
|
-
if (action === 'mode')
|
|
969
|
-
return {
|
|
970
|
-
text: [...ROUTING_MODES, 'default'].includes(value)
|
|
971
|
-
? await setRoutingMode($, router, value)
|
|
972
|
-
: `Choose mode ${[...ROUTING_MODES, 'default'].join(', ')}.`,
|
|
973
|
-
};
|
|
974
|
-
if (action === 'activities')
|
|
975
|
-
return {
|
|
976
|
-
text: ACTIVITY_MODES.includes(value)
|
|
977
|
-
? await setActivityRouting($, router, value)
|
|
978
|
-
: `Choose activities ${ACTIVITY_MODES.join(', ')}.`,
|
|
979
|
-
};
|
|
1049
|
+
const answer = await runSubcommand($, router, action, value);
|
|
1050
|
+
if (answer) return answer;
|
|
980
1051
|
const storedView = await readView($, router);
|
|
981
1052
|
const view = { ...storedView, mode: await modeOf($, router, storedView.mode) };
|
|
982
1053
|
if (!(await $.session.surfaces()).length)
|
|
@@ -1035,6 +1106,51 @@ async function credentialsOf($, options, config) {
|
|
|
1035
1106
|
);
|
|
1036
1107
|
}
|
|
1037
1108
|
|
|
1109
|
+
// A Set up or Edit press: in the terminal, the secure key dialog of `/plugin configure`; a graphical surface, which
|
|
1110
|
+
// has no such dialog (the command does nothing there), shows how to add the credentials from a terminal instead, in
|
|
1111
|
+
// the pane, for classifier `id`.
|
|
1112
|
+
async function keySetup($, router, press, id) {
|
|
1113
|
+
if (!isGraphical(press?.surface)) {
|
|
1114
|
+
openKeySettings($);
|
|
1115
|
+
return $.ui.close({ id: PANE });
|
|
1116
|
+
}
|
|
1117
|
+
await updateView($, router, { keyHelp: keyHelpFor(router, id ?? router.config.classifier), notice: null });
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// A graphical band or pane that throws draws the terminal tree; the error goes to the debug log, never the transcript.
|
|
1121
|
+
const logFallback = ($, what) => (error) =>
|
|
1122
|
+
$.ui.log(`graphical ${what} failed, drew the terminal one: ${error instanceof Error ? error.message : error}`, {
|
|
1123
|
+
to: 'debug',
|
|
1124
|
+
});
|
|
1125
|
+
|
|
1126
|
+
// The key help to open for classifier `id`: an Edit, when nothing is missing, stays open until Done.
|
|
1127
|
+
const keyHelpFor = (router, id) => ({
|
|
1128
|
+
id,
|
|
1129
|
+
edit: !missingCredentials(router.config, router.view ?? {}, id),
|
|
1130
|
+
});
|
|
1131
|
+
|
|
1132
|
+
// The plugin's id for `claude plugin configure`: from its install path, else settings' enabledPlugins.
|
|
1133
|
+
async function pluginIdFor($) {
|
|
1134
|
+
const plugin = { name: $.plugin.name, root: $.plugin.root };
|
|
1135
|
+
const settings = await $.settings.read().catch(() => ({}));
|
|
1136
|
+
return pluginIdOf(plugin, settings?.enabledPlugins ?? null);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// The pane's credential handlers: Set up or Edit, and the key help's Copy command and Done.
|
|
1140
|
+
function keyActions($, router, view) {
|
|
1141
|
+
return {
|
|
1142
|
+
key: (press, id) => keySetup($, router, press, id),
|
|
1143
|
+
copyKeyHelp: async (press) => {
|
|
1144
|
+
const open = (router.view ?? view).keyHelp;
|
|
1145
|
+
const help = open && keyHelpOf(router.config, open.id, await pluginIdFor($), { edit: open.edit });
|
|
1146
|
+
if (!help) return;
|
|
1147
|
+
await $.ui.copy({ text: help.command, surface: press?.surface });
|
|
1148
|
+
await updateView($, router, { notice: 'Copied' });
|
|
1149
|
+
},
|
|
1150
|
+
keyHelpDone: () => updateView($, router, { keyHelp: null, notice: null }),
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1038
1154
|
// The host refuses $.command.run from a hook the turn is holding, so the secure key dialog opens from a timer.
|
|
1039
1155
|
function openKeySettings($) {
|
|
1040
1156
|
$.clock.after(0, () => $.command.run({ command: 'plugin', args: `configure ${$.plugin.name}` }).catch(() => {}));
|
|
@@ -1649,8 +1765,7 @@ async function reviewMidTurn($, router, cfg, loop, e, { context, nativeModel, av
|
|
|
1649
1765
|
resolveCredentials(activeClassifier(cfg), (name) => settingOf($, router.options, name)),
|
|
1650
1766
|
$.session.messages({ as: 'api' }),
|
|
1651
1767
|
]);
|
|
1652
|
-
job
|
|
1653
|
-
job.prefix = historySignature(messages);
|
|
1768
|
+
Object.assign(job, { observedUpTo: messages.length, prefix: historySignature(messages) });
|
|
1654
1769
|
const facts = stepFacts(cfg, router, loop, e, messages, context);
|
|
1655
1770
|
clientOf(router, cfg.classifier)
|
|
1656
1771
|
.ask({
|
|
@@ -1679,7 +1794,17 @@ async function reviewMidTurn($, router, cfg, loop, e, { context, nativeModel, av
|
|
|
1679
1794
|
}
|
|
1680
1795
|
|
|
1681
1796
|
// 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.
|
|
1682
|
-
async function publishReply(
|
|
1797
|
+
async function publishReply(
|
|
1798
|
+
$,
|
|
1799
|
+
router,
|
|
1800
|
+
turnId,
|
|
1801
|
+
request,
|
|
1802
|
+
response,
|
|
1803
|
+
tier,
|
|
1804
|
+
route,
|
|
1805
|
+
routedUsd,
|
|
1806
|
+
cause = /** @type {string | null} */ (null),
|
|
1807
|
+
) {
|
|
1683
1808
|
const turn = router.turnActivities.get(turnId);
|
|
1684
1809
|
const metrics = responseMetrics(router.view, response, tier, Date.now());
|
|
1685
1810
|
await updateView($, router, {
|
|
@@ -1936,11 +2061,13 @@ async function submitPrompt(router, e, next) {
|
|
|
1936
2061
|
router.submitting = entry;
|
|
1937
2062
|
try {
|
|
1938
2063
|
const result = await next(e);
|
|
2064
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: turn.start sets `started` through router.submitting, an alias Biome cannot follow
|
|
1939
2065
|
if (!entry.started && typeof result?.text === 'string' && !result.drop)
|
|
1940
2066
|
router.origins = queueOrigin(router.origins, { kind: entry.kind, text: result.text, at: Date.now() });
|
|
1941
2067
|
return result;
|
|
1942
2068
|
} finally {
|
|
1943
|
-
|
|
2069
|
+
// Only the submission still current restores: a later one that started meanwhile keeps its entry.
|
|
2070
|
+
if (router.submitting === entry) router.submitting = outer;
|
|
1944
2071
|
}
|
|
1945
2072
|
}
|
|
1946
2073
|
|
|
@@ -1961,6 +2088,26 @@ function originOfTurn(router, text) {
|
|
|
1961
2088
|
return taken.kind;
|
|
1962
2089
|
}
|
|
1963
2090
|
|
|
2091
|
+
// A routed turn's route line: one record in `$.state`, one row placed at the transcript's end. Not for an interrupted
|
|
2092
|
+
// turn, a subagent's, or a turn with routing off, which has no route of its own to say. Display only, so a failure
|
|
2093
|
+
// here never breaks the turn, and a surface that cannot draw the row now is logged, not told.
|
|
2094
|
+
async function placeRouteLine($, router, e, turn) {
|
|
2095
|
+
try {
|
|
2096
|
+
const view = router.view;
|
|
2097
|
+
const route = routeOf(view);
|
|
2098
|
+
if (!turn || e.isAborted || !route || !view || view.phase === 'unavailable') return;
|
|
2099
|
+
if ((await modeOf($, router, view.mode)) === 'manual') return;
|
|
2100
|
+
const previous = router.lastRoute;
|
|
2101
|
+
router.lastRoute = route;
|
|
2102
|
+
const mode = lineModeOf(await $.store.get(ROUTE_LINE).catch(() => undefined));
|
|
2103
|
+
if (!showsLine(mode, previous, route)) return;
|
|
2104
|
+
const id = lineId(e.turnId);
|
|
2105
|
+
await $.state.set({ ...LINES, id }, lineOf(router.config, view, route));
|
|
2106
|
+
const placed = await $.ui.place({ id });
|
|
2107
|
+
if (!placed.isPlaced) $.ui.log(`router route line not drawn: ${placed.reason}`, { to: 'debug' });
|
|
2108
|
+
} catch {}
|
|
2109
|
+
}
|
|
2110
|
+
|
|
1964
2111
|
// A finished turn's decision rows and its end row, added to this session's record. Telemetry never breaks a turn: a
|
|
1965
2112
|
// failed read or write loses this turn's rows. A record from before the last reset reads as empty, and rows read
|
|
1966
2113
|
// before a Reset in this session are not written back.
|
|
@@ -1977,8 +2124,11 @@ async function flushDecisions($, router, turnId, turn) {
|
|
|
1977
2124
|
if (turn && turn.sessionId !== sessionId) return;
|
|
1978
2125
|
const key = `${DECISIONS_PREFIX}${sessionId}`;
|
|
1979
2126
|
const resetAt = await $.store.get(SAVINGS_RESET_KEY);
|
|
1980
|
-
if (router.decisionRecord?.sessionId !== sessionId)
|
|
1981
|
-
|
|
2127
|
+
if (router.decisionRecord?.sessionId !== sessionId) {
|
|
2128
|
+
const record = readDecisions(await $.store.get(key));
|
|
2129
|
+
// Another flush may have read it meanwhile and added its rows to it: that one stands.
|
|
2130
|
+
if (router.decisionRecord?.sessionId !== sessionId) router.decisionRecord = { sessionId, record };
|
|
2131
|
+
}
|
|
1982
2132
|
const held = router.decisionRecord;
|
|
1983
2133
|
const now = Date.now();
|
|
1984
2134
|
const end = turn
|
|
@@ -2065,12 +2215,12 @@ async function recordActivity($, router, turn) {
|
|
|
2065
2215
|
const start = promptIndex(messages);
|
|
2066
2216
|
if (start < 0) return;
|
|
2067
2217
|
const observed = observedActivity(messages, start);
|
|
2068
|
-
// The turn's end row reads it (flushDecisions).
|
|
2069
|
-
turn.observed = observed;
|
|
2070
2218
|
const view = router.view ?? (await readView($, router));
|
|
2071
2219
|
const manual = (await modeOf($, router)) === 'manual';
|
|
2072
2220
|
// The session may have changed during the reads; this turn is not the new session's.
|
|
2073
2221
|
if ((await $.session.id()) !== turn.sessionId) return;
|
|
2222
|
+
// The turn's end row reads it (flushDecisions).
|
|
2223
|
+
turn.observed = observed;
|
|
2074
2224
|
// Advanced before any further await, so a session change meanwhile cannot carry this run into the new session.
|
|
2075
2225
|
const { run, ended } = advanceRun(router.run, turn.answer);
|
|
2076
2226
|
router.run = run;
|
|
@@ -2212,6 +2362,17 @@ async function decideSpawn($, router, cfg, e, definition, signal) {
|
|
|
2212
2362
|
// Claude Code uses in place of the Agent call's `model` and the agent's frontmatter. Routing off or unavailable keeps
|
|
2213
2363
|
// every model Claude Code chooses, and records nothing.
|
|
2214
2364
|
async function spawnAgent($, router, e, next) {
|
|
2365
|
+
// A resumed agent keeps the model and the record of its first spawn: a hook can only refuse a resume. It runs again
|
|
2366
|
+
// from here, so the band's rows show it before its first reply.
|
|
2367
|
+
if (e.resume) {
|
|
2368
|
+
const result = await next(e);
|
|
2369
|
+
const agent = router.agents.get(e.resume.agentId);
|
|
2370
|
+
if (agent && !agent.running) {
|
|
2371
|
+
router.agents.set(e.resume.agentId, { ...agent, running: true });
|
|
2372
|
+
await showRunning($, router);
|
|
2373
|
+
}
|
|
2374
|
+
return result;
|
|
2375
|
+
}
|
|
2215
2376
|
if (!(await ensureInitialized($, router))) return next(e);
|
|
2216
2377
|
const cfg = router.config;
|
|
2217
2378
|
if (cfg.subagentRouting === 'off') return next(e);
|
|
@@ -2221,7 +2382,7 @@ async function spawnAgent($, router, e, next) {
|
|
|
2221
2382
|
const pass = passReason(cfg, e, definition);
|
|
2222
2383
|
const decision = pass ? null : await decideSpawn($, router, cfg, e, definition, next.signal).catch(() => null);
|
|
2223
2384
|
const routed = Boolean(decision?.model);
|
|
2224
|
-
const result = await next(
|
|
2385
|
+
const result = await next(decision?.model ? { ...e, model: decision.model } : e);
|
|
2225
2386
|
if (result.agentId && (pass || decision))
|
|
2226
2387
|
await noteSpawn($, router, { cfg, e, pass, decision, routed, result, definition }).catch(() => {});
|
|
2227
2388
|
return result;
|
|
@@ -2255,11 +2416,32 @@ async function noteSpawn($, router, { cfg, e, pass, decision, routed, result, de
|
|
|
2255
2416
|
await showRunning($, router);
|
|
2256
2417
|
}
|
|
2257
2418
|
|
|
2258
|
-
//
|
|
2259
|
-
// changes. An agent spawned with routing off has no record and is not
|
|
2419
|
+
// Which of the agents with a record are between their start or a step and their turn's end, and how many, in the view
|
|
2420
|
+
// when the set changes: the band draws a row for each. An agent spawned with routing off has no record and is not
|
|
2421
|
+
// listed.
|
|
2260
2422
|
async function showRunning($, router) {
|
|
2261
|
-
const
|
|
2262
|
-
if ((router.view?.
|
|
2423
|
+
const ids = [...router.agents].filter(([, agent]) => agent.running).map(([id]) => id);
|
|
2424
|
+
if ((router.view?.subagentIds ?? []).join(' ') !== ids.join(' '))
|
|
2425
|
+
await updateView($, router, { subagentsRunning: ids.length, subagentIds: ids });
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
// The band's rows: the agents with a record that Claude Code still lists as running, never a teammate, whose own
|
|
2429
|
+
// session the person opens from the tasks list. The list names the type and the task; the record the model.
|
|
2430
|
+
async function runningAgents($, router) {
|
|
2431
|
+
const listed = await $.agent.list().catch(() => []);
|
|
2432
|
+
return listed.flatMap((agent) => {
|
|
2433
|
+
const record = router.agents.get(agent.id);
|
|
2434
|
+
return record?.running && !agent.teammateId && ['running', 'waiting'].includes(agent.status)
|
|
2435
|
+
? [{ id: agent.id, type: agent.type, description: agent.description, record }]
|
|
2436
|
+
: [];
|
|
2437
|
+
});
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
// A press on an agent's row, or on Back, shows that transcript in the main view. Claude Code allows it only while the
|
|
2441
|
+
// call answers the person's own input, so a press is the one place to ask; a refusal is told.
|
|
2442
|
+
async function showView($, view) {
|
|
2443
|
+
const shown = await $.ui.view(view).catch(() => ({ deny: 'Could not switch the view.' }));
|
|
2444
|
+
if (shown.deny) $.ui.toast(shown.deny);
|
|
2263
2445
|
}
|
|
2264
2446
|
|
|
2265
2447
|
// A subagent's prompt-cache lifetime, by Claude Code's rule for requests outside the main conversation.
|
|
@@ -2326,6 +2508,14 @@ function traceConfigAdopted(router, loaded) {
|
|
|
2326
2508
|
}
|
|
2327
2509
|
}
|
|
2328
2510
|
|
|
2511
|
+
// The session row under the band, on or off. The band's button and the pane's setting share it; `view` is the view the
|
|
2512
|
+
// caller was drawn from, and `router.view` holds any press since.
|
|
2513
|
+
async function toggleBandDetail($, router, view) {
|
|
2514
|
+
const bandDetail = !(router.view ?? view).bandDetail;
|
|
2515
|
+
await $.store.set(BAND_DETAIL, bandDetail).catch(() => {});
|
|
2516
|
+
await updateView($, router, { bandDetail });
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2329
2519
|
// The pane's handlers. `view` is the view the pane was drawn from; a handler reads `router.view` first, which holds
|
|
2330
2520
|
// any press since.
|
|
2331
2521
|
function paneActions($, router, view) {
|
|
@@ -2426,14 +2616,12 @@ function paneActions($, router, view) {
|
|
|
2426
2616
|
mode: (mode) => changeMode($, router, mode),
|
|
2427
2617
|
tab: (tab) => updateView($, router, { tab, notice: null, confirmDelete: false }),
|
|
2428
2618
|
help: () => updateView($, router, { help: !(router.view ?? view).help }),
|
|
2619
|
+
toggleDetail: () => toggleBandDetail($, router, view),
|
|
2429
2620
|
pin: async (tier) => {
|
|
2430
2621
|
await updateView($, router, { notice: await setPin($, router, tier) });
|
|
2431
2622
|
},
|
|
2432
2623
|
unpin: () => updateView($, router, { pendingPin: null, notice: null }),
|
|
2433
|
-
|
|
2434
|
-
openKeySettings($);
|
|
2435
|
-
await $.ui.close({ id: PANE });
|
|
2436
|
-
},
|
|
2624
|
+
...keyActions($, router, view),
|
|
2437
2625
|
copyPath: async (press) => {
|
|
2438
2626
|
await $.ui.copy({ text: router.config.nativePath, surface: press?.surface });
|
|
2439
2627
|
await updateView($, router, { notice: 'Path copied.' });
|
|
@@ -2532,11 +2720,11 @@ function paneActions($, router, view) {
|
|
|
2532
2720
|
router.ledgers[kind] = { own: null, others: LEDGERS[kind].empty(), othersResetAt: resetAt };
|
|
2533
2721
|
return true;
|
|
2534
2722
|
};
|
|
2535
|
-
const cleared = await clearRecords().catch(() => false);
|
|
2536
2723
|
router.trace = null;
|
|
2537
2724
|
router.traceFreshSession = null;
|
|
2538
2725
|
router.traceClearSession = null;
|
|
2539
2726
|
router.traceOperations.clear();
|
|
2727
|
+
const cleared = await clearRecords().catch(() => false);
|
|
2540
2728
|
// The store kept the history: this session's records start over, and its next write replaces the stored one.
|
|
2541
2729
|
if (!cleared)
|
|
2542
2730
|
for (const kind of LEDGER_KINDS) {
|
|
@@ -2665,7 +2853,7 @@ async function imagesOf($, config, decision) {
|
|
|
2665
2853
|
}
|
|
2666
2854
|
|
|
2667
2855
|
function startError(supported, gateway) {
|
|
2668
|
-
if (!supported) return
|
|
2856
|
+
if (!supported) return `requires Claude Code ${MIN_CLAUDE_CODE} or newer`;
|
|
2669
2857
|
if (gateway) return GATEWAY_SETTINGS;
|
|
2670
2858
|
return null;
|
|
2671
2859
|
}
|
|
@@ -2756,7 +2944,7 @@ async function traceContinuation($, router, cfg, loop, e, traceInput) {
|
|
|
2756
2944
|
if (cfg.routingEngine === 'shadow') {
|
|
2757
2945
|
// Replay's incumbent may differ from the table's: retain unconditional modality and checkpoint evidence.
|
|
2758
2946
|
// Failure stays unknown, never a false assertion that the hypothetical route can accept this history.
|
|
2759
|
-
let replayMessages
|
|
2947
|
+
let replayMessages;
|
|
2760
2948
|
let actualHasImages = null;
|
|
2761
2949
|
try {
|
|
2762
2950
|
replayMessages = await transcript($);
|
|
@@ -2868,23 +3056,22 @@ async function initializeSession($, router) {
|
|
|
2868
3056
|
await $.command.register({
|
|
2869
3057
|
name: 'router',
|
|
2870
3058
|
description:
|
|
2871
|
-
'Open the Router pane, or switch routing: auto, off, pin <tier>, activities <mode>, mode <mode>; decisions [n] lists the last routing decisions.',
|
|
2872
|
-
argumentHint:
|
|
2873
|
-
'[auto|off|pin <tier>|activities off|on|mode cost|balanced|quality|default|decisions [n]|trace export]',
|
|
3059
|
+
'Open the Router pane, or switch routing: auto, off, pin <tier>, activities <mode>, mode <mode>, line <mode>; decisions [n] lists the last routing decisions.',
|
|
3060
|
+
argumentHint: argumentHint(),
|
|
2874
3061
|
immediate: true,
|
|
2875
3062
|
});
|
|
2876
3063
|
const model = await $.session.model();
|
|
2877
3064
|
try {
|
|
2878
|
-
|
|
3065
|
+
const config = await loadNativeConfig($);
|
|
3066
|
+
const project = await readProject($, config.nativePath);
|
|
3067
|
+
const existing = await readView($, router);
|
|
2879
3068
|
// A successful reload adopts this config even for the current turn; stale jobs must not route with the old engine.
|
|
3069
|
+
Object.assign(router, { config, project, view: existing });
|
|
2880
3070
|
for (const controller of router.controllers) controller.abort();
|
|
2881
3071
|
router.decisions.clear();
|
|
2882
3072
|
router.turnConfigs.clear();
|
|
2883
3073
|
for (const review of router.reviews.values()) review.controller.abort();
|
|
2884
3074
|
router.reviews.clear();
|
|
2885
|
-
router.project = await readProject($, router.config.nativePath);
|
|
2886
|
-
const existing = await readView($, router);
|
|
2887
|
-
router.view = existing;
|
|
2888
3075
|
const sameClassifier = existing.health?.classifier === router.config.classifier;
|
|
2889
3076
|
clientOf(router, router.config.classifier).restore(sameClassifier ? existing.health : null);
|
|
2890
3077
|
const base = await $.env.get('ANTHROPIC_BASE_URL');
|
|
@@ -2917,6 +3104,7 @@ async function initializeSession($, router) {
|
|
|
2917
3104
|
...(await storeView($, router)),
|
|
2918
3105
|
});
|
|
2919
3106
|
await pruneDecisionRecords($);
|
|
3107
|
+
// biome-ignore lint/nursery/useAtomicUpdates: ensureInitialized runs this once at a time, and the flag marks it done
|
|
2920
3108
|
router.initialized = true;
|
|
2921
3109
|
return true;
|
|
2922
3110
|
} catch (error) {
|
|
@@ -2928,6 +3116,29 @@ async function initializeSession($, router) {
|
|
|
2928
3116
|
}
|
|
2929
3117
|
}
|
|
2930
3118
|
|
|
3119
|
+
// What made routing unavailable, as the notice keys it: an old Claude Code, the v0.8 gateway settings, or a router.json
|
|
3120
|
+
// that did not load.
|
|
3121
|
+
function unavailableCause(error) {
|
|
3122
|
+
if (error?.startsWith('requires Claude Code')) return 'version';
|
|
3123
|
+
return error === GATEWAY_SETTINGS ? 'gateway' : 'config';
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
// One toast when a session starts with routing unavailable, at most once a day per cause: the band says it too, but not
|
|
3127
|
+
// once it is collapsed or scrolled away. Not for a headless run, which has no one to read it. The day is recorded before
|
|
3128
|
+
// the toast, so a toast no surface draws is not retried until tomorrow.
|
|
3129
|
+
async function noticeUnavailable($, router, e) {
|
|
3130
|
+
const view = router.view;
|
|
3131
|
+
if (view?.phase !== 'unavailable' || !e.isInteractive || !e.surface) return;
|
|
3132
|
+
try {
|
|
3133
|
+
const key = `${UNAVAILABLE_NOTICE_PREFIX}${unavailableCause(view.error)}`;
|
|
3134
|
+
const last = await $.store.get(key);
|
|
3135
|
+
const now = Date.now();
|
|
3136
|
+
if (Number.isFinite(last) && now - last >= 0 && now - last < UNAVAILABLE_NOTICE_INTERVAL_MS) return;
|
|
3137
|
+
await $.store.set(key, now);
|
|
3138
|
+
$.ui.toast(`Routing off: ${unavailableReason(view)}. Claude’s model is kept.`);
|
|
3139
|
+
} catch {}
|
|
3140
|
+
}
|
|
3141
|
+
|
|
2931
3142
|
async function ensureInitialized($, router) {
|
|
2932
3143
|
if (router.initialized) return true;
|
|
2933
3144
|
router.initializing ??= initializeSession($, router).finally(() => {
|
|
@@ -2946,8 +3157,10 @@ export function register(on, options) {
|
|
|
2946
3157
|
router.traceStartedSession = sessionId;
|
|
2947
3158
|
router.trace = null;
|
|
2948
3159
|
router.traceOperations.clear();
|
|
3160
|
+
router.lastRoute = null;
|
|
2949
3161
|
router.initialized = false;
|
|
2950
3162
|
await ensureInitialized($, router);
|
|
3163
|
+
await noticeUnavailable($, router, e);
|
|
2951
3164
|
return next(e);
|
|
2952
3165
|
});
|
|
2953
3166
|
|
|
@@ -2971,18 +3184,26 @@ export function register(on, options) {
|
|
|
2971
3184
|
|
|
2972
3185
|
on('command.run', { command: 'router' }, ($, e) => routerCommand($, router, e.args));
|
|
2973
3186
|
|
|
3187
|
+
// The words after `/router `, once one character is typed (the token is never empty). A matcher tests the token
|
|
3188
|
+
// alone, so the hook reads the text before it. Nothing is read or written but the router's own view.
|
|
3189
|
+
on('prompt.autocomplete', async (_$, e, next) => {
|
|
3190
|
+
const mine = routerSuggestions(e, {
|
|
3191
|
+
describe: (name, value) => (name === 'pin' && router.view ? pinCard(router.config, value) : undefined),
|
|
3192
|
+
});
|
|
3193
|
+
if (!mine.length) return next(e);
|
|
3194
|
+
return { suggestions: [...(await next(e)).suggestions, ...mine] };
|
|
3195
|
+
});
|
|
3196
|
+
|
|
3197
|
+
// Since 2.1.321 a subagent's loop raises turn.start too, with its agentId; the goal and prompt state is the main loop's.
|
|
2974
3198
|
on('turn.start', async ($, e, next) => {
|
|
2975
|
-
await startTurn($, router, e);
|
|
3199
|
+
if (e.agentId === undefined) await startTurn($, router, e);
|
|
2976
3200
|
return next(e);
|
|
2977
3201
|
});
|
|
2978
3202
|
|
|
2979
3203
|
on('prompt.submit', (_$, e, next) => submitPrompt(router, e, next));
|
|
2980
3204
|
|
|
2981
|
-
// A hook failure spawns the agent as Claude Code would; next(e) replays a spawn already made.
|
|
2982
|
-
|
|
2983
|
-
try {
|
|
2984
|
-
on('agent.spawn', ($, e, next) => spawnAgent($, router, e, next)).catch((_$, e, next) => next(e));
|
|
2985
|
-
} catch {}
|
|
3205
|
+
// A hook failure spawns the agent as Claude Code would; next(e) replays a spawn already made.
|
|
3206
|
+
on('agent.spawn', ($, e, next) => spawnAgent($, router, e, next)).catch((_$, e, next) => next(e));
|
|
2986
3207
|
|
|
2987
3208
|
on('turn.step', async function* ($, e, next) {
|
|
2988
3209
|
return yield* stepMain($, router, e, next);
|
|
@@ -2998,10 +3219,11 @@ export function register(on, options) {
|
|
|
2998
3219
|
router.reviews.get(e.turnId)?.controller.abort();
|
|
2999
3220
|
router.reviews.delete(e.turnId);
|
|
3000
3221
|
router.turnControllers.get(e.turnId)?.abort();
|
|
3001
|
-
|
|
3002
|
-
|
|
3222
|
+
const held = router.view;
|
|
3223
|
+
if (held !== null && held.activeTurnId === e.turnId && held.phase === 'choosing') {
|
|
3224
|
+
router.view = { ...held, activeTurnId: null };
|
|
3003
3225
|
const mode = await modeOf($, router);
|
|
3004
|
-
if (router.view
|
|
3226
|
+
if (router.view?.activeTurnId === null)
|
|
3005
3227
|
await updateView($, router, { phase: mode === 'manual' ? 'manual' : 'ready', reason: 'interrupted' });
|
|
3006
3228
|
}
|
|
3007
3229
|
router.prompts.delete(e.turnId);
|
|
@@ -3009,6 +3231,7 @@ export function register(on, options) {
|
|
|
3009
3231
|
// `off` asks nothing, so it records nothing; it only ends the watch after a cheaper activity move.
|
|
3010
3232
|
const turn = router.turnActivities.get(e.turnId);
|
|
3011
3233
|
router.turnActivities.delete(e.turnId);
|
|
3234
|
+
await placeRouteLine($, router, e, turn);
|
|
3012
3235
|
if (turn?.mode === 'off') {
|
|
3013
3236
|
if ((await $.session.id()) === turn.sessionId) router.afterDown = advanceDown(router.afterDown, turn).after;
|
|
3014
3237
|
} else if (turn) await recordActivity($, router, turn);
|
|
@@ -3026,6 +3249,7 @@ export function register(on, options) {
|
|
|
3026
3249
|
const result = await next(e);
|
|
3027
3250
|
// Only the main conversation is routed, so an agent compaction changes nothing here.
|
|
3028
3251
|
if (!result.skip && e.trigger !== 'precompute' && !e.agentId) {
|
|
3252
|
+
// biome-ignore lint/suspicious/noUnnecessaryConditions: router.trace is set by helpers that take `router`, which Biome cannot follow
|
|
3029
3253
|
if (!router.trace) {
|
|
3030
3254
|
router.traceFreshSession = null;
|
|
3031
3255
|
router.traceClearSession = null;
|
|
@@ -3056,6 +3280,7 @@ export function register(on, options) {
|
|
|
3056
3280
|
if (router.trace?.turns.size) router.trace.reasons.add('aborted');
|
|
3057
3281
|
await flushTrace($, router, null, true);
|
|
3058
3282
|
}
|
|
3283
|
+
// biome-ignore lint/nursery/useAtomicUpdates: the session is over, so whatever trace the final flush held or created is dropped
|
|
3059
3284
|
router.trace = null;
|
|
3060
3285
|
router.traceOperations.clear();
|
|
3061
3286
|
router.traceFreshSession = null;
|
|
@@ -3077,6 +3302,18 @@ export function register(on, options) {
|
|
|
3077
3302
|
return label ? next({ ...e, props: { ...e.props, modes: [...e.props.modes, label] } }) : next(e);
|
|
3078
3303
|
});
|
|
3079
3304
|
|
|
3305
|
+
// A route line: drawn from the record the turn's end kept under the row's id. Rows other plugins placed pass.
|
|
3306
|
+
on('ui.render', { component: 'Placed' }, async ($, e, next) => {
|
|
3307
|
+
if (e.props.plugin !== $.plugin.name) return next(e);
|
|
3308
|
+
const { value: line } = await $.state.get({ ...LINES, id: e.props.id });
|
|
3309
|
+
if (!line) return next(e);
|
|
3310
|
+
return renderRouteLine($.ui.resolve(e), router.config, line, {
|
|
3311
|
+
columns: e.viewport?.columns,
|
|
3312
|
+
surface: e.surface,
|
|
3313
|
+
onFallback: logFallback($, 'route line'),
|
|
3314
|
+
});
|
|
3315
|
+
});
|
|
3316
|
+
|
|
3080
3317
|
on('ui.render', { component: ['AbovePrompt', 'Pane'] }, async ($, e, next) => {
|
|
3081
3318
|
if (e.component === 'Pane' && e.requestId !== PANE) return next(e);
|
|
3082
3319
|
const storedView = await readView($, router);
|
|
@@ -3097,33 +3334,49 @@ export function register(on, options) {
|
|
|
3097
3334
|
usage,
|
|
3098
3335
|
{
|
|
3099
3336
|
columns: e.props.bodyColumns,
|
|
3337
|
+
surface: e.surface,
|
|
3338
|
+
onFallback: logFallback($, 'band'),
|
|
3100
3339
|
agentId: e.props.view?.agentId,
|
|
3101
3340
|
agent: router.agents.get(e.props.view?.agentId) ?? null,
|
|
3341
|
+
agents: e.props.view?.agentId ? [] : await runningAgents($, router),
|
|
3342
|
+
back: true,
|
|
3102
3343
|
},
|
|
3103
3344
|
{
|
|
3104
3345
|
open: () => openPane($, router),
|
|
3346
|
+
viewAgent: (agentId) => showView($, { agentId }),
|
|
3347
|
+
back: () => showView($, {}),
|
|
3105
3348
|
mode: (mode) => changeMode($, router, mode),
|
|
3106
3349
|
pin: async (tier) => $.ui.toast(await setPin($, router, tier)),
|
|
3107
3350
|
unpin: () => updateView($, router, { pendingPin: null }),
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
await
|
|
3112
|
-
await updateView($, router, {
|
|
3351
|
+
// On a graphical surface the band's Set up opens the pane on how to add the credentials.
|
|
3352
|
+
key: async (press) => {
|
|
3353
|
+
if (!isGraphical(press?.surface ?? e.surface)) return openKeySettings($);
|
|
3354
|
+
await openPane($, router);
|
|
3355
|
+
await updateView($, router, { keyHelp: keyHelpFor(router, router.config.classifier) });
|
|
3113
3356
|
},
|
|
3357
|
+
toggleDetail: () => toggleBandDetail($, router, view),
|
|
3114
3358
|
},
|
|
3115
3359
|
),
|
|
3116
3360
|
],
|
|
3117
3361
|
});
|
|
3118
3362
|
}
|
|
3119
3363
|
view.decisionRows = await paneDecisionRows($, router);
|
|
3364
|
+
// A Set up's key help hides by itself once the credentials it asks for are there; an Edit's waits for Done.
|
|
3365
|
+
const open = view.keyHelp;
|
|
3366
|
+
const keyHelp =
|
|
3367
|
+
open?.id && router.config.classifiers[open.id] && (open.edit || missingCredentials(router.config, view, open.id))
|
|
3368
|
+
? open
|
|
3369
|
+
: null;
|
|
3120
3370
|
const settings = await $.settings.read().catch(() => ({}));
|
|
3121
3371
|
const modelOptions = Object.keys(router.config.models).filter((alias) =>
|
|
3122
3372
|
isModelAllowed(router.config.models[alias].id, settings.availableModels, view.nativeModel),
|
|
3123
3373
|
);
|
|
3124
|
-
return renderPanel(elements, router.config, view, usage, paneActions($, router, view), {
|
|
3374
|
+
return renderPanel(elements, router.config, { ...view, keyHelp }, usage, paneActions($, router, view), {
|
|
3125
3375
|
modelOptions,
|
|
3126
3376
|
columns: e.props.bodyColumns,
|
|
3377
|
+
surface: e.surface,
|
|
3378
|
+
onFallback: logFallback($, 'pane'),
|
|
3379
|
+
pluginId: pluginIdOf({ name: $.plugin.name, root: $.plugin.root }, settings?.enabledPlugins ?? null),
|
|
3127
3380
|
});
|
|
3128
3381
|
});
|
|
3129
3382
|
}
|