@@ -3,8 +3,8 @@ import type { CoreMessage } from "../llm-api/turn.ts";
33 import type { TurnEvent } from "../llm-api/types.ts";
44 import { logError } from "./error-log.ts";
55 import { parseAppError } from "./error-parse.ts";
6-import { renderChunk } from "./markdown.ts";
7-import { G, writeln } from "./output.ts";
6+import { renderLine } from "./markdown.ts";
7+import { G, write, writeln } from "./output.ts";
88 import type { Spinner } from "./spinner.ts";
99 import { renderToolCall, renderToolResult } from "./tool-render.ts";
1010
@@ -20,76 +20,36 @@ export async function renderTurn(
2020 let inText = false;
2121 let rawBuffer = "";
2222 let inFence = false;
23- let printQueue = "";
24- let printPos = 0;
25- let tickerHandle: ReturnType<typeof setTimeout> | null = null;
2623
2724 let inputTokens = 0;
2825 let outputTokens = 0;
2926 let contextTokens = 0;
3027 let newMessages: CoreMessage[] = [];
3128
32- function enqueuePrint(ansi: string): void {
33- printQueue += ansi;
34- if (tickerHandle === null) {
35- scheduleTick();
36- }
37- }
38-
39- function tick(): void {
40- tickerHandle = null;
41- if (printPos < printQueue.length) {
42- process.stdout.write(printQueue[printPos] as string);
43- printPos++;
44- scheduleTick();
45- } else {
46- printQueue = "";
47- printPos = 0;
48- }
49- }
50-
51- function scheduleTick(): void {
52- tickerHandle = setTimeout(tick, 8);
53- }
54-
55- function drainQueue(): void {
56- if (tickerHandle !== null) {
57- clearTimeout(tickerHandle);
58- tickerHandle = null;
59- }
60- if (printPos < printQueue.length) {
61- process.stdout.write(printQueue.slice(printPos));
62- }
63- printQueue = "";
64- printPos = 0;
65- }
66-
67- function renderAndTrim(end: number): void {
68- const chunk = rawBuffer.slice(0, end);
69- const rendered = renderChunk(chunk, inFence);
29+ function renderAndWrite(raw: string, endWithNewline: boolean): void {
30+ const rendered = renderLine(raw, inFence);
7031 inFence = rendered.inFence;
71- enqueuePrint(rendered.output);
72- rawBuffer = rawBuffer.slice(end);
32+ write(rendered.output);
33+ if (endWithNewline) {
34+ write("\n");
35+ }
7336 }
7437
75- function flushChunks(): void {
76- let boundary = rawBuffer.indexOf("\n\n");
77- if (boundary !== -1) {
78- renderAndTrim(boundary + 2);
79- flushChunks();
80- return;
81- }
82- boundary = rawBuffer.lastIndexOf("\n");
83- if (boundary !== -1) {
84- renderAndTrim(boundary + 1);
38+ function flushCompleteLines(): void {
39+ let boundary = rawBuffer.indexOf("\n");
40+ while (boundary !== -1) {
41+ renderAndWrite(rawBuffer.slice(0, boundary), true);
42+ rawBuffer = rawBuffer.slice(boundary + 1);
43+ boundary = rawBuffer.indexOf("\n");
8544 }
8645 }
8746
8847 function flushAll(): void {
89- if (rawBuffer) {
90- renderAndTrim(rawBuffer.length);
48+ if (!rawBuffer) {
49+ return;
9150 }
92- drainQueue();
51+ renderAndWrite(rawBuffer, false);
52+ rawBuffer = "";
9353 }
9454
9555 for await (const event of events) {
@@ -97,11 +57,11 @@ export async function renderTurn(
9757 case "text-delta": {
9858 if (!inText) {
9959 spinner.stop();
100- process.stdout.write(`${G.reply} `);
60+ write(`${G.reply} `);
10161 inText = true;
10262 }
10363 rawBuffer += event.delta;
104- flushChunks();
64+ flushCompleteLines();
10565 break;
10666 }