vibetunnel/web/src/server/services/bell-event-handler.ts
2025-06-23 16:55:53 +02:00

156 lines
3.9 KiB
TypeScript

/**
* BellEventHandler - Ultra-simple bell event handler
*
* This simplified handler just sends notifications for bell events
* without any filtering, correlation, or user tracking.
*/
import { SessionInfo } from '../../shared/types.js';
import { createLogger } from '../utils/logger.js';
import { PushNotificationService } from './push-notification-service.js';
const logger = createLogger('bell-event-handler');
/**
* Simplified bell event context
*/
export interface BellEventContext {
sessionInfo: SessionInfo;
timestamp: Date;
}
/**
* Simple bell notification payload
*/
export interface BellNotificationPayload {
type: 'bell-event';
sessionId: string;
sessionName: string;
title: string;
body: string;
icon?: string;
badge?: string;
tag: string;
requireInteraction: boolean;
actions?: Array<{
action: string;
title: string;
}>;
data: {
sessionId: string;
timestamp: string;
};
}
/**
* Ultra-simple bell event handler
*/
export class BellEventHandler {
private pushNotificationService: PushNotificationService | null = null;
constructor() {
logger.debug('BellEventHandler initialized');
}
/**
* Set the push notification service for sending notifications
*/
setPushNotificationService(service: PushNotificationService): void {
this.pushNotificationService = service;
logger.debug('Push notification service configured');
}
/**
* Process a bell event - ultra-simple version
*/
async processBellEvent(context: BellEventContext): Promise<void> {
try {
logger.debug('Processing bell event', {
sessionId: context.sessionInfo.id,
timestamp: context.timestamp.toISOString(),
});
// Always send notification - no filtering
if (this.pushNotificationService) {
const payload = this.createNotificationPayload(context);
await this.sendPushNotification(payload);
}
logger.debug('Bell event processed successfully', {
sessionId: context.sessionInfo.id,
});
} catch (error) {
logger.error('Error processing bell event', {
sessionId: context.sessionInfo.id,
error: error instanceof Error ? error.message : String(error),
});
}
}
/**
* Create simple notification payload
*/
private createNotificationPayload(context: BellEventContext): BellNotificationPayload {
const sessionName = context.sessionInfo.name || 'Terminal Session';
const title = '🔔 Terminal Activity';
const body = `${sessionName} triggered a bell`;
const tag = `vibetunnel-bell-${context.sessionInfo.id}`;
return {
type: 'bell-event',
sessionId: context.sessionInfo.id,
sessionName,
title,
body,
icon: '/apple-touch-icon.png',
badge: '/favicon-32.png',
tag,
requireInteraction: false,
actions: [
{
action: 'view-session',
title: 'View Session',
},
{
action: 'dismiss',
title: 'Dismiss',
},
],
data: {
sessionId: context.sessionInfo.id,
timestamp: context.timestamp.toISOString(),
},
};
}
/**
* Send push notification
*/
private async sendPushNotification(payload: BellNotificationPayload): Promise<void> {
if (!this.pushNotificationService) {
logger.debug('No push notification service configured');
return;
}
try {
await this.pushNotificationService.sendBellNotification(payload);
logger.debug('Push notification sent', {
sessionId: payload.sessionId,
title: payload.title,
});
} catch (error) {
logger.error('Failed to send push notification', {
sessionId: payload.sessionId,
error: error instanceof Error ? error.message : String(error),
});
}
}
/**
* Clean up resources
*/
dispose(): void {
logger.debug('BellEventHandler disposed');
}
}