118 lines
4.5 KiB
Python
118 lines
4.5 KiB
Python
"""
|
||
services/shipping_notice/dify_inputs.py
|
||
|
||
用途:识别发货通知 Dify Chatflow Agent,并构建会话级 Dify inputs。
|
||
依赖:agent_runtime_context 平台协议、models.ShippingBatch/ShippingTask、shipping_notice.runtime_context。
|
||
逻辑:由 Context Adapter / api/chat 调用;本模块按会话号解析当前批次/任务焦点,
|
||
仅输出 agent_runtime_context JSON 字符串,供 Dify 画布统一读取平台运行态快照。
|
||
发货通知识别请使用 agent_registry.resolver.resolve_agent_code == 'shipping_notice'。
|
||
"""
|
||
|
||
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.runtime_context import build_shipping_runtime_context
|
||
|
||
|
||
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),
|
||
}
|