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): factory + browser route branch, default-OFF connect gate

Ruben Lütkee8e9a54c2484
diff --git a/jarvis/browser_voice/route.py b/jarvis/browser_voice/route.py
index 2778327123..888888ab2b 100644
--- a/jarvis/browser_voice/route.py
+++ b/jarvis/browser_voice/route.py
@@ -7,6 +7,7 @@ provider resolution (shared STT/TTS + a per-connection brain, with a test-factor
seam) and the ``/ws`` AP-20 receive-loop discipline: a ``RuntimeError`` on an
unclean disconnect is terminal — ``break``, never ``continue``.
"""
+
from __future__ import annotations
import json
@@ -27,11 +28,14 @@ _LANG_MAP = {"de": "de-DE", "en": "en-US", "es": "es-ES"}
def _browser_voice_enabled(cfg: Any) -> bool:
- """Default ON: a VPS browser user gets voice unless [browser_voice] disables it."""
- bv = getattr(cfg, "browser_voice", None)
- if bv is None:
+ """Default OFF. The socket is only served when the user has explicitly
+ enabled a browser voice surface: realtime mode ([voice].mode == "realtime")
+ or the classic browser bridge ([browser_voice].enabled == true).
+ """
+ if getattr(getattr(cfg, "voice", None), "mode", "pipeline") == "realtime":
return True
- return bool(getattr(bv, "enabled", True))
+ bv = getattr(cfg, "browser_voice", None)
+ return bool(getattr(bv, "enabled", False)) if bv is not None else False
def _resolve_language(cfg: Any) -> str:
@@ -50,6 +54,16 @@ def _build_browser_session(
key) — the caller then closes the socket cleanly. A test can inject
``state.browser_voice_session_factory`` to bypass the real provider build.
"""
+ # Realtime mode branch (default OFF): only when [voice].mode == "realtime"
+ # AND an OpenAI key exists. Otherwise fall through to the classic bridge.
+ from jarvis.realtime.factory import build_realtime_session
+
+ rt = build_realtime_session(
+ cfg=cfg, bus=bus, session_id=session_id, send_binary=send_binary, send_json=send_json
+ )
+ if rt is not None:
+ return rt
+
factory = getattr(state, "browser_voice_session_factory", None)
if factory is not None:
return factory(session_id=session_id, send_binary=send_binary, send_json=send_json)
diff --git a/jarvis/realtime/factory.py b/jarvis/realtime/factory.py
new file mode 100644
index 0000000000..5fcce1174b
--- /dev/null
+++ b/jarvis/realtime/factory.py
@@ -0,0 +1,42 @@
+# jarvis/realtime/factory.py
+"""Build a RealtimeVoiceSession for the browser /ws/audio path.
+
+Returns None (=> caller runs the classic path) when realtime is not selected or
+no OpenAI key is present. Phase 1 is OpenAI-only by scope; the cross-family
+chain lands in Phase 4.
+"""
+
+from __future__ import annotations
+
+import logging
+from typing import Any
+
+from jarvis.core.config import get_provider_secret
+
+log = logging.getLogger(__name__)
+
+
+def build_realtime_session(
+ *, cfg: Any, bus: Any, session_id: str, send_binary: Any, send_json: Any
+):
+ mode = getattr(getattr(cfg, "voice", None), "mode", "pipeline")
+ if mode != "realtime":
+ return None
+ if not get_provider_secret("openai"):
+ log.info("realtime: no OpenAI key — falling back to the classic path")
+ return None
+ try:
+ from jarvis.plugins.realtime.openai_realtime import OpenAIRealtimeProvider
+ from jarvis.realtime.session import RealtimeVoiceSession
+
+ return RealtimeVoiceSession(
+ session_id=session_id,
+ send_binary=send_binary,
+ send_json=send_json,
+ provider=OpenAIRealtimeProvider(),
+ config=cfg,
+ bus=bus,
+ )
+ except Exception as exc: # noqa: BLE001 — unbuildable stack => classic path
+ log.warning("realtime: session build failed: %s", exc)
+ return None
diff --git a/tests/unit/browser_voice/test_route.py b/tests/unit/browser_voice/test_route.py
index f9b0343790..7454dba43c 100644
--- a/tests/unit/browser_voice/test_route.py
+++ b/tests/unit/browser_voice/test_route.py
@@ -58,8 +58,13 @@ class _FakeWS:
def _state(session, cfg=None):
+ # Default cfg explicitly opts into the classic bridge (Task 7 inverted the
+ # gate to default-OFF; these tests exercise the classic dispatch/session
+ # behavior, not the gate itself — see test_route_closes_when_disabled for
+ # that).
+ default_cfg = SimpleNamespace(browser_voice=SimpleNamespace(enabled=True))
return SimpleNamespace(
- config=cfg if cfg is not None else SimpleNamespace(), # no browser_voice -> enabled
+ config=cfg if cfg is not None else default_cfg,
bus=None,
browser_voice_session_factory=lambda **kw: session,
)
@@ -93,7 +98,9 @@ async def test_route_closes_when_disabled():
async def test_route_closes_when_speech_stack_unavailable():
state = SimpleNamespace(
- config=SimpleNamespace(),
+ # Explicit opt-in (Task 7 default-OFF gate) so the socket reaches the
+ # session build instead of closing early on the disabled gate.
+ config=SimpleNamespace(browser_voice=SimpleNamespace(enabled=True)),
bus=None,
browser_voice_session_factory=lambda **kw: None, # build failed (no key)
)
diff --git a/tests/unit/realtime/test_factory.py b/tests/unit/realtime/test_factory.py
new file mode 100644
index 0000000000..980ea04056
--- /dev/null
+++ b/tests/unit/realtime/test_factory.py
@@ -0,0 +1,54 @@
+"""Task 7 — the realtime session factory for the browser /ws/audio path.
+
+``build_realtime_session`` returns ``None`` (=> caller runs the classic path)
+when [voice].mode != "realtime" or no OpenAI key is present; otherwise it
+builds an OpenAIRealtimeProvider-backed RealtimeVoiceSession. The provider
+gate goes through the module-level ``get_provider_secret`` name so a test can
+monkeypatch it without touching real credential storage.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from jarvis.realtime.factory import build_realtime_session
+
+
+def test_returns_none_when_mode_is_pipeline(monkeypatch):
+ import jarvis.realtime.factory as f
+
+ monkeypatch.setattr(f, "get_provider_secret", lambda _p: "sk-x")
+ cfg = SimpleNamespace(
+ voice=SimpleNamespace(mode="pipeline"), brain=SimpleNamespace(reply_language="en")
+ )
+ assert (
+ build_realtime_session(cfg=cfg, bus=None, session_id="s", send_binary=None, send_json=None)
+ is None
+ )
+
+
+def test_returns_none_when_no_openai_key(monkeypatch):
+ import jarvis.realtime.factory as f
+
+ monkeypatch.setattr(f, "get_provider_secret", lambda _p: None)
+ cfg = SimpleNamespace(
+ voice=SimpleNamespace(mode="realtime"), brain=SimpleNamespace(reply_language="en")
+ )
+ assert (
+ build_realtime_session(cfg=cfg, bus=None, session_id="s", send_binary=None, send_json=None)
+ is None
+ )
+
+
+def test_builds_session_when_realtime_and_keyed(monkeypatch):
+ import jarvis.realtime.factory as f
+
+ monkeypatch.setattr(f, "get_provider_secret", lambda _p: "sk-x")
+ cfg = SimpleNamespace(
+ voice=SimpleNamespace(mode="realtime"), brain=SimpleNamespace(reply_language="en")
+ )
+ sess = build_realtime_session(
+ cfg=cfg, bus=None, session_id="s", send_binary=lambda b: None, send_json=lambda m: None
+ )
+ assert sess is not None
+ assert sess.session_id == "s"
diff --git a/tests/unit/web/__init__.py b/tests/unit/web/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/unit/web/test_voice_mode_route.py b/tests/unit/web/test_voice_mode_route.py
new file mode 100644
index 0000000000..84215311bd
--- /dev/null
+++ b/tests/unit/web/test_voice_mode_route.py
@@ -0,0 +1,33 @@
+"""Task 7 — the browser-voice connect gate, inverted to default OFF.
+
+``_browser_voice_enabled`` now serves the /ws/audio socket only when the user
+has explicitly opted into a voice surface: realtime mode ([voice].mode ==
+"realtime") or the classic bridge ([browser_voice].enabled == True). A missing
+[browser_voice] section is False, not True (the old default-ON contract).
+
+NOTE: a later task (T8) appends settings-route handler tests to this same
+file — keep additions here additive and self-contained.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from jarvis.browser_voice.route import _browser_voice_enabled
+
+
+def test_gate_default_off_when_pipeline_and_no_browser_voice():
+ cfg = SimpleNamespace(voice=SimpleNamespace(mode="pipeline"))
+ assert _browser_voice_enabled(cfg) is False
+
+
+def test_gate_on_for_realtime_mode():
+ cfg = SimpleNamespace(voice=SimpleNamespace(mode="realtime"))
+ assert _browser_voice_enabled(cfg) is True
+
+
+def test_gate_on_for_explicit_classic_browser_voice():
+ cfg = SimpleNamespace(
+ voice=SimpleNamespace(mode="pipeline"), browser_voice=SimpleNamespace(enabled=True)
+ )
+ assert _browser_voice_enabled(cfg) is True