@@ -1,24 +1,32 @@
11 import * as c from "yoctocolors";
2-import { G, writeln } from "./output.ts";
2+import { G, write, writeln } from "./output.ts";
3+
4+const ITALIC_ON = "\x1b[3m";
5+const ITALIC_OFF = "\x1b[23m";
6+
7+function styleReasoningText(text: string): string {
8+ return `${ITALIC_ON}${c.dim(text)}${ITALIC_OFF}`;
9+}
310
411 export class LiveReasoningBlock {
512 private blockOpen = false;
6- private pending = "";
13+ private lineOpen = false;
714
815 append(delta: string): void {
916 if (!delta) return;
1017 this.openBlock();
11- this.pending += delta;
12- this.flushCompleteLines();
18+ const lines = delta.split("\n");
19+ for (const [index, line] of lines.entries()) {
20+ if (line) this.writeText(line);
21+ if (index < lines.length - 1) this.endLine();
22+ }
1323 }
1424
1525 finish(): void {
1626 if (!this.blockOpen) return;
17- if (this.pending.length > 0) {
18- this.writeLine(this.pending);
19- this.pending = "";
20- }
27+ if (this.lineOpen) writeln();
2128 this.blockOpen = false;
29+ this.lineOpen = false;
2230 }
2331
2432 private openBlock(): void {
@@ -27,16 +35,17 @@ export class LiveReasoningBlock {
2735 this.blockOpen = true;
2836 }
2937
30- private flushCompleteLines(): void {
31- let boundary = this.pending.indexOf("\n");
32- while (boundary !== -1) {
33- this.writeLine(this.pending.slice(0, boundary));
34- this.pending = this.pending.slice(boundary + 1);
35- boundary = this.pending.indexOf("\n");
38+ private writeText(text: string): void {
39+ if (!this.lineOpen) {
40+ write(" ");
41+ this.lineOpen = true;
3642 }
43+ write(styleReasoningText(text));
3744 }
3845
39- private writeLine(line: string): void {
40- writeln(` ${c.dim(line)}`);
46+ private endLine(): void {
47+ if (!this.lineOpen) write(" ");
48+ writeln();
49+ this.lineOpen = false;
4150 }
4251 }