feat(chat): 优化发货相关消息处理逻辑

- 在列出聊天消息时,过滤掉类型为'shipping-parse-review'的消息
- 移除不再使用的shippingConversationTaskIdCache
- 更新发货决策处理逻辑,简化保存消息的参数
- 添加发货动作按钮刷新时的任务定位优先级逻辑
- 移除解析复核消息的展示逻辑,统一处理到轻量工作台
- 增加单元测试,验证发货动作按钮刷新时的任务定位优先级
This commit is contained in:
hugo committed 2026-07-01 08:57:36 +08:00
1 parent 9f5493185d
commit 6a0a51313f
18 files changed
+1230 -199

No files matched your search

@@ -1124,6 +1124,7 @@ def test_build_dify_chat_inputs_returns_hxops_context_only(app):
{"field": "task", "value": "SN-20260622-DDDD0004"},
{"field": "batch", "value": "SB-DIFY-INPUTS"},
]
assert "instructions" not in inputs["hxops_context"]
def test_build_dify_chat_inputs_requires_local_session_not_provider_conversation(app):
@@ -1158,6 +1159,32 @@ def test_build_dify_chat_inputs_requires_local_session_not_provider_conversation
]
def test_build_dify_chat_inputs_does_not_fallback_to_latest_task_without_explicit_anchor(app):
"""多票/无码头场景缺少显式锚点时,不应按最近任务给 Dify 注入 task ref。"""
with app.app_context():
conversation_id = "conv-shipping-no-anchor-001"
older_task = ShippingTask(
task_no="SN-20260630-OLDER001",
conversation_id=conversation_id,
source_type="manual",
process_status="created",
)
newer_task = ShippingTask(
task_no="SN-20260630-NEWER001",
conversation_id=conversation_id,
source_type="manual",
process_status="created",
)
db.session.add_all([older_task, newer_task])
db.session.commit()
inputs = dify_inputs.build_dify_chat_inputs(conversation_id)
assert set(inputs.keys()) == {"hxops_context"}
assert inputs["hxops_context"]["session"]["id"] == conversation_id
assert inputs["hxops_context"]["refs"] == []
def test_is_shipping_notice_chat_agent_accepts_current_dify_app_name():
"""兼容 Dify 侧当前使用的智能发货通知Agent命名。"""
class AgentLike:
+44 -1
View File
@@ -1,7 +1,7 @@
import pytest
from app import db
from app.models import ShippingInventoryMapping, ShippingTask, ShippingTaskEvent
from app.models import ChatLog, ShippingInventoryMapping, ShippingTask, ShippingTaskEvent
from app.services.shipping_notice.match import match_notice_lines
@@ -1177,6 +1177,49 @@ def test_parse_notice_blocks_when_reservation_id_missing(app, client, monkeypatc
assert task.match_status is None
@pytest.mark.auto_rpa
def test_parse_notice_low_confidence_does_not_write_parse_review_chat_card(app, client):
headers = _tool_headers(app)
with app.app_context():
from app.models import User
user = User(username="parse_review_card_user", email="parse_review_card_user@example.com")
db.session.add(user)
db.session.commit()
user_id = user.id
create_res = client.post(
"/api/shipping-notice/tools/create_task",
headers=headers,
json={"source_type": "manual", "conversation_id": "conv-no-parse-card", "user_id": user_id},
)
task_id = create_res.get_json()["task_id"]
parse_res = client.post(
"/api/shipping-notice/tools/parse_notice",
headers=headers,
json={
"task_id": task_id,
"conversation_id": "conv-no-parse-card",
"structured": {
"customer_name": "ACME",
"lines": [{"sku": "A001", "reservation_id": "1000140351", "qty": 10, "uom": "EA"}],
"confidence": 0.4,
},
},
)
parse_data = parse_res.get_json()
assert parse_data["status"] == "needs_human"
assert parse_data.get("checkpoint") == "parse_review"
with app.app_context():
assert db.session.query(ChatLog).filter_by(
session_id="conv-no-parse-card",
message_type="shipping-parse-review",
).count() == 0
@pytest.mark.auto_rpa
def test_parse_notice_auto_continue_hides_parse_confirmation_hint(app, client, monkeypatch):
import app.services.shipping_notice.task_workflow as task_workflow
+33 -1
View File
@@ -56,7 +56,7 @@ def test_chat_history_with_query_param(client):
# 1. Test without agent_id (Should fail with 400)
res = client.get('/api/chat/sessions', headers=headers)
assert res.status_code == 400
assert 'agent_id is required' in res.get_json()['error']
assert res.get_json()['error']['message'] == 'agent_id is required'
# 2. Test with agent_id in query string (Should succeed)
res = client.get('/api/chat/sessions?agent_id=1', headers=headers)
@@ -86,6 +86,38 @@ def test_chat_messages_history(client):
assert msgs[1]['content'] == 'Hello'
def test_local_assistant_message_query_does_not_duplicate_user_bubble(client):
"""助手本地消息即使误传 query,历史回放也不能多生成一条用户确认。"""
headers = get_headers(client)
user_res = client.post('/api/chat/messages/local?agent_id=1', headers=headers, json={
'conversation_id': 'conv-local-confirm',
'role': 'user',
'type': 'text',
'content': '确认\n任务编号:SN-001',
'query': '确认',
})
assert user_res.status_code == 200
assistant_res = client.post('/api/chat/messages/local?agent_id=1', headers=headers, json={
'conversation_id': 'conv-local-confirm',
'role': 'assistant',
'type': 'text',
'content': '已确认,进入下一步。',
'query': '确认',
})
assert assistant_res.status_code == 200
res = client.get('/api/chat/messages?conversation_id=conv-local-confirm&agent_id=1', headers=headers)
assert res.status_code == 200
msgs = res.get_json()
user_messages = [item for item in msgs if item['role'] == 'user']
assert len(user_messages) == 1
assert user_messages[0]['content'] == '确认'
assert msgs[-1]['role'] == 'assistant'
assert msgs[-1]['content'] == '已确认,进入下一步。'
def test_chat_sessions_keep_latest_non_empty_query_as_title(client):
headers = get_headers(client)