From 2878a6998eefcc09e4d551478b14b3dc731e0d91 Mon Sep 17 00:00:00 2001 From: caohuipeng Date: Mon, 17 Aug 2026 20:16:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(file-check):=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=A3=80=E6=A0=B8=E6=9F=A5=E8=AF=A2=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=AF=B9=E8=AF=9D=E8=BD=AE=E6=AC=A1=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E6=89=B9=E6=AC=A1=E8=BF=9B=E5=BA=A6=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/file_check_tools.py | 34 +++++++ backend/app/api/workspace/file_check.py | 9 +- .../file_check_tools/openapi_base.json | 27 ++++++ backend/tests/test_file_check_tools.py | 89 +++++++++++++++++++ docs/features/file-check/technical.md | 5 ++ docs/openapi/file_check_tools_openapi.json | 29 +++++- 6 files changed, 190 insertions(+), 3 deletions(-) diff --git a/backend/app/api/file_check_tools.py b/backend/app/api/file_check_tools.py index f14336d1..d72c0e76 100644 --- a/backend/app/api/file_check_tools.py +++ b/backend/app/api/file_check_tools.py @@ -317,6 +317,34 @@ def _record_run_bind(run: ScheduleRun, data: dict) -> None: attach_run_to_turn(turn_id, int(run.id)) +def _bind_queried_run_to_turn(run: ScheduleRun, data: dict) -> None: + """ + 将已有检核批次关联到本轮查询对话,不改写批次原始执行上下文。 + + 查询最近/指定批次时触发;仅更新 chat_tool_runs 与内存 turn registry, + 使 message_end 可以把既有 ScheduleRun.id 写入当前 ChatLog.run_id。 + """ + from app.services.file_check_tools.chat_tool_run_binding import bind_schedule_run + from app.services.file_check_tools.chat_turn_registry import attach_run_to_turn + + turn_id = str(data.get("hxops_turn_id") or "").strip() + session_id = str(data.get("conversation_id") or "").strip() or None + user_message = str(data.get("user_message") or "").strip() or None + if not turn_id: + return + + bound = bind_schedule_run( + schedule_run_id=int(run.id), + turn_id=turn_id, + session_id=session_id, + user_message=user_message, + schedule_task_id=int(run.schedule_task_id), + bind_source="tool_query_latest", + ) + if bound: + attach_run_to_turn(turn_id, int(run.id)) + + def _build_no_data_response(task: ScheduleTask) -> dict: """无待检资料时直接返回提示,不创建批次也不绑定 run_id。""" return { @@ -523,10 +551,16 @@ def download_file_check_run_excel(run_id: int): @file_check_bp.route("/tools/tasks//latest-run", methods=["GET"]) def get_latest_file_check_run(task_id: int): + """查询任务最近批次;可绑定本轮对话以展示该批次进度。""" task = _resolve_check_task(task_id=task_id) run = ScheduleRun.query.filter_by(schedule_task_id=task.id).order_by(ScheduleRun.id.desc()).first() if not run: raise NotFoundError("run_not_found") + _bind_queried_run_to_turn(run, { + "hxops_turn_id": request.args.get("hxops_turn_id"), + "conversation_id": request.args.get("conversation_id"), + "user_message": request.args.get("user_message"), + }) return jsonify({"ok": True, "run": _serialize_tool_run(run)}) diff --git a/backend/app/api/workspace/file_check.py b/backend/app/api/workspace/file_check.py index 2a6e47e1..7a7bf85e 100644 --- a/backend/app/api/workspace/file_check.py +++ b/backend/app/api/workspace/file_check.py @@ -64,7 +64,12 @@ def _serialize_progress_run(run: ScheduleRun) -> dict: failed = int(payload.get('failed_count') or 0) skipped = int(payload.get('skipped_count') or 0) processed = success + failed + skipped - percent = int(round(processed / total * 100)) if total > 0 else 0 + if total > 0: + percent = int(round(processed / total * 100)) + elif payload.get('status') == 'success': + percent = 100 + else: + percent = 0 percent = max(0, min(percent, 100)) return {'run_id': payload['id'], 'run_no': payload['run_no'], 'task_id': payload['schedule_task_id'], 'title': payload.get('schedule_name') or '', 'status': payload['status'], 'total_count': total, 'processed_count': processed, 'success_count': success, 'failed_count': failed, 'skipped_count': skipped, 'judgement': serialize_file_judgement(payload), 'current_step': serialize_current_step(payload), 'progress_percent': percent, 'started_at': payload.get('started_at'), 'finished_at': payload.get('finished_at'), 'duration_ms': payload.get('duration_ms'), 'error_message': payload.get('error_message'), 'excel': {'file_name': payload.get('excel_file_name'), 'file_size': payload.get('excel_file_size'), 'download_url': f'/api/workspace/file-check/runs/{run.id}/excel' if payload.get('excel_file_name') else None}, 'knowledge_base': {'sync_status': payload.get('kb_sync_status'), 'group_id': payload.get('kb_group_id'), 'document_id': payload.get('kb_document_id'), 'document_version_id': payload.get('kb_document_version_id'), 'sync_job_id': payload.get('kb_sync_job_id'), 'error_message': payload.get('kb_sync_error')}} @@ -123,4 +128,4 @@ def workspace_file_check_run_excel(run_id: int): raise NotFoundError('run_not_found') if not run.excel_file_data: raise NotFoundError('excel_not_found') - return send_file(BytesIO(run.excel_file_data), as_attachment=True, download_name=run.excel_file_name or f'{run.run_no}.xlsx', mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') \ No newline at end of file + return send_file(BytesIO(run.excel_file_data), as_attachment=True, download_name=run.excel_file_name or f'{run.run_no}.xlsx', mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') diff --git a/backend/app/services/file_check_tools/openapi_base.json b/backend/app/services/file_check_tools/openapi_base.json index 4222cc60..4427f18d 100644 --- a/backend/app/services/file_check_tools/openapi_base.json +++ b/backend/app/services/file_check_tools/openapi_base.json @@ -220,6 +220,33 @@ "type": "integer", "example": 12 } + }, + { + "name": "hxops_turn_id", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前 HXOPS 对话轮次 ID;传入后将最近批次绑定到该轮进度卡" + }, + { + "name": "conversation_id", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前 Dify 会话 ID,仅用于关联当前查询轮次" + }, + { + "name": "user_message", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前用户问题,仅用于关联当前查询轮次" } ], "responses": { diff --git a/backend/tests/test_file_check_tools.py b/backend/tests/test_file_check_tools.py index 7c0bb94e..6a24c368 100644 --- a/backend/tests/test_file_check_tools.py +++ b/backend/tests/test_file_check_tools.py @@ -661,6 +661,66 @@ def test_file_check_tool_lists_runs_without_task_filter(app, client): assert {item['run_no'] for item in data['runs']} == {'RUN-ROHS-001', 'RUN-ROHS-002', 'RUN-OTHER-001'} +def test_latest_file_check_run_binds_existing_run_to_query_turn(app, client): + """查询最近批次只绑定当前对话,不得改写定时任务的原始执行上下文。""" + with app.app_context(): + app.config['INTEGRATION_API_KEY'] = 'secret' + admin = _mk_admin() + db.session.commit() + task = _mk_check_task(admin.id) + run = ScheduleRun( + run_no='RUN-ROHS-CRON-EMPTY', + schedule_task_id=task.id, + status='success', + trigger_type='cron', + total_count=0, + api_request_json={'trigger_source': 'cron'}, + ) + db.session.add(run) + db.session.commit() + task_id = task.id + run_id = run.id + + from app.models import ChatToolRun + from app.services.file_check_tools.chat_tool_run_binding import register_pending_turn + from app.services.file_check_tools.chat_turn_registry import register_file_check_chat_turn + + register_file_check_chat_turn( + turn_id='turn-query-latest', + session_id='session-query-latest', + user_id=admin.id, + agent_id=1, + query='查看最近执行记录', + ) + register_pending_turn( + turn_id='turn-query-latest', + session_id='session-query-latest', + user_id=admin.id, + agent_id=1, + user_message='查看最近执行记录', + schedule_task_id=task_id, + ) + + res = client.get( + f'/api/file-check/tools/tasks/{task_id}/latest-run', + query_string={ + 'hxops_turn_id': 'turn-query-latest', + 'conversation_id': 'session-query-latest', + 'user_message': '查看最近执行记录', + }, + headers={'X-Integration-Key': 'secret'}, + ) + + assert res.status_code == 200 + assert res.get_json()['run']['run_id'] == run_id + + with app.app_context(): + binding = ChatToolRun.query.filter_by(turn_id='turn-query-latest').first() + rebound_run = db.session.get(ScheduleRun, run_id) + assert binding.schedule_run_id == run_id + assert rebound_run.api_request_json == {'trigger_source': 'cron'} + + def test_workspace_file_check_active_run_returns_progress(app, client): with app.app_context(): admin = _mk_admin() @@ -702,6 +762,35 @@ def test_workspace_file_check_active_run_returns_progress(app, client): assert data['run']['current_step']['row_id'] == 'ROW-16' +def test_workspace_file_check_successful_empty_run_reports_complete_progress(app, client): + """已完成的空批次应展示 100% 进度,避免被误解为尚未执行。""" + with app.app_context(): + admin = _mk_admin() + db.session.commit() + task = _mk_check_task(admin.id) + run = ScheduleRun( + run_no='RUN-ROHS-EMPTY', + schedule_task_id=task.id, + status='success', + trigger_type='cron', + total_count=0, + success_count=0, + failed_count=0, + skipped_count=0, + ) + db.session.add(run) + db.session.commit() + run_id = run.id + + res = client.get( + f'/api/workspace/file-check/runs/{run_id}', + headers={'X-Username': 'admin'}, + ) + + assert res.status_code == 200 + assert res.get_json()['run']['progress_percent'] == 100 + + def test_workspace_file_check_active_run_filters_by_task_id(app, client): with app.app_context(): admin = _mk_admin() diff --git a/docs/features/file-check/technical.md b/docs/features/file-check/technical.md index b052fe95..a8096984 100644 --- a/docs/features/file-check/technical.md +++ b/docs/features/file-check/technical.md @@ -1984,6 +1984,11 @@ def main(user_query, intent_run_id, intent_task_id, task_id) -> dict: | 参数 | 类型 | 绑定 | | --- | --- | --- | | `task_id` | Number | `提取查询参数.task_id_int` | +| `hxops_turn_id` | String | `解析运行态上下文.hxops_turn_id` | +| `conversation_id` | String | `sys.conversation_id` | +| `user_message` | String | `用户输入.query` | + +> 查询接口会把返回的既有 `ScheduleRun` 关联到当前对话轮次,以便在该条回答下显示进度卡;不会改写定时任务或人工执行批次的原始触发上下文。若该批次 `status=success` 且 `total_count=0`,进度卡显示 **100%**,表示本次执行完成且未发现待检资料。 **下游**:→ [§3.18.2 Code:解析最近检核查询响应](#318-code解析检核查询响应两个节点) diff --git a/docs/openapi/file_check_tools_openapi.json b/docs/openapi/file_check_tools_openapi.json index a176c6f2..1b9fe533 100644 --- a/docs/openapi/file_check_tools_openapi.json +++ b/docs/openapi/file_check_tools_openapi.json @@ -220,6 +220,33 @@ "type": "integer", "example": 12 } + }, + { + "name": "hxops_turn_id", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前 HXOPS 对话轮次 ID;传入后将最近批次绑定到该轮进度卡" + }, + { + "name": "conversation_id", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前 Dify 会话 ID,仅用于关联当前查询轮次" + }, + { + "name": "user_message", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "当前用户问题,仅用于关联当前查询轮次" } ], "responses": { @@ -894,4 +921,4 @@ } } } -} \ No newline at end of file +}