import { type SessionService } from '@manacore/bot-services'; /** * Typed session helper for bot-specific session data * * Provides type-safe access to session data stored in SessionService. * * @example * ```typescript * interface ChatSessionData { * currentConversationId: string; * selectedModelId: string; * conversationList: string[]; * } * * const session = new SessionHelper(sessionService, matrixUserId); * await session.set('currentConversationId', 'abc123'); * const convId = await session.get('currentConversationId'); // string | null * ``` */ export class SessionHelper> { constructor( private readonly sessionService: SessionService, private readonly userId: string ) {} /** * Set a session value */ async set(key: K, value: T[K]): Promise { await this.sessionService.setSessionData(this.userId, key as string, value); } /** * Get a session value */ async get(key: K): Promise { return this.sessionService.getSessionData(this.userId, key as string); } /** * Delete a session value */ async delete(key: K): Promise { await this.sessionService.setSessionData(this.userId, key as string, null); } /** * Check if a session value exists */ async has(key: K): Promise { return (await this.get(key)) !== null; } /** * Get the underlying user ID */ getUserId(): string { return this.userId; } /** * Check if user is logged in */ async isLoggedIn(): Promise { return this.sessionService.isLoggedIn(this.userId); } /** * Get JWT token for API calls */ async getToken(): Promise { return this.sessionService.getToken(this.userId); } } /** * Factory function to create session helper */ export function createSessionHelper>( sessionService: SessionService, userId: string ): SessionHelper { return new SessionHelper(sessionService, userId); }