- 移除不必要的状态引用 - 更新上传处理提示文本,使用国际化支持 - 扩展发货任务摘要接口,增加错误信息和短缺信息 - 更新多语言支持的文本,改善用户体验 - 增加批次工作区的汇总信息和操作提示
222 lines
9.4 KiB
Python
222 lines
9.4 KiB
Python
"""
|
|
services/shipping_notice/openapi_spec.py
|
|
|
|
用途:构建发货通知 Tool API 的 OpenAPI 描述,供 Dify 导入和联调用。
|
|
依赖:shipping_notice.constants 中的请求字段常量、标准 `typing` 类型。
|
|
逻辑:由 api/shipping_notice_tools.py 调用本文件生成 JSON/YAML OpenAPI 文档,再暴露给 Dify 或开发工具。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from app.services.shipping_notice.constants import MAP_JSON
|
|
|
|
|
|
def _str_prop(description: str, *, enum: list[str] | None = None) -> dict[str, Any]:
|
|
prop: dict[str, Any] = {"type": "string", "description": description}
|
|
if enum:
|
|
prop["enum"] = enum
|
|
return prop
|
|
|
|
|
|
def _int_prop(description: str) -> dict[str, Any]:
|
|
return {"type": "integer", "description": description}
|
|
|
|
|
|
def _post_op(
|
|
operation_id: str,
|
|
summary: str,
|
|
properties: dict[str, Any],
|
|
*,
|
|
required: list[str] | None = None,
|
|
) -> dict[str, Any]:
|
|
schema: dict[str, Any] = {"type": "object", "properties": properties}
|
|
if required:
|
|
schema["required"] = required
|
|
return {
|
|
"post": {
|
|
"operationId": operation_id,
|
|
"summary": summary,
|
|
"description": summary,
|
|
"parameters": [],
|
|
"requestBody": {
|
|
"required": True,
|
|
"content": {
|
|
"application/json": {
|
|
"schema": schema,
|
|
}
|
|
},
|
|
},
|
|
"responses": {
|
|
"200": {
|
|
"description": (
|
|
"Tool response (status: ok|running|needs_human|failed). "
|
|
"May include checkpoint, human_options, action_buttons."
|
|
),
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"type": "object"},
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
|
"""Build OpenAPI 3.0 spec for Dify Chatflow custom tools (agent-facing only)."""
|
|
server = str(base_url or "").strip().rstrip("/")
|
|
if not server.endswith("/api/shipping-notice"):
|
|
server = f"{server}/api/shipping-notice" if server else "/api/shipping-notice"
|
|
|
|
task_id = _str_prop("发货任务编号(task id),如 SN-20260529-ABCD1234")
|
|
batch_id = _str_prop("发货批次编号(batch id),如 SB-20260617-ABCD1234")
|
|
|
|
return {
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Shipping Notice Tools",
|
|
"version": "1.0.0",
|
|
"description": (
|
|
"货代发货通知 Agent Tool API。仅导出适合 Dify AI 编排层使用的工具:"
|
|
"通知理解、字段修订、库存映射建议、人工确认收口与状态查询;"
|
|
"不包含由后台任务统一续跑的主推进工具,也不包含 RPA 回调。"
|
|
"如果响应包含 action_buttons,Dify 只解释当前状态、业务原因和影响,"
|
|
"不要在自然语言正文里列出按钮清单或“推荐下一步”动作列表;"
|
|
"可点击按钮由宿主系统根据 action_buttons 统一渲染。"
|
|
"鉴权:Authorization Bearer 或 X-Integration-Key。"
|
|
),
|
|
},
|
|
"servers": [{"url": server}],
|
|
"paths": {
|
|
"/tools/parse_notice": _post_op(
|
|
"parse_notice",
|
|
"解析发货通知文本",
|
|
{
|
|
"task_id": task_id,
|
|
"raw_text": _str_prop("待解析的发货通知全文"),
|
|
"structured": {
|
|
"type": "object",
|
|
"description": "Dify LLM 解析后的结构化结果对象。若提供,后端优先使用该对象中的 lines/confidence/reservation_id 等字段落库。",
|
|
"additionalProperties": True,
|
|
},
|
|
"lang": _str_prop("可选语言代码:zh|en|th"),
|
|
},
|
|
required=["task_id"],
|
|
),
|
|
"/tools/update_notice_fields": _post_op(
|
|
"update_notice_fields",
|
|
"更新解析后的发货通知单头字段,如客户、PO、交期、收货地",
|
|
{
|
|
"task_id": task_id,
|
|
"fields": {
|
|
"type": "object",
|
|
"description": "可更新字段:customer_name/customer_code/ship_to/po_number/required_date",
|
|
"additionalProperties": True,
|
|
},
|
|
"comment": _str_prop("修改备注(可选)"),
|
|
},
|
|
required=["task_id", "fields"],
|
|
),
|
|
"/tools/update_notice_line": _post_op(
|
|
"update_notice_line",
|
|
"更新解析结果中的某一行,如 SKU、Reservation ID、数量、交期",
|
|
{
|
|
"task_id": task_id,
|
|
"line_index": _int_prop("行下标,从 0 开始"),
|
|
"patch": {
|
|
"type": "object",
|
|
"description": "可更新字段:sku/reservation_id/quantity/qty/required_date/uom",
|
|
"additionalProperties": True,
|
|
},
|
|
"comment": _str_prop("修改备注(可选)"),
|
|
},
|
|
required=["task_id", "line_index", "patch"],
|
|
),
|
|
"/tools/add_notice_line": _post_op(
|
|
"add_notice_line",
|
|
"向解析结果新增一行发货需求",
|
|
{
|
|
"task_id": task_id,
|
|
"line": {
|
|
"type": "object",
|
|
"description": "新增行,至少包含 sku 和 qty/quantity",
|
|
"additionalProperties": True,
|
|
},
|
|
"comment": _str_prop("修改备注(可选)"),
|
|
},
|
|
required=["task_id", "line"],
|
|
),
|
|
"/tools/remove_notice_line": _post_op(
|
|
"remove_notice_line",
|
|
"删除解析结果中的某一行发货需求",
|
|
{
|
|
"task_id": task_id,
|
|
"line_index": _int_prop("行下标,从 0 开始"),
|
|
"comment": _str_prop("修改备注(可选)"),
|
|
},
|
|
required=["task_id", "line_index"],
|
|
),
|
|
"/tools/get_inventory_schema_preview": _post_op(
|
|
"get_inventory_schema_preview",
|
|
"读取已上传库存 Excel 的表头与样例行(小 payload,供 LLM 推断列映射)",
|
|
{"task_id": task_id},
|
|
required=["task_id"],
|
|
),
|
|
"/tools/apply_inventory_mapping": _post_op(
|
|
"apply_inventory_mapping",
|
|
"应用 LLM/人工列映射并解析完整库存文件;返回 field_alignment 等待用户确认,后续主流程由后台任务续跑",
|
|
{
|
|
"task_id": task_id,
|
|
MAP_JSON: _str_prop(
|
|
'列映射 JSON,含 sku/reservation_id/available_qty;可选 match_rule(LLM 自然语言匹配说明,缺省则后端动态兜底)'
|
|
),
|
|
},
|
|
required=["task_id", MAP_JSON],
|
|
),
|
|
"/tools/submit_decision": _post_op(
|
|
"submit_decision",
|
|
"人工复核决策。task_id 与 conversation_id 至少传一个;用户确认时禁止 create_task。决策提交后由后台任务统一续跑。",
|
|
{
|
|
"task_id": task_id,
|
|
"conversation_id": _str_prop("Dify 会话 ID;用户仅回复「确认」时用于定位任务"),
|
|
"decision": _str_prop(
|
|
"confirm=确认继续;reject=驳回;retry_rpa=重试 RPA 库存导出;rematch=重新匹配;reparse=重新解析;"
|
|
"confirm_inventory_mapping=确认库存字段对齐;correct_inventory_mapping=更正列映射;reparse_inventory=重新上传库存",
|
|
enum=[
|
|
"confirm",
|
|
"confirm_parse",
|
|
"reject",
|
|
"retry_rpa",
|
|
"rematch",
|
|
"reparse",
|
|
"confirm_inventory_mapping",
|
|
"correct_inventory_mapping",
|
|
"reparse_inventory",
|
|
],
|
|
),
|
|
"comment": _str_prop("备注(可选)"),
|
|
},
|
|
required=["decision"],
|
|
),
|
|
"/tools/get_task_status": _post_op(
|
|
"get_task_status",
|
|
"查询任务当前状态与 human_options。task_id 与 conversation_id 至少传一个",
|
|
{
|
|
"task_id": task_id,
|
|
"conversation_id": _str_prop("Dify 会话 ID"),
|
|
},
|
|
),
|
|
"/tools/get_batch_status": _post_op(
|
|
"get_batch_status",
|
|
"查询批次当前状态、任务列表与当前聚焦任务",
|
|
{
|
|
"batch_no": batch_id,
|
|
},
|
|
required=["batch_no"],
|
|
),
|
|
},
|
|
}
|