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.

fix(agent-core): replay Gemini thought signatures on Chat tool calls (#3025)

Call Whiteb3408b23c666
diff --git a/core/agent_core/ai/openai_chat.py b/core/agent_core/ai/openai_chat.py
index bf064e5e2..7660e515f 100644
--- a/core/agent_core/ai/openai_chat.py
+++ b/core/agent_core/ai/openai_chat.py
@@ -514,6 +514,7 @@ class OpenAIChatAdapter(ProviderAdapter):
if terminal is not None:
yield terminal
return
+ signature = _tool_call_signature(raw)
function = raw.get("function")
if function is not None and not isinstance(function, Mapping):
terminal = assembler.terminal(
@@ -542,7 +543,9 @@ class OpenAIChatAdapter(ProviderAdapter):
yield terminal
return
name = _string(raw_name)
- start = assembler.tool_start(key, name=name or None, native_id=raw_id or None)
+ start = assembler.tool_start(
+ key, name=name or None, native_id=raw_id or None, signature=signature
+ )
if start is not None:
yield start
pending = assembler.pending_tool_arguments(key)
@@ -563,8 +566,8 @@ class OpenAIChatAdapter(ProviderAdapter):
emitted = assembler.tool_arguments(key, arguments)
if emitted is not None:
yield emitted
- elif raw_id:
- assembler.tool_start(key, native_id=raw_id)
+ elif raw_id or signature:
+ assembler.tool_start(key, native_id=raw_id or None, signature=signature)
legacy_function = delta.get("function_call")
if legacy_function is not None:
if not isinstance(legacy_function, Mapping):
@@ -766,14 +769,7 @@ def _message_to_chat(
output["reasoning_content"] = block.text
calls = [block for block in message.content if isinstance(block, ToolCallBlock)]
if calls:
- output["tool_calls"] = [
- {
- "id": call.id,
- "type": "function",
- "function": {"name": call.name, "arguments": json.dumps(dict(call.arguments), ensure_ascii=False)},
- }
- for call in calls
- ]
+ output["tool_calls"] = [_tool_call_to_chat(call) for call in calls]
if output["content"] is None and "tool_calls" not in output:
output["content"] = ""
return output
@@ -878,6 +874,30 @@ def _first_reasoning_delta(delta: Mapping[str, Any]) -> tuple[str | None, str]:
return None, ""
+def _tool_call_to_chat(call: ToolCallBlock) -> dict[str, Any]:
+ output: dict[str, Any] = {
+ "id": call.id,
+ "type": "function",
+ "function": {"name": call.name, "arguments": json.dumps(dict(call.arguments), ensure_ascii=False)},
+ }
+ if call.signature is not None:
+ # Gemini rejects a replayed function call that lost the thought signature it was served with.
+ output["extra_content"] = {"google": {"thought_signature": call.signature}}
+ return output
+
+
+def _tool_call_signature(raw: Mapping[str, Any]) -> str | None:
+ """Gemini's thought signature for one Chat tool call (``extra_content.google.thought_signature``).
+
+ A vendor extension, so a missing or malformed value is ignored rather than failing the response.
+ """
+
+ extra_content = raw.get("extra_content")
+ google = extra_content.get("google") if isinstance(extra_content, Mapping) else None
+ signature = google.get("thought_signature") if isinstance(google, Mapping) else None
+ return signature if isinstance(signature, str) and signature else None
+
+
def _signature_details(signature: str) -> Any:
try:
value = json.loads(signature)
diff --git a/tests/agent_core/ai/test_provider_adapters.py b/tests/agent_core/ai/test_provider_adapters.py
index 28fa25077..52b43b344 100644
--- a/tests/agent_core/ai/test_provider_adapters.py
+++ b/tests/agent_core/ai/test_provider_adapters.py
@@ -2279,6 +2279,57 @@ async def test_chat_completions_reassembles_parallel_calls() -> None:
assert [call.arguments for call in final.message.tool_calls] == [{"path": "a"}, {"path": "b"}]
+ "tool_deltas",
+ [
+ pytest.param(
+ [{"index": 0, "id": "call_a", "function": {"name": "read", "arguments": "{}"},
+ "extra_content": {"google": {"thought_signature": "sig-a"}}}],
+ id="with-call",
+ ),
+ pytest.param(
+ [{"index": 0, "id": "call_a", "function": {"name": "read", "arguments": "{}"}},
+ {"index": 0, "extra_content": {"google": {"thought_signature": "sig-a"}}}],
+ id="own-delta",
+ ),
+ ],
+)
+async def test_chat_replays_gemini_tool_call_thought_signature(tool_deltas: list[dict[str, Any]]) -> None:
+ """Gemini answers the request after a tool call with 400 unless that call's thought signature comes back."""
+
+ frames = [{"choices": [{"delta": {"tool_calls": [delta]}}]} for delta in tool_deltas]
+ frames.append({"choices": [{"delta": {}, "finish_reason": "tool_calls"}]})
+ served = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames) + "data: [DONE]\n\n"
+ answered = 'data: {"choices":[{"delta":{"content":"done"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
+ bodies: list[dict[str, Any]] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ bodies.append(json.loads(request.content))
+ text = served if len(bodies) == 1 else answered
+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=text)
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
+ adapter = OpenAIChatAdapter(client)
+ first = await _events(adapter, _request("openai_chat"))
+ assert isinstance(first[-1], Done)
+ history = (
+ UserMessage((TextBlock(text="hello"),)),
+ first[-1].message,
+ ToolResultMessage("call_a", "read", (TextBlock(text="ok"),)),
+ )
+ second = await _events(adapter, _request("openai_chat", messages=history))
+
+ assert isinstance(second[-1], Done)
+ assistant = next(message for message in bodies[1]["messages"] if message["role"] == "assistant")
+ assert assistant["tool_calls"] == [{
+ "id": "call_a",
+ "type": "function",
+ "function": {"name": "read", "arguments": "{}"},
+ "extra_content": {"google": {"thought_signature": "sig-a"}},
+ }]
+
+
@pytest.mark.asyncio
async def test_chat_reuses_indexless_tool_slot_for_argument_only_delta() -> None:
body = (