@trazum/cli 2.4.2 → 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/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +17 -2
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +17 -2
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +22 -1
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +173 -13
- package/dist/index.js.map +1 -1
- package/package.json +14 -5
- package/src/i18n/en.ts +22 -2
- package/src/i18n/es.ts +22 -2
- package/src/i18n/types.ts +22 -1
- package/src/index.ts +165 -11
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trazum/cli",
|
|
3
|
-
"version": "2.4.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.4.3",
|
|
4
|
+
"description": "What your AI agents cost, per project, offline: prices Claude Code sessions and Anthropic, OpenAI, OpenRouter, OTel and LiteLLM usage, writes the bill as PDF, HTML or CSV, and enforces budgets in CI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "David Mu\u00f1oz Rey",
|
|
7
7
|
"repository": {
|
|
@@ -14,10 +14,19 @@
|
|
|
14
14
|
"url": "https://github.com/Davmunrey/Trazum/issues"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
|
-
"
|
|
17
|
+
"claude-code",
|
|
18
|
+
"claude",
|
|
19
|
+
"anthropic",
|
|
20
|
+
"openai",
|
|
18
21
|
"llm",
|
|
19
|
-
"
|
|
22
|
+
"ai-agents",
|
|
20
23
|
"cost",
|
|
24
|
+
"usage",
|
|
25
|
+
"billing",
|
|
26
|
+
"finops",
|
|
27
|
+
"tokens",
|
|
28
|
+
"prompt",
|
|
29
|
+
"pdf",
|
|
21
30
|
"cli",
|
|
22
31
|
"ci"
|
|
23
32
|
],
|
|
@@ -41,7 +50,7 @@
|
|
|
41
50
|
"prepublishOnly": "npm run build && npm test"
|
|
42
51
|
},
|
|
43
52
|
"dependencies": {
|
|
44
|
-
"@trazum/core": "2.4.
|
|
53
|
+
"@trazum/core": "2.4.3"
|
|
45
54
|
},
|
|
46
55
|
"devDependencies": {
|
|
47
56
|
"@types/node": "^26.6.3",
|
package/src/i18n/en.ts
CHANGED
|
@@ -60,7 +60,7 @@ ${bold('USAGE')}
|
|
|
60
60
|
trazum rollup <document...|dir> [--json] [--html-out <file>]
|
|
61
61
|
trazum position <usage.jsonl>
|
|
62
62
|
trazum receipt <usage.jsonl|dir> [--stamp] [-o <file>]
|
|
63
|
-
trazum bill <file|dir>... [--label <name>] [--stamp] [-o <file>]
|
|
63
|
+
trazum bill [<file|dir>...] [--label <name>] [--no-label-from-project] [--receipt|--table] [--stamp] [-o <file>]
|
|
64
64
|
[--pdf-out|--html-out|--markdown-out|--csv-out <file>]
|
|
65
65
|
trazum from-claude-code <file|dir> [--label <name>] [-o <file>]
|
|
66
66
|
trazum from-otel <file|dir> [--label-from-service] [-o <file>]
|
|
@@ -1998,7 +1998,27 @@ ${bold('EXAMPLES')}
|
|
|
1998
1998
|
},
|
|
1999
1999
|
|
|
2000
2000
|
bill: {
|
|
2001
|
-
noPath: () =>
|
|
2001
|
+
noPath: (tried) =>
|
|
2002
|
+
`bill needs a file or a directory to read, and found no Claude Code folder at ${tried}. Name one: trazum bill ~/.claude/projects, or trazum bill usage.jsonl.`,
|
|
2003
|
+
defaultPath: (dirs) => `No path given, so reading Claude Code's own folder: ${dirs}`,
|
|
2004
|
+
labelledByProject: (files) =>
|
|
2005
|
+
`${files} Claude Code transcript(s) labelled by their project folder. --label sets one label instead, --no-label-from-project leaves them unlabelled.`,
|
|
2006
|
+
unknownMore: (count) => `${count} more file(s) no shape claims, not guessed at and not in the bill.`,
|
|
2007
|
+
shapeSummary: (shape, files, records, leftOut) =>
|
|
2008
|
+
`${files} file(s): ${shape}${records === null ? ', read as they are' : `, ${records} record(s)`}${leftOut > 0 ? `, ${leftOut} row(s) left out. trazum from-${shape} on a file says why.` : '.'}`,
|
|
2009
|
+
statementHeading: () => 'Your bill',
|
|
2010
|
+
statementTotal: (usd, calls) => `${usd} across ${calls.toLocaleString('en-US')} priced call(s)`,
|
|
2011
|
+
colProject: () => 'project',
|
|
2012
|
+
colWorkload: () => 'workload',
|
|
2013
|
+
colModel: () => 'model',
|
|
2014
|
+
colCalls: () => 'calls',
|
|
2015
|
+
colShare: () => 'share',
|
|
2016
|
+
moreRows: (count) => `… and ${count} more`,
|
|
2017
|
+
statementUnpriced: (models, calls) =>
|
|
2018
|
+
`Not in the total: ${calls} call(s) to ${models}, which the catalogue does not price. Named, never counted as $0.`,
|
|
2019
|
+
statementUnread: (lines) => `${lines} line(s) could not be read as usage and are not in the total.`,
|
|
2020
|
+
statementNext: () =>
|
|
2021
|
+
'The receipt as data: --receipt, or -o receipt.json. The document for whoever pays: --pdf-out bill.pdf.',
|
|
2002
2022
|
notFound: (path) => `${path}: not found`,
|
|
2003
2023
|
noFiles: (path) => `${path}: no .json, .jsonl, .ndjson or .log files under it, plain or gzipped.`,
|
|
2004
2024
|
file: (path, shape, records, leftOut) =>
|
package/src/i18n/es.ts
CHANGED
|
@@ -47,7 +47,7 @@ ${bold('USO')}
|
|
|
47
47
|
trazum rollup <documento...|dir> [--json] [--html-out <fichero>]
|
|
48
48
|
trazum position <uso.jsonl>
|
|
49
49
|
trazum receipt <uso.jsonl|dir> [--stamp] [-o <fichero>]
|
|
50
|
-
trazum bill <fichero|dir>... [--label <nombre>] [--stamp] [-o <fichero>]
|
|
50
|
+
trazum bill [<fichero|dir>...] [--label <nombre>] [--no-label-from-project] [--receipt|--table] [--stamp] [-o <fichero>]
|
|
51
51
|
[--pdf-out|--html-out|--markdown-out|--csv-out <fichero>]
|
|
52
52
|
trazum from-claude-code <fichero|dir> [--label <nombre>] [-o <fichero>]
|
|
53
53
|
trazum from-otel <fichero|dir> [--label-from-service] [-o <fichero>]
|
|
@@ -2026,7 +2026,27 @@ ${bold('EJEMPLOS')}
|
|
|
2026
2026
|
},
|
|
2027
2027
|
|
|
2028
2028
|
bill: {
|
|
2029
|
-
noPath: () =>
|
|
2029
|
+
noPath: (tried) =>
|
|
2030
|
+
`bill necesita un archivo o un directorio que leer, y no encontró ninguna carpeta de Claude Code en ${tried}. Indica una: trazum bill ~/.claude/projects, o trazum bill uso.jsonl.`,
|
|
2031
|
+
defaultPath: (dirs) => `Sin ruta, así que leo la carpeta propia de Claude Code: ${dirs}`,
|
|
2032
|
+
labelledByProject: (files) =>
|
|
2033
|
+
`${files} transcripción(es) de Claude Code etiquetada(s) por su carpeta de proyecto. --label pone una sola etiqueta, --no-label-from-project las deja sin etiqueta.`,
|
|
2034
|
+
unknownMore: (count) => `${count} archivo(s) más que ninguna forma reclama, sin adivinar y fuera de la factura.`,
|
|
2035
|
+
shapeSummary: (shape, files, records, leftOut) =>
|
|
2036
|
+
`${files} archivo(s): ${shape}${records === null ? ', leídos tal cual' : `, ${records} registro(s)`}${leftOut > 0 ? `, ${leftOut} fila(s) fuera. trazum from-${shape} sobre un archivo dice por qué.` : '.'}`,
|
|
2037
|
+
statementHeading: () => 'Tu factura',
|
|
2038
|
+
statementTotal: (usd, calls) => `${usd} en ${calls.toLocaleString('es-ES')} llamada(s) con precio`,
|
|
2039
|
+
colProject: () => 'proyecto',
|
|
2040
|
+
colWorkload: () => 'carga',
|
|
2041
|
+
colModel: () => 'modelo',
|
|
2042
|
+
colCalls: () => 'llamadas',
|
|
2043
|
+
colShare: () => 'parte',
|
|
2044
|
+
moreRows: (count) => `… y ${count} más`,
|
|
2045
|
+
statementUnpriced: (models, calls) =>
|
|
2046
|
+
`Fuera del total: ${calls} llamada(s) a ${models}, que el catálogo no tiene con precio. Se nombran, nunca se cuentan como $0.`,
|
|
2047
|
+
statementUnread: (lines) => `${lines} línea(s) no se pudieron leer como uso y no están en el total.`,
|
|
2048
|
+
statementNext: () =>
|
|
2049
|
+
'El recibo como datos: --receipt, o -o recibo.json. El documento para quien paga: --pdf-out factura.pdf.',
|
|
2030
2050
|
notFound: (path) => `${path}: no encontrado`,
|
|
2031
2051
|
noFiles: (path) => `${path}: no hay archivos .json, .jsonl, .ndjson ni .log dentro, ni sin comprimir ni en .gz.`,
|
|
2032
2052
|
file: (path, shape, records, leftOut) =>
|
package/src/i18n/types.ts
CHANGED
|
@@ -724,7 +724,28 @@ export interface CliMessages {
|
|
|
724
724
|
};
|
|
725
725
|
|
|
726
726
|
bill: {
|
|
727
|
-
|
|
727
|
+
/** No path given and no Claude Code folder where Claude Code keeps one. */
|
|
728
|
+
noPath(tried: string): string;
|
|
729
|
+
/** No path given: the folders found and read instead, named. */
|
|
730
|
+
defaultPath(dirs: string): string;
|
|
731
|
+
/** Claude Code sessions took their label from their project folder. */
|
|
732
|
+
labelledByProject(files: number): string;
|
|
733
|
+
/** The unclaimed files past the first few, counted rather than listed. */
|
|
734
|
+
unknownMore(count: number): string;
|
|
735
|
+
/** Many files of one shape: one line for the shape, not one per file. */
|
|
736
|
+
shapeSummary(shape: string, files: number, records: number | null, leftOut: number): string;
|
|
737
|
+
/** The human statement a terminal gets instead of the JSON receipt. */
|
|
738
|
+
statementHeading(): string;
|
|
739
|
+
statementTotal(usd: string, calls: number): string;
|
|
740
|
+
colProject(): string;
|
|
741
|
+
colWorkload(): string;
|
|
742
|
+
colModel(): string;
|
|
743
|
+
colCalls(): string;
|
|
744
|
+
colShare(): string;
|
|
745
|
+
moreRows(count: number): string;
|
|
746
|
+
statementUnpriced(models: string, calls: number): string;
|
|
747
|
+
statementUnread(lines: number): string;
|
|
748
|
+
statementNext(): string;
|
|
728
749
|
notFound(path: string): string;
|
|
729
750
|
noFiles(path: string): string;
|
|
730
751
|
/** One line per file: its shape, what it became, what was left out. */
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { readdirSync, readFileSync } from 'node:fs';
|
|
4
4
|
import { mkdir, mkdtemp, open, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
5
|
-
import { cpus, tmpdir } from 'node:os';
|
|
5
|
+
import { cpus, homedir, tmpdir } from 'node:os';
|
|
6
6
|
import { createInterface } from 'node:readline/promises';
|
|
7
7
|
import { dirname, join, resolve as resolvePath } from 'node:path';
|
|
8
8
|
import { fileURLToPath } from 'node:url';
|
|
@@ -300,6 +300,7 @@ import type { CliMessages } from './i18n/index.js';
|
|
|
300
300
|
// style.test.js holds the contract: stripped colour output is byte-identical
|
|
301
301
|
// to plain output, and a pipe stays plain.
|
|
302
302
|
import { bar, c, sectionHeading, table } from './style.js';
|
|
303
|
+
import type { TableColumn } from './style.js';
|
|
303
304
|
|
|
304
305
|
// --------------------------------------------------------------------------
|
|
305
306
|
// Argument parsing
|
|
@@ -767,7 +768,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
|
|
|
767
768
|
'from-anthropic': ['label', 'label-by-workspace', 'out', 'o'],
|
|
768
769
|
'from-openai': ['label', 'label-by-project', 'out', 'o'],
|
|
769
770
|
'from-openrouter': ['label', 'label-by-workspace', 'out', 'o'],
|
|
770
|
-
bill: ['label', 'out', 'o', 'stamp', 'pricing', 'pricing-live', 'html-out', 'pdf-out', 'markdown-out', 'csv-out', 'csv-shape'],
|
|
771
|
+
bill: ['label', 'label-from-project', 'out', 'o', 'stamp', 'receipt', 'table', 'pricing', 'pricing-live', 'html-out', 'pdf-out', 'markdown-out', 'csv-out', 'csv-shape'],
|
|
771
772
|
'from-helicone': ['out', 'o'],
|
|
772
773
|
'from-langsmith': ['out', 'o'],
|
|
773
774
|
switch: ['to', 'migration-usd', 'cases'],
|
|
@@ -3420,11 +3421,24 @@ function usageSourcesOf(text: string): UsageSource[] {
|
|
|
3420
3421
|
* a bill rather than usage, and pointed at `reconcile`.
|
|
3421
3422
|
*/
|
|
3422
3423
|
async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages): Promise<void> {
|
|
3423
|
-
if (args.positional.length === 0) throw new Error(t.bill.noPath());
|
|
3424
3424
|
/* Refused before any file is read: a bad shape found after the receipt
|
|
3425
3425
|
printed would end a run that looked finished with an error. */
|
|
3426
3426
|
const csvShape = csvShapeOf(args, t);
|
|
3427
3427
|
|
|
3428
|
+
/*
|
|
3429
|
+
No path is the most common first run, and it used to be a refusal: the
|
|
3430
|
+
person had to know where Claude Code keeps its sessions before the one
|
|
3431
|
+
line on the README worked. Claude Code's own folders are searched
|
|
3432
|
+
instead, and the ones found are named, so nothing is read silently.
|
|
3433
|
+
*/
|
|
3434
|
+
let targets = args.positional;
|
|
3435
|
+
if (targets.length === 0) {
|
|
3436
|
+
const { found, tried } = await claudeProjectFolders(process.env);
|
|
3437
|
+
if (found.length === 0) throw new Error(t.bill.noPath(tried.join(', ')));
|
|
3438
|
+
console.error(t.bill.defaultPath(found.join(', ')));
|
|
3439
|
+
targets = found;
|
|
3440
|
+
}
|
|
3441
|
+
|
|
3428
3442
|
/*
|
|
3429
3443
|
Every path given, files and directories alike, because a shell glob hands
|
|
3430
3444
|
over many and reading the first was a bill missing the rest. A file named
|
|
@@ -3433,13 +3447,16 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3433
3447
|
*/
|
|
3434
3448
|
const files: string[] = [];
|
|
3435
3449
|
const seen = new Set<string>();
|
|
3436
|
-
|
|
3450
|
+
/* Reached through a directory: where a project folder can name the work. */
|
|
3451
|
+
const walked = new Set<string>();
|
|
3452
|
+
const add = (file: string, fromDirectory: boolean): void => {
|
|
3437
3453
|
const key = resolvePath(file);
|
|
3454
|
+
if (fromDirectory) walked.add(file);
|
|
3438
3455
|
if (seen.has(key)) return;
|
|
3439
3456
|
seen.add(key);
|
|
3440
3457
|
files.push(file);
|
|
3441
3458
|
};
|
|
3442
|
-
for (const target of
|
|
3459
|
+
for (const target of targets) {
|
|
3443
3460
|
let info;
|
|
3444
3461
|
try {
|
|
3445
3462
|
info = await stat(target);
|
|
@@ -3453,16 +3470,33 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3453
3470
|
.map((entry) => join(entry.parentPath, entry.name))
|
|
3454
3471
|
.sort();
|
|
3455
3472
|
if (found.length === 0) throw new Error(t.bill.noFiles(target));
|
|
3456
|
-
for (const file of found) add(file);
|
|
3473
|
+
for (const file of found) add(file, true);
|
|
3457
3474
|
} else {
|
|
3458
|
-
add(target);
|
|
3475
|
+
add(target, false);
|
|
3459
3476
|
}
|
|
3460
3477
|
}
|
|
3461
3478
|
|
|
3462
3479
|
const label = stringFlag(args, 'label');
|
|
3463
3480
|
const withLabel = label === undefined ? {} : { label };
|
|
3481
|
+
/*
|
|
3482
|
+
Claude Code keeps one folder per project, and "per project" is the
|
|
3483
|
+
question this door is for. `from-claude-code` has labelled a directory by
|
|
3484
|
+
its project folders since 1.77; `bill` read the same transcripts with no
|
|
3485
|
+
label at all, so the bill it printed could not say which project spent
|
|
3486
|
+
the money. Same default, same function, same way to decline.
|
|
3487
|
+
*/
|
|
3488
|
+
const labelFromProject = boolFlag(args, 'label-from-project', true);
|
|
3489
|
+
let labelledByProject = 0;
|
|
3464
3490
|
const lines: string[] = [];
|
|
3465
3491
|
let sources = 0;
|
|
3492
|
+
/*
|
|
3493
|
+
One line per file is right for a handful and noise for a Claude Code
|
|
3494
|
+
folder of hundreds. Past a handful, the lines are per shape, and the
|
|
3495
|
+
files no shape claims are named for the first few and counted after.
|
|
3496
|
+
*/
|
|
3497
|
+
const perFile = files.length <= 8;
|
|
3498
|
+
const unclaimed: string[] = [];
|
|
3499
|
+
const byShape = new Map<string, { files: number; records: number | null; leftOut: number }>();
|
|
3466
3500
|
|
|
3467
3501
|
for (const file of files) {
|
|
3468
3502
|
const text = await readUsageLog(file, t);
|
|
@@ -3472,7 +3506,7 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3472
3506
|
if (looksLikeAnthropicCost(text) || looksLikeOpenaiCost(text)) {
|
|
3473
3507
|
console.error(t.bill.costReport(file));
|
|
3474
3508
|
} else {
|
|
3475
|
-
|
|
3509
|
+
unclaimed.push(file);
|
|
3476
3510
|
}
|
|
3477
3511
|
continue;
|
|
3478
3512
|
}
|
|
@@ -3487,7 +3521,10 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3487
3521
|
let leftOut = 0;
|
|
3488
3522
|
switch (shape) {
|
|
3489
3523
|
case 'claude-code': {
|
|
3490
|
-
const
|
|
3524
|
+
const project =
|
|
3525
|
+
label === undefined && labelFromProject && walked.has(file) ? projectLabelFor(file) : undefined;
|
|
3526
|
+
if (project !== undefined && project !== '') labelledByProject += 1;
|
|
3527
|
+
const c = claudeCodeRecords(text, project !== undefined && project !== '' ? { label: project } : withLabel);
|
|
3491
3528
|
records = c.records;
|
|
3492
3529
|
leftOut = c.assistantWithoutUsage + c.unparseable;
|
|
3493
3530
|
break;
|
|
@@ -3543,8 +3580,22 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3543
3580
|
}
|
|
3544
3581
|
for (const record of records) lines.push(JSON.stringify(record));
|
|
3545
3582
|
sources += 1;
|
|
3546
|
-
|
|
3583
|
+
const count = shape === 'usage-log' ? null : records.length;
|
|
3584
|
+
if (perFile) {
|
|
3585
|
+
console.error(t.bill.file(file, shape, count, leftOut));
|
|
3586
|
+
} else {
|
|
3587
|
+
const sum = byShape.get(shape) ?? { files: 0, records: count === null ? null : 0, leftOut: 0 };
|
|
3588
|
+
sum.files += 1;
|
|
3589
|
+
if (sum.records !== null && count !== null) sum.records += count;
|
|
3590
|
+
sum.leftOut += leftOut;
|
|
3591
|
+
byShape.set(shape, sum);
|
|
3592
|
+
}
|
|
3547
3593
|
}
|
|
3594
|
+
for (const [shape, sum] of byShape) console.error(t.bill.shapeSummary(shape, sum.files, sum.records, sum.leftOut));
|
|
3595
|
+
const named = perFile ? unclaimed : unclaimed.slice(0, 3);
|
|
3596
|
+
for (const file of named) console.error(t.bill.unknown(file));
|
|
3597
|
+
if (unclaimed.length > named.length) console.error(t.bill.unknownMore(unclaimed.length - named.length));
|
|
3598
|
+
if (labelledByProject > 0) console.error(t.bill.labelledByProject(labelledByProject));
|
|
3548
3599
|
|
|
3549
3600
|
if (sources === 0) throw new Error(t.bill.nothingRead());
|
|
3550
3601
|
|
|
@@ -3552,15 +3603,26 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3552
3603
|
const document = receiptFrom(report, pricing, boolFlag(args, 'stamp') ? { emittedAt: new Date() } : {});
|
|
3553
3604
|
const json = JSON.stringify(document, null, 2);
|
|
3554
3605
|
|
|
3606
|
+
/*
|
|
3607
|
+
A terminal gets a statement a person can read; a pipe gets the receipt.
|
|
3608
|
+
The receipt on a terminal was 60 lines of JSON before the first figure a
|
|
3609
|
+
reader wanted, which is the screen a first run ends on. Scripts are
|
|
3610
|
+
untouched: they pipe, and a pipe is not a TTY. `--receipt` forces the
|
|
3611
|
+
receipt and `--table` forces the statement, either way round. Not
|
|
3612
|
+
`--json`: that flag means a documented contract in docs/format.md, and
|
|
3613
|
+
the receipt is `trazum receipt`'s document, emitted here unchanged.
|
|
3614
|
+
*/
|
|
3555
3615
|
const out = stringFlag(args, 'out') ?? stringFlag(args, 'o');
|
|
3616
|
+
const statement = !boolFlag(args, 'receipt') && boolFlag(args, 'table', process.stdout.isTTY === true);
|
|
3556
3617
|
if (out !== undefined) {
|
|
3557
3618
|
await writeFile(out, json + '\n', 'utf8');
|
|
3558
3619
|
console.error(t.bill.written(out));
|
|
3559
|
-
} else {
|
|
3620
|
+
} else if (!statement) {
|
|
3560
3621
|
console.log(json);
|
|
3561
3622
|
}
|
|
3562
3623
|
console.error(t.bill.sources(sources, files.length));
|
|
3563
3624
|
printReceiptSummary(document, t);
|
|
3625
|
+
if (statement) printBillStatement(report, labelledByProject > 0, t);
|
|
3564
3626
|
await writeBillSideFiles(args, report, pricing, csvShape, t);
|
|
3565
3627
|
|
|
3566
3628
|
/*
|
|
@@ -3577,6 +3639,98 @@ async function commandBill(args: Args, pricing: PricingCatalogue, t: CliMessages
|
|
|
3577
3639
|
}
|
|
3578
3640
|
}
|
|
3579
3641
|
|
|
3642
|
+
/**
|
|
3643
|
+
* Where Claude Code keeps its sessions, in the order it looks itself.
|
|
3644
|
+
*
|
|
3645
|
+
* `CLAUDE_CONFIG_DIR` first, because a person who set it moved the folder on
|
|
3646
|
+
* purpose; then the two defaults Claude Code has used. Every folder that
|
|
3647
|
+
* exists is returned, deduplicated, so a machine with both is billed for
|
|
3648
|
+
* both, once each. `tried` is every candidate, for the refusal to name.
|
|
3649
|
+
*/
|
|
3650
|
+
async function claudeProjectFolders(env: NodeJS.ProcessEnv): Promise<{ found: string[]; tried: string[] }> {
|
|
3651
|
+
const home = homedir();
|
|
3652
|
+
const tried = [
|
|
3653
|
+
...(env.CLAUDE_CONFIG_DIR ? [join(env.CLAUDE_CONFIG_DIR, 'projects')] : []),
|
|
3654
|
+
join(home, '.claude', 'projects'),
|
|
3655
|
+
join(home, '.config', 'claude', 'projects'),
|
|
3656
|
+
];
|
|
3657
|
+
const found: string[] = [];
|
|
3658
|
+
const seen = new Set<string>();
|
|
3659
|
+
for (const candidate of tried) {
|
|
3660
|
+
const key = resolvePath(candidate);
|
|
3661
|
+
if (seen.has(key)) continue;
|
|
3662
|
+
seen.add(key);
|
|
3663
|
+
try {
|
|
3664
|
+
if ((await stat(candidate)).isDirectory()) found.push(candidate);
|
|
3665
|
+
} catch {
|
|
3666
|
+
// Not there, which is the ordinary case for two of the three.
|
|
3667
|
+
}
|
|
3668
|
+
}
|
|
3669
|
+
return { found, tried };
|
|
3670
|
+
}
|
|
3671
|
+
|
|
3672
|
+
/**
|
|
3673
|
+
* The bill as a person reads it: the total, by project, by model, and what
|
|
3674
|
+
* is not in the total, on stdout. The receipt's figures, rendered: nothing
|
|
3675
|
+
* here is computed that the receipt does not carry.
|
|
3676
|
+
*/
|
|
3677
|
+
function printBillStatement(report: UsageProfileReport, byProject: boolean, t: CliMessages): void {
|
|
3678
|
+
const n = (value: number): string => value.toLocaleString(t.numberLocale);
|
|
3679
|
+
const pct = (share: number): string => `${(share * 100).toFixed(1)}%`;
|
|
3680
|
+
const total = report.total.totalUsd;
|
|
3681
|
+
const LIMIT = 12;
|
|
3682
|
+
const rowsOf = (entries: { name: string; calls: number; usd: number }[]): string[][] => {
|
|
3683
|
+
const shown = entries.slice(0, LIMIT).map(({ name, calls, usd }) => {
|
|
3684
|
+
const share = total > 0 ? usd / total : 0;
|
|
3685
|
+
return [name, n(calls), formatUsd(usd), `${bar(share)} ${pct(share)}`];
|
|
3686
|
+
});
|
|
3687
|
+
if (entries.length > LIMIT) shown.push([c.dim(t.bill.moreRows(entries.length - LIMIT)), '', '', '']);
|
|
3688
|
+
return shown;
|
|
3689
|
+
};
|
|
3690
|
+
const columns = (first: string): TableColumn[] => [
|
|
3691
|
+
{ header: first, align: 'left' },
|
|
3692
|
+
{ header: t.bill.colCalls(), align: 'right' },
|
|
3693
|
+
{ header: 'USD', align: 'right' },
|
|
3694
|
+
{ header: t.bill.colShare(), align: 'left' },
|
|
3695
|
+
];
|
|
3696
|
+
|
|
3697
|
+
console.log();
|
|
3698
|
+
console.log(sectionHeading(t.bill.statementHeading()));
|
|
3699
|
+
console.log(` ${c.bold(t.bill.statementTotal(formatUsd(total), report.total.calls))}`);
|
|
3700
|
+
if (report.span !== null) {
|
|
3701
|
+
const span = t.profile.spanLine(dayOf(report.span.fromMs), dayOf(report.span.toMs), spanDays(report.span.fromMs, report.span.toMs));
|
|
3702
|
+
console.log(c.dim(` ${wrap(span, 74, ' ')}`));
|
|
3703
|
+
}
|
|
3704
|
+
|
|
3705
|
+
const labelled = report.byLabel.filter((row) => row.label !== UNLABELLED);
|
|
3706
|
+
if (labelled.length > 0) {
|
|
3707
|
+
console.log();
|
|
3708
|
+
const entries = report.byLabel.map((row) => ({
|
|
3709
|
+
name: row.label === UNLABELLED ? t.profile.unlabelled() : row.label,
|
|
3710
|
+
calls: row.breakdown.calls,
|
|
3711
|
+
usd: row.breakdown.totalUsd,
|
|
3712
|
+
}));
|
|
3713
|
+
for (const line of table(columns(byProject ? t.bill.colProject() : t.bill.colWorkload()), rowsOf(entries))) {
|
|
3714
|
+
console.log(line);
|
|
3715
|
+
}
|
|
3716
|
+
}
|
|
3717
|
+
|
|
3718
|
+
const priced = report.byModel.filter((row) => !report.unpricedModels.includes(row.model));
|
|
3719
|
+
if (priced.length > 0) {
|
|
3720
|
+
console.log();
|
|
3721
|
+
const entries = priced.map((row) => ({ name: row.model, calls: row.breakdown.calls, usd: row.breakdown.totalUsd }));
|
|
3722
|
+
for (const line of table(columns(t.bill.colModel()), rowsOf(entries))) console.log(line);
|
|
3723
|
+
}
|
|
3724
|
+
|
|
3725
|
+
console.log();
|
|
3726
|
+
if (report.unpricedModels.length > 0) {
|
|
3727
|
+
console.log(` ${wrap(t.bill.statementUnpriced(report.unpricedModels.join(', '), report.unpriced.calls), 74, ' ')}`);
|
|
3728
|
+
}
|
|
3729
|
+
if (report.skippedLines.length > 0) console.log(` ${t.bill.statementUnread(report.skippedLines.length)}`);
|
|
3730
|
+
console.log(c.dim(` ${wrap(t.bill.statementNext(), 74, ' ')}`));
|
|
3731
|
+
console.log();
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3580
3734
|
/**
|
|
3581
3735
|
* The bill as a document for somebody else: PDF, HTML, Markdown, CSV.
|
|
3582
3736
|
*
|