feat(file-check-tool): 实现对话触发检核无待检资料时展示100%完成进度卡

更新工具API的OpenAPI定义,新增NoDataResponse结构体;修改预检与响应逻辑,无待检资料时创建已完成的空批次并绑定当前对话;同步更新相关文档并补充测试用例验证流程正确性。
This commit is contained in:
hugo committed 2026-08-17 22:07:07 +08:00
1 parent 2878a6998e
commit e6a44df260
7 files changed
+177 -16

No files matched your search

+68 -7
View File
@@ -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(),
@@ -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": [
+20 -5
View File
@@ -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):
+1
View File
@@ -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)(第二部分)。
+4 -1
View File
@@ -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
+1 -1
View File
@@ -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 注入 |
+49 -1
View File
@@ -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 驱动"
}
}
}
}
}
}