@duanz/pi-sol-pi-stats 0.0.0-stage → 1.0.1
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/README.en.md +58 -0
- package/README.md +57 -2
- package/index.ts +128 -0
- package/package.json +31 -4
package/README.en.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# sol-pi-stats
|
|
2
|
+
|
|
3
|
+
<p align="center"><strong>English</strong> | <a href="./README.md">中文</a></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/sol-pi-stats.png" alt="sol-pi-stats cover" width="880"></p>
|
|
6
|
+
|
|
7
|
+
<p align="center"><strong>Context saved by observation-pack, always visible in the status bar</strong><br><sub>Event-driven · no timers · silent on failure</sub></p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@duanz/pi-sol-pi-stats"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-sol-pi-stats.svg?style=flat-square" alt="npm"></a>
|
|
11
|
+
<a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-sol-pi-stats.svg?style=flat-square" alt="license"></a>
|
|
12
|
+
<a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<code>pi install npm:@duanz/pi-sol-pi-stats</code>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## Why
|
|
21
|
+
|
|
22
|
+
sol-pi's observation-pack folds large tool outputs into placeholders and saves a lot of context. But it only flashes for 4 seconds when it saves something, then clears the status — the cumulative amount is invisible afterwards.
|
|
23
|
+
|
|
24
|
+
This extension aggregates the ledger into one always-on status line:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
⚡SoL saved 36M tok · 22 packs
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`/sol` shows details: pack count, cumulative savings, cost range, top packs.
|
|
31
|
+
|
|
32
|
+
## Data source
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
<sessionDir>/sol-pi/<sessionId>/observation-pack/ledger.jsonl
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
In rows with `event=placeholder`, `removedTokens` is what that request avoided sending (one row per pack per request).
|
|
39
|
+
|
|
40
|
+
## Implementation notes
|
|
41
|
+
|
|
42
|
+
- **Event-driven, no timers** — refreshes on session events, never idles
|
|
43
|
+
- **Reads cache only in sync phases** — no `await`, so a reloaded session cannot leave a stale `ctx`
|
|
44
|
+
- **(size, mtime) cache** — an unchanged ledger is not re-parsed
|
|
45
|
+
- **Silent on failure** — stats never break a session
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Enabled on install. Status line is permanent; `/sol` for details.
|
|
50
|
+
|
|
51
|
+
## Limits
|
|
52
|
+
|
|
53
|
+
- Requires sol-pi / observation-pack to actually write `ledger.jsonl`; otherwise no numbers appear.
|
|
54
|
+
- Cost figures are estimates from input and cache prices, not a bill.
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT © DuanZ
|
package/README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# sol-pi-stats
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center"><a href="./README.en.md">English</a> | <strong>中文</strong></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/sol-pi-stats.png" alt="sol-pi-stats cover" width="880"></p>
|
|
6
|
+
|
|
7
|
+
<p align="center"><strong>observation-pack 省下的上下文,常驻显示在状态栏</strong><br><sub>事件驱动 · 不设定时器 · 异常静默</sub></p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@duanz/pi-sol-pi-stats"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-sol-pi-stats.svg?style=flat-square" alt="npm"></a>
|
|
11
|
+
<a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-sol-pi-stats.svg?style=flat-square" alt="license"></a>
|
|
12
|
+
<a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<code>pi install npm:@duanz/pi-sol-pi-stats</code>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## 它解决什么
|
|
21
|
+
|
|
22
|
+
sol-pi 的 observation-pack 会把大块工具输出折成占位符,省下大量上下文。但它自己只在「省到东西」时闪 4 秒,之后把状态清掉——省下的量到底累计了多少,事后完全看不到。
|
|
23
|
+
|
|
24
|
+
这个扩展把 ledger 汇总成一条常驻状态:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
⚡SoL 省 36M tok · 22 包
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
想看明细就用 `/sol`:包数、累计避免量、金额区间、Top 包。
|
|
31
|
+
|
|
32
|
+
## 数据源
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
<sessionDir>/sol-pi/<sessionId>/observation-pack/ledger.jsonl
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`event=placeholder` 的行里 `removedTokens` 是该次请求少发的 token(同一个包每次请求各记一条)。
|
|
39
|
+
|
|
40
|
+
## 实现要点
|
|
41
|
+
|
|
42
|
+
- **事件驱动,不设定时器**:跟着会话事件刷新,避免空转
|
|
43
|
+
- **只在同步阶段读缓存**:不 `await`,防止会话重载后 `ctx` 失效
|
|
44
|
+
- **(size, mtime)缓存**:ledger 没变就不重新解析
|
|
45
|
+
- **异常静默**:统计失败不影响会话
|
|
46
|
+
|
|
47
|
+
## 用法
|
|
48
|
+
|
|
49
|
+
装完即生效。状态栏常驻;`/sol` 看明细。
|
|
50
|
+
|
|
51
|
+
## 限制
|
|
52
|
+
|
|
53
|
+
- 需要 sol-pi / observation-pack 真的在产生 `ledger.jsonl`,否则状态栏不会出现数字。
|
|
54
|
+
- 金额是区间估算(按输入价与缓存价推算),不是账单。
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT © DuanZ
|
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,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duanz/pi-sol-pi-stats",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "状态栏常驻 token / 上下文用量,另有 /sol 明细",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"pi-web"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "DuanZ",
|
|
13
|
+
"homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/sol-pi-stats",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/DuanZGit/D-Hub.git",
|
|
17
|
+
"directory": "extensions/sol-pi-stats"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"README.en.md"
|
|
23
|
+
],
|
|
24
|
+
"pi": {
|
|
25
|
+
"extensions": [
|
|
26
|
+
"./index.ts"
|
|
27
|
+
],
|
|
28
|
+
"image": "https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/sol-pi-stats.png"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20"
|
|
32
|
+
}
|
|
33
|
+
}
|