Files
syops/backend/app/services/shipping_notice/dify_inputs.py
T
hugo 8846ce159f refactor(agent-runtime, shipping-notice, frontend): 重构上下文体系,修复重试RPA无响应问题
- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑
- 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断
- 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发
- 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案
- 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数
- 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图
- 更新OpenAPI文档与静态快照,补充工具使用约束说明
- 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例
- 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置
- 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
2026-07-03 17:01:42 +08:00

127 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
services/shipping_notice/dify_inputs.py
用途:识别发货通知 Dify Chatflow Agent,并构建会话级 Dify inputs。
依赖:agent_runtime_context 平台协议、agent_resolver、models.ShippingBatch/ShippingTask、shipping_notice.runtime_context。
逻辑:由 api/chat 在调用 DifyService.stream_chat 前引用;本模块按会话号解析当前批次/任务焦点,
仅输出 agent_runtime_context JSON 字符串,供 Dify 画布统一读取平台运行态快照。
"""
from __future__ import annotations
from typing import Any
from app.models import ShippingBatch, ShippingTask
from app.services.agent_runtime_context.builder import serialize_for_dify
from app.services.agent_runtime_context.schema import AGENT_RUNTIME_CONTEXT_INPUT
from app.services.shipping_notice.agent_resolver import CHAT_AGENT_NAME
from app.services.shipping_notice.runtime_context import build_shipping_runtime_context
def is_shipping_notice_chat_agent(agent) -> bool:
"""判断 Agent 记录是否为发货通知聊天 Agent。"""
agent_name = str(getattr(agent, "name", None) or "").strip()
if agent_name == CHAT_AGENT_NAME:
return True
normalized_name = agent_name.replace(" ", "")
return "发货通知" in normalized_name and "Agent" in normalized_name
def _latest_batch_for_conversation(conversation_id: str) -> ShippingBatch | None:
"""按会话号获取最近一个发货批次,用于恢复当前批次焦点。"""
conv_id = str(conversation_id or "").strip()
if not conv_id:
return None
return (
ShippingBatch.query.filter_by(conversation_id=conv_id)
.order_by(ShippingBatch.id.desc())
.first()
)
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(task_no=normalized).first()
def _focus_task_row(focus_task: str, explicit_task_row: ShippingTask | None) -> ShippingTask | None:
"""
解析当前显式焦点任务行。
核心逻辑:优先复用已查出的显式 task;若焦点来自 batch.current_focus_task_no,再按 task_no 查询,
但不在无焦点时回退到最近任务,避免多票批次串单。
Args:
focus_task: 已解析出的焦点任务号。
explicit_task_row: task_id 显式传入时查询到的任务。
Returns:
当前焦点任务;无法明确识别时返回 None。
"""
normalized = str(focus_task or "").strip().upper()
if not normalized:
return None
if explicit_task_row and str(explicit_task_row.task_no or "").strip().upper() == normalized:
return explicit_task_row
return ShippingTask.query.filter_by(task_no=normalized).first()
def build_dify_chat_inputs(
conversation_id: str | None = None,
*,
task_id: str | None = None,
batch_no: str | None = None,
trigger_source: str | None = None,
request_id: str | None = None,
locale: str | None = None,
) -> dict[str, Any]:
"""
构建发货通知 Dify Chatflow inputs。
OpenAPI 工具已在 Dify 侧配置 Server URL 与 Authorization / X-Integration-Key,
无需再注入 HXOPS_TOOLS_API_BASE;这里仅注入 agent_runtime_context,避免新旧上下文字段并存导致画布协议漂移。
"""
conv_id = str(conversation_id or "").strip()
if not conv_id:
return {}
explicit_task = str(task_id or "").strip().upper()
explicit_batch = str(batch_no or "").strip().upper()
batch = _latest_batch_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()
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 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(explicit_task_row.batch.batch_no or "").strip().upper()
runtime_context = build_shipping_runtime_context(
session_id=conv_id,
task=_focus_task_row(focus_task, explicit_task_row),
batch=batch or getattr(explicit_task_row, "batch", None),
trigger_source=trigger_source,
request_id=request_id,
locale=locale,
scope="task_run" if trigger_source == "backend_orchestrator" else "chat_turn",
run_purpose="parse_notice" if trigger_source == "backend_orchestrator" else "chat_turn",
)
if not runtime_context:
return {}
return {
AGENT_RUNTIME_CONTEXT_INPUT: serialize_for_dify(runtime_context),
}