Skip to content

Commit 604ecd8

Browse files
RhoninRhonin
authored andcommitted
fix(provider): harden StepFun ASR parsing
1 parent 523eefd commit 604ecd8

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

astrbot/core/provider/sources/stepfun_asr_source.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import uuid
44
from pathlib import Path
5-
from urllib.parse import urlparse
5+
from urllib.parse import urlparse, urlunparse
66

77
import httpx
88

@@ -64,13 +64,24 @@ def build_headers(api_key: str) -> dict[str, str]:
6464
return headers
6565

6666

67+
def sanitize_proxy_url(proxy: str) -> str:
68+
try:
69+
parsed = urlparse(proxy)
70+
if "@" not in parsed.netloc:
71+
return proxy
72+
sanitized_netloc = parsed.netloc.split("@", 1)[1]
73+
return urlunparse(parsed._replace(netloc=sanitized_netloc))
74+
except Exception:
75+
return "<redacted>"
76+
77+
6778
def create_http_client(timeout: int | None, proxy: str) -> httpx.AsyncClient:
6879
client_kwargs: dict[str, object] = {
6980
"timeout": timeout,
7081
"follow_redirects": True,
7182
}
7283
if proxy:
73-
logger.info("[StepFun ASR] Using proxy: %s", proxy)
84+
logger.info("[StepFun ASR] Using proxy: %s", sanitize_proxy_url(proxy))
7485
client_kwargs["proxy"] = proxy
7586
return httpx.AsyncClient(**client_kwargs)
7687

@@ -179,7 +190,8 @@ def cleanup_files(paths: list[Path]) -> None:
179190

180191

181192
def _iter_sse_payloads(content: str):
182-
for event in content.split("\n\n"):
193+
normalized_content = content.replace("\r\n", "\n")
194+
for event in normalized_content.split("\n\n"):
183195
data_lines = []
184196
for line in event.splitlines():
185197
if line.startswith("data:"):
@@ -230,6 +242,8 @@ def parse_sse_transcription(content: str) -> str:
230242
data = json.loads(payload)
231243
except json.JSONDecodeError:
232244
continue
245+
if not isinstance(data, dict):
246+
continue
233247

234248
event_type = str(data.get("type") or data.get("event") or "")
235249
error = data.get("error")

0 commit comments

Comments
 (0)