opencode-cache-hit 0.1.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/AGENTS.md +67 -0
- package/CONTRIBUTING.md +73 -0
- package/LICENSE +21 -0
- package/README.md +216 -0
- package/README.zh-CN.md +186 -0
- package/cache-hit.config.example.json +22 -0
- package/docs/README.md +8 -0
- package/docs/assets/cache-hit-panel.png +0 -0
- package/docs/en/design.md +228 -0
- package/docs/en/timeline.md +253 -0
- package/docs/zh-CN/design.md +229 -0
- package/docs/zh-CN/timeline.md +301 -0
- package/index.tsx +1 -0
- package/package.json +71 -0
- package/scripts/README.md +39 -0
- package/scripts/plot-hit-rate.ts +222 -0
- package/src/agents-view.tsx +55 -0
- package/src/cache-hit-rows.tsx +68 -0
- package/src/child-session-sync.ts +93 -0
- package/src/format-cache-ui.ts +21 -0
- package/src/format-cost.ts +90 -0
- package/src/format-tokens.ts +5 -0
- package/src/i18n.ts +82 -0
- package/src/load-config.ts +29 -0
- package/src/main-session-view.tsx +76 -0
- package/src/message-timing.ts +35 -0
- package/src/plugin-config.ts +116 -0
- package/src/plugin.tsx +33 -0
- package/src/session-list.ts +11 -0
- package/src/sidebar-host.tsx +121 -0
- package/src/stats.ts +141 -0
- package/src/timeline/collector.ts +156 -0
- package/src/timeline/records.ts +73 -0
- package/src/timeline/rotation.ts +47 -0
- package/src/timeline/types.ts +22 -0
- package/src/timeline/writer.ts +134 -0
- package/src/tui-panel/README.md +78 -0
- package/src/tui-panel/README.zh-CN.md +76 -0
- package/src/tui-panel/components.tsx +163 -0
- package/src/tui-panel/index.ts +41 -0
- package/src/tui-panel/layout.ts +107 -0
- package/src/tui-panel/palette.ts +93 -0
- package/src/tui-panel/use-panel-layout.ts +69 -0
- package/src/types.ts +71 -0
- package/src/use-cache-hit-metrics.ts +103 -0
- package/src/version.ts +1 -0
- package/src/widget.tsx +117 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# AGENTS.md — maintainer & AI assistant guide
|
|
2
|
+
|
|
3
|
+
Instructions for humans and coding agents working in **opencode-cache-hit**. End-user docs: [README.md](README.md) · [README.zh-CN.md](README.zh-CN.md).
|
|
4
|
+
|
|
5
|
+
## Project purpose
|
|
6
|
+
|
|
7
|
+
OpenCode TUI sidebar plugin: **cache hit rate**, **tokens**, **cost**, with **sub-agent (child session)** rollup. Optional per-call **JSONL timeline** (`timeline.enabled`).
|
|
8
|
+
|
|
9
|
+
**Not** a fork of [opencode-visual-cache](https://github.com/Hotakus/opencode-visual-cache). UI patterns and `src/tui-panel/` are **heavily inspired by** visual-cache; product focus differs (see README comparison table).
|
|
10
|
+
|
|
11
|
+
## Documentation map
|
|
12
|
+
|
|
13
|
+
| Topic | English | 中文 |
|
|
14
|
+
|-------|---------|------|
|
|
15
|
+
| Users | [README.md](README.md) | [README.zh-CN.md](README.zh-CN.md) |
|
|
16
|
+
| Architecture | [docs/en/design.md](docs/en/design.md) | [docs/zh-CN/design.md](docs/zh-CN/design.md) |
|
|
17
|
+
| Timeline / JSONL | [docs/en/timeline.md](docs/en/timeline.md) | [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md) |
|
|
18
|
+
| TUI panel | [src/tui-panel/README.md](src/tui-panel/README.md) | [src/tui-panel/README.zh-CN.md](src/tui-panel/README.zh-CN.md) |
|
|
19
|
+
| Contributing / npm | [CONTRIBUTING.md](CONTRIBUTING.md) | — |
|
|
20
|
+
| Index | [docs/README.md](docs/README.md) | |
|
|
21
|
+
|
|
22
|
+
## Commands
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bun test # full unit + module-load smoke
|
|
26
|
+
bun run check # same as test
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
After moving or renaming exports: run full `bun test`; `tests/module-load.test.ts` imports the real consumer graph.
|
|
30
|
+
|
|
31
|
+
## Code conventions
|
|
32
|
+
|
|
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).
|
|
35
|
+
- **`PLUGIN_ROOT`** in `load-config.ts` is `fileURLToPath(new URL("..", import.meta.url))` — do **not** wrap with an extra `dirname` (breaks config path).
|
|
36
|
+
- **Sub-agent ids**: only from `session.list` overwrite in `child-session-sync.ts`; do not append via `session.get`.
|
|
37
|
+
- **Agents UI totals**: child sessions only; main session excluded by design (see design doc).
|
|
38
|
+
- Comments only for non-obvious behavior.
|
|
39
|
+
|
|
40
|
+
## Configuration
|
|
41
|
+
|
|
42
|
+
- Example: [cache-hit.config.example.json](cache-hit.config.example.json) — **included in npm** `files`.
|
|
43
|
+
- Runtime: `cache-hit.config.json` beside package root (`CONFIG_PATH` in `load-config.ts`); **not** published; gitignored.
|
|
44
|
+
- Defaults: [src/plugin-config.ts](src/plugin-config.ts).
|
|
45
|
+
|
|
46
|
+
## npm publish
|
|
47
|
+
|
|
48
|
+
- Tarball = `package.json` `"files"` only (source TSX, example config, docs — no `tests/`, `logs/`, user config).
|
|
49
|
+
- No build step required for OpenCode `./tui` entry (`index.tsx`).
|
|
50
|
+
- Run `bun test` before `npm publish`; see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
51
|
+
|
|
52
|
+
## OpenCode integration
|
|
53
|
+
|
|
54
|
+
- Entry: [index.tsx](index.tsx) → [src/plugin.tsx](src/plugin.tsx).
|
|
55
|
+
- Slot: `sidebar_content`, `order: 56` (near visual-cache).
|
|
56
|
+
- Peers: `@opencode-ai/plugin`, `@opencode-ai/sdk`, `@opentui/solid`, `solid-js` (see [package.json](package.json)).
|
|
57
|
+
|
|
58
|
+
## Git / safety
|
|
59
|
+
|
|
60
|
+
- Do not `git reset --hard`, `checkout --`, or force-push unless the user explicitly asks.
|
|
61
|
+
- Do not commit unless asked; do not commit `logs/` or personal `cache-hit.config.json` if gitignored.
|
|
62
|
+
|
|
63
|
+
## When adding features
|
|
64
|
+
|
|
65
|
+
- Prefer config file over slash commands unless parity with visual-cache is an explicit goal.
|
|
66
|
+
- Timeline changes: update **both** `docs/en/timeline.md` and `docs/zh-CN/timeline.md`.
|
|
67
|
+
- User-facing README changes: update **English README** and mirror key points in **README.zh-CN.md**.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Development, packaging, and npm release notes for **opencode-cache-hit**. User install and configuration: [README.md](README.md) · [README.zh-CN.md](README.zh-CN.md).
|
|
4
|
+
|
|
5
|
+
## Development
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun install # dev: solid-js; peers resolved by OpenCode at runtime
|
|
9
|
+
bun test
|
|
10
|
+
bun run check
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Architecture: [docs/en/design.md](docs/en/design.md). After refactors, `tests/module-load.test.ts` catches broken import paths.
|
|
14
|
+
|
|
15
|
+
Coding agents: [AGENTS.md](AGENTS.md).
|
|
16
|
+
|
|
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
|
+
|
|
19
|
+
## Configuration file (local)
|
|
20
|
+
|
|
21
|
+
The plugin reads **`cache-hit.config.json`** from the package root (`PLUGIN_ROOT`, same directory as `index.tsx`). Code defaults apply if the file is missing.
|
|
22
|
+
|
|
23
|
+
| File | In npm tarball? | Purpose |
|
|
24
|
+
|------|-----------------|--------|
|
|
25
|
+
| `cache-hit.config.example.json` | **Yes** | Template; copy and edit |
|
|
26
|
+
| `cache-hit.config.json` | **No** | Your overrides (gitignored here) |
|
|
27
|
+
| `logs/` | **No** | Timeline output when enabled |
|
|
28
|
+
|
|
29
|
+
After OpenCode installs the package (npm or cache):
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
cd ~/.cache/opencode/packages/opencode-cache-hit@latest
|
|
33
|
+
cp cache-hit.config.example.json cache-hit.config.json
|
|
34
|
+
# edit, then restart OpenCode
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For a **local path** in `tui.json`, put `cache-hit.config.json` in that folder (e.g. `~/.config/opencode/plugins/opencode-cache-hit/`).
|
|
38
|
+
|
|
39
|
+
## Publishing to npm
|
|
40
|
+
|
|
41
|
+
Published **as source** (TypeScript / TSX)—**no `dist/` build**. OpenCode loads `index.tsx` via the `./tui` export (same pattern as many TUI plugins).
|
|
42
|
+
|
|
43
|
+
**Tarball contents** — `package.json` → `"files"`:
|
|
44
|
+
|
|
45
|
+
- `index.tsx`, `src/`, docs, `cache-hit.config.example.json`, READMEs, `AGENTS.md`, `CONTRIBUTING.md`
|
|
46
|
+
- Not included: `tests/`, `logs/`, personal `cache-hit.config.json`, `node_modules/`
|
|
47
|
+
|
|
48
|
+
**npmjs.com page**
|
|
49
|
+
|
|
50
|
+
| Source | Effect |
|
|
51
|
+
|--------|--------|
|
|
52
|
+
| `description` | One-line summary (keep under ~180 chars) |
|
|
53
|
+
| `README.md` | Main package page |
|
|
54
|
+
| `keywords` | Search tags |
|
|
55
|
+
| `LICENSE` | License tab |
|
|
56
|
+
|
|
57
|
+
`prepublishOnly` runs `bun test`. Set `author` in `package.json` before publish if not already present.
|
|
58
|
+
|
|
59
|
+
**Release**
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
bun test
|
|
63
|
+
npm publish --access public # first time: npm login
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
[opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) ships a `dist/` for its main export but still exposes `./tui` → source; we follow the source-only TUI entry for now.
|
|
67
|
+
|
|
68
|
+
## Pull requests
|
|
69
|
+
|
|
70
|
+
- Ensure the [test workflow](.github/workflows/test.yml) passes on your branch.
|
|
71
|
+
- Run `bun test` locally (or rely on pre-push / CI).
|
|
72
|
+
- User-facing README: update [README.md](README.md) and mirror key points in [README.zh-CN.md](README.zh-CN.md).
|
|
73
|
+
- Timeline: update [docs/en/timeline.md](docs/en/timeline.md) and [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zhumengzhu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# opencode-cache-hit
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
|
|
5
|
+
OpenCode **TUI sidebar plugin** for prompt **cache hit rate**, **token usage**, and **cost**—with first-class **sub-agent (child session)** rollup. **Standalone by default** (main + sub-agents in one panel). Optional coexistence with [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache).
|
|
6
|
+
|
|
7
|
+
**Languages:** English (this file) · [简体中文](README.zh-CN.md) · [Documentation](docs/README.md)
|
|
8
|
+
|
|
9
|
+
## Why this plugin
|
|
10
|
+
|
|
11
|
+
[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:
|
|
12
|
+
|
|
13
|
+
1. **Sub-agent visibility** — OpenCode spawns child sessions for Task / explore agents; you need **rolled-up** cache, tokens, and cost per sub-session, not only the main thread.
|
|
14
|
+
2. **One panel for the whole session** — Main session Hit/tokens/cost **and** a collapsible **Agents** section for sub-agent rollup.
|
|
15
|
+
3. **Analysis off the TUI** — Optional **timeline JSONL** (per assistant turn) for charts, jq, and billing post-mortems without scraping platform logs.
|
|
16
|
+
4. **Shared TUI building blocks** — `src/tui-panel/` extracted so other sidebar plugins can reuse the same layout language as visual-cache.
|
|
17
|
+
|
|
18
|
+
Roadmap items (sidebar Timeline section, metric windows, nested sub-agents) are described in [docs/en/timeline.md](docs/en/timeline.md) and [docs/en/design.md](docs/en/design.md).
|
|
19
|
+
|
|
20
|
+
## Acknowledgments
|
|
21
|
+
|
|
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**. We recommend installing both.
|
|
23
|
+
|
|
24
|
+
## Screenshots
|
|
25
|
+
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- **Cache hit rate**: session total + **per-turn** rate with trend (↑ / ↓ / `-`) on the main block
|
|
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)
|
|
34
|
+
- **Main + Agents**: main block always shown; **Agents** section when sub-agents exist (foldable)
|
|
35
|
+
- **Collapsible sections**: Detail / Model (and Agents); theme-adaptive hit bar colors
|
|
36
|
+
- **i18n**: `display.lang` — `en` / `zh` / `auto` via config (no slash commands yet)
|
|
37
|
+
- **Timeline** (optional): daily JSONL per assistant turn for `jq` / scripts
|
|
38
|
+
|
|
39
|
+
## Comparison with [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache)
|
|
40
|
+
|
|
41
|
+
**Standalone use is the default** (main + sub-agents in one panel). Layout patterns were inspired by visual-cache; that package is **not required**.
|
|
42
|
+
|
|
43
|
+
| | visual-cache | opencode-cache-hit |
|
|
44
|
+
|---|----------------|-------------------|
|
|
45
|
+
| Main session context / token **distribution** estimate | Yes | No — use visual-cache |
|
|
46
|
+
| Per-role token breakdown (system / tools / …) | Yes | No |
|
|
47
|
+
| Cache **savings** estimate | Yes | No |
|
|
48
|
+
| Model **per-million** pricing from provider | Yes | Model name + session cost only |
|
|
49
|
+
| **Slash commands** (`/cache-lang`, `/cache-currency`, …) | Yes | Config file only |
|
|
50
|
+
| Fold state in `api.kv` | Yes | In-session (not persisted) |
|
|
51
|
+
| Loaded **skills** panel | Yes | No |
|
|
52
|
+
| **Sub-agent** session rollup | No | **Yes** |
|
|
53
|
+
| **Combined** hit (main + subs) | No | Yes when sub-agents exist |
|
|
54
|
+
| Per-call **JSONL** export | No | Optional `timeline` |
|
|
55
|
+
|
|
56
|
+
## Quick start
|
|
57
|
+
|
|
58
|
+
### Option A: OpenCode command palette (recommended)
|
|
59
|
+
|
|
60
|
+
`Ctrl+P` → **install plugin** → `opencode-cache-hit@latest` (when published) or your local path.
|
|
61
|
+
|
|
62
|
+
### Option B: Manual
|
|
63
|
+
|
|
64
|
+
Create or edit `~/.config/opencode/tui.json` / `tui.jsonc`:
|
|
65
|
+
|
|
66
|
+
```jsonc
|
|
67
|
+
{
|
|
68
|
+
"$schema": "https://opencode.ai/tui.json",
|
|
69
|
+
"plugin": ["opencode-cache-hit@latest"]
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Local development: use `"./plugins/opencode-cache-hit"` instead of the npm name.
|
|
74
|
+
|
|
75
|
+
Copy `cache-hit.config.example.json` → `cache-hit.config.json` next to the plugin root ([Configuration file](#configuration-file)). **Restart OpenCode** after changing plugin code or config.
|
|
76
|
+
|
|
77
|
+
| Install | After update |
|
|
78
|
+
|---------|----------------|
|
|
79
|
+
| Local `./plugins/...` | Full restart |
|
|
80
|
+
| npm `@latest` | Restart; if UI is stale, remove `~/.cache/opencode/packages/opencode-cache-hit@latest` |
|
|
81
|
+
|
|
82
|
+
Load errors: `~/.local/share/opencode/log/` (search `cache-hit` or `failed to load tui plugin`).
|
|
83
|
+
|
|
84
|
+
## Configuration
|
|
85
|
+
|
|
86
|
+
### Cost display (USD → CNY example)
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"currency": "CNY",
|
|
91
|
+
"costUnit": "USD",
|
|
92
|
+
"rate": 7.2
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
| Field | Meaning |
|
|
97
|
+
|-------|---------|
|
|
98
|
+
| `costUnit` | Currency of `msg.cost` (usually `USD`) |
|
|
99
|
+
| `currency` | Sidebar display currency |
|
|
100
|
+
| `rate` | Multiply `costUnit` → `currency` |
|
|
101
|
+
|
|
102
|
+
Use `"currency": "USD", "costUnit": "USD"` when no conversion is needed.
|
|
103
|
+
|
|
104
|
+
Supported display currencies in config: `USD`, `CNY`, `EUR`, `GBP`, `JPY` (see `cache-hit.config.example.json`). Runtime slash switching like visual-cache’s `/cache-currency` is **not** implemented yet.
|
|
105
|
+
|
|
106
|
+
### Display (`display`)
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
"display": {
|
|
110
|
+
"lang": "en",
|
|
111
|
+
"panelBorder": true
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
| Field | Default | Meaning |
|
|
116
|
+
|-------|---------|---------|
|
|
117
|
+
| `lang` | `"en"` | `en` / `zh` / `auto` |
|
|
118
|
+
| `panelBorder` | `true` | Border/padding |
|
|
119
|
+
| `mainHitLabel` | (i18n) | Optional override for the Hit row label |
|
|
120
|
+
|
|
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.
|
|
122
|
+
|
|
123
|
+
### Timeline logs (`timeline`, default off)
|
|
124
|
+
|
|
125
|
+
Per assistant turn → JSONL. [docs/en/timeline.md](docs/en/timeline.md) · [中文](docs/zh-CN/timeline.md).
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
"timeline": {
|
|
129
|
+
"enabled": true,
|
|
130
|
+
"dir": "",
|
|
131
|
+
"rotateMaxBytes": 16777216,
|
|
132
|
+
"retainRotated": 5,
|
|
133
|
+
"maxAgeDays": 30,
|
|
134
|
+
"maxLogFiles": 20
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
| Field | Default | Meaning |
|
|
139
|
+
|-------|---------|---------|
|
|
140
|
+
| `enabled` | `false` | Master switch |
|
|
141
|
+
| `dir` | `""` | `logs/timeline-YYYY-MM-DD.jsonl` under plugin root |
|
|
142
|
+
| `rotateMaxBytes` | `0` | Same-day size roll to `.jsonl.1` |
|
|
143
|
+
| `retainRotated` | `5` | Backups kept per day |
|
|
144
|
+
| `maxLogFiles` | `0` | Cap file count; deletes **earliest** logs first |
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
LOG=~/.config/opencode/plugins/opencode-cache-hit/logs/timeline-$(date +%Y-%m-%d).jsonl
|
|
148
|
+
tail -f "$LOG"
|
|
149
|
+
jq -r 'select(.rootSessionId=="YOUR_ROOT") | [.created,.scope,.hitPercent,.cost]|@tsv' "$LOG"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Retention details: [Rotation and retention](docs/en/timeline.md#rotation-and-retention). Charts: [scripts/README.md](scripts/README.md).
|
|
153
|
+
|
|
154
|
+
## Updating
|
|
155
|
+
|
|
156
|
+
OpenCode may [cache plugins at first install](https://github.com/anomalyco/opencode/issues/6774) and not auto-refresh npm versions.
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
rm -rf ~/.cache/opencode/packages/opencode-cache-hit@latest
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Then reinstall via `Ctrl+P` → install plugin, and **restart OpenCode**.
|
|
163
|
+
|
|
164
|
+
## Compatibility
|
|
165
|
+
|
|
166
|
+
Model-agnostic: any OpenCode provider that exposes assistant `tokens` / `cost` on messages (DeepSeek, Claude, GPT, etc.). Data comes from the OpenCode session API, same as visual-cache.
|
|
167
|
+
|
|
168
|
+
**Requires** OpenCode with TUI plugin slots (`@opencode-ai/plugin` ≥ 1.14). Works alongside visual-cache; no extra dependencies at runtime beyond peers in [package.json](package.json).
|
|
169
|
+
|
|
170
|
+
## Documentation
|
|
171
|
+
|
|
172
|
+
| Audience | English | 中文 |
|
|
173
|
+
|----------|---------|------|
|
|
174
|
+
| Users | This README | [README.zh-CN.md](README.zh-CN.md) |
|
|
175
|
+
| Maintainers | [docs/en/design.md](docs/en/design.md) | [docs/zh-CN/design.md](docs/zh-CN/design.md) |
|
|
176
|
+
| Timeline / JSONL | [docs/en/timeline.md](docs/en/timeline.md) | [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md) |
|
|
177
|
+
| TUI panel reuse | [src/tui-panel/README.md](src/tui-panel/README.md) | [src/tui-panel/README.zh-CN.md](src/tui-panel/README.zh-CN.md) |
|
|
178
|
+
| Contributing / npm | [CONTRIBUTING.md](CONTRIBUTING.md) | — |
|
|
179
|
+
| Coding agents | [AGENTS.md](AGENTS.md) | — |
|
|
180
|
+
| Index | [docs/README.md](docs/README.md) | |
|
|
181
|
+
|
|
182
|
+
## Project layout
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
index.tsx
|
|
186
|
+
cache-hit.config.example.json
|
|
187
|
+
src/
|
|
188
|
+
plugin.tsx # sidebar_content slot
|
|
189
|
+
sidebar-host.tsx # messages, child sync, timeline
|
|
190
|
+
widget.tsx
|
|
191
|
+
stats.ts / timeline/ / tui-panel/
|
|
192
|
+
tests/
|
|
193
|
+
```
|
|
194
|
+
|
|
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
|
+
## Development
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
bun test
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup, PR notes, and npm publishing. Architecture: [docs/en/design.md](docs/en/design.md).
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# opencode-cache-hit
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
|
|
5
|
+
OpenCode **TUI 侧边栏插件**:展示 prompt cache 命中率、token 用量与成本;**主 session + 子 agent** 同屏汇总(默认开启主块)。可选与 [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) 共存。
|
|
6
|
+
|
|
7
|
+
**语言:** [English](README.md) · 简体中文(本页)· [文档索引](docs/README.md)
|
|
8
|
+
|
|
9
|
+
## 项目意图(为什么要做这个插件)
|
|
10
|
+
|
|
11
|
+
[opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache) 已经很好地覆盖**主 session** 的 cache 可视化(Token 分布、节省估算、斜杠改配置等)。本仓库要补的是它**刻意不做或未覆盖**的部分:
|
|
12
|
+
|
|
13
|
+
1. **子 agent 可观测性** — Task / explore 等会起子 session,需要把各子会话的 cache、token、费用**汇总**到侧栏。
|
|
14
|
+
2. **一场对话一块面板** — 主 session 与子 agent 同屏;**Agents** 段可折叠收起。
|
|
15
|
+
3. **离开 TUI 的分析** — 可选 **timeline JSONL**(按 assistant 轮次),便于画命中率曲线、jq 对账,而不去扒平台日志。
|
|
16
|
+
4. **可复用 TUI 骨架** — `src/tui-panel/` 抽出来,方便做其它侧栏插件。
|
|
17
|
+
|
|
18
|
+
后续(侧栏 Timeline 段、指标窗口、嵌套子 agent)见 [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md)、[docs/zh-CN/design.md](docs/zh-CN/design.md)。
|
|
19
|
+
|
|
20
|
+
## 致谢与借鉴说明
|
|
21
|
+
|
|
22
|
+
本插件是**独立项目**,并非 opencode-visual-cache 官方维护。侧栏布局、面板组件(`src/tui-panel/`)及与 visual-cache **共存**的策略,**大量借鉴**自 [opencode-visual-cache](https://www.npmjs.com/package/opencode-visual-cache):
|
|
23
|
+
|
|
24
|
+
- visual-cache:主 session **上下文 / token 分布预估**
|
|
25
|
+
- cache-hit:**按轮次指标、成本、子 agent 汇总**
|
|
26
|
+
|
|
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) | |
|
|
44
|
+
|
|
45
|
+
## 功能一览
|
|
46
|
+
|
|
47
|
+
- **命中率**:会话累计 + 主块**单轮**命中率与趋势
|
|
48
|
+
- **Token 明细**:缓存读/写/未命中/输出
|
|
49
|
+
- **费用**:多币种配置(`USD` / `CNY` / `EUR` / `GBP` / `JPY`)
|
|
50
|
+
- **子 agent**:**Agents** 段仅汇总**子 session**(UI 有范围提示)
|
|
51
|
+
- **主 session + Agents**:主块始终显示;有子 agent 时出现可折叠的 **Agents** 段
|
|
52
|
+
- **可选时间轴**:按天 JSONL 落盘
|
|
53
|
+
|
|
54
|
+
## 与 visual-cache 对比
|
|
55
|
+
|
|
56
|
+
**默认独立使用**(主 session + 子 agent 同面板)。版式借鉴 visual-cache,**不依赖**该插件。
|
|
57
|
+
|
|
58
|
+
| | visual-cache | cache-hit |
|
|
59
|
+
|---|----------------|-----------|
|
|
60
|
+
| 主 session 上下文 / Token **分布**估算 | 有 | 无 |
|
|
61
|
+
| 按角色 Token 分布、缓存**节省**、百万 token **单价** | 有 | 无 |
|
|
62
|
+
| **斜杠命令**改配置 | 有 | 仅配置文件 |
|
|
63
|
+
| **子 agent** 汇总 | 无 | **有** |
|
|
64
|
+
| 按次 **JSONL** | 无 | 可选 |
|
|
65
|
+
|
|
66
|
+
完整英文对照表见 [README.md](README.md#comparison-with-opencode-visual-cache)。
|
|
67
|
+
|
|
68
|
+
## 做什么、不做什么
|
|
69
|
+
|
|
70
|
+
**本插件负责**
|
|
71
|
+
|
|
72
|
+
- 主 session 与子 agent 的 cache / token / 成本聚合
|
|
73
|
+
- 侧边栏 **Cache Hit** 面板(布局对齐 visual-cache)
|
|
74
|
+
- 成本展示币种换算(默认 USD 成本 → CNY 显示)
|
|
75
|
+
|
|
76
|
+
**本插件不负责**
|
|
77
|
+
|
|
78
|
+
- 主 session 的「预估上下文 token」分布(由 visual-cache 提供)
|
|
79
|
+
- 修改 OpenCode 计费;`msg.cost` 仍按 `opencode.json` 美元单价
|
|
80
|
+
|
|
81
|
+
## 安装
|
|
82
|
+
|
|
83
|
+
**方式一:** `Ctrl+P` → install plugin → `opencode-cache-hit@latest`(发布后)或本地路径。
|
|
84
|
+
|
|
85
|
+
**方式二:** 编辑 `~/.config/opencode/tui.json` / `tui.jsonc`:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"plugin": ["./plugins/opencode-cache-hit"]
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
复制 `cache-hit.config.example.json` → `cache-hit.config.json`(与 `index.tsx` 同目录)。详见下文「[配置文件](#配置文件)」。
|
|
94
|
+
|
|
95
|
+
| 安装方式 | 更新后 |
|
|
96
|
+
|----------|--------|
|
|
97
|
+
| 本地路径 | 重启 |
|
|
98
|
+
| npm `@latest` | 重启;可删 `~/.cache/opencode/packages/opencode-cache-hit@latest` |
|
|
99
|
+
|
|
100
|
+
加载失败:`~/.local/share/opencode/log/`(搜 `cache-hit`)。
|
|
101
|
+
|
|
102
|
+
## 配置
|
|
103
|
+
|
|
104
|
+
### 成本(USD → 人民币)
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
{
|
|
108
|
+
"currency": "CNY",
|
|
109
|
+
"costUnit": "USD",
|
|
110
|
+
"rate": 7.2
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
定价与展示同为美元:`"currency": "USD", "costUnit": "USD"`。
|
|
115
|
+
|
|
116
|
+
### 展示(`display`)
|
|
117
|
+
|
|
118
|
+
```json
|
|
119
|
+
"display": {
|
|
120
|
+
"lang": "zh",
|
|
121
|
+
"panelBorder": true
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
| 字段 | 默认 | 含义 |
|
|
126
|
+
|------|------|------|
|
|
127
|
+
| `lang` | `"en"` | `en` / `zh` / `auto` |
|
|
128
|
+
| `panelBorder` | `true` | 外框与内边距 |
|
|
129
|
+
| `mainHitLabel` | (i18n) | 可选,覆盖 Hit 行前缀 |
|
|
130
|
+
|
|
131
|
+
**Agents 段**不含主 session token/费用(标题有「仅子会话」提示);不需要看子 agent 时折叠 **Agents** 即可。
|
|
132
|
+
|
|
133
|
+
### 时间轴日志(`timeline`,默认关闭)
|
|
134
|
+
|
|
135
|
+
详见 [docs/zh-CN/timeline.md](docs/zh-CN/timeline.md)。
|
|
136
|
+
|
|
137
|
+
```json
|
|
138
|
+
"timeline": {
|
|
139
|
+
"enabled": true,
|
|
140
|
+
"rotateMaxBytes": 16777216,
|
|
141
|
+
"retainRotated": 5,
|
|
142
|
+
"maxAgeDays": 30,
|
|
143
|
+
"maxLogFiles": 20
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
| 字段 | 代码默认 | 含义 |
|
|
148
|
+
|------|----------|------|
|
|
149
|
+
| `dir` | `""` | `logs/timeline-YYYY-MM-DD.jsonl` |
|
|
150
|
+
| `retainRotated` | `5` | 同日大小轮转备份数 |
|
|
151
|
+
| `maxLogFiles` | `0` | 超限时删**最早日期**日志 |
|
|
152
|
+
|
|
153
|
+
```fish
|
|
154
|
+
set log ~/.config/opencode/plugins/opencode-cache-hit/logs/timeline-(date +%Y-%m-%d).jsonl
|
|
155
|
+
tail -f $log
|
|
156
|
+
jq -r 'select(.rootSessionId=="YOUR_ROOT") | [.created,.scope,.hitPercent,.cost]|@tsv' $log
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
轮转与清理:[docs/zh-CN/timeline.md § 轮转与清理](docs/zh-CN/timeline.md#轮转与清理)。
|
|
160
|
+
|
|
161
|
+
## 配置文件
|
|
162
|
+
|
|
163
|
+
将 `cache-hit.config.example.json` 复制为 `cache-hit.config.json`,放在包根目录(与 `index.tsx` 同级)。修改后需重启 OpenCode。
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
cd ~/.cache/opencode/packages/opencode-cache-hit@latest # 或本地插件路径
|
|
167
|
+
cp cache-hit.config.example.json cache-hit.config.json
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
npm 打包、本地路径等说明见 [CONTRIBUTING.md](CONTRIBUTING.md)(英文)。
|
|
171
|
+
|
|
172
|
+
## 开发
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
bun test
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
架构:[docs/zh-CN/design.md](docs/zh-CN/design.md)。贡献 / 发布:[CONTRIBUTING.md](CONTRIBUTING.md)。AI 维护:[AGENTS.md](AGENTS.md)。
|
|
179
|
+
|
|
180
|
+
## 更新
|
|
181
|
+
|
|
182
|
+
若 npm 版本不刷新,参见 [OpenCode #6774](https://github.com/anomalyco/opencode/issues/6774),删除 `~/.cache/opencode/packages/opencode-cache-hit@latest` 后重装并重启。
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$comment": "lang: en | zh | auto (system locale). Default en.",
|
|
3
|
+
"currency": "CNY",
|
|
4
|
+
"costUnit": "USD",
|
|
5
|
+
"rate": 7.2,
|
|
6
|
+
"display": {
|
|
7
|
+
"lang": "en",
|
|
8
|
+
"panelBorder": true
|
|
9
|
+
},
|
|
10
|
+
"timeline": {
|
|
11
|
+
"enabled": false,
|
|
12
|
+
"dir": "",
|
|
13
|
+
"flushIncomplete": false,
|
|
14
|
+
"logSummaryMessages": true,
|
|
15
|
+
"maxMemoryRows": 50,
|
|
16
|
+
"maxLinesPerFile": 100000,
|
|
17
|
+
"rotateMaxBytes": 16777216,
|
|
18
|
+
"retainRotated": 5,
|
|
19
|
+
"maxAgeDays": 30,
|
|
20
|
+
"maxLogFiles": 20
|
|
21
|
+
}
|
|
22
|
+
}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Documentation
|
|
2
|
+
|
|
3
|
+
| | User guide | Design | Timeline | TUI panel |
|
|
4
|
+
|--|------------|--------|----------|-----------|
|
|
5
|
+
| English | [README.md](../README.md) | [en/design.md](./en/design.md) | [en/timeline.md](./en/timeline.md) | [../src/tui-panel/README.md](../src/tui-panel/README.md) |
|
|
6
|
+
| 中文 | [README.zh-CN.md](../README.zh-CN.md) | [zh-CN/design.md](./zh-CN/design.md) | [zh-CN/timeline.md](./zh-CN/timeline.md) | [../src/tui-panel/README.zh-CN.md](../src/tui-panel/README.zh-CN.md) |
|
|
7
|
+
|
|
8
|
+
Contributing: [CONTRIBUTING.md](../CONTRIBUTING.md).
|
|
Binary file
|