fix(ai): normalize AG-UI snapshot messages to UIMessage[] in MESSAGES_SNAPSHOT handler#698
fix(ai): normalize AG-UI snapshot messages to UIMessage[] in MESSAGES_SNAPSHOT handler#698amitsaroj wants to merge 2 commits into
Conversation
…_SNAPSHOT handler
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReplaces unsafe casting of MESSAGES_SNAPSHOT payloads by importing modelMessageToUIMessage and mapping snapshot messages: reuse messages with parts; convert messages without parts (using existing or generated ids) so downstream code can rely on message.parts. ChangesSafe MESSAGES_SNAPSHOT handling
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/ai/src/activities/chat/stream/processor.ts`:
- Around line 878-882: The snapshot branch is overwriting incoming message IDs
by always calling modelMessageToUIMessage with generateMessageId(); update the
mapping so when converting non-UI messages you preserve the original msg.id and
only generate a new id if msg.id is missing—i.e., in the chunk.messages mapping
(this.messages assignment) pass msg.id (or msg['id']) as the id argument to
modelMessageToUIMessage when present, otherwise call generateMessageId(); this
keeps existing message identity used by ensureAssistantMessage,
TEXT_MESSAGE_CONTENT, toolCallToMessage, and messageStates.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1d6f8123-d73d-4983-91d7-fbe5fe45edfa
📒 Files selected for processing (1)
packages/ai/src/activities/chat/stream/processor.ts
|
Thanks for the review @coderabbitai — great catch on the ID preservation issue. The second commit ( This ensures downstream event correlation (e.g. |
|
Tip For best results, initiate chat on the files or code changes.
The fix is well-reasoned. 🐇✨ |
🎯 Changes
Fixes #688
Root cause
handleMessagesSnapshotEventinprocessor.tswas doing an unsafe type cast:AG-UI
MESSAGES_SNAPSHOTmessages have the shape{ id, role, content }— they do not have thepartsarray thatUIMessagerequires. Theas Array<UIMessage>cast masked this at compile time.When any downstream code called
message.parts.find(...)— for example, theonToolCallStateChangedevtools handler inchat-client.ts— it crashed with:Fix
Normalize each message through
modelMessageToUIMessagewhen it does not already haveparts:This mirrors the pattern already used in
normalizeToUIMessage(used byChatClient.append) and is consistent with how all other message ingestion paths work in this codebase.✅ Checklist
pnpm run test:pr.🚀 Release Impact
🧪 Test Plan
MESSAGES_SNAPSHOTevent (e.g., using an AG-UI agent backend that emits snapshot events).TypeError: Cannot read properties of undefined (reading 'find')is thrown.packages/ai/tests/stream-processor.test.tsforMESSAGES_SNAPSHOT resets transient statecover the core normalization path.Summary by CodeRabbit