@ponbac/token-ledger 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +11 -3
  2. package/dist/main.js +60 -59
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -38,6 +38,11 @@ npx @ponbac/token-ledger@latest config-example > token-ledger.json
38
38
  JSON stdout contains only the structured report, including coverage and pricing
39
39
  metadata; diagnostics go to stderr. Unknown costs are `null`, not zero.
40
40
  Terminal reports sort projects by descending API estimate, with color when supported.
41
+ The `Cost %` column shows each project’s share of the total API estimate. With
42
+ incomplete pricing, `Known cost %` shows shares of the priced subtotal; unknown
43
+ costs are excluded. A zero subtotal shows `—`. Percentages use unrounded costs
44
+ and are displayed to one decimal place, so rounded project shares may not sum
45
+ exactly to 100%.
41
46
  Set `NO_COLOR=1` for plain output. Run `--help` for options.
42
47
 
43
48
  ### Run from source
@@ -319,8 +324,11 @@ tool charges, taxes, negotiated discounts, and subscription allocations are not
319
324
  modeled. Provider-reported credit or dollar figures do not override this estimate.
320
325
 
321
326
  JSON is a versioned report with pricing provenance, applied unit rates, coverage diagnostics, and rows
322
- grouped by project/day/provider/model. CSV exports those rows with empty estimated
323
- cost cells when any usage is unpriced. Neither format includes prompts or responses;
327
+ grouped by project/day/provider/model. CSV instead matches the terminal summary:
328
+ one row per project, sorted by known API cost, plus a Total row. It includes token
329
+ totals, API estimates rounded to two decimals, and cost percentages. Incomplete
330
+ costs display `+ unknown`, and percentages use only the known subtotal. Neither
331
+ format includes prompts or responses;
324
332
  JSON coverage does contain configured source paths. CSV escapes spreadsheet formula
325
333
  prefixes in identifiers. Coverage diagnostics also go to stderr, leaving stdout
326
334
  usable for piping.
@@ -361,7 +369,7 @@ For manual publication:
361
369
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
362
370
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
363
371
  the repository with lifecycle scripts disabled, and exercises the installed CLI.
364
- 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.1.0.tgz` (filename follows version).
372
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.1.2.tgz` (filename follows version).
365
373
  4. Inspect `npm publish ./dist/npm --dry-run`.
366
374
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
367
375
  prompt if requested.
package/dist/main.js CHANGED
@@ -32623,9 +32623,6 @@ function codexParser(file2) {
32623
32623
  };
32624
32624
  }
32625
32625
 
32626
- // packages/core/src/providers/copilot-db.ts
32627
- import { DatabaseSync } from "node:sqlite";
32628
-
32629
32626
  // packages/core/src/providers/copilot.ts
32630
32627
  var CopilotAttributes = exports_Schema.Struct({
32631
32628
  "gen_ai.operation.name": exports_Schema.optionalKey(exports_Schema.String),
@@ -32775,6 +32772,10 @@ var Row = exports_Schema.Struct({
32775
32772
  var databaseError = () => new CopilotDatabaseError({ message: "Cannot read the Copilot span database." });
32776
32773
  var copilotDatabaseLines = exports_Effect.fn("Copilot.readDatabase")(function* (file2) {
32777
32774
  return yield* exports_Effect.gen(function* () {
32775
+ const { DatabaseSync } = yield* exports_Effect.tryPromise({
32776
+ try: () => import("node:sqlite"),
32777
+ catch: databaseError
32778
+ });
32778
32779
  const db = yield* exports_Effect.acquireRelease(exports_Effect.try({ try: () => new DatabaseSync(file2, { readOnly: true }), catch: databaseError }), (connection) => exports_Effect.sync(() => connection.close()));
32779
32780
  const keys4 = Object.keys(CopilotAttributes.fields);
32780
32781
  const rows = yield* exports_Effect.try({
@@ -39400,7 +39401,7 @@ var runWith2 = (command, config) => {
39400
39401
  // packages/cli/package.json
39401
39402
  var package_default = {
39402
39403
  name: "@ponbac/token-ledger",
39403
- version: "0.1.0",
39404
+ version: "0.1.2",
39404
39405
  private: true,
39405
39406
  type: "module",
39406
39407
  bin: {
@@ -40340,40 +40341,15 @@ function csvCell(value5) {
40340
40341
  return `"${safe.replaceAll('"', '""')}"`;
40341
40342
  }
40342
40343
  function csv(report) {
40343
- const header = [
40344
- "project",
40345
- "day_utc",
40346
- "provider",
40347
- "model",
40348
- "input_tokens",
40349
- "cache_read_tokens",
40350
- "cache_write_tokens",
40351
- "output_tokens",
40352
- "estimated_api_cost_usd",
40353
- "priced_subtotal_usd",
40354
- "unpriced_records"
40355
- ];
40356
- const rows = report.rows.map((row) => [
40357
- row.project,
40358
- row.day,
40359
- row.provider,
40360
- row.model,
40361
- row.tokens.input,
40362
- row.tokens.cacheRead,
40363
- row.tokens.cacheWrite,
40364
- row.tokens.output,
40365
- row.estimatedCostUsd,
40366
- row.pricedCostUsd,
40367
- row.unpricedRecords
40368
- ].map(csvCell).join(","));
40369
- return [header.join(","), ...rows].join(`
40344
+ const summary = projectSummary(report, false);
40345
+ const headings = summary.headings.map((heading) => heading === "API estimate" ? "API estimate (USD)" : heading);
40346
+ return [headings, ...summary.rows].map((row) => row.map(csvCell).join(",")).join(`
40370
40347
  `);
40371
40348
  }
40372
40349
  function terminalText(value5) {
40373
40350
  return value5.replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
40374
40351
  }
40375
- function table2(report, columns = 120, colorful = false) {
40376
- const paint = (format8, value5) => colorful ? styleText(format8, value5, { validateStream: false }) : value5;
40352
+ function projectSummary(report, grouped2) {
40377
40353
  const projects = new Map;
40378
40354
  for (const row of report.rows) {
40379
40355
  const previous = projects.get(row.project);
@@ -40383,45 +40359,70 @@ function table2(report, columns = 120, colorful = false) {
40383
40359
  unpriced: (previous?.unpriced ?? 0) + row.unpricedRecords
40384
40360
  });
40385
40361
  }
40386
- if (projects.size === 0)
40387
- return `No observed usage from ${report.since} through ${report.until} (UTC).`;
40388
- const headings = ["Project", "Input", "Cache read", "Cache write", "Output", "API estimate"];
40389
- const number4 = new Intl.NumberFormat("en-US");
40390
- const dollars = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
40362
+ const cost = [...projects.values()].reduce((sum3, project) => sum3 + project.cost, 0);
40363
+ const unpriced = [...projects.values()].reduce((sum3, project) => sum3 + project.unpriced, 0);
40364
+ const headings = [
40365
+ "Project",
40366
+ "Input",
40367
+ "Cache read",
40368
+ "Cache write",
40369
+ "Output",
40370
+ "API estimate",
40371
+ unpriced ? "Known cost %" : "Cost %"
40372
+ ];
40373
+ const percentage = new Intl.NumberFormat("en-US", {
40374
+ style: "percent",
40375
+ minimumFractionDigits: 1,
40376
+ maximumFractionDigits: 1
40377
+ });
40378
+ const share2 = (value5) => cost > 0 ? percentage.format(value5 / cost) : "—";
40379
+ const number4 = new Intl.NumberFormat("en-US", { useGrouping: grouped2 });
40380
+ const dollars = new Intl.NumberFormat("en-US", {
40381
+ style: "currency",
40382
+ currency: "USD",
40383
+ useGrouping: grouped2
40384
+ });
40391
40385
  let allTokens = zeroTokens;
40392
- let cost = 0;
40393
- let unpriced = 0;
40394
40386
  const rows = [];
40395
40387
  const ranked = [...projects].toSorted(([leftProject, left], [rightProject, right]) => right.cost - left.cost || leftProject.localeCompare(rightProject));
40396
40388
  for (const [project, total] of ranked) {
40397
40389
  rows.push([
40398
- terminalText(project),
40390
+ project,
40399
40391
  number4.format(total.tokens.input),
40400
40392
  number4.format(total.tokens.cacheRead),
40401
40393
  number4.format(total.tokens.cacheWrite),
40402
40394
  number4.format(total.tokens.output),
40403
- `${dollars.format(total.cost)}${total.unpriced ? " + unknown" : ""}`
40395
+ `${dollars.format(total.cost)}${total.unpriced ? " + unknown" : ""}`,
40396
+ share2(total.cost)
40404
40397
  ]);
40405
40398
  allTokens = addTokens(allTokens, total.tokens);
40406
- cost += total.cost;
40407
- unpriced += total.unpriced;
40408
- }
40409
- rows.push([
40410
- "Total",
40411
- number4.format(allTokens.input),
40412
- number4.format(allTokens.cacheRead),
40413
- number4.format(allTokens.cacheWrite),
40414
- number4.format(allTokens.output),
40415
- `${dollars.format(cost)}${unpriced ? " + unknown" : ""}`
40416
- ]);
40399
+ }
40400
+ if (projects.size > 0)
40401
+ rows.push([
40402
+ "Total",
40403
+ number4.format(allTokens.input),
40404
+ number4.format(allTokens.cacheRead),
40405
+ number4.format(allTokens.cacheWrite),
40406
+ number4.format(allTokens.output),
40407
+ `${dollars.format(cost)}${unpriced ? " + unknown" : ""}`,
40408
+ share2(cost)
40409
+ ]);
40410
+ return { headings, rows, allTokens, unpriced, projectCount: projects.size };
40411
+ }
40412
+ function table2(report, columns = 120, colorful = false) {
40413
+ const paint = (format8, value5) => colorful ? styleText(format8, value5, { validateStream: false }) : value5;
40414
+ const { headings, rows, allTokens, unpriced, projectCount } = projectSummary(report, true);
40415
+ const number4 = new Intl.NumberFormat("en-US");
40416
+ if (projectCount === 0)
40417
+ return `No observed usage from ${report.since} through ${report.until} (UTC).`;
40417
40418
  const numericWidths = headings.slice(1).map((heading, i) => Math.max(heading.length, ...rows.map((row) => row[i + 1]?.length ?? 0)) + 2);
40418
- const projectWidth = columns - numericWidths.reduce((sum3, width2) => sum3 + width2, 0) - 7;
40419
+ const projectWidth = columns - numericWidths.reduce((sum3, width2) => sum3 + width2, 0) - (headings.length + 1);
40419
40420
  const narrow = projectWidth < 22;
40420
40421
  const colWidths = narrow ? [16, Math.max(4, columns - 19)] : [Math.min(50, projectWidth), ...numericWidths];
40421
40422
  const output = new import_cli_table3.default({
40422
40423
  head: narrow ? [] : headings.map((heading) => paint(["bold", "cyan"], heading)),
40423
40424
  colWidths,
40424
- colAligns: narrow ? ["left", "right"] : ["left", "right", "right", "right", "right", "right"],
40425
+ colAligns: narrow ? ["left", "right"] : ["left", "right", "right", "right", "right", "right", "right"],
40425
40426
  wordWrap: false,
40426
40427
  style: { head: [], border: [] }
40427
40428
  });
@@ -40436,7 +40437,7 @@ function table2(report, columns = 120, colorful = false) {
40436
40437
  for (const [index2, row] of rows.entries()) {
40437
40438
  const styled = row.map((value5, column) => {
40438
40439
  const width2 = narrow ? column === 0 ? columns - 4 : columns - 21 : (colWidths[column] ?? 4) - 2;
40439
- const cell = wrapAnsi(value5, Math.max(2, width2), {
40440
+ const cell = wrapAnsi(terminalText(value5), Math.max(2, width2), {
40440
40441
  hard: true,
40441
40442
  wordWrap: false,
40442
40443
  trim: false
@@ -40445,7 +40446,7 @@ function table2(report, columns = 120, colorful = false) {
40445
40446
  return paint(["bold", "magenta"], cell);
40446
40447
  if (column === 0)
40447
40448
  return paint(projectColors[index2 % projectColors.length] ?? "cyan", cell);
40448
- if (column === headings.length - 1)
40449
+ if (column >= headings.length - 2)
40449
40450
  return paint(value5.includes("unknown") ? "yellow" : "green", cell);
40450
40451
  return cell === "0" ? paint("dim", cell) : cell;
40451
40452
  });
@@ -40463,9 +40464,9 @@ function table2(report, columns = 120, colorful = false) {
40463
40464
  "",
40464
40465
  output.toString(),
40465
40466
  "",
40466
- paint("bold", `${colorful ? "\uD83D\uDCCA " : ""}${number4.format(totalTokens(allTokens))} observed tokens · ${number4.format(projects.size)} ${projects.size === 1 ? "project" : "projects"}`),
40467
+ paint("bold", `${colorful ? "\uD83D\uDCCA " : ""}${number4.format(totalTokens(allTokens))} observed tokens · ${number4.format(projectCount)} ${projectCount === 1 ? "project" : "projects"}`),
40467
40468
  ...unpriced > 0 ? [
40468
- paint("yellow", "Incomplete pricing: sorted by known API subtotal; unknown costs are additional.")
40469
+ paint("yellow", "Incomplete pricing: sorting and percentages use known API subtotals; unknown costs are additional.")
40469
40470
  ] : [],
40470
40471
  paint("dim", "USD API-equivalent estimates; not your subscription bill.")
40471
40472
  ].join(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {