import http.server,json,os,time
ROOT=os.path.dirname(__file__)
class Handler(http.server.BaseHTTPRequestHandler):
 def log_message(self,*args): pass
 def do_GET(self):
  self.send_response(200);self.end_headers();self.wfile.write(b'{}')
 def do_POST(self):
  body=json.loads(self.rfile.read(int(self.headers.get('content-length',0))) or '{}')
  model=body.get('model','unknown')
  with open(ROOT+'/requests.jsonl','a') as f:f.write(json.dumps({'path':self.path,'model':model,'time':time.time()})+'\n')
  text='Endpoint received the exact custom route: opus.' if model=='opus' else 'ALIAS COLLISION: endpoint received a built-in model instead of the custom route opus.'
  response={'id':'msg_proof','type':'message','role':'assistant','model':model,'content':[{'type':'text','text':text}],'stop_reason':'end_turn','stop_sequence':None,'usage':{'input_tokens':10,'output_tokens':20}}
  self.send_response(200)
  if body.get('stream'):
   self.send_header('Content-Type','text/event-stream');self.end_headers()
   events=[('message_start',{'type':'message_start','message':{**response,'content':[],'stop_reason':None}}),('content_block_start',{'type':'content_block_start','index':0,'content_block':{'type':'text','text':''}}),('content_block_delta',{'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':text}}),('content_block_stop',{'type':'content_block_stop','index':0}),('message_delta',{'type':'message_delta','delta':{'stop_reason':'end_turn','stop_sequence':None},'usage':{'output_tokens':20}}),('message_stop',{'type':'message_stop'})]
   for event,data in events:self.wfile.write(('event: '+event+'\ndata: '+json.dumps(data)+'\n\n').encode());self.wfile.flush()
  else:
   self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(json.dumps(response).encode())
http.server.ThreadingHTTPServer(('127.0.0.1',18826),Handler).serve_forever()
