Files
unisono/backend/dist/routes/sessions.js

130 lines
7.5 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
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 { 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.' });
}
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 {
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) {
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 (!ws_1.sessions.has(sessionId)) {
return res.status(404).json({ message: 'Session not found.' });
}
const sessionData = ws_1.sessions.get(sessionId);
if (sessionData.state !== ws_1.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 (!ws_1.sessions.has(sessionId)) {
return res.status(404).json({ message: 'Session not found.' });
}
ws_1.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.' });
});
exports.default = router;