Search⌘ K
AI Features

Stop and Regenerate: Rules for Controls and State

Explore how to implement Stop as a state transition that freezes the current AI assistant message and makes it immutable, while Regenerate starts a new turn preserving prior messages. Learn control rules for enabling or disabling Send, Stop, and Regenerate buttons to avoid interleaved or duplicated content and maintain a stable and predictable user interface during streaming output.

Rendering deltas smoothly still assumes the stream just runs to completion or gets cut off by the network. The next problem is what happens when the user, not the network, decides to stop or restart. A streaming chat UI fails in a specific way when Stop looks responsive but the transcript keeps changing. The user clicks Stop, the button flips, and a status line says Stopping, yet new tokens still append for a moment and the assistant message continues to mutate. A second failure appears when the user clicks Regenerate quickly and the UI ends up with two assistant messages that both receive streamed deltas, so the transcript shows interleaved or duplicated content.

Those symptoms do not require a broken cancellation mechanism. They usually come from missing UI state guards, so late stream events still apply, or from unclear control rules that allow a second action while the first turn is still mutable. The constraints for this design are a streaming task, immediate perceived response to user actions, a predictable control surface under keyboard use, and a hard requirement that the transcript stays immutable once a turn is stopped or replaced. The rest of this lesson treats network abort as best effort and focuses on reducer level rules that ignore late events.

Stop is a state transition, not a request

Stop needs a ...