|
2 | 2 | import json |
3 | 3 | import uuid |
4 | 4 | from pathlib import Path |
5 | | -from urllib.parse import urlparse |
| 5 | +from urllib.parse import urlparse, urlunparse |
6 | 6 |
|
7 | 7 | import httpx |
8 | 8 |
|
@@ -64,13 +64,24 @@ def build_headers(api_key: str) -> dict[str, str]: |
64 | 64 | return headers |
65 | 65 |
|
66 | 66 |
|
| 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 | + |
67 | 78 | def create_http_client(timeout: int | None, proxy: str) -> httpx.AsyncClient: |
68 | 79 | client_kwargs: dict[str, object] = { |
69 | 80 | "timeout": timeout, |
70 | 81 | "follow_redirects": True, |
71 | 82 | } |
72 | 83 | if proxy: |
73 | | - logger.info("[StepFun ASR] Using proxy: %s", proxy) |
| 84 | + logger.info("[StepFun ASR] Using proxy: %s", sanitize_proxy_url(proxy)) |
74 | 85 | client_kwargs["proxy"] = proxy |
75 | 86 | return httpx.AsyncClient(**client_kwargs) |
76 | 87 |
|
@@ -179,7 +190,8 @@ def cleanup_files(paths: list[Path]) -> None: |
179 | 190 |
|
180 | 191 |
|
181 | 192 | 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"): |
183 | 195 | data_lines = [] |
184 | 196 | for line in event.splitlines(): |
185 | 197 | if line.startswith("data:"): |
@@ -230,6 +242,8 @@ def parse_sse_transcription(content: str) -> str: |
230 | 242 | data = json.loads(payload) |
231 | 243 | except json.JSONDecodeError: |
232 | 244 | continue |
| 245 | + if not isinstance(data, dict): |
| 246 | + continue |
233 | 247 |
|
234 | 248 | event_type = str(data.get("type") or data.get("event") or "") |
235 | 249 | error = data.get("error") |
|
0 commit comments