Skip to content

feat: expose Go and Zen usage - #41824

Closed
opencode-agent[bot] wants to merge 1 commit into
v2from
go-usage-api
Closed

opencode-agent[bot] wants to merge 1 commit into
v2from
go-usage-api

Conversation

@opencode-agent

Copy link
Copy Markdown
Contributor

Summary

  • add an authenticated Console endpoint for normalized Go quota windows and Zen billing usage
  • expose the account data through the OpenCode server at GET /api/usage using the active OpenCode credential
  • generate Promise and Effect client methods under client.usage.get()
  • return { connected: false } when no OpenCode account is configured and sanitize upstream failures as service-unavailable errors

Validation

  • typechecked schema, protocol, core, server, client, and Console app packages
  • core OpenCode provider tests (14 passed)
  • server usage endpoint test (1 passed)
  • client contract tests (26 passed)

Requested by: @kitlangton (Kit via Slack)

@Enough1122

Copy link
Copy Markdown

AI code review — automated review for reference; please use your judgment.

Overall: Compact and readable endpoint; the join structure is sensible. A few things worth tightening before merge:

  • packages/console/app/src/routes/api/usage.ts L18+L58 — useBalance: BillingTable.lite is aliased from BillingTable.lite but then read back as row.useBalance?.useBalance ?? false — i.e., a property named useBalance inside a column aliased useBalance; either that's accidentally reading the wrong field or the naming is maximally confusing — please double-check the actual column type and rename (e.g. liteRow/billingLite) so intent is obvious.
  • packages/console/app/src/routes/api/usage.ts L69/L71 — the bare 100_000_000 divisors encode "nano-dollars → USD" implicitly; extract a named constant (NANO_USD_PER_USD or similar) shared wherever this unit appears — and confirm balance/monthlyUsage are non-nullable: a NULL would surface as null/1e8 = 0... wait, actually as NaN-free but null-typed math only if TS lies; if they can be null the response silently contains NaN-adjacent values that break JSON.stringify consumers.
  • packages/console/app/src/routes/api/usage.ts L38+ — after auth succeeds there's no rate limiting or caching on a per-request DB round-trip keyed by bearer token; even a cheap in-memory throttle or Cache-Control: private, max-age=N header would blunt accidental hammering from clients polling usage.
  • packages/console/app/src/routes/api/usage.ts L12-L37 — no error handling around Database.use: a transient DB failure surfaces as the framework's default 500 (potentially leaking internals); wrapping in try/catch and returning a minimal { error: "Internal" } keeps the surface clean.
  • packages/console/app/src/routes/api/usage.ts L51/L54/L56 — timeUpdated: row.time*Updated ?? new Date() masks missing timestamps as "window just reset"; if usage counters exist but their timestamps are null that indicates corrupted rows — consider logging/skipping rather than fabricating a fresh timestamp.
  • tests — no coverage for the three 401 paths (missing header, malformed Bearer, deleted key) nor the go: undefined vs subscribed shapes; a couple of fixture-based cases would pin the contract.

@Enough1122

Copy link
Copy Markdown

AI code review - automated review for reference; please use your judgment.

  • packages/console/app/src/routes/api/usage.ts:47 - LiteData.getLimits() is called with no arguments; if limits vary by subscription tier/plan, every subscriber gets the same defaults. Verify this intentionally ignores tier.
  • packages/console/app/src/routes/api/usage.ts:53/58/63 - falling back to new Date() for missing time*Updated makes stale-never-updated usage look current to analyzeRollingUsage/analyzeWeeklyUsage; a zero/epoch sentinel would more honestly represent "no data" and avoid understating usage windows.
  • packages/console/app/src/routes/api/usage.ts:8-9 - no rate limiting or logging on a token-authenticated balance/usage endpoint; consider basic throttling since the token grants repeated polling of billing info.
  • No tests added for the new route - recommend covering authorized/unauthorized and go-vs-zen shape given this is a public-ish API surface.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants