UNPKG

@mediacat/mcp

Version:

A Model Context Protocol (MCP) server for MediaCAT's subtitle generation workflow with XL8.ai integration. Supports local file processing, real-time SSE updates, and dynamic language detection.

86 lines 3.14 kB
export class SSEManager { clients = new Map(); addClient(clientId, response, lastEventId) { const client = { id: clientId, response, lastEventId, }; if (!this.clients.has(clientId)) { this.clients.set(clientId, []); } this.clients.get(clientId).push(client); const totalClients = Array.from(this.clients.values()).reduce((sum, clients) => sum + clients.length, 0); console.log(`📡 Client ${clientId} connected. Active clients: ${totalClients}`); } removeClient(clientId, req) { const clientList = this.clients.get(clientId); if (clientList) { // Find and remove the specific client by matching the request object const index = clientList.findIndex(client => client.response.req === req); if (index !== -1) { clientList.splice(index, 1); if (clientList.length === 0) { this.clients.delete(clientId); } } } const totalClients = Array.from(this.clients.values()).reduce((sum, clients) => sum + clients.length, 0); console.log(`📡 Client disconnected from ${clientId}. Active clients: ${totalClients}`); } sendToResponse(response, event, data) { response.write(`event: ${event}\n`); if (!data.timestamp) { data.timestamp = new Date().toISOString(); } response.write(`data: ${JSON.stringify(data)}\n\n`); return true; } sendToChannel(channel, event, data) { if (!channel) { return 0; } const clientList = this.clients.get(channel); if (!clientList || clientList.length === 0) { return 0; } if (!data.timestamp) { data.timestamp = new Date().toISOString(); } let sentCount = 0; const deadClients = []; clientList.forEach((client, index) => { try { this.sendToResponse(client.response, event, data); sentCount++; } catch (error) { console.error(`Failed to send to client ${client.id}:`, error); deadClients.push(index); } }); // Remove dead clients (in reverse order to maintain indices) deadClients.reverse().forEach(index => clientList.splice(index, 1)); if (clientList.length === 0) { this.clients.delete(channel); } return sentCount; } broadcast(event, data) { if (!data.timestamp) { data.timestamp = new Date().toISOString(); } let sentCount = 0; for (const [channelId, clientList] of this.clients) { sentCount += this.sendToChannel(channelId, event, data); } return sentCount; } getClientCount() { return Array.from(this.clients.values()).reduce((sum, clients) => sum + clients.length, 0); } getClientIds() { return Array.from(this.clients.keys()); } } //# sourceMappingURL=sse-manager.js.map