diff --git a/src/server.ts b/src/server.ts index 43919a3..65e73ca 100644 --- a/src/server.ts +++ b/src/server.ts @@ -81,8 +81,22 @@ function broadcastSSE(event: SSEEvent): void { } } -// Detect and split any incomplete ANSI control sequence at the end of a chunk. -// Returns [body, carry] where carry should be saved and prepended to the next chunk. +/** + * Detect and split any incomplete ANSI control sequence at the end of a chunk. + * + * ANSI sequences can span multiple network packets. This function identifies + * incomplete sequences at the end of a chunk to prevent them from being + * rendered as partial/broken control codes. + * + * @param chunk - Raw output string potentially containing ANSI sequences + * @returns Tuple of [body, carry] where: + * - body: Complete portion safe to render + * - carry: Incomplete sequence to prepend to next chunk + * + * @example + * const [body, carry] = splitAnsiCarryover("hello\x1b[31"); + * // body = "hello", carry = "\x1b[31" + */ function splitAnsiCarryover(chunk: string): [string, string] { if (!chunk) return ["", ""];