清理 Ollama/API URL 中的终端控制字符,修复 AI 调用失败。
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user