清理 Ollama/API URL 中的终端控制字符,修复 AI 调用失败。

This commit is contained in:
dekun
2026-06-28 15:42:28 +08:00
parent e5ff76c20b
commit 23be608521
3 changed files with 38 additions and 12 deletions
+24
View File
@@ -0,0 +1,24 @@
import re
# ANSI 颜色/光标控制序列(粘贴终端输出时常见)
_ANSI_ESCAPE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07]*\x07|\x1b[@-Z\\-_]")
# 其它不可见控制字符(保留普通 URL 字符)
_CTRL_CHARS = re.compile(r"[\x00-\x1f\x7f]")
def sanitize_http_url(url: str | None) -> str:
"""去掉 URL 中的 ANSI/控制字符,避免 httpx Invalid non-printable ASCII character。"""
if not url:
return ""
cleaned = _ANSI_ESCAPE.sub("", url)
cleaned = _CTRL_CHARS.sub("", cleaned)
return cleaned.strip()
def sanitize_model_name(name: str | None) -> str:
if not name:
return ""
cleaned = _ANSI_ESCAPE.sub("", name)
cleaned = _CTRL_CHARS.sub("", cleaned)
return cleaned.strip()