From 0fd767abd093539dfe2fe3e07696b95d24fea14d Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Mon, 16 Nov 2026 14:15:40 +0800 Subject: [PATCH 1/3] fix(prompts): write streamed messages to the selected output Use the supplied stream for both writes and the terminal width. Keep stdout as the default destination. Signed-off-by: Sakana <15715093608@163.com> --- packages/prompts/src/__tests__/output.spec.ts | 8 ++++++++ packages/prompts/src/stream.ts | 19 ++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/prompts/src/__tests__/output.spec.ts b/packages/prompts/src/__tests__/output.spec.ts index b814cdbc7f..06260915ec 100644 --- a/packages/prompts/src/__tests__/output.spec.ts +++ b/packages/prompts/src/__tests__/output.spec.ts @@ -115,6 +115,14 @@ describe('output identity', () => { } }); + it('writes the whole streamed message using the selected output and its width', async () => { + const c = capture(10); + const write = vi.spyOn(process.stdout, 'write').mockReturnValue(true); + await stream.message(['first', 'second'], { output: c.output, symbol: '!' }); + expect(c.text()).toBe('! first\n second\n'); + expect(write).not.toHaveBeenCalled(); + }); + it('uses one space in animated spinner frames', () => { vi.stubEnv('CI', ''); vi.useFakeTimers(); diff --git a/packages/prompts/src/stream.ts b/packages/prompts/src/stream.ts index 15dfa2da60..09f4295092 100644 --- a/packages/prompts/src/stream.ts +++ b/packages/prompts/src/stream.ts @@ -7,17 +7,12 @@ import type { LogMessageOptions } from './log.js'; const prefix = ' '; -// TODO (43081j): this currently doesn't support custom `output` writable -// because we rely on `columns` existing (i.e. `process.stdout.columns). -// -// If we want to support `output` being passed in, we will need to use -// a condition like `if (output instance Writable)` to check if it has columns export const stream = { message: async ( iterable: Iterable | AsyncIterable, - { symbol = '' }: LogMessageOptions = {}, + { symbol = '', output = process.stdout }: LogMessageOptions = {}, ) => { - process.stdout.write(symbol ? `${symbol} ` : ''); + output.write(symbol ? `${symbol} ` : ''); const initialWidth = symbol ? 2 : 0; let lineWidth = initialWidth; for await (let chunk of iterable) { @@ -26,15 +21,17 @@ export const stream = { lineWidth = initialWidth + strip(chunk.slice(chunk.lastIndexOf('\n'))).length; } const chunkLen = strip(chunk).length; - if (lineWidth + chunkLen < process.stdout.columns) { + const columns = + 'columns' in output && typeof output.columns === 'number' ? output.columns : 0; + if (lineWidth + chunkLen < columns) { lineWidth += chunkLen; - process.stdout.write(chunk); + output.write(chunk); } else { - process.stdout.write(`\n${prefix}${chunk.trimStart()}`); + output.write(`\n${prefix}${chunk.trimStart()}`); lineWidth = initialWidth + strip(chunk.trimStart()).length; } } - process.stdout.write('\n'); + output.write('\n'); }, info: (iterable: Iterable | AsyncIterable) => { return stream.message(iterable, { symbol: color.blue(S_INFO) }); From e5714a0496c17325dc0453b0fc6af6803299744a Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Tue, 17 Nov 2026 19:26:26 +0800 Subject: [PATCH 2/3] fix(prompts): keep streamed chunks together on widthless outputs A plain Writable and a redirected standard stream have no column count. Skip width-based wrapping on these destinations so chunk boundaries preserve words and leading whitespace. Signed-off-by: Sakana <15715093608@163.com> --- packages/prompts/src/__tests__/output.spec.ts | 12 ++++++++++++ packages/prompts/src/stream.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/prompts/src/__tests__/output.spec.ts b/packages/prompts/src/__tests__/output.spec.ts index 06260915ec..90a2d9bddf 100644 --- a/packages/prompts/src/__tests__/output.spec.ts +++ b/packages/prompts/src/__tests__/output.spec.ts @@ -123,6 +123,18 @@ describe('output identity', () => { expect(write).not.toHaveBeenCalled(); }); + it('keeps streamed chunks together when the output has no columns', async () => { + let content = ''; + const output = new Writable({ + write(chunk, _, done) { + content += chunk.toString(); + done(); + }, + }); + await stream.message(['hel', 'lo', ' world'], { output, symbol: '!' }); + expect(content).toBe('! hello world\n'); + }); + it('uses one space in animated spinner frames', () => { vi.stubEnv('CI', ''); vi.useFakeTimers(); diff --git a/packages/prompts/src/stream.ts b/packages/prompts/src/stream.ts index 09f4295092..7d8138e37c 100644 --- a/packages/prompts/src/stream.ts +++ b/packages/prompts/src/stream.ts @@ -22,7 +22,7 @@ export const stream = { } const chunkLen = strip(chunk).length; const columns = - 'columns' in output && typeof output.columns === 'number' ? output.columns : 0; + 'columns' in output && typeof output.columns === 'number' ? output.columns : Infinity; if (lineWidth + chunkLen < columns) { lineWidth += chunkLen; output.write(chunk); From 417d2caa4b7acdaa8594de0521aa07dee0da4cf7 Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Tue, 17 Nov 2026 19:31:23 +0800 Subject: [PATCH 3/3] test(prompts): use valid words in the stream chunk regression Signed-off-by: Sakana <15715093608@163.com> --- packages/prompts/src/__tests__/output.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/prompts/src/__tests__/output.spec.ts b/packages/prompts/src/__tests__/output.spec.ts index 90a2d9bddf..237b08e4ea 100644 --- a/packages/prompts/src/__tests__/output.spec.ts +++ b/packages/prompts/src/__tests__/output.spec.ts @@ -131,8 +131,8 @@ describe('output identity', () => { done(); }, }); - await stream.message(['hel', 'lo', ' world'], { output, symbol: '!' }); - expect(content).toBe('! hello world\n'); + await stream.message(['some', 'thing', ' else'], { output, symbol: '!' }); + expect(content).toBe('! something else\n'); }); it('uses one space in animated spinner frames', () => {