diff --git a/backend/Dockerfile b/backend/Dockerfile index c2ef287a..002544f7 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -5,6 +5,8 @@ FROM ${PYTHON_BASE_IMAGE} ARG APT_MIRROR=https://mirrors.aliyun.com ARG PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple ARG PIP_EXTRA_INDEX_URL +ARG PIP_TIMEOUT=300 +ARG PIP_RETRIES=15 WORKDIR /app diff --git a/backend/app/api/file_check_tools.py b/backend/app/api/file_check_tools.py index c90a8387..115d58c2 100644 --- a/backend/app/api/file_check_tools.py +++ b/backend/app/api/file_check_tools.py @@ -301,6 +301,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 { @@ -534,10 +562,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 8e2c9427..80aafbcb 100644 --- a/backend/app/api/workspace/file_check.py +++ b/backend/app/api/workspace/file_check.py @@ -89,7 +89,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'], @@ -180,4 +185,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 5b9471e5..e2f6d0c8 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 d8e3efbd..d94ed1d8 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 60e30e8f..0528a721 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 9a03b288..7ba2625b 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": { @@ -945,4 +972,4 @@ } } } -} \ No newline at end of file +}