-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
737 lines (626 loc) · 24.8 KB
/
server.js
File metadata and controls
737 lines (626 loc) · 24.8 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// server.js
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const { GoogleGenAI } = require('@google/genai');
const { Groq } = require('groq-sdk');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const { CommandHandler, buildAgentPrompt } = require('./command');
// Load environment variables
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const PORT = process.env.PORT || 3000;
// --- Agent Configuration ---
const agentTypes = {
System: 'system',
Human: 'human',
Gemini: 'gemini',
Llama: 'llama',
Qwen: 'qwen',
GPT: 'gpt'
};
// --- Message Class ---
class ChatMessage {
constructor(sender, content, timestamp = Date.now()) {
this.sender = sender;
this.content = content;
this.timestamp = timestamp;
this.senderType = agentTypes[sender] || 'system';
}
toJSON() {
return {
sender: this.sender,
content: this.content,
timestamp: this.timestamp,
senderType: this.senderType
};
}
toString() {
return `[${this.sender}]: ${this.content}`;
}
}
// --- Base Agent Class ---
class ChatAgent {
constructor(name) {
if (this.constructor === ChatAgent) {
throw new Error("Abstract classes can't be instantiated.");
}
this.name = name;
this.history = [];
}
addToHistory(message) {
if (this.history.length > 50) {
this.history.shift();
}
this.history.push(message);
}
async generateResponse(context, mode = 'normal') {
throw new Error('Method "generateResponse()" must be implemented.');
}
}
// --- Gemini Agent (Updated API with Role Support) ---
class GeminiAgent extends ChatAgent {
constructor(name = 'Gemini') {
super(name);
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.error('GEMINI_API_KEY not found in environment variables.');
this.ai = null;
return;
}
try {
this.ai = new GoogleGenAI({ apiKey: apiKey });
console.log("Gemini Agent Initialized Successfully.");
} catch (error) {
console.error("Failed to initialize GoogleGenAI:", error);
this.ai = null;
}
}
async generateResponse(context, mode = 'normal') {
if (!this.ai) {
return "Sorry, I'm currently unable to connect to my core systems (Gemini).";
}
const { system, context: formattedContext } = buildAgentPrompt(this.name, context, mode);
const prompt = `Recent conversation:\n${formattedContext}\n\nYour turn to contribute:`;
try {
const response = await this.ai.models.generateContent({
model: "gemini-2.0-flash-exp",
contents: prompt,
config: {
systemInstruction: system,
temperature: 0.8,
maxOutputTokens: mode === 'consensus' ? 100 : 200,
}
});
const responseText = response.text?.trim();
if (!responseText || responseText === '') {
console.warn("Gemini returned an empty response.");
return "...";
}
return responseText;
} catch (error) {
console.error(`Gemini API error: ${error.message}`);
return `Having trouble connecting (Gemini Error).`;
}
}
}
// --- Llama Agent (via Groq with Role Support) ---
class LlamaAgent extends ChatAgent {
constructor(name = 'Llama') {
super(name);
const apiKey = process.env.GROQ_API_KEY;
if (!apiKey) {
console.error('GROQ_API_KEY not found.');
this.groq = null;
return;
}
try {
this.groq = new Groq({ apiKey: apiKey });
console.log("Llama Agent (via Groq) Initialized.");
} catch(error) {
console.error("Failed to initialize Groq client:", error);
this.groq = null;
}
}
async generateResponse(context, mode = 'normal') {
if (!this.groq) {
return "Sorry, I'm having trouble connecting (Groq).";
}
const { system, context: formattedContext } = buildAgentPrompt(this.name, context, mode);
try {
const chatCompletion = await this.groq.chat.completions.create({
messages: [
{ role: "system", content: system },
{ role: "user", content: `Recent conversation:\n${formattedContext}\n\nYour turn:` }
],
model: "meta-llama/llama-4-scout-17b-16e-instruct",
temperature: 1,
max_completion_tokens: mode === 'consensus' ? 100 : 150,
top_p: 1,
stream: false,
stop: null
});
const responseText = chatCompletion.choices[0]?.message?.content?.trim();
if (!responseText) {
console.warn("Llama returned an empty response.");
return "...";
}
return responseText;
} catch (error) {
console.error(`Llama API error: ${error.message}`);
return `Technical difficulties (Llama).`;
}
}
}
// --- Qwen Agent (via Groq with Role Support) ---
class QwenAgent extends ChatAgent {
constructor(name = 'Qwen') {
super(name);
const apiKey = process.env.GROQ_API_KEY;
if (!apiKey) {
this.groq = null;
return;
}
try {
this.groq = new Groq({ apiKey: apiKey });
console.log("Qwen Agent (via Groq) Initialized.");
} catch(error) {
console.error("Failed to initialize Groq client for Qwen:", error);
this.groq = null;
}
}
async generateResponse(context, mode = 'normal') {
if (!this.groq) {
return "Sorry, I'm having trouble connecting (Groq).";
}
const { system, context: formattedContext } = buildAgentPrompt(this.name, context, mode);
try {
const chatCompletion = await this.groq.chat.completions.create({
messages: [
{ role: "system", content: system },
{ role: "user", content: `Recent conversation:\n${formattedContext}\n\nYour turn:` }
],
model: "qwen/qwen3-32b",
temperature: 0.6,
max_completion_tokens: mode === 'consensus' ? 100 : 150,
top_p: 0.95,
stream: false,
reasoning_effort: "default",
stop: null
});
const responseText = chatCompletion.choices[0]?.message?.content?.trim();
if (!responseText) {
console.warn("Qwen returned an empty response.");
return "...";
}
return responseText;
} catch (error) {
console.error(`Qwen API error: ${error.message}`);
return `Experiencing network turbulence (Qwen).`;
}
}
}
// --- GPT Agent (via Groq with Tools) ---
class GPTAgent extends ChatAgent {
constructor(name = 'GPT') {
super(name);
const apiKey = process.env.GROQ_API_KEY;
if (!apiKey) {
this.groq = null;
return;
}
try {
this.groq = new Groq({ apiKey: apiKey });
console.log("GPT Agent (via Groq) Initialized with Tools.");
} catch(error) {
console.error("Failed to initialize Groq client for GPT:", error);
this.groq = null;
}
}
async generateResponse(context, mode = 'normal') {
if (!this.groq) {
return "Sorry, I'm having trouble connecting (Groq).";
}
const { system, context: formattedContext } = buildAgentPrompt(this.name, context, mode);
try {
const chatCompletion = await this.groq.chat.completions.create({
messages: [
{ role: "system", content: system },
{ role: "user", content: `Recent conversation:\n${formattedContext}\n\nYour turn:` }
],
model: "openai/gpt-oss-20b",
temperature: 1,
max_completion_tokens: mode === 'consensus' ? 100 : 150,
top_p: 1,
stream: false,
reasoning_effort: "medium",
stop: null,
tools: [
{ type: "browser_search" },
{ type: "code_interpreter" }
]
});
// Handle tool calls if present
const choice = chatCompletion.choices[0];
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
const toolCall = choice.message.tool_calls[0];
const toolInfo = `[Used ${toolCall.function.name}] `;
const responseText = choice.message.content?.trim() || "Processed request using tools.";
return toolInfo + responseText;
}
const responseText = choice.message?.content?.trim();
if (!responseText) {
console.warn("GPT returned an empty response.");
return "...";
}
return responseText;
} catch (error) {
console.error(`GPT API error: ${error.message}`);
return `Connectivity issues (GPT).`;
}
}
}
// --- Multi-Agent Chat System (Server-Side) ---
class MultiAgentChatServer {
constructor(ioInstance) {
this.io = ioInstance;
this.agents = {};
this.messages = [];
this.conversationActive = false;
this.autoConverseInterval = [8000, 20000];
this.typingAgents = new Set();
this.responseTimeout = 25000;
this.autoConversationTimer = null;
this.isGenerating = false;
this.maxHistory = 150;
this.saveInterval = 5 * 60 * 1000;
this.saveTimer = null;
// Special modes
this.roundtableMode = null;
this.consensusMode = null;
this.isPaused = false; // Track pause state
// Initialize command handler
this.commandHandler = new CommandHandler(this);
}
createMessage(sender, content) {
return new ChatMessage(sender, content);
}
addSystemMessage(content) {
this.addMessage(new ChatMessage('System', content));
}
addAgent(agent) {
if (!agent || !agent.name) {
console.error("Attempted to add an invalid agent.");
return;
}
if (agent instanceof GeminiAgent && !agent.ai) {
console.warn(`Gemini agent disabled. Not adding.`);
return;
}
if ((agent instanceof LlamaAgent || agent instanceof QwenAgent || agent instanceof GPTAgent) && !agent.groq) {
console.warn(`${agent.name} agent disabled. Not adding.`);
return;
}
this.agents[agent.name] = agent;
console.log(`Agent ${agent.name} added to the chat.`);
}
handleConnection(socket) {
console.log(`Client connected: ${socket.id}`);
const recentHistory = this.messages.slice(-50).map(msg => msg.toJSON());
socket.emit('initialHistory', recentHistory);
socket.emit('typingStatus', Array.from(this.typingAgents));
socket.on('humanMessage', async (content) => {
if (typeof content === 'string' && content.trim().length > 0 && content.length < 2000) {
await this.handleHumanInput(content.trim(), socket.id);
}
});
socket.on('disconnect', (reason) => {
console.log(`Client disconnected: ${socket.id}, Reason: ${reason}`);
});
socket.on('error', (error) => {
console.error(`Socket error from ${socket.id}:`, error);
});
}
addMessage(message) {
if (!(message instanceof ChatMessage)) {
console.error("Attempted to add non-ChatMessage object:", message);
return;
}
this.messages.push(message);
if (this.messages.length > this.maxHistory) {
this.messages.shift();
}
Object.values(this.agents).forEach(agent => {
if (typeof agent.addToHistory === 'function') {
agent.addToHistory(message);
}
});
this.io.emit('newMessage', message.toJSON());
this.resetSaveTimer();
}
async handleHumanInput(content, clientId) {
console.log(`Human input from ${clientId}: ${content}`);
// Check if it's a command
if (this.commandHandler.isCommand(content)) {
const result = await this.commandHandler.execute(content, clientId);
if (result.message) {
this.addSystemMessage(result.message);
}
// If command handled everything, return
if (result.handled) {
return;
}
// If command failed, continue as normal message
if (!result.success) {
return;
}
}
// Normal message handling
this.addMessage(new ChatMessage('Human', content));
// Start conversation only when user sends their first message
if (!this.conversationActive) {
this.conversationActive = true;
console.log("Conversation activated by human input.");
this.addSystemMessage("🚀 Conversation started! The AI agents are now online.");
}
clearTimeout(this.autoConversationTimer);
this.autoConversationTimer = null;
const aiAgents = Object.keys(this.agents);
if (aiAgents.length > 0) {
const nextSpeaker = aiAgents[Math.floor(Math.random() * aiAgents.length)];
console.log(`Triggering response from ${nextSpeaker} after human input.`);
this.triggerAIResponse(nextSpeaker).catch(err => {
console.error(`Error triggering AI response: ${err}`);
this.scheduleAutoConversation(500);
});
}
}
setTyping(agentName, isTyping) {
const changed = isTyping ? this.typingAgents.add(agentName) : this.typingAgents.delete(agentName);
if (changed || isTyping) {
this.io.emit('typingStatus', Array.from(this.typingAgents));
}
}
async triggerAIResponse(agentName, mode = 'normal') {
if (this.isGenerating) {
return false;
}
const agent = this.agents[agentName];
if (!agent || typeof agent.generateResponse !== 'function') {
console.error(`Agent ${agentName} not found.`);
this.scheduleAutoConversation(1000);
return false;
}
const lastMessage = this.messages.length > 0 ? this.messages[this.messages.length - 1] : null;
const aiAgentNames = Object.keys(this.agents);
if (aiAgentNames.length > 1 && lastMessage && lastMessage.sender === agentName && mode === 'normal') {
this.scheduleAutoConversation(100);
return false;
}
this.isGenerating = true;
this.setTyping(agentName, true);
console.log(`Agent ${agentName} starting generation (${mode} mode)...`);
let response = null;
let success = false;
try {
const responsePromise = agent.generateResponse(this.messages, mode);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error(`Timeout after ${this.responseTimeout / 1000}s`)), this.responseTimeout)
);
response = await Promise.race([responsePromise, timeoutPromise]);
if (response && typeof response === 'string' && response.trim().length > 0) {
this.addMessage(new ChatMessage(agentName, response.trim()));
console.log(`Agent ${agentName} responded successfully.`);
success = true;
} else {
console.warn(`Agent ${agentName} returned empty response.`);
success = false;
}
} catch (error) {
console.error(`Error for ${agentName}: ${error.message}`);
this.addSystemMessage(`Agent ${agentName} encountered an error.`);
success = false;
} finally {
this.setTyping(agentName, false);
this.isGenerating = false;
// Handle special modes
if (this.roundtableMode && this.roundtableMode.active) {
setTimeout(() => this.triggerRoundtableResponse(), 2000);
} else if (this.consensusMode && this.consensusMode.active) {
if (success && response) {
this.consensusMode.responses.push({ agent: agentName, response });
}
setTimeout(() => this.triggerConsensusResponse(), 1500);
} else if (this.conversationActive && mode === 'normal') {
this.scheduleAutoConversation();
}
}
return success;
}
// Roundtable mode handler
async triggerRoundtableResponse() {
if (!this.roundtableMode || !this.roundtableMode.active) return;
const { agents, currentIndex } = this.roundtableMode;
if (currentIndex >= agents.length) {
// Roundtable complete
this.addSystemMessage('🔵 Roundtable complete. Resuming normal conversation.');
this.roundtableMode = null;
this.scheduleAutoConversation();
return;
}
const nextAgent = agents[currentIndex];
this.roundtableMode.currentIndex++;
await this.triggerAIResponse(nextAgent, 'roundtable');
}
// Consensus mode handler
async triggerConsensusResponse() {
if (!this.consensusMode || !this.consensusMode.active) return;
const { agents, currentIndex, responses } = this.consensusMode;
if (currentIndex >= agents.length) {
// Generate consensus summary
this.addSystemMessage('📊 Consensus Summary:');
responses.forEach(({ agent, response }) => {
this.addSystemMessage(` • ${agent}: ${response}`);
});
this.addSystemMessage('Consensus gathering complete. Resuming normal conversation.');
this.consensusMode = null;
this.scheduleAutoConversation();
return;
}
const nextAgent = agents[currentIndex];
this.consensusMode.currentIndex++;
await this.triggerAIResponse(nextAgent, 'consensus');
}
// Pause auto-conversation - agents still respond to direct messages
pauseConversation() {
this.isPaused = true;
clearTimeout(this.autoConversationTimer);
this.addSystemMessage("⏸️ Auto-conversation paused. Agents will only respond to direct messages.");
}
// Resume full conversation from paused state
resumeConversation() {
this.isPaused = false;
this.conversationActive = true;
this.addSystemMessage("▶️ Auto-conversation resumed. Agents will now participate actively.");
this.scheduleAutoConversation(2000);
}
// Stop all agent activity completely
stopConversation() {
this.isPaused = true;
this.conversationActive = false;
clearTimeout(this.autoConversationTimer);
this.typingAgents.clear();
this.addSystemMessage("⏹️ Conversation stopped. All agent activity halted.");
}
scheduleAutoConversation(forcedDelay) {
clearTimeout(this.autoConversationTimer);
this.autoConversationTimer = null;
const aiAgentNames = Object.keys(this.agents);
if (!this.conversationActive || aiAgentNames.length === 0 || this.isPaused) {
return;
}
const waitTime = typeof forcedDelay === 'number' ? forcedDelay : Math.floor(
Math.random() * (this.autoConverseInterval[1] - this.autoConverseInterval[0]) +
this.autoConverseInterval[0]
);
this.autoConversationTimer = setTimeout(async () => {
if (this.conversationActive && !this.isGenerating && aiAgentNames.length > 0) {
const lastSpeaker = this.messages.length > 0 ? this.messages[this.messages.length - 1].sender : null;
let availableAgents = aiAgentNames.filter(name => name !== lastSpeaker);
if (availableAgents.length === 0) {
availableAgents = aiAgentNames;
}
const nextSpeaker = availableAgents[Math.floor(Math.random() * availableAgents.length)];
await this.triggerAIResponse(nextSpeaker);
} else if (this.conversationActive && this.isGenerating) {
this.scheduleAutoConversation(2000);
}
}, waitTime);
}
resetSaveTimer() {
clearTimeout(this.saveTimer);
this.saveTimer = setTimeout(() => {
this.saveConversation();
}, this.saveInterval);
}
start() {
console.log("Initializing Multi-Agent Chat Server...");
const publicPath = path.join(__dirname, 'public');
if (!fs.existsSync(publicPath)) {
console.error(`Error: 'public' directory not found at ${publicPath}.`);
process.exit(1);
}
app.use(express.static(publicPath));
console.log(`Serving static files from: ${publicPath}`);
// Add all agents
this.addAgent(new GeminiAgent());
this.addAgent(new LlamaAgent());
this.addAgent(new QwenAgent());
this.addAgent(new GPTAgent());
if (Object.keys(this.agents).length === 0) {
console.warn("Warning: No AI agents initialized. Check API keys.");
}
this.io.on('connection', (socket) => {
this.handleConnection(socket);
});
console.log("WebSocket connection handler established.");
setTimeout(() => {
this.addSystemMessage("Nexus Chat Initialized. AI Brainstorming Collective online. Type /help for commands.");
}, 500);
server.listen(PORT, () => {
console.log(`Server is listening on http://localhost:${PORT}`);
// Don't start auto-conversation on startup - wait for user to initiate
this.conversationActive = false;
if (Object.keys(this.agents).length > 0) {
console.log("System ready. Waiting for user to start the conversation...");
} else {
console.warn("Warning: No AI agents initialized. Check API keys.");
}
this.resetSaveTimer();
});
server.on('error', (error) => {
console.error('Server startup error:', error);
process.exit(1);
});
}
stop() {
console.log("Stopping chat server...");
this.conversationActive = false;
clearTimeout(this.autoConversationTimer);
clearTimeout(this.saveTimer);
this.io.emit('newMessage', new ChatMessage('System', 'Chat server shutting down.').toJSON());
this.io.emit('typingStatus', []);
this.io.close((err) => {
if (err) console.error("Error closing Socket.IO:", err);
this.saveConversation();
server.close((err) => {
if (err) {
console.error('Error closing server:', err);
process.exit(1);
} else {
console.log('Server closed.');
process.exit(0);
}
});
setTimeout(() => {
console.error('Forcing shutdown.');
process.exit(1);
}, 5000);
});
}
saveConversation(filename = "conversation_log.json") {
console.log(`Saving conversation to ${filename}...`);
try {
const conversationData = JSON.stringify(
this.messages.map(msg => msg.toJSON()),
null,
2
);
fs.writeFileSync(filename, conversationData, 'utf8');
console.log(`Conversation saved. (${this.messages.length} messages)`);
} catch (error) {
console.error(`Failed to save: ${error}`);
}
}
}
// --- Initialize and Start ---
const chatServer = new MultiAgentChatServer(io);
chatServer.start();
// --- Graceful Shutdown ---
process.on('SIGINT', () => {
console.log("\nCaught SIGINT. Shutting down...");
chatServer.stop();
});
process.on('SIGTERM', () => {
console.log("\nCaught SIGTERM. Shutting down...");
chatServer.stop();
});
process.on('uncaughtException', (error) => {
console.error('UNCAUGHT EXCEPTION:', error);
chatServer.saveConversation('conversation_error_dump.json');
process.exit(1);
});