feat(file-check): 新增历史检核报告检索功能及相关 API
This commit is contained in:
1 parent
29a7704d46
commit
f6fe56f205
16 files changed
+1060
-163
No files matched your search
@@ -1428,3 +1428,108 @@ def test_file_check_turn_registry_binds_run_without_http_turn_id(app, client, mo
|
||||
assert chat_log_id
|
||||
log = db.session.get(ChatLog, chat_log_id)
|
||||
assert log.run_id == run_id
|
||||
|
||||
|
||||
def test_history_search_normalizes_dify_records():
|
||||
from app.services.file_check_tools.history_search_service import normalize_retrieve_records
|
||||
|
||||
payload = normalize_retrieve_records({
|
||||
'records': [
|
||||
{
|
||||
'score': 0.91,
|
||||
'segment': {
|
||||
'content': 'Pb 超标',
|
||||
'document_name': 'RoHS检核_RUN202606090001',
|
||||
'metadata': {'run_no': 'RUN202606090001'},
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
assert len(payload) == 1
|
||||
assert payload[0]['content'] == 'Pb 超标'
|
||||
assert payload[0]['run_no'] == 'RUN202606090001'
|
||||
assert payload[0]['document_title'] == 'RoHS检核_RUN202606090001'
|
||||
|
||||
|
||||
def test_history_search_tool_requires_query(app, client):
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
task_id = task.id
|
||||
|
||||
res = client.post(
|
||||
'/api/file-check/tools/history/search',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={'task_id': task_id, 'query': ' '},
|
||||
)
|
||||
assert res.status_code == 400
|
||||
assert res.get_json()['error_code'] == 'query_required'
|
||||
|
||||
|
||||
def test_history_search_tool_requires_kb_config(app, client):
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
task_id = task.id
|
||||
|
||||
res = client.post(
|
||||
'/api/file-check/tools/history/search',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={'task_id': task_id, 'query': '上周有哪些不合格'},
|
||||
)
|
||||
assert res.status_code == 400
|
||||
assert res.get_json()['error_code'] == 'kb_not_configured'
|
||||
|
||||
|
||||
def test_history_search_tool_success(app, client, monkeypatch):
|
||||
from app.models import KbGroup
|
||||
|
||||
with app.app_context():
|
||||
app.config['INTEGRATION_API_KEY'] = 'secret'
|
||||
admin = _mk_admin()
|
||||
db.session.commit()
|
||||
task = _mk_check_task(admin.id)
|
||||
group = KbGroup(name='RoHS KB Search', is_active=True, dify_dataset_id='ds-search-001')
|
||||
db.session.add(group)
|
||||
db.session.flush()
|
||||
task.kb_sync_enabled = True
|
||||
task.kb_group_id = group.id
|
||||
db.session.commit()
|
||||
task_id = task.id
|
||||
|
||||
def _fake_retrieve(self, **kwargs):
|
||||
assert kwargs['dataset_id'] == 'ds-search-001'
|
||||
assert kwargs['query'] == '上周有哪些不合格'
|
||||
return {
|
||||
'records': [
|
||||
{
|
||||
'score': 0.88,
|
||||
'segment': {
|
||||
'content': '供应商 A 铅含量不合格',
|
||||
'document_name': f'RoHS 自动检核_RUN-TEST-001',
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.knowledge_cleaning.dify_sync_service.KbDifySyncService.retrieve_dataset',
|
||||
_fake_retrieve,
|
||||
)
|
||||
|
||||
res = client.post(
|
||||
'/api/file-check/tools/history/search',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={'task_id': task_id, 'query': '上周有哪些不合格', 'top_k': 3},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
assert data['ok'] is True
|
||||
assert data['task_id'] == task_id
|
||||
assert data['count'] == 1
|
||||
assert data['segments'][0]['content'] == '供应商 A 铅含量不合格'
|
||||
assert data['dify_dataset_id'] == 'ds-search-001'
|
||||
Reference in new issue
Block a user