GeminiBot commited on
Commit
b3c25c2
·
1 Parent(s): 6aa271e

Add /v1/diagnose endpoint for deep system health check

Browse files
Files changed (1) hide show
  1. src/server.ts +76 -26
src/server.ts CHANGED
@@ -85,32 +85,82 @@ const server = createServer(async (req: IncomingMessage, res: ServerResponse) =>
85
  }
86
 
87
  // Chat Completions
88
- if (url.pathname === "/v1/chat/completions" && method === "POST") {
89
- let body = "";
90
- req.on("data", (chunk) => { body += chunk; });
91
- req.on("end", async () => {
92
- try {
93
- // Log full request for debugging
94
- log(`INCOMING REQUEST:\n${body}`);
95
-
96
- const jsonBody = JSON.parse(body);
97
- const completion = await openAIService.createChatCompletion(jsonBody);
98
-
99
- // Log success summary
100
- log(`SUCCESS: Sent response for ${jsonBody.model}`);
101
-
102
- sendJSON(200, completion);
103
- } catch (error: any) {
104
- log(`ERROR: ${error.message}\nSTACK: ${error.stack}`);
105
- sendJSON(500, { error: error.message, details: error.stack });
106
- }
107
- });
108
- return;
109
- }
110
-
111
- // Not Found
112
- res.writeHead(404, corsHeaders);
113
- res.end();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  } catch (error: any) {
116
  log(`CRITICAL ERR: ${error.message}`);
 
85
  }
86
 
87
  // Chat Completions
88
+ if (url.pathname === "/v1/chat/completions" && method === "POST") {
89
+ let body = "";
90
+ req.on("data", (chunk) => { body += chunk; });
91
+ req.on("end", async () => {
92
+ try {
93
+ // Log full request for debugging
94
+ log(`INCOMING REQUEST:\n${body}`);
95
+
96
+ const jsonBody = JSON.parse(body);
97
+ const completion = await openAIService.createChatCompletion(jsonBody);
98
+
99
+ // Log success summary
100
+ log(`SUCCESS: Sent response for ${jsonBody.model}`);
101
+
102
+ sendJSON(200, completion);
103
+ } catch (error: any) {
104
+ log(`ERROR: ${error.message}\nSTACK: ${error.stack}`);
105
+ sendJSON(500, { error: error.message, details: error.stack });
106
+ }
107
+ });
108
+ return;
109
+ }
110
+
111
+ // === DIAGNOSTIC ENDPOINT ===
112
+ if (url.pathname === "/v1/diagnose" && method === "GET") {
113
+ log("RUNNING SYSTEM DIAGNOSTICS...");
114
+ const report: any = {
115
+ status: "running",
116
+ checks: {}
117
+ };
118
+
119
+ // 1. Check Node.js Environment
120
+ report.checks.node = {
121
+ version: process.version,
122
+ memory: process.memoryUsage(),
123
+ uptime: process.uptime()
124
+ };
125
+
126
+ // 2. Check Network & DuckDuckGo Connectivity
127
+ try {
128
+ const start = Date.now();
129
+ const res = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", {
130
+ 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" }
131
+ });
132
+ const hash = res.headers.get("x-vqd-hash-1");
133
+ report.checks.duckduckgo = {
134
+ status: res.status,
135
+ latency_ms: Date.now() - start,
136
+ vqd_present: !!hash,
137
+ ip_blocked: res.status === 403 || res.status === 418
138
+ };
139
+ } catch (e: any) {
140
+ report.checks.duckduckgo = { error: e.message };
141
+ }
142
+
143
+ // 3. Check JSDOM & Canvas Mock
144
+ try {
145
+ const { JSDOM } = require("jsdom");
146
+ const dom = new JSDOM("<!DOCTYPE html><canvas id='c'></canvas>");
147
+ const canvas = dom.window.document.getElementById('c');
148
+ const ctx = canvas.getContext('2d'); // Should use our patch logic if imported, but here testing raw lib
149
+ report.checks.jsdom = {
150
+ ok: true,
151
+ canvas_created: !!canvas,
152
+ context_2d: !!ctx
153
+ };
154
+ } catch (e: any) {
155
+ report.checks.jsdom = { error: e.message };
156
+ }
157
+
158
+ sendJSON(200, report);
159
+ return;
160
+ }
161
+
162
+ // Not Found
163
+ res.writeHead(404, corsHeaders); res.end();
164
 
165
  } catch (error: any) {
166
  log(`CRITICAL ERR: ${error.message}`);