Personal AI, On Personal Devices
Python
feat(speech): add ElevenLabs TTS backend (#1133)
ricardofarhat80229e8f0eae
diff --git a/src/openjarvis/core/config.py b/src/openjarvis/core/config.pyindex 7c131608..762ec662 100644--- a/src/openjarvis/core/config.py+++ b/src/openjarvis/core/config.py@@ -1633,7 +1633,7 @@ class SpeechConfig:# back to a different backend that backend's own default voice is used.# Kokoro IDs: bm_george / bm_lewis (British male), bf_emma / bf_isabella# (British female), af_* / am_* (American).- tts_backend: str = "kokoro" # "kokoro", "openai_tts", "cartesia"+ tts_backend: str = "kokoro" # "kokoro", "openai_tts", "cartesia", "elevenlabs"voice_id: str = "bm_george"voice_speed: float = 1.0diff --git a/src/openjarvis/security/data_boundary_audit.py b/src/openjarvis/security/data_boundary_audit.pyindex e58f0d4f..80880c92 100644--- a/src/openjarvis/security/data_boundary_audit.py+++ b/src/openjarvis/security/data_boundary_audit.py@@ -59,6 +59,10 @@ API_KEY_ENV_VARS = {{"cartesia", "text_to_speech"},),"DEEPSEEK_API_KEY": ("DeepSeek cloud inference", {"deepseek"}),+ "ELEVENLABS_API_KEY": (+ "ElevenLabs cloud text-to-speech",+ {"elevenlabs", "text_to_speech"},+ ),"FXMACRODATA_API_KEY": ("FXMacroData macro and FX data lookup",{"fx_macro_data", "fxmacrodata"},@@ -227,7 +231,7 @@ MARKET_DATA_TOOLS = {"fx_macro_data"}CHANNEL_OUTBOUND_TOOLS = {"channel_send"}CONNECTOR_NETWORK_TOOLS = {"digest_collect", "execute_pending_actions"}CLOUD_MEDIA_TOOLS = {"audio_transcribe", "image_generate", "text_to_speech"}-CLOUD_TTS_BACKENDS = {"cartesia", "openai", "openai_tts"}+CLOUD_TTS_BACKENDS = {"cartesia", "elevenlabs", "openai", "openai_tts"}# Local knowledge chunks scanned by an inference engine (Deep Research path).KNOWLEDGE_ENGINE_TOOLS = {"scan_chunks"}# External egress surfaces (web, browser, HTTP, channels, media, knowledge LM).diff --git a/src/openjarvis/speech/__init__.py b/src/openjarvis/speech/__init__.pyindex 449f6214..05d9ea92 100644--- a/src/openjarvis/speech/__init__.py+++ b/src/openjarvis/speech/__init__.py@@ -10,7 +10,7 @@ for _mod in ("faster_whisper", "openai_whisper", "deepgram"):pass# Optional TTS backends — each registers itself via @TTSRegistry.register()-for _mod in ("cartesia_tts", "kokoro_tts", "openai_tts"):+for _mod in ("cartesia_tts", "elevenlabs_tts", "kokoro_tts", "openai_tts"):try:importlib.import_module(f".{_mod}", __name__)except ImportError:diff --git a/src/openjarvis/speech/_tts_discovery.py b/src/openjarvis/speech/_tts_discovery.pyindex b77b9615..9d39c213 100644--- a/src/openjarvis/speech/_tts_discovery.py+++ b/src/openjarvis/speech/_tts_discovery.py@@ -16,7 +16,7 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)# Priority order: local first, then cloud.-TTS_BACKEND_ORDER = ("kokoro", "openai_tts", "cartesia")+TTS_BACKEND_ORDER = ("kokoro", "openai_tts", "cartesia", "elevenlabs")# Voice IDs are backend-specific and NOT portable. ``speech.voice_id`` applies# only to ``speech.tts_backend``; if synthesis falls back to another backend we@@ -25,6 +25,7 @@ BACKEND_DEFAULT_VOICE = {"kokoro": "bm_george", # British male"openai_tts": "onyx", # deepest OpenAI preset"cartesia": "", # no safe static default; let Cartesia choose+ "elevenlabs": "JBFqnCBsd6RMkjVDRZzb", # "George", British male}diff --git a/src/openjarvis/speech/elevenlabs_tts.py b/src/openjarvis/speech/elevenlabs_tts.pynew file mode 100644index 00000000..ad8d35ef--- /dev/null+++ b/src/openjarvis/speech/elevenlabs_tts.py@@ -0,0 +1,194 @@+"""ElevenLabs text-to-speech backend.++Uses the ElevenLabs REST API for high-quality multilingual voice synthesis.+Requires ELEVENLABS_API_KEY environment variable or config.+"""++from __future__ import annotations++import io+import os+import re+import wave+from collections.abc import Callable+from typing import Any, List, TypeVar++import httpx++from openjarvis.core.registry import TTSRegistry+from openjarvis.speech.tts import TTSBackend, TTSResult++_ELEVENLABS_API_BASE = "https://api.elevenlabs.io/v1"++# "George" — warm British male premade voice, Jarvis-like+_DEFAULT_VOICE_ID = "JBFqnCBsd6RMkjVDRZzb"++# Kokoro-style IDs ("bm_george", "pf_dora"). SpeechConfig.voice_id defaults to+# a Kokoro voice, so it can reach this backend; ElevenLabs would 404 on it.+_KOKORO_VOICE_RE = re.compile(r"^[a-z][fm]_[a-z]+$")++_PCM_SAMPLE_RATE = 24000+_OUTPUT_FORMATS = {+ "mp3": "mp3_44100_128",+ "pcm": f"pcm_{_PCM_SAMPLE_RATE}",+ "wav": f"pcm_{_PCM_SAMPLE_RATE}", # wrapped in a WAV header locally+}+_ResponseT = TypeVar("_ResponseT")+++def _normalize_api_key(value: str) -> str:+ key = value.strip()+ if any(not 33 <= ord(char) <= 126 for char in key):+ # Reject unsupported header characters without echoing the credential.+ raise ValueError("ElevenLabs API key contains unsupported characters")+ return key+++def _request(+ request: Callable[..., httpx.Response],+ path: str,+ api_key: str,+ read_response: Callable[[httpx.Response], _ResponseT],+ **kwargs: Any,+) -> _ResponseT:+ """Keep provider request/response details out of errors and their tracebacks."""+ key = _normalize_api_key(api_key)+ if not key:+ raise RuntimeError("ELEVENLABS_API_KEY not set")+ try:+ response = request(+ f"{_ELEVENLABS_API_BASE}{path}",+ headers={"xi-api-key": key},+ **kwargs,+ )+ response.raise_for_status()+ # Reading audio/JSON belongs inside the same boundary as sending:+ # transport and decode errors can contain credential-bearing details.+ return read_response(response)+ except httpx.HTTPStatusError as exc:+ raise RuntimeError(+ f"ElevenLabs request failed (HTTP {exc.response.status_code})"+ ) from None+ except Exception as exc:+ # Preserve the error category, never its message, headers, URL, or body.+ raise RuntimeError(+ f"ElevenLabs request failed ({type(exc).__name__})"+ ) from None+++def _voice_ids(response: httpx.Response) -> List[str]:+ payload = response.json()+ voices = payload.get("voices", []) if isinstance(payload, dict) else None+ if not isinstance(voices, list) or any(+ not isinstance(voice, dict) or not isinstance(voice.get("voice_id"), str)+ for voice in voices+ ):+ raise ValueError("Invalid voice list")+ return [voice["voice_id"] for voice in voices]+++def _pcm_to_wav(pcm: bytes, sample_rate: int = _PCM_SAMPLE_RATE) -> bytes:+ """Wrap raw 16-bit mono PCM in a WAV container."""+ buf = io.BytesIO()+ with wave.open(buf, "wb") as wf:+ wf.setnchannels(1)+ wf.setsampwidth(2)+ wf.setframerate(sample_rate)+ wf.writeframes(pcm)+ return buf.getvalue()+++def _elevenlabs_synthesize(+ api_key: str,+ text: str,+ voice_id: str,+ model: str = "eleven_multilingual_v2",+ output_format: str = "mp3",+ speed: float = 1.0,+) -> bytes:+ """Call the ElevenLabs TTS API and return raw audio bytes."""+ return _request(+ httpx.post,+ f"/text-to-speech/{voice_id}",+ api_key,+ lambda response: response.content,+ params={"output_format": _OUTPUT_FORMATS.get(output_format, "mp3_44100_128")},+ json={+ "text": text,+ "model_id": model,+ # ElevenLabs accepts speed in [0.7, 1.2]+ **(+ {"voice_settings": {"speed": max(0.7, min(1.2, speed))}}+ if speed != 1.0+ else {}+ ),+ },+ timeout=120.0,+ )++[email protected]("elevenlabs")+class ElevenLabsTTSBackend(TTSBackend):+ """ElevenLabs TTS backend — premium multilingual synthesis."""++ backend_id = "elevenlabs"++ def __init__(+ self, *, api_key: str = "", model: str = "", voice_id: str = ""+ ) -> None:+ self._api_key = _normalize_api_key(+ api_key or os.environ.get("ELEVENLABS_API_KEY", "")+ )+ self._model = model or os.environ.get(+ "ELEVENLABS_MODEL", "eleven_multilingual_v2"+ )+ self._voice_id = voice_id or os.environ.get("ELEVENLABS_VOICE_ID", "")++ def synthesize(+ self,+ text: str,+ *,+ voice_id: str = "",+ speed: float = 1.0,+ output_format: str = "mp3",+ ) -> TTSResult:+ if not self._api_key:+ raise RuntimeError("ELEVENLABS_API_KEY not set")++ if _KOKORO_VOICE_RE.match(voice_id):+ voice_id = ""+ voice_id = voice_id or self._voice_id or _DEFAULT_VOICE_ID+ fmt = output_format if output_format in _OUTPUT_FORMATS else "mp3"++ audio = _elevenlabs_synthesize(+ self._api_key,+ text,+ voice_id=voice_id,+ model=self._model,+ output_format=fmt,+ speed=speed,+ )+ if fmt == "wav":+ audio = _pcm_to_wav(audio)++ return TTSResult(+ audio=audio,+ format=fmt,+ voice_id=voice_id,+ sample_rate=_PCM_SAMPLE_RATE if fmt != "mp3" else 44100,+ metadata={"backend": "elevenlabs", "model": self._model},+ )++ def available_voices(self) -> List[str]:+ if not self._api_key:+ return []+ return _request(+ httpx.get,+ "/voices",+ self._api_key,+ _voice_ids,+ timeout=30.0,+ )++ def health(self) -> bool:+ return bool(self._api_key)diff --git a/src/openjarvis/tools/text_to_speech.py b/src/openjarvis/tools/text_to_speech.pyindex c5133245..c8f832cd 100644--- a/src/openjarvis/tools/text_to_speech.py+++ b/src/openjarvis/tools/text_to_speech.py@@ -41,7 +41,9 @@ class TextToSpeechTool(BaseTool):},"backend": {"type": "string",- "description": "TTS backend (cartesia, kokoro, openai_tts).",+ "description": (+ "TTS backend (cartesia, elevenlabs, kokoro, openai_tts)."+ ),},"output_dir": {"type": "string",diff --git a/tests/speech/test_elevenlabs_credentials.py b/tests/speech/test_elevenlabs_credentials.pynew file mode 100644index 00000000..e44597da--- /dev/null+++ b/tests/speech/test_elevenlabs_credentials.py@@ -0,0 +1,140 @@+"""Keep ElevenLabs credentials out of provider errors and downstream logs."""++from __future__ import annotations++import logging+import traceback+from unittest.mock import Mock++import httpx+import pytest++from openjarvis.speech import elevenlabs_tts+from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++_KEY = "sk_" + "a" * 48++[email protected]("source", ["argument", "environment"])+def test_key_whitespace_is_normalized_before_request(monkeypatch, source):+ monkeypatch.delenv("ELEVENLABS_API_KEY", raising=False)+ key = f" \t{_KEY}\r\n"+ if source == "environment":+ monkeypatch.setenv("ELEVENLABS_API_KEY", key)+ backend = ElevenLabsTTSBackend()+ else:+ backend = ElevenLabsTTSBackend(api_key=key)+ post = Mock(+ return_value=httpx.Response(+ 200,+ content=b"audio",+ request=httpx.Request("POST", "https://api.elevenlabs.io/v1/test"),+ )+ )+ monkeypatch.setattr(elevenlabs_tts.httpx, "post", post)++ assert backend.synthesize("hello").audio == b"audio"+ assert post.call_args.kwargs["headers"]["xi-api-key"] == _KEY++[email protected]("source", ["argument", "environment"])+def test_whitespace_only_key_is_unconfigured(monkeypatch, source):+ monkeypatch.delenv("ELEVENLABS_API_KEY", raising=False)+ if source == "environment":+ monkeypatch.setenv("ELEVENLABS_API_KEY", " \r\n\t")+ backend = ElevenLabsTTSBackend()+ else:+ backend = ElevenLabsTTSBackend(api_key=" \r\n\t")+ assert not backend.health()+ assert backend.available_voices() == []+ with pytest.raises(RuntimeError, match="ELEVENLABS_API_KEY not set"):+ backend.synthesize("hello")+++ "bad_key",+ [_KEY + "\nextra", _KEY + "\x00extra", _KEY + " internal", _KEY + "\u00e9"],+)+def test_invalid_key_is_rejected_without_revealing_it(monkeypatch, bad_key):+ post = Mock()+ monkeypatch.setattr(elevenlabs_tts.httpx, "post", post)+ with pytest.raises(ValueError, match="ElevenLabs API key") as caught:+ ElevenLabsTTSBackend(api_key=bad_key).synthesize("hello")+ assert _KEY not in "".join(traceback.format_exception(caught.value))+ post.assert_not_called()++[email protected]("operation", ["synthesize", "available_voices"])+ "error_type",+ [+ httpx.ConnectError,+ httpx.ReadError,+ httpx.ReadTimeout,+ httpx.LocalProtocolError,+ ValueError,+ ],+)+def test_request_errors_do_not_reveal_keys(monkeypatch, caplog, operation, error_type):+ backend = ElevenLabsTTSBackend(api_key=_KEY)+ request = Mock(side_effect=error_type(f"provider diagnostic containing {_KEY}"))+ monkeypatch.setattr(+ elevenlabs_tts.httpx, "post" if operation == "synthesize" else "get", request+ )+ with pytest.raises(RuntimeError, match="ElevenLabs request failed") as caught:+ if operation == "synthesize":+ backend.synthesize("hello")+ else:+ backend.available_voices()+ try:+ raise caught.value+ except RuntimeError:+ logging.getLogger(__name__).exception("Speech request failed")+ assert _KEY not in str(caught.value)+ assert _KEY not in "".join(traceback.format_exception(caught.value))+ assert _KEY not in caplog.text++[email protected]("operation", ["synthesize", "available_voices"])+def test_status_errors_keep_status_without_provider_body(monkeypatch, operation):+ response = httpx.Response(+ 401,+ text=f"echoed credential {_KEY}",+ request=httpx.Request("POST", "https://api.elevenlabs.io/v1/" + _KEY),+ )+ monkeypatch.setattr(+ elevenlabs_tts.httpx,+ "post" if operation == "synthesize" else "get",+ Mock(return_value=response),+ )+ backend = ElevenLabsTTSBackend(api_key=_KEY)+ with pytest.raises(RuntimeError, match="HTTP 401") as caught:+ if operation == "synthesize":+ backend.synthesize("hello")+ else:+ backend.available_voices()+ assert _KEY not in "".join(traceback.format_exception(caught.value))+++def test_invalid_voice_response_hides_provider_diagnostics(monkeypatch):+ response = Mock()+ response.json.side_effect = ValueError(f"malformed payload {_KEY}")+ monkeypatch.setattr(elevenlabs_tts.httpx, "get", Mock(return_value=response))+ with pytest.raises(RuntimeError, match="ElevenLabs request failed") as caught:+ ElevenLabsTTSBackend(api_key=_KEY).available_voices()+ assert _KEY not in "".join(traceback.format_exception(caught.value))+++ "payload",+ [{"voices": "invalid"}, {"voices": [{}]}, {"voices": [{"voice_id": 123}]}],+)+def test_malformed_voice_entries_are_rejected(monkeypatch, payload):+ response = httpx.Response(+ 200,+ json=payload,+ request=httpx.Request("GET", "https://api.elevenlabs.io/v1/voices"),+ )+ monkeypatch.setattr(elevenlabs_tts.httpx, "get", Mock(return_value=response))+ with pytest.raises(RuntimeError, match="ElevenLabs request failed"):+ ElevenLabsTTSBackend(api_key=_KEY).available_voices()diff --git a/tests/speech/test_elevenlabs_error_propagation.py b/tests/speech/test_elevenlabs_error_propagation.pynew file mode 100644index 00000000..cdb4fed0--- /dev/null+++ b/tests/speech/test_elevenlabs_error_propagation.py@@ -0,0 +1,136 @@+"""Exercise credential-safe errors beyond the ElevenLabs backend boundary."""++from __future__ import annotations++import dataclasses+import json+import logging+import traceback+from unittest.mock import Mock++import httpx+import pytest++from openjarvis.speech import elevenlabs_tts+from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++_KEY = "sk_review_" + "credential_sentinel_" * 3++[email protected]("error_type", [httpx.LocalProtocolError, httpx.ReadTimeout])+def test_tool_events_and_persisted_trace_keep_credentials_private(+ monkeypatch, tmp_path, caplog, error_type+):+ from openjarvis.agents._stubs import AgentResult+ from openjarvis.core.events import EventBus, EventType+ from openjarvis.core.registry import TTSRegistry+ from openjarvis.core.types import StepType, ToolCall+ from openjarvis.tools._stubs import ToolExecutor+ from openjarvis.tools.text_to_speech import TextToSpeechTool+ from openjarvis.traces.collector import TraceCollector+ from openjarvis.traces.store import TraceStore++ monkeypatch.setenv("ELEVENLABS_API_KEY", _KEY)+ TTSRegistry.register_value("elevenlabs", ElevenLabsTTSBackend)+ post = Mock(side_effect=error_type(f"provider diagnostic: {_KEY}"))+ monkeypatch.setattr(elevenlabs_tts.httpx, "post", post)+ bus = EventBus(record_history=True)+ executor = ToolExecutor([TextToSpeechTool()], bus=bus)++ class SpeechAgent:+ agent_id = "speech-credential-regression"++ def run(self, *args, **kwargs):+ result = executor.execute(+ ToolCall(+ id="speech",+ name="text_to_speech",+ arguments=json.dumps(+ {+ "text": "A synthetic sample.",+ "backend": "elevenlabs",+ "output_dir": str(tmp_path / "audio"),+ }+ ),+ )+ )+ assert not result.success+ return AgentResult(content=result.content, tool_results=[result])++ store = TraceStore(tmp_path / "traces.db")+ try:+ collector = TraceCollector(SpeechAgent(), store=store, bus=bus)+ result = collector.run("Speak the sample.")+ post.assert_called_once()+ trace = store.get(collector.last_trace.trace_id)+ assert trace is not None+ assert any(step.step_type == StepType.TOOL_CALL for step in trace.steps)+ assert any(event.event_type == EventType.TOOL_CALL_END for event in bus.history)+ recorded = "\n".join(+ [+ repr(result),+ json.dumps(dataclasses.asdict(trace), default=str),+ repr(bus.history),+ caplog.text,+ ]+ )+ assert _KEY not in recorded+ finally:+ store.close()+++def test_audio_read_failure_has_a_safe_logged_traceback(monkeypatch, caplog):+ class UnreadableAudio:+ def raise_for_status(self):+ pass++ @property+ def content(self):+ raise httpx.ReadError(f"response diagnostic: {_KEY}")++ monkeypatch.setattr(+ elevenlabs_tts.httpx, "post", Mock(return_value=UnreadableAudio())+ )+ with pytest.raises(RuntimeError, match="ElevenLabs request failed") as caught:+ ElevenLabsTTSBackend(api_key=_KEY).synthesize("A synthetic sample.")+ try:+ raise caught.value+ except RuntimeError:+ logging.getLogger(__name__).exception("Speech audio could not be read")+ assert _KEY not in str(caught.value)+ assert _KEY not in "".join(traceback.format_exception(caught.value))+ assert _KEY not in caplog.text++[email protected]("operation", ["synthesize", "available_voices"])[email protected]("failure", ["transport", "stream"])+def test_httpx_transport_failures_do_not_escape_with_credentials(+ monkeypatch, caplog, operation, failure+):+ received = []++ class BrokenStream(httpx.SyncByteStream):+ def __iter__(self):+ raise httpx.ReadError(f"stream diagnostic: {_KEY}")+ yield b"" # pragma: no cover - preserves the byte-stream protocol++ def handler(request):+ received.append(request.headers["xi-api-key"])+ if failure == "transport":+ raise httpx.LocalProtocolError(+ f"header diagnostic: {_KEY}", request=request+ )+ return httpx.Response(200, request=request, stream=BrokenStream())++ with httpx.Client(transport=httpx.MockTransport(handler)) as client:+ method = "post" if operation == "synthesize" else "get"+ monkeypatch.setattr(elevenlabs_tts.httpx, method, getattr(client, method))+ backend = ElevenLabsTTSBackend(api_key=_KEY)+ with pytest.raises(RuntimeError, match="ElevenLabs request failed") as caught:+ if operation == "synthesize":+ backend.synthesize("A synthetic sample.")+ else:+ backend.available_voices()+ assert received == [_KEY]+ assert _KEY not in "".join(traceback.format_exception(caught.value))+ assert _KEY not in caplog.textdiff --git a/tests/speech/test_tts_backends.py b/tests/speech/test_tts_backends.pyindex 11f9b511..510f3352 100644--- a/tests/speech/test_tts_backends.py+++ b/tests/speech/test_tts_backends.py@@ -741,3 +741,118 @@ def test_openai_tts_synthesize():assert result.audio == b"fake-openai-audio"assert result.voice_id == "nova"+++# ---------------------------------------------------------------------------+# ElevenLabs backend tests+# ---------------------------------------------------------------------------+++def test_elevenlabs_registered():+ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ TTSRegistry.register_value("elevenlabs", ElevenLabsTTSBackend)+ assert TTSRegistry.contains("elevenlabs")+++def test_elevenlabs_health_requires_key(monkeypatch):+ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ monkeypatch.delenv("ELEVENLABS_API_KEY", raising=False)+ assert ElevenLabsTTSBackend().health() is False+ assert ElevenLabsTTSBackend(api_key="fake-key").health() is True+++def test_elevenlabs_synthesize_mp3():+ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ backend = ElevenLabsTTSBackend(api_key="fake-key")+ with patch(+ "openjarvis.speech.elevenlabs_tts._elevenlabs_synthesize",+ return_value=b"fake-mp3",+ ) as synth:+ result = backend.synthesize("Hello", voice_id="abc123XYZ")++ assert result.audio == b"fake-mp3"+ assert result.format == "mp3"+ assert result.voice_id == "abc123XYZ"+ assert synth.call_args.kwargs["voice_id"] == "abc123XYZ"+++def test_elevenlabs_synthesize_wav_wraps_pcm():+ import io+ import wave++ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ pcm = b"\x00\x01" * 100+ backend = ElevenLabsTTSBackend(api_key="fake-key")+ with patch(+ "openjarvis.speech.elevenlabs_tts._elevenlabs_synthesize",+ return_value=pcm,+ ) as synth:+ result = backend.synthesize("Hello", output_format="wav")++ assert synth.call_args.kwargs["output_format"] == "wav"+ assert result.format == "wav"+ assert result.sample_rate == 24000+ with wave.open(io.BytesIO(result.audio)) as wf:+ assert wf.getframerate() == 24000+ assert wf.getnchannels() == 1+ assert wf.readframes(wf.getnframes()) == pcm++[email protected]("kokoro_voice", ["bm_george", "pf_dora", "af_heart"])+def test_elevenlabs_ignores_kokoro_voice_ids(monkeypatch, kokoro_voice):+ # SpeechConfig.voice_id defaults to a Kokoro voice; it must not be sent+ # to ElevenLabs, which would return 404.+ from openjarvis.speech.elevenlabs_tts import (+ _DEFAULT_VOICE_ID,+ ElevenLabsTTSBackend,+ )++ monkeypatch.delenv("ELEVENLABS_VOICE_ID", raising=False)+ backend = ElevenLabsTTSBackend(api_key="fake-key")+ with patch(+ "openjarvis.speech.elevenlabs_tts._elevenlabs_synthesize",+ return_value=b"fake-mp3",+ ) as synth:+ result = backend.synthesize("Olá", voice_id=kokoro_voice)++ assert synth.call_args.kwargs["voice_id"] == _DEFAULT_VOICE_ID+ assert result.voice_id == _DEFAULT_VOICE_ID+++def test_elevenlabs_env_voice_used_when_none_given(monkeypatch):+ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ monkeypatch.setenv("ELEVENLABS_VOICE_ID", "envVoice123")+ backend = ElevenLabsTTSBackend(api_key="fake-key")+ with patch(+ "openjarvis.speech.elevenlabs_tts._elevenlabs_synthesize",+ return_value=b"fake-mp3",+ ) as synth:+ backend.synthesize("Hello", voice_id="bm_george")++ assert synth.call_args.kwargs["voice_id"] == "envVoice123"+++def test_elevenlabs_speed_is_clamped():+ from openjarvis.speech.elevenlabs_tts import _elevenlabs_synthesize++ with patch("openjarvis.speech.elevenlabs_tts.httpx.post") as post:+ post.return_value.content = b"audio"+ _elevenlabs_synthesize("key", "Hello", voice_id="v", speed=2.0)+ _elevenlabs_synthesize("key", "Hello", voice_id="v", speed=1.0)++ fast, normal = post.call_args_list+ assert fast.kwargs["json"]["voice_settings"] == {"speed": 1.2}+ assert "voice_settings" not in normal.kwargs["json"]+++def test_elevenlabs_missing_key_raises(monkeypatch):+ from openjarvis.speech.elevenlabs_tts import ElevenLabsTTSBackend++ monkeypatch.delenv("ELEVENLABS_API_KEY", raising=False)+ with pytest.raises(RuntimeError, match="ELEVENLABS_API_KEY"):+ ElevenLabsTTSBackend().synthesize("Hello")