feat(rohs-check): 新增 RoHS 检核工具进度查询功能及相关接口

This commit is contained in:
hugo committed 2026-06-11 10:05:29 +08:00
1 parent fee33f69d9
commit a18a07ef1a
19 files changed
+654 -58

No files matched your search

+2 -2
View File
@@ -7,14 +7,14 @@ from __future__ import annotations
from flask import current_app, has_request_context, request
def resolve_hxops_tool_api_base(*, config: dict | None = None, request_host: str | None = None) -> str:
def resolve_HXOPS_TOOLS_API_BASE(*, config: dict | None = None, request_host: str | None = None) -> str:
"""返回无尾斜杠的 hxops 对外根地址,如 http://192.168.100.192:8891。"""
cfg = config
if cfg is None and current_app:
cfg = current_app.config
cfg = cfg or {}
for key in ("HXOPS_TOOLS_API_BASE", "HXOPS_TOOL_API_BASE", "PUBLIC_BASE_URL"):
for key in ("HXOPS_TOOLS_API_BASE", "HXOPS_TOOLS_API_BASE", "PUBLIC_BASE_URL"):
configured = str(cfg.get(key) or "").strip().rstrip("/")
if configured:
return configured
@@ -2,7 +2,7 @@ from __future__ import annotations
from flask import current_app
from app.services.hxops_tool_api import resolve_hxops_tool_api_base
from app.services.hxops_tool_api import resolve_HXOPS_TOOLS_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)
@@ -12,9 +12,9 @@ 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()
base = resolve_HXOPS_TOOLS_API_BASE()
key = str(
current_app.config.get("ROHS_TOOL_API_KEY")
current_app.config.get("ROHS_TOOLS_API_KEY")
or current_app.config.get("INTEGRATION_API_KEY")
or ""
).strip()
@@ -7,7 +7,7 @@
},
"servers": [
{
"url": "http://192.168.100.192:8891",
"url": "http://192.168.100.192:8890",
"description": "部署时设为 HXOPS_TOOLS_API_BASE;GET /tools/openapi.json 会按服务端配置自动覆盖"
}
],
@@ -58,7 +58,7 @@
}
},
"409": {
"description": "检核进行中,同任务不可重复启动",
"description": "RoHS 检核进行中,全局不可重复启动",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/CheckRunningResponse" }
@@ -71,6 +71,41 @@
}
}
},
"/api/rohs-check/tools/runs": {
"get": {
"operationId": "list_rohs_runs",
"summary": "查询 RoHS 检核批次列表",
"tags": ["RoHS Check"],
"parameters": [
{
"name": "task_id",
"in": "query",
"required": false,
"schema": { "type": "integer", "example": 12 },
"description": "可选;指定检核任务 ID 时只返回该任务批次"
},
{
"name": "limit",
"in": "query",
"required": false,
"schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 },
"description": "返回条数,最大 100"
}
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/ListRunsResponse" }
}
}
},
"401": { "$ref": "#/components/responses/Unauthorized" },
"404": { "$ref": "#/components/responses/TaskNotFound" }
}
}
},
"/api/rohs-check/tools/runs/{run_id}": {
"get": {
"operationId": "get_rohs_run",
@@ -165,7 +200,7 @@
"BearerAuth": {
"type": "http",
"scheme": "bearer",
"description": "ROHS_TOOL_API_KEY 或 INTEGRATION_API_KEY"
"description": "ROHS_TOOLS_API_KEY 或 INTEGRATION_API_KEY"
},
"IntegrationKey": {
"type": "apiKey",
@@ -258,7 +293,8 @@
"conversation_id": { "type": "string", "nullable": true },
"user_message": { "type": "string", "nullable": true }
},
"description": "task_id 与 task_no 至少提供一个;优先使用 task_id"
"required": ["task_id"],
"description": "Chatflow Start 节点固定配置 task_id;后端仍兼容 task_no,但 RoHS Chatflow 不使用任务选择流程"
},
"RunCheckSuccessResponse": {
"type": "object",
@@ -287,6 +323,18 @@
"run": { "$ref": "#/components/schemas/RunDetail" }
}
},
"ListRunsResponse": {
"type": "object",
"required": ["ok", "runs", "count"],
"properties": {
"ok": { "type": "boolean", "example": true },
"count": { "type": "integer", "example": 2 },
"runs": {
"type": "array",
"items": { "$ref": "#/components/schemas/RunDetail" }
}
}
},
"ErrorResponse": {
"type": "object",
"required": ["ok", "error_code", "message"],
@@ -315,14 +363,14 @@
}
},
"MissingAuth": {
"description": "服务端未配置 ROHS_TOOL_API_KEY / INTEGRATION_API_KEY",
"description": "服务端未配置 ROHS_TOOLS_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"
"message": "ROHS_TOOLS_API_KEY or INTEGRATION_API_KEY is not configured"
}
}
}
+29 -6
View File
@@ -154,12 +154,38 @@ def _run_check_job_loop(
raise RuntimeError("dify_agent_id is set, but Dify api key is missing")
step_logs: list[dict[str, Any]] = []
details: list[dict[str, Any]] = []
success_count = 0
failed_count = 0
skipped_count = 0
all_errors: list[str] = []
def _save_progress(total: int | None = None) -> None:
if run_id is None:
return
try:
from app import db
from app.models import ScheduleRun
progress_run = db.session.get(ScheduleRun, run_id)
if not progress_run or progress_run.status != "running":
return
if total is not None:
progress_run.total_count = max(0, int(total))
progress_run.success_count = success_count
progress_run.failed_count = failed_count
progress_run.skipped_count = skipped_count
db.session.commit()
except Exception as progress_exc:
if current_app:
current_app.logger.warning("[%s] save progress failed run_id=%s error=%s", log_prefix, run_id, progress_exc)
t0 = _ts()
try:
rows = fetch_fn(conn_str)
if max_rows > 0:
rows = rows[:max_rows]
_save_progress(total=len(rows))
step_logs.append(_make_step(
"fetch_srm",
"success",
@@ -175,12 +201,6 @@ def _run_check_job_loop(
))
raise
details: list[dict[str, Any]] = []
success_count = 0
failed_count = 0
skipped_count = 0
all_errors: list[str] = []
for row in rows:
if run_id is not None and is_run_cancelled(run_id):
item = dict(row)
@@ -192,6 +212,7 @@ def _run_check_job_loop(
})
skipped_count += 1
details.append(item)
_save_progress()
continue
item = dict(row)
@@ -215,6 +236,7 @@ def _run_check_job_loop(
failed_count += 1
all_errors.append(pre_result.get("writeback_err", ""))
details.append(item)
_save_progress()
continue
record_text, attachment_url = build_record_fn(conn_str, item, row_id_value)
@@ -339,6 +361,7 @@ def _run_check_job_loop(
item["writeback_err"] = f"{item.get('writeback_err', '')}; writeback failed: {wb_exc}"
details.append(item)
_save_progress()
if len(step_logs) > _MAX_STEP_LOGS_PER_ROW:
step_logs = step_logs[:_MAX_STEP_LOGS_PER_ROW]
@@ -6,7 +6,7 @@ from app.services.shipping_notice.agent_resolver import CHAT_AGENT_NAME
from app.services.shipping_notice.tool_api_url import resolve_tool_api_base_url
# Dify Chatflow input variable names — must match canvas Start / 输入变量配置.
HXOPS_TOOL_API_BASE_INPUT = "hxops_tool_api_base"
HXOPS_TOOLS_API_BASE_INPUT = "HXOPS_TOOLS_API_BASE"
HXOPS_TOOL_API_KEY_INPUT = "hxops_tool_api_key"
@@ -20,7 +20,7 @@ def build_dify_chat_inputs() -> dict[str, str]:
key = str(current_app.config.get("SHIPPING_NOTICE_TOOL_API_KEY") or "").strip()
inputs: dict[str, str] = {}
if base:
inputs[HXOPS_TOOL_API_BASE_INPUT] = base
inputs[HXOPS_TOOLS_API_BASE_INPUT] = base
if key:
inputs[HXOPS_TOOL_API_KEY_INPUT] = key
return inputs
@@ -1,8 +1,8 @@
from __future__ import annotations
from app.services.hxops_tool_api import resolve_hxops_tool_api_base
from app.services.hxops_tool_api import resolve_HXOPS_TOOLS_API_BASE
def resolve_tool_api_base_url(*, config: dict | None = None, request_host: str | None = None) -> str:
"""Resolve hxops Tool API root URL (no trailing slash)."""
return resolve_hxops_tool_api_base(config=config, request_host=request_host)
return resolve_HXOPS_TOOLS_API_BASE(config=config, request_host=request_host)