new: [opencode-greenpt-usage] track API key consumption per day and model

Show real credit spend and a per-model breakdown under the balance:

- ``src/usage.ts`` (new, pure): idempotent day buckets keyed by message
  id, day/month totals, retention pruning, validated KV serialization.
- Real spend = delta of ``x-credits-remaining`` between two free probes
  (top-ups are not counted as spend), covering every client of the key.
- Per-model detail = reconciliation of local GreenPT assistant messages
  (tokens + estimated cost, marked ``~``), re-run on every refresh and
  on ``session.idle``; last 10 sessions backfilled at startup.
- Buckets persisted in the plugin KV store (90-day window) with new
  options ``trackUsage``, ``usageWindowDays``, ``usageBackfill`` and a
  ``greenpt.usage.reset`` command.
This commit is contained in:
Stéphan Sainléger
2026-10-19 11:36:45 +02:00
parent 5c11754e2d
commit 9c84473266
10 changed files with 871 additions and 10 deletions

View File

@@ -24,6 +24,10 @@ GREENPT_API_KEY=... npm run test:integration # live API test
no GreenPT credential is present (`showWhenUnavailable` opts into a hint).
- Balance probing must stay **free**: `HEAD /v1/chat/completions` returns
`400` plus `x-credits-remaining`. Never replace it with a billable call.
- Usage accounting lives in `src/usage.ts` (pure) and is consumed by the
balance store: real spend = delta of `x-credits-remaining` between
probes; per-model detail = reconciliation of local OpenCode assistant
messages, idempotent by message id, persisted in the plugin KV store.
- Credential resolution order: `apiKey` option, `GREENPT_API_KEY`, active
OpenCode `auth.json` (`greenpt`, `type: "api"`). No profile-switching
command may leak into this logic.

View File

@@ -1,11 +1,15 @@
# opencode-greenpt-usage
An [OpenCode](https://opencode.ai) TUI plugin that shows the remaining
**GreenPT API credit balance** in the sidebar.
**GreenPT API credit balance** and the **consumption** of the API key
(real credit spend plus a per-model breakdown) in the sidebar.
```
▼ GreenPT
Balance 5.31 € updated 21:35
Spent today 0.04 € month 0.31 €
glm-5.3-flash 12.4k tok ~0.02 €
kimi-k2 3.1k tok ~0.01 €
```
## How it works
@@ -20,10 +24,23 @@ Balance 5.31 € updated 21:35
the API answers `400 Bad Request` before any billing, yet the response
still carries the `x-credits-remaining` header. No credit is consumed
and no generation is triggered.
- **Usage tracking** keeps per-day buckets, persisted in the plugin KV
store (90 days by default):
- *real credit spend* is observed as the drop of `x-credits-remaining`
between two balance probes — this covers **every client** of the API
key, not just OpenCode, but has no per-model detail;
- *per-model breakdown* (tokens and estimated cost) is reconciled from
the GreenPT assistant messages of your local OpenCode sessions — this
detail is therefore **local to this machine** and priced with
OpenCode's model catalog, which may differ from GreenPT's credit
pricing (estimates are marked with `~`).
- At startup, the last 10 sessions are reconciled to backfill recent
history; counters can be cleared with `greenpt.usage.reset`.
- The section is **hidden while no GreenPT key is present**.
- Refresh happens on startup, at the end of a session, on server
reconnection, on a timer (300 s by default) and on demand through the
`greenpt.refresh` command.
`greenpt.refresh` command; usage buckets are reconciled on each refresh
and at the end of each session.
There is no dependency on any local profile or command: the plugin only
looks for a key.
@@ -78,6 +95,9 @@ All options are optional and go in the plugin entry of `tui.json`:
| `order` | number | `700` | Sidebar section position. |
| `showWhenUnavailable` | boolean | `false` | Show a hint when no GreenPT key exists. |
| `unit` | string | `€` | Unit appended after the amount. |
| `trackUsage` | boolean | `true` | Track credit spend and per-model usage. |
| `usageWindowDays` | number | `90` | Retention window for per-day usage buckets. |
| `usageBackfill` | number | `10` | Recent sessions reconciled at startup (`0` disables). |
## Troubleshooting

View File

@@ -3,6 +3,9 @@ import type { BalanceError } from "./balance"
/** Key used to persist the section fold state in the shared KV store. */
export const BALANCE_OPEN_KV = "greenpt.usage.open"
/** Key used to persist the usage day buckets in the shared KV store. */
export const USAGE_DAYS_KV = "greenpt.usage.days"
/** Default unit appended to the balance (GreenPT credits are euro-denominated). */
export const DEFAULT_UNIT = "€"
@@ -38,6 +41,23 @@ export function formatBalanceError(
}
}
/**
* Format a token count for compact display.
*
* @param tokens Token count.
* @returns e.g. `"0"`, `"842"`, `"12.4k"`, `"3.2M"`.
*/
export function formatTokens(tokens: number): string {
if (!Number.isFinite(tokens) || tokens < 0) return "0"
if (tokens < 1000) return String(Math.round(tokens))
if (tokens < 1_000_000) {
const k = tokens / 1000
return `${k < 100 ? k.toFixed(1) : String(Math.round(k))}k`
}
const m = tokens / 1_000_000
return `${m < 100 ? m.toFixed(1) : String(Math.round(m))}M`
}
/**
* Read the persisted fold state, tolerating a missing or broken KV store.
*

View File

@@ -36,6 +36,14 @@ const tui: TuiPlugin = async (api, options) => {
void store.refresh()
},
},
{
value: "greenpt.usage.reset",
title: "Reset GreenPT usage counters",
category: "Plugin",
onSelect() {
store.resetUsage()
},
},
])
api.lifecycle.onDispose(() => store.dispose())

View File

@@ -1,7 +1,17 @@
import { createSignal } from "solid-js"
import type { TuiPluginApi } from "@opencode-ai/plugin/tui"
import { fetchBalance, nativeFetch, type Balance, type BalanceError, type FetchLike } from "./balance"
import { USAGE_DAYS_KV } from "./format"
import { resolveGreenptKey, type CredentialEnv } from "./credential"
import {
addCreditsSpent,
applyMessages,
parseUsageDays,
pruneUsage,
serializeUsage,
type UsageDays,
type UsageMessage,
} from "./usage"
/** Default GreenPT API base URL. */
export const DEFAULT_BASE_URL = "https://api.greenpt.ai/v1"
@@ -9,6 +19,12 @@ export const DEFAULT_BASE_URL = "https://api.greenpt.ai/v1"
/** Default refresh interval, in seconds. The probe is free but should stay gentle. */
export const DEFAULT_REFRESH_SECONDS = 300
/** Default retention window for usage buckets, in days. */
export const DEFAULT_USAGE_WINDOW_DAYS = 90
/** Default number of recent sessions reconciled at startup. */
export const DEFAULT_USAGE_BACKFILL = 10
export type BalanceOptions = {
apiKey?: string
baseUrl?: string
@@ -16,6 +32,9 @@ export type BalanceOptions = {
order?: number
showWhenUnavailable?: boolean
unit?: string
trackUsage?: boolean
usageWindowDays?: number
usageBackfill?: number
}
export type BalanceState =
@@ -29,7 +48,9 @@ export type BalanceState =
export type BalanceStore = {
state: () => BalanceState
now: () => number
usageDays: () => UsageDays
refresh: () => Promise<void>
resetUsage: () => void
dispose: () => void
}
@@ -45,7 +66,13 @@ const TICK_MS = 30_000
* timer, and on demand via `refresh()`. Errors are retried with an
* exponential backoff so a transient outage does not hammer the API.
*
* @param api TUI plugin API (used for the auth store path and events).
* Usage tracking (opt-out via `trackUsage: false`) keeps per-day buckets:
* real credit spend from balance deltas, and per-model token/cost totals
* reconciled from local OpenCode assistant messages. Buckets are persisted
* in the plugin KV store and pruned to `usageWindowDays`.
*
* @param api TUI plugin API (used for the auth store path, events, KV and
* session messages).
* @param options Plugin options.
* @param env Environment mapping, injectable for tests.
* @param fetchImpl Fetch implementation, injectable for tests.
@@ -59,9 +86,13 @@ export function createBalanceStore(
): BalanceStore {
const [state, setState] = createSignal<BalanceState>({ status: "loading" })
const [now, setNow] = createSignal(Date.now())
const [usageDays, setUsageDays] = createSignal<UsageDays>(readUsageDays(api.kv))
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL
const interval = options.refreshInterval ?? DEFAULT_REFRESH_SECONDS
const trackUsage = options.trackUsage !== false
const usageWindowDays = options.usageWindowDays ?? DEFAULT_USAGE_WINDOW_DAYS
const usageBackfillLimit = options.usageBackfill ?? DEFAULT_USAGE_BACKFILL
let refreshTimer: ReturnType<typeof setTimeout> | undefined
let tickTimer: ReturnType<typeof setTimeout> | undefined
@@ -70,11 +101,116 @@ export function createBalanceStore(
let inFlight = false
let disposed = false
let retriedState = false
let lastCredits: number | undefined
let kvLoaded = false
const trackedSessions = new Set<string>()
const controller = new AbortController()
const unsubscribeIdle = api.event.on("session.idle", () => void refresh())
const unsubscribeIdle = api.event.on("session.idle", (event) => {
trackSession(event.properties.sessionID)
reconcileSessions()
void refresh()
})
const unsubscribeStatus = api.event.on("session.status", (event) => {
trackSession(event.properties.sessionID)
})
const unsubscribeConnected = api.event.on("server.connected", () => void refresh())
function readUsageDays(kv: TuiPluginApi["kv"]): UsageDays {
try {
const raw = kv.get(USAGE_DAYS_KV)
if (raw === undefined) return {}
return parseUsageDays(raw)
} catch {
return {}
}
}
function writeUsageDays(kv: TuiPluginApi["kv"], days: UsageDays): void {
try {
kv.set(USAGE_DAYS_KV, serializeUsage(days))
} catch {
// KV persistence is optional; in-memory buckets remain usable.
}
}
function trackSession(sessionID: string): void {
if (sessionID) trackedSessions.add(sessionID)
}
/** Apply usage mutations through a fresh top-level record for reactivity. */
function mutateUsage(mutate: (days: UsageDays) => void): void {
if (!trackUsage) return
const days: UsageDays = { ...usageDays() }
mutate(days)
pruneUsage(days, usageWindowDays, Date.now())
setUsageDays(days)
writeUsageDays(api.kv, days)
}
/**
* Reconcile per-model usage from the messages of every tracked session.
* Idempotent: messages are de-duplicated by id in `usage.ts`.
*/
function reconcileSessions(): void {
if (!trackUsage || trackedSessions.size === 0) return
for (const sessionID of trackedSessions) {
let messages: ReadonlyArray<UsageMessage> | undefined
try {
messages = api.state.session.messages(sessionID)
} catch {
continue
}
if (!messages?.length) continue
mutateUsage((days) => applyMessages(days, messages as ReadonlyArray<unknown>))
}
}
/** Best-effort startup backfill from recently updated sessions. */
async function backfillUsage(): Promise<void> {
if (!trackUsage || usageBackfillLimit <= 0) return
const client = api.client as
| {
session?: {
list?: (parameters?: { limit?: number }) => Promise<unknown>
messages?: (parameters: { sessionID: string }) => Promise<unknown>
}
}
| undefined
if (!client?.session?.list || !client.session.messages) return
try {
const listed = (await client.session.list({ limit: usageBackfillLimit })) as {
data?: unknown
}
if (!Array.isArray(listed?.data)) return
for (const session of listed.data) {
if (disposed) return
const sessionID = (session as { id?: unknown })?.id
if (typeof sessionID !== "string" || !sessionID) continue
trackedSessions.add(sessionID)
try {
const result = (await client.session.messages({ sessionID })) as { data?: unknown }
if (!Array.isArray(result?.data)) continue
const messages = result.data
.map((row) => (row as { info?: unknown })?.info)
.filter((info) => info !== undefined && info !== null)
mutateUsage((days) => applyMessages(days, messages))
} catch {
// One unreadable session must not abort the whole backfill.
}
}
} catch {
// Backfill is optional; live tracking continues regardless.
}
}
/** Re-read persisted buckets once the KV store becomes ready. */
function reloadUsageWhenReady(): void {
if (!trackUsage || kvLoaded || api.kv.ready !== true) return
kvLoaded = true
setUsageDays(readUsageDays(api.kv))
}
function scheduleNext() {
if (disposed) return
if (refreshTimer) clearTimeout(refreshTimer)
@@ -97,6 +233,7 @@ export function createBalanceStore(
async function refresh(): Promise<void> {
if (disposed || inFlight) return
inFlight = true
reloadUsageWhenReady()
try {
const credential = resolveGreenptKey(options, api.state.path.state, env)
if (!credential.ok) {
@@ -119,6 +256,14 @@ export function createBalanceStore(
if ("credits" in result) {
backoff = BACKOFF_BASE_MS
const credits = result.credits
if (trackUsage && Number.isFinite(credits)) {
if (lastCredits !== undefined && credits < lastCredits) {
const spent = lastCredits - credits
mutateUsage((days) => addCreditsSpent(days, Date.now(), spent))
}
lastCredits = credits
}
setState({ status: "ready", data: result, fetchedAt: Date.now() })
} else {
backoff = Math.min(backoff * 2, BACKOFF_MAX_MS)
@@ -128,9 +273,18 @@ export function createBalanceStore(
inFlight = false
}
if (disposed) return
reconcileSessions()
scheduleNext()
}
/** Clear every persisted and in-memory usage bucket. */
function resetUsage(): void {
lastCredits = undefined
mutateUsage((days) => {
for (const date of Object.keys(days)) delete days[date]
})
}
tickTimer = setTimeout(tick, TICK_MS)
function dispose() {
@@ -140,8 +294,11 @@ export function createBalanceStore(
if (tickTimer) clearTimeout(tickTimer)
if (stateRetryTimer) clearTimeout(stateRetryTimer)
unsubscribeIdle()
unsubscribeStatus()
unsubscribeConnected()
}
return { state, now, refresh, dispose }
void backfillUsage()
return { state, now, usageDays, refresh, resetUsage, dispose }
}

274
src/usage.ts Normal file
View File

@@ -0,0 +1,274 @@
/**
* Pure usage accounting for GreenPT messages.
*
* Consumption is tracked in per-day buckets, each holding:
* - `creditsSpent`: real credit spend observed as the delta between two
* balance probes (all clients of the API key combined);
* - `byModel`: per-model token/cost/request totals reconciled from local
* OpenCode assistant messages (only GreenPT-provider messages count).
*
* Aggregation is idempotent: assistant messages are de-duplicated by id, so
* reconciling the same session repeatedly is safe.
*/
export type UsageTokens = {
input: number
output: number
reasoning: number
cacheRead: number
cacheWrite: number
}
export type ModelUsage = {
requests: number
tokens: UsageTokens
cost: number
}
export type DayUsage = {
date: string
creditsSpent: number
byModel: Record<string, ModelUsage>
ids: string[]
}
export type UsageDays = Record<string, DayUsage>
export type ModelTotal = {
model: string
usage: ModelUsage
}
export type UsageTotals = {
creditsSpent: number
cost: number
requests: number
tokens: UsageTokens
byModel: ModelTotal[]
}
/** Structural subset of an OpenCode assistant message used for accounting. */
export type UsageMessage = {
id?: unknown
role?: unknown
providerID?: unknown
modelID?: unknown
cost?: unknown
time?: { created?: unknown }
tokens?: {
input?: unknown
output?: unknown
reasoning?: unknown
cache?: { read?: unknown; write?: unknown }
}
}
/** Messages from this provider pattern are accounted for. */
export const GREENPT_PROVIDER_PATTERN = /greenpt/i
/** Upper bound on remembered message ids per day, keeping KV payloads bounded. */
const MAX_IDS_PER_DAY = 5000
function num(value: unknown): number {
return typeof value === "number" && Number.isFinite(value) ? value : 0
}
export function emptyTokens(): UsageTokens {
return { input: 0, output: 0, reasoning: 0, cacheRead: 0, cacheWrite: 0 }
}
function addTokens(target: UsageTokens, source: UsageMessage["tokens"]): void {
target.input += num(source?.input)
target.output += num(source?.output)
target.reasoning += num(source?.reasoning)
target.cacheRead += num(source?.cache?.read)
target.cacheWrite += num(source?.cache?.write)
}
function totalTokens(tokens: UsageTokens): number {
return tokens.input + tokens.output + tokens.reasoning + tokens.cacheRead + tokens.cacheWrite
}
/** Local-date key (`YYYY-MM-DD`) for a timestamp. */
export function dayKey(ms: number): string {
const d = new Date(ms)
const month = String(d.getMonth() + 1).padStart(2, "0")
const day = String(d.getDate()).padStart(2, "0")
return `${d.getFullYear()}-${month}-${day}`
}
export function monthPrefix(ms: number): string {
return dayKey(ms).slice(0, 7)
}
export function emptyDay(date: string): DayUsage {
return { date, creditsSpent: 0, byModel: {}, ids: [] }
}
export function isUsageMessage(value: unknown): value is UsageMessage {
if (typeof value !== "object" || value === null) return false
const message = value as UsageMessage
return message.role === "assistant" && typeof message.id === "string"
}
/**
* Record one assistant message into its day bucket.
*
* @returns `true` when the message was newly recorded.
*/
export function recordMessage(days: UsageDays, message: UsageMessage): boolean {
const provider = typeof message.providerID === "string" ? message.providerID : ""
if (provider && !GREENPT_PROVIDER_PATTERN.test(provider)) return false
const created = message.time?.created
if (typeof created !== "number" || !Number.isFinite(created)) return false
const tokens = emptyTokens()
addTokens(tokens, message.tokens)
const cost = num(message.cost)
if (totalTokens(tokens) === 0 && cost === 0) return false
const model = typeof message.modelID === "string" && message.modelID ? message.modelID : "unknown"
const date = dayKey(created)
const day = days[date] ?? emptyDay(date)
if (day.ids.includes(message.id as string)) return false
days[date] = day
day.ids.push(message.id as string)
if (day.ids.length > MAX_IDS_PER_DAY) day.ids.splice(0, day.ids.length - MAX_IDS_PER_DAY)
const usage = day.byModel[model] ?? { requests: 0, tokens: emptyTokens(), cost: 0 }
day.byModel[model] = usage
usage.requests += 1
usage.cost += cost
addTokens(usage.tokens, message.tokens)
return true
}
/** Reconcile a batch of messages (assistant ones are accounted for). */
export function applyMessages(days: UsageDays, messages: ReadonlyArray<unknown>): void {
for (const message of messages) {
if (isUsageMessage(message)) recordMessage(days, message)
}
}
/**
* Add real credit spend to the bucket of the given day.
*
* Only positive deltas count as spend; a higher new balance means the key
* was topped up and must not be accounted.
*/
export function addCreditsSpent(days: UsageDays, ms: number, delta: number): void {
if (!Number.isFinite(delta) || delta <= 0) return
const date = dayKey(ms)
const day = days[date] ?? emptyDay(date)
days[date] = day
day.creditsSpent += delta
}
function totalsFrom(days: UsageDays, filter: (date: string) => boolean): UsageTotals {
const tokens = emptyTokens()
const byModel = new Map<string, ModelUsage>()
let creditsSpent = 0
let cost = 0
let requests = 0
for (const [date, day] of Object.entries(days)) {
if (!filter(date)) continue
creditsSpent += day.creditsSpent
for (const [model, usage] of Object.entries(day.byModel)) {
addTokens(tokens, usage.tokens)
cost += usage.cost
requests += usage.requests
const total = byModel.get(model) ?? { requests: 0, tokens: emptyTokens(), cost: 0 }
total.requests += usage.requests
total.cost += usage.cost
addTokens(total.tokens, usage.tokens)
byModel.set(model, total)
}
}
return {
creditsSpent,
cost,
requests,
tokens,
byModel: [...byModel.entries()]
.map(([model, usage]) => ({ model, usage }))
.sort((a, b) => b.usage.cost - a.usage.cost || b.usage.tokens.output - a.usage.tokens.output),
}
}
export function dayTotals(days: UsageDays, ms: number): UsageTotals {
const key = dayKey(ms)
return totalsFrom(days, (date) => date === key)
}
export function monthTotals(days: UsageDays, ms: number): UsageTotals {
const prefix = monthPrefix(ms)
return totalsFrom(days, (date) => date.startsWith(prefix))
}
/** Remove buckets older than the retention window. */
export function pruneUsage(days: UsageDays, windowDays: number, nowMs: number): void {
if (!(windowDays > 0)) return
const cutoff = dayKey(nowMs - (windowDays - 1) * 86_400_000)
for (const date of Object.keys(days)) {
if (date < cutoff) delete days[date]
}
}
/** Serialize days for the KV store. */
export function serializeUsage(days: UsageDays): unknown {
return {
version: 1,
days: Object.values(days).map((day) => ({
date: day.date,
creditsSpent: day.creditsSpent,
byModel: day.byModel,
ids: day.ids,
})),
}
}
/** Parse a KV payload, returning only structurally valid buckets. */
export function parseUsageDays(raw: unknown): UsageDays {
const days: UsageDays = {}
if (typeof raw !== "object" || raw === null) return days
const payload = raw as { version?: unknown; days?: unknown }
if (payload.version !== 1 || !Array.isArray(payload.days)) return days
for (const entry of payload.days) {
if (typeof entry !== "object" || entry === null) continue
const day = entry as Partial<DayUsage>
if (typeof day.date !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(day.date)) continue
if (typeof day.creditsSpent !== "number" || !Number.isFinite(day.creditsSpent)) continue
if (!Array.isArray(day.ids)) continue
const byModel: Record<string, ModelUsage> = {}
if (typeof day.byModel === "object" && day.byModel !== null) {
for (const [model, usage] of Object.entries(day.byModel as Record<string, unknown>)) {
if (typeof usage !== "object" || usage === null) continue
const item = usage as { requests?: unknown; tokens?: unknown; cost?: unknown }
const requests = num(item.requests)
const cost = num(item.cost)
if (typeof item.tokens !== "object" || item.tokens === null) continue
const tokens = item.tokens as Record<string, unknown>
byModel[model] = {
requests,
cost,
tokens: {
input: num(tokens.input),
output: num(tokens.output),
reasoning: num(tokens.reasoning),
cacheRead: num(tokens.cacheRead),
cacheWrite: num(tokens.cacheWrite),
},
}
}
}
days[day.date] = {
date: day.date,
creditsSpent: day.creditsSpent,
byModel,
ids: Array.isArray(day.ids) ? day.ids.filter((id): id is string => typeof id === "string") : [],
}
}
return days
}

View File

@@ -1,11 +1,21 @@
/** @jsxImportSource @opentui/solid */
import { createSignal, Match, Show, Switch } from "solid-js"
import { createSignal, For, Match, Show, Switch } from "solid-js"
import type { TuiPluginApi, TuiThemeCurrent } from "@opencode-ai/plugin/tui"
import { formatBalanceError, formatCredits, readFoldOpen, BALANCE_OPEN_KV, DEFAULT_UNIT } from "./format"
import {
formatBalanceError,
formatCredits,
formatTokens,
readFoldOpen,
BALANCE_OPEN_KV,
DEFAULT_UNIT,
} from "./format"
import { dayTotals, monthTotals, type ModelTotal } from "./usage"
import type { BalanceStore } from "./store"
/**
* Sidebar section showing the remaining GreenPT credit balance.
* Sidebar section showing the remaining GreenPT credit balance and the
* consumption of the API key: real credit spend per day/month plus a
* per-model breakdown reconciled from local OpenCode messages.
*
* The section is hidden entirely when no GreenPT credential is available
* and `showWhenUnavailable` is false, so a colleague who does not use
@@ -61,6 +71,15 @@ export function GreenptWidget(props: {
return `updated ${hh}:${mm}`
}
const usage = () => props.store.usageDays()
const todayUsage = () => dayTotals(usage(), props.store.now())
const monthUsage = () => monthTotals(usage(), props.store.now())
const usageVisible = () => todayUsage().creditsSpent > 0 || monthUsage().cost > 0
const todayText = () => `${formatCredits(todayUsage().creditsSpent)} ${unit()}`
const monthText = () => `${formatCredits(monthUsage().cost)} ${unit()}`
const foldedSuffix = () => (usageVisible() ? ` · today ${todayText()}` : "")
const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, 5)
return (
<Show when={!hidden()} fallback={<box />}>
<box flexDirection="column" gap={1}>
@@ -68,7 +87,13 @@ export function GreenptWidget(props: {
<span style={{ fg: theme().textMuted }}>{open() ? "▼ " : "▶ "}</span>
<b>GreenPT</b>
<Show when={!open() && balanceText()}>
{(text) => <span style={{ fg: theme()[balanceColor()] }}> {text()}</span>}
{(text) => (
<span style={{ fg: theme()[balanceColor()] }}>
{" "}
{text()}
{foldedSuffix()}
</span>
)}
</Show>
</text>
<Show when={open()}>
@@ -92,6 +117,26 @@ export function GreenptWidget(props: {
<text fg={theme().textMuted}>{formatBalanceError(error()!)}</text>
</Match>
</Switch>
<Show when={usageVisible()}>
<box flexDirection="row" gap={1} justifyContent="space-between">
<text fg={theme().textMuted}>Spent</text>
<box flexDirection="row" gap={1}>
<text fg={theme().text}>today {todayText()}</text>
<text fg={theme().textMuted}>month {monthText()}</text>
</box>
</box>
<For each={modelRows()}>
{(row) => (
<box flexDirection="row" gap={1} justifyContent="space-between">
<text fg={theme().textMuted}>{row.model}</text>
<box flexDirection="row" gap={1}>
<text fg={theme().textMuted}>{formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok</text>
<text fg={theme().text}>~{formatCredits(row.usage.cost)} {unit()}</text>
</box>
</box>
)}
</For>
</Show>
</Show>
</box>
</Show>

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"
import { formatBalanceError, formatCredits, readFoldOpen } from "../src/format"
import { formatBalanceError, formatCredits, formatTokens, readFoldOpen } from "../src/format"
describe("formatCredits", () => {
it("formats with two decimals", () => {
@@ -15,6 +15,32 @@ describe("formatCredits", () => {
})
})
describe("formatTokens", () => {
it("formats small counts as integers", () => {
expect(formatTokens(842)).toBe("842")
expect(formatTokens(0)).toBe("0")
})
it("formats thousands with one decimal below 100k", () => {
expect(formatTokens(12_400)).toBe("12.4k")
expect(formatTokens(999)).toBe("999")
})
it("rounds thousands above 100k", () => {
expect(formatTokens(234_500)).toBe("235k")
})
it("formats millions", () => {
expect(formatTokens(3_200_000)).toBe("3.2M")
expect(formatTokens(45_000_000)).toBe("45.0M")
})
it("handles non-finite and negative input", () => {
expect(formatTokens(Number.NaN)).toBe("0")
expect(formatTokens(-5)).toBe("0")
})
})
describe("formatBalanceError", () => {
it("renders each error kind", () => {
expect(formatBalanceError({ kind: "unauthorized" })).toContain("invalid")

154
test/store.test.ts Normal file
View File

@@ -0,0 +1,154 @@
import { describe, expect, it, vi } from "vitest"
import { createBalanceStore, type BalanceOptions } from "../src/store"
import { nativeFetch } from "../src/balance"
import { serializeUsage, type UsageDays } from "../src/usage"
const NOW = new Date(2026, 9, 19, 15, 0, 0).getTime()
const DAY = "2026-10-19"
type FakeMessage = Record<string, unknown>
function makeApi() {
const kvData = new Map<string, unknown>()
const messages = new Map<string, FakeMessage[]>()
const handlers = new Map<string, Array<(event: unknown) => void>>()
const api = {
event: {
on: (type: string, handler: (event: unknown) => void) => {
const list = handlers.get(type) ?? []
list.push(handler)
handlers.set(type, list)
return () => {
handlers.set(
type,
(handlers.get(type) ?? []).filter((item) => item !== handler),
)
}
},
},
kv: {
ready: true,
get: (key: string, fallback?: unknown) => (kvData.has(key) ? kvData.get(key) : fallback),
set: (key: string, value: unknown) => {
kvData.set(key, value)
},
},
state: {
path: { state: "/tmp/fake-state" },
session: {
messages: (sessionID: string) => messages.get(sessionID) ?? [],
},
},
client: {
session: {
list: async () => ({ data: [] }),
messages: async () => ({ data: [] }),
},
},
emit(type: string, event: unknown) {
for (const handler of handlers.get(type) ?? []) handler(event)
},
}
return { api, kvData, messages }
}
function balanceResponse(credits: number) {
return new Response(null, { status: 400, headers: { "x-credits-remaining": String(credits) } })
}
function fetchSequence(credits: number[]) {
let index = 0
return vi.fn(() => {
const response = balanceResponse(credits[Math.min(index, credits.length - 1)] ?? 0)
index += 1
return Promise.resolve(response)
}) as unknown as typeof nativeFetch
}
function createTrackedStore(api: unknown, options: BalanceOptions = {}) {
return createBalanceStore(api as never, { apiKey: "test-key", ...options }, {}, fetchSequence([10]))
}
describe("usage tracking in the balance store", () => {
it("accumulates real credit spend from balance deltas", async () => {
const { api } = makeApi()
const store = createBalanceStore(
api as never,
{ apiKey: "test-key" },
{},
fetchSequence([10, 9.95, 9.9, 12]),
)
await store.refresh()
await store.refresh()
await store.refresh()
await store.refresh()
expect(store.usageDays()[DAY]?.creditsSpent).toBeCloseTo(0.1)
store.dispose()
})
it("reconciles per-model usage from tracked sessions on refresh", async () => {
const { api, messages } = makeApi()
const store = createTrackedStore(api)
messages.set("s1", [
{
id: "m1",
role: "assistant",
providerID: "greenpt",
modelID: "glm-5.3-flash",
cost: 0.02,
time: { created: NOW },
tokens: { input: 10, output: 5, reasoning: 0, cache: { read: 0, write: 0 } },
},
])
api.emit("session.idle", { properties: { sessionID: "s1" } })
await store.refresh()
await store.refresh()
expect(store.usageDays()[DAY]?.byModel["glm-5.3-flash"]?.cost).toBeCloseTo(0.02)
store.dispose()
})
it("persists buckets to the KV store and reloads them", async () => {
const { api, kvData } = makeApi()
const first = createBalanceStore(
api as never,
{ apiKey: "test-key" },
{},
fetchSequence([10, 9.9]),
)
await first.refresh()
await first.refresh()
expect(kvData.has("greenpt.usage.days")).toBe(true)
first.dispose()
const stored: UsageDays = { [DAY]: { date: DAY, creditsSpent: 0.5, byModel: {}, ids: [] } }
kvData.set("greenpt.usage.days", serializeUsage(stored))
const second = createTrackedStore(api)
expect(second.usageDays()[DAY]?.creditsSpent).toBeCloseTo(0.5)
second.dispose()
})
it("resetUsage clears buckets", async () => {
const { api } = makeApi()
const store = createTrackedStore(api)
await store.refresh()
await store.refresh()
store.resetUsage()
expect(Object.keys(store.usageDays()).length).toBe(0)
store.dispose()
})
it("skips tracking when disabled", async () => {
const { api, kvData } = makeApi()
const store = createBalanceStore(
api as never,
{ apiKey: "test-key", trackUsage: false },
{},
fetchSequence([10, 5]),
)
await store.refresh()
await store.refresh()
expect(Object.keys(store.usageDays()).length).toBe(0)
expect(kvData.has("greenpt.usage.days")).toBe(false)
store.dispose()
})
})

153
test/usage.test.ts Normal file
View File

@@ -0,0 +1,153 @@
import { describe, expect, it } from "vitest"
import {
addCreditsSpent,
applyMessages,
dayKey,
dayTotals,
monthTotals,
parseUsageDays,
pruneUsage,
recordMessage,
serializeUsage,
type UsageDays,
} from "../src/usage"
const NOW = new Date(2026, 9, 19, 15, 30, 0).getTime()
function assistantMessage(overrides: Record<string, unknown> = {}) {
return {
id: "msg-1",
role: "assistant",
providerID: "greenpt",
modelID: "glm-5.3-flash",
cost: 0.01,
time: { created: NOW },
tokens: { input: 100, output: 50, reasoning: 10, cache: { read: 5, write: 2 } },
...overrides,
}
}
describe("dayKey", () => {
it("formats a local date key", () => {
expect(dayKey(new Date(2026, 0, 3, 8, 0).getTime())).toBe("2026-01-03")
})
})
describe("recordMessage", () => {
it("records tokens and cost per model", () => {
const days: UsageDays = {}
expect(recordMessage(days, assistantMessage())).toBe(true)
const day = days[dayKey(NOW)]!
expect(day.byModel["glm-5.3-flash"]!.requests).toBe(1)
expect(day.byModel["glm-5.3-flash"]!.cost).toBe(0.01)
expect(day.byModel["glm-5.3-flash"]!.tokens.input).toBe(100)
expect(day.byModel["glm-5.3-flash"]!.tokens.cacheRead).toBe(5)
})
it("is idempotent for the same message id", () => {
const days: UsageDays = {}
recordMessage(days, assistantMessage())
expect(recordMessage(days, assistantMessage())).toBe(false)
expect(Object.keys(days[dayKey(NOW)]!.byModel).length).toBe(1)
})
it("ignores non-GreenPT providers", () => {
const days: UsageDays = {}
expect(recordMessage(days, assistantMessage({ providerID: "anthropic" }))).toBe(false)
expect(Object.keys(days).length).toBe(0)
})
it("buckets messages into their own day", () => {
const yesterday = NOW - 86_400_000
const days: UsageDays = {}
recordMessage(days, assistantMessage())
recordMessage(days, assistantMessage({ id: "msg-2", time: { created: yesterday } }))
expect(Object.keys(days).sort().length).toBe(2)
})
it("ignores messages without tokens or cost", () => {
const days: UsageDays = {}
expect(
recordMessage(days, assistantMessage({ cost: 0, tokens: undefined })),
).toBe(false)
expect(Object.keys(days).length).toBe(0)
})
})
describe("applyMessages", () => {
it("skips user messages and unknown shapes", () => {
const days: UsageDays = {}
applyMessages(days, [
{ id: "u1", role: "user" },
assistantMessage(),
null,
undefined,
])
expect(Object.keys(days[dayKey(NOW)]!.byModel).length).toBe(1)
})
})
describe("addCreditsSpent", () => {
it("accumulates positive deltas only", () => {
const days: UsageDays = {}
addCreditsSpent(days, NOW, 0.02)
addCreditsSpent(days, NOW, 0.01)
addCreditsSpent(days, NOW, -5)
addCreditsSpent(days, NOW, Number.NaN)
expect(days[dayKey(NOW)]!.creditsSpent).toBeCloseTo(0.03)
})
})
describe("totals", () => {
it("aggregates day and month with models sorted by cost", () => {
const days: UsageDays = {}
recordMessage(days, assistantMessage())
recordMessage(
days,
assistantMessage({ id: "msg-2", modelID: "kimi-k2", cost: 0.05 }),
)
const day = dayTotals(days, NOW)
expect(day.byModel.map((row) => row.model)).toEqual(["kimi-k2", "glm-5.3-flash"])
expect(day.cost).toBeCloseTo(0.06)
expect(day.creditsSpent).toBe(0)
addCreditsSpent(days, NOW, 0.1)
expect(dayTotals(days, NOW).creditsSpent).toBeCloseTo(0.1)
expect(monthTotals(days, NOW).creditsSpent).toBeCloseTo(0.1)
})
it("returns zero totals for empty buckets", () => {
const totals = dayTotals({}, NOW)
expect(totals.creditsSpent).toBe(0)
expect(totals.byModel).toEqual([])
})
})
describe("pruneUsage", () => {
it("drops buckets older than the window", () => {
const days: UsageDays = {}
const old = NOW - 120 * 86_400_000
recordMessage(days, assistantMessage({ time: { created: old } }))
recordMessage(days, assistantMessage())
pruneUsage(days, 90, NOW)
expect(Object.keys(days)).toEqual([dayKey(NOW)])
})
})
describe("serialization round-trip", () => {
it("preserves buckets", () => {
const days: UsageDays = {}
recordMessage(days, assistantMessage())
addCreditsSpent(days, NOW, 0.07)
const restored = parseUsageDays(serializeUsage(days))
expect(dayTotals(restored, NOW).creditsSpent).toBeCloseTo(0.07)
expect(dayTotals(restored, NOW).byModel[0]?.model).toBe("glm-5.3-flash")
})
it("tolerates malformed payloads", () => {
expect(parseUsageDays(undefined)).toEqual({})
expect(parseUsageDays("nope")).toEqual({})
expect(parseUsageDays({ version: 2, days: [] })).toEqual({})
expect(parseUsageDays({ version: 1, days: [{ date: "bad" }] })).toEqual({})
})
})