my-opencode-toolkit 0.2.4 → 0.2.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/stats.ts +12 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "my-opencode-toolkit",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "OpenCode 插件:系统通知、持久目标、定时任务与 TUI 面板",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/stats.ts CHANGED
@@ -5,8 +5,10 @@ import { asRecord } from "./shared/json.ts"
5
5
  /** 单个会话的进程内统计。 */
6
6
  export type SessionStats = {
7
7
  readonly steps: number
8
+ /** 总提示词 token:`input + cache.read + cache.write`。 */
8
9
  readonly input: number
9
10
  readonly output: number
11
+ /** 缓存命中 token:只算 `cache.read`。 */
10
12
  readonly cached: number
11
13
  readonly tools: Readonly<Record<string, number>>
12
14
  readonly subagents: number
@@ -70,9 +72,10 @@ export function createStatsTracker(): StatsTracker {
70
72
  const cache = asRecord(record["cache"]) ?? {}
71
73
  const stats = of(sessionID)
72
74
  stats.steps += 1
73
- stats.input += count(record["input"])
75
+ // 分母是完整提示词;cache.write 是写入成本,不算命中。
76
+ stats.input += count(record["input"]) + count(cache["read"]) + count(cache["write"])
74
77
  stats.output += count(record["output"])
75
- stats.cached += count(cache["read"]) + count(cache["write"])
78
+ stats.cached += count(cache["read"])
76
79
  },
77
80
 
78
81
  recordTool(sessionID, tool) {
@@ -189,9 +192,10 @@ export async function buildStats(ctx: Plugin.Context, sessionID: string): Promis
189
192
  steps += 1
190
193
  const tokens = message.tokens
191
194
  if (tokens !== undefined) {
192
- input += tokens.input
195
+ // 与 recordStep 保持同一口径:input 是完整提示词,cached 只算读命中。
196
+ input += tokens.input + tokens.cache.read + tokens.cache.write
193
197
  output += tokens.output
194
- cached += tokens.cache.read + tokens.cache.write
198
+ cached += tokens.cache.read
195
199
  }
196
200
  for (const part of message.content) {
197
201
  if (part.type !== "tool") continue
@@ -204,12 +208,11 @@ export async function buildStats(ctx: Plugin.Context, sessionID: string): Promis
204
208
  }
205
209
 
206
210
  /**
207
- * 输入:输入 token 与缓存 token。
211
+ * 输入:完整提示词 token(含缓存读写)与命中 token(`cache.read`)。
208
212
  * 输出:缓存命中率百分比,向下截断到两位小数。
209
- * 预期:分母为 0 时返回 0;`100` 只代表真实全命中。
213
+ * 预期:分母为 0 时返回 0;结果夹在 0..100;`100` 只代表真实全命中。
210
214
  */
211
215
  export function hitRatePercent(input: number, cached: number): number {
212
- const total = input + cached
213
- if (total <= 0) return 0
214
- return Math.floor((cached / total) * 10_000) / 100
216
+ if (input <= 0 || cached <= 0) return 0
217
+ return Math.floor(Math.min(1, cached / input) * 10_000) / 100
215
218
  }