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.

feat(realtime): RealtimeVoiceSession duck interface for /ws/audio

Ruben Lütkea8c52d7efe72
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
new file mode 100644
index 0000000000..fedf4bdb4b
--- /dev/null
+++ b/jarvis/realtime/session.py
@@ -0,0 +1,175 @@
+# jarvis/realtime/session.py
+"""RealtimeVoiceSession — the duplex session that slots into /ws/audio.
+
+Implements the same duck interface (handle_audio_frame/handle_control/end) as
+BrowserVoiceSession, so the existing route branches to it with no receive-loop
+change. Server-VAD owns turn boundaries (no local endpointer). Model audio is
+held by ScrubHoldGate until its transcript is scrub-cleared (AP-11).
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+from typing import Any
+
+from jarvis.core.protocols import AudioChunk
+from jarvis.core.turn_language import resolve_output_language
+from jarvis.realtime.protocol import RealtimeSessionConfig
+from jarvis.realtime.scrub_gate import ScrubHoldGate
+from jarvis.telephony.audio import STT_SAMPLE_RATE, Resampler
+
+log = logging.getLogger(__name__)
+
+_INSTRUCTIONS = (
+ "You are Jarvis, a concise spoken assistant. Never read tool JSON, code, "
+ "stack traces, file paths, base64, or raw URLs aloud — speak only a natural "
+ "summary. Reply in the user's language."
+)
+
+
+class RealtimeVoiceSession:
+ def __init__(
+ self,
+ *,
+ session_id: str,
+ send_binary: Any,
+ send_json: Any,
+ provider: Any,
+ config: Any,
+ bus: Any = None,
+ browser_sample_rate: int = 48000,
+ ) -> None:
+ self.session_id = session_id
+ self._send_binary = send_binary
+ self._send_json = send_json
+ self._provider = provider
+ self._config = config
+ self._bus = bus
+ self.browser_sample_rate = int(browser_sample_rate or STT_SAMPLE_RATE)
+ self._in_resampler = Resampler(self.browser_sample_rate, STT_SAMPLE_RATE)
+
+ self._language = self._resolve_lang(text="")
+ self._gate = ScrubHoldGate(self._language)
+ self._session: Any = None
+ self._pump_task: asyncio.Task[None] | None = None
+ self._ms_sent = 0
+ self._ended = False
+
+ def _resolve_lang(self, *, text: str) -> str:
+ brain = getattr(self._config, "brain", None)
+ pin = getattr(brain, "reply_language", "auto")
+ return resolve_output_language(pin, "unknown", text)
+
+ async def handle_control(self, msg: dict[str, Any]) -> None:
+ kind = str(msg.get("type", ""))
+ if kind == "audio_start":
+ rate = int(msg.get("sample_rate", self.browser_sample_rate) or self.browser_sample_rate)
+ if rate != self.browser_sample_rate:
+ self.browser_sample_rate = rate
+ self._in_resampler = Resampler(rate, STT_SAMPLE_RATE)
+ if self._session is None:
+ await self._open()
+ await self._send_json({"type": "audio_ready"})
+ elif kind == "barge_in":
+ await self._barge_in()
+ elif kind == "audio_stop":
+ await self.end(reason="client_stop")
+
+ async def _open(self) -> None:
+ cfg = RealtimeSessionConfig(
+ instructions=_INSTRUCTIONS,
+ language=self._language,
+ voice=getattr(getattr(self._config, "voice", None), "realtime_voice", "") or "",
+ )
+ self._session = await self._provider.open_session(cfg)
+ self._pump_task = asyncio.create_task(self._pump(), name=f"rt-pump-{self.session_id}")
+
+ async def handle_audio_frame(self, pcm_native: bytes) -> None:
+ if self._ended or self._session is None or not pcm_native:
+ return
+ try:
+ pcm16 = bytes(pcm_native) if self.browser_sample_rate == STT_SAMPLE_RATE else self._in_resampler.process(bytes(pcm_native))
+ except Exception: # noqa: BLE001 — malformed frame, drop it
+ return
+ await self._session.send_audio(AudioChunk(pcm=pcm16, sample_rate=STT_SAMPLE_RATE, timestamp_ns=0))
+
+ async def _pump(self) -> None:
+ from jarvis.telemetry.latency import LatencyPhase, mark_phase
+
+ try:
+ async for ev in self._session.receive():
+ if ev.type == "input_transcript" and ev.text:
+ new_lang = self._resolve_lang(text=ev.text)
+ if new_lang != self._language:
+ self._language = new_lang
+ self._gate = ScrubHoldGate(new_lang)
+ await self._session.update_session(instructions=_INSTRUCTIONS, language=new_lang)
+ mark_phase(LatencyPhase.REALTIME_INPUT_COMMITTED)
+ elif ev.type == "output_transcript_delta" and ev.text:
+ mark_phase(LatencyPhase.REALTIME_FIRST_TRANSCRIPT)
+ display = await self._gate.feed_transcript(ev.text)
+ if self._gate.hard_leak_pending():
+ await self._session.interrupt()
+ await self._send_json({"type": "error_spoken", "text": self._gate.fallback_phrase()})
+ self._gate.drain()
+ continue
+ await self._send_json({"type": "transcript", "text": display, "is_final": False})
+ # Flush audio that this now-scrub-cleared transcript covers, so a
+ # LATER segment's first audio chunk is never released before its
+ # own transcript is scrubbed (closes the T4 one-chunk-boundary
+ # residual: push_audio's "cleared" branch bundles the
+ # release-triggering chunk, so without this flush a later
+ # audio_delta could ride the same "cleared" release).
+ for chunk in self._gate.release_available():
+ await self._emit_audio(chunk)
+ elif ev.type == "audio_delta" and ev.audio is not None:
+ mark_phase(LatencyPhase.REALTIME_FIRST_AUDIO)
+ for chunk in await self._gate.push_audio(ev.audio):
+ await self._emit_audio(chunk)
+ elif ev.type == "speech_started":
+ await self._barge_in()
+ elif ev.type == "turn_complete":
+ for chunk in self._gate.release_available():
+ await self._emit_audio(chunk)
+ self._gate.drain()
+ elif ev.type == "error":
+ log.warning("realtime[%s] provider error: %s", self.session_id, ev.error)
+ except Exception: # noqa: BLE001 — AP-20: any pump error is terminal
+ log.warning("realtime[%s] pump ended", self.session_id, exc_info=True)
+
+ async def _emit_audio(self, chunk: AudioChunk) -> None:
+ if self._ms_sent == 0 and self._bus is not None:
+ from jarvis.core.events import AudioOutFirst
+
+ try:
+ await self._bus.publish(AudioOutFirst())
+ except Exception: # noqa: BLE001
+ pass
+ self._ms_sent += len(chunk.pcm)
+ await self._send_binary(chunk.pcm)
+
+ async def _barge_in(self) -> None:
+ self._gate.drain()
+ try:
+ await self._session.truncate(audio_end_ms=self._ms_sent // (24000 * 2 // 1000) if self._ms_sent else 0)
+ except Exception: # noqa: BLE001
+ pass
+ self._ms_sent = 0
+ try:
+ await self._send_json({"type": "tts_cancel"})
+ except Exception: # noqa: BLE001, S110
+ pass
+
+ async def end(self, *, reason: str = "") -> None:
+ if self._ended:
+ return
+ self._ended = True
+ if self._pump_task is not None and not self._pump_task.done():
+ self._pump_task.cancel()
+ if self._session is not None:
+ try:
+ await self._session.close()
+ except Exception: # noqa: BLE001
+ pass
+ log.info("realtime[%s] ended: reason=%s", self.session_id, reason)
diff --git a/tests/unit/realtime/test_session.py b/tests/unit/realtime/test_session.py
new file mode 100644
index 0000000000..6b4b360edb
--- /dev/null
+++ b/tests/unit/realtime/test_session.py
@@ -0,0 +1,142 @@
+import asyncio
+
+import pytest
+
+from jarvis.core.protocols import AudioChunk
+from jarvis.realtime.protocol import RealtimeEvent, RealtimeSessionConfig
+from jarvis.realtime.session import RealtimeVoiceSession
+
+
+class FakeSession:
+ session_id = "fake"
+
+ def __init__(self, events):
+ self._events = events
+ self.sent_audio = []
+ self.truncated = []
+ self.closed = False
+
+ async def send_audio(self, chunk):
+ self.sent_audio.append(chunk)
+
+ async def receive(self):
+ for ev in self._events:
+ yield ev
+ await asyncio.sleep(0)
+
+ async def update_session(self, *, instructions=None, language=None):
+ pass
+
+ async def truncate(self, audio_end_ms):
+ self.truncated.append(audio_end_ms)
+
+ async def interrupt(self):
+ pass
+
+ async def close(self):
+ self.closed = True
+
+
+class FakeProvider:
+ name = "fake"
+ supports_realtime = True
+ input_sample_rate = 16000
+ output_sample_rate = 24000
+
+ def __init__(self, events):
+ self._events = events
+
+ async def can_open_duplex_session(self):
+ return True
+
+ async def open_session(self, cfg):
+ self.session = FakeSession(self._events)
+ return self.session
+
+
+def _cfg():
+ from types import SimpleNamespace
+
+ return SimpleNamespace(brain=SimpleNamespace(reply_language="en"), voice=SimpleNamespace(mode="realtime"))
+
+
+async def test_clean_turn_streams_audio_and_transcript():
+ events = [
+ RealtimeEvent(type="output_transcript_delta", text="Hello there."),
+ RealtimeEvent(type="audio_delta", audio=AudioChunk(pcm=b"\x01\x02" * 8, sample_rate=24000, timestamp_ns=0)),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ binaries, jsons = [], []
+ sess = RealtimeVoiceSession(
+ session_id="s1",
+ send_binary=lambda b: binaries.append(b) or asyncio.sleep(0),
+ send_json=lambda m: jsons.append(m) or asyncio.sleep(0),
+ provider=FakeProvider(events),
+ config=_cfg(),
+ bus=None,
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.05) # let the receive pump drain the fake events
+ await sess.end(reason="test")
+ assert any(m.get("type") == "transcript" for m in jsons)
+ assert binaries # audio was released after the clean transcript
+
+
+async def test_hard_leak_transcript_drops_audio():
+ events = [
+ RealtimeEvent(type="audio_delta", audio=AudioChunk(pcm=b"\x01\x02" * 8, sample_rate=24000, timestamp_ns=0)),
+ RealtimeEvent(type="output_transcript_delta", text="Traceback (most recent call last):\n File a\nValueError: b\n\n"),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ binaries, jsons = [], []
+ sess = RealtimeVoiceSession(
+ session_id="s2",
+ send_binary=lambda b: binaries.append(b) or asyncio.sleep(0),
+ send_json=lambda m: jsons.append(m) or asyncio.sleep(0),
+ provider=FakeProvider(events),
+ config=_cfg(),
+ bus=None,
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.05)
+ await sess.end(reason="test")
+ # The pre-leak audio was buffered, then dropped when the leak transcript arrived.
+ assert binaries == []
+
+
+async def test_later_segment_leak_audio_not_emitted():
+ # Regression test for the T4 ScrubHoldGate one-chunk-boundary residual:
+ # push_audio's "cleared" branch bundles the release-triggering chunk with
+ # the previously-buffered one, so a LATER segment's first audio chunk
+ # could ride along before its own transcript is scrubbed. The session
+ # must flush release_available() right after sending a clean transcript
+ # so the gate's _cleared flag never spans into the next segment's audio.
+ a1 = b"\x11\x22" * 8
+ a2 = b"\x33\x44" * 8
+ events = [
+ RealtimeEvent(type="audio_delta", audio=AudioChunk(pcm=a1, sample_rate=24000, timestamp_ns=0)),
+ RealtimeEvent(type="output_transcript_delta", text="A clean first sentence."),
+ RealtimeEvent(type="audio_delta", audio=AudioChunk(pcm=a2, sample_rate=24000, timestamp_ns=0)),
+ RealtimeEvent(
+ type="output_transcript_delta",
+ text="Traceback (most recent call last):\n File x\nValueError: y\n\n",
+ ),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ binaries, jsons = [], []
+ sess = RealtimeVoiceSession(
+ session_id="s3",
+ send_binary=lambda b: binaries.append(b) or asyncio.sleep(0),
+ send_json=lambda m: jsons.append(m) or asyncio.sleep(0),
+ provider=FakeProvider(events),
+ config=_cfg(),
+ bus=None,
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.05)
+ await sess.end(reason="test")
+ assert a1 in binaries
+ assert a2 not in binaries