Skip to content

fix(prompts): write streamed messages to the selected output - #3466

Open
SaKaNa-Y wants to merge 3 commits into
voidzero-dev:mainfrom
SaKaNa-Y:fix/prompts-stream-output
Open

SaKaNa-Y wants to merge 3 commits into
voidzero-dev:mainfrom
SaKaNa-Y:fix/prompts-stream-output

Conversation

@SaKaNa-Y

@SaKaNa-Y SaKaNa-Y commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A CLI can send logs to stderr and write a JSON result to stdout. When it passes stderr to stream.message, the message still goes to stdout, so a downstream command cannot parse the result.

stream.message(chunks, { output }) → write the symbol, chunks and newline to output

The renderer uses the selected output's width to wrap text. If the output has no column count, it writes consecutive chunks without adding line breaks between them. This also keeps words together when the caller uses the default stdout and redirects it to a file or pipe.

Evidence

  • Before the change, stdout contained both the log and JSON, and stderr was empty. With this change, the built package wrote ! hello world to redirected stderr and only {"ok":true} to stdout.
  • A regression test sends ['some', 'thing', ' else'] to a plain Node.js Writable. It reproduced the split word before the repair and now receives one line. A separate test checks wrapping at 10 columns and detects stray writes to stdout.
  • All 66 prompts tests passed.

Merge Danger

Door: Two-way.
Blast Radius: Streamed prompt messages with an explicit output or a redirected stdout.

Use the supplied stream for both writes and the terminal width. Keep stdout as the default destination.

Signed-off-by: Sakana <15715093608@163.com>
@SaKaNa-Y
SaKaNa-Y marked this pull request as ready for review November 16, 2026 13:43
Comment thread packages/prompts/src/stream.ts Outdated
const chunkLen = strip(chunk).length;
if (lineWidth + chunkLen < process.stdout.columns) {
const columns =
'columns' in output && typeof output.columns === 'number' ? output.columns : 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Use a usable width when the selected output has no columns

A plain Node.js Writable, or process.stderr redirected to a file or pipe, has no columns property. Falling back to 0 makes lineWidth + chunkLen < columns always false, so every chunk gets a leading newline and indentation, and trimStart() removes its leading whitespace. For example, stream.message(['hel', 'lo', ' world'], { output, symbol: '!' }) writes "! \n hel\n lo\n world\n" instead of "! hello world\n". This splits words at arbitrary chunk boundaries on the newly supported output destinations.

Please use getColumns(output) from @clack/core, as the other renderers do, or disable wrapping when width is unavailable. Add a regression case with a plain Writable that does not define columns; the new capture(10) test does not cover this case.

The default stdout path already had a similar issue without a terminal width; this finding concerns the incomplete support for custom outputs added here.

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>
Signed-off-by: Sakana <15715093608@163.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants