50 lines
1.2 KiB
TypeScript

/**
* In-Memory Session Store für SOS-Streaming
*
* POST /api/coach/sos-session speichert messages/locale hier,
* GET /api/coach/sos-stream lädt sie per sessionId.
*
* TTL: 5 Minuten (Auto-Cleanup)
*/
type SosSessionData = {
userId: string;
messages: Array<{ role: "user" | "assistant"; content: string }>;
locale: string;
createdAt: number;
};
const sessions = new Map<string, SosSessionData>();
const SESSION_TTL = 5 * 60 * 1000; // 5min
// Cleanup-Intervall: alle 2min alte Sessions löschen
setInterval(
() => {
const now = Date.now();
for (const [id, data] of sessions.entries()) {
if (now - data.createdAt > SESSION_TTL) {
sessions.delete(id);
}
}
},
2 * 60 * 1000,
);
export function setSosSession(sessionId: string, data: SosSessionData) {
sessions.set(sessionId, data);
}
export function getSosSession(sessionId: string): SosSessionData | undefined {
const data = sessions.get(sessionId);
if (!data) return undefined;
// TTL-Check
if (Date.now() - data.createdAt > SESSION_TTL) {
sessions.delete(sessionId);
return undefined;
}
return data;
}
export function deleteSosSession(sessionId: string) {
sessions.delete(sessionId);
}