@copilotkit/react-core 1.57.3 → 1.57.4

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 (266) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +12 -13
  3. package/skills/react-core/SKILL.md +108 -0
  4. package/skills/react-core/references/agent-access.md +288 -0
  5. package/skills/react-core/references/attachments.md +291 -0
  6. package/skills/react-core/references/capabilities.md +138 -0
  7. package/skills/react-core/references/chat-components.md +221 -0
  8. package/skills/react-core/references/client-side-tools.md +358 -0
  9. package/skills/react-core/references/custom-message-renderers.md +226 -0
  10. package/skills/react-core/references/debug-mode.md +153 -0
  11. package/skills/react-core/references/human-in-the-loop.md +312 -0
  12. package/skills/react-core/references/provider-setup.md +326 -0
  13. package/skills/react-core/references/rendering-activity-messages.md +207 -0
  14. package/skills/react-core/references/rendering-tool-calls.md +319 -0
  15. package/skills/react-core/references/suggestions.md +211 -0
  16. package/skills/react-core/references/switching-agents-recipes.md +160 -0
  17. package/skills/react-core/references/switching-agents.md +231 -0
  18. package/skills/react-core/references/threads.md +226 -0
  19. package/.attw.json +0 -3
  20. package/CHANGELOG.md +0 -5043
  21. package/scripts/scope-preflight.mjs +0 -100
  22. package/src/components/CopilotListeners.tsx +0 -137
  23. package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
  24. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
  25. package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
  26. package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
  27. package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
  28. package/src/components/copilot-provider/copilot-messages.tsx +0 -314
  29. package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
  30. package/src/components/copilot-provider/copilotkit.tsx +0 -853
  31. package/src/components/copilot-provider/index.ts +0 -3
  32. package/src/components/dev-console/console-trigger.tsx +0 -283
  33. package/src/components/dev-console/developer-console-modal.tsx +0 -1016
  34. package/src/components/dev-console/icons.tsx +0 -106
  35. package/src/components/error-boundary/error-boundary.tsx +0 -99
  36. package/src/components/error-boundary/error-utils.tsx +0 -105
  37. package/src/components/index.ts +0 -1
  38. package/src/components/toast/exclamation-mark-icon.tsx +0 -27
  39. package/src/components/toast/toast-provider.tsx +0 -448
  40. package/src/components/usage-banner.tsx +0 -266
  41. package/src/context/__tests__/threads-context.test.tsx +0 -141
  42. package/src/context/coagent-state-renders-context.tsx +0 -89
  43. package/src/context/copilot-context.tsx +0 -365
  44. package/src/context/copilot-messages-context.tsx +0 -35
  45. package/src/context/index.ts +0 -22
  46. package/src/context/threads-context.tsx +0 -69
  47. package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
  48. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
  49. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
  50. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
  51. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
  52. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
  53. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
  54. package/src/hooks/index.ts +0 -33
  55. package/src/hooks/use-agent-nodename.ts +0 -33
  56. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
  57. package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
  58. package/src/hooks/use-coagent-state-render-registry.ts +0 -230
  59. package/src/hooks/use-coagent-state-render.ts +0 -163
  60. package/src/hooks/use-coagent.ts +0 -377
  61. package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
  62. package/src/hooks/use-copilot-action.ts +0 -245
  63. package/src/hooks/use-copilot-additional-instructions.ts +0 -98
  64. package/src/hooks/use-copilot-authenticated-action.ts +0 -73
  65. package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
  66. package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
  67. package/src/hooks/use-copilot-chat.ts +0 -132
  68. package/src/hooks/use-copilot-chat_internal.ts +0 -875
  69. package/src/hooks/use-copilot-readable.ts +0 -135
  70. package/src/hooks/use-copilot-runtime-client.ts +0 -178
  71. package/src/hooks/use-default-tool.ts +0 -13
  72. package/src/hooks/use-flat-category-store.ts +0 -109
  73. package/src/hooks/use-frontend-tool.ts +0 -113
  74. package/src/hooks/use-human-in-the-loop.ts +0 -138
  75. package/src/hooks/use-langgraph-interrupt.ts +0 -103
  76. package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
  77. package/src/hooks/use-make-copilot-document-readable.ts +0 -30
  78. package/src/hooks/use-render-tool-call.ts +0 -89
  79. package/src/hooks/use-tree.ts +0 -222
  80. package/src/index.tsx +0 -7
  81. package/src/lib/copilot-task.ts +0 -215
  82. package/src/lib/index.ts +0 -1
  83. package/src/lib/status-checker.ts +0 -67
  84. package/src/setupTests.ts +0 -37
  85. package/src/test-helpers/copilot-context.ts +0 -91
  86. package/src/types/chat-suggestion-configuration.ts +0 -23
  87. package/src/types/coagent-action.ts +0 -35
  88. package/src/types/coagent-state.ts +0 -13
  89. package/src/types/crew.ts +0 -89
  90. package/src/types/document-pointer.ts +0 -7
  91. package/src/types/frontend-action.ts +0 -213
  92. package/src/types/index.ts +0 -17
  93. package/src/types/interrupt-action.ts +0 -58
  94. package/src/types/system-message.ts +0 -4
  95. package/src/utils/dev-console.ts +0 -19
  96. package/src/utils/index.ts +0 -2
  97. package/src/utils/suggestions-constants.ts +0 -8
  98. package/src/utils/utils.test.ts +0 -7
  99. package/src/utils/utils.ts +0 -6
  100. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
  101. package/src/v2/__tests__/globalSetup.ts +0 -14
  102. package/src/v2/__tests__/setup.ts +0 -93
  103. package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
  104. package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
  105. package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
  106. package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
  107. package/src/v2/components/CopilotKitInspector.tsx +0 -52
  108. package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
  109. package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
  110. package/src/v2/components/WildcardToolCallRender.tsx +0 -86
  111. package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
  112. package/src/v2/components/chat/CopilotChat.tsx +0 -664
  113. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
  114. package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
  115. package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
  116. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
  117. package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
  118. package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
  119. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
  120. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
  121. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
  122. package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
  123. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
  124. package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
  125. package/src/v2/components/chat/CopilotChatView.tsx +0 -890
  126. package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
  127. package/src/v2/components/chat/CopilotPopup.tsx +0 -81
  128. package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
  129. package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
  130. package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
  131. package/src/v2/components/chat/Lightbox.tsx +0 -103
  132. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
  133. package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
  134. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
  135. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
  136. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
  137. package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
  138. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
  139. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
  140. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
  141. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
  142. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
  143. package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
  144. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
  145. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
  146. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
  147. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
  148. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
  149. package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
  150. package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
  151. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
  152. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
  153. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
  154. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
  155. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
  156. package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
  157. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
  158. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
  159. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
  160. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
  161. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
  162. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
  163. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
  164. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
  165. package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
  166. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
  167. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
  168. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
  169. package/src/v2/components/chat/__tests__/setup.ts +0 -1
  170. package/src/v2/components/chat/index.ts +0 -90
  171. package/src/v2/components/chat/last-user-message-context.ts +0 -21
  172. package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
  173. package/src/v2/components/chat/scroll-element-context.ts +0 -13
  174. package/src/v2/components/index.ts +0 -8
  175. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
  176. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
  177. package/src/v2/components/intelligence-indicator/index.ts +0 -2
  178. package/src/v2/components/license-warning-banner.tsx +0 -217
  179. package/src/v2/components/ui/button.tsx +0 -124
  180. package/src/v2/components/ui/dropdown-menu.tsx +0 -258
  181. package/src/v2/components/ui/tooltip.tsx +0 -60
  182. package/src/v2/context.ts +0 -62
  183. package/src/v2/headless.ts +0 -64
  184. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
  185. package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
  186. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
  187. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
  188. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
  189. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
  190. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
  191. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
  192. package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
  193. package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
  194. package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
  195. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
  196. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
  197. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
  198. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
  199. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
  200. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
  201. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
  202. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
  203. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
  204. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
  205. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
  206. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
  207. package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
  208. package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
  209. package/src/v2/hooks/index.ts +0 -24
  210. package/src/v2/hooks/use-agent-context.tsx +0 -45
  211. package/src/v2/hooks/use-agent.tsx +0 -227
  212. package/src/v2/hooks/use-attachments.tsx +0 -269
  213. package/src/v2/hooks/use-capabilities.tsx +0 -25
  214. package/src/v2/hooks/use-component.tsx +0 -91
  215. package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
  216. package/src/v2/hooks/use-default-render-tool.tsx +0 -271
  217. package/src/v2/hooks/use-frontend-tool.tsx +0 -46
  218. package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
  219. package/src/v2/hooks/use-interrupt.tsx +0 -305
  220. package/src/v2/hooks/use-keyboard-height.tsx +0 -67
  221. package/src/v2/hooks/use-pin-to-send.ts +0 -94
  222. package/src/v2/hooks/use-render-activity-message.tsx +0 -72
  223. package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
  224. package/src/v2/hooks/use-render-tool-call.tsx +0 -208
  225. package/src/v2/hooks/use-render-tool.tsx +0 -184
  226. package/src/v2/hooks/use-suggestions.tsx +0 -91
  227. package/src/v2/hooks/use-threads.tsx +0 -325
  228. package/src/v2/hooks/useKatexStyles.ts +0 -27
  229. package/src/v2/index.css +0 -1
  230. package/src/v2/index.ts +0 -27
  231. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
  232. package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
  233. package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
  234. package/src/v2/lib/__tests__/slots.test.ts +0 -56
  235. package/src/v2/lib/processPartialHtml.ts +0 -45
  236. package/src/v2/lib/react-core.ts +0 -156
  237. package/src/v2/lib/slots.tsx +0 -184
  238. package/src/v2/lib/transcription-client.ts +0 -184
  239. package/src/v2/lib/utils.ts +0 -8
  240. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
  241. package/src/v2/providers/CopilotKitProvider.tsx +0 -800
  242. package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
  243. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
  244. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
  245. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
  246. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
  247. package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
  248. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
  249. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
  250. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
  251. package/src/v2/providers/index.ts +0 -21
  252. package/src/v2/styles/globals.css +0 -349
  253. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
  254. package/src/v2/types/defineToolCallRenderer.ts +0 -68
  255. package/src/v2/types/frontend-tool.ts +0 -8
  256. package/src/v2/types/human-in-the-loop.ts +0 -33
  257. package/src/v2/types/index.ts +0 -8
  258. package/src/v2/types/interrupt.ts +0 -15
  259. package/src/v2/types/react-activity-message-renderer.ts +0 -27
  260. package/src/v2/types/react-custom-message-renderer.ts +0 -17
  261. package/src/v2/types/react-tool-call-renderer.ts +0 -35
  262. package/src/v2/types/sandbox-function.ts +0 -11
  263. package/tsconfig.json +0 -8
  264. package/tsdown.config.ts +0 -193
  265. package/typedoc.json +0 -4
  266. package/vitest.config.mjs +0 -31
@@ -1,716 +0,0 @@
1
- import React, {
2
- useContext,
3
- useEffect,
4
- useLayoutEffect,
5
- useMemo,
6
- useReducer,
7
- useState,
8
- } from "react";
9
- import { useVirtualizer } from "@tanstack/react-virtual";
10
- import { ScrollElementContext } from "./scroll-element-context";
11
- import type { WithSlots } from "../../lib/slots";
12
- import { renderSlot, isReactComponentType } from "../../lib/slots";
13
- import CopilotChatAssistantMessage from "./CopilotChatAssistantMessage";
14
- import CopilotChatUserMessage from "./CopilotChatUserMessage";
15
- import CopilotChatReasoningMessage from "./CopilotChatReasoningMessage";
16
- import type {
17
- ActivityMessage,
18
- AssistantMessage,
19
- Message,
20
- ReasoningMessage,
21
- ToolMessage,
22
- UserMessage,
23
- } from "@ag-ui/core";
24
- import { twMerge } from "tailwind-merge";
25
- import { useRenderActivityMessage, useRenderCustomMessages } from "../../hooks";
26
- import { useCopilotKit } from "../../providers/CopilotKitProvider";
27
- import { useCopilotChatConfiguration } from "../../providers/CopilotChatConfigurationProvider";
28
- import { IntelligenceIndicator } from "../intelligence-indicator";
29
- import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
30
-
31
- /**
32
- * Resolves a slot value into a { Component, slotProps } pair, handling the three
33
- * slot forms: a component type, a className string, or a partial-props object.
34
- */
35
- function resolveSlotComponent<T extends React.ComponentType<any>>(
36
- slot: unknown,
37
- DefaultComponent: T,
38
- ): { Component: T; slotProps: Partial<React.ComponentProps<T>> | undefined } {
39
- if (isReactComponentType(slot)) {
40
- return { Component: slot as T, slotProps: undefined };
41
- }
42
- if (typeof slot === "string") {
43
- return {
44
- Component: DefaultComponent,
45
- slotProps: { className: slot } as unknown as Partial<
46
- React.ComponentProps<T>
47
- >,
48
- };
49
- }
50
- if (slot && typeof slot === "object") {
51
- return {
52
- Component: DefaultComponent,
53
- slotProps: slot as Partial<React.ComponentProps<T>>,
54
- };
55
- }
56
- return { Component: DefaultComponent, slotProps: undefined };
57
- }
58
-
59
- /**
60
- * Memoized wrapper for assistant messages to prevent re-renders when other messages change.
61
- */
62
- const MemoizedAssistantMessage = React.memo(
63
- function MemoizedAssistantMessage({
64
- message,
65
- messages,
66
- isRunning,
67
- AssistantMessageComponent,
68
- slotProps,
69
- }: {
70
- message: AssistantMessage;
71
- messages: Message[];
72
- isRunning: boolean;
73
- AssistantMessageComponent: typeof CopilotChatAssistantMessage;
74
- slotProps?: Partial<
75
- React.ComponentProps<typeof CopilotChatAssistantMessage>
76
- >;
77
- }) {
78
- return (
79
- <AssistantMessageComponent
80
- message={message}
81
- messages={messages}
82
- isRunning={isRunning}
83
- {...slotProps}
84
- />
85
- );
86
- },
87
- (prevProps, nextProps) => {
88
- // Only re-render if this specific message changed
89
- if (prevProps.message.id !== nextProps.message.id) return false;
90
- if (prevProps.message.content !== nextProps.message.content) return false;
91
-
92
- // Compare tool calls if present
93
- const prevToolCalls = prevProps.message.toolCalls;
94
- const nextToolCalls = nextProps.message.toolCalls;
95
- if (prevToolCalls?.length !== nextToolCalls?.length) return false;
96
- if (prevToolCalls && nextToolCalls) {
97
- for (let i = 0; i < prevToolCalls.length; i++) {
98
- const prevTc = prevToolCalls[i]!;
99
- const nextTc = nextToolCalls[i]!;
100
- if (prevTc.id !== nextTc.id) return false;
101
- if (prevTc.function.arguments !== nextTc.function.arguments)
102
- return false;
103
- }
104
- }
105
-
106
- // Check if tool results changed for this message's tool calls.
107
- // Tool results are separate messages with role="tool" that reference tool call IDs.
108
- if (prevToolCalls && prevToolCalls.length > 0) {
109
- const toolCallIds = new Set(prevToolCalls.map((tc) => tc.id));
110
-
111
- const prevToolResults = prevProps.messages.filter(
112
- (m): m is ToolMessage =>
113
- m.role === "tool" && toolCallIds.has(m.toolCallId),
114
- );
115
- const nextToolResults = nextProps.messages.filter(
116
- (m): m is ToolMessage =>
117
- m.role === "tool" && toolCallIds.has(m.toolCallId),
118
- );
119
-
120
- if (prevToolResults.length !== nextToolResults.length) return false;
121
-
122
- for (let i = 0; i < prevToolResults.length; i++) {
123
- if (prevToolResults[i]!.content !== nextToolResults[i]!.content)
124
- return false;
125
- }
126
- }
127
-
128
- // Only care about isRunning if this message is CURRENTLY the latest
129
- // (we don't need to re-render just because a message stopped being the latest)
130
- const nextIsLatest =
131
- nextProps.messages[nextProps.messages.length - 1]?.id ===
132
- nextProps.message.id;
133
- if (nextIsLatest && prevProps.isRunning !== nextProps.isRunning)
134
- return false;
135
-
136
- // Check if component reference changed
137
- if (
138
- prevProps.AssistantMessageComponent !==
139
- nextProps.AssistantMessageComponent
140
- )
141
- return false;
142
-
143
- // Check if slot props changed
144
- if (prevProps.slotProps !== nextProps.slotProps) return false;
145
-
146
- return true;
147
- },
148
- );
149
-
150
- /**
151
- * Memoized wrapper for user messages to prevent re-renders when other messages change.
152
- */
153
- const MemoizedUserMessage = React.memo(
154
- function MemoizedUserMessage({
155
- message,
156
- UserMessageComponent,
157
- slotProps,
158
- }: {
159
- message: UserMessage;
160
- UserMessageComponent: typeof CopilotChatUserMessage;
161
- slotProps?: Partial<React.ComponentProps<typeof CopilotChatUserMessage>>;
162
- }) {
163
- return <UserMessageComponent message={message} {...slotProps} />;
164
- },
165
- (prevProps, nextProps) => {
166
- // Only re-render if this specific message changed
167
- if (prevProps.message.id !== nextProps.message.id) return false;
168
- if (prevProps.message.content !== nextProps.message.content) return false;
169
- if (prevProps.UserMessageComponent !== nextProps.UserMessageComponent)
170
- return false;
171
- // Check if slot props changed
172
- if (prevProps.slotProps !== nextProps.slotProps) return false;
173
- return true;
174
- },
175
- );
176
-
177
- /**
178
- * Memoized wrapper for activity messages to prevent re-renders when other messages change.
179
- */
180
- const MemoizedActivityMessage = React.memo(
181
- function MemoizedActivityMessage({
182
- message,
183
- renderActivityMessage,
184
- }: {
185
- message: ActivityMessage;
186
- renderActivityMessage: (
187
- message: ActivityMessage,
188
- ) => React.ReactElement | null;
189
- }) {
190
- return renderActivityMessage(message);
191
- },
192
- (prevProps, nextProps) => {
193
- // Message ID changed = different message, must re-render
194
- if (prevProps.message.id !== nextProps.message.id) return false;
195
-
196
- // Activity type changed = must re-render
197
- if (prevProps.message.activityType !== nextProps.message.activityType)
198
- return false;
199
-
200
- // Compare content using JSON.stringify (native code, handles deep comparison)
201
- if (
202
- JSON.stringify(prevProps.message.content) !==
203
- JSON.stringify(nextProps.message.content)
204
- )
205
- return false;
206
-
207
- return true;
208
- },
209
- );
210
-
211
- /**
212
- * Memoized wrapper for reasoning messages to prevent re-renders when other messages change.
213
- */
214
- const MemoizedReasoningMessage = React.memo(
215
- function MemoizedReasoningMessage({
216
- message,
217
- messages,
218
- isRunning,
219
- ReasoningMessageComponent,
220
- slotProps,
221
- }: {
222
- message: ReasoningMessage;
223
- messages: Message[];
224
- isRunning: boolean;
225
- ReasoningMessageComponent: typeof CopilotChatReasoningMessage;
226
- slotProps?: Partial<
227
- React.ComponentProps<typeof CopilotChatReasoningMessage>
228
- >;
229
- }) {
230
- return (
231
- <ReasoningMessageComponent
232
- message={message}
233
- messages={messages}
234
- isRunning={isRunning}
235
- {...slotProps}
236
- />
237
- );
238
- },
239
- (prevProps, nextProps) => {
240
- // Only re-render if this specific message changed
241
- if (prevProps.message.id !== nextProps.message.id) return false;
242
- if (prevProps.message.content !== nextProps.message.content) return false;
243
-
244
- // Re-render when "latest" status changes (e.g. reasoning message is no longer the last message
245
- // because a text message was added after it — this transitions isStreaming from true to false)
246
- const prevIsLatest =
247
- prevProps.messages[prevProps.messages.length - 1]?.id ===
248
- prevProps.message.id;
249
- const nextIsLatest =
250
- nextProps.messages[nextProps.messages.length - 1]?.id ===
251
- nextProps.message.id;
252
- if (prevIsLatest !== nextIsLatest) return false;
253
-
254
- // Only care about isRunning if this message is CURRENTLY the latest
255
- if (nextIsLatest && prevProps.isRunning !== nextProps.isRunning)
256
- return false;
257
-
258
- // Check if component reference changed
259
- if (
260
- prevProps.ReasoningMessageComponent !==
261
- nextProps.ReasoningMessageComponent
262
- )
263
- return false;
264
-
265
- // Check if slot props changed
266
- if (prevProps.slotProps !== nextProps.slotProps) return false;
267
-
268
- return true;
269
- },
270
- );
271
-
272
- /**
273
- * Memoized wrapper for custom messages to prevent re-renders when other messages change.
274
- */
275
- const MemoizedCustomMessage = React.memo(
276
- function MemoizedCustomMessage({
277
- message,
278
- position,
279
- renderCustomMessage,
280
- }: {
281
- message: Message;
282
- position: "before" | "after";
283
- renderCustomMessage: (params: {
284
- message: Message;
285
- position: "before" | "after";
286
- }) => React.ReactElement | null;
287
- stateSnapshot?: unknown;
288
- }) {
289
- return renderCustomMessage({ message, position });
290
- },
291
- (prevProps, nextProps) => {
292
- // Only re-render if the message or position changed
293
- if (prevProps.message.id !== nextProps.message.id) return false;
294
- if (prevProps.position !== nextProps.position) return false;
295
- // Compare message content - for assistant messages this is a string, for others may differ
296
- if (prevProps.message.content !== nextProps.message.content) return false;
297
- if (prevProps.message.role !== nextProps.message.role) return false;
298
- // Compare state snapshot - custom renderers may depend on state
299
- if (
300
- JSON.stringify(prevProps.stateSnapshot) !==
301
- JSON.stringify(nextProps.stateSnapshot)
302
- )
303
- return false;
304
- // Note: We don't compare renderCustomMessage function reference because it changes
305
- // frequently. The message and state comparison is sufficient to determine if a re-render is needed.
306
- return true;
307
- },
308
- );
309
-
310
- /**
311
- * Deduplicates messages by ID. For assistant messages, merges occurrences:
312
- * recovers non-empty content from any earlier occurrence if the latest wiped it
313
- * (empty string means the streaming update cleared the field, not blank text),
314
- * and similarly recovers toolCalls from earlier occurrences if the latest is
315
- * undefined (an empty array [] is treated as intentional and kept as-is).
316
- * For all other roles, keeps the last entry.
317
- *
318
- * @internal Exported for unit testing only — not part of the public API.
319
- */
320
- export function deduplicateMessages(messages: Message[]): Message[] {
321
- const acc = new Map<string, Message>();
322
- for (const message of messages) {
323
- const existing = acc.get(message.id);
324
- if (
325
- existing &&
326
- message.role === "assistant" &&
327
- existing.role === "assistant"
328
- ) {
329
- // Empty string means the streaming update cleared the field — fall back to
330
- // any non-empty content seen earlier. Use { ...existing, ...message } so
331
- // fields present only in an earlier occurrence are not silently dropped.
332
- const content = message.content || existing.content;
333
- // undefined toolCalls means this chunk had no tool call activity — recover
334
- // from earlier occurrences. An explicit [] means all tool calls completed.
335
- const toolCalls = message.toolCalls ?? existing.toolCalls;
336
- acc.set(message.id, {
337
- ...existing,
338
- ...message,
339
- content,
340
- toolCalls,
341
- } as AssistantMessage);
342
- } else {
343
- acc.set(message.id, message);
344
- }
345
- }
346
- return [...acc.values()];
347
- }
348
-
349
- export type CopilotChatMessageViewProps = Omit<
350
- WithSlots<
351
- {
352
- assistantMessage: typeof CopilotChatAssistantMessage;
353
- userMessage: typeof CopilotChatUserMessage;
354
- reasoningMessage: typeof CopilotChatReasoningMessage;
355
- cursor: typeof CopilotChatMessageView.Cursor;
356
- },
357
- {
358
- isRunning?: boolean;
359
- messages?: Message[];
360
- } & React.HTMLAttributes<HTMLDivElement>
361
- >,
362
- "children"
363
- > & {
364
- children?: (props: {
365
- isRunning: boolean;
366
- messages: Message[];
367
- messageElements: React.ReactElement[];
368
- interruptElement: React.ReactElement | null;
369
- }) => React.ReactElement;
370
- };
371
-
372
- // Above this many messages, activate TanStack Virtual to avoid mounting the
373
- // full DOM tree. Below the threshold the overhead of virtualization isn't
374
- // worth it and the simpler flat render is faster.
375
- const VIRTUALIZE_THRESHOLD = 50;
376
-
377
- export function CopilotChatMessageView({
378
- messages = [],
379
- assistantMessage,
380
- userMessage,
381
- reasoningMessage,
382
- cursor,
383
- isRunning = false,
384
- children,
385
- className,
386
- ...props
387
- }: CopilotChatMessageViewProps) {
388
- const renderCustomMessage = useRenderCustomMessages();
389
- const { renderActivityMessage } = useRenderActivityMessage();
390
- const { copilotkit } = useCopilotKit();
391
- const config = useCopilotChatConfiguration();
392
- const [, forceUpdate] = useReducer((x) => x + 1, 0);
393
-
394
- // Subscribe to state changes so custom message renderers re-render when state updates.
395
- useEffect(() => {
396
- if (!config?.agentId) return;
397
- const agent = copilotkit.getAgent(config.agentId);
398
- if (!agent) return;
399
-
400
- const subscription = agent.subscribe({
401
- onStateChanged: forceUpdate,
402
- });
403
- return () => subscription.unsubscribe();
404
- }, [config?.agentId, copilotkit, forceUpdate]);
405
-
406
- // Subscribe to interrupt element changes for in-chat rendering.
407
- const [interruptElement, setInterruptElement] =
408
- useState<React.ReactElement | null>(null);
409
- useEffect(() => {
410
- setInterruptElement(copilotkit.interruptElement);
411
- const subscription = copilotkit.subscribe({
412
- onInterruptElementChanged: ({ interruptElement }) => {
413
- setInterruptElement(interruptElement);
414
- },
415
- });
416
- return () => subscription.unsubscribe();
417
- }, [copilotkit]);
418
-
419
- // Helper to get state snapshot for a message (used for memoization)
420
- const getStateSnapshotForMessage = (messageId: string): unknown => {
421
- if (!config) return undefined;
422
- const resolvedRunId =
423
- copilotkit.getRunIdForMessage(
424
- config.agentId,
425
- config.threadId,
426
- messageId,
427
- ) ??
428
- copilotkit
429
- .getRunIdsForThread(config.agentId, config.threadId)
430
- .slice(-1)[0];
431
- if (!resolvedRunId) return undefined;
432
- return copilotkit.getStateByRun(
433
- config.agentId,
434
- config.threadId,
435
- resolvedRunId,
436
- );
437
- };
438
-
439
- const deduplicatedMessages = useMemo(
440
- () => deduplicateMessages(messages),
441
- [messages],
442
- );
443
-
444
- if (
445
- process.env.NODE_ENV === "development" &&
446
- deduplicatedMessages.length < messages.length
447
- ) {
448
- console.warn(
449
- `CopilotChatMessageView: Merged ${messages.length - deduplicatedMessages.length} message(s) with duplicate IDs.`,
450
- );
451
- }
452
-
453
- // Resolve slot values once per prop change rather than inside renderMessageBlock.
454
- // resolveSlotComponent returns a new object every call when the slot is a CSS
455
- // class string, which would defeat MemoizedAssistantMessage's slotProps
456
- // reference-equality check and cause all completed messages to re-render.
457
- const { Component: AssistantComponent, slotProps: assistantSlotProps } =
458
- useMemo(
459
- () => resolveSlotComponent(assistantMessage, CopilotChatAssistantMessage),
460
- [assistantMessage],
461
- );
462
- const { Component: UserComponent, slotProps: userSlotProps } = useMemo(
463
- () => resolveSlotComponent(userMessage, CopilotChatUserMessage),
464
- [userMessage],
465
- );
466
- const { Component: ReasoningComponent, slotProps: reasoningSlotProps } =
467
- useMemo(
468
- () => resolveSlotComponent(reasoningMessage, CopilotChatReasoningMessage),
469
- [reasoningMessage],
470
- );
471
-
472
- // ---------------------------------------------------------------------------
473
- // Virtualization
474
- // ---------------------------------------------------------------------------
475
- // Receive the scroll container from context. ScrollView provides the element
476
- // as state (not a ref) so this component re-renders reactively when the
477
- // container first mounts. clientHeight === 0 means no real layout (jsdom) —
478
- // skip virtualization so tests run the flat path.
479
- const scrollElementFromCtx = useContext(ScrollElementContext);
480
- const scrollElement =
481
- scrollElementFromCtx && scrollElementFromCtx.clientHeight > 0
482
- ? scrollElementFromCtx
483
- : null;
484
-
485
- // Warn once in dev when a scroll element is provided but has no height —
486
- // this silently disables virtualization (e.g. chat inside display:none tab).
487
- useEffect(() => {
488
- if (
489
- process.env.NODE_ENV !== "production" &&
490
- scrollElementFromCtx &&
491
- scrollElementFromCtx.clientHeight === 0
492
- ) {
493
- console.warn(
494
- "[CopilotKit] Chat scroll container has clientHeight=0 — virtualization disabled. " +
495
- "Ensure the chat is rendered in a visible container with a non-zero height.",
496
- );
497
- }
498
- }, [scrollElementFromCtx]);
499
-
500
- // Virtualize only when we have a scroll element and enough messages. The
501
- // `children` render prop delegates layout to the caller, so we keep
502
- // messageElements flat for that case.
503
- const shouldVirtualize =
504
- !!scrollElement &&
505
- !children &&
506
- deduplicatedMessages.length > VIRTUALIZE_THRESHOLD;
507
-
508
- const virtualizer = useVirtualizer({
509
- // count=0 disables the virtualizer without changing hook call order.
510
- count: shouldVirtualize ? deduplicatedMessages.length : 0,
511
- getScrollElement: () => scrollElement,
512
- // Conservative height estimate. Items are measured by ResizeObserver after
513
- // first render so the estimate only affects the initial total height.
514
- estimateSize: () => 100,
515
- overscan: 5,
516
- measureElement: (el: Element) => el?.getBoundingClientRect().height ?? 0,
517
- // Assume a 600 px viewport before the real element is measured so that
518
- // the first virtual render shows ~6 items rather than 0.
519
- initialRect: { width: 0, height: 600 },
520
- });
521
-
522
- // Scroll to the bottom when virtual mode first activates or the thread changes
523
- // (detected by the first message ID changing). For streaming new messages,
524
- // use-stick-to-bottom handles auto-scroll via content height growth detection
525
- // on the virtualizer's total-size div — same as the flat path. Adding
526
- // deduplicatedMessages.length here would forcibly yank the user to the bottom
527
- // on every streaming chunk even if they've scrolled up to read history.
528
- const firstMessageId = deduplicatedMessages[0]?.id;
529
- useLayoutEffect(() => {
530
- if (!shouldVirtualize || !deduplicatedMessages.length) return;
531
- virtualizer.scrollToIndex(deduplicatedMessages.length - 1, {
532
- align: "end",
533
- });
534
- // eslint-disable-next-line react-hooks/exhaustive-deps
535
- }, [shouldVirtualize, firstMessageId]);
536
-
537
- // ---------------------------------------------------------------------------
538
- // Per-message rendering helper (shared by flat and virtual paths)
539
- // ---------------------------------------------------------------------------
540
- const renderMessageBlock = (message: Message): React.ReactElement[] => {
541
- const elements: (React.ReactElement | null | undefined)[] = [];
542
- const stateSnapshot = getStateSnapshotForMessage(message.id);
543
-
544
- if (renderCustomMessage) {
545
- elements.push(
546
- <MemoizedCustomMessage
547
- key={`${message.id}-custom-before`}
548
- message={message}
549
- position="before"
550
- renderCustomMessage={renderCustomMessage}
551
- stateSnapshot={stateSnapshot}
552
- />,
553
- );
554
- }
555
-
556
- if (message.role === "assistant") {
557
- elements.push(
558
- <MemoizedAssistantMessage
559
- key={message.id}
560
- message={message as AssistantMessage}
561
- messages={messages}
562
- isRunning={isRunning}
563
- AssistantMessageComponent={AssistantComponent}
564
- slotProps={assistantSlotProps}
565
- />,
566
- );
567
- } else if (message.role === "user") {
568
- elements.push(
569
- <MemoizedUserMessage
570
- key={message.id}
571
- message={message as UserMessage}
572
- UserMessageComponent={UserComponent}
573
- slotProps={userSlotProps}
574
- />,
575
- );
576
- } else if (message.role === "activity") {
577
- elements.push(
578
- <MemoizedActivityMessage
579
- key={message.id}
580
- message={message as ActivityMessage}
581
- renderActivityMessage={renderActivityMessage}
582
- />,
583
- );
584
- } else if (message.role === "reasoning") {
585
- elements.push(
586
- <MemoizedReasoningMessage
587
- key={message.id}
588
- message={message as ReasoningMessage}
589
- messages={messages}
590
- isRunning={isRunning}
591
- ReasoningMessageComponent={ReasoningComponent}
592
- slotProps={reasoningSlotProps}
593
- />,
594
- );
595
- }
596
-
597
- if (renderCustomMessage) {
598
- elements.push(
599
- <MemoizedCustomMessage
600
- key={`${message.id}-custom-after`}
601
- message={message}
602
- position="after"
603
- renderCustomMessage={renderCustomMessage}
604
- stateSnapshot={stateSnapshot}
605
- />,
606
- );
607
- }
608
-
609
- // Auto-mount the IntelligenceIndicator on assistant message slots
610
- // when the runtime is in intelligence mode. The component self-gates
611
- // further (latest matching-assistant slot, pending tool-call grace
612
- // window) so only one pill renders at a time — mounting only for
613
- // assistant messages avoids the per-slot `useAgent` subscription
614
- // and four effects on user/reasoning/activity slots that would just
615
- // return null at the role gate anyway.
616
- if (copilotkit.intelligence !== undefined && message.role === "assistant") {
617
- elements.push(
618
- <IntelligenceIndicator
619
- key={`${message.id}-intelligence`}
620
- message={message}
621
- agentId={config?.agentId ?? DEFAULT_AGENT_ID}
622
- />,
623
- );
624
- }
625
-
626
- return elements.filter(Boolean) as React.ReactElement[];
627
- };
628
-
629
- // Build the flat element list only when we're not virtualizing (avoids
630
- // creating 500 React elements that we'd immediately discard).
631
- const messageElements: React.ReactElement[] = shouldVirtualize
632
- ? []
633
- : deduplicatedMessages.flatMap(renderMessageBlock);
634
-
635
- // ---------------------------------------------------------------------------
636
- // children render prop (custom layout, always non-virtual)
637
- // ---------------------------------------------------------------------------
638
- if (children) {
639
- return (
640
- <div data-copilotkit style={{ display: "contents" }}>
641
- {children({ messageElements, messages, isRunning, interruptElement })}
642
- </div>
643
- );
644
- }
645
-
646
- // Hide the chat-level loading cursor when the last message is a reasoning
647
- // message — the reasoning card already shows its own loading indicator.
648
- const lastMessage = messages[messages.length - 1];
649
- const showCursor = isRunning && lastMessage?.role !== "reasoning";
650
-
651
- // ---------------------------------------------------------------------------
652
- // Render — shared wrapper, conditional inner content (virtual vs flat)
653
- // ---------------------------------------------------------------------------
654
- return (
655
- <div
656
- data-copilotkit
657
- data-testid="copilot-message-list"
658
- className={twMerge("copilotKitMessages cpk:flex cpk:flex-col", className)}
659
- {...props}
660
- >
661
- {shouldVirtualize ? (
662
- // Virtual path: only visible items are in the DOM; outer div maintains
663
- // total scroll height so the scrollbar reflects the full list size.
664
- <div
665
- style={{ height: virtualizer.getTotalSize(), position: "relative" }}
666
- >
667
- {virtualizer.getVirtualItems().map((virtualItem) => {
668
- const message = deduplicatedMessages[virtualItem.index]!;
669
- return (
670
- <div
671
- key={message.id}
672
- data-index={virtualItem.index}
673
- ref={virtualizer.measureElement}
674
- style={{
675
- position: "absolute",
676
- top: 0,
677
- left: 0,
678
- width: "100%",
679
- transform: `translateY(${virtualItem.start}px)`,
680
- }}
681
- >
682
- {renderMessageBlock(message)}
683
- </div>
684
- );
685
- })}
686
- </div>
687
- ) : (
688
- messageElements
689
- )}
690
- {interruptElement}
691
- {showCursor && (
692
- <div className="cpk:mt-2">
693
- {renderSlot(cursor, CopilotChatMessageView.Cursor, {})}
694
- </div>
695
- )}
696
- </div>
697
- );
698
- }
699
-
700
- CopilotChatMessageView.Cursor = function Cursor({
701
- className,
702
- ...props
703
- }: React.HTMLAttributes<HTMLDivElement>) {
704
- return (
705
- <div
706
- data-testid="copilot-loading-cursor"
707
- className={twMerge(
708
- "cpk:w-[11px] cpk:h-[11px] cpk:rounded-full cpk:bg-foreground cpk:animate-pulse-cursor cpk:ml-1",
709
- className,
710
- )}
711
- {...props}
712
- />
713
- );
714
- };
715
-
716
- export default CopilotChatMessageView;