import express from 'express'; import { v4 as uuidv4 } from 'uuid'; 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, { state: 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', async (req, res) => { const { sessionId } = req.params; 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.' }); } const sessionData = sessions.get(sessionId)!; 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 { 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); res.status(500).json({ message: 'Error processing response.', error: error.message }); } }); router.get('/sessions/:sessionId/results', (req, res) => { const { sessionId } = req.params; if (!sessions.has(sessionId)) { return res.status(404).json({ message: 'Session not found.' }); } const sessionData = sessions.get(sessionId)!; if (sessionData.state !== SessionState.FINAL || !sessionData.finalResult) { return res.status(200).json({ message: 'Session results not yet finalized.', harmonizedIdeas: [] }); } // Assuming finalResult directly contains the harmonized ideas as per openapi.yaml res.status(200).json({ sessionId, harmonizedIdeas: sessionData.finalResult }); }); router.post('/sessions/:sessionId/terminate', (req, res) => { const { sessionId } = req.params; if (!sessions.has(sessionId)) { return res.status(404).json({ message: 'Session not found.' }); } sessions.delete(sessionId); // Log the purging event // logEvent('session_terminated_and_purged', sessionId); console.log(`Session ${sessionId} terminated and data purged.`); res.status(200).json({ message: 'Session terminated and data purged successfully.' }); }); export default router;