import json, sqlite3, sys
from pathlib import Path
home, destination = sys.argv[1:]
db=sqlite3.connect(Path(home,'userdata','statev2.sqlite').resolve().as_uri()+'?mode=ro',uri=True)
messages=[json.loads(row[0]) for row in db.execute('select payload_json from orchestration_v2_projection_messages order by created_at')]
threads=[json.loads(row[0]) for row in db.execute('select payload_json from orchestration_v2_projection_threads')]
result={'notifications':[{'summary':m['notification']['summary'],'outcome':m['notification']['outcome'],'text':m['text']} for m in messages if 'notification' in m], 'agentResponses':[m['text'] for m in messages if m['role']=='assistant'], 'pullRequests':[{'number':p['number'],'state':p['snapshot']['state'],'checksState':p['snapshot']['checksState'],'watch':p.get('watch')} for t in threads for p in t.get('pullRequests',[])]}
Path(destination).write_text(json.dumps(result,indent=2)+'\n')
