Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

feat(agent-core): speak Gemini natively on google hops (#3033)

Call Whitef5162a63d8ea
diff --git a/core/agent_core/ai/_common.py b/core/agent_core/ai/_common.py
index 9a645d396..dad004e65 100644
--- a/core/agent_core/ai/_common.py
+++ b/core/agent_core/ai/_common.py
@@ -1622,6 +1622,8 @@ def auth_headers(endpoint: ModelEndpoint, *, provider: str, gateway: bool) -> di
headers["Authorization"] = f"Bearer {endpoint.token}"
elif provider == "anthropic":
headers["x-api-key"] = endpoint.token
+ elif provider == "google":
+ headers["x-goog-api-key"] = endpoint.token
else:
headers["Authorization"] = f"Bearer {endpoint.token}"
return headers
@@ -2395,4 +2397,5 @@ def _default_provider(protocol: str) -> str:
"anthropic": "anthropic",
"openai_chat": "openai",
"openai_responses": "openai",
+ "google": "google",
}.get(protocol, protocol)
diff --git a/core/agent_core/ai/google.py b/core/agent_core/ai/google.py
new file mode 100644
index 000000000..1dbb2355a
--- /dev/null
+++ b/core/agent_core/ai/google.py
@@ -0,0 +1,614 @@
+"""Google Gemini ``streamGenerateContent`` streaming adapter.
+
+Payload and stream rules are ported from Pi's ``packages/ai/src/api/google-shared.ts`` and
+``google-generative-ai.ts`` (MIT, ``7fbbd5f``) and tau's ``tau_ai/google.py`` (MIT) onto the shared stream
+assembler; ``agent-core-contracts/conversion-tables.md`` ("Google Gemini") is the table.
+"""
+
+from __future__ import annotations
+
+import json
+import re
+from collections.abc import AsyncIterator, Mapping
+from typing import Any
+from urllib.parse import quote
+
+import httpx
+
+from core.agent_core.ai.image_limits import LoadedImage
+from core.agent_core.ai._common import (
+ PinUnavailable,
+ ServedHopResolver,
+ StreamAssembler,
+ WireField,
+ WireRequest,
+ auth_headers,
+ content_parts,
+ drive_sse_stream,
+ endpoint_origin,
+ hosted_pin,
+ join_endpoint_url,
+ json_object,
+ pin_refusal,
+ prepare_messages,
+ request_credential_values,
+ usage_counter_error,
+ validate_wire_shape,
+)
+from core.agent_core.ai.provider import Done, MediaLoader, ModelRequest, ProviderAdapter, ProviderError
+from core.agent_core.cancel import CancelToken
+from core.agent_core.messages import (
+ AssistantMessage,
+ ImageBlock,
+ TextBlock,
+ ThinkingBlock,
+ ToolCallBlock,
+ ToolResultMessage,
+ UserMessage,
+)
+
+#: Google's documented stand-in for the signature of a function call Gemini did not produce ("Thought signatures",
+#: FAQ 1): another origin's call in the current turn, which Gemini 3 and later validate.
+DUMMY_THOUGHT_SIGNATURE = "skip_thought_signature_validator"
+# A thought signature is base64 bytes (TYPE_BYTES); anything else in a transcript is not Gemini's to receive.
+_SIGNATURE = re.compile(r"[A-Za-z0-9+/]+={0,2}")
+# Gemini 3 and later, and the aliases Google points at them, take a discrete thinking level; Gemini 2.5 a budget.
+_LEVEL_MODEL = re.compile(r"^(?:models/)?(?:gemini-(?:[3-9]|\d{2,})(?:\.\d+)?-|gemini-(?:flash|flash-lite|pro)-latest$)")
+_BUDGET_MODEL = re.compile(r"^(?:models/)?gemini-2\.5-")
+_MAJOR_VERSION = re.compile(r"^(?:models/)?gemini-(\d+)")
+_LEVELS = {"minimal": "minimal", "low": "low", "medium": "medium", "high": "high", "xhigh": "high", "max": "high"}
+# Google's own effort-to-budget table for Gemini 2.5 ("OpenAI compatibility", Thinking).
+_BUDGETS = {"none": 0, "minimal": 1024, "low": 1024, "medium": 8192, "high": 24576, "xhigh": 24576, "max": 24576}
+# The effort each budget stands for, so one wire budget is one evidence key.
+_BUDGET_EFFORTS = {0: "none", 1024: "low", 8192: "medium", 24576: "high"}
+_SAFETY_FINISH = frozenset({
+ "SAFETY", "RECITATION", "BLOCKLIST", "PROHIBITED_CONTENT", "SPII",
+ "IMAGE_SAFETY", "IMAGE_PROHIBITED_CONTENT", "IMAGE_RECITATION",
+})
+
+_GOOGLE_USAGE_FIELDS = (
+ "promptTokenCount", "cachedContentTokenCount", "candidatesTokenCount", "thoughtsTokenCount",
+ "toolUsePromptTokenCount", "totalTokenCount",
+)
+_GOOGLE_PART_FIELDS = (
+ WireField("text", str),
+ WireField("thought", bool),
+ WireField("thoughtSignature", str),
+ WireField(
+ "functionCall",
+ Mapping,
+ children=(WireField("name", str), WireField("id", str), WireField("args", Mapping)),
+ ),
+)
+_GOOGLE_WIRE_SHAPES = {
+ "chunk": (
+ WireField("usageMetadata", Mapping),
+ WireField("error", Mapping),
+ WireField("promptFeedback", Mapping, children=(WireField("blockReason", str),)),
+ WireField(
+ "candidates",
+ list,
+ item_children=(
+ WireField("finishReason", str),
+ WireField("finishMessage", str),
+ WireField(
+ "content",
+ Mapping,
+ children=(WireField("role", str), WireField("parts", list, item_children=_GOOGLE_PART_FIELDS)),
+ ),
+ ),
+ ),
+ ),
+}
+
+
+class GoogleAdapter(ProviderAdapter):
+ """Adapter for Gemini ``models/<model>:streamGenerateContent?alt=sse`` endpoints."""
+
+ protocol = "google"
+
+ def __init__(
+ self,
+ client: httpx.AsyncClient | None = None,
+ *,
+ media_loader: MediaLoader | None = None,
+ served_hop_resolver: ServedHopResolver | None = None,
+ gateway: bool = False,
+ ) -> None:
+ # The Model Hub gateway is loopback: an environment or system proxy must never carry it.
+ self._client = client or httpx.AsyncClient(timeout=None, trust_env=not gateway)
+ self._owns_client = client is None
+ self._media_loader = media_loader
+ self._served_hop_resolver = served_hop_resolver
+ self._gateway = gateway
+
+ async def aclose(self) -> None:
+ if self._owns_client:
+ await self._client.aclose()
+
+ def stream(self, request: ModelRequest, cancel: CancelToken) -> AsyncIterator[Any]:
+ return self._stream(request, cancel)
+
+ async def _stream(self, request: ModelRequest, cancel: CancelToken) -> AsyncIterator[Any]:
+ target = endpoint_origin(request.endpoint)
+ assembler = StreamAssembler(
+ origin=target,
+ protocol=self.protocol,
+ verified_origin=not self._gateway,
+ endpoint_url=request.endpoint.base_url,
+ sensitive_values=request_credential_values(request.endpoint),
+ )
+ if cancel.cancelled:
+ terminal = assembler.terminal(assembler.aborted(cancel.reason))
+ if terminal is not None:
+ yield terminal
+ return
+ prepared = await prepare_messages(
+ request.messages,
+ target=target,
+ supports_images=request.supports_images,
+ protocol=self.protocol,
+ media_loader=self._media_loader,
+ cancel=cancel,
+ endpoint=request.endpoint,
+ )
+ if isinstance(prepared, ProviderError):
+ terminal = assembler.terminal(prepared)
+ if terminal is not None:
+ yield terminal
+ return
+ transformed_messages, loaded_images = prepared
+ if cancel.cancelled:
+ terminal = assembler.terminal(assembler.aborted(cancel.reason))
+ if terminal is not None:
+ yield terminal
+ return
+ stream = _GoogleStream(assembler)
+ try:
+ wire = wire_request(request, transformed_messages, gateway=self._gateway, loaded_images=loaded_images)
+
+ async def translate(events: AsyncIterator[Any]) -> AsyncIterator[Any]:
+ async for event in events:
+ if cancel.cancelled:
+ terminal = assembler.terminal(assembler.aborted(cancel.reason))
+ if terminal is not None:
+ yield terminal
+ return
+ if not event.data:
+ continue
+ for item in stream.chunk(event.data):
+ yield item
+ if stream.ended:
+ return
+ if cancel.cancelled:
+ terminal = assembler.terminal(assembler.aborted(cancel.reason))
+ if terminal is not None:
+ yield terminal
+ return
+ for item in stream.finish():
+ yield item
+
+ driver = drive_sse_stream(
+ self._client,
+ method="POST",
+ url=wire.url,
+ json_body=wire.body,
+ headers=wire.headers,
+ cancel=cancel,
+ assembler=assembler,
+ endpoint=request.endpoint,
+ resolver=self._served_hop_resolver,
+ gateway=self._gateway,
+ translate=translate,
+ )
+ try:
+ async for event in driver:
+ if isinstance(event, PinUnavailable):
+ terminal = assembler.terminal(pin_refusal(assembler, request.endpoint, event))
+ if terminal is not None:
+ yield terminal
+ return
+ yield event
+ finally:
+ await driver.aclose()
+ except Exception as exc:
+ terminal = assembler.terminal(assembler.exception(exc))
+ if terminal is not None:
+ yield terminal
+
+
+class _GoogleStream:
+ """One response's chunks to canonical provider events.
+
+ Every chunk is a ``GenerateContentResponse`` whose parts are deltas. Consecutive parts of one kind (thought text,
+ answer text) extend one block; a ``functionCall`` part is a whole call. A signature stays with the part it came
+ on: a call's on its ``ToolCallBlock``, a thought's on its ``ThinkingBlock``. ``TextBlock`` holds none, so an
+ answer's goes on an empty ``ThinkingBlock`` right after its text (or alone, for the empty signed part Gemini may
+ stream), and the request builder puts it back on that text part.
+ """
+
+ def __init__(self, assembler: StreamAssembler) -> None:
+ self.assembler = assembler
+ self.ended = False
+ self._finish: str | None = None
+ self._finish_message: str | None = None
+ self._blocked: str | None = None
+ self._kind: str | None = None
+ self._block = 0
+ self._tools = 0
+ self._text_signature: str | None = None
+
+ def _fail(self, message: str, *, kind: str | None = "invalid_request") -> list[Any]:
+ self.ended = True
+ terminal = self.assembler.terminal(self.assembler.error(message, kind=kind))
+ return [terminal] if terminal is not None else []
+
+ def _key(self, kind: str) -> tuple[str, int]:
+ if self._kind != kind:
+ self._end_block()
+ self._kind = kind
+ self._block += 1
+ return (f"google-{kind}", self._block)
+
+ def _end_block(self) -> None:
+ """Close the open block; an answer's signature lands right after its text, before whatever follows."""
+
+ self._kind = None
+ if self._text_signature is not None:
+ self._block += 1
+ self.assembler.ensure_thinking_slot(("google-signature", self._block), signature=self._text_signature)
+ self._text_signature = None
+
+ def chunk(self, data: str) -> list[Any]:
+ chunk = json_object(data)
+ if chunk is None:
+ return self._fail("Provider returned invalid Gemini JSON")
+ shape_error = validate_wire_shape(chunk, "chunk", _GOOGLE_WIRE_SHAPES)
+ if shape_error is not None:
+ return self._fail(shape_error)
+ error = chunk.get("error")
+ if isinstance(error, Mapping):
+ return self._fail(json.dumps(dict(chunk), ensure_ascii=False), kind=None)
+ usage = chunk.get("usageMetadata")
+ usage_error = usage_counter_error(usage, label="Gemini usageMetadata", fields=_GOOGLE_USAGE_FIELDS)
+ if usage_error is not None:
+ return self._fail(usage_error)
+ if isinstance(usage, Mapping):
+ self.assembler.set_usage(_google_usage(usage))
+ feedback = chunk.get("promptFeedback")
+ if isinstance(feedback, Mapping) and feedback.get("blockReason"):
+ self._blocked = feedback["blockReason"]
+ events: list[Any] = []
+ candidates = chunk.get("candidates") or []
+ if not candidates:
+ return events
+ candidate = candidates[0]
+ content = candidate.get("content")
+ for part in (content.get("parts") or []) if isinstance(content, Mapping) else []:
+ events.extend(self._part(part))
+ if candidate.get("finishReason"):
+ self._finish = candidate["finishReason"]
+ self._finish_message = candidate.get("finishMessage")
+ return events
+
+ def _part(self, part: Mapping[str, Any]) -> list[Any]:
+ events: list[Any] = []
+ signature = part.get("thoughtSignature") or None
+ call = part.get("functionCall")
+ if isinstance(call, Mapping):
+ name = call.get("name")
+ if not name:
+ return self._fail("Gemini function call name must not be empty")
+ self._end_block()
+ self._tools += 1
+ key = ("google-tool", self._tools)
+ start = self.assembler.tool_start(key, name=name, native_id=call.get("id") or None, signature=signature)
+ if start is not None:
+ events.append(start)
+ delta = self.assembler.tool_arguments(key, json.dumps(dict(call.get("args") or {}), ensure_ascii=False))
+ if delta is not None:
+ events.append(delta)
+ return events
+ text = part.get("text")
+ if part.get("thought") is True:
+ key = self._key("thinking")
+ if text:
+ emitted = self.assembler.thinking_delta(key, text)
+ if emitted is not None:
+ events.append(emitted)
+ if signature:
+ self.assembler.set_thinking_signature(key, signature)
+ return events
+ if text:
+ emitted = self.assembler.text_delta(self._key("text"), text)
+ if emitted is not None:
+ events.append(emitted)
+ if signature:
+ # A signature can come on any delta of the answer, or on an empty part after it: it belongs to the text.
+ if self._kind != "text":
+ self._end_block()
+ self._text_signature = signature
+ if self._kind != "text":
+ self._end_block()
+ # Parts of a kind this adapter does not declare (inline data, code execution) carry nothing to the loop.
+ return events
+
+ def finish(self) -> list[Any]:
+ if self._finish is None and self._blocked is None:
+ terminal = self.assembler.terminal(self.assembler.incomplete())
+ return [terminal] if terminal is not None else []
+ self._end_block()
+ events: list[Any] = list(self.assembler.block_end_events())
+ if self._finish is None:
+ final = self.assembler.finalize("safety", error_message=f"Gemini blocked the prompt: {self._blocked}")
+ else:
+ stop, message = _normalize_stop(self._finish, self._finish_message)
+ final = self.assembler.finalize(stop, error_message=message)
+ terminal = self.assembler.terminal(final if isinstance(final, ProviderError) else Done(final))
+ if terminal is not None:
+ events.append(terminal)
+ return events
+
+
+def build_google_payload(
+ request: ModelRequest,
+ messages: tuple[Any, ...],
+ *,
+ loaded_images: Mapping[str, LoadedImage] | None = None,
+) -> dict[str, Any]:
+ model = request.endpoint.model_id
+ payload: dict[str, Any] = {"contents": _contents(messages, model, request.supports_images, loaded_images)}
+ if request.system:
+ payload["systemInstruction"] = {"parts": [{"text": request.system}]}
+ if request.tools:
+ payload["tools"] = [{
+ "functionDeclarations": [
+ {"name": tool.name, "description": tool.description, "parametersJsonSchema": dict(tool.input_schema)}
+ for tool in request.tools
+ ],
+ }]
+ generation: dict[str, Any] = {"maxOutputTokens": request.max_tokens}
+ thinking = _thinking_config(model, _effort(request.reasoning_effort or ""))
+ if thinking is not None:
+ generation["thinkingConfig"] = thinking
+ payload["generationConfig"] = generation
+ return payload
+
+
+def wire_request(
+ request: ModelRequest,
+ messages: tuple[Any, ...],
+ *,
+ gateway: bool,
+ loaded_images: Mapping[str, LoadedImage] | None = None,
+) -> WireRequest:
+ """The ``streamGenerateContent`` request for already prepared ``messages``, as the adapter sends it."""
+
+ payload = build_google_payload(request, messages, loaded_images=loaded_images)
+ headers = auth_headers(request.endpoint, provider="google", gateway=gateway)
+ headers.setdefault("content-type", "application/json")
+ headers.update(hosted_pin(request, messages))
+ model = quote(request.endpoint.model_id, safe="")
+ url = join_endpoint_url(request.endpoint.base_url, f"/models/{model}:streamGenerateContent?alt=sse")
+ return WireRequest(url, headers, payload)
+
+
+def _contents(
+ messages: tuple[Any, ...],
+ model: str,
+ supports_images: bool,
+ loaded_images: Mapping[str, LoadedImage] | None,
+) -> list[dict[str, Any]]:
+ # Gemini validates the signatures of the current turn: everything after the last user message.
+ turn_start = max((index for index, message in enumerate(messages) if isinstance(message, UserMessage)), default=-1)
+ multimodal_results = _major_version(model) is None or (_major_version(model) or 0) >= 3
+ contents: list[dict[str, Any]] = []
+ # Gemini before 3 reads a result's images only from a user content of their own, which must follow the whole
+ # step's results: a content between two of them splits a parallel batch, and Gemini refuses that.
+ deferred_images: list[dict[str, Any]] = []
+
+ def flush_images() -> None:
+ if deferred_images:
+ contents.append({"role": "user", "parts": [{"text": "Tool result image:"}, *deferred_images]})
+ deferred_images.clear()
+
+ for position, message in enumerate(messages):
+ if not isinstance(message, ToolResultMessage):
+ flush_images()
+ if isinstance(message, UserMessage):
+ contents.append({"role": "user", "parts": _user_parts(message.content, supports_images, loaded_images)})
+ elif isinstance(message, AssistantMessage):
+ parts = _model_parts(message, current_turn=position > turn_start)
+ if parts:
+ contents.append({"role": "model", "parts": parts})
+ elif isinstance(message, ToolResultMessage):
+ response, images = _function_response(message, supports_images, loaded_images)
+ if images and multimodal_results:
+ response["functionResponse"]["parts"] = images
+ last = contents[-1] if contents else None
+ # One user content holds a step's results in call order; Gemini refuses them interleaved with calls.
+ if last is not None and last["role"] == "user" and all("functionResponse" in part for part in last["parts"]):
+ last["parts"].append(response)
+ else:
+ contents.append({"role": "user", "parts": [response]})
+ if images and not multimodal_results:
+ deferred_images.extend(images)
+ else:
+ raise TypeError(f"unsupported message {type(message).__name__}")
+ flush_images()
+ return contents
+
+
+def _user_parts(
+ content: tuple[Any, ...],
+ supports_images: bool,
+ loaded_images: Mapping[str, LoadedImage] | None,
+) -> list[dict[str, Any]]:
+ parts = [
+ {"text": part["text"]} if part["type"] == "text"
+ else {"inlineData": {"mimeType": part["mime_type"], "data": part["data"]}}
+ for part in content_parts(content, include_images=supports_images, loaded_images=loaded_images)
+ ]
+ return parts or [{"text": "(no content)"}]
+
+
+def _model_parts(message: AssistantMessage, *, current_turn: bool) -> list[dict[str, Any]]:
+ parts: list[dict[str, Any]] = []
+ first_call = True
+ for block in message.content:
+ if isinstance(block, TextBlock):
+ if block.text:
+ parts.append({"text": block.text})
+ elif isinstance(block, ThinkingBlock):
+ signature = _signature(block.signature)
+ if block.text:
+ part: dict[str, Any] = {"text": block.text, "thought": True}
+ elif signature is None:
+ continue
+ elif parts and set(parts[-1]) == {"text"}:
+ # An answer's signature, kept beside its text: back on the part it came on.
+ parts[-1]["thoughtSignature"] = signature
+ continue
+ else:
+ part = {"text": ""}
+ if signature is not None:
+ part["thoughtSignature"] = signature
+ parts.append(part)
+ elif isinstance(block, ToolCallBlock):
+ call: dict[str, Any] = {"functionCall": {"name": block.name, "args": dict(block.arguments), "id": block.id}}
+ signature = _signature(block.signature)
+ if signature is None and first_call and current_turn:
+ # Gemini validates the first call of every step in the current turn; one it did not sign is another
+ # origin's, which Google's documented stand-in lets through.
+ signature = DUMMY_THOUGHT_SIGNATURE
+ if signature is not None:
+ call["thoughtSignature"] = signature
+ first_call = False
+ parts.append(call)
+ return parts
+
+
+def _function_response(
+ message: ToolResultMessage,
+ supports_images: bool,
+ loaded_images: Mapping[str, LoadedImage] | None,
+) -> tuple[dict[str, Any], list[dict[str, Any]]]:
+ text = "\n".join(block.text for block in message.content if isinstance(block, TextBlock) and block.text is not None)
+ images = [
+ {"inlineData": {"mimeType": part["mime_type"], "data": part["data"]}}
+ for part in content_parts(
+ tuple(block for block in message.content if isinstance(block, ImageBlock)),
+ include_images=supports_images,
+ loaded_images=loaded_images,
+ )
+ if part["type"] == "image"
+ ]
+ value = text or ("(see attached image)" if images else "(no tool output)")
+ response = {
+ "functionResponse": {
+ "name": message.tool_name,
+ "id": message.tool_call_id,
+ "response": {"error": value} if message.is_error else {"output": value},
+ },
+ }
+ return response, images
+
+
+def _signature(value: str | None) -> str | None:
+ """A signature Gemini can take back: well-formed base64, which a Chat reasoning marker in the transcript is not."""
+
+ if value and len(value) % 4 == 0 and _SIGNATURE.fullmatch(value):
+ return value
+ return None
+
+
+def _major_version(model: str) -> int | None:
+ match = _MAJOR_VERSION.match(model.lower())
+ return int(match.group(1)) if match else None
+
+
+def _thinking_config(model: str, effort: str | None) -> dict[str, Any] | None:
+ """Thoughts are always asked for on a thinking model; the effort sets its level or budget."""
+
+ name = model.lower()
+ if _LEVEL_MODEL.match(name):
+ config: dict[str, Any] = {"includeThoughts": True}
+ level = _LEVELS.get(effort or "") or ("minimal" if effort == "none" else None)
+ if level == "minimal" and "flash" not in name:
+ # Only the Flash family thinks at MINIMAL; Pro-class models refuse it, so Google's own OpenAI-compatible
+ # layer sends LOW for them, and so does this.
+ level = "low"
+ if level is not None:
+ config["thinkingLevel"] = level
+ return config
+ if _BUDGET_MODEL.match(name):
+ config = {"includeThoughts": True}
+ if effort in _BUDGETS:
+ config["thinkingBudget"] = _BUDGETS[effort]
+ if _BUDGETS[effort] == 0:
+ config["includeThoughts"] = False
+ return config
+ return None
+
+
+def _normalize_stop(reason: str, message: str | None) -> tuple[str, str | None]:
+ if reason == "STOP":
+ return "stop", None
+ if reason == "MAX_TOKENS":
+ return "length", None
+ detail = f"Gemini finished with {reason}" + (f": {message}" if message else "")
+ return ("safety" if reason in _SAFETY_FINISH else "error"), detail
+
+
+def _google_usage(value: Mapping[str, Any]) -> Any:
+ """Project ``usageMetadata``: the prompt count includes cached tokens, and thoughts are counted apart from the
+ candidates, so output is both (as every other adapter's output includes reasoning)."""
+
+ from core.agent_core.messages import Usage
+
+ cached = _count(value.get("cachedContentTokenCount"))
+ thoughts = value.get("thoughtsTokenCount")
+ return Usage(
+ input_tokens=max(0, _count(value.get("promptTokenCount")) - cached),
+ output_tokens=_count(value.get("candidatesTokenCount")) + _count(thoughts),
+ cache_read_tokens=cached,
+ reasoning_tokens=_count(thoughts) if thoughts is not None else None,
+ )
+
+
+def _effort(effort: str) -> str | None:
+ value = effort.lower()
+ return None if not value or value in {"off", "disabled"} else value
+
+
+def sent_effort(model_id: str, effort: str) -> str | None:
+ """The effort token a request to ``model_id`` carries for ``effort``: its thinking level, or the effort its thinking
+ budget stands for. Model Hub keys effort evidence by it, and the gateway reads it back from the request
+ (``wire_effort``), so a refusal governs every effort that sends the same thing."""
+
+ return _wire_effort((_thinking_config(model_id, _effort(effort)) or {}))
+
+
+def wire_effort(request: Mapping[str, Any]) -> str | None:
+ """The effort token a Gemini request body carries (``sent_effort``), or None without a thinking control."""
+
+ generation = request.get("generationConfig")
+ thinking = generation.get("thinkingConfig") if isinstance(generation, Mapping) else None
+ return _wire_effort(thinking) if isinstance(thinking, Mapping) else None
+
+
+def _wire_effort(thinking: Mapping[str, Any]) -> str | None:
+ level = thinking.get("thinkingLevel")
+ if isinstance(level, str) and level:
+ return level.lower()
+ budget = thinking.get("thinkingBudget")
+ if type(budget) is int:
+ return _BUDGET_EFFORTS.get(budget, str(budget))
+ return None
+
+
+def _count(value: Any) -> int:
+ return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else 0
+
+
+GoogleProvider = GoogleAdapter
diff --git a/core/agent_core/ai/provider.py b/core/agent_core/ai/provider.py
index 5d13834dd..4eaf60684 100644
--- a/core/agent_core/ai/provider.py
+++ b/core/agent_core/ai/provider.py
@@ -127,9 +127,6 @@ class ModelEndpoint:
hosted_tools: Mapping[str, HostedToolRoute] = field(default_factory=dict)
#: The models ``image_generate`` may call; None when the tool is off or no model is available.
image_models: Optional[ImageModels] = None
- #: The upstream protocol whose response origin and tokenizer anchor C-9 for a converted hop; ``None`` when the
- #: wire protocol is also the served protocol.
- origin_api: Optional[ProtocolName] = None
@dataclass(frozen=True)
diff --git a/core/agent_core/ai/registry.py b/core/agent_core/ai/registry.py
index 93c22b55d..f62191989 100644
--- a/core/agent_core/ai/registry.py
+++ b/core/agent_core/ai/registry.py
@@ -4,8 +4,9 @@ from __future__ import annotations
from typing import Any
-from core.agent_core.ai import anthropic, openai_chat, openai_responses
+from core.agent_core.ai import anthropic, google, openai_chat, openai_responses
from core.agent_core.ai.anthropic import AnthropicAdapter
+from core.agent_core.ai.google import GoogleAdapter
from core.agent_core.ai.openai_chat import OpenAIChatAdapter
from core.agent_core.ai.openai_responses import OpenAIResponsesAdapter
from core.agent_core.ai.provider import ProviderAdapter
@@ -13,22 +14,25 @@ from core.agent_core.messages import ProtocolName
ADAPTERS: dict[ProtocolName, type[ProviderAdapter]] = {
"anthropic": AnthropicAdapter,
+ "google": GoogleAdapter,
"openai_chat": OpenAIChatAdapter,
"openai_responses": OpenAIResponsesAdapter,
}
def sent_effort(protocol: str, model_id: str, effort: str) -> str | None:
"""The effort token a request on ``protocol`` to ``model_id`` carries for ``effort``, exactly as the adapter sends
- it: at ``output_config.effort`` (Anthropic), ``reasoning.effort`` (Responses) or ``reasoning_effort`` (Chat; a
- ``google`` hop is reached over Chat). None when the request carries no effort token. Model Hub's effort menu reads
- the same answer, so the menu and the wire cannot drift."""
+ it: at ``output_config.effort`` (Anthropic), ``reasoning.effort`` (Responses), ``reasoning_effort`` (Chat), or the
+ thinking level a Gemini request's ``thinkingConfig`` carries, a budget named by the effort it stands for
+ (``google``). None when the request carries no effort token. Model Hub's effort menu reads the same answer, so the menu and the wire cannot drift."""
if protocol == "anthropic":
return anthropic.sent_effort(model_id, effort)
if protocol == "openai_responses":
return openai_responses.sent_effort(effort)
- if protocol in ("openai_chat", "google"):
+ if protocol == "openai_chat":
return openai_chat.sent_effort(effort)
+ if protocol == "google":
+ return google.sent_effort(model_id, effort)
raise ValueError(f"unsupported provider protocol: {protocol}")
diff --git a/core/agent_core/harness/context.py b/core/agent_core/harness/context.py
index 525b54686..3f646fc0b 100644
--- a/core/agent_core/harness/context.py
+++ b/core/agent_core/harness/context.py
@@ -34,7 +34,6 @@ from core.agent_core.messages import (
HostedToolBlock,
ImageBlock,
Message,
- Origin,
TextBlock,
ThinkingBlock,
ToolCallBlock,
@@ -529,14 +528,6 @@ def checkpoint_max_tokens(capabilities: ModelCapabilities, configured: int) -> i
return min(CHECKPOINT_MAX_TOKENS, output_tokens(capabilities, configured))
-def _anchor_origin(endpoint: ModelEndpoint) -> Origin:
- """The served origin that anchors C-9 usage for this request, including converted routes."""
- target = endpoint_origin(endpoint)
- if endpoint.origin_api is None:
- return target
- return Origin(provider=target.provider, api=endpoint.origin_api, model=target.model)
-
-
def _estimate(request: ModelRequest, transcript: Sequence[Message], anchor: Optional[Anchor]) -> int:
"""The anchored usage while it holds, grown by the ``tokens()`` of what changed since; else ``tokens()`` of the
request (section 2).
@@ -549,7 +540,7 @@ def _estimate(request: ModelRequest, transcript: Sequence[Message], anchor: Opti
removed is a local estimate taken from a provider count.
"""
whole = sent_tokens(request)
- if anchor is None or anchor.response.origin != _anchor_origin(request.endpoint):
+ if anchor is None or anchor.response.origin != endpoint_origin(request.endpoint):
return whole
sent = len(anchor.transcript)
if len(transcript) <= sent or transcript[sent] != anchor.response:
diff --git a/core/handlers/model_hub/evidence_rules.py b/core/handlers/model_hub/evidence_rules.py
index c8502a937..fa95d8e0a 100644
--- a/core/handlers/model_hub/evidence_rules.py
+++ b/core/handlers/model_hub/evidence_rules.py
@@ -45,8 +45,12 @@ class LearnedEvidence:
def wire_effort(request: Mapping[str, Any], protocol: str) -> Optional[str]:
- """The reasoning effort this request sends, as the wire spells it."""
+ """The reasoning effort this request sends, as the wire spells it: the token ``registry.sent_effort`` names."""
+ if protocol == "google":
+ from core.agent_core.ai.google import wire_effort as google_wire_effort
+
+ return google_wire_effort(request)
value: Any = request
for key in _EFFORT_PATHS.get(protocol, ()):
value = value.get(key) if isinstance(value, Mapping) else None
diff --git a/core/handlers/model_hub/hosted_tools.py b/core/handlers/model_hub/hosted_tools.py
index 28b15e75c..f219e013f 100644
--- a/core/handlers/model_hub/hosted_tools.py
+++ b/core/handlers/model_hub/hosted_tools.py
@@ -89,12 +89,6 @@ def credential_eligible(source: ModelHubSourceConfig) -> bool:
return not (source.vendor == "anthropic" and source.kind == "subscription")
-def untranslated(source: ModelHubSourceConfig) -> bool:
- """Vibey speaks the Source's own protocol, except Chat Completions to a ``google`` Source."""
-
- return source.protocol != "google"
-
-
def _serves(source: ModelHubSourceConfig, model_id: str) -> bool:
"""Whether a hub Source serves this model by its admission rule.
@@ -139,12 +133,12 @@ def runs_on(source: ModelHubSourceConfig, model_id: str, tool: str, *, spec_for:
"""The declaration a search ``tool`` runs with natively on this (Source, model), or None where it cannot.
One rule for every place that asks: a hop's facts, an executor candidate,
- and admission. The Source is a hub Source with an eligible credential and an
- untranslated path, it serves the model, and the overlay declares the tool
- for it.
+ and admission. The Source is a hub Source with an eligible credential (Vibey
+ speaks every Source's own protocol, so its path is untranslated), it serves
+ the model, and the overlay declares the tool for it.
"""
- if source.supply_channel != "hub" or not credential_eligible(source) or not untranslated(source):
+ if source.supply_channel != "hub" or not credential_eligible(source):
return None
if not _serves(source, model_id):
return None
diff --git a/core/handlers/model_hub/request.py b/core/handlers/model_hub/request.py
index 1898c1f29..94277ab9c 100644
--- a/core/handlers/model_hub/request.py
+++ b/core/handlers/model_hub/request.py
@@ -88,6 +88,12 @@ def _plain_history(value: Any) -> Any:
]
return {"role": "assistant", "content": texts} if texts else None
plain = {key: item for key, item in value.items() if key not in _OPAQUE_FIELDS}
+ if "functionCall" in value and "thoughtSignature" in value:
+ # Gemini 3+ refuses a current-turn call without a signature, and this account's cannot go to another:
+ # Google's documented stand-in takes its place, as the engine's Chat translation always wrote.
+ from core.agent_core.ai.google import DUMMY_THOUGHT_SIGNATURE
+
+ plain["thoughtSignature"] = DUMMY_THOUGHT_SIGNATURE
if plain.get("thought") is True:
# Gemini's readable thought text is ordinary history across origins.
plain.pop("thought")
diff --git a/docs/plans/agent-core-contracts/conversion-tables.md b/docs/plans/agent-core-contracts/conversion-tables.md
index 5039d6d2f..ef7170781 100644
--- a/docs/plans/agent-core-contracts/conversion-tables.md
+++ b/docs/plans/agent-core-contracts/conversion-tables.md
@@ -29,7 +29,7 @@ rules on top of those mappings.
| Partial and abort | The shared `partial_message` policy is used by all three adapters. Usage captured before the first visible delta is retained, and errors/abort carry an assembled `AssistantMessage` whenever visible content or usage exists; empty placeholder slots alone do not count as streamed output, while non-empty terminal snapshots do. Consumer/task cancellation is preserved even if response cleanup fails. |
| Retry admission | `RetryPolicy.delay` in `core/agent_core/agent/models.py` applies only the bounded retry count/time budget and obeys `ProviderError.retryable`; it does not reject a usage-only partial or infer a second streamed-output boundary. It admits at most one `after_thinking` retry per request; that attempt's thinking partial is never context, like any retried attempt's. A retry-admitted usage-only partial is never context: the retry resends the original request, and the attempt's usage is one non-context `ModelAttempt` audit row, in every mode (C-9 `context.md` §10, invariant 4), so per-attempt usage is kept without adding prompt attempts together in one response. `test_retry_obeys_provider_flag_and_allows_usage_only_partial` and `test_retry_keeps_a_usage_only_partial_out_of_the_context` are the loop proofs. |
| Tool-call stop normalization | `StreamAssembler.finalize` changes only `stop` with one or more tool calls to canonical `tool_use`, following Pi. The loop admits execution only for `stop_reason="tool_use"`; calls attached to `length`, `safety`, `refusal`, or `error` are settled as `is_error` results explaining the stop reason. |
-| Parallel tool calls | No request turns them off, so a response may carry several calls: Anthropic Messages sends no `tool_choice`, so `disable_parallel_tool_use` keeps its default `false`; Chat Completions and Responses send no `parallel_tool_calls`, which defaults to `true`; a `google` hop is called over Chat Completions at the Model Hub gateway (`modules/agents/vibey/models.py`), whose translation to Gemini sets no `toolConfig` for such a request. No request may send `parallel_tool_calls: false`: Gemini has no switch for it, and the gateway's engine (CLIProxyAPI `c404af96`, `internal/translator/gemini/openai/chat-completions/gemini_openai_request.go`) then turns every tool off. The loop runs neighbouring concurrent calls at the same time and every `edit` or `write` alone (C-3 `loop-control.md` §2), a deviation from Pi, which runs a response's calls at once and serializes mutations only per file path. |
+| Parallel tool calls | No request turns them off, so a response may carry several calls: Anthropic Messages sends no `tool_choice`, so `disable_parallel_tool_use` keeps its default `false`; Chat Completions and Responses send no `parallel_tool_calls`, which defaults to `true`; a Gemini request sends no `toolConfig`, and Gemini has no switch for it. No Chat request may send `parallel_tool_calls: false`: a Chat relay serving Gemini has no switch to map it to, and the gateway's engine (CLIProxyAPI `c404af96`, `internal/translator/gemini/openai/chat-completions/gemini_openai_request.go`) then turns every tool off. The loop runs neighbouring concurrent calls at the same time and every `edit` or `write` alone (C-3 `loop-control.md` §2), a deviation from Pi, which runs a response's calls at once and serializes mutations only per file path. |
## Shared stream assembler and dispatch ledger
@@ -473,7 +473,8 @@ Pi's `processResponsesStream`, `openai-completions.stream`, and
`anthropic-messages.stream`, then projected both outputs to:
`outcome`, canonical stop reason, canonical blocks, usage, error kind, and
partial blocks. The run covered Responses, Chat, and Anthropic. The Google
-adapter was not included because the owner deferred it from v1.
+adapter was not included because the owner deferred it from v1; it was added on 2026-10-27
+(`../vibey-gemini-native.md`), after this recorded differential.
| Fixture | Pi projection | Avibe projection | Difference |
| --- | --- | --- | --- |
@@ -645,6 +646,48 @@ Responses terminal states:
| `incomplete_details.reason=content_filter` or `safety` | `safety` |
| Failed/cancelled | terminal `ProviderError` (classified `unknown` when no provider error code is present) |
+## Google Gemini
+
+Ported from Pi `google-shared.ts` and `google-generative-ai.ts` and tau `tau_ai/google.py`
+(`core/agent_core/ai/google.py`; plan `../vibey-gemini-native.md`). Vibey speaks it to a `google` hop at the
+gateway's Gemini surface, which serves it to a Gemini Source untranslated.
+
+| Canonical field | Request mapping |
+| --- | --- |
+| URL and auth | `<base>/models/<model>:streamGenerateContent?alt=sse`; the body names neither model nor stream mode. Direct requests send `x-goog-api-key`; gateway requests the gateway token as a bearer. |
+| `system` | `systemInstruction.parts[0].text`. |
+| User text/image | `role="user"` content; text parts and `inlineData {mimeType, data}`. |
+| Assistant text | `role="model"`, a `text` part per block. |
+| Thinking | Same origin: a signed or unsigned thought is `{text, thought: true}` with its `thoughtSignature`. An empty block that holds only a signature goes back on the text part right before it (an answer's signature, which `TextBlock` cannot hold), else as `{text: "", thoughtSignature}`. A signature that is not well-formed base64 (a Chat reasoning marker an older transcript holds) is not sent. |
+| Tool call | `functionCall {name, args, id}` with the call's `thoughtSignature`. In the current turn (after the last user message), the first call of a step that carries no valid signature gets Google's documented stand-in `skip_thought_signature_validator`, so another origin's calls are accepted; earlier turns are not validated and get none. |
+| Tool result | `functionResponse {name, id, response: {output}}`, or `{error}` for an error result; image-only text is `(see attached image)`, empty text `(no tool output)`. A step's results share one `role="user"` content in call order. Images ride in `functionResponse.parts` on Gemini 3 and later and on non-Gemini ids, else in one user content after `Tool result image:` that follows the whole step's results. |
+| Tools | One `functionDeclarations` list with `name`, `description`, and the JSON Schema as `parametersJsonSchema`. |
+| Reasoning effort | Gemini 3 and later and the `-latest` aliases get `thinkingConfig.thinkingLevel` (`xhigh`/`max` → `high`; `none` → `minimal`; `minimal` → `low` outside the Flash family, which alone takes it); Gemini 2.5 gets `thinkingBudget` (`minimal`/`low` 1024, `medium` 8192, `high` and above 24576, `none` 0 without thoughts). `includeThoughts` is set on every thinking model; other models get no `thinkingConfig`. The effort token (`registry.sent_effort`, the key Model Hub holds effort evidence under, and what its gateway reads back from the request) is the thinking level sent, or the effort a budget stands for (`0` none, `1024` low, `8192` medium, `24576` high). |
+| Output bound | `generationConfig.maxOutputTokens`. |
+
+Gemini stream events (each `data:` frame is a `GenerateContentResponse` whose parts are deltas):
+
+| Wire field | Outcome | Provider event / state |
+| --- | --- | --- |
+| `candidates[0].content.parts[].text` with `thought: true` | delta | `ThinkingDelta`; consecutive thought parts extend one block, and its `thoughtSignature` is the block's signature. |
+| `…parts[].text` | delta | `TextDelta`; consecutive text parts extend one block. A `thoughtSignature` on any of them, or on an empty part right after them, becomes an empty signed `ThinkingBlock` after that text once the text ends. |
+| `…parts[].functionCall` | delta | `ToolCallStart` and one `ToolCallDelta` of the JSON `args`; `id` is the native id, `thoughtSignature` the call's signature. An empty name is terminal `invalid_request`. |
+| any other part | ignored | Parts of a kind the request did not declare (inline data, code execution). |
+| `candidates[0].finishReason` | state | Stop reason (below). |
+| `usageMetadata` | state | `input = promptTokenCount - cachedContentTokenCount`; `output = candidatesTokenCount + thoughtsTokenCount`; `cache_read = cachedContentTokenCount`; `reasoning = thoughtsTokenCount`. The last frame's counts win. |
+| `promptFeedback.blockReason` without a finish | Done | `safety`, with the block reason as `error_message`. |
+| top-level `error` | terminal `ProviderError` | Classified from its body (`status`, `code`). |
+| EOF without a finish | terminal `ProviderError` | Incomplete `network` error, retryable only before output. |
+
+Gemini stop reasons:
+
+| Wire value | Canonical |
+| --- | --- |
+| `STOP` | `stop` (`tool_use` with calls) |
+| `MAX_TOKENS` | `length` |
+| `SAFETY`, `RECITATION`, `BLOCKLIST`, `PROHIBITED_CONTENT`, `SPII`, `IMAGE_SAFETY`, `IMAGE_PROHIBITED_CONTENT`, `IMAGE_RECITATION` | `safety` |
+| anything else (`MALFORMED_FUNCTION_CALL`, `OTHER`, …) | `error`, with the reason as `error_message` |
+
## Hosted tools
Designed by [`../vibey-hosted-tools.md`](../vibey-hosted-tools.md) (v2: the search tools `web_search` and `x_search`
@@ -700,7 +743,7 @@ An Anthropic citation covers its whole text block, so it carries no range.
| Concern | Anthropic Messages | OpenAI Responses (OpenAI, xAI) | OpenAI Chat Completions |
| --- | --- | --- | --- |
-| Native declaration (mode from `HopResolution.hosted_tools`) | `{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}` | `{"type": "web_search"}`; xAI also `{"type": "x_search"}`; plus the overlay entry's `include` values (OpenAI web search: `web_search_call.action.sources`) | none. A `google` hop is reached over this Chat form, and the engine translates it to Gemini. |
+| Native declaration (mode from `HopResolution.hosted_tools`) | `{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}` | `{"type": "web_search"}`; xAI also `{"type": "x_search"}`; plus the overlay entry's `include` values (OpenAI web search: `web_search_call.action.sources`) | none |
| Proxy declaration | `{"name": <tool>, "input_schema": <canonical>}` | `{"type": "function", "name": <tool>, "parameters": <canonical>}` | `{"type": "function", "function": {...}}` |
| Native result → `HostedToolBlock` | every item under a declared hosted tool, one block each, in stream order, mapped to a provider-neutral `action` by the rule in `../vibey-hosted-tools.md` §4.2. `server_tool_use` plus its `web_search_tool_result` → `search`; `encrypted_content` only in `native.payload`. | `web_search_call` `search` (`queries[]`, else `query`) / `open_page` / `find_in_page`; xAI `custom_tool_call` `x_keyword_search` / `x_semantic_search` / `x_user_search` / `x_thread_fetch`. The `tco_…` `reasoning` items stay thinking blocks. Unknown items become `other`, never dropped. | — |
| Response-level facts → `AssistantMessage.hosted_summary` | `usage.server_tool_use` counts | xAI `cost_in_usd_ticks` (reported once per response), `server_side_tool_usage_details` counts, and the X-search `url_citation`s as `cited_sources` | — |
diff --git a/docs/plans/agent-core-contracts/model-hub-consumer.md b/docs/plans/agent-core-contracts/model-hub-consumer.md
index 415c391d0..de63e4d9d 100644
--- a/docs/plans/agent-core-contracts/model-hub-consumer.md
+++ b/docs/plans/agent-core-contracts/model-hub-consumer.md
@@ -61,7 +61,9 @@ consumer's requirements, and the Model Hub lane lands the change in both places.
the origin: a provider refuses another account's ciphertext. The serving identity is read on the final admission
snapshot, so a key rotated mid-turn counts. A pinned request is never rewritten: one pinned to the primary hop
whose identity has changed since the launch answers `pin_unavailable`, and the agent re-resolves and renders it
- for the fresh hop, whose identity drops the old payloads.
+ for the fresh hop, whose identity drops the old payloads. A Gemini `functionCall` part whose signature is removed
+ gets Google's documented stand-in `skip_thought_signature_validator` instead, since Gemini 3+ refuses a
+ current-turn call without one (`../vibey-gemini-native.md`).
The identity stays in memory: it is never in the served-hop report or persisted provenance. Known limit:
the next request targets the primary origin again, so payloads returned by a fallback origin are not replayed if a
later request fails over to that same fallback; a provider that then rejects the call surfaces as a provider error.
diff --git a/docs/plans/vibey-gemini-native.md b/docs/plans/vibey-gemini-native.md
new file mode 100644
index 000000000..8e09af6fa
--- /dev/null
+++ b/docs/plans/vibey-gemini-native.md
@@ -0,0 +1,127 @@
+# Vibey on native Gemini
+
+Status: **plan**, 2026-10-27, owner-approved direction ("直接做第 2 步").
+Scope: Vibey only. Claude Code, Codex and OpenCode are unchanged.
+
+It implements the Google adapter that `avibe-agent-core.md` §3 item 3 always named ("Native protocols: … Google
+Gemini") and the owner deferred from v1 (`agent-core-contracts/conversion-tables.md`, "Recorded Pi stream
+differential"), and uses it to give Gemini models their own web search the way Claude and GPT already have theirs
+(`vibey-hosted-tools.md`).
+
+## 0. Owner decisions (2026-10-27)
+
+1. **Vibey speaks Gemini natively on a `google` hop.** It calls the gateway's Gemini frontend
+ (`/<backend>/v1beta/models/<model>:streamGenerateContent?alt=sse`, #2340) instead of Chat Completions, so Gemini
+ features reach Vibey untranslated. Keeping Chat and asking the engine to carry Gemini's server-side tool parts,
+ grounding and signatures through private Chat fields was rejected: it is a dialect nobody else speaks, it lives in
+ an upstream translator, and the thought-signature failure fixed by #3025 is exactly that class of bug.
+2. **Gemini's web search is Google Search grounding, declared natively** on a Gemini hop, as Claude's and GPT's are
+ on theirs (`vibey-hosted-tools.md` §4.1 native mode). One behaviour on every surface (Web and IM alike).
+3. **Google's Search Suggestions are not displayed.** The Gemini API Additional Terms ask an application to show
+ grounded results "with the associated Search Suggestion(s)" and not to modify them. The owner judged that no
+ agent product follows this (Google's own Gemini CLI returns only the answer and a source list to its model) and
+ accepted the risk, which falls on the account that owns the API key. The public docs state where queries go and
+ what they cost.
+4. **A Gemini API key speaks the `google` protocol.** New keys use it, and an existing key on Google's
+ OpenAI-compatible endpoint moves to it (§3, PR 2).
+
+## 1. Facts this plan rests on
+
+Measured on 2026-10-27 against the real Gemini API (`gemini-4-argon`, the regression environment's key) and the
+pinned engine (CLIProxyAPI v8.0.30, `5be2cdab`):
+
+- **Google's OpenAI-compatible endpoint cannot run Google Search.** `tools: [{"google_search": {}}]`,
+ `{"type": "google_search"}`, `{"type": "web_search"}` and `extra_body.google.tools` are each `400
+ INVALID_ARGUMENT`. Google documents grounding on that layer for the Images endpoint only.
+- **The native API runs it next to function calling**, with `toolConfig.includeServerSideToolInvocations: true`;
+ without it, `400 "Please enable tool_config.include_server_side_tool_invocations to use Built-in tools with
+ Function calling."`. The response carries the search as parts: `toolCall {toolType: "GOOGLE_SEARCH_WEB", args:
+ {queries}, id}` and `toolResponse {toolType, response: {search_suggestions}, id}`, each with a `thoughtSignature`,
+ then the answer text, and `candidates[0].groundingMetadata` with `webSearchQueries`, `groundingChunks[].web {uri,
+ title}` (Google redirect links, the title is the domain) and `groundingSupports` (text segments and the chunks
+ they cite).
+- **Price** (Gemini 3 and later): per search query the model runs, 5,000 free a month across those models, then
+ $14 per 1,000. One request may run several queries (two in the probe).
+- **The engine's Chat translation loses Gemini state**: the Gemini→Chat response translator drops every
+ `thoughtSignature`, so a Chat client never holds one to send back, and the Chat→Gemini request translator then
+ writes `skip_thought_signature_validator` on each call (the Antigravity translator writes it on every call
+ unconditionally); neither sets `includeServerSideToolInvocations` or converts server-side tool parts or grounding
+ (`internal/translator/{gemini,antigravity}/openai/chat-completions/`). A `gemini-api-key` entry serves the native
+ frontend without translation.
+- **The engine's images endpoint** serves GPT Image, xAI, and image models registered on an
+ `openai-compatibility` entry only (`sdk/api/handlers/openai/openai_images_handlers.go`), not a `gemini-api-key`
+ entry. Gemini image generation today relies on the OpenAI-compatible entry (`vibey-hosted-tools.md` §7.4).
+- **Thought signatures** (Google, "Thought signatures"): Gemini 3+ validates the signature of the first
+ `functionCall` part of every step in the *current turn* (since the last user message with ordinary content); a
+ signature belongs to the exact part it came on; parallel calls carry it on the first call only; a response
+ without calls may carry it on an empty final text part. A function call Gemini did not produce may carry the
+ documented dummy `skip_thought_signature_validator`.
+- **Model ids differ by surface.** The OpenAI-compatible listing names `models/gemini-4-argon`; the native listing
+ (`_project_google_inventory`) yields `gemini-4-argon`.
+
+## 2. Vibey's Google adapter (`core/agent_core/ai/google.py`)
+
+Ported from Pi (`packages/ai/src/api/google-shared.ts`, `google-generative-ai.ts`, MIT, `7fbbd5f`) and tau
+(`tau_ai/google.py`, MIT) onto the shared stream assembler, exactly as the other three adapters are. The
+conversion table is in `agent-core-contracts/conversion-tables.md` ("Google Gemini").
+
+- **Request**: `systemInstruction`; `contents` with `user`/`model` roles; `functionDeclarations` with
+ `parametersJsonSchema`; `generationConfig.maxOutputTokens`; `thinkingConfig` with `includeThoughts` and the
+ effort as `thinkingLevel` (Gemini 3 and later, the `-latest` aliases) or `thinkingBudget` (Gemini 2.5).
+- **Signatures**: a same-origin signature goes back on the part it came on, only when it is well-formed base64 (a
+ transcript written over Chat may hold Chat reasoning markers). The first function call of a current-turn step
+ that has none (another origin's call, or an unverified one) carries the documented dummy, so a mid-turn failover
+ onto Gemini is accepted.
+- **Tool results**: one `user` content holds a step's `functionResponse` parts in call order (Gemini refuses
+ interleaving); a result's images ride in `functionResponse.parts` on Gemini 3 and later, else in a following user
+ content.
+- **Effort tokens** stay Chat's (`registry.sent_effort`): the menu and the evidence keys Model Hub holds for a
+ `google` hop do not move; the adapter maps the token to `thinkingConfig`.
+- **Hop resolution** (`modules/agents/vibey/models.py`) stops rewriting a `google` hop to Chat, so every Vibey path is
+ untranslated and `hosted_tools.runs_on` no longer excludes `google` Sources; their native hosted payloads are not
+ replayed until §4 adds Gemini to `NATIVE_REPLAY_PROTOCOLS`.
+- **Verified** against the real Gemini API (`gemini-4-argon`, `gemini-flash-latest`, `gemini-2.5-flash`): sequential
+ and parallel calls with their signatures, another origin's current-turn call with the stand-in, and the same body
+ through the pinned engine's `gemini-api-key` entry, which forwards it untranslated.
+
+## 3. Gemini API keys on the `google` protocol (PR 2)
+
+- `vibe/data/api_key_vendors.json`'s `gemini` row pins `google` on `https://generativelanguage.googleapis.com`.
+- An existing official-endpoint `gemini` Source on `openai_chat` moves to `google`: protocol and base URL change,
+ its models are listed again natively, and every hop that names a `models/<id>` model of it names `<id>`. The
+ Source keeps its id, credential and routes; its identity changes, so stored opaque payloads stop replaying once
+ (the documented safe degradation, `model-hub-consumer.md` item 3).
+- Gemini image models stay on the engine's images endpoint: the Source's engine registration adds an internal
+ OpenAI-compatible entry for its image models only. The user still sees one Source.
+- The Web UI's protocol picker gains `google` (design frame first, `../avibe-docs/design.pen`).
+
+## 4. Google Search as Gemini's native `web_search` (PR 3)
+
+- The overlay's `gemini` provider row declares `web_search` on `google` as `{"googleSearch": {}}` with the per-query
+ price; the adapter adds `toolConfig.includeServerSideToolInvocations` when it declares one.
+- A response's `toolCall`/`toolResponse` pair becomes one `HostedToolBlock` (`action: search` with the queries;
+ the record's sources from `groundingChunks`; the answer's citations from `groundingSupports`), with the parts
+ verbatim as its native payload for same-Source replay; any other target gets the synthetic call and result
+ (`vibey-hosted-tools.md` §5). The raw `search_suggestions` markup is not kept in the record.
+- Evidence (`evidence_rules.py`): a served response with a `GOOGLE_SEARCH_WEB` `toolCall` is positive; an error
+ naming the tool before output is the narrow negative rule. Cost: queries × list price, from
+ `groundingMetadata.webSearchQueries`.
+- Public docs (`avibe-docs`, en and zh): Gemini searches with Google, queries go to Google, and the price.
+
+## 5. Delivery
+
+| PR | Scope | Visible result |
+| --- | --- | --- |
+| 1 | §2, this plan, the conversion table | A `google` Source (API-configured today) runs Vibey natively; no change for Chat Sources |
+| 2 | §3 | Gemini keys run natively; images unchanged |
+| 3 | §4 | Gemini models search with Google |
+
+Each PR is verified against the real Gemini API on the local regression environment. PR 2 and PR 3 are independent
+of each other once PR 1 is merged.
+
+## 6. Known limits
+
+- A Gemini subscription (Antigravity) is served by the engine over Chat only; it keeps Exa/Tavily search.
+- Source links in Google's grounding are redirect URLs; the card shows the domain as title.
+- The OpenAI-compatible Chat path keeps no dummy for another origin's current-turn call: after PR 2 no official
+ Gemini Source uses it, and a Chat relay that serves Gemini is the relay's to translate.
diff --git a/docs/plans/vibey-hosted-tools.md b/docs/plans/vibey-hosted-tools.md
index 80c0394aa..2f5393f78 100644
--- a/docs/plans/vibey-hosted-tools.md
+++ b/docs/plans/vibey-hosted-tools.md
@@ -164,7 +164,8 @@ The evidence store, its record and its owner are the capability model's §3. v2
current while file writes are throttled, and a Source that changes identity hands the line to the newest other
success rather than to an older file copy. There is no separate per-tool projection to drift from the records.
- Positive evidence never changes routing. It feeds the "最近一次 … 正常" lines (§8) and nothing else.
- - A `google` hop has no native hosted tools, and the Chat route Vibey uses gives no hosted item to observe.
+ - A `google` hop has no native hosted tools yet; Vibey speaks Gemini natively there (amended 2026-10-27,
+ `vibey-gemini-native.md`), and Google Search joins as Gemini's native `web_search` in its §4.
- **Negative (`unsupported`) is routing state.** When the file write fails, the narrow rule still updates the
in-memory store and logs the loss, so the re-resolve that follows (§3.5) cannot declare the refused tool natively
again. The narrow rules of #2551 fire on a real error before output, unchanged in form:
@@ -372,7 +373,8 @@ failover, and is refused unpinned (C-6 item 7). What changes is what happens aft
| --- | --- | --- |
| `openai_responses` (OpenAI, xAI) | `{"type": "web_search"}`, `{"type": "x_search"}`, plus the overlay entry's `include` values. OpenAI's web search requires `include: ["web_search_call.action.sources"]`, merged with Vibey's existing `reasoning.encrypted_content`. | `{"type": "function", "name": <tool>, "parameters": <canonical>}` |
| `anthropic` | `{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}` | `{"name": <tool>, "input_schema": <canonical>}` |
-| `openai_chat`, `google` | none | a function with the canonical schema. Vibey reaches a `google` hop over Chat Completions at the gateway (C-1 conversion tables), and the engine translates it to Gemini `functionDeclarations`. |
+| `openai_chat` | none | `{"type": "function", "function": {"name": <tool>, "parameters": <canonical>}}` |
+| `google` | none yet (`vibey-gemini-native.md` §4) | a `functionDeclarations` entry with the canonical schema as `parametersJsonSchema`; Vibey speaks Gemini natively (amended 2026-10-27) |
- Vibey declares a search tool natively exactly when it is enabled and `native` is `assumed`; otherwise it declares
the same-named function. It never declares a hosted image tool.
diff --git a/modules/agents/vibey/agent.py b/modules/agents/vibey/agent.py
index 7677db9d9..c6688c966 100644
--- a/modules/agents/vibey/agent.py
+++ b/modules/agents/vibey/agent.py
@@ -833,7 +833,7 @@ class VibeyAgent(BaseAgent):
raise
bind_launch(context, launch)
# Every resolved hop, the first and each retry's, carries the output the Agent asks for on it.
- return budgeted(selection_from_hop(launch.to_hop_resolution(), gateway_base_url=launch.gateway_base_url))
+ return budgeted(selection_from_hop(launch.to_hop_resolution()))
return HubModelRouter(resolve, self._providers, first=await resolve())
diff --git a/modules/agents/vibey/models.py b/modules/agents/vibey/models.py
index 1b713e908..e79f4e30f 100644
--- a/modules/agents/vibey/models.py
+++ b/modules/agents/vibey/models.py
@@ -6,10 +6,9 @@ turns it into the loop's ``ModelEndpoint`` and ``ModelCapabilities`` and keeps
unknown capabilities ``None``; the loop applies the conservative defaults.
Transport is the provider adapter registered for the endpoint's protocol.
-A ``google`` hop is called over Chat Completions at the same gateway: Model Hub
-serves every source on its OpenAI-compatible ``/v1`` surface and converts, so
-the agent needs no Gemini transport. The served origin still comes from
-``x-avibe-served-hop``.
+A ``google`` hop is called natively, at the gateway's ``/v1beta`` Gemini
+surface (``vibey-gemini-native.md``). The served origin comes from
+``x-avibe-served-hop`` on every protocol.
Each enabled hosted tool's route rides on the endpoint as Model Hub resolved it
(``hosted_tools``): an adapter declares a tool natively exactly where its mode
@@ -142,13 +141,8 @@ def _image_models(entry: Any) -> ImageModels:
return ImageModels(tuple(models))
-def selection_from_hop(hop: Mapping[str, Any], *, gateway_base_url: Optional[str] = None) -> ModelSelection:
- """Build the loop's selection from one ``HopResolution`` (hop-resolution.schema.json).
-
- ``gateway_base_url`` is the Model Hub gateway prefix the hop's ``base_url`` was
- built from; a ``google`` hop needs it, because its ``base_url`` is the
- ``/v1beta`` Gemini surface and the agent speaks Chat to ``/v1``.
- """
+def selection_from_hop(hop: Mapping[str, Any]) -> ModelSelection:
+ """Build the loop's selection from one ``HopResolution`` (hop-resolution.schema.json)."""
if hop.get("backend") != "vibey":
raise HopResolutionError(f"hop resolved for backend {hop.get('backend')!r}, not vibey")
protocol = hop.get("protocol")
@@ -185,12 +179,6 @@ def selection_from_hop(hop: Mapping[str, Any], *, gateway_base_url: Optional[str
context_price_steps=tuple(sorted(steps)),
)
base_url = hop["base_url"]
- original_protocol = protocol
- if protocol == "google":
- if not gateway_base_url:
- raise HopResolutionError("a google hop needs the gateway prefix to call Chat Completions")
- protocol, base_url = "openai_chat", f"{gateway_base_url.rstrip('/')}/v1"
- origin_api = original_protocol if original_protocol != protocol else None
raw_cache_ttl = hop.get("cache_ttl")
if raw_cache_ttl is not None and (
not isinstance(raw_cache_ttl, str)
@@ -217,7 +205,6 @@ def selection_from_hop(hop: Mapping[str, Any], *, gateway_base_url: Optional[str
source_identity=hop["source_identity"],
hosted_tools=hosted_tools,
image_models=image_models,
- origin_api=origin_api,
)
return ModelSelection(endpoint=endpoint, capabilities=capabilities)
diff --git a/tests/agent_core/agent/test_context.py b/tests/agent_core/agent/test_context.py
index b73c136ab..e5f560926 100644
--- a/tests/agent_core/agent/test_context.py
+++ b/tests/agent_core/agent/test_context.py
@@ -464,41 +464,6 @@ def test_a_stored_request_size_the_transcript_cannot_explain_hides_at_most_its_e
assert _est(rows, grown(rows, 40_000)) == 12_000 + after + 40_000 - excess # over it: all but the excess
-def test_a_converted_route_anchor_uses_the_served_api_without_widening_identity():
- from modules.agents.vibey.models import selection_from_hop
-
- endpoint = selection_from_hop(
- {
- "backend": "vibey",
- "protocol": "google",
- "base_url": "http://hub.invalid/v1beta",
- "token": "token",
- "runtime_model": "test-model",
- "provider": "test-provider",
- "source_id": "src_google2501",
- "source_identity": "sha256:test-google",
- "request_headers": {},
- "capabilities": {},
- "hosted_tools": {},
- },
- gateway_base_url="http://hub.invalid/vibey",
- ).endpoint
- assert (endpoint.protocol, endpoint.origin_api) == ("openai_chat", "google")
- rows, _, result = _anchored_rows(
- endpoint=endpoint,
- origin=Origin("test-provider", "google", "test-model"),
- )
- request = make_request(context_view(rows.rows).messages, endpoint=endpoint)
- after = message_tokens(result.message) + message_tokens(request.messages[-1])
- assert _est(rows, request) == 5_000 + after
- assert _est(rows, request) > _whole(rows, request)
-
- # The same provider/model on an ordinary Chat route is not treated as the converted Google route.
- plain_endpoint = replace(endpoint, origin_api=None)
- plain_request = replace(request, endpoint=plain_endpoint)
- assert _est(rows, plain_request) == _whole(rows, plain_request)
-
-
def test_a_checkpoint_or_another_route_invalidates_the_anchor():
rows, anchored, result = _anchored_rows()
other_route = ModelEndpoint("anthropic", "http://model.invalid", "other-model", "", provider="test-provider")
diff --git a/tests/agent_core/ai/_wire.py b/tests/agent_core/ai/_wire.py
index 452e96a75..61de2bb8f 100644
--- a/tests/agent_core/ai/_wire.py
+++ b/tests/agent_core/ai/_wire.py
@@ -4,13 +4,14 @@ from __future__ import annotations
from typing import Optional
-from core.agent_core.ai import anthropic, openai_chat, openai_responses
+from core.agent_core.ai import anthropic, google, openai_chat, openai_responses
from core.agent_core.ai._common import WireRequest, endpoint_origin, prepare_messages
from core.agent_core.ai.provider import MediaLoader, ModelRequest, ProviderError
from core.agent_core.cancel import CancelToken
_WIRE_REQUESTS = {
"anthropic": anthropic.wire_request,
+ "google": google.wire_request,
"openai_chat": openai_chat.wire_request,
"openai_responses": openai_responses.wire_request,
}
diff --git a/tests/agent_core/ai/test_google_adapter.py b/tests/agent_core/ai/test_google_adapter.py
new file mode 100644
index 000000000..e26e085b9
--- /dev/null
+++ b/tests/agent_core/ai/test_google_adapter.py
@@ -0,0 +1,315 @@
+"""The Gemini adapter at its boundary: the request it sends, and the transcript a recorded stream becomes."""
+
+from __future__ import annotations
+
+import base64
+import json
+from typing import Any
+
+import httpx
+import pytest
+
+from core.agent_core.ai.google import DUMMY_THOUGHT_SIGNATURE, GoogleAdapter
+from core.agent_core.ai.registry import sent_effort
+from core.agent_core.ai.provider import Done, ModelEndpoint, ModelRequest, ProviderError
+from core.agent_core.cancel import CancelToken
+from core.agent_core.messages import (
+ AssistantMessage,
+ ImageBlock,
+ Origin,
+ TextBlock,
+ ThinkingBlock,
+ ToolCallBlock,
+ ToolResultMessage,
+ Usage,
+ UserMessage,
+)
+from core.agent_core.tools.base import ToolSpec
+from tests.agent_core.ai._wire import build_wire_request
+
+SIG_A = base64.b64encode(b"signature-a").decode()
+SIG_B = base64.b64encode(b"signature-b!").decode()
+MODEL = "gemini-4-argon"
+BASH = ToolSpec("bash", "Run a command", {"type": "object", "properties": {"command": {"type": "string"}}})
+GEMINI = Origin("google", "google", MODEL)
+
+
+def _request(
+ *messages: Any, model: str = MODEL, effort: str | None = None, base_url: str = "https://gemini.invalid/v1beta",
+ tools: tuple[ToolSpec, ...] = (BASH,), supports_images: bool = False,
+) -> ModelRequest:
+ return ModelRequest(
+ endpoint=ModelEndpoint("google", base_url, model, "key", provider="google"),
+ system="system",
+ messages=messages or (UserMessage((TextBlock(text="hello"),)),),
+ tools=tools,
+ max_tokens=256,
+ reasoning_effort=effort,
+ supports_images=supports_images,
+ )
+
+
+def _user(text: str) -> UserMessage:
+ return UserMessage((TextBlock(text=text),))
+
+
+def _calls(*calls: ToolCallBlock, origin: Origin = GEMINI) -> AssistantMessage:
+ return AssistantMessage(content=calls, origin=origin, stop_reason="tool_use")
+
+
+def _result(call_id: str, text: str, *, images: tuple[ImageBlock, ...] = ()) -> ToolResultMessage:
+ return ToolResultMessage(tool_call_id=call_id, tool_name="bash", content=(TextBlock(text=text), *images))
+
+
+async def _body(request: ModelRequest, *, gateway: bool = False) -> dict[str, Any]:
+ return (await build_wire_request(request, gateway=gateway)).body
+
+
+async def _stream(*chunks: dict[str, Any], request: ModelRequest | None = None) -> list[Any]:
+ seen: list[httpx.Request] = []
+
+ def handler(sent: httpx.Request) -> httpx.Response:
+ seen.append(sent)
+ body = "".join(f"data: {json.dumps(chunk)}\r\n\r\n" for chunk in chunks)
+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
+ events = [event async for event in GoogleAdapter(client).stream(request or _request(), CancelToken())]
+ _stream.sent = seen # type: ignore[attr-defined]
+ return events
+
+
+def _final(events: list[Any]) -> AssistantMessage:
+ terminal = events[-1]
+ assert isinstance(terminal, Done), terminal
+ return terminal.message
+
+
+def _parts(*parts: dict[str, Any], finish: str | None = None, usage: dict[str, int] | None = None) -> dict[str, Any]:
+ candidate: dict[str, Any] = {"content": {"role": "model", "parts": list(parts)}}
+ if finish:
+ candidate["finishReason"] = finish
+ chunk: dict[str, Any] = {"candidates": [candidate]}
+ if usage:
+ chunk["usageMetadata"] = usage
+ return chunk
+
+
+async def test_the_request_is_native_gemini_at_the_stream_endpoint() -> None:
+ request = _request(_user("hi"))
+ direct = await build_wire_request(request, gateway=False)
+ through_gateway = await build_wire_request(request, gateway=True)
+
+ assert direct.url == "https://gemini.invalid/v1beta/models/gemini-4-argon:streamGenerateContent?alt=sse"
+ # Google takes its key in its own header; the gateway takes the gateway token as a bearer.
+ assert direct.headers["x-goog-api-key"] == "key" and "Authorization" not in direct.headers
+ assert through_gateway.headers["Authorization"] == "Bearer key"
+ # The gateway refuses a Gemini body that names its model or stream mode: both live in the URL.
+ assert direct.body == {
+ "contents": [{"role": "user", "parts": [{"text": "hi"}]}],
+ "systemInstruction": {"parts": [{"text": "system"}]},
+ "tools": [{"functionDeclarations": [
+ {"name": "bash", "description": "Run a command", "parametersJsonSchema": dict(BASH.input_schema)},
+ ]}],
+ "generationConfig": {"maxOutputTokens": 256, "thinkingConfig": {"includeThoughts": True}},
+ }
+
+
+ ("model", "effort", "thinking"),
+ [
+ ("gemini-4-argon", "high", {"includeThoughts": True, "thinkingLevel": "high"}),
+ ("gemini-4-argon", "xhigh", {"includeThoughts": True, "thinkingLevel": "high"}),
+ # Pro-class models refuse MINIMAL (measured on gemini-4-argon); Google's own Chat layer sends LOW.
+ ("gemini-4-argon", "minimal", {"includeThoughts": True, "thinkingLevel": "low"}),
+ ("gemini-3-flash-preview", "minimal", {"includeThoughts": True, "thinkingLevel": "minimal"}),
+ ("gemini-flash-latest", "none", {"includeThoughts": True, "thinkingLevel": "minimal"}),
+ ("gemini-2.5-flash", "medium", {"includeThoughts": True, "thinkingBudget": 8192}),
+ ("gemini-2.5-flash", "none", {"includeThoughts": False, "thinkingBudget": 0}),
+ ("gemini-2.5-pro", None, {"includeThoughts": True}),
+ ("gemma-4-31b-it", "high", None),
+ ],
+)
+async def test_the_effort_becomes_the_models_thinking_control(model: str, effort: str | None, thinking: Any) -> None:
+ body = await _body(_request(model=model, effort=effort))
+ assert body["generationConfig"].get("thinkingConfig") == thinking
+ # Model Hub keys effort evidence by the token the registry names, and its gateway reads that same token back from
+ # the request, so a refusal governs every effort that sends it (`minimal` and `low` both send LOW to a Pro model).
+ from core.handlers.model_hub.evidence_rules import wire_effort
+
+ assert sent_effort("google", model, effort or "") == wire_effort(body, "google")
+
+
+async def test_signatures_go_back_on_the_parts_they_came_on_and_only_as_base64() -> None:
+ served = AssistantMessage(
+ content=(
+ ThinkingBlock(text="plan", signature=SIG_A),
+ TextBlock(text="checking"),
+ ToolCallBlock(id="call_1", name="bash", arguments={"command": "ls"}, signature=SIG_B),
+ ToolCallBlock(id="call_2", name="bash", arguments={"command": "pwd"}),
+ ),
+ origin=GEMINI,
+ stop_reason="tool_use",
+ )
+ over_chat = AssistantMessage(
+ # A response a google hop served over Chat before: its thinking marker is no Gemini signature.
+ content=(ThinkingBlock(text="old", signature="reasoning_content"), TextBlock(text="earlier"),
+ ThinkingBlock(text="", signature=SIG_A)),
+ origin=GEMINI,
+ stop_reason="stop",
+ )
+ body = await _body(_request(
+ _user("before"), over_chat, _user("now"), served, _result("call_1", "a"), _result("call_2", "b"),
+ ))
+
+ earlier, current, results = body["contents"][1], body["contents"][3], body["contents"][4]
+ # An answer's signature, kept beside its text, goes back on that text part.
+ assert earlier == {"role": "model", "parts": [
+ {"text": "old", "thought": True}, {"text": "earlier", "thoughtSignature": SIG_A},
+ ]}
+ assert current == {"role": "model", "parts": [
+ {"text": "plan", "thought": True, "thoughtSignature": SIG_A},
+ {"text": "checking"},
+ {"functionCall": {"name": "bash", "args": {"command": "ls"}, "id": "call_1"}, "thoughtSignature": SIG_B},
+ # A parallel call carries no signature of its own; Gemini signs only the first.
+ {"functionCall": {"name": "bash", "args": {"command": "pwd"}, "id": "call_2"}},
+ ]}
+ # One user content holds the step's results, in call order.
+ assert results == {"role": "user", "parts": [
+ {"functionResponse": {"name": "bash", "id": "call_1", "response": {"output": "a"}}},
+ {"functionResponse": {"name": "bash", "id": "call_2", "response": {"output": "b"}}},
+ ]}
+
+
+async def test_another_origins_call_in_the_current_turn_carries_googles_stand_in_signature() -> None:
+ claude = Origin("anthropic", "anthropic", "claude-x")
+ earlier = _calls(ToolCallBlock(id="toolu_1", name="bash", arguments={}), origin=claude)
+ current = _calls(
+ ToolCallBlock(id="toolu_2", name="bash", arguments={}), ToolCallBlock(id="toolu_3", name="bash", arguments={}),
+ origin=claude,
+ )
+ body = await _body(_request(
+ _user("first"), earlier, _result("toolu_1", "x"),
+ _user("second"), current, _result("toolu_2", "y"), _result("toolu_3", "z"),
+ ))
+
+ contents = body["contents"]
+ # Gemini validates the first call of each step of the current turn only, so only that one needs it.
+ assert "thoughtSignature" not in contents[1]["parts"][0]
+ first, second = contents[4]["parts"]
+ assert first["thoughtSignature"] == DUMMY_THOUGHT_SIGNATURE
+ assert "thoughtSignature" not in second
+
+
[email protected](("model", "nested"), [("gemini-4-argon", True), ("gemini-2.5-flash", False)])
+async def test_a_tool_results_image_rides_where_the_model_reads_it(model: str, nested: bool) -> None:
+ image = ImageBlock(mime_type="image/png", media_token="tok", name="shot.png")
+ loaded = {"tok": (b"\x89PNG", "image/png")}
+
+ class Loader:
+ async def load(self, media_token: str) -> tuple[bytes, str]:
+ return loaded[media_token]
+
+ calls = _calls(
+ ToolCallBlock(id="call_1", name="bash", arguments={}, signature=SIG_A),
+ ToolCallBlock(id="call_2", name="bash", arguments={}),
+ )
+ request = _request(
+ _user("look"), calls, _result("call_1", "", images=(image,)), _result("call_2", "two"),
+ model=model, supports_images=True,
+ )
+ body = (await build_wire_request(request, gateway=False, media_loader=Loader())).body
+
+ # The parallel batch's results stay together, whichever of them carries the image.
+ first, second = body["contents"][2]["parts"]
+ assert first["functionResponse"]["response"] == {"output": "(see attached image)"}
+ assert second["functionResponse"]["response"] == {"output": "two"}
+ inline = {"inlineData": {"mimeType": "image/png", "data": base64.b64encode(b"\x89PNG").decode()}}
+ if nested:
+ assert first["functionResponse"]["parts"] == [inline] and len(body["contents"]) == 3
+ else:
+ assert "parts" not in first["functionResponse"]
+ assert body["contents"][3:] == [{"role": "user", "parts": [{"text": "Tool result image:"}, inline]}]
+
+
+async def test_a_stream_becomes_thinking_text_and_signed_calls() -> None:
+ events = await _stream(
+ _parts({"text": "Let me ", "thought": True}),
+ _parts({"text": "check.", "thought": True, "thoughtSignature": SIG_A}),
+ _parts({"text": "Running it."}),
+ _parts(
+ {"functionCall": {"name": "bash", "args": {"command": "ls"}, "id": "fc_1"}, "thoughtSignature": SIG_B},
+ {"functionCall": {"name": "bash", "args": {"command": "pwd"}, "id": "fc_2"}},
+ finish="STOP",
+ usage={"promptTokenCount": 100, "cachedContentTokenCount": 40, "candidatesTokenCount": 20,
+ "thoughtsTokenCount": 7, "totalTokenCount": 127},
+ ),
+ )
+
+ message = _final(events)
+ assert message.stop_reason == "tool_use"
+ assert message.content == (
+ ThinkingBlock(text="Let me check.", signature=SIG_A),
+ TextBlock(text="Running it."),
+ ToolCallBlock(id="fc_1", name="bash", arguments={"command": "ls"}, native_id="fc_1", signature=SIG_B),
+ ToolCallBlock(id="fc_2", name="bash", arguments={"command": "pwd"}, native_id="fc_2"),
+ )
+ # The prompt count includes the cache; thoughts are counted apart from the candidates.
+ assert message.usage == Usage(input_tokens=60, output_tokens=27, cache_read_tokens=40, reasoning_tokens=7)
+
+
+ "answer",
+ [
+ # Gemini streams the signature on an empty part after the text...
+ ({"text": "all "}, {"text": "done"}, {"text": "", "thoughtSignature": SIG_A}),
+ # ...or on a delta of the text itself, which goes on.
+ ({"text": "all ", "thoughtSignature": SIG_A}, {"text": "done"}),
+ ],
+ ids=["empty-part", "on-text"],
+)
+async def test_an_answers_signature_goes_back_on_its_text(answer: tuple[dict[str, Any], ...]) -> None:
+ events = await _stream(*(_parts(part) for part in answer), _parts(finish="STOP"))
+ message = _final(events)
+ assert message.content == (TextBlock(text="all done"), ThinkingBlock(text="", signature=SIG_A))
+
+ body = await _body(_request(_user("go"), message, _user("again")))
+ assert body["contents"][1] == {"role": "model", "parts": [{"text": "all done", "thoughtSignature": SIG_A}]}
+
+
+async def test_a_signed_empty_part_after_a_call_goes_back_on_its_own() -> None:
+ events = await _stream(
+ _parts({"functionCall": {"name": "bash", "args": {}, "id": "fc_1"}, "thoughtSignature": SIG_B}),
+ _parts({"text": "", "thoughtSignature": SIG_A}, finish="STOP"),
+ )
+ body = await _body(_request(_user("go"), _final(events), _result("fc_1", "ok")))
+ assert body["contents"][1]["parts"][1] == {"text": "", "thoughtSignature": SIG_A}
+
+
+ ("finish", "stop"),
+ [("MAX_TOKENS", "length"), ("SAFETY", "safety"), ("RECITATION", "safety"), ("MALFORMED_FUNCTION_CALL", "error")],
+)
+async def test_a_finish_reason_maps_to_its_canonical_stop(finish: str, stop: str) -> None:
+ message = _final(await _stream(_parts({"text": "partial"}, finish=finish)))
+ assert message.stop_reason == stop
+ assert (message.error_message is None) is (stop == "length")
+
+
+async def test_a_blocked_prompt_is_a_safety_stop() -> None:
+ message = _final(await _stream({"promptFeedback": {"blockReason": "PROHIBITED_CONTENT"}}))
+ assert (message.stop_reason, message.content) == ("safety", ())
+ assert "PROHIBITED_CONTENT" in (message.error_message or "")
+
+
+async def test_a_stream_without_a_finish_reason_is_incomplete() -> None:
+ error = (await _stream(_parts({"text": "cut"})))[-1]
+ assert isinstance(error, ProviderError)
+ assert (error.kind, error.retryable) == ("network", False)
+ assert error.partial is not None and error.partial.content == (TextBlock(text="cut"),)
+
+
+async def test_an_error_frame_is_classified_from_its_google_status() -> None:
+ error = (await _stream({"error": {"code": 429, "message": "Resource exhausted", "status": "RESOURCE_EXHAUSTED"}}))[-1]
+ assert isinstance(error, ProviderError)
+ assert error.kind == "rate_limit" and error.retryable
diff --git a/tests/agent_core/ai/test_provider_adapters.py b/tests/agent_core/ai/test_provider_adapters.py
index 52b43b344..3d04c0839 100644
--- a/tests/agent_core/ai/test_provider_adapters.py
+++ b/tests/agent_core/ai/test_provider_adapters.py
@@ -32,6 +32,7 @@ from core.agent_core.ai.provider import (
ToolCallStart,
)
from core.agent_core.ai.registry import ADAPTERS, adapter_class, sent_effort
+from core.agent_core.messages import PROTOCOLS
from tests.agent_core.ai._wire import build_wire_request
from core.agent_core.cancel import CancelToken
from core.agent_core.messages import (
@@ -96,10 +97,10 @@ def test_responses_prompt_cache_retention_is_wired_only_when_model_hub_confirms_
assert payload.get("prompt_cache_options") == expected
-def test_registry_exposes_only_v1_protocols() -> None:
- assert set(ADAPTERS) == {"anthropic", "openai_chat", "openai_responses"}
- with pytest.raises(ValueError, match="unsupported provider protocol: google"):
- adapter_class("google")
+def test_registry_has_an_adapter_for_every_protocol() -> None:
+ assert set(ADAPTERS) == set(PROTOCOLS)
+ with pytest.raises(ValueError, match="unsupported provider protocol: bedrock"):
+ adapter_class("bedrock") # type: ignore[arg-type]
def test_each_known_wire_event_has_a_declarative_shape_row() -> None:
diff --git a/tests/test_model_hub_google.py b/tests/test_model_hub_google.py
index fe3be37a4..457488913 100644
--- a/tests/test_model_hub_google.py
+++ b/tests/test_model_hub_google.py
@@ -332,7 +332,9 @@ async def test_google_router_gateway_failover_preserves_visible_history_and_orig
{"text": "Visible answer"},
{"text": ""},
{"inlineData": {"mimeType": "image/png", "data": "synthetic-image-data"}},
- {"functionCall": {"name": "verify", "args": {"signature": "user-data"}}},
+ # Gemini 3+ refuses a current-turn call without a signature: Google's stand-in replaces the Source's.
+ {"functionCall": {"name": "verify", "args": {"signature": "user-data"}},
+ "thoughtSignature": "skip_thought_signature_validator"},
]},
{"role": "user", "parts": [{"functionResponse": {"name": "verify", "response": {"signature": "user-data"}}}]},
],
diff --git a/tests/test_model_hub_hosted_tools_resolution.py b/tests/test_model_hub_hosted_tools_resolution.py
index 27c1a37a6..e41f01514 100644
--- a/tests/test_model_hub_hosted_tools_resolution.py
+++ b/tests/test_model_hub_hosted_tools_resolution.py
@@ -172,11 +172,13 @@ def _evidence(tmp_path, *refused) -> HostedEvidence:
(OPENAI, "x_search", (), "unsupported", "unsupported", False),
(OPENAI, "x_search", ((OPENAI, "gpt-6-astra", "x_search", "call"),), "unsupported", "unsupported", False),
(CLAUDE_SUB, "web_search", (), "unsupported", "unsupported", False),
- (GEMINI, "web_search", (), "unsupported", "unsupported", False),
+ # Vibey speaks Gemini on a google hop (vibey-gemini-native.md), so the overlay's declaration runs there too;
+ # its native payload is not replayed until Gemini's hosted replay is (§4).
+ (GEMINI, "web_search", (), "assumed", "unsupported", False),
],
ids=[
"assumed", "replay-refused", "call-refused", "not-offered", "not-offered-refused",
- "claude-subscription-never", "translated-never",
+ "claude-subscription-never", "google-native",
],
)
def test_a_hops_facts_come_from_priors_and_refusals(tmp_path, primary, tool, refused, native, replay, fallback):
diff --git a/tests/test_model_hub_vibey_consumer.py b/tests/test_model_hub_vibey_consumer.py
index 1c25094ea..9b842319f 100644
--- a/tests/test_model_hub_vibey_consumer.py
+++ b/tests/test_model_hub_vibey_consumer.py
@@ -226,6 +226,34 @@ def _signed_history(protocol):
)
+def test_another_sources_gemini_history_keeps_its_calls_admissible() -> None:
+ """A Gemini history stripped for another Source keeps Google's stand-in where a call was signed: Gemini 3+
+ refuses a current-turn call without one, and this account's signature cannot go to another."""
+
+ from core.agent_core.ai.google import DUMMY_THOUGHT_SIGNATURE
+ from core.handlers.model_hub.request import without_opaque_history
+
+ first = {"functionCall": {"name": "verify", "args": {"thoughtSignature": "user-data"}, "id": "c1"}}
+ second = {"functionCall": {"name": "verify", "args": {}, "id": "c2"}}
+ result = {"role": "user", "parts": [{"functionResponse": {"name": "verify", "id": "c1", "response": {}}}]}
+ signed = {"contents": [
+ {"role": "model", "parts": [
+ {"text": "plan", "thought": True, "thoughtSignature": "opaque-thought"},
+ {"text": "Answer", "thoughtSignature": "opaque-text"},
+ {**first, "thoughtSignature": "opaque-call"},
+ second,
+ ]},
+ result,
+ ]}
+
+ assert without_opaque_history(signed) == {"contents": [
+ {"role": "model", "parts": [
+ {"text": "plan"}, {"text": "Answer"}, {**first, "thoughtSignature": DUMMY_THOUGHT_SIGNATURE}, second,
+ ]},
+ result,
+ ]}
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", PROTOCOL_ENDPOINTS)
@pytest.mark.parametrize("change", ["source", "provider", "api", "model"])
diff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.py
index 1638de014..50630c765 100644
--- a/tests/test_vibey_agent.py
+++ b/tests/test_vibey_agent.py
@@ -1185,7 +1185,7 @@ async def test_a_self_fork_keeps_the_live_turns_input_and_finished_steps(engine,
await running
-async def test_a_google_hop_is_called_over_chat_at_the_gateway_prefix(engine, session, tmp_path, published) -> None:
+async def test_a_google_hop_is_called_natively_at_the_gateway_gemini_surface(engine, session, tmp_path, published) -> None:
asked: list[str] = []
harness = _Harness(
engine, tmp_path, "avibe", [[Done(assistant("hi"))]],
@@ -1195,11 +1195,11 @@ async def test_a_google_hop_is_called_over_chat_at_the_gateway_prefix(engine, se
await harness.agent.handle_message(harness.request("hello"))
- # Model Hub's google hop is its /v1beta Gemini surface; the agent speaks Chat to /v1.
+ # Model Hub's google hop is its /v1beta Gemini surface, and the agent speaks Gemini to it.
[request] = harness.provider.requests
- assert asked == ["openai_chat"]
+ assert asked == ["google"]
assert (request.endpoint.protocol, request.endpoint.base_url, request.endpoint.provider) == (
- "openai_chat", "http://hub.invalid/vibey/v1", "test-provider"
+ "google", "http://hub.invalid/vibey/v1beta", "test-provider"
)
assert harness.controller.terminals[-1]["is_error"] is False
@@ -3329,6 +3329,11 @@ _TEXT_STREAMS = {
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n",
),
+ "google": (
+ "/vibey/v1beta/models/{model}:streamGenerateContent",
+ 'data: {"candidates":[{"content":{"role":"model","parts":[{"text":"hi"}]}}]}\n\n'
+ 'data: {"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP"}]}\n\n',
+ ),
}
@@ -3338,10 +3343,10 @@ async def test_a_turn_runs_through_the_real_provider_registry(engine, session, t
from modules.agents.vibey.models import registry_providers
- wire = "openai_chat" if hub_protocol == "google" else hub_protocol
- path, body = _TEXT_STREAMS[wire]
+ path, body = _TEXT_STREAMS[hub_protocol]
served = json.dumps(
- {"provider": "upstream", "api": wire, "model": "served-model", "source": "src_upstream01"}, separators=(",", ":")
+ {"provider": "upstream", "api": hub_protocol, "model": "served-model", "source": "src_upstream01"},
+ separators=(",", ":"),
)
seen: list[str] = []
@@ -3358,12 +3363,12 @@ async def test_a_turn_runs_through_the_real_provider_registry(engine, session, t
harness.controller.hub_protocol = hub_protocol
await harness.agent.handle_message(harness.request("hello"))
- # Each protocol's adapter from the real registry speaks to its gateway path; a google hop is
- # called over Chat at the gateway prefix; the served origin comes from x-avibe-served-hop.
- assert seen == [path]
+ # Each protocol's adapter from the real registry speaks to its own gateway path; the served origin comes from
+ # x-avibe-served-hop.
+ assert seen == [path.format(model=harness.controller.hub_calls[-1])]
final = (await harness.context_rows())[-1].message
assert final.content == (TextBlock(text="hi"),)
- assert (final.origin.provider, final.origin.api, final.origin.model) == ("upstream", wire, "served-model")
+ assert (final.origin.provider, final.origin.api, final.origin.model) == ("upstream", hub_protocol, "served-model")
assert harness.controller.im_client.sent == ["hi"]
diff --git a/tests/test_vibey_web_fetch.py b/tests/test_vibey_web_fetch.py
index d3c18b433..472978ba1 100644
--- a/tests/test_vibey_web_fetch.py
+++ b/tests/test_vibey_web_fetch.py
@@ -29,12 +29,13 @@ SCHEMA = {"type": "object", "properties": {"url": {"type": "string"}}, "required
def selection(protocol="anthropic", *, offered=True, token="turn-token"):
return selection_from_hop({
- "backend": "vibey", "protocol": protocol, "base_url": "http://hub.invalid/vibey/v1",
+ "backend": "vibey", "protocol": protocol,
+ "base_url": f"http://hub.invalid/vibey/{'v1beta' if protocol == 'google' else 'v1'}",
"token": token, "runtime_model": "model", "provider": "anthropic" if protocol == "anthropic" else "openai",
"source_id": "src_modeltest", "source_identity": "test-identity",
"request_headers": {"x-avibe-turn-id": "turn-fetch"}, "capabilities": {},
"hosted_tools": {"web_fetch": {"native": "unsupported", "replay": "unsupported"}} if offered else {},
- }, gateway_base_url="http://hub.invalid/vibey")
+ })
def router(first):
@@ -67,8 +68,10 @@ async def test_fetch_declaration_is_a_frozen_url_only_function(monkeypatch, prot
entry = declared[0]
if request.endpoint.protocol == "openai_chat":
entry = entry["function"]
+ elif request.endpoint.protocol == "google":
+ (entry,) = entry["functionDeclarations"]
assert entry["name"] == "web_fetch"
- assert entry.get("input_schema", entry.get("parameters")) == SCHEMA
+ assert entry.get("input_schema", entry.get("parameters", entry.get("parametersJsonSchema"))) == SCHEMA
assert entry.get("type") != "web_fetch_20260318"
assert "untrusted quoted source" in entry["description"]
assert "x-avibe-pin-source" not in wire.headers