@trazum/core 2.5.0 → 2.5.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 +10 -0
- package/dist/codex.d.ts +37 -0
- package/dist/codex.d.ts.map +1 -0
- package/dist/codex.js +148 -0
- package/dist/codex.js.map +1 -0
- package/dist/gateway.d.ts +10 -8
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +89 -6
- package/dist/gateway.js.map +1 -1
- package/dist/harnesses.d.ts +32 -0
- package/dist/harnesses.d.ts.map +1 -0
- package/dist/harnesses.js +21 -0
- package/dist/harnesses.js.map +1 -0
- package/dist/host.d.ts +4 -5
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +5 -11
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/receipt.d.ts +13 -0
- package/dist/receipt.d.ts.map +1 -1
- package/dist/receipt.js +3 -0
- package/dist/receipt.js.map +1 -1
- package/dist/tool-output.d.ts +18 -0
- package/dist/tool-output.d.ts.map +1 -0
- package/dist/tool-output.js +63 -0
- package/dist/tool-output.js.map +1 -0
- package/dist/usage.d.ts +35 -0
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +37 -0
- package/dist/usage.js.map +1 -1
- package/package.json +1 -1
- package/src/codex.ts +136 -0
- package/src/gateway.ts +79 -5
- package/src/harnesses.ts +53 -0
- package/src/host.ts +6 -13
- package/src/index.ts +12 -0
- package/src/receipt.ts +14 -1
- package/src/tool-output.ts +69 -0
- package/src/usage.ts +63 -0
package/src/receipt.ts
CHANGED
|
@@ -221,7 +221,17 @@ export type ReceiptGap =
|
|
|
221
221
|
* rows. `ceilingUsd` is their share of it, `floorUsd` the same tokens at the
|
|
222
222
|
* cheaper rate; the true figure for them is between the two.
|
|
223
223
|
*/
|
|
224
|
-
| { kind: 'tier-ceiling'; calls: number; models: string[]; ceilingUsd: number; floorUsd: number }
|
|
224
|
+
| { kind: 'tier-ceiling'; calls: number; models: string[]; ceilingUsd: number; floorUsd: number }
|
|
225
|
+
/**
|
|
226
|
+
* Calls a time window had to leave out because they carry no timestamp.
|
|
227
|
+
*
|
|
228
|
+
* A bill for a period holds the calls it can place in that period. A call
|
|
229
|
+
* with no clock might belong to it or might not, so it is neither counted
|
|
230
|
+
* in nor dropped in silence: a total for "October" that quietly lost calls
|
|
231
|
+
* of unknown date is the flattering non-answer, and this says how many.
|
|
232
|
+
* Present only on a receipt built from a windowed report.
|
|
233
|
+
*/
|
|
234
|
+
| { kind: 'undated-excluded'; calls: number };
|
|
225
235
|
|
|
226
236
|
export interface ReceiptDocument {
|
|
227
237
|
/** The one thing a consumer must branch on. Set here, by the builder. */
|
|
@@ -389,6 +399,9 @@ export function receiptFrom(
|
|
|
389
399
|
gaps.push({ kind: 'unread-lines', count: report.skippedLines.length });
|
|
390
400
|
}
|
|
391
401
|
if (report.span === null) gaps.push({ kind: 'no-clock' });
|
|
402
|
+
if (report.timeWindow !== null && report.timeWindow.undatedExcluded > 0) {
|
|
403
|
+
gaps.push({ kind: 'undated-excluded', calls: report.timeWindow.undatedExcluded });
|
|
404
|
+
}
|
|
392
405
|
|
|
393
406
|
/*
|
|
394
407
|
* Summed across the lines rather than read off a total, because the profile
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { estimateTokens } from './tokenizer.js';
|
|
2
|
+
import { MAX_INPUT_CHARS } from './limits.js';
|
|
3
|
+
|
|
4
|
+
/** A display view of a log, never a replacement for machine-readable output. */
|
|
5
|
+
export interface ToolOutputView {
|
|
6
|
+
text: string;
|
|
7
|
+
applied: boolean;
|
|
8
|
+
reason: 'compacted' | 'unchanged' | 'unsupported' | 'too-large' | 'not-worthwhile';
|
|
9
|
+
estimatedTokensBefore: number | null;
|
|
10
|
+
estimatedTokensAfter: number | null;
|
|
11
|
+
repeatedLines: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const EFFICIENT_OUTPUT_INSTRUCTIONS =
|
|
15
|
+
'Answer the requested task directly. Avoid repeating the request, unchanged code, ' +
|
|
16
|
+
'and redundant explanations. Use only the detail needed for correctness. ' +
|
|
17
|
+
'Preserve errors, evidence, constraints and required checks. Expand compacted tool ' +
|
|
18
|
+
'output when exact lines are needed. Follow the user’s requested format and depth. ' +
|
|
19
|
+
'Never claim token reductions increase a provider’s quota.';
|
|
20
|
+
|
|
21
|
+
const HEADER = '[Trazum compacted log;';
|
|
22
|
+
// Diagnostic repetition may be evidence of frequency and must remain visible.
|
|
23
|
+
const DIAGNOSTIC = /error|warn|fail|fatal|exception|traceback|panic|assert|stderr/i;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Collapse only consecutive, identical, complete log lines. No semantic rewrite,
|
|
27
|
+
* source-code compression, tail truncation or deduplication across intervening
|
|
28
|
+
* lines. The caller must store the original before emitting an applied view.
|
|
29
|
+
* Counts are heuristic estimates, not provider usage or subscription capacity.
|
|
30
|
+
*/
|
|
31
|
+
export function compactToolOutput(input: string, recoveryCommand: string): ToolOutputView {
|
|
32
|
+
const unchanged = (reason: ToolOutputView['reason'], before: number | null): ToolOutputView => ({
|
|
33
|
+
text: input, applied: false, reason, estimatedTokensBefore: before,
|
|
34
|
+
estimatedTokensAfter: before, repeatedLines: 0,
|
|
35
|
+
});
|
|
36
|
+
if (input.length > MAX_INPUT_CHARS) return unchanged('too-large', null);
|
|
37
|
+
const before = estimateTokens(input);
|
|
38
|
+
if (!recoveryCommand || /[\r\n]/.test(recoveryCommand)) return unchanged('unsupported', before);
|
|
39
|
+
// Do not reinterpret terminal controls, fenced source or our own output.
|
|
40
|
+
if (/[\x00-\x08\x0b-\x1f\x7f]/.test(input) || /```|~~~/.test(input) || input.startsWith(HEADER)) {
|
|
41
|
+
return unchanged('unsupported', before);
|
|
42
|
+
}
|
|
43
|
+
const lines = input.split('\n');
|
|
44
|
+
const out: string[] = [];
|
|
45
|
+
let removed = 0;
|
|
46
|
+
for (let i = 0; i < lines.length;) {
|
|
47
|
+
const line = lines[i]!;
|
|
48
|
+
let end = i + 1;
|
|
49
|
+
// Leave the final unterminated line untouched.
|
|
50
|
+
while (end < lines.length - 1 && lines[end] === line) end++;
|
|
51
|
+
const count = end - i;
|
|
52
|
+
if (count >= 3 && line.trim() !== '' && !DIAGNOSTIC.test(line)) {
|
|
53
|
+
out.push(line, `[Trazum: previous line repeated ${count - 1} additional times]`);
|
|
54
|
+
removed += count - 1;
|
|
55
|
+
} else {
|
|
56
|
+
for (let j = i; j < end; j++) out.push(lines[j]!);
|
|
57
|
+
}
|
|
58
|
+
i = end;
|
|
59
|
+
}
|
|
60
|
+
if (!removed) return unchanged('unchanged', before);
|
|
61
|
+
const text = `${HEADER} consecutive repetitions only; token counts are estimates]\n` +
|
|
62
|
+
`Recover original: ${recoveryCommand}\n\n` + out.join('\n');
|
|
63
|
+
const after = estimateTokens(text);
|
|
64
|
+
// Include recovery instructions in the comparison; tiny reductions do not
|
|
65
|
+
// justify changing the display. This is a heuristic, not a token guarantee.
|
|
66
|
+
if (before - after < 128 || after > before * 0.8) return unchanged('not-worthwhile', before);
|
|
67
|
+
return { text, applied: true, reason: 'compacted', estimatedTokensBefore: before,
|
|
68
|
+
estimatedTokensAfter: after, repeatedLines: removed };
|
|
69
|
+
}
|
package/src/usage.ts
CHANGED
|
@@ -1530,6 +1530,69 @@ export function sharesOf(breakdown: UsageBreakdown): UsageShares {
|
|
|
1530
1530
|
};
|
|
1531
1531
|
}
|
|
1532
1532
|
|
|
1533
|
+
/** One calendar month of a bill, UTC. */
|
|
1534
|
+
export interface MonthSpend {
|
|
1535
|
+
/** `YYYY-MM`, UTC. */
|
|
1536
|
+
month: string;
|
|
1537
|
+
/** The first and last UTC day of the month, the `--since`/`--until` that bill it alone. */
|
|
1538
|
+
firstDay: string;
|
|
1539
|
+
lastDay: string;
|
|
1540
|
+
calls: number;
|
|
1541
|
+
usd: number;
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/** A bill by calendar month, and the part of it that sits in no month. */
|
|
1545
|
+
export interface SpendByMonth {
|
|
1546
|
+
/** Oldest first. Only months with priced, dated calls appear. */
|
|
1547
|
+
months: MonthSpend[];
|
|
1548
|
+
/**
|
|
1549
|
+
* Priced calls with no clock: in the total and in no month. Zero calls when
|
|
1550
|
+
* every priced call carried a timestamp. Without it the month rows add up
|
|
1551
|
+
* short of the total, and the reader goes looking for an error that is not
|
|
1552
|
+
* one.
|
|
1553
|
+
*/
|
|
1554
|
+
undated: { calls: number; usd: number };
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/**
|
|
1558
|
+
* A bill by calendar month, summed from the report's own days.
|
|
1559
|
+
*
|
|
1560
|
+
* A year of Claude Code sessions in one total says what it all cost and not
|
|
1561
|
+
* which month cost what, which is the next question. One function, so that
|
|
1562
|
+
* the statement, the bill's documents and the browser cannot disagree about
|
|
1563
|
+
* which month a call belongs to: a month is UTC, like the days it is summed
|
|
1564
|
+
* from, and the days hold only priced calls that carry a clock.
|
|
1565
|
+
*/
|
|
1566
|
+
export function spendByMonth(report: Pick<UsageProfileReport, 'spendByDay' | 'total'>): SpendByMonth {
|
|
1567
|
+
const byMonth = new Map<string, { calls: number; usd: number }>();
|
|
1568
|
+
for (const day of report.spendByDay) {
|
|
1569
|
+
const key = day.day.slice(0, 7);
|
|
1570
|
+
const month = byMonth.get(key) ?? { calls: 0, usd: 0 };
|
|
1571
|
+
month.calls += day.calls;
|
|
1572
|
+
month.usd += day.usd;
|
|
1573
|
+
byMonth.set(key, month);
|
|
1574
|
+
}
|
|
1575
|
+
const months = [...byMonth.entries()]
|
|
1576
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
1577
|
+
.map(([month, { calls, usd }]) => {
|
|
1578
|
+
const [year, monthNumber] = month.split('-').map(Number);
|
|
1579
|
+
const lastDay = new Date(Date.UTC(year ?? 1970, monthNumber ?? 1, 0)).toISOString().slice(0, 10);
|
|
1580
|
+
return { month, firstDay: `${month}-01`, lastDay, calls, usd };
|
|
1581
|
+
});
|
|
1582
|
+
const dated = months.reduce((sum, month) => ({ calls: sum.calls + month.calls, usd: sum.usd + month.usd }), {
|
|
1583
|
+
calls: 0,
|
|
1584
|
+
usd: 0,
|
|
1585
|
+
});
|
|
1586
|
+
const undatedCalls = report.total.calls - dated.calls;
|
|
1587
|
+
return {
|
|
1588
|
+
months,
|
|
1589
|
+
undated:
|
|
1590
|
+
undatedCalls > 0
|
|
1591
|
+
? { calls: undatedCalls, usd: Math.max(0, report.total.totalUsd - dated.usd) }
|
|
1592
|
+
: { calls: 0, usd: 0 },
|
|
1593
|
+
};
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1533
1596
|
/**
|
|
1534
1597
|
* How much of the input that could have been cached was.
|
|
1535
1598
|
*
|