/** * The assistant's own output: reasoning, then reply, as it arrives. * * This module owns every line the assistant produces, both the streamed form and * the committed one, because they represent one response and only one * presentation may reach the screen. A reply arrives as deltas, is written into * scrollback one completed line at a time, and only its unfinished trailing line * stays in the live region. `assistant/message` then contributes what streaming * could not have shown — the last partial line, or the whole reply from a * provider that does not stream at all. The assembled assistant message is * authoritative, and streamed content normally corresponds to its prefix. The * only mismatch treated as presentation-equivalent is trailing whitespace at the * stream boundary, on either channel: the assembled block can drop a trailing * line break the deltas carried, and that whitespace is invisible in the rows * anyway. Substantive or internal divergence is not equivalent and falls back to * the assembled form. Native scrollback already committed from the stream cannot * be retracted. * * Committing as lines complete is what keeps the cost flat. Holding the whole * reply live meant re-escaping, re-splitting, and retransmitting all of it on * every delta, so an 11 KB answer cost 2.6 MB of terminal writes and grew * quadratically; a completed line is written once and never revisited, and the * live region stays one unfinished line regardless of how long the answer runs. * * Every string here came from a model, so every string is escaped before it is * returned. Reasoning and reply are separate channels because they are separate * content: the model's own working notes are styled apart from its answer, and * emitting the first reply delta is what marks the reasoning finished. * @module dshline/stream */ import type { ContentBlock } from '@deepseek-ai/dsh-llm'; /** * The two kinds of assistant output, in the order a model emits them. * * `reasoning` arrives first when the model produces any, and the first `text` * delta is the signal that it stopped — the log carries no separate marker. */ export type StreamChannel = 'reasoning' | 'text'; /** * Accumulates one assistant attempt's output and hands back lines to commit. * * One instance spans one model attempt: {@link settle} commits its remainder * against the durable `assistant/message` that ends it, and {@link reset} * returns the buffer to its initial state for the next one. * * There is deliberately no "commit whatever is pending" escape. Every attempt * settles durably — a visible reply as `assistant/message`, an interrupted * prefix as the same event with `interrupted: true`, and an attempt that * produced no reply at all as the log-only `assistant/attempt` — so an * unfinished line either belongs to a message that is about to settle it, or * belongs to no reply and must not be committed as one. */ export declare class StreamBuffer { private readonly channels; /** Whether reasoning rows are projected into the terminal. */ private reasoningVisible; /** Whether hidden reasoning has entered this turn's authoritative stream. */ private reasoningHadHiddenContent; /** The channel currently receiving deltas, or none before the first one. */ private current; /** * @param reasoningVisible - whether reasoning should be rendered initially. */ constructor(reasoningVisible?: boolean); /** * Change reasoning projection without dropping its reconciliation prefix. * * The received prefix stays in `pushed` so an assembled message can still settle * against every delta, while the unfinished presentation tail is discarded at * the visibility boundary. This prevents a later show from replaying hidden text * and prevents a later hide from leaving a live row behind. * @param visible - whether future reasoning should be rendered. */ setReasoningVisible(visible: boolean): void; /** * Take one delta and return whatever it completed. * * Only whole lines are committed. A delta that adds no newline changes the live * region alone, which is the common case and costs one short redraw. Hidden * reasoning still advances the received prefix, but contributes no rows. * @param channel - which kind of output this delta belongs to. * @param delta - the text fragment, exactly as the model sent it. * @param columns - the terminal's current width, for the gutter's hanging indent. * @returns rows to write into scrollback, in order. */ push(channel: StreamChannel, delta: string, columns: number): string[]; /** * Reconcile the streamed presentation with the assembled assistant message. * * The assembled assistant message is authoritative. Streamed content normally * corresponds to its prefix, so the assembled message contributes only the * remainder — the last unterminated line for a streamed reply, or the whole * reply for a provider that does not stream. A mismatch made only of trailing * whitespace at the stream boundary is presentation-equivalent on either * channel and contributes no duplicate rows. Substantive, internal, or other * content divergence is not equivalent and falls back to the authoritative * assembled form. Already committed native scrollback cannot be retracted, so * preserving the assembled form is safer than silently dropping it. * @param content - the assembled assistant message's content blocks. * @param columns - the terminal's current width. * @returns rows to write into scrollback, reasoning before reply. */ settle(content: readonly ContentBlock[], columns: number): string[]; /** Return to the initial state, discarding channel and block state. */ reset(): void; /** * Blank rows held for a channel, for tests that assert nothing is left pending. * @param channel - the channel to read. * @returns how many blank rows are held back. */ heldBlanks(channel: StreamChannel): number; /** * The live-region rows for the unfinished line, or none when there is none. * * While a turn is running this is the only moving part of the screen, so it is * deliberately small: everything already complete is in scrollback, above and * behind it. * @param columns - the terminal's current width. * @param maxRows - most physical rows the caller's live-region budget allows; * the newest rows are kept when the unfinished line is taller. A stream view * that ignored this would spend rows the composer below it still needs, and * on a short terminal would push the region past the screen, where the first * rows can neither be reached nor erased. * @returns rows for the live region. */ live(columns: number, maxRows?: number): string[]; /** * Commit one channel's remainder against the assembled text. * @param channel - the channel to settle. * @param full - the assembled text for that channel, possibly empty. * @param columns - the terminal's current width. * @returns rows to write into scrollback. */ private settleChannel; /** * Commit a channel's unfinished line, if it has one. * @param channel - the channel to flush. * @param columns - the terminal's current width. * @returns rows to write into scrollback. */ private flush; /** * Drop only the currently drawable remainder of a channel. * @param state - the channel whose presentation epoch is ending. */ private clearPresentation; /** * Style and gutter complete source lines. * @param channel - the channel they belong to. * @param sources - complete source lines, without newlines. * @param columns - the terminal's current width. * @returns rows to write into scrollback. */ private emit; /** * The gutter mark for one channel, styled. * @param channel - the channel. * @returns the styled mark, without its trailing space. */ private mark; } //# sourceMappingURL=stream.d.ts.map