@copilotkit/react-core 1.54.1 → 1.55.0-next.8
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 +127 -116
- package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
- package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
- package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
- package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
- package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
- package/dist/index.cjs +27 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1941 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +77 -7
- package/dist/v2/index.css +1 -2
- package/dist/v2/index.d.cts +6 -4
- package/dist/v2/index.d.mts +6 -4
- package/dist/v2/index.mjs +7 -4
- package/dist/v2/index.umd.js +5725 -24
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +37 -9
- package/scripts/scope-preflight.mjs +101 -0
- package/src/components/CopilotListeners.tsx +2 -6
- package/src/components/copilot-provider/copilot-messages.tsx +1 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
- package/src/components/copilot-provider/copilotkit.tsx +4 -4
- package/src/context/copilot-messages-context.tsx +1 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
- package/src/hooks/use-agent-nodename.ts +1 -1
- package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat_internal.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +1 -1
- package/src/hooks/use-frontend-tool.ts +2 -2
- package/src/hooks/use-human-in-the-loop.ts +2 -2
- package/src/hooks/use-langgraph-interrupt.ts +2 -5
- package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
- package/src/hooks/use-render-tool-call.ts +1 -1
- package/src/lib/copilot-task.ts +1 -1
- package/src/setupTests.ts +18 -14
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
- package/src/v2/__tests__/globalSetup.ts +14 -0
- package/src/v2/__tests__/setup.ts +93 -0
- package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
- package/src/v2/components/CopilotKitInspector.tsx +50 -0
- package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
- package/src/v2/components/WildcardToolCallRender.tsx +86 -0
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
- package/src/v2/components/chat/CopilotChat.tsx +431 -0
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
- package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
- package/src/v2/components/chat/CopilotChatView.tsx +598 -0
- package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
- package/src/v2/components/chat/CopilotPopup.tsx +81 -0
- package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
- package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
- package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
- package/src/v2/components/chat/__tests__/setup.ts +1 -0
- package/src/v2/components/chat/index.ts +79 -0
- package/src/v2/components/index.ts +7 -0
- package/src/v2/components/license-warning-banner.tsx +198 -0
- package/src/v2/components/ui/button.tsx +123 -0
- package/src/v2/components/ui/dropdown-menu.tsx +258 -0
- package/src/v2/components/ui/tooltip.tsx +60 -0
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
- package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
- package/src/v2/hooks/index.ts +18 -0
- package/src/v2/hooks/use-agent-context.tsx +45 -0
- package/src/v2/hooks/use-agent.tsx +155 -0
- package/src/v2/hooks/use-component.tsx +89 -0
- package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
- package/src/v2/hooks/use-default-render-tool.tsx +254 -0
- package/src/v2/hooks/use-frontend-tool.tsx +43 -0
- package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
- package/src/v2/hooks/use-interrupt.tsx +305 -0
- package/src/v2/hooks/use-keyboard-height.tsx +67 -0
- package/src/v2/hooks/use-render-activity-message.tsx +73 -0
- package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
- package/src/v2/hooks/use-render-tool-call.tsx +175 -0
- package/src/v2/hooks/use-render-tool.tsx +181 -0
- package/src/v2/hooks/use-suggestions.tsx +91 -0
- package/src/v2/hooks/use-threads.tsx +256 -0
- package/src/v2/hooks/useKatexStyles.ts +27 -0
- package/src/v2/index.css +1 -1
- package/src/v2/index.ts +18 -2
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
- package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
- package/src/v2/lib/react-core.ts +156 -0
- package/src/v2/lib/slots.tsx +143 -0
- package/src/v2/lib/transcription-client.ts +184 -0
- package/src/v2/lib/utils.ts +8 -0
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
- package/src/v2/providers/CopilotKitProvider.tsx +600 -0
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
- package/src/v2/providers/index.ts +14 -0
- package/src/v2/styles/globals.css +230 -0
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
- package/src/v2/types/defineToolCallRenderer.ts +65 -0
- package/src/v2/types/frontend-tool.ts +8 -0
- package/src/v2/types/human-in-the-loop.ts +33 -0
- package/src/v2/types/index.ts +7 -0
- package/src/v2/types/interrupt.ts +15 -0
- package/src/v2/types/react-activity-message-renderer.ts +27 -0
- package/src/v2/types/react-custom-message-renderer.ts +17 -0
- package/src/v2/types/react-tool-call-renderer.ts +32 -0
- package/tsdown.config.ts +34 -10
- package/vitest.config.mjs +4 -3
- package/LICENSE +0 -21
- package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
- package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
- package/dist/copilotkit-C94ayZbs.cjs +0 -2161
- package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
- package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
- package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
- package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
- package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
- package/dist/v2/index.css.map +0 -1
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useCallback,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
} from "react";
|
|
8
|
+
import { useCopilotKit } from "../providers/CopilotKitProvider";
|
|
9
|
+
import { useAgent } from "./use-agent";
|
|
10
|
+
import type {
|
|
11
|
+
InterruptEvent,
|
|
12
|
+
InterruptRenderProps,
|
|
13
|
+
InterruptHandlerProps,
|
|
14
|
+
} from "../types/interrupt";
|
|
15
|
+
|
|
16
|
+
export type { InterruptEvent, InterruptRenderProps, InterruptHandlerProps };
|
|
17
|
+
|
|
18
|
+
const INTERRUPT_EVENT_NAME = "on_interrupt";
|
|
19
|
+
|
|
20
|
+
type InterruptHandlerFn<TValue, TResult> = (
|
|
21
|
+
props: InterruptHandlerProps<TValue>,
|
|
22
|
+
) => TResult | PromiseLike<TResult>;
|
|
23
|
+
|
|
24
|
+
type InterruptResultFromHandler<THandler> = THandler extends (
|
|
25
|
+
...args: never[]
|
|
26
|
+
) => infer TResult
|
|
27
|
+
? TResult extends PromiseLike<infer TResolved>
|
|
28
|
+
? TResolved | null
|
|
29
|
+
: TResult | null
|
|
30
|
+
: null;
|
|
31
|
+
|
|
32
|
+
type InterruptResult<TValue, TResult> = InterruptResultFromHandler<
|
|
33
|
+
InterruptHandlerFn<TValue, TResult>
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
type InterruptRenderInChat = boolean | undefined;
|
|
37
|
+
|
|
38
|
+
type UseInterruptReturn<TRenderInChat extends InterruptRenderInChat> =
|
|
39
|
+
TRenderInChat extends false
|
|
40
|
+
? React.ReactElement | null
|
|
41
|
+
: TRenderInChat extends true | undefined
|
|
42
|
+
? void
|
|
43
|
+
: React.ReactElement | null | void;
|
|
44
|
+
|
|
45
|
+
export function isPromiseLike<TValue>(
|
|
46
|
+
value: TValue | PromiseLike<TValue>,
|
|
47
|
+
): value is PromiseLike<TValue> {
|
|
48
|
+
return (
|
|
49
|
+
(typeof value === "object" || typeof value === "function") &&
|
|
50
|
+
value !== null &&
|
|
51
|
+
typeof Reflect.get(value, "then") === "function"
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configuration options for `useInterrupt`.
|
|
57
|
+
*/
|
|
58
|
+
interface UseInterruptConfigBase<TValue = unknown, TResult = never> {
|
|
59
|
+
/**
|
|
60
|
+
* Render function for the interrupt UI.
|
|
61
|
+
*
|
|
62
|
+
* This is called once an interrupt is finalized and accepted by `enabled` (if provided).
|
|
63
|
+
* Use `resolve` from render props to resume the agent run with user input.
|
|
64
|
+
*/
|
|
65
|
+
render: (
|
|
66
|
+
props: InterruptRenderProps<TValue, InterruptResult<TValue, TResult>>,
|
|
67
|
+
) => React.ReactElement;
|
|
68
|
+
/**
|
|
69
|
+
* Optional pre-render handler invoked when an interrupt is received.
|
|
70
|
+
*
|
|
71
|
+
* Return either a sync value or an async value to pass into `render` as `result`.
|
|
72
|
+
* Rejecting/throwing falls back to `result = null`.
|
|
73
|
+
*/
|
|
74
|
+
handler?: InterruptHandlerFn<TValue, TResult>;
|
|
75
|
+
/**
|
|
76
|
+
* Optional predicate to filter which interrupts should be handled by this hook.
|
|
77
|
+
* Return `false` to ignore an interrupt.
|
|
78
|
+
*/
|
|
79
|
+
enabled?: (event: InterruptEvent<TValue>) => boolean;
|
|
80
|
+
/** Optional agent id. Defaults to the current configured chat agent. */
|
|
81
|
+
agentId?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface UseInterruptInChatConfig<
|
|
85
|
+
TValue = unknown,
|
|
86
|
+
TResult = never,
|
|
87
|
+
> extends UseInterruptConfigBase<TValue, TResult> {
|
|
88
|
+
/** When true (default), the interrupt UI renders inside `<CopilotChat>` automatically. Set to false to render it yourself. */
|
|
89
|
+
renderInChat?: true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface UseInterruptExternalConfig<
|
|
93
|
+
TValue = unknown,
|
|
94
|
+
TResult = never,
|
|
95
|
+
> extends UseInterruptConfigBase<TValue, TResult> {
|
|
96
|
+
/** When true (default), the interrupt UI renders inside `<CopilotChat>` automatically. Set to false to render it yourself. */
|
|
97
|
+
renderInChat: false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface UseInterruptDynamicConfig<
|
|
101
|
+
TValue = unknown,
|
|
102
|
+
TResult = never,
|
|
103
|
+
> extends UseInterruptConfigBase<TValue, TResult> {
|
|
104
|
+
/** Dynamic boolean mode. When non-literal, return type is a union. */
|
|
105
|
+
renderInChat: boolean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type UseInterruptConfig<
|
|
109
|
+
TValue = unknown,
|
|
110
|
+
TResult = never,
|
|
111
|
+
TRenderInChat extends InterruptRenderInChat = undefined,
|
|
112
|
+
> = UseInterruptConfigBase<TValue, TResult> & {
|
|
113
|
+
/** When true (default), the interrupt UI renders inside `<CopilotChat>` automatically. Set to false to render it yourself. */
|
|
114
|
+
renderInChat?: TRenderInChat;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Handles agent interrupts (`on_interrupt`) with optional filtering, preprocessing, and resume behavior.
|
|
119
|
+
*
|
|
120
|
+
* The hook listens to custom events on the active agent, stores interrupt payloads per run,
|
|
121
|
+
* and surfaces a render callback once the run finalizes. Call `resolve` from your UI to resume
|
|
122
|
+
* execution with user-provided data.
|
|
123
|
+
*
|
|
124
|
+
* - `renderInChat: true` (default): the element is published into `<CopilotChat>` and this hook returns `void`.
|
|
125
|
+
* - `renderInChat: false`: the hook returns the interrupt element so you can place it anywhere in your component tree.
|
|
126
|
+
*
|
|
127
|
+
* `event.value` is typed as `any` since the interrupt payload shape depends on your agent.
|
|
128
|
+
* Type-narrow it in your callbacks (e.g. `handler`, `enabled`, `render`) as needed.
|
|
129
|
+
*
|
|
130
|
+
* @typeParam TResult - Inferred from `handler` return type. Exposed as `result` in `render`.
|
|
131
|
+
* @param config - Interrupt configuration (renderer, optional handler/filter, and render mode).
|
|
132
|
+
* @returns When `renderInChat` is `false`, returns the interrupt element (or `null` when idle).
|
|
133
|
+
* Otherwise returns `void` and publishes the element into chat. In `render`, `result` is always
|
|
134
|
+
* either the handler's resolved return value or `null` (including when no handler is provided,
|
|
135
|
+
* when filtering skips the interrupt, or when handler execution fails).
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```tsx
|
|
139
|
+
* import { useInterrupt } from "@copilotkit/react-core/v2";
|
|
140
|
+
*
|
|
141
|
+
* function InterruptUI() {
|
|
142
|
+
* useInterrupt({
|
|
143
|
+
* render: ({ event, resolve }) => (
|
|
144
|
+
* <div>
|
|
145
|
+
* <p>{event.value.question}</p>
|
|
146
|
+
* <button onClick={() => resolve({ approved: true })}>Approve</button>
|
|
147
|
+
* <button onClick={() => resolve({ approved: false })}>Reject</button>
|
|
148
|
+
* </div>
|
|
149
|
+
* ),
|
|
150
|
+
* });
|
|
151
|
+
*
|
|
152
|
+
* return null;
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```tsx
|
|
158
|
+
* import { useInterrupt } from "@copilotkit/react-core/v2";
|
|
159
|
+
*
|
|
160
|
+
* function CustomPanel() {
|
|
161
|
+
* const interruptElement = useInterrupt({
|
|
162
|
+
* renderInChat: false,
|
|
163
|
+
* enabled: (event) => event.value.startsWith("approval:"),
|
|
164
|
+
* handler: async ({ event }) => ({ label: event.value.toUpperCase() }),
|
|
165
|
+
* render: ({ event, result, resolve }) => (
|
|
166
|
+
* <aside>
|
|
167
|
+
* <strong>{result?.label ?? ""}</strong>
|
|
168
|
+
* <button onClick={() => resolve({ value: event.value })}>Continue</button>
|
|
169
|
+
* </aside>
|
|
170
|
+
* ),
|
|
171
|
+
* });
|
|
172
|
+
*
|
|
173
|
+
* return <>{interruptElement}</>;
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
178
|
+
export function useInterrupt<
|
|
179
|
+
TResult = never,
|
|
180
|
+
TRenderInChat extends InterruptRenderInChat = undefined,
|
|
181
|
+
>(
|
|
182
|
+
config: UseInterruptConfig<any, TResult, TRenderInChat>,
|
|
183
|
+
): UseInterruptReturn<TRenderInChat> {
|
|
184
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
185
|
+
const { copilotkit } = useCopilotKit();
|
|
186
|
+
const { agent } = useAgent({ agentId: config.agentId });
|
|
187
|
+
const [pendingEvent, setPendingEvent] = useState<InterruptEvent | null>(null);
|
|
188
|
+
const pendingEventRef = useRef(pendingEvent);
|
|
189
|
+
pendingEventRef.current = pendingEvent;
|
|
190
|
+
const [handlerResult, setHandlerResult] =
|
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
192
|
+
useState<InterruptResult<any, TResult>>(null);
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
let localInterrupt: InterruptEvent | null = null;
|
|
196
|
+
|
|
197
|
+
const subscription = agent.subscribe({
|
|
198
|
+
onCustomEvent: ({ event }) => {
|
|
199
|
+
if (event.name === INTERRUPT_EVENT_NAME) {
|
|
200
|
+
localInterrupt = { name: event.name, value: event.value };
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
onRunStartedEvent: () => {
|
|
204
|
+
localInterrupt = null;
|
|
205
|
+
setPendingEvent(null);
|
|
206
|
+
},
|
|
207
|
+
onRunFinalized: () => {
|
|
208
|
+
if (localInterrupt) {
|
|
209
|
+
setPendingEvent(localInterrupt);
|
|
210
|
+
localInterrupt = null;
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
onRunFailed: () => {
|
|
214
|
+
localInterrupt = null;
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return () => subscription.unsubscribe();
|
|
219
|
+
}, [agent]);
|
|
220
|
+
|
|
221
|
+
const resolve = useCallback(
|
|
222
|
+
(response: unknown) => {
|
|
223
|
+
setPendingEvent(null);
|
|
224
|
+
copilotkit.runAgent({
|
|
225
|
+
agent,
|
|
226
|
+
forwardedProps: {
|
|
227
|
+
command: {
|
|
228
|
+
resume: response,
|
|
229
|
+
interruptEvent: pendingEventRef.current?.value,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
[agent, copilotkit],
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
useEffect(() => {
|
|
238
|
+
// No interrupt to process — reset any stale handler result from a previous interrupt
|
|
239
|
+
if (!pendingEvent) {
|
|
240
|
+
setHandlerResult(null);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// Interrupt exists but the consumer's filter rejects it — treat as no-op
|
|
244
|
+
if (config.enabled && !config.enabled(pendingEvent)) {
|
|
245
|
+
setHandlerResult(null);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const handler = config.handler;
|
|
249
|
+
// No handler provided — skip straight to rendering with a null result
|
|
250
|
+
if (!handler) {
|
|
251
|
+
setHandlerResult(null);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let cancelled = false;
|
|
256
|
+
const maybePromise = handler({
|
|
257
|
+
event: pendingEvent,
|
|
258
|
+
resolve,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// If the handler returns a promise/thenable, wait for resolution before setting result.
|
|
262
|
+
if (isPromiseLike(maybePromise)) {
|
|
263
|
+
Promise.resolve(maybePromise)
|
|
264
|
+
.then((resolved) => {
|
|
265
|
+
if (!cancelled) setHandlerResult(resolved);
|
|
266
|
+
})
|
|
267
|
+
.catch(() => {
|
|
268
|
+
if (!cancelled) setHandlerResult(null);
|
|
269
|
+
});
|
|
270
|
+
} else {
|
|
271
|
+
setHandlerResult(maybePromise);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return () => {
|
|
275
|
+
cancelled = true;
|
|
276
|
+
};
|
|
277
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
278
|
+
}, [pendingEvent, config.enabled, config.handler, resolve]);
|
|
279
|
+
|
|
280
|
+
const element = useMemo(() => {
|
|
281
|
+
if (!pendingEvent) return null;
|
|
282
|
+
if (config.enabled && !config.enabled(pendingEvent)) return null;
|
|
283
|
+
|
|
284
|
+
return config.render({
|
|
285
|
+
event: pendingEvent,
|
|
286
|
+
result: handlerResult,
|
|
287
|
+
resolve,
|
|
288
|
+
});
|
|
289
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
290
|
+
}, [pendingEvent, handlerResult, config.enabled, config.render, resolve]);
|
|
291
|
+
|
|
292
|
+
// Publish to core for in-chat rendering
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
if (config.renderInChat === false) return;
|
|
295
|
+
copilotkit.setInterruptElement(element);
|
|
296
|
+
return () => copilotkit.setInterruptElement(null);
|
|
297
|
+
}, [element, config.renderInChat, copilotkit]);
|
|
298
|
+
|
|
299
|
+
// Only return element when rendering outside chat
|
|
300
|
+
if (config.renderInChat === false) {
|
|
301
|
+
return element as UseInterruptReturn<TRenderInChat>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return undefined as UseInterruptReturn<TRenderInChat>;
|
|
305
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
export interface KeyboardState {
|
|
4
|
+
isKeyboardOpen: boolean;
|
|
5
|
+
keyboardHeight: number;
|
|
6
|
+
availableHeight: number;
|
|
7
|
+
viewportHeight: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Hook to detect mobile keyboard appearance and calculate available viewport height.
|
|
12
|
+
* Uses the Visual Viewport API to track keyboard state on mobile devices.
|
|
13
|
+
*
|
|
14
|
+
* @returns KeyboardState object with keyboard information
|
|
15
|
+
*/
|
|
16
|
+
export function useKeyboardHeight(): KeyboardState {
|
|
17
|
+
const [keyboardState, setKeyboardState] = useState<KeyboardState>({
|
|
18
|
+
isKeyboardOpen: false,
|
|
19
|
+
keyboardHeight: 0,
|
|
20
|
+
availableHeight: typeof window !== "undefined" ? window.innerHeight : 0,
|
|
21
|
+
viewportHeight: typeof window !== "undefined" ? window.innerHeight : 0,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (typeof window === "undefined") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Check if Visual Viewport API is available
|
|
30
|
+
const visualViewport = window.visualViewport;
|
|
31
|
+
if (!visualViewport) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const updateKeyboardState = () => {
|
|
36
|
+
const layoutHeight = window.innerHeight;
|
|
37
|
+
const visualHeight = visualViewport.height;
|
|
38
|
+
|
|
39
|
+
// Calculate keyboard height (difference between layout and visual viewport)
|
|
40
|
+
const keyboardHeight = Math.max(0, layoutHeight - visualHeight);
|
|
41
|
+
|
|
42
|
+
// Keyboard is considered open if the height difference is significant (> 150px)
|
|
43
|
+
const isKeyboardOpen = keyboardHeight > 150;
|
|
44
|
+
|
|
45
|
+
setKeyboardState({
|
|
46
|
+
isKeyboardOpen,
|
|
47
|
+
keyboardHeight,
|
|
48
|
+
availableHeight: visualHeight,
|
|
49
|
+
viewportHeight: layoutHeight,
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Initial state
|
|
54
|
+
updateKeyboardState();
|
|
55
|
+
|
|
56
|
+
// Listen for viewport changes
|
|
57
|
+
visualViewport.addEventListener("resize", updateKeyboardState);
|
|
58
|
+
visualViewport.addEventListener("scroll", updateKeyboardState);
|
|
59
|
+
|
|
60
|
+
return () => {
|
|
61
|
+
visualViewport.removeEventListener("resize", updateKeyboardState);
|
|
62
|
+
visualViewport.removeEventListener("scroll", updateKeyboardState);
|
|
63
|
+
};
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
return keyboardState;
|
|
67
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { ActivityMessage } from "@ag-ui/core";
|
|
2
|
+
import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
|
|
3
|
+
import { useCopilotKit, useCopilotChatConfiguration } from "../providers";
|
|
4
|
+
import { useCallback, useMemo } from "react";
|
|
5
|
+
import { ReactActivityMessageRenderer } from "../types";
|
|
6
|
+
|
|
7
|
+
export function useRenderActivityMessage() {
|
|
8
|
+
const { copilotkit } = useCopilotKit();
|
|
9
|
+
const config = useCopilotChatConfiguration();
|
|
10
|
+
const agentId = config?.agentId ?? DEFAULT_AGENT_ID;
|
|
11
|
+
|
|
12
|
+
const renderers = copilotkit.renderActivityMessages;
|
|
13
|
+
|
|
14
|
+
// Find the renderer for a given activity type
|
|
15
|
+
const findRenderer = useCallback(
|
|
16
|
+
(activityType: string): ReactActivityMessageRenderer<unknown> | null => {
|
|
17
|
+
if (!renderers.length) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const matches = renderers.filter(
|
|
22
|
+
(renderer) => renderer.activityType === activityType,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
matches.find((candidate) => candidate.agentId === agentId) ??
|
|
27
|
+
matches.find((candidate) => candidate.agentId === undefined) ??
|
|
28
|
+
renderers.find((candidate) => candidate.activityType === "*") ??
|
|
29
|
+
null
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
[agentId, renderers],
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const renderActivityMessage = useCallback(
|
|
36
|
+
(message: ActivityMessage): React.ReactElement | null => {
|
|
37
|
+
const renderer = findRenderer(message.activityType);
|
|
38
|
+
|
|
39
|
+
if (!renderer) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const parseResult = renderer.content.safeParse(message.content);
|
|
44
|
+
|
|
45
|
+
if (!parseResult.success) {
|
|
46
|
+
console.warn(
|
|
47
|
+
`Failed to parse content for activity message '${message.activityType}':`,
|
|
48
|
+
parseResult.error,
|
|
49
|
+
);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const Component = renderer.render;
|
|
54
|
+
const agent = copilotkit.getAgent(agentId);
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<Component
|
|
58
|
+
key={message.id}
|
|
59
|
+
activityType={message.activityType}
|
|
60
|
+
content={parseResult.data}
|
|
61
|
+
message={message}
|
|
62
|
+
agent={agent}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
[agentId, copilotkit, findRenderer],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return useMemo(
|
|
70
|
+
() => ({ renderActivityMessage, findRenderer }),
|
|
71
|
+
[renderActivityMessage, findRenderer],
|
|
72
|
+
);
|
|
73
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useCopilotChatConfiguration, useCopilotKit } from "../providers";
|
|
2
|
+
import { ReactCustomMessageRendererPosition } from "../types/react-custom-message-renderer";
|
|
3
|
+
import { Message } from "@ag-ui/core";
|
|
4
|
+
|
|
5
|
+
interface UseRenderCustomMessagesParams {
|
|
6
|
+
message: Message;
|
|
7
|
+
position: ReactCustomMessageRendererPosition;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function useRenderCustomMessages() {
|
|
11
|
+
const { copilotkit } = useCopilotKit();
|
|
12
|
+
const config = useCopilotChatConfiguration();
|
|
13
|
+
|
|
14
|
+
if (!config) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { agentId, threadId } = config;
|
|
19
|
+
|
|
20
|
+
const customMessageRenderers = copilotkit.renderCustomMessages
|
|
21
|
+
.filter(
|
|
22
|
+
(renderer) =>
|
|
23
|
+
renderer.agentId === undefined || renderer.agentId === agentId,
|
|
24
|
+
)
|
|
25
|
+
.sort((a, b) => {
|
|
26
|
+
const aHasAgent = a.agentId !== undefined;
|
|
27
|
+
const bHasAgent = b.agentId !== undefined;
|
|
28
|
+
if (aHasAgent === bHasAgent) return 0;
|
|
29
|
+
return aHasAgent ? -1 : 1;
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return function (params: UseRenderCustomMessagesParams) {
|
|
33
|
+
if (!customMessageRenderers.length) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const { message, position } = params;
|
|
37
|
+
const resolvedRunId =
|
|
38
|
+
copilotkit.getRunIdForMessage(agentId, threadId, message.id) ??
|
|
39
|
+
copilotkit.getRunIdsForThread(agentId, threadId).slice(-1)[0];
|
|
40
|
+
const runId = resolvedRunId ?? `missing-run-id:${message.id}`;
|
|
41
|
+
const agent = copilotkit.getAgent(agentId);
|
|
42
|
+
if (!agent) {
|
|
43
|
+
throw new Error("Agent not found");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const messagesIdsInRun = resolvedRunId
|
|
47
|
+
? agent.messages
|
|
48
|
+
.filter(
|
|
49
|
+
(msg) =>
|
|
50
|
+
copilotkit.getRunIdForMessage(agentId, threadId, msg.id) ===
|
|
51
|
+
resolvedRunId,
|
|
52
|
+
)
|
|
53
|
+
.map((msg) => msg.id)
|
|
54
|
+
: [message.id];
|
|
55
|
+
|
|
56
|
+
const rawMessageIndex = agent.messages.findIndex(
|
|
57
|
+
(msg) => msg.id === message.id,
|
|
58
|
+
);
|
|
59
|
+
const messageIndex = rawMessageIndex >= 0 ? rawMessageIndex : 0;
|
|
60
|
+
const messageIndexInRun = resolvedRunId
|
|
61
|
+
? Math.max(messagesIdsInRun.indexOf(message.id), 0)
|
|
62
|
+
: 0;
|
|
63
|
+
const numberOfMessagesInRun = resolvedRunId ? messagesIdsInRun.length : 1;
|
|
64
|
+
const stateSnapshot = resolvedRunId
|
|
65
|
+
? copilotkit.getStateByRun(agentId, threadId, resolvedRunId)
|
|
66
|
+
: undefined;
|
|
67
|
+
|
|
68
|
+
let result = null;
|
|
69
|
+
for (const renderer of customMessageRenderers) {
|
|
70
|
+
if (!renderer.render) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const Component = renderer.render;
|
|
74
|
+
result = (
|
|
75
|
+
<Component
|
|
76
|
+
key={`${runId}-${message.id}-${position}`}
|
|
77
|
+
message={message}
|
|
78
|
+
position={position}
|
|
79
|
+
runId={runId}
|
|
80
|
+
messageIndex={messageIndex}
|
|
81
|
+
messageIndexInRun={messageIndexInRun}
|
|
82
|
+
numberOfMessagesInRun={numberOfMessagesInRun}
|
|
83
|
+
agentId={agentId}
|
|
84
|
+
stateSnapshot={stateSnapshot}
|
|
85
|
+
/>
|
|
86
|
+
);
|
|
87
|
+
if (result) {
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
93
|
+
}
|