add session token totals to sidebar
This commit is contained in:
parent
49d518871b
commit
177e8f6a58
@ -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 —
|
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
|
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.
|
`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
|
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`).
|
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`.
|
A slash-only entry is not expressible; registering `/cc-*` also adds them to `Ctrl+P`.
|
||||||
|
|||||||
@ -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) |
|
| 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 |
|
| 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 |
|
| 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` |
|
| Quota tracking | Yes — 5-hour/weekly/monthly meters via `/cc-usage`, the sidebar, and `npm run quota` |
|
||||||
|
|
||||||
### `tool_choice` handling
|
### `tool_choice` handling
|
||||||
|
|||||||
56
src/tui.tsx
56
src/tui.tsx
@ -51,9 +51,22 @@ type TuiProvider = { id?: string; key?: string; options?: Record<string, unknown
|
|||||||
|
|
||||||
type TuiProviderConfig = { options?: Record<string, unknown> };
|
type TuiProviderConfig = { options?: Record<string, unknown> };
|
||||||
|
|
||||||
|
type TuiMessageInfo = {
|
||||||
|
role?: string;
|
||||||
|
providerID?: string;
|
||||||
|
modelID?: string;
|
||||||
|
tokens?: {
|
||||||
|
input?: number;
|
||||||
|
output?: number;
|
||||||
|
reasoning?: number;
|
||||||
|
cache?: { read?: number; write?: number };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
type TuiState = {
|
type TuiState = {
|
||||||
config?: { provider?: Record<string, TuiProviderConfig | undefined> };
|
config?: { provider?: Record<string, TuiProviderConfig | undefined> };
|
||||||
provider?: ReadonlyArray<TuiProvider>;
|
provider?: ReadonlyArray<TuiProvider>;
|
||||||
|
session?: { messages(sessionID: string): ReadonlyArray<TuiMessageInfo> };
|
||||||
};
|
};
|
||||||
|
|
||||||
type TuiApi = {
|
type TuiApi = {
|
||||||
@ -165,6 +178,46 @@ function resolveBaseURL(api: TuiApi): string | undefined {
|
|||||||
return resolveRef(commandCodeOptions(api)?.["baseURL"]);
|
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<TuiMessageInfo>;
|
||||||
|
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 {
|
function quotaIntervalMs(): number {
|
||||||
const raw = Number(safeEnv("COMMANDCODE_QUOTA_INTERVAL_MS"));
|
const raw = Number(safeEnv("COMMANDCODE_QUOTA_INTERVAL_MS"));
|
||||||
return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_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<void> => {
|
|||||||
const [quota, setQuota] = createSignal<QuotaResult | null>(null);
|
const [quota, setQuota] = createSignal<QuotaResult | null>(null);
|
||||||
const [now, setNow] = createSignal(Date.now());
|
const [now, setNow] = createSignal(Date.now());
|
||||||
const [updatedAt, setUpdatedAt] = createSignal(0);
|
const [updatedAt, setUpdatedAt] = createSignal(0);
|
||||||
|
const [sessionId, setSessionId] = createSignal<string | undefined>(undefined);
|
||||||
|
|
||||||
let inflight = false;
|
let inflight = false;
|
||||||
let pending = false;
|
let pending = false;
|
||||||
@ -292,6 +346,7 @@ export const tui = async (api: TuiApi): Promise<void> => {
|
|||||||
<text fg={theme.current.textMuted}>
|
<text fg={theme.current.textMuted}>
|
||||||
zdr:{state(toggles().zdr)} debug:{state(toggles().debug)}
|
zdr:{state(toggles().zdr)} debug:{state(toggles().debug)}
|
||||||
</text>
|
</text>
|
||||||
|
<text fg={theme.current.textMuted}>{formatCache(sessionCacheSummary(api, sessionId()))}</text>
|
||||||
{windowsOf(quota()).map((w) => (
|
{windowsOf(quota()).map((w) => (
|
||||||
<text fg={tone(w, theme)}>{shortWindow(w, now())}</text>
|
<text fg={tone(w, theme)}>{shortWindow(w, now())}</text>
|
||||||
))}
|
))}
|
||||||
@ -314,6 +369,7 @@ export const tui = async (api: TuiApi): Promise<void> => {
|
|||||||
lastSessionId = id;
|
lastSessionId = id;
|
||||||
scheduleQuotaRefresh("session-change");
|
scheduleQuotaRefresh("session-change");
|
||||||
}
|
}
|
||||||
|
if (id !== undefined) setSessionId(id);
|
||||||
return <Status />;
|
return <Status />;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user