@alexeiled/claude-router 1.10.2 → 1.10.5
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 +36 -0
- package/README.md +2 -2
- package/docs/activity-routing.md +13 -4
- package/docs/architecture.md +11 -7
- package/docs/configuration.md +9 -7
- package/docs/evaluation.md +1 -1
- package/docs/native-router.md +1 -1
- package/docs/user-guide.md +43 -26
- package/hooks/native-router.mjs +328 -211
- package/lib/activity-stats.mjs +54 -20
- package/lib/activity.mjs +14 -12
- package/lib/config.mjs +9 -0
- package/lib/display.mjs +7 -3
- package/lib/facts.mjs +82 -0
- package/lib/panel.mjs +12 -2
- package/lib/policy.mjs +38 -14
- package/lib/records.mjs +14 -0
- package/lib/route.mjs +93 -7
- package/lib/savings.mjs +4 -8
- package/lib/subagents.mjs +3 -6
- package/package.json +1 -1
- package/scripts/migrate-config.mjs +23 -3
- package/types/index.d.ts +4 -1
package/lib/savings.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// the hook feeds each reply and keeps the totals in the view and, per session, in $.store under SAVINGS_PREFIX.
|
|
4
4
|
|
|
5
5
|
import { effortSharesCache, ratesAt } from './cost.mjs';
|
|
6
|
+
import { afterResetOf, earlierSince, isRecord } from './records.mjs';
|
|
6
7
|
import { HISTORY_SHRINK, isSameModel } from './route.mjs';
|
|
7
8
|
|
|
8
9
|
// Each session keeps its own record under this prefix and its session id: $.store has no atomic update, and Claude
|
|
@@ -213,9 +214,7 @@ const STORE_KEYS = Object.keys(emptySavingsStore());
|
|
|
213
214
|
// break a turn or the pane.
|
|
214
215
|
export function readSavingsStore(value) {
|
|
215
216
|
const ok =
|
|
216
|
-
value
|
|
217
|
-
typeof value === 'object' &&
|
|
218
|
-
!Array.isArray(value) &&
|
|
217
|
+
isRecord(value) &&
|
|
219
218
|
Object.keys(value).length === STORE_KEYS.length &&
|
|
220
219
|
STORE_KEYS.every((key) => Object.hasOwn(value, key)) &&
|
|
221
220
|
value.version === 1 &&
|
|
@@ -227,16 +226,13 @@ export function readSavingsStore(value) {
|
|
|
227
226
|
}
|
|
228
227
|
|
|
229
228
|
// A record whose first reply came before the last reset at `resetAt` (ms) reads as empty.
|
|
230
|
-
export
|
|
231
|
-
return Number.isFinite(resetAt) && record.since !== null && record.since < resetAt ? emptySavingsStore() : record;
|
|
232
|
-
}
|
|
229
|
+
export const afterReset = (record, resetAt) => afterResetOf(record, resetAt, emptySavingsStore);
|
|
233
230
|
|
|
234
231
|
// The totals kept across sessions: every session's record since the last reset added up, since the earliest.
|
|
235
232
|
export function mergeSavingsStores(values, resetAt = null) {
|
|
236
233
|
const merged = emptySavingsStore();
|
|
237
234
|
for (const one of values.map((value) => afterReset(readSavingsStore(value), resetAt))) {
|
|
238
|
-
|
|
239
|
-
merged.since = since.length ? Math.min(...since) : null;
|
|
235
|
+
merged.since = earlierSince(merged.since, one.since);
|
|
240
236
|
merged.replies += one.replies;
|
|
241
237
|
for (const sum of SUMS) merged[sum] += one[sum];
|
|
242
238
|
}
|
package/lib/subagents.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { CLASSIFY } from './config.mjs';
|
|
6
6
|
import { clip } from './facts.mjs';
|
|
7
|
+
import { afterResetOf, earlierSince, isRecord } from './records.mjs';
|
|
7
8
|
import { modelSpec, requestUsd } from './savings.mjs';
|
|
8
9
|
|
|
9
10
|
export const SUBAGENT_PREFIX = 'subagents:v1:';
|
|
@@ -169,7 +170,6 @@ export function emptySubagentStore() {
|
|
|
169
170
|
|
|
170
171
|
const ROUTED_SUMS = ['agents', 'differs', 'requests', 'pricedRequests', 'coreUsd', 'routedUsd', 'respawns', 'failures'];
|
|
171
172
|
const PASSED_SUMS = ['agents', 'requests', 'pricedRequests', 'usd'];
|
|
172
|
-
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
173
173
|
|
|
174
174
|
const emptyRouted = () => ({ ...Object.fromEntries(ROUTED_SUMS.map((sum) => [sum, 0])), choices: {} });
|
|
175
175
|
const emptyPassed = () => Object.fromEntries(PASSED_SUMS.map((sum) => [sum, 0]));
|
|
@@ -248,9 +248,7 @@ export function readSubagentStore(value) {
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
// A record whose first agent came before the last reset at `resetAt` (ms) reads as empty.
|
|
251
|
-
export
|
|
252
|
-
return Number.isFinite(resetAt) && record.since !== null && record.since < resetAt ? emptySubagentStore() : record;
|
|
253
|
-
}
|
|
251
|
+
export const subagentsAfterReset = (record, resetAt) => afterResetOf(record, resetAt, emptySubagentStore);
|
|
254
252
|
|
|
255
253
|
function addInto(target, source, sums, withChoices) {
|
|
256
254
|
for (const [key, counts] of Object.entries(source)) {
|
|
@@ -266,8 +264,7 @@ function addInto(target, source, sums, withChoices) {
|
|
|
266
264
|
export function mergeSubagentStores(values, resetAt = null) {
|
|
267
265
|
const merged = emptySubagentStore();
|
|
268
266
|
for (const one of values.map((value) => subagentsAfterReset(readSubagentStore(value), resetAt))) {
|
|
269
|
-
|
|
270
|
-
merged.since = since.length ? Math.min(...since) : null;
|
|
267
|
+
merged.since = earlierSince(merged.since, one.since);
|
|
271
268
|
addInto(merged.routed.on, one.routed.on, ROUTED_SUMS, true);
|
|
272
269
|
addInto(merged.passed, one.passed, PASSED_SUMS, false);
|
|
273
270
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.5",
|
|
4
4
|
"description": "Native Claude Code Mod: model and effort routing for the main conversation, advised by Jev, Clef, Clef Flash, OpenAI or Ollama.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFile, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { chmod, lstat, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { resolve } from 'node:path';
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
import { isGatewayLayout, loadConfig } from '../lib/config.mjs';
|
|
@@ -48,10 +48,30 @@ export async function migrateFile(path) {
|
|
|
48
48
|
const file = JSON.parse(original);
|
|
49
49
|
const next = migrateConfig(file);
|
|
50
50
|
if (JSON.stringify(file) === JSON.stringify(next)) return null;
|
|
51
|
+
// Renaming over a symlink would replace the link, not the file a dotfile manager keeps: as the pane, refuse it.
|
|
52
|
+
const info = await lstat(path);
|
|
53
|
+
if (info.isSymbolicLink()) throw new Error(`${path} is a symlink: migrate the file it points to`);
|
|
51
54
|
// Named for the layout it keeps, so a 1.1 migration never collides with a 0.8 backup already on disk.
|
|
52
55
|
const backup = `${path}.${isGatewayLayout(file) ? 'v0.8' : 'v1.1'}.backup`;
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
try {
|
|
57
|
+
await writeFile(backup, original, { flag: 'wx', mode: 0o600 });
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error.code === 'EEXIST') error.message = `${backup} already exists: move it away, then run again`;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
// Written beside it and renamed over it, so a crash mid-write leaves router.json whole. A failure removes this
|
|
63
|
+
// run's backup and its temporary file, never one it did not create, so the next run starts clean.
|
|
64
|
+
const temp = `${path}.${process.pid}.tmp`;
|
|
65
|
+
let created = false;
|
|
66
|
+
try {
|
|
67
|
+
await writeFile(temp, `${JSON.stringify(next, null, 2)}\n`, { flag: 'wx' });
|
|
68
|
+
created = true;
|
|
69
|
+
await chmod(temp, info.mode & 0o777);
|
|
70
|
+
await rename(temp, path);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
await Promise.all([created ? rm(temp, { force: true }) : null, rm(backup, { force: true })]);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
55
75
|
return backup;
|
|
56
76
|
}
|
|
57
77
|
|
package/types/index.d.ts
CHANGED
|
@@ -39,9 +39,12 @@ declare module 'claude-code' {
|
|
|
39
39
|
lateral?: { taken: number; refused: number };
|
|
40
40
|
agreement: { matched: number; total: number };
|
|
41
41
|
}
|
|
42
|
-
// Cross-session counts under store key 'activity:stats:v1'
|
|
42
|
+
// Cross-session counts, one record per session under store key 'activity:stats:v1:<session id>', and their sum.
|
|
43
|
+
// `since` is the first count after a reset (null in the key all sessions shared before 1.10.3). `runs` buckets run
|
|
44
|
+
// lengths 1, 2, 3, 4, 5+.
|
|
43
45
|
interface RouterActivityStore {
|
|
44
46
|
version: 1;
|
|
47
|
+
since: number | null;
|
|
45
48
|
confusion: Partial<Record<RouterActivityAnswer | 'none', Partial<Record<RouterObserved, number>>>>;
|
|
46
49
|
runs: Partial<Record<RouterActivity, [number, number, number, number, number]>>;
|
|
47
50
|
lateral: { taken: number; refused: number };
|