@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,222 +0,0 @@
|
|
|
1
|
-
import { ReactCustomMessageRendererPosition, useAgent } from "../v2";
|
|
2
|
-
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
3
|
-
import type { AgentSubscriber } from "@ag-ui/client";
|
|
4
|
-
import { useCoAgentStateRenders } from "../context";
|
|
5
|
-
import { parseJson } from "@copilotkit/shared";
|
|
6
|
-
import { RenderStatus } from "./use-coagent-state-render-bridge.helpers";
|
|
7
|
-
import { useStateRenderRegistry } from "./use-coagent-state-render-registry";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Bridge hook that connects agent state renders to chat messages.
|
|
11
|
-
*
|
|
12
|
-
* ## Purpose
|
|
13
|
-
* This hook finds matching state render configurations (registered via useCoAgentStateRender)
|
|
14
|
-
* and returns UI to render in chat.
|
|
15
|
-
* It ensures each state render appears bound to a specific message, preventing duplicates while
|
|
16
|
-
* allowing re-binding when the underlying state changes significantly.
|
|
17
|
-
*
|
|
18
|
-
* ## Message-ID-Based Claiming System
|
|
19
|
-
*
|
|
20
|
-
* ### The Problem
|
|
21
|
-
* Multiple bridge component instances render simultaneously (one per message). Without coordination,
|
|
22
|
-
* they would all try to render the same state render, causing duplicates.
|
|
23
|
-
*
|
|
24
|
-
* ### The Solution: Message-ID Claims with State Comparison
|
|
25
|
-
* Each state render is "claimed" by exactly one **message ID** (not runId):
|
|
26
|
-
*
|
|
27
|
-
* **Claim Structure**: `claimsRef.current[messageId] = { stateRenderId, runId, stateSnapshot, locked }`
|
|
28
|
-
*
|
|
29
|
-
* **Primary binding is by messageId because**:
|
|
30
|
-
* - runId is not always available immediately (starts as "pending")
|
|
31
|
-
* - messageId is the stable identifier throughout the message lifecycle
|
|
32
|
-
* - Claims persist across component remounts via context ref
|
|
33
|
-
*
|
|
34
|
-
* ### Claiming Logic Flow
|
|
35
|
-
*
|
|
36
|
-
* 1. **Message already has a claim**:
|
|
37
|
-
* - Check if the claim matches the current stateRenderId
|
|
38
|
-
* - If yes → render (this message owns this render)
|
|
39
|
-
* - Update runId if it was "pending" and now available
|
|
40
|
-
*
|
|
41
|
-
* 2. **State render claimed by another message**:
|
|
42
|
-
* - Compare state snapshots (ignoring constant keys: messages, tools, copilotkit)
|
|
43
|
-
* - If states are identical → block rendering (duplicate)
|
|
44
|
-
* - **If states are different → allow claiming** (new data, new message)
|
|
45
|
-
* - This handles cases where the same render type shows different states in different messages
|
|
46
|
-
*
|
|
47
|
-
* 3. **Unclaimed state render**:
|
|
48
|
-
* - Only allow claiming if runId is "pending" (initial render)
|
|
49
|
-
* - If runId is real but no claim exists → block (edge case protection)
|
|
50
|
-
* - Create new claim: `claimsRef.current[messageId] = { stateRenderId, runId }`
|
|
51
|
-
*
|
|
52
|
-
* ### State Snapshot Locking
|
|
53
|
-
*
|
|
54
|
-
* Once a state snapshot is captured and locked for a message:
|
|
55
|
-
* - The UI always renders with the locked snapshot (not live agent.state)
|
|
56
|
-
* - Prevents UI from appearing "wiped" during state transitions
|
|
57
|
-
* - Locked when: stateSnapshot prop is available (from message persistence)
|
|
58
|
-
* - Unlocked state: can still update from live agent.state
|
|
59
|
-
*
|
|
60
|
-
* ### Synchronous Claiming (Ref-based)
|
|
61
|
-
*
|
|
62
|
-
* Claims are stored in a context-level ref (not React state):
|
|
63
|
-
* - Multiple bridges render in the same tick
|
|
64
|
-
* - State updates are async - would allow duplicates before update completes
|
|
65
|
-
* - Ref provides immediate, synchronous claim checking
|
|
66
|
-
* - Survives component remounts (stored in context, not component)
|
|
67
|
-
*
|
|
68
|
-
* ## Flow Example
|
|
69
|
-
*
|
|
70
|
-
* ```
|
|
71
|
-
* Time 1: Message A renders, runId=undefined, state={progress: 50%}
|
|
72
|
-
* → effectiveRunId = "pending"
|
|
73
|
-
* → Claims: claimsRef["msgA"] = { stateRenderId: "tasks", runId: "pending", stateSnapshot: {progress: 50%} }
|
|
74
|
-
* → Renders UI with 50% progress
|
|
75
|
-
*
|
|
76
|
-
* Time 2: Message B renders, runId=undefined, same state
|
|
77
|
-
* → Checks: "tasks" already claimed by msgA with same state
|
|
78
|
-
* → Returns null (blocked - duplicate)
|
|
79
|
-
*
|
|
80
|
-
* Time 3: Real runId appears (e.g., "run-123")
|
|
81
|
-
* → Updates claim: claimsRef["msgA"].runId = "run-123"
|
|
82
|
-
* → Message A continues rendering
|
|
83
|
-
*
|
|
84
|
-
* Time 4: Agent processes more, state={progress: 100%}
|
|
85
|
-
* → Message A: locked to 50% (stateSnapshot locked)
|
|
86
|
-
* → Message C renders with state={progress: 100%}
|
|
87
|
-
* → Checks: "tasks" claimed by msgA but state is DIFFERENT (50% vs 100%)
|
|
88
|
-
* → Allows new claim: claimsRef["msgC"] = { stateRenderId: "tasks", runId: "run-123", stateSnapshot: {progress: 100%} }
|
|
89
|
-
* → Both messages render independently with their own snapshots
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
export interface CoAgentStateRenderBridgeProps {
|
|
93
|
-
message: any;
|
|
94
|
-
position: ReactCustomMessageRendererPosition;
|
|
95
|
-
runId: string;
|
|
96
|
-
messageIndex: number;
|
|
97
|
-
messageIndexInRun: number;
|
|
98
|
-
numberOfMessagesInRun: number;
|
|
99
|
-
agentId: string;
|
|
100
|
-
stateSnapshot: any;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function useCoagentStateRenderBridge(
|
|
104
|
-
agentId: string,
|
|
105
|
-
props: CoAgentStateRenderBridgeProps,
|
|
106
|
-
) {
|
|
107
|
-
const { stateSnapshot, message } = props;
|
|
108
|
-
const { coAgentStateRenders, claimsRef } = useCoAgentStateRenders();
|
|
109
|
-
const { agent } = useAgent({ agentId });
|
|
110
|
-
const [nodeName, setNodeName] = useState<string | undefined>(undefined);
|
|
111
|
-
const [, forceUpdate] = useState(0);
|
|
112
|
-
|
|
113
|
-
useEffect(() => {
|
|
114
|
-
if (!agent) return;
|
|
115
|
-
const subscriber: AgentSubscriber = {
|
|
116
|
-
onStateChanged: () => {
|
|
117
|
-
forceUpdate((value) => value + 1);
|
|
118
|
-
},
|
|
119
|
-
onStepStartedEvent: ({ event }) => {
|
|
120
|
-
if (event.stepName !== nodeName) {
|
|
121
|
-
setNodeName(event.stepName);
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
onStepFinishedEvent: ({ event }) => {
|
|
125
|
-
if (event.stepName === nodeName) {
|
|
126
|
-
setNodeName(undefined);
|
|
127
|
-
}
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const { unsubscribe } = agent.subscribe(subscriber);
|
|
132
|
-
return () => {
|
|
133
|
-
unsubscribe();
|
|
134
|
-
};
|
|
135
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
136
|
-
}, [agentId, nodeName]);
|
|
137
|
-
|
|
138
|
-
const getStateRender = useCallback(
|
|
139
|
-
(messageId: string) => {
|
|
140
|
-
return Object.entries(coAgentStateRenders).find(
|
|
141
|
-
([stateRenderId, stateRender]) => {
|
|
142
|
-
if (claimsRef.current[messageId]) {
|
|
143
|
-
return stateRenderId === claimsRef.current[messageId].stateRenderId;
|
|
144
|
-
}
|
|
145
|
-
const matchingAgentName = stateRender.name === agentId;
|
|
146
|
-
const matchesNodeContext = stateRender.nodeName
|
|
147
|
-
? stateRender.nodeName === nodeName
|
|
148
|
-
: true;
|
|
149
|
-
return matchingAgentName && matchesNodeContext;
|
|
150
|
-
},
|
|
151
|
-
);
|
|
152
|
-
},
|
|
153
|
-
[coAgentStateRenders, nodeName, agentId],
|
|
154
|
-
);
|
|
155
|
-
const stateRenderEntry = useMemo(
|
|
156
|
-
() => getStateRender(message.id),
|
|
157
|
-
[getStateRender, message.id],
|
|
158
|
-
);
|
|
159
|
-
const stateRenderId = stateRenderEntry?.[0];
|
|
160
|
-
const stateRender = stateRenderEntry?.[1];
|
|
161
|
-
|
|
162
|
-
const registryMessage = {
|
|
163
|
-
...message,
|
|
164
|
-
runId: props.runId ?? message.runId,
|
|
165
|
-
};
|
|
166
|
-
const { canRender } = useStateRenderRegistry({
|
|
167
|
-
agentId,
|
|
168
|
-
stateRenderId,
|
|
169
|
-
message: registryMessage,
|
|
170
|
-
messageIndex: props.messageIndex,
|
|
171
|
-
stateSnapshot,
|
|
172
|
-
agentState: agent?.state,
|
|
173
|
-
agentMessages: agent?.messages,
|
|
174
|
-
claimsRef,
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
return useMemo(() => {
|
|
178
|
-
if (!stateRender || !stateRenderId) {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
if (!canRender) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (stateRender.handler) {
|
|
186
|
-
stateRender.handler({
|
|
187
|
-
state: stateSnapshot
|
|
188
|
-
? parseJson(stateSnapshot, stateSnapshot)
|
|
189
|
-
: (agent?.state ?? {}),
|
|
190
|
-
nodeName: nodeName ?? "",
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (stateRender.render) {
|
|
195
|
-
const status = agent?.isRunning
|
|
196
|
-
? RenderStatus.InProgress
|
|
197
|
-
: RenderStatus.Complete;
|
|
198
|
-
|
|
199
|
-
if (typeof stateRender.render === "string") return stateRender.render;
|
|
200
|
-
|
|
201
|
-
return stateRender.render({
|
|
202
|
-
status,
|
|
203
|
-
// Always use state from claim, to make sure the state does not seem "wiped" for a fraction of a second
|
|
204
|
-
state: claimsRef.current[message.id].stateSnapshot ?? {},
|
|
205
|
-
nodeName: nodeName ?? "",
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}, [
|
|
209
|
-
stateRender,
|
|
210
|
-
stateRenderId,
|
|
211
|
-
agent?.state,
|
|
212
|
-
agent?.isRunning,
|
|
213
|
-
nodeName,
|
|
214
|
-
message.id,
|
|
215
|
-
stateSnapshot,
|
|
216
|
-
canRender,
|
|
217
|
-
]);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function CoAgentStateRenderBridge(props: CoAgentStateRenderBridgeProps) {
|
|
221
|
-
return useCoagentStateRenderBridge(props.agentId, props);
|
|
222
|
-
}
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { useEffect } from "react";
|
|
2
|
-
import {
|
|
3
|
-
areStatesEquals,
|
|
4
|
-
ClaimAction,
|
|
5
|
-
getEffectiveRunId,
|
|
6
|
-
isPlaceholderMessageId,
|
|
7
|
-
isPlaceholderMessageName,
|
|
8
|
-
readCachedMessageEntry,
|
|
9
|
-
resolveClaim,
|
|
10
|
-
selectSnapshot,
|
|
11
|
-
type Claim,
|
|
12
|
-
type ClaimsByMessageId,
|
|
13
|
-
type SnapshotCaches,
|
|
14
|
-
type StateRenderContext,
|
|
15
|
-
} from "./use-coagent-state-render-bridge.helpers";
|
|
16
|
-
|
|
17
|
-
export interface StateRenderRegistryInput {
|
|
18
|
-
agentId: string;
|
|
19
|
-
stateRenderId?: string;
|
|
20
|
-
message: { id: string; runId?: string; name?: string };
|
|
21
|
-
messageIndex?: number;
|
|
22
|
-
stateSnapshot?: any;
|
|
23
|
-
agentState?: any;
|
|
24
|
-
agentMessages?: Array<{ id: string; role?: string }>;
|
|
25
|
-
claimsRef: React.MutableRefObject<Record<string, Claim>>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface StateRenderRegistryResult {
|
|
29
|
-
canRender: boolean;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const LAST_SNAPSHOTS_BY_RENDER_AND_RUN = "__lastSnapshotsByStateRenderIdAndRun";
|
|
33
|
-
const LAST_SNAPSHOTS_BY_MESSAGE = "__lastSnapshotsByMessageId";
|
|
34
|
-
|
|
35
|
-
type SnapshotByMessageEntry = { snapshot: any; runId?: string } | any;
|
|
36
|
-
type ClaimsStore = Record<string, Claim> & {
|
|
37
|
-
[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]?: Record<string, any>;
|
|
38
|
-
[LAST_SNAPSHOTS_BY_MESSAGE]?: Record<string, SnapshotByMessageEntry>;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
function getClaimsStore(
|
|
42
|
-
claimsRef: React.MutableRefObject<Record<string, Claim>>,
|
|
43
|
-
): ClaimsStore {
|
|
44
|
-
return claimsRef.current as ClaimsStore;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function getSnapshotCaches(
|
|
48
|
-
claimsRef: React.MutableRefObject<Record<string, Claim>>,
|
|
49
|
-
): SnapshotCaches {
|
|
50
|
-
const store = getClaimsStore(claimsRef);
|
|
51
|
-
return {
|
|
52
|
-
byStateRenderAndRun: store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] ?? {},
|
|
53
|
-
byMessageId: store[LAST_SNAPSHOTS_BY_MESSAGE] ?? {},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function useStateRenderRegistry({
|
|
58
|
-
agentId,
|
|
59
|
-
stateRenderId,
|
|
60
|
-
message,
|
|
61
|
-
messageIndex,
|
|
62
|
-
stateSnapshot,
|
|
63
|
-
agentState,
|
|
64
|
-
agentMessages,
|
|
65
|
-
claimsRef,
|
|
66
|
-
}: StateRenderRegistryInput): StateRenderRegistryResult {
|
|
67
|
-
const store = getClaimsStore(claimsRef);
|
|
68
|
-
const runId = message.runId;
|
|
69
|
-
const cachedMessageEntry = store[LAST_SNAPSHOTS_BY_MESSAGE]?.[message.id];
|
|
70
|
-
const { runId: cachedMessageRunId } =
|
|
71
|
-
readCachedMessageEntry(cachedMessageEntry);
|
|
72
|
-
const existingClaimRunId = claimsRef.current[message.id]?.runId;
|
|
73
|
-
const effectiveRunId = getEffectiveRunId({
|
|
74
|
-
existingClaimRunId,
|
|
75
|
-
cachedMessageRunId,
|
|
76
|
-
runId,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
useEffect(() => {
|
|
80
|
-
return () => {
|
|
81
|
-
const existingClaim = claimsRef.current[message.id];
|
|
82
|
-
if (
|
|
83
|
-
existingClaim?.stateSnapshot &&
|
|
84
|
-
Object.keys(existingClaim.stateSnapshot).length > 0
|
|
85
|
-
) {
|
|
86
|
-
const snapshotCache = {
|
|
87
|
-
...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
|
|
88
|
-
};
|
|
89
|
-
const cacheKey = `${existingClaim.stateRenderId}::${existingClaim.runId ?? "pending"}`;
|
|
90
|
-
snapshotCache[cacheKey] = existingClaim.stateSnapshot;
|
|
91
|
-
snapshotCache[`${existingClaim.stateRenderId}::latest`] =
|
|
92
|
-
existingClaim.stateSnapshot;
|
|
93
|
-
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
94
|
-
|
|
95
|
-
const messageCache = {
|
|
96
|
-
...store[LAST_SNAPSHOTS_BY_MESSAGE],
|
|
97
|
-
};
|
|
98
|
-
messageCache[message.id] = {
|
|
99
|
-
snapshot: existingClaim.stateSnapshot,
|
|
100
|
-
runId: existingClaim.runId ?? effectiveRunId,
|
|
101
|
-
};
|
|
102
|
-
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
103
|
-
}
|
|
104
|
-
delete claimsRef.current[message.id];
|
|
105
|
-
};
|
|
106
|
-
}, [claimsRef, effectiveRunId, message.id]);
|
|
107
|
-
|
|
108
|
-
if (!stateRenderId) {
|
|
109
|
-
return { canRender: false };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const caches = getSnapshotCaches(claimsRef);
|
|
113
|
-
const existingClaim = claimsRef.current[message.id] as Claim | undefined;
|
|
114
|
-
|
|
115
|
-
const { snapshot, hasSnapshotKeys, allowEmptySnapshot, snapshotForClaim } =
|
|
116
|
-
selectSnapshot({
|
|
117
|
-
messageId: message.id,
|
|
118
|
-
messageName: message.name,
|
|
119
|
-
allowLiveState:
|
|
120
|
-
isPlaceholderMessageName(message.name) ||
|
|
121
|
-
isPlaceholderMessageId(message.id),
|
|
122
|
-
skipLatestCache:
|
|
123
|
-
isPlaceholderMessageName(message.name) ||
|
|
124
|
-
isPlaceholderMessageId(message.id),
|
|
125
|
-
stateRenderId,
|
|
126
|
-
effectiveRunId,
|
|
127
|
-
stateSnapshotProp: stateSnapshot,
|
|
128
|
-
agentState,
|
|
129
|
-
agentMessages,
|
|
130
|
-
existingClaim,
|
|
131
|
-
caches,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
const resolution = resolveClaim({
|
|
135
|
-
claims: claimsRef.current as ClaimsByMessageId,
|
|
136
|
-
context: {
|
|
137
|
-
agentId,
|
|
138
|
-
messageId: message.id,
|
|
139
|
-
stateRenderId,
|
|
140
|
-
runId: effectiveRunId,
|
|
141
|
-
messageIndex,
|
|
142
|
-
} satisfies StateRenderContext,
|
|
143
|
-
stateSnapshot: snapshotForClaim,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
if (resolution.action === ClaimAction.Block) {
|
|
147
|
-
return { canRender: false };
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (resolution.updateRunId && claimsRef.current[message.id]) {
|
|
151
|
-
claimsRef.current[message.id].runId = resolution.updateRunId;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (resolution.nextClaim) {
|
|
155
|
-
claimsRef.current[message.id] = resolution.nextClaim;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (resolution.lockOthers) {
|
|
159
|
-
Object.entries(claimsRef.current).forEach(([id, claim]) => {
|
|
160
|
-
if (id !== message.id && claim.stateRenderId === stateRenderId) {
|
|
161
|
-
claim.locked = true;
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (existingClaim && !existingClaim.locked && agentMessages?.length) {
|
|
167
|
-
const indexInAgentMessages = agentMessages.findIndex(
|
|
168
|
-
(msg: any) => msg.id === message.id,
|
|
169
|
-
);
|
|
170
|
-
if (
|
|
171
|
-
indexInAgentMessages >= 0 &&
|
|
172
|
-
indexInAgentMessages < agentMessages.length - 1
|
|
173
|
-
) {
|
|
174
|
-
existingClaim.locked = true;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
|
|
179
|
-
const snapshotChanged =
|
|
180
|
-
stateSnapshot &&
|
|
181
|
-
existingSnapshot !== undefined &&
|
|
182
|
-
!areStatesEquals(existingSnapshot, snapshot);
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
snapshot &&
|
|
186
|
-
(stateSnapshot || hasSnapshotKeys || allowEmptySnapshot) &&
|
|
187
|
-
(!claimsRef.current[message.id].locked || snapshotChanged)
|
|
188
|
-
) {
|
|
189
|
-
if (!claimsRef.current[message.id].locked || snapshotChanged) {
|
|
190
|
-
claimsRef.current[message.id].stateSnapshot = snapshot;
|
|
191
|
-
const snapshotCache = {
|
|
192
|
-
...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
|
|
193
|
-
};
|
|
194
|
-
const cacheKey = `${stateRenderId}::${effectiveRunId}`;
|
|
195
|
-
snapshotCache[cacheKey] = snapshot;
|
|
196
|
-
snapshotCache[`${stateRenderId}::latest`] = snapshot;
|
|
197
|
-
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
198
|
-
const messageCache = {
|
|
199
|
-
...store[LAST_SNAPSHOTS_BY_MESSAGE],
|
|
200
|
-
};
|
|
201
|
-
messageCache[message.id] = { snapshot, runId: effectiveRunId };
|
|
202
|
-
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
203
|
-
if (stateSnapshot) {
|
|
204
|
-
claimsRef.current[message.id].locked = true;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
} else if (snapshotForClaim) {
|
|
208
|
-
const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
|
|
209
|
-
if (!existingSnapshot) {
|
|
210
|
-
claimsRef.current[message.id].stateSnapshot = snapshotForClaim;
|
|
211
|
-
const snapshotCache = {
|
|
212
|
-
...store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN],
|
|
213
|
-
};
|
|
214
|
-
const cacheKey = `${stateRenderId}::${effectiveRunId}`;
|
|
215
|
-
snapshotCache[cacheKey] = snapshotForClaim;
|
|
216
|
-
snapshotCache[`${stateRenderId}::latest`] = snapshotForClaim;
|
|
217
|
-
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
218
|
-
const messageCache = {
|
|
219
|
-
...store[LAST_SNAPSHOTS_BY_MESSAGE],
|
|
220
|
-
};
|
|
221
|
-
messageCache[message.id] = {
|
|
222
|
-
snapshot: snapshotForClaim,
|
|
223
|
-
runId: effectiveRunId,
|
|
224
|
-
};
|
|
225
|
-
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return { canRender: true };
|
|
230
|
-
}
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The useCoAgentStateRender hook allows you to render UI or text based components on a Agentic Copilot's state in the chat.
|
|
3
|
-
* This is particularly useful for showing intermediate state or progress during Agentic Copilot operations.
|
|
4
|
-
*
|
|
5
|
-
* ## Usage
|
|
6
|
-
*
|
|
7
|
-
* ### Simple Usage
|
|
8
|
-
*
|
|
9
|
-
* ```tsx
|
|
10
|
-
* import { useCoAgentStateRender } from "@copilotkit/react-core";
|
|
11
|
-
*
|
|
12
|
-
* type YourAgentState = {
|
|
13
|
-
* agent_state_property: string;
|
|
14
|
-
* }
|
|
15
|
-
*
|
|
16
|
-
* useCoAgentStateRender<YourAgentState>({
|
|
17
|
-
* name: "basic_agent",
|
|
18
|
-
* nodeName: "optionally_specify_a_specific_node",
|
|
19
|
-
* render: ({ status, state, nodeName }) => {
|
|
20
|
-
* return (
|
|
21
|
-
* <YourComponent
|
|
22
|
-
* agentStateProperty={state.agent_state_property}
|
|
23
|
-
* status={status}
|
|
24
|
-
* nodeName={nodeName}
|
|
25
|
-
* />
|
|
26
|
-
* );
|
|
27
|
-
* },
|
|
28
|
-
* });
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* This allows for you to render UI components or text based on what is happening within the agent.
|
|
32
|
-
*
|
|
33
|
-
* ### Example
|
|
34
|
-
* A great example of this is in our Perplexity Clone where we render the progress of an agent's internet search as it is happening.
|
|
35
|
-
* You can play around with it below or learn how to build it with its [demo](/coagents/videos/perplexity-clone).
|
|
36
|
-
*
|
|
37
|
-
* <Callout type="info">
|
|
38
|
-
* This example is hosted on Vercel and may take a few seconds to load.
|
|
39
|
-
* </Callout>
|
|
40
|
-
*
|
|
41
|
-
* <iframe src="https://examples-coagents-ai-researcher-ui.vercel.app/" className="w-full rounded-lg border h-[700px] my-4" />
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
import { useRef, useContext, useEffect } from "react";
|
|
45
|
-
import { CopilotContext } from "../context/copilot-context";
|
|
46
|
-
import { randomId, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
|
|
47
|
-
import { CoAgentStateRender } from "../types/coagent-action";
|
|
48
|
-
import { useToast } from "../components/toast/toast-provider";
|
|
49
|
-
import { useCoAgentStateRenders } from "../context/coagent-state-renders-context";
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* This hook is used to render agent state with custom UI components or text. This is particularly
|
|
53
|
-
* useful for showing intermediate state or progress during Agentic Copilot operations.
|
|
54
|
-
* To get started using rendering intermediate state through this hook, checkout the documentation.
|
|
55
|
-
*
|
|
56
|
-
* https://docs.copilotkit.ai/coagents/shared-state/predictive-state-updates
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
// We implement useCoAgentStateRender dependency handling so that
|
|
60
|
-
// the developer has the option to not provide any dependencies.
|
|
61
|
-
// see useCopilotAction for more details about this approach.
|
|
62
|
-
export function useCoAgentStateRender<T = any>(
|
|
63
|
-
action: CoAgentStateRender<T>,
|
|
64
|
-
dependencies?: any[],
|
|
65
|
-
): void {
|
|
66
|
-
const { chatComponentsCache, availableAgents } = useContext(CopilotContext);
|
|
67
|
-
const {
|
|
68
|
-
setCoAgentStateRender,
|
|
69
|
-
removeCoAgentStateRender,
|
|
70
|
-
coAgentStateRenders,
|
|
71
|
-
} = useCoAgentStateRenders();
|
|
72
|
-
const idRef = useRef<string>(randomId());
|
|
73
|
-
const { setBannerError, addToast } = useToast();
|
|
74
|
-
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
if (
|
|
77
|
-
availableAgents?.length &&
|
|
78
|
-
!availableAgents.some((a) => a.name === action.name)
|
|
79
|
-
) {
|
|
80
|
-
const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
|
|
81
|
-
|
|
82
|
-
// Route to banner instead of toast for consistency
|
|
83
|
-
const agentError = new CopilotKitAgentDiscoveryError({
|
|
84
|
-
agentName: action.name,
|
|
85
|
-
availableAgents: availableAgents.map((a) => ({
|
|
86
|
-
name: a.name,
|
|
87
|
-
id: a.id,
|
|
88
|
-
})),
|
|
89
|
-
});
|
|
90
|
-
setBannerError(agentError);
|
|
91
|
-
}
|
|
92
|
-
}, [availableAgents]);
|
|
93
|
-
|
|
94
|
-
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
95
|
-
|
|
96
|
-
if (dependencies === undefined) {
|
|
97
|
-
if (coAgentStateRenders[idRef.current]) {
|
|
98
|
-
coAgentStateRenders[idRef.current].handler = action.handler as any;
|
|
99
|
-
if (typeof action.render === "function") {
|
|
100
|
-
if (chatComponentsCache.current !== null) {
|
|
101
|
-
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
useEffect(() => {
|
|
108
|
-
// Check for duplicates by comparing against all other actions
|
|
109
|
-
const currentId = idRef.current;
|
|
110
|
-
const hasDuplicate = Object.entries(coAgentStateRenders).some(
|
|
111
|
-
([id, otherAction]) => {
|
|
112
|
-
// Skip comparing with self
|
|
113
|
-
if (id === currentId) return false;
|
|
114
|
-
|
|
115
|
-
// Different agent names are never duplicates
|
|
116
|
-
if (otherAction.name !== action.name) return false;
|
|
117
|
-
|
|
118
|
-
// Same agent names:
|
|
119
|
-
const hasNodeName = !!action.nodeName;
|
|
120
|
-
const hasOtherNodeName = !!otherAction.nodeName;
|
|
121
|
-
|
|
122
|
-
// If neither has nodeName, they're duplicates
|
|
123
|
-
if (!hasNodeName && !hasOtherNodeName) return true;
|
|
124
|
-
|
|
125
|
-
// If one has nodeName and other doesn't, they're not duplicates
|
|
126
|
-
if (hasNodeName !== hasOtherNodeName) return false;
|
|
127
|
-
|
|
128
|
-
// If both have nodeName, they're duplicates only if the names match
|
|
129
|
-
return action.nodeName === otherAction.nodeName;
|
|
130
|
-
},
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
if (hasDuplicate) {
|
|
134
|
-
const message = action.nodeName
|
|
135
|
-
? `Found multiple state renders for agent ${action.name} and node ${action.nodeName}. State renders might get overridden`
|
|
136
|
-
: `Found multiple state renders for agent ${action.name}. State renders might get overridden`;
|
|
137
|
-
|
|
138
|
-
addToast({
|
|
139
|
-
type: "warning",
|
|
140
|
-
message,
|
|
141
|
-
id: `dup-action-${action.name}`,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}, [coAgentStateRenders]);
|
|
145
|
-
|
|
146
|
-
useEffect(() => {
|
|
147
|
-
setCoAgentStateRender(idRef.current, action as any);
|
|
148
|
-
if (chatComponentsCache.current !== null && action.render !== undefined) {
|
|
149
|
-
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
150
|
-
}
|
|
151
|
-
return () => {
|
|
152
|
-
removeCoAgentStateRender(idRef.current);
|
|
153
|
-
};
|
|
154
|
-
}, [
|
|
155
|
-
setCoAgentStateRender,
|
|
156
|
-
removeCoAgentStateRender,
|
|
157
|
-
action.name,
|
|
158
|
-
// include render only if it's a string
|
|
159
|
-
typeof action.render === "string" ? action.render : undefined,
|
|
160
|
-
// dependencies set by the developer
|
|
161
|
-
...(dependencies || []),
|
|
162
|
-
]);
|
|
163
|
-
}
|