@@ -2,21 +2,12 @@ import * as c from "yoctocolors";
22 import { renderLine } from "./markdown.ts";
33 import { G, write, writeln } from "./output.ts";
44 import type { Spinner } from "./spinner.ts";
5-import {
6- buildClearPartialPreview,
7- createPartialPreviewTracker,
8- resetPartialPreviewTracker,
9- setStreamPreviewPrefix,
10- streamPartialPreviewDelta,
11-} from "./stream-preview.ts";
12-import { terminal } from "./terminal-io.ts";
135
146 export class StreamRenderContent {
157 private inText = false;
168 private inReasoning = false;
179 private rawBuffer = "";
18- private styledPrefix = "";
19- private partialPreview = createPartialPreviewTracker();
10+ private streamedChars = 0;
2011 private inFence = false;
2112 private reasoningBlankLineRun = 0;
2213 private accumulatedText = "";
@@ -43,15 +34,12 @@ export class StreamRenderContent {
4334 if (!this.inText) {
4435 this.spinner.stop();
4536 write(`${G.reply} `);
46- setStreamPreviewPrefix(this.partialPreview, `${G.reply} `, true);
47- this.styledPrefix = `${G.reply} `;
4837 this.inText = true;
4938 }
5039 this.rawBuffer += delta;
5140 this.accumulatedText += delta;
52-
5341 this.flushCompleteLines();
54- this.streamPartialLine();
42+ this.streamPartialRemainder();
5543 }
5644
5745 appendReasoningDelta(delta: string, showReasoning: boolean): void {
@@ -66,50 +54,22 @@ export class StreamRenderContent {
6654 writeln(`${G.info} ${c.dim("reasoning")}`);
6755 this.inReasoning = true;
6856 this.inFence = false;
69- setStreamPreviewPrefix(this.partialPreview, " ", false);
70- this.styledPrefix = "";
7157 }
7258 this.rawBuffer += delta;
7359 this.flushCompleteLines();
74- this.streamPartialLine();
60+ this.streamPartialRemainder();
7561 }
7662
7763 flushOpenContent(): void {
7864 if (!this.inText && !this.inReasoning) return;
79-
80- if (this.rawBuffer) {
81- this.spinner.stop();
82- const raw = this.rawBuffer;
83- const out = this.renderSingleLine(raw);
84- this.rawBuffer = "";
85- if (this.partialPreview.partialWritten > 0) {
86- if (!this.canKeepStreamedPartial(raw, out)) {
87- const clearSeq = buildClearPartialPreview(
88- this.partialPreview,
89- terminal.stdoutColumns,
90- );
91- if (out !== null) write(`${clearSeq}${this.styledPrefix}${out}`);
92- else write(clearSeq);
93- }
94- } else if (out !== null) {
95- write(out);
96- }
97- }
65+ this.streamPartialRemainder();
9866 writeln();
9967 this.inText = false;
10068 this.inReasoning = false;
10169 this.inFence = false;
10270 this.reasoningBlankLineRun = 0;
103- this.styledPrefix = "";
104- this.resetLineState();
105- }
106-
107- private canKeepStreamedPartial(raw: string, out: string | null): boolean {
108- return (
109- this.inText &&
110- out === raw &&
111- this.partialPreview.partialWritten === raw.length
112- );
71+ this.rawBuffer = "";
72+ this.streamedChars = 0;
11373 }
11474
11575 private renderSingleLine(raw: string): string | null {
@@ -129,55 +89,46 @@ export class StreamRenderContent {
12989 return rendered.output;
13090 }
13191
132- private resetLineState(): void {
133- resetPartialPreviewTracker(this.partialPreview);
134- }
135-
13692 private flushCompleteLines(): void {
137- let start = 0;
138- let boundary = this.rawBuffer.indexOf("\n", start);
93+ let boundary = this.rawBuffer.indexOf("\n");
13994 if (boundary === -1) return;
140-
14195 this.spinner.stop();
142- let batchOutput = "";
143- let firstLine = true;
14496
14597 while (boundary !== -1) {
146- const raw = this.rawBuffer.slice(start, boundary);
147- start = boundary + 1;
148- boundary = this.rawBuffer.indexOf("\n", start);
149-
150- const out = this.renderSingleLine(raw);
151- if (firstLine && this.partialPreview.partialWritten > 0) {
152- if (this.canKeepStreamedPartial(raw, out)) {
153- batchOutput += "\n";
154- } else {
155- const clearSeq = buildClearPartialPreview(
156- this.partialPreview,
157- terminal.stdoutColumns,
158- );
159- if (out !== null) {
160- batchOutput += `${clearSeq}${this.styledPrefix}${out}\n`;
161- } else {
162- batchOutput += `${clearSeq}\n`;
163- }
98+ const raw = this.rawBuffer.slice(0, boundary);
99+ const rendered = this.renderSingleLine(raw);
100+ const streamedForLine = Math.min(this.streamedChars, raw.length);
101+
102+ if (streamedForLine > 0) {
103+ if (streamedForLine < raw.length) {
104+ this.writePartial(raw.slice(streamedForLine));
164105 }
165- } else if (out !== null) {
166- batchOutput += `${out}\n`;
106+ writeln();
107+ } else {
108+ if (rendered !== null) writeln(rendered);
109+ else writeln();
167110 }
168- firstLine = false;
169- }
170111
171- this.rawBuffer = start > 0 ? this.rawBuffer.slice(start) : this.rawBuffer;
172- this.styledPrefix = "";
173- this.resetLineState();
174- if (batchOutput) write(batchOutput);
112+ this.rawBuffer = this.rawBuffer.slice(boundary + 1);
113+ this.streamedChars = Math.max(0, this.streamedChars - raw.length);
114+ boundary = this.rawBuffer.indexOf("\n");
115+ }
175116 }
176117
177- private streamPartialLine(): void {
178- const out = streamPartialPreviewDelta(this.partialPreview, this.rawBuffer);
179- if (!out) return;
118+ private streamPartialRemainder(): void {
119+ if (this.rawBuffer.length <= this.streamedChars) return;
180120 this.spinner.stop();
181- write(out);
121+ this.writePartial(this.rawBuffer.slice(this.streamedChars));
122+ this.streamedChars = this.rawBuffer.length;
123+ }
124+
125+ private writePartial(text: string): void {
126+ if (!text) return;
127+ if (this.inReasoning) {
128+ const prefix = this.streamedChars === 0 ? " " : "";
129+ write(`${prefix}${c.dim(c.italic(text))}`);
130+ return;
131+ }
132+ write(text);
182133 }
183134 }