managarten/packages/matrix-bot-common/src/session/session-helper.ts
Till-JS 2777f604fd feat(bots): enable Redis SSO for todo-bot and calendar-bot
- Activate Redis session storage in both bots for cross-bot SSO
- Update SessionHelper to async methods for Redis-backed SessionService
- Fix async/await issues in todo-bot and calendar-bot matrix.service.ts
- Remove unused imports from calendar-api and todo-api services
- Add CALENDAR_BACKEND_URL and MANA_CORE_SERVICE_KEY to .env.development

Note: SessionService methods are now async (Redis-backed). Other bots
need their matrix.service.ts updated to await these async calls.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 14:51:23 +01:00

85 lines
2 KiB
TypeScript

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<ChatSessionData>(sessionService, matrixUserId);
* await session.set('currentConversationId', 'abc123');
* const convId = await session.get('currentConversationId'); // string | null
* ```
*/
export class SessionHelper<T extends Record<string, unknown>> {
constructor(
private readonly sessionService: SessionService,
private readonly userId: string
) {}
/**
* Set a session value
*/
async set<K extends keyof T>(key: K, value: T[K]): Promise<void> {
await this.sessionService.setSessionData(this.userId, key as string, value);
}
/**
* Get a session value
*/
async get<K extends keyof T>(key: K): Promise<T[K] | null> {
return this.sessionService.getSessionData<T[K]>(this.userId, key as string);
}
/**
* Delete a session value
*/
async delete<K extends keyof T>(key: K): Promise<void> {
await this.sessionService.setSessionData(this.userId, key as string, null);
}
/**
* Check if a session value exists
*/
async has<K extends keyof T>(key: K): Promise<boolean> {
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<boolean> {
return this.sessionService.isLoggedIn(this.userId);
}
/**
* Get JWT token for API calls
*/
async getToken(): Promise<string | null> {
return this.sessionService.getToken(this.userId);
}
}
/**
* Factory function to create session helper
*/
export function createSessionHelper<T extends Record<string, unknown>>(
sessionService: SessionService,
userId: string
): SessionHelper<T> {
return new SessionHelper<T>(sessionService, userId);
}