@deathbeam/pi-usage 0.0.6 → 0.0.8

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/index.ts CHANGED
@@ -23,7 +23,7 @@ import {
23
23
  type Stats,
24
24
  type UsageFilter,
25
25
  type UsageRecord,
26
- } from "./usage.ts";
26
+ } from "./src/usage.ts";
27
27
 
28
28
  type Column = {
29
29
  label: string;
@@ -32,6 +32,7 @@ type Column = {
32
32
  };
33
33
  type BodyRow = GroupRow & { parent?: string };
34
34
  type Frame = { filters: UsageFilter[]; group: number; cursor: number; top: number };
35
+ type SparkBar = { label: string; char: string };
35
36
 
36
37
  const GROUPS = ["day", "provider", "model", "project"] as const satisfies readonly GroupMode[];
37
38
  const GROUP_LABELS: Record<GroupMode, string> = {
@@ -88,7 +89,9 @@ function hitPct(stats: Stats): string {
88
89
  }
89
90
 
90
91
  function layout(width: number, allColumns: Column[]): { nameWidth: number; columns: Column[] } {
91
- const used = (cols: Column[]) => cols.reduce((sum, c) => sum + c.width + 1, 0);
92
+ function used(cols: Column[]): number {
93
+ return cols.reduce((sum, c) => sum + c.width + 1, 0);
94
+ }
92
95
  let columns = allColumns;
93
96
  for (const label of ["Share", "Hit%", "Cache", "Out", "In", "Reqs", "Δ"]) {
94
97
  if (width - used(columns) >= MIN_NAME_WIDTH) break;
@@ -97,7 +100,9 @@ function layout(width: number, allColumns: Column[]): { nameWidth: number; colum
97
100
  return { nameWidth: Math.max(4, width - used(columns)), columns };
98
101
  }
99
102
 
100
- const padWidth = (s: string, w: number): string => s + " ".repeat(Math.max(0, w - visibleWidth(s)));
103
+ function padWidth(s: string, w: number): string {
104
+ return s + " ".repeat(Math.max(0, w - visibleWidth(s)));
105
+ }
101
106
 
102
107
  function displayName(mode: GroupMode, name: string): string {
103
108
  if (mode === "day") return `${name} ${new Date(`${name}T00:00`).toLocaleDateString("en-US", { weekday: "short" })}`;
@@ -122,8 +127,6 @@ function tableLine(nameWidth: number, columns: Column[], row: GroupRow, totalCos
122
127
  .trimEnd();
123
128
  }
124
129
 
125
- type SparkBar = { label: string; char: string };
126
-
127
130
  function sparkline(daily: DailyCost[], width: number): { first: string; last: string; bars: SparkBar[] } | undefined {
128
131
  const recent = daily.slice(-Math.max(1, width - 12));
129
132
  if (recent.length === 0) return undefined;
@@ -158,12 +161,16 @@ export default function (pi: ExtensionAPI) {
158
161
  total: 0,
159
162
  };
160
163
  let render = () => {};
161
- // Scanning on open is fast enough for a dashboard; no cache file.
162
- const loaded = collectUsage(join(getAgentDir(), "sessions"), (done, total) => {
163
- state.done = done;
164
- state.total = total;
165
- render();
166
- }).then((records) => {
164
+ const agentDir = getAgentDir();
165
+ const loaded = collectUsage(
166
+ join(agentDir, "sessions"),
167
+ (done, total) => {
168
+ state.done = done;
169
+ state.total = total;
170
+ render();
171
+ },
172
+ join(agentDir, "usage-cache.json"),
173
+ ).then((records) => {
167
174
  state.records = records;
168
175
  });
169
176
  const screen = {
@@ -178,9 +185,13 @@ export default function (pi: ExtensionAPI) {
178
185
  let viewHeight = 1; // set on first render; 1 keeps page keys sane before it
179
186
  let body: BodyRow[] = [];
180
187
  const stack: Frame[] = [];
181
- const filters = () => stack.flatMap((frame) => frame.filters);
182
- const dayFilter = () => filters().findLast((f) => f.mode === "day");
183
- const derive = (source: UsageRecord[]) => {
188
+ function filters() {
189
+ return stack.flatMap((frame) => frame.filters);
190
+ }
191
+ function dayFilter() {
192
+ return filters().findLast((f) => f.mode === "day");
193
+ }
194
+ function derive(source: UsageRecord[]) {
184
195
  const active = filters();
185
196
  const records = source.filter((record) =>
186
197
  active.every((f) => f.mode === "day" || matchesFilter(record, f)),
@@ -191,24 +202,27 @@ export default function (pi: ExtensionAPI) {
191
202
  const summary = summarize(records, window.since, window.until);
192
203
  const body = flatten(buildRows(records, window.since, mode, window.until), mode);
193
204
  return { mode, summary, body, compare: day ? undefined : comparison(records) };
194
- };
205
+ }
195
206
  const getView = cacheView(() => derive(state.records!));
196
207
  let allDays: DailyCost[] | undefined;
197
- const allDaily = () => (allDays ??= dailyTotals(state.records!));
208
+ function allDaily() {
209
+ return (allDays ??= dailyTotals(state.records!));
210
+ }
198
211
  render = () => tui.requestRender();
199
212
  void loaded.then(render);
200
- const colorDelta = (delta: number | undefined): string => {
213
+ function colorDelta(delta: number | undefined): string {
201
214
  if (delta === undefined) return "-";
202
215
  if (delta === 0) return "0%";
203
216
  const text = Number.isFinite(delta)
204
217
  ? `${delta > 0 ? "▲" : "▼"}${Math.round(Math.abs(delta))}%`
205
218
  : "▲new";
206
219
  return theme.fg(delta > 0 ? "error" : "success", text);
207
- };
220
+ }
208
221
  const deltaColumn: Column = { label: "Δ", width: 9, cell: (_s, _r, _t, row) => colorDelta(row?.delta) };
209
- const title = (width: number): string => {
210
- const tab = (label: string, active: boolean) =>
211
- active ? theme.fg("accent", `[${label}]`) : theme.fg("muted", ` ${label} `);
222
+ function title(width: number): string {
223
+ function tab(label: string, active: boolean): string {
224
+ return active ? theme.fg("accent", `[${label}]`) : theme.fg("muted", ` ${label} `);
225
+ }
212
226
  const left =
213
227
  theme.fg("accent", theme.bold("Usage")) +
214
228
  filters()
@@ -229,12 +243,13 @@ export default function (pi: ExtensionAPI) {
229
243
  right,
230
244
  width,
231
245
  );
232
- };
233
- const footer = (width: number): string =>
234
- truncateToWidth(
246
+ }
247
+ function footer(width: number): string {
248
+ return truncateToWidth(
235
249
  theme.fg("dim", "enter open · esc back · tab/←→ group · [ ] day · q close · ↑↓ scroll"),
236
250
  width,
237
251
  );
252
+ }
238
253
  return {
239
254
  render(width: number) {
240
255
  const rows = tui.terminal.rows;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deathbeam/pi-usage",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Minimal extension for token usage and cost per provider, model and project.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "files": [
20
20
  "index.ts",
21
- "usage.ts",
21
+ "src",
22
22
  "README.md",
23
23
  "LICENSE"
24
24
  ],
@@ -1,9 +1,10 @@
1
1
  /** Usage aggregation over Pi's persisted session JSONL files. */
2
2
 
3
+ import { randomUUID } from "node:crypto";
3
4
  import { createReadStream } from "node:fs";
4
- import { readdir } from "node:fs/promises";
5
+ import { readdir, readFile, rename, stat, unlink, writeFile } from "node:fs/promises";
5
6
  import { createInterface } from "node:readline";
6
- import { join } from "node:path";
7
+ import { dirname, join } from "node:path";
7
8
 
8
9
  export type UsageRecord = {
9
10
  provider: string;
@@ -28,6 +29,35 @@ export type Stats = {
28
29
  cacheWrite: number;
29
30
  };
30
31
 
32
+ type CacheEntry = { size: number; mtimeMs: number; records: UsageRecord[] };
33
+ type Cache = { version: number; files: Record<string, CacheEntry> };
34
+
35
+ export type GroupMode = "day" | "provider" | "model" | "project";
36
+ export type UsageFilter = { mode: GroupMode; key: string };
37
+
38
+ export type GroupRow = {
39
+ key: string;
40
+ name: string;
41
+ stats: Stats;
42
+ requests: number;
43
+ delta?: number;
44
+ /** Nested model rows; only produced by provider grouping. */
45
+ children?: GroupRow[];
46
+ };
47
+
48
+ export type Summary = {
49
+ stats: Stats;
50
+ requests: number;
51
+ /** Distinct session ids (files) seen in the window. */
52
+ sessions: number;
53
+ };
54
+
55
+ export type DailyCost = { label: string; cost: number };
56
+
57
+ type Tally = { stats: Stats; requests: number; models?: Map<string, Tally>; providers?: Set<string> };
58
+
59
+ const CACHE_VERSION = 1;
60
+
31
61
  function emptyStats(): Stats {
32
62
  return { cost: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
33
63
  }
@@ -36,6 +66,31 @@ function num(value: unknown): number {
36
66
  return typeof value === "number" && Number.isFinite(value) ? value : 0;
37
67
  }
38
68
 
69
+ function addStats(stats: Stats, record: UsageRecord): void {
70
+ stats.cost += record.cost;
71
+ stats.input += record.input;
72
+ stats.output += record.output;
73
+ stats.cacheRead += record.cacheRead;
74
+ stats.cacheWrite += record.cacheWrite;
75
+ }
76
+
77
+ function recordKey(record: UsageRecord, mode: GroupMode): string {
78
+ return mode === "day" ? dayKey(record.ts) : mode === "project" ? record.cwd : record[mode];
79
+ }
80
+
81
+ function tokens(s: Stats): number {
82
+ return s.input + s.output + s.cacheRead + s.cacheWrite;
83
+ }
84
+
85
+ /** Cost descending, then total tokens (so free models still rank by usage). */
86
+ function byCost(a: GroupRow, b: GroupRow): number {
87
+ return b.stats.cost - a.stats.cost || tokens(b.stats) - tokens(a.stats);
88
+ }
89
+
90
+ function newTally(): Tally {
91
+ return { stats: emptyStats(), requests: 0 };
92
+ }
93
+
39
94
  async function sessionFiles(dir: string, out: string[]): Promise<void> {
40
95
  let entries;
41
96
  try {
@@ -50,6 +105,75 @@ async function sessionFiles(dir: string, out: string[]): Promise<void> {
50
105
  }
51
106
  }
52
107
 
108
+ async function readCache(file: string): Promise<Record<string, CacheEntry>> {
109
+ try {
110
+ const cache = JSON.parse(await readFile(file, "utf8")) as Cache;
111
+ if (cache?.version !== CACHE_VERSION || typeof cache.files !== "object" || cache.files === null) return {};
112
+ return cache.files;
113
+ } catch {
114
+ return {};
115
+ }
116
+ }
117
+
118
+ async function writeCache(file: string, files: Record<string, CacheEntry>): Promise<void> {
119
+ const temp = join(dirname(file), `.tmp-${randomUUID()}`);
120
+ try {
121
+ await writeFile(temp, JSON.stringify({ version: CACHE_VERSION, files } satisfies Cache));
122
+ await rename(temp, file);
123
+ } catch {
124
+ await unlink(temp).catch(() => undefined);
125
+ }
126
+ }
127
+
128
+ /** Throws only on read errors, so a partial parse is never cached. */
129
+ async function parseFile(file: string): Promise<UsageRecord[]> {
130
+ const records: UsageRecord[] = [];
131
+ let cwd = "";
132
+ let sessionId = "";
133
+ const lines = createInterface({ input: createReadStream(file, "utf8"), crlfDelay: Infinity });
134
+ for await (const line of lines) {
135
+ // Cheap prefilter: escaped JSON inside tool results never matches this literal.
136
+ if (!line.includes('"role":"assistant"')) {
137
+ // The session header (type:"session", cwd, id) is normally the first line.
138
+ if (sessionId === "" && line.includes('"type":"session"')) {
139
+ try {
140
+ const header = JSON.parse(line);
141
+ if (header?.type === "session") {
142
+ cwd = String(header.cwd ?? "");
143
+ sessionId = String(header.id ?? "");
144
+ }
145
+ } catch {
146
+ // malformed header line: keep scanning for a later one
147
+ }
148
+ }
149
+ continue;
150
+ }
151
+ let entry: any;
152
+ try {
153
+ entry = JSON.parse(line);
154
+ } catch {
155
+ continue;
156
+ }
157
+ const msg = entry?.message;
158
+ const usage = msg?.usage;
159
+ if (!usage || !msg.provider || !msg.model) continue;
160
+ const cost = typeof usage.cost === "number" ? usage.cost : num(usage.cost?.total);
161
+ records.push({
162
+ provider: String(msg.provider),
163
+ model: String(msg.model),
164
+ ts: num(msg.timestamp),
165
+ cost,
166
+ input: num(usage.input),
167
+ output: num(usage.output),
168
+ cacheRead: num(usage.cacheRead),
169
+ cacheWrite: num(usage.cacheWrite),
170
+ cwd,
171
+ sessionId,
172
+ });
173
+ }
174
+ return records;
175
+ }
176
+
53
177
  /**
54
178
  * One record per assistant message that carries usage, from every session file under `dir`.
55
179
  *
@@ -59,79 +183,50 @@ async function sessionFiles(dir: string, out: string[]): Promise<void> {
59
183
  * its own cwd. Parent sessions never embed the delegate child's usage (the delegate tool result
60
184
  * carries only text), so there is nothing to double-count. toolResult usage from other tools is
61
185
  * ignored for the same reason: those children also persist their own session files inside the scan.
186
+ *
187
+ * shortcut: a file rewritten with the same size and mtime is not re-parsed.
62
188
  * shortcut: usage reported by tools whose child sessions live outside this dir is not counted.
63
189
  */
64
190
  export async function collectUsage(
65
191
  dir: string,
66
192
  onProgress?: (done: number, total: number) => void,
193
+ cacheFile?: string,
67
194
  ): Promise<UsageRecord[]> {
68
195
  const files: string[] = [];
69
196
  await sessionFiles(dir, files);
197
+ const cached = cacheFile ? await readCache(cacheFile) : {};
198
+ const next: Record<string, CacheEntry> = {};
70
199
  const records: UsageRecord[] = [];
200
+ let dirty = false;
71
201
  for (const [done, file] of files.entries()) {
72
202
  onProgress?.(done, files.length);
73
- let cwd = "";
74
- let sessionId = "";
203
+ let info;
75
204
  try {
76
- const lines = createInterface({ input: createReadStream(file, "utf8"), crlfDelay: Infinity });
77
- for await (const line of lines) {
78
- // Cheap prefilter: escaped JSON inside tool results never matches this literal.
79
- if (!line.includes('"role":"assistant"')) {
80
- // The session header (type:"session", cwd, id) is normally the first line.
81
- if (sessionId === "" && line.includes('"type":"session"')) {
82
- try {
83
- const header = JSON.parse(line);
84
- if (header?.type === "session") {
85
- cwd = String(header.cwd ?? "");
86
- sessionId = String(header.id ?? "");
87
- }
88
- } catch {
89
- // malformed header line: keep scanning for a later one
90
- }
91
- }
92
- continue;
93
- }
94
- let entry: any;
95
- try {
96
- entry = JSON.parse(line);
97
- } catch {
98
- continue;
99
- }
100
- const msg = entry?.message;
101
- const usage = msg?.usage;
102
- if (!usage || !msg.provider || !msg.model) continue;
103
- const cost = typeof usage.cost === "number" ? usage.cost : num(usage.cost?.total);
104
- records.push({
105
- provider: String(msg.provider),
106
- model: String(msg.model),
107
- ts: num(msg.timestamp),
108
- cost,
109
- input: num(usage.input),
110
- output: num(usage.output),
111
- cacheRead: num(usage.cacheRead),
112
- cacheWrite: num(usage.cacheWrite),
113
- cwd,
114
- sessionId,
115
- });
116
- }
205
+ info = await stat(file);
206
+ } catch {
207
+ dirty = true;
208
+ continue;
209
+ }
210
+ const hit = cached[file];
211
+ if (hit?.size === info.size && hit.mtimeMs === info.mtimeMs && Array.isArray(hit.records)) {
212
+ next[file] = hit;
213
+ for (const record of hit.records) records.push(record);
214
+ continue;
215
+ }
216
+ dirty = true;
217
+ try {
218
+ const parsed = await parseFile(file);
219
+ next[file] = { size: info.size, mtimeMs: info.mtimeMs, records: parsed };
220
+ for (const record of parsed) records.push(record);
117
221
  } catch {
118
222
  continue; // unreadable or truncated file: skip it, keep what other files gave
119
223
  }
120
224
  }
225
+ if (!cacheFile) return records;
226
+ if (dirty || Object.keys(next).length !== Object.keys(cached).length) await writeCache(cacheFile, next);
121
227
  return records;
122
228
  }
123
229
 
124
- function addStats(stats: Stats, record: UsageRecord): void {
125
- stats.cost += record.cost;
126
- stats.input += record.input;
127
- stats.output += record.output;
128
- stats.cacheRead += record.cacheRead;
129
- stats.cacheWrite += record.cacheWrite;
130
- }
131
-
132
- export type GroupMode = "day" | "provider" | "model" | "project";
133
- export type UsageFilter = { mode: GroupMode; key: string };
134
-
135
230
  export function cacheView<T>(derive: () => T) {
136
231
  let cached: { key: string; value: T } | undefined;
137
232
  return (mode: GroupMode, filters: UsageFilter[], now = Date.now()): T => {
@@ -163,10 +258,10 @@ export function dayWindow(key: string, offset = 0): { since: number; until: numb
163
258
  // UTC ordinals count local calendar days without DST changing their length.
164
259
  export function windowDays(since: number, until: number): number {
165
260
  if (since >= until) return 0;
166
- const ordinal = (ts: number) => {
261
+ function ordinal(ts: number): number {
167
262
  const d = new Date(ts);
168
263
  return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / 86_400_000;
169
- };
264
+ }
170
265
  return ordinal(until - 1) - ordinal(since) + 1;
171
266
  }
172
267
 
@@ -174,10 +269,6 @@ export function percentChange(current: number, previous: number): number {
174
269
  return previous === 0 ? (current === 0 ? 0 : Infinity) : ((current - previous) / previous) * 100;
175
270
  }
176
271
 
177
- function recordKey(record: UsageRecord, mode: GroupMode): string {
178
- return mode === "day" ? dayKey(record.ts) : mode === "project" ? record.cwd : record[mode];
179
- }
180
-
181
272
  export function matchesFilter(record: UsageRecord, filter: UsageFilter): boolean {
182
273
  return recordKey(record, filter.mode) === filter.key;
183
274
  }
@@ -223,25 +314,6 @@ export function comparison(records: UsageRecord[], now = Date.now()) {
223
314
  };
224
315
  }
225
316
 
226
- export type GroupRow = {
227
- key: string;
228
- name: string;
229
- stats: Stats;
230
- requests: number;
231
- delta?: number;
232
- /** Nested model rows; only produced by provider grouping. */
233
- children?: GroupRow[];
234
- };
235
-
236
- const tokens = (s: Stats): number => s.input + s.output + s.cacheRead + s.cacheWrite;
237
-
238
- /** Cost descending, then total tokens (so free models still rank by usage). */
239
- const byCost = (a: GroupRow, b: GroupRow): number => b.stats.cost - a.stats.cost || tokens(b.stats) - tokens(a.stats);
240
-
241
- type Tally = { stats: Stats; requests: number; models?: Map<string, Tally>; providers?: Set<string> };
242
-
243
- const newTally = (): Tally => ({ stats: emptyStats(), requests: 0 });
244
-
245
317
  export function buildRows(
246
318
  records: UsageRecord[],
247
319
  sinceMs = 0,
@@ -287,23 +359,18 @@ export function buildRows(
287
359
  rows[i]!.delta = percentChange(rows[i]!.stats.cost, rows[i + 1]!.stats.cost);
288
360
  return rows;
289
361
  }
290
- const toRow = ([name, group]: [string, Tally]): GroupRow => ({
291
- key: name,
292
- name: mode === "model" && group.providers!.size > 1 ? `${name} (${group.providers!.size})` : name,
293
- stats: group.stats,
294
- requests: group.requests,
295
- children: group.models && [...group.models].map(toRow).sort(byCost),
296
- });
362
+ function toRow([name, group]: [string, Tally]): GroupRow {
363
+ return {
364
+ key: name,
365
+ name: mode === "model" && group.providers!.size > 1 ? `${name} (${group.providers!.size})` : name,
366
+ stats: group.stats,
367
+ requests: group.requests,
368
+ children: group.models && [...group.models].map(toRow).sort(byCost),
369
+ };
370
+ }
297
371
  return [...groups].map(toRow).sort(byCost);
298
372
  }
299
373
 
300
- export type Summary = {
301
- stats: Stats;
302
- requests: number;
303
- /** Distinct session ids (files) seen in the window. */
304
- sessions: number;
305
- };
306
-
307
374
  /** Totals for the summary block: one pass over [sinceMs, untilMs). */
308
375
  export function summarize(records: UsageRecord[], sinceMs = 0, untilMs = Infinity): Summary {
309
376
  const stats = emptyStats();
@@ -318,9 +385,9 @@ export function summarize(records: UsageRecord[], sinceMs = 0, untilMs = Infinit
318
385
  return { stats, requests, sessions: sessions.size };
319
386
  }
320
387
 
321
- export type DailyCost = { label: string; cost: number };
322
-
323
- export const seriesKey = (key: string, parent?: string): string => JSON.stringify([key, parent ?? null]);
388
+ export function seriesKey(key: string, parent?: string): string {
389
+ return JSON.stringify([key, parent ?? null]);
390
+ }
324
391
 
325
392
  export function buildDailySeries(
326
393
  records: UsageRecord[],
@@ -339,11 +406,11 @@ export function buildDailySeries(
339
406
  cursor = dayWindow(key).until;
340
407
  }
341
408
  const costs = new Map<string, Map<number, number>>();
342
- const add = (key: string, index: number, cost: number) => {
409
+ function add(key: string, index: number, cost: number): void {
343
410
  let values = costs.get(key);
344
411
  if (!values) costs.set(key, (values = new Map()));
345
412
  values.set(index, (values.get(index) ?? 0) + cost);
346
- };
413
+ }
347
414
  for (const record of records) {
348
415
  if (record.ts < since || record.ts >= until) continue;
349
416
  const index = positions.get(dayKey(record.ts));