refactor(api): 统一响应格式迁移 + 异步任务系统 + 状态码体系完善
- 将全部路由文件(12个)的 jsonify 响应迁移至 success_response/error_response 统一格式 - 修复 sync_routes.py error_response 参数错误(P0) - 新增异步任务系统:task_registry + task_routes - 新增状态码:TASK_NOT_FOUND(4014)、TASK_CONFLICT(4015)、REINDEX_ERROR(5040) - 修正 task_routes/exam_pkg 中语义不匹配的状态码 - 更新 curl 测试手册、后端对接规范文档 - 添加缓存性能报告和 Redis 迁移计划
This commit is contained in:
@@ -37,6 +37,8 @@ import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from auth.gateway import require_gateway_auth
|
||||
from core.status_codes import SUCCESS, BAD_REQUEST, NOT_FOUND, INTERNAL_ERROR
|
||||
from api.response_utils import success_response, error_response
|
||||
|
||||
from auth.security import validate_query, filter_response
|
||||
from config import RAG_CHAT_MODEL
|
||||
@@ -1238,12 +1240,12 @@ def chat():
|
||||
history = data.get('history', [])
|
||||
|
||||
if not message:
|
||||
return jsonify({"error": "缺少 message"}), 400
|
||||
return error_response("MISSING_PARAMS", BAD_REQUEST, "缺少 message", http_status=400)
|
||||
|
||||
# 输入安全验证
|
||||
is_valid, reason = validate_query(message)
|
||||
if not is_valid:
|
||||
return jsonify({"error": reason}), 400
|
||||
return error_response("INVALID_QUERY", BAD_REQUEST, reason, http_status=400)
|
||||
|
||||
# 智能聊天
|
||||
result = chat_with_llm(message, history)
|
||||
@@ -1251,7 +1253,7 @@ def chat():
|
||||
# 过滤敏感信息
|
||||
answer = filter_response(result["answer"])
|
||||
|
||||
return jsonify({
|
||||
return success_response(data={
|
||||
"answer": answer,
|
||||
"mode": "chat",
|
||||
"sources": result.get("sources", []),
|
||||
@@ -1309,19 +1311,16 @@ def rag():
|
||||
session_id = data.get('session_id')
|
||||
|
||||
if not message:
|
||||
return jsonify({"error": "缺少 message"}), 400
|
||||
return error_response("MISSING_PARAMS", BAD_REQUEST, "缺少 message", http_status=400)
|
||||
|
||||
# 生产环境强制校验 chat_history
|
||||
if IS_PROD and history is None:
|
||||
return jsonify({
|
||||
"error": "chat_history is required in production",
|
||||
"code": "MISSING_HISTORY"
|
||||
}), 400
|
||||
return error_response("MISSING_HISTORY", BAD_REQUEST, "chat_history is required in production", http_status=400)
|
||||
|
||||
# 输入安全验证
|
||||
is_valid, reason = validate_query(message)
|
||||
if not is_valid:
|
||||
return jsonify({"error": reason}), 400
|
||||
return error_response("INVALID_QUERY", BAD_REQUEST, reason, http_status=400)
|
||||
|
||||
# 如果没有指定 collections,使用默认的公开库
|
||||
if not collections:
|
||||
@@ -2048,12 +2047,12 @@ def search():
|
||||
collections = data.get('collections') # 后端传入的知识库列表
|
||||
|
||||
if not query:
|
||||
return jsonify({'error': 'query is required'}), 400
|
||||
return error_response("MISSING_PARAMS", BAD_REQUEST, "query is required", http_status=400)
|
||||
|
||||
# 输入安全校验(注入检测、违禁词、长度限制)
|
||||
is_valid, reason = validate_query(query)
|
||||
if not is_valid:
|
||||
return jsonify({'error': reason}), 400
|
||||
return error_response("INVALID_QUERY", BAD_REQUEST, reason, http_status=400)
|
||||
|
||||
# top_k 范围校验
|
||||
try:
|
||||
@@ -2067,7 +2066,7 @@ def search():
|
||||
|
||||
results = search_hybrid(query, top_k=top_k, allowed_collections=collections)
|
||||
|
||||
return jsonify({
|
||||
return success_response(data={
|
||||
'contexts': results['documents'][0],
|
||||
'metadatas': results['metadatas'][0],
|
||||
'scores': results['scores'][0]
|
||||
|
||||
Reference in New Issue
Block a user