1. Session persists in GATHERING state. 2. Added inactive sessions purging, but it does not work.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user