GeminiBot commited on
Commit ·
b3c25c2
1
Parent(s): 6aa271e
Add /v1/diagnose endpoint for deep system health check
Browse files84 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111211388899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516684 unmodified lines }
// Chat Completions if (url.pathname === "/v1/chat/completions" && method === "POST") { let body = ""; req.on("data", (chunk) => { body += chunk; }); req.on("end", async () => { try { // Log full request for debugging log(`INCOMING REQUEST:\n${body}`); const jsonBody = JSON.parse(body); const completion = await openAIService.createChatCompletion(jsonBody); // Log success summary log(`SUCCESS: Sent response for ${jsonBody.model}`); sendJSON(200, completion); } catch (error: any) { log(`ERROR: ${error.message}\nSTACK: ${error.stack}`); sendJSON(500, { error: error.message, details: error.stack }); } }); return; }
// Not Found res.writeHead(404, corsHeaders); res.end(); if (url.pathname === "/v1/chat/completions" && method === "POST") { let body = ""; req.on("data", (chunk) => { body += chunk; }); req.on("end", async () => { try { // Log full request for debugging log(`INCOMING REQUEST:\n${body}`); const jsonBody = JSON.parse(body); const completion = await openAIService.createChatCompletion(jsonBody); // Log success summary log(`SUCCESS: Sent response for ${jsonBody.model}`); sendJSON(200, completion); } catch (error: any) { log(`ERROR: ${error.message}\nSTACK: ${error.stack}`); sendJSON(500, { error: error.message, details: error.stack }); } }); 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(); 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" } }); 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 }; } catch (e: any) { report.checks.duckduckgo = { error: e.message }; } // 3. Check JSDOM & Canvas Mock try { const { JSDOM } = require("jsdom"); const dom = new JSDOM("<!DOCTYPE html><canvas id='c'></canvas>"); 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}`);src/server.ts+76-26