From e6a44df2603a11109aafc2dbb46f9856ba9cab87 Mon Sep 17 00:00:00 2001 From: caohuipeng Date: Mon, 17 Aug 2026 22:07:07 +0800 Subject: [PATCH] =?UTF-8?q?feat(file-check-tool):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E8=A7=A6=E5=8F=91=E6=A3=80=E6=A0=B8=E6=97=A0?= =?UTF-8?q?=E5=BE=85=E6=A3=80=E8=B5=84=E6=96=99=E6=97=B6=E5=B1=95=E7=A4=BA?= =?UTF-8?q?100%=E5=AE=8C=E6=88=90=E8=BF=9B=E5=BA=A6=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新工具API的OpenAPI定义,新增NoDataResponse结构体;修改预检与响应逻辑,无待检资料时创建已完成的空批次并绑定当前对话;同步更新相关文档并补充测试用例验证流程正确性。 --- backend/app/api/file_check_tools.py | 75 +++++++++++++++++-- .../file_check_tools/openapi_base.json | 35 ++++++++- backend/tests/test_file_check_tools.py | 25 +++++-- docs/demand.md | 1 + docs/features/file-check/technical.md | 5 +- docs/funclist.md | 2 +- docs/openapi/file_check_tools_openapi.json | 50 ++++++++++++- 7 files changed, 177 insertions(+), 16 deletions(-) diff --git a/backend/app/api/file_check_tools.py b/backend/app/api/file_check_tools.py index d72c0e76..f241700e 100644 --- a/backend/app/api/file_check_tools.py +++ b/backend/app/api/file_check_tools.py @@ -345,12 +345,26 @@ def _bind_queried_run_to_turn(run: ScheduleRun, data: dict) -> None: attach_run_to_turn(turn_id, int(run.id)) -def _build_no_data_response(task: ScheduleTask) -> dict: - """无待检资料时直接返回提示,不创建批次也不绑定 run_id。""" +def _build_no_data_response(task: ScheduleTask, run: ScheduleRun) -> dict: + """ + 组装无待检资料的 Tool 响应,并返回本轮已完成的空批次。 + + 【AI 触发时机】:用户要求立即执行文件检核,但预检未发现待检资料时调用。 + 核心逻辑:保留 no_data_to_check 供 Chatflow 回复无资料提示,同时携带已绑定的 Run, + 使前端能在该轮消息下展示总数 0、进度 100% 的完成进度卡。 + + Args: + task: 当前检核任务。 + run: 已持久化并绑定当前对话的成功空批次。 + + Returns: + dict: 包含 no_data_to_check、任务信息和 Run 摘要的 HTTP 200 响应体。 + """ return { "ok": False, "error_code": "no_data_to_check", "message": "目前没有资料需要检核", + "run": _serialize_tool_run(run), "task": { "task_id": task.id, "task_name": task.name, @@ -380,8 +394,8 @@ def _precheck_no_data(task: ScheduleTask) -> dict | None: """ 启动前预检当前任务是否存在待检资料。 - 核心逻辑:仅在数据库型检核任务上做轻量预检;若结果为空,则直接返回 no_data_to_check, - 避免创建空批次并向 ChatLog 回写 run_id。数据源连接失败时抛 503 data_source_unavailable。 + 核心逻辑:仅在数据库型检核任务上做轻量预检;若结果为空,则返回 no_data_to_check 标识, + 由启动入口创建并绑定完成态的 0 笔 Run。数据源连接失败时抛 503 data_source_unavailable。 """ if str(getattr(task, "data_source_type", "") or "database").lower() != "database": return None @@ -405,7 +419,52 @@ def _precheck_no_data(task: ScheduleTask) -> dict | None: if rows: return None - return _build_no_data_response(task) + return { + "ok": False, + "error_code": "no_data_to_check", + "message": "目前没有资料需要检核", + } + + +def _create_no_data_run(task: ScheduleTask, data: dict) -> ScheduleRun: + """ + 为预检为空的立即执行请求创建并绑定已完成的 0 笔检核批次。 + + 【AI 触发时机】:run_file_check 收到 no_data_to_check 预检结果时调用。 + 核心逻辑:不启动后台 worker,仅写入 success Run 与对话绑定;完成态空批次由工作区进度接口 + 序列化为 100%,让用户明确知道本次检核已经结束且没有待处理资料。 + + Args: + task: 已解析的文件检核任务。 + data: Tool 请求及对话绑定上下文。 + + Returns: + ScheduleRun: 已提交且已绑定当前对话的成功空批次。 + """ + finished_at = datetime.utcnow() + run = ScheduleRun( + run_no=_generate_run_no(), + schedule_task_id=task.id, + trigger_type="dify_chatflow", + started_at=finished_at, + finished_at=finished_at, + duration_ms=0, + status="success", + total_count=0, + success_count=0, + failed_count=0, + skipped_count=0, + api_request_json={ + "trigger_source": data.get("trigger_source") or "dify_chatflow", + "conversation_id": data.get("conversation_id"), + }, + response_summary_json={"errors": [], "no_data": True}, + ) + db.session.add(run) + db.session.commit() + _record_run_bind(run, data) + db.session.commit() + return run @file_check_bp.route("/tools/tasks/run", methods=["POST"]) @@ -445,16 +504,18 @@ def run_file_check(): no_data_response = _precheck_no_data(task) if no_data_response: + run = _create_no_data_run(task, data) lock_ms = int((time.perf_counter() - lock_started) * 1000) total_ms = int((time.perf_counter() - perf_started) * 1000) current_app.logger.info( - "[file-check-tool-perf] run_file_check outcome=no_data resolve_ms=%s lock_ms=%s total_ms=%s task_id=%s", + "[file-check-tool-perf] run_file_check outcome=no_data resolve_ms=%s lock_ms=%s total_ms=%s task_id=%s run_id=%s", resolve_ms, lock_ms, total_ms, task.id, + run.id, ) - return jsonify(no_data_response) + return jsonify(_build_no_data_response(task, run)) run = ScheduleRun( run_no=_generate_run_no(), diff --git a/backend/app/services/file_check_tools/openapi_base.json b/backend/app/services/file_check_tools/openapi_base.json index 4427f18d..e82e71dc 100644 --- a/backend/app/services/file_check_tools/openapi_base.json +++ b/backend/app/services/file_check_tools/openapi_base.json @@ -84,7 +84,7 @@ }, "responses": { "200": { - "description": "启动成功;或已有 文件检核进行中,返回 ok=false/error_code=check_running", + "description": "启动成功;已有检核进行中或预检无资料时返回 ok=false。预检无资料仍返回已完成的 0 笔 run,供前端展示 100% 进度。", "content": { "application/json": { "schema": { @@ -94,6 +94,9 @@ }, { "$ref": "#/components/schemas/CheckRunningResponse" + }, + { + "$ref": "#/components/schemas/NoDataResponse" } ] } @@ -641,6 +644,36 @@ } } }, + "NoDataResponse": { + "type": "object", + "required": [ + "ok", + "error_code", + "message", + "run", + "task" + ], + "properties": { + "ok": { + "type": "boolean", + "example": false + }, + "error_code": { + "type": "string", + "example": "no_data_to_check" + }, + "message": { + "type": "string", + "example": "目前没有资料需要检核" + }, + "run": { + "$ref": "#/components/schemas/RunDetail" + }, + "task": { + "$ref": "#/components/schemas/TaskSummary" + } + } + }, "GetRunResponse": { "type": "object", "required": [ diff --git a/backend/tests/test_file_check_tools.py b/backend/tests/test_file_check_tools.py index 6a24c368..a590f361 100644 --- a/backend/tests/test_file_check_tools.py +++ b/backend/tests/test_file_check_tools.py @@ -416,7 +416,8 @@ def test_file_check_tool_start_records_chat_log_bind_context(app, client, monkey assert resolve_run_id_for_chat_log(session_id='bind-on-start', query='执行 RoHS 检核') == run_id -def test_file_check_tool_no_data_does_not_create_run_or_bind_context(app, client, monkeypatch): +def test_file_check_tool_no_data_creates_bound_completed_run_with_full_progress(app, client, monkeypatch): + """无待检资料也要给本轮对话一条可展示 100% 进度的完成批次。""" with app.app_context(): app.config['INTEGRATION_API_KEY'] = 'secret' admin = _mk_admin() @@ -446,15 +447,29 @@ def test_file_check_tool_no_data_does_not_create_run_or_bind_context(app, client data = res.get_json() assert data['ok'] is False assert data['error_code'] == 'no_data_to_check' - assert 'run' not in data + run_id = data['run']['run_id'] with app.app_context(): after_count = ScheduleRun.query.filter_by(schedule_task_id=task_id).count() - assert after_count == before_count + assert after_count == before_count + 1 + run = db.session.get(ScheduleRun, run_id) + assert run.status == 'success' + assert run.total_count == 0 + assert run.success_count == 0 + assert run.failed_count == 0 + assert run.skipped_count == 0 + assert run.finished_at is not None from app.services.file_check_tools.chat_log_run_binding import resolve_run_id_for_chat_log - assert resolve_run_id_for_chat_log(turn_id='no-data-turn') is None - assert resolve_run_id_for_chat_log(session_id='no-data-session', query='立即执行检核') is None + assert resolve_run_id_for_chat_log(turn_id='no-data-turn') == run_id + assert resolve_run_id_for_chat_log(session_id='no-data-session', query='立即执行检核') == run_id + + progress_res = client.get( + f'/api/workspace/file-check/runs/{run_id}', + headers={'X-Username': 'admin'}, + ) + assert progress_res.status_code == 200 + assert progress_res.get_json()['run']['progress_percent'] == 100 def test_file_check_tool_no_data_keeps_existing_running_conflict_priority(app, client, monkeypatch): diff --git a/docs/demand.md b/docs/demand.md index f7cd38d1..fe4ae11c 100644 --- a/docs/demand.md +++ b/docs/demand.md @@ -36,6 +36,7 @@ - **核心目标**:提供通用文件检核能力(发起检核、查询实时结果、获取报告、可选同步知识库、基于知识库查询历史报告)。对用户统一称为「检核」,不区分排程触发与对话触发等实现方式。 - **通用域**:`FileCheckService` + `/api/file-check` Tool API + 单套 Dify Chatflow「文件检核助手」;RoHS、MSDS 等差异仅在排程 SQL、`task_id` 与判定 Workflow;历史检索走 `search_file_check_history` Tool,按运行时 `task_id` 解析 Dataset。 - **多 Agent**:RoHS / MSDS 等工作台 Agent 共用同一 `dify_app_id`,HXOPS 按 `agents.bound_schedule_task_id` 注入 `task_id`(`agent_code=file_check` 或 `capabilities.task_progress`)。 +- **零笔可视化**:用户从对话立即执行检核时,即使预检无待检资料,也须落一条已完成的 0 笔执行记录,并在该轮对话显示 100% 进度卡。 - 检核成功或部分成功并生成 Excel 报告后,可按配置自动同步到指定知识库分组;Chatflow 历史分支按 `task_id` 选择知识库 Dataset。 - Dify Chatflow 编排与 OpenAPI 导入见 [docs/features/file-check/technical.md](features/file-check/technical.md)(第二部分)。 diff --git a/docs/features/file-check/technical.md b/docs/features/file-check/technical.md index a8096984..e90f6faf 100644 --- a/docs/features/file-check/technical.md +++ b/docs/features/file-check/technical.md @@ -513,7 +513,7 @@ sequenceDiagram | 触发 | 用户表达立即检核意图,如「执行 RoHS 检核」「开始文件检核」 | | 配置 | HXOPS 注入 `agent_runtime_context.refs.schedule_task_id`;与 Agent `bound_schedule_task_id` / `extra_config.tool_run_progress.schedule_task_id` 一致 | | 处理 | Tool API 异步启动 `run_task_now`;Body 必传 `hxops_turn_id`(来自 `refs.hxops_turn_id`) | -| 输出 | Answer 确认启动;前端进度卡片挂于该轮助手消息下 | +| 输出 | Answer 确认启动;前端进度卡片挂于该轮助手消息下。预检无待检资料时仍写入成功的 0 笔 Run,并显示 100% 完成进度卡 | | 异常 | 任务不存在、鉴权失败 → 错误兜底 Answer | ##### F2 检核进行中防护 @@ -1185,6 +1185,7 @@ Layer 2 — 成功出口 → Code(仅 HTTP 200 且 JSON 已到) **仍用 Code+IF 的场景(仅 Layer 2)** - `run_file_check` 返回 **HTTP 200** + `ok=false` + `error_code=check_running`(检核进行中,非失败) +- `run_file_check` 返回 **HTTP 200** + `ok=false` + `error_code=no_data_to_check` 时,仍携带一条 `status=success`、`total_count=0` 的 Run;HXOPS 将其绑定到本轮消息并显示 100% 进度卡 - Tool 成功但 JSON 无法解析 → Code 输出 `invalid_response` → §3.24(启动)或提示重试 **历史检索 `ok=true` 且 `count=0`**:检索成功但未命中,走 LLM 说明「未找到记录」,**不是** Tool 失败。 @@ -1846,6 +1847,8 @@ def main(tool_json, tool_text: str = "") -> dict: **引用变量**:`解析启动检核响应.message` · **String** +**进度卡**:无需调整 Dify 节点。Tool 响应仍是 `no_data_to_check`,但同时返回已完成的 0 笔 `run`;HXOPS 在本轮消息下展示总数 0、进度 100% 的完成卡。 + **回复模板** ```text diff --git a/docs/funclist.md b/docs/funclist.md index 72a1f5e6..1dd892ff 100644 --- a/docs/funclist.md +++ b/docs/funclist.md @@ -100,7 +100,7 @@ | 功能项 | 状态 | 说明 | | --- | --- | --- | | FileCheckService 判定入口 | 已实现 | 检核执行链路统一调用 Dify Workflow(RoHS / MSDS 等按排程绑定不同 Workflow) | -| 文件检核 Chatflow Tool API | 已实现 | `/api/file-check/tools/*`:启动检核、查批次、Excel 报告链接 | +| 文件检核 Chatflow Tool API | 已实现 | `/api/file-check/tools/*`:启动检核、查批次、Excel 报告链接;对话立即执行预检为 0 笔时也写入完成 Run 并展示 100% 进度 | | 文件检核完整文档(原型 + Chatflow 编排) | 已文档化 | 见 [docs/features/file-check/technical.md](features/file-check/technical.md) | | 检核报告同步知识库 | 已实现 | 检核 success / partial_failed 且生成 Excel 后同步到所选知识库分组 | | 多 Agent 共用 Chatflow | 已实现 | RoHS / MSDS 等 Agent 共用同一 Dify Chatflow,`task_id` 由 Agent extra_config 注入 | diff --git a/docs/openapi/file_check_tools_openapi.json b/docs/openapi/file_check_tools_openapi.json index 1b9fe533..da7e3256 100644 --- a/docs/openapi/file_check_tools_openapi.json +++ b/docs/openapi/file_check_tools_openapi.json @@ -84,7 +84,7 @@ }, "responses": { "200": { - "description": "启动成功;或已有 文件检核进行中,返回 ok=false/error_code=check_running", + "description": "启动成功;已有检核进行中或预检无资料时返回 ok=false。预检无资料仍返回已完成的 0 笔 run,供前端展示 100% 进度。", "content": { "application/json": { "schema": { @@ -94,6 +94,9 @@ }, { "$ref": "#/components/schemas/CheckRunningResponse" + }, + { + "$ref": "#/components/schemas/NoDataResponse" } ] } @@ -641,6 +644,36 @@ } } }, + "NoDataResponse": { + "type": "object", + "required": [ + "ok", + "error_code", + "message", + "run", + "task" + ], + "properties": { + "ok": { + "type": "boolean", + "example": false + }, + "error_code": { + "type": "string", + "example": "no_data_to_check" + }, + "message": { + "type": "string", + "example": "目前没有资料需要检核" + }, + "run": { + "$ref": "#/components/schemas/RunDetail" + }, + "task": { + "$ref": "#/components/schemas/TaskSummary" + } + } + }, "GetRunResponse": { "type": "object", "required": [ @@ -918,6 +951,21 @@ } } } + }, + "DataSourceUnavailable": { + "description": "检核数据源不可用(ODBC/SQL Server 连接失败或 SRM 环境变量未配置)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + }, + "example": { + "ok": false, + "error_code": "data_source_unavailable", + "message": "检核数据源不可用,请检查排程 SQL 配置、SRM_SQLSERVER_* 环境变量及本机 ODBC 驱动" + } + } + } } } }