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
@@ -0,0 +1,239 @@
|
||||
"""
|
||||
backend/app/services/shipping_notice/runtime_context.py
|
||||
|
||||
用途:构建货代发货通知 Agent 的 agent_runtime_context 领域适配层。
|
||||
依赖:models.ShippingBatch/ShippingTask、agent_runtime_context.builder。
|
||||
逻辑:dify_inputs 与后台编排器在调用 Dify 前通过本模块生成最新业务现场,
|
||||
Dify 只消费 snapshot,任务推进仍以 HXOPS 状态机和 Tool API 为准。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app.models import ShippingBatch, ShippingTask
|
||||
from app.services.agent_runtime_context.builder import build_agent_runtime_context
|
||||
from app.services.shipping_notice.reference_context import build_shipping_reference_context
|
||||
|
||||
SHIPPING_NOTICE_AGENT_CODE = "shipping_notice"
|
||||
|
||||
|
||||
def _clean_text(value: Any) -> str:
|
||||
"""
|
||||
将模型字段规范化为短文本。
|
||||
|
||||
核心逻辑:统一处理 None/空白字符串,避免 domain/state_hint 中混入空值。
|
||||
|
||||
Args:
|
||||
value: 待规范化字段。
|
||||
|
||||
Returns:
|
||||
去除首尾空白后的字符串。
|
||||
"""
|
||||
return str(value or "").strip()
|
||||
|
||||
|
||||
def _batch_no(batch: ShippingBatch | None, task: ShippingTask | None) -> str:
|
||||
"""
|
||||
获取当前上下文的批次号。
|
||||
|
||||
核心逻辑:优先使用显式 batch;缺失时从 task.batch 补齐,避免后台单票 run 丢失批次锚点。
|
||||
|
||||
Args:
|
||||
batch: 当前批次。
|
||||
task: 当前任务。
|
||||
|
||||
Returns:
|
||||
批次号;无法识别时返回空字符串。
|
||||
"""
|
||||
if batch and _clean_text(getattr(batch, "batch_no", None)):
|
||||
return _clean_text(batch.batch_no).upper()
|
||||
task_batch = getattr(task, "batch", None)
|
||||
if task_batch and _clean_text(getattr(task_batch, "batch_no", None)):
|
||||
return _clean_text(task_batch.batch_no).upper()
|
||||
return ""
|
||||
|
||||
|
||||
def _task_no(task: ShippingTask | None) -> str:
|
||||
"""
|
||||
获取当前上下文的任务号。
|
||||
|
||||
核心逻辑:只使用已解析出的显式任务对象,不按会话猜测最近任务,避免多票串单。
|
||||
|
||||
Args:
|
||||
task: 当前任务。
|
||||
|
||||
Returns:
|
||||
任务号;缺失时返回空字符串。
|
||||
"""
|
||||
return _clean_text(getattr(task, "task_no", None)).upper()
|
||||
|
||||
|
||||
def _build_refs(
|
||||
*,
|
||||
task: ShippingTask | None,
|
||||
batch: ShippingBatch | None,
|
||||
run_purpose: str | None,
|
||||
) -> dict[str, str]:
|
||||
"""
|
||||
构建新版 agent_runtime_context.refs。
|
||||
|
||||
核心逻辑:使用稳定业务字段名 task_no/batch_no/run_purpose/file_name,区别于旧 hxops_context 的 task/batch。
|
||||
|
||||
Args:
|
||||
task: 当前任务。
|
||||
batch: 当前批次。
|
||||
run_purpose: 本次 Dify run 的目的。
|
||||
|
||||
Returns:
|
||||
非空业务锚点字典。
|
||||
"""
|
||||
refs: dict[str, str] = {}
|
||||
task_no = _task_no(task)
|
||||
batch_no = _batch_no(batch, task)
|
||||
file_name = _clean_text(getattr(task, "source_file_name", None))
|
||||
normalized_run_purpose = _clean_text(run_purpose)
|
||||
if task_no:
|
||||
refs["task_no"] = task_no
|
||||
if batch_no:
|
||||
refs["batch_no"] = batch_no
|
||||
if normalized_run_purpose:
|
||||
refs["run_purpose"] = normalized_run_purpose
|
||||
if file_name:
|
||||
refs["file_name"] = file_name
|
||||
return refs
|
||||
|
||||
|
||||
def _build_state_hint(task: ShippingTask | None, batch: ShippingBatch | None) -> dict[str, Any]:
|
||||
"""
|
||||
构建发货通知运行态提示。
|
||||
|
||||
核心逻辑:只放 Dify 判断流程所需的状态摘要;真正能否推进仍由 HXOPS Tool/状态机校验。
|
||||
|
||||
Args:
|
||||
task: 当前任务。
|
||||
batch: 当前批次。
|
||||
|
||||
Returns:
|
||||
状态提示字典。
|
||||
"""
|
||||
state_hint: dict[str, Any] = {}
|
||||
task_no = _task_no(task)
|
||||
if task_no:
|
||||
state_hint["focus_task_no"] = task_no
|
||||
elif batch and _clean_text(getattr(batch, "current_focus_task_no", None)):
|
||||
state_hint["focus_task_no"] = _clean_text(batch.current_focus_task_no).upper()
|
||||
|
||||
if task is not None:
|
||||
for attr, key in (
|
||||
("current_step", "current_step"),
|
||||
("process_status", "status"),
|
||||
("review_status", "review_status"),
|
||||
("match_status", "match_status"),
|
||||
("pending_human_reason", "pending_human_reason"),
|
||||
("upload_status", "upload_status"),
|
||||
):
|
||||
value = _clean_text(getattr(task, attr, None))
|
||||
if value:
|
||||
state_hint[key] = value
|
||||
|
||||
batch_no = _batch_no(batch, task)
|
||||
if batch_no:
|
||||
state_hint["batch_no"] = batch_no
|
||||
return state_hint
|
||||
|
||||
|
||||
def _build_summary(
|
||||
*,
|
||||
task: ShippingTask | None,
|
||||
batch: ShippingBatch | None,
|
||||
run_purpose: str | None,
|
||||
) -> str:
|
||||
"""
|
||||
构建给 Dify 阅读的短摘要。
|
||||
|
||||
核心逻辑:用自然语言说明当前 run 的任务、批次、文件和状态,避免 Dify 依赖 conversation memory 猜现场。
|
||||
|
||||
Args:
|
||||
task: 当前任务。
|
||||
batch: 当前批次。
|
||||
run_purpose: 本次 Dify run 的目的。
|
||||
|
||||
Returns:
|
||||
200 字以内的现场摘要。
|
||||
"""
|
||||
pieces: list[str] = []
|
||||
task_no = _task_no(task)
|
||||
batch_no = _batch_no(batch, task)
|
||||
file_name = _clean_text(getattr(task, "source_file_name", None))
|
||||
status = _clean_text(getattr(task, "process_status", None))
|
||||
current_step = _clean_text(getattr(task, "current_step", None))
|
||||
normalized_run_purpose = _clean_text(run_purpose)
|
||||
if task_no:
|
||||
pieces.append(f"当前任务 {task_no}")
|
||||
if batch_no:
|
||||
pieces.append(f"属于批次 {batch_no}")
|
||||
if file_name:
|
||||
pieces.append(f"来源文件 {file_name}")
|
||||
if normalized_run_purpose:
|
||||
pieces.append(f"本次执行目的为 {normalized_run_purpose}")
|
||||
if status:
|
||||
pieces.append(f"当前状态 {status}")
|
||||
if current_step:
|
||||
pieces.append(f"当前步骤 {current_step}")
|
||||
if not pieces:
|
||||
return "当前为货代发货通知 Agent 会话,尚未解析出明确任务或批次锚点。"
|
||||
return ",".join(pieces) + "。"
|
||||
|
||||
|
||||
def build_shipping_runtime_context(
|
||||
*,
|
||||
session_id: str,
|
||||
task: ShippingTask | None = None,
|
||||
batch: ShippingBatch | None = None,
|
||||
trigger_source: str | None = None,
|
||||
request_id: str | None = None,
|
||||
locale: str | None = None,
|
||||
scope: str = "task_run",
|
||||
run_purpose: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
构建货代发货通知 Agent 的 agent_runtime_context。
|
||||
|
||||
核心逻辑:以 HXOPS DB 中的 task/batch 为事实源,生成 refs/domain.state_hint/reference_context snapshot,
|
||||
供 Dify 每次 run 显式识别现场。
|
||||
|
||||
Args:
|
||||
session_id: HXOPS 本地 ChatSession.session_id。
|
||||
task: 当前显式任务;禁止在本函数内按会话猜测。
|
||||
batch: 当前批次。
|
||||
trigger_source: 触发来源,如 frontend_chat/backend_orchestrator。
|
||||
request_id: 本次请求 ID。
|
||||
locale: 用户语言。
|
||||
scope: 上下文作用域。
|
||||
run_purpose: 本次 Dify run 的目的,如 parse_notice。
|
||||
|
||||
Returns:
|
||||
agent_runtime_context 字典;session_id 为空时返回空 dict。
|
||||
"""
|
||||
domain = {
|
||||
"type": SHIPPING_NOTICE_AGENT_CODE,
|
||||
"summary": _build_summary(task=task, batch=batch, run_purpose=run_purpose),
|
||||
"state_hint": _build_state_hint(task, batch),
|
||||
"available_actions_hint": [],
|
||||
}
|
||||
reference_context = build_shipping_reference_context(
|
||||
batch=batch,
|
||||
focus_task=task,
|
||||
)
|
||||
return build_agent_runtime_context(
|
||||
agent_code=SHIPPING_NOTICE_AGENT_CODE,
|
||||
scope=scope,
|
||||
session_id=session_id,
|
||||
request_id=request_id,
|
||||
trigger_source=trigger_source,
|
||||
locale=locale,
|
||||
refs=_build_refs(task=task, batch=batch, run_purpose=run_purpose),
|
||||
domain=domain,
|
||||
reference_context=reference_context,
|
||||
)
|
||||
Reference in new issue
Block a user