feat: stream ai text output as tokens arrive#43
Open
tmchow wants to merge 2 commits into
Open
Conversation
Switches single-model interactive `ai text` runs to `streamText` so tokens appear in the terminal as they're generated. The fast path is gated on `models.length === 1 && countPerModel === 1 && !output && !AI_CLI_OUTPUT_DIR && !json && process.stdout.isTTY` -- multi-model, capture, --json, and piped stdout all stay on the existing `generateText` + `runJobs` path. The progress line moves to stderr in the streaming path so it doesn't pollute the streamed stdout. `-q` suppresses it (matching existing behavior). README and skills/ai-cli/SKILL.md updated. Tests cover both the streaming fast path and the buffered --json fallback.
streamText's textStream only yields text deltas; provider errors are emitted as error parts that the for-await loop silently ignores. Without this, a failed generation prints "Generated text..." and exits 0. Awaiting result.finishReason after the loop propagates any swallowed error as an exception, which the top-level commander catch handles (prints "Error: ..." to stderr, exits 1). Adds a regression test that mocks streamText with a rejecting finishReason and asserts the error surfaces.
|
@tmchow is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
@ctate any feedback on this? Happy to adjust it if I should approach another way. Lmk! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ai textcurrently waits for the full response fromgenerateTextbefore printing. On a one-sentence prompt that's a 1s blank terminal so not a big deal, but on a long-form generation it's much worse.AI SDK 6 already ships
streamText. This PR uses it for the simple single-model interactive case so tokens appear in the terminal as they arrive.Why this matters
The "agent-native CLI" tagline implies responsive feedback in interactive terminals. Buffered text feels broken on long prompts even when it's fast. Every comparable tool streams: MMX-CLI has
--stream, aichat is built around streaming, Charm's mods streams by default. Streaming is also strictly cheaper for the user, since they can ctrl-C as soon as a response is going off track instead of paying for the full generation.Demo
Live timing breakdown from a 5-sentence prompt against
openai/gpt-4.1-mini(captured via PTY so stdout is treated as a TTY). Thought it'd be helpful to see the breakdown and not just the gif.154 small chunks over 1.7 seconds, instead of one ~2-second blank-then-dump.
Testing
Manual: streaming verified live against
openai/gpt-4.1-miniandanthropic/claude-3.5-haikuvia AI Gateway.--json, multi-model, and piped paths confirmed unchanged via PTY-instrumented runs.