@copilotkit/react-core 0.0.0-main-20260220223427 → 0.0.0-main-20260402184302

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +538 -71
  2. package/dist/copilotkit-BK_zLoVm.mjs +7661 -0
  3. package/dist/copilotkit-BK_zLoVm.mjs.map +1 -0
  4. package/dist/copilotkit-BYNyIuE7.d.cts +2208 -0
  5. package/dist/copilotkit-BYNyIuE7.d.cts.map +1 -0
  6. package/dist/copilotkit-CDZGGOIB.cjs +8074 -0
  7. package/dist/copilotkit-CDZGGOIB.cjs.map +1 -0
  8. package/dist/copilotkit-rBCfdqIo.d.mts +2208 -0
  9. package/dist/copilotkit-rBCfdqIo.d.mts.map +1 -0
  10. package/dist/index.cjs +166 -2200
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +14 -674
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +14 -674
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +119 -2124
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/index.umd.js +2252 -230
  19. package/dist/index.umd.js.map +1 -1
  20. package/dist/v2/index.cjs +78 -6
  21. package/dist/v2/index.css +1 -2
  22. package/dist/v2/index.d.cts +6 -2
  23. package/dist/v2/index.d.mts +6 -2
  24. package/dist/v2/index.mjs +8 -3
  25. package/dist/v2/index.umd.js +7786 -12
  26. package/dist/v2/index.umd.js.map +1 -0
  27. package/package.json +65 -38
  28. package/scripts/scope-preflight.mjs +100 -0
  29. package/src/components/CopilotListeners.tsx +2 -6
  30. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +92 -0
  31. package/src/components/copilot-provider/copilot-messages.tsx +1 -1
  32. package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
  33. package/src/components/copilot-provider/copilotkit.tsx +120 -68
  34. package/src/context/copilot-messages-context.tsx +1 -1
  35. package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
  36. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +5 -6
  37. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +242 -0
  38. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
  39. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
  40. package/src/hooks/index.ts +0 -1
  41. package/src/hooks/use-agent-nodename.ts +1 -1
  42. package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
  43. package/src/hooks/use-coagent.ts +1 -1
  44. package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
  45. package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
  46. package/src/hooks/use-copilot-chat_internal.ts +73 -13
  47. package/src/hooks/use-copilot-readable.ts +1 -1
  48. package/src/hooks/use-frontend-tool.ts +2 -2
  49. package/src/hooks/use-human-in-the-loop.ts +2 -2
  50. package/src/hooks/use-langgraph-interrupt.ts +95 -38
  51. package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
  52. package/src/hooks/use-render-tool-call.ts +1 -1
  53. package/src/lib/copilot-task.ts +1 -1
  54. package/src/setupTests.ts +18 -14
  55. package/src/types/interrupt-action.ts +5 -0
  56. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
  57. package/src/v2/__tests__/globalSetup.ts +14 -0
  58. package/src/v2/__tests__/setup.ts +93 -0
  59. package/src/v2/__tests__/utils/test-helpers.tsx +503 -0
  60. package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
  61. package/src/v2/components/CopilotKitInspector.tsx +50 -0
  62. package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
  63. package/src/v2/components/WildcardToolCallRender.tsx +86 -0
  64. package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
  65. package/src/v2/components/chat/CopilotChat.tsx +433 -0
  66. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
  67. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
  68. package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
  69. package/src/v2/components/chat/CopilotChatMessageView.tsx +561 -0
  70. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
  71. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
  72. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
  73. package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
  74. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
  75. package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
  76. package/src/v2/components/chat/CopilotChatView.tsx +598 -0
  77. package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
  78. package/src/v2/components/chat/CopilotPopup.tsx +81 -0
  79. package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
  80. package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
  81. package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
  82. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
  83. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
  84. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
  85. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
  86. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
  87. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
  88. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
  89. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
  90. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
  91. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
  92. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
  93. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
  94. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +785 -0
  95. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2416 -0
  96. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
  97. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
  98. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
  99. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
  100. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
  101. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
  102. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1027 -0
  103. package/src/v2/components/chat/__tests__/setup.ts +1 -0
  104. package/src/v2/components/chat/index.ts +79 -0
  105. package/src/v2/components/index.ts +7 -0
  106. package/src/v2/components/license-warning-banner.tsx +198 -0
  107. package/src/v2/components/ui/button.tsx +123 -0
  108. package/src/v2/components/ui/dropdown-menu.tsx +258 -0
  109. package/src/v2/components/ui/tooltip.tsx +60 -0
  110. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
  111. package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
  112. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +140 -0
  113. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
  114. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
  115. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
  116. package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +327 -0
  117. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +728 -0
  118. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +159 -0
  119. package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
  120. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
  121. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
  122. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
  123. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2148 -0
  124. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
  125. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
  126. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
  127. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
  128. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
  129. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
  130. package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
  131. package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
  132. package/src/v2/hooks/index.ts +18 -0
  133. package/src/v2/hooks/use-agent-context.tsx +45 -0
  134. package/src/v2/hooks/use-agent.tsx +358 -0
  135. package/src/v2/hooks/use-component.tsx +89 -0
  136. package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
  137. package/src/v2/hooks/use-default-render-tool.tsx +254 -0
  138. package/src/v2/hooks/use-frontend-tool.tsx +43 -0
  139. package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
  140. package/src/v2/hooks/use-interrupt.tsx +305 -0
  141. package/src/v2/hooks/use-keyboard-height.tsx +67 -0
  142. package/src/v2/hooks/use-render-activity-message.tsx +73 -0
  143. package/src/v2/hooks/use-render-custom-messages.tsx +98 -0
  144. package/src/v2/hooks/use-render-tool-call.tsx +175 -0
  145. package/src/v2/hooks/use-render-tool.tsx +181 -0
  146. package/src/v2/hooks/use-suggestions.tsx +91 -0
  147. package/src/v2/hooks/use-threads.tsx +256 -0
  148. package/src/v2/hooks/useKatexStyles.ts +27 -0
  149. package/src/v2/index.css +1 -1
  150. package/src/v2/index.ts +21 -2
  151. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
  152. package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
  153. package/src/v2/lib/react-core.ts +156 -0
  154. package/src/v2/lib/slots.tsx +143 -0
  155. package/src/v2/lib/transcription-client.ts +184 -0
  156. package/src/v2/lib/utils.ts +8 -0
  157. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
  158. package/src/v2/providers/CopilotKitProvider.tsx +600 -0
  159. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
  160. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
  161. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
  162. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
  163. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
  164. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
  165. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
  166. package/src/v2/providers/index.ts +14 -0
  167. package/src/v2/styles/globals.css +231 -0
  168. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
  169. package/src/v2/types/defineToolCallRenderer.ts +65 -0
  170. package/src/v2/types/frontend-tool.ts +8 -0
  171. package/src/v2/types/human-in-the-loop.ts +33 -0
  172. package/src/v2/types/index.ts +7 -0
  173. package/src/v2/types/interrupt.ts +15 -0
  174. package/src/v2/types/react-activity-message-renderer.ts +27 -0
  175. package/src/v2/types/react-custom-message-renderer.ts +17 -0
  176. package/src/v2/types/react-tool-call-renderer.ts +32 -0
  177. package/tsdown.config.ts +34 -10
  178. package/vitest.config.mjs +4 -3
  179. package/LICENSE +0 -21
  180. package/dist/v2/index.css.map +0 -1
  181. package/src/hooks/use-langgraph-interrupt-render.ts +0 -147
@@ -0,0 +1,561 @@
1
+ import React, { useEffect, useReducer, useState } from "react";
2
+ import { WithSlots, renderSlot, isReactComponentType } from "../../lib/slots";
3
+ import CopilotChatAssistantMessage from "./CopilotChatAssistantMessage";
4
+ import CopilotChatUserMessage from "./CopilotChatUserMessage";
5
+ import CopilotChatReasoningMessage from "./CopilotChatReasoningMessage";
6
+ import {
7
+ ActivityMessage,
8
+ AssistantMessage,
9
+ Message,
10
+ ReasoningMessage,
11
+ UserMessage,
12
+ } from "@ag-ui/core";
13
+ import { twMerge } from "tailwind-merge";
14
+ import { useRenderActivityMessage, useRenderCustomMessages } from "../../hooks";
15
+ import { getThreadClone } from "../../hooks/use-agent";
16
+ import { useCopilotKit } from "../../providers/CopilotKitProvider";
17
+ import { useCopilotChatConfiguration } from "../../providers/CopilotChatConfigurationProvider";
18
+
19
+ /**
20
+ * Memoized wrapper for assistant messages to prevent re-renders when other messages change.
21
+ */
22
+ const MemoizedAssistantMessage = React.memo(
23
+ function MemoizedAssistantMessage({
24
+ message,
25
+ messages,
26
+ isRunning,
27
+ AssistantMessageComponent,
28
+ slotProps,
29
+ }: {
30
+ message: AssistantMessage;
31
+ messages: Message[];
32
+ isRunning: boolean;
33
+ AssistantMessageComponent: typeof CopilotChatAssistantMessage;
34
+ slotProps?: Partial<
35
+ React.ComponentProps<typeof CopilotChatAssistantMessage>
36
+ >;
37
+ }) {
38
+ return (
39
+ <AssistantMessageComponent
40
+ message={message}
41
+ messages={messages}
42
+ isRunning={isRunning}
43
+ {...slotProps}
44
+ />
45
+ );
46
+ },
47
+ (prevProps, nextProps) => {
48
+ // Only re-render if this specific message changed
49
+ if (prevProps.message.id !== nextProps.message.id) return false;
50
+ if (prevProps.message.content !== nextProps.message.content) return false;
51
+
52
+ // Compare tool calls if present
53
+ const prevToolCalls = prevProps.message.toolCalls;
54
+ const nextToolCalls = nextProps.message.toolCalls;
55
+ if (prevToolCalls?.length !== nextToolCalls?.length) return false;
56
+ if (prevToolCalls && nextToolCalls) {
57
+ for (let i = 0; i < prevToolCalls.length; i++) {
58
+ const prevTc = prevToolCalls[i];
59
+ const nextTc = nextToolCalls[i];
60
+ if (!prevTc || !nextTc) return false;
61
+ if (prevTc.id !== nextTc.id) return false;
62
+ if (prevTc.function.arguments !== nextTc.function.arguments)
63
+ return false;
64
+ }
65
+ }
66
+
67
+ // Check if tool results changed for this message's tool calls
68
+ // Tool results are separate messages with role="tool" that reference tool call IDs
69
+ if (prevToolCalls && prevToolCalls.length > 0) {
70
+ const toolCallIds = new Set(prevToolCalls.map((tc) => tc.id));
71
+
72
+ const prevToolResults = prevProps.messages.filter(
73
+ (m) => m.role === "tool" && toolCallIds.has((m as any).toolCallId),
74
+ );
75
+ const nextToolResults = nextProps.messages.filter(
76
+ (m) => m.role === "tool" && toolCallIds.has((m as any).toolCallId),
77
+ );
78
+
79
+ // If number of tool results changed, re-render
80
+ if (prevToolResults.length !== nextToolResults.length) return false;
81
+
82
+ // If any tool result content changed, re-render
83
+ for (let i = 0; i < prevToolResults.length; i++) {
84
+ if (
85
+ (prevToolResults[i] as any).content !==
86
+ (nextToolResults[i] as any).content
87
+ )
88
+ return false;
89
+ }
90
+ }
91
+
92
+ // Only care about isRunning if this message is CURRENTLY the latest
93
+ // (we don't need to re-render just because a message stopped being the latest)
94
+ const nextIsLatest =
95
+ nextProps.messages[nextProps.messages.length - 1]?.id ===
96
+ nextProps.message.id;
97
+ if (nextIsLatest && prevProps.isRunning !== nextProps.isRunning)
98
+ return false;
99
+
100
+ // Check if component reference changed
101
+ if (
102
+ prevProps.AssistantMessageComponent !==
103
+ nextProps.AssistantMessageComponent
104
+ )
105
+ return false;
106
+
107
+ // Check if slot props changed
108
+ if (prevProps.slotProps !== nextProps.slotProps) return false;
109
+
110
+ return true;
111
+ },
112
+ );
113
+
114
+ /**
115
+ * Memoized wrapper for user messages to prevent re-renders when other messages change.
116
+ */
117
+ const MemoizedUserMessage = React.memo(
118
+ function MemoizedUserMessage({
119
+ message,
120
+ UserMessageComponent,
121
+ slotProps,
122
+ }: {
123
+ message: UserMessage;
124
+ UserMessageComponent: typeof CopilotChatUserMessage;
125
+ slotProps?: Partial<React.ComponentProps<typeof CopilotChatUserMessage>>;
126
+ }) {
127
+ return <UserMessageComponent message={message} {...slotProps} />;
128
+ },
129
+ (prevProps, nextProps) => {
130
+ // Only re-render if this specific message changed
131
+ if (prevProps.message.id !== nextProps.message.id) return false;
132
+ if (prevProps.message.content !== nextProps.message.content) return false;
133
+ if (prevProps.UserMessageComponent !== nextProps.UserMessageComponent)
134
+ return false;
135
+ // Check if slot props changed
136
+ if (prevProps.slotProps !== nextProps.slotProps) return false;
137
+ return true;
138
+ },
139
+ );
140
+
141
+ /**
142
+ * Memoized wrapper for activity messages to prevent re-renders when other messages change.
143
+ */
144
+ const MemoizedActivityMessage = React.memo(
145
+ function MemoizedActivityMessage({
146
+ message,
147
+ renderActivityMessage,
148
+ }: {
149
+ message: ActivityMessage;
150
+ renderActivityMessage: (
151
+ message: ActivityMessage,
152
+ ) => React.ReactElement | null;
153
+ }) {
154
+ return renderActivityMessage(message);
155
+ },
156
+ (prevProps, nextProps) => {
157
+ // Message ID changed = different message, must re-render
158
+ if (prevProps.message.id !== nextProps.message.id) return false;
159
+
160
+ // Activity type changed = must re-render
161
+ if (prevProps.message.activityType !== nextProps.message.activityType)
162
+ return false;
163
+
164
+ // Compare content using JSON.stringify (native code, handles deep comparison)
165
+ if (
166
+ JSON.stringify(prevProps.message.content) !==
167
+ JSON.stringify(nextProps.message.content)
168
+ )
169
+ return false;
170
+
171
+ return true;
172
+ },
173
+ );
174
+
175
+ /**
176
+ * Memoized wrapper for reasoning messages to prevent re-renders when other messages change.
177
+ */
178
+ const MemoizedReasoningMessage = React.memo(
179
+ function MemoizedReasoningMessage({
180
+ message,
181
+ messages,
182
+ isRunning,
183
+ ReasoningMessageComponent,
184
+ slotProps,
185
+ }: {
186
+ message: ReasoningMessage;
187
+ messages: Message[];
188
+ isRunning: boolean;
189
+ ReasoningMessageComponent: typeof CopilotChatReasoningMessage;
190
+ slotProps?: Partial<
191
+ React.ComponentProps<typeof CopilotChatReasoningMessage>
192
+ >;
193
+ }) {
194
+ return (
195
+ <ReasoningMessageComponent
196
+ message={message}
197
+ messages={messages}
198
+ isRunning={isRunning}
199
+ {...slotProps}
200
+ />
201
+ );
202
+ },
203
+ (prevProps, nextProps) => {
204
+ // Only re-render if this specific message changed
205
+ if (prevProps.message.id !== nextProps.message.id) return false;
206
+ if (prevProps.message.content !== nextProps.message.content) return false;
207
+
208
+ // Re-render when "latest" status changes (e.g. reasoning message is no longer the last message
209
+ // because a text message was added after it — this transitions isStreaming from true to false)
210
+ const prevIsLatest =
211
+ prevProps.messages[prevProps.messages.length - 1]?.id ===
212
+ prevProps.message.id;
213
+ const nextIsLatest =
214
+ nextProps.messages[nextProps.messages.length - 1]?.id ===
215
+ nextProps.message.id;
216
+ if (prevIsLatest !== nextIsLatest) return false;
217
+
218
+ // Only care about isRunning if this message is CURRENTLY the latest
219
+ if (nextIsLatest && prevProps.isRunning !== nextProps.isRunning)
220
+ return false;
221
+
222
+ // Check if component reference changed
223
+ if (
224
+ prevProps.ReasoningMessageComponent !==
225
+ nextProps.ReasoningMessageComponent
226
+ )
227
+ return false;
228
+
229
+ // Check if slot props changed
230
+ if (prevProps.slotProps !== nextProps.slotProps) return false;
231
+
232
+ return true;
233
+ },
234
+ );
235
+
236
+ /**
237
+ * Memoized wrapper for custom messages to prevent re-renders when other messages change.
238
+ */
239
+ const MemoizedCustomMessage = React.memo(
240
+ function MemoizedCustomMessage({
241
+ message,
242
+ position,
243
+ renderCustomMessage,
244
+ }: {
245
+ message: Message;
246
+ position: "before" | "after";
247
+ renderCustomMessage: (params: {
248
+ message: Message;
249
+ position: "before" | "after";
250
+ }) => React.ReactElement | null;
251
+ stateSnapshot?: unknown;
252
+ }) {
253
+ return renderCustomMessage({ message, position });
254
+ },
255
+ (prevProps, nextProps) => {
256
+ // Only re-render if the message or position changed
257
+ if (prevProps.message.id !== nextProps.message.id) return false;
258
+ if (prevProps.position !== nextProps.position) return false;
259
+ // Compare message content - for assistant messages this is a string, for others may differ
260
+ if (prevProps.message.content !== nextProps.message.content) return false;
261
+ if (prevProps.message.role !== nextProps.message.role) return false;
262
+ // Compare state snapshot - custom renderers may depend on state
263
+ if (
264
+ JSON.stringify(prevProps.stateSnapshot) !==
265
+ JSON.stringify(nextProps.stateSnapshot)
266
+ )
267
+ return false;
268
+ // Note: We don't compare renderCustomMessage function reference because it changes
269
+ // frequently. The message and state comparison is sufficient to determine if a re-render is needed.
270
+ return true;
271
+ },
272
+ );
273
+
274
+ export type CopilotChatMessageViewProps = Omit<
275
+ WithSlots<
276
+ {
277
+ assistantMessage: typeof CopilotChatAssistantMessage;
278
+ userMessage: typeof CopilotChatUserMessage;
279
+ reasoningMessage: typeof CopilotChatReasoningMessage;
280
+ cursor: typeof CopilotChatMessageView.Cursor;
281
+ },
282
+ {
283
+ isRunning?: boolean;
284
+ messages?: Message[];
285
+ } & React.HTMLAttributes<HTMLDivElement>
286
+ >,
287
+ "children"
288
+ > & {
289
+ children?: (props: {
290
+ isRunning: boolean;
291
+ messages: Message[];
292
+ messageElements: React.ReactElement[];
293
+ interruptElement: React.ReactElement | null;
294
+ }) => React.ReactElement;
295
+ };
296
+
297
+ export function CopilotChatMessageView({
298
+ messages = [],
299
+ assistantMessage,
300
+ userMessage,
301
+ reasoningMessage,
302
+ cursor,
303
+ isRunning = false,
304
+ children,
305
+ className,
306
+ ...props
307
+ }: CopilotChatMessageViewProps) {
308
+ const renderCustomMessage = useRenderCustomMessages();
309
+ const { renderActivityMessage } = useRenderActivityMessage();
310
+ const { copilotkit } = useCopilotKit();
311
+ const config = useCopilotChatConfiguration();
312
+ const [, forceUpdate] = useReducer((x) => x + 1, 0);
313
+
314
+ // Subscribe to state changes so custom message renderers re-render when state updates.
315
+ useEffect(() => {
316
+ if (!config?.agentId) return;
317
+ const registryAgent = copilotkit.getAgent(config.agentId);
318
+ // Prefer the per-thread clone so that state changes from the running agent
319
+ // (which is the clone, not the registry) trigger re-renders.
320
+ const agent =
321
+ getThreadClone(registryAgent, config.threadId) ?? registryAgent;
322
+ if (!agent) return;
323
+
324
+ const subscription = agent.subscribe({
325
+ onStateChanged: forceUpdate,
326
+ });
327
+ return () => subscription.unsubscribe();
328
+ }, [config?.agentId, config?.threadId, copilotkit, forceUpdate]);
329
+
330
+ // Subscribe to interrupt element changes for in-chat rendering.
331
+ const [interruptElement, setInterruptElement] =
332
+ useState<React.ReactElement | null>(null);
333
+ useEffect(() => {
334
+ setInterruptElement(copilotkit.interruptElement);
335
+ const subscription = copilotkit.subscribe({
336
+ onInterruptElementChanged: ({ interruptElement }) => {
337
+ setInterruptElement(interruptElement);
338
+ },
339
+ });
340
+ return () => subscription.unsubscribe();
341
+ }, [copilotkit]);
342
+
343
+ // Helper to get state snapshot for a message (used for memoization)
344
+ const getStateSnapshotForMessage = (messageId: string): unknown => {
345
+ if (!config) return undefined;
346
+ const resolvedRunId =
347
+ copilotkit.getRunIdForMessage(
348
+ config.agentId,
349
+ config.threadId,
350
+ messageId,
351
+ ) ??
352
+ copilotkit
353
+ .getRunIdsForThread(config.agentId, config.threadId)
354
+ .slice(-1)[0];
355
+ if (!resolvedRunId) return undefined;
356
+ return copilotkit.getStateByRun(
357
+ config.agentId,
358
+ config.threadId,
359
+ resolvedRunId,
360
+ );
361
+ };
362
+
363
+ // Deduplicate messages by id, keeping the last occurrence of each.
364
+ // During streaming, AbstractAgent.addMessage() can push duplicate messages
365
+ // (same id) which causes React "duplicate key" warnings and rendering glitches.
366
+ const deduplicatedMessages = [
367
+ ...new Map(messages.map((m) => [m.id, m])).values(),
368
+ ];
369
+
370
+ if (
371
+ process.env.NODE_ENV === "development" &&
372
+ deduplicatedMessages.length < messages.length
373
+ ) {
374
+ console.warn(
375
+ `CopilotChatMessageView: Deduplicated ${messages.length - deduplicatedMessages.length} message(s) with duplicate IDs.`,
376
+ );
377
+ }
378
+
379
+ const messageElements: React.ReactElement[] = deduplicatedMessages
380
+ .flatMap((message) => {
381
+ const elements: (React.ReactElement | null | undefined)[] = [];
382
+ const stateSnapshot = getStateSnapshotForMessage(message.id);
383
+
384
+ // Render custom message before (using memoized wrapper)
385
+ if (renderCustomMessage) {
386
+ elements.push(
387
+ <MemoizedCustomMessage
388
+ key={`${message.id}-custom-before`}
389
+ message={message}
390
+ position="before"
391
+ renderCustomMessage={renderCustomMessage}
392
+ stateSnapshot={stateSnapshot}
393
+ />,
394
+ );
395
+ }
396
+
397
+ // Render the main message using memoized wrappers to prevent unnecessary re-renders
398
+ if (message.role === "assistant") {
399
+ // Determine the component and props from slot value
400
+ let AssistantComponent = CopilotChatAssistantMessage;
401
+ let assistantSlotProps:
402
+ | Partial<React.ComponentProps<typeof CopilotChatAssistantMessage>>
403
+ | undefined;
404
+
405
+ if (isReactComponentType(assistantMessage)) {
406
+ // Custom component (function, forwardRef, memo, etc.)
407
+ AssistantComponent =
408
+ assistantMessage as typeof CopilotChatAssistantMessage;
409
+ } else if (typeof assistantMessage === "string") {
410
+ // className string
411
+ assistantSlotProps = { className: assistantMessage };
412
+ } else if (assistantMessage && typeof assistantMessage === "object") {
413
+ // Props object
414
+ assistantSlotProps = assistantMessage as Partial<
415
+ React.ComponentProps<typeof CopilotChatAssistantMessage>
416
+ >;
417
+ }
418
+
419
+ elements.push(
420
+ <MemoizedAssistantMessage
421
+ key={message.id}
422
+ message={message as AssistantMessage}
423
+ messages={messages}
424
+ isRunning={isRunning}
425
+ AssistantMessageComponent={AssistantComponent}
426
+ slotProps={assistantSlotProps}
427
+ />,
428
+ );
429
+ } else if (message.role === "user") {
430
+ // Determine the component and props from slot value
431
+ let UserComponent = CopilotChatUserMessage;
432
+ let userSlotProps:
433
+ | Partial<React.ComponentProps<typeof CopilotChatUserMessage>>
434
+ | undefined;
435
+
436
+ if (isReactComponentType(userMessage)) {
437
+ // Custom component (function, forwardRef, memo, etc.)
438
+ UserComponent = userMessage as typeof CopilotChatUserMessage;
439
+ } else if (typeof userMessage === "string") {
440
+ // className string
441
+ userSlotProps = { className: userMessage };
442
+ } else if (userMessage && typeof userMessage === "object") {
443
+ // Props object
444
+ userSlotProps = userMessage as Partial<
445
+ React.ComponentProps<typeof CopilotChatUserMessage>
446
+ >;
447
+ }
448
+
449
+ elements.push(
450
+ <MemoizedUserMessage
451
+ key={message.id}
452
+ message={message as UserMessage}
453
+ UserMessageComponent={UserComponent}
454
+ slotProps={userSlotProps}
455
+ />,
456
+ );
457
+ } else if (message.role === "activity") {
458
+ // Use memoized wrapper to prevent re-renders when other messages change
459
+ const activityMsg = message as ActivityMessage;
460
+ elements.push(
461
+ <MemoizedActivityMessage
462
+ key={message.id}
463
+ message={activityMsg}
464
+ renderActivityMessage={renderActivityMessage}
465
+ />,
466
+ );
467
+ } else if (message.role === "reasoning") {
468
+ // Determine the component and props from slot value
469
+ let ReasoningComponent = CopilotChatReasoningMessage;
470
+ let reasoningSlotProps:
471
+ | Partial<React.ComponentProps<typeof CopilotChatReasoningMessage>>
472
+ | undefined;
473
+
474
+ if (isReactComponentType(reasoningMessage)) {
475
+ ReasoningComponent =
476
+ reasoningMessage as typeof CopilotChatReasoningMessage;
477
+ } else if (typeof reasoningMessage === "string") {
478
+ reasoningSlotProps = { className: reasoningMessage };
479
+ } else if (reasoningMessage && typeof reasoningMessage === "object") {
480
+ reasoningSlotProps = reasoningMessage as Partial<
481
+ React.ComponentProps<typeof CopilotChatReasoningMessage>
482
+ >;
483
+ }
484
+
485
+ elements.push(
486
+ <MemoizedReasoningMessage
487
+ key={message.id}
488
+ message={message as ReasoningMessage}
489
+ messages={messages}
490
+ isRunning={isRunning}
491
+ ReasoningMessageComponent={ReasoningComponent}
492
+ slotProps={reasoningSlotProps}
493
+ />,
494
+ );
495
+ }
496
+
497
+ // Render custom message after (using memoized wrapper)
498
+ if (renderCustomMessage) {
499
+ elements.push(
500
+ <MemoizedCustomMessage
501
+ key={`${message.id}-custom-after`}
502
+ message={message}
503
+ position="after"
504
+ renderCustomMessage={renderCustomMessage}
505
+ stateSnapshot={stateSnapshot}
506
+ />,
507
+ );
508
+ }
509
+
510
+ return elements;
511
+ })
512
+ .filter(Boolean) as React.ReactElement[];
513
+
514
+ if (children) {
515
+ return (
516
+ <div data-copilotkit style={{ display: "contents" }}>
517
+ {children({ messageElements, messages, isRunning, interruptElement })}
518
+ </div>
519
+ );
520
+ }
521
+
522
+ // Hide the chat-level loading cursor when the last message is a reasoning
523
+ // message — the reasoning card already shows its own loading indicator.
524
+ const lastMessage = messages[messages.length - 1];
525
+ const showCursor = isRunning && lastMessage?.role !== "reasoning";
526
+
527
+ return (
528
+ <div
529
+ data-copilotkit
530
+ data-testid="copilot-message-list"
531
+ className={twMerge("copilotKitMessages cpk:flex cpk:flex-col", className)}
532
+ {...props}
533
+ >
534
+ {messageElements}
535
+ {interruptElement}
536
+ {showCursor && (
537
+ <div className="cpk:mt-2">
538
+ {renderSlot(cursor, CopilotChatMessageView.Cursor, {})}
539
+ </div>
540
+ )}
541
+ </div>
542
+ );
543
+ }
544
+
545
+ CopilotChatMessageView.Cursor = function Cursor({
546
+ className,
547
+ ...props
548
+ }: React.HTMLAttributes<HTMLDivElement>) {
549
+ return (
550
+ <div
551
+ data-testid="copilot-loading-cursor"
552
+ className={twMerge(
553
+ "cpk:w-[11px] cpk:h-[11px] cpk:rounded-full cpk:bg-foreground cpk:animate-pulse-cursor cpk:ml-1",
554
+ className,
555
+ )}
556
+ {...props}
557
+ />
558
+ );
559
+ };
560
+
561
+ export default CopilotChatMessageView;