The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(opencode-go): show Go plan windows in /usage through the profile hook
teknium1627a12a90e51
diff --git a/plugins/model-providers/opencode-zen/__init__.py b/plugins/model-providers/opencode-zen/__init__.pyindex 8fb437b506c..e422e4f6a16 100644--- a/plugins/model-providers/opencode-zen/__init__.py+++ b/plugins/model-providers/opencode-zen/__init__.py@@ -53,6 +53,40 @@ class OpenCodeGoProfile(ProviderProfile):cap = self._MODEL_MAX_TOKENS.get(_flat_model_name(model))return self.default_max_tokens if cap is None else cap+ def fetch_account_usage(self, *, base_url: str | None = None, api_key: str | None = None):+ """Go subscription windows for /usage via ``/zen/go/v1/usage`` (anomalyco/opencode#16513).++ Percent-based payload ``{"usage": {"rolling"|"weekly"|"monthly": {"percent", "resetsAt"}}}``.+ Literal endpoint, NOT the runtime base_url: that one loses its /v1 suffix in+ anthropic_messages mode and /usage only exists under /v1.+ """+ from datetime import datetime, timezone++ import httpx++ from agent.account_usage import AccountUsageSnapshot, AccountUsageWindow+ from hermes_cli.runtime_provider import resolve_runtime_provider++ runtime = resolve_runtime_provider(requested=self.name, explicit_base_url=base_url, explicit_api_key=api_key)+ token = str(runtime.get("api_key", "") or "").strip()+ if not token:+ return None+ with httpx.Client(timeout=10.0) as client:+ response = client.get("https://opencode.ai/zen/go/v1/usage",+ headers={"Authorization": f"Bearer {token}", "Accept": "application/json"})+ response.raise_for_status()+ usage = (response.json() or {}).get("usage") or {}+ windows = []+ for key, label in (("rolling", "Rolling window"), ("weekly", "Weekly"), ("monthly", "Monthly")):+ window = usage.get(key) or {}+ if window.get("percent") is None:+ continue+ reset_raw = str(window.get("resetsAt") or "").replace("Z", "+00:00")+ reset_at = datetime.fromisoformat(reset_raw) if reset_raw else None+ windows.append(AccountUsageWindow(label=label, used_percent=float(window["percent"]), reset_at=reset_at))+ return AccountUsageSnapshot(provider=self.name, source="go_usage_api",+ fetched_at=datetime.now(timezone.utc), windows=tuple(windows))+def build_api_kwargs_extras(self, *, reasoning_config: dict | None = None, model: str | None = None, **context) -> tuple[dict[str, Any], dict[str, Any]]:diff --git a/providers/base.py b/providers/base.pyindex 91859963f22..45b4c6664b0 100644--- a/providers/base.py+++ b/providers/base.py@@ -13,11 +13,10 @@ from __future__ import annotationsimport loggingfrom dataclasses import dataclass, field-from typing import TYPE_CHECKING, Any+from typing import TYPE_CHECKING, Any, Callableif TYPE_CHECKING:from agent.account_usage import AccountUsageSnapshot-from typing import Any, Callablelogger = logging.getLogger(__name__)diff --git a/tests/plugins/model_providers/test_opencode_go_profile.py b/tests/plugins/model_providers/test_opencode_go_profile.pyindex 0540472c1b0..7f50c4da896 100644--- a/tests/plugins/model_providers/test_opencode_go_profile.py+++ b/tests/plugins/model_providers/test_opencode_go_profile.py@@ -260,3 +260,53 @@ class TestOpenCodeGoFullKwargsIntegration:assert "extra_body" not in kwargsassert kwargs["reasoning_effort"] == "high"+++def test_opencode_go_plan_windows_reach_usage_through_profile_hook(opencode_go_profile, monkeypatch):+ """The Go plan's rolling/weekly/monthly windows feed /usage via the profile hook — no core table entry."""+ from datetime import datetime, timezone++ from agent.account_usage import fetch_account_usage, render_account_usage_lines++ class _Response:+ status_code = 200++ def raise_for_status(self):+ return None++ def json(self):+ return {"usage": {+ "rolling": {"status": "ok", "percent": 3, "resetsAt": "2026-09-16T21:44:55.176Z"},+ "weekly": {"status": "ok", "percent": 2, "resetsAt": "2026-09-21T00:00:00.176Z"},+ "monthly": {"status": "ok", "percent": 2, "resetsAt": "2026-10-13T02:13:38.176Z"},+ }}++ class _Client:+ def __init__(self, timeout=None):+ self.urls = []++ def __enter__(self):+ return self++ def __exit__(self, *exc):+ return False++ def get(self, url, headers=None):+ self.urls.append(url)+ return _Response()++ monkeypatch.setattr("httpx.Client", _Client)+ monkeypatch.setattr(+ "hermes_cli.runtime_provider.resolve_runtime_provider",+ # /v1 stripped, as anthropic_messages routing leaves it — the hook must not reuse this base_url.+ lambda requested, explicit_base_url=None, explicit_api_key=None: {+ "provider": "opencode-go", "base_url": "https://opencode.ai/zen/go", "api_key": "sk-test"},+ )++ snapshot = fetch_account_usage("opencode-go")++ assert snapshot is not None and snapshot.provider == "opencode-go"+ assert [(w.label, w.used_percent) for w in snapshot.windows] == [+ ("Rolling window", 3.0), ("Weekly", 2.0), ("Monthly", 2.0)]+ assert snapshot.windows[0].reset_at == datetime(2026, 9, 16, 21, 44, 55, 176000, tzinfo=timezone.utc)+ assert "97% remaining (3% used)" in "\n".join(render_account_usage_lines(snapshot))diff --git a/website/docs/developer-guide/model-provider-plugin.md b/website/docs/developer-guide/model-provider-plugin.mdindex 04e626c88f7..fcdb077ee26 100644--- a/website/docs/developer-guide/model-provider-plugin.md+++ b/website/docs/developer-guide/model-provider-plugin.md@@ -175,6 +175,10 @@ exception, leaves `/usage` empty just as it does for providers without usagedata. Built-in usage fetchers always take precedence, so this hook cannotreplace the account-usage behavior for a built-in provider.+The bundled `plugins/model-providers/opencode-zen/` profile implements this hook for the+OpenCode Go plan windows; every `/usage` surface (CLI `hermes usage` and `/usage`, the messaging+gateway, the TUI/Desktop usage feed) renders the snapshot through the same core formatter.+```pythonfrom datetime import datetime, timezone