Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/eva/assistant/cartesia_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from eva.models.agents import AgentConfig
from eva.models.config import ModelConfig
from eva.utils import cartesia_agents
from eva.utils.audio_utils import create_twilio_media_message, parse_twilio_media_message
from eva.utils.audio_utils import create_twilio_media_message, parse_twilio_media_message, pcm16_mix
from eva.utils.logging import get_logger

logger = get_logger(__name__)
Expand Down Expand Up @@ -60,15 +60,6 @@ def _pad_buffer_to_walltime(buffer: bytearray, elapsed_s: float, sample_rate: in
buffer.extend(b"\x00" * (target_bytes - len(buffer)))


def _mix_pcm16(track_a: bytes | bytearray, track_b: bytes | bytearray) -> bytes:
"""eva.utils.audio_utils.pcm16_mix, vectorized so teardown does not block the event loop."""
a = np.frombuffer(track_a, dtype="<i2").astype(np.int32)
b = np.frombuffer(track_b, dtype="<i2").astype(np.int32)
n = max(len(a), len(b))
mixed = np.pad(a, (0, n - len(a))) + np.pad(b, (0, n - len(b)))
return np.clip(mixed, -32768, 32767).astype("<i2").tobytes()


class _SpeechSegment:
"""One spoken agent turn and the words in it.

Expand Down Expand Up @@ -214,9 +205,9 @@ async def _shutdown(self) -> None:
pass
self._server = None
self._server_task = None
# Mix here so the base class skips its slower per-sample mix.
# Mix here, straight from the bytearrays, so the base class skips its own copy-and-mix.
if self.user_audio_buffer and self.assistant_audio_buffer and not self._audio_buffer:
self._audio_buffer = bytearray(_mix_pcm16(self.user_audio_buffer, self.assistant_audio_buffer))
self._audio_buffer = bytearray(pcm16_mix(self.user_audio_buffer, self.assistant_audio_buffer))
logger.info(f"Cartesia managed server stopped on port {self.port} (call_id={self._call_id})")

def _session_create(self) -> dict:
Expand Down
5 changes: 3 additions & 2 deletions src/eva/user_simulator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from eva.utils.audio_utils import save_audio_track
from eva.utils.culture import add_user_language_directive
from eva.utils.logging import current_record_id, get_logger
from eva.utils.prompt_manager import PromptManager
from eva.utils.prompt_manager import get_prompt_manager

logger = get_logger(__name__)

Expand Down Expand Up @@ -104,7 +104,8 @@ def _build_prompt(self) -> str:
)

domain = self.agent_id.removeprefix("agent_")
return PromptManager().get_prompt(
# The shared instance: a fresh PromptManager() re-parses every prompt YAML on the shared event loop.
return get_prompt_manager().get_prompt(
f"user_simulator.system_prompt_{domain}",
high_level_user_goal=self.goal["high_level_user_goal"],
must_have_criteria=self.goal["decision_tree"]["must_have_criteria"],
Expand Down
6 changes: 4 additions & 2 deletions src/eva/user_simulator/cascade/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from eva.user_simulator.cascade.stt import ScribeStreamingSTT
from eva.user_simulator.cascade.tts import TTS_SAMPLE_RATE, CartesiaStreamingTTS
from eva.utils.logging import get_logger
from eva.utils.prompt_manager import PromptManager
from eva.utils.prompt_manager import get_prompt_manager

logger = get_logger(__name__)

Expand Down Expand Up @@ -199,7 +199,9 @@ def caller_voice(self) -> str:

def _build_prompt(self) -> str:
gender_word = "male" if self.caller_gender == "M" else "female"
gender_instruction = PromptManager().get_prompt("user_simulator.gender_instruction", gender_word=gender_word)
gender_instruction = get_prompt_manager().get_prompt(
"user_simulator.gender_instruction", gender_word=gender_word
)
return super()._build_prompt() + "\n\n" + gender_instruction

async def run_conversation(self) -> str:
Expand Down
13 changes: 9 additions & 4 deletions src/eva/user_simulator/elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ async def _check_end_call_via_api(self, conversation_id: str) -> bool | None:
attempt += 1
await asyncio.sleep(delay)
waited += delay
conv_details = self._client.conversational_ai.conversations.get(conversation_id)
# Sync HTTP call: run it in a thread so it doesn't block the event loop shared by every conversation.
conv_details = await asyncio.to_thread(self._client.conversational_ai.conversations.get, conversation_id)

# Dump full response to file for debugging/analysis
details_path = self.output_dir / "elevenlabs_conversation_details.json"
Expand Down Expand Up @@ -331,16 +332,20 @@ async def _check_end_call_via_api(self, conversation_id: str) -> bool | None:
)
return None

def _download_elevenlabs_audio(self, conversation_id: str, audio_path: Path) -> None:
"""Stream the server-side recording to ``audio_path``. Blocking: call it from a thread."""
audio_iter = self._client.conversational_ai.conversations.audio.get(conversation_id)
with open(audio_path, "wb") as f:
f.writelines(audio_iter)

async def _fetch_elevenlabs_audio(self, conversation_id: str) -> None:
max_attempts = 5
delay = 2.0

for attempt in range(max_attempts):
try:
audio_iter = self._client.conversational_ai.conversations.audio.get(conversation_id)
audio_path = self.output_dir / "elevenlabs_audio_recording.mp3"
with open(audio_path, "wb") as f:
f.writelines(audio_iter)
await asyncio.to_thread(self._download_elevenlabs_audio, conversation_id, audio_path)
logger.info(f"Saved ElevenLabs server-side audio to {audio_path}")
return
except Exception as e:
Expand Down
27 changes: 9 additions & 18 deletions src/eva/utils/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import base64
import io
import json
import struct
import wave
from pathlib import Path

Expand Down Expand Up @@ -128,28 +127,20 @@ def sync_buffer_to_position(buffer: bytearray, target_position: int) -> None:
buffer.extend(b"\x00" * (target_position - current_len))


def pcm16_mix(track_a: bytes, track_b: bytes) -> bytes:
def pcm16_mix(track_a: bytes | bytearray, track_b: bytes | bytearray) -> bytes:
"""Mix two 16-bit PCM tracks by sample-wise addition with clipping.

Both tracks must be the same sample rate. If lengths differ,
the shorter track is zero-padded.

Vectorized with numpy: this runs on the event loop at server teardown, and a per-sample
Python loop blocked every concurrent conversation for seconds on long calls.
"""
len_a, len_b = len(track_a), len(track_b)
max_len = max(len_a, len_b)

# Zero-pad shorter track
if len_a < max_len:
track_a = track_a + b"\x00" * (max_len - len_a)
if len_b < max_len:
track_b = track_b + b"\x00" * (max_len - len_b)

# Mix with clipping
n_samples = max_len // 2
fmt = f"<{n_samples}h"
samples_a = struct.unpack(fmt, track_a)
samples_b = struct.unpack(fmt, track_b)
mixed = struct.pack(fmt, *(max(-32768, min(32767, a + b)) for a, b in zip(samples_a, samples_b)))
return mixed
a = np.frombuffer(track_a, dtype="<i2").astype(np.int32)
b = np.frombuffer(track_b, dtype="<i2").astype(np.int32)
n = max(len(a), len(b))
mixed = np.pad(a, (0, n - len(a))) + np.pad(b, (0, n - len(b)))
return np.clip(mixed, -32768, 32767).astype("<i2").tobytes()


def resample_pcm16_soxr(pcm_bytes: bytes, from_rate: int, to_rate: int) -> bytes:
Expand Down
11 changes: 1 addition & 10 deletions tests/unit/assistant/test_cartesia_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import numpy as np

from eva.assistant.cartesia_server import CartesiaAssistantServer, _mix_pcm16, _SpeechSegment
from eva.utils.audio_utils import pcm16_mix
from eva.assistant.cartesia_server import CartesiaAssistantServer, _SpeechSegment


def _bare_server() -> CartesiaAssistantServer:
Expand Down Expand Up @@ -42,14 +41,6 @@ def test_speech_pieces_split_at_messages_and_tool_calls(monkeypatch):
assert segment.take_pieces() == [("One moment.", 1000), ("Checking now", 2500), ("and you are all set.", 4000)]


def test_mix_matches_eva_mix_with_clipping_and_unequal_lengths():
rng = np.random.default_rng(0)
a = rng.integers(-32768, 32767, 1000, dtype=np.int16).tobytes()
b = rng.integers(-32768, 32767, 700, dtype=np.int16).tobytes()
assert _mix_pcm16(a, b) == pcm16_mix(a, b)
assert _mix_pcm16(b, a) == pcm16_mix(b, a)


class _Recorder:
"""Records method calls, standing in for the audit and framework logs."""

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/user_simulator/test_cascade.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,24 @@ async def take_turn(text):
await asyncio.wait_for(simulator._run_turns(), timeout=1)

assert turns == [None] # asked to repeat once, then gave up


def test_system_prompt_reuses_the_loaded_prompt_files(tmp_path, monkeypatch):
"""The prompt is rebuilt every caller turn; a fresh PromptManager would re-parse the YAML on the shared loop."""
from eva.utils import prompt_manager

loads = []
original = prompt_manager.PromptManager._load_prompts

def counting_load(self):
loads.append(1)
original(self)

monkeypatch.setattr(prompt_manager.PromptManager, "_load_prompts", counting_load)
monkeypatch.setattr(prompt_manager, "_prompt_manager", None)

sim = _simulator(tmp_path)
prompts = [sim._build_prompt() for _ in range(3)]

assert len(set(prompts)) == 1
assert len(loads) == 1
53 changes: 53 additions & 0 deletions tests/unit/user_simulator/test_elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import asyncio
import threading
import wave
from pathlib import Path
from types import SimpleNamespace
Expand Down Expand Up @@ -447,3 +448,55 @@ def test_save_clean_user_audio_skips_when_empty(self, tmp_path):
sim._save_clean_user_audio(sample_rate=16000)

assert not (tmp_path / "audio_user_clean.wav").exists()


class TestBlockingCallsStayOffEventLoop:
"""The ElevenLabs REST client is synchronous: its calls must not run on the loop shared by every conversation."""

@pytest.mark.asyncio
async def test_end_call_check_runs_the_http_call_in_a_thread(self, tmp_path):
sim = _make_simulator(tmp_path)
sim._client = MagicMock()
loop_thread = threading.get_ident()
call_threads = []

def get(conversation_id):
call_threads.append(threading.get_ident())
return _make_conv_details(termination_reason="end_call tool was called")

sim._client.conversational_ai.conversations.get.side_effect = get

with patch("eva.user_simulator.elevenlabs.asyncio.sleep", new_callable=AsyncMock):
assert await sim._check_end_call_via_api("conv-123") is True

assert call_threads and all(t != loop_thread for t in call_threads)

@pytest.mark.asyncio
async def test_audio_download_runs_in_a_thread_and_writes_the_recording(self, tmp_path):
sim = _make_simulator(tmp_path)
sim._client = MagicMock()
loop_thread = threading.get_ident()
call_threads = []

def get(conversation_id):
call_threads.append(threading.get_ident())
return iter([b"abc", b"def"])

sim._client.conversational_ai.conversations.audio.get.side_effect = get

await sim._fetch_elevenlabs_audio("conv-123")

assert call_threads and all(t != loop_thread for t in call_threads)
assert (tmp_path / "elevenlabs_audio_recording.mp3").read_bytes() == b"abcdef"

@pytest.mark.asyncio
async def test_audio_download_retries_when_recording_not_ready(self, tmp_path):
sim = _make_simulator(tmp_path)
sim._client = MagicMock()
sim._client.conversational_ai.conversations.audio.get.side_effect = [RuntimeError("not ready"), iter([b"ok"])]

with patch("eva.user_simulator.elevenlabs.asyncio.sleep", new_callable=AsyncMock) as sleep:
await sim._fetch_elevenlabs_audio("conv-123")

sleep.assert_awaited_once_with(2.0)
assert (tmp_path / "elevenlabs_audio_recording.mp3").read_bytes() == b"ok"
18 changes: 18 additions & 0 deletions tests/unit/utils/test_audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import audioop
import json
import math
import random
import struct

import pytest
Expand Down Expand Up @@ -70,6 +71,23 @@ def test_adds_samples_and_clips_at_int16_boundaries(self):
result = struct.unpack("<2h", mixed)
assert result == (5100, 200)

def test_matches_per_sample_reference_on_random_unequal_tracks(self):
"""The vectorized mix must equal a plain per-sample mix, for bytes and bytearray inputs."""

def reference(x: bytes, y: bytes) -> bytes:
n = max(len(x), len(y)) // 2
x, y = x.ljust(n * 2, b"\x00"), y.ljust(n * 2, b"\x00")
sa, sb = struct.unpack(f"<{n}h", x), struct.unpack(f"<{n}h", y)
return struct.pack(f"<{n}h", *(max(-32768, min(32767, p + q)) for p, q in zip(sa, sb)))

rng = random.Random(0)
a = struct.pack("<1000h", *(rng.randint(-32768, 32767) for _ in range(1000)))
b = struct.pack("<700h", *(rng.randint(-32768, 32767) for _ in range(700)))
assert pcm16_mix(a, b) == reference(a, b)
assert pcm16_mix(b, a) == reference(b, a)
assert pcm16_mix(bytearray(a), bytearray(b)) == reference(a, b)
assert pcm16_mix(b"", b"") == b""


class TestTwilioProtocol:
def test_create_and_parse_round_trip(self):
Expand Down
Loading