@deathbeam/pi-usage 0.0.6 → 0.0.7
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 +11 -7
- package/package.json +2 -2
- package/{usage.ts → src/usage.ts} +102 -45
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;
|
|
@@ -158,12 +158,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
158
158
|
total: 0,
|
|
159
159
|
};
|
|
160
160
|
let render = () => {};
|
|
161
|
-
|
|
162
|
-
const loaded = collectUsage(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
161
|
+
const agentDir = getAgentDir();
|
|
162
|
+
const loaded = collectUsage(
|
|
163
|
+
join(agentDir, "sessions"),
|
|
164
|
+
(done, total) => {
|
|
165
|
+
state.done = done;
|
|
166
|
+
state.total = total;
|
|
167
|
+
render();
|
|
168
|
+
},
|
|
169
|
+
join(agentDir, "usage-cache.json"),
|
|
170
|
+
).then((records) => {
|
|
167
171
|
state.records = records;
|
|
168
172
|
});
|
|
169
173
|
const screen = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deathbeam/pi-usage",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
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
|
-
"
|
|
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;
|
|
@@ -50,6 +51,80 @@ async function sessionFiles(dir: string, out: string[]): Promise<void> {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
type CacheEntry = { size: number; mtimeMs: number; records: UsageRecord[] };
|
|
55
|
+
type Cache = { version: number; files: Record<string, CacheEntry> };
|
|
56
|
+
|
|
57
|
+
const CACHE_VERSION = 1;
|
|
58
|
+
|
|
59
|
+
async function readCache(file: string): Promise<Record<string, CacheEntry>> {
|
|
60
|
+
try {
|
|
61
|
+
const cache = JSON.parse(await readFile(file, "utf8")) as Cache;
|
|
62
|
+
if (cache?.version !== CACHE_VERSION || typeof cache.files !== "object" || cache.files === null) return {};
|
|
63
|
+
return cache.files;
|
|
64
|
+
} catch {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function writeCache(file: string, files: Record<string, CacheEntry>): Promise<void> {
|
|
70
|
+
const temp = join(dirname(file), `.tmp-${randomUUID()}`);
|
|
71
|
+
try {
|
|
72
|
+
await writeFile(temp, JSON.stringify({ version: CACHE_VERSION, files } satisfies Cache));
|
|
73
|
+
await rename(temp, file);
|
|
74
|
+
} catch {
|
|
75
|
+
await unlink(temp).catch(() => undefined);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Throws only on read errors, so a partial parse is never cached. */
|
|
80
|
+
async function parseFile(file: string): Promise<UsageRecord[]> {
|
|
81
|
+
const records: UsageRecord[] = [];
|
|
82
|
+
let cwd = "";
|
|
83
|
+
let sessionId = "";
|
|
84
|
+
const lines = createInterface({ input: createReadStream(file, "utf8"), crlfDelay: Infinity });
|
|
85
|
+
for await (const line of lines) {
|
|
86
|
+
// Cheap prefilter: escaped JSON inside tool results never matches this literal.
|
|
87
|
+
if (!line.includes('"role":"assistant"')) {
|
|
88
|
+
// The session header (type:"session", cwd, id) is normally the first line.
|
|
89
|
+
if (sessionId === "" && line.includes('"type":"session"')) {
|
|
90
|
+
try {
|
|
91
|
+
const header = JSON.parse(line);
|
|
92
|
+
if (header?.type === "session") {
|
|
93
|
+
cwd = String(header.cwd ?? "");
|
|
94
|
+
sessionId = String(header.id ?? "");
|
|
95
|
+
}
|
|
96
|
+
} catch {
|
|
97
|
+
// malformed header line: keep scanning for a later one
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
let entry: any;
|
|
103
|
+
try {
|
|
104
|
+
entry = JSON.parse(line);
|
|
105
|
+
} catch {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const msg = entry?.message;
|
|
109
|
+
const usage = msg?.usage;
|
|
110
|
+
if (!usage || !msg.provider || !msg.model) continue;
|
|
111
|
+
const cost = typeof usage.cost === "number" ? usage.cost : num(usage.cost?.total);
|
|
112
|
+
records.push({
|
|
113
|
+
provider: String(msg.provider),
|
|
114
|
+
model: String(msg.model),
|
|
115
|
+
ts: num(msg.timestamp),
|
|
116
|
+
cost,
|
|
117
|
+
input: num(usage.input),
|
|
118
|
+
output: num(usage.output),
|
|
119
|
+
cacheRead: num(usage.cacheRead),
|
|
120
|
+
cacheWrite: num(usage.cacheWrite),
|
|
121
|
+
cwd,
|
|
122
|
+
sessionId,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return records;
|
|
126
|
+
}
|
|
127
|
+
|
|
53
128
|
/**
|
|
54
129
|
* One record per assistant message that carries usage, from every session file under `dir`.
|
|
55
130
|
*
|
|
@@ -59,65 +134,47 @@ async function sessionFiles(dir: string, out: string[]): Promise<void> {
|
|
|
59
134
|
* its own cwd. Parent sessions never embed the delegate child's usage (the delegate tool result
|
|
60
135
|
* carries only text), so there is nothing to double-count. toolResult usage from other tools is
|
|
61
136
|
* ignored for the same reason: those children also persist their own session files inside the scan.
|
|
137
|
+
*
|
|
138
|
+
* shortcut: a file rewritten with the same size and mtime is not re-parsed.
|
|
62
139
|
* shortcut: usage reported by tools whose child sessions live outside this dir is not counted.
|
|
63
140
|
*/
|
|
64
141
|
export async function collectUsage(
|
|
65
142
|
dir: string,
|
|
66
143
|
onProgress?: (done: number, total: number) => void,
|
|
144
|
+
cacheFile?: string,
|
|
67
145
|
): Promise<UsageRecord[]> {
|
|
68
146
|
const files: string[] = [];
|
|
69
147
|
await sessionFiles(dir, files);
|
|
148
|
+
const cached = cacheFile ? await readCache(cacheFile) : {};
|
|
149
|
+
const next: Record<string, CacheEntry> = {};
|
|
70
150
|
const records: UsageRecord[] = [];
|
|
151
|
+
let dirty = false;
|
|
71
152
|
for (const [done, file] of files.entries()) {
|
|
72
153
|
onProgress?.(done, files.length);
|
|
73
|
-
let
|
|
74
|
-
let sessionId = "";
|
|
154
|
+
let info;
|
|
75
155
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
}
|
|
156
|
+
info = await stat(file);
|
|
157
|
+
} catch {
|
|
158
|
+
dirty = true;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const hit = cached[file];
|
|
162
|
+
if (hit?.size === info.size && hit.mtimeMs === info.mtimeMs && Array.isArray(hit.records)) {
|
|
163
|
+
next[file] = hit;
|
|
164
|
+
for (const record of hit.records) records.push(record);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
dirty = true;
|
|
168
|
+
try {
|
|
169
|
+
const parsed = await parseFile(file);
|
|
170
|
+
next[file] = { size: info.size, mtimeMs: info.mtimeMs, records: parsed };
|
|
171
|
+
for (const record of parsed) records.push(record);
|
|
117
172
|
} catch {
|
|
118
173
|
continue; // unreadable or truncated file: skip it, keep what other files gave
|
|
119
174
|
}
|
|
120
175
|
}
|
|
176
|
+
if (!cacheFile) return records;
|
|
177
|
+
if (dirty || Object.keys(next).length !== Object.keys(cached).length) await writeCache(cacheFile, next);
|
|
121
178
|
return records;
|
|
122
179
|
}
|
|
123
180
|
|