feat(rohs-check): 新增 RoHS 检核工具 API 及相关功能
This commit is contained in:
1 parent
8a541211b7
commit
24551e9a9a
39 files changed
+3128
-189
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,21 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.models import Agent
|
||||
|
||||
# Dify / HXOPS Agent.name 常见取值(含「助手」后缀)
|
||||
CHAT_AGENT_NAMES = frozenset(
|
||||
{
|
||||
"RoHS文件检核",
|
||||
"RoHS文件检核助手",
|
||||
"RoHS 文件检核助手",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def is_rohs_check_chat_agent(agent) -> bool:
|
||||
name = (getattr(agent, "name", None) or "").strip()
|
||||
return name in CHAT_AGENT_NAMES
|
||||
|
||||
|
||||
def resolve_chat_agent() -> Agent | None:
|
||||
return Agent.query.filter(Agent.name.in_(CHAT_AGENT_NAMES), Agent.is_active.is_(True)).first()
|
||||
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.services.hxops_tool_api import resolve_hxops_tool_api_base
|
||||
from app.services.rohs_check_tools.agent_resolver import is_rohs_check_chat_agent
|
||||
|
||||
# Dify Chatflow「用户输入」变量名 — 须与画布 Start 节点配置一致(见 docs/rohs_chatflow_guide.md §3.1)
|
||||
HXOPS_TOOLS_API_BASE_INPUT = "HXOPS_TOOLS_API_BASE"
|
||||
HXOPS_TOOL_API_TOKEN_INPUT = "hxops_tool_api_token"
|
||||
|
||||
|
||||
def build_dify_chat_inputs() -> dict[str, str]:
|
||||
"""Build Dify chat API inputs for RoHS Chatflow HTTP nodes."""
|
||||
base = resolve_hxops_tool_api_base()
|
||||
key = str(
|
||||
current_app.config.get("ROHS_TOOL_API_KEY")
|
||||
or current_app.config.get("INTEGRATION_API_KEY")
|
||||
or ""
|
||||
).strip()
|
||||
inputs: dict[str, str] = {}
|
||||
if base:
|
||||
inputs[HXOPS_TOOLS_API_BASE_INPUT] = base
|
||||
if key:
|
||||
inputs[HXOPS_TOOL_API_TOKEN_INPUT] = key
|
||||
return inputs
|
||||
|
||||
|
||||
__all__ = [
|
||||
"HXOPS_TOOLS_API_BASE_INPUT",
|
||||
"HXOPS_TOOL_API_TOKEN_INPUT",
|
||||
"build_dify_chat_inputs",
|
||||
"is_rohs_check_chat_agent",
|
||||
]
|
||||
@@ -0,0 +1,387 @@
|
||||
{
|
||||
"openapi": "3.0.3",
|
||||
"info": {
|
||||
"title": "HXOPS RoHS Check Tool API",
|
||||
"description": "RoHS 文件检核 Chatflow 显式 HTTP Tool API:检核任务列表、发起检核、查询检核批次、下载检核报告。鉴权:Authorization Bearer 或 X-Integration-Key。",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "http://192.168.100.192:8891",
|
||||
"description": "部署时设为 HXOPS_TOOLS_API_BASE;GET /tools/openapi.json 会按服务端配置自动覆盖"
|
||||
}
|
||||
],
|
||||
"security": [
|
||||
{ "BearerAuth": [] },
|
||||
{ "IntegrationKey": [] }
|
||||
],
|
||||
"paths": {
|
||||
"/api/rohs-check/tools/tasks": {
|
||||
"get": {
|
||||
"operationId": "list_rohs_tasks",
|
||||
"summary": "查询 RoHS 检核任务列表",
|
||||
"tags": ["RoHS Check"],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "成功",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ListTasksResponse" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": { "$ref": "#/components/responses/Unauthorized" },
|
||||
"503": { "$ref": "#/components/responses/MissingAuth" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/rohs-check/tools/tasks/run": {
|
||||
"post": {
|
||||
"operationId": "run_rohs_check",
|
||||
"summary": "异步启动 RoHS 检核",
|
||||
"tags": ["RoHS Check"],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/RunCheckRequest" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "启动成功",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/RunCheckSuccessResponse" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"409": {
|
||||
"description": "检核进行中,同任务不可重复启动",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/CheckRunningResponse" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": { "$ref": "#/components/responses/BadRequest" },
|
||||
"401": { "$ref": "#/components/responses/Unauthorized" },
|
||||
"404": { "$ref": "#/components/responses/TaskNotFound" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/rohs-check/tools/runs/{run_id}": {
|
||||
"get": {
|
||||
"operationId": "get_rohs_run",
|
||||
"summary": "查询指定检核批次",
|
||||
"tags": ["RoHS Check"],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "run_id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": { "type": "integer", "example": 88 }
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "成功",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/GetRunResponse" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": { "$ref": "#/components/responses/Unauthorized" },
|
||||
"404": { "$ref": "#/components/responses/RunNotFound" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/rohs-check/tools/tasks/{task_id}/latest-run": {
|
||||
"get": {
|
||||
"operationId": "get_latest_rohs_run",
|
||||
"summary": "查询检核任务最近一次检核",
|
||||
"tags": ["RoHS Check"],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "task_id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": { "type": "integer", "example": 12 }
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "成功",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/GetRunResponse" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": { "$ref": "#/components/responses/Unauthorized" },
|
||||
"404": { "$ref": "#/components/responses/RunNotFound" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/rohs-check/tools/runs/{run_id}/excel": {
|
||||
"get": {
|
||||
"operationId": "download_rohs_run_excel",
|
||||
"summary": "下载检核报告 Excel(短期签名 token)",
|
||||
"tags": ["RoHS Check"],
|
||||
"security": [],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "run_id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": { "type": "integer", "example": 88 }
|
||||
},
|
||||
{
|
||||
"name": "token",
|
||||
"in": "query",
|
||||
"required": true,
|
||||
"description": "由 get_rohs_run 返回的 excel.download_url 中的 JWT,有效期 600 秒",
|
||||
"schema": { "type": "string" }
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Excel 文件流",
|
||||
"content": {
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
|
||||
"schema": { "type": "string", "format": "binary" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"409": { "$ref": "#/components/responses/ExcelNotReady" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"securitySchemes": {
|
||||
"BearerAuth": {
|
||||
"type": "http",
|
||||
"scheme": "bearer",
|
||||
"description": "ROHS_TOOL_API_KEY 或 INTEGRATION_API_KEY"
|
||||
},
|
||||
"IntegrationKey": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "X-Integration-Key",
|
||||
"description": "与 Bearer 二选一"
|
||||
}
|
||||
},
|
||||
"schemas": {
|
||||
"TaskSummary": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"task_id": { "type": "integer", "example": 12 },
|
||||
"task_no": { "type": "string", "example": "CHK202606090001" },
|
||||
"name": { "type": "string", "example": "RoHS 自动检核" },
|
||||
"enabled": { "type": "boolean" },
|
||||
"last_status": { "type": "string", "nullable": true, "example": "success" },
|
||||
"last_run_at": { "type": "string", "format": "date-time", "nullable": true },
|
||||
"next_run_at": { "type": "string", "format": "date-time", "nullable": true }
|
||||
}
|
||||
},
|
||||
"RunExcel": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"file_name": { "type": "string", "nullable": true, "example": "RUN202606090001.xlsx" },
|
||||
"file_size": { "type": "integer", "nullable": true, "example": 45678 },
|
||||
"download_url": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"example": "http://192.168.100.192:8891/api/rohs-check/tools/runs/88/excel?token=..."
|
||||
}
|
||||
}
|
||||
},
|
||||
"RunKnowledgeBase": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"enabled": { "type": "boolean" },
|
||||
"sync_status": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"enum": ["skipped", "queued", "cleaning", "syncing", "ready", "success", "failed"]
|
||||
},
|
||||
"group_id": { "type": "integer", "nullable": true },
|
||||
"document_id": { "type": "integer", "nullable": true },
|
||||
"document_version_id": { "type": "integer", "nullable": true },
|
||||
"sync_job_id": { "type": "integer", "nullable": true },
|
||||
"error_message": { "type": "string", "nullable": true }
|
||||
}
|
||||
},
|
||||
"RunDetail": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"run_id": { "type": "integer", "example": 88 },
|
||||
"run_no": { "type": "string", "example": "RUN202606090001" },
|
||||
"task_id": { "type": "integer", "example": 12 },
|
||||
"task_name": { "type": "string", "example": "RoHS 自动检核" },
|
||||
"status": {
|
||||
"type": "string",
|
||||
"enum": ["running", "success", "partial_failed", "failed", "queued", "cancelled"]
|
||||
},
|
||||
"total_count": { "type": "integer" },
|
||||
"success_count": { "type": "integer" },
|
||||
"failed_count": { "type": "integer" },
|
||||
"skipped_count": { "type": "integer" },
|
||||
"started_at": { "type": "string", "format": "date-time", "nullable": true },
|
||||
"finished_at": { "type": "string", "format": "date-time", "nullable": true },
|
||||
"duration_ms": { "type": "integer", "nullable": true },
|
||||
"error_message": { "type": "string", "nullable": true },
|
||||
"excel": { "$ref": "#/components/schemas/RunExcel" },
|
||||
"knowledge_base": { "$ref": "#/components/schemas/RunKnowledgeBase" }
|
||||
}
|
||||
},
|
||||
"ListTasksResponse": {
|
||||
"type": "object",
|
||||
"required": ["ok", "tasks"],
|
||||
"properties": {
|
||||
"ok": { "type": "boolean", "example": true },
|
||||
"tasks": {
|
||||
"type": "array",
|
||||
"items": { "$ref": "#/components/schemas/TaskSummary" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"RunCheckRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"task_id": { "type": "integer", "example": 12 },
|
||||
"task_no": { "type": "string", "example": "CHK202606090001" },
|
||||
"trigger_source": { "type": "string", "default": "dify_chatflow" },
|
||||
"conversation_id": { "type": "string", "nullable": true },
|
||||
"user_message": { "type": "string", "nullable": true }
|
||||
},
|
||||
"description": "task_id 与 task_no 至少提供一个;优先使用 task_id"
|
||||
},
|
||||
"RunCheckSuccessResponse": {
|
||||
"type": "object",
|
||||
"required": ["ok", "message", "run"],
|
||||
"properties": {
|
||||
"ok": { "type": "boolean", "example": true },
|
||||
"message": { "type": "string", "example": "检核已启动" },
|
||||
"run": { "$ref": "#/components/schemas/RunDetail" }
|
||||
}
|
||||
},
|
||||
"CheckRunningResponse": {
|
||||
"type": "object",
|
||||
"required": ["ok", "error_code", "message", "run"],
|
||||
"properties": {
|
||||
"ok": { "type": "boolean", "example": false },
|
||||
"error_code": { "type": "string", "example": "check_running" },
|
||||
"message": { "type": "string", "example": "该检核任务正在执行中,请稍后查询结果" },
|
||||
"run": { "$ref": "#/components/schemas/RunDetail" }
|
||||
}
|
||||
},
|
||||
"GetRunResponse": {
|
||||
"type": "object",
|
||||
"required": ["ok", "run"],
|
||||
"properties": {
|
||||
"ok": { "type": "boolean", "example": true },
|
||||
"run": { "$ref": "#/components/schemas/RunDetail" }
|
||||
}
|
||||
},
|
||||
"ErrorResponse": {
|
||||
"type": "object",
|
||||
"required": ["ok", "error_code", "message"],
|
||||
"properties": {
|
||||
"ok": { "type": "boolean", "example": false },
|
||||
"error_code": { "type": "string" },
|
||||
"message": { "type": "string" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"Unauthorized": {
|
||||
"description": "鉴权失败",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"examples": {
|
||||
"missing_auth": {
|
||||
"value": { "ok": false, "error_code": "missing_auth", "message": "missing_auth" }
|
||||
},
|
||||
"invalid_auth": {
|
||||
"value": { "ok": false, "error_code": "invalid_auth", "message": "invalid_auth" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"MissingAuth": {
|
||||
"description": "服务端未配置 ROHS_TOOL_API_KEY / INTEGRATION_API_KEY",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"example": {
|
||||
"ok": false,
|
||||
"error_code": "missing_auth",
|
||||
"message": "ROHS_TOOL_API_KEY or INTEGRATION_API_KEY is not configured"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"BadRequest": {
|
||||
"description": "请求无效或检核任务已停用",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"example": {
|
||||
"ok": false,
|
||||
"error_code": "task_disabled",
|
||||
"message": "task_disabled"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"TaskNotFound": {
|
||||
"description": "检核任务不存在",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"example": {
|
||||
"ok": false,
|
||||
"error_code": "task_not_found",
|
||||
"message": "task_not_found"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"RunNotFound": {
|
||||
"description": "检核批次不存在",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"example": {
|
||||
"ok": false,
|
||||
"error_code": "run_not_found",
|
||||
"message": "run_not_found"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"ExcelNotReady": {
|
||||
"description": "Excel 尚未生成",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": { "$ref": "#/components/schemas/ErrorResponse" },
|
||||
"example": {
|
||||
"ok": false,
|
||||
"error_code": "excel_not_ready",
|
||||
"message": "excel_not_ready"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
_BASE_SPEC_PATH = Path(__file__).with_name("openapi_base.json")
|
||||
|
||||
|
||||
def _load_base_spec() -> dict[str, Any]:
|
||||
with _BASE_SPEC_PATH.open("r", encoding="utf-8") as fp:
|
||||
return json.load(fp)
|
||||
|
||||
|
||||
def build_rohs_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
"""Build OpenAPI 3.0 spec for RoHS Chatflow HTTP tools."""
|
||||
spec = copy.deepcopy(_load_base_spec())
|
||||
base = str(base_url or "").strip().rstrip("/")
|
||||
servers = spec.get("servers")
|
||||
if base and isinstance(servers, list) and servers:
|
||||
servers[0]["url"] = base
|
||||
servers[0]["description"] = "HXOPS_TOOLS_API_BASE"
|
||||
return spec
|
||||
Reference in new issue
Block a user