Files
syops/backend/app/services/shipping_notice/openapi_spec.py
T

106 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
services/shipping_notice/openapi_spec.py
用途:构建发货通知 Tool API 的 OpenAPI 描述,供 Dify 导入和联调用。
依赖:标准 `typing` 类型。
逻辑:由 api/shipping_notice_tools.py 调用本文件生成 JSON/YAML OpenAPI 文档,再暴露给 Dify 或开发工具;
co8 起不再导出 get_batch_status(shipping_batches 已 DROP)。
"""
from __future__ import annotations
from typing import Any
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,
description: 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": str(description or summary),
"parameters": [],
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": schema,
}
},
},
"responses": {
"200": {
"description": (
"Tool response (status: ok|running|needs_human|failed). "
"May include position; business buttons are exposed only by ui_state.actions."
),
"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("发货任务引用:技术任务号 SN-20260529-ABCD1234 或业务号 WYC26071501")
capability_token = _str_prop("HXOPS 每轮注入的短效只读能力令牌;必须原样透传")
return {
"openapi": "3.0.0",
"info": {
"title": "Shipping Notice Tools",
"version": "1.0.0",
"description": (
"货代 Chatflow 仅作自然语言只读解释。所有写操作必须由 HXOPS 确认卡和后端状态机执行;"
"本 OpenAPI 不包含改单、确认、冻结、库存、Excel 或上传接口。"
),
},
"servers": [{"url": server}],
"paths": {
"/tools/get_task_status": _post_op(
"get_task_status",
"查询任务当前状态;响应含库存源状态与候选结构",
{
"task_id": task_id,
"conversation_id": _str_prop("Dify 会话 ID"),
"shipping_chat_capability_token": capability_token,
},
required=["task_id", "shipping_chat_capability_token"],
description=(
"返回 inventory_source_code、inventory_warehouse_id、"
"inventory_source_status(unresolved|ambiguous|resolved|"
"candidates_frozen|invalid|conflicted)、inventory_source_method、"
"inventory_source_config_version、inventory_routing_fingerprint、"
"以及 step_payload.inventory_source_resolution.candidates。"
),
),
},
}