import { createServer } from "node:http"; // Simulador local: recibe JSON, no llama a un modelo. const server = createServer(async (request, response) => { let body = ""; for await (const chunk of request) body += chunk; const { input } = JSON.parse(body); response.writeHead(200, { "Content-Type": "application/json" }); response.end(JSON.stringify({ text: "Solicitud recibida: " + input })); }); await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); const { port } = server.address(); try { const response = await fetch("http://127.0.0.1:" + port, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ input: "Hola, IA" }), signal: AbortSignal.timeout(3000) }); if (!response.ok) throw new Error("HTTP " + response.status); const data = await response.json(); if (typeof data.text !== "string") throw new Error("Respuesta inválida"); console.log(data.text); } finally { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); }