Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(voice): preserve first SSE event after UTF-8 BOM

Rudy Celeklifc5dbf65ae29
diff --git a/app/services/voice.py b/app/services/voice.py
index e71e3710..0e93e2ae 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -3058,7 +3058,12 @@ def _iter_voxcpm_sse_events(response):
event_data = []
event_bytes = 0
+ first_line = True
for line in lines():
+ if first_line:
+ # SSE UTF-8 decoding ignores one BOM at the start of the stream.
+ line = line.removeprefix("\ufeff")
+ first_line = False
if not line:
if event_data:
yield parse_event(event_data)
diff --git a/test/services/test_voxcpm_sse_leading_bom.py b/test/services/test_voxcpm_sse_leading_bom.py
new file mode 100644
index 00000000..c6944586
--- /dev/null
+++ b/test/services/test_voxcpm_sse_leading_bom.py
@@ -0,0 +1,15 @@
+from types import SimpleNamespace
+
+import pytest
+
+from app.services import voice
+
+
[email protected]('split', [1, 2, 3, 9])
+def test_voxcpm_stream_keeps_first_event_after_utf8_bom(split):
+ content = b'\xef\xbb\xbfdata: {"type":"speech.audio.delta","data":"YWJj"}\n\ndata: {"type":"speech.audio.done"}\n\n'
+ response = SimpleNamespace(iter_content=lambda chunk_size: iter([content[:split], content[split:]]))
+ assert list(voice._iter_voxcpm_sse_events(response)) == [
+ {"type": "speech.audio.delta", "data": "YWJj"},
+ {"type": "speech.audio.done"},
+ ]