lake-gate 0.1.0 → 0.2.1
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 +63 -21
- package/action.yml +1 -1
- package/package.json +42 -10
- package/src/action.js +47 -1
- package/src/cli.js +34 -6
- package/src/config-core.js +38 -0
- package/src/config.js +2 -44
- package/src/estimate.js +33 -45
- package/src/github.js +81 -16
- package/src/hook.js +13 -5
- package/src/index.js +1 -0
- package/src/init.js +23 -3
- package/src/mcp.js +313 -0
- package/src/meter-core.js +535 -0
- package/src/models.js +193 -0
- package/src/plan.js +2 -2
- package/src/policy.js +12 -4
- package/src/review.js +319 -0
- package/src/sha256.js +37 -0
- package/src/store.js +1 -1
- package/src/sync.js +8 -4
- package/src/ui-server.js +81 -0
- package/src/workspace.js +104 -0
- package/templates/lake-gate.yml +1 -1
- package/ui/app.css +536 -0
- package/ui/app.js +836 -0
- package/ui/btn/approve.svg +5 -0
- package/ui/btn/changes.svg +5 -0
- package/ui/btn/open.svg +5 -0
- package/ui/data.js +95 -0
- package/ui/demo.js +262 -0
- package/ui/index.html +55 -0
- package/ui/inter.woff2 +0 -0
- package/ui/logo-mark.svg +1 -0
- package/ui/md.js +82 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
// GENERATED by scripts/sync-meter.mjs from lake-meter (src/plan.js, src/pricing.js, src/prices.json).
|
|
2
|
+
// One estimator for every Lake tool: the same plan gets the same steps, complexity (1-5), model picks and prices
|
|
3
|
+
// in lake-gate and lake-meter. Do not edit by hand; run `node scripts/sync-meter.mjs <path-to-lake-meter>`.
|
|
4
|
+
// Browser-safe: no Node imports.
|
|
5
|
+
export const PRICES = {
|
|
6
|
+
"$comment": "Lake Meter price and model table. Edit this one file when list prices change. Prices are USD per 1M tokens, standard (non-batch, non-fast, global) API list prices.",
|
|
7
|
+
"verifiedAt": "2026-11-12",
|
|
8
|
+
"sources": {
|
|
9
|
+
"anthropic": "https://platform.claude.com/docs/en/about-claude/pricing",
|
|
10
|
+
"openai": "https://developers.openai.com/api/docs/pricing",
|
|
11
|
+
"claudeCodeAliases": "https://code.claude.com/docs/en/model-config"
|
|
12
|
+
},
|
|
13
|
+
"models": {
|
|
14
|
+
"claude-haiku-5-5": {
|
|
15
|
+
"provider": "anthropic",
|
|
16
|
+
"label": "Haiku 5.5",
|
|
17
|
+
"tier": "light",
|
|
18
|
+
"alias": "haiku",
|
|
19
|
+
"match": [
|
|
20
|
+
"haiku-5-5",
|
|
21
|
+
"haiku-5.5"
|
|
22
|
+
],
|
|
23
|
+
"input": 0.1,
|
|
24
|
+
"cacheWrite5m": 0.125,
|
|
25
|
+
"cacheWrite1h": 0.2,
|
|
26
|
+
"cacheRead": 0.01,
|
|
27
|
+
"output": 0.5,
|
|
28
|
+
"longContext": {
|
|
29
|
+
"overPromptTokens": 100000,
|
|
30
|
+
"input": 0.5,
|
|
31
|
+
"cacheWrite5m": 0.625,
|
|
32
|
+
"cacheWrite1h": 1,
|
|
33
|
+
"cacheRead": 0.05,
|
|
34
|
+
"output": 2.5
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"claude-haiku-4-5": {
|
|
38
|
+
"provider": "anthropic",
|
|
39
|
+
"label": "Haiku 4.5",
|
|
40
|
+
"tier": "light",
|
|
41
|
+
"match": [
|
|
42
|
+
"haiku-4-5",
|
|
43
|
+
"haiku-4.5"
|
|
44
|
+
],
|
|
45
|
+
"input": 1,
|
|
46
|
+
"cacheWrite5m": 1.25,
|
|
47
|
+
"cacheWrite1h": 2,
|
|
48
|
+
"cacheRead": 0.1,
|
|
49
|
+
"output": 5
|
|
50
|
+
},
|
|
51
|
+
"claude-sonnet-5-5": {
|
|
52
|
+
"provider": "anthropic",
|
|
53
|
+
"label": "Sonnet 5.5",
|
|
54
|
+
"tier": "standard",
|
|
55
|
+
"alias": "sonnet",
|
|
56
|
+
"match": [
|
|
57
|
+
"sonnet-5-5",
|
|
58
|
+
"sonnet-5.5"
|
|
59
|
+
],
|
|
60
|
+
"input": 2,
|
|
61
|
+
"cacheWrite5m": 2.5,
|
|
62
|
+
"cacheWrite1h": 4,
|
|
63
|
+
"cacheRead": 0.1,
|
|
64
|
+
"output": 10
|
|
65
|
+
},
|
|
66
|
+
"claude-sonnet-5": {
|
|
67
|
+
"provider": "anthropic",
|
|
68
|
+
"label": "Sonnet 5",
|
|
69
|
+
"tier": "standard",
|
|
70
|
+
"match": [
|
|
71
|
+
"sonnet-5"
|
|
72
|
+
],
|
|
73
|
+
"input": 2,
|
|
74
|
+
"cacheWrite5m": 2.5,
|
|
75
|
+
"cacheWrite1h": 4,
|
|
76
|
+
"cacheRead": 0.2,
|
|
77
|
+
"output": 10
|
|
78
|
+
},
|
|
79
|
+
"claude-sonnet-4-6": {
|
|
80
|
+
"provider": "anthropic",
|
|
81
|
+
"label": "Sonnet 4.6",
|
|
82
|
+
"tier": "standard",
|
|
83
|
+
"match": [
|
|
84
|
+
"sonnet-4-6",
|
|
85
|
+
"sonnet-4.6",
|
|
86
|
+
"sonnet-4-5",
|
|
87
|
+
"sonnet-4.5"
|
|
88
|
+
],
|
|
89
|
+
"input": 3,
|
|
90
|
+
"cacheWrite5m": 3.75,
|
|
91
|
+
"cacheWrite1h": 6,
|
|
92
|
+
"cacheRead": 0.3,
|
|
93
|
+
"output": 15
|
|
94
|
+
},
|
|
95
|
+
"claude-opus-5-5": {
|
|
96
|
+
"provider": "anthropic",
|
|
97
|
+
"label": "Opus 5.5",
|
|
98
|
+
"tier": "heavy",
|
|
99
|
+
"alias": "opus",
|
|
100
|
+
"match": [
|
|
101
|
+
"opus-5-5",
|
|
102
|
+
"opus-5.5"
|
|
103
|
+
],
|
|
104
|
+
"input": 4,
|
|
105
|
+
"cacheWrite5m": 5,
|
|
106
|
+
"cacheWrite1h": 8,
|
|
107
|
+
"cacheRead": 0.2,
|
|
108
|
+
"output": 20
|
|
109
|
+
},
|
|
110
|
+
"claude-opus-5": {
|
|
111
|
+
"provider": "anthropic",
|
|
112
|
+
"label": "Opus 5",
|
|
113
|
+
"tier": "heavy",
|
|
114
|
+
"match": [
|
|
115
|
+
"opus-5",
|
|
116
|
+
"opus-4-8",
|
|
117
|
+
"opus-4-7",
|
|
118
|
+
"opus-4-6",
|
|
119
|
+
"opus-4-5"
|
|
120
|
+
],
|
|
121
|
+
"input": 5,
|
|
122
|
+
"cacheWrite5m": 6.25,
|
|
123
|
+
"cacheWrite1h": 10,
|
|
124
|
+
"cacheRead": 0.5,
|
|
125
|
+
"output": 25
|
|
126
|
+
},
|
|
127
|
+
"claude-fable-5-2": {
|
|
128
|
+
"provider": "anthropic",
|
|
129
|
+
"label": "Fable 5.2",
|
|
130
|
+
"tier": "frontier",
|
|
131
|
+
"alias": "fable",
|
|
132
|
+
"match": [
|
|
133
|
+
"fable-5-2",
|
|
134
|
+
"fable-5.2",
|
|
135
|
+
"mythos-5-2"
|
|
136
|
+
],
|
|
137
|
+
"input": 8,
|
|
138
|
+
"cacheWrite5m": 10,
|
|
139
|
+
"cacheWrite1h": 16,
|
|
140
|
+
"cacheRead": 0.2,
|
|
141
|
+
"output": 40
|
|
142
|
+
},
|
|
143
|
+
"claude-fable-5-1": {
|
|
144
|
+
"provider": "anthropic",
|
|
145
|
+
"label": "Fable 5.1",
|
|
146
|
+
"tier": "frontier",
|
|
147
|
+
"match": [
|
|
148
|
+
"fable-5-1",
|
|
149
|
+
"fable-5.1",
|
|
150
|
+
"mythos-5-1"
|
|
151
|
+
],
|
|
152
|
+
"input": 10,
|
|
153
|
+
"cacheWrite5m": 12.5,
|
|
154
|
+
"cacheWrite1h": 20,
|
|
155
|
+
"cacheRead": 0.25,
|
|
156
|
+
"output": 50
|
|
157
|
+
},
|
|
158
|
+
"gpt-6-luna": {
|
|
159
|
+
"provider": "openai",
|
|
160
|
+
"label": "GPT-6 Luna",
|
|
161
|
+
"tier": "light",
|
|
162
|
+
"match": [
|
|
163
|
+
"gpt-6-luna"
|
|
164
|
+
],
|
|
165
|
+
"input": 0.1,
|
|
166
|
+
"cacheWrite5m": 0.125,
|
|
167
|
+
"cacheRead": 0.01,
|
|
168
|
+
"output": 0.5,
|
|
169
|
+
"longContext": {
|
|
170
|
+
"overPromptTokens": 272000,
|
|
171
|
+
"input": 0.2,
|
|
172
|
+
"cacheWrite5m": 0.25,
|
|
173
|
+
"cacheRead": 0.02,
|
|
174
|
+
"output": 0.75
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
"gpt-6.1-terra": {
|
|
178
|
+
"provider": "openai",
|
|
179
|
+
"label": "GPT-6.1 Terra",
|
|
180
|
+
"tier": "standard",
|
|
181
|
+
"match": [
|
|
182
|
+
"gpt-6.1-terra",
|
|
183
|
+
"gpt-6-1-terra"
|
|
184
|
+
],
|
|
185
|
+
"input": 1,
|
|
186
|
+
"cacheWrite5m": 1.25,
|
|
187
|
+
"cacheRead": 0.05,
|
|
188
|
+
"output": 5,
|
|
189
|
+
"longContext": {
|
|
190
|
+
"overPromptTokens": 272000,
|
|
191
|
+
"input": 2,
|
|
192
|
+
"cacheWrite5m": 2.5,
|
|
193
|
+
"cacheRead": 0.1,
|
|
194
|
+
"output": 7.5
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
"gpt-6.1-sol": {
|
|
198
|
+
"provider": "openai",
|
|
199
|
+
"label": "GPT-6.1 Sol",
|
|
200
|
+
"tier": "heavy",
|
|
201
|
+
"match": [
|
|
202
|
+
"gpt-6.1-sol",
|
|
203
|
+
"gpt-6-1-sol"
|
|
204
|
+
],
|
|
205
|
+
"input": 2,
|
|
206
|
+
"cacheWrite5m": 2.5,
|
|
207
|
+
"cacheRead": 0.1,
|
|
208
|
+
"output": 10,
|
|
209
|
+
"longContext": {
|
|
210
|
+
"overPromptTokens": 272000,
|
|
211
|
+
"input": 4,
|
|
212
|
+
"cacheWrite5m": 5,
|
|
213
|
+
"cacheRead": 0.2,
|
|
214
|
+
"output": 15
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
"gpt-6-astra": {
|
|
218
|
+
"provider": "openai",
|
|
219
|
+
"label": "GPT-6 Astra",
|
|
220
|
+
"tier": "frontier",
|
|
221
|
+
"match": [
|
|
222
|
+
"gpt-6-astra"
|
|
223
|
+
],
|
|
224
|
+
"input": 10,
|
|
225
|
+
"cacheWrite5m": 12.5,
|
|
226
|
+
"cacheRead": 1,
|
|
227
|
+
"output": 50,
|
|
228
|
+
"longContext": {
|
|
229
|
+
"overPromptTokens": 272000,
|
|
230
|
+
"input": 20,
|
|
231
|
+
"cacheWrite5m": 25,
|
|
232
|
+
"cacheRead": 2,
|
|
233
|
+
"output": 75
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
"gpt-5.3-codex": {
|
|
237
|
+
"provider": "openai",
|
|
238
|
+
"label": "GPT-5.3 Codex",
|
|
239
|
+
"tier": "standard",
|
|
240
|
+
"match": [
|
|
241
|
+
"gpt-5.3-codex",
|
|
242
|
+
"gpt-5-3-codex"
|
|
243
|
+
],
|
|
244
|
+
"input": 1.75,
|
|
245
|
+
"cacheRead": 0.175,
|
|
246
|
+
"output": 14
|
|
247
|
+
},
|
|
248
|
+
"gpt-5-mini": {
|
|
249
|
+
"provider": "openai",
|
|
250
|
+
"label": "GPT-5 mini",
|
|
251
|
+
"tier": "light",
|
|
252
|
+
"match": [
|
|
253
|
+
"gpt-5-mini"
|
|
254
|
+
],
|
|
255
|
+
"input": 0.25,
|
|
256
|
+
"cacheRead": 0.025,
|
|
257
|
+
"output": 2
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
"recommend": {
|
|
261
|
+
"anthropic": {
|
|
262
|
+
"light": "claude-haiku-5-5",
|
|
263
|
+
"standard": "claude-sonnet-5-5",
|
|
264
|
+
"heavy": "claude-opus-5-5",
|
|
265
|
+
"frontier": "claude-fable-5-2"
|
|
266
|
+
},
|
|
267
|
+
"openai": {
|
|
268
|
+
"light": "gpt-6-luna",
|
|
269
|
+
"standard": "gpt-6.1-terra",
|
|
270
|
+
"heavy": "gpt-6.1-sol",
|
|
271
|
+
"frontier": "gpt-6-astra"
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
"baseline": {
|
|
275
|
+
"anthropic": "claude-opus-5-5",
|
|
276
|
+
"openai": "gpt-6.1-sol"
|
|
277
|
+
},
|
|
278
|
+
"familyFallback": {
|
|
279
|
+
"haiku": "claude-haiku-5-5",
|
|
280
|
+
"sonnet": "claude-sonnet-5-5",
|
|
281
|
+
"opus": "claude-opus-5-5",
|
|
282
|
+
"fable": "claude-fable-5-2",
|
|
283
|
+
"mythos": "claude-fable-5-2"
|
|
284
|
+
},
|
|
285
|
+
"assumptions": {
|
|
286
|
+
"$comment": "Heuristic token budgets per plan step by complexity (1-5). 'input' counts all prompt tokens processed across the step's agent turns, including cache reads. Tune these from your own `lake-meter report` history.",
|
|
287
|
+
"stepTokens": {
|
|
288
|
+
"1": {
|
|
289
|
+
"input": [
|
|
290
|
+
30000,
|
|
291
|
+
120000
|
|
292
|
+
],
|
|
293
|
+
"output": [
|
|
294
|
+
1000,
|
|
295
|
+
4000
|
|
296
|
+
]
|
|
297
|
+
},
|
|
298
|
+
"2": {
|
|
299
|
+
"input": [
|
|
300
|
+
80000,
|
|
301
|
+
300000
|
|
302
|
+
],
|
|
303
|
+
"output": [
|
|
304
|
+
3000,
|
|
305
|
+
10000
|
|
306
|
+
]
|
|
307
|
+
},
|
|
308
|
+
"3": {
|
|
309
|
+
"input": [
|
|
310
|
+
250000,
|
|
311
|
+
900000
|
|
312
|
+
],
|
|
313
|
+
"output": [
|
|
314
|
+
8000,
|
|
315
|
+
25000
|
|
316
|
+
]
|
|
317
|
+
},
|
|
318
|
+
"4": {
|
|
319
|
+
"input": [
|
|
320
|
+
700000,
|
|
321
|
+
2500000
|
|
322
|
+
],
|
|
323
|
+
"output": [
|
|
324
|
+
20000,
|
|
325
|
+
60000
|
|
326
|
+
]
|
|
327
|
+
},
|
|
328
|
+
"5": {
|
|
329
|
+
"input": [
|
|
330
|
+
1500000,
|
|
331
|
+
6000000
|
|
332
|
+
],
|
|
333
|
+
"output": [
|
|
334
|
+
40000,
|
|
335
|
+
150000
|
|
336
|
+
]
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
"inputMix": {
|
|
340
|
+
"uncached": 0.05,
|
|
341
|
+
"cacheWrite": 0.1,
|
|
342
|
+
"cacheRead": 0.85
|
|
343
|
+
},
|
|
344
|
+
"avgPromptTokens": 60000,
|
|
345
|
+
"tierTokenMultiplier": {
|
|
346
|
+
"light": 1.3,
|
|
347
|
+
"standard": 1.1,
|
|
348
|
+
"heavy": 1,
|
|
349
|
+
"frontier": 1
|
|
350
|
+
},
|
|
351
|
+
"complexityToTier": {
|
|
352
|
+
"1": "light",
|
|
353
|
+
"2": "light",
|
|
354
|
+
"3": "standard",
|
|
355
|
+
"4": "heavy",
|
|
356
|
+
"5": "heavy"
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
const SKIP_HEADINGS = /^(context|background|overview|summary|goal|goals|notes?|open questions?|assumptions|out of scope|non-goals|references|tl;?dr|why|problem|motivation)\b/i;
|
|
363
|
+
|
|
364
|
+
/** Split markdown into steps. Prefers step-like headings, then top-level numbered items, then bullets. */
|
|
365
|
+
export function splitPlan(markdown) {
|
|
366
|
+
const text = String(markdown || '').replace(/\r\n/g, '\n');
|
|
367
|
+
const lines = text.split('\n');
|
|
368
|
+
|
|
369
|
+
// 1) Headings (##..####) that are not the document title and not context sections.
|
|
370
|
+
const heads = [];
|
|
371
|
+
lines.forEach((l, i) => { const m = /^(#{2,4})\s+(.*)$/.exec(l); if (m) heads.push({ i, level: m[1].length, title: m[2].trim() }); });
|
|
372
|
+
const CONTAINER = /^(steps?|plan|implementation( plan)?|tasks?|changes|approach|proposed changes|work items?)\b/i;
|
|
373
|
+
const stepLike = (h) => !SKIP_HEADINGS.test(h.title.replace(/^(step|phase|part)?\s*\d+[.:)]?\s*/i, '')) && !CONTAINER.test(h.title);
|
|
374
|
+
const byLevel = {};
|
|
375
|
+
for (const h of heads) if (stepLike(h)) (byLevel[h.level] ||= []).push(h);
|
|
376
|
+
const levels = Object.keys(byLevel).map(Number).sort((a, b) => byLevel[b].length - byLevel[a].length || b - a);
|
|
377
|
+
if (levels.length && byLevel[levels[0]].length >= 2) {
|
|
378
|
+
const level = levels[0];
|
|
379
|
+
return byLevel[level].map((h) => {
|
|
380
|
+
const next = heads.find((x) => x.i > h.i && x.level <= level);
|
|
381
|
+
const body = lines.slice(h.i + 1, next ? next.i : lines.length).join('\n').trim();
|
|
382
|
+
return { title: clean(h.title), body };
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// 2) Top-level numbered list items (also "- [ ]" checkboxes), with indented continuation lines.
|
|
387
|
+
for (const re of [/^(\d+)[.)]\s+(.*)$/, /^[-*]\s+\[[ xX]\]\s+(.*)$/, /^[-*]\s+(.*)$/]) {
|
|
388
|
+
const items = [];
|
|
389
|
+
let cur = null;
|
|
390
|
+
for (const l of lines) {
|
|
391
|
+
const m = re.exec(l);
|
|
392
|
+
if (m) { cur = { title: clean(m[m.length - 1]), body: '' }; items.push(cur); continue; }
|
|
393
|
+
if (cur && (/^\s+\S/.test(l) || l.trim() === '')) { cur.body += l.trim() ? l.trim() + '\n' : ''; continue; }
|
|
394
|
+
if (cur && /^#{1,6}\s/.test(l)) cur = null;
|
|
395
|
+
else if (cur && l.trim()) cur = null;
|
|
396
|
+
}
|
|
397
|
+
if (items.length >= 2) return items.map((s) => ({ title: s.title, body: s.body.trim() }));
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// 3) Fallback: paragraphs.
|
|
401
|
+
const paras = text.split(/\n\s*\n/).map((p) => p.trim()).filter((p) => p && !/^#\s/.test(p));
|
|
402
|
+
return paras.slice(0, 30).map((p) => ({ title: clean(p.split('\n')[0]).slice(0, 90), body: p }));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function clean(s) {
|
|
406
|
+
return s.replace(/^(step|phase|part)\s*\d+\s*[.:)\-–—]\s*/i, '').replace(/^\d+[.)]\s*/, '').replace(/\*\*|__|`/g, '').trim();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const SIGNALS = [
|
|
410
|
+
// [regex, weight, reason]
|
|
411
|
+
[/\b(architect\w*|redesign|re-?architect|system design)\b/i, 2, 'architecture'],
|
|
412
|
+
[/\b(migrat\w+|schema change|data model|backfill)\b/i, 2, 'data/schema migration'],
|
|
413
|
+
[/\b(concurren\w+|race condition|deadlock|lock(ing)?|distributed|consisten\w+|idempoten\w+|transaction\w*)\b/i, 2, 'concurrency/consistency'],
|
|
414
|
+
[/\b(security|auth\w*|permission\w*|encrypt\w*|secret\w*|oauth|csrf|xss|injection)\b/i, 2, 'security-sensitive'],
|
|
415
|
+
[/\b(payment\w*|billing|invoice\w*|money)\b/i, 1, 'money paths'],
|
|
416
|
+
[/\b(performance|optimi[sz]\w+|latency|memory leak|profil\w+)\b/i, 1, 'performance work'],
|
|
417
|
+
[/\b(refactor\w*|restructur\w+|rewrite)\b/i, 1, 'refactor'],
|
|
418
|
+
[/\b(algorithm\w*|parser|compiler|protocol|state machine)\b/i, 2, 'algorithmic'],
|
|
419
|
+
[/\b(debug\w*|investigat\w+|root cause|flaky|intermittent)\b/i, 1, 'debugging/unknowns'],
|
|
420
|
+
[/\b(across|cross-cutting|every (file|module|service)|monorepo|multiple services)\b/i, 1, 'cross-cutting'],
|
|
421
|
+
[/\b(implement\w*|integrat\w+|endpoint|api|component|service|hook|middleware|query|cache|websocket|queue)\b/i, 1, 'new behavior'],
|
|
422
|
+
[/\b(rename|typo|readme|docs?|documentation|comment\w*|changelog|format\w*|lint\w*|prettier|bump|version|copy|wording|text|label)\b/i, -1, 'mechanical/docs'],
|
|
423
|
+
[/\b(config\w*|env(ironment)? var\w*|flag|constant|stub|scaffold\w*|boilerplate|placeholder)\b/i, -1, 'config/boilerplate'],
|
|
424
|
+
[/\b(delete|remove unused|cleanup|clean up|dead code)\b/i, -1, 'cleanup'],
|
|
425
|
+
];
|
|
426
|
+
|
|
427
|
+
/** Score one step. Returns { complexity 1-5, reasons[] }. */
|
|
428
|
+
export function scoreStep(step) {
|
|
429
|
+
const text = `${step.title}\n${step.body || ''}`;
|
|
430
|
+
let score = 0;
|
|
431
|
+
const reasons = [];
|
|
432
|
+
for (const [re, w, why] of SIGNALS) if (re.test(text)) { score += w; reasons.push(`${w > 0 ? '+' : ''}${w} ${why}`); }
|
|
433
|
+
const files = new Set((text.match(/[\w@./-]+\.(ts|tsx|js|jsx|mjs|py|go|rs|java|kt|rb|php|cs|swift|sql|vue|svelte|css|scss|json|yaml|yml|md)\b/g) || []));
|
|
434
|
+
if (files.size >= 6) { score += 2; reasons.push(`+2 touches ${files.size} files`); }
|
|
435
|
+
else if (files.size >= 3) { score += 1; reasons.push(`+1 touches ${files.size} files`); }
|
|
436
|
+
if (text.length > 900) { score += 1; reasons.push('+1 long step description'); }
|
|
437
|
+
const complexity = Math.max(1, Math.min(5, Math.round(2 + score * 0.75)));
|
|
438
|
+
if (!reasons.length) reasons.push('no strong signals (default 2)');
|
|
439
|
+
return { complexity, reasons };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Full breakdown. options: { provider: 'anthropic'|'openai', baseline?: modelKey, allowFrontier?: boolean }
|
|
444
|
+
*/
|
|
445
|
+
export function analyzePlan(markdown, prices, options = {}) {
|
|
446
|
+
const provider = options.provider || 'anthropic';
|
|
447
|
+
const rec = prices.recommend[provider];
|
|
448
|
+
const baseline = options.baseline || prices.baseline[provider];
|
|
449
|
+
const a = prices.assumptions;
|
|
450
|
+
const steps = splitPlan(markdown).map((s, idx) => {
|
|
451
|
+
const { complexity, reasons } = scoreStep(s);
|
|
452
|
+
let tier = a.complexityToTier[String(complexity)];
|
|
453
|
+
if (complexity === 5 && options.allowFrontier) tier = 'frontier';
|
|
454
|
+
const model = rec[tier];
|
|
455
|
+
const budget = a.stepTokens[String(complexity)];
|
|
456
|
+
const mult = a.tierTokenMultiplier[tier] ?? 1;
|
|
457
|
+
const tokLo = Math.round((budget.input[0] + budget.output[0]) * mult);
|
|
458
|
+
const tokHi = Math.round((budget.input[1] + budget.output[1]) * mult);
|
|
459
|
+
const usdLo = costOfBudget(model, budget.input[0] * mult, budget.output[0] * mult, prices);
|
|
460
|
+
const usdHi = costOfBudget(model, budget.input[1] * mult, budget.output[1] * mult, prices);
|
|
461
|
+
const baseLo = costOfBudget(baseline, budget.input[0], budget.output[0], prices);
|
|
462
|
+
const baseHi = costOfBudget(baseline, budget.input[1], budget.output[1], prices);
|
|
463
|
+
return { n: idx + 1, title: s.title, complexity, reasons, tier, model, modelLabel: prices.models[model].label, alias: prices.models[model].alias || model, tokens: [tokLo, tokHi], usd: [usdLo, usdHi], baselineUsd: [baseLo, baseHi] };
|
|
464
|
+
});
|
|
465
|
+
const sum = (k, i) => steps.reduce((t, s) => t + s[k][i], 0);
|
|
466
|
+
const total = { tokens: [sum('tokens', 0), sum('tokens', 1)], usd: [sum('usd', 0), sum('usd', 1)], baselineUsd: [sum('baselineUsd', 0), sum('baselineUsd', 1)] };
|
|
467
|
+
const mid = (r) => (r[0] + r[1]) / 2;
|
|
468
|
+
total.savingsPct = mid(total.baselineUsd) > 0 ? Math.round((1 - mid(total.usd) / mid(total.baselineUsd)) * 100) : 0;
|
|
469
|
+
return { provider, baseline, baselineLabel: prices.models[baseline].label, steps, total, pricesVerifiedAt: prices.verifiedAt, estimate: true };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
export function normalizeModelId(id) {
|
|
473
|
+
return String(id || '').toLowerCase().replace(/\[.*?\]/g, '').replace(/^.*\//, '').replace(/\./g, '-').trim();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** Resolve a raw model id (e.g. "claude-opus-5-5-20261001", "opus", "us.anthropic.claude-sonnet-5-5") to a table key. */
|
|
477
|
+
export function resolveModel(rawId, prices) {
|
|
478
|
+
if (!rawId) return null;
|
|
479
|
+
const id = normalizeModelId(rawId);
|
|
480
|
+
if (prices.models[rawId]) return rawId;
|
|
481
|
+
let best = null, bestLen = 0;
|
|
482
|
+
for (const [key, m] of Object.entries(prices.models)) {
|
|
483
|
+
for (const token of [key, ...(m.match || [])]) {
|
|
484
|
+
const t = normalizeModelId(token);
|
|
485
|
+
if (id.includes(t) && t.length > bestLen) { best = key; bestLen = t.length; }
|
|
486
|
+
}
|
|
487
|
+
if (m.alias && id === m.alias) return key;
|
|
488
|
+
}
|
|
489
|
+
if (best) return best;
|
|
490
|
+
for (const [fam, key] of Object.entries(prices.familyFallback || {})) if (id.includes(fam)) return key;
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function rate(m, field, promptTokens) {
|
|
495
|
+
const lc = m.longContext;
|
|
496
|
+
const src = lc && promptTokens > lc.overPromptTokens ? lc : m;
|
|
497
|
+
const v = src[field] ?? m[field];
|
|
498
|
+
if (v != null) return v;
|
|
499
|
+
if (field === 'cacheWrite1h') return (src.input ?? m.input) * 2;
|
|
500
|
+
if (field === 'cacheWrite5m') return (src.input ?? m.input) * 1.25;
|
|
501
|
+
if (field === 'cacheRead') return (src.input ?? m.input) * 0.1;
|
|
502
|
+
return 0;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Cost of one API response's usage object (Anthropic usage shape, as written to Claude Code transcripts).
|
|
507
|
+
* Long-context pricing is decided per request from its own prompt size.
|
|
508
|
+
*/
|
|
509
|
+
export function costOfUsage(modelKey, usage, prices) {
|
|
510
|
+
const m = prices.models[modelKey];
|
|
511
|
+
if (!m || !usage) return null;
|
|
512
|
+
const inp = usage.input_tokens || 0;
|
|
513
|
+
const cr = usage.cache_read_input_tokens || 0;
|
|
514
|
+
const cwTotal = usage.cache_creation_input_tokens || 0;
|
|
515
|
+
const cw1h = usage.cache_creation?.ephemeral_1h_input_tokens || 0;
|
|
516
|
+
const cw5m = usage.cache_creation?.ephemeral_5m_input_tokens ?? Math.max(0, cwTotal - cw1h);
|
|
517
|
+
const out = usage.output_tokens || 0;
|
|
518
|
+
const prompt = inp + cr + cwTotal;
|
|
519
|
+
return (inp * rate(m, 'input', prompt) + cr * rate(m, 'cacheRead', prompt) + cw5m * rate(m, 'cacheWrite5m', prompt)
|
|
520
|
+
+ cw1h * rate(m, 'cacheWrite1h', prompt) + out * rate(m, 'output', prompt)) / 1e6;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** Estimated cost of an aggregate token budget on a model, using the configured input mix. */
|
|
524
|
+
export function costOfBudget(modelKey, inputTokens, outputTokens, prices) {
|
|
525
|
+
const m = prices.models[modelKey];
|
|
526
|
+
const a = prices.assumptions;
|
|
527
|
+
const mix = a.inputMix;
|
|
528
|
+
const p = a.avgPromptTokens;
|
|
529
|
+
return (inputTokens * (mix.uncached * rate(m, 'input', p) + mix.cacheWrite * rate(m, 'cacheWrite5m', p) + mix.cacheRead * rate(m, 'cacheRead', p))
|
|
530
|
+
+ outputTokens * rate(m, 'output', p)) / 1e6;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export function providerOf(modelKey, prices) {
|
|
534
|
+
return prices.models[modelKey]?.provider || 'anthropic';
|
|
535
|
+
}
|