@copilotkit/react-core 1.57.3 → 1.58.0-canary.thread-id-propagation

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 (292) hide show
  1. package/LICENSE +21 -0
  2. package/dist/{copilotkit-CtXcs1ea.cjs → copilotkit-B4ouY7qC.cjs} +14 -3
  3. package/dist/copilotkit-B4ouY7qC.cjs.map +1 -0
  4. package/dist/copilotkit-BK9CVq9A.d.cts.map +1 -1
  5. package/dist/{copilotkit-CC8DjOiC.mjs → copilotkit-L4mM_JqG.mjs} +14 -3
  6. package/dist/copilotkit-L4mM_JqG.mjs.map +1 -0
  7. package/dist/copilotkit-WlmeVijs.d.mts.map +1 -1
  8. package/dist/index.cjs +3 -77
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +2 -2
  11. package/dist/index.d.mts +2 -2
  12. package/dist/index.mjs +3 -77
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/index.umd.js +15 -78
  15. package/dist/index.umd.js.map +1 -1
  16. package/dist/v2/headless.cjs +11 -0
  17. package/dist/v2/headless.cjs.map +1 -1
  18. package/dist/v2/headless.d.cts.map +1 -1
  19. package/dist/v2/headless.d.mts.map +1 -1
  20. package/dist/v2/headless.mjs +11 -0
  21. package/dist/v2/headless.mjs.map +1 -1
  22. package/dist/v2/index.cjs +1 -1
  23. package/dist/v2/index.mjs +1 -1
  24. package/dist/v2/index.umd.js +13 -2
  25. package/dist/v2/index.umd.js.map +1 -1
  26. package/package.json +12 -13
  27. package/skills/react-core/SKILL.md +108 -0
  28. package/skills/react-core/references/agent-access.md +288 -0
  29. package/skills/react-core/references/attachments.md +291 -0
  30. package/skills/react-core/references/capabilities.md +138 -0
  31. package/skills/react-core/references/chat-components.md +221 -0
  32. package/skills/react-core/references/client-side-tools.md +358 -0
  33. package/skills/react-core/references/custom-message-renderers.md +226 -0
  34. package/skills/react-core/references/debug-mode.md +153 -0
  35. package/skills/react-core/references/human-in-the-loop.md +312 -0
  36. package/skills/react-core/references/provider-setup.md +326 -0
  37. package/skills/react-core/references/rendering-activity-messages.md +207 -0
  38. package/skills/react-core/references/rendering-tool-calls.md +319 -0
  39. package/skills/react-core/references/suggestions.md +211 -0
  40. package/skills/react-core/references/switching-agents-recipes.md +160 -0
  41. package/skills/react-core/references/switching-agents.md +231 -0
  42. package/skills/react-core/references/threads.md +226 -0
  43. package/.attw.json +0 -3
  44. package/CHANGELOG.md +0 -5043
  45. package/dist/copilotkit-CC8DjOiC.mjs.map +0 -1
  46. package/dist/copilotkit-CtXcs1ea.cjs.map +0 -1
  47. package/scripts/scope-preflight.mjs +0 -100
  48. package/src/components/CopilotListeners.tsx +0 -137
  49. package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
  50. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
  51. package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
  52. package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
  53. package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
  54. package/src/components/copilot-provider/copilot-messages.tsx +0 -314
  55. package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
  56. package/src/components/copilot-provider/copilotkit.tsx +0 -853
  57. package/src/components/copilot-provider/index.ts +0 -3
  58. package/src/components/dev-console/console-trigger.tsx +0 -283
  59. package/src/components/dev-console/developer-console-modal.tsx +0 -1016
  60. package/src/components/dev-console/icons.tsx +0 -106
  61. package/src/components/error-boundary/error-boundary.tsx +0 -99
  62. package/src/components/error-boundary/error-utils.tsx +0 -105
  63. package/src/components/index.ts +0 -1
  64. package/src/components/toast/exclamation-mark-icon.tsx +0 -27
  65. package/src/components/toast/toast-provider.tsx +0 -448
  66. package/src/components/usage-banner.tsx +0 -266
  67. package/src/context/__tests__/threads-context.test.tsx +0 -141
  68. package/src/context/coagent-state-renders-context.tsx +0 -89
  69. package/src/context/copilot-context.tsx +0 -365
  70. package/src/context/copilot-messages-context.tsx +0 -35
  71. package/src/context/index.ts +0 -22
  72. package/src/context/threads-context.tsx +0 -69
  73. package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
  74. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
  75. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
  76. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
  77. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
  78. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
  79. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
  80. package/src/hooks/index.ts +0 -33
  81. package/src/hooks/use-agent-nodename.ts +0 -33
  82. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
  83. package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
  84. package/src/hooks/use-coagent-state-render-registry.ts +0 -230
  85. package/src/hooks/use-coagent-state-render.ts +0 -163
  86. package/src/hooks/use-coagent.ts +0 -377
  87. package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
  88. package/src/hooks/use-copilot-action.ts +0 -245
  89. package/src/hooks/use-copilot-additional-instructions.ts +0 -98
  90. package/src/hooks/use-copilot-authenticated-action.ts +0 -73
  91. package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
  92. package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
  93. package/src/hooks/use-copilot-chat.ts +0 -132
  94. package/src/hooks/use-copilot-chat_internal.ts +0 -875
  95. package/src/hooks/use-copilot-readable.ts +0 -135
  96. package/src/hooks/use-copilot-runtime-client.ts +0 -178
  97. package/src/hooks/use-default-tool.ts +0 -13
  98. package/src/hooks/use-flat-category-store.ts +0 -109
  99. package/src/hooks/use-frontend-tool.ts +0 -113
  100. package/src/hooks/use-human-in-the-loop.ts +0 -138
  101. package/src/hooks/use-langgraph-interrupt.ts +0 -103
  102. package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
  103. package/src/hooks/use-make-copilot-document-readable.ts +0 -30
  104. package/src/hooks/use-render-tool-call.ts +0 -89
  105. package/src/hooks/use-tree.ts +0 -222
  106. package/src/index.tsx +0 -7
  107. package/src/lib/copilot-task.ts +0 -215
  108. package/src/lib/index.ts +0 -1
  109. package/src/lib/status-checker.ts +0 -67
  110. package/src/setupTests.ts +0 -37
  111. package/src/test-helpers/copilot-context.ts +0 -91
  112. package/src/types/chat-suggestion-configuration.ts +0 -23
  113. package/src/types/coagent-action.ts +0 -35
  114. package/src/types/coagent-state.ts +0 -13
  115. package/src/types/crew.ts +0 -89
  116. package/src/types/document-pointer.ts +0 -7
  117. package/src/types/frontend-action.ts +0 -213
  118. package/src/types/index.ts +0 -17
  119. package/src/types/interrupt-action.ts +0 -58
  120. package/src/types/system-message.ts +0 -4
  121. package/src/utils/dev-console.ts +0 -19
  122. package/src/utils/index.ts +0 -2
  123. package/src/utils/suggestions-constants.ts +0 -8
  124. package/src/utils/utils.test.ts +0 -7
  125. package/src/utils/utils.ts +0 -6
  126. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
  127. package/src/v2/__tests__/globalSetup.ts +0 -14
  128. package/src/v2/__tests__/setup.ts +0 -93
  129. package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
  130. package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
  131. package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
  132. package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
  133. package/src/v2/components/CopilotKitInspector.tsx +0 -52
  134. package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
  135. package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
  136. package/src/v2/components/WildcardToolCallRender.tsx +0 -86
  137. package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
  138. package/src/v2/components/chat/CopilotChat.tsx +0 -664
  139. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
  140. package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
  141. package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
  142. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
  143. package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
  144. package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
  145. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
  146. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
  147. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
  148. package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
  149. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
  150. package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
  151. package/src/v2/components/chat/CopilotChatView.tsx +0 -890
  152. package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
  153. package/src/v2/components/chat/CopilotPopup.tsx +0 -81
  154. package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
  155. package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
  156. package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
  157. package/src/v2/components/chat/Lightbox.tsx +0 -103
  158. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
  159. package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
  160. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
  161. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
  162. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
  163. package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
  164. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
  165. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
  166. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
  167. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
  168. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
  169. package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
  170. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
  171. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
  172. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
  173. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
  174. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
  175. package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
  176. package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
  177. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
  178. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
  179. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
  180. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
  181. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
  182. package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
  183. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
  184. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
  185. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
  186. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
  187. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
  188. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
  189. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
  190. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
  191. package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
  192. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
  193. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
  194. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
  195. package/src/v2/components/chat/__tests__/setup.ts +0 -1
  196. package/src/v2/components/chat/index.ts +0 -90
  197. package/src/v2/components/chat/last-user-message-context.ts +0 -21
  198. package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
  199. package/src/v2/components/chat/scroll-element-context.ts +0 -13
  200. package/src/v2/components/index.ts +0 -8
  201. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
  202. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
  203. package/src/v2/components/intelligence-indicator/index.ts +0 -2
  204. package/src/v2/components/license-warning-banner.tsx +0 -217
  205. package/src/v2/components/ui/button.tsx +0 -124
  206. package/src/v2/components/ui/dropdown-menu.tsx +0 -258
  207. package/src/v2/components/ui/tooltip.tsx +0 -60
  208. package/src/v2/context.ts +0 -62
  209. package/src/v2/headless.ts +0 -64
  210. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
  211. package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
  212. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
  213. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
  214. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
  215. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
  216. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
  217. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
  218. package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
  219. package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
  220. package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
  221. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
  222. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
  223. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
  224. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
  225. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
  226. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
  227. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
  228. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
  229. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
  230. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
  231. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
  232. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
  233. package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
  234. package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
  235. package/src/v2/hooks/index.ts +0 -24
  236. package/src/v2/hooks/use-agent-context.tsx +0 -45
  237. package/src/v2/hooks/use-agent.tsx +0 -227
  238. package/src/v2/hooks/use-attachments.tsx +0 -269
  239. package/src/v2/hooks/use-capabilities.tsx +0 -25
  240. package/src/v2/hooks/use-component.tsx +0 -91
  241. package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
  242. package/src/v2/hooks/use-default-render-tool.tsx +0 -271
  243. package/src/v2/hooks/use-frontend-tool.tsx +0 -46
  244. package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
  245. package/src/v2/hooks/use-interrupt.tsx +0 -305
  246. package/src/v2/hooks/use-keyboard-height.tsx +0 -67
  247. package/src/v2/hooks/use-pin-to-send.ts +0 -94
  248. package/src/v2/hooks/use-render-activity-message.tsx +0 -72
  249. package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
  250. package/src/v2/hooks/use-render-tool-call.tsx +0 -208
  251. package/src/v2/hooks/use-render-tool.tsx +0 -184
  252. package/src/v2/hooks/use-suggestions.tsx +0 -91
  253. package/src/v2/hooks/use-threads.tsx +0 -325
  254. package/src/v2/hooks/useKatexStyles.ts +0 -27
  255. package/src/v2/index.css +0 -1
  256. package/src/v2/index.ts +0 -27
  257. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
  258. package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
  259. package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
  260. package/src/v2/lib/__tests__/slots.test.ts +0 -56
  261. package/src/v2/lib/processPartialHtml.ts +0 -45
  262. package/src/v2/lib/react-core.ts +0 -156
  263. package/src/v2/lib/slots.tsx +0 -184
  264. package/src/v2/lib/transcription-client.ts +0 -184
  265. package/src/v2/lib/utils.ts +0 -8
  266. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
  267. package/src/v2/providers/CopilotKitProvider.tsx +0 -800
  268. package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
  269. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
  270. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
  271. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
  272. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
  273. package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
  274. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
  275. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
  276. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
  277. package/src/v2/providers/index.ts +0 -21
  278. package/src/v2/styles/globals.css +0 -349
  279. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
  280. package/src/v2/types/defineToolCallRenderer.ts +0 -68
  281. package/src/v2/types/frontend-tool.ts +0 -8
  282. package/src/v2/types/human-in-the-loop.ts +0 -33
  283. package/src/v2/types/index.ts +0 -8
  284. package/src/v2/types/interrupt.ts +0 -15
  285. package/src/v2/types/react-activity-message-renderer.ts +0 -27
  286. package/src/v2/types/react-custom-message-renderer.ts +0 -17
  287. package/src/v2/types/react-tool-call-renderer.ts +0 -35
  288. package/src/v2/types/sandbox-function.ts +0 -11
  289. package/tsconfig.json +0 -8
  290. package/tsdown.config.ts +0 -193
  291. package/typedoc.json +0 -4
  292. package/vitest.config.mjs +0 -31
@@ -1,875 +0,0 @@
1
- import React, {
2
- useRef,
3
- useEffect,
4
- useCallback,
5
- useMemo,
6
- useState,
7
- createElement,
8
- } from "react";
9
- import { useCopilotContext } from "../context/copilot-context";
10
- import { SystemMessageFunction } from "../types";
11
- import { useAsyncCallback } from "../components/error-boundary/error-utils";
12
- import { Message } from "@copilotkit/shared";
13
- import {
14
- gqlToAGUI,
15
- Message as DeprecatedGqlMessage,
16
- } from "@copilotkit/runtime-client-gql";
17
- import {
18
- useAgent,
19
- useCopilotChatConfiguration,
20
- useCopilotKit,
21
- useRenderCustomMessages,
22
- useSuggestions,
23
- } from "../v2";
24
- import {
25
- Suggestion,
26
- CopilotKitCoreRuntimeConnectionStatus,
27
- } from "@copilotkit/core";
28
- import { useLazyToolRenderer } from "./use-lazy-tool-renderer";
29
- import {
30
- AbstractAgent,
31
- AGUIConnectNotImplementedError,
32
- HttpAgent,
33
- } from "@ag-ui/client";
34
- import {
35
- CoAgentStateRenderBridge,
36
- type CoAgentStateRenderBridgeProps,
37
- } from "./use-coagent-state-render-bridge";
38
-
39
- /**
40
- * The type of suggestions to use in the chat.
41
- *
42
- * `auto` - Suggestions are generated automatically.
43
- * `manual` - Suggestions are controlled programmatically.
44
- * `SuggestionItem[]` - Static suggestions array.
45
- */
46
- export type ChatSuggestions =
47
- | "auto"
48
- | "manual"
49
- | Omit<Suggestion, "isLoading">[];
50
-
51
- export interface AppendMessageOptions {
52
- /**
53
- * Whether to run the chat completion after appending the message. Defaults to `true`.
54
- */
55
- followUp?: boolean;
56
- /**
57
- * Whether to clear the suggestions after appending the message. Defaults to `true`.
58
- */
59
- clearSuggestions?: boolean;
60
- }
61
-
62
- export interface OnStopGenerationArguments {
63
- /**
64
- * The name of the currently executing agent.
65
- */
66
- currentAgentName: string | undefined;
67
-
68
- /**
69
- * The messages in the chat.
70
- */
71
- messages: Message[];
72
- }
73
-
74
- export type OnReloadMessagesArguments = OnStopGenerationArguments & {
75
- /**
76
- * The message on which "regenerate" was pressed
77
- */
78
- messageId: string;
79
- };
80
-
81
- export type OnStopGeneration = (args: OnStopGenerationArguments) => void;
82
-
83
- export type OnReloadMessages = (args: OnReloadMessagesArguments) => void;
84
-
85
- export interface UseCopilotChatOptions {
86
- /**
87
- * A unique identifier for the chat. If not provided, a random one will be
88
- * generated. When provided, the `useChat` hook with the same `id` will
89
- * have shared states across components.
90
- */
91
- id?: string;
92
-
93
- /**
94
- * HTTP headers to be sent with the API request.
95
- */
96
- headers?: Record<string, string> | Headers;
97
-
98
- /**
99
- * Initial messages to populate the chat with.
100
- */
101
- initialMessages?: Message[];
102
-
103
- /**
104
- * A function to generate the system message. Defaults to `defaultSystemMessage`.
105
- */
106
- makeSystemMessage?: SystemMessageFunction;
107
-
108
- /**
109
- * Disables inclusion of CopilotKit’s default system message. When true, no system message is sent (this also suppresses any custom message from <code>makeSystemMessage</code>).
110
- */
111
- disableSystemMessage?: boolean;
112
- /**
113
- * Controls the behavior of suggestions in the chat interface.
114
- *
115
- * `auto` (default) - Suggestions are generated automatically:
116
- * - When the chat is first opened (empty state)
117
- * - After each message exchange completes
118
- * - Uses configuration from `useCopilotChatSuggestions` hooks
119
- *
120
- * `manual` - Suggestions are controlled programmatically:
121
- * - Use `setSuggestions()` to set custom suggestions
122
- * - Use `generateSuggestions()` to trigger AI generation
123
- * - Access via `useCopilotChat` hook
124
- *
125
- * `SuggestionItem[]` - Static suggestions array:
126
- * - Always shows the same suggestions
127
- * - No AI generation involved
128
- */
129
- suggestions?: ChatSuggestions;
130
-
131
- onInProgress?: (isLoading: boolean) => void;
132
- onSubmitMessage?: (messageContent: string) => Promise<void> | void;
133
- onStopGeneration?: OnStopGeneration;
134
- onReloadMessages?: OnReloadMessages;
135
- }
136
-
137
- export interface MCPServerConfig {
138
- endpoint: string;
139
- apiKey?: string;
140
- }
141
-
142
- // Old suggestion item interface, for returning from useCopilotChatInternal
143
- interface SuggestionItem {
144
- title: string;
145
- message: string;
146
- partial?: boolean;
147
- className?: string;
148
- }
149
-
150
- export interface UseCopilotChatReturn {
151
- /**
152
- * @deprecated use `messages` instead, this is an old non ag-ui version of the messages
153
- * Array of messages currently visible in the chat interface
154
- *
155
- * This is the visible messages, not the raw messages from the runtime client.
156
- */
157
- visibleMessages: DeprecatedGqlMessage[];
158
-
159
- /**
160
- * The messages that are currently in the chat in AG-UI format.
161
- */
162
- messages: Message[];
163
-
164
- /** @deprecated use `sendMessage` in `useCopilotChatHeadless_c` instead. This will be removed in a future major version. */
165
- appendMessage: (
166
- message: DeprecatedGqlMessage,
167
- options?: AppendMessageOptions,
168
- ) => Promise<void>;
169
-
170
- /**
171
- * Send a new message to the chat
172
- *
173
- * ```tsx
174
- * await sendMessage({
175
- * id: "123",
176
- * role: "user",
177
- * content: "Hello, process this request",
178
- * });
179
- * ```
180
- */
181
- sendMessage: (
182
- message: Message,
183
- options?: AppendMessageOptions,
184
- ) => Promise<void>;
185
-
186
- /**
187
- * Replace all messages in the chat
188
- *
189
- * ```tsx
190
- * setMessages([
191
- * { id: "123", role: "user", content: "Hello, process this request" },
192
- * { id: "456", role: "assistant", content: "Hello, I'm the assistant" },
193
- * ]);
194
- * ```
195
- *
196
- * **Deprecated** non-ag-ui version:
197
- *
198
- * ```tsx
199
- * setMessages([
200
- * new TextMessage({
201
- * content: "Hello, process this request",
202
- * role: gqlRole.User,
203
- * }),
204
- * new TextMessage({
205
- * content: "Hello, I'm the assistant",
206
- * role: gqlRole.Assistant,
207
- * ]);
208
- * ```
209
- *
210
- */
211
- setMessages: (messages: Message[] | DeprecatedGqlMessage[]) => void;
212
-
213
- /**
214
- * Remove a specific message by ID
215
- *
216
- * ```tsx
217
- * deleteMessage("123");
218
- * ```
219
- */
220
- deleteMessage: (messageId: string) => void;
221
-
222
- /**
223
- * Regenerate the response for a specific message
224
- *
225
- * ```tsx
226
- * reloadMessages("123");
227
- * ```
228
- */
229
- reloadMessages: (messageId: string) => Promise<void>;
230
-
231
- /**
232
- * Stop the current message generation
233
- *
234
- * ```tsx
235
- * if (isLoading) {
236
- * stopGeneration();
237
- * }
238
- * ```
239
- */
240
- stopGeneration: () => void;
241
-
242
- /**
243
- * Clear all messages and reset chat state
244
- *
245
- * ```tsx
246
- * reset();
247
- * console.log(messages); // []
248
- * ```
249
- */
250
- reset: () => void;
251
-
252
- /**
253
- * Whether the chat is currently generating a response
254
- *
255
- * ```tsx
256
- * if (isLoading) {
257
- * console.log("Loading...");
258
- * } else {
259
- * console.log("Not loading");
260
- * }
261
- */
262
- isLoading: boolean;
263
-
264
- /**
265
- * Whether the chat agent is available to generate responses
266
- *
267
- * ```tsx
268
- * if (isAvailable) {
269
- * console.log("Loading...");
270
- * } else {
271
- * console.log("Not loading");
272
- * }
273
- */
274
- isAvailable: boolean;
275
-
276
- /** Manually trigger chat completion (advanced usage) */
277
- runChatCompletion: () => Promise<Message[]>;
278
-
279
- /** MCP (Model Context Protocol) server configurations */
280
- mcpServers: MCPServerConfig[];
281
-
282
- /** Update MCP server configurations */
283
- setMcpServers: (mcpServers: MCPServerConfig[]) => void;
284
-
285
- /**
286
- * Current suggestions array
287
- * Use this to read the current suggestions or in conjunction with setSuggestions for manual control
288
- */
289
- suggestions: Suggestion[];
290
-
291
- /**
292
- * Manually set suggestions
293
- * Useful for manual mode or custom suggestion workflows
294
- */
295
- setSuggestions: (suggestions: Omit<Suggestion, "isLoading">[]) => void;
296
-
297
- /**
298
- * Trigger AI-powered suggestion generation
299
- * Uses configurations from useCopilotChatSuggestions hooks
300
- * Respects global debouncing - only one generation can run at a time
301
- *
302
- * ```tsx
303
- * generateSuggestions();
304
- * console.log(suggestions); // [suggestion1, suggestion2, suggestion3]
305
- * ```
306
- */
307
- generateSuggestions: () => Promise<void>;
308
-
309
- /**
310
- * Clear all current suggestions
311
- * Also resets suggestion generation state
312
- */
313
- resetSuggestions: () => void;
314
-
315
- /** Whether suggestions are currently being generated */
316
- isLoadingSuggestions: boolean;
317
-
318
- /** Interrupt content for human-in-the-loop workflows */
319
- interrupt: string | React.ReactElement | null;
320
-
321
- agent?: ReturnType<typeof useAgent>["agent"];
322
-
323
- threadId?: string;
324
- }
325
-
326
- export function useCopilotChatInternal({
327
- suggestions,
328
- onInProgress,
329
- onSubmitMessage,
330
- onStopGeneration,
331
- onReloadMessages,
332
- }: UseCopilotChatOptions = {}): UseCopilotChatReturn {
333
- const { copilotkit } = useCopilotKit();
334
- const { threadId, agentSession } = useCopilotContext();
335
- const existingConfig = useCopilotChatConfiguration();
336
- const [agentAvailable, setAgentAvailable] = useState(false);
337
-
338
- // Apply priority: props > existing config > defaults
339
- const resolvedAgentId = existingConfig?.agentId ?? "default";
340
- const { agent } = useAgent({
341
- agentId: resolvedAgentId,
342
- });
343
-
344
- // Track the last agent instance we called connect() on. Without this,
345
- // connect() fires on every render where status is Connected — including
346
- // unrelated context re-renders and StrictMode double-invocations.
347
- // The ref is reset in the cleanup so that remounts (StrictMode, real
348
- // unmount/remount) always trigger a fresh connect.
349
- const lastConnectedAgentRef = useRef<AbstractAgent | null>(null);
350
-
351
- useEffect(() => {
352
- let detached = false;
353
-
354
- // Create a fresh AbortController so we can cancel the HTTP request on cleanup.
355
- // Mirrors the V2 CopilotChat pattern: HttpAgent uses abortController.signal in
356
- // its fetch config. connectAgent() does NOT create a new AbortController
357
- // automatically, so we must set one before connecting.
358
- const connectAbortController = new AbortController();
359
- if (agent instanceof HttpAgent) {
360
- agent.abortController = connectAbortController;
361
- }
362
-
363
- const connect = async (agent: AbstractAgent) => {
364
- setAgentAvailable(false);
365
- try {
366
- await copilotkit.connectAgent({ agent });
367
- // Guard against setting state after cleanup (e.g. React StrictMode unmount)
368
- if (!detached) {
369
- setAgentAvailable(true);
370
- }
371
- } catch (error) {
372
- // Ignore errors from aborted connections (e.g. React StrictMode cleanup)
373
- if (detached) return;
374
- if (error instanceof AGUIConnectNotImplementedError) {
375
- // connect not implemented, ignore
376
- } else {
377
- console.error("CopilotChat: connectAgent failed", error);
378
- // Error will be reported through subscription
379
- }
380
- }
381
- };
382
- if (
383
- agent &&
384
- agent !== lastConnectedAgentRef.current &&
385
- copilotkit.runtimeConnectionStatus ===
386
- CopilotKitCoreRuntimeConnectionStatus.Connected
387
- ) {
388
- lastConnectedAgentRef.current = agent;
389
- connect(agent);
390
- }
391
- return () => {
392
- // Abort the HTTP request and detach the active run.
393
- // This is critical for React StrictMode which unmounts+remounts in dev,
394
- // preventing duplicate /connect requests from reaching the server.
395
- // Reset the ref so remounts always trigger a fresh connect.
396
- lastConnectedAgentRef.current = null;
397
- detached = true;
398
- connectAbortController.abort();
399
- agent?.detachActiveRun();
400
- };
401
- }, [
402
- existingConfig?.threadId,
403
- agent,
404
- copilotkit,
405
- copilotkit.runtimeConnectionStatus,
406
- resolvedAgentId,
407
- ]);
408
-
409
- useEffect(() => {
410
- onInProgress?.(Boolean(agent?.isRunning));
411
- }, [agent?.isRunning, onInProgress]);
412
-
413
- // Subscribe to copilotkit.interruptElement so the v1 return type stays
414
- // reactive. The element is published by useInterrupt (v2) when user code
415
- // calls useLangGraphInterrupt({ render, ... }).
416
- const [interrupt, setInterrupt] = useState<React.ReactElement | null>(null);
417
- useEffect(() => {
418
- setInterrupt(copilotkit.interruptElement);
419
- const subscription = copilotkit.subscribe({
420
- onInterruptElementChanged: ({ interruptElement }) => {
421
- setInterrupt(interruptElement);
422
- },
423
- });
424
- return () => subscription.unsubscribe();
425
- }, [copilotkit]);
426
-
427
- const reset = () => {
428
- agent?.setMessages([]);
429
- agent?.setState(null);
430
- };
431
-
432
- const deleteMessage = useCallback(
433
- (messageId: string) => {
434
- const filteredMessages = (agent?.messages ?? []).filter(
435
- (message) => message.id !== messageId,
436
- );
437
- agent?.setMessages(filteredMessages);
438
- },
439
- [agent?.setMessages, agent?.messages],
440
- );
441
-
442
- const latestDelete = useUpdatedRef(deleteMessage);
443
- const latestDeleteFunc = useCallback(
444
- (messageId: string) => {
445
- return latestDelete.current(messageId);
446
- },
447
- [latestDelete],
448
- );
449
-
450
- const currentSuggestions = useSuggestions({ agentId: resolvedAgentId });
451
-
452
- const reload = useAsyncCallback(
453
- async (reloadMessageId: string): Promise<void> => {
454
- if (!agent) return;
455
- const messages = agent?.messages ?? [];
456
- const isLoading = agent.isRunning;
457
- if (isLoading || messages.length === 0) {
458
- return;
459
- }
460
-
461
- const reloadMessageIndex = messages.findIndex(
462
- (msg) => msg.id === reloadMessageId,
463
- );
464
- if (reloadMessageIndex === -1) {
465
- console.warn(`Message with id ${reloadMessageId} not found`);
466
- return;
467
- }
468
-
469
- const reloadMessageRole = messages[reloadMessageIndex].role;
470
- if (reloadMessageRole !== "assistant") {
471
- console.warn(
472
- `Regenerate cannot be performed on ${reloadMessageRole} role`,
473
- );
474
- return;
475
- }
476
- let historyCutoff: Message[] = [messages[0]];
477
-
478
- if (messages.length > 2 && reloadMessageIndex !== 0) {
479
- // message to regenerate from is now first.
480
- // Work backwards to find the first the closest user message
481
- const lastUserMessageBeforeRegenerate = messages
482
- .slice(0, reloadMessageIndex)
483
- .toReversed()
484
- .find((msg) => msg.role === "user");
485
-
486
- if (!lastUserMessageBeforeRegenerate) {
487
- historyCutoff = [messages[0]];
488
- } else {
489
- const indexOfLastUserMessageBeforeRegenerate = messages.findIndex(
490
- (msg) => msg.id === lastUserMessageBeforeRegenerate.id,
491
- );
492
- // Include the user message, remove everything after it
493
- historyCutoff = messages.slice(
494
- 0,
495
- indexOfLastUserMessageBeforeRegenerate + 1,
496
- );
497
- }
498
- } else if (messages.length > 2 && reloadMessageIndex === 0) {
499
- historyCutoff = [messages[0], messages[1]];
500
- }
501
-
502
- agent?.setMessages(historyCutoff);
503
-
504
- if (agent) {
505
- try {
506
- await copilotkit.runAgent({ agent });
507
- } catch (error) {
508
- console.error("CopilotChat: runAgent failed during reload", error);
509
- // Error will be reported through subscription
510
- }
511
- }
512
- return;
513
- },
514
- [
515
- agent?.messages.length,
516
- agent?.isRunning,
517
- agent?.setMessages,
518
- copilotkit?.runAgent,
519
- ],
520
- );
521
-
522
- const latestSendMessageFunc = useAsyncCallback(
523
- async (message: Message, options?: AppendMessageOptions) => {
524
- if (!agent) return;
525
- const followUp = options?.followUp ?? true;
526
- if (options?.clearSuggestions) {
527
- copilotkit.clearSuggestions(resolvedAgentId);
528
- }
529
-
530
- // Call onSubmitMessage BEFORE adding message and running agent
531
- // This allows users to perform actions (e.g., open chat window) before agent starts processing
532
- if (onSubmitMessage) {
533
- const content =
534
- typeof message.content === "string"
535
- ? message.content
536
- : message.content && "text" in message.content
537
- ? message.content.text
538
- : message.content && "filename" in message.content
539
- ? message.content.filename
540
- : "";
541
- try {
542
- await onSubmitMessage(content);
543
- } catch (error) {
544
- console.error("Error in onSubmitMessage:", error);
545
- }
546
- }
547
-
548
- agent?.addMessage(message);
549
- if (followUp) {
550
- try {
551
- await copilotkit.runAgent({ agent });
552
- } catch (error) {
553
- console.error("CopilotChat: runAgent failed", error);
554
- // Error will be reported through subscription
555
- }
556
- }
557
- },
558
- [agent, copilotkit, resolvedAgentId, onSubmitMessage],
559
- );
560
-
561
- const latestAppendFunc = useAsyncCallback(
562
- async (message: DeprecatedGqlMessage, options?: AppendMessageOptions) => {
563
- return latestSendMessageFunc(gqlToAGUI([message])[0], options);
564
- },
565
- [latestSendMessageFunc],
566
- );
567
-
568
- const latestSetMessagesFunc = useCallback(
569
- (messages: Message[] | DeprecatedGqlMessage[]) => {
570
- if (
571
- messages.every((message) => message instanceof DeprecatedGqlMessage)
572
- ) {
573
- return agent?.setMessages?.(gqlToAGUI(messages));
574
- }
575
- return agent?.setMessages?.(messages);
576
- },
577
- [agent?.setMessages, agent],
578
- );
579
-
580
- const latestReload = useUpdatedRef(reload);
581
- const latestReloadFunc = useAsyncCallback(
582
- async (messageId: string) => {
583
- onReloadMessages?.({
584
- messageId,
585
- currentAgentName: agent?.agentId,
586
- messages: agent?.messages ?? [],
587
- });
588
- return await latestReload.current(messageId);
589
- },
590
- [latestReload, agent, onReloadMessages],
591
- );
592
-
593
- const latestStopFunc = useCallback(() => {
594
- onStopGeneration?.({
595
- currentAgentName: agent?.agentId,
596
- messages: agent?.messages ?? [],
597
- });
598
- return agent?.abortRun?.();
599
- }, [onStopGeneration, agent]);
600
-
601
- const latestReset = useUpdatedRef(reset);
602
- const latestResetFunc = useCallback(() => {
603
- return latestReset.current();
604
- }, [latestReset]);
605
-
606
- const lazyToolRendered = useLazyToolRenderer();
607
- const renderCustomMessage = useRenderCustomMessages();
608
- const legacyCustomMessageRenderer = useLegacyCoagentRenderer({
609
- copilotkit,
610
- agent,
611
- agentId: resolvedAgentId,
612
- threadId: existingConfig?.threadId ?? threadId,
613
- });
614
- const allMessages = agent?.messages ?? [];
615
- const resolvedMessages = useMemo(() => {
616
- let processedMessages = allMessages.map((message) => {
617
- if (message.role !== "assistant") {
618
- return message;
619
- }
620
-
621
- const lazyRendered = lazyToolRendered(message, allMessages);
622
- if (lazyRendered) {
623
- const renderedGenUi = lazyRendered();
624
- if (renderedGenUi) {
625
- return { ...message, generativeUI: () => renderedGenUi };
626
- }
627
- }
628
-
629
- const bridgeRenderer =
630
- legacyCustomMessageRenderer || renderCustomMessage
631
- ? () => {
632
- if (legacyCustomMessageRenderer) {
633
- return legacyCustomMessageRenderer({
634
- message,
635
- position: "before",
636
- });
637
- }
638
- try {
639
- return (
640
- renderCustomMessage?.({ message, position: "before" }) ?? null
641
- );
642
- } catch (error) {
643
- console.warn(
644
- "[CopilotKit] renderCustomMessages failed, falling back to legacy renderer",
645
- error,
646
- );
647
- return null;
648
- }
649
- }
650
- : null;
651
-
652
- if (bridgeRenderer) {
653
- // Attach a position so react-ui can render the custom UI above the assistant content.
654
- return {
655
- ...message,
656
- generativeUI: bridgeRenderer,
657
- generativeUIPosition: "before" as const,
658
- };
659
- }
660
- return message;
661
- });
662
-
663
- const hasAssistantMessages = processedMessages.some(
664
- (msg) => msg.role === "assistant",
665
- );
666
- const canUseCustomRenderer = Boolean(
667
- renderCustomMessage && copilotkit?.getAgent?.(resolvedAgentId),
668
- );
669
- const placeholderRenderer = legacyCustomMessageRenderer
670
- ? legacyCustomMessageRenderer
671
- : canUseCustomRenderer
672
- ? renderCustomMessage
673
- : null;
674
-
675
- const shouldRenderPlaceholder =
676
- Boolean(agent?.isRunning) ||
677
- Boolean(agent?.state && Object.keys(agent.state).length);
678
-
679
- const effectiveThreadId = threadId ?? agent?.threadId ?? "default";
680
- let latestUserIndex = -1;
681
- for (let i = processedMessages.length - 1; i >= 0; i -= 1) {
682
- if (processedMessages[i].role === "user") {
683
- latestUserIndex = i;
684
- break;
685
- }
686
- }
687
- const latestUserMessageId =
688
- latestUserIndex >= 0 ? processedMessages[latestUserIndex].id : undefined;
689
- const currentRunId = latestUserMessageId
690
- ? copilotkit.getRunIdForMessage(
691
- resolvedAgentId,
692
- effectiveThreadId,
693
- latestUserMessageId,
694
- ) || `pending:${latestUserMessageId}`
695
- : undefined;
696
- const hasAssistantForCurrentRun =
697
- latestUserIndex >= 0
698
- ? processedMessages
699
- .slice(latestUserIndex + 1)
700
- .some((msg) => msg.role === "assistant")
701
- : hasAssistantMessages;
702
-
703
- // Insert a placeholder assistant message so state snapshots can render before any
704
- // assistant text exists for the current run.
705
- if (
706
- placeholderRenderer &&
707
- shouldRenderPlaceholder &&
708
- !hasAssistantForCurrentRun
709
- ) {
710
- const placeholderId = currentRunId
711
- ? `coagent-state-render-${resolvedAgentId}-${currentRunId}`
712
- : `coagent-state-render-${resolvedAgentId}`;
713
- const placeholderMessage: Message = {
714
- id: placeholderId,
715
- role: "assistant",
716
- content: "",
717
- name: "coagent-state-render",
718
- runId: currentRunId,
719
- };
720
- processedMessages = [
721
- ...processedMessages,
722
- {
723
- ...placeholderMessage,
724
- generativeUIPosition: "before" as const,
725
- generativeUI: () =>
726
- placeholderRenderer({
727
- message: placeholderMessage,
728
- position: "before",
729
- }),
730
- } as Message,
731
- ];
732
- }
733
-
734
- return processedMessages;
735
- }, [
736
- agent?.messages,
737
- lazyToolRendered,
738
- allMessages,
739
- renderCustomMessage,
740
- legacyCustomMessageRenderer,
741
- resolvedAgentId,
742
- copilotkit,
743
- agent?.isRunning,
744
- agent?.state,
745
- ]);
746
-
747
- const renderedSuggestions = useMemo(() => {
748
- if (Array.isArray(suggestions)) {
749
- return {
750
- suggestions: suggestions.map((s) => ({ ...s, isLoading: false })),
751
- isLoading: false,
752
- };
753
- }
754
- return currentSuggestions;
755
- }, [suggestions, currentSuggestions]);
756
-
757
- // @ts-ignore
758
- return {
759
- messages: resolvedMessages,
760
- sendMessage: latestSendMessageFunc,
761
- appendMessage: latestAppendFunc,
762
- setMessages: latestSetMessagesFunc,
763
- reloadMessages: latestReloadFunc,
764
- stopGeneration: latestStopFunc,
765
- reset: latestResetFunc,
766
- deleteMessage: latestDeleteFunc,
767
- isAvailable: agentAvailable,
768
- isLoading: Boolean(agent?.isRunning),
769
- // mcpServers,
770
- // setMcpServers,
771
- suggestions: renderedSuggestions.suggestions,
772
- setSuggestions: (suggestions: Omit<Suggestion, "isLoading">[]) =>
773
- copilotkit.addSuggestionsConfig({ suggestions }),
774
- generateSuggestions: async () =>
775
- copilotkit.reloadSuggestions(resolvedAgentId),
776
- resetSuggestions: () => copilotkit.clearSuggestions(resolvedAgentId),
777
- isLoadingSuggestions: renderedSuggestions.isLoading,
778
- interrupt,
779
- agent,
780
- threadId,
781
- };
782
- }
783
-
784
- // store `value` in a ref and update
785
- // it whenever it changes.
786
- function useUpdatedRef<T>(value: T) {
787
- const ref = useRef(value);
788
-
789
- useEffect(() => {
790
- ref.current = value;
791
- }, [value]);
792
-
793
- return ref;
794
- }
795
-
796
- type LegacyRenderParams = {
797
- message: Message;
798
- position: "before" | "after";
799
- };
800
-
801
- type LegacyRenderer = ((args: LegacyRenderParams) => any) | null;
802
-
803
- function useLegacyCoagentRenderer({
804
- copilotkit,
805
- agent,
806
- agentId,
807
- threadId,
808
- }: {
809
- copilotkit: ReturnType<typeof useCopilotKit>["copilotkit"];
810
- agent?: AbstractAgent;
811
- agentId: string;
812
- threadId?: string;
813
- }): LegacyRenderer {
814
- return useMemo(() => {
815
- if (!copilotkit || !agent) {
816
- return null;
817
- }
818
-
819
- return ({ message, position }: LegacyRenderParams) => {
820
- const effectiveThreadId = threadId ?? agent.threadId ?? "default";
821
- const providedRunId = (message as any).runId as string | undefined;
822
- const existingRunId = providedRunId
823
- ? providedRunId
824
- : copilotkit.getRunIdForMessage(agentId, effectiveThreadId, message.id);
825
- const runId = existingRunId || `pending:${message.id}`;
826
- const messageIndex = Math.max(
827
- agent.messages.findIndex((msg) => msg.id === message.id),
828
- 0,
829
- );
830
-
831
- const bridgeProps: CoAgentStateRenderBridgeProps = {
832
- message: message as any,
833
- position,
834
- runId,
835
- messageIndex,
836
- messageIndexInRun: 0,
837
- numberOfMessagesInRun: 1,
838
- agentId,
839
- stateSnapshot: (message as any).state,
840
- };
841
-
842
- return createElement(CoAgentStateRenderBridge, bridgeProps) as any;
843
- };
844
- }, [agent, agentId, copilotkit, threadId]);
845
- }
846
-
847
- export function defaultSystemMessage(
848
- contextString: string,
849
- additionalInstructions?: string,
850
- ): string {
851
- return (
852
- `
853
- Please act as an efficient, competent, conscientious, and industrious professional assistant.
854
-
855
- Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
856
- Always be polite and respectful, and prefer brevity over verbosity.
857
-
858
- The user has provided you with the following context:
859
- \`\`\`
860
- ${contextString}
861
- \`\`\`
862
-
863
- They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
864
-
865
- Please assist them as best you can.
866
-
867
- You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
868
-
869
- If you would like to call a function, call it without saying anything else.
870
- In case of a function error:
871
- - If this error stems from incorrect function parameters or syntax, you may retry with corrected arguments.
872
- - If the error's source is unclear or seems unrelated to your input, do not attempt further retries.
873
- ` + (additionalInstructions ? `\n\n${additionalInstructions}` : "")
874
- );
875
- }