@copilotkit/react-core 1.57.2 → 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 -296
  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,222 +0,0 @@
1
- import { ReactCustomMessageRendererPosition, useAgent } from "../v2";
2
- import { useCallback, useEffect, useMemo, useState } from "react";
3
- import type { AgentSubscriber } from "@ag-ui/client";
4
- import { useCoAgentStateRenders } from "../context";
5
- import { parseJson } from "@copilotkit/shared";
6
- import { RenderStatus } from "./use-coagent-state-render-bridge.helpers";
7
- import { useStateRenderRegistry } from "./use-coagent-state-render-registry";
8
-
9
- /**
10
- * Bridge hook that connects agent state renders to chat messages.
11
- *
12
- * ## Purpose
13
- * This hook finds matching state render configurations (registered via useCoAgentStateRender)
14
- * and returns UI to render in chat.
15
- * It ensures each state render appears bound to a specific message, preventing duplicates while
16
- * allowing re-binding when the underlying state changes significantly.
17
- *
18
- * ## Message-ID-Based Claiming System
19
- *
20
- * ### The Problem
21
- * Multiple bridge component instances render simultaneously (one per message). Without coordination,
22
- * they would all try to render the same state render, causing duplicates.
23
- *
24
- * ### The Solution: Message-ID Claims with State Comparison
25
- * Each state render is "claimed" by exactly one **message ID** (not runId):
26
- *
27
- * **Claim Structure**: `claimsRef.current[messageId] = { stateRenderId, runId, stateSnapshot, locked }`
28
- *
29
- * **Primary binding is by messageId because**:
30
- * - runId is not always available immediately (starts as "pending")
31
- * - messageId is the stable identifier throughout the message lifecycle
32
- * - Claims persist across component remounts via context ref
33
- *
34
- * ### Claiming Logic Flow
35
- *
36
- * 1. **Message already has a claim**:
37
- * - Check if the claim matches the current stateRenderId
38
- * - If yes → render (this message owns this render)
39
- * - Update runId if it was "pending" and now available
40
- *
41
- * 2. **State render claimed by another message**:
42
- * - Compare state snapshots (ignoring constant keys: messages, tools, copilotkit)
43
- * - If states are identical → block rendering (duplicate)
44
- * - **If states are different → allow claiming** (new data, new message)
45
- * - This handles cases where the same render type shows different states in different messages
46
- *
47
- * 3. **Unclaimed state render**:
48
- * - Only allow claiming if runId is "pending" (initial render)
49
- * - If runId is real but no claim exists → block (edge case protection)
50
- * - Create new claim: `claimsRef.current[messageId] = { stateRenderId, runId }`
51
- *
52
- * ### State Snapshot Locking
53
- *
54
- * Once a state snapshot is captured and locked for a message:
55
- * - The UI always renders with the locked snapshot (not live agent.state)
56
- * - Prevents UI from appearing "wiped" during state transitions
57
- * - Locked when: stateSnapshot prop is available (from message persistence)
58
- * - Unlocked state: can still update from live agent.state
59
- *
60
- * ### Synchronous Claiming (Ref-based)
61
- *
62
- * Claims are stored in a context-level ref (not React state):
63
- * - Multiple bridges render in the same tick
64
- * - State updates are async - would allow duplicates before update completes
65
- * - Ref provides immediate, synchronous claim checking
66
- * - Survives component remounts (stored in context, not component)
67
- *
68
- * ## Flow Example
69
- *
70
- * ```
71
- * Time 1: Message A renders, runId=undefined, state={progress: 50%}
72
- * → effectiveRunId = "pending"
73
- * → Claims: claimsRef["msgA"] = { stateRenderId: "tasks", runId: "pending", stateSnapshot: {progress: 50%} }
74
- * → Renders UI with 50% progress
75
- *
76
- * Time 2: Message B renders, runId=undefined, same state
77
- * → Checks: "tasks" already claimed by msgA with same state
78
- * → Returns null (blocked - duplicate)
79
- *
80
- * Time 3: Real runId appears (e.g., "run-123")
81
- * → Updates claim: claimsRef["msgA"].runId = "run-123"
82
- * → Message A continues rendering
83
- *
84
- * Time 4: Agent processes more, state={progress: 100%}
85
- * → Message A: locked to 50% (stateSnapshot locked)
86
- * → Message C renders with state={progress: 100%}
87
- * → Checks: "tasks" claimed by msgA but state is DIFFERENT (50% vs 100%)
88
- * → Allows new claim: claimsRef["msgC"] = { stateRenderId: "tasks", runId: "run-123", stateSnapshot: {progress: 100%} }
89
- * → Both messages render independently with their own snapshots
90
- * ```
91
- */
92
- export interface CoAgentStateRenderBridgeProps {
93
- message: any;
94
- position: ReactCustomMessageRendererPosition;
95
- runId: string;
96
- messageIndex: number;
97
- messageIndexInRun: number;
98
- numberOfMessagesInRun: number;
99
- agentId: string;
100
- stateSnapshot: any;
101
- }
102
-
103
- export function useCoagentStateRenderBridge(
104
- agentId: string,
105
- props: CoAgentStateRenderBridgeProps,
106
- ) {
107
- const { stateSnapshot, message } = props;
108
- const { coAgentStateRenders, claimsRef } = useCoAgentStateRenders();
109
- const { agent } = useAgent({ agentId });
110
- const [nodeName, setNodeName] = useState<string | undefined>(undefined);
111
- const [, forceUpdate] = useState(0);
112
-
113
- useEffect(() => {
114
- if (!agent) return;
115
- const subscriber: AgentSubscriber = {
116
- onStateChanged: () => {
117
- forceUpdate((value) => value + 1);
118
- },
119
- onStepStartedEvent: ({ event }) => {
120
- if (event.stepName !== nodeName) {
121
- setNodeName(event.stepName);
122
- }
123
- },
124
- onStepFinishedEvent: ({ event }) => {
125
- if (event.stepName === nodeName) {
126
- setNodeName(undefined);
127
- }
128
- },
129
- };
130
-
131
- const { unsubscribe } = agent.subscribe(subscriber);
132
- return () => {
133
- unsubscribe();
134
- };
135
- // eslint-disable-next-line react-hooks/exhaustive-deps
136
- }, [agentId, nodeName]);
137
-
138
- const getStateRender = useCallback(
139
- (messageId: string) => {
140
- return Object.entries(coAgentStateRenders).find(
141
- ([stateRenderId, stateRender]) => {
142
- if (claimsRef.current[messageId]) {
143
- return stateRenderId === claimsRef.current[messageId].stateRenderId;
144
- }
145
- const matchingAgentName = stateRender.name === agentId;
146
- const matchesNodeContext = stateRender.nodeName
147
- ? stateRender.nodeName === nodeName
148
- : true;
149
- return matchingAgentName && matchesNodeContext;
150
- },
151
- );
152
- },
153
- [coAgentStateRenders, nodeName, agentId],
154
- );
155
- const stateRenderEntry = useMemo(
156
- () => getStateRender(message.id),
157
- [getStateRender, message.id],
158
- );
159
- const stateRenderId = stateRenderEntry?.[0];
160
- const stateRender = stateRenderEntry?.[1];
161
-
162
- const registryMessage = {
163
- ...message,
164
- runId: props.runId ?? message.runId,
165
- };
166
- const { canRender } = useStateRenderRegistry({
167
- agentId,
168
- stateRenderId,
169
- message: registryMessage,
170
- messageIndex: props.messageIndex,
171
- stateSnapshot,
172
- agentState: agent?.state,
173
- agentMessages: agent?.messages,
174
- claimsRef,
175
- });
176
-
177
- return useMemo(() => {
178
- if (!stateRender || !stateRenderId) {
179
- return null;
180
- }
181
- if (!canRender) {
182
- return null;
183
- }
184
-
185
- if (stateRender.handler) {
186
- stateRender.handler({
187
- state: stateSnapshot
188
- ? parseJson(stateSnapshot, stateSnapshot)
189
- : (agent?.state ?? {}),
190
- nodeName: nodeName ?? "",
191
- });
192
- }
193
-
194
- if (stateRender.render) {
195
- const status = agent?.isRunning
196
- ? RenderStatus.InProgress
197
- : RenderStatus.Complete;
198
-
199
- if (typeof stateRender.render === "string") return stateRender.render;
200
-
201
- return stateRender.render({
202
- status,
203
- // Always use state from claim, to make sure the state does not seem "wiped" for a fraction of a second
204
- state: claimsRef.current[message.id].stateSnapshot ?? {},
205
- nodeName: nodeName ?? "",
206
- });
207
- }
208
- }, [
209
- stateRender,
210
- stateRenderId,
211
- agent?.state,
212
- agent?.isRunning,
213
- nodeName,
214
- message.id,
215
- stateSnapshot,
216
- canRender,
217
- ]);
218
- }
219
-
220
- export function CoAgentStateRenderBridge(props: CoAgentStateRenderBridgeProps) {
221
- return useCoagentStateRenderBridge(props.agentId, props);
222
- }
@@ -1,230 +0,0 @@
1
- import { useEffect } from "react";
2
- import {
3
- areStatesEquals,
4
- ClaimAction,
5
- getEffectiveRunId,
6
- isPlaceholderMessageId,
7
- isPlaceholderMessageName,
8
- readCachedMessageEntry,
9
- resolveClaim,
10
- selectSnapshot,
11
- type Claim,
12
- type ClaimsByMessageId,
13
- type SnapshotCaches,
14
- type StateRenderContext,
15
- } from "./use-coagent-state-render-bridge.helpers";
16
-
17
- export interface StateRenderRegistryInput {
18
- agentId: string;
19
- stateRenderId?: string;
20
- message: { id: string; runId?: string; name?: string };
21
- messageIndex?: number;
22
- stateSnapshot?: any;
23
- agentState?: any;
24
- agentMessages?: Array<{ id: string; role?: string }>;
25
- claimsRef: React.MutableRefObject<Record<string, Claim>>;
26
- }
27
-
28
- export interface StateRenderRegistryResult {
29
- canRender: boolean;
30
- }
31
-
32
- const LAST_SNAPSHOTS_BY_RENDER_AND_RUN = "__lastSnapshotsByStateRenderIdAndRun";
33
- const LAST_SNAPSHOTS_BY_MESSAGE = "__lastSnapshotsByMessageId";
34
-
35
- type SnapshotByMessageEntry = { snapshot: any; runId?: string } | any;
36
- type ClaimsStore = Record<string, Claim> & {
37
- [LAST_SNAPSHOTS_BY_RENDER_AND_RUN]?: Record<string, any>;
38
- [LAST_SNAPSHOTS_BY_MESSAGE]?: Record<string, SnapshotByMessageEntry>;
39
- };
40
-
41
- function getClaimsStore(
42
- claimsRef: React.MutableRefObject<Record<string, Claim>>,
43
- ): ClaimsStore {
44
- return claimsRef.current as ClaimsStore;
45
- }
46
-
47
- function getSnapshotCaches(
48
- claimsRef: React.MutableRefObject<Record<string, Claim>>,
49
- ): SnapshotCaches {
50
- const store = getClaimsStore(claimsRef);
51
- return {
52
- byStateRenderAndRun: store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] ?? {},
53
- byMessageId: store[LAST_SNAPSHOTS_BY_MESSAGE] ?? {},
54
- };
55
- }
56
-
57
- export function useStateRenderRegistry({
58
- agentId,
59
- stateRenderId,
60
- message,
61
- messageIndex,
62
- stateSnapshot,
63
- agentState,
64
- agentMessages,
65
- claimsRef,
66
- }: StateRenderRegistryInput): StateRenderRegistryResult {
67
- const store = getClaimsStore(claimsRef);
68
- const runId = message.runId;
69
- const cachedMessageEntry = store[LAST_SNAPSHOTS_BY_MESSAGE]?.[message.id];
70
- const { runId: cachedMessageRunId } =
71
- readCachedMessageEntry(cachedMessageEntry);
72
- const existingClaimRunId = claimsRef.current[message.id]?.runId;
73
- const effectiveRunId = getEffectiveRunId({
74
- existingClaimRunId,
75
- cachedMessageRunId,
76
- runId,
77
- });
78
-
79
- useEffect(() => {
80
- return () => {
81
- const existingClaim = claimsRef.current[message.id];
82
- if (
83
- existingClaim?.stateSnapshot &&
84
- Object.keys(existingClaim.stateSnapshot).length > 0
85
- ) {
86
- const snapshotCache = {
87
- ...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
88
- };
89
- const cacheKey = `${existingClaim.stateRenderId}::${existingClaim.runId ?? "pending"}`;
90
- snapshotCache[cacheKey] = existingClaim.stateSnapshot;
91
- snapshotCache[`${existingClaim.stateRenderId}::latest`] =
92
- existingClaim.stateSnapshot;
93
- store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
94
-
95
- const messageCache = {
96
- ...store[LAST_SNAPSHOTS_BY_MESSAGE],
97
- };
98
- messageCache[message.id] = {
99
- snapshot: existingClaim.stateSnapshot,
100
- runId: existingClaim.runId ?? effectiveRunId,
101
- };
102
- store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
103
- }
104
- delete claimsRef.current[message.id];
105
- };
106
- }, [claimsRef, effectiveRunId, message.id]);
107
-
108
- if (!stateRenderId) {
109
- return { canRender: false };
110
- }
111
-
112
- const caches = getSnapshotCaches(claimsRef);
113
- const existingClaim = claimsRef.current[message.id] as Claim | undefined;
114
-
115
- const { snapshot, hasSnapshotKeys, allowEmptySnapshot, snapshotForClaim } =
116
- selectSnapshot({
117
- messageId: message.id,
118
- messageName: message.name,
119
- allowLiveState:
120
- isPlaceholderMessageName(message.name) ||
121
- isPlaceholderMessageId(message.id),
122
- skipLatestCache:
123
- isPlaceholderMessageName(message.name) ||
124
- isPlaceholderMessageId(message.id),
125
- stateRenderId,
126
- effectiveRunId,
127
- stateSnapshotProp: stateSnapshot,
128
- agentState,
129
- agentMessages,
130
- existingClaim,
131
- caches,
132
- });
133
-
134
- const resolution = resolveClaim({
135
- claims: claimsRef.current as ClaimsByMessageId,
136
- context: {
137
- agentId,
138
- messageId: message.id,
139
- stateRenderId,
140
- runId: effectiveRunId,
141
- messageIndex,
142
- } satisfies StateRenderContext,
143
- stateSnapshot: snapshotForClaim,
144
- });
145
-
146
- if (resolution.action === ClaimAction.Block) {
147
- return { canRender: false };
148
- }
149
-
150
- if (resolution.updateRunId && claimsRef.current[message.id]) {
151
- claimsRef.current[message.id].runId = resolution.updateRunId;
152
- }
153
-
154
- if (resolution.nextClaim) {
155
- claimsRef.current[message.id] = resolution.nextClaim;
156
- }
157
-
158
- if (resolution.lockOthers) {
159
- Object.entries(claimsRef.current).forEach(([id, claim]) => {
160
- if (id !== message.id && claim.stateRenderId === stateRenderId) {
161
- claim.locked = true;
162
- }
163
- });
164
- }
165
-
166
- if (existingClaim && !existingClaim.locked && agentMessages?.length) {
167
- const indexInAgentMessages = agentMessages.findIndex(
168
- (msg: any) => msg.id === message.id,
169
- );
170
- if (
171
- indexInAgentMessages >= 0 &&
172
- indexInAgentMessages < agentMessages.length - 1
173
- ) {
174
- existingClaim.locked = true;
175
- }
176
- }
177
-
178
- const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
179
- const snapshotChanged =
180
- stateSnapshot &&
181
- existingSnapshot !== undefined &&
182
- !areStatesEquals(existingSnapshot, snapshot);
183
-
184
- if (
185
- snapshot &&
186
- (stateSnapshot || hasSnapshotKeys || allowEmptySnapshot) &&
187
- (!claimsRef.current[message.id].locked || snapshotChanged)
188
- ) {
189
- if (!claimsRef.current[message.id].locked || snapshotChanged) {
190
- claimsRef.current[message.id].stateSnapshot = snapshot;
191
- const snapshotCache = {
192
- ...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
193
- };
194
- const cacheKey = `${stateRenderId}::${effectiveRunId}`;
195
- snapshotCache[cacheKey] = snapshot;
196
- snapshotCache[`${stateRenderId}::latest`] = snapshot;
197
- store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
198
- const messageCache = {
199
- ...store[LAST_SNAPSHOTS_BY_MESSAGE],
200
- };
201
- messageCache[message.id] = { snapshot, runId: effectiveRunId };
202
- store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
203
- if (stateSnapshot) {
204
- claimsRef.current[message.id].locked = true;
205
- }
206
- }
207
- } else if (snapshotForClaim) {
208
- const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
209
- if (!existingSnapshot) {
210
- claimsRef.current[message.id].stateSnapshot = snapshotForClaim;
211
- const snapshotCache = {
212
- ...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
213
- };
214
- const cacheKey = `${stateRenderId}::${effectiveRunId}`;
215
- snapshotCache[cacheKey] = snapshotForClaim;
216
- snapshotCache[`${stateRenderId}::latest`] = snapshotForClaim;
217
- store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
218
- const messageCache = {
219
- ...store[LAST_SNAPSHOTS_BY_MESSAGE],
220
- };
221
- messageCache[message.id] = {
222
- snapshot: snapshotForClaim,
223
- runId: effectiveRunId,
224
- };
225
- store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
226
- }
227
- }
228
-
229
- return { canRender: true };
230
- }
@@ -1,163 +0,0 @@
1
- /**
2
- * The useCoAgentStateRender hook allows you to render UI or text based components on a Agentic Copilot's state in the chat.
3
- * This is particularly useful for showing intermediate state or progress during Agentic Copilot operations.
4
- *
5
- * ## Usage
6
- *
7
- * ### Simple Usage
8
- *
9
- * ```tsx
10
- * import { useCoAgentStateRender } from "@copilotkit/react-core";
11
- *
12
- * type YourAgentState = {
13
- * agent_state_property: string;
14
- * }
15
- *
16
- * useCoAgentStateRender<YourAgentState>({
17
- * name: "basic_agent",
18
- * nodeName: "optionally_specify_a_specific_node",
19
- * render: ({ status, state, nodeName }) => {
20
- * return (
21
- * <YourComponent
22
- * agentStateProperty={state.agent_state_property}
23
- * status={status}
24
- * nodeName={nodeName}
25
- * />
26
- * );
27
- * },
28
- * });
29
- * ```
30
- *
31
- * This allows for you to render UI components or text based on what is happening within the agent.
32
- *
33
- * ### Example
34
- * A great example of this is in our Perplexity Clone where we render the progress of an agent's internet search as it is happening.
35
- * You can play around with it below or learn how to build it with its [demo](/coagents/videos/perplexity-clone).
36
- *
37
- * <Callout type="info">
38
- * This example is hosted on Vercel and may take a few seconds to load.
39
- * </Callout>
40
- *
41
- * <iframe src="https://examples-coagents-ai-researcher-ui.vercel.app/" className="w-full rounded-lg border h-[700px] my-4" />
42
- */
43
-
44
- import { useRef, useContext, useEffect } from "react";
45
- import { CopilotContext } from "../context/copilot-context";
46
- import { randomId, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
47
- import { CoAgentStateRender } from "../types/coagent-action";
48
- import { useToast } from "../components/toast/toast-provider";
49
- import { useCoAgentStateRenders } from "../context/coagent-state-renders-context";
50
-
51
- /**
52
- * This hook is used to render agent state with custom UI components or text. This is particularly
53
- * useful for showing intermediate state or progress during Agentic Copilot operations.
54
- * To get started using rendering intermediate state through this hook, checkout the documentation.
55
- *
56
- * https://docs.copilotkit.ai/coagents/shared-state/predictive-state-updates
57
- */
58
-
59
- // We implement useCoAgentStateRender dependency handling so that
60
- // the developer has the option to not provide any dependencies.
61
- // see useCopilotAction for more details about this approach.
62
- export function useCoAgentStateRender<T = any>(
63
- action: CoAgentStateRender<T>,
64
- dependencies?: any[],
65
- ): void {
66
- const { chatComponentsCache, availableAgents } = useContext(CopilotContext);
67
- const {
68
- setCoAgentStateRender,
69
- removeCoAgentStateRender,
70
- coAgentStateRenders,
71
- } = useCoAgentStateRenders();
72
- const idRef = useRef<string>(randomId());
73
- const { setBannerError, addToast } = useToast();
74
-
75
- useEffect(() => {
76
- if (
77
- availableAgents?.length &&
78
- !availableAgents.some((a) => a.name === action.name)
79
- ) {
80
- const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
81
-
82
- // Route to banner instead of toast for consistency
83
- const agentError = new CopilotKitAgentDiscoveryError({
84
- agentName: action.name,
85
- availableAgents: availableAgents.map((a) => ({
86
- name: a.name,
87
- id: a.id,
88
- })),
89
- });
90
- setBannerError(agentError);
91
- }
92
- }, [availableAgents]);
93
-
94
- const key = `${action.name}-${action.nodeName || "global"}`;
95
-
96
- if (dependencies === undefined) {
97
- if (coAgentStateRenders[idRef.current]) {
98
- coAgentStateRenders[idRef.current].handler = action.handler as any;
99
- if (typeof action.render === "function") {
100
- if (chatComponentsCache.current !== null) {
101
- chatComponentsCache.current.coAgentStateRenders[key] = action.render;
102
- }
103
- }
104
- }
105
- }
106
-
107
- useEffect(() => {
108
- // Check for duplicates by comparing against all other actions
109
- const currentId = idRef.current;
110
- const hasDuplicate = Object.entries(coAgentStateRenders).some(
111
- ([id, otherAction]) => {
112
- // Skip comparing with self
113
- if (id === currentId) return false;
114
-
115
- // Different agent names are never duplicates
116
- if (otherAction.name !== action.name) return false;
117
-
118
- // Same agent names:
119
- const hasNodeName = !!action.nodeName;
120
- const hasOtherNodeName = !!otherAction.nodeName;
121
-
122
- // If neither has nodeName, they're duplicates
123
- if (!hasNodeName && !hasOtherNodeName) return true;
124
-
125
- // If one has nodeName and other doesn't, they're not duplicates
126
- if (hasNodeName !== hasOtherNodeName) return false;
127
-
128
- // If both have nodeName, they're duplicates only if the names match
129
- return action.nodeName === otherAction.nodeName;
130
- },
131
- );
132
-
133
- if (hasDuplicate) {
134
- const message = action.nodeName
135
- ? `Found multiple state renders for agent ${action.name} and node ${action.nodeName}. State renders might get overridden`
136
- : `Found multiple state renders for agent ${action.name}. State renders might get overridden`;
137
-
138
- addToast({
139
- type: "warning",
140
- message,
141
- id: `dup-action-${action.name}`,
142
- });
143
- }
144
- }, [coAgentStateRenders]);
145
-
146
- useEffect(() => {
147
- setCoAgentStateRender(idRef.current, action as any);
148
- if (chatComponentsCache.current !== null && action.render !== undefined) {
149
- chatComponentsCache.current.coAgentStateRenders[key] = action.render;
150
- }
151
- return () => {
152
- removeCoAgentStateRender(idRef.current);
153
- };
154
- }, [
155
- setCoAgentStateRender,
156
- removeCoAgentStateRender,
157
- action.name,
158
- // include render only if it's a string
159
- typeof action.render === "string" ? action.render : undefined,
160
- // dependencies set by the developer
161
- ...(dependencies || []),
162
- ]);
163
- }