fix: improve streaming, alarms, and AI TTS
This commit is contained in:
@@ -283,11 +283,7 @@ def _extract_response_content(cfg, data: dict) -> str:
|
||||
|
||||
|
||||
def _iter_openai_compatible_stream(response):
|
||||
for line in response.iter_lines(decode_unicode=True):
|
||||
if not line or not line.startswith("data:"):
|
||||
continue
|
||||
|
||||
data_str = line[5:].strip()
|
||||
for data_str in _iter_sse_data_lines(response):
|
||||
if data_str == "[DONE]":
|
||||
break
|
||||
|
||||
@@ -317,11 +313,7 @@ def _iter_openai_compatible_stream(response):
|
||||
|
||||
|
||||
def _iter_anthropic_stream(response):
|
||||
for line in response.iter_lines(decode_unicode=True):
|
||||
if not line or not line.startswith("data:"):
|
||||
continue
|
||||
|
||||
data_str = line[5:].strip()
|
||||
for data_str in _iter_sse_data_lines(response):
|
||||
if data_str == "[DONE]":
|
||||
break
|
||||
|
||||
@@ -344,6 +336,28 @@ def _iter_anthropic_stream(response):
|
||||
yield str(text)
|
||||
|
||||
|
||||
def _iter_sse_data_lines(response):
|
||||
"""
|
||||
Читает SSE-стрим и возвращает только payload после "data:".
|
||||
Явно декодируем как UTF-8, чтобы избежать mojibake вида "ÐÑ...".
|
||||
"""
|
||||
for raw_line in response.iter_lines(decode_unicode=False):
|
||||
if not raw_line:
|
||||
continue
|
||||
|
||||
if isinstance(raw_line, bytes):
|
||||
line = raw_line.decode("utf-8", errors="replace")
|
||||
else:
|
||||
line = str(raw_line)
|
||||
|
||||
if not line.startswith("data:"):
|
||||
continue
|
||||
|
||||
data_str = line[5:].strip()
|
||||
if data_str:
|
||||
yield data_str
|
||||
|
||||
|
||||
def _iter_stream_chunks(cfg, response):
|
||||
if cfg["protocol"] == "anthropic":
|
||||
yield from _iter_anthropic_stream(response)
|
||||
|
||||
Reference in New Issue
Block a user