Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ function collapseWhitespace(value: string): string {
return value.replace(/\s+/g, " ").trim();
}

function normalizeLineEndings(value: string): string {
return value.replace(/\r\n?/g, "\n");
}

function truncate(value: string, maxChars: number): string {
if (value.length <= maxChars) {
return value;
Expand Down Expand Up @@ -656,14 +660,18 @@ class TextOutputFormatter implements OutputFormatter {
}

private flushThoughtBuffer(): void {
const thought = truncate(collapseWhitespace(this.thoughtBuffer), MAX_THOUGHT_CHARS);
const thought = truncate(normalizeLineEndings(this.thoughtBuffer).trim(), MAX_THOUGHT_CHARS);
this.thoughtBuffer = "";
if (!thought) {
return;
}

this.beginSection("thought");
this.writeLine(this.dim(`[thinking] ${thought}`));
const [firstLine, ...restLines] = thought.split("\n");
this.writeLine(this.dim(`[thinking] ${firstLine}`));
for (const line of restLines) {
this.writeLine(this.dim(` ${line}`));
}
}

private renderToolUpdate(update: ToolCall | ToolCallUpdate): void {
Expand Down
12 changes: 12 additions & 0 deletions test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ test("text formatter batches thought chunks from ACP notifications", () => {
assert.match(output, /\[done\] end_turn/);
});

test("text formatter preserves line breaks in thought chunks", () => {
const writer = new CaptureWriter();
const formatter = createOutputFormatter("text", { stdout: writer });

formatter.onAcpMessage(thoughtChunk("Line one\n\nLine two") as never);
formatter.onAcpMessage(doneResult("end_turn") as never);

const output = writer.toString();
assert.match(output, /\[thinking\] Line one\n\s*\n\s*Line two/);
assert.doesNotMatch(output, /\[thinking\] Line one Line two/);
});

test("text formatter renders tool call lifecycle from ACP updates", () => {
const writer = new CaptureWriter();
const formatter = createOutputFormatter("text", { stdout: writer });
Expand Down