70 lines
3.2 KiB
Python
70 lines
3.2 KiB
Python
"""Isolated browser acceptance fixture. No production DB or external model calls."""
|
|
from pathlib import Path
|
|
import sys
|
|
import threading
|
|
import time
|
|
import json
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / 'wechat_rpa'))
|
|
import admin_backend
|
|
import admin_api
|
|
from archive_store import ArchiveStore
|
|
from knowledge_store import KnowledgeStore
|
|
from knowledge_worker import KnowledgeWorker
|
|
from test_knowledge import KnowledgeTest
|
|
|
|
folder=Path(__file__).resolve().parent
|
|
db=admin_backend.Database(folder/'backend.db')
|
|
if not db.path.exists():
|
|
db.initialize('InitialAdmin123')
|
|
actor=db.authenticate('admin','InitialAdmin123')['id']
|
|
db.change_password(actor,'InitialAdmin123','KnowledgeQA@2026','')
|
|
_, account=db.create_desktop_session({'user_id':'knowledge-qa','sn':'QA001','nickname':'流程验收客服'},
|
|
'qa-device','QA','1.0','127.0.0.1')
|
|
store=KnowledgeStore(db)
|
|
store.initialize()
|
|
archive=ArchiveStore(db)
|
|
for index, (question,answer) in enumerate([
|
|
('预约门店服务需要提前提供哪些信息?','请先提供希望到店的日期与服务项目,客服核实可预约时段后再确认。'),
|
|
('修改预约时间应该如何办理?','请提供原预约日期和希望调整的时间,客服确认新时段可用后再修改预约。'),
|
|
('到店之前是否需要再次确认预约?','建议到店前向客服确认预约日期、门店地址和接待时间,以当天确认结果为准。'),
|
|
]):
|
|
payload=KnowledgeTest.payload(None,account['tenant_id'],[('inbound',question),('outbound',answer)],f'S:qa-{index}')
|
|
for row in payload['messages']:
|
|
row['source_message_id']=f'qa-{index}-'+row['source_message_id']
|
|
archive.import_messages(payload,actor,'')
|
|
(folder/'fixture.json').write_text(json.dumps({'account_id':account['id'],'tenant_id':account['tenant_id']}),encoding='utf-8')
|
|
|
|
# In-memory synthetic model providers; no network requests or production secrets.
|
|
import knowledge_models
|
|
import knowledge_worker
|
|
from test_knowledge_tasks import fake_catalog
|
|
knowledge_models.catalog_for = lambda database: fake_catalog()
|
|
calls = 0
|
|
def fake_enhance(candidate, database, options):
|
|
global calls
|
|
calls += 1
|
|
if calls == 2 and options['model_provider_id'] == 'alpha':
|
|
raise ValueError('模型请求超时,请增加单次等待时间或更换模型后继续')
|
|
candidate['conditions'] = '仅供虚构门店流程验收'
|
|
return candidate, 200, 150
|
|
knowledge_worker.enhance = fake_enhance
|
|
app=admin_api.create_app(db.path)
|
|
@app.middleware('http')
|
|
async def api_prefix(request, call_next):
|
|
if request.scope['path'].startswith('/api/') and not request.scope['path'].startswith('/api/v2/'):
|
|
request.scope['path']=request.scope['path'].replace('/api/','/api/v2/',1)
|
|
request.scope['raw_path']=request.scope['path'].encode()
|
|
return await call_next(request)
|
|
|
|
def worker_loop():
|
|
worker=KnowledgeWorker(app.state.knowledge_store)
|
|
while True:
|
|
worker.step()
|
|
time.sleep(0.3)
|
|
|
|
if __name__=='__main__':
|
|
import uvicorn
|
|
threading.Thread(target=worker_loop,daemon=True).start()
|
|
uvicorn.run(app,host='127.0.0.1',port=18867,log_level='warning')
|