Skip to content

Commit e6b1442

Browse files
feat(opencode): add v2 status line plugin
Port the Claude Code status line to an OpenCode v2 TUI plugin, rendered above the prompt: model and variant, context bar and token split, cost and duration, and prompt cache hit rate with cached tokens. The directory/branch line is omitted because v2.0.20 can't resolve projects under ~/Documents (anomalyco/opencode#52458). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1c4c459 commit e6b1442

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

  • .opencode/plugins/statusline
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/** @jsxImportSource @opentui/solid */
2+
// OpenCode status line (ported from ~/.claude/statusline-command.sh)
3+
//
4+
// Renders the Claude Code status line block just above the prompt, plus a
5+
// cache line (hit rate and cached tokens; OpenCode has no cache TTL or miss
6+
// data). Rate limits (OpenCode has none) and the directory/branch line are left
7+
// out: v2.0.20 can't resolve projects under ~/Documents
8+
// (anomalyco/opencode#52458), so it reports the wrong location. Auto-discovered
9+
// from `.opencode/plugins/statusline/tui.tsx` (project) or
10+
// `~/.config/opencode/plugins/statusline/tui.tsx` (global).
11+
import { Plugin } from '@opencode/plugin/tui';
12+
import { Show, createMemo } from 'solid-js';
13+
14+
const BAR_WIDTH = 10;
15+
16+
function formatTokens(n: number) {
17+
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
18+
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
19+
return String(n);
20+
}
21+
22+
function formatDuration(ms: number) {
23+
const s = Math.max(0, Math.floor(ms / 1000));
24+
const h = Math.floor(s / 3600);
25+
const m = Math.floor((s % 3600) / 60);
26+
return h > 0 ? `${h}h ${m}m` : `${m}m ${s % 60}s`;
27+
}
28+
29+
function StatusLine(props: { context: Plugin.Context; sessionID: string }) {
30+
const { context } = props;
31+
const theme = () => context.theme;
32+
const location = () => context.location;
33+
34+
const stats = createMemo(() => {
35+
const session = context.data.session.get(props.sessionID);
36+
const last = context.data.session.message
37+
.list(props.sessionID)
38+
.findLast((m) => m.type === 'assistant');
39+
const assistant = last?.type === 'assistant' ? last : undefined;
40+
41+
// Before the first response, fall back to the prompt's selected model
42+
const ref = assistant?.model ?? session?.model ?? context.ui.model.current();
43+
const refID = ref && ('id' in ref ? ref.id : ref.modelID);
44+
const model = ref
45+
? context.data.location.model
46+
.list(location())
47+
?.find((m) => m.providerID === ref.providerID && (m.id === refID || m.modelID === refID))
48+
: undefined;
49+
50+
// Live context = the latest response's total tokens
51+
const t = assistant?.tokens;
52+
const ctxUsed = t ? t.input + t.output + t.reasoning + t.cache.read + t.cache.write : 0;
53+
const ctxLimit = model?.limit.context ?? 0;
54+
const pct = ctxLimit > 0 ? Math.min(100, Math.round((ctxUsed * 100) / ctxLimit)) : 0;
55+
56+
const s = session?.tokens;
57+
// Session totals only: OpenCode has no cache TTL, warm/cold state or miss count
58+
const cacheTotal = s ? s.input + s.cache.read + s.cache.write : 0;
59+
return {
60+
model: model?.name ?? refID,
61+
variant: ref?.variant,
62+
pct,
63+
input: s ? s.input + s.cache.read + s.cache.write : 0,
64+
output: s ? s.output + s.reasoning : 0,
65+
cached: s?.cache.read ?? 0,
66+
hit: cacheTotal > 0 ? Math.round(((s?.cache.read ?? 0) * 100) / cacheTotal) : undefined,
67+
cost: session?.cost ?? 0,
68+
// v2 only bumps session.time.updated on metadata changes, so use the latest response
69+
duration: session
70+
? (assistant?.time.completed ?? assistant?.time.created ?? session.time.idle ?? session.time.updated) -
71+
session.time.created
72+
: 0,
73+
};
74+
});
75+
76+
// Same palette as the Claude Code script: green/yellow/red, cyan, magenta, dim
77+
const green = () => theme().text.feedback.success.base;
78+
const yellow = () => theme().text.feedback.warning.base;
79+
const red = () => theme().text.feedback.error.base;
80+
const cyan = () => theme().text.feedback.info.base;
81+
// No theme token is magenta in every theme, so use the classic terminal magenta
82+
const magenta = () => '#c678dd';
83+
const dim = () => theme().text.muted;
84+
85+
const barColor = () => (stats().pct >= 80 ? red() : stats().pct >= 50 ? yellow() : green());
86+
// Higher is better for cache hits, so the scale runs the other way
87+
const hitColor = () => {
88+
const hit = stats().hit ?? 0;
89+
return hit >= 80 ? green() : hit >= 50 ? yellow() : red();
90+
};
91+
const bar = () => {
92+
const filled = Math.round(stats().pct / BAR_WIDTH);
93+
return '█'.repeat(filled) + '░'.repeat(BAR_WIDTH - filled);
94+
};
95+
const sep = () => <span style={{ fg: dim() }}>{' | '}</span>;
96+
97+
return (
98+
<box flexDirection="column" paddingBottom={1}>
99+
<text wrapMode="none">
100+
<span style={{ fg: cyan() }}>
101+
<b>[{stats().model ?? 'no model'}]</b>
102+
</span>
103+
<Show when={stats().variant}>
104+
{sep()}🧠 <span style={{ fg: magenta() }}>{stats().variant}</span>
105+
</Show>
106+
</text>
107+
<text wrapMode="none">
108+
<span style={{ fg: barColor() }}>{bar()}</span>
109+
<span style={{ fg: yellow() }}> {stats().pct}%</span>
110+
{sep()}
111+
<span style={{ fg: cyan() }}>
112+
{formatTokens(stats().input)} ↑ / {formatTokens(stats().output)} ↓
113+
</span>
114+
</text>
115+
<text wrapMode="none">
116+
<span style={{ fg: green() }}>${stats().cost.toFixed(2)}</span>
117+
{sep()}🕐 {formatDuration(stats().duration)}
118+
</text>
119+
<Show when={stats().hit !== undefined} fallback={<text style={{ fg: dim() }}>💾 cache --</text>}>
120+
<text wrapMode="none">
121+
💾 cache hit <span style={{ fg: hitColor() }}>{stats().hit}%</span>
122+
{sep()}
123+
<span style={{ fg: cyan() }}>{formatTokens(stats().cached)} cached</span>
124+
</text>
125+
</Show>
126+
</box>
127+
);
128+
}
129+
130+
export default Plugin.define({
131+
id: 'statusline',
132+
setup(context) {
133+
return context.ui.slot({
134+
append: 'session.composer.top',
135+
render: (input) => <StatusLine context={context} sessionID={input.sessionID} />,
136+
});
137+
},
138+
});

0 commit comments

Comments
 (0)