mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
feat(hub,cli): reconcile OpenCode usage from its own token ledger
The live pipeline records only the final model step of an OpenCode turn, so the usage dashboard showed roughly 42% of OpenCode's own totals. Add an offline reconciliation ledger instead of touching the streaming path (a previous attempt that fed cumulative counters into the live pipeline made the chat/status UI numbers jump): - shared: read and aggregate `~/.local/share/opencode/opencode.db` per (day, model), exposed as `@hapi/protocol/opencodeUsage` - hub: additive `usage_reconciliation` table (no schema bump, safe to roll back), local scan at startup and every 30 minutes, and the usage summary prefers snapshots over live events for reconciled sessions - cli: each runner scans its machine's store every 30 minutes and reports snapshots over the new `opencode-usage-report` socket event, covering remote machines and sessions whose process already exited - the hub-local scan skips sessions whose store lives on another machine, so it can never wipe remotely reported rows
This commit is contained in:
@@ -9,6 +9,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve as resolvePath }
|
||||
import { logger } from '@/ui/logger'
|
||||
import { configuration } from '@/configuration'
|
||||
import type { ClientToServerEvents, ServerToClientEvents, Update, UpdateMachineBody } from '@hapi/protocol'
|
||||
import type { OpencodeUsageSessionReport } from '@hapi/protocol/usage'
|
||||
import {
|
||||
ArchiveCodexSessionRpcRequestSchema,
|
||||
ListClaudeSessionsRpcRequestSchema,
|
||||
@@ -649,6 +650,14 @@ export class ApiMachineClient {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Report this machine's OpenCode usage snapshots to the hub. Best-effort:
|
||||
* a dropped report is re-sent by the next scan interval.
|
||||
*/
|
||||
reportOpencodeUsage(sessions: OpencodeUsageSessionReport[]): void {
|
||||
this.socket.emit('opencode-usage-report', { machineId: this.machine.id, sessions })
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
this.socket = io(`${configuration.apiUrl}/cli`, {
|
||||
transports: ['websocket'],
|
||||
|
||||
Reference in New Issue
Block a user