Enhance session management and logging configuration

- Updated .env.example to clarify audio frame size validation and default codec settings.
- Refactored logging setup in main.py to support JSON serialization based on log format configuration.
- Improved session.py to dynamically compute audio frame bytes and include protocol version in session events.
- Added tests to validate session start events and audio frame handling based on chunk size settings.
This commit is contained in:
Xin Wang
2026-03-05 21:44:23 +08:00
parent 1cecbaa172
commit 6b589a1b7c
4 changed files with 105 additions and 17 deletions

View File

@@ -290,6 +290,8 @@ async def test_handle_session_start_applies_whitelisted_overrides_and_ignores_wo
@pytest.mark.asyncio
async def test_handle_session_start_emits_config_resolved_when_enabled(monkeypatch):
monkeypatch.setattr("core.session.settings.ws_emit_config_resolved", True)
monkeypatch.setattr("core.session.settings.ws_protocol_version", "v1-custom")
monkeypatch.setattr("core.session.settings.default_codec", "pcmu")
session = Session.__new__(Session)
session.id = "sess_start_emit_config"
@@ -368,10 +370,46 @@ async def test_handle_session_start_emits_config_resolved_when_enabled(monkeypat
)
config_event = next(item for item in events if item.get("type") == "config.resolved")
session_started_event = next(item for item in events if item.get("type") == "session.started")
assert session_started_event["protocolVersion"] == "v1-custom"
assert "appId" not in config_event["config"]
assert "configVersionId" not in config_event["config"]
assert "services" not in config_event["config"]
assert config_event["config"]["protocolVersion"] == "v1-custom"
assert config_event["config"]["channel"] == "web_debug"
assert config_event["config"]["output"]["mode"] == "text"
assert config_event["config"]["output"]["codec"] == "pcmu"
assert config_event["config"]["tools"]["enabled"] is True
assert config_event["config"]["tools"]["count"] == 1
@pytest.mark.asyncio
async def test_handle_audio_uses_chunk_size_for_frame_validation(monkeypatch):
monkeypatch.setattr("core.session.settings.sample_rate", 16000)
monkeypatch.setattr("core.session.settings.chunk_size_ms", 10)
session = Session.__new__(Session)
session.id = "sess_chunk_frame"
session.ws_state = WsSessionState.ACTIVE
class _Pipeline:
def __init__(self):
self.frames = []
async def process_audio(self, frame: bytes):
self.frames.append(frame)
session.pipeline = _Pipeline()
errors = []
async def _send_error(sender, message, code, **kwargs):
_ = (sender, kwargs)
errors.append((code, message))
session._send_error = _send_error
payload = b"\x00\x01" * 320 # 640 bytes = 2 frames when chunk_size_ms=10
await session.handle_audio(payload)
assert errors == []
assert len(session.pipeline.frames) == 2
assert all(len(frame) == 320 for frame in session.pipeline.frames)