Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
feat(llm): Vertex / Agent Platform Gemini support via AQ. key auto-detection
trohitg1df7131c4858
diff --git a/client/src/components/credentials/catalogueAdapter.ts b/client/src/components/credentials/catalogueAdapter.tsindex c9704588..2df94c13 100644--- a/client/src/components/credentials/catalogueAdapter.ts+++ b/client/src/components/credentials/catalogueAdapter.ts@@ -156,6 +156,7 @@ export function rehydrateProvider(entry: ServerProviderConfig): ProviderConfig {return {id: entry.id,name: entry.name,+ vertexName: entry.vertex_display_name,category: entry.category,categoryLabel: entry.category_label,color: entry.color,diff --git a/client/src/components/credentials/panels/ApiKeyPanel.tsx b/client/src/components/credentials/panels/ApiKeyPanel.tsxindex c13b1853..06045032 100644--- a/client/src/components/credentials/panels/ApiKeyPanel.tsx+++ b/client/src/components/credentials/panels/ApiKeyPanel.tsx@@ -54,7 +54,12 @@ const ApiKeyPanel: React.FC<{ config: ProviderConfig; visible: boolean }> = ({ cclassName="h-12 w-12 text-2xl"/></div>- <CardTitle className="text-lg">{config.name}</CardTitle>+ {/* AQ.-prefixed keys route to the Vertex backend — show the+ vertex display name so the user knows which billing path+ is active. */}+ <CardTitle className="text-lg">+ {config.vertexName && inputValue.startsWith('AQ.') ? config.vertexName : config.name}+ </CardTitle></div>{validated && (<Badge variant="success" className="gap-1">@@ -94,6 +99,9 @@ const ApiKeyPanel: React.FC<{ config: ProviderConfig; visible: boolean }> = ({ csavedLabel={field.key === 'apiKey' ? 'Valid' : 'Connected'}/>)}+ {field?.help && (+ <p className="mt-2 text-xs text-muted-foreground">{field.help}</p>+ )}</CardContent></Card>diff --git a/client/src/components/credentials/types.ts b/client/src/components/credentials/types.tsindex fb195ea6..129746dc 100644--- a/client/src/components/credentials/types.ts+++ b/client/src/components/credentials/types.ts@@ -84,6 +84,8 @@ export interface QrPairingDef {export interface ProviderConfig {id: string;name: string;+ /** Display name shown instead of `name` while the key in the field is a Vertex / Agent Platform key (AQ. prefix). */+ vertexName?: string;category: string;categoryLabel: string;/** Theme color key (resolved via theme.colors[color] at render). */diff --git a/client/src/hooks/useCatalogueQuery.ts b/client/src/hooks/useCatalogueQuery.tsindex 56daf633..5166cf70 100644--- a/client/src/hooks/useCatalogueQuery.ts+++ b/client/src/hooks/useCatalogueQuery.ts@@ -95,6 +95,8 @@ export interface ServerQrDef {export interface ServerProviderConfig {id: string;name: string;+ /** Display name shown instead of `name` while the stored key is a Vertex / Agent Platform key (AQ. prefix). */+ vertex_display_name?: string;category: string;category_label: string;color: string;diff --git a/server/config/credential_providers.json b/server/config/credential_providers.jsonindex d1a6a638..6eac0df8 100644--- a/server/config/credential_providers.json+++ b/server/config/credential_providers.json@@ -54,10 +54,11 @@"gemini": {"extends": "_ai_base","name": "Gemini",+ "vertex_display_name": "Gemini Vertex","color": "solarized.blue","icon_ref": "lobehub:gemini","fields": [- { "key": "apiKey", "placeholder": "AIza..." }+ { "key": "apiKey", "placeholder": "AIza... (AI Studio) or AQ.... (Agent Platform / Vertex)", "help": "AIza keys come from Google AI Studio and bill personal Gemini API credits. AQ. keys come from the Gemini Enterprise Agent Platform / Vertex AI Express console and bill your GCP project instead - eligible for Cloud credits. With an AQ. key the model list shows Vertex-supported versions only (no -latest aliases); re-validate after switching key types to refresh it." }]},"groq": {diff --git a/server/config/llm_defaults.json b/server/config/llm_defaults.jsonindex cfc7b8f9..91b4bd10 100644--- a/server/config/llm_defaults.json+++ b/server/config/llm_defaults.json@@ -70,32 +70,34 @@"temperature_range": [0.0, 1.0]},"gemini": {- "default_model": "gemini-flash-latest",+ "default_model": "gemini-3.5-flash","detection_patterns": ["gemini", "google"],"base_url": "https://generativelanguage.googleapis.com/v1beta","models_endpoint": "https://generativelanguage.googleapis.com/v1beta/models","api_key_param": "google_api_key","max_tokens_param": "max_output_tokens",- "_alias_note": "gemini-flash-latest / gemini-pro-latest are family-level rolling aliases (cross-generation, 2-week notice on rotation per https://ai.google.dev/gemini-api/docs/changelog). Bare gemini-2.5-flash / gemini-2.5-pro are themselves rolling within their generation; -001/-002 suffixes pin a specific revision. Currently flash-latest -> gemini-3-flash-preview, pro-latest -> gemini-3.1-pro-preview.",+ "_models_note": "Real (concrete) model names only — no -latest rolling aliases. The same curated list (max_output_tokens keys) serves BOTH the Gemini Developer API (AIza keys) and Vertex / Agent Platform (AQ. keys); every entry live-verified on both endpoints (2026-06-12). Vertex 404s on -latest aliases and its models.list rejects API keys, which is why the dropdown is curated instead of live-fetched.","max_output_tokens": {- "gemini-flash-latest": 65536,- "gemini-pro-latest": 65536,+ "gemini-3.5-flash": 65536,"gemini-3.1-pro-preview": 65536,+ "gemini-3.1-flash-lite": 65536,"gemini-3-flash-preview": 65536,"gemini-2.5-pro": 65536,"gemini-2.5-flash": 65535,+ "gemini-2.5-flash-lite": 65535,"_default": 8192},"context_length": {- "gemini-flash-latest": 1048576,- "gemini-pro-latest": 1048576,+ "gemini-3.5-flash": 1048576,"gemini-3.1-pro-preview": 1048576,+ "gemini-3.1-flash-lite": 1048576,"gemini-3-flash-preview": 1048576,"gemini-2.5-pro": 1048576,"gemini-2.5-flash": 1048576,+ "gemini-2.5-flash-lite": 1048576,"_default": 1048576},- "thinking_models": ["gemini-2.5", "gemini-3", "gemini-flash-latest", "gemini-pro-latest"],+ "thinking_models": ["gemini-2.5", "gemini-3"],"thinking_type": "budget","temperature_range": [0.0, 2.0],"_incompatible_models_note": "Models that the dropdown should hide because they need a different API endpoint than `generate_content`. antigravity-preview-05-2026 requires the Interactions API; selecting it currently surfaces a `google.genai.errors.APIError`. ChatUnifier reads this list in fetch_models and filters before returning. Drop entries here when the unifier gains routing support for the alternate endpoint.",diff --git a/server/services/ai.py b/server/services/ai.pyindex 1992c2b5..0bb13773 100644--- a/server/services/ai.py+++ b/server/services/ai.py@@ -147,6 +147,7 @@ from services.llm.config import (resolve_max_tokens as native_resolve_max_tokens,resolve_temperature as native_resolve_temperature,)+from services.llm.vertex import is_vertex_express_key# =============================================================================@@ -1248,6 +1249,16 @@ class AIService:# Build kwargs dynamically from registry configkwargs = {config.api_key_param: api_key, "model": model, "temperature": temperature, config.max_tokens_param: max_tokens}+ # Agent Platform / Vertex Express keys ("AQ." prefix) route the same+ # ChatGoogleGenerativeAI to the Vertex backend — billed to the key's+ # GCP project instead of personal AI Studio credits. The library+ # handles endpoint construction and key transport.+ if provider == "gemini" and is_vertex_express_key(api_key):+ kwargs["vertexai"] = True+ if proxy_url:+ logger.warning("[AI] gemini: proxy_url ignored in Vertex AI mode")+ proxy_url = None+# Proxy mode: route through local proxy that handles auth (Ollama pattern)# Proxy URL stored as {provider}_proxy credentialif proxy_url:@@ -1432,7 +1443,10 @@ class AIService:enabled=True,budget=int(flattened.get("thinking_budget", 2048)),effort=flattened.get("reasoning_effort", "medium"),- level=flattened.get("thinking_level", "medium"),+ # No fabricated default — only forward thinking_level+ # when the node actually configured it (Vertex rejects+ # an unsolicited thinking_level on 2.5-era models).+ level=flattened.get("thinking_level"),format=flattened.get("reasoning_format", "parsed"),)diff --git a/server/services/llm/protocol.py b/server/services/llm/protocol.pyindex 428986e4..a9e24ac4 100644--- a/server/services/llm/protocol.py+++ b/server/services/llm/protocol.py@@ -28,7 +28,11 @@ class ThinkingConfig:enabled: bool = Falsebudget: int = 2048effort: str = "medium"- level: str = "medium" # Gemini 3+ thinking_level+ # Gemini 3+ thinking_level — None unless the user explicitly set it.+ # Fabricating a default here makes the gemini provider send+ # thinking_level alongside thinking_budget, which Vertex rejects on+ # 2.5-era models (400 INVALID_ARGUMENT).+ level: Optional[str] = Noneformat: str = "parsed"diff --git a/server/services/llm/providers/gemini.py b/server/services/llm/providers/gemini.pyindex ff387d88..8b6ba955 100644--- a/server/services/llm/providers/gemini.py+++ b/server/services/llm/providers/gemini.py@@ -23,6 +23,19 @@ class GeminiProvider:def __init__(self, api_key: str, *, proxy_url: Optional[str] = None):from google import genai+ from services.llm.vertex import is_vertex_express_key++ self._vertex = is_vertex_express_key(api_key)+ if self._vertex:+ # Agent Platform / Vertex Express key — the SDK routes to+ # aiplatform.googleapis.com and bills the key's GCP project.+ # proxy_url rewrites base_url to a local auth-delegating relay,+ # which is meaningless against the Vertex endpoint.+ if proxy_url:+ logger.warning("gemini: proxy_url ignored in Vertex AI mode")+ self._client = genai.Client(vertexai=True, api_key=api_key)+ return+kwargs: Dict[str, Any] = {"api_key": api_key}if proxy_url:kwargs["http_options"] = {"base_url": proxy_url}@@ -54,7 +67,10 @@ class GeminiProvider:if system_instruction:config["system_instruction"] = system_instruction- # Thinking -- pass available fields, let the SDK/API resolve per model+ # Thinking -- forward exactly the fields the caller configured and+ # let the SDK/API validate per model. `level` is None unless the+ # user explicitly set thinking_level (Vertex rejects an unsolicited+ # thinking_level on 2.5-era models with 400 INVALID_ARGUMENT).if thinking and thinking.enabled:thinking_kwargs: Dict[str, Any] = {}if thinking.budget:@@ -79,6 +95,27 @@ class GeminiProvider:# ------------------------------------------------------------------async def fetch_models(self, api_key: str) -> List[str]:+ """Validate the key, then return the curated model list.++ The same curated list (``max_output_tokens`` keys from the gemini+ block in llm_defaults.json — real model names, no ``-latest``+ aliases) serves both backends, so the dropdown is identical for+ AI Studio and Vertex keys. Only the key probe differs: Vertex+ rejects API keys on ``models.list`` (401 — requires an OAuth+ principal), so a free ``count_tokens`` call verifies the key+ there; the Developer API uses its models endpoint. Invalid keys+ raise the typed SDK error which the unifier translates into+ ``NodeUserError``.+ """+ models = self._curated_models()++ if self._vertex:+ probe_model = models[0] if models else "gemini-2.5-flash"+ await self._client.aio.models.count_tokens(+ model=probe_model, contents="hello"+ )+ return models+import httpxasync with httpx.AsyncClient() as client:@@ -87,14 +124,18 @@ class GeminiProvider:timeout=15.0,)r.raise_for_status()- data = r.json()- models = []- for m in data.get("models", []):- name = m.get("name", "")- if name.startswith("models/"):- name = name[7:]- models.append(name)- return sorted(models)+ return models++ @staticmethod+ def _curated_models() -> List[str]:+ from services.llm import config as llm_config++ max_tokens_map = (+ llm_config.LLM_DEFAULTS.get("providers", {})+ .get("gemini", {})+ .get("max_output_tokens", {})+ )+ return [m for m in max_tokens_map if m != "_default"]# ------------------------------------------------------------------# internalsdiff --git a/server/services/llm/vertex.py b/server/services/llm/vertex.pynew file mode 100644index 00000000..a9478afe--- /dev/null+++ b/server/services/llm/vertex.py@@ -0,0 +1,28 @@+"""Vertex / Agent-Platform key detection for the gemini provider.++Gemini accepts two kinds of API keys:++- AI Studio Developer-API keys (``AIza...``) — billed to the user's+ personal Gemini API credits via ``generativelanguage.googleapis.com``.+- Gemini Enterprise Agent Platform / Vertex AI Express keys (``AQ.``) —+ billed to the key's GCP project (eligible for Cloud credits) via+ ``aiplatform.googleapis.com``.++Both the native ``google-genai`` SDK and ``langchain-google-genai``+route to the Vertex backend with ``vertexai=True`` plus the same+``api_key`` — endpoint construction, auth headers, and backend parity+are entirely handled by the official libraries. The only application+concern is detecting which backend a stored key belongs to, which the+key prefix encodes.+"""++from __future__ import annotations++from typing import Optional++VERTEX_KEY_PREFIX = "AQ."+++def is_vertex_express_key(api_key: Optional[str]) -> bool:+ """True when the key is an Agent Platform / Vertex AI Express key."""+ return bool(api_key) and api_key.startswith(VERTEX_KEY_PREFIX)diff --git a/server/tests/llm/test_vertex_key.py b/server/tests/llm/test_vertex_key.pynew file mode 100644index 00000000..05b661a4--- /dev/null+++ b/server/tests/llm/test_vertex_key.py@@ -0,0 +1,254 @@+"""Vertex / Agent-Platform key support for the gemini provider.++AI Studio keys (``AIza...``) ride the Gemini Developer API; Agent+Platform / Vertex Express keys (``AQ.``) route the SAME provider to the+Vertex backend (``vertexai=True``) so usage bills the key's GCP project+instead of personal AI Studio credits. Detection lives in+``services/llm/vertex.py`` and is applied inside the two construction+points (``GeminiProvider.__init__`` and ``AIService.create_model``) so+every execution path picks it up with no call-site edits.++One curated model list (the ``max_output_tokens`` keys in+llm_defaults.json — real model names, no ``-latest`` aliases) serves+both backends.+"""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from services.llm.vertex import VERTEX_KEY_PREFIX, is_vertex_express_key+++def _curated_gemini_models():+ from services.llm import config as llm_config++ max_tokens_map = (+ llm_config.LLM_DEFAULTS["providers"]["gemini"]["max_output_tokens"]+ )+ return [m for m in max_tokens_map if m != "_default"]+++# ---------------------------------------------------------------------------+# Key-prefix detection helper+# ---------------------------------------------------------------------------+++class TestIsVertexExpressKey:+ def test_agent_platform_key_detected(self):+ assert is_vertex_express_key("AQ.Ab8RN6-test") is True++ def test_ai_studio_key_not_detected(self):+ assert is_vertex_express_key("AIzaSyTest123") is False++ def test_empty_and_none_are_false(self):+ assert is_vertex_express_key("") is False+ assert is_vertex_express_key(None) is False++ def test_prefix_constant(self):+ assert VERTEX_KEY_PREFIX == "AQ."+++# ---------------------------------------------------------------------------+# Shared curated model list — real names only, same for both backends+# ---------------------------------------------------------------------------+++class TestCuratedModelList:+ def test_no_alias_models(self):+ """Vertex 404s on ``-latest`` rolling aliases — the shared list+ must contain concrete versions only (live-verified on both+ endpoints)."""+ models = _curated_gemini_models()+ assert models, "gemini max_output_tokens list missing from llm_defaults.json"+ for model in models:+ assert "-latest" not in model, (+ f"{model!r} is a rolling alias — Vertex returns 404 for these"+ )++ def test_default_model_is_real_and_curated(self):+ from services.llm import config as llm_config++ default = llm_config.LLM_DEFAULTS["providers"]["gemini"]["default_model"]+ assert "-latest" not in default+ assert default in _curated_gemini_models()+++# ---------------------------------------------------------------------------+# Native path: GeminiProvider client construction + fetch_models+# ---------------------------------------------------------------------------+++class TestGeminiProviderVertexMode:+ def _make(self, api_key, proxy_url=None):+ with patch("google.genai.Client") as client_cls:+ from services.llm.providers.gemini import GeminiProvider++ provider = GeminiProvider(api_key, proxy_url=proxy_url)+ return provider, client_cls++ def test_vertex_key_builds_vertex_client(self):+ provider, client_cls = self._make("AQ.test-key")+ client_cls.assert_called_once_with(vertexai=True, api_key="AQ.test-key")+ assert provider._vertex is True++ def test_vertex_key_ignores_proxy_url(self):+ _, client_cls = self._make("AQ.test-key", proxy_url="http://localhost:11434")+ kwargs = client_cls.call_args[1]+ assert "http_options" not in kwargs+ assert kwargs["vertexai"] is True++ def test_ai_studio_key_unchanged(self):+ provider, client_cls = self._make("AIzaSyTest")+ client_cls.assert_called_once_with(api_key="AIzaSyTest")+ assert provider._vertex is False++ def test_ai_studio_key_with_proxy_unchanged(self):+ _, client_cls = self._make("AIzaSyTest", proxy_url="http://localhost:11434")+ kwargs = client_cls.call_args[1]+ assert kwargs["http_options"] == {"base_url": "http://localhost:11434"}+ assert "vertexai" not in kwargs++ @pytest.mark.asyncio+ async def test_fetch_models_vertex_probes_and_returns_curated_list(self):+ """Vertex model listing rejects API keys, so the key is verified+ with a free count_tokens call and the shared curated list is+ returned."""+ provider, _ = self._make("AQ.test-key")+ provider._client = MagicMock()+ provider._client.aio.models.count_tokens = AsyncMock()++ models = await provider.fetch_models("AQ.test-key")++ expected = _curated_gemini_models()+ assert models == expected+ assert len(models) > 0+ provider._client.aio.models.count_tokens.assert_awaited_once_with(+ model=expected[0], contents="hello"+ )++ @pytest.mark.asyncio+ async def test_fetch_models_ai_studio_returns_same_curated_list(self):+ """Both key types serve the SAME model list — the AI Studio+ branch probes the key via the models endpoint, then returns the+ shared curated list."""+ provider, _ = self._make("AIzaSyTest")++ mock_response = MagicMock()+ mock_response.raise_for_status = MagicMock()+ mock_client = MagicMock()+ mock_client.get = AsyncMock(return_value=mock_response)+ mock_client.__aenter__ = AsyncMock(return_value=mock_client)+ mock_client.__aexit__ = AsyncMock(return_value=False)++ with patch("httpx.AsyncClient", return_value=mock_client):+ models = await provider.fetch_models("AIzaSyTest")++ assert models == _curated_gemini_models()+ mock_response.raise_for_status.assert_called_once()++ @pytest.mark.asyncio+ async def test_fetch_models_vertex_propagates_invalid_key_error(self):+ """An invalid key raises the typed SDK error, which the unifier+ translates into NodeUserError for the credentials modal."""+ provider, _ = self._make("AQ.bad-key")+ provider._client = MagicMock()+ provider._client.aio.models.count_tokens = AsyncMock(+ side_effect=RuntimeError("401 UNAUTHENTICATED")+ )++ with pytest.raises(RuntimeError, match="401"):+ await provider.fetch_models("AQ.bad-key")++ @pytest.mark.asyncio+ async def test_thinking_level_not_fabricated(self):+ """``ThinkingConfig.level`` defaults to None — when the user only+ configured a budget, the provider must send thinking_budget alone.+ Vertex rejects an unsolicited thinking_level on 2.5-era models with+ 400 INVALID_ARGUMENT (live-verified); the SDK/API owns per-model+ validation of whatever the user explicitly set."""+ from services.llm.protocol import Message, ThinkingConfig++ provider, _ = self._make("AQ.test-key")+ mock_types = MagicMock()+ mock_resp = MagicMock()+ mock_resp.candidates = []+ mock_resp.usage_metadata = None+ provider._client = MagicMock()+ provider._client.aio.models.generate_content = AsyncMock(return_value=mock_resp)++ with patch("google.genai.types", mock_types):+ await provider.chat(+ [Message(role="user", content="test")],+ model="gemini-2.5-flash",+ thinking=ThinkingConfig(enabled=True, budget=1024),+ )++ kwargs = mock_types.ThinkingConfig.call_args[1]+ assert kwargs == {"thinking_budget": 1024}+++# ---------------------------------------------------------------------------+# LangChain agent path: AIService.create_model+# ---------------------------------------------------------------------------+++class _KwargsCapture:+ """Stand-in for ChatGoogleGenerativeAI that records its kwargs."""++ def __init__(self, **kwargs):+ self.kwargs = kwargs+++class TestCreateModelVertexMode:+ def _create(self, api_key, *, thinking=None, proxy_url=None):+ from services.ai import AIService++ with patch("services.ai._get_google_genai_class", return_value=_KwargsCapture):+ model = AIService.create_model(+ MagicMock(), # create_model touches no instance state+ provider="gemini",+ api_key=api_key,+ model="gemini-2.5-flash",+ temperature=0.7,+ max_tokens=4096,+ thinking=thinking,+ proxy_url=proxy_url,+ )+ return model.kwargs++ def test_vertex_key_sets_vertexai_flag(self):+ kwargs = self._create("AQ.test-key")+ assert kwargs["vertexai"] is True+ assert kwargs["google_api_key"] == "AQ.test-key"+ assert kwargs["max_output_tokens"] == 4096++ def test_vertex_key_nulls_proxy(self):+ kwargs = self._create("AQ.test-key", proxy_url="http://localhost:11434")+ assert "base_url" not in kwargs+ assert kwargs["google_api_key"] == "AQ.test-key"++ def test_vertex_key_does_not_alter_thinking_handling(self):+ """The vertex branch only adds ``vertexai=True`` — thinking kwargs+ (resolved by the model registry) must be byte-identical to the+ AI-Studio path for the same model + thinking config."""+ from services.ai import ThinkingConfig++ thinking = ThinkingConfig(enabled=True, budget=2048)+ vertex_kwargs = self._create("AQ.test-key", thinking=thinking)+ studio_kwargs = self._create("AIzaSyTest", thinking=thinking)++ assert vertex_kwargs.pop("vertexai") is True+ assert vertex_kwargs.pop("google_api_key") == "AQ.test-key"+ assert studio_kwargs.pop("google_api_key") == "AIzaSyTest"+ assert vertex_kwargs == studio_kwargs++ def test_ai_studio_key_has_no_vertexai_flag(self):+ kwargs = self._create("AIzaSyTest")+ assert "vertexai" not in kwargs+ assert kwargs["google_api_key"] == "AIzaSyTest"++ def test_ai_studio_key_proxy_still_applies(self):+ kwargs = self._create("AIzaSyTest", proxy_url="http://localhost:11434")+ assert kwargs["base_url"] == "http://localhost:11434"+ assert "vertexai" not in kwargs