@johannus22/opencode-tps-meter 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/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.jsx +215 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeyyy-arr
|
|
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,67 @@
|
|
|
1
|
+
# opencode-tps-meter
|
|
2
|
+
|
|
3
|
+
A plugin for [OpenCode](https://opencode.ai) that displays a live **Tokens Per Second (TPS)** meter with a visual bar indicator in the terminal UI.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Live TPS display** — real-time token generation speed with a 5-second rolling window
|
|
8
|
+
- **Visual bar indicator** — fills proportionally to show performance at a glance
|
|
9
|
+
- **Color-coded performance**:
|
|
10
|
+
- 🔴 Red: < 10 TPS (slow)
|
|
11
|
+
- 🟡 Yellow: 10-30 TPS (moderate)
|
|
12
|
+
- 🟢 Green: 30+ TPS (fast)
|
|
13
|
+
- **Total tokens counter** — running count of tokens streamed in current response
|
|
14
|
+
- **Elapsed time** — live timer since streaming started
|
|
15
|
+
- **Session summary** — shows final TPS and total tokens when streaming completes
|
|
16
|
+
|
|
17
|
+
## Display Format
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
TPS ▓▓▓▓▓░░░░░ 25.3 | 847 tok | 3.2s
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
### Via npm
|
|
26
|
+
|
|
27
|
+
1. Add the plugin to your `opencode.json`:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"$schema": "https://opencode.ai/config.json",
|
|
32
|
+
"plugin": ["@johannus22/opencode-tps-meter"]
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
2. Install the package:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
cd ~/.opencode
|
|
40
|
+
npm install @johannus22/opencode-tps-meter
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Via local plugin
|
|
44
|
+
|
|
45
|
+
Copy `src/index.tsx` to your plugin directory:
|
|
46
|
+
|
|
47
|
+
- `.opencode/plugins/` — Project-level
|
|
48
|
+
- `~/.config/opencode/plugins/` — Global
|
|
49
|
+
|
|
50
|
+
## Requirements
|
|
51
|
+
|
|
52
|
+
- OpenCode >= 1.3.14
|
|
53
|
+
- OpenCode TUI (Web UI not supported)
|
|
54
|
+
|
|
55
|
+
## How it works
|
|
56
|
+
|
|
57
|
+
The plugin hooks into OpenCode's message streaming events:
|
|
58
|
+
|
|
59
|
+
- **`message.part.delta`** — tracks token deltas as they arrive from the AI model
|
|
60
|
+
- **`message.updated`** — detects completion and shows session summary
|
|
61
|
+
- **`message.part.updated`** — clears live data when tool calls run
|
|
62
|
+
- Calculates TPS using a 5-second rolling window
|
|
63
|
+
- Estimates tokens using byte-length heuristic (~5 bytes per token)
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.jsx
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import { createSignal, createMemo } from "solid-js";
|
|
3
|
+
const BAR_LENGTH = 10;
|
|
4
|
+
const MAX_TPS_FOR_BAR = 50;
|
|
5
|
+
const SAMPLE_WINDOW_MS = 5000;
|
|
6
|
+
const LIVE_STALE_MS = 1500;
|
|
7
|
+
const SINGLE_SAMPLE_MIN_MS = 250;
|
|
8
|
+
const SINGLE_SAMPLE_MAX_MS = 1000;
|
|
9
|
+
function estimateTokens(text) {
|
|
10
|
+
const byteLen = new TextEncoder().encode(text).length;
|
|
11
|
+
return Math.max(1, Math.ceil(byteLen / 5));
|
|
12
|
+
}
|
|
13
|
+
function formatTps(value) {
|
|
14
|
+
if (value < 0)
|
|
15
|
+
return "-";
|
|
16
|
+
if (value < 10)
|
|
17
|
+
return value.toFixed(2);
|
|
18
|
+
if (value < 100)
|
|
19
|
+
return value.toFixed(1);
|
|
20
|
+
return Math.round(value).toString();
|
|
21
|
+
}
|
|
22
|
+
function formatTokens(count) {
|
|
23
|
+
if (count >= 1000)
|
|
24
|
+
return `${(count / 1000).toFixed(1)}k`;
|
|
25
|
+
return count.toString();
|
|
26
|
+
}
|
|
27
|
+
function formatElapsed(ms) {
|
|
28
|
+
const seconds = ms / 1000;
|
|
29
|
+
if (seconds < 60)
|
|
30
|
+
return `${seconds.toFixed(1)}s`;
|
|
31
|
+
const minutes = Math.floor(seconds / 60);
|
|
32
|
+
const remainingSeconds = Math.floor(seconds % 60);
|
|
33
|
+
return `${minutes}m ${remainingSeconds}s`;
|
|
34
|
+
}
|
|
35
|
+
function buildBar(tps) {
|
|
36
|
+
if (tps < 0)
|
|
37
|
+
return "░".repeat(BAR_LENGTH);
|
|
38
|
+
const filled = Math.min(BAR_LENGTH, Math.round((tps / MAX_TPS_FOR_BAR) * BAR_LENGTH));
|
|
39
|
+
const empty = BAR_LENGTH - filled;
|
|
40
|
+
return "▓".repeat(filled) + "░".repeat(empty);
|
|
41
|
+
}
|
|
42
|
+
function getTpsColor(tps, theme) {
|
|
43
|
+
if (tps < 0)
|
|
44
|
+
return theme.textMuted;
|
|
45
|
+
if (tps < 10)
|
|
46
|
+
return theme.error;
|
|
47
|
+
if (tps < 30)
|
|
48
|
+
return theme.warning;
|
|
49
|
+
return theme.success;
|
|
50
|
+
}
|
|
51
|
+
function calcTps(samples) {
|
|
52
|
+
if (samples.length === 0)
|
|
53
|
+
return -1;
|
|
54
|
+
if (samples.length === 1) {
|
|
55
|
+
const elapsed = Date.now() - samples[0].timestamp;
|
|
56
|
+
const duration = Math.max(SINGLE_SAMPLE_MIN_MS, Math.min(elapsed, SINGLE_SAMPLE_MAX_MS));
|
|
57
|
+
return (samples[0].tokens / duration) * 1000;
|
|
58
|
+
}
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
const cutoff = now - SAMPLE_WINDOW_MS;
|
|
61
|
+
const active = samples.filter((s) => s.timestamp >= cutoff);
|
|
62
|
+
if (active.length === 0)
|
|
63
|
+
return -1;
|
|
64
|
+
const last = active[active.length - 1];
|
|
65
|
+
if (now - last.timestamp > LIVE_STALE_MS)
|
|
66
|
+
return -1;
|
|
67
|
+
let totalDuration = 0;
|
|
68
|
+
for (let i = 1; i < active.length; i++) {
|
|
69
|
+
totalDuration += Math.max(0, active[i].timestamp - active[i - 1].timestamp);
|
|
70
|
+
}
|
|
71
|
+
const tail = now - active[active.length - 1].timestamp;
|
|
72
|
+
totalDuration += Math.min(tail, 1000);
|
|
73
|
+
totalDuration = Math.max(totalDuration, SINGLE_SAMPLE_MIN_MS);
|
|
74
|
+
const totalTokens = active.reduce((sum, s) => sum + s.tokens, 0);
|
|
75
|
+
return (totalTokens / totalDuration) * 1000;
|
|
76
|
+
}
|
|
77
|
+
const tui = async (api, _options, _meta) => {
|
|
78
|
+
const sessions = new Map();
|
|
79
|
+
const [version, setVersion] = createSignal(0);
|
|
80
|
+
const [tick, setTick] = createSignal(0);
|
|
81
|
+
function getState(sessionID) {
|
|
82
|
+
let state = sessions.get(sessionID);
|
|
83
|
+
if (!state) {
|
|
84
|
+
state = {
|
|
85
|
+
samples: [],
|
|
86
|
+
totalTokens: 0,
|
|
87
|
+
startTime: Date.now(),
|
|
88
|
+
completed: false,
|
|
89
|
+
finalTps: 0,
|
|
90
|
+
};
|
|
91
|
+
sessions.set(sessionID, state);
|
|
92
|
+
}
|
|
93
|
+
return state;
|
|
94
|
+
}
|
|
95
|
+
function clearSession(sessionID) {
|
|
96
|
+
sessions.delete(sessionID);
|
|
97
|
+
setVersion((v) => v + 1);
|
|
98
|
+
}
|
|
99
|
+
const unsubDelta = api.event.on("message.part.delta", (evt) => {
|
|
100
|
+
const sessionID = evt.properties.sessionID;
|
|
101
|
+
if (!sessionID)
|
|
102
|
+
return;
|
|
103
|
+
if (api.state.session.status(sessionID)?.type === "idle")
|
|
104
|
+
return;
|
|
105
|
+
if (evt.properties.field !== "text")
|
|
106
|
+
return;
|
|
107
|
+
const parts = api.state.part(evt.properties.messageID);
|
|
108
|
+
const hasTextOrReasoning = parts?.some((p) => p.type === "text" || p.type === "reasoning");
|
|
109
|
+
if (!hasTextOrReasoning)
|
|
110
|
+
return;
|
|
111
|
+
const deltaText = evt.properties.delta;
|
|
112
|
+
if (!deltaText || typeof deltaText !== "string")
|
|
113
|
+
return;
|
|
114
|
+
const tokens = estimateTokens(deltaText);
|
|
115
|
+
const now = Date.now();
|
|
116
|
+
const state = getState(sessionID);
|
|
117
|
+
state.samples.push({ tokens, timestamp: now });
|
|
118
|
+
state.totalTokens += tokens;
|
|
119
|
+
state.completed = false;
|
|
120
|
+
setVersion((v) => v + 1);
|
|
121
|
+
});
|
|
122
|
+
const unsubUpdated = api.event.on("message.updated", (evt) => {
|
|
123
|
+
const info = evt.properties.info;
|
|
124
|
+
if (info.role !== "assistant")
|
|
125
|
+
return;
|
|
126
|
+
const sessionID = info.sessionID;
|
|
127
|
+
const state = sessions.get(sessionID);
|
|
128
|
+
if (!state)
|
|
129
|
+
return;
|
|
130
|
+
if (info.time.completed) {
|
|
131
|
+
state.completed = true;
|
|
132
|
+
state.finalTps = calcTps(state.samples);
|
|
133
|
+
setVersion((v) => v + 1);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
const unsubPartUpdated = api.event.on("message.part.updated", (evt) => {
|
|
137
|
+
const part = evt.properties.part;
|
|
138
|
+
if (part.type !== "tool")
|
|
139
|
+
return;
|
|
140
|
+
const sessionID = part.sessionID;
|
|
141
|
+
const state = part.state;
|
|
142
|
+
if (state?.status === "running" || state?.status === "completed" || state?.status === "error") {
|
|
143
|
+
clearSession(sessionID);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
const interval = setInterval(() => {
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const cutoff = now - SAMPLE_WINDOW_MS;
|
|
149
|
+
for (const [, state] of sessions) {
|
|
150
|
+
const pruned = state.samples.filter((s) => s.timestamp >= cutoff);
|
|
151
|
+
if (pruned.length !== state.samples.length) {
|
|
152
|
+
state.samples = pruned;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
setTick((t) => t + 1);
|
|
156
|
+
}, 1000);
|
|
157
|
+
api.lifecycle.onDispose(() => {
|
|
158
|
+
unsubDelta();
|
|
159
|
+
unsubUpdated();
|
|
160
|
+
unsubPartUpdated();
|
|
161
|
+
clearInterval(interval);
|
|
162
|
+
});
|
|
163
|
+
api.slots.register({
|
|
164
|
+
slots: {
|
|
165
|
+
session_prompt_right(ctx, props) {
|
|
166
|
+
const sessionID = props.session_id;
|
|
167
|
+
const liveData = createMemo(() => {
|
|
168
|
+
version();
|
|
169
|
+
tick();
|
|
170
|
+
const state = sessions.get(sessionID);
|
|
171
|
+
if (!state)
|
|
172
|
+
return null;
|
|
173
|
+
const tps = calcTps(state.samples);
|
|
174
|
+
const elapsed = Date.now() - state.startTime;
|
|
175
|
+
const isLive = tps >= 0;
|
|
176
|
+
return {
|
|
177
|
+
tps,
|
|
178
|
+
totalTokens: state.totalTokens,
|
|
179
|
+
elapsed,
|
|
180
|
+
isLive,
|
|
181
|
+
completed: state.completed,
|
|
182
|
+
finalTps: state.finalTps,
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
return (<text>
|
|
186
|
+
{(() => {
|
|
187
|
+
const data = liveData();
|
|
188
|
+
if (!data)
|
|
189
|
+
return null;
|
|
190
|
+
if (data.completed) {
|
|
191
|
+
const color = getTpsColor(data.finalTps, ctx.theme.current);
|
|
192
|
+
return (<text fg={color}>
|
|
193
|
+
TPS {formatTps(data.finalTps)} | {formatTokens(data.totalTokens)} tok
|
|
194
|
+
</text>);
|
|
195
|
+
}
|
|
196
|
+
if (!data.isLive) {
|
|
197
|
+
return (<text fg={ctx.theme.current.textMuted}>
|
|
198
|
+
TPS - | {formatTokens(data.totalTokens)} tok | {formatElapsed(data.elapsed)}
|
|
199
|
+
</text>);
|
|
200
|
+
}
|
|
201
|
+
const color = getTpsColor(data.tps, ctx.theme.current);
|
|
202
|
+
const bar = buildBar(data.tps);
|
|
203
|
+
return (<text fg={color}>
|
|
204
|
+
TPS {bar} {formatTps(data.tps)} | {formatTokens(data.totalTokens)} tok | {formatElapsed(data.elapsed)}
|
|
205
|
+
</text>);
|
|
206
|
+
})()}
|
|
207
|
+
</text>);
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
export default {
|
|
213
|
+
id: "opencode-tps-meter",
|
|
214
|
+
tui,
|
|
215
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@johannus22/opencode-tps-meter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Live tokens-per-second meter with visual bar indicator for OpenCode TUI",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc --pretty false",
|
|
8
|
+
"typecheck": "tsc --noEmit --pretty false",
|
|
9
|
+
"prepack": "npm run build",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.jsx",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./tui": {
|
|
19
|
+
"import": "./dist/index.jsx",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"opencode": ">=1.3.14"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@opencode-ai/plugin": "*",
|
|
36
|
+
"@opentui/core": "*",
|
|
37
|
+
"@opentui/solid": "*",
|
|
38
|
+
"solid-js": "*"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"opencode",
|
|
42
|
+
"plugin",
|
|
43
|
+
"tps",
|
|
44
|
+
"tokens-per-second",
|
|
45
|
+
"performance",
|
|
46
|
+
"meter",
|
|
47
|
+
"solid-js"
|
|
48
|
+
],
|
|
49
|
+
"author": "jeyyy-arr",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "https://github.com/jeyyy-arr/opencode-tps-meter.git"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"typescript": "^5.8.2"
|
|
57
|
+
}
|
|
58
|
+
}
|