Skip to content

feat: replace ref-based messages with component-based message bubbles#555

Open
avoidwork wants to merge 8 commits into
mainfrom
feat/replace-ref-based-messages-with-component-message-bubbles
Open

feat: replace ref-based messages with component-based message bubbles#555
avoidwork wants to merge 8 commits into
mainfrom
feat/replace-ref-based-messages-with-component-message-bubbles

Conversation

@avoidwork

Copy link
Copy Markdown
Owner

Summary

Replace the ref-based message array architecture in the TUI with a component-based system where each message is a standalone MessageBubble component managing its own state.

Changes

  • Created src/tui/messageBubble.js: MessageBubble component with internal state management (content, streaming, toolCallDisplay, reasoningContent). Exposes update() method for streaming events.
  • Created src/tui/messageList.js: Manages array of MessageBubble instances with addMessage(), updateMessage(), clear() methods. Handles MAX_RENDER_MESSAGES windowing and scroll-to-bottom.
  • Updated src/tui/conversationPanel.js: Simplified to thin wrapper rendering MessageList.
  • Updated src/tui/app.js: Removed messagesRef, forceRender, renderCount. Uses MessageList instance.
  • Updated tests for new component API.

Motivation

The current architecture uses messagesRef (mutable array) + forceRender (useState counter) + array spreading + useEffect scroll-to-bottom. This fights React's model and causes memory leaks, unnecessary re-renders, and unreliable scroll management.

Fixes #541

Replace the ref-based message array with modular MessageBubble and
MessageList components for better state management and readability.

- Add messageBubble.js: memo-wrapped component with imperative update()
  API via useImperativeHandle for streaming content updates
- Add messageList.js: container managing bubble instances, imperative
  APIs (addMessage, updateMessage, clear, setMessages), scroll
  throttle, and auto-scroll behavior
- Refactor conversationPanel.js: thin wrapper delegating to MessageList
- Refactor app.js: remove messages array state, forceRender, and manual
  scroll logic, delegate entirely to messageListRef imperatives
Implement pub/sub architecture where each MessageBubble subscribes to
its own topic (msg-{id}). MessageList publishes streaming updates that
bubble catches and appends its own chunks array, triggering independent
re-renders without parent re-render. This solves the streaming gap bug
where app.js called updateMessage() but the bubble never updated mid-stream.

- Add createPubSub() factory in messageBubble.js for testable pub/sub
- Add PubSubContext for React context propagation
- Replace forceRenderKeyRef in MessageList with topicsRef + subscribe/publish
- MessageBubble uses useContext + useEffect subscription with dedup
- Add 12 pubsub unit tests and 11 messageListApi simulation tests
@avoidwork

Copy link
Copy Markdown
Owner Author

Pub/sub fix committed (5a27498):

  • MessageBubble now uses to subscribe to its own topic ()
  • Each pub/sub update appends to the bubble's state → re-renders just that bubble
  • Deduplication: identical consecutive content chunks are skipped (no duplicate renders)
  • PubSubContext provided by MessageList via context, scoped to this tree only
  • createPubSub() factory exported for standalone testing (12 pubsub tests + 11 messageListApi simulation tests)

This solves the streaming gap bug where called but the bubble never updated mid-stream because mutating refs doesn't trigger React reconciliation.

Spread syntax requires children to be an array, but React children can
be a single element, null, or other non-array types. Wrap with
toArray() to guarantee an iterable array before spreading.
The TUI message bubble refactoring was implemented with a pub/sub topic
system instead of the original forwardRef + useImperativeHandle ref callback
approach. This simplifies the architecture by eliminating the need for
MessageList to track bubble instance refs.

- Replaced ref callback pattern with unique pub/sub topics per message
- MessageBubble uses chunk accumulation via useState instead of streamingId
- Scroll ref accessed via getScrollRef() instead of prop forwarding
- Added PubSub API requirements and scenarios to specs
- Updated design doc with pub/sub rationale and trade-offs
Remove the inline memo-wrapped MessageBubble component and its
associated imports from conversationPanel.js. The module now
delegates entirely to the component-based MessageList with pub/sub.

Update conversationPanel.test.js and tui.test.js to remove stale
renderMessages tests and replace them with MessageList integration tests.

- Removed MessageBubble memo wrapper and related imports
- Removed renderMessages utility and associated tests
- 302 lines deleted, 61 added
Remove the inline memo-wrapped MessageBubble component and its
associated imports from conversationPanel.js. The module now
delegates entirely to the component-based MessageList with pub/sub.

Update conversationPanel.test.js and tui.test.js to remove stale
renderMessages tests and replace them with MessageList integration tests.

- Removed MessageBubble memo wrapper and related imports
- Removed renderMessages utility and associated tests
- 302 lines deleted, 61 added
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.

feat: replace ref-based messages with component-based message bubbles

1 participant