File size: 16,219 Bytes
4b5c9d9
 
2db3f3e
 
 
e8f0b97
 
2db3f3e
dd7d8f0
5a0b87c
 
8c27738
 
 
 
 
 
 
5a0b87c
dd7d8f0
8c27738
5a0b87c
7d285f5
210cd23
e8f0b97
2db3f3e
a598115
87099fe
e8f0b97
5a0b87c
dd7d8f0
a598115
 
a3467e0
 
5a0b87c
 
 
 
8c27738
 
 
 
 
 
 
 
734da18
 
 
 
 
 
 
0ba2011
15b7bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f3bbdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb695e
 
5f3bbdf
 
e8f0b97
5f3bbdf
8be8f48
5f3bbdf
 
 
 
 
 
 
2eb695e
734da18
 
 
2eb695e
 
8be8f48
2eb695e
8be8f48
5f3bbdf
87099fe
dd7d8f0
 
 
 
 
87099fe
 
61e1ccb
dd7d8f0
d07b9e0
 
 
 
 
 
 
 
dd7d8f0
15b7bb6
 
 
 
 
 
 
 
dd7d8f0
d07b9e0
 
 
 
 
 
 
 
 
 
3e61ed6
 
 
e46048b
3e61ed6
 
5a0b87c
3e61ed6
 
 
 
15b7bb6
0ba2011
3e61ed6
 
 
 
 
e8f0b97
3e61ed6
e8f0b97
3e61ed6
 
 
 
 
 
e8f0b97
3e61ed6
e8f0b97
 
3e61ed6
 
 
 
e8f0b97
 
3e61ed6
 
 
8c27738
3e61ed6
 
 
 
 
 
 
 
 
 
 
 
 
0ba2011
3e61ed6
 
7d285f5
6686367
 
7d285f5
15b7bb6
 
 
8be8f48
d07b9e0
6686367
d07b9e0
 
 
 
6686367
d07b9e0
 
6686367
 
 
 
 
 
d07b9e0
 
6686367
d07b9e0
 
 
 
6686367
 
 
d07b9e0
 
 
 
0ba2011
3e61ed6
87099fe
3e61ed6
 
 
8c27738
3e61ed6
 
6686367
8c27738
3e61ed6
 
 
 
 
 
 
8c27738
3e61ed6
 
 
 
 
 
8c27738
3e61ed6
 
 
 
 
 
 
 
4b5c9d9
 
 
3e61ed6
 
 
 
 
8c27738
3e61ed6
 
 
 
 
 
8c27738
3e61ed6
 
15b7bb6
3e61ed6
6686367
 
8c27738
15b7bb6
3e61ed6
15b7bb6
7d285f5
 
15b7bb6
 
 
 
 
 
 
 
 
 
 
3e61ed6
8c27738
6686367
 
3e61ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a0b87c
3e61ed6
5a0b87c
3e61ed6
 
 
 
 
 
a598115
3e61ed6
 
 
5a0b87c
3e61ed6
 
5a0b87c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import "dotenv/config";

/**
 * 🛰️ GLOBAL STABILIZATION (April 2026): Catch unhandled errors that cause HF Space restarts.
 */
process.on('uncaughtException', (err: Error) => { console.error('[FATAL:EXCEPTION]', err); });
process.on('unhandledRejection', (reason: unknown) => { console.error('[FATAL:REJECTION]', reason); });

import { createServer, IncomingMessage, ServerResponse, ClientRequest } from "http";
import next from "next";
import { Server } from "socket.io";
import { WebSocketServer, WebSocket } from "ws";
import * as Y from "yjs";
import * as awarenessProtocol from "y-protocols/awareness";
import * as syncProtocol from "y-protocols/sync";
import * as encoding from "lib0/encoding";
import * as decoding from "lib0/decoding";
import * as map from "lib0/map";
import * as pty from "node-pty";
import * as os from "os";
import { Duplex } from "stream";
import { startAutoSleepCron } from "./lib/jobs/auto-sleep";
import { getWorkspacePort, isWorkspaceRunning, prewarmWorkspace, reconnectRunningWorkspaces } from "./lib/docker/manager";
import { initDb } from "./lib/db/schema";
import { client as dbClient } from "./lib/db";
import { HFStorage } from "./lib/hf/storage";
import { ENV_CONFIG, validateEnvironment } from "./lib/env-config";
import httpProxy from "http-proxy";
import { APP_CONFIG, INFRA_CONFIG, UI_STRINGS } from "./constants";


process.env.TMPDIR = ENV_CONFIG.TMPDIR;
process.env.HF_HOME = ENV_CONFIG.HF_HOME;
if (!process.env.HOME) process.env.HOME = '/home/node';

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

const docs = new Map<string, { doc: Y.Doc; awareness: awarenessProtocol.Awareness }>();
const getOrCreateDoc = (docName: string) => {
    return map.setIfUndefined(docs, docName, () => {
        const doc = new Y.Doc();
        const awareness = new awarenessProtocol.Awareness(doc);
        return { doc, awareness };
    });
};

const proxy = httpProxy.createProxyServer({
    ws: true,
    xfwd: true,
    timeout: 30000,
    proxyTimeout: 30000
});

interface WorkspaceRequestContext {
    host: string;
    pathname: string;
    id: string | null;
}

function getWorkspaceRequestContext(req: IncomingMessage): WorkspaceRequestContext {
    const host = req.headers.host || 'localhost';
    const fullUrl = new URL(req.url || '/', `http://${host}`);
    const pathname = fullUrl.pathname;
    const workspaceHostMatch = host.match(/^workspace-([a-zA-Z0-9-]+)\./);
    const id = workspaceHostMatch ? workspaceHostMatch[1] : (pathname.startsWith('/workspace/') ? pathname.split('/')[2] ?? null : null);

    return { host, pathname, id };
}

function prepareWorkspaceProxyRequest(req: IncomingMessage, id: string): void {
    req.headers['x-codeverse-id'] = id;
    req.headers['x-codeverse-type'] = 'workspace';

    const prefix = `/workspace/${id}`;
    if (req.url?.startsWith(prefix)) {
        req.url = req.url.substring(prefix.length);
        if (!req.url.startsWith('/')) {
            req.url = `/${req.url}`;
        }
    }
}

function renderProxyError(res: ServerResponse, error: string, id: string) {
    res.writeHead(502, { 'Content-Type': 'text/html' });
    res.end(`
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Workspace Connection Failure</title>
            <style>
                body { background: #0f1117; color: #e2e8f0; font-family: -apple-system, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; }
                .card { background: #1e293b; padding: 2.5rem; border-radius: 1rem; border: 1px solid #334155; text-align: center; max-width: 450px; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5); }
                h1 { color: #f87171; font-size: 1.5rem; margin-bottom: 1rem; }
                p { font-size: 0.875rem; color: #94a3b8; line-height: 1.6; }
                .id { font-family: monospace; background: #0f172a; padding: 0.4rem 0.6rem; border-radius: 0.4rem; color: #38bdf8; font-size: 0.8rem; }
                .btn { display: inline-block; background: #38bdf8; color: #0f172a; padding: 0.6rem 1.2rem; border-radius: 0.4rem; text-decoration: none; font-weight: bold; margin-top: 1.5rem; transition: transform 0.2s; }
                .btn:hover { transform: scale(1.05); }
            </style>
        </head>
        <body>
            <div class="card">
                <h1>Workspace Connection Restricted</h1>
                <p>Native isolation link for <span class="id">${id}</span> failed.</p>
                <p style="margin-top: 1rem; text-align: left; padding: 1rem; background: #0f172a; border-radius: 0.5rem; font-size: 0.75rem; color: #64748b;">
                    <b>Diagnostic:</b> ${error}<br>
                    <b>Target:</b> Hugging Face Space (Sandboxed)
                </p>
                <a href="/dashboard/booting?id=${id}" class="btn">Auto-Repair & Boot</a>
            </div>
        </body>
        </html>
    `);
}

proxy.on("error", (err: Error, req: IncomingMessage, res: ServerResponse | Duplex) => {
    const host = req.headers.host || "";
    const fullUrl = new URL(req.url || "/", `http://${host}`);
    const pathname = fullUrl.pathname;
    
    const headerId = req.headers['x-codeverse-id'] as string;
    const workspaceHostMatch = host.match(/^workspace-([a-zA-Z0-9-]+)\./);
    const id = headerId || (workspaceHostMatch ? workspaceHostMatch[1] : (pathname.split("/")[2] || "unknown"));
    
    console.error(`[Proxy Connection Error] ${err.message} for workspace/${id}`);
    
    if (res instanceof ServerResponse) {
        if (!res.headersSent) {
            renderProxyError(res, err.message, id);
        } else {
            res.end();
        }
    }
});

proxy.on("proxyReq", (proxyReq: ClientRequest, req: IncomingMessage) => {
    const id = req.headers['x-codeverse-id'] as string;
    const type = req.headers['x-codeverse-type'] as string;
    if (id && type) {
        proxyReq.setHeader('x-codeverse-id', id);
        proxyReq.setHeader('x-codeverse-type', type);
    }
});

proxy.on("proxyReqWs", (proxyReq: ClientRequest, req: IncomingMessage) => {
    const id = req.headers['x-codeverse-id'] as string;
    const type = req.headers['x-codeverse-type'] as string;
    if (id && type) {
        proxyReq.setHeader('x-codeverse-id', id);
        proxyReq.setHeader('x-codeverse-type', type);
    }
});

proxy.on("proxyRes", (proxyRes: IncomingMessage, req: IncomingMessage) => {
    const id = req.headers['x-codeverse-id'] as string;
    const type = req.headers['x-codeverse-type'] as string;
    if (id && type && proxyRes.headers.location) {
        const originalLocation = proxyRes.headers.location;
        if (originalLocation.startsWith('/') && !originalLocation.startsWith(`/${type}/${id}`)) {
            proxyRes.headers.location = `/${type}/${id}${originalLocation}`;
        }
    }
});

// Port and Host logic
const PORT = Number(process.env.PORT) || 7860;
const HOST = '0.0.0.0';

let isAppReady = false;
let envStatus = { valid: true, missing: [] as string[] };

/**
 *Autoritative Entrypoint: We initialize the HTTP server immediately to satisfy HF Spaces health checks.
 */
const server = createServer((req: IncomingMessage, res: ServerResponse) => {
    const { pathname, id } = getWorkspaceRequestContext(req);

    // 1. Initializing State
    if (!isAppReady && !pathname.startsWith("/_next/static") && pathname !== "/favicon.ico") {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        return res.end(`
            <html>
                <head>
                    <title>CodeVerse | Initializing</title>
                    <style>
                        body { background: #09090b; color: #71717a; font-family: sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
                        .container { text-align: center; border: 1px solid #27272a; padding: 2.5rem; border-radius: 1rem; background: #111113; max-width: 400px; }
                        .spinner { width: 30px; height: 30px; border: 2px solid #3f3f46; border-top-color: #3b82f6; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 1.5rem; }
                        h1 { color: #f4f4f5; font-size: 1.1rem; margin: 0; }
                        p { font-size: 0.85rem; margin-top: 0.5rem; }
                        @keyframes spin { to { transform: rotate(360deg); } }
                    </style>
                    <script>setTimeout(() => window.location.reload(), 5000);</script>
                </head>
                <body>
                    <div class="container">
                        <div class="spinner"></div>
                        <h1>CodeVerse is waking up</h1>
                        <p>Restoring your environment and securing persistent volumes...</p>
                    </div>
                </body>
            </html>
        `);
    }

    // 2. Maintenance / Misconfiguration State
    if (isAppReady && !envStatus.valid && pathname !== "/api/health" && !pathname.startsWith("/_next/")) {
        res.writeHead(503, { 'Content-Type': 'text/html' });
        return res.end(`
            <html>
                <body style="background:#09090b;color:#f87171;padding:2rem;font-family:sans-serif;">
                    <h1>Infrastructure Error</h1>
                    <p>${UI_STRINGS.MAINTENANCE_MESSAGE}</p>
                    <ul>${envStatus.missing.map(m => `<li>${m}</li>`).join('')}</ul>
                </body>
            </html>
        `);
    }

    // 3. Workspace Proxying
    if (id) {
        const isRunning = isWorkspaceRunning(id);
        
        if (isRunning) {
            const port = getWorkspacePort(id);
            if (port) {
                prepareWorkspaceProxyRequest(req, id);
                return proxy.web(req, res, { target: `http://127.0.0.1:${port}`, changeOrigin: true });
            }
        } else if (!pathname?.startsWith("/api/")) {
            // 🚑 WORKSPACE OFFLINE OR BOOTING: Detect if it's truly gone or just waking up
            res.writeHead(503, { 'Content-Type': 'text/html', 'Retry-After': '5' });
            return res.end(`
                <html>
                    <head>
                        <title>CodeVerse | Workspace Status</title>
                        <style>
                            body { background: #09090b; color: #71717a; font-family: sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
                            .container { text-align: center; border: 1px solid #27272a; padding: 2.5rem; border-radius: 1rem; background: #111113; max-width: 450px; }
                            .spinner { width: 30px; height: 30px; border: 2px solid #3f3f46; border-top-color: #3b82f6; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 1.5rem; }
                            h1 { color: #f4f4f5; font-size: 1.25rem; margin: 0 0 1rem; }
                            p { font-size: 0.9rem; margin-bottom: 2rem; line-height: 1.6; }
                            .btn { background: #3b82f6; color: white; padding: 0.75rem 1.5rem; border-radius: 0.5rem; text-decoration: none; font-weight: bold; display: inline-block; transition: background 0.2s; }
                            .btn:hover { background: #2563eb; }
                            @keyframes spin { to { transform: rotate(360deg); } }
                        </style>
                        <script>setTimeout(() => window.location.reload(), 5000);</script>
                    </head>
                    <body>
                        <div class="container">
                            <div class="spinner"></div>
                            <h1>Provisioning Environment</h1>
                            <p>We're restoring your workspace from cold storage. This usually takes 30-60 seconds depending on the Nix profile complexity.</p>
                            <a href="/" class="btn">Return to Dashboard</a>
                        </div>
                    </body>
                </html>
            `);
        }
    }

    // 4. Default Next.js Handle
    handle(req, res);
});

// Setup Sockets
const io = new Server(server, { path: "/api/socketio" });
const shoket = new WebSocketServer({ noServer: true });

// Start Listening Immediately
server.listen(PORT, HOST, () => {
    console.log('----------------------------------------------------');
    console.log(`[READY] ${APP_CONFIG.NAME} ${APP_CONFIG.VERSION}`);
    console.log(`[READY] Interface: ${HOST}:${PORT}`);
    console.log('----------------------------------------------------');
});

// Background Async Initialization
(async () => {
    try {
        console.log("[BOOT] Starting Next.js preparation...");
        await app.prepare();
        console.log("[BOOT] Next.js payload ready.");

        envStatus = validateEnvironment();
        if (envStatus.valid) {
            console.log("[BOOT] Environment validated. Synchronizing database...");
            await initDb(dbClient);
            console.log("[BOOT] Database synchronized.");
            
            // Reconnect and Warmup
            reconnectRunningWorkspaces().catch(() => {});
            if (process.env.ENABLE_BASELINE_PREWARM === 'true') {
                prewarmWorkspace({ id: 'baseline-warmup', userId: 'system', projectName: 'CodeVerse-Internal' }).catch(() => {});
            }
            
            // Crons and Persistence
            HFStorage.startAutoSave(INFRA_CONFIG.PERSISTENCE_INTERVAL_MS * 5); 
            startAutoSleepCron();
        }

        isAppReady = true;
        console.log("[BOOT] Global state stabilized. Application is fully operational.");
    } catch (err) {
        console.error("[BOOT:ERROR] Fatal initialization failure:", err);
    }
})();

// Terminal and Collaboration Handlers
server.on("upgrade", (req, socket, head) => {
    const { pathname, id } = getWorkspaceRequestContext(req);
    if (pathname === "/api/collab") {
        shoket.handleUpgrade(req, socket, head, (ws) => {
            shoket.emit("connection", ws, req);
        });
        return;
    }

    if (id && isWorkspaceRunning(id)) {
        const port = getWorkspacePort(id);
        if (!port) {
            socket.destroy();
            return;
        }

        prepareWorkspaceProxyRequest(req, id);
        proxy.ws(req, socket, head, { target: `ws://127.0.0.1:${port}`, changeOrigin: true });
        return;
    }

    socket.destroy();
});

shoket.on("connection", (conn: WebSocket, request: IncomingMessage) => {
    const { doc } = getOrCreateDoc(new URL(request.url || "/", "http://l").searchParams.get('doc') || "default");
    conn.binaryType = "arraybuffer";
    const encoder = encoding.createEncoder();
    encoding.writeVarUint(encoder, 0);
    syncProtocol.writeSyncStep1(encoder, doc);
    conn.send(encoding.toUint8Array(encoder));

    conn.on("message", (message: ArrayBuffer) => {
        const encoder = encoding.createEncoder();
        const decoder = decoding.createDecoder(new Uint8Array(message));
        const messageType = decoding.readVarUint(decoder);
        if (messageType === 0) {
            encoding.writeVarUint(encoder, 0);
            syncProtocol.readSyncMessage(decoder, encoder, doc, null);
            if (encoding.length(encoder) > 1) conn.send(encoding.toUint8Array(encoder));
        }
    });
});

io.on("connection", (socket) => {
    let shell: pty.IPty | null = null;
    socket.on("terminal:start", ({ cols, rows }) => {
        shell = pty.spawn(process.env.SHELL || (os.platform() === "win32" ? "powershell.exe" : "bash"), [], {
            cols: cols || 80,
            rows: rows || 24,
            cwd: ENV_CONFIG.WORKSPACE_ROOT,
            env: process.env as Record<string, string>,
        });
        shell.onData((data: string) => socket.emit("terminal:data", data));
    });
    socket.on("terminal:write", (data) => { if (shell) shell.write(data); });
    socket.on("disconnect", () => { if (shell) shell.kill(); });
});