The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: send Hermes Agent attribution headers to OpenCode Zen and Go
Adolanium7fa084f58ecf
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.pyindex 52a86513fb0..3c36ee3b91a 100644--- a/agent/anthropic_adapter.py+++ b/agent/anthropic_adapter.py@@ -474,6 +474,11 @@ def _is_kimi_coding_endpoint(base_url: str | None) -> bool:return normalized.rstrip("/").lower().startswith("https://api.kimi.com/coding")+def _is_opencode_endpoint(base_url: str | None) -> bool:+ """Return True for OpenCode's Zen/Go relay (opencode.ai)."""+ return base_url_host_matches(base_url or "", "opencode.ai")++# Model-name prefixes that identify the Kimi / Moonshot family. Covers# - official slugs: ``kimi-k2.5``, ``kimi_thinking``, ``moonshot-v1-8k``# - common release lines: ``k1.5-...``, ``k2-thinking``, ``k25-...``, ``k2.5-...``,@@ -913,6 +918,18 @@ def build_anthropic_client(if common_betas:kwargs["default_headers"] = {"anthropic-beta": ",".join(common_betas)}+ if _is_opencode_endpoint(base_url):+ # OpenCode identifies clients by request headers, like OpenRouter does.+ # The OpenAI-wire paths pick these up from profile.default_headers+ # (plugins/model-providers/opencode-zen), but the Anthropic Messages+ # route builds its client right here and never sees the profile. Merge+ # the same set on top of whatever auth branch ran above.+ headers = dict(kwargs.get("default_headers") or {})+ headers.setdefault("HTTP-Referer", "https://hermes-agent.nousresearch.com")+ headers.setdefault("X-Title", "Hermes Agent")+ headers.setdefault("User-Agent", f"HermesAgent/{_HERMES_VERSION}")+ kwargs["default_headers"] = headers+client = _anthropic_sdk.Anthropic(**kwargs)# Bearer-only construction leaves ``api_key`` unset, so the SDK fills it# from ``ANTHROPIC_API_KEY`` (Hermes loads that into the process env fromdiff --git a/plugins/model-providers/opencode-zen/__init__.py b/plugins/model-providers/opencode-zen/__init__.pyindex 8942660f5cc..7704442deba 100644--- a/plugins/model-providers/opencode-zen/__init__.py+++ b/plugins/model-providers/opencode-zen/__init__.py@@ -11,9 +11,21 @@ from __future__ import annotationsfrom typing import Any+from hermes_cli import __version__ as _HERMES_VERSIONfrom providers import register_providerfrom providers.base import ProviderProfile+# Attribution headers sent on every OpenCode request. Same values we send+# to OpenRouter, Vercel AI Gateway, and Fireworks. Going through+# profile.default_headers means they survive model switches and credential+# rotation. Without them OpenCode only sees the OpenAI SDK's generic+# "OpenAI/Python x.y.z" User-Agent and can't tell the traffic is Hermes Agent.+_ATTRIBUTION_HEADERS = {+ "HTTP-Referer": "https://hermes-agent.nousresearch.com",+ "X-Title": "Hermes Agent",+ "User-Agent": f"HermesAgent/{_HERMES_VERSION}",+}+def _flat_model_name(model: str | None) -> str:"""Return the bare OpenCode model ID, tolerating aggregator prefixes."""@@ -132,6 +144,7 @@ opencode_zen = ProviderProfile(aliases=("opencode", "opencode_zen", "zen"),env_vars=("OPENCODE_ZEN_API_KEY",),base_url="https://opencode.ai/zen/v1",+ default_headers=dict(_ATTRIBUTION_HEADERS),default_aux_model="gemini-3-flash",)@@ -140,6 +153,7 @@ opencode_go = OpenCodeGoProfile(aliases=("opencode_go", "go", "opencode-go-sub"),env_vars=("OPENCODE_GO_API_KEY",),base_url="https://opencode.ai/zen/go/v1",+ default_headers=dict(_ATTRIBUTION_HEADERS),default_aux_model="glm-5",)diff --git a/tests/agent/test_anthropic_adapter.py b/tests/agent/test_anthropic_adapter.pyindex 0b8a7b08bb7..e4cba60440b 100644--- a/tests/agent/test_anthropic_adapter.py+++ b/tests/agent/test_anthropic_adapter.py@@ -66,6 +66,27 @@ class TestBuildAnthropicClient:+ def test_opencode_endpoint_gets_attribution_headers(self):+ """OpenCode identifies clients by request headers, like OpenRouter.++ The OpenAI-wire paths get HTTP-Referer / X-Title / User-Agent from+ profile.default_headers. The Anthropic Messages route builds its+ client here and must merge the same set.+ """+ with patch("agent.anthropic_adapter._anthropic_sdk") as mock_sdk:+ build_anthropic_client(+ "sk-opencode-secret",+ base_url="https://opencode.ai/zen/go/v1",+ )+ kwargs = mock_sdk.Anthropic.call_args[1]+ headers = kwargs["default_headers"]+ assert headers["HTTP-Referer"] == "https://hermes-agent.nousresearch.com"+ assert headers["X-Title"] == "Hermes Agent"+ assert headers["User-Agent"].startswith("HermesAgent/")+ # Auth branch is unchanged: x-api-key via api_key, betas kept.+ assert kwargs["api_key"] == "sk-opencode-secret"+ assert "anthropic-beta" in headers+def test_minimax_anthropic_endpoint_uses_bearer_auth_for_regular_api_keys(self):with patch("agent.anthropic_adapter._anthropic_sdk") as mock_sdk:build_anthropic_client(diff --git a/tests/run_agent/test_provider_attribution_headers.py b/tests/run_agent/test_provider_attribution_headers.pyindex ff4ef15e07f..d24b28ef2c4 100644--- a/tests/run_agent/test_provider_attribution_headers.py+++ b/tests/run_agent/test_provider_attribution_headers.py@@ -111,6 +111,51 @@ def test_fireworks_applies_attribution_via_profile_fallback(mock_openai):assert headers["User-Agent"].startswith("HermesAgent/")+@patch("run_agent.OpenAI")+def test_opencode_go_applies_attribution_via_profile_fallback(mock_openai):+ """OpenCode (Zen/Go) attributes traffic by header like OpenRouter does.+ Without profile.default_headers the relay only sees the OpenAI SDK's+ generic User-Agent and Hermes Agent traffic shows up unattributed."""+ mock_openai.return_value = MagicMock()+ agent = AIAgent(+ api_key="test-key",+ base_url="https://opencode.ai/zen/go/v1",+ model="glm-5",+ provider="opencode-go",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ )++ agent._apply_client_headers_for_base_url("https://opencode.ai/zen/go/v1")++ headers = agent._client_kwargs["default_headers"]+ assert headers["HTTP-Referer"] == "https://hermes-agent.nousresearch.com"+ assert headers["X-Title"] == "Hermes Agent"+ assert headers["User-Agent"].startswith("HermesAgent/")+++@patch("run_agent.OpenAI")+def test_opencode_zen_applies_attribution_via_profile_fallback(mock_openai):+ mock_openai.return_value = MagicMock()+ agent = AIAgent(+ api_key="test-key",+ base_url="https://opencode.ai/zen/v1",+ model="claude-sonnet-4-5",+ provider="opencode-zen",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ )++ agent._apply_client_headers_for_base_url("https://opencode.ai/zen/v1")++ headers = agent._client_kwargs["default_headers"]+ assert headers["HTTP-Referer"] == "https://hermes-agent.nousresearch.com"+ assert headers["X-Title"] == "Hermes Agent"+ assert headers["User-Agent"].startswith("HermesAgent/")++@patch("run_agent.OpenAI")def test_routed_client_preserves_openai_sdk_custom_headers(mock_openai):mock_openai.return_value = MagicMock()