Files
syops/backend/app/services/shipping_notice/dify_inputs.py
T
hugo 08360a4142 feat(shipping): 新增货代发货批量自动处理与任务重试功能
本次提交完整实现货代发货通知的自动化批量处理能力:
1.  新增发货单票后台任务类型与专用worker队列,实现任务持久化调度
2.  实现发货批次后台编排器,支持上传后自动逐票推进解析、匹配、导出全流程
3.  新增单票重试、批次待处理重试的API与前端操作界面
4.  补充全量多语言翻译,新增重试、任务列表、状态展示相关文案
5.  优化上传交互逻辑,改为后台自动处理任务,无需手动触发解析指令
6.  修复文件检核工具无数据预检逻辑,避免创建空检核批次
7.  调整发货工作流细节,优化Excel生成幂等性与任务状态同步
8.  更新环境配置与API文档,修正货代工具接口地址端口
2026-06-23 11:22:09 +08:00

103 lines
3.8 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_resolver、models.ShippingBatch/ShippingTask。
逻辑:由 api/chat 在调用 DifyService.stream_chat 前引用;本模块按会话号解析当前批次/任务焦点,
把 Dify 画布需要的 conversation_id、current_batch_no、current_task_no 注入 Start 节点变量。
"""
from __future__ import annotations
from app.models import ShippingBatch, ShippingTask
from app.services.shipping_notice.agent_resolver import CHAT_AGENT_NAME
SHIPPING_CONVERSATION_ID_INPUT = "conversation_id"
SHIPPING_CURRENT_BATCH_NO_INPUT = "current_batch_no"
SHIPPING_CURRENT_TASK_NO_INPUT = "current_task_no"
SHIPPING_ENTRY_MODE_INPUT = "entry_mode"
SHIPPING_USER_MESSAGE_INPUT = "user_message"
SHIPPING_TRIGGER_SOURCE_INPUT = "trigger_source"
def is_shipping_notice_chat_agent(agent) -> bool:
"""判断 Agent 记录是否为发货通知聊天 Agent。"""
return getattr(agent, "name", None) == CHAT_AGENT_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 _latest_task_for_conversation(conversation_id: str) -> ShippingTask | None:
"""按会话号获取最近一个未拒绝任务,用于恢复当前任务焦点。"""
conv_id = str(conversation_id or "").strip()
if not conv_id:
return None
return (
ShippingTask.query.filter_by(conversation_id=conv_id)
.filter(ShippingTask.process_status.notin_(["rejected"]))
.order_by(ShippingTask.id.desc())
.first()
)
def build_dify_chat_inputs(
conversation_id: str | None = None,
*,
task_id: str | None = None,
batch_no: str | None = None,
entry_mode: str | None = None,
user_message: str | None = None,
trigger_source: str | None = None,
) -> dict[str, str]:
"""
构建发货通知 Dify Chatflow inputs。
OpenAPI 工具已在 Dify 侧配置 Server URL 与 Authorization / X-Integration-Key,
无需再注入 HXOPS_TOOLS_API_BASE;这里只补会话级上下文,供 Dify 画布稳定读取当前 task/batch。
"""
conv_id = str(conversation_id or "").strip()
if not conv_id:
return {}
explicit_task_no = str(task_id or "").strip().upper()
explicit_batch_no = str(batch_no or "").strip().upper()
batch = _latest_batch_for_conversation(conv_id)
task = _latest_task_for_conversation(conv_id)
current_task_no = explicit_task_no
if not current_task_no and batch and batch.current_focus_task_no:
current_task_no = str(batch.current_focus_task_no or "").strip().upper()
elif not current_task_no and task and task.task_no:
current_task_no = str(task.task_no or "").strip().upper()
current_batch_no = explicit_batch_no
if not current_batch_no and batch and batch.batch_no:
current_batch_no = str(batch.batch_no or "").strip().upper()
elif (
not current_batch_no
and task
and getattr(task, "batch", None) is not None
and getattr(task.batch, "batch_no", None)
):
current_batch_no = str(task.batch.batch_no or "").strip().upper()
return {
SHIPPING_ENTRY_MODE_INPUT: str(entry_mode or "").strip(),
SHIPPING_CONVERSATION_ID_INPUT: conv_id,
SHIPPING_CURRENT_BATCH_NO_INPUT: current_batch_no,
SHIPPING_CURRENT_TASK_NO_INPUT: current_task_no,
SHIPPING_USER_MESSAGE_INPUT: str(user_message or "").strip(),
SHIPPING_TRIGGER_SOURCE_INPUT: str(trigger_source or "").strip(),
}