54 lines
3.1 KiB
Python
54 lines
3.1 KiB
Python
"""Exercise the actual upstream nao HTTP agent and preserve its SSE evidence."""
|
|
import argparse,http.cookiejar,json,time,urllib.request,urllib.error
|
|
from pathlib import Path
|
|
from nao_trial import TRIAL,ROOT,model_settings
|
|
|
|
BASE='http://127.0.0.1:5674'
|
|
OUT=ROOT/'output/nao-trial'
|
|
STATE=TRIAL/'chat-state.json'
|
|
|
|
def opener():
|
|
jar=http.cookiejar.LWPCookieJar(str(TRIAL/'cookies.txt'))
|
|
if Path(jar.filename).exists():jar.load(ignore_discard=True)
|
|
client=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
|
|
p=json.loads((TRIAL/'private.json').read_text())
|
|
for endpoint in ('sign-in/email','sign-up/email'):
|
|
body={k:p[k] for k in ('email','password')};body['name']='Quality Trial'
|
|
req=urllib.request.Request(BASE+'/api/auth/'+endpoint,data=json.dumps(body).encode(),headers={'Content-Type':'application/json','Origin':BASE})
|
|
try:
|
|
with client.open(req,timeout=20) as r: result=json.load(r)
|
|
jar.save(ignore_discard=True);Path(jar.filename).chmod(0o600)
|
|
return client
|
|
except urllib.error.HTTPError as e:
|
|
if e.code not in (400,401,403,404,422):raise
|
|
raise RuntimeError('Local nao authentication failed')
|
|
|
|
def run(label,question,new=False,chat_id=None):
|
|
client=opener();state={'chatId':chat_id} if chat_id else json.loads(STATE.read_text()) if STATE.exists() and not new else {}
|
|
model=model_settings()[2]
|
|
body={'message':{'text':question},'model':{'provider':'openaiCompatible','modelId':model},'timezone':'Asia/Shanghai'}
|
|
if state.get('chatId'):body['chatId']=state['chatId']
|
|
(OUT/(label+'-request.json')).write_text(json.dumps(body,ensure_ascii=False,indent=2))
|
|
req=urllib.request.Request(BASE+'/api/agent/',data=json.dumps(body,ensure_ascii=False).encode(),headers={'Content-Type':'application/json','Origin':BASE,'Accept':'text/event-stream'})
|
|
begin=time.monotonic();events=[];seen=set();status='done'
|
|
with client.open(req,timeout=120) as response,(OUT/(label+'.sse')).open('w') as stream:
|
|
for raw in response:
|
|
line=raw.decode('utf-8',errors='replace');stream.write(line);stream.flush()
|
|
if not line.startswith('data:'):continue
|
|
data=line[5:].strip()
|
|
if data=='[DONE]':break
|
|
try:e=json.loads(data)
|
|
except json.JSONDecodeError:continue
|
|
events.append(e);kind=e.get('type','')
|
|
if kind=='data-newChat' and e.get('data',{}).get('id'):
|
|
state['chatId']=e['data']['id'];STATE.write_text(json.dumps(state));STATE.chmod(0o600)
|
|
if kind not in seen:print('event',kind,flush=True);seen.add(kind)
|
|
if kind=='tool-input-available':print('tool',e.get('toolName'),flush=True)
|
|
if time.monotonic()-begin>600:status='stopped_at_ten_minute_budget';break
|
|
(OUT/(label+'-events.json')).write_text(json.dumps(events,ensure_ascii=False,indent=2))
|
|
(OUT/(label+'-run.json')).write_text(json.dumps({'status':status,'elapsed_seconds':round(time.monotonic()-begin,1),'model':model,**state},indent=2))
|
|
print('saved',len(events),'events;',state,'elapsed',round(time.monotonic()-begin,1),flush=True)
|
|
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('label');p.add_argument('question');p.add_argument('--new',action='store_true');p.add_argument('--chat-id');a=p.parse_args();run(a.label,a.question,a.new,a.chat_id)
|