fix: AGNES图像/视频生成强制经智能路由代理(172.18.0.1:7890),修复容器内直连被重置导致图像生成异常

This commit is contained in:
QihuAI Design committed 2026-07-14 17:11:12 +08:00
1 parent 202a181ebe
commit ba09017884
1 file changed
+10 -2
+10 -2
View File
@@ -79,6 +79,14 @@ def _html_escape(s: str) -> str:
.replace('"', """).replace("'", "'")) .replace('"', """).replace("'", "'"))
def _outbound_proxy() -> str:
"""出网代理地址。容器内直连 apihub.agnes-ai.com 会被对端重置
(httpx RemoteProtocolError: Server disconnected without sending a response),
必须经 172.18.0.1:7890 智能路由代理(其对国内 AGNES 域名走直连)。"""
import os
return os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") or "http://172.18.0.1:7890"
async def _generate_media(db, ai_model, prompt: str, user_id: str = None) -> str: async def _generate_media(db, ai_model, prompt: str, user_id: str = None) -> str:
"""图像/视频生成模型统一入口:按 capabilities 调用 AGNES 等媒体生成 API。 """图像/视频生成模型统一入口:按 capabilities 调用 AGNES 等媒体生成 API。
返回可直接在 AI 聊天中渲染的富文本(含 <img>/<video> 标签)。""" 返回可直接在 AI 聊天中渲染的富文本(含 <img>/<video> 标签)。"""
@@ -105,7 +113,7 @@ async def _generate_media(db, ai_model, prompt: str, user_id: str = None) -> str
"extra_body": {"response_format": "url"}, "extra_body": {"response_format": "url"},
} }
try: try:
async with httpx.AsyncClient(timeout=timeout) as client: async with httpx.AsyncClient(timeout=timeout, proxy=_outbound_proxy()) as client:
resp = await client.post(api_url, headers=headers, json=payload) resp = await client.post(api_url, headers=headers, json=payload)
if resp.status_code != 200: if resp.status_code != 200:
return f"⚠️ 图像生成失败 (HTTP {resp.status_code}): {resp.text[:200]}" return f"⚠️ 图像生成失败 (HTTP {resp.status_code}): {resp.text[:200]}"
@@ -141,7 +149,7 @@ async def _generate_media(db, ai_model, prompt: str, user_id: str = None) -> str
"frame_rate": 24, "frame_rate": 24,
} }
try: try:
async with httpx.AsyncClient(timeout=timeout) as client: async with httpx.AsyncClient(timeout=timeout, proxy=_outbound_proxy()) as client:
resp = await client.post(api_url, headers=headers, json=payload) resp = await client.post(api_url, headers=headers, json=payload)
if resp.status_code != 200: if resp.status_code != 200:
return f"⚠️ 视频生成失败 (HTTP {resp.status_code}): {resp.text[:200]}" return f"⚠️ 视频生成失败 (HTTP {resp.status_code}): {resp.text[:200]}"