fix: 支持原文件下载,同时将postgres地址由localhost变更为8.152.207.171
This commit is contained in:
1 parent
70f95e9f5c
commit
c380248502
12 files changed
+1273
-273
No files matched your search
+50
-1
@@ -8,6 +8,7 @@ backend/app/api/chat.py
|
||||
from flask import Blueprint, request, Response, stream_with_context, jsonify, g, current_app
|
||||
from sqlalchemy import func, desc
|
||||
from app.services.dify import DifyService
|
||||
from app.services.integration_api import IntegrationApiService
|
||||
from app.decorators import require_agent_permission
|
||||
from app.models import Agent, ChatLog, Feedback
|
||||
from app import db
|
||||
@@ -85,7 +86,12 @@ def get_chat_messages():
|
||||
messages = []
|
||||
for log in logs:
|
||||
messages.append({'role': 'user', 'content': log.query})
|
||||
messages.append({'role': 'assistant', 'content': log.answer})
|
||||
messages.append({
|
||||
'role': 'assistant',
|
||||
'content': log.answer,
|
||||
'retriever_resources': log.retriever_resources or [],
|
||||
'attribution_parts': log.attribution_parts or []
|
||||
})
|
||||
|
||||
return jsonify(messages)
|
||||
|
||||
@@ -116,8 +122,17 @@ def chat_completions():
|
||||
user = getattr(g, 'current_user', None)
|
||||
agent = getattr(g, 'current_agent', None)
|
||||
|
||||
current_app.logger.info(
|
||||
"[chat-api] /completions request user_id=%s agent_id=%s query_len=%s conversation_id=%s",
|
||||
getattr(user, 'id', None),
|
||||
getattr(agent, 'id', None),
|
||||
len(query or ''),
|
||||
conversation_id,
|
||||
)
|
||||
|
||||
# 安全兜底:如果装饰器未正确注入,返回错误
|
||||
if user is None or agent is None:
|
||||
current_app.logger.error("[chat-api] missing user/agent context user=%s agent=%s", bool(user), bool(agent))
|
||||
return jsonify({'error': 'Missing user or agent context'}), 500
|
||||
|
||||
# 返回 SSE 响应,mimetype 设为 text/event-stream
|
||||
@@ -128,6 +143,8 @@ def chat_completions():
|
||||
mimetype='text/event-stream',
|
||||
headers={
|
||||
'Cache-Control': 'no-cache',
|
||||
'Content-Type': 'text/event-stream; charset=utf-8',
|
||||
'Connection': 'keep-alive',
|
||||
# 对于 Nginx 反向代理,需要关闭缓冲以支持实时流式传输
|
||||
'X-Accel-Buffering': 'no'
|
||||
}
|
||||
@@ -193,3 +210,35 @@ def submit_feedback():
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@chat_bp.route('/source-file', methods=['GET'])
|
||||
@require_agent_permission
|
||||
def resolve_source_file():
|
||||
"""
|
||||
Resolve original source download URL by Dify dataset/document IDs.
|
||||
"""
|
||||
dataset_id = (request.args.get('dataset_id') or '').strip()
|
||||
remote_document_id = (request.args.get('remote_document_id') or '').strip()
|
||||
if not dataset_id or not remote_document_id:
|
||||
current_app.logger.warning(
|
||||
"[source-file] missing required params: dataset_id=%r remote_document_id_present=%s remote_document_id_preview=%r query_args=%s",
|
||||
dataset_id,
|
||||
bool(remote_document_id),
|
||||
(remote_document_id[:8] + "...") if remote_document_id else "",
|
||||
dict(request.args),
|
||||
)
|
||||
return jsonify({'error': 'dataset_id and remote_document_id are required'}), 400
|
||||
|
||||
payload = IntegrationApiService.lookup_file_by_dify(dataset_id, remote_document_id)
|
||||
if not payload:
|
||||
return jsonify({'error': 'source file mapping not found'}), 404
|
||||
|
||||
return jsonify({
|
||||
'dataset_id': dataset_id,
|
||||
'remote_document_id': remote_document_id,
|
||||
'source_filename': payload.get('source_filename'),
|
||||
'source_ext': payload.get('source_ext'),
|
||||
'source_download_url': payload.get('source_download_url'),
|
||||
'download_url_expires_at': payload.get('download_url_expires_at'),
|
||||
})
|
||||
Reference in new issue
Block a user