feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
+98
-19
@@ -2,8 +2,10 @@
|
||||
backend/app/api/chat.py
|
||||
|
||||
用途:聊天 BFF(SSE 流式、会话 CRUD、PLM/Excel 预览、反馈)。
|
||||
依赖:app.core.decorators、app.core.errors、services.dify、integrations。
|
||||
逻辑:/completions 按意图分流(Wave 3 抽至 services/chat/router.py);错误统一 raise AppError。
|
||||
依赖:app.core.decorators、app.core.errors、app.core.response、services.dify、integrations、
|
||||
shipping_notice.audit_service(货代会话删除审计)。
|
||||
逻辑:/completions 按意图分流(Wave 3 抽至 services/chat/router.py);错误统一 raise AppError;
|
||||
shipping_notice 会话删除写不可变审计并返回标准 envelope。
|
||||
"""
|
||||
import io
|
||||
import ipaddress
|
||||
@@ -28,7 +30,7 @@ from app.services.dify import DifyService
|
||||
from app.core.config.settings import Config
|
||||
from app.services.integration_api import IntegrationApiService
|
||||
from app.core.decorators import require_agent_permission
|
||||
from app.models import Agent, ChatLog, ChatSession, Feedback, ShippingOrderExtractionRun
|
||||
from app.models import Agent, ChatLog, ChatSession, Feedback, ShippingOrderProcessingRun
|
||||
from app import db
|
||||
from app.core.errors import (
|
||||
AppError,
|
||||
@@ -237,14 +239,14 @@ def _order_extraction_attention_by_conversation(
|
||||
'cancelled': 'error',
|
||||
}
|
||||
rows = db.session.query(
|
||||
ShippingOrderExtractionRun.conversation_id,
|
||||
ShippingOrderExtractionRun.status,
|
||||
ShippingOrderProcessingRun.conversation_id,
|
||||
ShippingOrderProcessingRun.status,
|
||||
).filter(
|
||||
ShippingOrderExtractionRun.user_id == user_id,
|
||||
ShippingOrderExtractionRun.owner_agent_id == agent_id,
|
||||
ShippingOrderExtractionRun.conversation_id.in_(conversation_ids),
|
||||
ShippingOrderExtractionRun.attention_generated_at.isnot(None),
|
||||
ShippingOrderExtractionRun.seen_at.is_(None),
|
||||
ShippingOrderProcessingRun.user_id == user_id,
|
||||
ShippingOrderProcessingRun.owner_agent_id == agent_id,
|
||||
ShippingOrderProcessingRun.conversation_id.in_(conversation_ids),
|
||||
ShippingOrderProcessingRun.attention_generated_at.isnot(None),
|
||||
ShippingOrderProcessingRun.seen_at.is_(None),
|
||||
).all()
|
||||
aggregate: dict[str, tuple[int, str]] = {}
|
||||
for conversation_id, status in rows:
|
||||
@@ -1087,30 +1089,75 @@ def get_chat_sessions():
|
||||
@require_agent_permission
|
||||
def delete_all_chat_sessions():
|
||||
"""
|
||||
删除当前用户与当前 Agent 的所有历史会话
|
||||
删除当前用户与当前 Agent 的所有历史会话。
|
||||
|
||||
核心逻辑:批量清理 ChatLog / Feedback / ChatSession;shipping_notice 写审计;
|
||||
返回标准 success envelope。
|
||||
|
||||
Returns:
|
||||
Flask JSON:`{ success, data: { deleted: true } }`。
|
||||
"""
|
||||
from app.core.response import ok
|
||||
|
||||
user = getattr(g, 'current_user', None)
|
||||
agent = getattr(g, 'current_agent', None)
|
||||
if user is None or agent is None:
|
||||
raise AppError(code='SYSTEM.INTERNAL_ERROR', message='Missing context', http_status=500)
|
||||
chat_logs = db.session.query(ChatLog).filter_by(user_id=user.id, agent_id=agent.id).all()
|
||||
chat_log_ids = [log.id for log in chat_logs]
|
||||
session_count = (
|
||||
db.session.query(ChatSession)
|
||||
.filter_by(user_id=user.id, agent_id=agent.id)
|
||||
.count()
|
||||
)
|
||||
if chat_log_ids:
|
||||
from app.models import GeneralFeedback
|
||||
db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False)
|
||||
db.session.query(GeneralFeedback).filter(GeneralFeedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False)
|
||||
db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False)
|
||||
db.session.query(ChatSession).filter_by(user_id=user.id, agent_id=agent.id).delete(synchronize_session=False)
|
||||
|
||||
if str(getattr(agent, 'code', None) or '').strip() == SHIPPING_NOTICE_AGENT_CODE:
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
|
||||
append_shipping_audit_event(
|
||||
event_type='conversation_sessions_deleted_all',
|
||||
aggregate_type='chat_session',
|
||||
aggregate_id=f'user:{int(user.id)}:agent:{int(agent.id)}',
|
||||
actor_type='user',
|
||||
actor_id=user.id,
|
||||
owner_user_id=user.id,
|
||||
action='delete_all_chat_sessions',
|
||||
reason_code='user_deleted_all_sessions',
|
||||
before={'session_count': int(session_count), 'chat_log_count': len(chat_log_ids)},
|
||||
after={'deleted': True},
|
||||
metadata={'agent_id': int(agent.id)},
|
||||
source='workspace_api',
|
||||
sensitivity_level='sensitive',
|
||||
request_id=str(getattr(g, 'request_id', '') or '') or None,
|
||||
)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
return ok({'deleted': True, 'session_count': int(session_count)})
|
||||
|
||||
@chat_bp.route('/sessions/<conversation_id>', methods=['DELETE'])
|
||||
@require_agent_permission
|
||||
def delete_chat_session(conversation_id):
|
||||
"""
|
||||
删除指定的会话记录
|
||||
删除指定的会话记录。
|
||||
|
||||
核心逻辑:清理 ChatLog / Feedback;shipping_notice Agent 额外写不可变审计;
|
||||
返回标准 success envelope。
|
||||
|
||||
Args:
|
||||
conversation_id: 会话 ID(路径参数)。
|
||||
|
||||
Returns:
|
||||
Flask JSON:`{ success, data: { deleted: true, conversation_id } }`。
|
||||
"""
|
||||
from app.core.response import ok
|
||||
|
||||
user = getattr(g, 'current_user', None)
|
||||
agent = getattr(g, 'current_agent', None)
|
||||
if user is None or agent is None:
|
||||
@@ -1134,6 +1181,9 @@ def delete_chat_session(conversation_id):
|
||||
).all()
|
||||
|
||||
chat_log_ids = [log.id for log in chat_logs]
|
||||
session_id_for_audit = str(
|
||||
getattr(session, 'session_id', None) or conversation_id or ''
|
||||
).strip()
|
||||
if chat_log_ids:
|
||||
from app.models import GeneralFeedback
|
||||
db.session.query(Feedback).filter(Feedback.chat_log_id.in_(chat_log_ids)).delete(synchronize_session=False)
|
||||
@@ -1141,9 +1191,38 @@ def delete_chat_session(conversation_id):
|
||||
db.session.query(ChatLog).filter(ChatLog.id.in_(chat_log_ids)).delete(synchronize_session=False)
|
||||
if session:
|
||||
db.session.delete(session)
|
||||
|
||||
# [BE-16] 货代会话删除必须留审计;与业务删除同事务提交。
|
||||
if str(getattr(agent, 'code', None) or '').strip() == SHIPPING_NOTICE_AGENT_CODE:
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
|
||||
append_shipping_audit_event(
|
||||
event_type='conversation_session_deleted',
|
||||
aggregate_type='chat_session',
|
||||
aggregate_id=session_id_for_audit or str(conversation_id),
|
||||
actor_type='user',
|
||||
actor_id=user.id,
|
||||
owner_user_id=user.id,
|
||||
conversation_id=session_id_for_audit or str(conversation_id),
|
||||
action='delete_chat_session',
|
||||
reason_code='user_deleted_session',
|
||||
before={
|
||||
'conversation_id': session_id_for_audit or str(conversation_id),
|
||||
'chat_log_count': len(chat_log_ids),
|
||||
},
|
||||
after={'deleted': True},
|
||||
metadata={'agent_id': int(agent.id), 'chat_log_count': len(chat_log_ids)},
|
||||
source='workspace_api',
|
||||
sensitivity_level='sensitive',
|
||||
request_id=str(getattr(g, 'request_id', '') or '') or None,
|
||||
)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
return ok({
|
||||
'deleted': True,
|
||||
'conversation_id': session_id_for_audit or str(conversation_id),
|
||||
})
|
||||
|
||||
@chat_bp.route('/messages', methods=['GET'])
|
||||
@require_agent_permission
|
||||
@@ -1180,11 +1259,11 @@ def get_chat_messages():
|
||||
if run_nos:
|
||||
order_extraction_runs = {
|
||||
run.run_no: run
|
||||
for run in db.session.query(ShippingOrderExtractionRun).filter(
|
||||
ShippingOrderExtractionRun.user_id == g.current_user.id,
|
||||
ShippingOrderExtractionRun.owner_agent_id == g.current_agent.id,
|
||||
ShippingOrderExtractionRun.conversation_id == (session.session_id if session else conversation_id),
|
||||
ShippingOrderExtractionRun.run_no.in_(run_nos),
|
||||
for run in db.session.query(ShippingOrderProcessingRun).filter(
|
||||
ShippingOrderProcessingRun.user_id == g.current_user.id,
|
||||
ShippingOrderProcessingRun.owner_agent_id == g.current_agent.id,
|
||||
ShippingOrderProcessingRun.conversation_id == (session.session_id if session else conversation_id),
|
||||
ShippingOrderProcessingRun.run_no.in_(run_nos),
|
||||
).all()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user