Repository navigation
fix(react-core): merge toolCalls by id when deduplicating duplicate assistant messages - #4059
Closed
octo-patch wants to merge 1 commit into
Closed
octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…ssistant messages When a TOOL_CALL_RESULT is interleaved between TOOL_CALL_START events for the same parentMessageId, @ag-ui/client creates a second assistant message with the same id rather than appending to the existing one (because the parent is no longer the last message in the array). The previous deduplicateMessages logic used: toolCalls = message.toolCalls ?? existing.toolCalls This discards any toolCalls from earlier occurrences when the later duplicate has its own non-undefined toolCalls array. Fix: when both occurrences have non-empty toolCalls arrays, merge them by id (later occurrence wins), so no tool call is silently dropped. Explicit [] (all tool calls completed) still resets the set. Adds two new tests covering the merge scenario and the tie-break rule. Fixes CopilotKit#3644 Signed-off-by: octo-patch <[email protected]>
octo-patch
requested review from
jpr5,
marthakelly,
mme,
ranst91 and
tylerslaton
as code owners
April 18, 2026 01:50
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
Someone is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
Closing as already-landed — the |
4 tasks done
tylerslaton
added a commit
that referenced
this pull request
Nov 16, 2026
…8408) <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. **Please PLEASE reach out to us first before starting any significant work on new or existing features.** By the time you've gotten here, you're looking at creating a pull request so hopefully we're not too late. We love community contributions! That said, we want to make sure we're all on the same page before you start. Investing a lot of time and effort just to find out it doesn't align with the upstream project feels awful, and we don't want that to happen. It also helps to make sure the work you're planning isn't already in progress. As described in our contributing guide, please file an issue first: https://github.com/ag-ui-protocol/ag-ui/issues Or, reach out to us on Discord: https://discord.com/invite/6dffbvGU3D You can learn more about contributing to copilotkit here: https://github.com/copilotkit/copilotkit/blob/master/CONTRIBUTING.md Happy contributing! --> ## What does this PR do? Fixes the remaining half of #3644: when two copies of an assistant message share an id, the chat view dropped the tool calls held only by the earlier copy. The original cause was in `@ag-ui/client`: up to 0.0.51, `TOOL_CALL_START` attached a tool call to its parent only if the parent was the *last* message. A `TOOL_CALL_RESULT` between two tool calls for the same parent therefore created a second assistant message with the same id. Upstream fixed this in 0.0.52 (2026-04-08), and CopilotKit has used a fixed version since `a5c1137465`. I reproduced the reporter's event sequence against an old and a current client to confirm this. `deduplicateMessages` in `CopilotChatMessageView.tsx` already merges duplicate assistant messages (it recovers wiped content and `undefined` toolCalls), but when both copies had tool calls it kept only the later copy's list. For the reporter's sequence — `[tool_a, tool_b]` on the first copy and `[tool_c]` on the second — only `tool_c` rendered. Duplicates can still reach the view from older clients, custom agents, or stored history. - When both copies carry tool calls, `deduplicateMessages` now keeps the calls from both. The existing rules are unchanged: `undefined` recovers the earlier list, and an explicit `[]` is kept. - A new mergeToolCalls helper combines the lists: for a tool call in both copies, a later copy with arguments wins, and a blank copy never replaces a populated one. dedupeToolCalls is unchanged, so within a single message the first populated copy still wins (TOOL_CALL_ARGS deltas go to the first copy). - Moved the `deduplicateMessages` doc comment back above its function; an earlier change had placed `dedupeToolCalls` between them. - Four new deduplicateMessages tests: the reporter's scenario (all three tool calls survive and the content is kept), a blank later copy does not replace a populated one, a later populated copy wins when both are populated, and within one message a complete copy is kept over a stale later copy. This builds on #4059, which proposed combining the lists but merged shared ids with "later wins". As noted when #4058 was closed, the blank copy of a duplicated tool call usually arrives second, so this PR keeps the existing "prefer the populated copy" protection. ## Related PRs and Issues - Fixes #3644 - #4059 - #4058 - #3928 ## Checklist - [x] I have read the [Contribution Guide](https://github.com/copilotkit/copilotkit/blob/master/CONTRIBUTING.md) - [x] If the PR changes or adds functionality, I have updated the relevant documentation - [x] If the PR contains a breaking change, the title has `!` (for example `feat(react-core)!: ...`) and the description has a `BREAKING CHANGE:` line with migration steps. See [VERSIONING.md](https://github.com/CopilotKit/CopilotKit/blob/main/VERSIONING.md) - [x] "Allow edits by maintainers" is checked (lets us help iterate on your PR directly — faster turnaround for everyone)
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3644
Problem
When a
TOOL_CALL_RESULTis interleaved betweenTOOL_CALL_STARTevents for the sameparentMessageId,@ag-ui/clientcreates a second assistant message with the same id instead of appending to the existing one (because the parent is no longer the last message in the array).This produces a sequence like:
msg_xcreated withtoolCalls: [tool_a, tool_b]TOOL_CALL_RESULTfortool_ais inserted as a separate"tool"messageTOOL_CALL_START(tool_c)— parentmsg_xis no longer last, so a newmsg_xentry is created withtoolCalls: [tool_c]The previous
deduplicateMessageslogic:picks the later occurrence's
toolCallswhen it's defined — silently droppingtool_aandtool_b. The dev-mode warning fires but the data is already wrong.Solution
When both occurrences have non-empty
toolCallsarrays, merge them by id (later occurrence wins for any given id):Semantics preserved:
undefined→ fall back to existing (no tool call activity in this chunk)[]→ intentional reset ("all tool calls finished"), resets the setTesting
Two new unit tests added to the existing
deduplicateMessagessuite: