@qmxme/pi-stats 0.2.0 → 0.3.0
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/package.json +1 -1
- package/stats.ts +51 -50
package/package.json
CHANGED
package/stats.ts
CHANGED
|
@@ -1,65 +1,66 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
2
|
|
|
3
3
|
export default function (pi: ExtensionAPI) {
|
|
4
|
-
let
|
|
5
|
-
let initialOutputTokens: number = 0;
|
|
6
|
-
let widgetKey = "streaming-stats";
|
|
4
|
+
let agentStartMs: number | null = null;
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
|
|
12
|
-
return n.toString();
|
|
13
|
-
}
|
|
6
|
+
pi.on("agent_start", () => {
|
|
7
|
+
agentStartMs = Date.now();
|
|
8
|
+
});
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (s < 60) return `${s.toFixed(1)}s`;
|
|
19
|
-
const m = Math.floor(s / 60);
|
|
20
|
-
const rem = (s % 60).toFixed(1);
|
|
21
|
-
return `${m}m${rem}s`;
|
|
22
|
-
}
|
|
10
|
+
pi.on("agent_end", async (event, ctx) => {
|
|
11
|
+
if (!ctx.hasUI) return;
|
|
12
|
+
if (agentStartMs === null) return;
|
|
23
13
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
initialOutputTokens = event.message.usage.output;
|
|
28
|
-
ctx.ui.setWidget(widgetKey, undefined);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
14
|
+
const elapsedMs = Date.now() - agentStartMs;
|
|
15
|
+
agentStartMs = null;
|
|
16
|
+
if (elapsedMs <= 0) return;
|
|
31
17
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
18
|
+
let input = 0;
|
|
19
|
+
let output = 0;
|
|
20
|
+
let cacheRead = 0;
|
|
21
|
+
let cacheWrite = 0;
|
|
22
|
+
let totalTokens = 0;
|
|
36
23
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
24
|
+
for (const message of event.messages) {
|
|
25
|
+
if (message.role !== "assistant") continue;
|
|
26
|
+
input += message.usage?.input ?? 0;
|
|
27
|
+
output += message.usage?.output ?? 0;
|
|
28
|
+
cacheRead += message.usage?.cacheRead ?? 0;
|
|
29
|
+
cacheWrite += message.usage?.cacheWrite ?? 0;
|
|
30
|
+
totalTokens += message.usage?.totalTokens ?? 0;
|
|
31
|
+
}
|
|
40
32
|
|
|
41
|
-
|
|
33
|
+
if (output <= 0) return;
|
|
42
34
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
cacheStr = ` | cache ${cacheParts.join(" ")}`;
|
|
50
|
-
}
|
|
35
|
+
// Format numbers with K/M suffix
|
|
36
|
+
function formatNum(n: number): string {
|
|
37
|
+
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
|
|
38
|
+
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
|
|
39
|
+
return n.toString();
|
|
40
|
+
}
|
|
51
41
|
|
|
52
|
-
|
|
42
|
+
// Format duration
|
|
43
|
+
function formatDuration(ms: number): string {
|
|
44
|
+
const s = ms / 1000;
|
|
45
|
+
if (s < 60) return `${s.toFixed(1)}s`;
|
|
46
|
+
const m = Math.floor(s / 60);
|
|
47
|
+
const rem = (s % 60).toFixed(1);
|
|
48
|
+
return `${m}m${rem}s`;
|
|
49
|
+
}
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
render: () => lines,
|
|
58
|
-
invalidate: () => {},
|
|
59
|
-
};
|
|
60
|
-
});
|
|
51
|
+
const elapsedSeconds = elapsedMs / 1000;
|
|
52
|
+
const tps = output / elapsedSeconds;
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
// Build cache string, only showing non-zero values
|
|
55
|
+
let cacheStr = "";
|
|
56
|
+
const cacheParts: string[] = [];
|
|
57
|
+
if (cacheRead > 0) cacheParts.push(`${formatNum(cacheRead)}↓`);
|
|
58
|
+
if (cacheWrite > 0) cacheParts.push(`${formatNum(cacheWrite)}↑`);
|
|
59
|
+
if (cacheParts.length > 0) {
|
|
60
|
+
cacheStr = ` | cache ${cacheParts.join(" ")}`;
|
|
63
61
|
}
|
|
62
|
+
|
|
63
|
+
const stats = `${tps.toFixed(1)} tok/s | ${formatNum(input)} → ${formatNum(output)}${cacheStr} | total ${formatNum(totalTokens)} | ${formatDuration(elapsedMs)}`;
|
|
64
|
+
ctx.ui.notify(stats, "info");
|
|
64
65
|
});
|
|
65
|
-
}
|
|
66
|
+
}
|