Files
syops/backend/app/services/shipping_notice/dify_inputs.py
T
hugo 6a0a51313f feat(chat): 优化发货相关消息处理逻辑
- 在列出聊天消息时,过滤掉类型为'shipping-parse-review'的消息
- 移除不再使用的shippingConversationTaskIdCache
- 更新发货决策处理逻辑,简化保存消息的参数
- 添加发货动作按钮刷新时的任务定位优先级逻辑
- 移除解析复核消息的展示逻辑,统一处理到轻量工作台
- 增加单元测试,验证发货动作按钮刷新时的任务定位优先级
2026-07-01 08:57:36 +08:00

106 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、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 _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 _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)
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()
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}