Spaces:
Runtime error
Runtime error
Delete server.js
Browse files
server.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
| 1 |
-
const http = require('http');
|
| 2 |
-
const fs = require('fs');
|
| 3 |
-
const path = require('path');
|
| 4 |
-
const WebSocket = require('ws');
|
| 5 |
-
|
| 6 |
-
const PORT = 7860;
|
| 7 |
-
const GATEWAY_PORT = 18789;
|
| 8 |
-
|
| 9 |
-
let gatewayWs = null;
|
| 10 |
-
const sessions = new Map();
|
| 11 |
-
|
| 12 |
-
// Connect to local OpenClaw gateway (no pairing needed for loopback)
|
| 13 |
-
function connectToGateway() {
|
| 14 |
-
return new Promise((resolve, reject) => {
|
| 15 |
-
console.log('๐ Connecting to OpenClaw Gateway (localhost)...');
|
| 16 |
-
|
| 17 |
-
const ws = new WebSocket(`ws://127.0.0.1:${GATEWAY_PORT}/`);
|
| 18 |
-
|
| 19 |
-
ws.on('open', () => {
|
| 20 |
-
console.log('โ
Connected to OpenClaw Gateway');
|
| 21 |
-
gatewayWs = ws;
|
| 22 |
-
resolve();
|
| 23 |
-
});
|
| 24 |
-
|
| 25 |
-
ws.on('message', (data) => {
|
| 26 |
-
try {
|
| 27 |
-
const msg = JSON.parse(data.toString());
|
| 28 |
-
console.log('๐ฅ OpenClaw response:', JSON.stringify(msg).substring(0, 150));
|
| 29 |
-
handleGatewayMessage(msg);
|
| 30 |
-
} catch (e) {
|
| 31 |
-
console.log('๐ฅ Raw:', data.toString().substring(0, 150));
|
| 32 |
-
}
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
ws.on('error', (err) => {
|
| 36 |
-
console.error('โ Gateway connection error:', err.message);
|
| 37 |
-
gatewayWs = null;
|
| 38 |
-
});
|
| 39 |
-
|
| 40 |
-
ws.on('close', () => {
|
| 41 |
-
console.log('Gateway connection closed');
|
| 42 |
-
gatewayWs = null;
|
| 43 |
-
// Reconnect after delay
|
| 44 |
-
setTimeout(() => {
|
| 45 |
-
if (!gatewayWs) connectToGateway().catch(() => {});
|
| 46 |
-
}, 3000);
|
| 47 |
-
});
|
| 48 |
-
});
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
// Handle messages from OpenClaw gateway
|
| 52 |
-
function handleGatewayMessage(msg) {
|
| 53 |
-
// Extract session key
|
| 54 |
-
const sessionKey = msg.sessionKey;
|
| 55 |
-
if (!sessionKey) return;
|
| 56 |
-
|
| 57 |
-
const session = sessions.get(sessionKey);
|
| 58 |
-
if (session && session.pendingResolve) {
|
| 59 |
-
// Extract reply from various message formats
|
| 60 |
-
let reply = null;
|
| 61 |
-
|
| 62 |
-
if (msg.type === 'text' || msg.type === 'chunk') {
|
| 63 |
-
reply = msg.content || msg.text;
|
| 64 |
-
} else if (msg.choices && msg.choices[0]?.message?.content) {
|
| 65 |
-
reply = msg.choices[0].message.content;
|
| 66 |
-
} else if (msg.content) {
|
| 67 |
-
reply = msg.content;
|
| 68 |
-
} else if (msg.message) {
|
| 69 |
-
reply = msg.message;
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
if (reply) {
|
| 73 |
-
// Accumulate chunks
|
| 74 |
-
if (!session.reply) session.reply = '';
|
| 75 |
-
session.reply += reply;
|
| 76 |
-
|
| 77 |
-
// If done or complete message, resolve
|
| 78 |
-
if (msg.type === 'done' || msg.type === 'text' || msg.complete) {
|
| 79 |
-
session.pendingResolve({ reply: session.reply });
|
| 80 |
-
session.pendingResolve = null;
|
| 81 |
-
session.reply = '';
|
| 82 |
-
}
|
| 83 |
-
}
|
| 84 |
-
}
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
// Send message through OpenClaw
|
| 88 |
-
function sendToOpenClaw(message, sessionId) {
|
| 89 |
-
return new Promise((resolve, reject) => {
|
| 90 |
-
if (!gatewayWs || gatewayWs.readyState !== WebSocket.OPEN) {
|
| 91 |
-
reject(new Error('Gateway not connected'));
|
| 92 |
-
return;
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
const sessionKey = `webchat:direct:${sessionId}`;
|
| 96 |
-
|
| 97 |
-
// Track session
|
| 98 |
-
if (!sessions.has(sessionKey)) {
|
| 99 |
-
sessions.set(sessionKey, { messages: [], reply: '' });
|
| 100 |
-
}
|
| 101 |
-
const session = sessions.get(sessionKey);
|
| 102 |
-
session.pendingResolve = resolve;
|
| 103 |
-
|
| 104 |
-
// Send to OpenClaw gateway
|
| 105 |
-
const payload = {
|
| 106 |
-
type: 'message',
|
| 107 |
-
content: message,
|
| 108 |
-
sessionKey: sessionKey,
|
| 109 |
-
channel: 'webchat',
|
| 110 |
-
chatType: 'direct',
|
| 111 |
-
from: { id: sessionId, name: 'Web User' }
|
| 112 |
-
};
|
| 113 |
-
|
| 114 |
-
console.log('๐ค To OpenClaw:', message.substring(0, 50));
|
| 115 |
-
gatewayWs.send(JSON.stringify(payload));
|
| 116 |
-
|
| 117 |
-
// Timeout after 2 minutes
|
| 118 |
-
setTimeout(() => {
|
| 119 |
-
if (session.pendingResolve === resolve) {
|
| 120 |
-
session.pendingResolve = null;
|
| 121 |
-
if (session.reply) {
|
| 122 |
-
resolve({ reply: session.reply });
|
| 123 |
-
session.reply = '';
|
| 124 |
-
} else {
|
| 125 |
-
reject(new Error('Response timeout'));
|
| 126 |
-
}
|
| 127 |
-
}
|
| 128 |
-
}, 120000);
|
| 129 |
-
});
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
// HTTP Server - serves UI and proxies to OpenClaw
|
| 133 |
-
const server = http.createServer(async (req, res) => {
|
| 134 |
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 135 |
-
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 136 |
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
| 137 |
-
|
| 138 |
-
if (req.method === 'OPTIONS') {
|
| 139 |
-
res.writeHead(200);
|
| 140 |
-
res.end();
|
| 141 |
-
return;
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
// Serve UI
|
| 145 |
-
if (req.url === '/' || req.url === '/index.html') {
|
| 146 |
-
const html = fs.readFileSync(path.join(__dirname, 'public', 'index.html'), 'utf8');
|
| 147 |
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
| 148 |
-
res.end(html);
|
| 149 |
-
return;
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
// Chat endpoint - goes through OpenClaw
|
| 153 |
-
if (req.url === '/chat' && req.method === 'POST') {
|
| 154 |
-
let body = '';
|
| 155 |
-
req.on('data', chunk => body += chunk);
|
| 156 |
-
req.on('end', async () => {
|
| 157 |
-
try {
|
| 158 |
-
const { message, sessionId } = JSON.parse(body);
|
| 159 |
-
const sid = sessionId || `web-${Date.now()}`;
|
| 160 |
-
|
| 161 |
-
// Send through OpenClaw gateway
|
| 162 |
-
const response = await sendToOpenClaw(message, sid);
|
| 163 |
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
| 164 |
-
res.end(JSON.stringify(response));
|
| 165 |
-
} catch (err) {
|
| 166 |
-
console.error('โ Chat error:', err.message);
|
| 167 |
-
res.writeHead(500, { 'Content-Type': 'application/json' });
|
| 168 |
-
res.end(JSON.stringify({ error: err.message }));
|
| 169 |
-
}
|
| 170 |
-
});
|
| 171 |
-
return;
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
res.writeHead(404);
|
| 175 |
-
res.end('Not found');
|
| 176 |
-
});
|
| 177 |
-
|
| 178 |
-
// Main
|
| 179 |
-
async function main() {
|
| 180 |
-
try {
|
| 181 |
-
// Wait for gateway to be ready, then connect
|
| 182 |
-
let retries = 30;
|
| 183 |
-
while (retries > 0) {
|
| 184 |
-
try {
|
| 185 |
-
await connectToGateway();
|
| 186 |
-
break;
|
| 187 |
-
} catch (e) {
|
| 188 |
-
retries--;
|
| 189 |
-
await new Promise(r => setTimeout(r, 1000));
|
| 190 |
-
}
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
if (!gatewayWs) {
|
| 194 |
-
throw new Error('Could not connect to OpenClaw Gateway');
|
| 195 |
-
}
|
| 196 |
-
|
| 197 |
-
server.listen(PORT, '0.0.0.0', () => {
|
| 198 |
-
console.log(`\n๐ OpenClaw Web Interface running on port ${PORT}`);
|
| 199 |
-
console.log(`๐ Open your Space URL to chat with Zero!\n`);
|
| 200 |
-
});
|
| 201 |
-
} catch (err) {
|
| 202 |
-
console.error('โ Startup failed:', err);
|
| 203 |
-
process.exit(1);
|
| 204 |
-
}
|
| 205 |
-
}
|
| 206 |
-
|
| 207 |
-
main();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|