session start works
This commit is contained in:
254
backend/src/ws/index.ts
Normal file
254
backend/src/ws/index.ts
Normal file
@@ -0,0 +1,254 @@
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import { LLMService } from '../services/LLMService';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
// Types from the frontend
|
||||
interface SemanticDesire {
|
||||
title: string;
|
||||
rawInputs: string[];
|
||||
}
|
||||
|
||||
interface Decision {
|
||||
goTos: SemanticDesire[];
|
||||
alsoGoods: SemanticDesire[];
|
||||
considerables: SemanticDesire[];
|
||||
noGoes: SemanticDesire[];
|
||||
}
|
||||
|
||||
// Define the SessionState enum
|
||||
export enum SessionState {
|
||||
SETUP = 'SETUP',
|
||||
GATHERING = 'GATHERING',
|
||||
HARMONIZING = 'HARMONIZING',
|
||||
FINAL = 'FINAL',
|
||||
}
|
||||
|
||||
// A map to hold session data, including clients and the latest state
|
||||
interface SessionData {
|
||||
state: SessionState; // Current phase of the session
|
||||
topic: string | null; // The topic of the session
|
||||
expectedResponses: number; // The number set by the first user in State A.
|
||||
submittedCount: number; // The current count of submitted responses.
|
||||
responses: Map<string, any>; // Stores the submitted desire objects. Map<ClientID, ResponseData>
|
||||
clients: Map<string, WebSocket>; // Maps the persistent Client ID to their active WebSocket connection object.
|
||||
finalResult: any | null; // The result returned by the LLM.
|
||||
}
|
||||
export const sessions = new Map<string, SessionData>();
|
||||
|
||||
// Initialize LLM Service (API key from environment)
|
||||
const llmService = new LLMService(process.env.GEMINI_API_KEY || '');
|
||||
|
||||
export const broadcastToSession = (sessionId: string, message: any, excludeClientId: string | null = null) => {
|
||||
const sessionData = sessions.get(sessionId);
|
||||
if (sessionData) {
|
||||
sessionData.clients.forEach((client, clientId) => {
|
||||
if (clientId !== excludeClientId && client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify(message));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const createWebSocketServer = (server: any) => {
|
||||
const wss = new WebSocketServer({ server });
|
||||
|
||||
wss.on('connection', (ws, req) => {
|
||||
const url = new URL(req.url || '', `http://${req.headers.host}`);
|
||||
const sessionId = url.pathname.split('/').pop();
|
||||
|
||||
if (!sessionId) {
|
||||
ws.close(1008, 'Invalid session ID');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sessions.has(sessionId)) {
|
||||
sessions.set(sessionId, {
|
||||
state: SessionState.SETUP,
|
||||
topic: null,
|
||||
expectedResponses: 0,
|
||||
submittedCount: 0,
|
||||
responses: new Map<string, any>(),
|
||||
clients: new Map<string, WebSocket>(),
|
||||
finalResult: null,
|
||||
});
|
||||
}
|
||||
const sessionData = sessions.get(sessionId)!;
|
||||
|
||||
console.log(`Client connecting to session: ${sessionId}`);
|
||||
|
||||
ws.on('message', async (message) => {
|
||||
const parsedMessage = JSON.parse(message.toString());
|
||||
const { type, clientId, payload } = parsedMessage;
|
||||
|
||||
if (!clientId) {
|
||||
console.error(`Received message without clientId in session ${sessionId}. Type: ${type}`);
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'clientId is required' } }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sessionData.clients.has(clientId)) {
|
||||
sessionData.clients.set(clientId, ws);
|
||||
console.log(`Client ${clientId} registered for session: ${sessionId}. Total clients: ${sessionData.clients.size}`);
|
||||
ws.send(JSON.stringify({ type: 'STATE_UPDATE', payload: { session: sessionData } }));
|
||||
}
|
||||
|
||||
console.log(`Received message from ${clientId} in session ${sessionId}:`, type);
|
||||
|
||||
switch (type) {
|
||||
case 'REGISTER_CLIENT':
|
||||
break;
|
||||
|
||||
case 'SETUP_SESSION':
|
||||
if (sessionData.state === SessionState.SETUP) {
|
||||
const { expectedResponses, topic } = payload;
|
||||
if (typeof expectedResponses !== 'number' || expectedResponses <= 0) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Invalid expectedResponses' } }));
|
||||
return;
|
||||
}
|
||||
sessionData.expectedResponses = expectedResponses;
|
||||
sessionData.topic = topic || 'Untitled Session';
|
||||
sessionData.state = SessionState.GATHERING;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: { session: sessionData } });
|
||||
console.log(`Session ${sessionId} moved to GATHERING with topic "${sessionData.topic}" and ${expectedResponses} expected responses.`);
|
||||
} else {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in SETUP state. Current state: ${sessionData.state}` } }));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SUBMIT_RESPONSE':
|
||||
if (sessionData.state === SessionState.GATHERING) {
|
||||
if (sessionData.responses.has(clientId)) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'You have already submitted a response for this session.' } }));
|
||||
return;
|
||||
}
|
||||
sessionData.responses.set(clientId, payload.response);
|
||||
sessionData.submittedCount++;
|
||||
console.log(`Client ${clientId} submitted response. Submitted count: ${sessionData.submittedCount}/${sessionData.expectedResponses}`);
|
||||
|
||||
if (sessionData.submittedCount === sessionData.expectedResponses) {
|
||||
sessionData.state = SessionState.HARMONIZING;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: { session: sessionData } });
|
||||
console.log(`Session ${sessionId} moved to HARMONIZING. Triggering LLM analysis.`);
|
||||
|
||||
try {
|
||||
const allDesires = Array.from(sessionData.responses.values());
|
||||
const canonicalMap = await llmService.analyzeDesires(allDesires);
|
||||
|
||||
const semanticDesiresMap = new Map<string, SemanticDesire>();
|
||||
|
||||
for (const originalDesire in canonicalMap) {
|
||||
const canonicalName = canonicalMap[originalDesire];
|
||||
if (!semanticDesiresMap.has(canonicalName)) {
|
||||
semanticDesiresMap.set(canonicalName, { title: canonicalName, rawInputs: [] });
|
||||
}
|
||||
semanticDesiresMap.get(canonicalName)?.rawInputs.push(originalDesire);
|
||||
}
|
||||
|
||||
const decision: Decision = {
|
||||
goTos: [],
|
||||
alsoGoods: [],
|
||||
considerables: [],
|
||||
noGoes: [],
|
||||
};
|
||||
|
||||
const participantIds = Array.from(sessionData.responses.keys());
|
||||
|
||||
semanticDesiresMap.forEach(semanticDesire => {
|
||||
let isNoGo = false;
|
||||
let allWant = true;
|
||||
let atLeastOneWant = false;
|
||||
let allAcceptOrWant = true;
|
||||
|
||||
for (const pId of participantIds) {
|
||||
const participantDesireSet = sessionData.responses.get(pId);
|
||||
if (!participantDesireSet) continue;
|
||||
|
||||
const participantWants = new Set(participantDesireSet.wants.map((d: string) => canonicalMap[d] || d));
|
||||
const participantAccepts = new Set(participantDesireSet.accepts.map((d: string) => canonicalMap[d] || d));
|
||||
const participantNoGoes = new Set(participantDesireSet.noGoes.map((d: string) => canonicalMap[d] || d));
|
||||
|
||||
const canonicalTitle = semanticDesire.title;
|
||||
|
||||
if (participantNoGoes.has(canonicalTitle)) {
|
||||
isNoGo = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!participantWants.has(canonicalTitle)) {
|
||||
allWant = false;
|
||||
}
|
||||
|
||||
if (participantWants.has(canonicalTitle)) {
|
||||
atLeastOneWant = true;
|
||||
}
|
||||
|
||||
if (!participantWants.has(canonicalTitle) && !participantAccepts.has(canonicalTitle)) {
|
||||
allAcceptOrWant = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isNoGo) {
|
||||
decision.noGoes.push(semanticDesire);
|
||||
} else if (allWant) {
|
||||
decision.goTos.push(semanticDesire);
|
||||
} else if (atLeastOneWant && allAcceptOrWant) {
|
||||
decision.alsoGoods.push(semanticDesire);
|
||||
} else if (atLeastOneWant || !allAcceptOrWant) {
|
||||
decision.considerables.push(semanticDesire);
|
||||
}
|
||||
});
|
||||
|
||||
sessionData.finalResult = decision;
|
||||
sessionData.state = SessionState.FINAL;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: { session: sessionData } });
|
||||
console.log(`Analysis complete for session ${sessionId}. Result:`, decision);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error during analysis for session ${sessionId}:`, error);
|
||||
sessionData.state = SessionState.GATHERING;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: { session: sessionData } });
|
||||
}
|
||||
} else {
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: { session: sessionData } });
|
||||
}
|
||||
} else {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in GATHERING state. Current state: ${sessionData.state}` } }));
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
console.warn(`Unknown message type: ${type} from client ${clientId} in session ${sessionId}`);
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Unknown message type: ${type}` } }));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
let disconnectedClientId: string | null = null;
|
||||
for (const [clientId, clientWs] of sessionData.clients.entries()) {
|
||||
if (clientWs === ws) {
|
||||
disconnectedClientId = clientId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (disconnectedClientId) {
|
||||
sessionData.clients.delete(disconnectedClientId);
|
||||
console.log(`Client ${disconnectedClientId} disconnected from session: ${sessionId}. Remaining clients: ${sessionData.clients.size}`);
|
||||
} else {
|
||||
console.log(`An unregistered client disconnected from session: ${sessionId}.`);
|
||||
}
|
||||
|
||||
if (sessionData.clients.size === 0) {
|
||||
sessions.delete(sessionId);
|
||||
console.log(`Session ${sessionId} closed and state cleared.`);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (error) => {
|
||||
console.error(`WebSocket error in session ${sessionId}:`, error);
|
||||
});
|
||||
});
|
||||
|
||||
return wss;
|
||||
};
|
||||
Reference in New Issue
Block a user