Repository navigation
fix(react-core): deduplicate tool calls in CopilotChatToolCallsView to prevent React key warning - #4058
fix(react-core): deduplicate tool calls in CopilotChatToolCallsView to prevent React key warning#4058octo-patch wants to merge 1 commit into
Conversation
…o prevent React key warning When connectAgent() replays historic TOOL_CALL_START events over a pre-populated messages state, defaultApplyEvents pushes to toolCalls unconditionally, causing the same toolCallId to appear twice in the array. CopilotChatToolCallsView used toolCall.id as the React key, triggering a duplicate-key warning. Fix by deduplicating toolCalls by id before rendering, keeping the last occurrence so the most up-to-date arguments are used. Fixes CopilotKit#3928 Signed-off-by: octo-patch <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Someone is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
|
Thanks for the detailed writeup here — the root-cause analysis in the description is accurate, and it's the analysis that shaped how this got fixed. Closing as superseded. Tool-call dedup now lives one layer up, in One behavioral difference worth recording, since it's the reason the fix landed there rather than here: this PR keeps the last occurrence of a duplicated id, while the merged version prefers whichever occurrence actually carries Tracking for the wider tool-call dedup family is consolidated in #3644 (this branch's linked issue, #3928, was folded into it). If you still see a duplicate-key warning for tool calls on a current release, a reproduction on #3644 would be genuinely useful — the message-level and within-array facets were separate bugs and it's worth knowing if a third one exists. |
…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)
Fixes #3928
Problem
When
connectAgent()is called,InMemoryAgentRunner.connect()replays compacted historic events (includingTOOL_CALL_STARTfor completed tool calls) via@ag-ui/client'sdefaultApplyEvents. However, at that point the agent'smessagesstate already contains those tool calls from the prior run. BecausedefaultApplyEventspushes tomessage.toolCallsunconditionally — with no duplicate guard — the sametoolCallIdends up in the array twice.CopilotChatToolCallsViewthen maps this array usingtoolCall.idas a Reactkey, producing the duplicate-key warning:Solution
Deduplicate
message.toolCallsbyidbefore rendering inCopilotChatToolCallsView, keeping the last occurrence so the most up-to-date arguments are used:This is a defensive fix at the view layer. The underlying idempotency issue in
@ag-ui/client'sdefaultApplyEventshas been separately reported upstream.Testing
connectAgent()is called on an agent whosemessagesstate already contains completed tool calls.