fix: 修复已知问题

This commit is contained in:
hp0912 2026-05-02 01:03:10 +08:00
parent 40d6077398
commit b7918ca615

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import argparse import argparse
import base64 import base64
import gzip
import json import json
import os import os
import subprocess import subprocess
@ -14,6 +15,7 @@ import urllib.error
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import uuid import uuid
import zlib
from pathlib import Path from pathlib import Path
sys.stderr = sys.stdout sys.stderr = sys.stdout
@ -619,6 +621,35 @@ def _build_mimo_payload(config: dict, params: dict) -> tuple[dict, str, bool]:
return payload, audio_format, stream return payload, audio_format, stream
def _decompress_response_bytes(raw: bytes, encoding: str) -> bytes:
encoding = (encoding or "").strip().lower()
if not encoding or encoding == "identity":
return raw
if encoding == "gzip":
return gzip.decompress(raw)
if encoding == "deflate":
try:
return zlib.decompress(raw)
except zlib.error:
return zlib.decompress(raw, -zlib.MAX_WBITS)
if encoding == "br":
try:
import brotli # type: ignore
except ModuleNotFoundError as exc:
raise RuntimeError(
"mimo 响应使用了 brotli 压缩,但当前环境未安装 brotli请安装后重试"
) from exc
return brotli.decompress(raw)
raise RuntimeError(f"mimo 响应使用了不支持的 Content-Encoding: {encoding}")
def _read_response_text(response) -> str:
raw = response.read()
encoding = response.headers.get("Content-Encoding", "")
raw = _decompress_response_bytes(raw, encoding)
return raw.decode("utf-8", errors="replace")
def _decode_mimo_audio(audio_b64: object, audio_format: str) -> tuple[bytes, str]: def _decode_mimo_audio(audio_b64: object, audio_format: str) -> tuple[bytes, str]:
if not isinstance(audio_b64, str) or not audio_b64: if not isinstance(audio_b64, str) or not audio_b64:
raise RuntimeError("mimo 响应未包含音频数据") raise RuntimeError("mimo 响应未包含音频数据")
@ -632,7 +663,7 @@ def _decode_mimo_audio(audio_b64: object, audio_format: str) -> tuple[bytes, str
def _read_mimo_non_stream_response(response, audio_format: str) -> tuple[bytes, str]: def _read_mimo_non_stream_response(response, audio_format: str) -> tuple[bytes, str]:
raw_body = response.read().decode("utf-8", errors="replace") raw_body = _read_response_text(response)
try: try:
payload = json.loads(raw_body) payload = json.loads(raw_body)
except json.JSONDecodeError as exc: except json.JSONDecodeError as exc:
@ -703,6 +734,8 @@ def synthesize_audio_mimo(config: dict, params: dict) -> tuple[bytes, str]:
headers={ headers={
"Content-Type": "application/json", "Content-Type": "application/json",
"api-key": api_key, "api-key": api_key,
"Accept": "application/json, text/event-stream",
"Accept-Encoding": "identity",
}, },
method="POST", method="POST",
) )
@ -710,6 +743,9 @@ def synthesize_audio_mimo(config: dict, params: dict) -> tuple[bytes, str]:
try: try:
response = urllib.request.urlopen(req, timeout=300) response = urllib.request.urlopen(req, timeout=300)
except urllib.error.HTTPError as exc: except urllib.error.HTTPError as exc:
try:
error_body = _read_response_text(exc)
except Exception:
error_body = exc.read().decode("utf-8", errors="replace") error_body = exc.read().decode("utf-8", errors="replace")
raise RuntimeError(f"mimo API请求失败状态码 {exc.code}: {error_body}") from exc raise RuntimeError(f"mimo API请求失败状态码 {exc.code}: {error_body}") from exc
except urllib.error.URLError as exc: except urllib.error.URLError as exc: