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(tts): key-aware internal fallback for all plugins (AP-22)

Ruben Lütkeeb3e8125a1b0
diff --git a/jarvis/plugins/tts/cartesia_tts.py b/jarvis/plugins/tts/cartesia_tts.py
index 1b5a92f393..936a200822 100644
--- a/jarvis/plugins/tts/cartesia_tts.py
+++ b/jarvis/plugins/tts/cartesia_tts.py
@@ -345,23 +345,34 @@ class CartesiaTTS:
self, text: str, language_code: str | None
) -> AsyncIterator[AudioChunk]:
log = logging.getLogger("jarvis.tts.cartesia")
- try:
- from jarvis.plugins.tts.gemini_flash_tts import GeminiFlashTTS
- gemini = GeminiFlashTTS(
- language_code=language_code or "de-DE",
- allow_sapi5_fallback=self._allow_sapi5_fallback,
- )
- async for chunk in gemini.synthesize(
- text, language_code=language_code
- ):
- yield chunk
- return
- except Exception as exc: # noqa: BLE001
- log.error(
- "Gemini fallback after Cartesia error also failed (%s).",
- exc.__class__.__name__, exc_info=True,
- )
+ # Stage 1: cross to whatever TTS family the host actually has a key for
+ # (AP-22) — never a hardcoded keyless Gemini that would go silently mute
+ # for a Cartesia-only downloader.
+ from jarvis.plugins.tts import resolve_keyed_fallback
+
+ fb = resolve_keyed_fallback(
+ self.name,
+ allow_sapi5=self._allow_sapi5_fallback,
+ language_code=language_code,
+ )
+ if fb is not None:
+ produced = False
+ try:
+ async for chunk in fb.synthesize(text, language_code=language_code):
+ produced = True
+ yield chunk
+ except Exception as exc: # noqa: BLE001
+ log.error(
+ "Key-aware fallback after Cartesia error also failed (%s).",
+ exc.__class__.__name__, exc_info=True,
+ )
+ finally:
+ aclose = getattr(fb, "aclose", None)
+ if aclose is not None:
+ await aclose()
+ if produced:
+ return
if not self._allow_sapi5_fallback:
log.error(
diff --git a/jarvis/plugins/tts/elevenlabs_tts.py b/jarvis/plugins/tts/elevenlabs_tts.py
index a8dd0656dd..77409d27ef 100644
--- a/jarvis/plugins/tts/elevenlabs_tts.py
+++ b/jarvis/plugins/tts/elevenlabs_tts.py
@@ -301,22 +301,33 @@ class ElevenLabsTTS:
"""
log = logging.getLogger("jarvis.tts.elevenlabs")
- # Stage 1: Gemini probieren (separate Quota).
- try:
- from jarvis.plugins.tts.gemini_flash_tts import GeminiFlashTTS
-
- gemini = GeminiFlashTTS(
- language_code=language_code or self._language_code,
- allow_sapi5_fallback=self._allow_sapi5_fallback,
- )
- async for chunk in gemini.synthesize(text, language_code=language_code):
- yield chunk
- return
- except Exception as exc: # noqa: BLE001
- log.error(
- "Gemini fallback after the ElevenLabs error also failed (%s).",
- exc.__class__.__name__, exc_info=True,
- )
+ # Stage 1: cross to whatever TTS family the host actually has a key for
+ # (AP-22) — never a hardcoded keyless Gemini that would go silently mute
+ # for an ElevenLabs-only downloader.
+ from jarvis.plugins.tts import resolve_keyed_fallback
+
+ fb = resolve_keyed_fallback(
+ self.name,
+ allow_sapi5=self._allow_sapi5_fallback,
+ language_code=language_code,
+ )
+ if fb is not None:
+ produced = False
+ try:
+ async for chunk in fb.synthesize(text, language_code=language_code):
+ produced = True
+ yield chunk
+ except Exception as exc: # noqa: BLE001
+ log.error(
+ "Key-aware fallback after the ElevenLabs error also failed (%s).",
+ exc.__class__.__name__, exc_info=True,
+ )
+ finally:
+ aclose = getattr(fb, "aclose", None)
+ if aclose is not None:
+ await aclose()
+ if produced:
+ return
if not self._allow_sapi5_fallback:
log.error(
diff --git a/jarvis/plugins/tts/grok_voice_tts.py b/jarvis/plugins/tts/grok_voice_tts.py
index b0a46fb985..e8c9c3b6f4 100644
--- a/jarvis/plugins/tts/grok_voice_tts.py
+++ b/jarvis/plugins/tts/grok_voice_tts.py
@@ -342,24 +342,33 @@ class GrokVoiceTTS:
"""
log = logging.getLogger("jarvis.tts.grok-voice")
- # Stage 1: try Gemini (separate quota).
- try:
- from jarvis.plugins.tts.gemini_flash_tts import GeminiFlashTTS
-
- gemini = GeminiFlashTTS(
- language_code=language_code or "de-DE",
- allow_sapi5_fallback=self._allow_sapi5_fallback,
- )
- async for chunk in gemini.synthesize(
- text, language_code=language_code
- ):
- yield chunk
- return
- except Exception as exc: # noqa: BLE001
- log.error(
- "Gemini fallback after the Grok error also failed (%s).",
- exc.__class__.__name__, exc_info=True,
- )
+ # Stage 1: cross to whatever TTS family the host actually has a key for
+ # (AP-22) — never a hardcoded keyless Gemini that would go silently mute
+ # for a Grok-only downloader.
+ from jarvis.plugins.tts import resolve_keyed_fallback
+
+ fb = resolve_keyed_fallback(
+ self.name,
+ allow_sapi5=self._allow_sapi5_fallback,
+ language_code=language_code,
+ )
+ if fb is not None:
+ produced = False
+ try:
+ async for chunk in fb.synthesize(text, language_code=language_code):
+ produced = True
+ yield chunk
+ except Exception as exc: # noqa: BLE001
+ log.error(
+ "Key-aware fallback after the Grok error also failed (%s).",
+ exc.__class__.__name__, exc_info=True,
+ )
+ finally:
+ aclose = getattr(fb, "aclose", None)
+ if aclose is not None:
+ await aclose()
+ if produced:
+ return
if not self._allow_sapi5_fallback:
log.error(
diff --git a/jarvis/plugins/tts/inworld_tts.py b/jarvis/plugins/tts/inworld_tts.py
index d2585ca3d4..c3b9245f89 100644
--- a/jarvis/plugins/tts/inworld_tts.py
+++ b/jarvis/plugins/tts/inworld_tts.py
@@ -335,27 +335,41 @@ class InworldTTS:
self, text: str, language_code: str | None
) -> AsyncIterator[AudioChunk]:
log = logging.getLogger("jarvis.tts.inworld")
- try:
- from jarvis.plugins.tts.gemini_flash_tts import GeminiFlashTTS
- gemini = GeminiFlashTTS(
- language_code=language_code or "de-DE",
- allow_sapi5_fallback=self._allow_sapi5_fallback,
- )
- async for chunk in gemini.synthesize(text, language_code=language_code):
- yield chunk
- return
- except Exception as exc: # noqa: BLE001
- log.error(
- "Gemini fallback after Inworld error also failed (%s).",
- exc.__class__.__name__, exc_info=True,
- )
+ # Stage 1: cross to whatever TTS family the host actually has a key for
+ # (AP-22) — never a hardcoded keyless Gemini that would go silently mute
+ # for an Inworld-only downloader.
+ from jarvis.plugins.tts import resolve_keyed_fallback
+
+ fb = resolve_keyed_fallback(
+ self.name,
+ allow_sapi5=self._allow_sapi5_fallback,
+ language_code=language_code,
+ )
+ if fb is not None:
+ produced = False
+ try:
+ async for chunk in fb.synthesize(text, language_code=language_code):
+ produced = True
+ yield chunk
+ except Exception as exc: # noqa: BLE001
+ log.error(
+ "Key-aware fallback after Inworld error also failed (%s).",
+ exc.__class__.__name__, exc_info=True,
+ )
+ finally:
+ aclose = getattr(fb, "aclose", None)
+ if aclose is not None:
+ await aclose()
+ if produced:
+ return
if not self._allow_sapi5_fallback:
log.error(
- "Both Inworld and Gemini TTS produced no audio. SAPI5 emergency "
- "disabled -- staying silent. Set tts.allow_sapi5_fallback=true "
- "if Windows TTS is an acceptable last resort."
+ "Inworld failed and no other keyed TTS family is available. SAPI5 "
+ "emergency disabled -- staying silent. Set "
+ "tts.allow_sapi5_fallback=true if Windows TTS is an acceptable "
+ "last resort."
)
return
diff --git a/tests/unit/plugins/tts/test_keyed_fallback.py b/tests/unit/plugins/tts/test_keyed_fallback.py
new file mode 100644
index 0000000000..8e0f826236
--- /dev/null
+++ b/tests/unit/plugins/tts/test_keyed_fallback.py
@@ -0,0 +1,53 @@
+"""resolve_keyed_fallback: a plugin's internal fallback crosses to a family the
+host actually has a key for (AP-22), never a keyless mute provider."""
+from __future__ import annotations
+
+import jarvis.core.config as cfg
+import jarvis.plugins.tts as tts_pkg
+
+
+def _keys(*env_names: str):
+ have = set(env_names)
+
+ def fake(candidates: tuple[tuple[str, str], ...]) -> str | None:
+ for _keyring, env in candidates:
+ if env in have:
+ return "KEY"
+ return None
+
+ return fake
+
+
+def test_crosses_to_a_keyed_family_excluding_self(monkeypatch):
+ monkeypatch.setattr(cfg, "get_secret_any", _keys("GEMINI_API_KEY"))
+ fb = tts_pkg.resolve_keyed_fallback("cartesia")
+ assert fb is not None
+ assert tts_pkg._canonical_tts_name(fb.name) == "gemini-flash-tts"
+
+
+def test_none_when_only_the_excluded_family_has_a_key(monkeypatch):
+ # Only Cartesia has a key, and Cartesia is excluded → no keyed fallback.
+ monkeypatch.setattr(cfg, "get_secret_any", _keys("CARTESIA_API_KEY"))
+ assert tts_pkg.resolve_keyed_fallback("cartesia") is None
+
+
+def test_named_family_is_excluded_even_when_it_has_a_key(monkeypatch):
+ # Inworld + Gemini keyed; excluding Inworld must pick Gemini, not Inworld.
+ monkeypatch.setattr(cfg, "get_secret_any", _keys("INWORLD_API_KEY", "GEMINI_API_KEY"))
+ fb = tts_pkg.resolve_keyed_fallback("inworld")
+ assert fb is not None
+ assert tts_pkg._canonical_tts_name(fb.name) == "gemini-flash-tts"
+
+
+def test_no_key_anywhere_returns_none(monkeypatch):
+ monkeypatch.setattr(cfg, "get_secret_any", _keys())
+ assert tts_pkg.resolve_keyed_fallback("inworld") is None
+
+
+def test_openrouter_is_last_resort(monkeypatch):
+ # Only an OpenRouter key present → it IS chosen (last in the order, but the
+ # only keyed family that isn't excluded).
+ monkeypatch.setattr(cfg, "get_secret_any", _keys("OPENROUTER_API_KEY"))
+ fb = tts_pkg.resolve_keyed_fallback("cartesia")
+ assert fb is not None
+ assert tts_pkg._canonical_tts_name(fb.name) == "openrouter"
diff --git a/tests/unit/ui/test_desktop_console_hide.py b/tests/unit/ui/test_desktop_console_hide.py
index 2b5870a19a..623fbf8e64 100644
--- a/tests/unit/ui/test_desktop_console_hide.py
+++ b/tests/unit/ui/test_desktop_console_hide.py
@@ -13,12 +13,12 @@ These tests pin the pure decision (the sole-owner rule) and the orchestration
(probe → decide → hide) without needing a real Windows console — the win32
probes are injectable.
"""
+
from __future__ import annotations
from jarvis.ui import desktop_app
from jarvis.ui.desktop_app import _console_owned_exclusively, hide_accidental_console
-
# --- the pure sole-owner decision (cross-platform) -------------------------