Adds Vertex AI as a first-class provider for Gemini models via Vertex's OpenAI-compatible endpoint. Vertex authenticates with short-lived OAuth2 access tokens (service-account JSON or ADC), not a static API key — the missing piece behind the recurring requests (#13484, #12639, #56259). - agent/vertex_adapter.py: OAuth2 token minting + refresh-on-expiry (5-min margin), ADC->service-account fallback, global vs regional endpoint URLs. Config precedence: env var > config.yaml > default. - plugins/model-providers/vertex/: provider profile (auth_type=vertex), reuses Gemini's extra_body.google.thinking_config translation. - runtime_provider: vertex short-circuit BEFORE the credential pool so a credentials-file path is never mistaken for a static API key; mints a fresh token + computes base_url per resolve. - run_agent + conversation_loop: _try_refresh_vertex_client_credentials() re-mints the token and rebuilds the client on a mid-session 401, so a long-lived gateway agent survives token expiry (~1h). - auxiliary_client: vertex auth_type branch for side-LLM tasks. - config.yaml: vertex.project_id / vertex.region (non-secret, bridged to env); credential path stays in .env (VERTEX_CREDENTIALS_PATH). - setup wizard + model picker: dedicated _model_flow_vertex; curated google/gemini-* model list; --provider choices. - pricing/metadata: Vertex prices off the gemini docs snapshot; endpoint host auto-maps to the vertex provider (no probe spam). - lazy_deps + pyproject [vertex] extra: google-auth, opt-in only. - docs: guides/google-vertex.md + providers page; tests for adapter + runtime resolution. Salvages and modernizes #8427 by @slawt onto current main: rewired from the legacy PROVIDER_REGISTRY path to the provider-profile architecture, moved non-secret config out of .env into config.yaml, and added the per-turn 401 token-refresh the original lacked.
100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
"""Tests for Vertex AI runtime-provider resolution and profile registration.
|
|
|
|
Covers: provider-profile registration + aliases, alias canonicalization,
|
|
resolve_runtime_provider(vertex) minting an OAuth token, and the friendly
|
|
AuthError when credentials can't be resolved. No network calls.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
def test_vertex_profile_registered():
|
|
from providers import get_provider_profile
|
|
|
|
p = get_provider_profile("vertex")
|
|
assert p is not None
|
|
assert p.name == "vertex"
|
|
assert p.api_mode == "chat_completions"
|
|
assert p.auth_type == "vertex"
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["google-vertex", "vertex-ai", "gcp-vertex"])
|
|
def test_vertex_aliases_resolve(alias):
|
|
from providers import get_provider_profile
|
|
|
|
assert get_provider_profile(alias).name == "vertex"
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["google-vertex", "vertex-ai", "gcp-vertex", "vertexai"])
|
|
def test_alias_canonicalizes_to_vertex(alias):
|
|
from hermes_cli.models import _PROVIDER_ALIASES
|
|
|
|
assert _PROVIDER_ALIASES[alias] == "vertex"
|
|
|
|
|
|
def test_google_vertex_not_confused_with_gemini():
|
|
"""`google-vertex` must map to vertex, not the AI-Studio `gemini` provider."""
|
|
from hermes_cli.models import _PROVIDER_ALIASES
|
|
|
|
assert _PROVIDER_ALIASES["google-vertex"] == "vertex"
|
|
assert _PROVIDER_ALIASES["google-gemini"] == "gemini"
|
|
|
|
|
|
def test_resolve_runtime_provider_mints_token(monkeypatch):
|
|
import agent.vertex_adapter as va
|
|
from hermes_cli import runtime_provider as rp
|
|
|
|
monkeypatch.setattr(
|
|
va, "get_vertex_config",
|
|
lambda: ("ya29.TOKEN", "https://aiplatform.googleapis.com/v1beta1/projects/p/locations/global/endpoints/openapi"),
|
|
)
|
|
rt = rp.resolve_runtime_provider(requested="vertex")
|
|
assert rt["provider"] == "vertex"
|
|
assert rt["api_mode"] == "chat_completions"
|
|
assert rt["source"] == "vertex-oauth"
|
|
assert rt["api_key"] == "ya29.TOKEN"
|
|
assert "aiplatform.googleapis.com" in rt["base_url"]
|
|
|
|
|
|
def test_resolve_runtime_provider_alias(monkeypatch):
|
|
import agent.vertex_adapter as va
|
|
from hermes_cli import runtime_provider as rp
|
|
|
|
monkeypatch.setattr(va, "get_vertex_config", lambda: ("t", "https://aiplatform.googleapis.com/v1beta1/projects/p/locations/global/endpoints/openapi"))
|
|
rt = rp.resolve_runtime_provider(requested="google-vertex")
|
|
assert rt["provider"] == "vertex"
|
|
|
|
|
|
def test_resolve_runtime_provider_raises_autherror_when_unresolved(monkeypatch):
|
|
import agent.vertex_adapter as va
|
|
from hermes_cli import runtime_provider as rp
|
|
from hermes_cli.auth import AuthError
|
|
|
|
monkeypatch.setattr(va, "get_vertex_config", lambda: (None, None))
|
|
with pytest.raises(AuthError) as exc:
|
|
rp.resolve_runtime_provider(requested="vertex")
|
|
msg = str(exc.value)
|
|
assert "OAuth2" in msg
|
|
assert "not a static API key" in msg
|
|
|
|
|
|
def test_vertex_extra_body_thinking_config():
|
|
from providers import get_provider_profile
|
|
|
|
p = get_provider_profile("vertex")
|
|
body = p.build_extra_body(
|
|
model="google/gemini-3-pro-preview",
|
|
reasoning_config={"effort": "high"},
|
|
)
|
|
assert "extra_body" in body
|
|
assert "google" in body["extra_body"]
|
|
assert "thinking_config" in body["extra_body"]["google"]
|
|
|
|
|
|
def test_vertex_extra_body_empty_without_reasoning():
|
|
from providers import get_provider_profile
|
|
|
|
p = get_provider_profile("vertex")
|
|
assert p.build_extra_body(model="google/gemini-3-flash-preview") == {}
|