Merge branch 'runny&xinxin' into dev
This commit is contained in:
commit
8d73343bd8
77 files changed
+17955
-274
No files matched your search
@@ -38,22 +38,33 @@ class DifyService:
|
||||
"""Dify service wrapper."""
|
||||
|
||||
@staticmethod
|
||||
def build_chat_messages_url(raw_base_url: str) -> str:
|
||||
def _build_chat_messages_url(base_url: str) -> str:
|
||||
"""
|
||||
统一构建 Dify chat-messages URL,兼容以下输入:
|
||||
- http(s)://host
|
||||
- http(s)://host/v1
|
||||
- http(s)://host/chat-messages
|
||||
- http(s)://host/v1/chat-messages
|
||||
Build the Dify chat-messages endpoint URL from a base URL.
|
||||
|
||||
Our config/agent base_url may be:
|
||||
- https://api.dify.ai/v1
|
||||
- http://host:port
|
||||
- http://host:port/v1
|
||||
- (user mistake) http://host:port/v1/chat-messages
|
||||
|
||||
This helper normalizes common mistakes and prevents duplicate `/v1/v1/...`.
|
||||
"""
|
||||
base_url = (raw_base_url or '').strip().rstrip('/')
|
||||
if not base_url:
|
||||
return ''
|
||||
if base_url.endswith('/v1/chat-messages') or base_url.endswith('/chat-messages'):
|
||||
return base_url
|
||||
if base_url.endswith('/v1'):
|
||||
return f'{base_url}/chat-messages'
|
||||
return f'{base_url}/v1/chat-messages'
|
||||
base = str(base_url or "").strip().rstrip("/")
|
||||
if not base:
|
||||
# Keep old behavior: downstream will fail with a request error.
|
||||
return "/v1/chat-messages"
|
||||
|
||||
# User mistake hardening: strip endpoint suffix if someone stored it as base_url.
|
||||
if base.endswith("/v1/chat-messages"):
|
||||
base = base[: -len("/v1/chat-messages")].rstrip("/")
|
||||
elif base.endswith("/chat-messages"):
|
||||
base = base[: -len("/chat-messages")].rstrip("/")
|
||||
|
||||
# Config.DIFY_API_BASE defaults to ".../v1". Avoid double "/v1".
|
||||
if base.endswith("/v1"):
|
||||
return f"{base}/chat-messages"
|
||||
return f"{base}/v1/chat-messages"
|
||||
|
||||
@staticmethod
|
||||
def stream_chat(query: str, user_id: int, agent, conversation_id: str = None):
|
||||
@@ -78,7 +89,7 @@ class DifyService:
|
||||
"""
|
||||
# Prefer agent-specific base URL; fallback to global config.
|
||||
base_url = agent.api_base_url if agent.api_base_url else Config.DIFY_API_BASE
|
||||
url = DifyService.build_chat_messages_url(base_url)
|
||||
url = DifyService._build_chat_messages_url(base_url)
|
||||
|
||||
headers = {
|
||||
'Authorization': f'Bearer {agent.dify_api_key}',
|
||||
|
||||
Reference in new issue
Block a user