84 lines
2.9 KiB
Python
84 lines
2.9 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 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,
|
|
) -> dict[str, Any]:
|
|
"""Build a shipping-notice tool JSON response.
|
|
|
|
When ``checkpoint`` is omitted and ``task`` is given, checkpoint / human_options /
|
|
action_buttons are derived from ``resolve_task_ui_state(task)`` (single read path).
|
|
Explicit ``checkpoint`` wins (e.g. ``parse_ok``, ``task_exists``).
|
|
"""
|
|
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": 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"] = next_required_action
|
|
if forbidden_next_tools:
|
|
payload["forbidden_next_tools"] = forbidden_next_tools
|
|
if stop_agent_turn:
|
|
payload["stop_agent_turn"] = True
|
|
return payload
|