import http.server,json,time,threading
class Handler(http.server.BaseHTTPRequestHandler):
 def log_message(self,*args): pass
 def do_GET(self):
  self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(b'{"data":[{"id":"gpt-5.4","object":"model"}]}')
 def do_POST(self):
  body=json.loads(self.rfile.read(int(self.headers['Content-Length'])))
  first=not any(x.get('type')=='function_call_output' for x in body.get('input',[]) if isinstance(x,dict))
  print('request', 'alert' if first else 'held-final',flush=True)
  if not first: time.sleep(30)
  item=({'id':'fc_alert','type':'function_call','call_id':'call_alert_1','name':'send_message_to_user_async','arguments':'{"message":"I found an important detail."}','status':'completed'} if first else {'id':'msg_final','type':'message','role':'assistant','phase':'final_answer','status':'completed','content':[{'type':'output_text','text':'The work is complete.','annotations':[]}]})
  self.send_response(200);self.send_header('Content-Type','text/event-stream');self.end_headers()
  def event(type,**data):
   self.wfile.write(('event: '+type+'\ndata: '+json.dumps({'type':type,**data})+'\n\n').encode());self.wfile.flush()
  event('response.created',response={'id':'resp_1','object':'response','status':'in_progress','output':[]})
  event('response.output_item.added',output_index=0,item=item)
  event('response.output_item.done',output_index=0,item=item)
  event('response.completed',response={'id':'resp_1','object':'response','status':'completed','output':[item],'usage':{'input_tokens':10,'output_tokens':10,'total_tokens':20}})
http.server.ThreadingHTTPServer(('127.0.0.1',18765),Handler).serve_forever()
