feat(file-check): 添加文件检核查询结果绑定对话轮次功能,修复空成功批次进度显示问题
This commit is contained in:
1 parent
88fcf3c70b
commit
2878a6998e
6 files changed
+190
-3
No files matched your search
@@ -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/<int:task_id>/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)})
|
||||
|
||||
|
||||
|
||||
@@ -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')
|
||||
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')
|
||||
@@ -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": {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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解析检核查询响应两个节点)
|
||||
|
||||
|
||||
@@ -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 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user