diff --git a/src/server.ts b/src/server.ts index 9c38fad..6f6b495 100644 --- a/src/server.ts +++ b/src/server.ts @@ -520,20 +520,21 @@ const server = Bun.serve({ return; } - // Write to terminal emulator (handles all ANSI sequences) - termSession.terminal.write(msg.data); - // Store raw ANSI in database appendOutput(sessionId, msg.data); - // Serialize current terminal state as HTML - const html = serializeAsHTML(termSession); + // Write to terminal emulator (handles all ANSI sequences) + // Use callback to wait for write completion before serializing + termSession.terminal.write(msg.data, () => { + // Serialize current terminal state as HTML + const html = serializeAsHTML(termSession); - // Broadcast to dashboards - broadcastSSE({ - type: "output", - session_id: sessionId, - data: html, + // Broadcast to dashboards + broadcastSSE({ + type: "output", + session_id: sessionId, + data: html, + }); }); return;