opencode-visual-cache 1.2.12 → 1.2.13

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.
@@ -1 +1 @@
1
- export declare const PLUGIN_VERSION = "1.2.12";
1
+ export declare const PLUGIN_VERSION = "1.2.13";
package/dist/_version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // auto-generated
2
- export const PLUGIN_VERSION = "1.2.12";
2
+ export const PLUGIN_VERSION = "1.2.13";
package/dist/index.js CHANGED
@@ -348,7 +348,19 @@ function TokenCachePanel(props) {
348
348
  void partVersion();
349
349
  // 自然追踪 messages 和 provider(SDK 数据就绪时自动重新执行)
350
350
  const msgs = props.api.state.session.messages(sid);
351
- let input = 0, read = 0, write = 0, output = 0, cost = 0, pid = "", mid = "";
351
+ const session = props.api.state.session.get(sid);
352
+ // 累计值优先使用 Session 聚合字段(数据库级,不受 sync 层 limit:100 截断)
353
+ // 若字段不存在(旧版本 SDK),降级到消息遍历累加
354
+ let input = session?.tokens?.input ?? 0;
355
+ let read = session?.tokens?.cache?.read ?? 0;
356
+ let write = session?.tokens?.cache?.write ?? 0;
357
+ let output = session?.tokens?.output ?? 0;
358
+ let cost = session?.cost ?? 0;
359
+ let pid = session?.model?.providerID ?? "";
360
+ let mid = session?.model?.id ?? "";
361
+ const fallbackTokens = session?.tokens == null;
362
+ const fallbackCost = session?.cost == null;
363
+ const fallbackModel = !pid || !mid;
352
364
  let prevMsgHitRate = -1, lastMsgHitRate = -1;
353
365
  for (const msg of msgs) {
354
366
  if (msg.role !== "assistant")
@@ -361,12 +373,16 @@ function TokenCachePanel(props) {
361
373
  prevMsgHitRate = lastMsgHitRate;
362
374
  lastMsgHitRate = (mrt / mit) * 100;
363
375
  }
364
- input += num(t.input);
365
- read += num(t.cache?.read);
366
- write += num(t.cache?.write);
367
- output += num(t.output);
368
- cost += num(msg.cost);
369
- if (msg.providerID && msg.modelID) {
376
+ if (fallbackTokens) {
377
+ input += num(t.input);
378
+ read += num(t.cache?.read);
379
+ write += num(t.cache?.write);
380
+ output += num(t.output);
381
+ }
382
+ if (fallbackCost) {
383
+ cost += num(msg.cost);
384
+ }
385
+ if (fallbackModel && msg.providerID && msg.modelID) {
370
386
  pid = msg.providerID;
371
387
  mid = msg.modelID;
372
388
  }
@@ -397,7 +413,7 @@ function TokenCachePanel(props) {
397
413
  let hasDistData = false;
398
414
  const loadedSkills = new Map();
399
415
  try {
400
- const session = props.api.state.session.get(sid), cfg = props.api.state.config;
416
+ const cfg = props.api.state.config;
401
417
  const agentName = String(session?.agent ?? cfg?.default_agent ?? "build");
402
418
  const agents = cfg?.agent;
403
419
  const agentCfg = agents?.[agentName];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-visual-cache",
3
- "version": "1.2.12",
3
+ "version": "1.2.13",
4
4
  "description": "OpenCode TUI plugin displaying real-time token cache hit rate in the sidebar",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/_version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // auto-generated
2
- export const PLUGIN_VERSION="1.2.12";
2
+ export const PLUGIN_VERSION="1.2.13";
package/src/index.tsx CHANGED
@@ -431,16 +431,37 @@ function TokenCachePanel(props: {
431
431
 
432
432
  // 自然追踪 messages 和 provider(SDK 数据就绪时自动重新执行)
433
433
  const msgs = props.api.state.session.messages(sid) as Message[]
434
- let input = 0, read = 0, write = 0, output = 0, cost = 0, pid = "", mid = ""
434
+ const session = props.api.state.session.get(sid)
435
+
436
+ // 累计值优先使用 Session 聚合字段(数据库级,不受 sync 层 limit:100 截断)
437
+ // 若字段不存在(旧版本 SDK),降级到消息遍历累加
438
+ let input = session?.tokens?.input ?? 0
439
+ let read = session?.tokens?.cache?.read ?? 0
440
+ let write = session?.tokens?.cache?.write ?? 0
441
+ let output = session?.tokens?.output ?? 0
442
+ let cost = session?.cost ?? 0
443
+ let pid = session?.model?.providerID ?? ""
444
+ let mid = session?.model?.id ?? ""
445
+
446
+ const fallbackTokens = session?.tokens == null
447
+ const fallbackCost = session?.cost == null
448
+ const fallbackModel = !pid || !mid
449
+
435
450
  let prevMsgHitRate = -1, lastMsgHitRate = -1
436
451
  for (const msg of msgs) {
437
452
  if (msg.role !== "assistant") continue
438
453
  const t = (msg as AssistantMessage).tokens; if (!t) continue
439
454
  const mit = num(t.input) + num(t.cache?.read), mrt = num(t.cache?.read)
440
455
  if (mit > 0) { prevMsgHitRate = lastMsgHitRate; lastMsgHitRate = (mrt / mit) * 100 }
441
- input += num(t.input); read += num(t.cache?.read); write += num(t.cache?.write); output += num(t.output)
442
- cost += num((msg as AssistantMessage).cost)
443
- if ((msg as AssistantMessage).providerID && (msg as AssistantMessage).modelID) { pid = (msg as AssistantMessage).providerID; mid = (msg as AssistantMessage).modelID }
456
+ if (fallbackTokens) {
457
+ input += num(t.input); read += num(t.cache?.read); write += num(t.cache?.write); output += num(t.output)
458
+ }
459
+ if (fallbackCost) {
460
+ cost += num((msg as AssistantMessage).cost)
461
+ }
462
+ if (fallbackModel && (msg as AssistantMessage).providerID && (msg as AssistantMessage).modelID) {
463
+ pid = (msg as AssistantMessage).providerID; mid = (msg as AssistantMessage).modelID
464
+ }
444
465
  }
445
466
  let saved = 0, inputRate = 0, cacheReadRate = 0, cacheWriteRate = 0
446
467
  if (read > 0 && pid && mid) for (const provider of props.api.state.provider) {
@@ -462,7 +483,7 @@ function TokenCachePanel(props: {
462
483
  let hasDistData = false
463
484
  const loadedSkills = new Map<string, { name: string; tokens: number }>()
464
485
  try {
465
- const session = props.api.state.session.get(sid), cfg = props.api.state.config as Record<string, unknown>
486
+ const cfg = props.api.state.config as Record<string, unknown>
466
487
  const agentName = String(session?.agent ?? (cfg as any)?.default_agent ?? "build")
467
488
  const agents = cfg?.agent as Record<string, unknown> | undefined
468
489
  const agentCfg = agents?.[agentName] as Record<string, unknown> | undefined