25 lines
764 B
Python
25 lines
764 B
Python
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()
|