Personal AI, On Personal Devices
Python
fix(cloud): handle parallel Gemini tool calls
Elliot Sluskyf08ad574d30a
diff --git a/src/openjarvis/engine/cloud.py b/src/openjarvis/engine/cloud.pyindex fe5172f6..e4485772 100644--- a/src/openjarvis/engine/cloud.py+++ b/src/openjarvis/engine/cloud.py@@ -1350,14 +1350,16 @@ class CloudEngine(InferenceEngine):args = json.loads(args)except (json.JSONDecodeError, TypeError):args = {"input": args}- function_call: Dict[str, Any] = {- "name": tool_call.name,- "args": args if isinstance(args, dict) else {},+ function_call_part: Dict[str, Any] = {+ "function_call": {+ "name": tool_call.name,+ "args": args if isinstance(args, dict) else {},+ }}signature = self._thought_sigs.get(tool_call.id)if signature is not None:- function_call["thought_signature"] = signature- parts.append({"function_call": function_call})+ function_call_part["thought_signature"] = signature+ parts.append(function_call_part)contents.append({"role": "model", "parts": parts})elif message.role.value == "assistant":contents.append({"role": "model", "parts": [{"text": message.content}]})@@ -1377,7 +1379,7 @@ class CloudEngine(InferenceEngine):if tools:config.tools = [{"function_declarations": _convert_tools_to_google(tools)}]- tool_ids: Dict[str, int] = {}+ tool_call_count = 0for chunk in self._google_client.models.generate_content_stream(model=model,contents=contents,@@ -1402,8 +1404,12 @@ class CloudEngine(InferenceEngine):name = getattr(function_call, "name", "")raw_args = getattr(function_call, "args", {})args = dict(raw_args) if hasattr(raw_args, "items") else {}- tool_id = f"google_{name}"- tool_index = tool_ids.setdefault(tool_id, len(tool_ids))+ # Gemini emits complete function-call parts, so each part is+ # a distinct invocation. The same function may legitimately+ # be called more than once in a parallel response.+ tool_index = tool_call_count+ tool_id = f"google_{name}_{tool_index}"+ tool_call_count += 1tool_call = {"index": tool_index,"id": tool_id,@@ -1430,7 +1436,7 @@ class CloudEngine(InferenceEngine):if text:yield StreamChunk(content=text)- yield StreamChunk(finish_reason="tool_calls" if tool_ids else "stop")+ yield StreamChunk(finish_reason="tool_calls" if tool_call_count else "stop")async def _stream_openrouter(self,diff --git a/tests/engine/test_cloud_stream_full.py b/tests/engine/test_cloud_stream_full.pyindex 8adf235e..a57373a0 100644--- a/tests/engine/test_cloud_stream_full.py+++ b/tests/engine/test_cloud_stream_full.py@@ -506,12 +506,12 @@ async def test_stream_full_google_preserves_tool_calls(monkeypatch: pytest.Monketool_call = result[0].tool_calls[0]assert tool_call == {"index": 0,- "id": "google_get_weather",+ "id": "google_get_weather_0","type": "function","function": {"name": "get_weather", "arguments": '{"city": "Berlin"}'},"thought_signature": b"sig",}- assert engine._thought_sigs["google_get_weather"] == b"sig"+ assert engine._thought_sigs["google_get_weather_0"] == b"sig"assert result[-1].finish_reason == "tool_calls"config = client.models.generate_content_stream.call_args.kwargs["config"]assert config.tools == [@@ -545,7 +545,7 @@ async def test_stream_full_google_preserves_mixed_and_multiple_calls(client.models.generate_content_stream.return_value = iter([_google_stream_chunk(text_part, weather_part),- _google_stream_chunk(weather_part, calendar_part),+ _google_stream_chunk(calendar_part),])engine = _make_cloud_engine(google_client=client)@@ -566,7 +566,7 @@ async def test_stream_full_google_preserves_mixed_and_multiple_calls(assert result[1].tool_calls == [{"index": 0,- "id": "google_get_weather",+ "id": "google_get_weather_0","type": "function","function": {"name": "get_weather",@@ -575,18 +575,9 @@ async def test_stream_full_google_preserves_mixed_and_multiple_calls(}]assert result[2].tool_calls == [- {- "index": 0,- "id": "google_get_weather",- "type": "function",- "function": {- "name": "get_weather",- "arguments": '{"city": "Berlin"}',- },- },{"index": 1,- "id": "google_get_calendar",+ "id": "google_get_calendar_1","type": "function","function": {"name": "get_calendar",@@ -597,6 +588,94 @@ async def test_stream_full_google_preserves_mixed_and_multiple_calls(assert result[-1].finish_reason == "tool_calls"+async def test_stream_full_google_keeps_parallel_same_name_calls_distinct(+ monkeypatch: pytest.MonkeyPatch,+):+ """Parallel invocations of one function receive unique indexes and IDs."""+ paris = SimpleNamespace(name="get_weather", args={"city": "Paris"})+ london = SimpleNamespace(name="get_weather", args={"city": "London"})+ parts = [+ SimpleNamespace(function_call=paris, text=None, thought_signature=b"sig"),+ SimpleNamespace(function_call=london, text=None, thought_signature=None),+ ]+ client = MagicMock()+ client.models.generate_content_stream.return_value = iter(+ [_google_stream_chunk(*parts)]+ )+ engine = _make_cloud_engine(google_client=client)+ engine._thought_sigs = {}++ with monkeypatch.context() as patch:+ for name, module in _google_types_modules().items():+ patch.setitem(sys.modules, name, module)+ result = [+ chunk+ async for chunk in engine.stream_full(+ [Message(role=Role.USER, content="Weather in Paris and London")],+ model="gemini-3-flash-preview",+ )+ ]++ calls = result[0].tool_calls+ assert [(call["index"], call["id"]) for call in calls] == [+ (0, "google_get_weather_0"),+ (1, "google_get_weather_1"),+ ]+ assert [call["function"]["arguments"] for call in calls] == [+ '{"city": "Paris"}',+ '{"city": "London"}',+ ]+++async def test_stream_full_google_replays_signature_on_part(+ monkeypatch: pytest.MonkeyPatch,+):+ """A saved Gemini signature is replayed beside, not inside, function_call."""+ client = MagicMock()+ client.models.generate_content_stream.return_value = iter([])+ engine = _make_cloud_engine(google_client=client)+ engine._thought_sigs = {"google_get_weather_0": b"sig"}+ messages = [+ Message(role=Role.USER, content="weather"),+ Message(+ role=Role.ASSISTANT,+ content=None,+ tool_calls=[+ ToolCall(+ id="google_get_weather_0",+ name="get_weather",+ arguments='{"city": "Berlin"}',+ )+ ],+ ),+ Message(role=Role.TOOL, name="get_weather", content='{"temp": 20}'),+ ]++ with monkeypatch.context() as patch:+ for name, module in _google_types_modules().items():+ patch.setitem(sys.modules, name, module)+ result = [+ chunk+ async for chunk in engine.stream_full(+ messages, model="gemini-3-flash-preview"+ )+ ]++ contents = client.models.generate_content_stream.call_args.kwargs["contents"]+ assert contents[1]["parts"] == [+ {+ "function_call": {+ "name": "get_weather",+ "args": {"city": "Berlin"},+ },+ "thought_signature": b"sig",+ }+ ]+ assert result[-1].finish_reason == "stop"++# ---------------------------------------------------------------------------# stream_full routing tests# ---------------------------------------------------------------------------