@alexeiled/claude-router 0.8.0 → 1.1.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.
Files changed (48) hide show
  1. package/.claude-plugin/plugin.json +11 -7
  2. package/CHANGELOG.md +43 -0
  3. package/README.md +55 -98
  4. package/docs/architecture.md +55 -419
  5. package/docs/configuration.md +88 -182
  6. package/docs/evaluation.md +62 -81
  7. package/docs/native-router.md +80 -0
  8. package/docs/router-band.svg +119 -0
  9. package/docs/router-pane-now.svg +85 -0
  10. package/docs/router-pane-tiers.svg +62 -0
  11. package/docs/user-guide.md +127 -172
  12. package/hooks/hooks.json +1 -26
  13. package/hooks/native-router.mjs +701 -0
  14. package/lib/config.mjs +97 -63
  15. package/lib/{facts.mjs → facts-pure.mjs} +2 -23
  16. package/lib/{jev.mjs → jev-contract.mjs} +17 -38
  17. package/lib/native-band.mjs +206 -0
  18. package/lib/native-cost.mjs +88 -0
  19. package/lib/native-display.mjs +122 -0
  20. package/lib/native-jev.mjs +125 -0
  21. package/lib/native-panel.mjs +492 -0
  22. package/lib/native-router.mjs +221 -0
  23. package/lib/policy.mjs +17 -21
  24. package/package.json +10 -8
  25. package/scripts/migrate-config.mjs +57 -0
  26. package/types/index.d.ts +112 -0
  27. package/lib/cost.mjs +0 -93
  28. package/lib/gateway.mjs +0 -221
  29. package/lib/idle.mjs +0 -41
  30. package/lib/rewrite.mjs +0 -130
  31. package/lib/router.mjs +0 -276
  32. package/lib/runtime.mjs +0 -78
  33. package/lib/sse.mjs +0 -57
  34. package/lib/status.mjs +0 -152
  35. package/lib/store.mjs +0 -76
  36. package/scripts/ensure-gateway.mjs +0 -143
  37. package/scripts/gateway.mjs +0 -81
  38. package/scripts/git-hooks/pre-commit +0 -41
  39. package/scripts/git-hooks/pre-push +0 -34
  40. package/scripts/status.mjs +0 -20
  41. package/scripts/statusline.mjs +0 -45
  42. package/scripts/transcript-models.sh +0 -4
  43. package/skills/high/SKILL.md +0 -9
  44. package/skills/low/SKILL.md +0 -7
  45. package/skills/medium/SKILL.md +0 -8
  46. package/skills/micro/SKILL.md +0 -7
  47. package/skills/setup/SKILL.md +0 -31
  48. package/skills/status/SKILL.md +0 -12
package/lib/config.mjs CHANGED
@@ -2,36 +2,22 @@
2
2
 
3
3
  export const TIERS = ['micro', 'low', 'medium', 'high'];
4
4
  export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
5
- // Request features that Claude Code uses with its own models and that only some models accept (a beta each):
6
- // `system` messages after the prompt, per-turn control, and tool additions or removals in the history.
7
- export const FEATURES = ['mid-conversation-system', 'per-turn-control', 'mid-conversation-tool-changes'];
8
-
9
- // The alias before 0.4.2. Settings written by an older /router:setup still send it; drop it once they are rare.
10
- export const LEGACY_ALIAS = 'router';
11
-
12
5
  export const DEFAULTS = {
13
- // `idleShutdownMs`: the gateway exits after this long without requests while no turn waits for a tool result;
14
- // the hooks start it again before the next prompt. Two hours outlast /loop wakeups and Monitor waits (up to one
15
- // hour), which reach the gateway without a prompt. 0 keeps it running.
16
- gateway: { port: 43170, alias: 'jev-router', baselineTier: 'low', auxiliaryTier: 'low', idleShutdownMs: 7_200_000 },
17
- // Sonnet 5.5 is the worker: `low` at the effort the user picked, `medium` at xhigh, so an escalation from `low`
18
- // stays on Sonnet's cache instead of a cold write to Opus. `high` is Opus 5.5 at xhigh, the strongest setting this
19
- // router asks for: Anthropic keeps Opus for open-ended, long-horizon work. xhigh on Sonnet is not yet measured
20
- // against Opus at high; the `observed` lines in decisions.jsonl are the input for that.
6
+ baselineTier: 'low',
7
+ // Sonnet 5.5 is the worker at `low`, at the effort the user picked. `medium` is Opus 5.5 at medium effort, a balance
8
+ // of speed and cost per task; `high` is Opus 5.5 at xhigh, the strongest setting this router asks for.
9
+ // These are routing defaults, not a claim of equal model quality.
21
10
  routes: {
22
11
  high: { model: 'opus', effort: 'xhigh' },
23
- medium: { model: 'sonnet', effort: 'xhigh' },
12
+ medium: { model: 'opus', effort: 'medium' },
24
13
  low: { model: 'sonnet' },
25
14
  micro: { model: 'haiku' },
26
15
  },
27
- // `id` is sent upstream verbatim. List prices in USD per million tokens; `cacheRead` is absolute,
16
+ // `id` selects the native model. List prices in USD per million tokens; `cacheRead` is absolute,
28
17
  // not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). A price change must also change
29
18
  // test/fixtures/list-prices.json, with its source and date. `output` feeds the shadow estimate and the downgrade tax.
30
19
  // `efforts` lists what the model accepts; an empty list means no effort field and no adaptive thinking. A change
31
20
  // must also change test/fixtures/effort-support.json, from a new probe against the real API.
32
- // `features` lists the FEATURES the model accepts; the gateway removes the others from the request. A model
33
- // without the field gets none: a removed feature is lost, a feature the model rejects fails the turn. A change
34
- // must also change test/fixtures/model-features.json, from a new probe.
35
21
  models: {
36
22
  opus: {
37
23
  id: 'claude-opus-5-5',
@@ -41,7 +27,6 @@ export const DEFAULTS = {
41
27
  contextWindow: 1_000_000,
42
28
  billing: 'plan',
43
29
  efforts: EFFORTS,
44
- features: FEATURES,
45
30
  },
46
31
  sonnet: {
47
32
  id: 'claude-sonnet-5-5',
@@ -51,19 +36,15 @@ export const DEFAULTS = {
51
36
  contextWindow: 1_000_000,
52
37
  billing: 'plan',
53
38
  efforts: EFFORTS,
54
- features: FEATURES,
55
39
  },
56
- // `maxOutput` caps `max_tokens`: Claude Code asks for 128K, Haiku 4.5 answers more than 64K with 400.
57
40
  haiku: {
58
41
  id: 'claude-haiku-4-5',
59
42
  input: 1,
60
43
  output: 5,
61
44
  cacheRead: 0.1,
62
45
  contextWindow: 200_000,
63
- maxOutput: 64_000,
64
46
  billing: 'plan',
65
47
  efforts: [],
66
- features: [],
67
48
  },
68
49
  },
69
50
  cache: {
@@ -90,37 +71,93 @@ export const DEFAULTS = {
90
71
  },
91
72
  jev: { endpoint: 'https://api.typesafe.ai/v1/systemone', model: 'jev-1.13.0', timeoutMs: 1500 },
92
73
  context: { recentTurns: 6, maxTextChars: 1200 },
93
- log: true,
94
74
  };
95
75
 
96
76
  const API_KEY_VAR = 'TYPESAFE_API_KEY';
97
77
 
98
78
  // Keys a route or a model entry may carry. The other closed sections take their key sets from DEFAULTS.
99
79
  const ROUTE_KEYS = ['model', 'effort'];
100
- const MODEL_KEYS = [
101
- 'id',
102
- 'input',
103
- 'output',
104
- 'cacheRead',
105
- 'contextWindow',
106
- 'maxOutput',
107
- 'billing',
108
- 'efforts',
109
- 'features',
110
- ];
80
+ const MODEL_KEYS = ['id', 'input', 'output', 'cacheRead', 'contextWindow', 'billing', 'efforts'];
111
81
  // A JSON file can hold these as own keys; merged into a plain object they reach the prototype chain.
112
82
  const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
113
83
 
114
- // `forcedTier` skips Jev and the policy. It comes from the daemon's --force-tier flag, never from env: a project's
115
- // settings can set a hook's env, and a forced tier changes spend.
116
- export function loadConfig({ env = {}, userFile = null, forcedTier = null } = {}) {
84
+ export function loadConfig({ env = {}, userFile = null } = {}) {
117
85
  if (userFile !== null) checkShape(userFile);
118
86
  const config = merge(DEFAULTS, userFile ?? {});
119
87
  validate(config);
120
88
  const apiKey = env[API_KEY_VAR]?.trim() || null;
121
- if (forcedTier !== null && !TIERS.includes(forcedTier))
122
- throw new Error(`the forced tier must be one of ${TIERS.join(', ')}`);
123
- return { ...config, apiKey, forcedTier };
89
+ return { ...config, apiKey };
90
+ }
91
+
92
+ export const sameRoute = (a, b) => a.model === b.model && (a.effort ?? null) === (b.effort ?? null);
93
+
94
+ // A route draft carries `base`, the routes and baseline it was made from. A tier counts as edited when it differs
95
+ // from its base; everything else follows `current`, so a change saved elsewhere meanwhile is neither shown stale
96
+ // nor written back.
97
+ export function effectiveRoutes(draft, current) {
98
+ const { base } = draft;
99
+ return {
100
+ routes: Object.fromEntries(
101
+ TIERS.map((tier) => [
102
+ tier,
103
+ sameRoute(draft.routes[tier], base.routes[tier]) ? current.routes[tier] : draft.routes[tier],
104
+ ]),
105
+ ),
106
+ baselineTier: draft.baselineTier === base.baselineTier ? current.baselineTier : draft.baselineTier,
107
+ };
108
+ }
109
+
110
+ // The router.json `file` with the tiers and baseline the person edited in `draft`; everything else stays as the
111
+ // file has it now, so an edit made on disk meanwhile survives. Only differences from DEFAULTS are written, so a later
112
+ // change of a default still reaches tiers the user never edited; a route equal to the default is removed.
113
+ export function withRoutes(file, draft) {
114
+ const { base } = draft;
115
+ const { routes: kept = {}, baselineTier: keptBaseline, ...rest } = file;
116
+ const routes = {};
117
+ for (const tier of TIERS) {
118
+ if (sameRoute(draft.routes[tier], base.routes[tier])) {
119
+ // Untouched here: keep the file's own entry verbatim, so an inherited effort stays inherited.
120
+ if (kept[tier]) routes[tier] = kept[tier];
121
+ continue;
122
+ }
123
+ const { model, effort = null } = draft.routes[tier];
124
+ const preset = DEFAULTS.routes[tier];
125
+ if (model === preset.model && effort === (preset.effort ?? null)) continue;
126
+ routes[tier] = effort !== null || preset.effort ? { model, effort } : { model };
127
+ }
128
+ const baseline =
129
+ draft.baselineTier === base.baselineTier
130
+ ? keptBaseline
131
+ : draft.baselineTier === DEFAULTS.baselineTier
132
+ ? undefined
133
+ : draft.baselineTier;
134
+ return {
135
+ ...rest,
136
+ ...(Object.keys(routes).length ? { routes } : {}),
137
+ ...(baseline === undefined ? {} : { baselineTier: baseline }),
138
+ };
139
+ }
140
+
141
+ // The pane's tuning controls and the router.json fields they stand for.
142
+ const TUNING_FIELDS = {
143
+ timeoutMs: ['jev', 'timeoutMs'],
144
+ downgradeVotes: ['policy', 'downgradeVotes'],
145
+ horizon: ['policy', 'downgradeHorizonTurns'],
146
+ cashCapUsd: ['policy', 'cashCapUsd'],
147
+ };
148
+
149
+ export function tuningOf(config) {
150
+ return Object.fromEntries(
151
+ Object.entries(TUNING_FIELDS).map(([key, [section, field]]) => [key, config[section][field]]),
152
+ );
153
+ }
154
+
155
+ // The router.json `file` with only the tuning values the person changed in `draft` against `saved`.
156
+ export function withTuning(file, draft, saved) {
157
+ let next = file;
158
+ for (const [key, [section, field]] of Object.entries(TUNING_FIELDS))
159
+ if (draft[key] !== saved[key]) next = { ...next, [section]: { ...next[section], [field]: draft[key] } };
160
+ return next;
124
161
  }
125
162
 
126
163
  export function rank(tier) {
@@ -142,9 +179,20 @@ function merge(base, override) {
142
179
  // The raw file, before the merge: only known keys, and every section an object. Messages name the path, never the
143
180
  // value. An open map (model aliases, cache TTL labels) takes any key but the forbidden ones.
144
181
  function checkShape(file) {
182
+ if (
183
+ file &&
184
+ typeof file === 'object' &&
185
+ (Object.hasOwn(file, 'gateway') ||
186
+ Object.hasOwn(file, 'log') ||
187
+ Object.values(file.models ?? {}).some(
188
+ (model) => model && (Object.hasOwn(model, 'features') || Object.hasOwn(model, 'maxOutput')),
189
+ ))
190
+ )
191
+ throw new Error(
192
+ 'router.json uses gateway settings; run node scripts/migrate-config.mjs /path/to/router.json from the router plugin directory',
193
+ );
145
194
  checkObject(file, 'router.json', Object.keys(DEFAULTS));
146
- const { gateway, routes, models, cache, policy, jev, context } = file;
147
- if (gateway !== undefined) checkObject(gateway, 'gateway', Object.keys(DEFAULTS.gateway));
195
+ const { routes, models, cache, policy, jev, context } = file;
148
196
  if (routes !== undefined) {
149
197
  checkObject(routes, 'routes', TIERS);
150
198
  for (const [tier, route] of Object.entries(routes)) checkObject(route, `routes.${tier}`, ROUTE_KEYS);
@@ -181,8 +229,9 @@ function validate(config) {
181
229
  const route = config.routes[tier];
182
230
  if (!route || typeof route.model !== 'string') throw new Error(`routes.${tier}.model is required`);
183
231
  if (!Object.hasOwn(config.models, route.model)) throw new Error(`routes.${tier}.model is not in models`);
184
- if (route.effort !== undefined && !EFFORTS.includes(route.effort))
185
- throw new Error(`routes.${tier}.effort must be one of ${EFFORTS.join(', ')}`);
232
+ // null keeps the session effort; it is how a file overrides a default route that names one.
233
+ if (route.effort != null && !EFFORTS.includes(route.effort))
234
+ throw new Error(`routes.${tier}.effort must be one of ${EFFORTS.join(', ')} or null`);
186
235
  }
187
236
  for (const [alias, model] of Object.entries(config.models)) {
188
237
  for (const field of ['input', 'cacheRead', 'contextWindow']) {
@@ -192,26 +241,12 @@ function validate(config) {
192
241
  if (model.output !== undefined && !(Number.isFinite(model.output) && model.output >= 0))
193
242
  throw new Error(`models.${alias}.output must be a non-negative number`);
194
243
  if (typeof model.id !== 'string' || !model.id) throw new Error(`models.${alias}.id is required`);
195
- if (model.maxOutput !== undefined && !(Number.isInteger(model.maxOutput) && model.maxOutput > 0))
196
- throw new Error(`models.${alias}.maxOutput must be a positive integer`);
197
244
  if (!['plan', 'credits'].includes(model.billing))
198
245
  throw new Error(`models.${alias}.billing must be plan or credits`);
199
246
  if (!Array.isArray(model.efforts) || model.efforts.some((e) => !EFFORTS.includes(e)))
200
247
  throw new Error(`models.${alias}.efforts must list valid effort levels`);
201
- if (
202
- model.features !== undefined &&
203
- !(Array.isArray(model.features) && model.features.every((f) => FEATURES.includes(f)))
204
- )
205
- throw new Error(`models.${alias}.features must list known features (${FEATURES.join(', ')})`);
206
- }
207
- for (const key of ['baselineTier', 'auxiliaryTier']) {
208
- if (!TIERS.includes(config.gateway[key])) throw new Error(`gateway.${key} must be one of ${TIERS.join(', ')}`);
209
248
  }
210
- if (!Number.isInteger(config.gateway.port) || config.gateway.port < 1 || config.gateway.port > 65535)
211
- throw new Error('gateway.port must be a port number');
212
- if (typeof config.gateway.alias !== 'string' || !config.gateway.alias) throw new Error('gateway.alias is required');
213
- if (!(Number.isFinite(config.gateway.idleShutdownMs) && config.gateway.idleShutdownMs >= 0))
214
- throw new Error('gateway.idleShutdownMs must be a non-negative number (0 keeps the gateway running)');
249
+ if (!TIERS.includes(config.baselineTier)) throw new Error(`baselineTier must be one of ${TIERS.join(', ')}`);
215
250
  const p = config.policy;
216
251
  for (const field of ['upgradeBase', 'jumpConfidence', 'downgradeMass', 'continuationMass']) {
217
252
  if (!(isNumber(p[field], 0) && p[field] <= 1)) throw new Error(`policy.${field} must be between 0 and 1`);
@@ -238,5 +273,4 @@ function validate(config) {
238
273
  for (const field of ['recentTurns', 'maxTextChars']) {
239
274
  if (!isNumber(context[field], 1, { integer: true })) throw new Error(`context.${field} must be a positive integer`);
240
275
  }
241
- if (typeof config.log !== 'boolean') throw new Error('log must be true or false');
242
276
  }
@@ -1,16 +1,12 @@
1
1
  // Facts the policy needs, derived from one Messages request body plus what the router remembers
2
2
  // about the conversation. Pure: body and memory in, plain object out.
3
- import { createHash } from 'node:crypto';
4
3
 
5
4
  const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit', 'Bash']);
6
5
  const FAILURE_WINDOW = 40;
7
6
  const REMINDER = /<system-reminder>[\s\S]*?<\/system-reminder>/g;
8
7
  const CLIP_MARKER = ' […] ';
9
- // A typed /router:<tier> reaches the gateway as a command block at the start of a text block. Anchored, so a pasted
10
- // file or quoted log that mentions the marker cannot pin a tier.
11
- const PIN = /^<command-message>router:([a-z]+)<\/command-message>/;
12
8
 
13
- export function factsFromRequest(body, memory, { recentTurns, maxTextChars }) {
9
+ export function extractFacts(body, memory, { recentTurns, maxTextChars }) {
14
10
  const messages = Array.isArray(body.messages) ? body.messages : [];
15
11
  // Claude Code puts hook output and tool additions in `system` messages after the user message, and keeps them in
16
12
  // the history: the turn is the last user or assistant message.
@@ -38,11 +34,6 @@ export function factsFromRequest(body, memory, { recentTurns, maxTextChars }) {
38
34
  turns: turns.slice(-recentTurns),
39
35
  prompt: current === -1 ? '' : clip(textOf(lastBlocks), maxTextChars),
40
36
  continuation: last?.role === 'user' && lastBlocks.some((b) => b.type === 'tool_result'),
41
- // The tier named by a /router:<tier> in the current message; the router checks that it is a tier.
42
- pin: current === -1 ? null : pinOf(lastBlocks),
43
- // The same position and the same last user message: Claude Code resent the request (429, 529, a dropped stream, or
44
- // a 400 it answers by dropping a feature from the system message after it).
45
- turnKey: last?.role === 'user' ? `${lastIndex + 1}:${hash(JSON.stringify(last.content))}` : null,
46
37
  failure: repeatedFailure(errors, edits, messages.length - 1),
47
38
  // The effort Claude Code sent: a route without its own effort keeps it, and it is part of the cache key.
48
39
  effort: body.output_config?.effort ?? null,
@@ -66,7 +57,7 @@ function repeatedFailure(errors, edits, lastIndex) {
66
57
  }
67
58
 
68
59
  // At most `max` characters: the head and the tail of a long text, where a request and its question usually are.
69
- function clip(text, max) {
60
+ export function clip(text, max) {
70
61
  if (text.length <= max) return text;
71
62
  if (max <= CLIP_MARKER.length) return text.slice(0, max);
72
63
  const room = max - CLIP_MARKER.length;
@@ -78,10 +69,6 @@ function signatureOf(block) {
78
69
  return textOf(blocks(block.content)).toLowerCase().replace(/\d+/g, '#').replace(/\s+/g, ' ').slice(0, 120);
79
70
  }
80
71
 
81
- function hash(text) {
82
- return createHash('sha256').update(text).digest('hex').slice(0, 16);
83
- }
84
-
85
72
  function blocks(content) {
86
73
  if (typeof content === 'string') return [{ type: 'text', text: content }];
87
74
  return Array.isArray(content) ? content : [];
@@ -94,11 +81,3 @@ function textOf(content) {
94
81
  .join('\n')
95
82
  .trim();
96
83
  }
97
-
98
- function pinOf(content) {
99
- for (const block of content) {
100
- const match = block.type === 'text' && typeof block.text === 'string' ? PIN.exec(block.text) : null;
101
- if (match) return match[1];
102
- }
103
- return null;
104
- }
@@ -1,4 +1,4 @@
1
- // One bounded TypeSafe System One request per user turn. Transport is injected; nothing here knows the transcript.
1
+ // Pure Jev wire contract: request body, answer parser and retry rules.
2
2
  import { TIERS } from './config.mjs';
3
3
 
4
4
  const CRITERIA = {
@@ -33,9 +33,14 @@ const ROUTE_INSTRUCTIONS = {
33
33
  };
34
34
  const CONTINUATION_INSTRUCTIONS =
35
35
  'Is `currentRequest.text` a continuation of the task in `recentDialogue` (for example "continue", "yes", "now fix the tests"), rather than a new task?';
36
+ export const RETRY_DELAY_MS = 100;
37
+ export const JEV_FAILURES_TO_PAUSE = 3;
38
+ export const JEV_PAUSE_MS = 60_000;
36
39
  const TRANSIENT = new Set([408, 429, 500, 502, 503, 504]);
37
- const RETRY_DELAY_MS = 100;
38
- const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
40
+
41
+ export function isTransientStatus(status) {
42
+ return TRANSIENT.has(status);
43
+ }
39
44
 
40
45
  export function buildRequest(config, prompt, turns) {
41
46
  const criteria = {};
@@ -51,40 +56,8 @@ export function buildRequest(config, prompt, turns) {
51
56
  };
52
57
  }
53
58
 
54
- // Returns { choice, confidence, probabilities, continuation } or throws. One retry on a network error or a transient
55
- // status, after Retry-After when the server sends one; a wait that would not fit the budget is not taken.
56
- export async function askJev({ fetchFn, config, apiKey, prompt, turns, now = Date.now, sleep = delay }) {
57
- const deadline = now() + config.jev.timeoutMs;
58
- const body = JSON.stringify(buildRequest(config, prompt, turns));
59
- for (let attempt = 0; attempt < 2; attempt += 1) {
60
- const remaining = deadline - now();
61
- if (remaining <= 0) throw new Error('jev timeout');
62
- let response;
63
- try {
64
- response = await fetchFn(config.jev.endpoint, {
65
- method: 'POST',
66
- headers: { Authorization: `Bearer ${apiKey}`, 'content-type': 'application/json' },
67
- body,
68
- signal: AbortSignal.timeout(remaining),
69
- });
70
- } catch (error) {
71
- if (error.name === 'TimeoutError') throw new Error('jev timeout');
72
- if (attempt === 1) throw new Error(`jev unreachable: ${error.message}`);
73
- await sleep(RETRY_DELAY_MS);
74
- continue;
75
- }
76
- if (response.ok) return parseAnswers(await response.json());
77
- if (!TRANSIENT.has(response.status) || attempt === 1) throw new Error(`jev http ${response.status}`);
78
- const wait = retryAfterMs(response.headers, now()) ?? RETRY_DELAY_MS;
79
- if (wait >= deadline - now()) throw new Error(`jev http ${response.status}, retry-after beyond the budget`);
80
- await sleep(wait);
81
- }
82
- throw new Error('jev unreachable');
83
- }
84
-
85
59
  // Delay-seconds or an HTTP-date (RFC 9110). Seconds first: Date.parse also reads a bare number, as a year.
86
- function retryAfterMs(headers, now) {
87
- const value = headers?.get?.('retry-after');
60
+ export function retryDelayMs(value, now) {
88
61
  if (value === null || value === undefined || value.trim() === '') return null;
89
62
  const seconds = Number(value);
90
63
  if (Number.isFinite(seconds)) return seconds >= 0 ? seconds * 1000 : null;
@@ -94,9 +67,15 @@ function retryAfterMs(headers, now) {
94
67
 
95
68
  export function parseAnswers(json) {
96
69
  const route = json?.answers?.route;
97
- if (route?.type !== 'choice' || typeof route.probabilities !== 'object') throw new Error('jev malformed answer');
70
+ if (
71
+ route?.type !== 'choice' ||
72
+ !route.probabilities ||
73
+ typeof route.probabilities !== 'object' ||
74
+ Array.isArray(route.probabilities)
75
+ )
76
+ throw new Error('jev malformed answer');
98
77
  const allowed = new Set([...TIERS, 'uncertain']);
99
- if (!allowed.has(route.choice)) throw new Error(`jev unknown choice ${route.choice}`);
78
+ if (!allowed.has(route.choice)) throw new Error('jev unknown choice');
100
79
  const probabilities = {};
101
80
  for (const key of allowed) probabilities[key] = clamp(route.probabilities[key]);
102
81
  const continuation = json.answers.continuation?.type === 'noul' ? clamp(json.answers.continuation.noul) : null;
@@ -0,0 +1,206 @@
1
+ import { TIERS } from './config.mjs';
2
+ import { CLASSIFIER, CLASSIFIER_ERRORS, GATEWAY_SETTINGS, SHORT_REASONS, usageMetrics } from './native-display.mjs';
3
+ import { modelName, TIER_COLOR } from './native-panel.mjs';
4
+
5
+ const BARS = '▂▄▆█';
6
+ const SEPARATOR = ' · ';
7
+ // Space the band keeps for the trailing Router button.
8
+ const ROUTER_BUTTON = 8;
9
+ const STRIP = 20;
10
+ // Classifier failures worth a warning; busy and cancelled are routine and pass silently.
11
+ const WARNED = new Set(['missing-key', 'paused', 'timeout', 'auth', 'http', 'unreachable', 'policy', 'malformed']);
12
+
13
+ const part = (text, style = {}) => ({ text, style });
14
+ const button = (props) => ({ button: props });
15
+ // `priority` 0 never drops; higher numbers drop first when the band is narrow.
16
+ const segment = (priority, ...parts) => ({ priority, parts: parts.flat().filter(Boolean) });
17
+ const partWidth = (p) => (p.button ? p.button.label.length + (p.button.plain ? 0 : 4) : p.text.length);
18
+ const lineWidth = (segments) =>
19
+ segments.reduce((sum, s, i) => sum + (i ? SEPARATOR.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
20
+
21
+ // Drops the highest-priority segments, later ones first on a tie, until the line fits `columns`. If the priority-0
22
+ // segments alone are too wide, the later ones go too, and the first is cut with an ellipsis: Ink would otherwise
23
+ // shrink every Text in the row and garble it.
24
+ export function fitSegments(segments, columns) {
25
+ const kept = [...segments];
26
+ while (lineWidth(kept) > columns && kept.length > 1) {
27
+ let drop = -1;
28
+ kept.forEach((s, i) => {
29
+ if (s.priority > 0 && (drop < 0 || s.priority >= kept[drop].priority)) drop = i;
30
+ });
31
+ kept.splice(drop < 0 ? kept.length - 1 : drop, 1);
32
+ }
33
+ const overflow = lineWidth(kept) - columns;
34
+ if (overflow > 0 && kept.length) {
35
+ const parts = [...kept[0].parts];
36
+ const last = parts.findLastIndex((p) => !p.button && p.text.length > overflow);
37
+ if (last >= 0) parts[last] = { ...parts[last], text: `${parts[last].text.slice(0, -overflow - 1)}…` };
38
+ kept[0] = { ...kept[0], parts };
39
+ }
40
+ return kept;
41
+ }
42
+
43
+ // Signal-strength bars: lit up to the tier in its color, dim above it or when `dim`.
44
+ function meter(tier, dim = false) {
45
+ const lit = dim ? -1 : TIERS.indexOf(tier);
46
+ return [...BARS].map((bar, i) => part(bar, i <= lit ? { color: TIER_COLOR[tier] } : { dimColor: true }));
47
+ }
48
+
49
+ function route(view) {
50
+ const id = view.actualModel ?? view.selectedModel ?? view.nativeModel;
51
+ const effort = view.effort === null || view.effort === undefined ? '' : ` · ${view.effort}`;
52
+ return `${modelName(id)}${effort}`;
53
+ }
54
+
55
+ function served(view) {
56
+ const { actualModel: actual, selectedModel: selected } = view;
57
+ return Boolean(actual && selected && actual !== selected && !actual.startsWith(`${selected}-`));
58
+ }
59
+
60
+ function supportText(estimate) {
61
+ if (!Number.isFinite(estimate?.threshold)) return null;
62
+ const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
63
+ if (!Number.isFinite(mass)) return null;
64
+ const pct = (n) => `${Math.round(n * 100)}%`;
65
+ return `${CLASSIFIER} ${pct(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${pct(estimate.threshold)}`;
66
+ }
67
+
68
+ export function bandSegments(config, view, usage, actions) {
69
+ if (view.phase === 'unavailable')
70
+ return [
71
+ segment(0, part('✕ Router unavailable', { color: 'red' })),
72
+ segment(
73
+ 2,
74
+ part(view.error === GATEWAY_SETTINGS ? 'v0.8 gateway settings remain' : (view.error ?? 'unknown error'), {
75
+ color: 'red',
76
+ }),
77
+ ),
78
+ segment(1, button({ key: 'band-fix', label: 'Fix', onPress: actions.open })),
79
+ ];
80
+ if (view.mode === 'manual')
81
+ return [
82
+ segment(
83
+ 0,
84
+ part('○ ', { dimColor: true }),
85
+ part('Router '),
86
+ part('off', { color: 'yellow' }),
87
+ part(` · keeping ${modelName(view.nativeModel)}`),
88
+ view.reason === 'model selected manually' ? part(' (/model)', { dimColor: true }) : null,
89
+ ),
90
+ segment(1, button({ key: 'band-auto', label: 'Auto', onPress: () => actions.mode('auto') })),
91
+ ];
92
+ if (view.phase === 'choosing')
93
+ return [
94
+ segment(0, meter(view.tier, true), part(' choosing for this turn…', { dimColor: true })),
95
+ segment(3, part(`${CLASSIFIER} · ${config.jev.timeoutMs / 1000} s deadline`, { dimColor: true })),
96
+ ];
97
+ const pin = view.pendingPin
98
+ ? segment(
99
+ 0,
100
+ part('⏵ next turn: ', { color: 'yellow' }),
101
+ part(view.pendingPin, { bold: true, color: TIER_COLOR[view.pendingPin] }),
102
+ part(' '),
103
+ button({ key: 'band-unpin', label: '✕', plain: true, onPress: actions.unpin }),
104
+ )
105
+ : null;
106
+ if (!view.tier && view.phase === 'ready' && !view.actualModel)
107
+ return [segment(0, meter(null, true), part(' Auto · ready', { dimColor: true })), pin].filter(Boolean);
108
+ const main = segment(
109
+ 0,
110
+ view.tier
111
+ ? [...meter(view.tier), part(` ${view.tier} `, { bold: true, color: TIER_COLOR[view.tier] })]
112
+ : part('○ '),
113
+ part(route(view)),
114
+ served(view) ? part(' fallback', { color: 'yellow' }) : null,
115
+ );
116
+ if (WARNED.has(view.error))
117
+ return [
118
+ main,
119
+ pin,
120
+ segment(0, part(`⚠ ${CLASSIFIER} ${CLASSIFIER_ERRORS[view.error]}`, { color: 'yellow' })),
121
+ segment(3, part('keeping model', { color: 'yellow' })),
122
+ view.error === 'missing-key'
123
+ ? segment(1, button({ key: 'band-key', label: 'Set key', onPress: actions.key }))
124
+ : segment(2, button({ key: 'band-fix', label: 'Details', onPress: actions.open })),
125
+ ].filter(Boolean);
126
+ const metrics = usageMetrics(config, view, usage);
127
+ const percent = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
128
+ const support = supportText(view.estimate);
129
+ return [
130
+ main,
131
+ pin,
132
+ segment(1, part(SHORT_REASONS[view.reason] ?? view.reason ?? '', { dimColor: true })),
133
+ support ? segment(4, part(support, { dimColor: true })) : null,
134
+ metrics.contextBar.percent === null && metrics.cacheBar.percent === null
135
+ ? null
136
+ : segment(3, part('ctx '), percent(metrics.contextBar), part(' · cache '), percent(metrics.cacheBar)),
137
+ ].filter(Boolean);
138
+ }
139
+
140
+ // The second row of the detailed band: recent replies by tier and the session's switch economics.
141
+ function detailRow(config, view, usage) {
142
+ const tiers = (view.tiers ?? []).slice(-STRIP);
143
+ if (!tiers.length) return [part('no replies yet', { dimColor: true })];
144
+ const switches = tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
145
+ const metrics = usageMetrics(config, view, usage);
146
+ return [
147
+ part('replies ', { dimColor: true }),
148
+ ...tiers.map((tier) => (tier ? part('█', { color: TIER_COLOR[tier] }) : part('·', { dimColor: true }))),
149
+ part(
150
+ ` ${switches} switch${switches === 1 ? '' : 'es'}` +
151
+ `${Number.isFinite(view.estimate?.taxUsd) ? ` · tax ≈ $${view.estimate.taxUsd.toFixed(2)}` : ''}` +
152
+ `${Number.isFinite(metrics.cacheBenefit) ? ` · cache saved ≈ $${metrics.cacheBenefit.toFixed(2)}` : ''}`,
153
+ { dimColor: true },
154
+ ),
155
+ ];
156
+ }
157
+
158
+ export function renderBand({ Box, Text, Button }, config, view, usage, { columns, agentId }, actions) {
159
+ const draw = (p) => (p.button ? Button(p.button) : Text({ ...p.style, children: p.text }));
160
+ const row = (parts) => Box({ children: parts.map(draw) });
161
+ if (agentId) return row([part('○ Router · subagents keep their own model', { dimColor: true })]);
162
+ const kept = fitSegments(bandSegments(config, view, usage, actions), Math.max(0, columns - ROUTER_BUTTON));
163
+ const line = kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
164
+ line.push(part(' '), button({ key: 'details', label: 'Router', plain: true, onPress: actions.open }));
165
+ const routing = view.mode === 'auto' && view.phase !== 'unavailable';
166
+ return Box({
167
+ key: 'router-band',
168
+ flexDirection: 'column',
169
+ children: [
170
+ row(line),
171
+ view.bandDetail && routing ? row(detailRow(config, view, usage)) : null,
172
+ // Revealed while the pointer is over the band; no digit hotkeys, since a bare digit in an
173
+ // empty prompt would press them.
174
+ Box({
175
+ display: 'none',
176
+ hover: { display: 'flex' },
177
+ children: [
178
+ Text({ dimColor: true, children: routing ? 'pin next turn ' : '' }),
179
+ ...(routing
180
+ ? TIERS.flatMap((tier) => [
181
+ Button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
182
+ Text({ children: ' ' }),
183
+ ])
184
+ : []),
185
+ Text({ children: routing ? ' ' : '' }),
186
+ routing
187
+ ? Button({ key: 'band-manual', label: 'Manual', onPress: () => actions.mode('manual') })
188
+ : Button({ key: 'band-auto-row', label: 'Auto', onPress: () => actions.mode('auto') }),
189
+ Text({ children: ' ' }),
190
+ Button({
191
+ key: 'band-detail',
192
+ label: view.bandDetail ? '1 row' : '2 rows',
193
+ onPress: actions.toggleDetail,
194
+ }),
195
+ ],
196
+ }),
197
+ ],
198
+ });
199
+ }
200
+
201
+ export function switchToast(previous, next, estimate) {
202
+ const label = (decision) =>
203
+ `${modelName(decision.model)}${decision.effort === null || decision.effort === undefined ? '' : ` · ${decision.effort}`}`;
204
+ const support = supportText(estimate);
205
+ return `${label(previous)} → ${label(next)} — ${SHORT_REASONS[next.reason]?.replace(/^\W+\s*/, '') ?? next.reason}${support ? ` (${support})` : ''}`;
206
+ }