@alexeiled/claude-router 0.8.0 → 1.0.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 +10 -6
- package/CHANGELOG.md +19 -0
- package/README.md +51 -100
- package/docs/architecture.md +55 -419
- package/docs/configuration.md +86 -182
- package/docs/evaluation.md +62 -81
- package/docs/native-router.md +79 -0
- package/docs/user-guide.md +103 -172
- package/hooks/hooks.json +1 -26
- package/hooks/native-router.mjs +635 -0
- package/lib/config.mjs +20 -58
- package/lib/{facts.mjs → facts-pure.mjs} +2 -23
- package/lib/{jev.mjs → jev-contract.mjs} +17 -38
- package/lib/native-cost.mjs +88 -0
- package/lib/native-display.mjs +75 -0
- package/lib/native-jev.mjs +125 -0
- package/lib/native-panel.mjs +150 -0
- package/lib/native-router.mjs +216 -0
- package/lib/policy.mjs +17 -21
- package/package.json +9 -8
- package/scripts/migrate-config.mjs +57 -0
- package/types/index.d.ts +95 -0
- package/lib/cost.mjs +0 -93
- package/lib/gateway.mjs +0 -221
- package/lib/idle.mjs +0 -41
- package/lib/rewrite.mjs +0 -130
- package/lib/router.mjs +0 -276
- package/lib/runtime.mjs +0 -78
- package/lib/sse.mjs +0 -57
- package/lib/status.mjs +0 -152
- package/lib/store.mjs +0 -76
- package/scripts/ensure-gateway.mjs +0 -143
- package/scripts/gateway.mjs +0 -81
- package/scripts/git-hooks/pre-commit +0 -41
- package/scripts/git-hooks/pre-push +0 -34
- package/scripts/status.mjs +0 -20
- package/scripts/statusline.mjs +0 -45
- package/scripts/transcript-models.sh +0 -4
- package/skills/high/SKILL.md +0 -9
- package/skills/low/SKILL.md +0 -7
- package/skills/medium/SKILL.md +0 -8
- package/skills/micro/SKILL.md +0 -7
- package/skills/setup/SKILL.md +0 -31
- package/skills/status/SKILL.md +0 -12
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import { loadConfig, TIERS } from '../lib/config.mjs';
|
|
2
|
+
import { clip } from '../lib/facts-pure.mjs';
|
|
3
|
+
import { REASONS } from '../lib/native-display.mjs';
|
|
4
|
+
import { NativeJev } from '../lib/native-jev.mjs';
|
|
5
|
+
import { renderPanel } from '../lib/native-panel.mjs';
|
|
6
|
+
import {
|
|
7
|
+
chooseRoute,
|
|
8
|
+
continueRoute,
|
|
9
|
+
emptyLoop,
|
|
10
|
+
nativeFacts,
|
|
11
|
+
observeResponse,
|
|
12
|
+
prepareLoop,
|
|
13
|
+
resetHistory,
|
|
14
|
+
SNAPSHOTS,
|
|
15
|
+
} from '../lib/native-router.mjs';
|
|
16
|
+
|
|
17
|
+
const VIEW = { plugin: 'router', key: 'view' };
|
|
18
|
+
const LOOP = { plugin: 'router', key: 'loops' };
|
|
19
|
+
const PANE = 'jev-router';
|
|
20
|
+
const MODE_PREFIX = 'mode:';
|
|
21
|
+
const GATEWAY_SETTINGS = 'v0.8 gateway settings remain · see /router status';
|
|
22
|
+
|
|
23
|
+
function initialView(model) {
|
|
24
|
+
return {
|
|
25
|
+
phase: 'ready',
|
|
26
|
+
activeTurnId: null,
|
|
27
|
+
mode: 'auto',
|
|
28
|
+
nativeModel: model,
|
|
29
|
+
selectedModel: null,
|
|
30
|
+
actualModel: null,
|
|
31
|
+
tier: null,
|
|
32
|
+
effort: null,
|
|
33
|
+
reason: 'ready',
|
|
34
|
+
error: null,
|
|
35
|
+
pendingPin: null,
|
|
36
|
+
keySet: false,
|
|
37
|
+
contextTokens: null,
|
|
38
|
+
contextKnown: false,
|
|
39
|
+
cacheRead: null,
|
|
40
|
+
cacheWrite: null,
|
|
41
|
+
health: { failures: 0, pausedUntil: 0 },
|
|
42
|
+
inputTokens: null,
|
|
43
|
+
outputTokens: null,
|
|
44
|
+
adviceMs: null,
|
|
45
|
+
adviceChoice: null,
|
|
46
|
+
estimate: null,
|
|
47
|
+
comparison: null,
|
|
48
|
+
history: [],
|
|
49
|
+
configPath: null,
|
|
50
|
+
tuning: null,
|
|
51
|
+
notice: null,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function readView($, runtime) {
|
|
56
|
+
return (await $.state.get(VIEW)).value ?? runtime.view ?? initialView(await $.session.model());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function modeOf($, runtime, fallback = 'auto') {
|
|
60
|
+
const sessionId = await $.session.id();
|
|
61
|
+
if (runtime.modes.has(sessionId)) return runtime.modes.get(sessionId);
|
|
62
|
+
try {
|
|
63
|
+
const saved = await $.store.get(`${MODE_PREFIX}${sessionId}`);
|
|
64
|
+
const mode = saved === 'manual' || saved === 'auto' ? saved : fallback;
|
|
65
|
+
runtime.modes.set(sessionId, mode);
|
|
66
|
+
return mode;
|
|
67
|
+
} catch {
|
|
68
|
+
return 'manual';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function rememberMode($, mode) {
|
|
73
|
+
try {
|
|
74
|
+
await $.store.set(`${MODE_PREFIX}${await $.session.id()}`, mode);
|
|
75
|
+
return true;
|
|
76
|
+
} catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function updateView($, runtime, patch) {
|
|
82
|
+
const value = runtime.view ?? (await readView($, runtime));
|
|
83
|
+
runtime.view = { ...value, ...patch };
|
|
84
|
+
await $.state.set(VIEW, runtime.view);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function changeMode($, runtime, mode) {
|
|
88
|
+
const view = runtime.view ?? (await readView($, runtime));
|
|
89
|
+
for (const controller of runtime.controllers) controller.abort();
|
|
90
|
+
runtime.modes.set(await $.session.id(), mode);
|
|
91
|
+
const saved = await rememberMode($, mode);
|
|
92
|
+
await updateView($, runtime, {
|
|
93
|
+
mode,
|
|
94
|
+
pendingPin: null,
|
|
95
|
+
phase: view.phase === 'unavailable' ? 'unavailable' : mode === 'manual' ? 'manual' : 'ready',
|
|
96
|
+
reason: mode === 'manual' ? 'routing paused' : 'ready',
|
|
97
|
+
...(!saved ? { notice: 'Mode changed for this session. Resume preference could not be saved.' } : {}),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function setPin($, runtime, tier) {
|
|
102
|
+
const view = runtime.view ?? (await readView($, runtime));
|
|
103
|
+
if (view.phase === 'unavailable') return `Routing unavailable: ${view.error}.`;
|
|
104
|
+
const mode = await modeOf($, runtime, view.mode);
|
|
105
|
+
if (mode === 'manual') return 'Routing is paused. Select Auto before pinning a turn.';
|
|
106
|
+
await updateView($, runtime, { pendingPin: tier });
|
|
107
|
+
return `${tier} pinned for the next turn and its tool continuations.`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function apiKeyOf($, options) {
|
|
111
|
+
return typeof options.typesafe_api_key === 'string' && options.typesafe_api_key.trim()
|
|
112
|
+
? options.typesafe_api_key.trim()
|
|
113
|
+
: (await $.env.get('TYPESAFE_API_KEY')) || (await $.env.get('CLAUDE_PLUGIN_OPTION_TYPESAFE_API_KEY'));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function configureKey($) {
|
|
117
|
+
return $.command.run({ command: 'plugin', args: `configure ${$.plugin.name}` });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function modelLabel(id) {
|
|
121
|
+
if (!id) return 'native model';
|
|
122
|
+
return id.replace(/^claude-/, '').replace(/-([0-9])-([0-9])/, ' $1.$2');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function bandText(view) {
|
|
126
|
+
if (view.phase === 'unavailable') return `Jev Router · ${view.error}`;
|
|
127
|
+
if (view.mode === 'manual') return `Jev Router · Manual · ${modelLabel(view.nativeModel)}`;
|
|
128
|
+
if (view.pendingPin) return `Jev Router · ${view.pendingPin} pinned for next turn`;
|
|
129
|
+
if (view.phase === 'ready') return 'Jev Router · Auto · ready';
|
|
130
|
+
if (view.phase === 'choosing') return 'Jev Router · choosing for this turn…';
|
|
131
|
+
if (view.error === 'missing-key')
|
|
132
|
+
return `Jev Router · key not set · keeping ${modelLabel(view.actualModel ?? view.nativeModel)}`;
|
|
133
|
+
if (view.error === 'paused')
|
|
134
|
+
return `Jev Router · Jev paused until ${new Date(view.health.pausedUntil).toLocaleTimeString()}`;
|
|
135
|
+
if (view.error) return `Jev Router · Jev ${view.error} · keeping ${modelLabel(view.actualModel ?? view.nativeModel)}`;
|
|
136
|
+
if (view.reason === 'context-unknown') return 'Jev Router · context unknown · keeping native model';
|
|
137
|
+
if (
|
|
138
|
+
view.actualModel &&
|
|
139
|
+
view.selectedModel &&
|
|
140
|
+
view.actualModel !== view.selectedModel &&
|
|
141
|
+
!view.actualModel.startsWith(`${view.selectedModel}-`)
|
|
142
|
+
) {
|
|
143
|
+
return `Jev Router · ${modelLabel(view.selectedModel)} → ${modelLabel(view.actualModel)} · native fallback`;
|
|
144
|
+
}
|
|
145
|
+
return `Jev Router · ${view.actualModel ? 'last reply' : 'selected'} ${modelLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel)}${view.effort ? ` · ${view.effort}` : ''} · ${REASONS[view.reason] ?? view.reason}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function detailText(view) {
|
|
149
|
+
return [
|
|
150
|
+
`Jev Router — ${view.mode === 'auto' ? 'Auto' : 'Manual'}`,
|
|
151
|
+
`Native model: ${view.nativeModel}`,
|
|
152
|
+
`Selected: ${view.selectedModel ?? 'not selected'}`,
|
|
153
|
+
`Observed: ${view.actualModel ?? 'no response yet'}`,
|
|
154
|
+
`Reason: ${view.reason}`,
|
|
155
|
+
`Jev: ${view.error ?? (view.keySet ? 'ready' : 'key not set')}`,
|
|
156
|
+
`Context: ${view.contextKnown ? `${view.contextTokens} tokens (estimate)` : 'unknown'}`,
|
|
157
|
+
`Observed cache: ${view.cacheRead ?? 'unknown'} read, ${view.cacheWrite ?? 'unknown'} written tokens`,
|
|
158
|
+
view.pendingPin ? `Next turn pin: ${view.pendingPin}` : 'No next-turn pin.',
|
|
159
|
+
'Auto enables routing. Manual preserves Claude’s model. Pins serve one turn only.',
|
|
160
|
+
'Cache lifetime is an estimate. Claude’s cost ledger owns session totals.',
|
|
161
|
+
...(view.error === GATEWAY_SETTINGS
|
|
162
|
+
? [
|
|
163
|
+
'Remove from settings.json, then restart: model jev-router[1m], its modelPicker row,',
|
|
164
|
+
'env.ANTHROPIC_BASE_URL for 127.0.0.1:43170, env.CLAUDE_CODE_GATEWAY_HINT_HEADERS,',
|
|
165
|
+
'and a statusLine that runs the router scripts/statusline.mjs.',
|
|
166
|
+
]
|
|
167
|
+
: []),
|
|
168
|
+
].join('\n');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function loadNativeConfig($) {
|
|
172
|
+
for (const source of ['project', 'local']) {
|
|
173
|
+
const settings = await $.settings.read({ source });
|
|
174
|
+
if (settings.env && ['HOME', 'CLAUDE_CONFIG_DIR'].some((key) => Object.hasOwn(settings.env, key)))
|
|
175
|
+
throw new Error('project settings cannot redirect the router profile');
|
|
176
|
+
}
|
|
177
|
+
const home = await $.env.get('HOME');
|
|
178
|
+
const profile = (await $.env.get('CLAUDE_CONFIG_DIR')) ?? `${home}/.claude`;
|
|
179
|
+
const path = `${profile}/router.json`;
|
|
180
|
+
const userFile = (await $.fs.exists(path)) ? JSON.parse(await $.fs.read(path)) : null;
|
|
181
|
+
return { ...loadConfig({ userFile }), nativePath: path };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function supportedVersion(version) {
|
|
185
|
+
const parts = /^(\d+)\.(\d+)\.(\d+)(?:$|-)/.exec(version ?? '');
|
|
186
|
+
if (!parts) return false;
|
|
187
|
+
const [major, minor, patch] = parts.slice(1).map(Number);
|
|
188
|
+
return major > 2 || (major === 2 && (minor > 1 || (minor === 1 && patch >= 289)));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function contextOf($, loop) {
|
|
192
|
+
const previous = loop.lastRequest ? loop.lastRequest.tokens + loop.lastRequest.outputTokens : null;
|
|
193
|
+
try {
|
|
194
|
+
const usage = await $.session.usage({ breakdown: 'summary' });
|
|
195
|
+
const estimate = usage.context.breakdown?.totalTokens;
|
|
196
|
+
const observed = usage.context.tokens;
|
|
197
|
+
const values = [previous, estimate, observed].filter((n) => Number.isFinite(n) && n >= 0);
|
|
198
|
+
return { tokens: values.length ? Math.max(...values) : null, known: Number.isFinite(estimate) };
|
|
199
|
+
} catch {
|
|
200
|
+
return { tokens: previous, known: false };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function tuningOf(config) {
|
|
205
|
+
return {
|
|
206
|
+
timeoutMs: config.jev.timeoutMs,
|
|
207
|
+
downgradeVotes: config.policy.downgradeVotes,
|
|
208
|
+
horizon: config.policy.downgradeHorizonTurns,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function responseMetrics(view, response) {
|
|
213
|
+
const usage = response.usage;
|
|
214
|
+
const counters = usage ? [usage.input_tokens, usage.cache_read_input_tokens, usage.cache_creation_input_tokens] : [];
|
|
215
|
+
const inputTokens =
|
|
216
|
+
counters.length && counters.every(Number.isFinite) ? counters.reduce((sum, n) => sum + n, 0) : null;
|
|
217
|
+
return {
|
|
218
|
+
actualModel: usage?.model ?? null,
|
|
219
|
+
cacheRead: usage?.cache_read_input_tokens ?? null,
|
|
220
|
+
cacheWrite: usage?.cache_creation_input_tokens ?? null,
|
|
221
|
+
inputTokens,
|
|
222
|
+
outputTokens: usage?.output_tokens ?? null,
|
|
223
|
+
history: Number.isFinite(inputTokens) ? [...(view?.history ?? []), inputTokens].slice(-10) : (view?.history ?? []),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// The engine may echo our own routed model on a continuation; any third model is its fallback.
|
|
228
|
+
function isNativeFallback(loop, model) {
|
|
229
|
+
if (loop.suspended) return true;
|
|
230
|
+
const known = [loop.engineModel, loop.decision?.model].filter(Boolean);
|
|
231
|
+
return known.length > 0 && !known.some((id) => id === model || SNAPSHOTS[id] === model);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function* passMain($, e, next, loop, version, nativeModel, reason, config, runtime) {
|
|
235
|
+
const ref = { ...LOOP, id: 'main' };
|
|
236
|
+
const context = await contextOf($, loop);
|
|
237
|
+
await updateView($, runtime, {
|
|
238
|
+
nativeModel,
|
|
239
|
+
selectedModel: e.model,
|
|
240
|
+
effort: e.effort ?? null,
|
|
241
|
+
reason,
|
|
242
|
+
contextTokens: context.tokens,
|
|
243
|
+
contextKnown: context.known,
|
|
244
|
+
});
|
|
245
|
+
const sessionId = await $.session.id();
|
|
246
|
+
const response = yield* next(e);
|
|
247
|
+
if (!next.signal.aborted && (await $.session.id()) === sessionId) {
|
|
248
|
+
const observed = observeResponse(config, loop, {
|
|
249
|
+
usage: response.usage,
|
|
250
|
+
requestedModel: e.model,
|
|
251
|
+
effort: e.effort ?? null,
|
|
252
|
+
stopReason: response.stopReason,
|
|
253
|
+
now: Date.now(),
|
|
254
|
+
});
|
|
255
|
+
const written = await $.state.set(ref, observed, { ifVersion: version });
|
|
256
|
+
if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response));
|
|
257
|
+
}
|
|
258
|
+
return response;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function register(on, options) {
|
|
262
|
+
let config = loadConfig();
|
|
263
|
+
const turnConfigs = new Map();
|
|
264
|
+
const classifier = new NativeJev();
|
|
265
|
+
const prompts = new Map();
|
|
266
|
+
const decisions = new Map();
|
|
267
|
+
const controllers = new Set();
|
|
268
|
+
const turnControllers = new Map();
|
|
269
|
+
const runtime = { view: null, controllers, modes: new Map() };
|
|
270
|
+
|
|
271
|
+
on('session.start', async ($, e, next) => {
|
|
272
|
+
await $.command.register({
|
|
273
|
+
name: 'router',
|
|
274
|
+
description: 'Jev routing, next-turn pins and model controls.',
|
|
275
|
+
argumentHint: '[auto|off|pin <tier>|status|setup]',
|
|
276
|
+
immediate: true,
|
|
277
|
+
});
|
|
278
|
+
const model = await $.session.model();
|
|
279
|
+
try {
|
|
280
|
+
config = await loadNativeConfig($);
|
|
281
|
+
const existing = await readView($, runtime);
|
|
282
|
+
runtime.view = existing;
|
|
283
|
+
classifier.restore(existing.health);
|
|
284
|
+
const base = await $.env.get('ANTHROPIC_BASE_URL');
|
|
285
|
+
const version = await $.session.version().catch(() => null);
|
|
286
|
+
const supported = supportedVersion(version?.version);
|
|
287
|
+
const baselineModel = config.models[config.routes[config.baselineTier].model].id;
|
|
288
|
+
const gateway =
|
|
289
|
+
['jev-router', 'jev-router[1m]'].includes(model) ||
|
|
290
|
+
model === 'router' ||
|
|
291
|
+
Boolean(base?.includes('127.0.0.1:43170') || base?.includes('localhost:43170'));
|
|
292
|
+
await updateView($, runtime, {
|
|
293
|
+
nativeModel: model,
|
|
294
|
+
mode: await modeOf($, runtime, model === baselineModel ? 'auto' : 'manual'),
|
|
295
|
+
phase: gateway || !supported ? 'unavailable' : 'ready',
|
|
296
|
+
error: !supported ? 'requires Claude Code 2.1.289 or newer' : gateway ? GATEWAY_SETTINGS : null,
|
|
297
|
+
keySet: Boolean(await apiKeyOf($, options)),
|
|
298
|
+
configPath: config.nativePath,
|
|
299
|
+
tuning: tuningOf(config),
|
|
300
|
+
});
|
|
301
|
+
} catch (error) {
|
|
302
|
+
await updateView($, runtime, {
|
|
303
|
+
phase: 'unavailable',
|
|
304
|
+
error: error.message?.startsWith('router.json uses gateway settings;')
|
|
305
|
+
? 'router.json needs migration; run the plugin scripts/migrate-config.mjs with your router.json path'
|
|
306
|
+
: 'invalid router configuration',
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
return next(e);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
on('command.run', { command: 'model' }, async ($, e, next) => {
|
|
313
|
+
const result = await next(e);
|
|
314
|
+
if (e.origin.kind !== 'plugin') {
|
|
315
|
+
await changeMode($, runtime, 'manual');
|
|
316
|
+
await updateView($, runtime, { nativeModel: await $.session.model(), reason: 'model selected manually' });
|
|
317
|
+
}
|
|
318
|
+
return result;
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
on('config.set', { key: 'model' }, async ($, e, next) => {
|
|
322
|
+
const result = await next(e);
|
|
323
|
+
if (!result.deny && e.origin.kind !== 'plugin') {
|
|
324
|
+
await changeMode($, runtime, 'manual');
|
|
325
|
+
await updateView($, runtime, { nativeModel: await $.session.model() });
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
on('command.run', { command: 'router' }, async ($, e) => {
|
|
331
|
+
const [action, tier] = e.args.trim().split(/\s+/);
|
|
332
|
+
if (action === 'auto' || action === 'off') {
|
|
333
|
+
await changeMode($, runtime, action === 'auto' ? 'auto' : 'manual');
|
|
334
|
+
return { text: action === 'auto' ? 'Auto routing enabled.' : 'Manual mode: Claude’s model is preserved.' };
|
|
335
|
+
}
|
|
336
|
+
if (action === 'pin')
|
|
337
|
+
return { text: TIERS.includes(tier) ? await setPin($, runtime, tier) : `Choose ${TIERS.join(', ')}.` };
|
|
338
|
+
if (action === 'setup') return configureKey($);
|
|
339
|
+
const storedView = await readView($, runtime);
|
|
340
|
+
const view = { ...storedView, mode: await modeOf($, runtime, storedView.mode) };
|
|
341
|
+
if (action === 'status' || !(await $.session.surfaces()).length) return { text: detailText(view) };
|
|
342
|
+
await $.ui.open({ id: PANE, title: 'Jev Router', focus: true, closeOnEscape: true });
|
|
343
|
+
return {};
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
on('turn.start', (_$, e, next) => {
|
|
347
|
+
turnConfigs.set(e.turnId, config);
|
|
348
|
+
prompts.set(e.turnId, clip(e.text, config.context.maxTextChars));
|
|
349
|
+
return next(e);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
on('turn.step', async function* ($, e, next) {
|
|
353
|
+
if (e.agentId) return yield* next(e);
|
|
354
|
+
const cfg = turnConfigs.get(e.turnId) ?? config;
|
|
355
|
+
const sessionId = await $.session.id();
|
|
356
|
+
const nativeModel = await $.session.model();
|
|
357
|
+
const view = await readView($, runtime);
|
|
358
|
+
const mode = await modeOf($, runtime, view.mode);
|
|
359
|
+
if (e.index === 0) {
|
|
360
|
+
const saved = await rememberMode($, mode);
|
|
361
|
+
await updateView($, runtime, {
|
|
362
|
+
mode,
|
|
363
|
+
...(!saved ? { notice: 'Resume preference could not be saved. Current mode remains active.' } : {}),
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
const ref = { ...LOOP, id: 'main' };
|
|
367
|
+
const loaded = await $.state.get(ref);
|
|
368
|
+
let loopVersion = loaded.version;
|
|
369
|
+
let loop = prepareLoop(loaded.value ?? emptyLoop(cfg, e.model), e.messageCount);
|
|
370
|
+
if (view.phase === 'unavailable' || mode === 'manual') {
|
|
371
|
+
return yield* passMain($, e, next, loop, loopVersion, nativeModel, 'manual', cfg, runtime);
|
|
372
|
+
}
|
|
373
|
+
if (loop.turnId === e.turnId && isNativeFallback(loop, e.model)) {
|
|
374
|
+
return yield* passMain($, e, next, loop, loopVersion, nativeModel, 'native-fallback', cfg, runtime);
|
|
375
|
+
}
|
|
376
|
+
const context = await contextOf($, loop);
|
|
377
|
+
const settings = await $.settings.read();
|
|
378
|
+
const availableModels = settings.availableModels;
|
|
379
|
+
const key = `${sessionId}:${e.turnId}:${loop.generation}`;
|
|
380
|
+
if (loop.turnId !== e.turnId) {
|
|
381
|
+
let job = decisions.get(key);
|
|
382
|
+
if (!job) {
|
|
383
|
+
job = (async () => {
|
|
384
|
+
const controller = new AbortController();
|
|
385
|
+
const cancel = () => controller.abort();
|
|
386
|
+
next.signal.addEventListener('abort', cancel, { once: true });
|
|
387
|
+
controllers.add(controller);
|
|
388
|
+
turnControllers.set(e.turnId, controller);
|
|
389
|
+
const live = async () => !controller.signal.aborted && (await $.session.id()) === sessionId;
|
|
390
|
+
try {
|
|
391
|
+
const pin = view.pendingPin;
|
|
392
|
+
const apiKey = await apiKeyOf($, options);
|
|
393
|
+
const messages = await $.session.messages({ as: 'api' });
|
|
394
|
+
if (!(await live())) return null;
|
|
395
|
+
const facts = nativeFacts(cfg, loop, {
|
|
396
|
+
messages,
|
|
397
|
+
prompt: prompts.get(e.turnId),
|
|
398
|
+
effort: e.effort,
|
|
399
|
+
turnId: e.turnId,
|
|
400
|
+
contextTokens: context.tokens,
|
|
401
|
+
});
|
|
402
|
+
await updateView($, runtime, {
|
|
403
|
+
phase: 'choosing',
|
|
404
|
+
activeTurnId: e.turnId,
|
|
405
|
+
nativeModel,
|
|
406
|
+
pendingPin: null,
|
|
407
|
+
keySet: Boolean(apiKey),
|
|
408
|
+
});
|
|
409
|
+
const adviceStarted = Date.now();
|
|
410
|
+
const result = pin
|
|
411
|
+
? { advice: null, error: null }
|
|
412
|
+
: await classifier.ask({
|
|
413
|
+
request: (url, init) => $.http.fetch(url, init),
|
|
414
|
+
sleep: (ms, args) => $.clock.sleep(ms, args),
|
|
415
|
+
config: cfg,
|
|
416
|
+
apiKey,
|
|
417
|
+
prompt: facts.prompt,
|
|
418
|
+
turns: facts.turns,
|
|
419
|
+
signal: controller.signal,
|
|
420
|
+
});
|
|
421
|
+
if (
|
|
422
|
+
controller.signal.aborted ||
|
|
423
|
+
(await $.session.id()) !== sessionId ||
|
|
424
|
+
(await modeOf($, runtime)) !== 'auto'
|
|
425
|
+
)
|
|
426
|
+
return null;
|
|
427
|
+
const selected = chooseRoute(cfg, loop, {
|
|
428
|
+
facts,
|
|
429
|
+
advice: result.advice,
|
|
430
|
+
pin,
|
|
431
|
+
nativeModel,
|
|
432
|
+
contextKnown: context.known,
|
|
433
|
+
availableModels,
|
|
434
|
+
now: Date.now(),
|
|
435
|
+
});
|
|
436
|
+
selected.engineModel = e.model;
|
|
437
|
+
const written = await $.state.set(ref, selected, { ifVersion: loopVersion });
|
|
438
|
+
// Manual, /clear and turn completion abort the controller; one may land during the write.
|
|
439
|
+
if (!written.isSet || controller.signal.aborted) return null;
|
|
440
|
+
await updateView($, runtime, {
|
|
441
|
+
phase: 'routed',
|
|
442
|
+
selectedModel: selected.decision.model,
|
|
443
|
+
actualModel: null,
|
|
444
|
+
tier: selected.decision.tier,
|
|
445
|
+
effort: selected.decision.effort,
|
|
446
|
+
reason: selected.decision.reason,
|
|
447
|
+
error: result.error,
|
|
448
|
+
health: classifier.snapshot(),
|
|
449
|
+
contextTokens: context.tokens,
|
|
450
|
+
contextKnown: context.known,
|
|
451
|
+
adviceMs:
|
|
452
|
+
pin || ['missing-key', 'busy', 'paused'].includes(result.error) ? null : Date.now() - adviceStarted,
|
|
453
|
+
adviceChoice: result.advice?.choice ?? null,
|
|
454
|
+
estimate: selected.decision.estimate ?? null,
|
|
455
|
+
comparison: selected.decision.comparison,
|
|
456
|
+
});
|
|
457
|
+
return { loop: selected, version: written.version };
|
|
458
|
+
} finally {
|
|
459
|
+
next.signal.removeEventListener('abort', cancel);
|
|
460
|
+
controllers.delete(controller);
|
|
461
|
+
if (turnControllers.get(e.turnId) === controller) turnControllers.delete(e.turnId);
|
|
462
|
+
}
|
|
463
|
+
})();
|
|
464
|
+
decisions.set(key, job);
|
|
465
|
+
}
|
|
466
|
+
const selected = await job;
|
|
467
|
+
if (!selected) {
|
|
468
|
+
if (!next.signal.aborted && (await $.session.id()) === sessionId && runtime.view?.activeTurnId === e.turnId) {
|
|
469
|
+
const reserved = await $.state.set(ref, loop, { ifVersion: loopVersion });
|
|
470
|
+
if (reserved.isSet)
|
|
471
|
+
return yield* passMain($, e, next, loop, reserved.version, nativeModel, 'manual', cfg, runtime);
|
|
472
|
+
}
|
|
473
|
+
return yield* next(e);
|
|
474
|
+
}
|
|
475
|
+
loop = selected.loop;
|
|
476
|
+
loopVersion = selected.version;
|
|
477
|
+
} else {
|
|
478
|
+
loop = continueRoute(cfg, loop, {
|
|
479
|
+
nativeModel,
|
|
480
|
+
contextTokens: context.tokens,
|
|
481
|
+
contextKnown: context.known,
|
|
482
|
+
availableModels,
|
|
483
|
+
effort: e.effort,
|
|
484
|
+
});
|
|
485
|
+
await updateView($, runtime, {
|
|
486
|
+
selectedModel: loop.decision.model,
|
|
487
|
+
effort: loop.decision.effort,
|
|
488
|
+
tier: loop.decision.tier,
|
|
489
|
+
reason: loop.decision.reason,
|
|
490
|
+
contextTokens: context.tokens,
|
|
491
|
+
contextKnown: context.known,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
const request = { ...e, model: loop.decision.model };
|
|
495
|
+
if (loop.decision.effort === null) delete request.effort;
|
|
496
|
+
else request.effort = loop.decision.effort;
|
|
497
|
+
const response = yield* next(request);
|
|
498
|
+
if (!next.signal.aborted && (await $.session.id()) === sessionId) {
|
|
499
|
+
const observed = observeResponse(cfg, loop, {
|
|
500
|
+
usage: response.usage,
|
|
501
|
+
requestedModel: request.model,
|
|
502
|
+
effort: request.effort ?? null,
|
|
503
|
+
stopReason: response.stopReason,
|
|
504
|
+
now: Date.now(),
|
|
505
|
+
});
|
|
506
|
+
if (response.stopReason === null) observed.suspended = true;
|
|
507
|
+
const written = await $.state.set(ref, observed, { ifVersion: loopVersion });
|
|
508
|
+
if (written.isSet) await updateView($, runtime, responseMetrics(runtime.view, response));
|
|
509
|
+
}
|
|
510
|
+
return response;
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
on('turn.complete', async ($, e, next) => {
|
|
514
|
+
turnConfigs.delete(e.turnId);
|
|
515
|
+
turnControllers.get(e.turnId)?.abort();
|
|
516
|
+
if (runtime.view?.activeTurnId === e.turnId && runtime.view.phase === 'choosing') {
|
|
517
|
+
runtime.view = { ...runtime.view, activeTurnId: null };
|
|
518
|
+
const mode = await modeOf($, runtime);
|
|
519
|
+
if (runtime.view.activeTurnId === null)
|
|
520
|
+
await updateView($, runtime, { phase: mode === 'manual' ? 'manual' : 'ready', reason: 'interrupted' });
|
|
521
|
+
}
|
|
522
|
+
prompts.delete(e.turnId);
|
|
523
|
+
for (const key of decisions.keys()) if (key.includes(`:${e.turnId}:`)) decisions.delete(key);
|
|
524
|
+
return next(e);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
on('session.compact', async ($, e, next) => {
|
|
528
|
+
const result = await next(e);
|
|
529
|
+
// Only the main conversation is routed, so an agent compaction changes nothing here.
|
|
530
|
+
if (!result.skip && e.trigger !== 'precompute' && !e.agentId) {
|
|
531
|
+
const ref = { ...LOOP, id: 'main' };
|
|
532
|
+
const loop = (await $.state.get(ref)).value;
|
|
533
|
+
if (loop) await $.state.set(ref, resetHistory(loop));
|
|
534
|
+
for (const controller of controllers) controller.abort();
|
|
535
|
+
}
|
|
536
|
+
return result;
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
on('session.end', (_$, e, next) => {
|
|
540
|
+
for (const controller of controllers) controller.abort();
|
|
541
|
+
decisions.clear();
|
|
542
|
+
prompts.clear();
|
|
543
|
+
turnConfigs.clear();
|
|
544
|
+
runtime.view = {
|
|
545
|
+
...initialView(runtime.view?.nativeModel ?? ''),
|
|
546
|
+
mode: 'auto',
|
|
547
|
+
keySet: runtime.view?.keySet ?? false,
|
|
548
|
+
health: classifier.snapshot(),
|
|
549
|
+
configPath: config.nativePath,
|
|
550
|
+
tuning: tuningOf(config),
|
|
551
|
+
phase: runtime.view?.phase === 'unavailable' ? 'unavailable' : 'ready',
|
|
552
|
+
error: runtime.view?.phase === 'unavailable' ? runtime.view.error : null,
|
|
553
|
+
};
|
|
554
|
+
return next(e);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
on('ui.render', { component: ['AbovePrompt', 'Pane'] }, async ($, e, next) => {
|
|
558
|
+
if (e.component === 'Pane' && e.requestId !== PANE) return next(e);
|
|
559
|
+
const storedView = await readView($, runtime);
|
|
560
|
+
const view = { ...storedView, mode: await modeOf($, runtime, storedView.mode) };
|
|
561
|
+
const elements = $.ui.resolve(e);
|
|
562
|
+
const { Box, Text, Button } = elements;
|
|
563
|
+
if (e.component === 'AbovePrompt')
|
|
564
|
+
return Box({
|
|
565
|
+
flexDirection: 'column',
|
|
566
|
+
children: [
|
|
567
|
+
await next(e),
|
|
568
|
+
Box({
|
|
569
|
+
children: [
|
|
570
|
+
Text({ color: view.error ? 'yellow' : 'cyan', children: bandText(view) }),
|
|
571
|
+
Text({ children: ' ' }),
|
|
572
|
+
Button({
|
|
573
|
+
key: 'details',
|
|
574
|
+
label: 'Router',
|
|
575
|
+
plain: true,
|
|
576
|
+
onPress: () => $.ui.open({ id: PANE, title: 'Jev Router', focus: true, closeOnEscape: true }),
|
|
577
|
+
}),
|
|
578
|
+
],
|
|
579
|
+
}),
|
|
580
|
+
],
|
|
581
|
+
});
|
|
582
|
+
const usage = await $.session.usage().catch(() => null);
|
|
583
|
+
return renderPanel(elements, config, view, usage, {
|
|
584
|
+
mode: (mode) => changeMode($, runtime, mode),
|
|
585
|
+
pin: async (tier) => {
|
|
586
|
+
$.ui.log(await setPin($, runtime, tier));
|
|
587
|
+
},
|
|
588
|
+
key: async () => {
|
|
589
|
+
const command = `/plugin configure ${$.plugin.name}`;
|
|
590
|
+
const draft = await $.prompt.read();
|
|
591
|
+
await updateView($, runtime, {
|
|
592
|
+
notice: `Run ${command} to edit the secure key. Your existing prompt is preserved.`,
|
|
593
|
+
});
|
|
594
|
+
await $.ui.close({ id: PANE });
|
|
595
|
+
if (!draft.text) {
|
|
596
|
+
const filled = await $.prompt.fill({ text: command });
|
|
597
|
+
$.ui.log(
|
|
598
|
+
filled.isFilled
|
|
599
|
+
? 'Press Enter to open secure Jev key configuration.'
|
|
600
|
+
: `Run ${command} to configure the secure key.`,
|
|
601
|
+
);
|
|
602
|
+
} else $.ui.log(`Run ${command} after finishing your current draft.`);
|
|
603
|
+
},
|
|
604
|
+
close: () => $.ui.close({ id: PANE }),
|
|
605
|
+
tune: async (key, value) => {
|
|
606
|
+
await updateView($, runtime, {
|
|
607
|
+
tuning: { ...tuningOf(config), ...runtime.view?.tuning, [key]: value },
|
|
608
|
+
notice: 'Draft — select Save tuning to apply.',
|
|
609
|
+
});
|
|
610
|
+
},
|
|
611
|
+
save: async () => {
|
|
612
|
+
try {
|
|
613
|
+
const draft = runtime.view.tuning;
|
|
614
|
+
const path = config.nativePath;
|
|
615
|
+
if ((await $.fs.exists(path)) && (await $.fs.stat(path)).isLink) {
|
|
616
|
+
await updateView($, runtime, { notice: 'Config is a symlink. Edit its maintained source instead.' });
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const previous = (await $.fs.exists(path)) ? JSON.parse(await $.fs.read(path)) : {};
|
|
620
|
+
const file = {
|
|
621
|
+
...previous,
|
|
622
|
+
jev: { ...previous.jev, timeoutMs: draft.timeoutMs },
|
|
623
|
+
policy: { ...previous.policy, downgradeVotes: draft.downgradeVotes, downgradeHorizonTurns: draft.horizon },
|
|
624
|
+
};
|
|
625
|
+
const checked = loadConfig({ userFile: file });
|
|
626
|
+
await $.fs.write(path, `${JSON.stringify(file, null, 2)}\n`);
|
|
627
|
+
config = { ...checked, nativePath: path };
|
|
628
|
+
await updateView($, runtime, { notice: 'Saved. New tuning applies to future turns.' });
|
|
629
|
+
} catch {
|
|
630
|
+
await updateView($, runtime, { notice: 'Could not save valid tuning. Existing configuration is unchanged.' });
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
}
|