feat(rohs-check): 新增 RoHS 对话轮次注册与检核批次绑定功能
This commit is contained in:
1 parent
9cba517bff
commit
fa0cdee8df
20 files changed
+1050
-201
No files matched your search
@@ -8,6 +8,15 @@ from app.models import ChatLog, ChatSession, ScheduleRun, ScheduleTask, User
|
||||
from app.services.rohs_check import RoHSCheckService
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clear_rohs_turn_registry():
|
||||
from app.services.rohs_check_tools.chat_turn_registry import clear_registry_for_tests
|
||||
|
||||
clear_registry_for_tests()
|
||||
yield
|
||||
clear_registry_for_tests()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _stub_minio(monkeypatch):
|
||||
minio_module = types.ModuleType('minio')
|
||||
@@ -1157,3 +1166,81 @@ def test_kb_sync_inline_processes_clean_and_sync_jobs(app, monkeypatch, tmp_path
|
||||
assert clean_calls == [clean_job_id]
|
||||
assert sync_calls
|
||||
assert run.kb_sync_status in ('success', 'ready', 'syncing')
|
||||
|
||||
|
||||
def test_rohs_turn_registry_binds_run_without_http_turn_id(app, client, monkeypatch):
|
||||
"""Dify HTTP 仅传 task_id + user_message 时,仍应通过 turn 注册表回写 run_id。"""
|
||||
class _Thread:
|
||||
def __init__(self, *, target, daemon, name):
|
||||
self.target = target
|
||||
self.daemon = daemon
|
||||
self.name = name
|
||||
|
||||
def start(self):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr('app.api.rohs_check_tools.threading.Thread', _Thread)
|
||||
|
||||
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
|
||||
admin_id = admin.id
|
||||
|
||||
from app.services.rohs_check_tools.chat_turn_registry import register_rohs_chat_turn
|
||||
from app.services.rohs_check_tools.chat_log_run_binding import resolve_run_id_for_chat_log
|
||||
|
||||
register_rohs_chat_turn(
|
||||
turn_id='turn-registry-001',
|
||||
session_id='sess-registry-001',
|
||||
user_id=1,
|
||||
agent_id=1,
|
||||
query='立即执行 RoHS 检核',
|
||||
)
|
||||
|
||||
res = client.post(
|
||||
'/api/rohs-check/tools/tasks/run',
|
||||
headers={'X-Integration-Key': 'secret', 'Content-Type': 'application/json'},
|
||||
json={
|
||||
'task_id': task_id,
|
||||
'user_message': '立即执行 RoHS 检核',
|
||||
},
|
||||
)
|
||||
assert res.status_code == 200
|
||||
run_id = res.get_json()['run']['run_id']
|
||||
|
||||
with app.app_context():
|
||||
run = db.session.get(ScheduleRun, run_id)
|
||||
req = run.api_request_json or {}
|
||||
assert req.get('bind_session_id') == 'sess-registry-001'
|
||||
assert req.get('bind_turn_id') == 'turn-registry-001'
|
||||
assert resolve_run_id_for_chat_log(turn_id='turn-registry-001') == run_id
|
||||
assert resolve_run_id_for_chat_log(
|
||||
session_id='sess-registry-001',
|
||||
query='立即执行 RoHS 检核',
|
||||
) == run_id
|
||||
|
||||
chat_log_id = None
|
||||
from app.services.dify import DifyService
|
||||
|
||||
result = DifyService._persist_stream_chat_record(
|
||||
data={'conversation_id': 'sess-registry-001'},
|
||||
event_name='message_end',
|
||||
request_id='req-bind-test',
|
||||
user_id=admin_id,
|
||||
agent=type('AgentProxy', (), {'id': 1, 'name': 'RoHS文件检核助手'})(),
|
||||
session_id='sess-registry-001',
|
||||
hxops_turn_id='turn-registry-001',
|
||||
normalized_conversation_id='sess-registry-001',
|
||||
query='立即执行 RoHS 检核',
|
||||
answer_text='检核已启动',
|
||||
latency=10,
|
||||
retriever_resources=[],
|
||||
attribution_parts=[],
|
||||
)
|
||||
chat_log_id = result.get('chat_log_id')
|
||||
assert chat_log_id
|
||||
log = db.session.get(ChatLog, chat_log_id)
|
||||
assert log.run_id == run_id
|
||||
Reference in new issue
Block a user