Skip to content

feat(opencode): show quota usage toast after quota refresh#36

Open
iceteaSA wants to merge 2 commits into
cortexkit:mainfrom
iceteaSA:feat/quota-toast
Open

feat(opencode): show quota usage toast after quota refresh#36
iceteaSA wants to merge 2 commits into
cortexkit:mainfrom
iceteaSA:feat/quota-toast

Conversation

@iceteaSA
Copy link
Copy Markdown
Contributor

@iceteaSA iceteaSA commented May 21, 2026

Rebased onto upstream/main (v1.5.0) now that #34 (QuotaManager) is merged — this branch no longer depends on feat/quota-manager.

Displays quota-usage bar notifications via client.tui.showToast after quota data is refreshed.

  • Shows main and enabled fallback account usage with visual bars (█░).
  • Padded percentage and reset time per account; formatResetIn handles sub-1-minute (<1m), "now", and invalid-date inputs.
  • Toast variant reflects severity: info (<70%), warning (≥70%), error (≥90%).
  • Clamped bar rendering prevents RangeError on >100% utilization.
  • Reads main/fallback quota from the shared, token-aware QuotaManager cache (not the request-start snapshot).
  • The "active" account label mirrors actual routing: the first fallback that passes quota policy, otherwise main — never a failing fallback.
  • Fires on a synchronous cold-start cache miss and on the resolved .then() of each periodic background refresh; never blocks the request.

Single file change: packages/opencode/src/index.ts.


Summary by cubic

Shows a quota usage toast after quota refresh, matching the TUI sidebar. Uses the shared QuotaManager from @cortexkit/anthropic-auth-core to keep requests non‑blocking and display a toast when refresh completes, including background every‑N refreshes.

  • New Features
    • Toast via client.tui.showToast: shows main and enabled fallbacks with shared-width █░ bars, padded percent, 5h/7d lines, reset-in time (<1m/now supported), active/idle labels; clamps >100%; info/warning/error variants.
    • Triggers after refreshMain completes and after periodic background refreshes; no request blocking.
    • Uses QuotaManager cache for main and fallback snapshots; picks the active account that passes policy (mirrors routing).

Written for commit 617634b. Summary will update on new commits.

Review in cubic

Greptile Summary

This PR wires quota usage toast notifications into the OpenCode plugin after each QuotaManager main-quota refresh. It adds four helper functions (quotaBar, quotaLine, formatResetIn, showQuotaToast) and an inner showQuotaToastFromCache that reads the shared QuotaManager cache to build per-account bars, labels the active route, and fires client.tui.showToast with an info/warning/error variant.

  • New helpers render visual quota bars and human-readable reset times; quotaBar clamps >100 % correctly and formatResetIn now returns \"resets <1m\" for sub-minute windows.
  • showQuotaToastFromCache is called both on the inline (blocking) refresh path and in the .then() callback of the background every-N refresh, keeping it non-blocking on the critical request path.
  • The toast variant is determined by globalMaxUsed across all displayed accounts; an idle fallback near exhaustion can drive the error variant even when the active account has plenty of quota, which may mislead users.

Confidence Score: 5/5

Safe to merge — the change is display-only (toast notifications) and does not touch routing logic, token handling, or quota policy decisions.

The toast helpers are all pure display functions that are fire-and-forget; any exception in the showToast call is silently swallowed by .catch(() => {}). The showQuotaToastFromCache function is correctly scoped and uses the same QuotaManager cache that drives the sidebar. None of the new code affects routing, token refresh, or quota enforcement paths.

packages/opencode/src/index.ts — specifically the variant-selection logic in showQuotaToast and the reset-time display in the account header lines.

Important Files Changed

Filename Overview
packages/opencode/src/index.ts Adds quota toast helpers (quotaBar, quotaLine, formatResetIn, showQuotaToast) and wires showQuotaToastFromCache into the main-quota refresh paths; toast variant and reset-time display have minor UX gaps for edge cases

Sequence Diagram

sequenceDiagram
    participant FH as Fetch Handler
    participant QM as QuotaManager
    participant TC as showQuotaToastFromCache
    participant TUI as client.tui.showToast

    FH->>QM: getMain(auth.access)
    alt cache miss (!routingQuota)
        FH->>QM: await refreshMain(auth.access)
        QM-->>FH: OAuthQuotaSnapshot
        FH->>TC: showQuotaToastFromCache() [sync]
        TC->>QM: getMain() [no token, display]
        TC->>QM: getFallback(id, access) per account
        TC->>TUI: "void showToast({ variant, message })"
    else needsRefresh (every-N or stale)
        FH->>QM: void refreshMain(auth.access)
        Note over FH: Non-blocking
        QM-->>FH: .then() resolves
        FH->>FH: writeSidebarState(...)
        FH->>TC: showQuotaToastFromCache() [in .then]
        TC->>QM: getMain() [no token, display]
        TC->>TUI: "void showToast({ variant, message })"
    end
Loading

Reviews (3): Last reviewed commit: "feat(opencode): restyle quota toast to m..." | Re-trigger Greptile

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/opencode/src/index.ts">

<violation number="1" location="packages/opencode/src/index.ts:428">
P2: Fallback quota command output drops fresh per-account refresh errors by always passing an empty error map.</violation>
</file>

<file name="packages/core/src/quota-manager.ts">

<violation number="1" location="packages/core/src/quota-manager.ts:88">
P2: Persisted main quota cache uses interval-only staleness instead of policy-aware `getQuotaNextRefreshAt`, causing premature refreshes.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread packages/opencode/src/index.ts Outdated
Comment thread packages/core/src/quota-manager.ts Outdated
Comment thread packages/core/src/quota-manager.ts
@iceteaSA iceteaSA force-pushed the feat/quota-toast branch 5 times, most recently from 4ad7d49 to b20b6c0 Compare May 22, 2026 17:08
@iceteaSA iceteaSA force-pushed the feat/quota-toast branch 8 times, most recently from 2ec54d4 to 95a1614 Compare June 1, 2026 14:51
Displays quota usage bar notifications via client.tui.showToast after
quota data is refreshed. Shows main and fallback account usage with
visual bars, percentage, and reset time. Toast variant reflects
severity (info < 70%, warning >= 70%, error >= 90%).
@iceteaSA iceteaSA force-pushed the feat/quota-toast branch from 95a1614 to 58f05a8 Compare June 3, 2026 18:21
Comment thread packages/opencode/src/index.ts
Comment thread packages/opencode/src/index.ts Outdated
Comment thread packages/opencode/src/index.ts
@iceteaSA iceteaSA force-pushed the feat/quota-toast branch from 58f05a8 to f4bee10 Compare June 3, 2026 18:50
Align the quota refresh toast with the sidebar's visual language:

- Replace the emoji status dots with status words (active/idle)
- Use the shared bar width and a padded percentage via a quotaLine helper
- Rename the seven-day label from 1w to 7d to match the sidebar
- Keep severity-driven variant color (info/warning/error)
@iceteaSA iceteaSA force-pushed the feat/quota-toast branch from f4bee10 to 617634b Compare June 3, 2026 19:51
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.

1 participant