@copilotkit/react-core 1.10.6 → 1.50.0-beta.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 (333) hide show
  1. package/dist/chunk-24SCZAB4.mjs +143 -0
  2. package/dist/chunk-24SCZAB4.mjs.map +1 -0
  3. package/dist/chunk-36KQV2NA.mjs +1 -0
  4. package/dist/{chunk-U7QULEVO.mjs → chunk-3GURHDG7.mjs} +3 -3
  5. package/dist/{chunk-4BHIQMN6.mjs → chunk-3R423LZT.mjs} +6 -5
  6. package/dist/{chunk-4BHIQMN6.mjs.map → chunk-3R423LZT.mjs.map} +1 -1
  7. package/dist/chunk-5X5DJRQQ.mjs +71 -0
  8. package/dist/chunk-5X5DJRQQ.mjs.map +1 -0
  9. package/dist/{chunk-N4WEHORG.mjs → chunk-6PUNP7CD.mjs} +4 -4
  10. package/dist/chunk-6PUNP7CD.mjs.map +1 -0
  11. package/dist/chunk-7BYHZLPL.mjs +32 -0
  12. package/dist/chunk-7BYHZLPL.mjs.map +1 -0
  13. package/dist/chunk-7DTB7S5V.mjs +83 -0
  14. package/dist/chunk-7DTB7S5V.mjs.map +1 -0
  15. package/dist/chunk-A6NKSGH3.mjs +1 -0
  16. package/dist/{chunk-GT5WI3AF.mjs → chunk-BR5YEYZJ.mjs} +7 -9
  17. package/dist/chunk-BR5YEYZJ.mjs.map +1 -0
  18. package/dist/chunk-CB7CRBDG.mjs +48 -0
  19. package/dist/chunk-CB7CRBDG.mjs.map +1 -0
  20. package/dist/chunk-CYDWEPFL.mjs +1 -0
  21. package/dist/{chunk-BBUQMG45.mjs → chunk-D3QSYDJR.mjs} +3 -3
  22. package/dist/{chunk-BBUQMG45.mjs.map → chunk-D3QSYDJR.mjs.map} +1 -1
  23. package/dist/chunk-DCHSCK62.mjs +549 -0
  24. package/dist/chunk-DCHSCK62.mjs.map +1 -0
  25. package/dist/{chunk-ZLQVRPDS.mjs → chunk-DMLQZG75.mjs} +1 -1
  26. package/dist/chunk-DMLQZG75.mjs.map +1 -0
  27. package/dist/chunk-F555TVE4.mjs +33 -0
  28. package/dist/chunk-F555TVE4.mjs.map +1 -0
  29. package/dist/{chunk-C6IANC2R.mjs → chunk-FBD24VEH.mjs} +7 -43
  30. package/dist/chunk-FBD24VEH.mjs.map +1 -0
  31. package/dist/chunk-FD6FGKYY.mjs +1 -0
  32. package/dist/chunk-FDOMAPJY.mjs +59 -0
  33. package/dist/chunk-FDOMAPJY.mjs.map +1 -0
  34. package/dist/{chunk-X7MJWPI7.mjs → chunk-GMI4KO4X.mjs} +8 -6
  35. package/dist/{chunk-X7MJWPI7.mjs.map → chunk-GMI4KO4X.mjs.map} +1 -1
  36. package/dist/chunk-IUSKVYUI.mjs +13 -0
  37. package/dist/chunk-IUSKVYUI.mjs.map +1 -0
  38. package/dist/chunk-JD7BAH7U.mjs +1 -0
  39. package/dist/{chunk-N5HOP3PG.mjs → chunk-JRT5BJF3.mjs} +2 -2
  40. package/dist/{chunk-N4VN2B5S.mjs → chunk-LHERIF3L.mjs} +2 -2
  41. package/dist/{chunk-DXEQPN43.mjs → chunk-LHKZJ2ND.mjs} +3 -3
  42. package/dist/chunk-NB2FKV2V.mjs +1 -0
  43. package/dist/chunk-NBK4KBLX.mjs +54 -0
  44. package/dist/chunk-NBK4KBLX.mjs.map +1 -0
  45. package/dist/chunk-NG26QEGF.mjs +46 -0
  46. package/dist/chunk-NG26QEGF.mjs.map +1 -0
  47. package/dist/chunk-NROJOTQP.mjs +163 -0
  48. package/dist/chunk-NROJOTQP.mjs.map +1 -0
  49. package/dist/{chunk-MGWRDFBE.mjs → chunk-OVYFRPSN.mjs} +2 -2
  50. package/dist/{chunk-MQYWH4E6.mjs → chunk-QU6NONOD.mjs} +2 -2
  51. package/dist/chunk-R4MR43UQ.mjs +101 -0
  52. package/dist/chunk-R4MR43UQ.mjs.map +1 -0
  53. package/dist/chunk-SPCZTZCY.mjs +1 -0
  54. package/dist/chunk-SPCZTZCY.mjs.map +1 -0
  55. package/dist/chunk-TXI72QHK.mjs +80 -0
  56. package/dist/chunk-TXI72QHK.mjs.map +1 -0
  57. package/dist/chunk-UJBV5GAG.mjs +767 -0
  58. package/dist/chunk-UJBV5GAG.mjs.map +1 -0
  59. package/dist/{chunk-OKCYPO4I.mjs → chunk-WMJVBMUX.mjs} +3 -3
  60. package/dist/{chunk-EUX2P2E7.mjs → chunk-WVLHXIFP.mjs} +11 -9
  61. package/dist/chunk-WVLHXIFP.mjs.map +1 -0
  62. package/dist/{chunk-5FHSUKQL.mjs → chunk-YYN33GSG.mjs} +5 -2
  63. package/dist/chunk-YYN33GSG.mjs.map +1 -0
  64. package/dist/chunk-ZVF5Q6IH.mjs +29 -0
  65. package/dist/chunk-ZVF5Q6IH.mjs.map +1 -0
  66. package/dist/components/CopilotListeners.d.ts +3 -0
  67. package/dist/components/CopilotListeners.js +234 -0
  68. package/dist/components/CopilotListeners.js.map +1 -0
  69. package/dist/components/CopilotListeners.mjs +14 -0
  70. package/dist/components/CopilotListeners.mjs.map +1 -0
  71. package/dist/components/copilot-provider/copilot-messages.js +14 -48
  72. package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
  73. package/dist/components/copilot-provider/copilot-messages.mjs +3 -3
  74. package/dist/components/copilot-provider/copilotkit-props.d.ts +9 -2
  75. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  76. package/dist/components/copilot-provider/copilotkit.d.ts +2 -1
  77. package/dist/components/copilot-provider/copilotkit.js +528 -370
  78. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  79. package/dist/components/copilot-provider/copilotkit.mjs +17 -12
  80. package/dist/components/copilot-provider/index.d.ts +2 -1
  81. package/dist/components/copilot-provider/index.js +528 -370
  82. package/dist/components/copilot-provider/index.js.map +1 -1
  83. package/dist/components/copilot-provider/index.mjs +18 -12
  84. package/dist/components/dev-console/console-trigger.js +10 -8
  85. package/dist/components/dev-console/console-trigger.js.map +1 -1
  86. package/dist/components/dev-console/console-trigger.mjs +4 -4
  87. package/dist/components/dev-console/developer-console-modal.js +10 -8
  88. package/dist/components/dev-console/developer-console-modal.js.map +1 -1
  89. package/dist/components/dev-console/developer-console-modal.mjs +3 -3
  90. package/dist/components/error-boundary/error-boundary.d.ts +7 -7
  91. package/dist/components/error-boundary/error-boundary.js +3 -3
  92. package/dist/components/error-boundary/error-boundary.js.map +1 -1
  93. package/dist/components/error-boundary/error-boundary.mjs +2 -2
  94. package/dist/components/error-boundary/error-utils.d.ts +1 -1
  95. package/dist/components/error-boundary/error-utils.js +3 -3
  96. package/dist/components/error-boundary/error-utils.js.map +1 -1
  97. package/dist/components/error-boundary/error-utils.mjs +1 -1
  98. package/dist/components/index.d.ts +2 -1
  99. package/dist/components/index.js +528 -370
  100. package/dist/components/index.js.map +1 -1
  101. package/dist/components/index.mjs +19 -12
  102. package/dist/components/toast/exclamation-mark-icon.d.ts +2 -2
  103. package/dist/components/toast/toast-provider.d.ts +3 -3
  104. package/dist/components/usage-banner.d.ts +2 -2
  105. package/dist/context/coagent-state-renders-context.d.ts +24 -0
  106. package/dist/context/coagent-state-renders-context.js +91 -0
  107. package/dist/context/coagent-state-renders-context.js.map +1 -0
  108. package/dist/context/coagent-state-renders-context.mjs +12 -0
  109. package/dist/context/coagent-state-renders-context.mjs.map +1 -0
  110. package/dist/context/copilot-context.d.ts +1 -1
  111. package/dist/context/copilot-context.js +10 -8
  112. package/dist/context/copilot-context.js.map +1 -1
  113. package/dist/context/copilot-context.mjs +1 -1
  114. package/dist/context/copilot-messages-context.d.ts +18 -11
  115. package/dist/context/copilot-messages-context.js.map +1 -1
  116. package/dist/context/copilot-messages-context.mjs +1 -1
  117. package/dist/context/index.d.ts +6 -2
  118. package/dist/context/index.js +110 -10
  119. package/dist/context/index.js.map +1 -1
  120. package/dist/context/index.mjs +20 -4
  121. package/dist/context/threads-context.d.ts +16 -0
  122. package/dist/context/threads-context.js +59 -0
  123. package/dist/context/threads-context.js.map +1 -0
  124. package/dist/context/threads-context.mjs +12 -0
  125. package/dist/context/threads-context.mjs.map +1 -0
  126. package/dist/{copilot-context-cf9de848.d.ts → copilot-context-1cd70a3f.d.ts} +36 -32
  127. package/dist/hooks/index.d.ts +9 -6
  128. package/dist/hooks/index.js +1445 -1839
  129. package/dist/hooks/index.js.map +1 -1
  130. package/dist/hooks/index.mjs +47 -56
  131. package/dist/hooks/use-coagent-state-render-bridge.d.ts +100 -0
  132. package/dist/hooks/use-coagent-state-render-bridge.js +211 -0
  133. package/dist/hooks/use-coagent-state-render-bridge.js.map +1 -0
  134. package/dist/hooks/use-coagent-state-render-bridge.mjs +15 -0
  135. package/dist/hooks/use-coagent-state-render-bridge.mjs.map +1 -0
  136. package/dist/hooks/use-coagent-state-render.js +31 -20
  137. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  138. package/dist/hooks/use-coagent-state-render.mjs +3 -2
  139. package/dist/hooks/use-coagent.d.ts +2 -14
  140. package/dist/hooks/use-coagent.js +109 -1906
  141. package/dist/hooks/use-coagent.js.map +1 -1
  142. package/dist/hooks/use-coagent.mjs +1 -30
  143. package/dist/hooks/use-configure-chat-suggestions.d.ts +36 -0
  144. package/dist/hooks/use-configure-chat-suggestions.js +210 -0
  145. package/dist/hooks/use-configure-chat-suggestions.js.map +1 -0
  146. package/dist/hooks/use-configure-chat-suggestions.mjs +13 -0
  147. package/dist/hooks/use-configure-chat-suggestions.mjs.map +1 -0
  148. package/dist/hooks/use-copilot-action.d.ts +8 -45
  149. package/dist/hooks/use-copilot-action.js +207 -349
  150. package/dist/hooks/use-copilot-action.js.map +1 -1
  151. package/dist/hooks/use-copilot-action.mjs +4 -5
  152. package/dist/hooks/use-copilot-additional-instructions.js +10 -8
  153. package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
  154. package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
  155. package/dist/hooks/use-copilot-authenticated-action.js +224 -248
  156. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
  157. package/dist/hooks/use-copilot-authenticated-action.mjs +6 -6
  158. package/dist/hooks/use-copilot-chat-headless_c.d.ts +2 -10
  159. package/dist/hooks/use-copilot-chat-headless_c.js +973 -1369
  160. package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -1
  161. package/dist/hooks/use-copilot-chat-headless_c.mjs +12 -22
  162. package/dist/hooks/use-copilot-chat.d.ts +2 -10
  163. package/dist/hooks/use-copilot-chat.js +967 -1388
  164. package/dist/hooks/use-copilot-chat.js.map +1 -1
  165. package/dist/hooks/use-copilot-chat.mjs +12 -22
  166. package/dist/hooks/use-copilot-chat_internal.d.ts +38 -14
  167. package/dist/hooks/use-copilot-chat_internal.js +965 -1362
  168. package/dist/hooks/use-copilot-chat_internal.js.map +1 -1
  169. package/dist/hooks/use-copilot-chat_internal.mjs +13 -23
  170. package/dist/hooks/use-copilot-readable.d.ts +1 -1
  171. package/dist/hooks/use-copilot-readable.js +6 -157
  172. package/dist/hooks/use-copilot-readable.js.map +1 -1
  173. package/dist/hooks/use-copilot-readable.mjs +1 -2
  174. package/dist/hooks/use-default-tool.js +204 -349
  175. package/dist/hooks/use-default-tool.js.map +1 -1
  176. package/dist/hooks/use-default-tool.mjs +5 -6
  177. package/dist/hooks/use-flat-category-store.js +4 -1
  178. package/dist/hooks/use-flat-category-store.js.map +1 -1
  179. package/dist/hooks/use-flat-category-store.mjs +1 -1
  180. package/dist/hooks/use-frontend-tool.d.ts +3 -3
  181. package/dist/hooks/use-frontend-tool.js +30 -370
  182. package/dist/hooks/use-frontend-tool.js.map +1 -1
  183. package/dist/hooks/use-frontend-tool.mjs +1 -6
  184. package/dist/hooks/use-human-in-the-loop.d.ts +4 -3
  185. package/dist/hooks/use-human-in-the-loop.js +61 -393
  186. package/dist/hooks/use-human-in-the-loop.js.map +1 -1
  187. package/dist/hooks/use-human-in-the-loop.mjs +1 -6
  188. package/dist/hooks/use-langgraph-interrupt-render.d.ts +3 -2
  189. package/dist/hooks/use-langgraph-interrupt-render.js +92 -36
  190. package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
  191. package/dist/hooks/use-langgraph-interrupt-render.mjs +6 -4
  192. package/dist/hooks/use-langgraph-interrupt.d.ts +1 -1
  193. package/dist/hooks/use-langgraph-interrupt.js +24 -1642
  194. package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
  195. package/dist/hooks/use-langgraph-interrupt.mjs +2 -23
  196. package/dist/hooks/use-lazy-tool-renderer.d.ts +6 -0
  197. package/dist/hooks/use-lazy-tool-renderer.js +53 -0
  198. package/dist/hooks/use-lazy-tool-renderer.js.map +1 -0
  199. package/dist/hooks/use-lazy-tool-renderer.mjs +8 -0
  200. package/dist/hooks/use-lazy-tool-renderer.mjs.map +1 -0
  201. package/dist/hooks/use-make-copilot-document-readable.js +11 -9
  202. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  203. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  204. package/dist/hooks/use-render-tool-call.d.ts +7 -6
  205. package/dist/hooks/use-render-tool-call.js +36 -386
  206. package/dist/hooks/use-render-tool-call.js.map +1 -1
  207. package/dist/hooks/use-render-tool-call.mjs +1 -6
  208. package/dist/index.d.ts +11 -6
  209. package/dist/index.js +1841 -2056
  210. package/dist/index.js.map +1 -1
  211. package/dist/index.mjs +82 -63
  212. package/dist/lib/copilot-task.d.ts +1 -1
  213. package/dist/lib/copilot-task.js +1 -0
  214. package/dist/lib/copilot-task.js.map +1 -1
  215. package/dist/lib/copilot-task.mjs +20 -13
  216. package/dist/lib/index.d.ts +1 -1
  217. package/dist/lib/index.js +1 -0
  218. package/dist/lib/index.js.map +1 -1
  219. package/dist/lib/index.mjs +20 -13
  220. package/dist/setupTests.d.ts +2 -2
  221. package/dist/types/frontend-action.d.ts +5 -5
  222. package/dist/types/index.d.ts +4 -0
  223. package/dist/types/index.js.map +1 -1
  224. package/dist/types/index.mjs +2 -1
  225. package/dist/types/interrupt-action.d.ts +1 -1
  226. package/dist/types/interrupt-action.js.map +1 -1
  227. package/dist/types/interrupt-action.mjs +1 -0
  228. package/dist/utils/index.d.ts +0 -12
  229. package/dist/utils/index.js +6 -325
  230. package/dist/utils/index.js.map +1 -1
  231. package/dist/utils/index.mjs +3 -23
  232. package/dist/utils/utils.js.map +1 -1
  233. package/dist/v2/index.d.ts +2 -0
  234. package/dist/v2/index.js +27 -0
  235. package/dist/v2/index.js.map +1 -0
  236. package/dist/v2/index.mjs +4 -0
  237. package/dist/v2/index.mjs.map +1 -0
  238. package/package.json +34 -23
  239. package/src/components/CopilotListeners.tsx +82 -0
  240. package/src/components/copilot-provider/copilot-messages.tsx +46 -47
  241. package/src/components/copilot-provider/copilotkit-props.tsx +8 -1
  242. package/src/components/copilot-provider/copilotkit.tsx +177 -144
  243. package/src/components/error-boundary/error-utils.tsx +3 -3
  244. package/src/context/coagent-state-renders-context.tsx +76 -0
  245. package/src/context/copilot-context.tsx +21 -21
  246. package/src/context/copilot-messages-context.tsx +3 -3
  247. package/src/context/index.ts +8 -0
  248. package/src/context/threads-context.tsx +41 -0
  249. package/src/hooks/index.ts +7 -3
  250. package/src/hooks/use-coagent-state-render-bridge.tsx +293 -0
  251. package/src/hooks/use-coagent-state-render.ts +4 -7
  252. package/src/hooks/use-coagent.ts +129 -249
  253. package/src/hooks/use-configure-chat-suggestions.tsx +85 -0
  254. package/src/hooks/use-copilot-action.ts +88 -199
  255. package/src/hooks/use-copilot-chat-headless_c.ts +2 -1
  256. package/src/hooks/use-copilot-chat.ts +4 -1
  257. package/src/hooks/use-copilot-chat_internal.ts +349 -258
  258. package/src/hooks/use-copilot-readable.ts +6 -26
  259. package/src/hooks/use-flat-category-store.ts +4 -3
  260. package/src/hooks/use-frontend-tool.ts +67 -8
  261. package/src/hooks/use-human-in-the-loop.ts +111 -13
  262. package/src/hooks/use-langgraph-interrupt-render.ts +90 -40
  263. package/src/hooks/use-langgraph-interrupt.ts +18 -47
  264. package/src/hooks/use-lazy-tool-renderer.tsx +30 -0
  265. package/src/hooks/use-make-copilot-document-readable.ts +1 -1
  266. package/src/hooks/use-render-tool-call.ts +73 -13
  267. package/src/types/index.ts +1 -0
  268. package/src/types/interrupt-action.ts +8 -4
  269. package/src/utils/index.ts +0 -3
  270. package/src/utils/utils.ts +7 -0
  271. package/src/v2/index.ts +2 -0
  272. package/src/v2/styles.css +1 -0
  273. package/tsup.config.ts +1 -1
  274. package/dist/chunk-5FHSUKQL.mjs.map +0 -1
  275. package/dist/chunk-6GPVUMBU.mjs +0 -13
  276. package/dist/chunk-6GPVUMBU.mjs.map +0 -1
  277. package/dist/chunk-AKR2SSUJ.mjs +0 -22
  278. package/dist/chunk-AKR2SSUJ.mjs.map +0 -1
  279. package/dist/chunk-C6IANC2R.mjs.map +0 -1
  280. package/dist/chunk-CI4KD357.mjs +0 -804
  281. package/dist/chunk-CI4KD357.mjs.map +0 -1
  282. package/dist/chunk-DZTU6PGS.mjs +0 -1
  283. package/dist/chunk-EUX2P2E7.mjs.map +0 -1
  284. package/dist/chunk-EVVO67QO.mjs +0 -25
  285. package/dist/chunk-EVVO67QO.mjs.map +0 -1
  286. package/dist/chunk-GT5WI3AF.mjs.map +0 -1
  287. package/dist/chunk-HDOG2RTM.mjs +0 -37
  288. package/dist/chunk-HDOG2RTM.mjs.map +0 -1
  289. package/dist/chunk-N4WEHORG.mjs.map +0 -1
  290. package/dist/chunk-OT5NHMEC.mjs +0 -723
  291. package/dist/chunk-OT5NHMEC.mjs.map +0 -1
  292. package/dist/chunk-QHCLJODF.mjs +0 -332
  293. package/dist/chunk-QHCLJODF.mjs.map +0 -1
  294. package/dist/chunk-QV5SBF2S.mjs +0 -239
  295. package/dist/chunk-QV5SBF2S.mjs.map +0 -1
  296. package/dist/chunk-T3376SZS.mjs +0 -142
  297. package/dist/chunk-T3376SZS.mjs.map +0 -1
  298. package/dist/chunk-U3DQY67T.mjs +0 -71
  299. package/dist/chunk-U3DQY67T.mjs.map +0 -1
  300. package/dist/chunk-YPBKY4KY.mjs +0 -1
  301. package/dist/chunk-YPSGKPDA.mjs +0 -1
  302. package/dist/chunk-ZLQVRPDS.mjs.map +0 -1
  303. package/dist/chunk-ZVOLWJCY.mjs +0 -47
  304. package/dist/chunk-ZVOLWJCY.mjs.map +0 -1
  305. package/dist/hooks/use-chat.d.ts +0 -145
  306. package/dist/hooks/use-chat.js +0 -1129
  307. package/dist/hooks/use-chat.js.map +0 -1
  308. package/dist/hooks/use-chat.mjs +0 -14
  309. package/dist/index-1f5b66ac.d.ts +0 -36
  310. package/dist/utils/extract.d.ts +0 -43
  311. package/dist/utils/extract.js +0 -208
  312. package/dist/utils/extract.js.map +0 -1
  313. package/dist/utils/extract.mjs +0 -25
  314. package/dist/utils/suggestions.d.ts +0 -11
  315. package/dist/utils/suggestions.js +0 -343
  316. package/dist/utils/suggestions.js.map +0 -1
  317. package/dist/utils/suggestions.mjs +0 -25
  318. package/src/hooks/use-chat.ts +0 -1142
  319. package/src/utils/extract.ts +0 -215
  320. package/src/utils/suggestions.ts +0 -208
  321. /package/dist/{chunk-DZTU6PGS.mjs.map → chunk-36KQV2NA.mjs.map} +0 -0
  322. /package/dist/{chunk-U7QULEVO.mjs.map → chunk-3GURHDG7.mjs.map} +0 -0
  323. /package/dist/{chunk-YPBKY4KY.mjs.map → chunk-A6NKSGH3.mjs.map} +0 -0
  324. /package/dist/{chunk-YPSGKPDA.mjs.map → chunk-CYDWEPFL.mjs.map} +0 -0
  325. /package/dist/{hooks/use-chat.mjs.map → chunk-FD6FGKYY.mjs.map} +0 -0
  326. /package/dist/{utils/extract.mjs.map → chunk-JD7BAH7U.mjs.map} +0 -0
  327. /package/dist/{chunk-N5HOP3PG.mjs.map → chunk-JRT5BJF3.mjs.map} +0 -0
  328. /package/dist/{chunk-N4VN2B5S.mjs.map → chunk-LHERIF3L.mjs.map} +0 -0
  329. /package/dist/{chunk-DXEQPN43.mjs.map → chunk-LHKZJ2ND.mjs.map} +0 -0
  330. /package/dist/{utils/suggestions.mjs.map → chunk-NB2FKV2V.mjs.map} +0 -0
  331. /package/dist/{chunk-MGWRDFBE.mjs.map → chunk-OVYFRPSN.mjs.map} +0 -0
  332. /package/dist/{chunk-MQYWH4E6.mjs.map → chunk-QU6NONOD.mjs.map} +0 -0
  333. /package/dist/{chunk-OKCYPO4I.mjs.map → chunk-WMJVBMUX.mjs.map} +0 -0
@@ -1,239 +0,0 @@
1
- import {
2
- useCopilotChat
3
- } from "./chunk-QHCLJODF.mjs";
4
- import {
5
- useMessagesTap
6
- } from "./chunk-C6IANC2R.mjs";
7
- import {
8
- useCopilotRuntimeClient
9
- } from "./chunk-6ESSSQ7Q.mjs";
10
- import {
11
- useAsyncCallback
12
- } from "./chunk-N4WEHORG.mjs";
13
- import {
14
- useToast
15
- } from "./chunk-EFL5OBKN.mjs";
16
- import {
17
- useCopilotContext
18
- } from "./chunk-EUX2P2E7.mjs";
19
- import {
20
- __async,
21
- __spreadProps,
22
- __spreadValues
23
- } from "./chunk-SKC7AJIV.mjs";
24
-
25
- // src/hooks/use-coagent.ts
26
- import { useCallback, useEffect, useMemo, useRef } from "react";
27
- import { parseJson, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
28
- function useCoAgent(options) {
29
- const context = useCopilotContext();
30
- const { availableAgents, onError } = context;
31
- const { setBannerError } = useToast();
32
- const lastLoadedThreadId = useRef();
33
- const lastLoadedState = useRef();
34
- const { name } = options;
35
- useEffect(() => {
36
- if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
37
- const message = `(useCoAgent): Agent "${name}" not found. Make sure the agent exists and is properly configured.`;
38
- console.warn(message);
39
- const agentError = new CopilotKitAgentDiscoveryError({
40
- agentName: name,
41
- availableAgents: availableAgents.map((a) => ({ name: a.name, id: a.id }))
42
- });
43
- setBannerError(agentError);
44
- }
45
- }, [availableAgents]);
46
- const { getMessagesFromTap } = useMessagesTap();
47
- const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
48
- const { sendMessage, runChatCompletion } = useCopilotChat();
49
- const headers = __spreadValues({}, copilotApiConfig.headers || {});
50
- const runtimeClient = useCopilotRuntimeClient({
51
- url: copilotApiConfig.chatApiEndpoint,
52
- publicApiKey: copilotApiConfig.publicApiKey,
53
- headers,
54
- credentials: copilotApiConfig.credentials,
55
- showDevConsole: context.showDevConsole,
56
- onError
57
- });
58
- const setState = useCallback(
59
- (newState) => {
60
- let coagentState = getCoagentState({ coagentStates, name, options });
61
- const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
62
- setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
63
- [name]: __spreadProps(__spreadValues({}, coagentState), {
64
- state: updatedState
65
- })
66
- }));
67
- },
68
- [coagentStates, name]
69
- );
70
- useEffect(() => {
71
- const fetchAgentState = () => __async(this, null, function* () {
72
- var _a, _b, _c, _d;
73
- if (!threadId || threadId === lastLoadedThreadId.current)
74
- return;
75
- const result = yield runtimeClient.loadAgentState({
76
- threadId,
77
- agentName: name
78
- });
79
- if (result.error) {
80
- return;
81
- }
82
- const newState = (_b = (_a = result.data) == null ? void 0 : _a.loadAgentState) == null ? void 0 : _b.state;
83
- if (newState === lastLoadedState.current)
84
- return;
85
- if (((_d = (_c = result.data) == null ? void 0 : _c.loadAgentState) == null ? void 0 : _d.threadExists) && newState && newState != "{}") {
86
- lastLoadedState.current = newState;
87
- lastLoadedThreadId.current = threadId;
88
- const fetchedState = parseJson(newState, {});
89
- isExternalStateManagement(options) ? options.setState(fetchedState) : setState(fetchedState);
90
- }
91
- });
92
- void fetchAgentState();
93
- }, [threadId]);
94
- useEffect(() => {
95
- if (isExternalStateManagement(options)) {
96
- setState(options.state);
97
- } else if (coagentStates[name] === void 0) {
98
- setState(options.initialState === void 0 ? {} : options.initialState);
99
- }
100
- }, [
101
- isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
102
- // reset initialstate on reset
103
- coagentStates[name] === void 0
104
- ]);
105
- useEffect(() => {
106
- const newConfig = options.config ? options.config : options.configurable ? { configurable: options.configurable } : void 0;
107
- if (newConfig === void 0)
108
- return;
109
- setCoagentStatesWithRef((prev) => {
110
- var _a;
111
- const existing = (_a = prev[name]) != null ? _a : {
112
- name,
113
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
114
- config: {},
115
- running: false,
116
- active: false,
117
- threadId: void 0,
118
- nodeName: void 0,
119
- runId: void 0
120
- };
121
- if (JSON.stringify(existing.config) === JSON.stringify(newConfig)) {
122
- return prev;
123
- }
124
- return __spreadProps(__spreadValues({}, prev), {
125
- [name]: __spreadProps(__spreadValues({}, existing), {
126
- config: newConfig
127
- })
128
- });
129
- });
130
- }, [JSON.stringify(options.config), JSON.stringify(options.configurable)]);
131
- const runAgentCallback = useAsyncCallback(
132
- (hint) => __async(this, null, function* () {
133
- yield runAgent(name, context, getMessagesFromTap(), sendMessage, runChatCompletion, hint);
134
- }),
135
- [name, context, sendMessage, runChatCompletion]
136
- );
137
- return useMemo(() => {
138
- const coagentState = getCoagentState({ coagentStates, name, options });
139
- return {
140
- name,
141
- nodeName: coagentState.nodeName,
142
- threadId: coagentState.threadId,
143
- running: coagentState.running,
144
- state: coagentState.state,
145
- setState: isExternalStateManagement(options) ? options.setState : setState,
146
- start: () => startAgent(name, context),
147
- stop: () => stopAgent(name, context),
148
- run: runAgentCallback
149
- };
150
- }, [name, coagentStates, options, setState, runAgentCallback]);
151
- }
152
- function startAgent(name, context) {
153
- const { setAgentSession } = context;
154
- setAgentSession({
155
- agentName: name
156
- });
157
- }
158
- function stopAgent(name, context) {
159
- const { agentSession, setAgentSession } = context;
160
- if (agentSession && agentSession.agentName === name) {
161
- setAgentSession(null);
162
- context.setCoagentStates((prevAgentStates) => {
163
- return __spreadProps(__spreadValues({}, prevAgentStates), {
164
- [name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
165
- running: false,
166
- active: false,
167
- threadId: void 0,
168
- nodeName: void 0,
169
- runId: void 0
170
- })
171
- });
172
- });
173
- } else {
174
- console.warn(`No agent session found for ${name}`);
175
- }
176
- }
177
- function runAgent(name, context, messages, sendMessage, runChatCompletion, hint) {
178
- return __async(this, null, function* () {
179
- var _a, _b;
180
- const { agentSession, setAgentSession } = context;
181
- if (!agentSession || agentSession.agentName !== name) {
182
- setAgentSession({
183
- agentName: name
184
- });
185
- }
186
- let previousState = null;
187
- for (let i = messages.length - 1; i >= 0; i--) {
188
- const message = messages[i];
189
- if (message.isAgentStateMessage() && message.agentName === name) {
190
- previousState = message.state;
191
- }
192
- }
193
- let state = ((_b = (_a = context.coagentStatesRef.current) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
194
- if (hint) {
195
- const hintMessage = hint({ previousState, currentState: state });
196
- if (hintMessage) {
197
- yield sendMessage(hintMessage);
198
- } else {
199
- yield runChatCompletion();
200
- }
201
- } else {
202
- yield runChatCompletion();
203
- }
204
- });
205
- }
206
- var isExternalStateManagement = (options) => {
207
- return "state" in options && "setState" in options;
208
- };
209
- var isInternalStateManagementWithInitial = (options) => {
210
- return "initialState" in options;
211
- };
212
- var getCoagentState = ({
213
- coagentStates,
214
- name,
215
- options
216
- }) => {
217
- if (coagentStates[name]) {
218
- return coagentStates[name];
219
- } else {
220
- return {
221
- name,
222
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
223
- config: options.config ? options.config : options.configurable ? { configurable: options.configurable } : {},
224
- running: false,
225
- active: false,
226
- threadId: void 0,
227
- nodeName: void 0,
228
- runId: void 0
229
- };
230
- }
231
- };
232
-
233
- export {
234
- useCoAgent,
235
- startAgent,
236
- stopAgent,
237
- runAgent
238
- };
239
- //# sourceMappingURL=chunk-QV5SBF2S.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-coagent.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * Usage of this hook assumes some additional setup in your application, for more information\n * on that see the CoAgents <span className=\"text-blue-500\">[getting started guide](/coagents/quickstart/langgraph)</span>.\n * </Callout>\n * <Frame className=\"my-12\">\n * <img\n * src=\"https://cdn.copilotkit.ai/docs/copilotkit/images/coagents/SharedStateCoAgents.gif\"\n * alt=\"CoAgents demonstration\"\n * className=\"w-auto\"\n * />\n * </Frame>\n *\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of an agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences agentic copilots, or CoAgents for short.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCoAgent } from \"@copilotkit/react-core\";\n *\n * type AgentState = {\n * count: number;\n * }\n *\n * const agent = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * ```\n *\n * `useCoAgent` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * name, // The name of the agent currently being used.\n * nodeName, // The name of the current LangGraph node.\n * state, // The current state of the agent.\n * setState, // A function to update the state of the agent.\n * running, // A boolean indicating if the agent is currently running.\n * start, // A function to start the agent.\n * stop, // A function to stop the agent.\n * run, // A function to re-run the agent. Takes a HintFunction to inform the agent why it is being re-run.\n * } = agent;\n * ```\n *\n * Finally we can leverage these properties to create reactive experiences with the agent!\n *\n * ```tsx\n * const { state, setState } = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * return (\n * <div>\n * <p>Count: {state.count}</p>\n * <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>\n * </div>\n * );\n * ```\n *\n * This reactivity is bidirectional, meaning that changes to the state from the agent will be reflected in the UI and vice versa.\n *\n * ## Parameters\n * <PropertyReference name=\"options\" type=\"UseCoagentOptions<T>\" required>\n * The options to use when creating the coagent.\n * <PropertyReference name=\"name\" type=\"string\" required>\n * The name of the agent to use.\n * </PropertyReference>\n * <PropertyReference name=\"initialState\" type=\"T | any\">\n * The initial state of the agent.\n * </PropertyReference>\n * <PropertyReference name=\"state\" type=\"T | any\">\n * State to manage externally if you are using this hook with external state management.\n * </PropertyReference>\n * <PropertyReference name=\"setState\" type=\"(newState: T | ((prevState: T | undefined) => T)) => void\">\n * A function to update the state of the agent if you are using this hook with external state management.\n * </PropertyReference>\n * </PropertyReference>\n */\n\nimport { useCallback, useEffect, useMemo, useRef } from \"react\";\nimport { CopilotContextParams, useCopilotContext } from \"../context\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { useCopilotChat } from \"./use-copilot-chat_internal\";\nimport { Message } from \"@copilotkit/shared\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\nimport { parseJson, CopilotKitAgentDiscoveryError } from \"@copilotkit/shared\";\nimport { useMessagesTap } from \"../components/copilot-provider/copilot-messages\";\nimport { Message as GqlMessage } from \"@copilotkit/runtime-client-gql\";\n\ninterface UseCoagentOptionsBase {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * @deprecated - use \"config.configurable\"\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n /**\n * Config to pass to a LangGraph Agent\n */\n config?: {\n configurable?: Record<string, any>;\n [key: string]: any;\n };\n}\n\ninterface WithInternalStateManagementAndInitial<T> extends UseCoagentOptionsBase {\n /**\n * The initial state of the agent.\n */\n initialState: T;\n}\n\ninterface WithInternalStateManagement extends UseCoagentOptionsBase {\n /**\n * Optional initialState with default type any\n */\n initialState?: any;\n}\n\ninterface WithExternalStateManagement<T> extends UseCoagentOptionsBase {\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n}\n\ntype UseCoagentOptions<T> =\n | WithInternalStateManagementAndInitial<T>\n | WithInternalStateManagement\n | WithExternalStateManagement<T>;\n\nexport interface UseCoagentReturnType<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The name of the current LangGraph node.\n */\n nodeName?: string;\n /**\n * The ID of the thread the agent is running in.\n */\n threadId?: string;\n /**\n * A boolean indicating if the agent is currently running.\n */\n running: boolean;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * A function to start the agent.\n */\n start: () => void;\n /**\n * A function to stop the agent.\n */\n stop: () => void;\n /**\n * A function to re-run the agent. The hint function can be used to provide a hint to the agent\n * about why it is being re-run again.\n */\n run: (hint?: HintFunction) => Promise<void>;\n}\n\nexport interface HintFunctionParams {\n /**\n * The previous state of the agent.\n */\n previousState: any;\n /**\n * The current state of the agent.\n */\n currentState: any;\n}\n\nexport type HintFunction = (params: HintFunctionParams) => Message | undefined;\n\n/**\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of the agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences \"agentic copilots\". To get started using agentic copilots, which\n * we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart/langgraph.\n */\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const context = useCopilotContext();\n const { availableAgents, onError } = context;\n const { setBannerError } = useToast();\n const lastLoadedThreadId = useRef<string>();\n const lastLoadedState = useRef<any>();\n\n const { name } = options;\n useEffect(() => {\n if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {\n const message = `(useCoAgent): Agent \"${name}\" not found. Make sure the agent exists and is properly configured.`;\n console.warn(message);\n\n // Route to banner instead of toast for consistency\n const agentError = new CopilotKitAgentDiscoveryError({\n agentName: name,\n availableAgents: availableAgents.map((a) => ({ name: a.name, id: a.id })),\n });\n setBannerError(agentError);\n }\n }, [availableAgents]);\n\n const { getMessagesFromTap } = useMessagesTap();\n\n const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =\n context;\n const { sendMessage, runChatCompletion } = useCopilotChat();\n const headers = {\n ...(copilotApiConfig.headers || {}),\n };\n\n const runtimeClient = useCopilotRuntimeClient({\n url: copilotApiConfig.chatApiEndpoint,\n publicApiKey: copilotApiConfig.publicApiKey,\n headers,\n credentials: copilotApiConfig.credentials,\n showDevConsole: context.showDevConsole,\n onError,\n });\n\n // if we manage state internally, we need to provide a function to set the state\n const setState = useCallback(\n (newState: T | ((prevState: T | undefined) => T)) => {\n // coagentStatesRef.current || {}\n let coagentState: CoagentState = getCoagentState({ coagentStates, name, options });\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n setCoagentStatesWithRef({\n ...coagentStatesRef.current,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n });\n },\n [coagentStates, name],\n );\n\n useEffect(() => {\n const fetchAgentState = async () => {\n if (!threadId || threadId === lastLoadedThreadId.current) return;\n\n const result = await runtimeClient.loadAgentState({\n threadId,\n agentName: name,\n });\n\n // Runtime client handles errors automatically via handleGQLErrors\n if (result.error) {\n return; // Don't process data on error\n }\n\n const newState = result.data?.loadAgentState?.state;\n if (newState === lastLoadedState.current) return;\n\n if (result.data?.loadAgentState?.threadExists && newState && newState != \"{}\") {\n lastLoadedState.current = newState;\n lastLoadedThreadId.current = threadId;\n const fetchedState = parseJson(newState, {});\n isExternalStateManagement(options)\n ? options.setState(fetchedState)\n : setState(fetchedState);\n }\n };\n void fetchAgentState();\n }, [threadId]);\n\n // Sync internal state with external state if state management is external\n useEffect(() => {\n if (isExternalStateManagement(options)) {\n setState(options.state);\n } else if (coagentStates[name] === undefined) {\n setState(options.initialState === undefined ? {} : options.initialState);\n }\n }, [\n isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined,\n // reset initialstate on reset\n coagentStates[name] === undefined,\n ]);\n\n // Sync config when runtime configuration changes\n useEffect(() => {\n const newConfig = options.config\n ? options.config\n : options.configurable\n ? { configurable: options.configurable }\n : undefined;\n\n if (newConfig === undefined) return;\n\n setCoagentStatesWithRef((prev) => {\n const existing = prev[name] ?? {\n name,\n state: isInternalStateManagementWithInitial(options) ? options.initialState : {},\n config: {},\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n };\n\n if (JSON.stringify(existing.config) === JSON.stringify(newConfig)) {\n return prev;\n }\n\n return {\n ...prev,\n [name]: {\n ...existing,\n config: newConfig,\n },\n };\n });\n }, [JSON.stringify(options.config), JSON.stringify(options.configurable)]);\n\n const runAgentCallback = useAsyncCallback(\n async (hint?: HintFunction) => {\n await runAgent(name, context, getMessagesFromTap(), sendMessage, runChatCompletion, hint);\n },\n [name, context, sendMessage, runChatCompletion],\n );\n\n // Return the state and setState function\n return useMemo(() => {\n const coagentState = getCoagentState({ coagentStates, name, options });\n return {\n name,\n nodeName: coagentState.nodeName,\n threadId: coagentState.threadId,\n running: coagentState.running,\n state: coagentState.state,\n setState: isExternalStateManagement(options) ? options.setState : setState,\n start: () => startAgent(name, context),\n stop: () => stopAgent(name, context),\n run: runAgentCallback,\n };\n }, [name, coagentStates, options, setState, runAgentCallback]);\n}\n\nexport function startAgent(name: string, context: CopilotContextParams) {\n const { setAgentSession } = context;\n setAgentSession({\n agentName: name,\n });\n}\n\nexport function stopAgent(name: string, context: CopilotContextParams) {\n const { agentSession, setAgentSession } = context;\n if (agentSession && agentSession.agentName === name) {\n setAgentSession(null);\n context.setCoagentStates((prevAgentStates) => {\n return {\n ...prevAgentStates,\n [name]: {\n ...prevAgentStates[name],\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n },\n };\n });\n } else {\n console.warn(`No agent session found for ${name}`);\n }\n}\n\nexport async function runAgent(\n name: string,\n context: CopilotContextParams,\n messages: GqlMessage[],\n sendMessage: (message: Message) => Promise<void>,\n runChatCompletion: () => Promise<Message[]>,\n hint?: HintFunction,\n) {\n const { agentSession, setAgentSession } = context;\n if (!agentSession || agentSession.agentName !== name) {\n setAgentSession({\n agentName: name,\n });\n }\n\n let previousState: any = null;\n for (let i = messages.length - 1; i >= 0; i--) {\n const message = messages[i];\n if (message.isAgentStateMessage() && message.agentName === name) {\n previousState = message.state;\n }\n }\n\n let state = context.coagentStatesRef.current?.[name]?.state || {};\n\n if (hint) {\n const hintMessage = hint({ previousState, currentState: state });\n if (hintMessage) {\n await sendMessage(hintMessage);\n } else {\n await runChatCompletion();\n }\n } else {\n await runChatCompletion();\n }\n}\n\nconst isExternalStateManagement = <T>(\n options: UseCoagentOptions<T>,\n): options is WithExternalStateManagement<T> => {\n return \"state\" in options && \"setState\" in options;\n};\n\nconst isInternalStateManagementWithInitial = <T>(\n options: UseCoagentOptions<T>,\n): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n};\n\nconst getCoagentState = <T>({\n coagentStates,\n name,\n options,\n}: {\n coagentStates: Record<string, CoagentState>;\n name: string;\n options: UseCoagentOptions<T>;\n}) => {\n if (coagentStates[name]) {\n return coagentStates[name];\n } else {\n return {\n name,\n state: isInternalStateManagementWithInitial<T>(options) ? options.initialState : {},\n config: options.config\n ? options.config\n : options.configurable\n ? { configurable: options.configurable }\n : {},\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n };\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0FA,SAAS,aAAa,WAAW,SAAS,cAAc;AAQxD,SAAS,WAAW,qCAAqC;AAgHlD,SAAS,WAAoB,SAAwD;AAC1F,QAAM,UAAU,kBAAkB;AAClC,QAAM,EAAE,iBAAiB,QAAQ,IAAI;AACrC,QAAM,EAAE,eAAe,IAAI,SAAS;AACpC,QAAM,qBAAqB,OAAe;AAC1C,QAAM,kBAAkB,OAAY;AAEpC,QAAM,EAAE,KAAK,IAAI;AACjB,YAAU,MAAM;AACd,SAAI,mDAAiB,WAAU,CAAC,gBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,IAAI,GAAG;AAC5E,YAAM,UAAU,wBAAwB;AACxC,cAAQ,KAAK,OAAO;AAGpB,YAAM,aAAa,IAAI,8BAA8B;AAAA,QACnD,WAAW;AAAA,QACX,iBAAiB,gBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,EAAE,GAAG,EAAE;AAAA,MAC1E,CAAC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yncCallback
3
- } from "./chunk-N4WEHORG.mjs";
4
- import {
5
- useToast
6
- } from "./chunk-EFL5OBKN.mjs";
7
- import {
8
- useCopilotContext
9
- } from "./chunk-EUX2P2E7.mjs";
10
- import {
11
- __async,
12
- __spreadValues
13
- } from "./chunk-SKC7AJIV.mjs";
14
-
15
- // src/hooks/use-copilot-action.ts
16
- import { randomId } from "@copilotkit/shared";
17
- import { createElement, Fragment, useEffect, useRef } from "react";
18
- function useCopilotAction(action, dependencies) {
19
- const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
20
- const idRef = useRef(randomId());
21
- const renderAndWaitRef = useRef(null);
22
- const activatingMessageIdRef = useRef(null);
23
- const { addToast } = useToast();
24
- action = __spreadValues({}, action);
25
- if (
26
- // renderAndWaitForResponse is not available for catch all actions
27
- isFrontendAction(action) && // check if renderAndWaitForResponse is set
28
- (action.renderAndWait || action.renderAndWaitForResponse)
29
- ) {
30
- action._isRenderAndWait = true;
31
- const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
32
- action.renderAndWait = void 0;
33
- action.renderAndWaitForResponse = void 0;
34
- action._setActivatingMessageId = (id) => {
35
- activatingMessageIdRef.current = id;
36
- };
37
- action.handler = useAsyncCallback(() => __async(this, null, function* () {
38
- const currentActivatingId = activatingMessageIdRef.current;
39
- let resolve;
40
- let reject;
41
- const promise = new Promise((resolvePromise, rejectPromise) => {
42
- resolve = resolvePromise;
43
- reject = rejectPromise;
44
- });
45
- renderAndWaitRef.current = {
46
- promise,
47
- resolve,
48
- reject,
49
- messageId: currentActivatingId
50
- };
51
- const result = yield promise;
52
- return result;
53
- }), []);
54
- action.render = (props) => {
55
- const currentRenderMessageId = props.messageId;
56
- let status = props.status;
57
- if (props.status === "executing") {
58
- if (!renderAndWaitRef.current || !renderAndWaitRef.current.promise) {
59
- status = "inProgress";
60
- } else if (renderAndWaitRef.current.messageId !== currentRenderMessageId && activatingMessageIdRef.current !== currentRenderMessageId) {
61
- status = "inProgress";
62
- }
63
- }
64
- const waitProps = {
65
- status,
66
- args: props.args,
67
- result: props.result,
68
- // handler and respond should only be provided if this is the truly active instance
69
- // and its promise infrastructure is ready.
70
- handler: status === "executing" && renderAndWaitRef.current && renderAndWaitRef.current.messageId === currentRenderMessageId ? renderAndWaitRef.current.resolve : void 0,
71
- respond: status === "executing" && renderAndWaitRef.current && renderAndWaitRef.current.messageId === currentRenderMessageId ? renderAndWaitRef.current.resolve : void 0
72
- };
73
- const isNoArgsRenderWait = (_fn) => {
74
- var _a;
75
- return ((_a = action.parameters) == null ? void 0 : _a.length) === 0;
76
- };
77
- if (renderAndWait) {
78
- if (isNoArgsRenderWait(renderAndWait)) {
79
- return renderAndWait(waitProps);
80
- } else {
81
- return renderAndWait(waitProps);
82
- }
83
- }
84
- return createElement(Fragment);
85
- };
86
- }
87
- if (dependencies === void 0) {
88
- if (actions[idRef.current]) {
89
- if (isFrontendAction(action)) {
90
- actions[idRef.current].handler = action.handler;
91
- }
92
- if (typeof action.render === "function") {
93
- if (chatComponentsCache.current !== null) {
94
- chatComponentsCache.current.actions[action.name] = action.render;
95
- }
96
- }
97
- }
98
- }
99
- useEffect(() => {
100
- const hasDuplicate = Object.values(actions).some(
101
- (otherAction) => otherAction.name === action.name && otherAction !== actions[idRef.current]
102
- );
103
- if (hasDuplicate) {
104
- addToast({
105
- type: "warning",
106
- message: `Found an already registered action with name ${action.name}.`,
107
- id: `dup-action-${action.name}`
108
- });
109
- }
110
- }, [actions]);
111
- useEffect(() => {
112
- setAction(idRef.current, action);
113
- if (chatComponentsCache.current !== null && action.render !== void 0) {
114
- chatComponentsCache.current.actions[action.name] = action.render;
115
- }
116
- return () => {
117
- removeAction(idRef.current);
118
- };
119
- }, [
120
- setAction,
121
- removeAction,
122
- isFrontendAction(action) ? action.description : void 0,
123
- action.name,
124
- isFrontendAction(action) ? action.disabled : void 0,
125
- isFrontendAction(action) ? action.available : void 0,
126
- // This should be faster than deep equality checking
127
- // In addition, all major JS engines guarantee the order of object keys
128
- JSON.stringify(isFrontendAction(action) ? action.parameters : []),
129
- // include render only if it's a string
130
- typeof action.render === "string" ? action.render : void 0,
131
- // dependencies set by the developer
132
- ...dependencies || []
133
- ]);
134
- }
135
- function isFrontendAction(action) {
136
- return action.name !== "*";
137
- }
138
-
139
- export {
140
- useCopilotAction
141
- };
142
- //# sourceMappingURL=chunk-T3376SZS.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-action.ts"],"sourcesContent":["/**\n * Example usage of useCopilotAction with complex parameters:\n *\n * @example\n * useCopilotAction({\n * name: \"myAction\",\n * parameters: [\n * { name: \"arg1\", type: \"string\", enum: [\"option1\", \"option2\", \"option3\"], required: false },\n * { name: \"arg2\", type: \"number\" },\n * {\n * name: \"arg3\",\n * type: \"object\",\n * attributes: [\n * { name: \"nestedArg1\", type: \"boolean\" },\n * { name: \"xyz\", required: false },\n * ],\n * },\n * { name: \"arg4\", type: \"number[]\" },\n * ],\n * handler: ({ arg1, arg2, arg3, arg4 }) => {\n * const x = arg3.nestedArg1;\n * const z = arg3.xyz;\n * console.log(arg1, arg2, arg3);\n * },\n * });\n *\n * @example\n * // Simple action without parameters\n * useCopilotAction({\n * name: \"myAction\",\n * handler: () => {\n * console.log(\"No parameters provided.\");\n * },\n * });\n *\n * @example\n * // Interactive action with UI rendering and response handling\n * useCopilotAction({\n * name: \"handleMeeting\",\n * description: \"Handle a meeting by booking or canceling\",\n * parameters: [\n * {\n * name: \"meeting\",\n * type: \"string\",\n * description: \"The meeting to handle\",\n * required: true,\n * },\n * {\n * name: \"date\",\n * type: \"string\",\n * description: \"The date of the meeting\",\n * required: true,\n * },\n * {\n * name: \"title\",\n * type: \"string\",\n * description: \"The title of the meeting\",\n * required: true,\n * },\n * ],\n * renderAndWaitForResponse: ({ args, respond, status }) => {\n * const { meeting, date, title } = args;\n * return (\n * <MeetingConfirmationDialog\n * meeting={meeting}\n * date={date}\n * title={title}\n * onConfirm={() => respond('meeting confirmed')}\n * onCancel={() => respond('meeting canceled')}\n * />\n * );\n * },\n * });\n *\n * @example\n * // Catch all action allows you to render actions that are not defined in the frontend\n * useCopilotAction({\n * name: \"*\",\n * render: ({ name, args, status, result, handler, respond }) => {\n * return <div>Rendering action: {name}</div>;\n * },\n * });\n */\n\n/**\n * <img src=\"https://cdn.copilotkit.ai/docs/copilotkit/images/use-copilot-action/useCopilotAction.gif\" width=\"500\" />\n * `useCopilotAction` is a React hook that you can use in your application to provide\n * custom actions that can be called by the AI. Essentially, it allows the Copilot to\n * execute these actions contextually during a chat, based on the user's interactions\n * and needs.\n *\n * Here's how it works:\n *\n * Use `useCopilotAction` to set up actions that the Copilot can call. To provide\n * more context to the Copilot, you can provide it with a `description` (for example to explain\n * what the action does, under which conditions it can be called, etc.).\n *\n * Then you define the parameters of the action, which can be simple, e.g. primitives like strings or numbers,\n * or complex, e.g. objects or arrays.\n *\n * Finally, you provide a `handler` function that receives the parameters and returns a result.\n * CopilotKit takes care of automatically inferring the parameter types, so you get type safety\n * and autocompletion for free.\n *\n * To render a custom UI for the action, you can provide a `render()` function. This function\n * lets you render a custom component or return a string to display.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * useCopilotAction({\n * name: \"sayHello\",\n * description: \"Say hello to someone.\",\n * parameters: [\n * {\n * name: \"name\",\n * type: \"string\",\n * description: \"name of the person to say greet\",\n * },\n * ],\n * handler: async ({ name }) => {\n * alert(`Hello, ${name}!`);\n * },\n * });\n * ```\n *\n * ## Generative UI\n *\n * This hooks enables you to dynamically generate UI elements and render them in the copilot chat. For more information, check out the [Generative UI](/guides/generative-ui) page.\n */\nimport { Parameter, randomId } from \"@copilotkit/shared\";\nimport { createElement, Fragment, useEffect, useRef } from \"react\";\nimport { useCopilotContext } from \"../context/copilot-context\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport {\n ActionRenderProps,\n ActionRenderPropsNoArgsWait,\n ActionRenderPropsWait,\n CatchAllFrontendAction,\n FrontendAction,\n} from \"../types/frontend-action\";\nimport { useToast } from \"../components/toast/toast-provider\";\n\n// We implement useCopilotAction dependency handling so that\n// the developer has the option to not provide any dependencies.\n// In this case, we assume they want to update the handler on each rerender.\n// To avoid getting stuck in an infinite loop, we update the handler directly,\n// skipping React state updates.\n// This is ok in this case, because the handler is not part of any UI that\n// needs to be updated.\n// useCallback, useMemo or other memoization techniques are not suitable here,\n// because they will cause a infinite rerender loop.\nexport function useCopilotAction<const T extends Parameter[] | [] = []>(\n action: FrontendAction<T> | CatchAllFrontendAction,\n dependencies?: any[],\n): void {\n const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();\n const idRef = useRef<string>(randomId());\n const renderAndWaitRef = useRef<RenderAndWaitForResponse | null>(null);\n const activatingMessageIdRef = useRef<string | null>(null);\n const { addToast } = useToast();\n\n // clone the action to avoid mutating the original object\n action = { ...action };\n\n // const { currentlyActivatingHitlActionMessageIdRef } = useCopilotContext() as any; // <-- REMOVE THIS FOR NOW\n\n // If the developer provides a renderAndWaitForResponse function, we transform the action\n // to use a promise internally, so that we can treat it like a normal action.\n if (\n // renderAndWaitForResponse is not available for catch all actions\n isFrontendAction(action) &&\n // check if renderAndWaitForResponse is set\n (action.renderAndWait || action.renderAndWaitForResponse)\n ) {\n (action as any)._isRenderAndWait = true; // Internal flag to identify this action type later\n const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;\n // remove the renderAndWait function from the action\n action.renderAndWait = undefined;\n action.renderAndWaitForResponse = undefined;\n\n // Add a method for use-chat.ts to set the activating message ID.\n // This helps correlate the action instance with the message being processed by use-chat.\n (action as any)._setActivatingMessageId = (id: string | null) => {\n activatingMessageIdRef.current = id;\n };\n\n // add a handler that will be called when the action is executed\n action.handler = useAsyncCallback(async () => {\n const currentActivatingId = activatingMessageIdRef.current;\n // we create a new promise when the handler is called\n let resolve: (result: any) => void;\n let reject: (error: any) => void;\n const promise = new Promise<any>((resolvePromise, rejectPromise) => {\n resolve = resolvePromise;\n reject = rejectPromise;\n });\n renderAndWaitRef.current = {\n promise,\n resolve: resolve!,\n reject: reject!,\n messageId: currentActivatingId,\n };\n // then we await the promise (it will be resolved in the original renderAndWait function)\n const result = await promise;\n return result;\n }, []) as any;\n\n // add a render function that will be called when the action is rendered\n action.render = ((props: ActionRenderProps<T> & { messageId?: string }): React.ReactElement => {\n const currentRenderMessageId = props.messageId;\n // For renderAndWaitForResponse, the 'executing' state might be set by use-chat before\n // this specific action instance's handler (and thus its promise) is ready.\n // This logic adjusts the status to 'inProgress' if the current render\n // isn't for the actively processing HITL action, preventing premature interaction.\n let status = props.status;\n if (props.status === \"executing\") {\n if (!renderAndWaitRef.current || !renderAndWaitRef.current.promise) {\n status = \"inProgress\";\n } else if (\n renderAndWaitRef.current.messageId !== currentRenderMessageId &&\n activatingMessageIdRef.current !== currentRenderMessageId\n ) {\n status = \"inProgress\";\n }\n // If conditions met, status remains 'executing'\n }\n // Create type safe waitProps based on whether T extends empty array or not\n const waitProps = {\n status,\n args: props.args,\n result: props.result,\n // handler and respond should only be provided if this is the truly active instance\n // and its promise infrastructure is ready.\n handler:\n status === \"executing\" &&\n renderAndWaitRef.current &&\n renderAndWaitRef.current.messageId === currentRenderMessageId\n ? renderAndWaitRef.current!.resolve\n : undefined,\n respond:\n status === \"executing\" &&\n renderAndWaitRef.current &&\n renderAndWaitRef.current.messageId === currentRenderMessageId\n ? renderAndWaitRef.current!.resolve\n : undefined,\n } as T extends [] ? ActionRenderPropsNoArgsWait<T> : ActionRenderPropsWait<T>;\n\n // Type guard to check if renderAndWait is for no args case\n const isNoArgsRenderWait = (\n _fn:\n | ((props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement)\n | ((props: ActionRenderPropsWait<T>) => React.ReactElement),\n ): _fn is (props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement => {\n return action.parameters?.length === 0;\n };\n\n // Safely call renderAndWait with correct props type\n if (renderAndWait) {\n if (isNoArgsRenderWait(renderAndWait)) {\n return renderAndWait(waitProps as ActionRenderPropsNoArgsWait<T>);\n } else {\n return renderAndWait(waitProps as ActionRenderPropsWait<T>);\n }\n }\n\n // Return empty Fragment instead of null\n return createElement(Fragment);\n }) as any;\n }\n\n // If the developer doesn't provide dependencies, we assume they want to\n // update handler and render function when the action object changes.\n // This ensures that any captured variables in the handler are up to date.\n if (dependencies === undefined) {\n if (actions[idRef.current]) {\n // catch all actions don't have a handler\n if (isFrontendAction(action)) {\n actions[idRef.current].handler = action.handler as any;\n }\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n // TODO: using as any here because the type definitions are getting to tricky\n // not wasting time on this now - we know the types are compatible\n chatComponentsCache.current.actions[action.name] = action.render as any;\n }\n }\n }\n }\n\n useEffect(() => {\n const hasDuplicate = Object.values(actions).some(\n (otherAction) => otherAction.name === action.name && otherAction !== actions[idRef.current],\n );\n\n if (hasDuplicate) {\n addToast({\n type: \"warning\",\n message: `Found an already registered action with name ${action.name}.`,\n id: `dup-action-${action.name}`,\n });\n }\n }, [actions]);\n\n useEffect(() => {\n setAction(idRef.current, action as any);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n // see comment about type safety above\n chatComponentsCache.current.actions[action.name] = action.render as any;\n }\n return () => {\n // NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.\n // This is because we currently don't have access to the messages array in CopilotContext.\n // UPDATE: We now have access, we should remove the entry if not referenced by any message.\n removeAction(idRef.current);\n };\n }, [\n setAction,\n removeAction,\n isFrontendAction(action) ? action.description : undefined,\n action.name,\n isFrontendAction(action) ? action.disabled : undefined,\n isFrontendAction(action) ? action.available : undefined,\n // This should be faster than deep equality checking\n // In addition, all major JS engines guarantee the order of object keys\n JSON.stringify(isFrontendAction(action) ? action.parameters : []),\n // include render only if it's a string\n typeof action.render === \"string\" ? action.render : undefined,\n // dependencies set by the developer\n ...(dependencies || []),\n ]);\n}\n\nfunction isFrontendAction<T extends Parameter[]>(\n action: FrontendAction<T> | CatchAllFrontendAction,\n): action is FrontendAction<T> {\n return action.name !== \"*\";\n}\n\ninterface RenderAndWaitForResponse {\n promise: Promise<any>;\n resolve: (result: any) => void;\n reject: (error: any) => void;\n messageId: string | null;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoIA,SAAoB,gBAAgB;AACpC,SAAS,eAAe,UAAU,WAAW,cAAc;AAqBpD,SAAS,iBACd,QACA,cACM;AACN,QAAM,EAAE,WAAW,cAAc,SAAS,oBAAoB,IAAI,kBAAkB;AACpF,QAAM,QAAQ,OAAe,SAAS,CAAC;AACvC,QAAM,mBAAmB,OAAwC,IAAI;AACrE,QAAM,yBAAyB,OAAsB,IAAI;AACzD,QAAM,EAAE,SAAS,IAAI,SAAS;AAG9B,WAAS,mBAAK;AAMd;AAAA;AAAA,IAEE,iBAAiB,MAAM;AAAA,KAEtB,OAAO,iBAAiB,OAAO;AAAA,IAChC;AACA,IAAC,OAAe,mBAAmB;AACnC,UAAM,gBAAgB,OAAO,iBAAiB,OAAO;AAErD,WAAO,gBAAgB;AACvB,WAAO,2BAA2B;AAIlC,IAAC,OAAe,0BAA0B,CAAC,OAAsB;AAC/D,6BAAuB,UAAU;AAAA,IACnC;AAGA,WAAO,UAAU,iBAAiB,MAAY;AAC5C,YAAM,sBAAsB,uBAAuB;AAEnD,UAAI;AACJ,UAAI;AACJ,YAAM,UAAU,IAAI,QAAa,CAAC,gBAAgB,kBAAkB;AAClE,kBAAU;AACV,iBAAS;AAAA,MACX,CAAC;AACD,uBAAiB,UAAU;AAAA,QACzB;AAAA,QACA;AAAA,QACA;AAAA,QACA,WAAW;AAAA,MACb;AAEA,YAAM,SAAS,MAAM;AACrB,aAAO;AAAA,IACT,IAAG,CAAC,CAAC;AAGL,WAAO,SAAU,CAAC,UAA6E;AAC7F,YAAM,yBAAyB,MAAM;AAKrC,UAAI,SAAS,MAAM;AACnB,UAAI,MAAM,WAAW,aAAa;AAChC,YAAI,CAAC,iBAAiB,WAAW,CAAC,iBAAiB,QAAQ,SAAS;AAClE,mBAAS;AAAA,QACX,WAC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AAI,cAAc,OAAO;AAAA,MAC3B,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,YAAU,MAAM;AACd,cAAU,MAAM,SAAS,MAAa;AACtC,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AAEvE,0BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,IAC5D;AACA,WAAO,MAAM;AAIX,mBAAa,MAAM,OAAO;AAAA,IAC5B;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,iBAAiB,MAAM,IAAI,OAAO,cAAc;AAAA,IAChD,OAAO;AAAA,IACP,iBAAiB,MAAM,IAAI,OAAO,WAAW;AAAA,IAC7C,iBAAiB,MAAM,IAAI,OAAO,YAAY;AAAA;AAAA;AAAA,IAG9C,KAAK,UAAU,iBAAiB,MAAM,IAAI,OAAO,aAAa,CAAC,CAAC;AAAA;AAAA,IAEhE,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;AAEA,SAAS,iBACP,QAC6B;AAC7B,SAAO,OAAO,SAAS;AACzB;","names":[]}
@@ -1,71 +0,0 @@
1
- import {
2
- useCopilotChat
3
- } from "./chunk-QHCLJODF.mjs";
4
- import {
5
- useToast
6
- } from "./chunk-EFL5OBKN.mjs";
7
- import {
8
- CopilotContext
9
- } from "./chunk-EUX2P2E7.mjs";
10
- import {
11
- __spreadProps,
12
- __spreadValues
13
- } from "./chunk-SKC7AJIV.mjs";
14
-
15
- // src/hooks/use-langgraph-interrupt.ts
16
- import { useContext, useEffect, useMemo } from "react";
17
- import { dataToUUID } from "@copilotkit/shared";
18
- function useLangGraphInterrupt(action, dependencies) {
19
- var _a;
20
- const {
21
- setLangGraphInterruptAction,
22
- removeLangGraphInterruptAction,
23
- langGraphInterruptAction,
24
- threadId
25
- } = useContext(CopilotContext);
26
- const { runChatCompletion } = useCopilotChat();
27
- const { addToast } = useToast();
28
- const actionId = dataToUUID(JSON.stringify(action), "lgAction");
29
- const hasAction = useMemo(
30
- () => Boolean(langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.id),
31
- [langGraphInterruptAction]
32
- );
33
- const isCurrentAction = useMemo(
34
- () => (langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.id) && (langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.id) === actionId,
35
- [langGraphInterruptAction]
36
- );
37
- useEffect(() => {
38
- var _a2;
39
- if (hasAction && isCurrentAction && ((_a2 = langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.event) == null ? void 0 : _a2.response)) {
40
- runChatCompletion();
41
- }
42
- }, [(_a = langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.event) == null ? void 0 : _a.response, runChatCompletion, hasAction, isCurrentAction]);
43
- useEffect(() => {
44
- if (!action)
45
- return;
46
- if (hasAction && !isCurrentAction && !action.enabled) {
47
- addToast({
48
- type: "warning",
49
- message: "An action is already registered for the interrupt event"
50
- });
51
- return;
52
- }
53
- if (hasAction && isCurrentAction) {
54
- return;
55
- }
56
- setLangGraphInterruptAction(threadId, __spreadProps(__spreadValues({}, action), { id: actionId }));
57
- }, [
58
- action,
59
- hasAction,
60
- isCurrentAction,
61
- setLangGraphInterruptAction,
62
- removeLangGraphInterruptAction,
63
- threadId,
64
- ...dependencies || []
65
- ]);
66
- }
67
-
68
- export {
69
- useLangGraphInterrupt
70
- };
71
- //# sourceMappingURL=chunk-U3DQY67T.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-langgraph-interrupt.ts"],"sourcesContent":["import { useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { LangGraphInterruptRender } from \"../types/interrupt-action\";\nimport { useCopilotChat } from \"./use-copilot-chat_internal\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { dataToUUID } from \"@copilotkit/shared\";\n\nexport function useLangGraphInterrupt<TEventValue = any>(\n action: Omit<LangGraphInterruptRender<TEventValue>, \"id\">,\n dependencies?: any[],\n) {\n const {\n setLangGraphInterruptAction,\n removeLangGraphInterruptAction,\n langGraphInterruptAction,\n threadId,\n } = useContext(CopilotContext);\n const { runChatCompletion } = useCopilotChat();\n const { addToast } = useToast();\n\n const actionId = dataToUUID(JSON.stringify(action), \"lgAction\");\n // We only consider action to be defined once the ID is there\n const hasAction = useMemo(\n () => Boolean(langGraphInterruptAction?.id),\n [langGraphInterruptAction],\n );\n\n const isCurrentAction = useMemo(\n () => langGraphInterruptAction?.id && langGraphInterruptAction?.id === actionId,\n [langGraphInterruptAction],\n );\n\n // Run chat completion to submit a response event. Only if it's the current action\n useEffect(() => {\n if (hasAction && isCurrentAction && langGraphInterruptAction?.event?.response) {\n runChatCompletion();\n }\n }, [langGraphInterruptAction?.event?.response, runChatCompletion, hasAction, isCurrentAction]);\n\n useEffect(() => {\n if (!action) return;\n // An action was already set, with no conditions and it's not the action we're using right now.\n // Show a warning, as this action will not be executed\n if (hasAction && !isCurrentAction && !action.enabled) {\n addToast({\n type: \"warning\",\n message: \"An action is already registered for the interrupt event\",\n });\n return;\n }\n\n if (hasAction && isCurrentAction) {\n return;\n }\n\n setLangGraphInterruptAction(threadId, { ...action, id: actionId });\n }, [\n action,\n hasAction,\n isCurrentAction,\n setLangGraphInterruptAction,\n removeLangGraphInterruptAction,\n threadId,\n ...(dependencies || []),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,YAAY,WAAW,eAAe;AAK/C,SAAS,kBAAkB;AAEpB,SAAS,sBACd,QACA,cACA;AAVF;AAWE,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAC7B,QAAM,EAAE,kBAAkB,IAAI,eAAe;AAC7C,QAAM,EAAE,SAAS,IAAI,SAAS;AAE9B,QAAM,WAAW,WAAW,KAAK,UAAU,MAAM,GAAG,UAAU;AAE9D,QAAM,YAAY;AAAA,IAChB,MAAM,QAAQ,qEAA0B,EAAE;AAAA,IAC1C,CAAC,wBAAwB;AAAA,EAC3B;AAEA,QAAM,kBAAkB;AAAA,IACtB,OAAM,qEAA0B,QAAM,qEAA0B,QAAO;AAAA,IACvE,CAAC,wBAAwB;AAAA,EAC3B;AAGA,YAAU,MAAM;AAjClB,QAAAA;AAkCI,QAAI,aAAa,qBAAmBA,MAAA,qEAA0B,UAA1B,gBAAAA,IAAiC,WAAU;AAC7E,wBAAkB;AAAA,IACpB;AAAA,EACF,GAAG,EAAC,0EAA0B,UAA1B,mBAAiC,UAAU,mBAAmB,WAAW,eAAe,CAAC;AAE7F,YAAU,MAAM;AACd,QAAI,CAAC;AAAQ;AAGb,QAAI,aAAa,CAAC,mBAAmB,CAAC,OAAO,SAAS;AACpD,eAAS;AAAA,QACP,MAAM;AAAA,QACN,SAAS;AAAA,MACX,CAAC;AACD;AAAA,IACF;AAEA,QAAI,aAAa,iBAAiB;AAChC;AAAA,IACF;AAEA,gCAA4B,UAAU,iCAAK,SAAL,EAAa,IAAI,SAAS,EAAC;AAAA,EACnE,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":["_a"]}
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-YPBKY4KY.mjs.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-YPSGKPDA.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context/copilot-messages-context.tsx"],"sourcesContent":["/**\n * An internal context to separate the messages state (which is constantly changing) from the rest of CopilotKit context\n */\n\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport React from \"react\";\nimport { SuggestionItem } from \"../utils/suggestions\";\n\nexport interface CopilotMessagesContextParams {\n messages: Message[];\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>; // suggestions state\n suggestions: SuggestionItem[];\n setSuggestions: React.Dispatch<React.SetStateAction<SuggestionItem[]>>;\n}\n\nconst emptyCopilotContext: CopilotMessagesContextParams = {\n messages: [],\n setMessages: () => [],\n // suggestions state\n suggestions: [],\n setSuggestions: () => [],\n};\n\nexport const CopilotMessagesContext =\n React.createContext<CopilotMessagesContextParams>(emptyCopilotContext);\n\nexport function useCopilotMessagesContext(): CopilotMessagesContextParams {\n const context = React.useContext(CopilotMessagesContext);\n if (context === emptyCopilotContext) {\n throw new Error(\n \"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`\",\n );\n }\n return context;\n}\n"],"mappings":";AAKA,OAAO,WAAW;AAUlB,IAAM,sBAAoD;AAAA,EACxD,UAAU,CAAC;AAAA,EACX,aAAa,MAAM,CAAC;AAAA;AAAA,EAEpB,aAAa,CAAC;AAAA,EACd,gBAAgB,MAAM,CAAC;AACzB;AAEO,IAAM,yBACX,MAAM,cAA4C,mBAAmB;AAEhE,SAAS,4BAA0D;AACxE,QAAM,UAAU,MAAM,WAAW,sBAAsB;AACvD,MAAI,YAAY,qBAAqB;AACnC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
@@ -1,47 +0,0 @@
1
- import {
2
- useCopilotContext
3
- } from "./chunk-EUX2P2E7.mjs";
4
-
5
- // src/hooks/use-langgraph-interrupt-render.ts
6
- import React, { useCallback } from "react";
7
- var InterruptRenderer = ({ event, result, render, resolve }) => {
8
- return render({ event, result, resolve });
9
- };
10
- function useLangGraphInterruptRender() {
11
- const { langGraphInterruptAction, setLangGraphInterruptAction, agentSession, threadId } = useCopilotContext();
12
- const responseRef = React.useRef();
13
- const resolveInterrupt = useCallback(
14
- (response) => {
15
- responseRef.current = response;
16
- setTimeout(() => {
17
- setLangGraphInterruptAction(threadId, { event: { response } });
18
- }, 0);
19
- },
20
- [setLangGraphInterruptAction, threadId]
21
- );
22
- if (!langGraphInterruptAction || !langGraphInterruptAction.event || !langGraphInterruptAction.render)
23
- return null;
24
- const { render, handler, event, enabled } = langGraphInterruptAction;
25
- const conditionsMet = !agentSession || !enabled ? true : enabled({ eventValue: event.value, agentMetadata: agentSession });
26
- if (!conditionsMet) {
27
- return null;
28
- }
29
- let result = null;
30
- if (handler) {
31
- result = handler({
32
- event,
33
- resolve: resolveInterrupt
34
- });
35
- }
36
- return React.createElement(InterruptRenderer, {
37
- event,
38
- result,
39
- render,
40
- resolve: resolveInterrupt
41
- });
42
- }
43
-
44
- export {
45
- useLangGraphInterruptRender
46
- };
47
- //# sourceMappingURL=chunk-ZVOLWJCY.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-langgraph-interrupt-render.ts"],"sourcesContent":["import { useCopilotContext } from \"../context\";\nimport React, { useCallback } from \"react\";\nimport { executeConditions } from \"@copilotkit/shared\";\n\ntype InterruptProps = {\n event: any;\n result: any;\n render: (props: {\n event: any;\n result: any;\n resolve: (response: string) => void;\n }) => string | React.ReactElement;\n resolve: (response: string) => void;\n};\n\nconst InterruptRenderer: React.FC<InterruptProps> = ({ event, result, render, resolve }) => {\n return render({ event, result, resolve });\n};\n\nexport function useLangGraphInterruptRender(): string | React.ReactElement | null {\n const { langGraphInterruptAction, setLangGraphInterruptAction, agentSession, threadId } =\n useCopilotContext();\n\n const responseRef = React.useRef<string>();\n const resolveInterrupt = useCallback(\n (response: string) => {\n responseRef.current = response;\n // Use setTimeout to defer the state update to next tick\n setTimeout(() => {\n setLangGraphInterruptAction(threadId, { event: { response } });\n }, 0);\n },\n [setLangGraphInterruptAction, threadId],\n );\n\n if (\n !langGraphInterruptAction ||\n !langGraphInterruptAction.event ||\n !langGraphInterruptAction.render\n )\n return null;\n\n const { render, handler, event, enabled } = langGraphInterruptAction;\n\n const conditionsMet =\n !agentSession || !enabled\n ? true\n : enabled({ eventValue: event.value, agentMetadata: agentSession });\n\n if (!conditionsMet) {\n return null;\n }\n\n let result = null;\n if (handler) {\n result = handler({\n event,\n resolve: resolveInterrupt,\n });\n }\n\n return React.createElement(InterruptRenderer, {\n event,\n result,\n render,\n resolve: resolveInterrupt,\n });\n}\n"],"mappings":";;;;;AACA,OAAO,SAAS,mBAAmB;AAcnC,IAAM,oBAA8C,CAAC,EAAE,OAAO,QAAQ,QAAQ,QAAQ,MAAM;AAC1F,SAAO,OAAO,EAAE,OAAO,QAAQ,QAAQ,CAAC;AAC1C;AAEO,SAAS,8BAAkE;AAChF,QAAM,EAAE,0BAA0B,6BAA6B,cAAc,SAAS,IACpF,kBAAkB;AAEpB,QAAM,cAAc,MAAM,OAAe;AACzC,QAAM,mBAAmB;AAAA,IACvB,CAAC,aAAqB;AACpB,kBAAY,UAAU;AAEtB,iBAAW,MAAM;AACf,oCAA4B,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAAA,MAC/D,GAAG,CAAC;AAAA,IACN;AAAA,IACA,CAAC,6BAA6B,QAAQ;AAAA,EACxC;AAEA,MACE,CAAC,4BACD,CAAC,yBAAyB,SAC1B,CAAC,yBAAyB;AAE1B,WAAO;AAET,QAAM,EAAE,QAAQ,SAAS,OAAO,QAAQ,IAAI;AAE5C,QAAM,gBACJ,CAAC,gBAAgB,CAAC,UACd,OACA,QAAQ,EAAE,YAAY,MAAM,OAAO,eAAe,aAAa,CAAC;AAEtE,MAAI,CAAC,eAAe;AAClB,WAAO;AAAA,EACT;AAEA,MAAI,SAAS;AACb,MAAI,SAAS;AACX,aAAS,QAAQ;AAAA,MACf;AAAA,MACA,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AAEA,SAAO,MAAM,cAAc,mBAAmB;AAAA,IAC5C;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS;AAAA,EACX,CAAC;AACH;","names":[]}