-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
439 lines (370 loc) · 15.8 KB
/
server.js
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import { AgentProcess } from './src/process/agent-process.js';
import { app as electronApp } from 'electron';
import express from 'express';
import http from 'http';
import { WebSocketServer, WebSocket } from 'ws';
import { HTTPS_BACKEND_URL, WSS_BACKEND_URL } from './src/constants.js';
import fs from 'fs';
import cors from 'cors';
import path from 'path';
import net from 'net';
import { GlobalKeyboardListener } from 'node-global-key-listener';
const logFile = path.join(electronApp.getPath('userData'), 'app.log');
const logStream = fs.createWriteStream(logFile, { flags: 'a' });
let wss; // Declare wss in the outer scope
let isToggleToTalkActive = false; // Global state for toggle_to_talk
let isKeyDown = false; // Track key state
let gkl; // Declare the global keyboard listener
let voice_mode = 'off'; // Global voice_mode variable with default value
function logToFile(message) {
logStream.write(`${new Date().toISOString()} - ${message}\n`);
}
function broadcastMessage(message) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
function notifyBotKicked() {
logToFile("Bot was kicked");
broadcastMessage("Error: Bot kicked.");
}
function setupVoice(settings) {
voice_mode = settings.voice_mode; // Assign voice_mode from settings
const { key_binding } = settings;
if ((voice_mode === 'push_to_talk' || voice_mode === 'toggle_to_talk') && key_binding) {
gkl = new GlobalKeyboardListener();
gkl.addListener((e) => {
if (e.name.toLowerCase() === key_binding.toLowerCase()) {
if (e.state === 'DOWN') {
if (voice_mode === 'push_to_talk') {
isKeyDown = true;
console.log('Push-to-talk key down:', isKeyDown);
} else if (voice_mode === 'toggle_to_talk') {
isToggleToTalkActive = !isToggleToTalkActive;
console.log('Toggle-to-talk active:', isToggleToTalkActive);
}
} else if (e.state === 'UP' && voice_mode === 'push_to_talk') {
isKeyDown = false;
console.log('Push-to-talk key up:', isKeyDown);
}
}
});
}
}
function startServer() {
logToFile("Starting server...");
const userDataDir = electronApp.getPath('userData');
if (!userDataDir || !fs.existsSync(userDataDir)) {
throw new Error("userDataDir must be provided and must exist");
}
const settingsPath = `${userDataDir}/settings.json`;
let settings;
if (!fs.existsSync(settingsPath)) {
settings = {
"minecraft_version": "1.20.4",
"host": "localhost",
"port": "25565",
"auth": "offline",
"player_username": "",
"profiles": [
"./ethan.json"
],
"load_memory": true,
"allow_insecure_coding": false,
"code_timeout_mins": 10,
"whisper_to_player": false,
"voice_mode": "always_on",
"key_binding": "",
"openai_api_key": "",
"model": "",
"language": "en"
};
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4));
} else {
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
}
let profiles = settings.profiles;
let load_memory = settings.load_memory;
let agentProcessStarted = false;
let agentProcesses = [];
const app = express();
const port = 10101;
const server = http.createServer(app);
wss = new WebSocketServer({ server }); // Initialize wss within startServer
// Configure CORS to allow credentials
app.use(cors({
origin: ['http://localhost', 'http://localhost:5173', 'http://localhost:4173'],
credentials: true
}));
// Debugging middleware to log incoming requests
app.use((req, res, next) => {
// logToFile(`Incoming request: ${req.method} ${req.url}`);
next();
});
let transcriptBuffer = "";
function broadcastMessage(message) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
wss.on("connection", (ws) => {
logToFile("socket: client connected");
const proxyWs = new WebSocket(`${WSS_BACKEND_URL}?language=${settings.language}`);
// Update settings
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
voice_mode = settings.voice_mode;
setupVoice(settings);
proxyWs.on('open', () => {
logToFile(`proxy: connected to ${WSS_BACKEND_URL}`);
});
proxyWs.on('message', (message) => {
// console.log(`Voice Mode: ${settings.voice_mode}, isKeyDown: ${isKeyDown}, isToggleToTalkActive: ${isToggleToTalkActive}`);
if (voice_mode === 'always_on' ||
(voice_mode === 'push_to_talk' && isKeyDown) ||
(voice_mode === 'toggle_to_talk' && isToggleToTalkActive)) {
const parsedMessage = JSON.parse(message.toString('utf8'));
const { is_final, speech_final, transcript } = parsedMessage;
if (is_final) {
transcriptBuffer += transcript;
}
if (speech_final) {
ws.send(transcriptBuffer); // to frontend
agentProcesses.forEach(agentProcess => {
agentProcess.sendTranscription(transcriptBuffer);
});
transcriptBuffer = "";
}
} else {
ws.send("Voice Disabled");
}
});
proxyWs.on('close', () => {
logToFile(`proxy: connection to ${WSS_BACKEND_URL} closed`);
ws.close();
});
proxyWs.on('error', (error) => {
logToFile(`proxy: error ${error}`);
ws.close();
});
ws.on("message", (message) => {
if (proxyWs.readyState === WebSocket.OPEN) {
proxyWs.send(message);
} else {
logToFile("socket: data couldn't be sent to proxy");
}
});
ws.on("close", () => {
logToFile("socket: client disconnected");
proxyWs.close();
});
});
app.get('/backend-alive', async (req, res) => {
try {
const response = await fetch(`${HTTPS_BACKEND_URL}/ping`);
if (response.ok && await response.text() === 'pong') {
res.json({ backend_alive: true });
} else {
res.json({ backend_alive: false });
}
} catch (error) {
logToFile(`Heartbeat error: ${error.message}`);
res.json({ backend_alive: false });
}
});
app.get('/settings', (req, res) => {
const profilesDir = path.join(userDataDir, 'profiles');
const updatedProfiles = [];
const ethanTemplatePath = path.join(electronApp.getAppPath(), 'ethan.json');
const ethanTemplate = JSON.parse(fs.readFileSync(ethanTemplatePath, 'utf8'));
fs.readdirSync(profilesDir).forEach(file => {
if (file.endsWith('.json')) {
const profilePath = path.join(profilesDir, file);
const profileData = JSON.parse(fs.readFileSync(profilePath, 'utf8'));
// Replace fields with those from ethanTemplate
profileData.conversing = ethanTemplate.conversing;
profileData.coding = ethanTemplate.coding;
profileData.saving_memory = ethanTemplate.saving_memory;
// Write the updated profile back to the file
fs.writeFileSync(profilePath, JSON.stringify(profileData, null, 4));
updatedProfiles.push({
name: profileData.name,
personality: profileData.personality,
conversing: profileData.conversing,
coding: profileData.coding,
saving_memory: profileData.saving_memory
});
}
});
const updatedSettings = {
...settings,
profiles: updatedProfiles
};
fs.writeFileSync(settingsPath, JSON.stringify(updatedSettings, null, 4));
res.json(updatedSettings);
});
app.get('/check-server', (req, res) => {
const { host, port } = req.query;
const socket = new net.Socket();
socket.setTimeout(2000); // Set a timeout for the connection
socket.on('connect', () => {
logToFile(`Server at ${host}:${port} is reachable.`);
res.json({ alive: true });
socket.destroy(); // Close the connection
}).on('error', (err) => {
logToFile(`Server at ${host}:${port} is not reachable. Error: ${err.message}`);
res.json({ alive: false, error: err.message });
}).on('timeout', () => {
logToFile(`Server at ${host}:${port} is not reachable. Error: Timeout`);
res.json({ alive: false, error: 'Timeout' });
socket.destroy();
}).connect(port, host);
});
app.get('/agent-status', (req, res) => {
res.json({ agentStarted: agentProcessStarted });
});
app.post('/stop', (req, res) => {
logToFile('API: POST /stop called');
if (!agentProcessStarted) {
logToFile('API: No agent processes running');
return res.status(404).send('No agent processes are currently running.');
}
agentProcesses.forEach(agentProcess => {
agentProcess.agentProcess.kill();
});
agentProcesses = [];
agentProcessStarted = false;
logToFile('API: All agent processes stopped');
res.send('All agent processes have been stopped.');
});
app.post('/manual-chat', express.json(), (req, res) => {
const { botName, message } = req.body;
if (!botName || !message) {
return res.status(400).json({ error: "Both 'botName' and 'message' fields are required." });
}
let botFound = false;
agentProcesses.forEach(agentProcess => {
if (agentProcess.botName === botName) {
agentProcess.sendMessage(message);
botFound = true;
}
});
if (botFound) {
res.json({ message: "Message sent to the bot." });
} else {
res.status(404).json({ error: "Bot is not in game." });
}
});
app.post('/start', express.json(), (req, res) => {
logToFile('API: POST /start called');
if (agentProcessStarted) {
logToFile('API: Agent process already started');
return res.status(409).send('Agent process already started. Restart not allowed.');
}
const newSettings = req.body;
// Check for empty fields in newSettings, except for key_binding if voice_mode is always_on or off
const emptyFields = Object.entries(newSettings)
.filter(([key, value]) => {
// Skip API key and model checks if not using own API key
if (!newSettings.useOwnApiKey && (key === 'openai_api_key' || key === 'model')) {
return false;
}
if (key === 'profiles') return !Array.isArray(value) || value.length === 0;
if (key === 'key_binding' && (newSettings.voice_mode === 'always_on' || newSettings.voice_mode === 'off')) return false;
return value === "" || value === null || value === undefined;
})
.map(([key]) => key);
if (emptyFields.length > 0) {
return res.status(400).json({
error: "Empty fields not allowed",
emptyFields: emptyFields
});
}
// removed from UI, hardcoding these settings
newSettings.allow_insecure_coding = false;
newSettings.code_timeout_mins = 10;
newSettings.auth = "offline";
newSettings.load_memory = true;
Object.assign(settings, newSettings);
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4));
profiles = newSettings.profiles;
load_memory = newSettings.load_memory;
for (let profile of profiles) {
const profileBotName = profile.name;
const agentProcess = new AgentProcess(notifyBotKicked);
agentProcess.start(profileBotName, userDataDir, newSettings.useOwnApiKey, newSettings.openai_api_key, load_memory);
agentProcesses.push(agentProcess);
}
agentProcessStarted = true;
logToFile('API: Settings updated and AgentProcess started for all profiles');
res.send('Settings updated and AgentProcess started for all profiles');
});
app.post('/save-profiles', express.json(), (req, res) => {
const profilesDir = path.join(userDataDir, 'profiles');
const ethanTemplatePath = path.join(electronApp.getAppPath(), 'ethan.json');
const newProfiles = req.body.profiles;
// Validate input
if (!Array.isArray(newProfiles) || newProfiles.some(profile => !profile.name || !profile.personality)) {
return res.status(400).json({ error: "Invalid input. Each profile must have 'name' and 'personality' fields." });
}
// Delete all existing profiles
fs.readdirSync(profilesDir).forEach(file => {
if (file.endsWith('.json')) {
fs.unlinkSync(path.join(profilesDir, file));
}
});
// Create new profiles
newProfiles.forEach(profile => {
const newProfilePath = path.join(profilesDir, `${profile.name}.json`);
const profileData = JSON.parse(fs.readFileSync(ethanTemplatePath, 'utf8'));
profileData.name = profile.name;
profileData.personality = profile.personality;
fs.writeFileSync(newProfilePath, JSON.stringify(profileData, null, 4));
});
res.json({ message: "Profiles saved successfully." });
});
const shutdown = () => {
logToFile('Shutting down gracefully...');
if (agentProcessStarted) {
agentProcesses.forEach(agentProcess => {
agentProcess.agentProcess.kill('SIGTERM');
});
agentProcesses = [];
agentProcessStarted = false;
}
server.close(() => {
logToFile('HTTP server closed');
process.exit(0);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
server.listen(port, '0.0.0.0', () => {
logToFile(`Server running at http://0.0.0.0:${port}`);
});
logToFile("Server started successfully.");
// // CPU and Memory Usage Tracking
// let maxCpu = 0;
// let maxMemory = 0;
// setInterval(async () => {
// try {
// const pids = [process.pid, ...agentProcesses.map(ap => ap.agentProcess.pid)];
// const stats = await Promise.all(pids.map(pid => pidusage(pid)));
// const totalCpu = stats.reduce((acc, stat) => acc + stat.cpu, 0);
// const totalMemory = stats.reduce((acc, stat) => acc + stat.memory, 0);
// if (totalCpu > maxCpu) maxCpu = totalCpu;
// if (totalMemory > maxMemory) maxMemory = totalMemory;
// } catch (err) {
// logToFile(`Error fetching usage stats: ${err.message}`);
// }
// }, 300);
// setInterval(() => {
// logToFile(`Max CPU: ${maxCpu.toFixed(2)}%, Max Memory: ${(maxMemory / 1024 / 1024).toFixed(2)} MB`);
// maxCpu = 0;
// maxMemory = 0;
// }, 5000);
}
export { startServer };