import http from 'node:http';import {writeFileSync} from 'node:fs';import {randomUUID,createHash} from 'node:crypto'; const receipts=[];const save=()=>writeFileSync('/tmp/codex3475-llm-receipts.json',JSON.stringify(receipts,null,2));save(); const idSeed=()=>randomUUID();const server=http.createServer(async(req,res)=>{try{const chunks=[];for await(const x of req)chunks.push(x);const body=chunks.length?JSON.parse(Buffer.concat(chunks).toString()):{};const url=new URL(req.url,'http://127.0.0.1:39752');if(req.method==='GET'&&url.pathname.endsWith('/models')){res.writeHead(200,{'content-type':'application/json'});return res.end(JSON.stringify({object:'list',data:[{id:'openai/gpt-4o-mini',object:'model',created:0,owned_by:'qa3475-controlled'}]}));}if(req.method!=='POST'||!url.pathname.endsWith('/chat/completions')){res.writeHead(404);return res.end('{}');}const serialized=JSON.stringify(body.messages??[]);const invisible=serialized.includes('QA3475_INVISIBLE');const toolResponse=(body.messages??[]).filter(m=>m.role==='tool');const sawEmpty=toolResponse.some(m=>JSON.stringify(m.content).includes('corpo_vazio'));const tools=(body.tools??[]).map(t=>t.function?.name);const tool=tools.includes('send_message')&&!toolResponse.length;const closing=(body.messages??[]).some(m=>m.role==='user'&&JSON.stringify(m.content).includes('Feche o turno AGORA.'));const text=closing?JSON.stringify({commitments:[],objections:[],next_action:null,rolling_summary:'Prova sintética da guarda de conteúdo.',declaracao:{intencoes:[],promessas:[],nada_a_declarar:true}}):tools.length&&toolResponse.length?'':'Olá! A mensagem de controle foi recebida e registrada.';const message=tool?{role:'assistant',content:null,tool_calls:[{id:'call_'+randomUUID(),type:'function',function:{name:'send_message',arguments:JSON.stringify({body:invisible?'\u200e':text})}}]}:{role:'assistant',content:text};const id='chatcmpl_'+randomUUID();const finish=tool?'tool_calls':'stop';receipts.push({id,path:url.pathname,model:body.model,stream:!!body.stream,tools,invisible,corpo_vazio_observado:sawEmpty,tool_body_codepoints:tool?[...JSON.parse(message.tool_calls[0].function.arguments).body].map(c=>c.codePointAt(0)):null,tool_result_content:toolResponse.map(m=>m.content),tool_schemas:(body.tools??[]).filter(t=>t.function?.name==='send_message').map(t=>t.function.parameters),tool_result_codes:toolResponse.map(m=>JSON.stringify(m.content).includes('corpo_vazio')?'corpo_vazio':'outro'),request_sha256:createHash('sha256').update(serialized).digest('hex'),at:new Date().toISOString()});save();if(invisible&&tools.length&&toolResponse.length&&sawEmpty){res.writeHead(400,{'content-type':'application/json'});return res.end(JSON.stringify({error:{message:'QA3475 guard observed; provider stops this bounded probe',type:'qa_probe_stop',code:'qa_probe_stop'}}));}if(!body.stream){res.writeHead(200,{'content-type':'application/json'});return res.end(JSON.stringify({id,object:'chat.completion',created:Math.floor(Date.now()/1000),model:body.model,choices:[{index:0,message,finish_reason:finish}],usage:{prompt_tokens:100,completion_tokens:15,total_tokens:115}}));}res.writeHead(200,{'content-type':'text/event-stream'});const delta=tool?{role:'assistant',tool_calls:message.tool_calls.map((t,index)=>({...t,index}))}:{role:'assistant',content:text};for(const [d,f]of[[delta,null],[{},finish]])res.write('data: '+JSON.stringify({id,object:'chat.completion.chunk',created:Math.floor(Date.now()/1000),model:body.model,choices:[{index:0,delta:d,finish_reason:f}]})+'\n\n');res.end('data: [DONE]\n\n');}catch{res.writeHead(400,{'content-type':'application/json'});res.end('{"error":"invalid_synthetic_request"}');}});server.listen(39752,'127.0.0.1',()=>process.stdout.write('LLM_RECEIVER_OWN_READY\n'));