[BUG] Related-question click during streaming corrupts useChat state, leaves input stuck disabled
Is there an existing issue for this?
- [X] I have searched the existing issues
Vercel Runtime Logs
- [ ] I have checked the Vercel Runtime Logs for errors (if applicable)
Current Behavior
When the assistant is mid-stream and the user clicks a follow-up "Related" question rendered by a spec block, the second sendMessage is dispatched while useChat is still processing the first one. The two streams interleave and corrupt the hook's internal state. Visible symptoms after both responses complete:
- The chat input stays disabled for ~5–10 seconds after the final token arrives.
- The devtools console emits one or more React warnings:
Encountered two children with the same key, '<messageId>'(one warning per re-render ofchat-messages.tsx, often 10+). - The next render path throws
TypeError: Cannot read properties of undefined (reading 'state')from a tool part (search-section.tsx/dynamic-tool-display.tsx) because the second assistant message reaches the React tree with a half-populatedtoolpart.
Expected Behavior
Clicking a Related button while a response is in flight should be a no-op (or visually disabled), not start a second concurrent dispatch. The input should re-enable as soon as the response is finished.
Steps To Reproduce
- Configure any working AI provider so a chat actually responds.
- Ask any question whose response includes Related follow-up buttons.
- While the assistant is still streaming, click one of the Related buttons.
- Immediately click a second Related button.
- Wait for both responses to finish.
- Observe the symptoms above (stuck input, dup-key warnings, console TypeError).
Environment
- OS: macOS 14
- Browser: Chrome 138
- Morphic: current
main(commit6df470c) - Node / Bun: bun 1.2.12
- AI SDK:
[email protected],@ai-sdk/[email protected]
Anything else?
Root cause — components/spec-block.tsx calls chatContext.sendMessage(...) directly with no guard; it does not check whether useChat is currently busy. Firing a second dispatch mid-stream is what breaks the hook.
Why a simple useChat.status guard is not enough — I tried that locally and discovered two follow-on traps:
useChat.statusdoes not reliably transition back to'ready'once a stream contains tool calls or spec blocks. It can stay'streaming'indefinitely from the consumer's perspective, so a status-based guard rejects clicks that should be allowed.- The
handlersprop in@json-render/react'sActionProvideris stored asuseState(initialHandlers)(seenode_modules/@json-render/react/dist/index.js:460) and never refreshed. Any closure captured at mount time freezes itschatContextsnapshot, so a React-state value read through that closure stays stale forever.
A working shape we tested locally:
chat.tsxmaintains a localisStreamingRef: MutableRefObject<boolean>— set totrueinside a wrappedsafeSendMessage, cleared insideuseChat'sonFinish/onError.ChatContextexposes the ref (not a boolean) so spec-block's frozen closure can still read.currentand always see the latest value.spec-block.tsxcheckschatContext.isStreamingRef.currentand shows a toast / no-ops while a response is in flight.
Happy to send a PR with that shape if it sounds right, but it does touch the ChatContext API so I wanted to check whether a different design (e.g. a disabled prop on the Related button itself, or a server-side check) would fit the project better.
PR #853 implements the fix described above (MutableRefObject<boolean> exposed through ChatContext). Happy to rework the API shape if you prefer a different design — see the open design question at the bottom of the PR.