把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
142 lines
5.3 KiB
Python
142 lines
5.3 KiB
Python
"""
|
||
services/shipping_notice/tool_response.py
|
||
|
||
用途:统一构建发货通知 Tool 的 JSON 响应结构。
|
||
依赖:state_machine.resolve_task_ui_state(位置派生);ShippingProcessExecution。
|
||
逻辑:由 task_workflow、onhand_workflow、fulfillment_workflow 等 workflow 调用本文件,把状态机信息整理成一致的 Tool 响应;
|
||
已绑定 ProcessExecution 的任务不再下发旧动作字段(恢复仅由 Gate 驱动)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import TYPE_CHECKING, Any
|
||
|
||
if TYPE_CHECKING:
|
||
from app.models import ShippingNotice, ShippingOrderTask
|
||
|
||
|
||
def _task_has_process_execution(task: ShippingOrderTask | ShippingNotice | None) -> bool:
|
||
"""
|
||
判断任务是否已绑定 ProcessExecution(Gate 为恢复真相源)。
|
||
|
||
Args:
|
||
task: ShippingNotice 或 None。
|
||
|
||
Returns:
|
||
存在任意关联 Execution 时为 True。
|
||
"""
|
||
if task is None or getattr(task, 'id', None) is None:
|
||
return False
|
||
from app.models import ShippingNotice, ShippingProcessExecution
|
||
|
||
if isinstance(task, ShippingNotice):
|
||
task_id = int(task.order_task_id) if task.order_task_id is not None else None
|
||
else:
|
||
task_id = int(task.id) if getattr(task, 'id', None) is not None else None
|
||
if task_id is None:
|
||
return False
|
||
return (
|
||
ShippingProcessExecution.query
|
||
.filter_by(task_id=task_id)
|
||
.filter(ShippingProcessExecution.status != 'superseded')
|
||
.first()
|
||
is not None
|
||
)
|
||
|
||
|
||
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: ShippingNotice | 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,
|
||
position: 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 响应。
|
||
|
||
核心逻辑:未显式传 position 时从 task 派生位置;显式 position 优先
|
||
(如 parse_ok、task_exists)。若任务已有 ProcessExecution:不再填充旧动作字段,
|
||
避免与 Gate 双真相。
|
||
|
||
Args:
|
||
position: 进程位置标签;None 时从 resolve_task_ui_state(task).position 推导。
|
||
stop_agent_turn: True 时要求 Agent 本回合停止,等待用户操作。
|
||
forbidden_next_tools: 禁止 Agent 同轮调用的 Tool 名称列表。
|
||
extra: 需要保留在顶层的额外业务字段。
|
||
"""
|
||
from app.services.shipping_notice.state_machine import resolve_task_ui_state
|
||
|
||
if task is not None and not task_id:
|
||
task_id = task.task_no
|
||
|
||
gate_owns_recovery = _task_has_process_execution(task)
|
||
# action_buttons 是历史调用参数,仅为兼容调用方签名;绝不进入对外响应。
|
||
del action_buttons
|
||
resolved_position = position
|
||
# 旧 human_options 参数仅保留调用签名兼容,绝不返回。用户动作必须由 Gate DTO 授权。
|
||
del human_options
|
||
if position is None and task is not None:
|
||
ui = resolve_task_ui_state(task)
|
||
resolved_position = ui.position
|
||
|
||
payload: dict[str, Any] = {
|
||
"status": status,
|
||
"message": _sanitize_user_facing_text(message),
|
||
"data": data or {},
|
||
}
|
||
if task_id:
|
||
payload["task_id"] = task_id
|
||
# [STEP] 业务按钮统一由 ui_state.actions 返回;Tool 响应不再生成旧 human_options/action_buttons。
|
||
if resolved_position:
|
||
payload["position"] = resolved_position
|
||
if next_required_action and not gate_owns_recovery:
|
||
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)
|
||
# [STEP] extra 不得绕过 Gate 绑定任务的旧 next_required_action 约束。
|
||
if gate_owns_recovery:
|
||
payload.pop("next_required_action", None)
|
||
return payload
|