diff --git a/AGENTS.md b/AGENTS.md index 15054e9..199ed93 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,11 @@ These are load-bearing. Breaking one causes silent failures in opencode. never re-renders). The slot handler itself re-runs on session change, which masks the bug — it looks "fixed" after `/session` but stays stale on fresh launch. The built-in `internal:sidebar-context` plugin reads its memos inside JSX for the same reason. + Session token accounting is available to the panel via the same reactive store the + built-in context panel uses, `api.state.session.messages(sessionID)`: each assistant + info's `tokens.cache.read`/`write` come from the provider's V3 + `inputTokens.cacheRead`/`cacheWrite`, and `tokens.input` is non-cached — so total + prompt tokens are `input+cache.read+cache.write` and output includes `reasoning`. 14. **Slash commands and `Ctrl+P` share one registry.** In opencode 1.x the slash menu queries the same `namespace: "palette"` commands the palette lists (both filter out `hidden: true`). A slash-only entry is not expressible; registering `/cc-*` also adds them to `Ctrl+P`. diff --git a/README.md b/README.md index 4e9e67a..47bf07a 100644 --- a/README.md +++ b/README.md @@ -291,7 +291,7 @@ The API key is never logged; quota errors pass through `redact()` like every oth | Retry with backoff | Yes — 429/5xx and network errors, honouring `Retry-After`; also retryable `error` events that arrive inside an HTTP 200 stream before any content (gateway 520s) | | Credential redaction | Yes — error bodies are scrubbed before surfacing | | Runtime toggles | Yes — `/cc-zdr` and `/cc-debug` flip the shared toggle file without a restart | -| Sidebar status panel | Yes — a `sidebar_content` panel shows `zdr`/`debug` and live quota | +| Sidebar status panel | Yes — a `sidebar_content` panel shows `zdr`/`debug`, session token totals (`in`/`out`/cache `hit`) and live quota | | Quota tracking | Yes — 5-hour/weekly/monthly meters via `/cc-usage`, the sidebar, and `npm run quota` | ### `tool_choice` handling diff --git a/src/tui.tsx b/src/tui.tsx index 14a86d0..efcfdf3 100644 --- a/src/tui.tsx +++ b/src/tui.tsx @@ -51,9 +51,22 @@ type TuiProvider = { id?: string; key?: string; options?: Record }; +type TuiMessageInfo = { + role?: string; + providerID?: string; + modelID?: string; + tokens?: { + input?: number; + output?: number; + reasoning?: number; + cache?: { read?: number; write?: number }; + }; +}; + type TuiState = { config?: { provider?: Record }; provider?: ReadonlyArray; + session?: { messages(sessionID: string): ReadonlyArray }; }; type TuiApi = { @@ -165,6 +178,46 @@ function resolveBaseURL(api: TuiApi): string | undefined { return resolveRef(commandCodeOptions(api)?.["baseURL"]); } +type CacheSummary = { input: number; output: number; hit: number | undefined }; + +/** + * Session-cumulative token accounting from the reactive session store opencode's built-in + * context panel also reads. `tokens.input` is non-cached, so total prompt tokens are + * input+cache.read+cache.write; output includes reasoning. `hit` is cacheRead/total-input. + */ +function sessionCacheSummary(api: TuiApi, sessionID: string | undefined): CacheSummary | undefined { + if (!sessionID) return undefined; + let messages: ReadonlyArray; + try { + const list = api.state?.session?.messages(sessionID); + if (!Array.isArray(list)) return undefined; + messages = list; + } catch { + return undefined; + } + let input = 0; + let output = 0; + let read = 0; + let seen = 0; + for (const message of messages) { + if (message?.role !== "assistant" || !message.tokens) continue; + const tokens = message.tokens; + const cacheRead = tokens.cache?.read ?? 0; + input += (tokens.input ?? 0) + cacheRead + (tokens.cache?.write ?? 0); + output += (tokens.output ?? 0) + (tokens.reasoning ?? 0); + read += cacheRead; + seen++; + } + if (seen === 0) return undefined; + return { input, output, hit: input > 0 ? (read / input) * 100 : undefined }; +} + +function formatCache(summary: CacheSummary | undefined): string { + if (!summary) return "in: -- · out: -- · hit: --"; + const hit = summary.hit === undefined ? "--" : `${summary.hit.toFixed(1)}%`; + return `in: ${summary.input.toLocaleString()} · out: ${summary.output.toLocaleString()} · hit: ${hit}`; +} + function quotaIntervalMs(): number { const raw = Number(safeEnv("COMMANDCODE_QUOTA_INTERVAL_MS")); return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_QUOTA_INTERVAL_MS; @@ -188,6 +241,7 @@ export const tui = async (api: TuiApi): Promise => { const [quota, setQuota] = createSignal(null); const [now, setNow] = createSignal(Date.now()); const [updatedAt, setUpdatedAt] = createSignal(0); + const [sessionId, setSessionId] = createSignal(undefined); let inflight = false; let pending = false; @@ -292,6 +346,7 @@ export const tui = async (api: TuiApi): Promise => { zdr:{state(toggles().zdr)} debug:{state(toggles().debug)} + {formatCache(sessionCacheSummary(api, sessionId()))} {windowsOf(quota()).map((w) => ( {shortWindow(w, now())} ))} @@ -314,6 +369,7 @@ export const tui = async (api: TuiApi): Promise => { lastSessionId = id; scheduleQuotaRefresh("session-change"); } + if (id !== undefined) setSessionId(id); return ; }, },