- 添加单票与多票发货需求的处理方式 - 说明库存 Excel 上传后的保存与关联机制 - 解释库存匹配与 Excel 生成的独立执行方式 - 明确批次库存的真相来源 feat(chat): 优化发货库存上传提示语 - 根据上传结果生成更详细的提示信息 - 增加批次号的提示逻辑 - 使用国际化支持替换硬编码字符串 refactor(chat): 重构发货库存上传处理逻辑 - 移除多余的任务选择逻辑 - 简化上传处理流程 - 增强代码可读性与维护性 feat(locales): 更新多语言支持的提示信息 - 添加库存上传与任务处理的多语言提示 - 更新库存缺口与任务状态的描述
113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""
|
||
services/shipping_notice/dify_inputs.py
|
||
|
||
用途:识别发货通知 Dify Chatflow Agent,并构建会话级 Dify inputs。
|
||
依赖:agent_resolver、models.ShippingBatch/ShippingTask、chat.hxops_context。
|
||
逻辑:由 api/chat 在调用 DifyService.stream_chat 前引用;本模块按会话号解析当前批次/任务焦点,
|
||
仅补充货代发货场景可识别的 refs,再委托平台级上下文构造器生成 hxops_context。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
from app.models import ShippingBatch, ShippingTask
|
||
from app.services.chat.hxops_context import HXOPS_CONTEXT_INPUT, build_hxops_context, build_ref
|
||
from app.services.shipping_notice.agent_resolver import CHAT_AGENT_NAME
|
||
|
||
|
||
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 _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_hxops_refs(*, task: str, batch: str) -> list[dict[str, str]]:
|
||
"""构建货代发货 Agent 当前已知业务对象锚点。"""
|
||
return [
|
||
build_ref("task", task),
|
||
build_ref("batch", batch),
|
||
]
|
||
|
||
|
||
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;这里只补会话级上下文,供 Dify 画布通过 refs 稳定定位当前 task/batch。
|
||
"""
|
||
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)
|
||
task = _latest_task_for_conversation(conv_id)
|
||
|
||
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()
|
||
elif not focus_task and task and task.task_no:
|
||
focus_task = str(task.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 task
|
||
and getattr(task, "batch", None) is not None
|
||
and getattr(task.batch, "batch_no", None)
|
||
):
|
||
focus_batch = str(task.batch.batch_no or "").strip().upper()
|
||
|
||
hxops_context = build_hxops_context(
|
||
session_id=conv_id,
|
||
request_id=request_id,
|
||
trigger_source=trigger_source,
|
||
locale=locale,
|
||
refs=_build_hxops_refs(task=focus_task, batch=focus_batch),
|
||
)
|
||
if not hxops_context:
|
||
return {}
|
||
return {HXOPS_CONTEXT_INPUT: hxops_context}
|