diff --git a/README.md b/README.md index 2a9e240..6aac0e5 100644 --- a/README.md +++ b/README.md @@ -225,19 +225,21 @@ Linux/macOS). Two rules for the entry: not a plugin — it loads as dead weight into every session. Restart opencode after registering the plugin; then type `/cc-` for autocomplete. The plugin also -renders a **CommandCode panel in the session sidebar** (`zdr` / `debug`, session token totals, a -`sub:` line for sub-agent descendants, plus live quota, above the built-in panels) that updates live -as you toggle. Note that in opencode 1.x the slash menu and the +renders a **CommandCode panel in the session sidebar** (`zdr` / `debug`, combined main + sub-agent +token totals (`in` / `out` / cache `hit`), plus live quota, above the built-in panels) that updates +live as you toggle. `in` and `out` show the combined main-agent and descendant total with the +sub-agent-only contribution in parentheses (omitted when there are no sub-agents); `hit` is the +cache-read percentage weighted across both. Note that in opencode 1.x the slash menu and the `Ctrl+P` palette read the same command registry, so these entries appear in both. If you run opencode against a remote server, the TUI-side file is not visible to the provider on the server host — use the env vars or `providerOptions` there. -Sub-agent tokens are aggregated separately: `task`-tool subagents run in child sessions +Sub-agent tokens are included in the same totals: `task`-tool subagents run in child sessions (`session.parent_id`), so their usage is not in the current session's message list. The panel walks descendants with the SDK's `session.children` endpoint (bounded depth/breadth), reads each child's -aggregate tokens from the session store or falls back to summing its assistant messages, and shows -the total on the `sub:` line. The parent's own tokens are unchanged; `sub: none` means no descendant -sessions were found. +aggregate tokens from the session store or falls back to summing its assistant messages, and adds +them to the combined `in` / `out` / `hit`. The parenthetical delta in `in` and `out` is sub-agent +usage alone; it is omitted when no descendant sessions were found. ## Quota tracking @@ -264,10 +266,10 @@ event that may never reach the TUI bus — all debounced) and when the active se but never more than once every 2 minutes; every 5 minutes as a fallback (`COMMANDCODE_QUOTA_INTERVAL_MS` overrides the fallback interval — see [Environment variables](#environment-variables)) and the -countdown ticks every 30 seconds. The `sub:` line is refreshed on the same triggers (it is -independent of quota rate-limiting). Key resolution and fetch failures are redacted and shown -inline; the panel never stays on `loading…` and never blocks the provider. Extra trace lines -(`tui-quota`) are appended to the debug log only when debug tracing is on. +countdown ticks every 30 seconds. The token totals (including the sub-agent delta) are refreshed on +the same triggers (they are independent of quota rate-limiting). Key resolution and fetch failures +are redacted and shown inline; the panel never stays on `loading…` and never blocks the provider. +Extra trace lines (`tui-quota`) are appended to the debug log only when debug tracing is on. Headless check (no TUI), reading the key from `COMMANDCODE_API_KEY` or the opencode config: @@ -300,7 +302,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`, session token totals (`in`/`out`/cache `hit`), a separate `sub:` line aggregating every descendant sub-agent session's tokens, and live quota | +| Sidebar status panel | Yes — a `sidebar_content` panel shows `zdr`/`debug`, combined main + sub-agent token totals (`in`/`out` with a sub-agent-only delta, plus 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 89c671f..28d1068 100644 --- a/src/tui.tsx +++ b/src/tui.tsx @@ -203,7 +203,7 @@ function resolveBaseURL(api: TuiApi): string | undefined { return resolveRef(commandCodeOptions(api)?.["baseURL"]); } -type CacheSummary = { input: number; output: number; hit: number | undefined }; +type CacheSummary = { input: number; output: number; hit: number | undefined; read: number }; type TokenTotals = { input: number; output: number; read: number; seen: number }; @@ -218,7 +218,12 @@ function addMessageTokens(totals: TokenTotals, tokens: TuiSessionTokens | undefi function summarizeTotals(totals: TokenTotals): CacheSummary | undefined { if (totals.seen === 0) return undefined; - return { input: totals.input, output: totals.output, hit: totals.input > 0 ? (totals.read / totals.input) * 100 : undefined }; + return { + input: totals.input, + output: totals.output, + hit: totals.input > 0 ? (totals.read / totals.input) * 100 : undefined, + read: totals.read, + }; } /** @@ -312,16 +317,46 @@ async function collectFamily(api: TuiApi, rootID: string): Promise 0 ? (read / input) * 100 : undefined, + empty: main === undefined && sub === undefined, + }; } -function formatFamily(summary: CacheSummary | undefined): string { - if (!summary) return "sub: none"; - const hit = summary.hit === undefined ? "--" : `${summary.hit.toFixed(1)}%`; - return `sub: in: ${summary.input.toLocaleString()} · out: ${summary.output.toLocaleString()} · hit: ${hit}`; +function formatIn(view: TokenView): string { + if (view.empty) return "in: --"; + const delta = view.hasSub ? ` (+${view.subInput.toLocaleString()})` : ""; + return `in: ${view.input.toLocaleString()}${delta}`; +} + +function formatOut(view: TokenView): string { + if (view.empty) return "out: --"; + const delta = view.hasSub ? ` (+${view.subOutput.toLocaleString()})` : ""; + return `out: ${view.output.toLocaleString()}${delta}`; +} + +function formatHit(view: TokenView): string { + if (view.empty || view.hit === undefined) return "hit: --"; + return `hit: ${view.hit.toFixed(1)}%`; } function quotaIntervalMs(): number { @@ -472,6 +507,7 @@ export const tui = async (api: TuiApi): Promise => { const Status = () => { const theme = api.theme; + const view = () => combineTokens(sessionCacheSummary(api, sessionId()), family()); return ( @@ -480,8 +516,9 @@ export const tui = async (api: TuiApi): Promise => { zdr:{state(toggles().zdr)} debug:{state(toggles().debug)} - {formatCache(sessionCacheSummary(api, sessionId()))} - {formatFamily(family())} + {formatIn(view())} + {formatOut(view())} + {formatHit(view())} {windowsOf(quota()).map((w) => ( {shortWindow(w, now())} ))}