@trazum/cli 2.4.1 → 2.4.3
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/dist/html.d.ts.map +1 -1
- package/dist/html.js +9 -4
- package/dist/html.js.map +1 -1
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +32 -3
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +33 -3
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +32 -1
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +308 -36
- package/dist/index.js.map +1 -1
- package/dist/pdf.d.ts +8 -0
- package/dist/pdf.d.ts.map +1 -0
- package/dist/pdf.js +261 -0
- package/dist/pdf.js.map +1 -0
- package/package.json +15 -6
- package/src/html.ts +16 -4
- package/src/i18n/en.ts +39 -3
- package/src/i18n/es.ts +40 -3
- package/src/i18n/types.ts +33 -1
- package/src/index.ts +308 -31
- package/src/pdf.ts +318 -0
package/src/pdf.ts
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { PdfDocument, UNLABELLED, formatUsd, pdfFit, pdfWrap, sharesOf } from '@trazum/core';
|
|
2
|
+
import type { PdfColor, PdfFont, UsageBreakdown } from '@trazum/core';
|
|
3
|
+
import type { ProfileMarkdownInput } from './markdown.js';
|
|
4
|
+
import { dayOf, spanDays } from './time.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The profile as a PDF, for the person who signs the bill off.
|
|
8
|
+
*
|
|
9
|
+
* The same input object the HTML and Markdown doors take, and the same
|
|
10
|
+
* sentences from the same message catalogue, so the three files are one
|
|
11
|
+
* report in three envelopes: a figure that differed between them would be a
|
|
12
|
+
* second computation, and there is none. The rules the HTML door applies are
|
|
13
|
+
* applied here, for the same reasons:
|
|
14
|
+
*
|
|
15
|
+
* - **The caveats are furniture.** What the report cannot say renders in a
|
|
16
|
+
* bordered block ahead of every table, at the weight of the totals it
|
|
17
|
+
* qualifies. A PDF is the format most likely to be forwarded without the
|
|
18
|
+
* terminal it came from, which makes it the one where a caveat that can be
|
|
19
|
+
* cropped out matters most.
|
|
20
|
+
* - **Every page carries the footer** that says where the figures came from,
|
|
21
|
+
* because a printed page gets separated from its first one.
|
|
22
|
+
*
|
|
23
|
+
* Written with core's `PdfDocument`: no dependency, no embedded font, the
|
|
24
|
+
* same bytes for the same report.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const INK: PdfColor = '#1f2328';
|
|
28
|
+
const MUTED: PdfColor = '#59606a';
|
|
29
|
+
const RULE: PdfColor = '#d9d5cc';
|
|
30
|
+
const ZEBRA: PdfColor = '#f7f6f3';
|
|
31
|
+
const CAVEAT_EDGE: PdfColor = '#8a6d1a';
|
|
32
|
+
const CAVEAT_FILL: PdfColor = '#fbf6e9';
|
|
33
|
+
const CAVEAT_INK: PdfColor = '#6d5510';
|
|
34
|
+
const FAIL_EDGE: PdfColor = '#b42318';
|
|
35
|
+
const FAIL_FILL: PdfColor = '#fdf1f0';
|
|
36
|
+
const FAIL_INK: PdfColor = '#7a1a12';
|
|
37
|
+
const PASS_INK: PdfColor = '#365f37';
|
|
38
|
+
|
|
39
|
+
const MARGIN = 48;
|
|
40
|
+
const TOP = 56;
|
|
41
|
+
/** Below this the footer lives; content that would cross it starts a page. */
|
|
42
|
+
const FOOTER_SPACE = 70;
|
|
43
|
+
|
|
44
|
+
export interface ProfilePdfOptions {
|
|
45
|
+
/** Written into the file's metadata only when given, so tests can compare bytes. */
|
|
46
|
+
createdAt?: Date;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The profile as one PDF. Same input as the HTML and Markdown doors. */
|
|
50
|
+
export function renderProfilePdf(input: ProfileMarkdownInput, options: ProfilePdfOptions = {}): Uint8Array {
|
|
51
|
+
const { report, levers, cache, t, gates, window, stalePricing } = input;
|
|
52
|
+
const n = (value: number): string => value.toLocaleString(t.numberLocale);
|
|
53
|
+
const pct = (share: number): string => `${(share * 100).toFixed(1)}%`;
|
|
54
|
+
const showLabel = (label: string): string => (label === UNLABELLED ? t.profile.unlabelled() : label);
|
|
55
|
+
|
|
56
|
+
const doc = new PdfDocument();
|
|
57
|
+
const width = doc.width - MARGIN * 2;
|
|
58
|
+
const bottom = doc.height - FOOTER_SPACE;
|
|
59
|
+
let y = TOP;
|
|
60
|
+
|
|
61
|
+
const need = (height: number): boolean => {
|
|
62
|
+
if (y + height <= bottom) return false;
|
|
63
|
+
doc.addPage();
|
|
64
|
+
y = TOP;
|
|
65
|
+
return true;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** A wrapped paragraph; returns nothing, moves the cursor. */
|
|
69
|
+
const paragraph = (text: string, font: PdfFont, size: number, color: PdfColor, after = 4): void => {
|
|
70
|
+
const leading = size * 1.35;
|
|
71
|
+
for (const line of pdfWrap(text, font, size, width)) {
|
|
72
|
+
need(leading);
|
|
73
|
+
doc.text(MARGIN, y + size, line, { font, size, color });
|
|
74
|
+
y += leading;
|
|
75
|
+
}
|
|
76
|
+
y += after;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const heading = (text: string): void => {
|
|
80
|
+
need(40);
|
|
81
|
+
y += 14;
|
|
82
|
+
doc.text(MARGIN, y + 11, text, { font: 'bold', size: 11.5, color: INK });
|
|
83
|
+
y += 17;
|
|
84
|
+
doc.line(MARGIN, y, MARGIN + width, y, { color: RULE, width: 0.75 });
|
|
85
|
+
y += 8;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A box of sentences: the caveats and the gate verdict. Measured before it
|
|
90
|
+
* is drawn so the border fits what is inside it, and moved whole to the
|
|
91
|
+
* next page rather than split, because half a caveat is a different claim.
|
|
92
|
+
*/
|
|
93
|
+
const box = (
|
|
94
|
+
title: string | null,
|
|
95
|
+
lines: string[],
|
|
96
|
+
colors: { edge: PdfColor; fill: PdfColor; ink: PdfColor },
|
|
97
|
+
bullet: boolean,
|
|
98
|
+
): void => {
|
|
99
|
+
const size = 9;
|
|
100
|
+
const leading = size * 1.4;
|
|
101
|
+
const pad = 10;
|
|
102
|
+
const indent = bullet ? 10 : 0;
|
|
103
|
+
const wrapped = lines.map((line) => pdfWrap(line, bullet ? 'regular' : 'bold', size, width - pad * 2 - indent));
|
|
104
|
+
const titleHeight = title === null ? 0 : 18;
|
|
105
|
+
const height = pad * 2 + titleHeight + wrapped.reduce((sum, w) => sum + w.length * leading + 2, 0);
|
|
106
|
+
need(height + 8);
|
|
107
|
+
doc.rect(MARGIN, y, width, height, { fill: colors.fill, stroke: colors.edge, lineWidth: 1.5 });
|
|
108
|
+
let inner = y + pad;
|
|
109
|
+
if (title !== null) {
|
|
110
|
+
doc.text(MARGIN + pad, inner + 10, title, { font: 'bold', size: 10.5, color: colors.ink });
|
|
111
|
+
inner += titleHeight;
|
|
112
|
+
}
|
|
113
|
+
for (const lineSet of wrapped) {
|
|
114
|
+
lineSet.forEach((line, i) => {
|
|
115
|
+
if (bullet && i === 0) doc.text(MARGIN + pad, inner + size, '•', { size, color: colors.ink });
|
|
116
|
+
doc.text(MARGIN + pad + indent, inner + size, line, { font: bullet ? 'regular' : 'bold', size, color: colors.ink });
|
|
117
|
+
inner += leading;
|
|
118
|
+
});
|
|
119
|
+
inner += 2;
|
|
120
|
+
}
|
|
121
|
+
y += height + 10;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
interface Row {
|
|
125
|
+
name: string;
|
|
126
|
+
breakdown: UsageBreakdown;
|
|
127
|
+
/** Money as a dash, never `$0`, as in the HTML door: zero claims the calls were free. */
|
|
128
|
+
unpriced?: boolean;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The breakdown table, its header repeated on every page it crosses. */
|
|
132
|
+
const table = (rows: Row[], nameHeading: string): void => {
|
|
133
|
+
const size = 7.5;
|
|
134
|
+
const rowHeight = 13;
|
|
135
|
+
const figureColumns = [
|
|
136
|
+
t.html.colCalls(),
|
|
137
|
+
t.html.colInput(),
|
|
138
|
+
t.html.colCacheRead(),
|
|
139
|
+
t.html.colCacheWrite(),
|
|
140
|
+
t.html.colOutput(),
|
|
141
|
+
'USD',
|
|
142
|
+
'%',
|
|
143
|
+
];
|
|
144
|
+
const figureWidth = 54;
|
|
145
|
+
const nameWidth = width - figureWidth * figureColumns.length;
|
|
146
|
+
const right = (column: number): number => MARGIN + nameWidth + figureWidth * (column + 1) - 4;
|
|
147
|
+
|
|
148
|
+
/* A heading wraps to two lines rather than being cut: "lecturas de
|
|
149
|
+
caché" cut to "lecturas de…" no longer says which column it is. */
|
|
150
|
+
const headings = figureColumns.map((column) => pdfWrap(column, 'bold', size, figureWidth - 6).slice(0, 2));
|
|
151
|
+
const headerLines = Math.max(1, ...headings.map((lines) => lines.length));
|
|
152
|
+
const header = (): void => {
|
|
153
|
+
const base = y + 9 + (headerLines - 1) * (size + 1.5);
|
|
154
|
+
doc.text(MARGIN + 4, base, pdfFit(nameHeading, 'bold', size, nameWidth - 8), { font: 'bold', size, color: INK });
|
|
155
|
+
headings.forEach((lines, i) => {
|
|
156
|
+
lines.forEach((line, j) => {
|
|
157
|
+
const lineY = base - (lines.length - 1 - j) * (size + 1.5);
|
|
158
|
+
doc.textRight(right(i), lineY, pdfFit(line, 'bold', size, figureWidth - 6), { font: 'bold', size, color: INK });
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
y += rowHeight + (headerLines - 1) * (size + 1.5);
|
|
162
|
+
doc.line(MARGIN, y, MARGIN + width, y, { color: RULE, width: 1.25 });
|
|
163
|
+
y += 2;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
need(rowHeight * 3);
|
|
167
|
+
header();
|
|
168
|
+
rows.forEach(({ name, breakdown: b, unpriced }, index) => {
|
|
169
|
+
if (need(rowHeight)) header();
|
|
170
|
+
if (index % 2 === 1) doc.rect(MARGIN, y, width, rowHeight, { fill: ZEBRA });
|
|
171
|
+
const share = report.total.totalUsd > 0 && !unpriced ? pct(b.totalUsd / report.total.totalUsd) : '—';
|
|
172
|
+
const cells = [
|
|
173
|
+
n(b.calls),
|
|
174
|
+
n(b.inputTokens),
|
|
175
|
+
n(b.cacheReadTokens),
|
|
176
|
+
n(b.cacheWriteTokens),
|
|
177
|
+
n(b.outputTokens),
|
|
178
|
+
unpriced ? '—' : formatUsd(b.totalUsd),
|
|
179
|
+
share,
|
|
180
|
+
];
|
|
181
|
+
doc.text(MARGIN + 4, y + 9, pdfFit(name, 'regular', size, nameWidth - 8), { size, color: INK });
|
|
182
|
+
cells.forEach((cell, i) => {
|
|
183
|
+
doc.textRight(right(i), y + 9, pdfFit(cell, 'regular', size, figureWidth - 6), { size, color: INK });
|
|
184
|
+
});
|
|
185
|
+
y += rowHeight;
|
|
186
|
+
});
|
|
187
|
+
y += 6;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// The masthead: what this is and the one figure a reader came for.
|
|
191
|
+
doc.text(MARGIN, y + 9, 'trazum', { font: 'bold', size: 9, color: MUTED });
|
|
192
|
+
y += 16;
|
|
193
|
+
doc.line(MARGIN, y, MARGIN + width, y, { color: RULE, width: 0.75 });
|
|
194
|
+
y += 14;
|
|
195
|
+
paragraph(t.profile.heading(), 'bold', 19, INK, 2);
|
|
196
|
+
paragraph(t.profile.spent(t.profile.calls(report.total.calls), formatUsd(report.total.totalUsd)), 'bold', 14, INK, 4);
|
|
197
|
+
if (report.span !== null) {
|
|
198
|
+
paragraph(
|
|
199
|
+
t.profile.spanLine(dayOf(report.span.fromMs), dayOf(report.span.toMs), spanDays(report.span.fromMs, report.span.toMs)),
|
|
200
|
+
'regular',
|
|
201
|
+
9.5,
|
|
202
|
+
MUTED,
|
|
203
|
+
0,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (window !== undefined) paragraph(t.profile.windowLine(window.since, window.until), 'regular', 9.5, MUTED, 0);
|
|
207
|
+
y += 8;
|
|
208
|
+
|
|
209
|
+
if (gates !== undefined && gates.lines.length > 0) {
|
|
210
|
+
if (gates.failed) {
|
|
211
|
+
box(null, gates.lines, { edge: FAIL_EDGE, fill: FAIL_FILL, ink: FAIL_INK }, false);
|
|
212
|
+
} else {
|
|
213
|
+
for (const line of gates.lines) paragraph(line, 'regular', 9.5, PASS_INK, 0);
|
|
214
|
+
y += 6;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// The caveats, ahead of every table, as in the HTML door.
|
|
219
|
+
const caveats: string[] = [];
|
|
220
|
+
if (report.unpricedModels.length > 0) {
|
|
221
|
+
caveats.push(t.profile.unpriced(report.unpricedModels.join(', '), report.unpriced.calls));
|
|
222
|
+
}
|
|
223
|
+
if (report.skippedLines.length > 0) {
|
|
224
|
+
const shown = report.skippedLines.slice(0, 5).join(', ');
|
|
225
|
+
caveats.push(t.profile.skipped(report.skippedLines.length, report.skippedLines.length > 5 ? `${shown}…` : shown));
|
|
226
|
+
}
|
|
227
|
+
if (report.span === null) caveats.push(t.html.noClock());
|
|
228
|
+
if (!report.hasSessions) caveats.push(t.html.noSessions());
|
|
229
|
+
if (report.total.assumedWriteTtlCalls > 0) {
|
|
230
|
+
caveats.push(
|
|
231
|
+
t.profile.cacheTtlUnsettled(
|
|
232
|
+
report.total.assumedWriteTtlCalls,
|
|
233
|
+
formatUsd(-cache.deltaUsd),
|
|
234
|
+
formatUsd(cache.worstCaseDeltaUsd),
|
|
235
|
+
),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (stalePricing !== undefined) caveats.push(t.profile.pricesStale(stalePricing.date, stalePricing.days));
|
|
239
|
+
if (caveats.length > 0) {
|
|
240
|
+
box(t.html.caveatsHeading(), caveats, { edge: CAVEAT_EDGE, fill: CAVEAT_FILL, ink: CAVEAT_INK }, true);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
heading(t.html.byLabelHeading());
|
|
244
|
+
table(
|
|
245
|
+
report.byLabel.map((row) => ({ name: showLabel(row.label), breakdown: row.breakdown })),
|
|
246
|
+
t.html.colLabel(),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
heading(t.html.byModelHeading());
|
|
250
|
+
table(
|
|
251
|
+
report.byModel.map((row) => ({
|
|
252
|
+
name: row.model,
|
|
253
|
+
breakdown: row.breakdown,
|
|
254
|
+
unpriced: report.unpricedModels.includes(row.model),
|
|
255
|
+
})),
|
|
256
|
+
t.html.colModel(),
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
// The findings, as the sentences the terminal says.
|
|
260
|
+
const findings: string[] = [];
|
|
261
|
+
for (const lever of levers.slices.slice(0, 5)) {
|
|
262
|
+
findings.push(
|
|
263
|
+
t.profile.leverSlice(showLabel(lever.label), lever.modelName, formatUsd(lever.combinedUsd), pct(lever.shareOfBill)),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
if (report.total.cacheReadTokens + report.total.cacheWriteTokens > 0) {
|
|
267
|
+
findings.push(
|
|
268
|
+
cache.deltaUsd > 0
|
|
269
|
+
? t.profile.cacheLost(formatUsd(cache.deltaUsd), n(report.total.cacheWriteTokens), n(report.total.cacheReadTokens))
|
|
270
|
+
: t.profile.cachePaidOff(formatUsd(-cache.deltaUsd)),
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
if (findings.length > 0) {
|
|
274
|
+
heading(t.html.findingsHeading());
|
|
275
|
+
const size = 9.5;
|
|
276
|
+
const leading = size * 1.4;
|
|
277
|
+
for (const finding of findings) {
|
|
278
|
+
const lines = pdfWrap(finding, 'regular', size, width - 12);
|
|
279
|
+
need(lines.length * leading);
|
|
280
|
+
doc.text(MARGIN, y + size, '•', { size, color: INK });
|
|
281
|
+
for (const line of lines) {
|
|
282
|
+
doc.text(MARGIN + 12, y + size, line, { size, color: INK });
|
|
283
|
+
y += leading;
|
|
284
|
+
}
|
|
285
|
+
y += 3;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const shares = sharesOf(report.total);
|
|
290
|
+
y += 6;
|
|
291
|
+
paragraph(
|
|
292
|
+
t.html.sharesLine(pct(shares.input), pct(shares.cacheRead), pct(shares.cacheWrite), pct(shares.output)),
|
|
293
|
+
'regular',
|
|
294
|
+
9,
|
|
295
|
+
MUTED,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
// The footer on every page, now that the page count is known.
|
|
299
|
+
const footerSize = 7.5;
|
|
300
|
+
const pageLabelWidth = 70;
|
|
301
|
+
const footerLines = pdfWrap(t.html.footer(), 'regular', footerSize, width - pageLabelWidth);
|
|
302
|
+
for (let page = 0; page < doc.pageCount; page += 1) {
|
|
303
|
+
doc.setPage(page);
|
|
304
|
+
const top = doc.height - FOOTER_SPACE + 22;
|
|
305
|
+
doc.line(MARGIN, top, MARGIN + width, top, { color: RULE, width: 0.75 });
|
|
306
|
+
footerLines.forEach((line, i) => {
|
|
307
|
+
doc.text(MARGIN, top + 11 + i * footerSize * 1.35, line, { size: footerSize, color: MUTED });
|
|
308
|
+
});
|
|
309
|
+
doc.textRight(MARGIN + width, top + 11, t.pdf.page(page + 1, doc.pageCount), { size: footerSize, color: MUTED });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return doc.toBytes({
|
|
313
|
+
title: t.profile.heading(),
|
|
314
|
+
subject: t.profile.spent(t.profile.calls(report.total.calls), formatUsd(report.total.totalUsd)),
|
|
315
|
+
...(options.createdAt !== undefined ? { createdAt: options.createdAt } : {}),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|