@duanz/pi-sol-pi-stats 0.0.0-stage → 1.0.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/index.ts +128 -0
- package/package.json +27 -4
- package/README.md +0 -3
package/index.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SoL-Pi 状态栏(常驻)+ /sol 明细
|
|
3
|
+
*
|
|
4
|
+
* NVIDIA 的 sol-pi 插件只在自己省下东西时闪 4 秒(src/sol-pi/tui.ts: STATUS_DURATION_MS=4000),
|
|
5
|
+
* 之后把状态清掉。这里把 observation-pack 的 ledger 汇总成一条常驻状态:
|
|
6
|
+
*
|
|
7
|
+
* ⚡SoL 省 36M tok · 22 包 (状态栏)
|
|
8
|
+
* /sol (明细:包数、累计避免量、金额区间、Top 包)
|
|
9
|
+
*
|
|
10
|
+
* 数据源:<sessionDir>/sol-pi/<sessionId>/observation-pack/ledger.jsonl
|
|
11
|
+
* event=placeholder 的行里 removedTokens = 该次请求少发的 token(同一个包每次请求各记一条)
|
|
12
|
+
*
|
|
13
|
+
* 约束(与 antigravity-quota 一致):事件驱动、不设定时器;只在同步阶段读缓存,
|
|
14
|
+
* 不 await,避免会话重载后 ctx stale;异常静默。
|
|
15
|
+
*/
|
|
16
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import { readFileSync, statSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
|
|
20
|
+
const KEY = "sol-pi-stats";
|
|
21
|
+
|
|
22
|
+
type Pack = { tokens: number; sends: number };
|
|
23
|
+
type Stats = { avoided: number; packs: Map<string, Pack>; inputPrice: number; cacheReadPrice: number };
|
|
24
|
+
|
|
25
|
+
/** 同一份 ledger 只在 (size, mtime) 变化时重新解析。 */
|
|
26
|
+
const cache = new Map<string, { stamp: string; value: Stats | null }>();
|
|
27
|
+
|
|
28
|
+
export function shortTokens(value: number): string {
|
|
29
|
+
const n = Math.max(0, Math.round(Number(value) || 0));
|
|
30
|
+
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
|
|
31
|
+
if (n >= 10_000) return `${Math.round(n / 1000)}k`;
|
|
32
|
+
if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
|
|
33
|
+
return String(n);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function collect(ctx: any): Stats | null {
|
|
37
|
+
try {
|
|
38
|
+
const dir = ctx?.sessionManager?.getSessionDir?.();
|
|
39
|
+
const sid = ctx?.sessionManager?.getSessionId?.();
|
|
40
|
+
if (!dir || !sid) return null;
|
|
41
|
+
const ledger = join(dir, "sol-pi", sid, "observation-pack", "ledger.jsonl");
|
|
42
|
+
|
|
43
|
+
const stat = statSync(ledger);
|
|
44
|
+
const stamp = `${stat.size}:${stat.mtimeMs}`;
|
|
45
|
+
const hit = cache.get(ledger);
|
|
46
|
+
const inputPrice = Number(ctx?.model?.cost?.input) || 0;
|
|
47
|
+
if (hit?.stamp === stamp) return hit.value;
|
|
48
|
+
|
|
49
|
+
let avoided = 0;
|
|
50
|
+
const packs = new Map<string, Pack>();
|
|
51
|
+
for (const line of readFileSync(ledger, "utf8").split("\n")) {
|
|
52
|
+
if (!line) continue;
|
|
53
|
+
let entry: any;
|
|
54
|
+
try {
|
|
55
|
+
entry = JSON.parse(line);
|
|
56
|
+
} catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (entry?.event !== "placeholder") continue;
|
|
60
|
+
const removed = Math.max(0, Math.round(Number(entry.removedTokens) || 0));
|
|
61
|
+
avoided += removed;
|
|
62
|
+
if (typeof entry.id === "string") {
|
|
63
|
+
const pack = packs.get(entry.id) ?? { tokens: 0, sends: 0 };
|
|
64
|
+
pack.tokens += removed;
|
|
65
|
+
pack.sends += 1;
|
|
66
|
+
packs.set(entry.id, pack);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const value: Stats | null = avoided > 0
|
|
70
|
+
? { avoided, packs, inputPrice, cacheReadPrice: Number(ctx?.model?.cost?.cacheRead) || 0 }
|
|
71
|
+
: null;
|
|
72
|
+
cache.set(ledger, { stamp, value });
|
|
73
|
+
return value;
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function statusText(stats: Stats | null): string {
|
|
80
|
+
if (!stats) return "";
|
|
81
|
+
// 2026-09-30:「· 」只做插件间分隔(OV / sol / cache 统一格式);
|
|
82
|
+
// 包数收括号,避免和分隔点撞车(原来 tok · N 包是插件内部格式)。
|
|
83
|
+
return `· ⚡SoL 省 ${shortTokens(stats.avoided)} tok(${stats.packs.size} 包)`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function detailText(stats: Stats | null): string {
|
|
87
|
+
if (!stats) return "SoL-Pi: 本会话还没有省量记录(observation-pack 未触发)";
|
|
88
|
+
const money = (price: number) => `$${((stats.avoided / 1_000_000) * price).toFixed(2)}`;
|
|
89
|
+
const lines = [
|
|
90
|
+
"【SoL-Pi 本会话省量(observation-pack)】",
|
|
91
|
+
` 观察包 ${stats.packs.size} 个 · 累计少发 ${stats.avoided.toLocaleString("en-US")} tokens`,
|
|
92
|
+
];
|
|
93
|
+
if (stats.inputPrice > 0) lines.push(` 换算金额 ≈${money(stats.inputPrice)}(按输入价 $${stats.inputPrice}/M)`);
|
|
94
|
+
if (stats.cacheReadPrice > 0) lines.push(` ≈${money(stats.cacheReadPrice)}(按缓存价 $${stats.cacheReadPrice}/M)`);
|
|
95
|
+
lines.push(" 说明:每条 placeholder 事件记一次,数字是「多次请求累计少发」。");
|
|
96
|
+
const top = [...stats.packs.entries()].sort((a, b) => b[1].tokens - a[1].tokens).slice(0, 5);
|
|
97
|
+
if (top.length) {
|
|
98
|
+
lines.push(" 最省的 5 个包:");
|
|
99
|
+
for (const [id, p] of top) lines.push(` ${id} ${shortTokens(p.tokens)} tok / ${p.sends} 次`);
|
|
100
|
+
}
|
|
101
|
+
return lines.join("\n");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export default function (pi: ExtensionAPI) {
|
|
105
|
+
const render = (ctx: any) => {
|
|
106
|
+
try {
|
|
107
|
+
const text = statusText(collect(ctx));
|
|
108
|
+
if (text) ctx.ui.setStatus(KEY, text);
|
|
109
|
+
} catch {
|
|
110
|
+
/* ctx stale 或读不到 ledger:下个事件再试 */
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
pi.on("session_start", (_e: any, ctx: any) => render(ctx));
|
|
115
|
+
pi.on("turn_start", (_e: any, ctx: any) => render(ctx));
|
|
116
|
+
pi.on("turn_end", (_e: any, ctx: any) => render(ctx));
|
|
117
|
+
|
|
118
|
+
pi.registerCommand("sol", {
|
|
119
|
+
description: "查看 SoL-Pi 本会话省下的 token(observation-pack 汇总)",
|
|
120
|
+
handler: (_a: any, ctx: any) => {
|
|
121
|
+
try {
|
|
122
|
+
ctx.ui.notify(detailText(collect(ctx)), "info");
|
|
123
|
+
} catch {
|
|
124
|
+
/* ctx 失效则静默 */
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duanz/pi-sol-pi-stats",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "状态栏常驻 token / 上下文用量,另有 /sol 明细",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "DuanZ",
|
|
12
|
+
"homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/sol-pi-stats",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/DuanZGit/D-Hub.git",
|
|
16
|
+
"directory": "extensions/sol-pi-stats"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"index.ts"
|
|
20
|
+
],
|
|
21
|
+
"pi": {
|
|
22
|
+
"extensions": [
|
|
23
|
+
"./index.ts"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/README.md
DELETED