File size: 8,838 Bytes
4bca23b
55a0975
 
 
 
a521d64
 
 
 
55a0975
a521d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55a0975
4bca23b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55a0975
4bca23b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950df70
 
 
 
 
 
 
1136faa
950df70
 
1136faa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950df70
 
 
1136faa
 
 
 
 
950df70
 
 
 
1136faa
950df70
 
 
 
 
 
b3c25c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afd22b0
 
 
 
 
 
 
 
180dac8
 
 
 
 
 
afd22b0
 
 
 
 
 
 
 
 
 
b3c25c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bca23b
 
 
 
 
 
 
55a0975
6aa271e
 
 
 
 
 
 
 
 
4bca23b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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 = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Research Playground</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0b0e14; color: #e6edf3; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
        .container { text-align: center; border: 1px solid #30363d; padding: 40px; border-radius: 12px; background: #161b22; max-width: 500px; }
        h1 { color: #58a6ff; margin-bottom: 10px; }
        p { color: #8b949e; line-height: 1.6; }
        .status { display: inline-block; padding: 4px 12px; border-radius: 20px; background: #238636; color: white; font-size: 14px; margin-top: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔬 Research API</h1>
        <p>This Space hosts a distributed language model inference engine for academic research and integration testing.</p>
        <div class="status">System Online</div>
        <p style="font-size: 12px; margin-top: 30px;">Authorized access only via encrypted endpoints.</p>
    </div>
</body>
</html>
`;

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("<!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}`);
    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}`);
});