import config from '../config';
interface Session {
id: string;
createdAt: Date;
lastAccessed: Date;
data: Record<string, any>;
}
interface TranscriptionData {
text: string;
timestamp: Date;
metadata?: Record<string, any>;
}
interface SonarQuery {
query: string;
timestamp: Date;
response?: any;
}
interface WhatsAppSession {
phoneNumber: string;
lastInteraction: Date;
context?: Record<string, any>;
}
class TempStore {
private sessions: Map<string, Session>;
private transcriptions: Map<string, TranscriptionData>;
private sonarQueries: Map<string, SonarQuery>;
private whatsappSessions: Map<string, WhatsAppSession>;
constructor() {
this.sessions = new Map();
this.transcriptions = new Map();
this.sonarQueries = new Map();
this.whatsappSessions = new Map();
// Start cleanup interval
setInterval(() => this.cleanup(), 5 * 60 * 1000); // Run every 5 minutes
}
// Session Management
saveSession(id: string, data: Record<string, any>): void {
this.sessions.set(id, {
id,
createdAt: new Date(),
lastAccessed: new Date(),
data,
});
}
getSession(id: string): Record<string, any> | null {
const session = this.sessions.get(id);
if (!session) return null;
session.lastAccessed = new Date();
return session.data;
}
deleteSession(id: string): boolean {
return this.sessions.delete(id);
}
// Transcription Management
saveTranscription(id: string, text: string, metadata?: Record<string, any>): void {
this.transcriptions.set(id, {
text,
timestamp: new Date(),
metadata,
});
}
getTranscription(id: string): TranscriptionData | null {
return this.transcriptions.get(id) || null;
}
// Sonar Query Management
saveSonarQuery(id: string, query: string, response?: any): void {
this.sonarQueries.set(id, {
query,
timestamp: new Date(),
response,
});
}
getSonarQuery(id: string): SonarQuery | null {
return this.sonarQueries.get(id) || null;
}
// WhatsApp Session Management
saveWhatsAppSession(phoneNumber: string, context?: Record<string, any>): void {
this.whatsappSessions.set(phoneNumber, {
phoneNumber,
lastInteraction: new Date(),
context,
});
}
getWhatsAppSession(phoneNumber: string): WhatsAppSession | null {
return this.whatsappSessions.get(phoneNumber) || null;
}
// Cleanup old data
private cleanup(): void {
const now = new Date();
const timeoutMs = config.sessionTimeout * 60 * 1000;
// Cleanup sessions
for (const [id, session] of this.sessions) {
if (now.getTime() - session.lastAccessed.getTime() > timeoutMs) {
this.sessions.delete(id);
}
}
// Cleanup transcriptions older than 24 hours
for (const [id, transcription] of this.transcriptions) {
if (now.getTime() - transcription.timestamp.getTime() > 24 * 60 * 60 * 1000) {
this.transcriptions.delete(id);
}
}
// Cleanup Sonar queries older than 1 hour
for (const [id, query] of this.sonarQueries) {
if (now.getTime() - query.timestamp.getTime() > 60 * 60 * 1000) {
this.sonarQueries.delete(id);
}
}
// Cleanup WhatsApp sessions
for (const [phoneNumber, session] of this.whatsappSessions) {
if (now.getTime() - session.lastInteraction.getTime() > timeoutMs) {
this.whatsappSessions.delete(phoneNumber);
}
}
}
}
// Export a singleton instance
export const store = new TempStore();