124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
"""
|
|
backend/app/services/maintenance_tools/openapi_spec.py
|
|
|
|
用途:构建设备维保助手 Dify Tool API 的 OpenAPI 描述。
|
|
依赖:标准 typing。
|
|
逻辑:/api/maintenance/tools/openapi.json 调用本模块;Dify 导入后可调用只读设备台账与维修记录查询工具。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def _str_prop(description: str) -> dict[str, Any]:
|
|
"""构造字符串字段描述。"""
|
|
return {"type": "string", "description": description}
|
|
|
|
|
|
def _bool_prop(description: str, *, default: bool = False) -> dict[str, Any]:
|
|
"""构造布尔字段描述。"""
|
|
return {"type": "boolean", "description": description, "default": default}
|
|
|
|
|
|
def _int_prop(description: str, *, default: int | None = None) -> dict[str, Any]:
|
|
"""构造整数字段描述。"""
|
|
prop: dict[str, Any] = {"type": "integer", "description": description}
|
|
if default is not None:
|
|
prop["default"] = default
|
|
return prop
|
|
|
|
|
|
def _post_op(
|
|
operation_id: str,
|
|
summary: str,
|
|
properties: dict[str, Any],
|
|
*,
|
|
required: list[str] | None = None,
|
|
) -> dict[str, Any]:
|
|
"""构造 Dify Tool 兼容的 POST OpenAPI 操作。"""
|
|
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. ok=true 表示调用成功;matched=false 表示未匹配到设备或记录。",
|
|
"content": {"application/json": {"schema": {"type": "object"}}},
|
|
}
|
|
},
|
|
"security": [{"BearerAuth": []}, {"IntegrationKey": []}],
|
|
}
|
|
}
|
|
|
|
|
|
def build_maintenance_openapi_spec(base_url: str) -> dict[str, Any]:
|
|
"""
|
|
构建设备维保 Dify Tool OpenAPI。
|
|
Args:
|
|
base_url: HXOPS 对外 Tool API 根地址。
|
|
Returns:
|
|
OpenAPI 3.0 dict。
|
|
"""
|
|
server = str(base_url or "").strip().rstrip("/")
|
|
if not server.endswith("/api/maintenance"):
|
|
server = f"{server}/api/maintenance" if server else "/api/maintenance"
|
|
|
|
return {
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Maintenance Tools",
|
|
"version": "1.0.0",
|
|
"description": (
|
|
"设备维保助手只读 Tool API。适合 Dify 处理复杂自然语言问法时查询设备台账、最近维修记录;"
|
|
"新增、编辑、删除等配置动作不得通过 Dify Tool 执行,应由 HXOPS 右侧管理面板完成。"
|
|
"鉴权:Authorization Bearer 或 X-Integration-Key。"
|
|
),
|
|
},
|
|
"servers": [{"url": server, "description": "HXOPS_TOOLS_API_BASE"}],
|
|
"paths": {
|
|
"/tools/query_device": _post_op(
|
|
"queryMaintenanceDevice",
|
|
"查询设备台账信息,可附带最近维修记录",
|
|
{
|
|
"keyword": _str_prop("设备编号或设备名称,例如 A022700978 或 注塑件自动冲切水口机"),
|
|
"include_records": _bool_prop("是否返回最近维修记录", default=True),
|
|
"record_limit": _int_prop("最多返回几条最近维修记录,建议 3-10", default=5),
|
|
},
|
|
required=["keyword"],
|
|
),
|
|
"/tools/query_records": _post_op(
|
|
"queryMaintenanceRecords",
|
|
"查询设备维修记录",
|
|
{
|
|
"keyword": _str_prop("设备编号或设备名称关键词"),
|
|
"status": _str_prop("维修状态,可选:pending、processing、done、cancelled"),
|
|
"limit": _int_prop("最多返回记录数,建议 5-20", default=10),
|
|
},
|
|
required=["keyword"],
|
|
),
|
|
},
|
|
"components": {
|
|
"securitySchemes": {
|
|
"BearerAuth": {"type": "http", "scheme": "bearer"},
|
|
"IntegrationKey": {
|
|
"type": "apiKey",
|
|
"in": "header",
|
|
"name": "X-Integration-Key",
|
|
"description": "与 Bearer 二选一",
|
|
},
|
|
}
|
|
},
|
|
"security": [{"BearerAuth": []}, {"IntegrationKey": []}],
|
|
}
|
|
|