commit 2 refactor

This commit is contained in:
Boki 2026-10-20 14:35:45 -04:00
parent 588f666943
commit 32e43c9243
310 changed files with 33318 additions and 31189 deletions

View file

@ -1,160 +1,118 @@
import { getLogger } from '@stock-bot/logger';
import type { IServiceContainer } from '@stock-bot/handlers';
const logger = getLogger('websocket-run-updates');
// Store active WebSocket connections per run
// Using 'any' type for Bun WebSocket compatibility
const runConnections = new Map<string, Set<any>>();
export interface RunUpdateMessage {
type: 'run_update' | 'progress' | 'error' | 'completed';
runId: string;
data: any;
}
export function createWebSocketHandler(container: IServiceContainer) {
return {
// Handle new WebSocket connection (ws is Bun's WebSocket type)
handleConnection(ws: any, runId: string) {
logger.info(`WebSocket connected for run: ${runId}`);
// Add connection to the run's connection set
if (!runConnections.has(runId)) {
runConnections.set(runId, new Set());
}
runConnections.get(runId)!.add(ws);
// Send initial connection confirmation
ws.send(JSON.stringify({
type: 'connected',
runId,
timestamp: new Date().toISOString()
}));
// In Bun, WebSocket handlers are set directly as properties
ws.onmessage = (event: MessageEvent) => {
try {
const message = JSON.parse(event.data.toString());
logger.debug('Received WebSocket message:', message);
// Handle different message types
switch (message.type) {
case 'ping':
ws.send(JSON.stringify({ type: 'pong' }));
break;
case 'subscribe':
// Already subscribed to this run
break;
default:
logger.warn('Unknown message type:', message.type);
}
} catch (error) {
logger.error('Error handling WebSocket message:', error);
}
};
// Handle connection close
ws.onclose = (event: any) => {
logger.info(`WebSocket disconnected for run: ${runId}`, { code: event.code, reason: event.reason });
const connections = runConnections.get(runId);
if (connections) {
connections.delete(ws);
if (connections.size === 0) {
runConnections.delete(runId);
}
}
};
// Handle errors
ws.onerror = (error: Event) => {
logger.error(`WebSocket error for run ${runId}:`, error);
};
},
// Broadcast update to all connected clients for a run
broadcastRunUpdate(runId: string, update: Partial<RunUpdateMessage>) {
const connections = runConnections.get(runId);
if (!connections || connections.size === 0) {
return;
}
const message: RunUpdateMessage = {
type: 'run_update',
runId,
data: update.data || update,
...update
};
const messageStr = JSON.stringify(message);
connections.forEach(ws => {
try {
// Bun WebSocket - just try to send, it will throw if not open
ws.send(messageStr);
} catch (error) {
// Connection is closed, ignore
}
});
},
// Send progress update
sendProgressUpdate(runId: string, progress: number, currentDate?: string) {
this.broadcastRunUpdate(runId, {
type: 'progress',
data: {
progress,
currentDate,
timestamp: new Date().toISOString()
}
});
},
// Send error
sendError(runId: string, error: string) {
this.broadcastRunUpdate(runId, {
type: 'error',
data: {
error,
timestamp: new Date().toISOString()
}
});
},
// Send completion
sendCompletion(runId: string, results?: any) {
this.broadcastRunUpdate(runId, {
type: 'completed',
data: {
results,
timestamp: new Date().toISOString()
}
});
// Clean up connections after completion
setTimeout(() => {
const connections = runConnections.get(runId);
if (connections) {
connections.forEach(ws => ws.close());
runConnections.delete(runId);
}
}, 5000);
},
// Get active connections count
getConnectionsCount(runId: string): number {
return runConnections.get(runId)?.size || 0;
},
// Clean up all connections
cleanup() {
runConnections.forEach((connections, runId) => {
connections.forEach(ws => ws.close());
});
runConnections.clear();
}
};
}
// Export the WebSocket handler type
export type WebSocketHandler = ReturnType<typeof createWebSocketHandler>;
import type { ServerWebSocket } from 'bun';
import { getLogger } from '@stock-bot/logger';
import type { IServiceContainer } from '@stock-bot/services';
const logger = getLogger('websocket-run-updates');
export interface RunSocketData {
runId: string;
}
type RunSocket = ServerWebSocket<RunSocketData>;
export interface RunUpdateMessage {
type: 'run_update' | 'progress' | 'error' | 'completed';
runId: string;
data: unknown;
}
export function createWebSocketHandler(_container: IServiceContainer) {
const connections = new Map<string, Set<RunSocket>>();
const completionTimers = new Map<string, ReturnType<typeof setTimeout>>();
return {
handleConnection(ws: RunSocket, runId: string) {
const clients = connections.get(runId) ?? new Set<RunSocket>();
clients.add(ws);
connections.set(runId, clients);
ws.send(JSON.stringify({ type: 'connected', runId, timestamp: new Date().toISOString() }));
},
handleMessage(ws: RunSocket, payload: string | Buffer) {
try {
const message: unknown = JSON.parse(payload.toString());
if (
message &&
typeof message === 'object' &&
'type' in message &&
message.type === 'ping'
) {
ws.send(JSON.stringify({ type: 'pong' }));
}
} catch (error) {
logger.warn('Invalid WebSocket message', { error });
}
},
handleClose(ws: RunSocket) {
const runId = ws.data.runId;
const clients = connections.get(runId);
clients?.delete(ws);
if (clients?.size === 0) {
connections.delete(runId);
}
},
broadcastRunUpdate(runId: string, update: Partial<RunUpdateMessage>) {
const message: RunUpdateMessage = {
type: 'run_update',
runId,
data: update.data ?? update,
...update,
};
for (const ws of connections.get(runId) ?? []) {
try {
ws.send(JSON.stringify(message));
} catch {
this.handleClose(ws);
}
}
},
sendProgressUpdate(runId: string, progress: number, currentDate?: string) {
this.broadcastRunUpdate(runId, {
type: 'progress',
data: { progress, currentDate, timestamp: new Date().toISOString() },
});
},
sendError(runId: string, error: string) {
this.broadcastRunUpdate(runId, {
type: 'error',
data: { error, timestamp: new Date().toISOString() },
});
},
sendCompletion(runId: string, results?: unknown) {
this.broadcastRunUpdate(runId, {
type: 'completed',
data: { results, timestamp: new Date().toISOString() },
});
clearTimeout(completionTimers.get(runId));
completionTimers.set(
runId,
setTimeout(() => {
for (const ws of connections.get(runId) ?? []) {
ws.close();
}
connections.delete(runId);
completionTimers.delete(runId);
}, 5000)
);
},
getConnectionsCount(runId: string): number {
return connections.get(runId)?.size ?? 0;
},
cleanup() {
for (const timer of completionTimers.values()) {
clearTimeout(timer);
}
completionTimers.clear();
for (const clients of connections.values()) {
for (const ws of clients) {
ws.close();
}
}
connections.clear();
},
};
}
export type WebSocketHandler = ReturnType<typeof createWebSocketHandler>;
let sharedHandler: WebSocketHandler | null = null;
export function setWebSocketHandler(handler: WebSocketHandler): void {
sharedHandler = handler;
}
export function getWebSocketHandler(): WebSocketHandler | null {
return sharedHandler;
}