import { createServer, IncomingMessage, ServerResponse } from "node:http";
import { OpenAIService } from "./openai-service";
const openAIService = new OpenAIService();
const log = (msg: string) => {
const time = new Date().toISOString().split('T')[1].split('.')[0];
console.log(`[${time}] ${msg}`);
};
const HTML_FRONTEND = `
AI Research Playground
🔬 Research API
This Space hosts a distributed language model inference engine for academic research and integration testing.
System Online
Authorized access only via encrypted endpoints.
`;
const API_KEY = process.env.API_KEY || "MySecretKey_12345";
const PORT = Number(process.env.PORT) || 7860;
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
try {
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
const method = req.method;
// CORS Headers
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
// Helper to send JSON response
const sendJSON = (statusCode: number, data: any) => {
res.writeHead(statusCode, { "Content-Type": "application/json", ...corsHeaders });
res.end(JSON.stringify(data));
};
// Helper to send HTML
const sendHTML = (html: string) => {
res.writeHead(200, { "Content-Type": "text/html", ...corsHeaders });
res.end(html);
};
// Masking Frontend
if (url.pathname === "/" && method === "GET") {
return sendHTML(HTML_FRONTEND);
}
// Handle Preflight
if (method === "OPTIONS") {
res.writeHead(204, corsHeaders);
res.end();
return;
}
// Auth Check
const authHeader = req.headers["authorization"];
if (url.pathname.startsWith("/v1/") && authHeader !== `Bearer ${API_KEY}`) {
return sendJSON(401, { error: "Unauthorized" });
}
// Health Check
if (url.pathname === "/health") {
return sendJSON(200, { status: "online", model: "distributed-v1" });
}
// Chat Completions
if (url.pathname === "/v1/chat/completions" && method === "POST") {
let body = "";
req.on("data", (chunk) => { body += chunk; });
req.on("end", async () => {
try {
log(`INCOMING REQUEST (Size: ${body.length})`);
const incomingVqd = req.headers["x-vqd-4"] as string | undefined;
const jsonBody = JSON.parse(body);
if (jsonBody.stream) {
const { stream, vqd } = await openAIService.createChatCompletionStream(jsonBody, incomingVqd);
res.writeHead(200, {
"Content-Type": "text/event-stream",
...corsHeaders,
"x-vqd-4": vqd || "",
"X-Content-Type-Options": "nosniff"
});
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
}
} finally {
res.end();
reader.releaseLock();
}
return;
}
const { completion, vqd } = await openAIService.createChatCompletion(jsonBody, incomingVqd);
log(`SUCCESS: Sent response for ${jsonBody.model}`);
res.writeHead(200, {
"Content-Type": "application/json",
...corsHeaders,
"x-vqd-4": vqd || ""
});
res.write(JSON.stringify(completion));
res.end();
} catch (error: any) {
log(`ERROR: ${error.message}`);
res.writeHead(500, { "Content-Type": "application/json", ...corsHeaders });
res.write(JSON.stringify({ error: error.message, details: error.stack }));
res.end();
}
});
return;
}
// === DIAGNOSTIC ENDPOINT ===
if (url.pathname === "/v1/diagnose" && method === "GET") {
log("RUNNING SYSTEM DIAGNOSTICS...");
const report: any = {
status: "running",
checks: {}
};
// 1. Check Node.js Environment
report.checks.node = {
version: process.version,
memory: process.memoryUsage(),
uptime: process.uptime()
};
// 2. Check Network & DuckDuckGo Connectivity
try {
const start = Date.now();
// Check IP
const ipRes = await fetch("https://api.ipify.org?format=json");
const ipData = await ipRes.json() as any;
const res = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
"x-vqd-accept": "1"
}
}); const hash = res.headers.get("x-vqd-hash-1");
report.checks.duckduckgo = {
status: res.status,
latency_ms: Date.now() - start,
vqd_present: !!hash,
ip_blocked: res.status === 403 || res.status === 418,
server_ip: ipData.ip
};
} catch (e: any) {
report.checks.duckduckgo = { error: e.message };
}
// 3. Check JSDOM & Canvas Mock
try {
const { JSDOM } = require("jsdom");
const dom = new JSDOM("");
const canvas = dom.window.document.getElementById('c');
const ctx = canvas.getContext('2d'); // Should use our patch logic if imported, but here testing raw lib
report.checks.jsdom = {
ok: true,
canvas_created: !!canvas,
context_2d: !!ctx
};
} catch (e: any) {
report.checks.jsdom = { error: e.message };
}
sendJSON(200, report);
return;
}
// Not Found
res.writeHead(404, corsHeaders); res.end();
} catch (error: any) {
log(`CRITICAL ERR: ${error.message}`);
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Internal Server Error" }));
}
});
// Prevent server from crashing
process.on('uncaughtException', (err) => {
log(`CRITICAL: Uncaught Exception: ${err.message}\n${err.stack}`);
});
process.on('unhandledRejection', (reason, promise) => {
log(`CRITICAL: Unhandled Rejection: ${reason}`);
});
server.listen(PORT, () => {
log(`API Gateway started on port ${PORT}`);
});