@copilotkit/react-core 1.57.3 → 1.58.0

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 (282) hide show
  1. package/LICENSE +21 -0
  2. package/dist/{copilotkit-CC8DjOiC.mjs → copilotkit-BIn7HE8f.mjs} +2 -2
  3. package/dist/{copilotkit-CC8DjOiC.mjs.map → copilotkit-BIn7HE8f.mjs.map} +1 -1
  4. package/dist/{copilotkit-CtXcs1ea.cjs → copilotkit-Drw-g6zA.cjs} +2 -2
  5. package/dist/{copilotkit-CtXcs1ea.cjs.map → copilotkit-Drw-g6zA.cjs.map} +1 -1
  6. package/dist/index.cjs +3 -77
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +2 -2
  9. package/dist/index.d.mts +2 -2
  10. package/dist/index.mjs +3 -77
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/index.umd.js +3 -77
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/v2/index.cjs +1 -1
  15. package/dist/v2/index.mjs +1 -1
  16. package/dist/v2/index.umd.js +1 -1
  17. package/dist/v2/index.umd.js.map +1 -1
  18. package/package.json +12 -13
  19. package/skills/react-core/SKILL.md +108 -0
  20. package/skills/react-core/references/agent-access.md +288 -0
  21. package/skills/react-core/references/attachments.md +291 -0
  22. package/skills/react-core/references/capabilities.md +138 -0
  23. package/skills/react-core/references/chat-components.md +221 -0
  24. package/skills/react-core/references/client-side-tools.md +358 -0
  25. package/skills/react-core/references/custom-message-renderers.md +226 -0
  26. package/skills/react-core/references/debug-mode.md +153 -0
  27. package/skills/react-core/references/human-in-the-loop.md +312 -0
  28. package/skills/react-core/references/provider-setup.md +326 -0
  29. package/skills/react-core/references/rendering-activity-messages.md +207 -0
  30. package/skills/react-core/references/rendering-tool-calls.md +319 -0
  31. package/skills/react-core/references/suggestions.md +211 -0
  32. package/skills/react-core/references/switching-agents-recipes.md +160 -0
  33. package/skills/react-core/references/switching-agents.md +231 -0
  34. package/skills/react-core/references/threads.md +226 -0
  35. package/.attw.json +0 -3
  36. package/CHANGELOG.md +0 -5043
  37. package/scripts/scope-preflight.mjs +0 -100
  38. package/src/components/CopilotListeners.tsx +0 -137
  39. package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
  40. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
  41. package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
  42. package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
  43. package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
  44. package/src/components/copilot-provider/copilot-messages.tsx +0 -314
  45. package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
  46. package/src/components/copilot-provider/copilotkit.tsx +0 -853
  47. package/src/components/copilot-provider/index.ts +0 -3
  48. package/src/components/dev-console/console-trigger.tsx +0 -283
  49. package/src/components/dev-console/developer-console-modal.tsx +0 -1016
  50. package/src/components/dev-console/icons.tsx +0 -106
  51. package/src/components/error-boundary/error-boundary.tsx +0 -99
  52. package/src/components/error-boundary/error-utils.tsx +0 -105
  53. package/src/components/index.ts +0 -1
  54. package/src/components/toast/exclamation-mark-icon.tsx +0 -27
  55. package/src/components/toast/toast-provider.tsx +0 -448
  56. package/src/components/usage-banner.tsx +0 -266
  57. package/src/context/__tests__/threads-context.test.tsx +0 -141
  58. package/src/context/coagent-state-renders-context.tsx +0 -89
  59. package/src/context/copilot-context.tsx +0 -365
  60. package/src/context/copilot-messages-context.tsx +0 -35
  61. package/src/context/index.ts +0 -22
  62. package/src/context/threads-context.tsx +0 -69
  63. package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
  64. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
  65. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
  66. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
  67. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
  68. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
  69. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
  70. package/src/hooks/index.ts +0 -33
  71. package/src/hooks/use-agent-nodename.ts +0 -33
  72. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
  73. package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
  74. package/src/hooks/use-coagent-state-render-registry.ts +0 -230
  75. package/src/hooks/use-coagent-state-render.ts +0 -163
  76. package/src/hooks/use-coagent.ts +0 -377
  77. package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
  78. package/src/hooks/use-copilot-action.ts +0 -245
  79. package/src/hooks/use-copilot-additional-instructions.ts +0 -98
  80. package/src/hooks/use-copilot-authenticated-action.ts +0 -73
  81. package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
  82. package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
  83. package/src/hooks/use-copilot-chat.ts +0 -132
  84. package/src/hooks/use-copilot-chat_internal.ts +0 -875
  85. package/src/hooks/use-copilot-readable.ts +0 -135
  86. package/src/hooks/use-copilot-runtime-client.ts +0 -178
  87. package/src/hooks/use-default-tool.ts +0 -13
  88. package/src/hooks/use-flat-category-store.ts +0 -109
  89. package/src/hooks/use-frontend-tool.ts +0 -113
  90. package/src/hooks/use-human-in-the-loop.ts +0 -138
  91. package/src/hooks/use-langgraph-interrupt.ts +0 -103
  92. package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
  93. package/src/hooks/use-make-copilot-document-readable.ts +0 -30
  94. package/src/hooks/use-render-tool-call.ts +0 -89
  95. package/src/hooks/use-tree.ts +0 -222
  96. package/src/index.tsx +0 -7
  97. package/src/lib/copilot-task.ts +0 -215
  98. package/src/lib/index.ts +0 -1
  99. package/src/lib/status-checker.ts +0 -67
  100. package/src/setupTests.ts +0 -37
  101. package/src/test-helpers/copilot-context.ts +0 -91
  102. package/src/types/chat-suggestion-configuration.ts +0 -23
  103. package/src/types/coagent-action.ts +0 -35
  104. package/src/types/coagent-state.ts +0 -13
  105. package/src/types/crew.ts +0 -89
  106. package/src/types/document-pointer.ts +0 -7
  107. package/src/types/frontend-action.ts +0 -213
  108. package/src/types/index.ts +0 -17
  109. package/src/types/interrupt-action.ts +0 -58
  110. package/src/types/system-message.ts +0 -4
  111. package/src/utils/dev-console.ts +0 -19
  112. package/src/utils/index.ts +0 -2
  113. package/src/utils/suggestions-constants.ts +0 -8
  114. package/src/utils/utils.test.ts +0 -7
  115. package/src/utils/utils.ts +0 -6
  116. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
  117. package/src/v2/__tests__/globalSetup.ts +0 -14
  118. package/src/v2/__tests__/setup.ts +0 -93
  119. package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
  120. package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
  121. package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
  122. package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
  123. package/src/v2/components/CopilotKitInspector.tsx +0 -52
  124. package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
  125. package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
  126. package/src/v2/components/WildcardToolCallRender.tsx +0 -86
  127. package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
  128. package/src/v2/components/chat/CopilotChat.tsx +0 -664
  129. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
  130. package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
  131. package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
  132. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
  133. package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
  134. package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
  135. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
  136. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
  137. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
  138. package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
  139. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
  140. package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
  141. package/src/v2/components/chat/CopilotChatView.tsx +0 -890
  142. package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
  143. package/src/v2/components/chat/CopilotPopup.tsx +0 -81
  144. package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
  145. package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
  146. package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
  147. package/src/v2/components/chat/Lightbox.tsx +0 -103
  148. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
  149. package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
  150. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
  151. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
  152. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
  153. package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
  154. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
  155. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
  156. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
  157. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
  158. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
  159. package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
  160. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
  161. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
  162. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
  163. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
  164. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
  165. package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
  166. package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
  167. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
  168. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
  169. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
  170. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
  171. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
  172. package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
  173. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
  174. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
  175. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
  176. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
  177. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
  178. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
  179. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
  180. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
  181. package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
  182. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
  183. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
  184. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
  185. package/src/v2/components/chat/__tests__/setup.ts +0 -1
  186. package/src/v2/components/chat/index.ts +0 -90
  187. package/src/v2/components/chat/last-user-message-context.ts +0 -21
  188. package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
  189. package/src/v2/components/chat/scroll-element-context.ts +0 -13
  190. package/src/v2/components/index.ts +0 -8
  191. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
  192. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
  193. package/src/v2/components/intelligence-indicator/index.ts +0 -2
  194. package/src/v2/components/license-warning-banner.tsx +0 -217
  195. package/src/v2/components/ui/button.tsx +0 -124
  196. package/src/v2/components/ui/dropdown-menu.tsx +0 -258
  197. package/src/v2/components/ui/tooltip.tsx +0 -60
  198. package/src/v2/context.ts +0 -62
  199. package/src/v2/headless.ts +0 -64
  200. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
  201. package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
  202. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
  203. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
  204. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
  205. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
  206. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
  207. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
  208. package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
  209. package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
  210. package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
  211. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
  212. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
  213. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
  214. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
  215. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
  216. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
  217. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
  218. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
  219. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
  220. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
  221. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
  222. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
  223. package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
  224. package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
  225. package/src/v2/hooks/index.ts +0 -24
  226. package/src/v2/hooks/use-agent-context.tsx +0 -45
  227. package/src/v2/hooks/use-agent.tsx +0 -227
  228. package/src/v2/hooks/use-attachments.tsx +0 -269
  229. package/src/v2/hooks/use-capabilities.tsx +0 -25
  230. package/src/v2/hooks/use-component.tsx +0 -91
  231. package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
  232. package/src/v2/hooks/use-default-render-tool.tsx +0 -271
  233. package/src/v2/hooks/use-frontend-tool.tsx +0 -46
  234. package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
  235. package/src/v2/hooks/use-interrupt.tsx +0 -305
  236. package/src/v2/hooks/use-keyboard-height.tsx +0 -67
  237. package/src/v2/hooks/use-pin-to-send.ts +0 -94
  238. package/src/v2/hooks/use-render-activity-message.tsx +0 -72
  239. package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
  240. package/src/v2/hooks/use-render-tool-call.tsx +0 -208
  241. package/src/v2/hooks/use-render-tool.tsx +0 -184
  242. package/src/v2/hooks/use-suggestions.tsx +0 -91
  243. package/src/v2/hooks/use-threads.tsx +0 -325
  244. package/src/v2/hooks/useKatexStyles.ts +0 -27
  245. package/src/v2/index.css +0 -1
  246. package/src/v2/index.ts +0 -27
  247. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
  248. package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
  249. package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
  250. package/src/v2/lib/__tests__/slots.test.ts +0 -56
  251. package/src/v2/lib/processPartialHtml.ts +0 -45
  252. package/src/v2/lib/react-core.ts +0 -156
  253. package/src/v2/lib/slots.tsx +0 -184
  254. package/src/v2/lib/transcription-client.ts +0 -184
  255. package/src/v2/lib/utils.ts +0 -8
  256. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
  257. package/src/v2/providers/CopilotKitProvider.tsx +0 -800
  258. package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
  259. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
  260. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
  261. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
  262. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
  263. package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
  264. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
  265. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
  266. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
  267. package/src/v2/providers/index.ts +0 -21
  268. package/src/v2/styles/globals.css +0 -349
  269. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
  270. package/src/v2/types/defineToolCallRenderer.ts +0 -68
  271. package/src/v2/types/frontend-tool.ts +0 -8
  272. package/src/v2/types/human-in-the-loop.ts +0 -33
  273. package/src/v2/types/index.ts +0 -8
  274. package/src/v2/types/interrupt.ts +0 -15
  275. package/src/v2/types/react-activity-message-renderer.ts +0 -27
  276. package/src/v2/types/react-custom-message-renderer.ts +0 -17
  277. package/src/v2/types/react-tool-call-renderer.ts +0 -35
  278. package/src/v2/types/sandbox-function.ts +0 -11
  279. package/tsconfig.json +0 -8
  280. package/tsdown.config.ts +0 -193
  281. package/typedoc.json +0 -4
  282. 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;