const express = require('express'); const { spawn, exec } = require('child_process'); const path = require('path'); const app = express(); const PORT = 7860; let shellularLogs = "Awaiting System Initialization...\r\n"; app.use(express.static('public')); app.use(express.json()); app.get('/api/logs', (req, res) => { res.send({ logs: shellularLogs }); }); app.post('/api/login', (req, res) => { const { hfToken } = req.body; shellularLogs = "\x1b[33m[System]\x1b[0m Cleaning up previous sessions...\r\n"; // 1. Kill any existing shellular process & clear corrupt cache to prevent 'invalid_union' errors const cleanupCommand = "npx -y shellular stop || pkill -f shellular || true"; exec(cleanupCommand, () => { shellularLogs += "\x1b[32m[System]\x1b[0m Cleanup complete.\r\n"; shellularLogs += "\x1b[34m[HFAuth]\x1b[0m Initiating Hugging Face Authentication...\r\n"; // 2. HF Login const hfCommand = hfToken ? `huggingface-cli login --token ${hfToken} --add-to-git-credential` : 'echo "No HF Token provided, skipping HF auth"'; exec(hfCommand, (err, stdout, stderr) => { if (err) shellularLogs += `\x1b[31m[HF Error]\x1b[0m ${stderr}\r\n`; else shellularLogs += `\x1b[32m[HF Success]\x1b[0m Authenticated.\r\n`; shellularLogs += "\r\n\x1b[36m[Shellular]\x1b[0m Starting Daemon Host...\r\n"; // 3. Start fresh Shellular const shellularProcess = spawn('npx', ['-y', 'shellular', '--unknown-clients', 'always-allow']); shellularProcess.stdout.on('data', (data) => { shellularLogs += data.toString().replace(/\n/g, '\r\n'); }); shellularProcess.stderr.on('data', (data) => { shellularLogs += data.toString().replace(/\n/g, '\r\n'); }); shellularProcess.on('close', (code) => { shellularLogs += `\r\n\x1b[31m[System]\x1b[0m Process exited with code ${code}\r\n`; }); }); }); res.json({ success: true, message: "System Booting..." }); }); app.listen(PORT, '0.0.0.0', () => { console.log(`Server is running on http://0.0.0.0:${PORT}`); });