feat: Introduce SessionEventBroadcaster and SessionCompletionHandler for improved session management
- Added SessionEventBroadcaster to handle broadcasting of session lifecycle events, consolidating SSE broadcasting and processing status updates. - Refactored SessionRoutes to utilize SessionEventBroadcaster for broadcasting events related to new prompts, session starts, and completions. - Created SessionCompletionHandler to centralize session completion logic, reducing duplication across multiple endpoints. - Updated WorkerService to initialize SessionEventBroadcaster and pass it to SessionRoutes.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Session Event Broadcaster
|
||||
*
|
||||
* Provides semantic broadcast methods for session lifecycle events.
|
||||
* Consolidates SSE broadcasting and processing status updates.
|
||||
*/
|
||||
|
||||
import { SSEBroadcaster } from '../SSEBroadcaster.js';
|
||||
import type { WorkerService } from '../../worker-service.js';
|
||||
|
||||
export class SessionEventBroadcaster {
|
||||
constructor(
|
||||
private sseBroadcaster: SSEBroadcaster,
|
||||
private workerService: WorkerService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Broadcast new user prompt arrival
|
||||
* Starts activity indicator to show work is beginning
|
||||
*/
|
||||
broadcastNewPrompt(prompt: {
|
||||
id: number;
|
||||
claude_session_id: string;
|
||||
project: string;
|
||||
prompt_number: number;
|
||||
prompt_text: string;
|
||||
created_at_epoch: number;
|
||||
}): void {
|
||||
// Broadcast prompt details
|
||||
this.sseBroadcaster.broadcast({
|
||||
type: 'new_prompt',
|
||||
prompt
|
||||
});
|
||||
|
||||
// Start activity indicator (work is about to begin)
|
||||
this.sseBroadcaster.broadcast({
|
||||
type: 'processing_status',
|
||||
isProcessing: true
|
||||
});
|
||||
|
||||
// Update processing status based on queue depth
|
||||
this.workerService.broadcastProcessingStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast session initialization
|
||||
*/
|
||||
broadcastSessionStarted(sessionDbId: number, project: string): void {
|
||||
this.sseBroadcaster.broadcast({
|
||||
type: 'session_started',
|
||||
sessionDbId,
|
||||
project
|
||||
});
|
||||
|
||||
// Update processing status
|
||||
this.workerService.broadcastProcessingStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast observation queued
|
||||
* Updates processing status to reflect new queue depth
|
||||
*/
|
||||
broadcastObservationQueued(sessionDbId: number): void {
|
||||
this.sseBroadcaster.broadcast({
|
||||
type: 'observation_queued',
|
||||
sessionDbId
|
||||
});
|
||||
|
||||
// Update processing status (queue depth changed)
|
||||
this.workerService.broadcastProcessingStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast session completion
|
||||
* Updates processing status to reflect session removal
|
||||
*/
|
||||
broadcastSessionCompleted(sessionDbId: number): void {
|
||||
this.sseBroadcaster.broadcast({
|
||||
type: 'session_completed',
|
||||
timestamp: Date.now(),
|
||||
sessionDbId
|
||||
});
|
||||
|
||||
// Update processing status (session removed from queue)
|
||||
this.workerService.broadcastProcessingStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast summarize request queued
|
||||
* Updates processing status to reflect new queue depth
|
||||
*/
|
||||
broadcastSummarizeQueued(): void {
|
||||
// Update processing status (queue depth changed)
|
||||
this.workerService.broadcastProcessingStatus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user