Ships the first half of the queued Hermes adapter expansion. PR 2 only
supported Nous Portal + OpenRouter; this adds 13 more providers reachable
via OpenAI-compat endpoints. Native SDK paths for Anthropic + Gemini are
Phase 2 (better tool-calling + vision fidelity).
## What's new
**`workspace-template/adapters/hermes/providers.py`** (new file, 220 LOC):
- ``ProviderConfig`` dataclass: name, env vars, base URL, default model, auth scheme, docs
- ``PROVIDERS`` dict with 15 entries across 4 groups:
- PR 2 baseline: nous_portal, openrouter
- Frontier commercial: openai, anthropic, xai, gemini
- Chinese providers: qwen, glm, kimi, minimax, deepseek
- OSS/alt: groq, together, fireworks, mistral
- ``RESOLUTION_ORDER`` tuple: priority for auto-detect (back-compat first,
then commercial, then Chinese, then OSS/alt)
- ``resolve_provider(explicit=None)`` -> (ProviderConfig, api_key)
- With explicit name: routes to that provider, raises if env var empty
- Without: walks RESOLUTION_ORDER, first env-var-set provider wins
**`workspace-template/adapters/hermes/executor.py`** (refactored):
- `create_executor(hermes_api_key=None, provider=None, model=None)` now has
three parameters:
- `hermes_api_key`: PR 2 back-compat — routes to Nous Portal
- `provider`: canonical short name from the registry (e.g. "anthropic")
- `model`: optional override of the provider's default model
- Delegates all resolution to `providers.resolve_provider()` — no more
hardcoded URLs or env var lookups in the executor itself
- `HermesA2AExecutor.__init__` no longer has Nous-specific defaults; callers
pass base_url + model explicitly (which create_executor always does)
**`workspace-template/tests/test_hermes_providers.py`** (new file, 26 tests):
- Registry shape invariants (count >= 15, no duplicates, every config valid)
- PR 2 back-compat: HERMES_API_KEY / OPENROUTER_API_KEY still route correctly
- Auto-detect for every provider in the registry (parametrized — guards against
typos in env var lists)
- Explicit `provider=` bypass of auto-detect
- Error cases: unknown provider, explicit-but-empty, auto-detect-with-no-env
- All 26 tests pass locally in 0.08s
## Back-compat guarantees
| Scenario | PR 2 behavior | This PR behavior |
|---|---|---|
| `create_executor(hermes_api_key="x")` | Nous Portal | Nous Portal (unchanged) |
| `HERMES_API_KEY=x` env, auto-detect | Nous Portal | Nous Portal (unchanged) |
| `OPENROUTER_API_KEY=x` env, auto-detect | OpenRouter | OpenRouter (unchanged) |
| Both env + explicit hermes_api_key param | Nous Portal (param wins) | Nous Portal (param wins, unchanged) |
Nothing existing can break. New callers gain access to 13 more providers.
## What's NOT in this PR (Phase 2)
- **Native Anthropic Messages API path** — better tool calling, vision, extended
thinking. Requires pulling in `anthropic` SDK. ~50 LOC.
- **Native Gemini generateContent path** — for vision + google tools. Requires
`google-genai` SDK. ~50 LOC.
- **Streaming support across all providers** — current executor is non-streaming
(single chat.completions.create call). Streaming works with openai.AsyncOpenAI
but hasn't been wired to the A2A event queue path. ~30 LOC.
- **Per-provider model overrides in config.yaml** — Phase 1 uses the registry's
default_model. Phase 2 adds a `hermes: { provider: qwen, model: qwen3-coder-plus }`
block in the workspace config.
- **`.env.example` updates** — not critical since the registry itself documents
every env var via the `env_vars` field, but nice-to-have.
## Related
- Queued memory: `project_hermes_multi_provider.md`
- CEO directive 2026-04-15: *"once current works are cleared, I want you to
focus on supporting hermes agent, right now it doesnt take too much providers"*
- `docs/ecosystem-watch.md` → `### Hermes Agent` — Research Lead's eco-watch
entry listed "Nous Portal, OpenRouter, GLM, Kimi, MiniMax, OpenAI, …" which
shaped this registry's initial set
## Test plan
- [x] Unit tests: 26/26 pass locally (pytest)
- [ ] CI will run on the self-hosted macOS arm64 runner
- [ ] Smoke test in a real workspace: set QWEN_API_KEY and verify Technical
Researcher actually hits Alibaba DashScope successfully
- [ ] Integration test per provider with real API keys (gated on env, skip
when not set — Phase 2 CI addition)
167 lines
5.9 KiB
Python
167 lines
5.9 KiB
Python
"""Hermes adapter executor — Phase 1 multi-provider.
|
|
|
|
Hermes models are accessed via an OpenAI-compatible API. Phase 1 supports 15
|
|
providers via the shared ``providers.py`` registry: Nous Portal, OpenRouter,
|
|
OpenAI, Anthropic, xAI, Gemini, Qwen, GLM, Kimi, MiniMax, DeepSeek, Groq,
|
|
Together, Fireworks, Mistral. Every provider is reached through an OpenAI-compat
|
|
``/v1/chat/completions`` endpoint, so one code path handles all of them.
|
|
|
|
Key resolution order (unchanged from PR 2, extended)
|
|
-----------------------------------------------------
|
|
1. ``hermes_api_key`` parameter (explicit call-site override — routes to Nous Portal)
|
|
2. ``provider`` parameter (explicit provider name — looks up its env var(s))
|
|
3. Auto-detect: walk ``providers.RESOLUTION_ORDER`` and pick the first provider
|
|
whose env var is set (``HERMES_API_KEY`` / ``OPENROUTER_API_KEY`` still come
|
|
first so PR 2 back-compat holds).
|
|
|
|
Raises ``ValueError`` if nothing resolves. The error message lists every env var
|
|
that was checked so the operator knows their options without reading source.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
from typing import Optional
|
|
|
|
from .providers import PROVIDERS, resolve_provider
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def create_executor(
|
|
hermes_api_key: Optional[str] = None,
|
|
provider: Optional[str] = None,
|
|
model: Optional[str] = None,
|
|
):
|
|
"""Create and return a LangGraph-compatible executor for the Hermes adapter.
|
|
|
|
Parameters
|
|
----------
|
|
hermes_api_key:
|
|
Explicit API key. When provided, the call routes to Nous Portal (the
|
|
PR 2 back-compat path) regardless of ``provider``.
|
|
provider:
|
|
Canonical provider short name from ``providers.PROVIDERS`` (e.g.
|
|
``"openai"``, ``"anthropic"``, ``"qwen"``, ``"xai"``). When set, the
|
|
registry entry's env vars are used to find the API key and its
|
|
base URL + default model override the auto-detect path. When unset,
|
|
auto-detect walks ``providers.RESOLUTION_ORDER`` until it finds a
|
|
provider whose env var is set.
|
|
model:
|
|
Override the provider's default model. Passed straight through to
|
|
``chat.completions.create``.
|
|
|
|
Returns
|
|
-------
|
|
HermesA2AExecutor
|
|
A ready-to-use executor wired with the resolved api_key + base_url
|
|
+ model.
|
|
|
|
Raises
|
|
------
|
|
ValueError
|
|
If ``provider`` is an unknown name, if ``provider`` is known but its
|
|
env vars are all empty, or if auto-detect finds nothing.
|
|
"""
|
|
# Path 1: PR 2 back-compat — explicit hermes_api_key routes to Nous Portal.
|
|
if hermes_api_key:
|
|
cfg = PROVIDERS["nous_portal"]
|
|
logger.debug("Hermes: using explicit hermes_api_key param (Nous Portal)")
|
|
return HermesA2AExecutor(
|
|
api_key=hermes_api_key,
|
|
base_url=cfg.base_url,
|
|
model=model or cfg.default_model,
|
|
)
|
|
|
|
# Path 2/3: registry resolution (either explicit provider name or auto-detect).
|
|
cfg, api_key = resolve_provider(provider)
|
|
logger.info(
|
|
"Hermes: provider=%s base_url=%s model=%s",
|
|
cfg.name,
|
|
cfg.base_url,
|
|
model or cfg.default_model,
|
|
)
|
|
return HermesA2AExecutor(
|
|
api_key=api_key,
|
|
base_url=cfg.base_url,
|
|
model=model or cfg.default_model,
|
|
)
|
|
|
|
|
|
class HermesA2AExecutor:
|
|
"""LangGraph-compatible AgentExecutor for Hermes-style multi-provider LLMs.
|
|
|
|
Uses the OpenAI-compatible ``openai`` client pointed at whichever provider
|
|
was resolved by ``create_executor`` (Nous Portal, OpenRouter, OpenAI,
|
|
Anthropic, xAI, Gemini, Qwen, GLM, Kimi, MiniMax, DeepSeek, Groq, Together,
|
|
Fireworks, Mistral). Matches the pattern of sibling adapters (AutoGen,
|
|
LangGraph) which also use OpenAI-compat clients.
|
|
|
|
The ``execute()`` and ``cancel()`` async methods satisfy the
|
|
``a2a.server.agent_execution.AgentExecutor`` interface so this
|
|
executor can be dropped into the A2A server's DefaultRequestHandler.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
base_url: str,
|
|
model: str,
|
|
heartbeat=None,
|
|
):
|
|
self.api_key = api_key
|
|
self.base_url = base_url
|
|
self.model = model
|
|
self._heartbeat = heartbeat
|
|
|
|
# ------------------------------------------------------------------
|
|
# AgentExecutor interface
|
|
# ------------------------------------------------------------------
|
|
|
|
async def execute(self, context, event_queue): # pragma: no cover
|
|
"""Execute a Hermes inference request and push the reply to event_queue."""
|
|
from a2a.utils import new_agent_text_message
|
|
from adapters.shared_runtime import (
|
|
brief_task,
|
|
build_task_text,
|
|
extract_history,
|
|
extract_message_text,
|
|
set_current_task,
|
|
)
|
|
|
|
user_message = extract_message_text(context)
|
|
if not user_message:
|
|
await event_queue.enqueue_event(new_agent_text_message("No message provided"))
|
|
return
|
|
|
|
await set_current_task(self._heartbeat, brief_task(user_message))
|
|
|
|
try:
|
|
import openai
|
|
|
|
client = openai.AsyncOpenAI(
|
|
api_key=self.api_key,
|
|
base_url=self.base_url,
|
|
)
|
|
|
|
task_text = build_task_text(user_message, extract_history(context))
|
|
|
|
response = await client.chat.completions.create(
|
|
model=self.model,
|
|
messages=[{"role": "user", "content": task_text}],
|
|
)
|
|
reply = response.choices[0].message.content or ""
|
|
|
|
except Exception as exc:
|
|
logger.exception("Hermes executor error: %s", exc)
|
|
reply = f"Hermes error: {exc}"
|
|
finally:
|
|
await set_current_task(self._heartbeat, "")
|
|
|
|
await event_queue.enqueue_event(new_agent_text_message(reply))
|
|
|
|
async def cancel(self, context, event_queue): # pragma: no cover
|
|
"""No-op cancel — Hermes requests are not cancellable mid-flight."""
|
|
pass
|