1. Session persists in GATHERING state. 2. Added inactive sessions purging, but it does not work.
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +1,8 @@
|
||||
/frontend/node_modules
|
||||
/frontend/build/
|
||||
/frontend/dist/
|
||||
/backend/node_modules
|
||||
/backend/build/
|
||||
/backend/dist/
|
||||
/.context
|
||||
.env
|
||||
@@ -1,3 +1,4 @@
|
||||
GEMINI_API_KEY="AIzaSyDke9H2NhiG6rBwxT0qrdYgnNoNZm_0j58"
|
||||
ENCRYPTION_KEY=a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2
|
||||
CORS_ORIGIN=http://localhost:3000
|
||||
SESSION_TIMEOUT_MINUTES=1
|
||||
1
backend/dist/index.js
vendored
1
backend/dist/index.js
vendored
@@ -49,6 +49,7 @@ app.post('/sessions', (req, res) => {
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
lastActivity: Date.now(),
|
||||
});
|
||||
console.log(`New session created: ${sessionId}`);
|
||||
res.status(201).json({ sessionId });
|
||||
|
||||
93
backend/dist/routes/sessions.js
vendored
93
backend/dist/routes/sessions.js
vendored
@@ -13,30 +13,89 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = __importDefault(require("express"));
|
||||
const ws_1 = require("../ws"); // Import sessions, SessionState, broadcastToSession, and handleWebSocketMessage from ws/index.ts
|
||||
const uuid_1 = require("uuid");
|
||||
const ws_1 = require("../ws"); // Import sessions, SessionState, broadcastToSession from ws/index.ts
|
||||
const LLMService_1 = require("../services/LLMService");
|
||||
const EncryptionService_1 = require("../services/EncryptionService");
|
||||
const router = express_1.default.Router();
|
||||
// Initialize LLM Service (API key from environment)
|
||||
const llmService = new LLMService_1.LLMService(process.env.GEMINI_API_KEY || '');
|
||||
// Initialize Encryption Service
|
||||
const encryptionService = new EncryptionService_1.EncryptionService(process.env.ENCRYPTION_KEY || '');
|
||||
router.post('/sessions', (req, res) => {
|
||||
const sessionId = (0, uuid_1.v4)();
|
||||
ws_1.sessions.set(sessionId, {
|
||||
state: ws_1.SessionState.SETUP,
|
||||
topic: null,
|
||||
description: null,
|
||||
expectedResponses: 0,
|
||||
submittedCount: 0,
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
lastActivity: Date.now(),
|
||||
});
|
||||
res.status(201).json({ sessionId });
|
||||
});
|
||||
router.post('/sessions/:sessionId/responses', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const { sessionId } = req.params;
|
||||
const { userId, wants, accepts, afraidToAsk } = req.body;
|
||||
const { clientId, wants, accepts, noGoes, afraidToAsk } = req.body; // Use clientId instead of userId
|
||||
if (!ws_1.sessions.has(sessionId)) {
|
||||
return res.status(404).json({ message: 'Session not found.' });
|
||||
}
|
||||
// Create a dummy WebSocket object for the handleWebSocketMessage function.
|
||||
// This is a workaround to reuse the WebSocket message handling logic.
|
||||
// In a real application, consider a more robust event-driven architecture.
|
||||
const dummyWs = {
|
||||
send: (message) => console.log('Dummy WS send:', message),
|
||||
readyState: 1, // OPEN
|
||||
};
|
||||
const message = {
|
||||
type: 'SUBMIT_RESPONSE',
|
||||
clientId: userId,
|
||||
payload: {
|
||||
response: { wants, accepts, afraidToAsk },
|
||||
},
|
||||
};
|
||||
const sessionData = ws_1.sessions.get(sessionId);
|
||||
if (sessionData.state !== ws_1.SessionState.GATHERING) {
|
||||
return res.status(400).json({ message: `Session is not in GATHERING state. Current state: ${sessionData.state}` });
|
||||
}
|
||||
if (sessionData.responses.has(clientId)) {
|
||||
return res.status(400).json({ message: 'You have already submitted a response for this session.' });
|
||||
}
|
||||
if ([...wants, ...accepts, ...noGoes].some((desire) => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
return res.status(400).json({ message: 'One of your desires or afraidToAsk exceeds the 500 character limit.' });
|
||||
}
|
||||
try {
|
||||
yield (0, ws_1.handleWebSocketMessage)(dummyWs, sessionId, message);
|
||||
const hasContradictionsGist = yield llmService.checkForInnerContradictions({ wants, accepts, noGoes, afraidToAsk });
|
||||
if (hasContradictionsGist) {
|
||||
return res.status(400).json({ message: `Your submission contains inner contradictions: ${hasContradictionsGist} Please resolve them and submit again.` });
|
||||
}
|
||||
const encryptedWants = wants.map((d) => encryptionService.encrypt(d));
|
||||
const encryptedAccepts = accepts.map((d) => encryptionService.encrypt(d));
|
||||
const encryptedNoGoes = noGoes.map((d) => encryptionService.encrypt(d));
|
||||
const encryptedAfraidToAsk = encryptionService.encrypt(afraidToAsk);
|
||||
sessionData.responses.set(clientId, { wants: encryptedWants, accepts: encryptedAccepts, noGoes: encryptedNoGoes, afraidToAsk: encryptedAfraidToAsk });
|
||||
sessionData.submittedCount++;
|
||||
console.log(`Client ${clientId} submitted response via HTTP. Submitted count: ${sessionData.submittedCount}/${sessionData.expectedResponses}`);
|
||||
if (sessionData.submittedCount === sessionData.expectedResponses) {
|
||||
sessionData.state = ws_1.SessionState.HARMONIZING;
|
||||
(0, ws_1.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
console.log(`Session ${sessionId} moved to HARMONIZING. Triggering LLM analysis.`);
|
||||
// Perform LLM analysis asynchronously
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
try {
|
||||
const allDecryptedDesires = Array.from(sessionData.responses.values()).map(encryptedResponse => {
|
||||
const decryptedWants = encryptedResponse.wants.map((d) => encryptionService.decrypt(d));
|
||||
const decryptedAccepts = encryptedResponse.accepts.map((d) => encryptionService.decrypt(d));
|
||||
const decryptedNoGoes = encryptedResponse.noGoes.map((d) => encryptionService.decrypt(d));
|
||||
const decryptedAfraidToAsk = encryptionService.decrypt(encryptedResponse.afraidToAsk);
|
||||
return { wants: decryptedWants, accepts: decryptedAccepts, noGoes: decryptedNoGoes, afraidToAsk: decryptedAfraidToAsk };
|
||||
});
|
||||
const decision = yield llmService.analyzeDesires(allDecryptedDesires);
|
||||
sessionData.finalResult = decision;
|
||||
sessionData.state = ws_1.SessionState.FINAL;
|
||||
(0, ws_1.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
console.log(`Analysis complete for session ${sessionId}. Result:`, decision);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error during analysis for session ${sessionId}:`, error.message);
|
||||
sessionData.state = ws_1.SessionState.ERROR;
|
||||
(0, ws_1.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
}
|
||||
}))();
|
||||
}
|
||||
else {
|
||||
// Only broadcast the latest count if the session is not yet harmonizing
|
||||
(0, ws_1.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
}
|
||||
res.status(202).json({ message: 'Response submission acknowledged and processed.' });
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
170
backend/dist/ws/index.js
vendored
170
backend/dist/ws/index.js
vendored
@@ -69,8 +69,23 @@ const broadcastToSession = (sessionId, message, excludeClientId = null) => {
|
||||
}
|
||||
};
|
||||
exports.broadcastToSession = broadcastToSession;
|
||||
const SESSION_TIMEOUT_MINUTES = parseInt(process.env.SESSION_TIMEOUT_MINUTES || '1440', 10);
|
||||
const SESSION_TIMEOUT_MS = SESSION_TIMEOUT_MINUTES * 60 * 1000; // Convert minutes to milliseconds
|
||||
// Function to clean up inactive sessions
|
||||
const cleanupInactiveSessions = () => {
|
||||
const now = Date.now();
|
||||
for (const [sessionId, sessionData] of exports.sessions.entries()) {
|
||||
if (sessionData.clients.size === 0 && (now - sessionData.lastActivity > SESSION_TIMEOUT_MS)) {
|
||||
exports.sessions.delete(sessionId);
|
||||
logEvent('session_purged_inactive', sessionId);
|
||||
console.log(`Inactive session ${sessionId} purged.`);
|
||||
}
|
||||
}
|
||||
};
|
||||
const createWebSocketServer = (server) => {
|
||||
const wss = new ws_1.WebSocketServer({ server });
|
||||
// Schedule periodic cleanup of inactive sessions
|
||||
setInterval(cleanupInactiveSessions, 60 * 60 * 1000); // Run every hour
|
||||
wss.on('connection', (ws, req) => {
|
||||
const url = new URL(req.url || '', `http://${req.headers.host}`);
|
||||
const sessionId = url.pathname.split('/').pop();
|
||||
@@ -78,20 +93,7 @@ const createWebSocketServer = (server) => {
|
||||
ws.close(1008, 'Invalid session ID');
|
||||
return;
|
||||
}
|
||||
if (!exports.sessions.has(sessionId)) {
|
||||
exports.sessions.set(sessionId, {
|
||||
state: SessionState.SETUP,
|
||||
topic: null,
|
||||
description: null,
|
||||
expectedResponses: 0,
|
||||
submittedCount: 0,
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
});
|
||||
}
|
||||
const sessionData = exports.sessions.get(sessionId);
|
||||
console.log(`Client connecting to session: ${sessionId}`);
|
||||
let sessionData = null;
|
||||
// Set up a ping interval to keep the connection alive
|
||||
const pingInterval = setInterval(() => {
|
||||
if (ws.readyState === ws_1.WebSocket.OPEN) {
|
||||
@@ -100,41 +102,46 @@ const createWebSocketServer = (server) => {
|
||||
}, 30000); // Send ping every 30 seconds
|
||||
ws.on('message', (message) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
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;
|
||||
const updatedSessionData = yield (0, exports.handleWebSocketMessage)(ws, sessionId, parsedMessage);
|
||||
if (updatedSessionData) {
|
||||
sessionData = updatedSessionData;
|
||||
}
|
||||
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: getSerializableSession(sessionData, clientId) } }));
|
||||
}
|
||||
console.log(`Received message from ${clientId} in session ${sessionId}:`, type);
|
||||
yield (0, exports.handleWebSocketMessage)(ws, sessionId, parsedMessage);
|
||||
}));
|
||||
ws.on('close', () => {
|
||||
clearInterval(pingInterval); // Clear the interval when the connection closes
|
||||
let disconnectedClientId = null;
|
||||
for (const [clientId, clientWs] of sessionData.clients.entries()) {
|
||||
const currentSessionData = exports.sessions.get(sessionId); // Retrieve the latest sessionData
|
||||
if (currentSessionData) { // Check if sessionData is not null
|
||||
for (const [clientId, clientWs] of currentSessionData.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}`);
|
||||
currentSessionData.clients.delete(disconnectedClientId);
|
||||
console.log(`Client ${disconnectedClientId} disconnected from session: ${sessionId}. Remaining clients: ${currentSessionData.clients.size}`);
|
||||
}
|
||||
else {
|
||||
console.log(`An unregistered client disconnected from session: ${sessionId}.`);
|
||||
}
|
||||
if (sessionData.clients.size === 0) {
|
||||
if (currentSessionData.clients.size === 0) {
|
||||
// Only purge session if it's in SETUP, FINAL, or ERROR state
|
||||
if (currentSessionData.state === SessionState.SETUP ||
|
||||
currentSessionData.state === SessionState.FINAL ||
|
||||
currentSessionData.state === SessionState.ERROR) {
|
||||
exports.sessions.delete(sessionId);
|
||||
logEvent('session_purged', sessionId);
|
||||
console.log(`Session ${sessionId} closed and state cleared.`);
|
||||
}
|
||||
else {
|
||||
console.log(`Session ${sessionId} is in ${currentSessionData.state} state. Not purging despite no active clients.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log(`Client disconnected from session: ${sessionId}. Session data was null.`);
|
||||
}
|
||||
});
|
||||
ws.on('error', (error) => {
|
||||
console.error(`WebSocket error in session ${sessionId}:`, error);
|
||||
@@ -148,31 +155,67 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
|
||||
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;
|
||||
return exports.sessions.get(sessionId) || null; // Return current session state if available
|
||||
}
|
||||
const sessionData = exports.sessions.get(sessionId);
|
||||
let sessionData = exports.sessions.get(sessionId);
|
||||
// Update lastActivity timestamp on any message
|
||||
if (sessionData) {
|
||||
sessionData.lastActivity = Date.now();
|
||||
console.log(`Session ${sessionId}: lastActivity updated to ${sessionData.lastActivity}`);
|
||||
}
|
||||
// If sessionData is null here, it means a JOIN_SESSION message hasn't been processed yet for a new session.
|
||||
// The JOIN_SESSION case will handle its creation.
|
||||
if (!sessionData && type !== 'JOIN_SESSION') {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session not found and message is not JOIN_SESSION' } }));
|
||||
return null; // Session not found, return null
|
||||
}
|
||||
switch (type) {
|
||||
case 'JOIN_SESSION':
|
||||
if (!sessionData) {
|
||||
// Create a new session if it doesn't exist
|
||||
const newSessionData = {
|
||||
state: SessionState.SETUP,
|
||||
topic: null,
|
||||
description: null,
|
||||
expectedResponses: 0,
|
||||
submittedCount: 0,
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
lastActivity: Date.now(), // Initialize lastActivity
|
||||
};
|
||||
exports.sessions.set(sessionId, newSessionData); // Explicitly set in global map
|
||||
sessionData = newSessionData; // Update local reference to the newly created session
|
||||
console.log(`New session ${sessionId} created upon client ${clientId} joining. lastActivity: ${sessionData.lastActivity}`);
|
||||
}
|
||||
// Register the client to the session's clients map
|
||||
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: getSerializableSession(sessionData, clientId) } }));
|
||||
console.log(`Client ${clientId} joined session: ${sessionId}. Total clients: ${sessionData.clients.size}`);
|
||||
}
|
||||
console.log(`Received message from ${clientId} in session ${sessionId}:`, type);
|
||||
switch (type) {
|
||||
case 'REGISTER_CLIENT':
|
||||
console.log(`Client ${clientId} registered successfully for session ${sessionId}.`);
|
||||
break;
|
||||
ws.send(JSON.stringify({ type: 'STATE_UPDATE', payload: { session: getSerializableSession(sessionData, clientId) } }));
|
||||
console.log(`Client ${clientId} received STATE_UPDATE for session ${sessionId} upon joining.`);
|
||||
return sessionData; // Return the updated sessionData
|
||||
case 'PING':
|
||||
if (!sessionData) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for PING' } }));
|
||||
return null;
|
||||
}
|
||||
// Respond to client pings with a pong
|
||||
if (ws.readyState === ws_1.WebSocket.OPEN) {
|
||||
ws.pong();
|
||||
}
|
||||
break;
|
||||
return sessionData || null; // Return current sessionData or null if undefined
|
||||
case 'SETUP_SESSION':
|
||||
if (!sessionData) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for SETUP_SESSION' } }));
|
||||
return null;
|
||||
}
|
||||
if (sessionData.state === SessionState.SETUP) {
|
||||
const { expectedResponses, topic, description } = payload;
|
||||
if (typeof expectedResponses !== 'number' || expectedResponses <= 0) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Invalid expectedResponses' } }));
|
||||
return;
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
}
|
||||
sessionData.expectedResponses = expectedResponses;
|
||||
sessionData.topic = topic || 'Untitled Session';
|
||||
@@ -184,22 +227,26 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
|
||||
else {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in SETUP state. Current state: ${sessionData.state}` } }));
|
||||
}
|
||||
break;
|
||||
return sessionData || null; // Return current sessionData
|
||||
case 'SUBMIT_RESPONSE':
|
||||
if (!sessionData) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for SUBMIT_RESPONSE' } }));
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
}
|
||||
const { wants, accepts, noGoes, afraidToAsk } = payload.response;
|
||||
if ([...wants, ...accepts, ...noGoes].some(desire => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
if ([...wants, ...accepts, ...noGoes].some((desire) => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'One of your desires or afraidToAsk exceeds the 500 character limit.' } }));
|
||||
return;
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
}
|
||||
const hasContradictionsGist = yield llmService.checkForInnerContradictions(payload.response);
|
||||
if (hasContradictionsGist) {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Your submission contains inner contradictions: ${hasContradictionsGist} Please resolve them and submit again.` } }));
|
||||
return;
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
}
|
||||
const encryptedWants = wants.map((d) => encryptionService.encrypt(d));
|
||||
const encryptedAccepts = accepts.map((d) => encryptionService.encrypt(d));
|
||||
@@ -207,18 +254,15 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
|
||||
const encryptedAfraidToAsk = encryptionService.encrypt(afraidToAsk);
|
||||
sessionData.responses.set(clientId, { wants: encryptedWants, accepts: encryptedAccepts, noGoes: encryptedNoGoes, afraidToAsk: encryptedAfraidToAsk });
|
||||
sessionData.submittedCount++;
|
||||
logEvent('response_submitted', sessionId, { clientId, submittedCount: sessionData.submittedCount });
|
||||
console.log(`Client ${clientId} submitted response. Submitted count: ${sessionData.submittedCount}/${sessionData.expectedResponses}`);
|
||||
if (sessionData.submittedCount === sessionData.expectedResponses) {
|
||||
sessionData.state = SessionState.HARMONIZING;
|
||||
(0, exports.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
logEvent('session_harmonizing', sessionId, { expectedResponses: sessionData.expectedResponses });
|
||||
console.log(`Session ${sessionId} moved to HARMONIZING. Triggering LLM analysis.`);
|
||||
// Perform LLM analysis asynchronously
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
let durationMs = 0; // Declare here
|
||||
try {
|
||||
logEvent('llm_analysis_started', sessionId);
|
||||
console.log('llm_analysis_started', sessionId);
|
||||
const startTime = process.hrtime.bigint();
|
||||
const allDecryptedDesires = Array.from(sessionData.responses.values()).map(encryptedResponse => {
|
||||
const decryptedWants = encryptedResponse.wants.map((d) => encryptionService.decrypt(d));
|
||||
@@ -231,17 +275,17 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
|
||||
sessionData.finalResult = decision;
|
||||
sessionData.state = SessionState.FINAL;
|
||||
(0, exports.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
logEvent('llm_analysis_completed', sessionId, { result: decision });
|
||||
recordMetric('llm_analysis_duration', durationMs, sessionId, { status: 'success' });
|
||||
recordMetric('llm_analysis_availability', 'available', sessionId);
|
||||
console.log('llm_analysis_completed', sessionId, { result: decision });
|
||||
console.log('llm_analysis_duration', 0, sessionId, { status: 'success' });
|
||||
console.log('llm_analysis_availability', 'available', sessionId);
|
||||
console.log(`Analysis complete for session ${sessionId}. Result:`, decision);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error during analysis for session ${sessionId}:`, error.message);
|
||||
sessionData.state = SessionState.ERROR;
|
||||
(0, exports.broadcastToSession)(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
logEvent('llm_analysis_error', sessionId, { error: error.message });
|
||||
recordMetric('llm_analysis_availability', 'unavailable', sessionId, { error: error.message });
|
||||
console.log('llm_analysis_error', sessionId, { error: error.message });
|
||||
console.log('llm_analysis_availability', 'unavailable', sessionId, { error: error.message });
|
||||
}
|
||||
}))();
|
||||
}
|
||||
@@ -253,11 +297,23 @@ const handleWebSocketMessage = (ws, sessionId, parsedMessage) => __awaiter(void
|
||||
else {
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in GATHERING state. Current state: ${sessionData.state}` } }));
|
||||
}
|
||||
break;
|
||||
return sessionData || null; // Return current sessionData
|
||||
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;
|
||||
return sessionData || null; // Return current sessionData
|
||||
}
|
||||
});
|
||||
exports.handleWebSocketMessage = handleWebSocketMessage;
|
||||
const cleanupInactiveSessions = () => {
|
||||
console.log('Running cleanupInactiveSessions...');
|
||||
const now = Date.now();
|
||||
for (const [sessionId, sessionData] of exports.sessions.entries()) {
|
||||
console.log(`Session ${sessionId}: clients.size=${sessionData.clients.size}, lastActivity=${sessionData.lastActivity}, timeSinceLastActivity=${now - sessionData.lastActivity}, SESSION_TIMEOUT_MS=${SESSION_TIMEOUT_MS}`);
|
||||
if (sessionData.clients.size === 0 && (now - sessionData.lastActivity > SESSION_TIMEOUT_MS)) {
|
||||
exports.sessions.delete(sessionId);
|
||||
logEvent('session_purged_inactive', sessionId);
|
||||
console.log(`Inactive session ${sessionId} purged.`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -51,6 +51,7 @@ app.post('/sessions', (req, res) => {
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
lastActivity: Date.now(),
|
||||
});
|
||||
console.log(`New session created: ${sessionId}`);
|
||||
res.status(201).json({ sessionId });
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
|
||||
|
||||
import express from 'express';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { sessions, SessionState, broadcastToSession, handleWebSocketMessage } from '../ws'; // Import sessions, SessionState, broadcastToSession, and handleWebSocketMessage from ws/index.ts
|
||||
import { sessions, SessionState, broadcastToSession } from '../ws'; // Import sessions, SessionState, broadcastToSession from ws/index.ts
|
||||
import { LLMService } from '../services/LLMService';
|
||||
import { EncryptionService } from '../services/EncryptionService';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Initialize LLM Service (API key from environment)
|
||||
const llmService = new LLMService(process.env.GEMINI_API_KEY || '');
|
||||
// Initialize Encryption Service
|
||||
const encryptionService = new EncryptionService(process.env.ENCRYPTION_KEY || '');
|
||||
|
||||
router.post('/sessions', (req, res) => {
|
||||
const sessionId = uuidv4();
|
||||
sessions.set(sessionId, {
|
||||
@@ -15,36 +24,81 @@ router.post('/sessions', (req, res) => {
|
||||
responses: new Map(),
|
||||
clients: new Map(),
|
||||
finalResult: null,
|
||||
lastActivity: Date.now(),
|
||||
});
|
||||
res.status(201).json({ sessionId });
|
||||
});
|
||||
|
||||
router.post('/sessions/:sessionId/responses', async (req, res) => {
|
||||
const { sessionId } = req.params;
|
||||
const { userId, wants, accepts, afraidToAsk } = req.body;
|
||||
const { clientId, wants, accepts, noGoes, afraidToAsk } = req.body; // Use clientId instead of userId
|
||||
|
||||
if (!sessions.has(sessionId)) {
|
||||
return res.status(404).json({ message: 'Session not found.' });
|
||||
}
|
||||
|
||||
// Create a dummy WebSocket object for the handleWebSocketMessage function.
|
||||
// This is a workaround to reuse the WebSocket message handling logic.
|
||||
// In a real application, consider a more robust event-driven architecture.
|
||||
const dummyWs: any = {
|
||||
send: (message: string) => console.log('Dummy WS send:', message),
|
||||
readyState: 1, // OPEN
|
||||
};
|
||||
const sessionData = sessions.get(sessionId)!;
|
||||
|
||||
const message = {
|
||||
type: 'SUBMIT_RESPONSE',
|
||||
clientId: userId, // Using userId as clientId for simplicity in this context
|
||||
payload: {
|
||||
response: { wants, accepts, afraidToAsk },
|
||||
},
|
||||
};
|
||||
if (sessionData.state !== SessionState.GATHERING) {
|
||||
return res.status(400).json({ message: `Session is not in GATHERING state. Current state: ${sessionData.state}` });
|
||||
}
|
||||
|
||||
if (sessionData.responses.has(clientId)) {
|
||||
return res.status(400).json({ message: 'You have already submitted a response for this session.' });
|
||||
}
|
||||
|
||||
if ([...wants, ...accepts, ...noGoes].some((desire: string) => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
return res.status(400).json({ message: 'One of your desires or afraidToAsk exceeds the 500 character limit.' });
|
||||
}
|
||||
|
||||
try {
|
||||
await handleWebSocketMessage(dummyWs, sessionId, message);
|
||||
const hasContradictionsGist = await llmService.checkForInnerContradictions({ wants, accepts, noGoes, afraidToAsk });
|
||||
if (hasContradictionsGist) {
|
||||
return res.status(400).json({ message: `Your submission contains inner contradictions: ${hasContradictionsGist} Please resolve them and submit again.` });
|
||||
}
|
||||
|
||||
const encryptedWants = wants.map((d: string) => encryptionService.encrypt(d));
|
||||
const encryptedAccepts = accepts.map((d: string) => encryptionService.encrypt(d));
|
||||
const encryptedNoGoes = noGoes.map((d: string) => encryptionService.encrypt(d));
|
||||
const encryptedAfraidToAsk = encryptionService.encrypt(afraidToAsk);
|
||||
|
||||
sessionData.responses.set(clientId, { wants: encryptedWants, accepts: encryptedAccepts, noGoes: encryptedNoGoes, afraidToAsk: encryptedAfraidToAsk });
|
||||
sessionData.submittedCount++;
|
||||
|
||||
console.log(`Client ${clientId} submitted response via HTTP. Submitted count: ${sessionData.submittedCount}/${sessionData.expectedResponses}`);
|
||||
|
||||
if (sessionData.submittedCount === sessionData.expectedResponses) {
|
||||
sessionData.state = SessionState.HARMONIZING;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
console.log(`Session ${sessionId} moved to HARMONIZING. Triggering LLM analysis.`);
|
||||
|
||||
// Perform LLM analysis asynchronously
|
||||
(async () => {
|
||||
try {
|
||||
const allDecryptedDesires = Array.from(sessionData.responses.values()).map(encryptedResponse => {
|
||||
const decryptedWants = encryptedResponse.wants.map((d: string) => encryptionService.decrypt(d));
|
||||
const decryptedAccepts = encryptedResponse.accepts.map((d: string) => encryptionService.decrypt(d));
|
||||
const decryptedNoGoes = encryptedResponse.noGoes.map((d: string) => encryptionService.decrypt(d));
|
||||
const decryptedAfraidToAsk = encryptionService.decrypt(encryptedResponse.afraidToAsk);
|
||||
return { wants: decryptedWants, accepts: decryptedAccepts, noGoes: decryptedNoGoes, afraidToAsk: decryptedAfraidToAsk };
|
||||
});
|
||||
const decision = await llmService.analyzeDesires(allDecryptedDesires);
|
||||
|
||||
sessionData.finalResult = decision;
|
||||
sessionData.state = SessionState.FINAL;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
console.log(`Analysis complete for session ${sessionId}. Result:`, decision);
|
||||
} catch (error: any) {
|
||||
console.error(`Error during analysis for session ${sessionId}:`, error.message);
|
||||
sessionData.state = SessionState.ERROR;
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
// Only broadcast the latest count if the session is not yet harmonizing
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
}
|
||||
|
||||
res.status(202).json({ message: 'Response submission acknowledged and processed.' });
|
||||
} catch (error: any) {
|
||||
console.error('Error processing response via HTTP route:', error);
|
||||
|
||||
@@ -41,6 +41,7 @@ interface SessionData {
|
||||
responses: Map<string, EncryptedResponseData>; // Stores the submitted desire objects. Map<ClientID, EncryptedResponseData>
|
||||
clients: Map<string, WebSocket>; // Maps the persistent Client ID to their active WebSocket connection object.
|
||||
finalResult: any | null; // The result returned by the LLM.
|
||||
lastActivity: number; // Timestamp of the last activity (e.g., message, client join/leave)
|
||||
}
|
||||
export const sessions = new Map<string, SessionData>();
|
||||
|
||||
@@ -102,9 +103,27 @@ export const broadcastToSession = (sessionId: string, message: any, excludeClien
|
||||
}
|
||||
};
|
||||
|
||||
const SESSION_TIMEOUT_MINUTES = parseInt(process.env.SESSION_TIMEOUT_MINUTES || '1440', 10);
|
||||
const SESSION_TIMEOUT_MS = SESSION_TIMEOUT_MINUTES * 60 * 1000; // Convert minutes to milliseconds
|
||||
|
||||
// Function to clean up inactive sessions
|
||||
const cleanupInactiveSessions = () => {
|
||||
const now = Date.now();
|
||||
for (const [sessionId, sessionData] of sessions.entries()) {
|
||||
if (sessionData.clients.size === 0 && (now - sessionData.lastActivity > SESSION_TIMEOUT_MS)) {
|
||||
sessions.delete(sessionId);
|
||||
logEvent('session_purged_inactive', sessionId);
|
||||
console.log(`Inactive session ${sessionId} purged.`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const createWebSocketServer = (server: any) => {
|
||||
const wss = new WebSocketServer({ server });
|
||||
|
||||
// Schedule periodic cleanup of inactive sessions
|
||||
setInterval(cleanupInactiveSessions, 60 * 60 * 1000); // Run every hour
|
||||
|
||||
wss.on('connection', (ws, req) => {
|
||||
const url = new URL(req.url || '', `http://${req.headers.host}`);
|
||||
const sessionId = url.pathname.split('/').pop();
|
||||
@@ -114,21 +133,9 @@ export const createWebSocketServer = (server: any) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sessions.has(sessionId)) {
|
||||
sessions.set(sessionId, {
|
||||
state: SessionState.SETUP,
|
||||
topic: null,
|
||||
description: 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}`);
|
||||
|
||||
let sessionData: SessionData | null = null;
|
||||
|
||||
// Set up a ping interval to keep the connection alive
|
||||
const pingInterval = setInterval(() => {
|
||||
@@ -139,28 +146,16 @@ export const createWebSocketServer = (server: any) => {
|
||||
|
||||
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;
|
||||
const updatedSessionData = await handleWebSocketMessage(ws, sessionId, parsedMessage);
|
||||
if (updatedSessionData) {
|
||||
sessionData = updatedSessionData;
|
||||
}
|
||||
|
||||
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: getSerializableSession(sessionData, clientId) } }));
|
||||
}
|
||||
|
||||
console.log(`Received message from ${clientId} in session ${sessionId}:`, type);
|
||||
await handleWebSocketMessage(ws, sessionId, parsedMessage);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
}); ws.on('close', () => {
|
||||
clearInterval(pingInterval); // Clear the interval when the connection closes
|
||||
let disconnectedClientId: string | null = null;
|
||||
for (const [clientId, clientWs] of sessionData.clients.entries()) {
|
||||
const currentSessionData = sessions.get(sessionId); // Retrieve the latest sessionData
|
||||
if (currentSessionData) { // Check if sessionData is not null
|
||||
for (const [clientId, clientWs] of currentSessionData.clients.entries()) {
|
||||
if (clientWs === ws) {
|
||||
disconnectedClientId = clientId;
|
||||
break;
|
||||
@@ -168,16 +163,26 @@ export const createWebSocketServer = (server: any) => {
|
||||
}
|
||||
|
||||
if (disconnectedClientId) {
|
||||
sessionData.clients.delete(disconnectedClientId);
|
||||
console.log(`Client ${disconnectedClientId} disconnected from session: ${sessionId}. Remaining clients: ${sessionData.clients.size}`);
|
||||
currentSessionData.clients.delete(disconnectedClientId);
|
||||
console.log(`Client ${disconnectedClientId} disconnected from session: ${sessionId}. Remaining clients: ${currentSessionData.clients.size}`);
|
||||
} else {
|
||||
console.log(`An unregistered client disconnected from session: ${sessionId}.`);
|
||||
}
|
||||
|
||||
if (sessionData.clients.size === 0) {
|
||||
if (currentSessionData.clients.size === 0) {
|
||||
// Only purge session if it's in SETUP, FINAL, or ERROR state
|
||||
if (currentSessionData.state === SessionState.SETUP ||
|
||||
currentSessionData.state === SessionState.FINAL ||
|
||||
currentSessionData.state === SessionState.ERROR) {
|
||||
sessions.delete(sessionId);
|
||||
logEvent('session_purged', sessionId);
|
||||
console.log(`Session ${sessionId} closed and state cleared.`);
|
||||
} else {
|
||||
console.log(`Session ${sessionId} is in ${currentSessionData.state} state. Not purging despite no active clients.`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(`Client disconnected from session: ${sessionId}. Session data was null.`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -189,131 +194,337 @@ export const createWebSocketServer = (server: any) => {
|
||||
return wss;
|
||||
};
|
||||
|
||||
export const handleWebSocketMessage = async (ws: WebSocket, sessionId: string, parsedMessage: any) => {
|
||||
export const handleWebSocketMessage = async (ws: WebSocket, sessionId: string, parsedMessage: any): Promise<SessionData | null> => {
|
||||
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;
|
||||
return sessions.get(sessionId) || null; // Return current session state if available
|
||||
}
|
||||
|
||||
const sessionData = sessions.get(sessionId)!;
|
||||
let sessionData = sessions.get(sessionId);
|
||||
|
||||
|
||||
|
||||
// Update lastActivity timestamp on any message
|
||||
|
||||
if (sessionData) {
|
||||
|
||||
sessionData.lastActivity = Date.now();
|
||||
|
||||
console.log(`Session ${sessionId}: lastActivity updated to ${sessionData.lastActivity}`);
|
||||
|
||||
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: getSerializableSession(sessionData, clientId) } }));
|
||||
}
|
||||
|
||||
console.log(`Received message from ${clientId} in session ${sessionId}:`, type);
|
||||
|
||||
|
||||
// If sessionData is null here, it means a JOIN_SESSION message hasn't been processed yet for a new session.
|
||||
|
||||
// The JOIN_SESSION case will handle its creation.
|
||||
|
||||
if (!sessionData && type !== 'JOIN_SESSION') {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session not found and message is not JOIN_SESSION' } }));
|
||||
|
||||
return null; // Session not found, return null
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
switch (type) {
|
||||
case 'REGISTER_CLIENT':
|
||||
console.log(`Client ${clientId} registered successfully for session ${sessionId}.`);
|
||||
break;
|
||||
|
||||
case 'JOIN_SESSION':
|
||||
|
||||
if (!sessionData) {
|
||||
|
||||
// Create a new session if it doesn't exist
|
||||
|
||||
const newSessionData: SessionData = {
|
||||
|
||||
state: SessionState.SETUP,
|
||||
|
||||
topic: null,
|
||||
|
||||
description: null,
|
||||
|
||||
expectedResponses: 0,
|
||||
|
||||
submittedCount: 0,
|
||||
|
||||
responses: new Map<string, any>(),
|
||||
|
||||
clients: new Map<string, WebSocket>(),
|
||||
|
||||
finalResult: null,
|
||||
|
||||
lastActivity: Date.now(), // Initialize lastActivity
|
||||
|
||||
};
|
||||
|
||||
sessions.set(sessionId, newSessionData); // Explicitly set in global map
|
||||
|
||||
sessionData = newSessionData; // Update local reference to the newly created session
|
||||
|
||||
console.log(`New session ${sessionId} created upon client ${clientId} joining. lastActivity: ${sessionData.lastActivity}`);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Register the client to the session's clients map
|
||||
|
||||
if (!sessionData.clients.has(clientId)) {
|
||||
|
||||
sessionData.clients.set(clientId, ws);
|
||||
|
||||
console.log(`Client ${clientId} joined session: ${sessionId}. Total clients: ${sessionData.clients.size}`);
|
||||
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({ type: 'STATE_UPDATE', payload: { session: getSerializableSession(sessionData, clientId) } }));
|
||||
|
||||
console.log(`Client ${clientId} received STATE_UPDATE for session ${sessionId} upon joining.`);
|
||||
|
||||
return sessionData; // Return the updated sessionData
|
||||
|
||||
|
||||
|
||||
case 'PING':
|
||||
|
||||
if (!sessionData) { ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for PING' } })); return null; }
|
||||
|
||||
// Respond to client pings with a pong
|
||||
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
|
||||
ws.pong();
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
return sessionData || null; // Return current sessionData or null if undefined
|
||||
|
||||
case 'SETUP_SESSION':
|
||||
|
||||
if (!sessionData) { ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for SETUP_SESSION' } })); return null; }
|
||||
|
||||
if (sessionData.state === SessionState.SETUP) {
|
||||
|
||||
const { expectedResponses, topic, description } = payload;
|
||||
|
||||
if (typeof expectedResponses !== 'number' || expectedResponses <= 0) {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Invalid expectedResponses' } }));
|
||||
return;
|
||||
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
|
||||
}
|
||||
|
||||
sessionData.expectedResponses = expectedResponses;
|
||||
|
||||
sessionData.topic = topic || 'Untitled Session';
|
||||
|
||||
sessionData.description = description || null;
|
||||
|
||||
sessionData.state = SessionState.GATHERING;
|
||||
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
|
||||
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;
|
||||
|
||||
else {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in SETUP state. Current state: ${sessionData.state}` } }));
|
||||
|
||||
}
|
||||
|
||||
return sessionData || null; // Return current sessionData
|
||||
|
||||
|
||||
|
||||
case 'SUBMIT_RESPONSE':
|
||||
|
||||
if (!sessionData) { ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'Session data not available for SUBMIT_RESPONSE' } })); return null; }
|
||||
|
||||
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;
|
||||
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const { wants, accepts, noGoes, afraidToAsk } = payload.response;
|
||||
if ([...wants, ...accepts, ...noGoes].some(desire => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
|
||||
if ([...wants, ...accepts, ...noGoes].some((desire: string) => desire.length > 500) || afraidToAsk.length > 500) {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: 'One of your desires or afraidToAsk exceeds the 500 character limit.' } }));
|
||||
return;
|
||||
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const hasContradictionsGist = await llmService.checkForInnerContradictions(payload.response);
|
||||
|
||||
if (hasContradictionsGist) {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Your submission contains inner contradictions: ${hasContradictionsGist} Please resolve them and submit again.` } }));
|
||||
return;
|
||||
|
||||
return sessionData || null; // Return current sessionData on error
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const encryptedWants = wants.map((d: string) => encryptionService.encrypt(d));
|
||||
|
||||
const encryptedAccepts = accepts.map((d: string) => encryptionService.encrypt(d));
|
||||
|
||||
const encryptedNoGoes = noGoes.map((d: string) => encryptionService.encrypt(d));
|
||||
|
||||
const encryptedAfraidToAsk = encryptionService.encrypt(afraidToAsk);
|
||||
|
||||
|
||||
|
||||
sessionData.responses.set(clientId, { wants: encryptedWants, accepts: encryptedAccepts, noGoes: encryptedNoGoes, afraidToAsk: encryptedAfraidToAsk });
|
||||
|
||||
sessionData.submittedCount++;
|
||||
logEvent('response_submitted', sessionId, { clientId, submittedCount: 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: {} });
|
||||
logEvent('session_harmonizing', sessionId, { expectedResponses: sessionData.expectedResponses });
|
||||
|
||||
console.log(`Session ${sessionId} moved to HARMONIZING. Triggering LLM analysis.`);
|
||||
|
||||
|
||||
|
||||
// Perform LLM analysis asynchronously
|
||||
|
||||
(async () => {
|
||||
let durationMs: number = 0; // Declare here
|
||||
|
||||
try {
|
||||
logEvent('llm_analysis_started', sessionId);
|
||||
|
||||
console.log('llm_analysis_started', sessionId);
|
||||
|
||||
const startTime = process.hrtime.bigint(); const allDecryptedDesires = Array.from(sessionData.responses.values()).map(encryptedResponse => {
|
||||
|
||||
const decryptedWants = encryptedResponse.wants.map((d: string) => encryptionService.decrypt(d));
|
||||
|
||||
const decryptedAccepts = encryptedResponse.accepts.map((d: string) => encryptionService.decrypt(d));
|
||||
|
||||
const decryptedNoGoes = encryptedResponse.noGoes.map((d: string) => encryptionService.decrypt(d));
|
||||
|
||||
const decryptedAfraidToAsk = encryptionService.decrypt(encryptedResponse.afraidToAsk);
|
||||
|
||||
return { wants: decryptedWants, accepts: decryptedAccepts, noGoes: decryptedNoGoes, afraidToAsk: decryptedAfraidToAsk };
|
||||
|
||||
});
|
||||
|
||||
const decision = await llmService.analyzeDesires(allDecryptedDesires);
|
||||
|
||||
|
||||
|
||||
sessionData.finalResult = decision;
|
||||
|
||||
sessionData.state = SessionState.FINAL;
|
||||
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
logEvent('llm_analysis_completed', sessionId, { result: decision });
|
||||
recordMetric('llm_analysis_duration', durationMs, sessionId, { status: 'success' }); recordMetric('llm_analysis_availability', 'available', sessionId);
|
||||
|
||||
console.log('llm_analysis_completed', sessionId, { result: decision });
|
||||
|
||||
console.log('llm_analysis_duration', 0, sessionId, { status: 'success' }); console.log('llm_analysis_availability', 'available', sessionId);
|
||||
|
||||
console.log(`Analysis complete for session ${sessionId}. Result:`, decision);
|
||||
|
||||
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
console.error(`Error during analysis for session ${sessionId}:`, error.message);
|
||||
|
||||
sessionData.state = SessionState.ERROR;
|
||||
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
logEvent('llm_analysis_error', sessionId, { error: error.message });
|
||||
recordMetric('llm_analysis_availability', 'unavailable', sessionId, { error: error.message });
|
||||
|
||||
console.log('llm_analysis_error', sessionId, { error: error.message });
|
||||
|
||||
console.log('llm_analysis_availability', 'unavailable', sessionId, { error: error.message });
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
} else {
|
||||
|
||||
// Only broadcast the latest count if the session is not yet harmonizing
|
||||
|
||||
broadcastToSession(sessionId, { type: 'STATE_UPDATE', payload: {} });
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
ws.send(JSON.stringify({ type: 'ERROR', payload: { message: `Session is not in GATHERING state. Current state: ${sessionData.state}` } }));
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
return sessionData || null; // Return current sessionData
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
return sessionData || null; // Return current sessionData
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
const cleanupInactiveSessions = () => {
|
||||
|
||||
console.log('Running cleanupInactiveSessions...');
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
for (const [sessionId, sessionData] of sessions.entries()) {
|
||||
|
||||
console.log(`Session ${sessionId}: clients.size=${sessionData.clients.size}, lastActivity=${sessionData.lastActivity}, timeSinceLastActivity=${now - sessionData.lastActivity}, SESSION_TIMEOUT_MS=${SESSION_TIMEOUT_MS}`);
|
||||
|
||||
if (sessionData.clients.size === 0 && (now - sessionData.lastActivity > SESSION_TIMEOUT_MS)) {
|
||||
|
||||
sessions.delete(sessionId);
|
||||
|
||||
logEvent('session_purged_inactive', sessionId);
|
||||
|
||||
console.log(`Inactive session ${sessionId} purged.`);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"files": {
|
||||
"main.js": "/static/js/main.d2d83152.js",
|
||||
"main.js": "/static/js/main.53017931.js",
|
||||
"index.html": "/index.html",
|
||||
"main.d2d83152.js.map": "/static/js/main.d2d83152.js.map"
|
||||
"main.53017931.js.map": "/static/js/main.53017931.js.map"
|
||||
},
|
||||
"entrypoints": [
|
||||
"static/js/main.d2d83152.js"
|
||||
"static/js/main.53017931.js"
|
||||
]
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/logo.svg"/><meta name="theme-color" content="#000000"/><meta name="description" content="A real-time app for collaborative decision-making."/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><script src="/config.js"></script><title>Unisono</title><script defer="defer" src="/static/js/main.d2d83152.js"></script></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/logo.svg"/><meta name="theme-color" content="#000000"/><meta name="description" content="A real-time app for collaborative decision-making."/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><script src="/config.js"></script><title>Unisono</title><script defer="defer" src="/static/js/main.53017931.js"></script></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
||||
File diff suppressed because one or more lines are too long
@@ -1,80 +0,0 @@
|
||||
/**
|
||||
* @license React
|
||||
* react-dom.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-is.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-jsx-runtime.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* scheduler.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @remix-run/router v1.23.0
|
||||
*
|
||||
* Copyright (c) Remix Software Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE.md file in the root directory of this source tree.
|
||||
*
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/**
|
||||
* React Router v6.30.1
|
||||
*
|
||||
* Copyright (c) Remix Software Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE.md file in the root directory of this source tree.
|
||||
*
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/** @license React v16.13.1
|
||||
* react-is.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
File diff suppressed because one or more lines are too long
@@ -32,8 +32,8 @@ class WebSocketService {
|
||||
|
||||
this.ws.onopen = () => {
|
||||
console.log('WebSocket connected');
|
||||
// Directly send registration message on open
|
||||
this.sendMessage({ type: 'REGISTER_CLIENT' });
|
||||
// Send JOIN_SESSION message on open to inform the server of the client and session IDs
|
||||
this.sendMessage({ type: 'JOIN_SESSION', payload: { clientId: this.currentClientId, sessionId: this.currentSessionId } });
|
||||
|
||||
// Start heartbeat to keep connection alive
|
||||
this.heartbeatInterval = setInterval(() => {
|
||||
|
||||
Reference in New Issue
Block a user