@trazum/core 3.0.1 → 3.0.2
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 +1 -1
- package/dist/claude-model-pricing.d.ts +73 -0
- package/dist/claude-model-pricing.d.ts.map +1 -0
- package/dist/claude-model-pricing.js +208 -0
- package/dist/claude-model-pricing.js.map +1 -0
- package/dist/config-schema.d.ts +14 -1
- package/dist/config-schema.d.ts.map +1 -1
- package/dist/config-schema.js +24 -0
- package/dist/config-schema.js.map +1 -1
- package/dist/index.d.ts +13 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1 -1
- package/dist/node.js.map +1 -1
- package/dist/otlp.d.ts +103 -0
- package/dist/otlp.d.ts.map +1 -1
- package/dist/otlp.js +185 -0
- package/dist/otlp.js.map +1 -1
- package/dist/partner-model-id.d.ts +26 -0
- package/dist/partner-model-id.d.ts.map +1 -0
- package/dist/partner-model-id.js +60 -0
- package/dist/partner-model-id.js.map +1 -0
- package/dist/plan-edit.d.ts +99 -0
- package/dist/plan-edit.d.ts.map +1 -0
- package/dist/plan-edit.js +142 -0
- package/dist/plan-edit.js.map +1 -0
- package/dist/plan.d.ts +63 -2
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +55 -0
- package/dist/plan.js.map +1 -1
- package/dist/pricing.d.ts +19 -0
- package/dist/pricing.d.ts.map +1 -1
- package/dist/pricing.js +32 -0
- package/dist/pricing.js.map +1 -1
- package/dist/prometheus.d.ts +42 -0
- package/dist/prometheus.d.ts.map +1 -0
- package/dist/prometheus.js +112 -0
- package/dist/prometheus.js.map +1 -0
- package/dist/receipt.d.ts +2 -1
- package/dist/receipt.d.ts.map +1 -1
- package/dist/receipt.js +11 -1
- package/dist/receipt.js.map +1 -1
- package/dist/session-waste.d.ts +145 -0
- package/dist/session-waste.d.ts.map +1 -0
- package/dist/session-waste.js +489 -0
- package/dist/session-waste.js.map +1 -0
- package/dist/structure.js +3 -3
- package/dist/structure.js.map +1 -1
- package/dist/tokenizer.d.ts +9 -0
- package/dist/tokenizer.d.ts.map +1 -1
- package/dist/tokenizer.js +1 -1
- package/dist/tokenizer.js.map +1 -1
- package/dist/verify.d.ts +11 -1
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +54 -0
- package/dist/verify.js.map +1 -1
- package/dist/walk.d.ts +14 -0
- package/dist/walk.d.ts.map +1 -1
- package/dist/walk.js +35 -0
- package/dist/walk.js.map +1 -1
- package/package.json +1 -1
- package/src/claude-model-pricing.ts +245 -0
- package/src/config-schema.ts +44 -0
- package/src/index.ts +46 -4
- package/src/node.ts +1 -1
- package/src/otlp.ts +281 -0
- package/src/partner-model-id.ts +69 -0
- package/src/plan-edit.ts +180 -0
- package/src/plan.ts +104 -3
- package/src/pricing.ts +33 -0
- package/src/prometheus.ts +131 -0
- package/src/receipt.ts +16 -2
- package/src/session-waste.ts +570 -0
- package/src/structure.ts +3 -3
- package/src/tokenizer.ts +1 -1
- package/src/verify.ts +78 -2
- package/src/walk.ts +33 -0
package/src/otlp.ts
CHANGED
|
@@ -20,8 +20,16 @@
|
|
|
20
20
|
* loses the last digits of every timestamp.
|
|
21
21
|
* - **A gauge is `asDouble` or `asInt`, never both**, and a money figure is a
|
|
22
22
|
* double. Sending `asInt` for `$41.20` silently reports `41`.
|
|
23
|
+
*
|
|
24
|
+
* The bill (`toBillOtlpMetrics`) follows the same two rules and adds three of
|
|
25
|
+
* its own, which the functions below explain where they apply: a day still
|
|
26
|
+
* being measured is never written as a closed one, every cost point says it is
|
|
27
|
+
* API-equivalent cost, and what could not be priced is its own metric.
|
|
23
28
|
*/
|
|
24
29
|
|
|
30
|
+
import type { PricingCatalogue } from './pricing.js';
|
|
31
|
+
import { UNLABELLED, parseUsageLine, profileUsage } from './usage.js';
|
|
32
|
+
|
|
25
33
|
/** Attributes as OTLP encodes them: a list of key/value, not an object. */
|
|
26
34
|
export interface OtlpAttribute {
|
|
27
35
|
key: string;
|
|
@@ -177,3 +185,276 @@ export function toOtlpMetrics(input: OtlpInput, atMillis: number): OtlpPayload {
|
|
|
177
185
|
],
|
|
178
186
|
};
|
|
179
187
|
}
|
|
188
|
+
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
// The bill
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
|
|
193
|
+
const DAY_MS = 86_400_000;
|
|
194
|
+
const UNDATED = 'undated';
|
|
195
|
+
|
|
196
|
+
/** What every cost point says about itself, and the attribute that says it. */
|
|
197
|
+
export const OTLP_COST_BASIS_KEY = 'trazum.cost.basis';
|
|
198
|
+
export const OTLP_COST_BASIS = 'api-equivalent';
|
|
199
|
+
|
|
200
|
+
/** The four token kinds a call is billed in, as the `trazum.token.type` values. */
|
|
201
|
+
export const OTLP_TOKEN_TYPES = ['input', 'cache_read', 'cache_write', 'output'] as const;
|
|
202
|
+
export type OtlpTokenType = (typeof OTLP_TOKEN_TYPES)[number];
|
|
203
|
+
|
|
204
|
+
/** One day of one project's calls to one model, through one agent. */
|
|
205
|
+
export interface BillOtlpRow {
|
|
206
|
+
/** `YYYY-MM-DD`, UTC. */
|
|
207
|
+
day: string;
|
|
208
|
+
/** The label the log carried, or `null` when it carried none. */
|
|
209
|
+
project: string | null;
|
|
210
|
+
model: string;
|
|
211
|
+
/** Which coding agent's sessions the calls came from, or `null` for any other source. */
|
|
212
|
+
agent: string | null;
|
|
213
|
+
calls: number;
|
|
214
|
+
inputTokens: number;
|
|
215
|
+
cacheReadTokens: number;
|
|
216
|
+
cacheWriteTokens: number;
|
|
217
|
+
outputTokens: number;
|
|
218
|
+
/** Absent on an unpriced row: there is no figure to write, and a zero would be one. */
|
|
219
|
+
usd?: number;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** What the payload does not hold as a closed day, so the series and these add to the bill. */
|
|
223
|
+
export interface BillOtlpExcluded {
|
|
224
|
+
calls: number;
|
|
225
|
+
usd: number;
|
|
226
|
+
tokens: number;
|
|
227
|
+
unpricedCalls: number;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export interface BillOtlpInput {
|
|
231
|
+
/** Priced rows of closed days only. */
|
|
232
|
+
priced: readonly BillOtlpRow[];
|
|
233
|
+
/** Rows of closed days the catalogue could not price. */
|
|
234
|
+
unpriced: readonly BillOtlpRow[];
|
|
235
|
+
/** Days still open, or cut by the window: left out of the series, counted here. */
|
|
236
|
+
open: BillOtlpExcluded & { days: readonly string[] };
|
|
237
|
+
/** Calls with no clock: they belong to no day, so to none of the series. */
|
|
238
|
+
undated: BillOtlpExcluded;
|
|
239
|
+
/** The instant "closed" was judged against, epoch milliseconds. */
|
|
240
|
+
measuredAtMs: number;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export interface BillOtlpOptions {
|
|
244
|
+
catalogue: PricingCatalogue;
|
|
245
|
+
/** The window the bill was asked for, as `profileUsage` takes it. */
|
|
246
|
+
sinceMs?: number;
|
|
247
|
+
untilMs?: number;
|
|
248
|
+
/** Now. A parameter, for the reason `toOtlpMetrics` takes its timestamp. */
|
|
249
|
+
nowMs: number;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Whether the bill holds all of a UTC day.
|
|
254
|
+
*
|
|
255
|
+
* Closed means its last millisecond has passed, and the window neither starts
|
|
256
|
+
* after its first nor ends before its last. A day the window cuts is open in
|
|
257
|
+
* the sense that matters to a chart: its figure is a part of a day, and a
|
|
258
|
+
* series that mixes parts with wholes reads as a day that was cheap.
|
|
259
|
+
*/
|
|
260
|
+
export function dayIsClosed(day: string, nowMs: number, sinceMs?: number, untilMs?: number): boolean {
|
|
261
|
+
const start = Date.parse(`${day}T00:00:00Z`);
|
|
262
|
+
const end = start + DAY_MS;
|
|
263
|
+
if (!Number.isFinite(start) || end > nowMs) return false;
|
|
264
|
+
if (sinceMs !== undefined && sinceMs > start) return false;
|
|
265
|
+
if (untilMs !== undefined && untilMs < end) return false;
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Groups a bill's records by day, project, model and agent, and prices each
|
|
271
|
+
* group through `profileUsage`, the function the bill itself is priced by, so
|
|
272
|
+
* a figure here is the bill's figure rather than a second arithmetic.
|
|
273
|
+
*
|
|
274
|
+
* A record outside the window, or with no clock under a window, is left out
|
|
275
|
+
* exactly as `profileUsage` leaves it out, which is what keeps the totals equal.
|
|
276
|
+
*/
|
|
277
|
+
export function billOtlpInput(
|
|
278
|
+
sources: readonly { agent: string | null; lines: readonly string[] }[],
|
|
279
|
+
options: BillOtlpOptions,
|
|
280
|
+
): BillOtlpInput {
|
|
281
|
+
const { catalogue, sinceMs, untilMs, nowMs } = options;
|
|
282
|
+
const windowed = sinceMs !== undefined || untilMs !== undefined;
|
|
283
|
+
|
|
284
|
+
const groups = new Map<
|
|
285
|
+
string,
|
|
286
|
+
{ day: string; project: string | null; model: string; agent: string | null; lines: string[] }
|
|
287
|
+
>();
|
|
288
|
+
for (const source of sources) {
|
|
289
|
+
for (const line of source.lines) {
|
|
290
|
+
const record = parseUsageLine(line);
|
|
291
|
+
if (record === null) continue;
|
|
292
|
+
const outside =
|
|
293
|
+
record.ts === null
|
|
294
|
+
? windowed
|
|
295
|
+
: (sinceMs !== undefined && record.ts < sinceMs) || (untilMs !== undefined && record.ts >= untilMs);
|
|
296
|
+
if (outside) continue;
|
|
297
|
+
const day = record.ts === null ? UNDATED : new Date(record.ts).toISOString().slice(0, 10);
|
|
298
|
+
const project = record.label === null || record.label === UNLABELLED ? null : record.label;
|
|
299
|
+
const key = JSON.stringify([day, project, record.model, source.agent]);
|
|
300
|
+
const group = groups.get(key) ?? { day, project, model: record.model, agent: source.agent, lines: [] };
|
|
301
|
+
group.lines.push(line);
|
|
302
|
+
groups.set(key, group);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const priced: BillOtlpRow[] = [];
|
|
307
|
+
const unpriced: BillOtlpRow[] = [];
|
|
308
|
+
const open = { days: new Set<string>(), calls: 0, usd: 0, tokens: 0, unpricedCalls: 0 };
|
|
309
|
+
const undated = { calls: 0, usd: 0, tokens: 0, unpricedCalls: 0 };
|
|
310
|
+
|
|
311
|
+
const ordered = [...groups.entries()].sort(([a], [b]) => a.localeCompare(b));
|
|
312
|
+
for (const [, group] of ordered) {
|
|
313
|
+
const report = profileUsage(group.lines.join('\n'), { catalogue });
|
|
314
|
+
const closed = group.day !== UNDATED && dayIsClosed(group.day, nowMs, sinceMs, untilMs);
|
|
315
|
+
for (const [part, isPriced] of [
|
|
316
|
+
[report.total, true],
|
|
317
|
+
[report.unpriced, false],
|
|
318
|
+
] as const) {
|
|
319
|
+
if (part.calls === 0) continue;
|
|
320
|
+
if (closed) {
|
|
321
|
+
(isPriced ? priced : unpriced).push({
|
|
322
|
+
day: group.day,
|
|
323
|
+
project: group.project,
|
|
324
|
+
model: group.model,
|
|
325
|
+
agent: group.agent,
|
|
326
|
+
calls: part.calls,
|
|
327
|
+
inputTokens: part.inputTokens,
|
|
328
|
+
cacheReadTokens: part.cacheReadTokens,
|
|
329
|
+
cacheWriteTokens: part.cacheWriteTokens,
|
|
330
|
+
outputTokens: part.outputTokens,
|
|
331
|
+
...(isPriced ? { usd: part.totalUsd } : {}),
|
|
332
|
+
});
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const into = group.day === UNDATED ? undated : open;
|
|
336
|
+
if (group.day !== UNDATED) open.days.add(group.day);
|
|
337
|
+
if (isPriced) {
|
|
338
|
+
into.calls += part.calls;
|
|
339
|
+
into.usd += part.totalUsd;
|
|
340
|
+
into.tokens += part.inputTokens + part.cacheReadTokens + part.cacheWriteTokens + part.outputTokens;
|
|
341
|
+
} else {
|
|
342
|
+
into.unpricedCalls += part.calls;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return { priced, unpriced, open: { ...open, days: [...open.days].sort() }, undated, measuredAtMs: nowMs };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const rowAttributes = (row: BillOtlpRow): OtlpAttribute[] => [
|
|
350
|
+
str('trazum.day', row.day),
|
|
351
|
+
...(row.project !== null ? [str('trazum.project', row.project)] : []),
|
|
352
|
+
str('trazum.model', row.model),
|
|
353
|
+
...(row.agent !== null ? [str('trazum.agent', row.agent)] : []),
|
|
354
|
+
];
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The bill as OTLP gauges, one point per day, project, model and agent.
|
|
358
|
+
*
|
|
359
|
+
* **Each point is that day's total, not a running one**, stamped at the day's
|
|
360
|
+
* first instant and carrying `trazum.day`, so a collector that rewrites
|
|
361
|
+
* timestamps cannot move a figure to another day.
|
|
362
|
+
*
|
|
363
|
+
* - `trazum.bill.cost` (USD, `asDouble`), every point carrying
|
|
364
|
+
* `trazum.cost.basis=api-equivalent`: the tokens at the provider's list
|
|
365
|
+
* rates, which on a subscription is not what was paid.
|
|
366
|
+
* - `trazum.bill.tokens` (`asInt`), by `trazum.token.type`.
|
|
367
|
+
* - `trazum.bill.calls` (`asInt`).
|
|
368
|
+
* - `trazum.bill.unpriced.calls` and `trazum.bill.unpriced.tokens`: what the
|
|
369
|
+
* catalogue does not price, with no dollar figure beside it.
|
|
370
|
+
*
|
|
371
|
+
* **A day still being measured is not a point.** Today, and any day the
|
|
372
|
+
* requested window cuts, are left out of every metric and named in the
|
|
373
|
+
* resource attributes (`trazum.bill.open_days` and the figures under
|
|
374
|
+
* `trazum.bill.open_*`), as are calls with no clock (`trazum.bill.undated_*`).
|
|
375
|
+
* The series plus those attributes add to the bill, and a chart never draws a
|
|
376
|
+
* partial day beside whole ones. The attributes are present at zero too:
|
|
377
|
+
* nothing excluded and nothing recorded are different statements.
|
|
378
|
+
*/
|
|
379
|
+
export function toBillOtlpMetrics(input: BillOtlpInput): OtlpPayload {
|
|
380
|
+
const metrics: OtlpMetric[] = [];
|
|
381
|
+
const point = (
|
|
382
|
+
row: BillOtlpRow,
|
|
383
|
+
extra: OtlpAttribute[],
|
|
384
|
+
value: Pick<OtlpDataPoint, 'asInt' | 'asDouble'>,
|
|
385
|
+
): OtlpDataPoint => ({
|
|
386
|
+
attributes: [...rowAttributes(row), ...extra],
|
|
387
|
+
timeUnixNano: nanos(Date.parse(`${row.day}T00:00:00Z`)),
|
|
388
|
+
...value,
|
|
389
|
+
});
|
|
390
|
+
const gauge = (name: string, description: string, unit: string, dataPoints: OtlpDataPoint[]): void => {
|
|
391
|
+
if (dataPoints.length > 0) metrics.push({ name, description, unit, gauge: { dataPoints } });
|
|
392
|
+
};
|
|
393
|
+
const tokenPoints = (rows: readonly BillOtlpRow[]): OtlpDataPoint[] =>
|
|
394
|
+
rows.flatMap((row) =>
|
|
395
|
+
(
|
|
396
|
+
[
|
|
397
|
+
['input', row.inputTokens],
|
|
398
|
+
['cache_read', row.cacheReadTokens],
|
|
399
|
+
['cache_write', row.cacheWriteTokens],
|
|
400
|
+
['output', row.outputTokens],
|
|
401
|
+
] as [OtlpTokenType, number][]
|
|
402
|
+
).map(([type, count]) => point(row, [str('trazum.token.type', type)], { asInt: `${Math.round(count)}` })),
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
gauge(
|
|
406
|
+
'trazum.bill.cost',
|
|
407
|
+
'API-equivalent cost of one closed UTC day: the tokens at the provider list rates, not what a subscription paid.',
|
|
408
|
+
'USD',
|
|
409
|
+
input.priced.map((row) => point(row, [str(OTLP_COST_BASIS_KEY, OTLP_COST_BASIS)], { asDouble: row.usd ?? 0 })),
|
|
410
|
+
);
|
|
411
|
+
gauge(
|
|
412
|
+
'trazum.bill.tokens',
|
|
413
|
+
'Tokens of one closed UTC day, by kind: input, cache_read, cache_write and output.',
|
|
414
|
+
'{token}',
|
|
415
|
+
tokenPoints(input.priced),
|
|
416
|
+
);
|
|
417
|
+
gauge(
|
|
418
|
+
'trazum.bill.calls',
|
|
419
|
+
'Priced calls of one closed UTC day.',
|
|
420
|
+
'{call}',
|
|
421
|
+
input.priced.map((row) => point(row, [], { asInt: `${row.calls}` })),
|
|
422
|
+
);
|
|
423
|
+
gauge(
|
|
424
|
+
'trazum.bill.unpriced.calls',
|
|
425
|
+
'Calls of one closed UTC day to a model the catalogue does not price. They are in no cost figure.',
|
|
426
|
+
'{call}',
|
|
427
|
+
input.unpriced.map((row) => point(row, [], { asInt: `${row.calls}` })),
|
|
428
|
+
);
|
|
429
|
+
gauge(
|
|
430
|
+
'trazum.bill.unpriced.tokens',
|
|
431
|
+
'Tokens of one closed UTC day on a model the catalogue does not price, by kind. They are in no cost figure.',
|
|
432
|
+
'{token}',
|
|
433
|
+
tokenPoints(input.unpriced),
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
const count = (key: string, value: number): OtlpAttribute => ({ key, value: { intValue: `${Math.round(value)}` } });
|
|
437
|
+
const money = (key: string, value: number): OtlpAttribute => ({ key, value: { doubleValue: value } });
|
|
438
|
+
return {
|
|
439
|
+
resourceMetrics: [
|
|
440
|
+
{
|
|
441
|
+
resource: {
|
|
442
|
+
attributes: [
|
|
443
|
+
str('service.name', 'trazum'),
|
|
444
|
+
str('trazum.bill.measured_at', new Date(input.measuredAtMs).toISOString()),
|
|
445
|
+
str('trazum.bill.open_days', input.open.days.join(',')),
|
|
446
|
+
count('trazum.bill.open_calls', input.open.calls),
|
|
447
|
+
money('trazum.bill.open_cost_usd', input.open.usd),
|
|
448
|
+
count('trazum.bill.open_tokens', input.open.tokens),
|
|
449
|
+
count('trazum.bill.open_unpriced_calls', input.open.unpricedCalls),
|
|
450
|
+
count('trazum.bill.undated_calls', input.undated.calls),
|
|
451
|
+
money('trazum.bill.undated_cost_usd', input.undated.usd),
|
|
452
|
+
count('trazum.bill.undated_tokens', input.undated.tokens),
|
|
453
|
+
count('trazum.bill.undated_unpriced_calls', input.undated.unpricedCalls),
|
|
454
|
+
],
|
|
455
|
+
},
|
|
456
|
+
scopeMetrics: [{ scope: { name: 'trazum', version: '1.0.0' }, metrics }],
|
|
457
|
+
},
|
|
458
|
+
],
|
|
459
|
+
};
|
|
460
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Claude model as Amazon Bedrock or Google Cloud's Vertex AI names it (#858).
|
|
3
|
+
*
|
|
4
|
+
* Claude Code on those platforms records the platform's id: on Bedrock a
|
|
5
|
+
* cross-region inference profile such as `us.anthropic.claude-sonnet-4-6` or
|
|
6
|
+
* `us.anthropic.claude-haiku-4-5-20251001-v1:0`, on Vertex `claude-sonnet-4-5@20250929`.
|
|
7
|
+
* The forms are the ones Claude Code's own pages show, read 2026-11-21
|
|
8
|
+
* (code.claude.com/docs/en/amazon-bedrock, .../google-vertex-ai): the profile
|
|
9
|
+
* prefixes `us`, `eu`, `apac`, `jp`, `au`, `global` and `us-gov`, the
|
|
10
|
+
* `anthropic.` namespace, an optional `-YYYYMMDD` and an optional `-vN:M`; and
|
|
11
|
+
* Vertex's `@YYYYMMDD`.
|
|
12
|
+
*
|
|
13
|
+
* **Read, never priced.** Each platform sets its own prices, and the catalogue
|
|
14
|
+
* carries Anthropic's. So this only says which platform and which model a
|
|
15
|
+
* refused id names, so the reader can price it from the platform's page. An
|
|
16
|
+
* application inference profile ARN is opaque and is not read at all.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type PartnerPlatform = 'bedrock' | 'vertex';
|
|
20
|
+
|
|
21
|
+
export interface PartnerModelId {
|
|
22
|
+
platform: PartnerPlatform;
|
|
23
|
+
/** The Claude model id inside, as Anthropic names it: `claude-haiku-4-5-20251001`. */
|
|
24
|
+
model: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PROFILE_PREFIXES = ['us-gov', 'us', 'eu', 'apac', 'jp', 'au', 'global'];
|
|
28
|
+
const DIGITS = /^\d+$/;
|
|
29
|
+
const WORD = /^[a-z0-9]+$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A Claude id as Anthropic writes it: `claude`, then one or more hyphenated
|
|
33
|
+
* words of lowercase letters and digits. Read by splitting, not by one pattern
|
|
34
|
+
* with a repeated group, so no input can make it backtrack.
|
|
35
|
+
*/
|
|
36
|
+
function isClaudeId(value: string): boolean {
|
|
37
|
+
const words = value.split('-');
|
|
38
|
+
return words.length >= 2 && words[0] === 'claude' && words.slice(1).every((word) => WORD.test(word));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The id without a Bedrock version such as `-v1` or `-v1:0`. */
|
|
42
|
+
function withoutVersion(value: string): string {
|
|
43
|
+
const at = value.lastIndexOf('-v');
|
|
44
|
+
if (at === -1) return value;
|
|
45
|
+
// A major and at most one minor, each all digits: `1` or `1:0`.
|
|
46
|
+
const parts = value.slice(at + 2).split(':');
|
|
47
|
+
const isVersion = parts.length <= 2 && parts.every((part) => DIGITS.test(part));
|
|
48
|
+
return isVersion ? value.slice(0, at) : value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The platform and model a Bedrock or Vertex id names, or `undefined` for any other id. */
|
|
52
|
+
export function partnerModelId(id: string): PartnerModelId | undefined {
|
|
53
|
+
// Bedrock: an optional profile prefix, the `anthropic.` namespace, the id, an optional version.
|
|
54
|
+
const dot = id.indexOf('.');
|
|
55
|
+
const prefix = dot === -1 ? '' : id.slice(0, dot);
|
|
56
|
+
const rest = PROFILE_PREFIXES.includes(prefix) ? id.slice(dot + 1) : id;
|
|
57
|
+
if (rest.startsWith('anthropic.')) {
|
|
58
|
+
const model = withoutVersion(rest.slice('anthropic.'.length));
|
|
59
|
+
return isClaudeId(model) ? { platform: 'bedrock', model } : undefined;
|
|
60
|
+
}
|
|
61
|
+
// Vertex: the id, `@`, an eight-digit date.
|
|
62
|
+
const at = id.indexOf('@');
|
|
63
|
+
if (at !== -1) {
|
|
64
|
+
const model = id.slice(0, at);
|
|
65
|
+
const date = id.slice(at + 1);
|
|
66
|
+
if (isClaudeId(model) && date.length === 8 && DIGITS.test(date)) return { platform: 'vertex', model: `${model}-${date}` };
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
package/src/plan-edit.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plan action that is an edit to a tracked file (#796).
|
|
3
|
+
*
|
|
4
|
+
* Everything else in a plan is advice a person carries out somewhere Trazum
|
|
5
|
+
* cannot see: move these calls, batch those. An edit is different, because
|
|
6
|
+
* Trazum already writes the new text: `optimize` at the `safe` level trims a
|
|
7
|
+
* prompt, and `reorderForCache` moves its stable blocks in front of the first
|
|
8
|
+
* placeholder. Until now that text was printed and the plan stopped. This
|
|
9
|
+
* module makes the edit a plan action, and gives whoever applies it, later
|
|
10
|
+
* and elsewhere, a way to make exactly that edit and nothing else.
|
|
11
|
+
*
|
|
12
|
+
* Three rules, each a refusal:
|
|
13
|
+
*
|
|
14
|
+
* - **Only the deterministic steps.** An edit is `reorder`, `trim`, or both,
|
|
15
|
+
* recomputed from the file when it is applied. No model writes any of it,
|
|
16
|
+
* and a plan has no field that could carry text somebody else wrote.
|
|
17
|
+
* - **Only the file the plan saw.** The file is hashed when the plan is made
|
|
18
|
+
* and again when the edit is applied; a file that moved on in between is
|
|
19
|
+
* refused, not merged.
|
|
20
|
+
* - **Only the text the plan promised.** The recomputed text is hashed and
|
|
21
|
+
* held to the plan's hash, so a change in the rules between the two runs is
|
|
22
|
+
* a refusal rather than a different edit under the same name.
|
|
23
|
+
*
|
|
24
|
+
* No saving is projected. The figure an edit is expected against is the
|
|
25
|
+
* workload's bill as the log measured it (`detail.baseline`); the file's
|
|
26
|
+
* tokens before and after are the estimator's, and are labelled as such
|
|
27
|
+
* wherever they are shown. `trazum verify` reads the file and the newer log
|
|
28
|
+
* and says whether the edit landed and whether the calls got shorter.
|
|
29
|
+
*
|
|
30
|
+
* Browser-safe like the rest of the core: the hash is passed in, because the
|
|
31
|
+
* core imports no Node builtin and SubtleCrypto is asynchronous.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { SOURCE_EXTENSIONS } from './extract.js';
|
|
35
|
+
import type { RuleId } from './i18n/types.js';
|
|
36
|
+
import { optimize } from './optimize.js';
|
|
37
|
+
import type { PlanAction, PlanEdit, PlanEditStep } from './plan.js';
|
|
38
|
+
import { planEditProblem } from './plan.js';
|
|
39
|
+
import type { PricingCatalogue } from './pricing.js';
|
|
40
|
+
import { reorderForCache } from './reorder.js';
|
|
41
|
+
import { estimateTokens } from './tokenizer.js';
|
|
42
|
+
import type { UsageProfileReport } from './usage.js';
|
|
43
|
+
|
|
44
|
+
/** SHA-256 of a text, lowercase hex. The caller supplies it. */
|
|
45
|
+
export type PlanDigest = (text: string) => string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether a path is a file an edit may rewrite whole.
|
|
49
|
+
*
|
|
50
|
+
* A source file is not a prompt. `optimize` once rewrote `import OpenAI` into
|
|
51
|
+
* `Import OpenAI` in somebody's TypeScript, and an edit that replaces a whole
|
|
52
|
+
* file must never be pointed at one.
|
|
53
|
+
*/
|
|
54
|
+
export function isEditableFile(path: string): boolean {
|
|
55
|
+
const lower = path.toLowerCase();
|
|
56
|
+
return !SOURCE_EXTENSIONS.some((ext) => lower.endsWith(ext));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Runs an edit's steps over a text, in order. Pure and deterministic. */
|
|
60
|
+
export function runPlanEditSteps(
|
|
61
|
+
text: string,
|
|
62
|
+
edit: Pick<PlanEdit, 'steps' | 'disabledRules' | 'minPrefixTokens'>,
|
|
63
|
+
): string {
|
|
64
|
+
let current = text;
|
|
65
|
+
for (const step of edit.steps) {
|
|
66
|
+
if (step === 'reorder') {
|
|
67
|
+
current = reorderForCache(current, { minPrefixTokens: edit.minPrefixTokens }).text;
|
|
68
|
+
} else {
|
|
69
|
+
current = optimize(current, { level: 'safe', disableRules: edit.disabledRules as RuleId[] }).optimized;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return current;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PlanFileEditInput {
|
|
76
|
+
report: UsageProfileReport;
|
|
77
|
+
/** The workload the configuration says sends this file. */
|
|
78
|
+
label: string;
|
|
79
|
+
/** Relative to the repository root, forward slashes. */
|
|
80
|
+
path: string;
|
|
81
|
+
/** The file's text now. */
|
|
82
|
+
text: string;
|
|
83
|
+
/** `trim` always; `reorder` only when asked for, and first. */
|
|
84
|
+
steps: PlanEditStep[];
|
|
85
|
+
disabledRules: string[];
|
|
86
|
+
pricing: PricingCatalogue;
|
|
87
|
+
digest: PlanDigest;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The `edit-file` action for one file, or null when there is none to make.
|
|
92
|
+
*
|
|
93
|
+
* Null when the workload has no priced traffic in the log (there is then no
|
|
94
|
+
* measured figure to expect the edit against), when the file is a source
|
|
95
|
+
* file, or when the steps change nothing.
|
|
96
|
+
*/
|
|
97
|
+
export function planFileEdit(input: PlanFileEditInput): PlanAction | null {
|
|
98
|
+
if (!isEditableFile(input.path)) return null;
|
|
99
|
+
const slice = input.report.byLabelAndModel
|
|
100
|
+
.filter((s) => s.label === input.label && s.breakdown.calls > 0)
|
|
101
|
+
.sort((a, b) => b.breakdown.totalUsd - a.breakdown.totalUsd)[0];
|
|
102
|
+
if (slice === undefined) return null;
|
|
103
|
+
|
|
104
|
+
const minPrefixTokens = input.pricing.byId.get(slice.model)?.cacheMinTokens ?? 0;
|
|
105
|
+
const steps = (['reorder', 'trim'] as const).filter((step) => input.steps.includes(step));
|
|
106
|
+
const after = runPlanEditSteps(input.text, { steps, disabledRules: input.disabledRules, minPrefixTokens });
|
|
107
|
+
if (after === input.text) return null;
|
|
108
|
+
|
|
109
|
+
const b = slice.breakdown;
|
|
110
|
+
const edit: PlanEdit = {
|
|
111
|
+
path: input.path,
|
|
112
|
+
steps,
|
|
113
|
+
level: 'safe',
|
|
114
|
+
disabledRules: [...input.disabledRules],
|
|
115
|
+
minPrefixTokens,
|
|
116
|
+
beforeSha256: input.digest(input.text),
|
|
117
|
+
afterSha256: input.digest(after),
|
|
118
|
+
beforeTokens: estimateTokens(input.text),
|
|
119
|
+
afterTokens: estimateTokens(after),
|
|
120
|
+
};
|
|
121
|
+
// Two texts that differ and hash the same would make every check below
|
|
122
|
+
// meaningless; a digest that does that is not SHA-256.
|
|
123
|
+
if (planEditProblem(edit) !== null) return null;
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
kind: 'edit-file',
|
|
127
|
+
label: input.label,
|
|
128
|
+
model: slice.model,
|
|
129
|
+
savingUsd: null,
|
|
130
|
+
stakeUsd: null,
|
|
131
|
+
assumes: [{ kind: 'file-is-what-label-sends', path: input.path }],
|
|
132
|
+
check: 'trazum verify <plan.json> --against <newer-log>',
|
|
133
|
+
detail: {
|
|
134
|
+
baseline: {
|
|
135
|
+
calls: b.calls,
|
|
136
|
+
usd: b.totalUsd,
|
|
137
|
+
inputPerCallTokens: (b.inputTokens + b.cacheReadTokens + b.cacheWriteTokens) / b.calls,
|
|
138
|
+
outputPerCallTokens: b.outputTokens / b.calls,
|
|
139
|
+
},
|
|
140
|
+
edit,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Why an edit could not be applied. */
|
|
146
|
+
export type PlanEditRefusal =
|
|
147
|
+
/** The edit itself is malformed, or names a source file. */
|
|
148
|
+
| 'not-an-edit'
|
|
149
|
+
/** The file is not the one the plan was made against, nor the one it promised. */
|
|
150
|
+
| 'not-the-planned-file'
|
|
151
|
+
/** The steps, run again on the planned file, did not give the promised text. */
|
|
152
|
+
| 'steps-disagree';
|
|
153
|
+
|
|
154
|
+
export type PlanEditApplication =
|
|
155
|
+
/** `changed` is false when the file already holds the promised text. */
|
|
156
|
+
| { ok: true; text: string; changed: boolean }
|
|
157
|
+
| { ok: false; why: PlanEditRefusal };
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Makes the edit again, from the file as it is now, or says why not.
|
|
161
|
+
*
|
|
162
|
+
* The only way to the new text: the steps, run on a file whose hash is the
|
|
163
|
+
* plan's `beforeSha256`, producing a text whose hash is its `afterSha256`.
|
|
164
|
+
*/
|
|
165
|
+
export function applyPlanEdit(edit: PlanEdit, current: string, digest: PlanDigest): PlanEditApplication {
|
|
166
|
+
if (planEditProblem(edit) !== null || !isEditableFile(edit.path)) return { ok: false, why: 'not-an-edit' };
|
|
167
|
+
const now = digest(current);
|
|
168
|
+
if (now === edit.afterSha256) return { ok: true, text: current, changed: false };
|
|
169
|
+
if (now !== edit.beforeSha256) return { ok: false, why: 'not-the-planned-file' };
|
|
170
|
+
const after = runPlanEditSteps(current, edit);
|
|
171
|
+
if (digest(after) !== edit.afterSha256) return { ok: false, why: 'steps-disagree' };
|
|
172
|
+
return { ok: true, text: after, changed: true };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The edits a plan carries, in its order. */
|
|
176
|
+
export function planEdits(plan: { actions: readonly PlanAction[] }): PlanEdit[] {
|
|
177
|
+
return plan.actions
|
|
178
|
+
.filter((action) => action.kind === 'edit-file' && action.detail.edit !== undefined)
|
|
179
|
+
.map((action) => action.detail.edit as PlanEdit);
|
|
180
|
+
}
|