Skip to content

Add OpenCode Go usage support with dashboard credentials and local DB fallback#50

Open
jmtt89 wants to merge 1 commit into
CodeZeno:mainfrom
jmtt89:feat/opencode-go-support
Open

Add OpenCode Go usage support with dashboard credentials and local DB fallback#50
jmtt89 wants to merge 1 commit into
CodeZeno:mainfrom
jmtt89:feat/opencode-go-support

Conversation

@jmtt89

@jmtt89 jmtt89 commented Jul 3, 2026

Copy link
Copy Markdown

Summary

Adds an optional OpenCode Go model to the widget, alongside the existing Claude Code, Codex, and Antigravity providers. The first gauge always shows the rolling 5h window; the second gauge shows whichever of the 7-day and 30-day windows is currently more used (with the window name prefixed in the value text so the user can tell which one is being displayed).

Two data sources are supported, in priority order:

  1. OpenCode dashboard (recommended) — uses the user auth cookie and workspace ID to fetch real Go usage from https://opencode.ai/workspace/<wrk_id>/go. The dashboard returns usagePercent and resetInSec per window (rolling / weekly / monthly), so the widget shows the actual server-side reset countdowns and includes usage from other devices.

  2. Local SQLite database (fallback) — if no dashboard credentials are configured, the monitor falls back to the local opencode.db opened in SQLITE_OPEN_READ_ONLY mode. This reflects only this machine session costs and shows estimated 5h / 7d reset windows from the current poll time (the local DB has no server-side reset signal).

How it works

  • The tray icon uses a green badge style (#2EBD85) with a white "O" label.
  • Credentials are discovered in this order:
    • OPENCODE_GO_WORKSPACE_ID + OPENCODE_GO_AUTH_COOKIE environment variables
    • The path from OPENCODE_GO_CONFIG_FILE (if set)
    • $XDG_CONFIG_HOME/opencode-bar/opencode-go.json (or opencode-quota/)
    • ~/.config/opencode-bar/opencode-go.json (or opencode-quota/)
    • %APPDATA%\opencode-go\config.json (Windows-friendly default)
    • Falls back to the local DB at $OPENCODE_DB~/.local/share/opencode/opencode.db%APPDATA%\opencode.db
  • The dashboard HTML is parsed with the same regex approach used by opgginc/opencode-bar (escaped JSON and Solid $R[N] resource references are both supported).
  • The window picker (pick_opencode_second_window) compares weekly.usage_percent against monthly.usage_percent and uses the larger one for the second gauge. If only one window is present, that one is used; if both are missing, the gauge is empty.

Setup for dashboard mode

  1. Sign in to https://opencode.ai/go in your browser.
  2. Open DevTools → Application → Cookies → https://opencode.ai and copy the value of auth.
  3. Copy the workspace ID from the URL (/workspace/<wrk_id>/go).
  4. Create %APPDATA%\opencode-go\config.json with:
    {
      "workspaceId": "wrk_01...",
      "authCookie": "eyJhbGciOi..."
    }
    (or set the two environment variables instead).
  5. Enable OpenCode from the right-click Models menu.

Setup for local DB fallback

Just install OpenCode, sign in to a Go subscription, run at least one session so the local database exists, and enable OpenCode from the Models menu. No additional configuration is required.

Notes

  • The dashboard call is fully read-only; the monitor never writes to the OpenCode account, cookies, or local files.
  • The local DB path can be overridden with the OPENCODE_DB environment variable.
  • The credential watch signature detects changes to the API key, the dashboard config file, and the local DB mtime so the UI refreshes automatically when any of them change.
  • The OpenCode tray icon uses a green badge to match the rest of the provider visual language (warm colors for Claude, B&W for Codex, blue for Antigravity, green for OpenCode).

AI development disclosure

This feature was developed end-to-end with an AI coding agent (opencode-go running the MiniMax-M3 model). The development followed an iterative, user-driven workflow:

  1. Initial SQLite approach — the first pass read the local OpenCode SQLite database (opencode.db) and computed cost aggregations against hardcoded $12 / $30 limits.
  2. Path resolution fix — the local DB lives under the XDG data home (%USERPROFILE%\.local\share\opencode\opencode.db on Windows), not %APPDATA%\Roaming\opencode\opencode.db. The path discovery was extended to check XDG_DATA_HOME and the cross-platform default.
  3. Reset-time accuracy issue — the user reported the 7-day reset countdown was wrong (showed 6 days; real was 3). Two attempts were made to estimate the reset from the local DB alone (a "rolling window" based on the oldest time_updated in the 7-day range). Both were rejected because the local DB only tracks this machine'"'"'s sessions, while the server-side reset depends on usage from all devices.
  4. Reference implementation — the user pointed to opgginc/opencode-bar, which solves the same problem by scraping the OpenCode dashboard HTML. The agent cloned the repo, read OpenCodeGoProvider.swift, and ported the approach to Rust: env vars / config file credential discovery, the Cookie: auth=<value> + desktop Chrome User-Agent request, and the two regex patterns (window body capture + numeric field extraction) that handle both the Next.js escaped-JSON shape and the Solid $R[N] resource-reference shape. The 3 parser test fixtures from the Swift test suite were used to validate the Rust port.
  5. Local DB fallback — the user requested the original SQLite approach be kept as a fallback for users who don'"'"'t want to set up dashboard credentials. The local DB poll was restored alongside the dashboard poll, with the local mode becoming active only when no dashboard credentials are configured.
  6. Dynamic second gauge — the user asked for the second gauge to surface whichever of 7d / 30d is currently more used. A new pick_opencode_second_window helper compares the two windows; the chosen window'"'"'s label is carried through UsageData.weekly_label and prepended to the value text at render time.
  7. Localizations — error and label strings updated in all 10 languages (English, Spanish, Dutch, French, German, Japanese, Korean, Portuguese (Brazil), Russian, Traditional Chinese).

All 18 changed files were reviewed and the final build (cargo build --release) compiles cleanly with zero warnings.

… fallback

Adds an optional OpenCode Go model to the widget. The first gauge shows the rolling 5h window; the second gauge dynamically shows whichever of the 7-day and 30-day windows is currently more used.

Two data sources are supported in priority order:

- OpenCode dashboard (https://opencode.ai/workspace/<wrk_id>/go) using the user's auth cookie and workspace ID, with server-side reset times for the full subscription.

- Local SQLite database (opencode.db) opened in read-only mode as a fallback that requires no extra configuration but only reflects this machine's session costs.

The dashboard parsing approach is ported from opgginc/opencode-bar (Swift) and validated against its test fixtures. Localizations updated in all 10 languages. New dependency: regex 1.
@jmtt89
jmtt89 marked this pull request as ready for review July 3, 2026 18:35
@sim186

sim186 commented Jul 14, 2026

Copy link
Copy Markdown

Would be awesome to see this merged :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants