import http.server,json,time,uuid
from pathlib import Path
root=Path(__file__).parent
class Handler(http.server.BaseHTTPRequestHandler):
 def log_message(self,*args): pass
 def do_POST(self):
  data=json.loads(self.rfile.read(int(self.headers.get('content-length',0))))
  reasoning=data.get('reasoning',{})
  with (root/'requests.jsonl').open('a') as f: f.write(json.dumps({'reasoning':reasoning,'rejected':reasoning.get('summary') in ['detailed','concise']})+'\n')
  if reasoning.get('summary') in ['detailed','concise']:
   body=json.dumps({'error':{'code':'unsupported_parameter','type':'invalid_request_error','param':'reasoning.summary','message':"Unsupported parameter: 'reasoning.summary' is not supported with the 'bedrock-test' model."}}).encode()
   self.send_response(400); self.send_header('Content-Type','application/json'); self.send_header('Content-Length',str(len(body))); self.end_headers(); self.wfile.write(body); return
  self.send_response(200); self.send_header('Content-Type','text/event-stream'); self.end_headers()
  msg_id='msg_'+uuid.uuid4().hex
  resp_id='resp_'+uuid.uuid4().hex
  message={'id':msg_id,'type':'message','role':'assistant','status':'completed','content':[{'type':'output_text','text':'ok','annotations':[]}]}
  response={'id':resp_id,'object':'response','created_at':int(time.time()),'status':'completed','model':'bedrock-test','output':[message],'usage':{'input_tokens':1,'output_tokens':1,'total_tokens':2}}
  events=[{'type':'response.created','response':{**response,'status':'in_progress','output':[]}}, {'type':'response.output_item.added','output_index':0,'item':{**message,'status':'in_progress','content':[]}}, {'type':'response.content_part.added','item_id':msg_id,'output_index':0,'content_index':0,'part':{'type':'output_text','text':'','annotations':[]}}, {'type':'response.output_text.delta','item_id':msg_id,'output_index':0,'content_index':0,'delta':'ok'}, {'type':'response.output_text.done','item_id':msg_id,'output_index':0,'content_index':0,'text':'ok'}, {'type':'response.output_item.done','output_index':0,'item':message}, {'type':'response.completed','response':response}]
  for i,e in enumerate(events):
   e['sequence_number']=i; self.wfile.write(('event: '+e['type']+'\ndata: '+json.dumps(e)+'\n\n').encode()); self.wfile.flush()
http.server.ThreadingHTTPServer(('127.0.0.1',18952),Handler).serve_forever()
