feat(chat): 优化发货相关消息处理逻辑
- 在列出聊天消息时,过滤掉类型为'shipping-parse-review'的消息 - 移除不再使用的shippingConversationTaskIdCache - 更新发货决策处理逻辑,简化保存消息的参数 - 添加发货动作按钮刷新时的任务定位优先级逻辑 - 移除解析复核消息的展示逻辑,统一处理到轻量工作台 - 增加单元测试,验证发货动作按钮刷新时的任务定位优先级
This commit is contained in:
1 parent
9f5493185d
commit
6a0a51313f
18 files changed
+1230
-199
No files matched your search
@@ -67,10 +67,11 @@ def build_hxops_context(
|
||||
normalized_refs.append({"field": field, "value": value})
|
||||
seen_fields.add(field)
|
||||
|
||||
return {
|
||||
payload: dict[str, Any] = {
|
||||
"schema_version": HXOPS_CONTEXT_SCHEMA_VERSION,
|
||||
"session": {"id": normalized_session_id},
|
||||
"request": request_payload,
|
||||
"refs": normalized_refs,
|
||||
}
|
||||
return payload
|
||||
|
||||
@@ -37,17 +37,12 @@ def _latest_batch_for_conversation(conversation_id: str) -> ShippingBatch | None
|
||||
)
|
||||
|
||||
|
||||
def _latest_task_for_conversation(conversation_id: str) -> ShippingTask | None:
|
||||
"""按会话号获取最近一个未拒绝任务,用于恢复当前任务焦点。"""
|
||||
conv_id = str(conversation_id or "").strip()
|
||||
if not conv_id:
|
||||
def _task_by_task_no(task_id: str) -> ShippingTask | None:
|
||||
"""按显式 task_id 获取任务;仅用于补齐批次 ref,禁止按会话号猜测当前任务。"""
|
||||
normalized = str(task_id or "").strip().upper()
|
||||
if not normalized:
|
||||
return None
|
||||
return (
|
||||
ShippingTask.query.filter_by(conversation_id=conv_id)
|
||||
.filter(ShippingTask.process_status.notin_(["rejected"]))
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.first()
|
||||
)
|
||||
return ShippingTask.query.filter_by(task_no=normalized).first()
|
||||
|
||||
|
||||
def _build_hxops_refs(*, task: str, batch: str) -> list[dict[str, str]]:
|
||||
@@ -81,24 +76,22 @@ def build_dify_chat_inputs(
|
||||
explicit_batch = str(batch_no or "").strip().upper()
|
||||
|
||||
batch = _latest_batch_for_conversation(conv_id)
|
||||
task = _latest_task_for_conversation(conv_id)
|
||||
explicit_task_row = _task_by_task_no(explicit_task)
|
||||
|
||||
focus_task = explicit_task
|
||||
if not focus_task and batch and batch.current_focus_task_no:
|
||||
focus_task = str(batch.current_focus_task_no or "").strip().upper()
|
||||
elif not focus_task and task and task.task_no:
|
||||
focus_task = str(task.task_no or "").strip().upper()
|
||||
|
||||
focus_batch = explicit_batch
|
||||
if not focus_batch and batch and batch.batch_no:
|
||||
focus_batch = str(batch.batch_no or "").strip().upper()
|
||||
elif (
|
||||
not focus_batch
|
||||
and task
|
||||
and getattr(task, "batch", None) is not None
|
||||
and getattr(task.batch, "batch_no", None)
|
||||
and explicit_task_row
|
||||
and getattr(explicit_task_row, "batch", None) is not None
|
||||
and getattr(explicit_task_row.batch, "batch_no", None)
|
||||
):
|
||||
focus_batch = str(task.batch.batch_no or "").strip().upper()
|
||||
focus_batch = str(explicit_task_row.batch.batch_no or "").strip().upper()
|
||||
|
||||
hxops_context = build_hxops_context(
|
||||
session_id=conv_id,
|
||||
|
||||
@@ -15,8 +15,7 @@ from typing import Any
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.models import ChatLog, ShippingBatch, ShippingTask
|
||||
from app.services.shipping_notice.agent_resolver import resolve_chat_agent
|
||||
from app.models import ShippingBatch, ShippingTask
|
||||
from app.services.shipping_notice.constants import (
|
||||
CP_NOTICE_TEXT_EXTRACTION_FAILED,
|
||||
CP_PARSE_OK,
|
||||
@@ -133,75 +132,13 @@ def start_task_orchestrator(task_no: str | None) -> bool:
|
||||
return _start(task_no)
|
||||
|
||||
|
||||
def _build_parse_review_chat_payload(task: ShippingTask) -> dict[str, Any]:
|
||||
return {
|
||||
"taskId": task.task_no,
|
||||
"checkpoint": "parse_review",
|
||||
"header": {
|
||||
"customer_name": task.customer_name,
|
||||
"customer_code": task.customer_code,
|
||||
"ship_to": task.ship_to,
|
||||
"po_number": task.po_number,
|
||||
"required_date": task.required_date,
|
||||
},
|
||||
"lines": task.lines_json or [],
|
||||
"saving": False,
|
||||
}
|
||||
|
||||
|
||||
def _upsert_parse_review_chat_message(task: ShippingTask) -> None:
|
||||
"""
|
||||
将 parse_review 结果直接回填到当前聊天会话。
|
||||
保留旧调用点但不再写聊天表单消息。
|
||||
|
||||
设计意图:
|
||||
- 不再依赖“前端再次发一轮消息”去索取解析结果;
|
||||
- 同任务只保留一条 shipping-parse-review 消息,重复解析时直接更新。
|
||||
核心逻辑:解析复核动作统一由当前任务动作按钮和轻量工作台承载,避免可编辑表单进入聊天历史后在其他会话复活。
|
||||
"""
|
||||
session_id = str(task.conversation_id or "").strip()
|
||||
if not session_id or not task.user_id:
|
||||
return
|
||||
agent = resolve_chat_agent()
|
||||
if not agent or not getattr(agent, "id", None):
|
||||
return
|
||||
|
||||
payload = _build_parse_review_chat_payload(task)
|
||||
content = f"请确认或修订以下解析结果。\n任务编号:{task.task_no}"
|
||||
chat_logs = (
|
||||
db.session.query(ChatLog)
|
||||
.filter_by(
|
||||
user_id=task.user_id,
|
||||
agent_id=agent.id,
|
||||
session_id=session_id,
|
||||
message_type="shipping-parse-review",
|
||||
)
|
||||
.order_by(ChatLog.created_at.desc(), ChatLog.id.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
existing = None
|
||||
for item in chat_logs:
|
||||
item_payload = item.payload if isinstance(item.payload, dict) else {}
|
||||
if str(item_payload.get("taskId") or "").strip().upper() == str(task.task_no or "").strip().upper():
|
||||
existing = item
|
||||
break
|
||||
|
||||
if existing:
|
||||
existing.answer = content
|
||||
existing.payload = payload
|
||||
return
|
||||
|
||||
db.session.add(
|
||||
ChatLog(
|
||||
user_id=task.user_id,
|
||||
agent_id=agent.id,
|
||||
conversation_id=session_id,
|
||||
session_id=session_id,
|
||||
query="",
|
||||
answer=content,
|
||||
message_type="shipping-parse-review",
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
_ = task
|
||||
|
||||
|
||||
def recompute_batch_summary(batch: ShippingBatch | None) -> None:
|
||||
|
||||
Reference in new issue
Block a user