context-doctor 0.44.0 → 0.46.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/README.md +5 -2
- package/dist/budget.d.ts +14 -1
- package/dist/budget.js +50 -28
- package/dist/cache.js +5 -4
- package/dist/cachemisses.js +6 -4
- package/dist/cacheplan.js +2 -2
- package/dist/cachettl.js +8 -5
- package/dist/ci.js +18 -2
- package/dist/cli.js +27 -6
- package/dist/codexcompact.js +14 -7
- package/dist/coldresume.d.ts +6 -0
- package/dist/coldresume.js +20 -11
- package/dist/compactwindow.js +14 -8
- package/dist/doctor.js +26 -2
- package/dist/extract.d.ts +2 -2
- package/dist/extract.js +14 -3
- package/dist/fanout.js +1 -1
- package/dist/fix.js +14 -10
- package/dist/hook.js +6 -2
- package/dist/httpguard.d.ts +9 -1
- package/dist/httpguard.js +5 -4
- package/dist/lens.js +7 -7
- package/dist/limits.d.ts +14 -5
- package/dist/limits.js +26 -17
- package/dist/mcp.js +62 -62
- package/dist/mcpschema.js +5 -3
- package/dist/mod/api.d.ts +129 -0
- package/dist/mod/api.js +12 -0
- package/dist/mod/logic.d.ts +146 -0
- package/dist/mod/logic.js +222 -0
- package/dist/mod/register.d.ts +58 -0
- package/dist/mod/register.js +237 -0
- package/dist/modelids.d.ts +23 -0
- package/dist/modelids.js +49 -0
- package/dist/modmeasure.d.ts +121 -0
- package/dist/modmeasure.js +298 -0
- package/dist/orgrates.d.ts +75 -0
- package/dist/orgrates.js +165 -0
- package/dist/overhead.js +11 -8
- package/dist/pack.js +95 -22
- package/dist/pricing.d.ts +123 -6
- package/dist/pricing.js +236 -62
- package/dist/proxy.d.ts +12 -1
- package/dist/proxy.js +13 -1
- package/dist/prune.js +55 -3
- package/dist/savings.js +19 -10
- package/dist/scan.d.ts +7 -1
- package/dist/scan.js +19 -1
- package/dist/sessionprice.d.ts +25 -2
- package/dist/sessionprice.js +60 -20
- package/dist/spend.d.ts +62 -10
- package/dist/spend.js +255 -29
- package/dist/statusline.d.ts +10 -2
- package/dist/statusline.js +18 -9
- package/dist/subagentmodel.d.ts +2 -2
- package/dist/subagentmodel.js +31 -24
- package/dist/subagentpay.d.ts +7 -1
- package/dist/subagentpay.js +51 -25
- package/dist/subagents.js +15 -10
- package/dist/tasks.js +9 -6
- package/dist/tooloutput.js +14 -5
- package/package.json +1 -1
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* context-doctor's mod: the status line's measurements and `fix`'s plan where
|
|
3
|
+
* the status line never runs (Claude Code's Desktop app, the VS Code
|
|
4
|
+
* extension), and in the terminal too.
|
|
5
|
+
*
|
|
6
|
+
* Shipped inside the plugin: hooks/hooks.json names this file under
|
|
7
|
+
* `modules`. Claude Code runs mods from 2.1.287 (terminal), 2.1.286 (the
|
|
8
|
+
* Desktop app's Code tab) and 2.1.299 (VS Code); an older one never loads
|
|
9
|
+
* it and keeps the rest of the plugin.
|
|
10
|
+
*
|
|
11
|
+
* - After each turn (`session.measure`) it runs `context-doctor mod measure`
|
|
12
|
+
* with Claude Code's rate-limit windows and session cost: the CLI keeps
|
|
13
|
+
* them (the limits ledger; Claude Code's cost beside ours) and answers
|
|
14
|
+
* with the band's figures.
|
|
15
|
+
* - A band above the prompt, only when it pays: the cache about to go cold
|
|
16
|
+
* or gone cold on a big context (with the re-write's price), the session
|
|
17
|
+
* past a price threshold. On a cold return a "Compact first" button calls
|
|
18
|
+
* `$.session.compact()`, between turns, on the press alone.
|
|
19
|
+
* - `/context-doctor-fix` opens a pane with `fix`'s plan and an Apply / Undo
|
|
20
|
+
* button per lever. Each press runs the CLI's own `fix --apply <id>` /
|
|
21
|
+
* `fix undo <id>`: the same lock, backup and record as in a terminal.
|
|
22
|
+
*
|
|
23
|
+
* Nothing happens without a press: no auto-compaction, no auto-apply. The
|
|
24
|
+
* mod writes no file itself; the CLI writes ours. Off: CONTEXT_DOCTOR_NO_MOD=1.
|
|
25
|
+
*
|
|
26
|
+
* The mods runtime has no Node, so this file imports only ./logic.js (no
|
|
27
|
+
* imports of its own) and reaches everything else through `$`. Claude Code
|
|
28
|
+
* reads which calls a module makes from its source: `$` is passed only to
|
|
29
|
+
* functions declared at the top of this file.
|
|
30
|
+
*/
|
|
31
|
+
import { applyOutcome, band, fixArgv, MODEL_CHANGE, outcome, paneRows, parsePlan, withPresses } from "./logic.js";
|
|
32
|
+
export const COMMAND = "context-doctor-fix";
|
|
33
|
+
export const PANE = "context-doctor-fix";
|
|
34
|
+
/** How often the band checks whether its text changed (a countdown in minutes). */
|
|
35
|
+
const TICK_MS = 30_000;
|
|
36
|
+
/** `fix` measures 30 days of history: give it time. */
|
|
37
|
+
const FIX_TIMEOUT_MS = 10 * 60_000;
|
|
38
|
+
const MEASURE_TIMEOUT_MS = 30_000;
|
|
39
|
+
const fresh = () => ({ off: false, always: false, hidden: new Set(), drawn: "", compacting: false, loading: false, presses: new Map() });
|
|
40
|
+
let S = fresh();
|
|
41
|
+
/** For tests: the state the hooks share. */
|
|
42
|
+
export const state = () => S;
|
|
43
|
+
/** The CLI beside this module, in the same plugin: dist/mod/register.js → dist/cli.js. */
|
|
44
|
+
function cliOf($) {
|
|
45
|
+
// The module may import nothing but ./logic.js (no node:path): join with the
|
|
46
|
+
// separator the root already uses, so Windows gets D:\…\dist\cli.js.
|
|
47
|
+
const root = $.plugin.root;
|
|
48
|
+
const sep = root.includes("\\") && !root.includes("/") ? "\\" : "/";
|
|
49
|
+
return `${root}${sep}dist${sep}cli.js`;
|
|
50
|
+
}
|
|
51
|
+
function bandKey(now, surface) {
|
|
52
|
+
const b = band(S.data, now, surface, { hidden: S.hidden, always: S.always });
|
|
53
|
+
return b.kind === "none" ? "" : `${b.key}|${b.text}`;
|
|
54
|
+
}
|
|
55
|
+
/** Hand the reading to the CLI; with no CLI (no node on PATH), keep what Claude Code said. */
|
|
56
|
+
async function measure($, e) {
|
|
57
|
+
const fallback = { at: await $.clock.now(), coldMinTokens: 150_000, thresholds: [25, 100, 500], ...(e.cost ? { ccUsd: e.cost.usd } : {}) };
|
|
58
|
+
try {
|
|
59
|
+
const sessionId = await $.session.id();
|
|
60
|
+
const cwd = await $.session.cwd();
|
|
61
|
+
let model;
|
|
62
|
+
try {
|
|
63
|
+
model = await $.session.model();
|
|
64
|
+
}
|
|
65
|
+
catch { /* measured without it */ }
|
|
66
|
+
const run = await $.process.run(["node", cliOf($), "mod", "measure"], { stdin: JSON.stringify({ sessionId, cwd, model, rateLimits: e.rateLimits, cost: e.cost, context: e.context }), timeoutMs: MEASURE_TIMEOUT_MS });
|
|
67
|
+
const got = JSON.parse(run.stdout || "{}");
|
|
68
|
+
S.data = got && typeof got.at === "number" ? got : fallback;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
S.data = fallback;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The press is the consent: one compaction, between turns, and a line in the ledger that it was taken. */
|
|
75
|
+
async function compact($) {
|
|
76
|
+
if (S.compacting)
|
|
77
|
+
return;
|
|
78
|
+
S.compacting = true;
|
|
79
|
+
$.ui.invalidate("ui.render");
|
|
80
|
+
const tokens = S.data?.tokens;
|
|
81
|
+
try {
|
|
82
|
+
const r = await $.session.compact();
|
|
83
|
+
if (r && typeof r.skip === "string") {
|
|
84
|
+
$.ui.toast(`context-doctor: not compacted: ${r.skip}`);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// The old context's figures no longer hold: the next turn measures the new one.
|
|
88
|
+
if (S.data)
|
|
89
|
+
S.data = { ...S.data, tokens: undefined, lastReplyAt: undefined, rewriteUsd: undefined };
|
|
90
|
+
try {
|
|
91
|
+
const sessionId = await $.session.id();
|
|
92
|
+
await $.process.run(["node", cliOf($), "mod", "event"], { stdin: JSON.stringify({ sessionId, ev: "compact", tokens }), timeoutMs: MEASURE_TIMEOUT_MS });
|
|
93
|
+
}
|
|
94
|
+
catch { /* the count is a nicety */ }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
$.ui.toast("context-doctor: Claude Code is busy; compact once the turn ends.");
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
S.compacting = false;
|
|
102
|
+
$.ui.invalidate("ui.render");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** `fix --json`: the plan, measured on the history (it takes a while on a long one). */
|
|
106
|
+
async function loadPlan($) {
|
|
107
|
+
S.loading = true;
|
|
108
|
+
S.planError = undefined;
|
|
109
|
+
$.ui.invalidate("ui.render");
|
|
110
|
+
try {
|
|
111
|
+
const run = await $.process.run(fixArgv("node", cliOf($), "plan"), { timeoutMs: FIX_TIMEOUT_MS });
|
|
112
|
+
const p = parsePlan(run.stdout);
|
|
113
|
+
if (p) {
|
|
114
|
+
S.plan = p;
|
|
115
|
+
S.presses.clear();
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
S.planError = `context-doctor fix did not answer with a plan (exit ${run.exitCode}): ${(run.stderr || run.stdout).trim().split("\n").slice(-2).join(" ")}`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
S.planError = `context-doctor could not run: ${err instanceof Error ? err.message : String(err)}. It needs Node.js on the PATH Claude Code starts with.`;
|
|
123
|
+
}
|
|
124
|
+
S.loading = false;
|
|
125
|
+
$.ui.invalidate("ui.render");
|
|
126
|
+
}
|
|
127
|
+
/** One press, one CLI run: `fix --apply <id>` or `fix undo <id>`. */
|
|
128
|
+
async function press($, op, id) {
|
|
129
|
+
if (S.busy)
|
|
130
|
+
return;
|
|
131
|
+
S.busy = `${op}:${id}`;
|
|
132
|
+
$.ui.invalidate("ui.render");
|
|
133
|
+
try {
|
|
134
|
+
const run = await $.process.run(fixArgv("node", cliOf($), op, id), { timeoutMs: FIX_TIMEOUT_MS });
|
|
135
|
+
const r = op === "apply" ? applyOutcome(run.stdout, run.stderr, run.exitCode) : outcome(run.stdout, run.stderr, run.exitCode);
|
|
136
|
+
S.last = { id, ...r };
|
|
137
|
+
if (r.ok)
|
|
138
|
+
S.presses.set(id, op === "apply" ? "applied" : "undone");
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
S.last = { id, ok: false, lines: [`could not run context-doctor: ${err instanceof Error ? err.message : String(err)}`] };
|
|
142
|
+
}
|
|
143
|
+
S.busy = undefined;
|
|
144
|
+
$.ui.invalidate("ui.render");
|
|
145
|
+
}
|
|
146
|
+
function hide($, key) {
|
|
147
|
+
S.hidden.add(key);
|
|
148
|
+
$.ui.invalidate("ui.render");
|
|
149
|
+
}
|
|
150
|
+
function refresh($) {
|
|
151
|
+
if (!S.loading)
|
|
152
|
+
void loadPlan($);
|
|
153
|
+
}
|
|
154
|
+
/** The countdown is in minutes: redraw only when the band's words change. */
|
|
155
|
+
async function tick($) {
|
|
156
|
+
const now = await $.clock.now();
|
|
157
|
+
const k = bandKey(now, "desktop") + bandKey(now, "terminal");
|
|
158
|
+
if (k !== S.drawn) {
|
|
159
|
+
S.drawn = k;
|
|
160
|
+
$.ui.invalidate("ui.render");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export function register(on) {
|
|
164
|
+
S = fresh();
|
|
165
|
+
on("session.start", async ($, e, next) => {
|
|
166
|
+
try {
|
|
167
|
+
S.off = (await $.env.get("CONTEXT_DOCTOR_NO_MOD")) === "1";
|
|
168
|
+
S.always = (await $.env.get("CONTEXT_DOCTOR_MOD_BAND")) === "always";
|
|
169
|
+
if (!S.off) {
|
|
170
|
+
await $.command.register({ name: "context-doctor-fix", description: "context-doctor: the fix plan, with an Apply and an Undo button per lever (nothing applies without a press)" });
|
|
171
|
+
$.clock.every(TICK_MS, () => void tick($));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
/* an engine without one of these: the mod stays quiet */
|
|
176
|
+
}
|
|
177
|
+
return next(e);
|
|
178
|
+
});
|
|
179
|
+
on("session.measure", async ($, e, next) => {
|
|
180
|
+
const r = await next(e);
|
|
181
|
+
if (S.off)
|
|
182
|
+
return r;
|
|
183
|
+
await measure($, e);
|
|
184
|
+
$.ui.invalidate("ui.render");
|
|
185
|
+
return r;
|
|
186
|
+
});
|
|
187
|
+
on("ui.render", { component: "AbovePrompt" }, async ($, e, next) => {
|
|
188
|
+
if (S.off || e.props.hasSurvey)
|
|
189
|
+
return next(e);
|
|
190
|
+
const b = band(S.data, await $.clock.now(), e.surface, { hidden: S.hidden, always: S.always });
|
|
191
|
+
// A compaction only runs between turns: no button while one runs.
|
|
192
|
+
if (b.kind === "none" || (b.kind === "cold" && e.props.isWorking))
|
|
193
|
+
return next(e);
|
|
194
|
+
const { Box, Text, Button } = $.ui.resolve(e);
|
|
195
|
+
const hideButton = h(Button, { key: "cd-hide", label: "Hide", onPress: () => hide($, b.key) });
|
|
196
|
+
if (b.kind !== "cold")
|
|
197
|
+
return h(Box, { flexDirection: "row", flexWrap: "wrap", columnGap: 1 }, h(Text, { dimColor: true }, b.text), hideButton);
|
|
198
|
+
return h(Box, { flexDirection: "column" }, h(Text, { dimColor: true }, b.text), h(Box, { flexDirection: "row", flexWrap: "wrap", columnGap: 1 }, h(Button, { key: "cd-compact", label: S.compacting ? "Compacting…" : "Compact first", variant: "primary", onPress: () => compact($) }), h(Text, { dimColor: true }, b.compact), hideButton));
|
|
199
|
+
});
|
|
200
|
+
on("command.run", { command: "context-doctor-fix" }, async ($) => {
|
|
201
|
+
if (S.off)
|
|
202
|
+
return { text: "context-doctor's mod is off (CONTEXT_DOCTOR_NO_MOD=1)." };
|
|
203
|
+
await $.ui.open({ id: PANE, title: "context-doctor fix" });
|
|
204
|
+
if (!S.plan && !S.loading)
|
|
205
|
+
void loadPlan($);
|
|
206
|
+
return { text: "Opened context-doctor's fix plan. Nothing is applied until you press Apply." };
|
|
207
|
+
});
|
|
208
|
+
on("ui.render", { component: "Pane", requestId: PANE }, async ($, e) => {
|
|
209
|
+
const { Box, Text, Button } = $.ui.resolve(e);
|
|
210
|
+
const body = [h(Text, { bold: true }, "context-doctor fix: each lever, by what it would have saved you")];
|
|
211
|
+
if (S.loading)
|
|
212
|
+
body.push(h(Text, { dimColor: true }, "Measuring your history (30 days)…"));
|
|
213
|
+
if (S.planError)
|
|
214
|
+
body.push(h(Text, null, S.planError));
|
|
215
|
+
const last = S.last;
|
|
216
|
+
if (last)
|
|
217
|
+
body.push(h(Box, { flexDirection: "column", marginTop: 1 }, ...[`${last.ok ? "Done" : "Not done"}: ${last.id}`, ...last.lines].map((l) => h(Text, { dimColor: last.ok }, l))));
|
|
218
|
+
if (S.plan) {
|
|
219
|
+
if (S.plan.recordProblem)
|
|
220
|
+
body.push(h(Text, null, `⚠ ${S.plan.recordProblem}`));
|
|
221
|
+
const rows = withPresses(paneRows(S.plan), S.presses);
|
|
222
|
+
if (!rows.length)
|
|
223
|
+
body.push(h(Text, { dimColor: true }, "Nothing to fix here."));
|
|
224
|
+
for (const row of rows) {
|
|
225
|
+
const buttons = [];
|
|
226
|
+
if (row.apply)
|
|
227
|
+
buttons.push(h(Button, { key: `apply:${row.id}`, label: S.busy === `apply:${row.id}` ? "Applying…" : row.apply, ...(row.id === MODEL_CHANGE ? {} : { variant: "primary" }), onPress: () => press($, "apply", row.id) }));
|
|
228
|
+
if (row.undo)
|
|
229
|
+
buttons.push(h(Button, { key: `undo:${row.id}`, label: S.busy === `undo:${row.id}` ? "Undoing…" : row.undo, onPress: () => press($, "undo", row.id) }));
|
|
230
|
+
body.push(h(Box, { key: `row:${row.id}`, flexDirection: "column", marginTop: 1 }, h(Text, { bold: true }, row.head), ...row.lines.map((l) => h(Text, { dimColor: !(row.modelChange && l.startsWith("Model change")) }, l)), ...(buttons.length ? [h(Box, { flexDirection: "row", columnGap: 1 }, ...buttons)] : [])));
|
|
231
|
+
}
|
|
232
|
+
body.push(h(Text, { dimColor: true }, "Each figure is measured alone; the levers overlap, so together they save less. Undo puts back exactly what was there."));
|
|
233
|
+
}
|
|
234
|
+
body.push(h(Box, { flexDirection: "row", marginTop: 1 }, h(Button, { key: "refresh", label: S.loading ? "Measuring…" : "Refresh", onPress: () => refresh($) })));
|
|
235
|
+
return h(Box, { flexDirection: "column" }, ...body);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Claude models a text names, to check each has a price of its own
|
|
3
|
+
* (pricing.ts hasKnownPrice) before a user runs it: Claude Code ships a
|
|
4
|
+
* model days before a context-doctor release (2.1.322 made Fable 5.2 the
|
|
5
|
+
* default the day its price changed), and an id the table does not know is
|
|
6
|
+
* priced by its family's row, an estimate that was five times too high on
|
|
7
|
+
* Fable 5.2's cache reads. Read from Claude Code's binary (its model
|
|
8
|
+
* catalog), its CHANGELOG and the price page by scripts/check-models.mjs.
|
|
9
|
+
*
|
|
10
|
+
* Three spellings: Bedrock ids (`anthropic.claude-opus-5-5`,
|
|
11
|
+
* `us.anthropic.claude-sonnet-4-5-20250929-v1:0`), the catalog's cleanest
|
|
12
|
+
* list; first-party ids of the current naming (`claude-fable-5-2`); and
|
|
13
|
+
* display names (`Claude Fable 5.2`), which is how the price page names
|
|
14
|
+
* them. First-party and display names count from major version 4 (the
|
|
15
|
+
* naming the table keys on): older aliases Claude Code still accepts
|
|
16
|
+
* (`claude-haiku-3-5`, `claude-sonnet-3-7`) are not ids anything bills.
|
|
17
|
+
*/
|
|
18
|
+
/** Ids that have no price on purpose, with the reason. */
|
|
19
|
+
export declare const UNPRICED_ON_PURPOSE: Record<string, string>;
|
|
20
|
+
/** Every Claude model id `text` names, each once, sorted. */
|
|
21
|
+
export declare function claudeModelIds(text: string): string[];
|
|
22
|
+
/** The Claude models `text` names that have no price of their own and no reason to lack one. */
|
|
23
|
+
export declare function unpricedClaudeIds(text: string): string[];
|
package/dist/modelids.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Claude models a text names, to check each has a price of its own
|
|
3
|
+
* (pricing.ts hasKnownPrice) before a user runs it: Claude Code ships a
|
|
4
|
+
* model days before a context-doctor release (2.1.322 made Fable 5.2 the
|
|
5
|
+
* default the day its price changed), and an id the table does not know is
|
|
6
|
+
* priced by its family's row, an estimate that was five times too high on
|
|
7
|
+
* Fable 5.2's cache reads. Read from Claude Code's binary (its model
|
|
8
|
+
* catalog), its CHANGELOG and the price page by scripts/check-models.mjs.
|
|
9
|
+
*
|
|
10
|
+
* Three spellings: Bedrock ids (`anthropic.claude-opus-5-5`,
|
|
11
|
+
* `us.anthropic.claude-sonnet-4-5-20250929-v1:0`), the catalog's cleanest
|
|
12
|
+
* list; first-party ids of the current naming (`claude-fable-5-2`); and
|
|
13
|
+
* display names (`Claude Fable 5.2`), which is how the price page names
|
|
14
|
+
* them. First-party and display names count from major version 4 (the
|
|
15
|
+
* naming the table keys on): older aliases Claude Code still accepts
|
|
16
|
+
* (`claude-haiku-3-5`, `claude-sonnet-3-7`) are not ids anything bills.
|
|
17
|
+
*/
|
|
18
|
+
import { hasKnownPrice } from "./pricing.js";
|
|
19
|
+
const FAMILY = "opus|sonnet|haiku|fable|mythos";
|
|
20
|
+
/** Ids that have no price on purpose, with the reason. */
|
|
21
|
+
export const UNPRICED_ON_PURPOSE = {
|
|
22
|
+
"claude-mythos-preview": "no list price on the page; priced by the Fable/Mythos family row as an estimate",
|
|
23
|
+
};
|
|
24
|
+
/** The model a Bedrock id names, as a first-party id: region prefix, `-v1:0` and the date dropped. */
|
|
25
|
+
function fromBedrock(id) {
|
|
26
|
+
return id.replace(/^(?:[a-z]{2,6}\.)?anthropic\./, "").replace(/-v\d+(?::\d+)?$/, "").replace(/-\d{8}$/, "");
|
|
27
|
+
}
|
|
28
|
+
/** Every Claude model id `text` names, each once, sorted. */
|
|
29
|
+
export function claudeModelIds(text) {
|
|
30
|
+
const out = new Set();
|
|
31
|
+
// A model's Bedrock id has a version number (com.anthropic.claude-code, an app id, has none).
|
|
32
|
+
for (const m of text.matchAll(/(?<![\w.-])(?:(?:us|eu|apac|global|au|jp|ca|us-gov)\.)?anthropic\.(claude-[a-z0-9-]*?\d[a-z0-9-]*?)(?:-v\d+(?::\d+)?)?(?![\w.-]*\w)/g))
|
|
33
|
+
out.add(fromBedrock(m[1]));
|
|
34
|
+
for (const m of text.matchAll(new RegExp(`(?<![\\w.-])claude-(${FAMILY})-(\\d{1,2})(?:-(\\d{1,2}))?(?:-\\d{8})?(?![\\w-])`, "g"))) {
|
|
35
|
+
if (Number(m[2]) < 4)
|
|
36
|
+
continue;
|
|
37
|
+
out.add(`claude-${m[1]}-${m[2]}${m[3] && m[3] !== "0" ? `-${m[3]}` : ""}`);
|
|
38
|
+
}
|
|
39
|
+
for (const m of text.matchAll(new RegExp(`\\bClaude (${FAMILY.replace(/\b\w/g, (c) => c.toUpperCase())}) (\\d{1,2})(?:\\.(\\d{1,2}))?(?![\\w.]*\\d)`, "g"))) {
|
|
40
|
+
if (Number(m[2]) < 4)
|
|
41
|
+
continue;
|
|
42
|
+
out.add(`claude-${m[1].toLowerCase()}-${m[2]}${m[3] && m[3] !== "0" ? `-${m[3]}` : ""}`);
|
|
43
|
+
}
|
|
44
|
+
return [...out].sort();
|
|
45
|
+
}
|
|
46
|
+
/** The Claude models `text` names that have no price of their own and no reason to lack one. */
|
|
47
|
+
export function unpricedClaudeIds(text) {
|
|
48
|
+
return claudeModelIds(text).filter((id) => !hasKnownPrice(id) && !(id in UNPRICED_ON_PURPOSE));
|
|
49
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `context-doctor mod measure`: what the plugin's mod hands over after each
|
|
3
|
+
* turn, kept the way the status line keeps it.
|
|
4
|
+
*
|
|
5
|
+
* The status line never runs in the Desktop app's Code tab or the VS Code
|
|
6
|
+
* extension, so there the limit ledger stayed empty and Claude Code's own
|
|
7
|
+
* cost was never compared with ours (ROADMAP K2). A mod runs there: after
|
|
8
|
+
* each main-thread turn Claude Code raises `session.measure` with the
|
|
9
|
+
* rate-limit windows (`kind`, `percentUsed`, `resetsAt` as ISO 8601) and the
|
|
10
|
+
* session's cost (`cost.usd`, as /cost totals it). The mod has no file
|
|
11
|
+
* access of its own worth trusting with our formats, so it runs this command
|
|
12
|
+
* with that reading on stdin, and this:
|
|
13
|
+
*
|
|
14
|
+
* - appends the windows to the limits ledger in the status line's own
|
|
15
|
+
* format and with its rules (recordLimits: changes only, at most once a
|
|
16
|
+
* minute, never a lower figure of the same window from a parallel
|
|
17
|
+
* session), so `context-doctor limits` reads Desktop's readings too;
|
|
18
|
+
* - appends Claude Code's cost beside our own price of the same session
|
|
19
|
+
* (sessionTotal: the transcript and its subagents, as the every-prompt
|
|
20
|
+
* hook prices it) to a small capped ledger, for `doctor`'s comparison;
|
|
21
|
+
* - prints what the band needs, as JSON: the cache clock read from the
|
|
22
|
+
* transcript's tail (coldresume.ts), the re-write's price, the price.
|
|
23
|
+
*
|
|
24
|
+
* Silent like the status line: anything unreadable prints `{}` and exits 0.
|
|
25
|
+
* Off: CONTEXT_DOCTOR_NO_LIMITS_LOG=1 (the limits), CONTEXT_DOCTOR_NO_COST_LOG=1
|
|
26
|
+
* (the cost ledger).
|
|
27
|
+
*/
|
|
28
|
+
import type { BandData } from "./mod/logic.js";
|
|
29
|
+
/** What the mod sends: `session.measure`'s input, and who the session is. All optional. */
|
|
30
|
+
export interface ModMeasureInput {
|
|
31
|
+
sessionId?: string;
|
|
32
|
+
cwd?: string;
|
|
33
|
+
model?: string;
|
|
34
|
+
rateLimits?: Array<{
|
|
35
|
+
kind?: unknown;
|
|
36
|
+
percentUsed?: unknown;
|
|
37
|
+
resetsAt?: unknown;
|
|
38
|
+
}>;
|
|
39
|
+
cost?: {
|
|
40
|
+
usd?: unknown;
|
|
41
|
+
};
|
|
42
|
+
context?: {
|
|
43
|
+
tokens?: unknown;
|
|
44
|
+
window?: unknown;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The mod's windows in the status line's shape, `{ five_hour: { used_percentage,
|
|
49
|
+
* resets_at } }`, resets in epoch seconds: the one shape recordLimits reads.
|
|
50
|
+
* A gateway's `spend_limit` has no place in the ledger and is left out.
|
|
51
|
+
*/
|
|
52
|
+
export declare function statusLineRateLimits(list: unknown): Record<string, {
|
|
53
|
+
used_percentage: number;
|
|
54
|
+
resets_at?: number;
|
|
55
|
+
}> | undefined;
|
|
56
|
+
/** A Claude Code session's transcript: in its folder's project, else wherever a project holds the id (a session that moved folders). */
|
|
57
|
+
export declare function findTranscript(sessionId: string | undefined, cwd: string | undefined, home?: string): string | undefined;
|
|
58
|
+
/** One line: Claude Code's cost of a session and ours, at one moment; or a press of the mod's Compact button. */
|
|
59
|
+
export interface CostLine {
|
|
60
|
+
ts: number;
|
|
61
|
+
sid: string;
|
|
62
|
+
/** Claude Code's figure, USD. */
|
|
63
|
+
cc?: number;
|
|
64
|
+
/** context-doctor's, USD. */
|
|
65
|
+
cd?: number;
|
|
66
|
+
model?: string;
|
|
67
|
+
/** Requests with no list price, left out of `cd`. */
|
|
68
|
+
unpriced?: number;
|
|
69
|
+
/** The part of `cd` priced at its family's row. */
|
|
70
|
+
est?: number;
|
|
71
|
+
/** "compact": the mod's Compact first button was pressed (`tokens`: the context it compacted). */
|
|
72
|
+
ev?: string;
|
|
73
|
+
tokens?: number;
|
|
74
|
+
}
|
|
75
|
+
export declare function costLogPath(): string;
|
|
76
|
+
/**
|
|
77
|
+
* Append a session's two figures, unless neither moved since its last line
|
|
78
|
+
* (a second measure of the same turn, a parallel window of the same session).
|
|
79
|
+
* Never throws. True when a line was written.
|
|
80
|
+
*/
|
|
81
|
+
export declare function recordSessionCost(line: Omit<CostLine, "ev">, path?: string): boolean;
|
|
82
|
+
/** A press of one of the mod's buttons that changes nothing on disk (the Compact button), for the follow-through count. */
|
|
83
|
+
export declare function recordModEvent(sid: string, ev: string, tokens?: number, now?: number, path?: string): boolean;
|
|
84
|
+
/** Every line of the ledger since `since`. */
|
|
85
|
+
export declare function readCostLines(since?: number, path?: string): CostLine[];
|
|
86
|
+
/** A session's last pair of figures, and how far ours is from Claude Code's. */
|
|
87
|
+
export interface CostGap {
|
|
88
|
+
sid: string;
|
|
89
|
+
ts: number;
|
|
90
|
+
cc: number;
|
|
91
|
+
cd: number;
|
|
92
|
+
/** (cd - cc) / cc. */
|
|
93
|
+
gap: number;
|
|
94
|
+
model?: string;
|
|
95
|
+
unpriced?: number;
|
|
96
|
+
}
|
|
97
|
+
/** The target: within this of Claude Code's figure wherever every model is priced (ROADMAP K2). */
|
|
98
|
+
export declare const COST_GAP_TARGET = 0.02;
|
|
99
|
+
/** Each session's last pair, where both figures are there and Claude Code's is worth comparing. */
|
|
100
|
+
export declare function costGaps(lines: CostLine[]): CostGap[];
|
|
101
|
+
/**
|
|
102
|
+
* `doctor`'s line: how close our session price is to Claude Code's own cost,
|
|
103
|
+
* on the sessions the mod measured in the last `days`. Undefined when it
|
|
104
|
+
* measured none (no mod, or no session above $0.50 yet).
|
|
105
|
+
*/
|
|
106
|
+
export declare function costCheck(days?: number, now?: number, path?: string): {
|
|
107
|
+
label: string;
|
|
108
|
+
status: "ok" | "warn";
|
|
109
|
+
detail: string;
|
|
110
|
+
} | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* Keep what the mod measured and work out the band's inputs. Never throws;
|
|
113
|
+
* what it cannot read is left out of the answer.
|
|
114
|
+
*/
|
|
115
|
+
export declare function modMeasure(input: ModMeasureInput, now?: number, env?: NodeJS.ProcessEnv): Partial<BandData> & Pick<BandData, "at" | "coldMinTokens" | "thresholds">;
|
|
116
|
+
/**
|
|
117
|
+
* The CLI side: `mod measure` keeps the reading and prints the band's
|
|
118
|
+
* figures; `mod event` records a press (`{ sessionId, ev, tokens }`). Both
|
|
119
|
+
* print JSON and exit 0 whatever happens, as the status line does.
|
|
120
|
+
*/
|
|
121
|
+
export declare function runMod(sub: "measure" | "event"): Promise<void>;
|