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:
@@ -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
154
test/store.test.ts
Normal 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
153
test/usage.test.ts
Normal 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({})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user