feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制
This commit is contained in:
1 parent
ce26d6d0c4
commit
c4095b08da
175 files changed
+13064
-1814
No files matched your search
+83
-2
@@ -28,7 +28,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
|
||||
from app.models import Agent, ChatLog, ChatSession, Feedback, ShippingOrderExtractionRun
|
||||
from app import db
|
||||
from app.core.errors import (
|
||||
AppError,
|
||||
@@ -209,7 +209,13 @@ def _user_facing_chat_query(text: str, *, agent_code: str | None = None) -> str:
|
||||
return value
|
||||
|
||||
|
||||
def _serialize_session(session: ChatSession) -> dict:
|
||||
def _serialize_session(
|
||||
session: ChatSession,
|
||||
*,
|
||||
unseen_order_extraction_count: int = 0,
|
||||
unseen_order_extraction_severity: str | None = None,
|
||||
) -> dict:
|
||||
"""输出会话摘要及订单识别待查看聚合,不返回 Run 内容或错误正文。"""
|
||||
last_active = session.last_active_at or session.created_at
|
||||
return {
|
||||
'id': session.id,
|
||||
@@ -217,9 +223,38 @@ def _serialize_session(session: ChatSession) -> dict:
|
||||
'title': session.title or '',
|
||||
'created_at': last_active.isoformat() if last_active else '',
|
||||
'status': session.status,
|
||||
'unseen_order_extraction_count': max(0, int(unseen_order_extraction_count or 0)),
|
||||
'unseen_order_extraction_severity': unseen_order_extraction_severity,
|
||||
}
|
||||
|
||||
|
||||
def _order_extraction_attention_by_conversation(*, user_id: int, agent_id: int, conversation_ids: set[str]) -> dict[str, tuple[int, str]]:
|
||||
"""聚合阶段4之后生成、且当前用户尚未确认的终态订单识别提醒。"""
|
||||
if not conversation_ids:
|
||||
return {}
|
||||
severity_rank = {'info': 1, 'warning': 2, 'error': 3}
|
||||
status_severity = {'completed': 'info', 'needs_review': 'warning', 'failed': 'error', 'cancelled': 'error'}
|
||||
rows = db.session.query(
|
||||
ShippingOrderExtractionRun.conversation_id,
|
||||
ShippingOrderExtractionRun.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),
|
||||
).all()
|
||||
aggregate: dict[str, tuple[int, str]] = {}
|
||||
for conversation_id, status in rows:
|
||||
severity = status_severity.get(str(status or ''), 'info')
|
||||
current_count, current_severity = aggregate.get(conversation_id, (0, severity))
|
||||
aggregate[conversation_id] = (
|
||||
current_count + 1,
|
||||
severity if severity_rank[severity] >= severity_rank[current_severity] else current_severity,
|
||||
)
|
||||
return aggregate
|
||||
|
||||
|
||||
def _get_chat_session(user_id: int, agent_id: int, session_id: str | None):
|
||||
session_key = str(session_id or '').strip()
|
||||
if not session_key:
|
||||
@@ -1035,6 +1070,15 @@ def get_chat_sessions():
|
||||
})
|
||||
|
||||
sessions.sort(key=lambda item: item.get('created_at') or '', reverse=True)
|
||||
attention_by_conversation = _order_extraction_attention_by_conversation(
|
||||
user_id=user.id,
|
||||
agent_id=agent.id,
|
||||
conversation_ids={str(item['conversation_id']) for item in sessions if item.get('conversation_id')},
|
||||
)
|
||||
for item in sessions:
|
||||
count, severity = attention_by_conversation.get(str(item.get('conversation_id') or ''), (0, None))
|
||||
item['unseen_order_extraction_count'] = count
|
||||
item['unseen_order_extraction_severity'] = severity
|
||||
return jsonify(sessions[:20])
|
||||
|
||||
@chat_bp.route('/sessions', methods=['DELETE'])
|
||||
@@ -1119,6 +1163,29 @@ def get_chat_messages():
|
||||
ChatLog.conversation_id == conversation_id,
|
||||
).order_by(ChatLog.created_at.asc()).all()
|
||||
|
||||
def _is_order_extraction_state_card(payload):
|
||||
"""仅识别正式 Run 状态卡,避免用户上传记录在历史回放中被投影成重复助手卡。"""
|
||||
if not isinstance(payload, dict):
|
||||
return False
|
||||
run_no = str(payload.get('order_extraction_run_no') or '').strip()
|
||||
return bool(run_no and payload.get('formId') == f'shipping-order-extraction-run:{run_no}')
|
||||
|
||||
run_nos = {
|
||||
str(log.payload.get('order_extraction_run_no') or '').strip()
|
||||
for log in logs
|
||||
if _is_order_extraction_state_card(log.payload)
|
||||
}
|
||||
order_extraction_runs = {}
|
||||
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),
|
||||
).all()
|
||||
}
|
||||
messages = []
|
||||
agent_code = str(getattr(g.current_agent, 'agent_code', '') or '').strip() or 'generic'
|
||||
for log in logs:
|
||||
@@ -1129,11 +1196,25 @@ def get_chat_messages():
|
||||
for key, value in raw_payload.items()
|
||||
if key != 'user_attachments'
|
||||
}
|
||||
run_no = str(assistant_payload.get('order_extraction_run_no') or '').strip()
|
||||
is_order_extraction_state_card = _is_order_extraction_state_card(raw_payload)
|
||||
if is_order_extraction_state_card and run_no in order_extraction_runs:
|
||||
from app.services.shipping_notice.order_extraction_run_service import serialize_order_extraction_message_state
|
||||
|
||||
assistant_payload['shipping_order_extraction_state'] = serialize_order_extraction_message_state(
|
||||
order_extraction_runs[run_no],
|
||||
)
|
||||
if log.query or user_attachments:
|
||||
user_msg = {'role': 'user', 'content': _user_facing_chat_query(log.query or '', agent_code=agent_code)}
|
||||
if user_attachments:
|
||||
user_msg['attachments'] = user_attachments
|
||||
messages.append(user_msg)
|
||||
if (
|
||||
str(raw_payload.get('formId') or '').strip()
|
||||
== f'shipping-order-extraction-user:{run_no}'
|
||||
):
|
||||
# 用户上传记录携带 Run 锚点只用于关联,不能再回放为一张助手状态卡。
|
||||
continue
|
||||
if (
|
||||
not log.answer
|
||||
and not log.message_type
|
||||
|
||||
Reference in new issue
Block a user