- 移除不必要的状态引用 - 更新上传处理提示文本,使用国际化支持 - 扩展发货任务摘要接口,增加错误信息和短缺信息 - 更新多语言支持的文本,改善用户体验 - 增加批次工作区的汇总信息和操作提示
122 lines
4.4 KiB
Python
122 lines
4.4 KiB
Python
"""
|
||
services/shipping_notice/tool_response.py
|
||
|
||
用途:统一构建发货通知 Tool 的 JSON 响应结构。
|
||
依赖:state_machine 中的 checkpoint/human_options/action_buttons 解析能力。
|
||
逻辑:由 task_workflow、inventory_workflow、fulfillment_workflow 等 workflow 调用本文件,把状态机信息整理成一致的 Tool 响应。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import TYPE_CHECKING, Any
|
||
|
||
if TYPE_CHECKING:
|
||
from app.models import ShippingTask
|
||
|
||
|
||
def _sanitize_user_facing_text(value: str) -> str:
|
||
"""
|
||
清理会被 Agent 复述给业务用户的内部项目名。
|
||
|
||
核心逻辑:Tool response 会进入 Dify 上下文,先在后端把项目代号替换为业务表达,
|
||
避免模型把内部系统名称带到对话气泡中。
|
||
"""
|
||
text = str(value or "")
|
||
replacements = (
|
||
("通过 Hxops 聊天区上传", "在当前对话中上传"),
|
||
("通过 HXOPS 聊天区上传", "在当前对话中上传"),
|
||
("通过 hxops 聊天区上传", "在当前对话中上传"),
|
||
("在 Hxops 聊天区上传", "在当前对话中上传"),
|
||
("在 HXOPS 聊天区上传", "在当前对话中上传"),
|
||
("在 hxops 聊天区上传", "在当前对话中上传"),
|
||
("Hxops 聊天区", "当前对话"),
|
||
("HXOPS 聊天区", "当前对话"),
|
||
("hxops 聊天区", "当前对话"),
|
||
("Hxops Chat", "当前对话"),
|
||
("HXOPS Chat", "当前对话"),
|
||
("hxops chat", "当前对话"),
|
||
("Hxops", "当前系统"),
|
||
("HXOPS", "当前系统"),
|
||
("hxops", "当前系统"),
|
||
)
|
||
for old, new in replacements:
|
||
text = text.replace(old, new)
|
||
return text
|
||
|
||
|
||
def tool_response(
|
||
*,
|
||
status: str,
|
||
task: ShippingTask | None = None,
|
||
task_id: str | None = None,
|
||
message: str = "",
|
||
data: dict[str, Any] | None = None,
|
||
human_options: list[str] | None = None,
|
||
action_buttons: list[dict[str, str]] | None = None,
|
||
checkpoint: str | None = None,
|
||
next_required_action: str | None = None,
|
||
forbidden_next_tools: list[str] | None = None,
|
||
stop_agent_turn: bool = False,
|
||
extra: dict[str, Any] | None = None,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
构建发货通知 Tool 标准 JSON 响应。
|
||
|
||
核心逻辑:未显式传 checkpoint 时从 task DB 状态推导 checkpoint/human_options/action_buttons;
|
||
显式 checkpoint 优先(如 parse_ok、task_exists)。
|
||
|
||
Args:
|
||
stop_agent_turn: True 时要求 Agent 本回合停止,等待用户操作。
|
||
forbidden_next_tools: 禁止 Agent 同轮调用的 Tool 名称列表。
|
||
extra: 需要保留在顶层的兼容字段,例如 batch_no。
|
||
"""
|
||
from app.services.shipping_notice.state_machine import (
|
||
action_buttons_for_checkpoint,
|
||
build_action_buttons,
|
||
resolve_task_ui_state,
|
||
)
|
||
|
||
if task is not None and not task_id:
|
||
task_id = task.task_no
|
||
|
||
resolved_checkpoint = checkpoint
|
||
resolved_human_options = human_options
|
||
resolved_action_buttons = action_buttons
|
||
|
||
if checkpoint is None and task is not None:
|
||
ui = resolve_task_ui_state(task)
|
||
resolved_checkpoint = ui.checkpoint
|
||
if human_options is None:
|
||
resolved_human_options = ui.human_options
|
||
if action_buttons is None and ui.action_buttons:
|
||
resolved_action_buttons = ui.action_buttons
|
||
|
||
payload: dict[str, Any] = {
|
||
"status": status,
|
||
"message": _sanitize_user_facing_text(message),
|
||
"data": data or {},
|
||
}
|
||
if task_id:
|
||
payload["task_id"] = task_id
|
||
if resolved_human_options is not None:
|
||
payload["human_options"] = resolved_human_options
|
||
if resolved_action_buttons is not None:
|
||
payload["action_buttons"] = resolved_action_buttons
|
||
elif resolved_human_options is not None:
|
||
payload["action_buttons"] = build_action_buttons(resolved_human_options)
|
||
elif resolved_checkpoint:
|
||
buttons = action_buttons_for_checkpoint(resolved_checkpoint)
|
||
if buttons:
|
||
payload["action_buttons"] = buttons
|
||
if resolved_checkpoint:
|
||
payload["checkpoint"] = resolved_checkpoint
|
||
if next_required_action:
|
||
payload["next_required_action"] = _sanitize_user_facing_text(next_required_action)
|
||
if forbidden_next_tools:
|
||
payload["forbidden_next_tools"] = forbidden_next_tools
|
||
if stop_agent_turn:
|
||
payload["stop_agent_turn"] = True
|
||
if extra:
|
||
payload.update(extra)
|
||
return payload
|