@copilotkit/react-core 1.57.3 → 1.58.0-canary.thread-id-propagation
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/{copilotkit-CtXcs1ea.cjs → copilotkit-B4ouY7qC.cjs} +14 -3
- package/dist/copilotkit-B4ouY7qC.cjs.map +1 -0
- package/dist/copilotkit-BK9CVq9A.d.cts.map +1 -1
- package/dist/{copilotkit-CC8DjOiC.mjs → copilotkit-L4mM_JqG.mjs} +14 -3
- package/dist/copilotkit-L4mM_JqG.mjs.map +1 -0
- package/dist/copilotkit-WlmeVijs.d.mts.map +1 -1
- package/dist/index.cjs +3 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +3 -77
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +15 -78
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/headless.cjs +11 -0
- package/dist/v2/headless.cjs.map +1 -1
- package/dist/v2/headless.d.cts.map +1 -1
- package/dist/v2/headless.d.mts.map +1 -1
- package/dist/v2/headless.mjs +11 -0
- package/dist/v2/headless.mjs.map +1 -1
- package/dist/v2/index.cjs +1 -1
- package/dist/v2/index.mjs +1 -1
- package/dist/v2/index.umd.js +13 -2
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +12 -13
- package/skills/react-core/SKILL.md +108 -0
- package/skills/react-core/references/agent-access.md +288 -0
- package/skills/react-core/references/attachments.md +291 -0
- package/skills/react-core/references/capabilities.md +138 -0
- package/skills/react-core/references/chat-components.md +221 -0
- package/skills/react-core/references/client-side-tools.md +358 -0
- package/skills/react-core/references/custom-message-renderers.md +226 -0
- package/skills/react-core/references/debug-mode.md +153 -0
- package/skills/react-core/references/human-in-the-loop.md +312 -0
- package/skills/react-core/references/provider-setup.md +326 -0
- package/skills/react-core/references/rendering-activity-messages.md +207 -0
- package/skills/react-core/references/rendering-tool-calls.md +319 -0
- package/skills/react-core/references/suggestions.md +211 -0
- package/skills/react-core/references/switching-agents-recipes.md +160 -0
- package/skills/react-core/references/switching-agents.md +231 -0
- package/skills/react-core/references/threads.md +226 -0
- package/.attw.json +0 -3
- package/CHANGELOG.md +0 -5043
- package/dist/copilotkit-CC8DjOiC.mjs.map +0 -1
- package/dist/copilotkit-CtXcs1ea.cjs.map +0 -1
- package/scripts/scope-preflight.mjs +0 -100
- package/src/components/CopilotListeners.tsx +0 -137
- package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
- package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
- package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
- package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
- package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
- package/src/components/copilot-provider/copilot-messages.tsx +0 -314
- package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
- package/src/components/copilot-provider/copilotkit.tsx +0 -853
- package/src/components/copilot-provider/index.ts +0 -3
- package/src/components/dev-console/console-trigger.tsx +0 -283
- package/src/components/dev-console/developer-console-modal.tsx +0 -1016
- package/src/components/dev-console/icons.tsx +0 -106
- package/src/components/error-boundary/error-boundary.tsx +0 -99
- package/src/components/error-boundary/error-utils.tsx +0 -105
- package/src/components/index.ts +0 -1
- package/src/components/toast/exclamation-mark-icon.tsx +0 -27
- package/src/components/toast/toast-provider.tsx +0 -448
- package/src/components/usage-banner.tsx +0 -266
- package/src/context/__tests__/threads-context.test.tsx +0 -141
- package/src/context/coagent-state-renders-context.tsx +0 -89
- package/src/context/copilot-context.tsx +0 -365
- package/src/context/copilot-messages-context.tsx +0 -35
- package/src/context/index.ts +0 -22
- package/src/context/threads-context.tsx +0 -69
- package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
- package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
- package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
- package/src/hooks/index.ts +0 -33
- package/src/hooks/use-agent-nodename.ts +0 -33
- package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
- package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
- package/src/hooks/use-coagent-state-render-registry.ts +0 -230
- package/src/hooks/use-coagent-state-render.ts +0 -163
- package/src/hooks/use-coagent.ts +0 -377
- package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
- package/src/hooks/use-copilot-action.ts +0 -245
- package/src/hooks/use-copilot-additional-instructions.ts +0 -98
- package/src/hooks/use-copilot-authenticated-action.ts +0 -73
- package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
- package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
- package/src/hooks/use-copilot-chat.ts +0 -132
- package/src/hooks/use-copilot-chat_internal.ts +0 -875
- package/src/hooks/use-copilot-readable.ts +0 -135
- package/src/hooks/use-copilot-runtime-client.ts +0 -178
- package/src/hooks/use-default-tool.ts +0 -13
- package/src/hooks/use-flat-category-store.ts +0 -109
- package/src/hooks/use-frontend-tool.ts +0 -113
- package/src/hooks/use-human-in-the-loop.ts +0 -138
- package/src/hooks/use-langgraph-interrupt.ts +0 -103
- package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
- package/src/hooks/use-make-copilot-document-readable.ts +0 -30
- package/src/hooks/use-render-tool-call.ts +0 -89
- package/src/hooks/use-tree.ts +0 -222
- package/src/index.tsx +0 -7
- package/src/lib/copilot-task.ts +0 -215
- package/src/lib/index.ts +0 -1
- package/src/lib/status-checker.ts +0 -67
- package/src/setupTests.ts +0 -37
- package/src/test-helpers/copilot-context.ts +0 -91
- package/src/types/chat-suggestion-configuration.ts +0 -23
- package/src/types/coagent-action.ts +0 -35
- package/src/types/coagent-state.ts +0 -13
- package/src/types/crew.ts +0 -89
- package/src/types/document-pointer.ts +0 -7
- package/src/types/frontend-action.ts +0 -213
- package/src/types/index.ts +0 -17
- package/src/types/interrupt-action.ts +0 -58
- package/src/types/system-message.ts +0 -4
- package/src/utils/dev-console.ts +0 -19
- package/src/utils/index.ts +0 -2
- package/src/utils/suggestions-constants.ts +0 -8
- package/src/utils/utils.test.ts +0 -7
- package/src/utils/utils.ts +0 -6
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
- package/src/v2/__tests__/globalSetup.ts +0 -14
- package/src/v2/__tests__/setup.ts +0 -93
- package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
- package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
- package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
- package/src/v2/components/CopilotKitInspector.tsx +0 -52
- package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
- package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
- package/src/v2/components/WildcardToolCallRender.tsx +0 -86
- package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
- package/src/v2/components/chat/CopilotChat.tsx +0 -664
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
- package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
- package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
- package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
- package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
- package/src/v2/components/chat/CopilotChatView.tsx +0 -890
- package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
- package/src/v2/components/chat/CopilotPopup.tsx +0 -81
- package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
- package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
- package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
- package/src/v2/components/chat/Lightbox.tsx +0 -103
- package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
- package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
- package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
- package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
- package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
- package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
- package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
- package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
- package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
- package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
- package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
- package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
- package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
- package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
- package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
- package/src/v2/components/chat/__tests__/setup.ts +0 -1
- package/src/v2/components/chat/index.ts +0 -90
- package/src/v2/components/chat/last-user-message-context.ts +0 -21
- package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
- package/src/v2/components/chat/scroll-element-context.ts +0 -13
- package/src/v2/components/index.ts +0 -8
- package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
- package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
- package/src/v2/components/intelligence-indicator/index.ts +0 -2
- package/src/v2/components/license-warning-banner.tsx +0 -217
- package/src/v2/components/ui/button.tsx +0 -124
- package/src/v2/components/ui/dropdown-menu.tsx +0 -258
- package/src/v2/components/ui/tooltip.tsx +0 -60
- package/src/v2/context.ts +0 -62
- package/src/v2/headless.ts +0 -64
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
- package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
- package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
- package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
- package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
- package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
- package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
- package/src/v2/hooks/index.ts +0 -24
- package/src/v2/hooks/use-agent-context.tsx +0 -45
- package/src/v2/hooks/use-agent.tsx +0 -227
- package/src/v2/hooks/use-attachments.tsx +0 -269
- package/src/v2/hooks/use-capabilities.tsx +0 -25
- package/src/v2/hooks/use-component.tsx +0 -91
- package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
- package/src/v2/hooks/use-default-render-tool.tsx +0 -271
- package/src/v2/hooks/use-frontend-tool.tsx +0 -46
- package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
- package/src/v2/hooks/use-interrupt.tsx +0 -305
- package/src/v2/hooks/use-keyboard-height.tsx +0 -67
- package/src/v2/hooks/use-pin-to-send.ts +0 -94
- package/src/v2/hooks/use-render-activity-message.tsx +0 -72
- package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
- package/src/v2/hooks/use-render-tool-call.tsx +0 -208
- package/src/v2/hooks/use-render-tool.tsx +0 -184
- package/src/v2/hooks/use-suggestions.tsx +0 -91
- package/src/v2/hooks/use-threads.tsx +0 -325
- package/src/v2/hooks/useKatexStyles.ts +0 -27
- package/src/v2/index.css +0 -1
- package/src/v2/index.ts +0 -27
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
- package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
- package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
- package/src/v2/lib/__tests__/slots.test.ts +0 -56
- package/src/v2/lib/processPartialHtml.ts +0 -45
- package/src/v2/lib/react-core.ts +0 -156
- package/src/v2/lib/slots.tsx +0 -184
- package/src/v2/lib/transcription-client.ts +0 -184
- package/src/v2/lib/utils.ts +0 -8
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
- package/src/v2/providers/CopilotKitProvider.tsx +0 -800
- package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
- package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
- package/src/v2/providers/index.ts +0 -21
- package/src/v2/styles/globals.css +0 -349
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
- package/src/v2/types/defineToolCallRenderer.ts +0 -68
- package/src/v2/types/frontend-tool.ts +0 -8
- package/src/v2/types/human-in-the-loop.ts +0 -33
- package/src/v2/types/index.ts +0 -8
- package/src/v2/types/interrupt.ts +0 -15
- package/src/v2/types/react-activity-message-renderer.ts +0 -27
- package/src/v2/types/react-custom-message-renderer.ts +0 -17
- package/src/v2/types/react-tool-call-renderer.ts +0 -35
- package/src/v2/types/sandbox-function.ts +0 -11
- package/tsconfig.json +0 -8
- package/tsdown.config.ts +0 -193
- package/typedoc.json +0 -4
- package/vitest.config.mjs +0 -31
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `useCopilotReadable` is a React hook that provides app-state and other information
|
|
3
|
-
* to the Copilot. Optionally, the hook can also handle hierarchical state within your
|
|
4
|
-
* application, passing these parent-child relationships to the Copilot.
|
|
5
|
-
*
|
|
6
|
-
* ## Usage
|
|
7
|
-
*
|
|
8
|
-
* ### Simple Usage
|
|
9
|
-
*
|
|
10
|
-
* In its most basic usage, useCopilotReadable accepts a single string argument
|
|
11
|
-
* representing any piece of app state, making it available for the Copilot to use
|
|
12
|
-
* as context when responding to user input.
|
|
13
|
-
*
|
|
14
|
-
* ```tsx
|
|
15
|
-
* import { useCopilotReadable } from "@copilotkit/react-core";
|
|
16
|
-
*
|
|
17
|
-
* export function MyComponent() {
|
|
18
|
-
* const [employees, setEmployees] = useState([]);
|
|
19
|
-
*
|
|
20
|
-
* useCopilotReadable({
|
|
21
|
-
* description: "The list of employees",
|
|
22
|
-
* value: employees,
|
|
23
|
-
* });
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* ### Nested Components
|
|
28
|
-
*
|
|
29
|
-
* Optionally, you can maintain the hierarchical structure of information by passing
|
|
30
|
-
* `parentId`. This allows you to use `useCopilotReadable` in nested components:
|
|
31
|
-
*
|
|
32
|
-
* ```tsx /employeeContextId/1 {17,23}
|
|
33
|
-
* import { useCopilotReadable } from "@copilotkit/react-core";
|
|
34
|
-
*
|
|
35
|
-
* function Employee(props: EmployeeProps) {
|
|
36
|
-
* const { employeeName, workProfile, metadata } = props;
|
|
37
|
-
*
|
|
38
|
-
* // propagate any information to copilot
|
|
39
|
-
* const employeeContextId = useCopilotReadable({
|
|
40
|
-
* description: "Employee name",
|
|
41
|
-
* value: employeeName
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* // Pass a parentID to maintain a hierarchical structure.
|
|
45
|
-
* // Especially useful with child React components, list elements, etc.
|
|
46
|
-
* useCopilotReadable({
|
|
47
|
-
* description: "Work profile",
|
|
48
|
-
* value: workProfile.description(),
|
|
49
|
-
* parentId: employeeContextId
|
|
50
|
-
* });
|
|
51
|
-
*
|
|
52
|
-
* useCopilotReadable({
|
|
53
|
-
* description: "Employee metadata",
|
|
54
|
-
* value: metadata.description(),
|
|
55
|
-
* parentId: employeeContextId
|
|
56
|
-
* });
|
|
57
|
-
*
|
|
58
|
-
* return (
|
|
59
|
-
* // Render as usual...
|
|
60
|
-
* );
|
|
61
|
-
* }
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
import { useCopilotKit } from "../v2";
|
|
65
|
-
import { useEffect, useRef } from "react";
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Options for the useCopilotReadable hook.
|
|
69
|
-
*/
|
|
70
|
-
export interface UseCopilotReadableOptions {
|
|
71
|
-
/**
|
|
72
|
-
* The description of the information to be added to the Copilot context.
|
|
73
|
-
*/
|
|
74
|
-
description: string;
|
|
75
|
-
/**
|
|
76
|
-
* The value to be added to the Copilot context. Object values are automatically stringified.
|
|
77
|
-
*/
|
|
78
|
-
value: any;
|
|
79
|
-
/**
|
|
80
|
-
* The ID of the parent context, if any.
|
|
81
|
-
*/
|
|
82
|
-
parentId?: string;
|
|
83
|
-
/**
|
|
84
|
-
* An array of categories to control which context are visible where. Particularly useful
|
|
85
|
-
* with CopilotTextarea (see `useMakeAutosuggestionFunction`)
|
|
86
|
-
*/
|
|
87
|
-
categories?: string[];
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Whether the context is available to the Copilot.
|
|
91
|
-
*/
|
|
92
|
-
available?: "enabled" | "disabled";
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* A custom conversion function to use to serialize the value to a string. If not provided, the value
|
|
96
|
-
* will be serialized using `JSON.stringify`.
|
|
97
|
-
*/
|
|
98
|
-
convert?: (description: string, value: any) => string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Adds the given information to the Copilot context to make it readable by Copilot.
|
|
103
|
-
*/
|
|
104
|
-
export function useCopilotReadable(
|
|
105
|
-
{ description, value, convert, available }: UseCopilotReadableOptions,
|
|
106
|
-
dependencies?: any[],
|
|
107
|
-
): string | undefined {
|
|
108
|
-
const { copilotkit } = useCopilotKit();
|
|
109
|
-
const ctxIdRef = useRef<string | undefined>(undefined);
|
|
110
|
-
useEffect(() => {
|
|
111
|
-
if (!copilotkit) return;
|
|
112
|
-
|
|
113
|
-
const found = Object.entries(copilotkit.context).find(([id, ctxItem]) => {
|
|
114
|
-
return JSON.stringify({ description, value }) == JSON.stringify(ctxItem);
|
|
115
|
-
});
|
|
116
|
-
if (found) {
|
|
117
|
-
ctxIdRef.current = found[0];
|
|
118
|
-
if (available === "disabled") copilotkit.removeContext(ctxIdRef.current);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
if (!found && available === "disabled") return;
|
|
122
|
-
|
|
123
|
-
ctxIdRef.current = copilotkit.addContext({
|
|
124
|
-
description,
|
|
125
|
-
value: (convert ?? JSON.stringify)(value),
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
return () => {
|
|
129
|
-
if (!ctxIdRef.current) return;
|
|
130
|
-
copilotkit.removeContext(ctxIdRef.current);
|
|
131
|
-
};
|
|
132
|
-
}, [description, value, convert]);
|
|
133
|
-
|
|
134
|
-
return ctxIdRef.current;
|
|
135
|
-
}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CopilotRuntimeClient,
|
|
3
|
-
CopilotRuntimeClientOptions,
|
|
4
|
-
GraphQLError,
|
|
5
|
-
} from "@copilotkit/runtime-client-gql";
|
|
6
|
-
import { useToast } from "../components/toast/toast-provider";
|
|
7
|
-
import { useMemo, useRef } from "react";
|
|
8
|
-
import {
|
|
9
|
-
ErrorVisibility,
|
|
10
|
-
CopilotKitApiDiscoveryError,
|
|
11
|
-
CopilotKitRemoteEndpointDiscoveryError,
|
|
12
|
-
CopilotKitAgentDiscoveryError,
|
|
13
|
-
CopilotKitError,
|
|
14
|
-
CopilotKitErrorCode,
|
|
15
|
-
CopilotErrorHandler,
|
|
16
|
-
CopilotErrorEvent,
|
|
17
|
-
} from "@copilotkit/shared";
|
|
18
|
-
import { shouldShowDevConsole } from "../utils/dev-console";
|
|
19
|
-
|
|
20
|
-
export interface CopilotRuntimeClientHookOptions extends CopilotRuntimeClientOptions {
|
|
21
|
-
showDevConsole?: boolean;
|
|
22
|
-
onError: CopilotErrorHandler;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const useCopilotRuntimeClient = (
|
|
26
|
-
options: CopilotRuntimeClientHookOptions,
|
|
27
|
-
) => {
|
|
28
|
-
const { setBannerError } = useToast();
|
|
29
|
-
const { showDevConsole, onError, ...runtimeOptions } = options;
|
|
30
|
-
|
|
31
|
-
// Deduplication state for structured errors
|
|
32
|
-
const lastStructuredErrorRef = useRef<{
|
|
33
|
-
message: string;
|
|
34
|
-
timestamp: number;
|
|
35
|
-
} | null>(null);
|
|
36
|
-
|
|
37
|
-
// Helper function to trace UI errors
|
|
38
|
-
const traceUIError = async (error: CopilotKitError, originalError?: any) => {
|
|
39
|
-
try {
|
|
40
|
-
const errorEvent: CopilotErrorEvent = {
|
|
41
|
-
type: "error",
|
|
42
|
-
timestamp: Date.now(),
|
|
43
|
-
context: {
|
|
44
|
-
source: "ui",
|
|
45
|
-
request: {
|
|
46
|
-
operation: "runtimeClient",
|
|
47
|
-
url: runtimeOptions.url,
|
|
48
|
-
startTime: Date.now(),
|
|
49
|
-
},
|
|
50
|
-
technical: {
|
|
51
|
-
environment: "browser",
|
|
52
|
-
userAgent:
|
|
53
|
-
typeof navigator !== "undefined"
|
|
54
|
-
? navigator.userAgent
|
|
55
|
-
: undefined,
|
|
56
|
-
stackTrace:
|
|
57
|
-
originalError instanceof Error ? originalError.stack : undefined,
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
error,
|
|
61
|
-
};
|
|
62
|
-
await onError(errorEvent);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
console.error("Error in onError handler:", error);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const runtimeClient = useMemo(() => {
|
|
69
|
-
return new CopilotRuntimeClient({
|
|
70
|
-
...runtimeOptions,
|
|
71
|
-
handleGQLErrors: (error) => {
|
|
72
|
-
if ((error as any).graphQLErrors?.length) {
|
|
73
|
-
const graphQLErrors = (error as any).graphQLErrors as GraphQLError[];
|
|
74
|
-
|
|
75
|
-
// Route all errors to banners for consistent UI
|
|
76
|
-
const routeError = (gqlError: GraphQLError) => {
|
|
77
|
-
const extensions = gqlError.extensions;
|
|
78
|
-
const visibility = extensions?.visibility as ErrorVisibility;
|
|
79
|
-
|
|
80
|
-
// Silent errors - just log
|
|
81
|
-
if (visibility === ErrorVisibility.SILENT) {
|
|
82
|
-
console.error("CopilotKit Silent Error:", gqlError.message);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// All errors (including DEV_ONLY) show as banners for consistency
|
|
87
|
-
// Deduplicate to prevent spam
|
|
88
|
-
const now = Date.now();
|
|
89
|
-
const errorMessage = gqlError.message;
|
|
90
|
-
if (
|
|
91
|
-
lastStructuredErrorRef.current &&
|
|
92
|
-
lastStructuredErrorRef.current.message === errorMessage &&
|
|
93
|
-
now - lastStructuredErrorRef.current.timestamp < 150
|
|
94
|
-
) {
|
|
95
|
-
return; // Skip duplicate
|
|
96
|
-
}
|
|
97
|
-
lastStructuredErrorRef.current = {
|
|
98
|
-
message: errorMessage,
|
|
99
|
-
timestamp: now,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const ckError = createStructuredError(gqlError);
|
|
103
|
-
if (ckError) {
|
|
104
|
-
setBannerError(ckError);
|
|
105
|
-
// Trace the error
|
|
106
|
-
traceUIError(ckError, gqlError);
|
|
107
|
-
// TODO: if onError & renderError should work without key, insert here
|
|
108
|
-
} else {
|
|
109
|
-
// Fallback for unstructured errors
|
|
110
|
-
const fallbackError = new CopilotKitError({
|
|
111
|
-
message: gqlError.message,
|
|
112
|
-
code: CopilotKitErrorCode.UNKNOWN,
|
|
113
|
-
});
|
|
114
|
-
setBannerError(fallbackError);
|
|
115
|
-
// Trace the fallback error
|
|
116
|
-
traceUIError(fallbackError, gqlError);
|
|
117
|
-
// TODO: if onError & renderError should work without key, insert here
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// Process all errors as banners
|
|
122
|
-
graphQLErrors.forEach(routeError);
|
|
123
|
-
} else {
|
|
124
|
-
// Route non-GraphQL errors to banner as well
|
|
125
|
-
const fallbackError = new CopilotKitError({
|
|
126
|
-
message: error?.message || String(error),
|
|
127
|
-
code: CopilotKitErrorCode.UNKNOWN,
|
|
128
|
-
});
|
|
129
|
-
setBannerError(fallbackError);
|
|
130
|
-
// Trace the non-GraphQL error
|
|
131
|
-
traceUIError(fallbackError, error);
|
|
132
|
-
// TODO: if onError & renderError should work without key, insert here
|
|
133
|
-
}
|
|
134
|
-
},
|
|
135
|
-
handleGQLWarning: (message: string) => {
|
|
136
|
-
console.warn(message);
|
|
137
|
-
// Show warnings as banners too for consistency
|
|
138
|
-
const warningError = new CopilotKitError({
|
|
139
|
-
message,
|
|
140
|
-
code: CopilotKitErrorCode.UNKNOWN,
|
|
141
|
-
});
|
|
142
|
-
setBannerError(warningError);
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
}, [runtimeOptions, setBannerError, onError]);
|
|
146
|
-
|
|
147
|
-
return runtimeClient;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
// Create appropriate structured error from GraphQL error
|
|
151
|
-
function createStructuredError(gqlError: GraphQLError): CopilotKitError | null {
|
|
152
|
-
const extensions = gqlError.extensions;
|
|
153
|
-
const originalError = extensions?.originalError as any;
|
|
154
|
-
const message = originalError?.message || gqlError.message;
|
|
155
|
-
const code = extensions?.code as CopilotKitErrorCode;
|
|
156
|
-
|
|
157
|
-
if (code) {
|
|
158
|
-
return new CopilotKitError({ message, code });
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Legacy error detection by stack trace
|
|
162
|
-
if (originalError?.stack?.includes("CopilotApiDiscoveryError")) {
|
|
163
|
-
return new CopilotKitApiDiscoveryError({ message });
|
|
164
|
-
}
|
|
165
|
-
if (
|
|
166
|
-
originalError?.stack?.includes("CopilotKitRemoteEndpointDiscoveryError")
|
|
167
|
-
) {
|
|
168
|
-
return new CopilotKitRemoteEndpointDiscoveryError({ message });
|
|
169
|
-
}
|
|
170
|
-
if (originalError?.stack?.includes("CopilotKitAgentDiscoveryError")) {
|
|
171
|
-
return new CopilotKitAgentDiscoveryError({
|
|
172
|
-
agentName: "",
|
|
173
|
-
availableAgents: [],
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { useCopilotAction } from "./use-copilot-action";
|
|
2
|
-
import { CatchAllFrontendAction } from "../types/frontend-action";
|
|
3
|
-
|
|
4
|
-
export function useDefaultTool(
|
|
5
|
-
tool: Omit<CatchAllFrontendAction, "name">,
|
|
6
|
-
dependencies?: any[],
|
|
7
|
-
) {
|
|
8
|
-
// Use the existing useCopilotAction hook
|
|
9
|
-
useCopilotAction(
|
|
10
|
-
{ ...tool, name: "*" } satisfies CatchAllFrontendAction,
|
|
11
|
-
dependencies,
|
|
12
|
-
);
|
|
13
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { useCallback, useReducer } from "react";
|
|
2
|
-
import { randomId } from "@copilotkit/shared";
|
|
3
|
-
|
|
4
|
-
export type FlatCategoryStoreId = string;
|
|
5
|
-
|
|
6
|
-
export interface UseFlatCategoryStoreReturn<T> {
|
|
7
|
-
addElement: (value: T, categories: string[]) => FlatCategoryStoreId;
|
|
8
|
-
removeElement: (id: FlatCategoryStoreId) => void;
|
|
9
|
-
allElements: (categories: string[]) => T[];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface FlatCategoryStoreElement<T> {
|
|
13
|
-
id: FlatCategoryStoreId;
|
|
14
|
-
value: T;
|
|
15
|
-
categories: Set<string>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {
|
|
19
|
-
const [elements, dispatch] = useReducer(
|
|
20
|
-
flatCategoryStoreReducer<T>,
|
|
21
|
-
new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>(),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const addElement = useCallback(
|
|
25
|
-
(value: T, categories: string[]): FlatCategoryStoreId => {
|
|
26
|
-
const newId = randomId();
|
|
27
|
-
dispatch({
|
|
28
|
-
type: "ADD_ELEMENT",
|
|
29
|
-
value,
|
|
30
|
-
id: newId,
|
|
31
|
-
categories,
|
|
32
|
-
});
|
|
33
|
-
return newId;
|
|
34
|
-
},
|
|
35
|
-
[],
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const removeElement = useCallback((id: FlatCategoryStoreId): void => {
|
|
39
|
-
dispatch({ type: "REMOVE_ELEMENT", id });
|
|
40
|
-
}, []);
|
|
41
|
-
|
|
42
|
-
const allElements = useCallback(
|
|
43
|
-
(categories: string[]): T[] => {
|
|
44
|
-
const categoriesSet = new Set(categories);
|
|
45
|
-
const result: T[] = [];
|
|
46
|
-
elements.forEach((element) => {
|
|
47
|
-
if (setsHaveIntersection(categoriesSet, element.categories)) {
|
|
48
|
-
result.push(element.value);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
return result;
|
|
52
|
-
},
|
|
53
|
-
[elements],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
return { addElement, removeElement, allElements };
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export default useFlatCategoryStore;
|
|
60
|
-
|
|
61
|
-
// Action types
|
|
62
|
-
type Action<T> =
|
|
63
|
-
| {
|
|
64
|
-
type: "ADD_ELEMENT";
|
|
65
|
-
value: T;
|
|
66
|
-
id: FlatCategoryStoreId;
|
|
67
|
-
categories: string[];
|
|
68
|
-
}
|
|
69
|
-
| { type: "REMOVE_ELEMENT"; id: FlatCategoryStoreId };
|
|
70
|
-
|
|
71
|
-
// Reducer
|
|
72
|
-
function flatCategoryStoreReducer<T>(
|
|
73
|
-
state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
|
|
74
|
-
action: Action<T>,
|
|
75
|
-
): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {
|
|
76
|
-
switch (action.type) {
|
|
77
|
-
case "ADD_ELEMENT": {
|
|
78
|
-
const { value, id, categories } = action;
|
|
79
|
-
const newElement: FlatCategoryStoreElement<T> = {
|
|
80
|
-
id,
|
|
81
|
-
value,
|
|
82
|
-
categories: new Set(categories),
|
|
83
|
-
};
|
|
84
|
-
const newState = new Map(state);
|
|
85
|
-
newState.set(id, newElement);
|
|
86
|
-
return newState;
|
|
87
|
-
}
|
|
88
|
-
case "REMOVE_ELEMENT": {
|
|
89
|
-
const newState = new Map(state);
|
|
90
|
-
newState.delete(action.id);
|
|
91
|
-
return newState;
|
|
92
|
-
}
|
|
93
|
-
default:
|
|
94
|
-
return state;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
|
|
99
|
-
const [smallerSet, largerSet] =
|
|
100
|
-
setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
101
|
-
|
|
102
|
-
for (let item of smallerSet) {
|
|
103
|
-
if (largerSet.has(item)) {
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, useMemo, useRef } from "react";
|
|
2
|
-
import { ActionRenderProps, FrontendAction } from "../types/frontend-action";
|
|
3
|
-
import {
|
|
4
|
-
Parameter,
|
|
5
|
-
getZodParameters,
|
|
6
|
-
MappedParameterTypes,
|
|
7
|
-
} from "@copilotkit/shared";
|
|
8
|
-
import { parseJson } from "@copilotkit/shared";
|
|
9
|
-
import { ToolCallStatus } from "@copilotkit/core";
|
|
10
|
-
import {
|
|
11
|
-
type ReactFrontendTool,
|
|
12
|
-
useFrontendTool as useFrontendToolVNext,
|
|
13
|
-
} from "../v2";
|
|
14
|
-
|
|
15
|
-
type FrontendToolOptions<T extends Parameter[] | []> = ReactFrontendTool<
|
|
16
|
-
MappedParameterTypes<T>
|
|
17
|
-
>;
|
|
18
|
-
type FrontendToolRenderArgs<T extends Parameter[] | []> =
|
|
19
|
-
| {
|
|
20
|
-
name: string;
|
|
21
|
-
args: Partial<MappedParameterTypes<T>>;
|
|
22
|
-
status: ToolCallStatus.InProgress;
|
|
23
|
-
result: undefined;
|
|
24
|
-
}
|
|
25
|
-
| {
|
|
26
|
-
name: string;
|
|
27
|
-
args: MappedParameterTypes<T>;
|
|
28
|
-
status: ToolCallStatus.Executing;
|
|
29
|
-
result: undefined;
|
|
30
|
-
}
|
|
31
|
-
| {
|
|
32
|
-
name: string;
|
|
33
|
-
args: MappedParameterTypes<T>;
|
|
34
|
-
status: ToolCallStatus.Complete;
|
|
35
|
-
result: string;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type UseFrontendToolArgs<T extends Parameter[] | [] = []> = {
|
|
39
|
-
available?: "disabled" | "enabled";
|
|
40
|
-
} & Pick<
|
|
41
|
-
FrontendAction<T>,
|
|
42
|
-
"name" | "description" | "parameters" | "handler" | "followUp" | "render"
|
|
43
|
-
>;
|
|
44
|
-
|
|
45
|
-
export function useFrontendTool<const T extends Parameter[] = []>(
|
|
46
|
-
tool: UseFrontendToolArgs<T>,
|
|
47
|
-
dependencies?: any[],
|
|
48
|
-
) {
|
|
49
|
-
const { name, description, parameters, render, followUp, available } = tool;
|
|
50
|
-
const zodParameters = getZodParameters(parameters);
|
|
51
|
-
|
|
52
|
-
const renderRef = useRef<typeof render>(render);
|
|
53
|
-
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
renderRef.current = render;
|
|
56
|
-
}, [render, ...(dependencies ?? [])]);
|
|
57
|
-
|
|
58
|
-
const normalizedRender: FrontendToolOptions<T>["render"] | undefined =
|
|
59
|
-
useMemo(() => {
|
|
60
|
-
if (typeof render === "undefined") {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return ((args: FrontendToolRenderArgs<T>) => {
|
|
65
|
-
const currentRender = renderRef.current;
|
|
66
|
-
|
|
67
|
-
if (typeof currentRender === "undefined") {
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (typeof currentRender === "string") {
|
|
72
|
-
return React.createElement(React.Fragment, null, currentRender);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const renderArgs = {
|
|
76
|
-
...args,
|
|
77
|
-
result:
|
|
78
|
-
typeof args.result === "string"
|
|
79
|
-
? parseJson(args.result, args.result)
|
|
80
|
-
: args.result,
|
|
81
|
-
} as ActionRenderProps<T>;
|
|
82
|
-
|
|
83
|
-
const rendered = currentRender(renderArgs);
|
|
84
|
-
|
|
85
|
-
if (typeof rendered === "string") {
|
|
86
|
-
return React.createElement(React.Fragment, null, rendered);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return rendered ?? null;
|
|
90
|
-
}) as FrontendToolOptions<T>["render"];
|
|
91
|
-
}, []);
|
|
92
|
-
|
|
93
|
-
// Handler ref to avoid stale closures
|
|
94
|
-
const handlerRef = useRef<typeof tool.handler>(tool.handler);
|
|
95
|
-
|
|
96
|
-
useEffect(() => {
|
|
97
|
-
handlerRef.current = tool.handler;
|
|
98
|
-
}, [tool.handler, ...(dependencies ?? [])]);
|
|
99
|
-
|
|
100
|
-
const normalizedHandler = tool.handler
|
|
101
|
-
? (args: MappedParameterTypes<T>) => handlerRef.current?.(args)
|
|
102
|
-
: undefined;
|
|
103
|
-
|
|
104
|
-
useFrontendToolVNext<MappedParameterTypes<T>>({
|
|
105
|
-
name,
|
|
106
|
-
description,
|
|
107
|
-
parameters: zodParameters,
|
|
108
|
-
handler: normalizedHandler,
|
|
109
|
-
followUp,
|
|
110
|
-
render: normalizedRender,
|
|
111
|
-
available: available === undefined ? undefined : available !== "disabled",
|
|
112
|
-
});
|
|
113
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ActionRenderProps,
|
|
3
|
-
ActionRenderPropsWait,
|
|
4
|
-
FrontendAction,
|
|
5
|
-
} from "../types";
|
|
6
|
-
import {
|
|
7
|
-
CopilotKitError,
|
|
8
|
-
CopilotKitErrorCode,
|
|
9
|
-
MappedParameterTypes,
|
|
10
|
-
Parameter,
|
|
11
|
-
getZodParameters,
|
|
12
|
-
parseJson,
|
|
13
|
-
} from "@copilotkit/shared";
|
|
14
|
-
import { useHumanInTheLoop as useHumanInTheLoopVNext } from "../v2";
|
|
15
|
-
import { ToolCallStatus } from "@copilotkit/core";
|
|
16
|
-
import React, {
|
|
17
|
-
ComponentType,
|
|
18
|
-
FunctionComponent,
|
|
19
|
-
useEffect,
|
|
20
|
-
useRef,
|
|
21
|
-
} from "react";
|
|
22
|
-
|
|
23
|
-
type HumanInTheLoopOptions = Parameters<typeof useHumanInTheLoopVNext>[0];
|
|
24
|
-
type HumanInTheLoopRender = HumanInTheLoopOptions["render"];
|
|
25
|
-
type HumanInTheLoopRenderArgs = HumanInTheLoopRender extends (
|
|
26
|
-
props: infer P,
|
|
27
|
-
) => any
|
|
28
|
-
? P
|
|
29
|
-
: never;
|
|
30
|
-
|
|
31
|
-
export type UseHumanInTheLoopArgs<T extends Parameter[] | [] = []> = {
|
|
32
|
-
available?: "disabled" | "enabled";
|
|
33
|
-
render: FrontendAction<T>["renderAndWaitForResponse"];
|
|
34
|
-
followUp?: FrontendAction<T>["followUp"];
|
|
35
|
-
} & Pick<FrontendAction<T>, "name" | "description" | "parameters">;
|
|
36
|
-
|
|
37
|
-
type HitlRendererArgs =
|
|
38
|
-
| {
|
|
39
|
-
name: string;
|
|
40
|
-
description: string;
|
|
41
|
-
args: Partial<Record<string, unknown>>;
|
|
42
|
-
status: ToolCallStatus.InProgress;
|
|
43
|
-
result: undefined;
|
|
44
|
-
respond: undefined;
|
|
45
|
-
}
|
|
46
|
-
| {
|
|
47
|
-
name: string;
|
|
48
|
-
description: string;
|
|
49
|
-
args: Record<string, unknown>;
|
|
50
|
-
status: ToolCallStatus.Executing;
|
|
51
|
-
result: undefined;
|
|
52
|
-
respond: (result: unknown) => Promise<void>;
|
|
53
|
-
}
|
|
54
|
-
| {
|
|
55
|
-
name: string;
|
|
56
|
-
description: string;
|
|
57
|
-
args: Record<string, unknown>;
|
|
58
|
-
status: ToolCallStatus.Complete;
|
|
59
|
-
result: string;
|
|
60
|
-
respond: undefined;
|
|
61
|
-
};
|
|
62
|
-
type HitlRenderer = FunctionComponent<HitlRendererArgs>;
|
|
63
|
-
|
|
64
|
-
export function useHumanInTheLoop<const T extends Parameter[] | [] = []>(
|
|
65
|
-
tool: UseHumanInTheLoopArgs<T>,
|
|
66
|
-
dependencies?: any[],
|
|
67
|
-
) {
|
|
68
|
-
const { render, ...toolRest } = tool;
|
|
69
|
-
const { name, description, parameters, followUp } = toolRest;
|
|
70
|
-
const zodParameters = getZodParameters(parameters);
|
|
71
|
-
const renderRef = useRef<HitlRenderer | null>(null);
|
|
72
|
-
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
renderRef.current = (args: HitlRendererArgs): React.ReactElement | null => {
|
|
75
|
-
if (typeof render === "string") {
|
|
76
|
-
return React.createElement(React.Fragment, null, render);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!render) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const renderProps: ActionRenderPropsWait<T> = (() => {
|
|
84
|
-
const mappedArgs = args.args as unknown as MappedParameterTypes<T>;
|
|
85
|
-
|
|
86
|
-
switch (args.status) {
|
|
87
|
-
case ToolCallStatus.InProgress:
|
|
88
|
-
return {
|
|
89
|
-
args: mappedArgs,
|
|
90
|
-
respond: args.respond,
|
|
91
|
-
status: args.status,
|
|
92
|
-
handler: undefined,
|
|
93
|
-
};
|
|
94
|
-
case ToolCallStatus.Executing:
|
|
95
|
-
return {
|
|
96
|
-
args: mappedArgs,
|
|
97
|
-
respond: args.respond,
|
|
98
|
-
status: args.status,
|
|
99
|
-
handler: () => {},
|
|
100
|
-
};
|
|
101
|
-
case ToolCallStatus.Complete:
|
|
102
|
-
return {
|
|
103
|
-
args: mappedArgs,
|
|
104
|
-
respond: args.respond,
|
|
105
|
-
status: args.status,
|
|
106
|
-
result: args.result
|
|
107
|
-
? parseJson(args.result, args.result)
|
|
108
|
-
: args.result,
|
|
109
|
-
handler: undefined,
|
|
110
|
-
};
|
|
111
|
-
default:
|
|
112
|
-
throw new CopilotKitError({
|
|
113
|
-
code: CopilotKitErrorCode.UNKNOWN,
|
|
114
|
-
message: `Invalid tool call status: ${(args as unknown as { status: string }).status}`,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
})();
|
|
118
|
-
|
|
119
|
-
const rendered = render(renderProps);
|
|
120
|
-
|
|
121
|
-
if (typeof rendered === "string") {
|
|
122
|
-
return React.createElement(React.Fragment, null, rendered);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return rendered ?? null;
|
|
126
|
-
};
|
|
127
|
-
}, [render, ...(dependencies ?? [])]);
|
|
128
|
-
|
|
129
|
-
useHumanInTheLoopVNext({
|
|
130
|
-
name,
|
|
131
|
-
description,
|
|
132
|
-
followUp,
|
|
133
|
-
parameters: zodParameters,
|
|
134
|
-
render: ((args: HumanInTheLoopRenderArgs) =>
|
|
135
|
-
renderRef.current?.(args as HitlRendererArgs) ??
|
|
136
|
-
null) as HumanInTheLoopOptions["render"],
|
|
137
|
-
});
|
|
138
|
-
}
|