from http.server import BaseHTTPRequestHandler, HTTPServer
import json, time
class Handler(BaseHTTPRequestHandler):
 def log_message(self,*args): pass
 def do_GET(self):
  payload={'object':'list','data':[{'id':x,'object':'model','owned_by':'synthetic'} for x in ['proof-a','proof-b']]}
  raw=json.dumps(payload).encode();self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(raw)
 def do_POST(self):
  data=json.loads(self.rfile.read(int(self.headers['Content-Length']))); model=data.get('model','proof-a'); answer='Synthetic reply complete.'
  print(json.dumps({'model':model,'stream':data.get('stream'),'messages':len(data.get('messages',[]))}),flush=True)
  self.send_response(200)
  if data.get('stream'):
   self.send_header('Content-Type','text/event-stream');self.end_headers()
   for delta,finish in [({'role':'assistant','content':answer},None),({},'stop')]:
    chunk={'id':'proof','object':'chat.completion.chunk','created':int(time.time()),'model':model,'choices':[{'index':0,'delta':delta,'finish_reason':finish}]}
    self.wfile.write(('data: '+json.dumps(chunk)+'\n\n').encode());self.wfile.flush()
   self.wfile.write(('data: '+json.dumps({'id':'proof','object':'chat.completion.chunk','created':int(time.time()),'model':model,'choices':[],'usage':{'prompt_tokens':1000,'completion_tokens':5,'total_tokens':1005}})+'\n\n').encode());self.wfile.write(b'data: [DONE]\n\n')
  else:
   self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(json.dumps({'id':'proof','object':'chat.completion','created':int(time.time()),'model':model,'choices':[{'index':0,'message':{'role':'assistant','content':answer},'finish_reason':'stop'}],'usage':{'prompt_tokens':100,'completion_tokens':5,'total_tokens':105}}).encode())
HTTPServer(('127.0.0.1',18871),Handler).serve_forever()
