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
10 changes: 9 additions & 1 deletion src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class TextOutputFormatter implements OutputFormatter {
}
case "agent_thought_chunk": {
if (update.content.type === "text") {
this.thoughtBuffer += update.content.text;
this.writeThoughtChunk(update.content.text);
}
return;
}
Expand Down Expand Up @@ -655,6 +655,14 @@ class TextOutputFormatter implements OutputFormatter {
this.write(text);
}

private writeThoughtChunk(text: string): void {
if (!text) {
return;
}
this.beginSection("thought");
this.writeLine(this.dim(`[thinking] ${truncate(collapseWhitespace(text), MAX_THOUGHT_CHARS)}`));
}

private flushThoughtBuffer(): void {
const thought = truncate(collapseWhitespace(this.thoughtBuffer), MAX_THOUGHT_CHARS);
this.thoughtBuffer = "";
Expand Down
10 changes: 7 additions & 3 deletions test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ function doneResult(stopReason: string): unknown {
};
}

test("text formatter batches thought chunks from ACP notifications", () => {
test("text formatter streams thought chunks from ACP notifications immediately", () => {
const writer = new CaptureWriter();
const formatter = createOutputFormatter("text", { stdout: writer });

formatter.onAcpMessage(thoughtChunk("Investigating ") as never);
assert.match(writer.toString(), /\[thinking\] Investigating/);

formatter.onAcpMessage(thoughtChunk("the issue") as never);
assert.equal((writer.toString().match(/\[thinking\]/g) ?? []).length, 2);

formatter.onAcpMessage(messageChunk("Done.") as never);
formatter.onAcpMessage(doneResult("end_turn") as never);

const output = writer.toString();
assert.equal((output.match(/\[thinking\]/g) ?? []).length, 1);
assert.match(output, /\[thinking\] Investigating the issue/);
assert.match(output, /\[thinking\] Investigating/);
assert.match(output, /\[thinking\] the issue/);
assert.match(output, /\[done\] end_turn/);
});

Expand Down