Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix: keep Gemini Live voice sessions open between turns

Ruben Lütkef815fc2d03ba
diff --git a/jarvis/plugins/realtime/gemini_live.py b/jarvis/plugins/realtime/gemini_live.py
index 0e53ecfa12..eb68ee81ce 100644
--- a/jarvis/plugins/realtime/gemini_live.py
+++ b/jarvis/plugins/realtime/gemini_live.py
@@ -83,68 +83,93 @@ class _GeminiLiveSession:
)
async def receive(self) -> AsyncIterator[_ProviderEvent]:
- async for message in self._session.receive():
- # ``LiveServerMessage.data`` concatenates every inline audio part,
- # including Gemini 3.1 events that carry multiple parts at once.
- data = getattr(message, "data", None)
- if data:
- yield _ProviderEvent(
- type="audio_delta",
- audio=_PcmChunk(pcm=bytes(data), sample_rate=_OUTPUT_RATE),
- )
+ # ``google.genai.live.AsyncSession.receive()`` intentionally ends after
+ # one model turn. The Jarvis provider contract spans the whole call, so
+ # re-enter the SDK iterator after every clean turn boundary instead of
+ # making the desktop supervisor mistake a completed answer for a dead
+ # provider session.
+ while not self._closed:
+ turn_boundary_seen = False
+ async for message in self._session.receive():
+ # ``LiveServerMessage.data`` concatenates every inline audio part,
+ # including Gemini 3.1 events that carry multiple parts at once.
+ data = getattr(message, "data", None)
+ if data:
+ yield _ProviderEvent(
+ type="audio_delta",
+ audio=_PcmChunk(pcm=bytes(data), sample_rate=_OUTPUT_RATE),
+ )
- tool_call = getattr(message, "tool_call", None)
- function_calls = tuple(
- getattr(tool_call, "function_calls", None) or ()
- )
- for function_call in function_calls:
- raw_args = getattr(function_call, "args", None) or {}
- if hasattr(raw_args, "model_dump"):
- raw_args = raw_args.model_dump()
- try:
- args = dict(raw_args)
- except (TypeError, ValueError):
- args = {}
- yield _ProviderEvent(
- type="tool_call",
- call_id=str(getattr(function_call, "id", "") or ""),
- tool_name=str(getattr(function_call, "name", "") or ""),
- tool_args=args,
+ tool_call = getattr(message, "tool_call", None)
+ function_calls = tuple(
+ getattr(tool_call, "function_calls", None) or ()
)
-
- content = getattr(message, "server_content", None)
- if content is not None:
- output_transcription = getattr(content, "output_transcription", None)
- output_text = str(getattr(output_transcription, "text", "") or "")
- if output_text:
+ for function_call in function_calls:
+ raw_args = getattr(function_call, "args", None) or {}
+ if hasattr(raw_args, "model_dump"):
+ raw_args = raw_args.model_dump()
+ try:
+ args = dict(raw_args)
+ except (TypeError, ValueError):
+ args = {}
yield _ProviderEvent(
- type="output_transcript_delta", text=output_text
+ type="tool_call",
+ call_id=str(getattr(function_call, "id", "") or ""),
+ tool_name=str(getattr(function_call, "name", "") or ""),
+ tool_args=args,
)
- # The interrupted flag is the boundary between the partial
- # assistant reply above and any new user transcript carried by
- # the same server-content message. Emit it first so the shared
- # session closes the old turn before adopting the new words.
- if bool(getattr(content, "interrupted", False)):
- yield _ProviderEvent(type="interrupted")
-
- input_transcription = getattr(content, "input_transcription", None)
- input_text = str(getattr(input_transcription, "text", "") or "")
- if input_text:
+ content = getattr(message, "server_content", None)
+ if content is not None:
+ output_transcription = getattr(
+ content, "output_transcription", None
+ )
+ output_text = str(
+ getattr(output_transcription, "text", "") or ""
+ )
+ if output_text:
+ yield _ProviderEvent(
+ type="output_transcript_delta", text=output_text
+ )
+
+ # The interrupted flag is the boundary between the partial
+ # assistant reply above and any new user transcript carried by
+ # the same server-content message. Emit it first so the shared
+ # session closes the old turn before adopting the new words.
+ if bool(getattr(content, "interrupted", False)):
+ yield _ProviderEvent(type="interrupted")
+
+ input_transcription = getattr(
+ content, "input_transcription", None
+ )
+ input_text = str(
+ getattr(input_transcription, "text", "") or ""
+ )
+ if input_text:
+ yield _ProviderEvent(
+ type="input_transcript", text=input_text, is_final=True
+ )
+
+ if bool(getattr(content, "turn_complete", False)):
+ turn_boundary_seen = True
+ if not function_calls:
+ yield _ProviderEvent(type="turn_complete")
+
+ go_away = getattr(message, "go_away", None)
+ if go_away is not None:
+ retry_ms = getattr(go_away, "time_left", None)
+ suffix = (
+ f" (time_left={retry_ms})" if retry_ms is not None else ""
+ )
yield _ProviderEvent(
- type="input_transcript", text=input_text, is_final=True
+ type="error", error=f"Gemini Live requested reconnect{suffix}"
)
- if bool(getattr(content, "turn_complete", False)) and not function_calls:
- yield _ProviderEvent(type="turn_complete")
-
- go_away = getattr(message, "go_away", None)
- if go_away is not None:
- retry_ms = getattr(go_away, "time_left", None)
- suffix = f" (time_left={retry_ms})" if retry_ms is not None else ""
- yield _ProviderEvent(
- type="error", error=f"Gemini Live requested reconnect{suffix}"
- )
+ # An iterator that vanishes without a model-turn boundary signals a
+ # closed/broken transport. Let the shared session observe that end;
+ # retrying it here would spin on an empty iterator forever.
+ if not turn_boundary_seen:
+ return
async def update_session(
self,
diff --git a/tests/unit/realtime/test_gemini_live.py b/tests/unit/realtime/test_gemini_live.py
index 509c9f8207..5e8b2055d2 100644
--- a/tests/unit/realtime/test_gemini_live.py
+++ b/tests/unit/realtime/test_gemini_live.py
@@ -40,9 +40,14 @@ async def test_receive_maps_audio_transcripts_interrupt_and_completion():
),
]
+ receive_calls = 0
+
async def fake_receive():
- for message in messages:
- yield message
+ nonlocal receive_calls
+ receive_calls += 1
+ if receive_calls == 1:
+ for message in messages:
+ yield message
fake_session = SimpleNamespace(receive=fake_receive)
session = _GeminiLiveSession(
@@ -65,6 +70,63 @@ async def test_receive_maps_audio_transcripts_interrupt_and_completion():
assert events[0].audio.sample_rate == 24_000
assert events[1].text == "hello there"
assert events[3].text == "what the user said"
+ assert receive_calls == 2
+
+
+async def test_receive_reenters_sdk_iterator_for_a_second_user_turn() -> None:
+ sdk_turns = [
+ [
+ _fake_message(
+ server_content=SimpleNamespace(
+ output_transcription=SimpleNamespace(text="first answer"),
+ input_transcription=None,
+ interrupted=False,
+ turn_complete=True,
+ )
+ )
+ ],
+ [
+ _fake_message(
+ server_content=SimpleNamespace(
+ output_transcription=SimpleNamespace(text="second answer"),
+ input_transcription=None,
+ interrupted=False,
+ turn_complete=True,
+ )
+ )
+ ],
+ ]
+ receive_calls = 0
+
+ async def fake_receive():
+ nonlocal receive_calls
+ receive_calls += 1
+ turn_index = receive_calls - 1
+ if turn_index < len(sdk_turns):
+ for message in sdk_turns[turn_index]:
+ yield message
+
+ session = _GeminiLiveSession(
+ session=SimpleNamespace(receive=fake_receive),
+ connection_cm=SimpleNamespace(),
+ client=SimpleNamespace(),
+ session_id="two-turns",
+ )
+
+ events = [event async for event in session.receive()]
+
+ assert [event.type for event in events] == [
+ "output_transcript_delta",
+ "turn_complete",
+ "output_transcript_delta",
+ "turn_complete",
+ ]
+ assert [event.text for event in events if event.text] == [
+ "first answer",
+ "second answer",
+ ]
+ assert receive_calls == 3
@pytest.mark.asyncio
@@ -272,33 +334,42 @@ async def test_tools_are_declared_mapped_and_answered(
@pytest.mark.asyncio
async def test_tool_call_suppresses_intermediate_turn_complete() -> None:
- messages = [
- _fake_message(
- tool_call=SimpleNamespace(
- function_calls=[
- SimpleNamespace(id="call-1", name="open_app", args={})
- ]
- ),
- server_content=SimpleNamespace(
- output_transcription=None,
- input_transcription=None,
- interrupted=False,
- turn_complete=True,
- ),
- ),
- _fake_message(
- server_content=SimpleNamespace(
- output_transcription=None,
- input_transcription=None,
- interrupted=False,
- turn_complete=True,
+ sdk_turns = [
+ [
+ _fake_message(
+ tool_call=SimpleNamespace(
+ function_calls=[
+ SimpleNamespace(id="call-1", name="open_app", args={})
+ ]
+ ),
+ server_content=SimpleNamespace(
+ output_transcription=None,
+ input_transcription=None,
+ interrupted=False,
+ turn_complete=True,
+ ),
)
- ),
+ ],
+ [
+ _fake_message(
+ server_content=SimpleNamespace(
+ output_transcription=None,
+ input_transcription=None,
+ interrupted=False,
+ turn_complete=True,
+ )
+ )
+ ],
]
+ receive_calls = 0
async def fake_receive():
- for message in messages:
- yield message
+ nonlocal receive_calls
+ receive_calls += 1
+ turn_index = receive_calls - 1
+ if turn_index < len(sdk_turns):
+ for message in sdk_turns[turn_index]:
+ yield message
session = _GeminiLiveSession(
session=SimpleNamespace(receive=fake_receive),
@@ -310,6 +381,7 @@ async def test_tool_call_suppresses_intermediate_turn_complete() -> None:
events = [event async for event in session.receive()]
assert [event.type for event in events] == ["tool_call", "turn_complete"]
+ assert receive_calls == 3
# --- function_declarations schema sanitizing --------------------------------