opencode-cache-hit 0.1.0 → 0.2.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/AGENTS.md +4 -2
- package/CONTRIBUTING.md +24 -8
- package/README.md +55 -22
- package/README.zh-CN.md +155 -92
- package/cache-hit.config.example.json +7 -2
- package/docs/assets/cache-hit-panel.v3.png +0 -0
- package/docs/en/design.md +30 -8
- package/docs/en/timeline-duplicate-writes.md +125 -0
- package/docs/en/timeline.md +26 -21
- package/docs/zh-CN/design.md +31 -9
- package/docs/zh-CN/timeline.md +28 -24
- package/package.json +1 -2
- package/scripts/README.md +64 -1
- package/scripts/plot-hit-rate.ts +4 -3
- package/scripts/timeline-dashboard.ts +728 -0
- package/src/agents-view.tsx +24 -10
- package/src/cache-ttl-view.tsx +128 -0
- package/src/format-cost.ts +83 -1
- package/src/format-model.ts +227 -0
- package/src/i18n.ts +18 -3
- package/src/load-config.ts +24 -5
- package/src/main-session-view.tsx +43 -3
- package/src/plugin-config.ts +59 -1
- package/src/plugin.tsx +4 -1
- package/src/pricing.ts +57 -0
- package/src/sidebar-host.tsx +13 -7
- package/src/stats.ts +6 -15
- package/src/timeline/collector.ts +40 -87
- package/src/timeline/records.ts +18 -29
- package/src/timeline/types.ts +3 -3
- package/src/timeline/writer.ts +5 -4
- package/src/tui-panel/README.md +2 -2
- package/src/tui-panel/README.zh-CN.md +2 -2
- package/src/tui-panel/components.tsx +31 -4
- package/src/tui-panel/index.ts +6 -1
- package/src/tui-panel/palette.ts +5 -0
- package/src/types.ts +16 -0
- package/src/use-cache-hit-metrics.ts +8 -9
- package/src/version.ts +4 -1
- package/src/widget.tsx +11 -3
- package/docs/assets/cache-hit-panel.png +0 -0
package/AGENTS.md
CHANGED
|
@@ -31,7 +31,8 @@ After moving or renaming exports: run full `bun test`; `tests/module-load.test.t
|
|
|
31
31
|
## Code conventions
|
|
32
32
|
|
|
33
33
|
- **Minimal diffs**; match existing naming and module boundaries.
|
|
34
|
-
- **Pure logic** in `stats.ts`, `timeline/`, `format-*.ts`, `tui-panel/layout.ts` — avoid pulling JSX into modules used by tests (import `layout.ts` / `palette.ts` directly, not `tui-panel/index.ts` when possible).
|
|
34
|
+
- **Pure logic** in `stats.ts`, `timeline/`, `format-*.ts`, `format-model.ts`, `tui-panel/layout.ts` — avoid pulling JSX into modules used by tests (import `layout.ts` / `palette.ts` directly, not `tui-panel/index.ts` when possible).
|
|
35
|
+
- **Sub-agent row UI**: `format-model.ts` + `agents-view.tsx`; behavior in design doc § Sub-agent row display / 子 session 行展示.
|
|
35
36
|
- **`PLUGIN_ROOT`** in `load-config.ts` is `fileURLToPath(new URL("..", import.meta.url))` — do **not** wrap with an extra `dirname` (breaks config path).
|
|
36
37
|
- **Sub-agent ids**: only from `session.list` overwrite in `child-session-sync.ts`; do not append via `session.get`.
|
|
37
38
|
- **Agents UI totals**: child sessions only; main session excluded by design (see design doc).
|
|
@@ -40,8 +41,9 @@ After moving or renaming exports: run full `bun test`; `tests/module-load.test.t
|
|
|
40
41
|
## Configuration
|
|
41
42
|
|
|
42
43
|
- Example: [cache-hit.config.example.json](cache-hit.config.example.json) — **included in npm** `files`.
|
|
43
|
-
- Runtime: `cache-hit.config.json` beside package root (
|
|
44
|
+
- Runtime: `~/.config/opencode/cache-hit.json` (preferred) or `cache-hit.config.json` beside package root (legacy fallback); **not** published; gitignored.
|
|
44
45
|
- Defaults: [src/plugin-config.ts](src/plugin-config.ts).
|
|
46
|
+
- Timeline log dir default: `~/.local/share/opencode/logs/cache-hit/`. Supports `~/` expansion in `timeline.dir`.
|
|
45
47
|
|
|
46
48
|
## npm publish
|
|
47
49
|
|
package/CONTRIBUTING.md
CHANGED
|
@@ -16,25 +16,41 @@ Coding agents: [AGENTS.md](AGENTS.md).
|
|
|
16
16
|
|
|
17
17
|
After `bun install`, a **pre-push** hook runs `bun test` (skip with `git push --no-verify`). **CI** (GitHub Actions on `main` and PRs) runs the same tests on the server.
|
|
18
18
|
|
|
19
|
-
## Configuration file
|
|
19
|
+
## Configuration file
|
|
20
20
|
|
|
21
|
-
The plugin reads
|
|
21
|
+
The plugin reads config from two locations (priority order):
|
|
22
|
+
|
|
23
|
+
1. **XDG**: `~/.config/opencode/cache-hit.json` (preferred — persists across plugin updates)
|
|
24
|
+
2. **Legacy**: `cache-hit.config.json` beside plugin root (backward compatible)
|
|
25
|
+
|
|
26
|
+
The XDG path is recommended for npm global installs; the legacy path still works for local installs and existing setups.
|
|
22
27
|
|
|
23
28
|
| File | In npm tarball? | Purpose |
|
|
24
29
|
|------|-----------------|--------|
|
|
25
30
|
| `cache-hit.config.example.json` | **Yes** | Template; copy and edit |
|
|
26
|
-
| `cache-hit.config.json` | **No** |
|
|
27
|
-
| `logs/` | **No** | Timeline output
|
|
31
|
+
| `cache-hit.config.json` | **No** | Legacy plugin-root overrides |
|
|
32
|
+
| `logs/` | **No** | Timeline output (default: `~/.local/share/opencode/logs/cache-hit/`) |
|
|
28
33
|
|
|
29
|
-
After
|
|
34
|
+
After installing:
|
|
30
35
|
|
|
31
36
|
```bash
|
|
32
|
-
|
|
33
|
-
cp cache-hit.config.example.json cache-hit.config.json
|
|
37
|
+
cp cache-hit.config.example.json ~/.config/opencode/cache-hit.json
|
|
34
38
|
# edit, then restart OpenCode
|
|
35
39
|
```
|
|
36
40
|
|
|
37
|
-
For a **local path** in
|
|
41
|
+
For a **local path** plugin in `~/.config/opencode/plugins/opencode-cache-hit/`, placing `cache-hit.config.json` in that folder also works (matches the legacy fallback).
|
|
42
|
+
|
|
43
|
+
## Versioning
|
|
44
|
+
|
|
45
|
+
Follow [Semantic Versioning](https://semver.org/):
|
|
46
|
+
|
|
47
|
+
| Change | Bump | Example |
|
|
48
|
+
|--------|------|---------|
|
|
49
|
+
| Bug fix (backward-compatible) | **patch** | `0.1.0` → `0.1.1` |
|
|
50
|
+
| New feature (backward-compatible) | **minor** | `0.1.0` → `0.2.0` |
|
|
51
|
+
| Breaking change | **major** | `0.2.0` → `1.0.0` |
|
|
52
|
+
|
|
53
|
+
Default changes (rate, paths) accompanied by new features → minor. Pure bugfixes only → patch.
|
|
38
54
|
|
|
39
55
|
## Publishing to npm
|
|
40
56
|
|
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ OpenCode **TUI sidebar plugin** for prompt **cache hit rate**, **token usage**,
|
|
|
6
6
|
|
|
7
7
|
**Languages:** English (this file) · [简体中文](README.zh-CN.md) · [Documentation](docs/README.md)
|
|
8
8
|
|
|
9
|
+

|
|
10
|
+
|
|
9
11
|
## Why this plugin
|
|
10
12
|
|
|
11
13
|
[opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) already covers **main-session** cache visualization (token distribution, savings, slash-driven settings). This project exists because that scope does not fit several real workflows:
|
|
@@ -19,18 +21,16 @@ Roadmap items (sidebar Timeline section, metric windows, nested sub-agents) are
|
|
|
19
21
|
|
|
20
22
|
## Acknowledgments
|
|
21
23
|
|
|
22
|
-
This plugin is **not** part of opencode-visual-cache. Its sidebar layout, panel components (`src/tui-panel/`), and coexistence patterns are **heavily inspired by** [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache). visual-cache focuses on **main-session context / token distribution**; cache-hit focuses on **per-turn metrics and sub-agent totals**.
|
|
23
|
-
|
|
24
|
-
## Screenshots
|
|
24
|
+
This plugin is **not** part of opencode-visual-cache. Its sidebar layout, panel components (`src/tui-panel/`), and coexistence patterns are **heavily inspired by** [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache). visual-cache focuses on **main-session context / token distribution**; cache-hit focuses on **per-turn metrics and sub-agent totals**.
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
The **cache TTL** feature (elapsed time display with color-coded status) is inspired by [opencode-cache-timer](https://github.com/nero-sensei/opencode-cache-timer) by nero-sensei. The original plugin provides a standalone sidebar countdown for prompt cache expiration; this plugin integrates the concept directly into the cache-hit panel.
|
|
27
27
|
|
|
28
28
|
## Features
|
|
29
29
|
|
|
30
30
|
- **Cache hit rate**: session total + **per-turn** rate with trend (↑ / ↓ / `-`) on the main block
|
|
31
31
|
- **Token breakdown**: cache read / write / miss / output (aligned rows with visual-cache)
|
|
32
|
-
- **Cost**: session cost with multi-currency config (`USD`, `CNY`, `EUR`, `GBP`, `JPY`)
|
|
33
|
-
- **Sub-agents**: **Agents** section rolls up **child sessions only** (scope labeled in UI)
|
|
32
|
+
- **Cost**: session cost with multi-currency config (`USD`, `CNY`, `EUR`, `GBP`, `JPY`); per-million rates and cache savings from provider config
|
|
33
|
+
- **Sub-agents**: **Agents** section rolls up **child sessions only** (scope labeled in UI); each row shows model name + session ID suffix with **vendor-tinted** label (cost in muted gray)
|
|
34
34
|
- **Main + Agents**: main block always shown; **Agents** section when sub-agents exist (foldable)
|
|
35
35
|
- **Collapsible sections**: Detail / Model (and Agents); theme-adaptive hit bar colors
|
|
36
36
|
- **i18n**: `display.lang` — `en` / `zh` / `auto` via config (no slash commands yet)
|
|
@@ -44,8 +44,8 @@ This plugin is **not** part of opencode-visual-cache. Its sidebar layout, panel
|
|
|
44
44
|
|---|----------------|-------------------|
|
|
45
45
|
| Main session context / token **distribution** estimate | Yes | No — use visual-cache |
|
|
46
46
|
| Per-role token breakdown (system / tools / …) | Yes | No |
|
|
47
|
-
| Cache **savings** estimate | Yes |
|
|
48
|
-
| Model **per-million** pricing from provider | Yes |
|
|
47
|
+
| Cache **savings** estimate | Yes | Yes (from provider pricing) |
|
|
48
|
+
| Model **per-million** pricing from provider | Yes | Yes (from SDK provider config) |
|
|
49
49
|
| **Slash commands** (`/cache-lang`, `/cache-currency`, …) | Yes | Config file only |
|
|
50
50
|
| Fold state in `api.kv` | Yes | In-session (not persisted) |
|
|
51
51
|
| Loaded **skills** panel | Yes | No |
|
|
@@ -57,7 +57,9 @@ This plugin is **not** part of opencode-visual-cache. Its sidebar layout, panel
|
|
|
57
57
|
|
|
58
58
|
### Option A: OpenCode command palette (recommended)
|
|
59
59
|
|
|
60
|
-
`Ctrl+P` → **install plugin** → `opencode-cache-hit@latest`
|
|
60
|
+
`Ctrl+P` → type **install plugin** → press `Tab` to switch scope to **global** (default is local) → type `opencode-cache-hit@latest` → press Enter.
|
|
61
|
+
|
|
62
|
+
Global plugins install to `~/.cache/opencode/packages/opencode-cache-hit@latest/`. Create config at `~/.config/opencode/cache-hit.json`:
|
|
61
63
|
|
|
62
64
|
### Option B: Manual
|
|
63
65
|
|
|
@@ -72,7 +74,7 @@ Create or edit `~/.config/opencode/tui.json` / `tui.jsonc`:
|
|
|
72
74
|
|
|
73
75
|
Local development: use `"./plugins/opencode-cache-hit"` instead of the npm name.
|
|
74
76
|
|
|
75
|
-
Copy `cache-hit.config.example.json` →
|
|
77
|
+
Copy `cache-hit.config.example.json` → `~/.config/opencode/cache-hit.json` (recommended) or next to the plugin root. **Restart OpenCode** after changing plugin code or config.
|
|
76
78
|
|
|
77
79
|
| Install | After update |
|
|
78
80
|
|---------|----------------|
|
|
@@ -118,7 +120,7 @@ Supported display currencies in config: `USD`, `CNY`, `EUR`, `GBP`, `JPY` (see `
|
|
|
118
120
|
| `panelBorder` | `true` | Border/padding |
|
|
119
121
|
| `mainHitLabel` | (i18n) | Optional override for the Hit row label |
|
|
120
122
|
|
|
121
|
-
**Agents** totals sum **child sessions only**, not the main session (see `agentsScopeHint`). Main session metrics stay in the block above; collapse **Agents** to save space.
|
|
123
|
+
**Agents** totals sum **child sessions only**, not the main session (see `agentsScopeHint`). Main session metrics stay in the block above; collapse **Agents** to save space. Per-child rows use the same model slug as the main **Model** line (truncated when the sidebar is narrow); see [docs/en/design.md](docs/en/design.md) § Sub-agent row display.
|
|
122
124
|
|
|
123
125
|
### Timeline logs (`timeline`, default off)
|
|
124
126
|
|
|
@@ -146,11 +148,53 @@ Per assistant turn → JSONL. [docs/en/timeline.md](docs/en/timeline.md) · [中
|
|
|
146
148
|
```bash
|
|
147
149
|
LOG=~/.config/opencode/plugins/opencode-cache-hit/logs/timeline-$(date +%Y-%m-%d).jsonl
|
|
148
150
|
tail -f "$LOG"
|
|
151
|
+
# time fields are ISO 8601 strings with local timezone (e.g. "2024-05-30T08:00:00.000+08:00")
|
|
149
152
|
jq -r 'select(.rootSessionId=="YOUR_ROOT") | [.created,.scope,.hitPercent,.cost]|@tsv' "$LOG"
|
|
150
153
|
```
|
|
151
154
|
|
|
152
155
|
Retention details: [Rotation and retention](docs/en/timeline.md#rotation-and-retention). Charts: [scripts/README.md](scripts/README.md).
|
|
153
156
|
|
|
157
|
+
### Cache TTL (`cacheTTL`, default on)
|
|
158
|
+
|
|
159
|
+
Shows how long the prompt cache has been alive. Color changes when exceeding TTL:
|
|
160
|
+
|
|
161
|
+
- Green: elapsed < TTL
|
|
162
|
+
- Yellow: TTL ≤ elapsed < 2×TTL
|
|
163
|
+
- Red: elapsed ≥ 2×TTL
|
|
164
|
+
|
|
165
|
+
```json
|
|
166
|
+
"cacheTTL": {
|
|
167
|
+
"enabled": true,
|
|
168
|
+
"providers": {
|
|
169
|
+
"anthropic": "5m",
|
|
170
|
+
"openai": "5m",
|
|
171
|
+
"deepseek": "2h",
|
|
172
|
+
"google": "1h"
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
| Field | Default | Meaning |
|
|
178
|
+
|-------|---------|---------|
|
|
179
|
+
| `enabled` | `true` | Master switch |
|
|
180
|
+
| `providers` | `{}` | TTL per provider (or `provider:model`). Human-readable: `30s`, `5m`, `1.5h` |
|
|
181
|
+
|
|
182
|
+
**Built-in defaults** (used when provider not in config):
|
|
183
|
+
|
|
184
|
+
| Provider | Default TTL | Source |
|
|
185
|
+
|----------|-------------|--------|
|
|
186
|
+
| anthropic | 5 min | [Anthropic docs](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) |
|
|
187
|
+
| openai | 5 min | [OpenAI docs](https://platform.openai.com/docs/guides/prompt-caching) |
|
|
188
|
+
| deepseek | 2 hours | [DeepSeek docs](https://api-docs.deepseek.com/guides/kv_cache) |
|
|
189
|
+
| google | 1 hour | [Google docs](https://ai.google.dev/api/caching) |
|
|
190
|
+
| xai | 5 min | [xAI docs](https://docs.x.ai/developers/advanced-api-usage/prompt-caching) |
|
|
191
|
+
| minimax | 5 min | [MiniMax docs](https://platform.minimax.io/docs/api-reference/text-prompt-caching) |
|
|
192
|
+
| xiaomi | 5 min | Implicit caching |
|
|
193
|
+
| qwen | 5 min | Implicit caching |
|
|
194
|
+
| moonshot | 5 min | Implicit caching |
|
|
195
|
+
|
|
196
|
+
**Default TTL**: 5 minutes for all providers not listed above. Color changes based on elapsed time vs TTL: green (< TTL), yellow (TTL-2x TTL), red (≥ 2x TTL).
|
|
197
|
+
|
|
154
198
|
## Updating
|
|
155
199
|
|
|
156
200
|
OpenCode may [cache plugins at first install](https://github.com/anomalyco/opencode/issues/6774) and not auto-refresh npm versions.
|
|
@@ -192,17 +236,6 @@ src/
|
|
|
192
236
|
tests/
|
|
193
237
|
```
|
|
194
238
|
|
|
195
|
-
## Configuration file
|
|
196
|
-
|
|
197
|
-
Copy `cache-hit.config.example.json` → `cache-hit.config.json` next to the plugin root (`PLUGIN_ROOT`, same directory as `index.tsx`). Restart OpenCode after edits.
|
|
198
|
-
|
|
199
|
-
```bash
|
|
200
|
-
cd ~/.cache/opencode/packages/opencode-cache-hit@latest # or your local plugin path
|
|
201
|
-
cp cache-hit.config.example.json cache-hit.config.json
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
Details (tarball contents, local paths): [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
205
|
-
|
|
206
239
|
## Development
|
|
207
240
|
|
|
208
241
|
```bash
|
package/README.zh-CN.md
CHANGED
|
@@ -2,106 +2,90 @@
|
|
|
2
2
|
|
|
3
3
|
[](LICENSE)
|
|
4
4
|
|
|
5
|
-
OpenCode **TUI 侧边栏插件**:展示 prompt cache 命中率、token 用量与成本;**主 session + 子 agent**
|
|
5
|
+
OpenCode **TUI 侧边栏插件**:展示 prompt cache 命中率、token 用量与成本;**主 session + 子 agent** 同屏汇总(默认独立使用)。可选与 [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) 共存。
|
|
6
6
|
|
|
7
7
|
**语言:** [English](README.md) · 简体中文(本页)· [文档索引](docs/README.md)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+

|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## 项目意图
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
2. **一场对话一块面板** — 主 session 与子 agent 同屏;**Agents** 段可折叠收起。
|
|
15
|
-
3. **离开 TUI 的分析** — 可选 **timeline JSONL**(按 assistant 轮次),便于画命中率曲线、jq 对账,而不去扒平台日志。
|
|
16
|
-
4. **可复用 TUI 骨架** — `src/tui-panel/` 抽出来,方便做其它侧栏插件。
|
|
13
|
+
[opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) 已经很好地覆盖**主 session** 的 cache 可视化(Token 分布、节省估算、斜杠命令配置等)。本项目的存在是因为其范围无法满足以下几种实际工作流:
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
1. **子 agent 可观测性** — OpenCode 会为 Task / explore agent 创建子 session;你需要**汇总**各子会话的 cache、token 和费用,而非仅主线程。
|
|
16
|
+
2. **一场对话一块面板** — 主 session 的命中率/token/费用**与**可折叠 **Agents** 段(子 agent 汇总)同屏展示。
|
|
17
|
+
3. **离开 TUI 的分析** — 可选 **timeline JSONL**(每 assistant 轮次),用于绘图、jq 对账,无需扒取平台日志。
|
|
18
|
+
4. **可复用 TUI 组件** — `src/tui-panel/` 被提取出来,方便其他侧边栏插件复用与 visual-cache 相同的布局语言。
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
后续规划(侧栏 Timeline 段、指标窗口、嵌套子 agent)见 [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md) 和 [docs/zh-CN/design.md](docs/zh-CN/design.md)。
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
## 致谢
|
|
23
23
|
|
|
24
|
-
- visual-cache
|
|
25
|
-
- cache-hit:**按轮次指标、成本、子 agent 汇总**
|
|
24
|
+
本插件**并非** opencode-visual-cache 的一部分。其侧栏布局、面板组件(`src/tui-panel/`)及共存策略**大量借鉴**自 [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache)。visual-cache 关注**主 session 上下文 / token 分布**;cache-hit 关注**按轮次指标和子 agent 汇总**。
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
## 截图
|
|
30
|
-
|
|
31
|
-

|
|
32
|
-
|
|
33
|
-
## 文档
|
|
34
|
-
|
|
35
|
-
| 读者 | English | 中文 |
|
|
36
|
-
|------|---------|------|
|
|
37
|
-
| 使用者 | [README.md](README.md) | 本文 |
|
|
38
|
-
| 维护者 | [docs/en/design.md](docs/en/design.md) | [docs/zh-CN/design.md](docs/zh-CN/design.md) |
|
|
39
|
-
| 时间轴 / JSONL | [docs/en/timeline.md](docs/en/timeline.md) | [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md) |
|
|
40
|
-
| TUI 面板框架 | [src/tui-panel/README.md](src/tui-panel/README.md) | [src/tui-panel/README.zh-CN.md](src/tui-panel/README.zh-CN.md) |
|
|
41
|
-
| 贡献 / npm | [CONTRIBUTING.md](CONTRIBUTING.md) | — |
|
|
42
|
-
| AI 维护 | [AGENTS.md](AGENTS.md) | — |
|
|
43
|
-
| 索引 | [docs/README.md](docs/README.md) | |
|
|
26
|
+
**缓存存活时间**功能(显示已缓存时长 + 颜色状态)借鉴自 [opencode-cache-timer](https://github.com/nero-sensei/opencode-cache-timer)(作者:nero-sensei)。原插件提供独立侧边栏倒计时;本插件将该概念直接集成到 cache-hit 面板中。
|
|
44
27
|
|
|
45
28
|
## 功能一览
|
|
46
29
|
|
|
47
|
-
- **命中率**:会话累计 +
|
|
48
|
-
- **Token
|
|
49
|
-
- **费用**:多币种配置(`USD` / `CNY` / `EUR` / `GBP` / `JPY
|
|
50
|
-
- **子 agent**:**Agents** 段仅汇总**子 session**(UI
|
|
51
|
-
- **主
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
**默认独立使用**(主 session + 子 agent 同面板)。版式借鉴 visual-cache,**不依赖**该插件。
|
|
57
|
-
|
|
58
|
-
| | visual-cache | cache-hit |
|
|
59
|
-
|---|----------------|-----------|
|
|
60
|
-
| 主 session 上下文 / Token **分布**估算 | 有 | 无 |
|
|
61
|
-
| 按角色 Token 分布、缓存**节省**、百万 token **单价** | 有 | 无 |
|
|
62
|
-
| **斜杠命令**改配置 | 有 | 仅配置文件 |
|
|
63
|
-
| **子 agent** 汇总 | 无 | **有** |
|
|
64
|
-
| 按次 **JSONL** | 无 | 可选 |
|
|
30
|
+
- **命中率**:会话累计 + **单轮**命中率与趋势(↑ / ↓ / `-`)
|
|
31
|
+
- **Token 明细**:缓存读 / 写 / 未命中 / 输出(对齐 visual-cache 的行布局)
|
|
32
|
+
- **费用**:多币种配置(`USD` / `CNY` / `EUR` / `GBP` / `JPY`);从 provider 配置读取百万 token 单价及缓存节省
|
|
33
|
+
- **子 agent**:**Agents** 段仅汇总**子 session**(UI 有范围标注);每行显示模型名 + session ID 后缀,**label 按厂商近似品牌色**,金额为灰色
|
|
34
|
+
- **主 + Agents**:主块始终显示;有子 agent 时出现可折叠的 **Agents** 段
|
|
35
|
+
- **可折叠段落**:Detail / Model(以及 Agents);主题自适应的命中率条颜色
|
|
36
|
+
- **国际化**:`display.lang` — `en` / `zh` / `auto`(配置文件,暂无斜杠命令)
|
|
37
|
+
- **时间轴**(可选):按天 JSONL,每 assistant 轮次一条,可用于 `jq` / 脚本分析
|
|
65
38
|
|
|
66
|
-
|
|
39
|
+
## 与 [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) 对比
|
|
67
40
|
|
|
68
|
-
|
|
41
|
+
**默认独立使用**(主 session + 子 agent 同面板)。布局借鉴 visual-cache,**不依赖**该插件。
|
|
69
42
|
|
|
70
|
-
|
|
43
|
+
| | visual-cache | opencode-cache-hit |
|
|
44
|
+
|---|----------------|-------------------|
|
|
45
|
+
| 主 session 上下文 / token **分布**估算 | 有 | 无(使用 visual-cache) |
|
|
46
|
+
| 按角色 token 分布(system / tools / …) | 有 | 无 |
|
|
47
|
+
| 缓存**节省**估算 | 有 | 有(从 provider 定价计算) |
|
|
48
|
+
| 模型**百万 token** 单价 | 有 | 有(从 SDK provider 配置读取) |
|
|
49
|
+
| **斜杠命令**(`/cache-lang`, `/cache-currency`, …) | 有 | 仅配置文件 |
|
|
50
|
+
| 折叠状态持久化(`api.kv`) | 有 | 仅内存(不持久) |
|
|
51
|
+
| **已加载 skill** 面板 | 有 | 无 |
|
|
52
|
+
| **子 agent** 会话汇总 | 无 | **有** |
|
|
53
|
+
| **合并**命中率(主 + 子) | 无 | 有子 agent 时显示 |
|
|
54
|
+
| 按次 **JSONL** 导出 | 无 | 可选 `timeline` |
|
|
71
55
|
|
|
72
|
-
|
|
73
|
-
- 侧边栏 **Cache Hit** 面板(布局对齐 visual-cache)
|
|
74
|
-
- 成本展示币种换算(默认 USD 成本 → CNY 显示)
|
|
56
|
+
## 快速开始
|
|
75
57
|
|
|
76
|
-
|
|
58
|
+
### 方式 A:OpenCode 命令面板(推荐)
|
|
77
59
|
|
|
78
|
-
|
|
79
|
-
- 修改 OpenCode 计费;`msg.cost` 仍按 `opencode.json` 美元单价
|
|
60
|
+
`Ctrl+P` → 输入 **install plugin** → 按 `Tab` 切换范围为 **global**(默认是 local)→ 输入 `opencode-cache-hit@latest` → 回车。
|
|
80
61
|
|
|
81
|
-
|
|
62
|
+
全局插件安装到 `~/.cache/opencode/packages/opencode-cache-hit@latest/`。在 `~/.config/opencode/cache-hit.json` 创建配置:
|
|
82
63
|
|
|
83
|
-
|
|
64
|
+
### 方式 B:手动
|
|
84
65
|
|
|
85
|
-
|
|
66
|
+
创建或编辑 `~/.config/opencode/tui.json` / `tui.jsonc`:
|
|
86
67
|
|
|
87
|
-
```
|
|
68
|
+
```jsonc
|
|
88
69
|
{
|
|
89
|
-
"
|
|
70
|
+
"$schema": "https://opencode.ai/tui.json",
|
|
71
|
+
"plugin": ["opencode-cache-hit@latest"]
|
|
90
72
|
}
|
|
91
73
|
```
|
|
92
74
|
|
|
93
|
-
|
|
75
|
+
本地开发:将 npm 名称替换为 `"./plugins/opencode-cache-hit"`。
|
|
76
|
+
|
|
77
|
+
复制 `cache-hit.config.example.json` → `~/.config/opencode/cache-hit.json`(推荐)或放在插件根目录旁。更改插件代码或配置后**重启 OpenCode**。
|
|
94
78
|
|
|
95
79
|
| 安装方式 | 更新后 |
|
|
96
80
|
|----------|--------|
|
|
97
|
-
|
|
|
98
|
-
| npm `@latest` |
|
|
81
|
+
| 本地 `./plugins/...` | 完全重启 |
|
|
82
|
+
| npm `@latest` | 重启;如果 UI 未更新,删除 `~/.cache/opencode/packages/opencode-cache-hit@latest` |
|
|
99
83
|
|
|
100
|
-
加载失败:`~/.local/share/opencode/log
|
|
84
|
+
加载失败:`~/.local/share/opencode/log/`(搜索 `cache-hit` 或 `failed to load tui plugin`)。
|
|
101
85
|
|
|
102
86
|
## 配置
|
|
103
87
|
|
|
104
|
-
###
|
|
88
|
+
### 成本展示(USD → CNY 示例)
|
|
105
89
|
|
|
106
90
|
```json
|
|
107
91
|
{
|
|
@@ -111,32 +95,41 @@ OpenCode **TUI 侧边栏插件**:展示 prompt cache 命中率、token 用量
|
|
|
111
95
|
}
|
|
112
96
|
```
|
|
113
97
|
|
|
114
|
-
|
|
98
|
+
| 字段 | 含义 |
|
|
99
|
+
|-------|-------|
|
|
100
|
+
| `costUnit` | `msg.cost` 的币种(通常为 `USD`) |
|
|
101
|
+
| `currency` | 侧边栏展示币种 |
|
|
102
|
+
| `rate` | `costUnit` → `currency` 的汇率 |
|
|
103
|
+
|
|
104
|
+
当无需换算时使用 `"currency": "USD", "costUnit": "USD"`。
|
|
105
|
+
|
|
106
|
+
支持的展示币种:`USD`、`CNY`、`EUR`、`GBP`、`JPY`(见 `cache-hit.config.example.json`)。暂未实现类似 visual-cache 的 `/cache-currency` 斜杠命令。
|
|
115
107
|
|
|
116
108
|
### 展示(`display`)
|
|
117
109
|
|
|
118
110
|
```json
|
|
119
111
|
"display": {
|
|
120
|
-
"lang": "
|
|
112
|
+
"lang": "en",
|
|
121
113
|
"panelBorder": true
|
|
122
114
|
}
|
|
123
115
|
```
|
|
124
116
|
|
|
125
117
|
| 字段 | 默认 | 含义 |
|
|
126
|
-
|
|
118
|
+
|-------|---------|------|
|
|
127
119
|
| `lang` | `"en"` | `en` / `zh` / `auto` |
|
|
128
120
|
| `panelBorder` | `true` | 外框与内边距 |
|
|
129
|
-
| `mainHitLabel` | (i18n) | 可选,覆盖 Hit
|
|
121
|
+
| `mainHitLabel` | (i18n) | 可选,覆盖 Hit 行标签 |
|
|
130
122
|
|
|
131
|
-
**Agents
|
|
123
|
+
**Agents** 段仅汇总**子 session**,不含主 session(详见 `agentsScopeHint`)。主 session 指标始终在上方块中;折叠 **Agents** 可节省空间。各子 session 行与主块 **Model** 行同源模型名(侧栏窄时会截断);详见 [docs/zh-CN/design.md](docs/zh-CN/design.md)「子 session 行展示」。
|
|
132
124
|
|
|
133
125
|
### 时间轴日志(`timeline`,默认关闭)
|
|
134
126
|
|
|
135
|
-
|
|
127
|
+
每 assistant 轮次 → JSONL。详见 [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md)。
|
|
136
128
|
|
|
137
129
|
```json
|
|
138
130
|
"timeline": {
|
|
139
131
|
"enabled": true,
|
|
132
|
+
"dir": "",
|
|
140
133
|
"rotateMaxBytes": 16777216,
|
|
141
134
|
"retainRotated": 5,
|
|
142
135
|
"maxAgeDays": 30,
|
|
@@ -144,30 +137,104 @@ OpenCode **TUI 侧边栏插件**:展示 prompt cache 命中率、token 用量
|
|
|
144
137
|
}
|
|
145
138
|
```
|
|
146
139
|
|
|
147
|
-
| 字段 |
|
|
148
|
-
|
|
149
|
-
| `
|
|
150
|
-
| `
|
|
151
|
-
| `
|
|
140
|
+
| 字段 | 默认 | 含义 |
|
|
141
|
+
|-------|---------|-------|
|
|
142
|
+
| `enabled` | `false` | 总开关 |
|
|
143
|
+
| `dir` | `""` | `logs/timeline-YYYY-MM-DD.jsonl`(插件根目录下) |
|
|
144
|
+
| `rotateMaxBytes` | `0` | 同日大小轮转至 `.jsonl.1` |
|
|
145
|
+
| `retainRotated` | `5` | 每日保留的轮转备份数 |
|
|
146
|
+
| `maxLogFiles` | `0` | 文件数量上限;删除**最早**日志 |
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
LOG=~/.config/opencode/plugins/opencode-cache-hit/logs/timeline-$(date +%Y-%m-%d).jsonl
|
|
150
|
+
tail -f "$LOG"
|
|
151
|
+
# 时间字段为 ISO 8601 字符串,含本地时区(如 "2024-05-30T08:00:00.000+08:00")
|
|
152
|
+
jq -r 'select(.rootSessionId=="YOUR_ROOT") | [.created,.scope,.hitPercent,.cost]|@tsv' "$LOG"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
轮转与清理:[docs/zh-CN/timeline.md#轮转与清理](docs/zh-CN/timeline.md#轮转与清理)。图表工具:[scripts/README.md](scripts/README.md)。
|
|
156
|
+
|
|
157
|
+
### 缓存 TTL(`cacheTTL`,默认开启)
|
|
158
|
+
|
|
159
|
+
显示 prompt cache 已存活时间。超过 TTL 时颜色变化:
|
|
152
160
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
161
|
+
- 绿色:已存活 < TTL
|
|
162
|
+
- 黄色:TTL ≤ 已存活 < 2×TTL
|
|
163
|
+
- 红色:已存活 ≥ 2×TTL
|
|
164
|
+
|
|
165
|
+
```json
|
|
166
|
+
"cacheTTL": {
|
|
167
|
+
"enabled": true,
|
|
168
|
+
"providers": {
|
|
169
|
+
"anthropic": "5m",
|
|
170
|
+
"openai": "5m",
|
|
171
|
+
"deepseek": "2h",
|
|
172
|
+
"google": "1h"
|
|
173
|
+
}
|
|
174
|
+
}
|
|
157
175
|
```
|
|
158
176
|
|
|
159
|
-
|
|
177
|
+
| 字段 | 默认 | 含义 |
|
|
178
|
+
|-------|---------|-------|
|
|
179
|
+
| `enabled` | `true` | 总开关 |
|
|
180
|
+
| `providers` | `{}` | 每个 provider 的 TTL(或 `provider:model`)。可读格式:`30s`、`5m`、`1.5h` |
|
|
181
|
+
|
|
182
|
+
**内置默认值**(配置中未指定时使用):
|
|
183
|
+
|
|
184
|
+
| Provider | 默认 TTL | 来源 |
|
|
185
|
+
|----------|----------|------|
|
|
186
|
+
| anthropic | 5 分钟 | [Anthropic 文档](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) |
|
|
187
|
+
| openai | 5 分钟 | [OpenAI 文档](https://platform.openai.com/docs/guides/prompt-caching) |
|
|
188
|
+
| deepseek | 2 小时 | [DeepSeek 文档](https://api-docs.deepseek.com/guides/kv_cache) |
|
|
189
|
+
| google | 1 小时 | [Google 文档](https://ai.google.dev/api/caching) |
|
|
190
|
+
| xai | 5 分钟 | [xAI 文档](https://docs.x.ai/developers/advanced-api-usage/prompt-caching) |
|
|
191
|
+
| minimax | 5 分钟 | [MiniMax 文档](https://platform.minimax.io/docs/api-reference/text-prompt-caching) |
|
|
192
|
+
| xiaomi | 5 分钟 | 隐式缓存 |
|
|
193
|
+
| qwen | 5 分钟 | 隐式缓存 |
|
|
194
|
+
| moonshot | 5 分钟 | 隐式缓存 |
|
|
195
|
+
|
|
196
|
+
**默认 TTL**:上表未列出的 provider 均为 5 分钟。颜色基于已存活时间与 TTL 的比值:绿色(< TTL)、黄色(TTL–2×TTL)、红色(≥ 2×TTL)。
|
|
160
197
|
|
|
161
|
-
##
|
|
198
|
+
## 更新
|
|
162
199
|
|
|
163
|
-
|
|
200
|
+
OpenCode 可能在首次安装时[缓存插件](https://github.com/anomalyco/opencode/issues/6774)且不自动刷新 npm 版本。
|
|
164
201
|
|
|
165
202
|
```bash
|
|
166
|
-
|
|
167
|
-
cp cache-hit.config.example.json cache-hit.config.json
|
|
203
|
+
rm -rf ~/.cache/opencode/packages/opencode-cache-hit@latest
|
|
168
204
|
```
|
|
169
205
|
|
|
170
|
-
|
|
206
|
+
然后通过 `Ctrl+P` → install plugin 重装并**重启 OpenCode**。
|
|
207
|
+
|
|
208
|
+
## 兼容性
|
|
209
|
+
|
|
210
|
+
模型无关:任何在消息中暴露 assistant `tokens` / `cost` 的 OpenCode provider 均可(DeepSeek、Claude、GPT 等)。数据来自 OpenCode session API,与 visual-cache 一致。
|
|
211
|
+
|
|
212
|
+
**需要**支持 TUI 插件槽位的 OpenCode(`@opencode-ai/plugin` ≥ 1.14)。可与 visual-cache 共存;运行时除 `package.json` 中声明的 peer 依赖外无额外依赖。
|
|
213
|
+
|
|
214
|
+
## 文档索引
|
|
215
|
+
|
|
216
|
+
| 读者 | English | 中文 |
|
|
217
|
+
|----------|---------|------|
|
|
218
|
+
| 使用者 | [README.md](README.md) | 本文 |
|
|
219
|
+
| 维护者 | [docs/en/design.md](docs/en/design.md) | [docs/zh-CN/design.md](docs/zh-CN/design.md) |
|
|
220
|
+
| 时间轴 / JSONL | [docs/en/timeline.md](docs/en/timeline.md) | [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md) |
|
|
221
|
+
| TUI 面板复用 | [src/tui-panel/README.md](src/tui-panel/README.md) | [src/tui-panel/README.zh-CN.md](src/tui-panel/README.zh-CN.md) |
|
|
222
|
+
| 贡献 / npm | [CONTRIBUTING.md](CONTRIBUTING.md) | — |
|
|
223
|
+
| AI 维护指南 | [AGENTS.md](AGENTS.md) | — |
|
|
224
|
+
| 总索引 | [docs/README.md](docs/README.md) | |
|
|
225
|
+
|
|
226
|
+
## 项目结构
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
index.tsx
|
|
230
|
+
cache-hit.config.example.json
|
|
231
|
+
src/
|
|
232
|
+
plugin.tsx # sidebar_content 插槽
|
|
233
|
+
sidebar-host.tsx # 消息、子会话同步、timeline
|
|
234
|
+
widget.tsx
|
|
235
|
+
stats.ts / timeline/ / tui-panel/
|
|
236
|
+
tests/
|
|
237
|
+
```
|
|
171
238
|
|
|
172
239
|
## 开发
|
|
173
240
|
|
|
@@ -175,11 +242,7 @@ npm 打包、本地路径等说明见 [CONTRIBUTING.md](CONTRIBUTING.md)(英
|
|
|
175
242
|
bun test
|
|
176
243
|
```
|
|
177
244
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
## 更新
|
|
181
|
-
|
|
182
|
-
若 npm 版本不刷新,参见 [OpenCode #6774](https://github.com/anomalyco/opencode/issues/6774),删除 `~/.cache/opencode/packages/opencode-cache-hit@latest` 后重装并重启。
|
|
245
|
+
环境搭建、PR 规范、npm 发布见 [CONTRIBUTING.md](CONTRIBUTING.md)。架构:[docs/zh-CN/design.md](docs/zh-CN/design.md)。
|
|
183
246
|
|
|
184
247
|
## License
|
|
185
248
|
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
"$comment": "lang: en | zh | auto (system locale). Default en.",
|
|
3
3
|
"currency": "CNY",
|
|
4
4
|
"costUnit": "USD",
|
|
5
|
-
"rate":
|
|
5
|
+
"rate": 6.77,
|
|
6
6
|
"display": {
|
|
7
7
|
"lang": "en",
|
|
8
8
|
"panelBorder": true
|
|
9
9
|
},
|
|
10
10
|
"timeline": {
|
|
11
11
|
"enabled": false,
|
|
12
|
-
"dir": "",
|
|
12
|
+
"dir": "~/.local/share/opencode/logs/cache-hit",
|
|
13
13
|
"flushIncomplete": false,
|
|
14
14
|
"logSummaryMessages": true,
|
|
15
15
|
"maxMemoryRows": 50,
|
|
@@ -18,5 +18,10 @@
|
|
|
18
18
|
"retainRotated": 5,
|
|
19
19
|
"maxAgeDays": 30,
|
|
20
20
|
"maxLogFiles": 20
|
|
21
|
+
},
|
|
22
|
+
"cacheTTL": {
|
|
23
|
+
"enabled": true,
|
|
24
|
+
"providers": {
|
|
25
|
+
}
|
|
21
26
|
}
|
|
22
27
|
}
|
|
Binary file
|