import { randomUUID } from 'node:crypto' import { log } from '../logger' import { getEventBus } from '../transport/event-bus' import { publishSessionEvent } from './transport' import { deletePersistedEvents } from './event-persistence' /** * Wipe a session's conversation: drop the in-memory replay window and the * durable SQLite event log, notify live web viewers with a transient * session_status event (status=conversation_cleared — the web chat adapter * maps it to an empty entry list), and re-seed models_state from the bus * snapshot. The worker only reports models_state once per bridge connect, so * a plain history wipe would leave the model selector permanently gray after * re-entering the session. * * Shared by POST /web/sessions/:id/clear (web-initiated /clear) and the * ws-handler conversation_cleared status path (CLI worker's own /clear) so * both directions produce identical server-side state. */ export function wipeSessionConversation(sessionId: string): void { // Drop the replay window first so events arriving after the clear are // retained, then delete the durable log. The notification is transient: // it must reach live subscribers but never re-populate history. const bus = getEventBus(sessionId) bus.clearHistory() deletePersistedEvents(sessionId) bus.publish( { id: randomUUID(), sessionId, type: 'session_status', payload: { status: 'conversation_cleared', message: 'conversation_cleared', }, direction: 'inbound', }, { transient: true }, ) if (bus.modelsStateSnapshot) { publishSessionEvent( sessionId, 'models_state', { type: 'models_state', models_state: bus.modelsStateSnapshot }, 'inbound', ) } log(`[RCS] Cleared conversation history for session ${sessionId}`) }