fix(platform): 合并本地修复到测试环境
This commit is contained in:
commit
975e8bbcf5
41 files changed
+4564
-768
No files matched your search
@@ -134,6 +134,38 @@ class DifyService:
|
||||
return "当前会话上下文过长,模型无法继续回答。请新建对话后重新查询。"
|
||||
return f"{resolved_agent_name}暂时无法完成查询,请稍后重试。"
|
||||
|
||||
@staticmethod
|
||||
def _friendly_upstream_error_message(status_code: int, raw_body: str, agent_name: str | None = None) -> str:
|
||||
"""将 Dify HTTP 错误转换为可执行的用户提示。
|
||||
|
||||
AI 触发时机:Dify 在建立 SSE 流前直接返回非 2xx 响应时调用。
|
||||
核心逻辑:优先识别知识检索常用的 rerank 插件授权失败,避免把上游 403 误报为网络超时;
|
||||
其余状态码则给出不泄露密钥和内部地址的通用排查方向。
|
||||
|
||||
Args:
|
||||
status_code: Dify HTTP 响应状态码。
|
||||
raw_body: 已截断的 Dify 错误正文,仅用于分类,不向用户原样透传。
|
||||
agent_name: 当前智能体名称。
|
||||
|
||||
Returns:
|
||||
面向最终用户的安全错误提示。
|
||||
"""
|
||||
text = str(raw_body or "")
|
||||
lower = text.lower()
|
||||
resolved_agent_name = str(agent_name or "").strip() or "当前智能体"
|
||||
if "rerank" in lower and ("403" in lower or "forbidden" in lower):
|
||||
return (
|
||||
f"{resolved_agent_name}的知识检索重排服务授权失败。"
|
||||
"请联系管理员在 Dify 中更新 Rerank 模型供应商的 API Key 后重新发布应用。"
|
||||
)
|
||||
if status_code in (401, 403):
|
||||
return f"{resolved_agent_name}暂时无法访问 Dify 服务,请联系管理员检查该应用的 API Key 与服务授权。"
|
||||
if status_code == 404:
|
||||
return f"{resolved_agent_name}的 Dify 应用地址或应用类型配置错误,请联系管理员检查 API Base URL。"
|
||||
if status_code in (429, 502, 503, 504):
|
||||
return f"{resolved_agent_name}的 Dify 服务暂时不可用或繁忙,请稍后重试。"
|
||||
return f"{resolved_agent_name}暂时无法完成查询,请稍后重试;如持续失败请联系管理员检查 Dify 工作流。"
|
||||
|
||||
@staticmethod
|
||||
def _merge_stream_answer_text(
|
||||
streamed_text: str,
|
||||
@@ -1028,6 +1060,23 @@ class DifyService:
|
||||
if retryable and attempt < max_retries:
|
||||
time.sleep(attempt * 2)
|
||||
continue
|
||||
if not response.ok:
|
||||
message = DifyService._friendly_upstream_error_message(
|
||||
response.status_code,
|
||||
response.text[:2000],
|
||||
getattr(agent, 'name', None),
|
||||
)
|
||||
persist_stream_chat(
|
||||
{
|
||||
'event': 'error',
|
||||
'conversation_id': normalized_conversation_id,
|
||||
'answer': f"[Error: {message}]",
|
||||
'metadata': {},
|
||||
},
|
||||
'error',
|
||||
)
|
||||
yield f"data: {json.dumps({'event': 'error', 'message': message}, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
response.raise_for_status()
|
||||
yield f"data: {json.dumps({'event': 'upstream_connected', 'message': 'Dify 已连接'}, ensure_ascii=False)}\n\n"
|
||||
|
||||
|
||||
Reference in new issue
Block a user