94 lines
3.4 KiB
Python
94 lines
3.4 KiB
Python
"""
|
||
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"
|
||
|
||
|
||
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,
|
||
) -> 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_CONVERSATION_ID_INPUT: conv_id,
|
||
SHIPPING_CURRENT_BATCH_NO_INPUT: current_batch_no,
|
||
SHIPPING_CURRENT_TASK_NO_INPUT: current_task_no,
|
||
}
|