opencode-copilot-chat icon indicating copy to clipboard operation
opencode-copilot-chat copied to clipboard

[FEATURE] Sync Go usage from official /zen/go/v1/usage endpoint

Open ltmoerdani opened this issue 3 months ago • 1 comments

✨ The feature

Have the Go usage status bar read Session (rolling 5h), Weekly, and Monthly meters from OpenCode's official /zen/go/v1/usage endpoint instead of estimating them locally. This makes the numbers account-wide and server-accurate.

🎯 The problem

Issue #23 reported drift from day one: the tracker showed "27d left" where opencode.ai said "19d left", and weekly/monthly percentages ran several points low. Root cause: OpenCode had no public usage API, so the extension had to estimate cost from tokens it saw and multiply by per-model pricing. That misses usage from the CLI/TUI, other devices, and anything before the extension was installed.

We shipped partial fixes: manual usage targets (#50) and a SQLite reader that pulls real billed amounts from the local opencode.db (#59/#60). They help, but SQLite is still per-machine, so cross-device usage stays out of the numbers.

Upstream just merged an official endpoint (anomalyco/opencode#16513): GET /zen/go/v1/usage. It authenticates with the same opencode-go key we already store in SecretStorage and returns rolling/weekly/monthly percent plus reset time computed server-side.

💡 Proposed solution

Add a sync layer in goUsageTracker:

  1. On refresh, call GET https://opencode.ai/zen/go/v1/usage with the existing opencodego.apiKey from SecretStorage (same key and base URL as the models fetch).
  2. Map the response onto UsageSummary:
    • usage.rolling → session (5h window, $12)
    • usage.weekly → weekly ($30)
    • usage.monthly → monthly ($60)
    • Use resetsAt for the "resets in" timer, which fixes the monthly anchor bug for real.
  3. Keep Today / Yesterday and per-session spend from local tracking + SQLite, since the API does not return those.
  4. Fall back to the current SQLite → tracked path when the API returns 401/403/404 or fails (offline, not live yet, no subscription).
  5. Never persist or log the key, and keep the existing "device only" wording for the local numbers.

🔄 Alternatives considered

  • Manual sync from opencode.ai, already shipped as "Set Usage Targets". Works, but needs a re-sync whenever usage happens elsewhere.
  • OAuth browser flow to read the dashboard directly. The upstream PR makes this unnecessary.
  • Rate-limit headers on ordinary responses. Not shipped yet, and a dedicated endpoint is cleaner.

📋 Additional context

  • Upstream PR: anomalyco/opencode#16513 (merged to dev Aug 11 2026; confirm it is live in production before building on it).
  • Follows the same split as robinebers/openusage#1095: API for account-wide limits, local history for spend.
  • Related: #23, #50, #59, #60.
  • The extension already talks to https://opencode.ai/zen/go/v1/* for models and chat completions, so no new auth surface is needed.

ltmoerdani avatar Aug 11 '26 23:08 ltmoerdani

Working on this issue — the sync layer is implemented and being verified on the feat/issue-130-go-usage-sync branch:

  • src/goUsageSync.ts: fetches GET /zen/go/v1/usage with the existing Go key (failure classification: unauthorized / no-subscription / not-found / network / invalid) and merges the server-accurate rolling/weekly/monthly percent + resetsAt onto the local summary (60s TTL cache, graceful fallback to local estimates).
  • Wired on startup, per-request (uses the exact key of the request, covering BYOK groups), and status-bar refresh.
  • Today/Yesterday/per-session spend stay device-local and are now labeled ⓘ Counted locally in the UI; the quick-pick Reset action was fixed (it previously did nothing).
  • 188/188 unit tests green; endpoint verified live in production.

Will open the PR once local verification is done.

Fahad090NP avatar Aug 12 '26 06:08 Fahad090NP