Repository navigation
Feat: When a Wait Node precedes a Message Node within a Loop Node, the outgoing message is split into two separate messages. - #14839
Conversation
…e outgoing message is split into two separate messages.
📝 WalkthroughWalkthroughThis PR corrects a typo in the chat-logic hook name, removes an unused canvasId parameter, and adds message-splitting functionality with wait-suffix tracking. The chat awaiting logic now sends form messages without agent_id when shared, and incoming assistant messages can be split into answer and wait-state entries for rendering. ChangesAgent Chat Message Splitting and Hook Refactoring
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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
🧹 Nitpick comments (1)
web/src/pages/agent/hooks/use-cache-chat-log.ts (1)
15-16: ⚡ Quick winPrefer string methods over RegExp constructor for simple static patterns.
The static analysis warning flags a potential ReDoS risk. While the risk is negligible here (the pattern is simple and constant), constructing a
RegExpon every call is less efficient and less readable than using string methods.♻️ Proposed alternatives
Option 1: Use string methods (recommended for simplicity)
-const resolveMessageId = (messageId: string) => - messageId?.replace(new RegExp(`${MessageWaitSuffix}$`), ''); +const resolveMessageId = (messageId: string) => { + if (messageId?.endsWith(MessageWaitSuffix)) { + return messageId.slice(0, -MessageWaitSuffix.length); + } + return messageId; +};Option 2: Module-level compiled regex (if you prefer regex)
+const WAIT_SUFFIX_REGEX = new RegExp(`${MessageWaitSuffix}$`); + -const resolveMessageId = (messageId: string) => - messageId?.replace(new RegExp(`${MessageWaitSuffix}$`), ''); +const resolveMessageId = (messageId: string) => + messageId?.replace(WAIT_SUFFIX_REGEX, '');🤖 Prompt for 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. In `@web/src/pages/agent/hooks/use-cache-chat-log.ts` around lines 15 - 16, The resolveMessageId function uses new RegExp(`${MessageWaitSuffix}$`) which is unnecessary and flagged for ReDoS; replace the RegExp usage with a simple string check and slice using MessageWaitSuffix (e.g., if messageId.endsWith(MessageWaitSuffix) then return messageId.slice(0, -MessageWaitSuffix.length) else return messageId) to avoid constructing a regex on every call and improve readability and performance.
🤖 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 `@web/src/pages/agent/chat/use-send-agent-message.ts`:
- Around line 464-488: The split branch can create an invalid ID like
"undefined-wait" because id (from findMessageFromList(answerList)) may be
undefined; update the logic around shouldSplitMessage/ addNewestOneAnswer to
ensure id is defined before constructing `${id}${MessageWaitSuffix}`—either bail
out of the split path, derive a safe fallback ID (e.g., generate a unique id or
use a guaranteed string like `newMessageId()` or `temporary-${timestamp}`), or
use the original message's id when available; modify the code that calls
findMessageFromList, the split branch that calls addNewestOneAnswer, and any
reliance on MessageWaitSuffix so the wait message ID is never "undefined-wait".
---
Nitpick comments:
In `@web/src/pages/agent/hooks/use-cache-chat-log.ts`:
- Around line 15-16: The resolveMessageId function uses new
RegExp(`${MessageWaitSuffix}$`) which is unnecessary and flagged for ReDoS;
replace the RegExp usage with a simple string check and slice using
MessageWaitSuffix (e.g., if messageId.endsWith(MessageWaitSuffix) then return
messageId.slice(0, -MessageWaitSuffix.length) else return messageId) to avoid
constructing a regex on every call and improve readability and performance.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6f7cfde2-8afd-441f-a5e2-c16c4e8dfa3a
📒 Files selected for processing (7)
web/src/pages/agent/chat/box.tsxweb/src/pages/agent/chat/use-send-agent-message.tsweb/src/pages/agent/constant/chat.tsweb/src/pages/agent/hooks/use-cache-chat-log.tsweb/src/pages/agent/hooks/use-chat-logic.tsweb/src/pages/agent/share/index.tsxweb/src/pages/agent/utils/chat.ts
| const shouldSplit = shouldSplitMessage(answerList, content); | ||
|
|
||
| if (shouldSplit) { | ||
| addNewestOneAnswer({ | ||
| answer: answer ?? '', | ||
| audio_binary: audio_binary, | ||
| attachment: attachment as IAttachment, | ||
| downloads, | ||
| id, | ||
| }); | ||
| addNewestOneAnswer({ | ||
| answer: '', | ||
| ...inputAnswer, | ||
| id: `${id}${MessageWaitSuffix}`, | ||
| }); | ||
| } else { | ||
| addNewestOneAnswer({ | ||
| answer: answer ?? '', | ||
| audio_binary: audio_binary, | ||
| attachment: attachment as IAttachment, | ||
| downloads, | ||
| id, | ||
| ...inputAnswer, | ||
| }); | ||
| } |
There was a problem hiding this comment.
Potential undefined message ID in split logic.
When shouldSplitMessage returns true, the wait message ID is constructed as ${id}${MessageWaitSuffix} (line 477). However, id comes from findMessageFromList(answerList) which can return undefined if eventList[0]?.message_id is falsy. This could produce an ID like "undefined-wait".
Consider adding a guard to ensure id is defined before splitting, or handle the undefined case explicitly.
🛡️ Proposed fix
if (answerList.length > 0) {
const shouldSplit = shouldSplitMessage(answerList, content);
- if (shouldSplit) {
+ if (shouldSplit && id) {
addNewestOneAnswer({
answer: answer ?? '',📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const shouldSplit = shouldSplitMessage(answerList, content); | |
| if (shouldSplit) { | |
| addNewestOneAnswer({ | |
| answer: answer ?? '', | |
| audio_binary: audio_binary, | |
| attachment: attachment as IAttachment, | |
| downloads, | |
| id, | |
| }); | |
| addNewestOneAnswer({ | |
| answer: '', | |
| ...inputAnswer, | |
| id: `${id}${MessageWaitSuffix}`, | |
| }); | |
| } else { | |
| addNewestOneAnswer({ | |
| answer: answer ?? '', | |
| audio_binary: audio_binary, | |
| attachment: attachment as IAttachment, | |
| downloads, | |
| id, | |
| ...inputAnswer, | |
| }); | |
| } | |
| const shouldSplit = shouldSplitMessage(answerList, content); | |
| if (shouldSplit && id) { | |
| addNewestOneAnswer({ | |
| answer: answer ?? '', | |
| audio_binary: audio_binary, | |
| attachment: attachment as IAttachment, | |
| downloads, | |
| id, | |
| }); | |
| addNewestOneAnswer({ | |
| answer: '', | |
| ...inputAnswer, | |
| id: `${id}${MessageWaitSuffix}`, | |
| }); | |
| } else { | |
| addNewestOneAnswer({ | |
| answer: answer ?? '', | |
| audio_binary: audio_binary, | |
| attachment: attachment as IAttachment, | |
| downloads, | |
| id, | |
| ...inputAnswer, | |
| }); | |
| } |
🤖 Prompt for 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.
In `@web/src/pages/agent/chat/use-send-agent-message.ts` around lines 464 - 488,
The split branch can create an invalid ID like "undefined-wait" because id (from
findMessageFromList(answerList)) may be undefined; update the logic around
shouldSplitMessage/ addNewestOneAnswer to ensure id is defined before
constructing `${id}${MessageWaitSuffix}`—either bail out of the split path,
derive a safe fallback ID (e.g., generate a unique id or use a guaranteed string
like `newMessageId()` or `temporary-${timestamp}`), or use the original
message's id when available; modify the code that calls findMessageFromList, the
split branch that calls addNewestOneAnswer, and any reliance on
MessageWaitSuffix so the wait message ID is never "undefined-wait".
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14839 +/- ##
=======================================
Coverage 94.16% 94.16%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 662 662
Misses 25 25
Partials 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…e outgoing message is split into two separate messages. (infiniflow#14839) ### What problem does this PR solve? Feat: When a Wait Node precedes a Message Node within a Loop Node, the outgoing message is split into two separate messages. ### Type of change - [x] New Feature (non-breaking change which adds functionality)
What problem does this PR solve?
Feat: When a Wait Node precedes a Message Node within a Loop Node, the outgoing message is split into two separate messages.
Type of change