refactor(agent-runtime, shipping-notice, frontend): 重构上下文体系,修复重试RPA无响应问题
- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
This commit is contained in:
1 parent
60dd0c0f58
commit
8846ce159f
102 files changed
+18289
-9072
No files matched your search
@@ -7,6 +7,7 @@
|
||||
4. 工作台批量上传通知时仅为可解析文件建任务,并返回 envelope + 文件级摘要。
|
||||
"""
|
||||
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from io import BytesIO
|
||||
|
||||
@@ -131,6 +132,145 @@ def test_create_batch_tasks_and_switch_focus(app, client):
|
||||
assert batch_row.current_focus_task_no == target_task_no
|
||||
assert task_rows[0].batch_total == 2
|
||||
assert task_rows[1].batch_total == 2
|
||||
focus_events = (
|
||||
ShippingTaskEvent.query.filter_by(task_id=task_rows[1].id, event_type="focus_switched")
|
||||
.order_by(ShippingTaskEvent.id.desc())
|
||||
.all()
|
||||
)
|
||||
assert len(focus_events) == 1
|
||||
assert focus_events[0].detail_json["action"] == "switch_focus"
|
||||
assert focus_events[0].detail_json["source"] == "tool_api"
|
||||
assert focus_events[0].detail_json["previous_focus_task_no"] == task_rows[0].task_no
|
||||
assert focus_events[0].detail_json["next_focus_task_no"] == task_rows[1].task_no
|
||||
|
||||
|
||||
def test_workspace_submit_decision_updates_reference_context_last_action(app, client):
|
||||
"""用户在工作台点击确认后,下次 runtime_context 应暴露标准 confirm 动作。"""
|
||||
headers = _workspace_headers(app)
|
||||
with app.app_context():
|
||||
user = User.query.filter_by(username="shipping_workspace_user").order_by(User.id.desc()).first()
|
||||
assert user is not None
|
||||
batch = ShippingBatch(
|
||||
batch_no="SB-WS-CONFIRM-001",
|
||||
user_id=user.id,
|
||||
conversation_id="conv-ws-confirm-001",
|
||||
current_focus_task_no="SN-WS-CONFIRM-001",
|
||||
file_count=1,
|
||||
)
|
||||
task = ShippingTask(
|
||||
task_no="SN-WS-CONFIRM-001",
|
||||
batch=batch,
|
||||
user_id=user.id,
|
||||
conversation_id="conv-ws-confirm-001",
|
||||
source_type="manual",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
pending_human_reason="low_confidence",
|
||||
raw_content="客户:ACME\nA001 x 10",
|
||||
)
|
||||
db.session.add_all([batch, task])
|
||||
db.session.commit()
|
||||
|
||||
res = client.post(
|
||||
"/api/workspace/shipping-tasks/SN-WS-CONFIRM-001/submit-decision",
|
||||
headers=headers,
|
||||
json={"decision": "confirm", "comment": "用户确认"},
|
||||
)
|
||||
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
assert data["status"] in {"ok", "running"}
|
||||
with app.app_context():
|
||||
task = ShippingTask.query.filter_by(task_no="SN-WS-CONFIRM-001").one()
|
||||
event = (
|
||||
ShippingTaskEvent.query.filter_by(task_id=task.id, event_type="parse_confirmed")
|
||||
.order_by(ShippingTaskEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
assert event is not None
|
||||
assert event.detail_json["action"] == "confirm"
|
||||
assert event.detail_json["source"] == "workspace_api"
|
||||
|
||||
inputs = dify_inputs.build_dify_chat_inputs("conv-ws-confirm-001")
|
||||
runtime_context = json.loads(inputs["agent_runtime_context"])
|
||||
assert runtime_context["reference_context"]["last_action_target"] == {
|
||||
"type": "task",
|
||||
"id": "SN-WS-CONFIRM-001",
|
||||
"action": "confirm_parse",
|
||||
}
|
||||
|
||||
|
||||
def test_workspace_switch_focus_updates_reference_context(app, client):
|
||||
"""用户切换 focus 后,下次 runtime_context 应同时反映新的 focus_object 与 switch_focus 动作。"""
|
||||
headers = _workspace_headers(app)
|
||||
with app.app_context():
|
||||
user = User.query.filter_by(username="shipping_workspace_user").order_by(User.id.desc()).first()
|
||||
assert user is not None
|
||||
batch = ShippingBatch(
|
||||
batch_no="SB-WS-FOCUS-001",
|
||||
user_id=user.id,
|
||||
conversation_id="conv-ws-focus-001",
|
||||
current_focus_task_no="SN-WS-FOCUS-001",
|
||||
file_count=2,
|
||||
)
|
||||
old_focus = ShippingTask(
|
||||
task_no="SN-WS-FOCUS-001",
|
||||
batch=batch,
|
||||
user_id=user.id,
|
||||
conversation_id="conv-ws-focus-001",
|
||||
source_type="manual",
|
||||
process_status="created",
|
||||
batch_index=1,
|
||||
batch_total=2,
|
||||
)
|
||||
new_focus = ShippingTask(
|
||||
task_no="SN-WS-FOCUS-002",
|
||||
batch=batch,
|
||||
user_id=user.id,
|
||||
conversation_id="conv-ws-focus-001",
|
||||
source_type="manual",
|
||||
process_status="pending_review",
|
||||
match_status="shortage",
|
||||
pending_human_reason="shortage",
|
||||
shortage_json={"A001": 3},
|
||||
batch_index=2,
|
||||
batch_total=2,
|
||||
)
|
||||
db.session.add_all([batch, old_focus, new_focus])
|
||||
db.session.commit()
|
||||
|
||||
res = client.post(
|
||||
"/api/workspace/shipping-batches/SB-WS-FOCUS-001/focus-task",
|
||||
headers=headers,
|
||||
json={"task_no": "SN-WS-FOCUS-002", "comment": "切到异常票"},
|
||||
)
|
||||
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
assert data["batch"]["current_focus_task_no"] == "SN-WS-FOCUS-002"
|
||||
with app.app_context():
|
||||
target = ShippingTask.query.filter_by(task_no="SN-WS-FOCUS-002").one()
|
||||
event = (
|
||||
ShippingTaskEvent.query.filter_by(task_id=target.id, event_type="focus_switched")
|
||||
.order_by(ShippingTaskEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
assert event is not None
|
||||
assert event.detail_json["action"] == "switch_focus"
|
||||
assert event.detail_json["source"] == "workspace_api"
|
||||
assert event.detail_json["previous_focus_task_no"] == "SN-WS-FOCUS-001"
|
||||
assert event.detail_json["next_focus_task_no"] == "SN-WS-FOCUS-002"
|
||||
|
||||
inputs = dify_inputs.build_dify_chat_inputs("conv-ws-focus-001")
|
||||
runtime_context = json.loads(inputs["agent_runtime_context"])
|
||||
assert runtime_context["reference_context"]["focus_object"]["id"] == "SN-WS-FOCUS-002"
|
||||
assert runtime_context["reference_context"]["last_action_target"] == {
|
||||
"type": "task",
|
||||
"id": "SN-WS-FOCUS-002",
|
||||
"action": "switch_focus",
|
||||
}
|
||||
assert runtime_context["reference_context"]["last_failed_target"]["id"] == "SN-WS-FOCUS-002"
|
||||
assert "candidates" not in runtime_context["reference_context"]
|
||||
|
||||
|
||||
def test_contract_batch_status_keeps_single_focus_without_advancing_other_tasks(app, client):
|
||||
@@ -487,10 +627,11 @@ def test_workspace_replace_notice_overwrites_current_task_without_new_batch(app,
|
||||
assert task.export_file_name is None
|
||||
|
||||
|
||||
def test_auto_progress_inventory_trigger_failure_marks_step3(app):
|
||||
def test_auto_progress_inventory_trigger_failure_marks_step3(app, tmp_path):
|
||||
"""库存自动获取失败时应进入第 3 步库存准备,而不是继续显示解析确认。"""
|
||||
app.config["SHIPPING_NOTICE_AUTO_PROGRESS_ENABLED"] = True
|
||||
app.config["SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED"] = True
|
||||
app.config["FREIGHT_INVENTORY_DROP_DIR"] = str(tmp_path / "empty_inventory_drop")
|
||||
app.config["FREIGHT_RPA_INVENTORY_TRIGGER_DIR"] = ""
|
||||
with app.app_context():
|
||||
user = User(username="shipping_inventory_step_user", email="shipping_inventory_step_user@example.com")
|
||||
@@ -592,6 +733,7 @@ def test_auto_progress_resolves_dub_inventory_source_into_rpa_trigger(app, tmp_p
|
||||
"""目的地 DUB 应解析到 DSV360_AWSDU,并写入 RPA 库存导出 trigger。"""
|
||||
app.config["SHIPPING_NOTICE_AUTO_PROGRESS_ENABLED"] = True
|
||||
app.config["SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED"] = True
|
||||
app.config["FREIGHT_INVENTORY_DROP_DIR"] = str(tmp_path / "empty_inventory_drop")
|
||||
app.config["FREIGHT_RPA_INVENTORY_TRIGGER_DIR"] = str(tmp_path / "inventory_triggers")
|
||||
with app.app_context():
|
||||
user = User(username="shipping_inventory_source_user", email="shipping_inventory_source_user@example.com")
|
||||
@@ -979,6 +1121,8 @@ def test_workspace_conversation_batch_workspace_aggregates_multiple_batches(app,
|
||||
assert [item["batch_no"] for item in data["batches"]] == ["SB-CONV-WS-A", "SB-CONV-WS-B"]
|
||||
assert data["batches"][0]["focus_task"]["task_no"] == "SN-20260626-CONVA001"
|
||||
assert len(data["batches"][1]["tasks"]) == 2
|
||||
assert "ui_state" in data["batches"][0]["tasks"][0]
|
||||
assert isinstance(data["batches"][0]["tasks"][0]["ui_state"]["action_buttons"], list)
|
||||
|
||||
|
||||
def test_workspace_conversation_batch_workspace_filters_by_user(app, client):
|
||||
@@ -1344,21 +1488,28 @@ def test_run_shipping_parse_via_dify_passes_task_context_without_persisting_chat
|
||||
assert captured["session_id"] == session.session_id
|
||||
assert captured["persist_chat_log"] is False
|
||||
assert isinstance(captured["inputs"], dict)
|
||||
assert captured["inputs"]["hxops_context"]["schema_version"] == "1"
|
||||
assert captured["inputs"]["hxops_context"]["session"]["id"] == session.session_id
|
||||
assert captured["inputs"]["hxops_context"]["request"]["id"] == f"shipping-orch-{task.task_no}"
|
||||
assert captured["inputs"]["hxops_context"]["request"]["trigger_source"] == "backend_orchestrator"
|
||||
assert captured["inputs"]["hxops_context"]["refs"] == [
|
||||
{"field": "task", "value": task.task_no},
|
||||
{"field": "batch", "value": batch.batch_no},
|
||||
]
|
||||
runtime_context = json.loads(captured["inputs"]["agent_runtime_context"])
|
||||
assert runtime_context["schema_version"] == "1"
|
||||
assert runtime_context["agent_code"] == "shipping_notice"
|
||||
assert runtime_context["scope"] == "task_run"
|
||||
assert runtime_context["platform"]["session_id"] == session.session_id
|
||||
assert runtime_context["platform"]["request_id"] == f"shipping-orch-{task.task_no}"
|
||||
assert runtime_context["platform"]["trigger_source"] == "backend_orchestrator"
|
||||
assert runtime_context["refs"]["task_no"] == task.task_no
|
||||
assert runtime_context["refs"]["batch_no"] == batch.batch_no
|
||||
assert runtime_context["refs"]["run_purpose"] == "parse_notice"
|
||||
assert runtime_context["domain"]["type"] == "shipping_notice"
|
||||
assert runtime_context["domain"]["state_hint"]["focus_task_no"] == task.task_no
|
||||
assert runtime_context["domain"]["state_hint"]["status"] == task.process_status
|
||||
assert runtime_context["reference_context"]["focus_object"]["id"] == task.task_no
|
||||
assert set(captured["inputs"].keys()) == {"agent_runtime_context"}
|
||||
refreshed_session = ChatSession.query.filter_by(session_id=session.session_id).first()
|
||||
assert refreshed_session is not None
|
||||
assert refreshed_session.external_conversation_id == "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
|
||||
def test_build_dify_chat_inputs_returns_hxops_context_only(app):
|
||||
"""货代发货 Agent 应只注入平台统一的 hxops_context。"""
|
||||
def test_build_dify_chat_inputs_returns_agent_runtime_context_only(app):
|
||||
"""货代发货 Agent 仅应向 Dify 注入 agent_runtime_context。"""
|
||||
with app.app_context():
|
||||
batch = ShippingBatch(
|
||||
batch_no="SB-DIFY-INPUTS",
|
||||
@@ -1382,20 +1533,26 @@ def test_build_dify_chat_inputs_returns_hxops_context_only(app):
|
||||
request_id="req-frontend-001",
|
||||
)
|
||||
|
||||
assert set(inputs.keys()) == {"hxops_context"}
|
||||
assert inputs["hxops_context"]["schema_version"] == "1"
|
||||
assert inputs["hxops_context"]["session"]["id"] == "conv-shipping-inputs-001"
|
||||
assert inputs["hxops_context"]["request"]["id"] == "req-frontend-001"
|
||||
assert inputs["hxops_context"]["request"]["trigger_source"] == "frontend_chat"
|
||||
assert inputs["hxops_context"]["refs"] == [
|
||||
{"field": "task", "value": "SN-20260622-DDDD0004"},
|
||||
{"field": "batch", "value": "SB-DIFY-INPUTS"},
|
||||
]
|
||||
assert "instructions" not in inputs["hxops_context"]
|
||||
assert set(inputs.keys()) == {"agent_runtime_context"}
|
||||
runtime_context = json.loads(inputs["agent_runtime_context"])
|
||||
assert runtime_context["schema_version"] == "1"
|
||||
assert runtime_context["agent_code"] == "shipping_notice"
|
||||
assert runtime_context["scope"] == "chat_turn"
|
||||
assert runtime_context["platform"]["session_id"] == "conv-shipping-inputs-001"
|
||||
assert runtime_context["platform"]["request_id"] == "req-frontend-001"
|
||||
assert runtime_context["platform"]["trigger_source"] == "frontend_chat"
|
||||
assert runtime_context["refs"] == {
|
||||
"task_no": "SN-20260622-DDDD0004",
|
||||
"batch_no": "SB-DIFY-INPUTS",
|
||||
"run_purpose": "chat_turn",
|
||||
}
|
||||
assert runtime_context["domain"]["state_hint"]["focus_task_no"] == "SN-20260622-DDDD0004"
|
||||
assert runtime_context["domain"]["state_hint"]["status"] == "created"
|
||||
assert runtime_context["reference_context"]["focus_object"]["id"] == "SN-20260622-DDDD0004"
|
||||
|
||||
|
||||
def test_build_dify_chat_inputs_requires_local_session_not_provider_conversation(app):
|
||||
"""已有 Dify provider 会话号时,HXOPS 上下文仍应按本地 session 解析。"""
|
||||
"""已有 Dify provider 会话号时,agent_runtime_context 仍应按本地 session 解析。"""
|
||||
with app.app_context():
|
||||
local_session_id = "conv-shipping-local-session-001"
|
||||
provider_conversation_id = "f3b371e7-4d8b-40b6-80be-76fd98459c65"
|
||||
@@ -1418,12 +1575,13 @@ def test_build_dify_chat_inputs_requires_local_session_not_provider_conversation
|
||||
provider_inputs = dify_inputs.build_dify_chat_inputs(provider_conversation_id)
|
||||
local_inputs = dify_inputs.build_dify_chat_inputs(local_session_id)
|
||||
|
||||
assert provider_inputs["hxops_context"]["refs"] == []
|
||||
assert local_inputs["hxops_context"]["session"]["id"] == local_session_id
|
||||
assert local_inputs["hxops_context"]["refs"] == [
|
||||
{"field": "task", "value": "SN-20260626-LOCAL001"},
|
||||
{"field": "batch", "value": "SB-DIFY-LOCAL"},
|
||||
]
|
||||
provider_runtime_context = json.loads(provider_inputs["agent_runtime_context"])
|
||||
assert provider_runtime_context["refs"] == {"run_purpose": "chat_turn"}
|
||||
local_runtime_context = json.loads(local_inputs["agent_runtime_context"])
|
||||
assert local_runtime_context["refs"]["task_no"] == "SN-20260626-LOCAL001"
|
||||
assert local_runtime_context["refs"]["batch_no"] == "SB-DIFY-LOCAL"
|
||||
assert set(provider_inputs.keys()) == {"agent_runtime_context"}
|
||||
assert set(local_inputs.keys()) == {"agent_runtime_context"}
|
||||
|
||||
|
||||
def test_build_dify_chat_inputs_does_not_fallback_to_latest_task_without_explicit_anchor(app):
|
||||
@@ -1447,9 +1605,9 @@ def test_build_dify_chat_inputs_does_not_fallback_to_latest_task_without_explici
|
||||
|
||||
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"] == []
|
||||
assert set(inputs.keys()) == {"agent_runtime_context"}
|
||||
runtime_context = json.loads(inputs["agent_runtime_context"])
|
||||
assert runtime_context["refs"] == {"run_purpose": "chat_turn"}
|
||||
|
||||
|
||||
def test_is_shipping_notice_chat_agent_accepts_current_dify_app_name():
|
||||
|
||||
Reference in new issue
Block a user