@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
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
*/
|
|
64
64
|
import { useEffect, useRef } from "react";
|
|
65
65
|
import { useCopilotContext } from "../context/copilot-context";
|
|
66
|
+
import { useAgentContext } from "@copilotkitnext/react";
|
|
66
67
|
|
|
67
68
|
/**
|
|
68
69
|
* Options for the useCopilotReadable hook.
|
|
@@ -106,32 +107,11 @@ function convertToJSON(description: string, value: any): string {
|
|
|
106
107
|
* Adds the given information to the Copilot context to make it readable by Copilot.
|
|
107
108
|
*/
|
|
108
109
|
export function useCopilotReadable(
|
|
109
|
-
{
|
|
110
|
+
{ description, value }: UseCopilotReadableOptions,
|
|
111
|
+
dependencies?: any[],
|
|
112
|
+
): void {
|
|
113
|
+
useAgentContext({
|
|
110
114
|
description,
|
|
111
115
|
value,
|
|
112
|
-
|
|
113
|
-
categories,
|
|
114
|
-
convert,
|
|
115
|
-
available = "enabled",
|
|
116
|
-
}: UseCopilotReadableOptions,
|
|
117
|
-
dependencies?: any[],
|
|
118
|
-
): string | undefined {
|
|
119
|
-
const { addContext, removeContext } = useCopilotContext();
|
|
120
|
-
const idRef = useRef<string>();
|
|
121
|
-
convert = convert || convertToJSON;
|
|
122
|
-
|
|
123
|
-
const information = convert(description, value);
|
|
124
|
-
|
|
125
|
-
useEffect(() => {
|
|
126
|
-
if (available === "disabled") return;
|
|
127
|
-
|
|
128
|
-
const id = addContext(information, parentId, categories);
|
|
129
|
-
idRef.current = id;
|
|
130
|
-
|
|
131
|
-
return () => {
|
|
132
|
-
removeContext(id);
|
|
133
|
-
};
|
|
134
|
-
}, [available, information, parentId, addContext, removeContext, ...(dependencies || [])]);
|
|
135
|
-
|
|
136
|
-
return idRef.current;
|
|
116
|
+
});
|
|
137
117
|
}
|
|
@@ -16,9 +16,10 @@ interface FlatCategoryStoreElement<T> {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {
|
|
19
|
-
const [elements, dispatch] = useReducer
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const [elements, dispatch] = useReducer(
|
|
20
|
+
flatCategoryStoreReducer<T>,
|
|
21
|
+
new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>(),
|
|
22
|
+
);
|
|
22
23
|
|
|
23
24
|
const addElement = useCallback((value: T, categories: string[]): FlatCategoryStoreId => {
|
|
24
25
|
const newId = randomId();
|
|
@@ -1,18 +1,77 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { FrontendAction } from "../types/frontend-action";
|
|
3
|
-
import { Parameter } from "@copilotkit/shared";
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ActionRenderProps, FrontendAction } from "../types/frontend-action";
|
|
3
|
+
import { Parameter, getZodParameters, MappedParameterTypes } from "@copilotkit/shared";
|
|
4
|
+
import { parseJson } from "@copilotkit/shared";
|
|
5
|
+
import { ToolCallStatus } from "@copilotkitnext/core";
|
|
6
|
+
import { type ReactFrontendTool, useFrontendTool as useFrontendToolVNext } from "@copilotkitnext/react";
|
|
4
7
|
|
|
5
|
-
type
|
|
8
|
+
type FrontendToolOptions<T extends Parameter[] | []> = ReactFrontendTool<MappedParameterTypes<T>>;
|
|
9
|
+
type FrontendToolRenderArgs<T extends Parameter[] | []> =
|
|
10
|
+
| {
|
|
11
|
+
name: string;
|
|
12
|
+
args: Partial<MappedParameterTypes<T>>;
|
|
13
|
+
status: ToolCallStatus.InProgress;
|
|
14
|
+
result: undefined;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
name: string;
|
|
18
|
+
args: MappedParameterTypes<T>;
|
|
19
|
+
status: ToolCallStatus.Executing;
|
|
20
|
+
result: undefined;
|
|
21
|
+
}
|
|
22
|
+
| {
|
|
23
|
+
name: string;
|
|
24
|
+
args: MappedParameterTypes<T>;
|
|
25
|
+
status: ToolCallStatus.Complete;
|
|
26
|
+
result: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type UseFrontendToolArgs<T extends Parameter[] | [] = []> = {
|
|
6
30
|
available?: "disabled" | "enabled";
|
|
7
31
|
} & Pick<
|
|
8
32
|
FrontendAction<T>,
|
|
9
33
|
"name" | "description" | "parameters" | "handler" | "followUp" | "render"
|
|
10
34
|
>;
|
|
11
35
|
|
|
12
|
-
export function useFrontendTool<const T extends Parameter[]
|
|
13
|
-
tool:
|
|
36
|
+
export function useFrontendTool<const T extends Parameter[] = []>(
|
|
37
|
+
tool: UseFrontendToolArgs<T>,
|
|
14
38
|
dependencies?: any[],
|
|
15
39
|
) {
|
|
16
|
-
|
|
17
|
-
|
|
40
|
+
const { name, description, parameters, render, followUp } = tool;
|
|
41
|
+
const zodParameters = getZodParameters(parameters);
|
|
42
|
+
|
|
43
|
+
const normalizedRender: FrontendToolOptions<T>["render"] | undefined = (() => {
|
|
44
|
+
if (typeof render === "undefined") {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (typeof render === "string") {
|
|
49
|
+
const staticRender = render;
|
|
50
|
+
return (() => React.createElement(React.Fragment, null, staticRender)) as FrontendToolOptions<T>["render"];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return ((args: FrontendToolRenderArgs<T>) => {
|
|
54
|
+
const renderArgs = {
|
|
55
|
+
...args,
|
|
56
|
+
result: typeof args.result === "string" ? parseJson(args.result, args.result) : args.result,
|
|
57
|
+
} as ActionRenderProps<T>;
|
|
58
|
+
|
|
59
|
+
const rendered = render(renderArgs);
|
|
60
|
+
|
|
61
|
+
if (typeof rendered === "string") {
|
|
62
|
+
return React.createElement(React.Fragment, null, rendered);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return rendered ?? null;
|
|
66
|
+
}) as FrontendToolOptions<T>["render"];
|
|
67
|
+
})();
|
|
68
|
+
|
|
69
|
+
useFrontendToolVNext<MappedParameterTypes<T>>({
|
|
70
|
+
name,
|
|
71
|
+
description,
|
|
72
|
+
parameters: zodParameters,
|
|
73
|
+
handler: tool.handler,
|
|
74
|
+
followUp,
|
|
75
|
+
render: normalizedRender,
|
|
76
|
+
});
|
|
18
77
|
}
|
|
@@ -1,24 +1,122 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { ActionRenderProps, ActionRenderPropsWait, FrontendAction } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
CopilotKitError,
|
|
4
|
+
CopilotKitErrorCode,
|
|
5
|
+
MappedParameterTypes,
|
|
6
|
+
Parameter,
|
|
7
|
+
getZodParameters,
|
|
8
|
+
parseJson,
|
|
9
|
+
} from "@copilotkit/shared";
|
|
10
|
+
import { useHumanInTheLoop as useHumanInTheLoopVNext } from "@copilotkitnext/react";
|
|
11
|
+
import { ToolCallStatus } from "@copilotkitnext/core";
|
|
12
|
+
import React, { ComponentType, FunctionComponent, useEffect, useRef } from "react";
|
|
4
13
|
|
|
5
|
-
type
|
|
14
|
+
type HumanInTheLoopOptions = Parameters<typeof useHumanInTheLoopVNext>[0];
|
|
15
|
+
type HumanInTheLoopRender = HumanInTheLoopOptions["render"];
|
|
16
|
+
type HumanInTheLoopRenderArgs = HumanInTheLoopRender extends (props: infer P) => any ? P : never;
|
|
17
|
+
|
|
18
|
+
export type UseHumanInTheLoopArgs<T extends Parameter[] | [] = []> = {
|
|
6
19
|
available?: "disabled" | "enabled";
|
|
7
20
|
render: FrontendAction<T>["renderAndWaitForResponse"];
|
|
21
|
+
followUp?: FrontendAction<T>["followUp"];
|
|
8
22
|
} & Pick<FrontendAction<T>, "name" | "description" | "parameters">;
|
|
9
23
|
|
|
24
|
+
type HitlRendererArgs =
|
|
25
|
+
| {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
args: Partial<Record<string, unknown>>;
|
|
29
|
+
status: ToolCallStatus.InProgress;
|
|
30
|
+
result: undefined;
|
|
31
|
+
respond: undefined;
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
name: string;
|
|
35
|
+
description: string;
|
|
36
|
+
args: Record<string, unknown>;
|
|
37
|
+
status: ToolCallStatus.Executing;
|
|
38
|
+
result: undefined;
|
|
39
|
+
respond: (result: unknown) => Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
name: string;
|
|
43
|
+
description: string;
|
|
44
|
+
args: Record<string, unknown>;
|
|
45
|
+
status: ToolCallStatus.Complete;
|
|
46
|
+
result: string;
|
|
47
|
+
respond: undefined;
|
|
48
|
+
};
|
|
49
|
+
type HitlRenderer = FunctionComponent<HitlRendererArgs>;
|
|
50
|
+
|
|
10
51
|
export function useHumanInTheLoop<const T extends Parameter[] | [] = []>(
|
|
11
|
-
tool:
|
|
52
|
+
tool: UseHumanInTheLoopArgs<T>,
|
|
12
53
|
dependencies?: any[],
|
|
13
54
|
) {
|
|
14
55
|
const { render, ...toolRest } = tool;
|
|
56
|
+
const { name, description, parameters, followUp } = toolRest;
|
|
57
|
+
const zodParameters = getZodParameters(parameters);
|
|
58
|
+
const renderRef = useRef<HitlRenderer | null>(null);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
renderRef.current = (args: HitlRendererArgs): React.ReactElement | null => {
|
|
62
|
+
if (typeof render === "string") {
|
|
63
|
+
return React.createElement(React.Fragment, null, render);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!render) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const renderProps: ActionRenderPropsWait<T> = (() => {
|
|
71
|
+
const mappedArgs = args.args as unknown as MappedParameterTypes<T>;
|
|
72
|
+
|
|
73
|
+
switch (args.status) {
|
|
74
|
+
case ToolCallStatus.InProgress:
|
|
75
|
+
return {
|
|
76
|
+
args: mappedArgs,
|
|
77
|
+
respond: args.respond,
|
|
78
|
+
status: args.status,
|
|
79
|
+
handler: undefined,
|
|
80
|
+
};
|
|
81
|
+
case ToolCallStatus.Executing:
|
|
82
|
+
return {
|
|
83
|
+
args: mappedArgs,
|
|
84
|
+
respond: args.respond,
|
|
85
|
+
status: args.status,
|
|
86
|
+
handler: () => {},
|
|
87
|
+
};
|
|
88
|
+
case ToolCallStatus.Complete:
|
|
89
|
+
return {
|
|
90
|
+
args: mappedArgs,
|
|
91
|
+
respond: args.respond,
|
|
92
|
+
status: args.status,
|
|
93
|
+
result: args.result ? parseJson(args.result, args.result) : args.result,
|
|
94
|
+
handler: undefined,
|
|
95
|
+
};
|
|
96
|
+
default:
|
|
97
|
+
throw new CopilotKitError({
|
|
98
|
+
code: CopilotKitErrorCode.UNKNOWN,
|
|
99
|
+
message: `Invalid tool call status: ${(args as unknown as { status: string }).status}`,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
})();
|
|
103
|
+
|
|
104
|
+
const rendered = render(renderProps);
|
|
105
|
+
|
|
106
|
+
if (typeof rendered === "string") {
|
|
107
|
+
return React.createElement(React.Fragment, null, rendered);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return rendered ?? null;
|
|
111
|
+
};
|
|
112
|
+
}, [render, ...(dependencies ?? [])]);
|
|
15
113
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
);
|
|
114
|
+
useHumanInTheLoopVNext({
|
|
115
|
+
name,
|
|
116
|
+
description,
|
|
117
|
+
followUp,
|
|
118
|
+
parameters: zodParameters,
|
|
119
|
+
render: ((args: HumanInTheLoopRenderArgs) =>
|
|
120
|
+
renderRef.current?.(args as HitlRendererArgs) ?? null) as HumanInTheLoopOptions["render"],
|
|
121
|
+
});
|
|
24
122
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useCopilotContext } from "../context";
|
|
2
|
-
import React, { useCallback } from "react";
|
|
3
|
-
import {
|
|
2
|
+
import React, { useCallback, useEffect, useMemo } from "react";
|
|
3
|
+
import type { AbstractAgent, AgentSubscriber } from "@ag-ui/client";
|
|
4
|
+
import { MetaEventName } from "@copilotkit/runtime-client-gql";
|
|
5
|
+
import { dataToUUID, parseJson } from "@copilotkit/shared";
|
|
4
6
|
|
|
5
7
|
type InterruptProps = {
|
|
6
8
|
event: any;
|
|
@@ -17,52 +19,100 @@ const InterruptRenderer: React.FC<InterruptProps> = ({ event, result, render, re
|
|
|
17
19
|
return render({ event, result, resolve });
|
|
18
20
|
};
|
|
19
21
|
|
|
20
|
-
export function useLangGraphInterruptRender(
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
export function useLangGraphInterruptRender(
|
|
23
|
+
agent: AbstractAgent,
|
|
24
|
+
): string | React.ReactElement | null {
|
|
25
|
+
const {
|
|
26
|
+
interruptActions,
|
|
27
|
+
agentSession,
|
|
28
|
+
threadId,
|
|
29
|
+
interruptEventQueue,
|
|
30
|
+
addInterruptEvent,
|
|
31
|
+
removeInterruptEvent,
|
|
32
|
+
} = useCopilotContext();
|
|
23
33
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (!agent) return;
|
|
36
|
+
const subscriber: AgentSubscriber = {
|
|
37
|
+
onCustomEvent: ({ event }) => {
|
|
38
|
+
if (event.name === "on_interrupt") {
|
|
39
|
+
const eventData = {
|
|
40
|
+
name: MetaEventName.LangGraphInterruptEvent,
|
|
41
|
+
type: event.type,
|
|
42
|
+
value: parseJson(event.value, event.value),
|
|
43
|
+
};
|
|
44
|
+
const eventId = dataToUUID(JSON.stringify(eventData), "interruptEvents");
|
|
45
|
+
addInterruptEvent({
|
|
46
|
+
eventId,
|
|
47
|
+
threadId,
|
|
48
|
+
event: eventData,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const { unsubscribe } = agent.subscribe(subscriber);
|
|
55
|
+
return () => {
|
|
56
|
+
unsubscribe();
|
|
57
|
+
};
|
|
58
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
+
}, [agent, threadId]);
|
|
60
|
+
|
|
61
|
+
const handleResolve = useCallback(
|
|
62
|
+
(eventId: string, response?: string) => {
|
|
63
|
+
agent?.runAgent({
|
|
64
|
+
forwardedProps: {
|
|
65
|
+
command: {
|
|
66
|
+
resume: response,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
removeInterruptEvent(threadId, eventId);
|
|
32
71
|
},
|
|
33
|
-
|
|
72
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
73
|
+
[agent, threadId],
|
|
34
74
|
);
|
|
35
75
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
!
|
|
40
|
-
|
|
41
|
-
return null;
|
|
76
|
+
return useMemo(() => {
|
|
77
|
+
// Get the queue for this thread and find the first unresponded event
|
|
78
|
+
const eventQueue = interruptEventQueue[threadId] || [];
|
|
79
|
+
const currentQueuedEvent = eventQueue.find((qe) => !qe.event.response);
|
|
80
|
+
|
|
81
|
+
if (!currentQueuedEvent) return null;
|
|
82
|
+
|
|
83
|
+
// Find the first matching action from all registered actions
|
|
84
|
+
const allActions = Object.values(interruptActions);
|
|
85
|
+
const matchingAction = allActions.find((action) => {
|
|
86
|
+
if (!action.enabled) return true; // No filter = match all
|
|
87
|
+
return action.enabled({
|
|
88
|
+
eventValue: currentQueuedEvent.event.value,
|
|
89
|
+
agentMetadata: agentSession,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (!matchingAction) return null;
|
|
42
94
|
|
|
43
|
-
|
|
95
|
+
const { render, handler } = matchingAction;
|
|
44
96
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
: enabled({ eventValue: event.value, agentMetadata: agentSession });
|
|
97
|
+
const resolveInterrupt = (response: string) => {
|
|
98
|
+
handleResolve(currentQueuedEvent.eventId, response);
|
|
99
|
+
};
|
|
49
100
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
101
|
+
let result = null;
|
|
102
|
+
if (handler) {
|
|
103
|
+
result = handler({
|
|
104
|
+
event: currentQueuedEvent.event,
|
|
105
|
+
resolve: resolveInterrupt,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
53
108
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
event,
|
|
109
|
+
if (!render) return null;
|
|
110
|
+
|
|
111
|
+
return React.createElement(InterruptRenderer, {
|
|
112
|
+
event: currentQueuedEvent.event,
|
|
113
|
+
result,
|
|
114
|
+
render,
|
|
58
115
|
resolve: resolveInterrupt,
|
|
59
116
|
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return React.createElement(InterruptRenderer, {
|
|
63
|
-
event,
|
|
64
|
-
result,
|
|
65
|
-
render,
|
|
66
|
-
resolve: resolveInterrupt,
|
|
67
|
-
});
|
|
117
|
+
}, [interruptActions, interruptEventQueue, threadId, agentSession, handleResolve]);
|
|
68
118
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useContext, useEffect, useMemo } from "react";
|
|
2
2
|
import { CopilotContext } from "../context/copilot-context";
|
|
3
3
|
import { LangGraphInterruptRender } from "../types/interrupt-action";
|
|
4
|
-
import {
|
|
4
|
+
import { useCopilotChatInternal } from "./use-copilot-chat_internal";
|
|
5
5
|
import { useToast } from "../components/toast/toast-provider";
|
|
6
6
|
import { dataToUUID } from "@copilotkit/shared";
|
|
7
7
|
|
|
@@ -9,58 +9,29 @@ export function useLangGraphInterrupt<TEventValue = any>(
|
|
|
9
9
|
action: Omit<LangGraphInterruptRender<TEventValue>, "id">,
|
|
10
10
|
dependencies?: any[],
|
|
11
11
|
) {
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
-
removeLangGraphInterruptAction,
|
|
15
|
-
langGraphInterruptAction,
|
|
16
|
-
threadId,
|
|
17
|
-
} = useContext(CopilotContext);
|
|
18
|
-
const { runChatCompletion } = useCopilotChat();
|
|
12
|
+
const { setInterruptAction, removeInterruptAction, interruptActions, threadId } =
|
|
13
|
+
useContext(CopilotContext);
|
|
19
14
|
const { addToast } = useToast();
|
|
20
15
|
|
|
21
16
|
const actionId = dataToUUID(JSON.stringify(action), "lgAction");
|
|
22
|
-
// We only consider action to be defined once the ID is there
|
|
23
|
-
const hasAction = useMemo(
|
|
24
|
-
() => Boolean(langGraphInterruptAction?.id),
|
|
25
|
-
[langGraphInterruptAction],
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
const isCurrentAction = useMemo(
|
|
29
|
-
() => langGraphInterruptAction?.id && langGraphInterruptAction?.id === actionId,
|
|
30
|
-
[langGraphInterruptAction],
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// Run chat completion to submit a response event. Only if it's the current action
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
if (hasAction && isCurrentAction && langGraphInterruptAction?.event?.response) {
|
|
36
|
-
runChatCompletion();
|
|
37
|
-
}
|
|
38
|
-
}, [langGraphInterruptAction?.event?.response, runChatCompletion, hasAction, isCurrentAction]);
|
|
39
17
|
|
|
40
18
|
useEffect(() => {
|
|
41
19
|
if (!action) return;
|
|
42
|
-
// An action was already set, with no conditions and it's not the action we're using right now.
|
|
43
|
-
// Show a warning, as this action will not be executed
|
|
44
|
-
if (hasAction && !isCurrentAction && !action.enabled) {
|
|
45
|
-
addToast({
|
|
46
|
-
type: "warning",
|
|
47
|
-
message: "An action is already registered for the interrupt event",
|
|
48
|
-
});
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
20
|
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
// if (!action.enabled) {
|
|
22
|
+
// TODO: if there are any other actions registered, we need to warn the user that a current action without "enabled" might render for everything
|
|
23
|
+
// addToast({
|
|
24
|
+
// type: "warning",
|
|
25
|
+
// message: "An action is already registered for the interrupt event",
|
|
26
|
+
// });
|
|
27
|
+
// return;
|
|
28
|
+
// }
|
|
29
|
+
|
|
30
|
+
setInterruptAction(threadId, { ...action, id: actionId });
|
|
55
31
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
setLangGraphInterruptAction,
|
|
62
|
-
removeLangGraphInterruptAction,
|
|
63
|
-
threadId,
|
|
64
|
-
...(dependencies || []),
|
|
65
|
-
]);
|
|
32
|
+
// Cleanup: remove action on unmount
|
|
33
|
+
return () => {
|
|
34
|
+
removeInterruptAction(actionId);
|
|
35
|
+
};
|
|
36
|
+
}, [setInterruptAction, removeInterruptAction, threadId, actionId, ...(dependencies || [])]);
|
|
66
37
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useRenderToolCall } from "@copilotkitnext/react";
|
|
2
|
+
import { AIMessage, Message, ToolResult } from "@copilotkit/shared";
|
|
3
|
+
import React, { useCallback } from "react";
|
|
4
|
+
|
|
5
|
+
export function useLazyToolRenderer(): (
|
|
6
|
+
message?: AIMessage,
|
|
7
|
+
messages?: Message[],
|
|
8
|
+
) => null | (() => ReturnType<ReturnType<typeof useRenderToolCall>> | null) {
|
|
9
|
+
const renderToolCall = useRenderToolCall();
|
|
10
|
+
|
|
11
|
+
return useCallback(
|
|
12
|
+
(message?: AIMessage, messages?: Message[]) => {
|
|
13
|
+
if (!message?.toolCalls?.length) return null;
|
|
14
|
+
|
|
15
|
+
const toolCall = message.toolCalls[0];
|
|
16
|
+
if (!toolCall) return null;
|
|
17
|
+
|
|
18
|
+
const toolMessage = messages?.find(
|
|
19
|
+
(m) => m.role === "tool" && m.toolCallId === toolCall.id,
|
|
20
|
+
) as ToolResult;
|
|
21
|
+
|
|
22
|
+
return () =>
|
|
23
|
+
renderToolCall({
|
|
24
|
+
toolCall,
|
|
25
|
+
toolMessage,
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
[renderToolCall],
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -15,7 +15,7 @@ export function useMakeCopilotDocumentReadable(
|
|
|
15
15
|
dependencies: any[] = [],
|
|
16
16
|
): string | undefined {
|
|
17
17
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
18
|
-
const idRef = useRef<string>();
|
|
18
|
+
const idRef = useRef<string>(undefined!);
|
|
19
19
|
|
|
20
20
|
useEffect(() => {
|
|
21
21
|
const id = addDocumentContext(document, categories);
|
|
@@ -1,20 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
ActionRenderProps,
|
|
3
|
+
ActionRenderPropsNoArgs,
|
|
4
|
+
ActionRenderPropsWait,
|
|
5
|
+
FrontendAction,
|
|
6
|
+
} from "../types";
|
|
7
|
+
import { Parameter, getZodParameters } from "@copilotkit/shared";
|
|
8
|
+
import React, { useEffect, useRef } from "react";
|
|
9
|
+
import { defineToolCallRenderer, useCopilotKit } from "@copilotkitnext/react";
|
|
10
|
+
import { parseJson } from "@copilotkit/shared";
|
|
4
11
|
|
|
5
|
-
type
|
|
12
|
+
type ToolCallRendererDefinition = Parameters<typeof defineToolCallRenderer>[0];
|
|
13
|
+
|
|
14
|
+
export type UseRenderToolCallArgs<T extends Parameter[] | [] = []> = Pick<
|
|
15
|
+
FrontendAction<T>,
|
|
16
|
+
"name" | "description" | "parameters"
|
|
17
|
+
> & {
|
|
6
18
|
available?: "disabled" | "enabled";
|
|
7
|
-
|
|
19
|
+
render: T extends []
|
|
20
|
+
? (props: ActionRenderPropsNoArgs<T>) => React.ReactElement
|
|
21
|
+
: (props: ActionRenderProps<T>) => React.ReactElement;
|
|
22
|
+
};
|
|
8
23
|
|
|
9
24
|
export function useRenderToolCall<const T extends Parameter[] | [] = []>(
|
|
10
|
-
tool:
|
|
25
|
+
tool: UseRenderToolCallArgs<T>,
|
|
11
26
|
dependencies?: any[],
|
|
12
27
|
) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
const { copilotkit } = useCopilotKit();
|
|
29
|
+
|
|
30
|
+
// Track whether we've already added this renderer to avoid duplicates
|
|
31
|
+
const hasAddedRef = useRef(false);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
const { name, parameters, render } = tool;
|
|
35
|
+
const zodParameters = getZodParameters(parameters);
|
|
36
|
+
|
|
37
|
+
const renderToolCall =
|
|
38
|
+
name === "*"
|
|
39
|
+
? defineToolCallRenderer({
|
|
40
|
+
name: "*",
|
|
41
|
+
render: ((args) => {
|
|
42
|
+
return render({
|
|
43
|
+
...args,
|
|
44
|
+
result: args.result ? parseJson(args.result, args.result) : args.result,
|
|
45
|
+
});
|
|
46
|
+
}) as ToolCallRendererDefinition["render"],
|
|
47
|
+
})
|
|
48
|
+
: defineToolCallRenderer({
|
|
49
|
+
name,
|
|
50
|
+
args: zodParameters,
|
|
51
|
+
render: ((args) => {
|
|
52
|
+
return render({
|
|
53
|
+
...args,
|
|
54
|
+
result: args.result ? parseJson(args.result, args.result) : args.result,
|
|
55
|
+
});
|
|
56
|
+
}) as ToolCallRendererDefinition["render"],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Remove any existing renderer with the same name
|
|
60
|
+
const existingIndex = copilotkit.renderToolCalls.findIndex((r) => r.name === name);
|
|
61
|
+
if (existingIndex !== -1) {
|
|
62
|
+
copilotkit.renderToolCalls.splice(existingIndex, 1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Add the new renderer
|
|
66
|
+
copilotkit.renderToolCalls.push(renderToolCall);
|
|
67
|
+
hasAddedRef.current = true;
|
|
68
|
+
|
|
69
|
+
// Cleanup: remove this renderer when the component unmounts or tool changes
|
|
70
|
+
return () => {
|
|
71
|
+
if (hasAddedRef.current) {
|
|
72
|
+
const index = copilotkit.renderToolCalls.findIndex((r) => r.name === name);
|
|
73
|
+
if (index !== -1) {
|
|
74
|
+
copilotkit.renderToolCalls.splice(index, 1);
|
|
75
|
+
}
|
|
76
|
+
hasAddedRef.current = false;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}, [tool, ...(dependencies ?? [])]);
|
|
20
80
|
}
|
package/src/types/index.ts
CHANGED