@copilotkit/react-core 1.10.7-next.0 → 1.50.0-beta.1
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.
- package/CHANGELOG.md +0 -7
- package/dist/chunk-24SCZAB4.mjs +143 -0
- package/dist/chunk-24SCZAB4.mjs.map +1 -0
- package/dist/chunk-36KQV2NA.mjs +1 -0
- package/dist/{chunk-U7QULEVO.mjs → chunk-3GURHDG7.mjs} +3 -3
- package/dist/{chunk-OAGAFMUM.mjs → chunk-3R423LZT.mjs} +6 -5
- package/dist/{chunk-OAGAFMUM.mjs.map → chunk-3R423LZT.mjs.map} +1 -1
- package/dist/chunk-5X5DJRQQ.mjs +71 -0
- package/dist/chunk-5X5DJRQQ.mjs.map +1 -0
- package/dist/{chunk-N4WEHORG.mjs → chunk-6PUNP7CD.mjs} +4 -4
- package/dist/chunk-6PUNP7CD.mjs.map +1 -0
- package/dist/chunk-7BYHZLPL.mjs +32 -0
- package/dist/chunk-7BYHZLPL.mjs.map +1 -0
- package/dist/chunk-7DTB7S5V.mjs +83 -0
- package/dist/chunk-7DTB7S5V.mjs.map +1 -0
- package/dist/chunk-A6NKSGH3.mjs +1 -0
- package/dist/{chunk-GT5WI3AF.mjs → chunk-BR5YEYZJ.mjs} +7 -9
- package/dist/chunk-BR5YEYZJ.mjs.map +1 -0
- package/dist/chunk-CB7CRBDG.mjs +48 -0
- package/dist/chunk-CB7CRBDG.mjs.map +1 -0
- package/dist/chunk-CYDWEPFL.mjs +1 -0
- package/dist/{chunk-BBUQMG45.mjs → chunk-D3QSYDJR.mjs} +3 -3
- package/dist/{chunk-BBUQMG45.mjs.map → chunk-D3QSYDJR.mjs.map} +1 -1
- package/dist/chunk-DCHSCK62.mjs +549 -0
- package/dist/chunk-DCHSCK62.mjs.map +1 -0
- package/dist/{chunk-ZLQVRPDS.mjs → chunk-DMLQZG75.mjs} +1 -1
- package/dist/chunk-DMLQZG75.mjs.map +1 -0
- package/dist/chunk-F555TVE4.mjs +33 -0
- package/dist/chunk-F555TVE4.mjs.map +1 -0
- package/dist/{chunk-C6IANC2R.mjs → chunk-FBD24VEH.mjs} +7 -43
- package/dist/chunk-FBD24VEH.mjs.map +1 -0
- package/dist/chunk-FD6FGKYY.mjs +1 -0
- package/dist/chunk-FDOMAPJY.mjs +59 -0
- package/dist/chunk-FDOMAPJY.mjs.map +1 -0
- package/dist/{chunk-E3624YDU.mjs → chunk-GMI4KO4X.mjs} +8 -6
- package/dist/{chunk-E3624YDU.mjs.map → chunk-GMI4KO4X.mjs.map} +1 -1
- package/dist/chunk-IUSKVYUI.mjs +13 -0
- package/dist/chunk-IUSKVYUI.mjs.map +1 -0
- package/dist/chunk-JD7BAH7U.mjs +1 -0
- package/dist/{chunk-EDBI5PCJ.mjs → chunk-JRT5BJF3.mjs} +2 -2
- package/dist/{chunk-N4VN2B5S.mjs → chunk-LHERIF3L.mjs} +2 -2
- package/dist/{chunk-DXEQPN43.mjs → chunk-LHKZJ2ND.mjs} +3 -3
- package/dist/chunk-NB2FKV2V.mjs +1 -0
- package/dist/chunk-NBK4KBLX.mjs +54 -0
- package/dist/chunk-NBK4KBLX.mjs.map +1 -0
- package/dist/chunk-NG26QEGF.mjs +46 -0
- package/dist/chunk-NG26QEGF.mjs.map +1 -0
- package/dist/chunk-NROJOTQP.mjs +163 -0
- package/dist/chunk-NROJOTQP.mjs.map +1 -0
- package/dist/{chunk-MGWRDFBE.mjs → chunk-OVYFRPSN.mjs} +2 -2
- package/dist/{chunk-MQYWH4E6.mjs → chunk-QU6NONOD.mjs} +2 -2
- package/dist/chunk-R4MR43UQ.mjs +101 -0
- package/dist/chunk-R4MR43UQ.mjs.map +1 -0
- package/dist/chunk-SPCZTZCY.mjs +1 -0
- package/dist/chunk-SPCZTZCY.mjs.map +1 -0
- package/dist/chunk-TXI72QHK.mjs +80 -0
- package/dist/chunk-TXI72QHK.mjs.map +1 -0
- package/dist/chunk-UJBV5GAG.mjs +767 -0
- package/dist/chunk-UJBV5GAG.mjs.map +1 -0
- package/dist/{chunk-OKCYPO4I.mjs → chunk-WMJVBMUX.mjs} +3 -3
- package/dist/{chunk-EUX2P2E7.mjs → chunk-WVLHXIFP.mjs} +11 -9
- package/dist/chunk-WVLHXIFP.mjs.map +1 -0
- package/dist/{chunk-5FHSUKQL.mjs → chunk-YYN33GSG.mjs} +5 -2
- package/dist/chunk-YYN33GSG.mjs.map +1 -0
- package/dist/chunk-ZVF5Q6IH.mjs +29 -0
- package/dist/chunk-ZVF5Q6IH.mjs.map +1 -0
- package/dist/components/CopilotListeners.d.ts +3 -0
- package/dist/components/CopilotListeners.js +234 -0
- package/dist/components/CopilotListeners.js.map +1 -0
- package/dist/components/CopilotListeners.mjs +14 -0
- package/dist/components/CopilotListeners.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.js +14 -48
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
- package/dist/components/copilot-provider/copilot-messages.mjs +3 -3
- package/dist/components/copilot-provider/copilotkit-props.d.ts +9 -2
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +2 -1
- package/dist/components/copilot-provider/copilotkit.js +528 -370
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +15 -10
- package/dist/components/copilot-provider/index.d.ts +2 -1
- package/dist/components/copilot-provider/index.js +528 -370
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +16 -10
- package/dist/components/dev-console/console-trigger.js +10 -8
- package/dist/components/dev-console/console-trigger.js.map +1 -1
- package/dist/components/dev-console/console-trigger.mjs +4 -4
- package/dist/components/dev-console/developer-console-modal.js +10 -8
- package/dist/components/dev-console/developer-console-modal.js.map +1 -1
- package/dist/components/dev-console/developer-console-modal.mjs +3 -3
- package/dist/components/error-boundary/error-boundary.d.ts +7 -7
- package/dist/components/error-boundary/error-boundary.js +3 -3
- package/dist/components/error-boundary/error-boundary.js.map +1 -1
- package/dist/components/error-boundary/error-boundary.mjs +2 -2
- package/dist/components/error-boundary/error-utils.d.ts +1 -1
- package/dist/components/error-boundary/error-utils.js +3 -3
- package/dist/components/error-boundary/error-utils.js.map +1 -1
- package/dist/components/error-boundary/error-utils.mjs +1 -1
- package/dist/components/index.d.ts +2 -1
- package/dist/components/index.js +528 -370
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +17 -10
- package/dist/components/toast/exclamation-mark-icon.d.ts +2 -2
- package/dist/components/toast/toast-provider.d.ts +3 -3
- package/dist/components/usage-banner.d.ts +2 -2
- package/dist/context/coagent-state-renders-context.d.ts +24 -0
- package/dist/context/coagent-state-renders-context.js +91 -0
- package/dist/context/coagent-state-renders-context.js.map +1 -0
- package/dist/context/coagent-state-renders-context.mjs +12 -0
- package/dist/context/coagent-state-renders-context.mjs.map +1 -0
- package/dist/context/copilot-context.d.ts +1 -1
- package/dist/context/copilot-context.js +10 -8
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/copilot-messages-context.d.ts +18 -11
- package/dist/context/copilot-messages-context.js.map +1 -1
- package/dist/context/copilot-messages-context.mjs +1 -1
- package/dist/context/index.d.ts +6 -2
- package/dist/context/index.js +110 -10
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +20 -4
- package/dist/context/threads-context.d.ts +16 -0
- package/dist/context/threads-context.js +59 -0
- package/dist/context/threads-context.js.map +1 -0
- package/dist/context/threads-context.mjs +12 -0
- package/dist/context/threads-context.mjs.map +1 -0
- package/dist/{copilot-context-cf9de848.d.ts → copilot-context-1cd70a3f.d.ts} +36 -32
- package/dist/hooks/index.d.ts +9 -6
- package/dist/hooks/index.js +1445 -1839
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +47 -56
- package/dist/hooks/use-coagent-state-render-bridge.d.ts +100 -0
- package/dist/hooks/use-coagent-state-render-bridge.js +211 -0
- package/dist/hooks/use-coagent-state-render-bridge.js.map +1 -0
- package/dist/hooks/use-coagent-state-render-bridge.mjs +15 -0
- package/dist/hooks/use-coagent-state-render-bridge.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render.js +31 -20
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +3 -2
- package/dist/hooks/use-coagent.d.ts +2 -14
- package/dist/hooks/use-coagent.js +109 -1906
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +1 -30
- package/dist/hooks/use-configure-chat-suggestions.d.ts +36 -0
- package/dist/hooks/use-configure-chat-suggestions.js +210 -0
- package/dist/hooks/use-configure-chat-suggestions.js.map +1 -0
- package/dist/hooks/use-configure-chat-suggestions.mjs +13 -0
- package/dist/hooks/use-configure-chat-suggestions.mjs.map +1 -0
- package/dist/hooks/use-copilot-action.d.ts +8 -45
- package/dist/hooks/use-copilot-action.js +207 -349
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -5
- package/dist/hooks/use-copilot-additional-instructions.js +10 -8
- package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
- package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
- package/dist/hooks/use-copilot-authenticated-action.js +224 -248
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +6 -6
- package/dist/hooks/use-copilot-chat-headless_c.d.ts +2 -10
- package/dist/hooks/use-copilot-chat-headless_c.js +973 -1369
- package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -1
- package/dist/hooks/use-copilot-chat-headless_c.mjs +12 -22
- package/dist/hooks/use-copilot-chat.d.ts +2 -10
- package/dist/hooks/use-copilot-chat.js +967 -1388
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +12 -22
- package/dist/hooks/use-copilot-chat_internal.d.ts +38 -14
- package/dist/hooks/use-copilot-chat_internal.js +965 -1362
- package/dist/hooks/use-copilot-chat_internal.js.map +1 -1
- package/dist/hooks/use-copilot-chat_internal.mjs +13 -23
- package/dist/hooks/use-copilot-readable.d.ts +1 -1
- package/dist/hooks/use-copilot-readable.js +6 -157
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +1 -2
- package/dist/hooks/use-default-tool.js +204 -349
- package/dist/hooks/use-default-tool.js.map +1 -1
- package/dist/hooks/use-default-tool.mjs +5 -6
- package/dist/hooks/use-flat-category-store.js +4 -1
- package/dist/hooks/use-flat-category-store.js.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +1 -1
- package/dist/hooks/use-frontend-tool.d.ts +3 -3
- package/dist/hooks/use-frontend-tool.js +30 -370
- package/dist/hooks/use-frontend-tool.js.map +1 -1
- package/dist/hooks/use-frontend-tool.mjs +1 -6
- package/dist/hooks/use-human-in-the-loop.d.ts +4 -3
- package/dist/hooks/use-human-in-the-loop.js +61 -393
- package/dist/hooks/use-human-in-the-loop.js.map +1 -1
- package/dist/hooks/use-human-in-the-loop.mjs +1 -6
- package/dist/hooks/use-langgraph-interrupt-render.d.ts +3 -2
- package/dist/hooks/use-langgraph-interrupt-render.js +92 -36
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +6 -4
- package/dist/hooks/use-langgraph-interrupt.d.ts +1 -1
- package/dist/hooks/use-langgraph-interrupt.js +24 -1642
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +2 -23
- package/dist/hooks/use-lazy-tool-renderer.d.ts +6 -0
- package/dist/hooks/use-lazy-tool-renderer.js +53 -0
- package/dist/hooks/use-lazy-tool-renderer.js.map +1 -0
- package/dist/hooks/use-lazy-tool-renderer.mjs +8 -0
- package/dist/hooks/use-lazy-tool-renderer.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.js +11 -9
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/hooks/use-render-tool-call.d.ts +7 -6
- package/dist/hooks/use-render-tool-call.js +36 -386
- package/dist/hooks/use-render-tool-call.js.map +1 -1
- package/dist/hooks/use-render-tool-call.mjs +1 -6
- package/dist/index.d.ts +11 -6
- package/dist/index.js +1841 -2056
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -59
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js +1 -0
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +18 -11
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +1 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +18 -11
- package/dist/setupTests.d.ts +2 -2
- package/dist/types/frontend-action.d.ts +5 -5
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +2 -1
- package/dist/types/interrupt-action.d.ts +1 -1
- package/dist/types/interrupt-action.js.map +1 -1
- package/dist/types/interrupt-action.mjs +1 -0
- package/dist/utils/index.d.ts +0 -12
- package/dist/utils/index.js +6 -325
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +1 -21
- package/dist/utils/utils.js.map +1 -1
- package/dist/v2/index.d.ts +2 -0
- package/dist/v2/index.js +27 -0
- package/dist/v2/index.js.map +1 -0
- package/dist/v2/index.mjs +4 -0
- package/dist/v2/index.mjs.map +1 -0
- package/package.json +20 -9
- package/src/components/CopilotListeners.tsx +82 -0
- package/src/components/copilot-provider/copilot-messages.tsx +46 -47
- package/src/components/copilot-provider/copilotkit-props.tsx +8 -1
- package/src/components/copilot-provider/copilotkit.tsx +177 -144
- package/src/components/error-boundary/error-utils.tsx +3 -3
- package/src/context/coagent-state-renders-context.tsx +76 -0
- package/src/context/copilot-context.tsx +21 -21
- package/src/context/copilot-messages-context.tsx +3 -3
- package/src/context/index.ts +8 -0
- package/src/context/threads-context.tsx +41 -0
- package/src/hooks/index.ts +7 -3
- package/src/hooks/use-coagent-state-render-bridge.tsx +293 -0
- package/src/hooks/use-coagent-state-render.ts +4 -7
- package/src/hooks/use-coagent.ts +129 -249
- package/src/hooks/use-configure-chat-suggestions.tsx +85 -0
- package/src/hooks/use-copilot-action.ts +88 -199
- package/src/hooks/use-copilot-chat-headless_c.ts +2 -1
- package/src/hooks/use-copilot-chat.ts +4 -1
- package/src/hooks/use-copilot-chat_internal.ts +349 -258
- package/src/hooks/use-copilot-readable.ts +6 -26
- package/src/hooks/use-flat-category-store.ts +4 -3
- package/src/hooks/use-frontend-tool.ts +67 -8
- package/src/hooks/use-human-in-the-loop.ts +111 -13
- package/src/hooks/use-langgraph-interrupt-render.ts +90 -40
- package/src/hooks/use-langgraph-interrupt.ts +18 -47
- package/src/hooks/use-lazy-tool-renderer.tsx +30 -0
- package/src/hooks/use-make-copilot-document-readable.ts +1 -1
- package/src/hooks/use-render-tool-call.ts +73 -13
- package/src/types/index.ts +1 -0
- package/src/types/interrupt-action.ts +8 -4
- package/src/utils/index.ts +0 -3
- package/src/utils/utils.ts +7 -0
- package/src/v2/index.ts +2 -0
- package/src/v2/styles.css +1 -0
- package/tsup.config.ts +1 -1
- package/dist/chunk-5FHSUKQL.mjs.map +0 -1
- package/dist/chunk-6GPVUMBU.mjs +0 -13
- package/dist/chunk-6GPVUMBU.mjs.map +0 -1
- package/dist/chunk-AKR2SSUJ.mjs +0 -22
- package/dist/chunk-AKR2SSUJ.mjs.map +0 -1
- package/dist/chunk-BXXJQ2BL.mjs +0 -239
- package/dist/chunk-BXXJQ2BL.mjs.map +0 -1
- package/dist/chunk-C6IANC2R.mjs.map +0 -1
- package/dist/chunk-DZTU6PGS.mjs +0 -1
- package/dist/chunk-EUX2P2E7.mjs.map +0 -1
- package/dist/chunk-EVVO67QO.mjs +0 -25
- package/dist/chunk-EVVO67QO.mjs.map +0 -1
- package/dist/chunk-GGLAHG3I.mjs +0 -804
- package/dist/chunk-GGLAHG3I.mjs.map +0 -1
- package/dist/chunk-GT5WI3AF.mjs.map +0 -1
- package/dist/chunk-HDOG2RTM.mjs +0 -37
- package/dist/chunk-HDOG2RTM.mjs.map +0 -1
- package/dist/chunk-MIXSWZSB.mjs +0 -71
- package/dist/chunk-MIXSWZSB.mjs.map +0 -1
- package/dist/chunk-N4WEHORG.mjs.map +0 -1
- package/dist/chunk-NSBAWITG.mjs +0 -332
- package/dist/chunk-NSBAWITG.mjs.map +0 -1
- package/dist/chunk-OT5NHMEC.mjs +0 -723
- package/dist/chunk-OT5NHMEC.mjs.map +0 -1
- package/dist/chunk-T3376SZS.mjs +0 -142
- package/dist/chunk-T3376SZS.mjs.map +0 -1
- package/dist/chunk-YPBKY4KY.mjs +0 -1
- package/dist/chunk-YPSGKPDA.mjs +0 -1
- package/dist/chunk-ZLQVRPDS.mjs.map +0 -1
- package/dist/chunk-ZVOLWJCY.mjs +0 -47
- package/dist/chunk-ZVOLWJCY.mjs.map +0 -1
- package/dist/hooks/use-chat.d.ts +0 -145
- package/dist/hooks/use-chat.js +0 -1129
- package/dist/hooks/use-chat.js.map +0 -1
- package/dist/hooks/use-chat.mjs +0 -14
- package/dist/index-1f5b66ac.d.ts +0 -36
- package/dist/utils/extract.d.ts +0 -43
- package/dist/utils/extract.js +0 -208
- package/dist/utils/extract.js.map +0 -1
- package/dist/utils/extract.mjs +0 -25
- package/dist/utils/suggestions.d.ts +0 -11
- package/dist/utils/suggestions.js +0 -343
- package/dist/utils/suggestions.js.map +0 -1
- package/dist/utils/suggestions.mjs +0 -25
- package/src/hooks/use-chat.ts +0 -1142
- package/src/utils/extract.ts +0 -215
- package/src/utils/suggestions.ts +0 -208
- /package/dist/{chunk-DZTU6PGS.mjs.map → chunk-36KQV2NA.mjs.map} +0 -0
- /package/dist/{chunk-U7QULEVO.mjs.map → chunk-3GURHDG7.mjs.map} +0 -0
- /package/dist/{chunk-YPBKY4KY.mjs.map → chunk-A6NKSGH3.mjs.map} +0 -0
- /package/dist/{chunk-YPSGKPDA.mjs.map → chunk-CYDWEPFL.mjs.map} +0 -0
- /package/dist/{hooks/use-chat.mjs.map → chunk-FD6FGKYY.mjs.map} +0 -0
- /package/dist/{utils/extract.mjs.map → chunk-JD7BAH7U.mjs.map} +0 -0
- /package/dist/{chunk-EDBI5PCJ.mjs.map → chunk-JRT5BJF3.mjs.map} +0 -0
- /package/dist/{chunk-N4VN2B5S.mjs.map → chunk-LHERIF3L.mjs.map} +0 -0
- /package/dist/{chunk-DXEQPN43.mjs.map → chunk-LHKZJ2ND.mjs.map} +0 -0
- /package/dist/{utils/suggestions.mjs.map → chunk-NB2FKV2V.mjs.map} +0 -0
- /package/dist/{chunk-MGWRDFBE.mjs.map → chunk-OVYFRPSN.mjs.map} +0 -0
- /package/dist/{chunk-MQYWH4E6.mjs.map → chunk-QU6NONOD.mjs.map} +0 -0
- /package/dist/{chunk-OKCYPO4I.mjs.map → chunk-WMJVBMUX.mjs.map} +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useState,
|
|
6
|
+
useRef,
|
|
7
|
+
ReactNode,
|
|
8
|
+
RefObject,
|
|
9
|
+
} from "react";
|
|
10
|
+
import { CoAgentStateRender } from "../types/coagent-action";
|
|
11
|
+
|
|
12
|
+
interface StateRenderClaim {
|
|
13
|
+
stateRenderId: string;
|
|
14
|
+
stateSnapshot?: any;
|
|
15
|
+
runId?: string;
|
|
16
|
+
locked?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type ClaimsRef = Record<string, StateRenderClaim>;
|
|
20
|
+
|
|
21
|
+
export interface CoAgentStateRendersContextValue {
|
|
22
|
+
coAgentStateRenders: Record<string, CoAgentStateRender<any>>;
|
|
23
|
+
setCoAgentStateRender: (id: string, stateRender: CoAgentStateRender<any>) => void;
|
|
24
|
+
removeCoAgentStateRender: (id: string) => void;
|
|
25
|
+
claimsRef: RefObject<ClaimsRef>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const CoAgentStateRendersContext = createContext<CoAgentStateRendersContextValue | undefined>(
|
|
29
|
+
undefined,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export function CoAgentStateRendersProvider({ children }: { children: ReactNode }) {
|
|
33
|
+
const [coAgentStateRenders, setCoAgentStateRenders] = useState<
|
|
34
|
+
Record<string, CoAgentStateRender<any>>
|
|
35
|
+
>({});
|
|
36
|
+
|
|
37
|
+
const setCoAgentStateRender = useCallback((id: string, stateRender: CoAgentStateRender<any>) => {
|
|
38
|
+
setCoAgentStateRenders((prevPoints) => ({
|
|
39
|
+
...prevPoints,
|
|
40
|
+
[id]: stateRender,
|
|
41
|
+
}));
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
const removeCoAgentStateRender = useCallback((id: string) => {
|
|
45
|
+
setCoAgentStateRenders((prevPoints) => {
|
|
46
|
+
const newPoints = { ...prevPoints };
|
|
47
|
+
delete newPoints[id];
|
|
48
|
+
return newPoints;
|
|
49
|
+
});
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
const claimsRef = useRef<ClaimsRef>({});
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<CoAgentStateRendersContext.Provider
|
|
56
|
+
value={{
|
|
57
|
+
coAgentStateRenders,
|
|
58
|
+
setCoAgentStateRender,
|
|
59
|
+
removeCoAgentStateRender,
|
|
60
|
+
claimsRef,
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
{children}
|
|
64
|
+
</CoAgentStateRendersContext.Provider>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useCoAgentStateRenders() {
|
|
69
|
+
const context = useContext(CoAgentStateRendersContext);
|
|
70
|
+
if (!context) {
|
|
71
|
+
throw new Error("useCoAgentStateRenders must be used within CoAgentStateRendersProvider");
|
|
72
|
+
}
|
|
73
|
+
return context;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { CoAgentStateRendersContext };
|
|
@@ -22,10 +22,10 @@ import {
|
|
|
22
22
|
} from "@copilotkit/runtime-client-gql";
|
|
23
23
|
import { Agent } from "@copilotkit/runtime-client-gql";
|
|
24
24
|
import {
|
|
25
|
-
|
|
25
|
+
LangGraphInterruptRender,
|
|
26
26
|
LangGraphInterruptActionSetter,
|
|
27
|
+
QueuedInterruptEvent,
|
|
27
28
|
} from "../types/interrupt-action";
|
|
28
|
-
import { SuggestionItem } from "../utils/suggestions";
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Interface for the configuration of the Copilot API.
|
|
@@ -95,10 +95,10 @@ export interface CopilotApiConfig {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
export type InChatRenderFunction<TProps = ActionRenderProps<any> | CatchAllActionRenderProps<any>> =
|
|
98
|
-
(props: TProps) => string | JSX.Element;
|
|
98
|
+
(props: TProps) => string | React.JSX.Element;
|
|
99
99
|
export type CoagentInChatRenderFunction = (
|
|
100
100
|
props: CoAgentStateRenderProps<any>,
|
|
101
|
-
) => string | JSX.Element | undefined | null;
|
|
101
|
+
) => string | React.JSX.Element | undefined | null;
|
|
102
102
|
|
|
103
103
|
export interface ChatComponentsCache {
|
|
104
104
|
actions: Record<string, InChatRenderFunction | string>;
|
|
@@ -127,10 +127,9 @@ export interface CopilotContextParams {
|
|
|
127
127
|
setAction: (id: string, action: FrontendAction<any>) => void;
|
|
128
128
|
removeAction: (id: string) => void;
|
|
129
129
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
removeCoAgentStateRender: (id: string) => void;
|
|
130
|
+
// registered actions for component-based rendering
|
|
131
|
+
setRegisteredActions: (actionConfig: any) => string;
|
|
132
|
+
removeRegisteredAction: (actionKey: string) => void;
|
|
134
133
|
|
|
135
134
|
chatComponentsCache: React.RefObject<ChatComponentsCache>;
|
|
136
135
|
|
|
@@ -195,9 +194,6 @@ export interface CopilotContextParams {
|
|
|
195
194
|
// i.e. when using `stop()` from `useChat`
|
|
196
195
|
chatAbortControllerRef: React.MutableRefObject<AbortController | null>;
|
|
197
196
|
|
|
198
|
-
// runtime
|
|
199
|
-
runtimeClient: CopilotRuntimeClient;
|
|
200
|
-
|
|
201
197
|
/**
|
|
202
198
|
* The forwarded parameters to use for the task.
|
|
203
199
|
*/
|
|
@@ -221,9 +217,12 @@ export interface CopilotContextParams {
|
|
|
221
217
|
|
|
222
218
|
extensions: ExtensionsInput;
|
|
223
219
|
setExtensions: React.Dispatch<React.SetStateAction<ExtensionsInput>>;
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
220
|
+
interruptActions: Record<string, LangGraphInterruptRender>;
|
|
221
|
+
setInterruptAction: LangGraphInterruptActionSetter;
|
|
222
|
+
removeInterruptAction: (actionId: string) => void;
|
|
223
|
+
interruptEventQueue: Record<string, QueuedInterruptEvent[]>;
|
|
224
|
+
addInterruptEvent: (queuedEvent: QueuedInterruptEvent) => void;
|
|
225
|
+
removeInterruptEvent: (threadId: string, eventId: string) => void;
|
|
227
226
|
|
|
228
227
|
/**
|
|
229
228
|
* Optional trace handler for comprehensive debugging and observability.
|
|
@@ -246,9 +245,8 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
246
245
|
setAction: () => {},
|
|
247
246
|
removeAction: () => {},
|
|
248
247
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
removeCoAgentStateRender: () => {},
|
|
248
|
+
setRegisteredActions: () => "",
|
|
249
|
+
removeRegisteredAction: () => {},
|
|
252
250
|
|
|
253
251
|
chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
|
|
254
252
|
getContextString: (documents: DocumentPointer[], categories: string[]) =>
|
|
@@ -271,7 +269,6 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
271
269
|
getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),
|
|
272
270
|
addDocumentContext: () => returnAndThrowInDebug(""),
|
|
273
271
|
removeDocumentContext: () => {},
|
|
274
|
-
runtimeClient: {} as any,
|
|
275
272
|
|
|
276
273
|
copilotApiConfig: new (class implements CopilotApiConfig {
|
|
277
274
|
get chatApiEndpoint(): string {
|
|
@@ -306,9 +303,12 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
306
303
|
availableAgents: [],
|
|
307
304
|
extensions: {},
|
|
308
305
|
setExtensions: () => {},
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
306
|
+
interruptActions: {},
|
|
307
|
+
setInterruptAction: () => {},
|
|
308
|
+
removeInterruptAction: () => {},
|
|
309
|
+
interruptEventQueue: {},
|
|
310
|
+
addInterruptEvent: () => {},
|
|
311
|
+
removeInterruptEvent: () => {},
|
|
312
312
|
onError: () => {},
|
|
313
313
|
bannerError: null,
|
|
314
314
|
setBannerError: () => {},
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
import { Message } from "@copilotkit/runtime-client-gql";
|
|
6
6
|
import React from "react";
|
|
7
|
-
import {
|
|
7
|
+
import { Suggestion } from "@copilotkitnext/core";
|
|
8
8
|
|
|
9
9
|
export interface CopilotMessagesContextParams {
|
|
10
10
|
messages: Message[];
|
|
11
11
|
setMessages: React.Dispatch<React.SetStateAction<Message[]>>; // suggestions state
|
|
12
|
-
suggestions:
|
|
13
|
-
setSuggestions: React.Dispatch<React.SetStateAction<
|
|
12
|
+
suggestions: Suggestion[];
|
|
13
|
+
setSuggestions: React.Dispatch<React.SetStateAction<Suggestion[]>>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
const emptyCopilotContext: CopilotMessagesContextParams = {
|
package/src/context/index.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
export { CopilotContext, useCopilotContext } from "./copilot-context";
|
|
2
2
|
export { CopilotMessagesContext, useCopilotMessagesContext } from "./copilot-messages-context";
|
|
3
|
+
export {
|
|
4
|
+
CoAgentStateRendersContext,
|
|
5
|
+
CoAgentStateRendersProvider,
|
|
6
|
+
useCoAgentStateRenders,
|
|
7
|
+
} from "./coagent-state-renders-context";
|
|
8
|
+
export { ThreadsContext, ThreadsProvider, useThreads } from "./threads-context";
|
|
3
9
|
export type {
|
|
4
10
|
CopilotContextParams,
|
|
5
11
|
CoagentInChatRenderFunction,
|
|
6
12
|
CopilotApiConfig,
|
|
7
13
|
} from "./copilot-context";
|
|
8
14
|
export type { CopilotMessagesContextParams } from "./copilot-messages-context";
|
|
15
|
+
export type { CoAgentStateRendersContextValue } from "./coagent-state-renders-context";
|
|
16
|
+
export type { ThreadsContextValue, ThreadsProviderProps } from "./threads-context";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { createContext, useContext, useState, ReactNode, SetStateAction } from "react";
|
|
2
|
+
import { randomUUID } from "@copilotkit/shared";
|
|
3
|
+
|
|
4
|
+
export interface ThreadsContextValue {
|
|
5
|
+
threadId: string;
|
|
6
|
+
setThreadId: (value: SetStateAction<string>) => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ThreadsContext = createContext<ThreadsContextValue | undefined>(undefined);
|
|
10
|
+
|
|
11
|
+
export interface ThreadsProviderProps {
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
threadId?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function ThreadsProvider({ children, threadId: explicitThreadId }: ThreadsProviderProps) {
|
|
17
|
+
const [internalThreadId, setThreadId] = useState<string>(explicitThreadId ?? randomUUID());
|
|
18
|
+
|
|
19
|
+
const threadId = internalThreadId;
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<ThreadsContext.Provider
|
|
23
|
+
value={{
|
|
24
|
+
threadId,
|
|
25
|
+
setThreadId,
|
|
26
|
+
}}
|
|
27
|
+
>
|
|
28
|
+
{children}
|
|
29
|
+
</ThreadsContext.Provider>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function useThreads() {
|
|
34
|
+
const context = useContext(ThreadsContext);
|
|
35
|
+
if (!context) {
|
|
36
|
+
throw new Error("useThreads must be used within ThreadsProvider");
|
|
37
|
+
}
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { ThreadsContext };
|
package/src/hooks/index.ts
CHANGED
|
@@ -6,13 +6,12 @@ export {
|
|
|
6
6
|
type UseCopilotChatOptions_c,
|
|
7
7
|
useCopilotChatHeadless_c,
|
|
8
8
|
} from "./use-copilot-chat-headless_c";
|
|
9
|
-
export {
|
|
9
|
+
export { useCopilotChatInternal, type ChatSuggestions } from "./use-copilot-chat_internal";
|
|
10
10
|
export { useCopilotAction } from "./use-copilot-action";
|
|
11
11
|
export { useCoAgentStateRender } from "./use-coagent-state-render";
|
|
12
12
|
export { useMakeCopilotDocumentReadable } from "./use-make-copilot-document-readable";
|
|
13
|
-
export { type UseChatHelpers } from "./use-chat";
|
|
14
13
|
export { useCopilotReadable } from "./use-copilot-readable";
|
|
15
|
-
export { useCoAgent, type HintFunction
|
|
14
|
+
export { useCoAgent, type HintFunction } from "./use-coagent";
|
|
16
15
|
export { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
|
|
17
16
|
export { useCopilotAuthenticatedAction_c } from "./use-copilot-authenticated-action";
|
|
18
17
|
export { useLangGraphInterrupt } from "./use-langgraph-interrupt";
|
|
@@ -23,3 +22,8 @@ export { useFrontendTool } from "./use-frontend-tool";
|
|
|
23
22
|
export { useHumanInTheLoop } from "./use-human-in-the-loop";
|
|
24
23
|
export { useRenderToolCall } from "./use-render-tool-call";
|
|
25
24
|
export { useDefaultTool } from "./use-default-tool";
|
|
25
|
+
export { useLazyToolRenderer } from "./use-lazy-tool-renderer";
|
|
26
|
+
export {
|
|
27
|
+
useConfigureChatSuggestions,
|
|
28
|
+
type UseCopilotChatSuggestionsConfiguration,
|
|
29
|
+
} from "./use-configure-chat-suggestions";
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { ReactCustomMessageRendererPosition, useAgent } from "@copilotkitnext/react";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
3
|
+
import type { AgentSubscriber } from "@ag-ui/client";
|
|
4
|
+
import { useCoAgentStateRenders } from "../context";
|
|
5
|
+
import { dataToUUID, parseJson } from "@copilotkit/shared";
|
|
6
|
+
|
|
7
|
+
function getStateWithoutConstantKeys(state: any) {
|
|
8
|
+
const { messages, tools, copilotkit, ...stateWithoutConstantKeys } = state;
|
|
9
|
+
return stateWithoutConstantKeys;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Function that compares states, without the constant keys
|
|
13
|
+
function areStatesEquals(a: any, b: any) {
|
|
14
|
+
if ((a && !b) || (!a && b)) return false;
|
|
15
|
+
const { messages, tools, copilotkit, ...aWithoutConstantKeys } = a;
|
|
16
|
+
const {
|
|
17
|
+
messages: bMessages,
|
|
18
|
+
tools: bTools,
|
|
19
|
+
copilotkit: bCopilotkit,
|
|
20
|
+
...bWithoutConstantKeys
|
|
21
|
+
} = b;
|
|
22
|
+
|
|
23
|
+
return JSON.stringify(aWithoutConstantKeys) === JSON.stringify(bWithoutConstantKeys);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Bridge hook that connects agent state renders to chat messages.
|
|
28
|
+
*
|
|
29
|
+
* ## Purpose
|
|
30
|
+
* This hook finds matching state render configurations (registered via useCoAgentStateRender)
|
|
31
|
+
* and returns UI to render in chat.
|
|
32
|
+
* It ensures each state render appears bound to a specific message, preventing duplicates while
|
|
33
|
+
* allowing re-binding when the underlying state changes significantly.
|
|
34
|
+
*
|
|
35
|
+
* ## Message-ID-Based Claiming System
|
|
36
|
+
*
|
|
37
|
+
* ### The Problem
|
|
38
|
+
* Multiple bridge component instances render simultaneously (one per message). Without coordination,
|
|
39
|
+
* they would all try to render the same state render, causing duplicates.
|
|
40
|
+
*
|
|
41
|
+
* ### The Solution: Message-ID Claims with State Comparison
|
|
42
|
+
* Each state render is "claimed" by exactly one **message ID** (not runId):
|
|
43
|
+
*
|
|
44
|
+
* **Claim Structure**: `claimsRef.current[messageId] = { stateRenderId, runId, stateSnapshot, locked }`
|
|
45
|
+
*
|
|
46
|
+
* **Primary binding is by messageId because**:
|
|
47
|
+
* - runId is not always available immediately (starts as "pending")
|
|
48
|
+
* - messageId is the stable identifier throughout the message lifecycle
|
|
49
|
+
* - Claims persist across component remounts via context ref
|
|
50
|
+
*
|
|
51
|
+
* ### Claiming Logic Flow
|
|
52
|
+
*
|
|
53
|
+
* 1. **Message already has a claim**:
|
|
54
|
+
* - Check if the claim matches the current stateRenderId
|
|
55
|
+
* - If yes → render (this message owns this render)
|
|
56
|
+
* - Update runId if it was "pending" and now available
|
|
57
|
+
*
|
|
58
|
+
* 2. **State render claimed by another message**:
|
|
59
|
+
* - Compare state snapshots (ignoring constant keys: messages, tools, copilotkit)
|
|
60
|
+
* - If states are identical → block rendering (duplicate)
|
|
61
|
+
* - **If states are different → allow claiming** (new data, new message)
|
|
62
|
+
* - This handles cases where the same render type shows different states in different messages
|
|
63
|
+
*
|
|
64
|
+
* 3. **Unclaimed state render**:
|
|
65
|
+
* - Only allow claiming if runId is "pending" (initial render)
|
|
66
|
+
* - If runId is real but no claim exists → block (edge case protection)
|
|
67
|
+
* - Create new claim: `claimsRef.current[messageId] = { stateRenderId, runId }`
|
|
68
|
+
*
|
|
69
|
+
* ### State Snapshot Locking
|
|
70
|
+
*
|
|
71
|
+
* Once a state snapshot is captured and locked for a message:
|
|
72
|
+
* - The UI always renders with the locked snapshot (not live agent.state)
|
|
73
|
+
* - Prevents UI from appearing "wiped" during state transitions
|
|
74
|
+
* - Locked when: stateSnapshot prop is available (from message persistence)
|
|
75
|
+
* - Unlocked state: can still update from live agent.state
|
|
76
|
+
*
|
|
77
|
+
* ### Synchronous Claiming (Ref-based)
|
|
78
|
+
*
|
|
79
|
+
* Claims are stored in a context-level ref (not React state):
|
|
80
|
+
* - Multiple bridges render in the same tick
|
|
81
|
+
* - State updates are async - would allow duplicates before update completes
|
|
82
|
+
* - Ref provides immediate, synchronous claim checking
|
|
83
|
+
* - Survives component remounts (stored in context, not component)
|
|
84
|
+
*
|
|
85
|
+
* ## Flow Example
|
|
86
|
+
*
|
|
87
|
+
* ```
|
|
88
|
+
* Time 1: Message A renders, runId=undefined, state={progress: 50%}
|
|
89
|
+
* → effectiveRunId = "pending"
|
|
90
|
+
* → Claims: claimsRef["msgA"] = { stateRenderId: "tasks", runId: "pending", stateSnapshot: {progress: 50%} }
|
|
91
|
+
* → Renders UI with 50% progress
|
|
92
|
+
*
|
|
93
|
+
* Time 2: Message B renders, runId=undefined, same state
|
|
94
|
+
* → Checks: "tasks" already claimed by msgA with same state
|
|
95
|
+
* → Returns null (blocked - duplicate)
|
|
96
|
+
*
|
|
97
|
+
* Time 3: Real runId appears (e.g., "run-123")
|
|
98
|
+
* → Updates claim: claimsRef["msgA"].runId = "run-123"
|
|
99
|
+
* → Message A continues rendering
|
|
100
|
+
*
|
|
101
|
+
* Time 4: Agent processes more, state={progress: 100%}
|
|
102
|
+
* → Message A: locked to 50% (stateSnapshot locked)
|
|
103
|
+
* → Message C renders with state={progress: 100%}
|
|
104
|
+
* → Checks: "tasks" claimed by msgA but state is DIFFERENT (50% vs 100%)
|
|
105
|
+
* → Allows new claim: claimsRef["msgC"] = { stateRenderId: "tasks", runId: "run-123", stateSnapshot: {progress: 100%} }
|
|
106
|
+
* → Both messages render independently with their own snapshots
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export interface CoAgentStateRenderBridgeProps {
|
|
110
|
+
message: any;
|
|
111
|
+
position: ReactCustomMessageRendererPosition;
|
|
112
|
+
runId: string;
|
|
113
|
+
messageIndex: number;
|
|
114
|
+
messageIndexInRun: number;
|
|
115
|
+
numberOfMessagesInRun: number;
|
|
116
|
+
agentId: string;
|
|
117
|
+
stateSnapshot: any;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function useCoagentStateRenderBridge(agentId: string, props: CoAgentStateRenderBridgeProps) {
|
|
121
|
+
const { stateSnapshot, messageIndexInRun, message } = props;
|
|
122
|
+
const { coAgentStateRenders, claimsRef } = useCoAgentStateRenders();
|
|
123
|
+
const { agent } = useAgent({ agentId });
|
|
124
|
+
const [nodeName, setNodeName] = useState<string | undefined>(undefined);
|
|
125
|
+
|
|
126
|
+
const runId = props.runId ?? message.runId;
|
|
127
|
+
const effectiveRunId = runId || "pending";
|
|
128
|
+
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
if (!agent) return;
|
|
131
|
+
const subscriber: AgentSubscriber = {
|
|
132
|
+
onStepStartedEvent: ({ event }) => {
|
|
133
|
+
if (event.stepName !== nodeName) {
|
|
134
|
+
setNodeName(event.stepName);
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
onStepFinishedEvent: ({ event }) => {
|
|
138
|
+
if (event.stepName === nodeName) {
|
|
139
|
+
setNodeName(undefined);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const { unsubscribe } = agent.subscribe(subscriber);
|
|
145
|
+
return () => {
|
|
146
|
+
unsubscribe();
|
|
147
|
+
};
|
|
148
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
149
|
+
}, [agentId, nodeName]);
|
|
150
|
+
|
|
151
|
+
if (messageIndexInRun !== 0) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const getStateRender = useCallback(
|
|
156
|
+
(messageId: string) => {
|
|
157
|
+
return Object.entries(coAgentStateRenders).find(([stateRenderId, stateRender]) => {
|
|
158
|
+
if (claimsRef.current[messageId]) {
|
|
159
|
+
return stateRenderId === claimsRef.current[messageId].stateRenderId;
|
|
160
|
+
}
|
|
161
|
+
const matchingAgentName = stateRender.name === agentId;
|
|
162
|
+
const matchesNodeContext = stateRender.nodeName
|
|
163
|
+
? stateRender.nodeName === nodeName
|
|
164
|
+
: true;
|
|
165
|
+
return matchingAgentName && matchesNodeContext;
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
[coAgentStateRenders, nodeName, agentId],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// Message ID-based claim system - A state render can only be claimed by one message ID
|
|
172
|
+
const handleRenderRequest = ({
|
|
173
|
+
stateRenderId,
|
|
174
|
+
messageId,
|
|
175
|
+
runId,
|
|
176
|
+
stateSnapshot: renderSnapshot,
|
|
177
|
+
}: {
|
|
178
|
+
stateRenderId: string;
|
|
179
|
+
messageId: string;
|
|
180
|
+
runId?: string;
|
|
181
|
+
stateSnapshot?: any;
|
|
182
|
+
}): boolean => {
|
|
183
|
+
// Check if this message has already claimed this state render
|
|
184
|
+
if (claimsRef.current[messageId]) {
|
|
185
|
+
const canRender = claimsRef.current[messageId].stateRenderId === stateRenderId;
|
|
186
|
+
|
|
187
|
+
// Update runId if it doesn't exist
|
|
188
|
+
if (
|
|
189
|
+
canRender &&
|
|
190
|
+
runId &&
|
|
191
|
+
(!claimsRef.current[messageId].runId || claimsRef.current[messageId].runId === "pending")
|
|
192
|
+
) {
|
|
193
|
+
claimsRef.current[messageId].runId = runId;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return canRender;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Do not allow render if any other message has claimed this state render
|
|
200
|
+
const renderClaimedByOtherMessage = Object.values(claimsRef.current).find(
|
|
201
|
+
(c) =>
|
|
202
|
+
c.stateRenderId === stateRenderId &&
|
|
203
|
+
dataToUUID(JSON.stringify(getStateWithoutConstantKeys(c.stateSnapshot))) ===
|
|
204
|
+
dataToUUID(JSON.stringify(getStateWithoutConstantKeys(renderSnapshot))),
|
|
205
|
+
);
|
|
206
|
+
if (renderClaimedByOtherMessage) {
|
|
207
|
+
// If:
|
|
208
|
+
// - state render already claimed
|
|
209
|
+
// - snapshot exists in the claiming object and is different from current,
|
|
210
|
+
if (
|
|
211
|
+
renderSnapshot &&
|
|
212
|
+
renderClaimedByOtherMessage.stateSnapshot &&
|
|
213
|
+
!areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, renderSnapshot)
|
|
214
|
+
) {
|
|
215
|
+
claimsRef.current[messageId] = { stateRenderId, runId };
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// No existing claim anywhere yet – allow this message to claim even if we already know the runId.
|
|
222
|
+
if (!runId) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
claimsRef.current[messageId] = { stateRenderId, runId };
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return useMemo(() => {
|
|
231
|
+
const [stateRenderId, stateRender] = getStateRender(message.id) ?? [];
|
|
232
|
+
|
|
233
|
+
if (!stateRender || !stateRenderId) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Is there any state we can use?
|
|
238
|
+
const snapshot = stateSnapshot ? parseJson(stateSnapshot, stateSnapshot) : agent?.state;
|
|
239
|
+
|
|
240
|
+
// Synchronously check/claim - returns true if this message can render
|
|
241
|
+
const canRender = handleRenderRequest({
|
|
242
|
+
stateRenderId,
|
|
243
|
+
messageId: message.id,
|
|
244
|
+
runId: effectiveRunId,
|
|
245
|
+
stateSnapshot: snapshot,
|
|
246
|
+
});
|
|
247
|
+
if (!canRender) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// If we found state, and given that now there's a claim for the current message, let's save it in the claim
|
|
252
|
+
if (snapshot && !claimsRef.current[message.id].locked) {
|
|
253
|
+
if (stateSnapshot) {
|
|
254
|
+
claimsRef.current[message.id].stateSnapshot = snapshot;
|
|
255
|
+
claimsRef.current[message.id].locked = true;
|
|
256
|
+
} else {
|
|
257
|
+
claimsRef.current[message.id].stateSnapshot = snapshot;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (stateRender.handler) {
|
|
262
|
+
stateRender.handler({
|
|
263
|
+
state: stateSnapshot ? parseJson(stateSnapshot, stateSnapshot) : (agent?.state ?? {}),
|
|
264
|
+
nodeName: nodeName ?? "",
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (stateRender.render) {
|
|
269
|
+
const status = agent?.isRunning ? "inProgress" : "complete";
|
|
270
|
+
|
|
271
|
+
if (typeof stateRender.render === "string") return stateRender.render;
|
|
272
|
+
|
|
273
|
+
return stateRender.render({
|
|
274
|
+
status,
|
|
275
|
+
// Always use state from claim, to make sure the state does not seem "wiped" for a fraction of a second
|
|
276
|
+
state: claimsRef.current[message.id].stateSnapshot ?? {},
|
|
277
|
+
nodeName: nodeName ?? "",
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}, [
|
|
281
|
+
getStateRender,
|
|
282
|
+
stateSnapshot,
|
|
283
|
+
agent?.state,
|
|
284
|
+
agent?.isRunning,
|
|
285
|
+
nodeName,
|
|
286
|
+
effectiveRunId,
|
|
287
|
+
message.id,
|
|
288
|
+
]);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function CoAgentStateRenderBridge(props: CoAgentStateRenderBridgeProps) {
|
|
292
|
+
return useCoagentStateRenderBridge(props.agentId, props);
|
|
293
|
+
}
|
|
@@ -46,6 +46,7 @@ import { CopilotContext } from "../context/copilot-context";
|
|
|
46
46
|
import { randomId, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
|
|
47
47
|
import { CoAgentStateRender } from "../types/coagent-action";
|
|
48
48
|
import { useToast } from "../components/toast/toast-provider";
|
|
49
|
+
import { useCoAgentStateRenders } from "../context/coagent-state-renders-context";
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
52
|
* This hook is used to render agent state with custom UI components or text. This is particularly
|
|
@@ -62,13 +63,9 @@ export function useCoAgentStateRender<T = any>(
|
|
|
62
63
|
action: CoAgentStateRender<T>,
|
|
63
64
|
dependencies?: any[],
|
|
64
65
|
): void {
|
|
65
|
-
const {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
coAgentStateRenders,
|
|
69
|
-
chatComponentsCache,
|
|
70
|
-
availableAgents,
|
|
71
|
-
} = useContext(CopilotContext);
|
|
66
|
+
const { chatComponentsCache, availableAgents } = useContext(CopilotContext);
|
|
67
|
+
const { setCoAgentStateRender, removeCoAgentStateRender, coAgentStateRenders } =
|
|
68
|
+
useCoAgentStateRenders();
|
|
72
69
|
const idRef = useRef<string>(randomId());
|
|
73
70
|
const { setBannerError, addToast } = useToast();
|
|
74
71
|
|