Add initial_state SSE event for reconnect sync (Phase 3)

When a dashboard connects via SSE, it now receives the current terminal
state for all active sessions. This allows dashboards to immediately
display the full terminal content without waiting for new output.
This commit is contained in:
Jared Miller 2026-01-31 09:51:13 -05:00
parent 88afb7249d
commit 43648f7d60
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 20 additions and 1 deletions

View file

@ -135,8 +135,26 @@ const server = Bun.serve<SessionData>({
start(controller) {
ctrl = controller;
sseClients.add(controller);
// Send initial headers
// Send initial connection acknowledgment
controller.enqueue(": connected\n\n");
// Send current terminal state for all active sessions
for (const [sessionId, termSession] of sessionTerminals.entries()) {
const session = getSession(sessionId);
if (!session || session.ended_at) continue;
// Serialize full terminal state as HTML
const html = serializeAsHTML(termSession);
// Send as initial_state event
const event: SSEEvent = {
type: "initial_state",
session_id: sessionId,
html,
};
const eventStr = `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`;
controller.enqueue(eventStr);
}
},
cancel() {
sseClients.delete(ctrl);

View file

@ -157,6 +157,7 @@ export type ServerMessage =
// SSE events (Server -> Dashboard)
export type SSEEvent =
| { type: "initial_state"; session_id: number; html: string }
| {
type: "session_start";
session_id: number;