@copilotkit/react-core 0.0.0-main-20260406172622 → 0.0.0-main-20260407183406
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 +21 -6
- package/dist/{copilotkit-BYNyIuE7.d.cts → copilotkit-B_nbOmBS.d.cts} +96 -13
- package/dist/copilotkit-B_nbOmBS.d.cts.map +1 -0
- package/dist/{copilotkit-C8yWo1ap.mjs → copilotkit-CF9UzB5N.mjs} +1005 -222
- package/dist/copilotkit-CF9UzB5N.mjs.map +1 -0
- package/dist/{copilotkit-Cs3FSw_2.cjs → copilotkit-CNuaM2pw.cjs} +1021 -220
- package/dist/copilotkit-CNuaM2pw.cjs.map +1 -0
- package/dist/{copilotkit-rBCfdqIo.d.mts → copilotkit-Cs-8EAok.d.mts} +96 -13
- package/dist/copilotkit-Cs-8EAok.d.mts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.umd.js +96 -10
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +4 -1
- package/dist/v2/index.css +1 -1
- package/dist/v2/index.d.cts +2 -2
- package/dist/v2/index.d.mts +2 -2
- package/dist/v2/index.mjs +2 -2
- package/dist/v2/index.umd.js +1027 -221
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +18 -11
- package/src/v2/__tests__/utils/test-helpers.tsx +64 -0
- package/src/v2/components/chat/CopilotChat.tsx +179 -50
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +17 -2
- package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +481 -0
- package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +139 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1 -1
- package/src/v2/components/chat/CopilotChatMessageView.tsx +253 -149
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +54 -0
- package/src/v2/components/chat/CopilotChatView.tsx +179 -66
- package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +168 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +63 -2
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +1 -1
- package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +268 -0
- package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +249 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +43 -2
- package/src/v2/components/chat/index.ts +9 -0
- package/src/v2/components/chat/scroll-element-context.ts +13 -0
- package/src/v2/hooks/__tests__/use-attachments.test.tsx +169 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +54 -0
- package/src/v2/hooks/index.ts +5 -0
- package/src/v2/hooks/use-attachments.tsx +269 -0
- package/src/v2/hooks/use-render-activity-message.tsx +9 -2
- package/src/v2/hooks/use-threads.tsx +35 -15
- package/src/v2/lib/__tests__/slots.test.ts +56 -0
- package/src/v2/lib/slots.tsx +42 -1
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +9 -3
- package/src/v2/providers/CopilotKitProvider.tsx +14 -3
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +70 -0
- package/dist/copilotkit-BYNyIuE7.d.cts.map +0 -1
- package/dist/copilotkit-BYNyIuE7.d.ts +0 -2208
- package/dist/copilotkit-BYNyIuE7.d.ts.map +0 -1
- package/dist/copilotkit-C8yWo1ap.mjs.map +0 -1
- package/dist/copilotkit-Cs3FSw_2.cjs.map +0 -1
- package/dist/copilotkit-rBCfdqIo.d.mts.map +0 -1
- package/dist/index.d.ts +0 -667
- package/dist/index.d.ts.map +0 -1
- package/dist/v2/index.d.ts +0 -6
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as React$1 from "react";
|
|
2
|
-
import React, { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import React, { createContext, forwardRef, memo, useCallback, useContext, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
3
3
|
import { CopilotKitCore, CopilotKitCoreRuntimeConnectionStatus, ProxiedCopilotRuntimeAgent, ToolCallStatus, ɵcreateThreadStore, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading } from "@copilotkit/core";
|
|
4
4
|
import { HttpAgent } from "@ag-ui/client";
|
|
5
5
|
import { extendTailwindMerge, twMerge } from "tailwind-merge";
|
|
6
|
-
import { ArrowUp, Check, ChevronDown, ChevronLeft, ChevronRight, ChevronRightIcon, Copy, Edit, Loader2, MessageCircle, Mic, Plus, RefreshCw, Square, ThumbsDown, ThumbsUp, Volume2, X } from "lucide-react";
|
|
7
|
-
import { COPILOT_CLOUD_API_URL, COPILOT_CLOUD_CHAT_URL, COPILOT_CLOUD_PUBLIC_API_KEY_HEADER, ConfigurationError, CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError, CopilotKitError, CopilotKitErrorCode, CopilotKitLowLevelError, CopilotKitRemoteEndpointDiscoveryError, DEFAULT_AGENT_ID, ErrorVisibility, MissingPublicApiKeyError, Severity, TranscriptionErrorCode, TranscriptionErrorCode as TranscriptionErrorCode$1, createLicenseContextValue, dataToUUID, parseJson, partialJSONParse, randomId, randomUUID } from "@copilotkit/shared";
|
|
6
|
+
import { ArrowUp, Check, ChevronDown, ChevronLeft, ChevronRight, ChevronRightIcon, Copy, Edit, Loader2, MessageCircle, Mic, Play, Plus, RefreshCw, Square, ThumbsDown, ThumbsUp, Upload, Volume2, X } from "lucide-react";
|
|
7
|
+
import { COPILOT_CLOUD_API_URL, COPILOT_CLOUD_CHAT_URL, COPILOT_CLOUD_PUBLIC_API_KEY_HEADER, ConfigurationError, CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError, CopilotKitError, CopilotKitErrorCode, CopilotKitLowLevelError, CopilotKitRemoteEndpointDiscoveryError, DEFAULT_AGENT_ID, ErrorVisibility, MissingPublicApiKeyError, Severity, TranscriptionErrorCode, TranscriptionErrorCode as TranscriptionErrorCode$1, createLicenseContextValue, dataToUUID, exceedsMaxSize, formatFileSize, generateVideoThumbnail, getDocumentIcon, getModalityFromMimeType, getSourceUrl, matchesAcceptFilter, parseJson, partialJSONParse, randomId, randomUUID, readFileAsBase64 } from "@copilotkit/shared";
|
|
8
8
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
9
9
|
import { Slot } from "@radix-ui/react-slot";
|
|
10
10
|
import { cva } from "class-variance-authority";
|
|
@@ -15,18 +15,118 @@ import { Streamdown } from "streamdown";
|
|
|
15
15
|
import { z } from "zod";
|
|
16
16
|
import { createComponent } from "@lit-labs/react";
|
|
17
17
|
import { A2UIProvider, A2UIRenderer, DEFAULT_SURFACE_ID, initializeDefaultCatalog, injectStyles, useA2UIActions, viewerTheme } from "@copilotkit/a2ui-renderer";
|
|
18
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
19
|
+
import { createPortal, flushSync } from "react-dom";
|
|
18
20
|
import { StickToBottom, useStickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
|
19
|
-
import { merge } from "ts-deepmerge";
|
|
20
|
-
import { flushSync } from "react-dom";
|
|
21
21
|
import ReactMarkdown from "react-markdown";
|
|
22
22
|
|
|
23
|
+
//#region src/v2/lib/slots.tsx
|
|
24
|
+
/**
|
|
25
|
+
* Shallow equality comparison for objects.
|
|
26
|
+
*/
|
|
27
|
+
function shallowEqual(obj1, obj2) {
|
|
28
|
+
const keys1 = Object.keys(obj1);
|
|
29
|
+
const keys2 = Object.keys(obj2);
|
|
30
|
+
if (keys1.length !== keys2.length) return false;
|
|
31
|
+
for (const key of keys1) if (obj1[key] !== obj2[key]) return false;
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns true only for plain JS objects (`{}`), excluding arrays, Dates,
|
|
36
|
+
* class instances, and other exotic objects that happen to have typeof "object".
|
|
37
|
+
*/
|
|
38
|
+
function isPlainObject(obj) {
|
|
39
|
+
return obj !== null && typeof obj === "object" && Object.prototype.toString.call(obj) === "[object Object]";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the same reference as long as the value is shallowly equal to the
|
|
43
|
+
* previous render's value.
|
|
44
|
+
*
|
|
45
|
+
* - Identical references bail out immediately (O(1)).
|
|
46
|
+
* - Plain objects ({}) are shallow-compared key-by-key.
|
|
47
|
+
* - Arrays, Dates, class instances, functions, and primitives are compared by
|
|
48
|
+
* reference only — shallowEqual is never called on non-plain objects, which
|
|
49
|
+
* avoids incorrect equality for e.g. [1,2] vs [1,2] (different arrays).
|
|
50
|
+
*
|
|
51
|
+
* Typical use: stabilize inline slot props so MemoizedSlotWrapper's shallow
|
|
52
|
+
* equality check isn't defeated by a new object reference on every render.
|
|
53
|
+
*/
|
|
54
|
+
function useShallowStableRef(value) {
|
|
55
|
+
const ref = useRef(value);
|
|
56
|
+
if (ref.current === value) return ref.current;
|
|
57
|
+
if (isPlainObject(ref.current) && isPlainObject(value)) {
|
|
58
|
+
if (shallowEqual(ref.current, value)) return ref.current;
|
|
59
|
+
}
|
|
60
|
+
ref.current = value;
|
|
61
|
+
return ref.current;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if a value is a React component type (function, class, forwardRef, memo, etc.)
|
|
65
|
+
*/
|
|
66
|
+
function isReactComponentType(value) {
|
|
67
|
+
if (typeof value === "function") return true;
|
|
68
|
+
if (value && typeof value === "object" && "$$typeof" in value && !React.isValidElement(value)) return true;
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Internal function to render a slot value as a React element (non-memoized).
|
|
73
|
+
*/
|
|
74
|
+
function renderSlotElement(slot, DefaultComponent, props) {
|
|
75
|
+
if (typeof slot === "string") {
|
|
76
|
+
const existingClassName = props.className;
|
|
77
|
+
return React.createElement(DefaultComponent, {
|
|
78
|
+
...props,
|
|
79
|
+
className: twMerge(existingClassName, slot)
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (isReactComponentType(slot)) return React.createElement(slot, props);
|
|
83
|
+
if (slot && typeof slot === "object" && !React.isValidElement(slot)) return React.createElement(DefaultComponent, {
|
|
84
|
+
...props,
|
|
85
|
+
...slot
|
|
86
|
+
});
|
|
87
|
+
return React.createElement(DefaultComponent, props);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Internal memoized wrapper component for renderSlot.
|
|
91
|
+
* Uses forwardRef to support ref forwarding.
|
|
92
|
+
*/
|
|
93
|
+
const MemoizedSlotWrapper = React.memo(React.forwardRef(function MemoizedSlotWrapper(props, ref) {
|
|
94
|
+
const { $slot, $component, ...rest } = props;
|
|
95
|
+
return renderSlotElement($slot, $component, ref !== null ? {
|
|
96
|
+
...rest,
|
|
97
|
+
ref
|
|
98
|
+
} : rest);
|
|
99
|
+
}), (prev, next) => {
|
|
100
|
+
if (prev.$slot !== next.$slot) return false;
|
|
101
|
+
if (prev.$component !== next.$component) return false;
|
|
102
|
+
const { $slot: _ps, $component: _pc, ...prevRest } = prev;
|
|
103
|
+
const { $slot: _ns, $component: _nc, ...nextRest } = next;
|
|
104
|
+
return shallowEqual(prevRest, nextRest);
|
|
105
|
+
});
|
|
106
|
+
/**
|
|
107
|
+
* Renders a slot value as a memoized React element.
|
|
108
|
+
* Automatically prevents unnecessary re-renders using shallow prop comparison.
|
|
109
|
+
* Supports ref forwarding.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* renderSlot(customInput, CopilotChatInput, { onSubmit: handleSubmit })
|
|
113
|
+
*/
|
|
114
|
+
function renderSlot(slot, DefaultComponent, props) {
|
|
115
|
+
return React.createElement(MemoizedSlotWrapper, {
|
|
116
|
+
...props,
|
|
117
|
+
$slot: slot,
|
|
118
|
+
$component: DefaultComponent
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
//#endregion
|
|
23
123
|
//#region src/v2/providers/CopilotChatConfigurationProvider.tsx
|
|
24
124
|
const CopilotChatDefaultLabels = {
|
|
25
125
|
chatInputPlaceholder: "Type a message...",
|
|
26
126
|
chatInputToolbarStartTranscribeButtonLabel: "Transcribe",
|
|
27
127
|
chatInputToolbarCancelTranscribeButtonLabel: "Cancel",
|
|
28
128
|
chatInputToolbarFinishTranscribeButtonLabel: "Finish",
|
|
29
|
-
chatInputToolbarAddButtonLabel: "Add
|
|
129
|
+
chatInputToolbarAddButtonLabel: "Add attachments",
|
|
30
130
|
chatInputToolbarToolsButtonLabel: "Tools",
|
|
31
131
|
assistantMessageToolbarCopyCodeLabel: "Copy",
|
|
32
132
|
assistantMessageToolbarCopyCodeCopiedLabel: "Copied",
|
|
@@ -46,11 +146,12 @@ const CopilotChatDefaultLabels = {
|
|
|
46
146
|
const CopilotChatConfiguration = createContext(null);
|
|
47
147
|
const CopilotChatConfigurationProvider = ({ children, labels, agentId, threadId, isModalDefaultOpen }) => {
|
|
48
148
|
const parentConfig = useContext(CopilotChatConfiguration);
|
|
149
|
+
const stableLabels = useShallowStableRef(labels);
|
|
49
150
|
const mergedLabels = useMemo(() => ({
|
|
50
151
|
...CopilotChatDefaultLabels,
|
|
51
152
|
...parentConfig?.labels ?? {},
|
|
52
|
-
...
|
|
53
|
-
}), [
|
|
153
|
+
...stableLabels ?? {}
|
|
154
|
+
}), [stableLabels, parentConfig?.labels]);
|
|
54
155
|
const resolvedAgentId = agentId ?? parentConfig?.agentId ?? DEFAULT_AGENT_ID;
|
|
55
156
|
const resolvedThreadId = useMemo(() => {
|
|
56
157
|
if (threadId) return threadId;
|
|
@@ -473,77 +574,6 @@ const CopilotChatAudioRecorder = forwardRef((props, ref) => {
|
|
|
473
574
|
});
|
|
474
575
|
CopilotChatAudioRecorder.displayName = "CopilotChatAudioRecorder";
|
|
475
576
|
|
|
476
|
-
//#endregion
|
|
477
|
-
//#region src/v2/lib/slots.tsx
|
|
478
|
-
/**
|
|
479
|
-
* Shallow equality comparison for objects.
|
|
480
|
-
*/
|
|
481
|
-
function shallowEqual(obj1, obj2) {
|
|
482
|
-
const keys1 = Object.keys(obj1);
|
|
483
|
-
const keys2 = Object.keys(obj2);
|
|
484
|
-
if (keys1.length !== keys2.length) return false;
|
|
485
|
-
for (const key of keys1) if (obj1[key] !== obj2[key]) return false;
|
|
486
|
-
return true;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Check if a value is a React component type (function, class, forwardRef, memo, etc.)
|
|
490
|
-
*/
|
|
491
|
-
function isReactComponentType(value) {
|
|
492
|
-
if (typeof value === "function") return true;
|
|
493
|
-
if (value && typeof value === "object" && "$$typeof" in value && !React.isValidElement(value)) return true;
|
|
494
|
-
return false;
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* Internal function to render a slot value as a React element (non-memoized).
|
|
498
|
-
*/
|
|
499
|
-
function renderSlotElement(slot, DefaultComponent, props) {
|
|
500
|
-
if (typeof slot === "string") {
|
|
501
|
-
const existingClassName = props.className;
|
|
502
|
-
return React.createElement(DefaultComponent, {
|
|
503
|
-
...props,
|
|
504
|
-
className: twMerge(existingClassName, slot)
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
if (isReactComponentType(slot)) return React.createElement(slot, props);
|
|
508
|
-
if (slot && typeof slot === "object" && !React.isValidElement(slot)) return React.createElement(DefaultComponent, {
|
|
509
|
-
...props,
|
|
510
|
-
...slot
|
|
511
|
-
});
|
|
512
|
-
return React.createElement(DefaultComponent, props);
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* Internal memoized wrapper component for renderSlot.
|
|
516
|
-
* Uses forwardRef to support ref forwarding.
|
|
517
|
-
*/
|
|
518
|
-
const MemoizedSlotWrapper = React.memo(React.forwardRef(function MemoizedSlotWrapper(props, ref) {
|
|
519
|
-
const { $slot, $component, ...rest } = props;
|
|
520
|
-
return renderSlotElement($slot, $component, ref !== null ? {
|
|
521
|
-
...rest,
|
|
522
|
-
ref
|
|
523
|
-
} : rest);
|
|
524
|
-
}), (prev, next) => {
|
|
525
|
-
if (prev.$slot !== next.$slot) return false;
|
|
526
|
-
if (prev.$component !== next.$component) return false;
|
|
527
|
-
const { $slot: _ps, $component: _pc, ...prevRest } = prev;
|
|
528
|
-
const { $slot: _ns, $component: _nc, ...nextRest } = next;
|
|
529
|
-
return shallowEqual(prevRest, nextRest);
|
|
530
|
-
});
|
|
531
|
-
/**
|
|
532
|
-
* Renders a slot value as a memoized React element.
|
|
533
|
-
* Automatically prevents unnecessary re-renders using shallow prop comparison.
|
|
534
|
-
* Supports ref forwarding.
|
|
535
|
-
*
|
|
536
|
-
* @example
|
|
537
|
-
* renderSlot(customInput, CopilotChatInput, { onSubmit: handleSubmit })
|
|
538
|
-
*/
|
|
539
|
-
function renderSlot(slot, DefaultComponent, props) {
|
|
540
|
-
return React.createElement(MemoizedSlotWrapper, {
|
|
541
|
-
...props,
|
|
542
|
-
$slot: slot,
|
|
543
|
-
$component: DefaultComponent
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
|
|
547
577
|
//#endregion
|
|
548
578
|
//#region src/v2/components/chat/CopilotChatInput.tsx
|
|
549
579
|
const SLASH_MENU_MAX_VISIBLE_ITEMS = 5;
|
|
@@ -1145,7 +1175,7 @@ function CopilotChatInput({ mode = "input", onSubmitMessage, onStop, isRunning =
|
|
|
1145
1175
|
side: "bottom",
|
|
1146
1176
|
children: /* @__PURE__ */ jsxs("p", {
|
|
1147
1177
|
className: "cpk:flex cpk:items-center cpk:gap-1 cpk:text-xs cpk:font-medium",
|
|
1148
|
-
children: [/* @__PURE__ */ jsx("span", { children: "Add
|
|
1178
|
+
children: [/* @__PURE__ */ jsx("span", { children: "Add attachments" }), /* @__PURE__ */ jsx("code", {
|
|
1149
1179
|
className: "cpk:rounded cpk:bg-[#4a4a4a] cpk:px-1 cpk:py-[1px] cpk:font-mono cpk:text-[11px] cpk:text-white cpk:dark:bg-[#e0e0e0] cpk:dark:text-black",
|
|
1150
1180
|
children: "/"
|
|
1151
1181
|
})]
|
|
@@ -2105,7 +2135,7 @@ function useStableArrayProp(prop, warningMessage, isMeaningfulChange) {
|
|
|
2105
2135
|
}, [value, warningMessage]);
|
|
2106
2136
|
return value;
|
|
2107
2137
|
}
|
|
2108
|
-
const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, publicApiKey, publicLicenseKey, licenseToken, properties = {}, agents__unsafe_dev_only: agents = {}, selfManagedAgents = {}, renderToolCalls, renderActivityMessages, renderCustomMessages, frontendTools, humanInTheLoop, showDevConsole = false, useSingleEndpoint
|
|
2138
|
+
const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, publicApiKey, publicLicenseKey, licenseToken, properties = {}, agents__unsafe_dev_only: agents = {}, selfManagedAgents = {}, renderToolCalls, renderActivityMessages, renderCustomMessages, frontendTools, humanInTheLoop, showDevConsole = false, useSingleEndpoint, onError, a2ui }) => {
|
|
2109
2139
|
const [shouldRenderInspector, setShouldRenderInspector] = useState(false);
|
|
2110
2140
|
const [runtimeA2UIEnabled, setRuntimeA2UIEnabled] = useState(false);
|
|
2111
2141
|
const [runtimeLicenseStatus, setRuntimeLicenseStatus] = useState(void 0);
|
|
@@ -2219,7 +2249,7 @@ const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, p
|
|
|
2219
2249
|
const copilotkitRef = useRef(null);
|
|
2220
2250
|
if (copilotkitRef.current === null) copilotkitRef.current = new CopilotKitCoreReact({
|
|
2221
2251
|
runtimeUrl: chatApiEndpoint,
|
|
2222
|
-
runtimeTransport: useSingleEndpoint ? "single" : "rest",
|
|
2252
|
+
runtimeTransport: useSingleEndpoint === true ? "single" : useSingleEndpoint === false ? "rest" : "auto",
|
|
2223
2253
|
headers: mergedHeaders,
|
|
2224
2254
|
credentials,
|
|
2225
2255
|
properties,
|
|
@@ -2291,7 +2321,7 @@ const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, p
|
|
|
2291
2321
|
}, [copilotkit]);
|
|
2292
2322
|
useEffect(() => {
|
|
2293
2323
|
copilotkit.setRuntimeUrl(chatApiEndpoint);
|
|
2294
|
-
copilotkit.setRuntimeTransport(useSingleEndpoint ? "single" : "rest");
|
|
2324
|
+
copilotkit.setRuntimeTransport(useSingleEndpoint === true ? "single" : useSingleEndpoint === false ? "rest" : "auto");
|
|
2295
2325
|
copilotkit.setHeaders(mergedHeaders);
|
|
2296
2326
|
copilotkit.setCredentials(credentials);
|
|
2297
2327
|
copilotkit.setProperties(properties);
|
|
@@ -2657,7 +2687,8 @@ function useRenderCustomMessages() {
|
|
|
2657
2687
|
//#region src/v2/hooks/use-render-activity-message.tsx
|
|
2658
2688
|
function useRenderActivityMessage() {
|
|
2659
2689
|
const { copilotkit } = useCopilotKit();
|
|
2660
|
-
const
|
|
2690
|
+
const config = useCopilotChatConfiguration();
|
|
2691
|
+
const agentId = config?.agentId ?? DEFAULT_AGENT_ID;
|
|
2661
2692
|
const renderers = copilotkit.renderActivityMessages;
|
|
2662
2693
|
const findRenderer = useCallback((activityType) => {
|
|
2663
2694
|
if (!renderers.length) return null;
|
|
@@ -2673,7 +2704,8 @@ function useRenderActivityMessage() {
|
|
|
2673
2704
|
return null;
|
|
2674
2705
|
}
|
|
2675
2706
|
const Component = renderer.render;
|
|
2676
|
-
const
|
|
2707
|
+
const registryAgent = copilotkit.getAgent(agentId);
|
|
2708
|
+
const agent = getThreadClone(registryAgent, config?.threadId) ?? registryAgent;
|
|
2677
2709
|
return /* @__PURE__ */ jsx(Component, {
|
|
2678
2710
|
activityType: message.activityType,
|
|
2679
2711
|
content: parseResult.data,
|
|
@@ -2682,6 +2714,7 @@ function useRenderActivityMessage() {
|
|
|
2682
2714
|
}, message.id);
|
|
2683
2715
|
}, [
|
|
2684
2716
|
agentId,
|
|
2717
|
+
config?.threadId,
|
|
2685
2718
|
copilotkit,
|
|
2686
2719
|
findRenderer
|
|
2687
2720
|
]);
|
|
@@ -3567,11 +3600,19 @@ function useThreadStoreSelector(store, selector) {
|
|
|
3567
3600
|
function useThreads$1({ agentId, includeArchived, limit }) {
|
|
3568
3601
|
const { copilotkit } = useCopilotKit();
|
|
3569
3602
|
const [store] = useState(() => ɵcreateThreadStore({ fetch: globalThis.fetch }));
|
|
3570
|
-
const
|
|
3603
|
+
const coreThreads = useThreadStoreSelector(store, ɵselectThreads);
|
|
3604
|
+
const threads = useMemo(() => coreThreads.map(({ id, agentId, name, archived, createdAt, updatedAt }) => ({
|
|
3605
|
+
id,
|
|
3606
|
+
agentId,
|
|
3607
|
+
name,
|
|
3608
|
+
archived,
|
|
3609
|
+
createdAt,
|
|
3610
|
+
updatedAt
|
|
3611
|
+
})), [coreThreads]);
|
|
3571
3612
|
const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);
|
|
3572
3613
|
const storeError = useThreadStoreSelector(store, ɵselectThreadsError);
|
|
3573
|
-
const
|
|
3574
|
-
const
|
|
3614
|
+
const hasMoreThreads = useThreadStoreSelector(store, ɵselectHasNextPage);
|
|
3615
|
+
const isFetchingMoreThreads = useThreadStoreSelector(store, ɵselectIsFetchingNextPage);
|
|
3575
3616
|
const headersKey = useMemo(() => {
|
|
3576
3617
|
return JSON.stringify(Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) => left.localeCompare(right)));
|
|
3577
3618
|
}, [copilotkit.headers]);
|
|
@@ -3614,15 +3655,173 @@ function useThreads$1({ agentId, includeArchived, limit }) {
|
|
|
3614
3655
|
threads,
|
|
3615
3656
|
isLoading,
|
|
3616
3657
|
error,
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3658
|
+
hasMoreThreads,
|
|
3659
|
+
isFetchingMoreThreads,
|
|
3660
|
+
fetchMoreThreads: useCallback(() => store.fetchNextPage(), [store]),
|
|
3620
3661
|
renameThread,
|
|
3621
3662
|
archiveThread,
|
|
3622
3663
|
deleteThread
|
|
3623
3664
|
};
|
|
3624
3665
|
}
|
|
3625
3666
|
|
|
3667
|
+
//#endregion
|
|
3668
|
+
//#region src/v2/hooks/use-attachments.tsx
|
|
3669
|
+
/**
|
|
3670
|
+
* Hook that manages file attachment state — uploads, drag-and-drop, paste,
|
|
3671
|
+
* and lifecycle. All returned callbacks are referentially stable across
|
|
3672
|
+
* renders (via useCallback) to avoid destabilizing downstream memoization.
|
|
3673
|
+
*/
|
|
3674
|
+
function useAttachments({ config }) {
|
|
3675
|
+
const enabled = config?.enabled ?? false;
|
|
3676
|
+
const [attachments, setAttachments] = useState([]);
|
|
3677
|
+
const [dragOver, setDragOver] = useState(false);
|
|
3678
|
+
const fileInputRef = useRef(null);
|
|
3679
|
+
const containerRef = useRef(null);
|
|
3680
|
+
const configRef = useRef(config);
|
|
3681
|
+
configRef.current = config;
|
|
3682
|
+
const attachmentsRef = useRef([]);
|
|
3683
|
+
attachmentsRef.current = attachments;
|
|
3684
|
+
const processFiles = useCallback(async (files) => {
|
|
3685
|
+
const cfg = configRef.current;
|
|
3686
|
+
const accept = cfg?.accept ?? "*/*";
|
|
3687
|
+
const maxSize = cfg?.maxSize ?? 20 * 1024 * 1024;
|
|
3688
|
+
const rejectedFiles = files.filter((file) => !matchesAcceptFilter(file, accept));
|
|
3689
|
+
for (const file of rejectedFiles) cfg?.onUploadFailed?.({
|
|
3690
|
+
reason: "invalid-type",
|
|
3691
|
+
file,
|
|
3692
|
+
message: `File "${file.name}" is not accepted. Supported types: ${accept}`
|
|
3693
|
+
});
|
|
3694
|
+
const validFiles = files.filter((file) => matchesAcceptFilter(file, accept));
|
|
3695
|
+
for (const file of validFiles) {
|
|
3696
|
+
if (exceedsMaxSize(file, maxSize)) {
|
|
3697
|
+
cfg?.onUploadFailed?.({
|
|
3698
|
+
reason: "file-too-large",
|
|
3699
|
+
file,
|
|
3700
|
+
message: `File "${file.name}" exceeds the maximum size of ${formatFileSize(maxSize)}`
|
|
3701
|
+
});
|
|
3702
|
+
continue;
|
|
3703
|
+
}
|
|
3704
|
+
const modality = getModalityFromMimeType(file.type);
|
|
3705
|
+
const placeholderId = randomUUID();
|
|
3706
|
+
const placeholder = {
|
|
3707
|
+
id: placeholderId,
|
|
3708
|
+
type: modality,
|
|
3709
|
+
source: {
|
|
3710
|
+
type: "data",
|
|
3711
|
+
value: "",
|
|
3712
|
+
mimeType: file.type
|
|
3713
|
+
},
|
|
3714
|
+
filename: file.name,
|
|
3715
|
+
size: file.size,
|
|
3716
|
+
status: "uploading"
|
|
3717
|
+
};
|
|
3718
|
+
setAttachments((prev) => [...prev, placeholder]);
|
|
3719
|
+
try {
|
|
3720
|
+
let source;
|
|
3721
|
+
let uploadMetadata;
|
|
3722
|
+
if (cfg?.onUpload) {
|
|
3723
|
+
const { metadata: meta, ...uploadSource } = await cfg.onUpload(file);
|
|
3724
|
+
source = uploadSource;
|
|
3725
|
+
uploadMetadata = meta;
|
|
3726
|
+
} else source = {
|
|
3727
|
+
type: "data",
|
|
3728
|
+
value: await readFileAsBase64(file),
|
|
3729
|
+
mimeType: file.type
|
|
3730
|
+
};
|
|
3731
|
+
let thumbnail;
|
|
3732
|
+
if (modality === "video") thumbnail = await generateVideoThumbnail(file);
|
|
3733
|
+
setAttachments((prev) => prev.map((att) => att.id === placeholderId ? {
|
|
3734
|
+
...att,
|
|
3735
|
+
source,
|
|
3736
|
+
status: "ready",
|
|
3737
|
+
thumbnail,
|
|
3738
|
+
metadata: uploadMetadata
|
|
3739
|
+
} : att));
|
|
3740
|
+
} catch (error) {
|
|
3741
|
+
setAttachments((prev) => prev.filter((att) => att.id !== placeholderId));
|
|
3742
|
+
console.error(`[CopilotKit] Failed to upload "${file.name}":`, error);
|
|
3743
|
+
cfg?.onUploadFailed?.({
|
|
3744
|
+
reason: "upload-failed",
|
|
3745
|
+
file,
|
|
3746
|
+
message: error instanceof Error ? error.message : `Failed to upload "${file.name}"`
|
|
3747
|
+
});
|
|
3748
|
+
}
|
|
3749
|
+
}
|
|
3750
|
+
}, []);
|
|
3751
|
+
const handleFileUpload = useCallback(async (e) => {
|
|
3752
|
+
if (!e.target.files?.length) return;
|
|
3753
|
+
try {
|
|
3754
|
+
await processFiles(Array.from(e.target.files));
|
|
3755
|
+
} catch (error) {
|
|
3756
|
+
console.error("[CopilotKit] Upload error:", error);
|
|
3757
|
+
}
|
|
3758
|
+
}, [processFiles]);
|
|
3759
|
+
const handleDragOver = useCallback((e) => {
|
|
3760
|
+
if (!configRef.current?.enabled) return;
|
|
3761
|
+
e.preventDefault();
|
|
3762
|
+
e.stopPropagation();
|
|
3763
|
+
setDragOver(true);
|
|
3764
|
+
}, []);
|
|
3765
|
+
const handleDragLeave = useCallback((e) => {
|
|
3766
|
+
e.preventDefault();
|
|
3767
|
+
e.stopPropagation();
|
|
3768
|
+
setDragOver(false);
|
|
3769
|
+
}, []);
|
|
3770
|
+
const handleDrop = useCallback(async (e) => {
|
|
3771
|
+
e.preventDefault();
|
|
3772
|
+
e.stopPropagation();
|
|
3773
|
+
setDragOver(false);
|
|
3774
|
+
if (!configRef.current?.enabled) return;
|
|
3775
|
+
const files = Array.from(e.dataTransfer.files);
|
|
3776
|
+
if (files.length > 0) try {
|
|
3777
|
+
await processFiles(files);
|
|
3778
|
+
} catch (error) {
|
|
3779
|
+
console.error("[CopilotKit] Drop error:", error);
|
|
3780
|
+
}
|
|
3781
|
+
}, [processFiles]);
|
|
3782
|
+
useEffect(() => {
|
|
3783
|
+
if (!enabled) return;
|
|
3784
|
+
const handlePaste = async (e) => {
|
|
3785
|
+
const target = e.target;
|
|
3786
|
+
if (!target || !containerRef.current?.contains(target)) return;
|
|
3787
|
+
const accept = configRef.current?.accept ?? "*/*";
|
|
3788
|
+
const fileItems = Array.from(e.clipboardData?.items || []).filter((item) => item.kind === "file" && item.getAsFile() !== null && matchesAcceptFilter(item.getAsFile(), accept));
|
|
3789
|
+
if (fileItems.length === 0) return;
|
|
3790
|
+
e.preventDefault();
|
|
3791
|
+
const files = fileItems.map((item) => item.getAsFile()).filter((f) => f !== null);
|
|
3792
|
+
try {
|
|
3793
|
+
await processFiles(files);
|
|
3794
|
+
} catch (error) {
|
|
3795
|
+
console.error("[CopilotKit] Paste error:", error);
|
|
3796
|
+
}
|
|
3797
|
+
};
|
|
3798
|
+
document.addEventListener("paste", handlePaste);
|
|
3799
|
+
return () => document.removeEventListener("paste", handlePaste);
|
|
3800
|
+
}, [enabled, processFiles]);
|
|
3801
|
+
return {
|
|
3802
|
+
attachments,
|
|
3803
|
+
enabled,
|
|
3804
|
+
dragOver,
|
|
3805
|
+
fileInputRef,
|
|
3806
|
+
containerRef,
|
|
3807
|
+
processFiles,
|
|
3808
|
+
handleFileUpload,
|
|
3809
|
+
handleDragOver,
|
|
3810
|
+
handleDragLeave,
|
|
3811
|
+
handleDrop,
|
|
3812
|
+
removeAttachment: useCallback((id) => {
|
|
3813
|
+
setAttachments((prev) => prev.filter((a) => a.id !== id));
|
|
3814
|
+
}, []),
|
|
3815
|
+
consumeAttachments: useCallback(() => {
|
|
3816
|
+
const ready = attachmentsRef.current.filter((a) => a.status === "ready");
|
|
3817
|
+
if (ready.length === 0) return ready;
|
|
3818
|
+
setAttachments((prev) => prev.filter((a) => a.status !== "ready"));
|
|
3819
|
+
if (fileInputRef.current) fileInputRef.current.value = "";
|
|
3820
|
+
return ready;
|
|
3821
|
+
}, [])
|
|
3822
|
+
};
|
|
3823
|
+
}
|
|
3824
|
+
|
|
3626
3825
|
//#endregion
|
|
3627
3826
|
//#region src/v2/components/chat/CopilotChatToolCallsView.tsx
|
|
3628
3827
|
function CopilotChatToolCallsView({ message, messages = [] }) {
|
|
@@ -3739,9 +3938,19 @@ function CopilotChatAssistantMessage({ message, messages, isRunning, onThumbsUp,
|
|
|
3739
3938
|
_CopilotChatAssistantMessage.CopyButton = ({ className, title, onClick, ...props }) => {
|
|
3740
3939
|
const labels = useCopilotChatConfiguration()?.labels ?? CopilotChatDefaultLabels;
|
|
3741
3940
|
const [copied, setCopied] = useState(false);
|
|
3941
|
+
const timerRef = useRef(null);
|
|
3942
|
+
useEffect(() => {
|
|
3943
|
+
return () => {
|
|
3944
|
+
if (timerRef.current !== null) clearTimeout(timerRef.current);
|
|
3945
|
+
};
|
|
3946
|
+
}, []);
|
|
3742
3947
|
const handleClick = (event) => {
|
|
3743
3948
|
setCopied(true);
|
|
3744
|
-
|
|
3949
|
+
if (timerRef.current !== null) clearTimeout(timerRef.current);
|
|
3950
|
+
timerRef.current = setTimeout(() => {
|
|
3951
|
+
timerRef.current = null;
|
|
3952
|
+
setCopied(false);
|
|
3953
|
+
}, 2e3);
|
|
3745
3954
|
if (onClick) onClick(event);
|
|
3746
3955
|
};
|
|
3747
3956
|
return /* @__PURE__ */ jsx(ToolbarButton, {
|
|
@@ -3799,6 +4008,79 @@ CopilotChatAssistantMessage.ReadAloudButton.displayName = "CopilotChatAssistantM
|
|
|
3799
4008
|
CopilotChatAssistantMessage.RegenerateButton.displayName = "CopilotChatAssistantMessage.RegenerateButton";
|
|
3800
4009
|
var CopilotChatAssistantMessage_default = CopilotChatAssistantMessage;
|
|
3801
4010
|
|
|
4011
|
+
//#endregion
|
|
4012
|
+
//#region src/v2/components/chat/CopilotChatAttachmentRenderer.tsx
|
|
4013
|
+
const ImageAttachment = memo(function ImageAttachment({ src, className }) {
|
|
4014
|
+
const [error, setError] = useState(false);
|
|
4015
|
+
if (error) return /* @__PURE__ */ jsx("div", {
|
|
4016
|
+
className: cn("cpk:flex cpk:flex-col cpk:items-center cpk:justify-center cpk:rounded-lg cpk:bg-muted cpk:p-4 cpk:text-sm cpk:text-muted-foreground", className),
|
|
4017
|
+
children: /* @__PURE__ */ jsx("span", { children: "Failed to load image" })
|
|
4018
|
+
});
|
|
4019
|
+
return /* @__PURE__ */ jsx("img", {
|
|
4020
|
+
src,
|
|
4021
|
+
alt: "Image attachment",
|
|
4022
|
+
className: cn("cpk:max-w-full cpk:h-auto cpk:rounded-lg", className),
|
|
4023
|
+
onError: () => setError(true)
|
|
4024
|
+
});
|
|
4025
|
+
});
|
|
4026
|
+
const AudioAttachment = memo(function AudioAttachment({ src, filename, className }) {
|
|
4027
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4028
|
+
className: cn("cpk:flex cpk:flex-col cpk:gap-1", className),
|
|
4029
|
+
children: [/* @__PURE__ */ jsx("audio", {
|
|
4030
|
+
src,
|
|
4031
|
+
controls: true,
|
|
4032
|
+
preload: "metadata",
|
|
4033
|
+
className: "cpk:max-w-[300px] cpk:w-full cpk:h-10"
|
|
4034
|
+
}), filename && /* @__PURE__ */ jsx("span", {
|
|
4035
|
+
className: "cpk:text-xs cpk:text-muted-foreground cpk:truncate cpk:max-w-[300px]",
|
|
4036
|
+
children: filename
|
|
4037
|
+
})]
|
|
4038
|
+
});
|
|
4039
|
+
});
|
|
4040
|
+
const VideoAttachment = memo(function VideoAttachment({ src, className }) {
|
|
4041
|
+
return /* @__PURE__ */ jsx("video", {
|
|
4042
|
+
src,
|
|
4043
|
+
controls: true,
|
|
4044
|
+
preload: "metadata",
|
|
4045
|
+
className: cn("cpk:max-w-[400px] cpk:w-full cpk:rounded-lg", className)
|
|
4046
|
+
});
|
|
4047
|
+
});
|
|
4048
|
+
const DocumentAttachment = memo(function DocumentAttachment({ source, filename, className }) {
|
|
4049
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4050
|
+
className: cn("cpk:inline-flex cpk:items-center cpk:gap-2 cpk:px-3 cpk:py-2 cpk:border cpk:border-border cpk:rounded-lg cpk:bg-muted", className),
|
|
4051
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
4052
|
+
className: "cpk:text-xs cpk:font-bold cpk:uppercase",
|
|
4053
|
+
children: getDocumentIcon(source.mimeType ?? "")
|
|
4054
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
4055
|
+
className: "cpk:text-sm cpk:text-muted-foreground cpk:truncate",
|
|
4056
|
+
children: filename || source.mimeType || "Unknown type"
|
|
4057
|
+
})]
|
|
4058
|
+
});
|
|
4059
|
+
});
|
|
4060
|
+
const CopilotChatAttachmentRenderer = ({ type, source, filename, className }) => {
|
|
4061
|
+
const src = getSourceUrl(source);
|
|
4062
|
+
switch (type) {
|
|
4063
|
+
case "image": return /* @__PURE__ */ jsx(ImageAttachment, {
|
|
4064
|
+
src,
|
|
4065
|
+
className
|
|
4066
|
+
});
|
|
4067
|
+
case "audio": return /* @__PURE__ */ jsx(AudioAttachment, {
|
|
4068
|
+
src,
|
|
4069
|
+
filename,
|
|
4070
|
+
className
|
|
4071
|
+
});
|
|
4072
|
+
case "video": return /* @__PURE__ */ jsx(VideoAttachment, {
|
|
4073
|
+
src,
|
|
4074
|
+
className
|
|
4075
|
+
});
|
|
4076
|
+
case "document": return /* @__PURE__ */ jsx(DocumentAttachment, {
|
|
4077
|
+
source,
|
|
4078
|
+
filename,
|
|
4079
|
+
className
|
|
4080
|
+
});
|
|
4081
|
+
}
|
|
4082
|
+
};
|
|
4083
|
+
|
|
3802
4084
|
//#endregion
|
|
3803
4085
|
//#region src/v2/components/chat/CopilotChatUserMessage.tsx
|
|
3804
4086
|
function flattenUserMessageContent(content) {
|
|
@@ -3809,8 +4091,17 @@ function flattenUserMessageContent(content) {
|
|
|
3809
4091
|
return "";
|
|
3810
4092
|
}).filter((text) => text.length > 0).join("\n");
|
|
3811
4093
|
}
|
|
4094
|
+
function getMediaParts(content) {
|
|
4095
|
+
if (!content || typeof content === "string") return [];
|
|
4096
|
+
return content.filter((part) => part.type === "image" || part.type === "audio" || part.type === "video" || part.type === "document");
|
|
4097
|
+
}
|
|
4098
|
+
function getFilename(part) {
|
|
4099
|
+
const meta = part.metadata;
|
|
4100
|
+
if (meta != null && typeof meta === "object" && "filename" in meta && typeof meta.filename === "string") return meta.filename;
|
|
4101
|
+
}
|
|
3812
4102
|
function CopilotChatUserMessage({ message, onEditMessage, branchIndex, numberOfBranches, onSwitchToBranch, additionalToolbarItems, messageRenderer, toolbar, copyButton, editButton, branchNavigation, children, className, ...props }) {
|
|
3813
4103
|
const flattenedContent = useMemo(() => flattenUserMessageContent(message.content), [message.content]);
|
|
4104
|
+
const mediaParts = useMemo(() => getMediaParts(message.content), [message.content]);
|
|
3814
4105
|
const BoundMessageRenderer = renderSlot(messageRenderer, CopilotChatUserMessage.MessageRenderer, { content: flattenedContent });
|
|
3815
4106
|
const BoundCopyButton = renderSlot(copyButton, CopilotChatUserMessage.CopyButton, { onClick: async () => {
|
|
3816
4107
|
if (flattenedContent) try {
|
|
@@ -3857,7 +4148,18 @@ function CopilotChatUserMessage({ message, onEditMessage, branchIndex, numberOfB
|
|
|
3857
4148
|
className: twMerge("copilotKitMessage copilotKitUserMessage cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10", className),
|
|
3858
4149
|
"data-message-id": message.id,
|
|
3859
4150
|
...props,
|
|
3860
|
-
children: [
|
|
4151
|
+
children: [
|
|
4152
|
+
BoundMessageRenderer,
|
|
4153
|
+
mediaParts.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
4154
|
+
className: "cpk:flex cpk:flex-col cpk:items-end cpk:gap-2 cpk:mt-2",
|
|
4155
|
+
children: mediaParts.map((part, index) => /* @__PURE__ */ jsx(CopilotChatAttachmentRenderer, {
|
|
4156
|
+
type: part.type,
|
|
4157
|
+
source: part.source,
|
|
4158
|
+
filename: getFilename(part)
|
|
4159
|
+
}, index))
|
|
4160
|
+
}),
|
|
4161
|
+
BoundToolbar
|
|
4162
|
+
]
|
|
3861
4163
|
});
|
|
3862
4164
|
}
|
|
3863
4165
|
(function(_CopilotChatUserMessage) {
|
|
@@ -4191,9 +4493,43 @@ const CopilotChatSuggestionView = React.forwardRef(function CopilotChatSuggestio
|
|
|
4191
4493
|
});
|
|
4192
4494
|
CopilotChatSuggestionView.displayName = "CopilotChatSuggestionView";
|
|
4193
4495
|
|
|
4496
|
+
//#endregion
|
|
4497
|
+
//#region src/v2/components/chat/scroll-element-context.ts
|
|
4498
|
+
/**
|
|
4499
|
+
* Provides the scroll container element to child components that need it for
|
|
4500
|
+
* virtualization. Set by CopilotChatView.ScrollView; consumed by
|
|
4501
|
+
* CopilotChatMessageView to feed useVirtualizer's getScrollElement.
|
|
4502
|
+
*
|
|
4503
|
+
* Carries the element itself (not a ref) so that context consumers re-render
|
|
4504
|
+
* reactively when the scroll container is first mounted.
|
|
4505
|
+
*/
|
|
4506
|
+
const ScrollElementContext = React.createContext(null);
|
|
4507
|
+
|
|
4194
4508
|
//#endregion
|
|
4195
4509
|
//#region src/v2/components/chat/CopilotChatMessageView.tsx
|
|
4196
4510
|
/**
|
|
4511
|
+
* Resolves a slot value into a { Component, slotProps } pair, handling the three
|
|
4512
|
+
* slot forms: a component type, a className string, or a partial-props object.
|
|
4513
|
+
*/
|
|
4514
|
+
function resolveSlotComponent(slot, DefaultComponent) {
|
|
4515
|
+
if (isReactComponentType(slot)) return {
|
|
4516
|
+
Component: slot,
|
|
4517
|
+
slotProps: void 0
|
|
4518
|
+
};
|
|
4519
|
+
if (typeof slot === "string") return {
|
|
4520
|
+
Component: DefaultComponent,
|
|
4521
|
+
slotProps: { className: slot }
|
|
4522
|
+
};
|
|
4523
|
+
if (slot && typeof slot === "object") return {
|
|
4524
|
+
Component: DefaultComponent,
|
|
4525
|
+
slotProps: slot
|
|
4526
|
+
};
|
|
4527
|
+
return {
|
|
4528
|
+
Component: DefaultComponent,
|
|
4529
|
+
slotProps: void 0
|
|
4530
|
+
};
|
|
4531
|
+
}
|
|
4532
|
+
/**
|
|
4197
4533
|
* Memoized wrapper for assistant messages to prevent re-renders when other messages change.
|
|
4198
4534
|
*/
|
|
4199
4535
|
const MemoizedAssistantMessage = React.memo(function MemoizedAssistantMessage({ message, messages, isRunning, AssistantMessageComponent, slotProps }) {
|
|
@@ -4212,7 +4548,6 @@ const MemoizedAssistantMessage = React.memo(function MemoizedAssistantMessage({
|
|
|
4212
4548
|
if (prevToolCalls && nextToolCalls) for (let i = 0; i < prevToolCalls.length; i++) {
|
|
4213
4549
|
const prevTc = prevToolCalls[i];
|
|
4214
4550
|
const nextTc = nextToolCalls[i];
|
|
4215
|
-
if (!prevTc || !nextTc) return false;
|
|
4216
4551
|
if (prevTc.id !== nextTc.id) return false;
|
|
4217
4552
|
if (prevTc.function.arguments !== nextTc.function.arguments) return false;
|
|
4218
4553
|
}
|
|
@@ -4291,6 +4626,7 @@ const MemoizedCustomMessage = React.memo(function MemoizedCustomMessage({ messag
|
|
|
4291
4626
|
if (JSON.stringify(prevProps.stateSnapshot) !== JSON.stringify(nextProps.stateSnapshot)) return false;
|
|
4292
4627
|
return true;
|
|
4293
4628
|
});
|
|
4629
|
+
const VIRTUALIZE_THRESHOLD = 50;
|
|
4294
4630
|
function CopilotChatMessageView({ messages = [], assistantMessage, userMessage, reasoningMessage, cursor, isRunning = false, children, className, ...props }) {
|
|
4295
4631
|
const renderCustomMessage = useRenderCustomMessages();
|
|
4296
4632
|
const { renderActivityMessage } = useRenderActivityMessage();
|
|
@@ -4324,9 +4660,34 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
|
|
|
4324
4660
|
if (!resolvedRunId) return void 0;
|
|
4325
4661
|
return copilotkit.getStateByRun(config.agentId, config.threadId, resolvedRunId);
|
|
4326
4662
|
};
|
|
4327
|
-
const deduplicatedMessages = [...new Map(messages.map((m) => [m.id, m])).values()];
|
|
4663
|
+
const deduplicatedMessages = useMemo(() => [...new Map(messages.map((m) => [m.id, m])).values()], [messages]);
|
|
4328
4664
|
if (process.env.NODE_ENV === "development" && deduplicatedMessages.length < messages.length) console.warn(`CopilotChatMessageView: Deduplicated ${messages.length - deduplicatedMessages.length} message(s) with duplicate IDs.`);
|
|
4329
|
-
const
|
|
4665
|
+
const { Component: AssistantComponent, slotProps: assistantSlotProps } = useMemo(() => resolveSlotComponent(assistantMessage, CopilotChatAssistantMessage_default), [assistantMessage]);
|
|
4666
|
+
const { Component: UserComponent, slotProps: userSlotProps } = useMemo(() => resolveSlotComponent(userMessage, CopilotChatUserMessage_default), [userMessage]);
|
|
4667
|
+
const { Component: ReasoningComponent, slotProps: reasoningSlotProps } = useMemo(() => resolveSlotComponent(reasoningMessage, CopilotChatReasoningMessage_default), [reasoningMessage]);
|
|
4668
|
+
const scrollElementFromCtx = useContext(ScrollElementContext);
|
|
4669
|
+
const scrollElement = scrollElementFromCtx && scrollElementFromCtx.clientHeight > 0 ? scrollElementFromCtx : null;
|
|
4670
|
+
useEffect(() => {
|
|
4671
|
+
if (process.env.NODE_ENV !== "production" && scrollElementFromCtx && scrollElementFromCtx.clientHeight === 0) console.warn("[CopilotKit] Chat scroll container has clientHeight=0 — virtualization disabled. Ensure the chat is rendered in a visible container with a non-zero height.");
|
|
4672
|
+
}, [scrollElementFromCtx]);
|
|
4673
|
+
const shouldVirtualize = !!scrollElement && !children && deduplicatedMessages.length > VIRTUALIZE_THRESHOLD;
|
|
4674
|
+
const virtualizer = useVirtualizer({
|
|
4675
|
+
count: shouldVirtualize ? deduplicatedMessages.length : 0,
|
|
4676
|
+
getScrollElement: () => scrollElement,
|
|
4677
|
+
estimateSize: () => 100,
|
|
4678
|
+
overscan: 5,
|
|
4679
|
+
measureElement: (el) => el?.getBoundingClientRect().height ?? 0,
|
|
4680
|
+
initialRect: {
|
|
4681
|
+
width: 0,
|
|
4682
|
+
height: 600
|
|
4683
|
+
}
|
|
4684
|
+
});
|
|
4685
|
+
const firstMessageId = deduplicatedMessages[0]?.id;
|
|
4686
|
+
useLayoutEffect(() => {
|
|
4687
|
+
if (!shouldVirtualize || !deduplicatedMessages.length) return;
|
|
4688
|
+
virtualizer.scrollToIndex(deduplicatedMessages.length - 1, { align: "end" });
|
|
4689
|
+
}, [shouldVirtualize, firstMessageId]);
|
|
4690
|
+
const renderMessageBlock = (message) => {
|
|
4330
4691
|
const elements = [];
|
|
4331
4692
|
const stateSnapshot = getStateSnapshotForMessage(message.id);
|
|
4332
4693
|
if (renderCustomMessage) elements.push(/* @__PURE__ */ jsx(MemoizedCustomMessage, {
|
|
@@ -4335,58 +4696,38 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
|
|
|
4335
4696
|
renderCustomMessage,
|
|
4336
4697
|
stateSnapshot
|
|
4337
4698
|
}, `${message.id}-custom-before`));
|
|
4338
|
-
if (message.role === "assistant") {
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
}, message.id));
|
|
4362
|
-
} else if (message.role === "activity") {
|
|
4363
|
-
const activityMsg = message;
|
|
4364
|
-
elements.push(/* @__PURE__ */ jsx(MemoizedActivityMessage, {
|
|
4365
|
-
message: activityMsg,
|
|
4366
|
-
renderActivityMessage
|
|
4367
|
-
}, message.id));
|
|
4368
|
-
} else if (message.role === "reasoning") {
|
|
4369
|
-
let ReasoningComponent = CopilotChatReasoningMessage_default;
|
|
4370
|
-
let reasoningSlotProps;
|
|
4371
|
-
if (isReactComponentType(reasoningMessage)) ReasoningComponent = reasoningMessage;
|
|
4372
|
-
else if (typeof reasoningMessage === "string") reasoningSlotProps = { className: reasoningMessage };
|
|
4373
|
-
else if (reasoningMessage && typeof reasoningMessage === "object") reasoningSlotProps = reasoningMessage;
|
|
4374
|
-
elements.push(/* @__PURE__ */ jsx(MemoizedReasoningMessage, {
|
|
4375
|
-
message,
|
|
4376
|
-
messages,
|
|
4377
|
-
isRunning,
|
|
4378
|
-
ReasoningMessageComponent: ReasoningComponent,
|
|
4379
|
-
slotProps: reasoningSlotProps
|
|
4380
|
-
}, message.id));
|
|
4381
|
-
}
|
|
4699
|
+
if (message.role === "assistant") elements.push(/* @__PURE__ */ jsx(MemoizedAssistantMessage, {
|
|
4700
|
+
message,
|
|
4701
|
+
messages,
|
|
4702
|
+
isRunning,
|
|
4703
|
+
AssistantMessageComponent: AssistantComponent,
|
|
4704
|
+
slotProps: assistantSlotProps
|
|
4705
|
+
}, message.id));
|
|
4706
|
+
else if (message.role === "user") elements.push(/* @__PURE__ */ jsx(MemoizedUserMessage, {
|
|
4707
|
+
message,
|
|
4708
|
+
UserMessageComponent: UserComponent,
|
|
4709
|
+
slotProps: userSlotProps
|
|
4710
|
+
}, message.id));
|
|
4711
|
+
else if (message.role === "activity") elements.push(/* @__PURE__ */ jsx(MemoizedActivityMessage, {
|
|
4712
|
+
message,
|
|
4713
|
+
renderActivityMessage
|
|
4714
|
+
}, message.id));
|
|
4715
|
+
else if (message.role === "reasoning") elements.push(/* @__PURE__ */ jsx(MemoizedReasoningMessage, {
|
|
4716
|
+
message,
|
|
4717
|
+
messages,
|
|
4718
|
+
isRunning,
|
|
4719
|
+
ReasoningMessageComponent: ReasoningComponent,
|
|
4720
|
+
slotProps: reasoningSlotProps
|
|
4721
|
+
}, message.id));
|
|
4382
4722
|
if (renderCustomMessage) elements.push(/* @__PURE__ */ jsx(MemoizedCustomMessage, {
|
|
4383
4723
|
message,
|
|
4384
4724
|
position: "after",
|
|
4385
4725
|
renderCustomMessage,
|
|
4386
4726
|
stateSnapshot
|
|
4387
4727
|
}, `${message.id}-custom-after`));
|
|
4388
|
-
return elements;
|
|
4389
|
-
}
|
|
4728
|
+
return elements.filter(Boolean);
|
|
4729
|
+
};
|
|
4730
|
+
const messageElements = shouldVirtualize ? [] : deduplicatedMessages.flatMap(renderMessageBlock);
|
|
4390
4731
|
if (children) return /* @__PURE__ */ jsx("div", {
|
|
4391
4732
|
"data-copilotkit": true,
|
|
4392
4733
|
style: { display: "contents" },
|
|
@@ -4405,7 +4746,27 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
|
|
|
4405
4746
|
className: twMerge("copilotKitMessages cpk:flex cpk:flex-col", className),
|
|
4406
4747
|
...props,
|
|
4407
4748
|
children: [
|
|
4408
|
-
|
|
4749
|
+
shouldVirtualize ? /* @__PURE__ */ jsx("div", {
|
|
4750
|
+
style: {
|
|
4751
|
+
height: virtualizer.getTotalSize(),
|
|
4752
|
+
position: "relative"
|
|
4753
|
+
},
|
|
4754
|
+
children: virtualizer.getVirtualItems().map((virtualItem) => {
|
|
4755
|
+
const message = deduplicatedMessages[virtualItem.index];
|
|
4756
|
+
return /* @__PURE__ */ jsx("div", {
|
|
4757
|
+
"data-index": virtualItem.index,
|
|
4758
|
+
ref: virtualizer.measureElement,
|
|
4759
|
+
style: {
|
|
4760
|
+
position: "absolute",
|
|
4761
|
+
top: 0,
|
|
4762
|
+
left: 0,
|
|
4763
|
+
width: "100%",
|
|
4764
|
+
transform: `translateY(${virtualItem.start}px)`
|
|
4765
|
+
},
|
|
4766
|
+
children: renderMessageBlock(message)
|
|
4767
|
+
}, message.id);
|
|
4768
|
+
})
|
|
4769
|
+
}) : messageElements,
|
|
4409
4770
|
interruptElement,
|
|
4410
4771
|
showCursor && /* @__PURE__ */ jsx("div", {
|
|
4411
4772
|
className: "cpk:mt-2",
|
|
@@ -4422,6 +4783,312 @@ CopilotChatMessageView.Cursor = function Cursor({ className, ...props }) {
|
|
|
4422
4783
|
});
|
|
4423
4784
|
};
|
|
4424
4785
|
|
|
4786
|
+
//#endregion
|
|
4787
|
+
//#region src/v2/components/chat/CopilotChatAttachmentQueue.tsx
|
|
4788
|
+
const CopilotChatAttachmentQueue = ({ attachments, onRemoveAttachment, className }) => {
|
|
4789
|
+
if (attachments.length === 0) return null;
|
|
4790
|
+
return /* @__PURE__ */ jsx("div", {
|
|
4791
|
+
className: cn("cpk:flex cpk:flex-wrap cpk:gap-2 cpk:p-2", className),
|
|
4792
|
+
children: attachments.map((attachment) => {
|
|
4793
|
+
const isMedia = attachment.type === "image" || attachment.type === "video";
|
|
4794
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4795
|
+
className: cn("cpk:relative cpk:inline-flex cpk:rounded-lg cpk:overflow-hidden cpk:border cpk:border-border", isMedia ? "cpk:w-[72px] cpk:h-[72px]" : attachment.type === "audio" ? "cpk:min-w-[200px] cpk:max-w-[280px] cpk:flex-col cpk:p-1 cpk:pr-8" : "cpk:p-2 cpk:px-3 cpk:pr-8 cpk:max-w-[240px]"),
|
|
4796
|
+
children: [
|
|
4797
|
+
attachment.status === "uploading" && /* @__PURE__ */ jsx(UploadingOverlay, {}),
|
|
4798
|
+
/* @__PURE__ */ jsx(AttachmentPreview, { attachment }),
|
|
4799
|
+
/* @__PURE__ */ jsx("button", {
|
|
4800
|
+
onClick: () => onRemoveAttachment(attachment.id),
|
|
4801
|
+
className: cn("cpk:absolute cpk:bg-black/60 cpk:text-white cpk:border-none cpk:rounded-full cpk:w-5 cpk:h-5 cpk:flex cpk:items-center cpk:justify-center cpk:cursor-pointer cpk:text-[10px] cpk:z-20", isMedia ? "cpk:top-1 cpk:right-1" : "cpk:top-1.5 cpk:right-1.5"),
|
|
4802
|
+
"aria-label": "Remove attachment",
|
|
4803
|
+
children: "✕"
|
|
4804
|
+
})
|
|
4805
|
+
]
|
|
4806
|
+
}, attachment.id);
|
|
4807
|
+
})
|
|
4808
|
+
});
|
|
4809
|
+
};
|
|
4810
|
+
function UploadingOverlay() {
|
|
4811
|
+
return /* @__PURE__ */ jsx("div", {
|
|
4812
|
+
className: "cpk:absolute cpk:inset-0 cpk:flex cpk:items-center cpk:justify-center cpk:bg-black/40 cpk:z-10",
|
|
4813
|
+
children: /* @__PURE__ */ jsx("div", { className: "cpk:w-5 cpk:h-5 cpk:border-2 cpk:border-white cpk:border-t-transparent cpk:rounded-full cpk:animate-spin" })
|
|
4814
|
+
});
|
|
4815
|
+
}
|
|
4816
|
+
function AttachmentPreview({ attachment }) {
|
|
4817
|
+
if (attachment.status === "uploading") return /* @__PURE__ */ jsx("div", { className: "cpk:w-full cpk:h-full" });
|
|
4818
|
+
switch (attachment.type) {
|
|
4819
|
+
case "image": return /* @__PURE__ */ jsx(ImagePreview, { attachment });
|
|
4820
|
+
case "audio": return /* @__PURE__ */ jsx(AudioPreview, { attachment });
|
|
4821
|
+
case "video": return /* @__PURE__ */ jsx(VideoPreview, { attachment });
|
|
4822
|
+
case "document": return /* @__PURE__ */ jsx(DocumentPreview, { attachment });
|
|
4823
|
+
}
|
|
4824
|
+
}
|
|
4825
|
+
function Lightbox({ onClose, children }) {
|
|
4826
|
+
useEffect(() => {
|
|
4827
|
+
const handleKey = (e) => {
|
|
4828
|
+
if (e.key === "Escape") onClose();
|
|
4829
|
+
};
|
|
4830
|
+
document.addEventListener("keydown", handleKey);
|
|
4831
|
+
return () => document.removeEventListener("keydown", handleKey);
|
|
4832
|
+
}, [onClose]);
|
|
4833
|
+
if (typeof document === "undefined") return null;
|
|
4834
|
+
return createPortal(/* @__PURE__ */ jsxs("div", {
|
|
4835
|
+
className: "cpk:fixed cpk:inset-0 cpk:z-[9999] cpk:flex cpk:items-center cpk:justify-center cpk:bg-black/80 cpk:animate-fade-in",
|
|
4836
|
+
onClick: onClose,
|
|
4837
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
4838
|
+
onClick: onClose,
|
|
4839
|
+
className: "cpk:absolute cpk:top-4 cpk:right-4 cpk:text-white cpk:bg-white/10 cpk:hover:bg-white/20 cpk:rounded-full cpk:w-10 cpk:h-10 cpk:flex cpk:items-center cpk:justify-center cpk:cursor-pointer cpk:border-none cpk:z-10",
|
|
4840
|
+
"aria-label": "Close preview",
|
|
4841
|
+
children: /* @__PURE__ */ jsx(X, { className: "cpk:w-5 cpk:h-5" })
|
|
4842
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
4843
|
+
onClick: (e) => e.stopPropagation(),
|
|
4844
|
+
children
|
|
4845
|
+
})]
|
|
4846
|
+
}), document.body);
|
|
4847
|
+
}
|
|
4848
|
+
/**
|
|
4849
|
+
* Hook that manages lightbox open/close and uses the View Transition API to
|
|
4850
|
+
* morph the thumbnail into fullscreen content.
|
|
4851
|
+
*
|
|
4852
|
+
* The trick: `view-transition-name` must live on exactly ONE element at a time.
|
|
4853
|
+
* - Old state (thumbnail visible): name is on the thumbnail.
|
|
4854
|
+
* - New state (lightbox visible): name moves to the lightbox content.
|
|
4855
|
+
* `flushSync` ensures React commits the DOM change synchronously inside the
|
|
4856
|
+
* `startViewTransition` callback so the API can snapshot old → new correctly.
|
|
4857
|
+
*/
|
|
4858
|
+
function useLightbox() {
|
|
4859
|
+
const thumbnailRef = useRef(null);
|
|
4860
|
+
const [open, setOpen] = useState(false);
|
|
4861
|
+
const vtName = useId();
|
|
4862
|
+
return {
|
|
4863
|
+
thumbnailRef,
|
|
4864
|
+
vtName,
|
|
4865
|
+
open,
|
|
4866
|
+
openLightbox: useCallback(() => {
|
|
4867
|
+
const thumb = thumbnailRef.current;
|
|
4868
|
+
const doc = document;
|
|
4869
|
+
if (doc.startViewTransition && thumb) {
|
|
4870
|
+
thumb.style.viewTransitionName = vtName;
|
|
4871
|
+
doc.startViewTransition(() => {
|
|
4872
|
+
thumb.style.viewTransitionName = "";
|
|
4873
|
+
flushSync(() => setOpen(true));
|
|
4874
|
+
});
|
|
4875
|
+
} else setOpen(true);
|
|
4876
|
+
}, []),
|
|
4877
|
+
closeLightbox: useCallback(() => {
|
|
4878
|
+
const thumb = thumbnailRef.current;
|
|
4879
|
+
const doc = document;
|
|
4880
|
+
if (doc.startViewTransition && thumb) doc.startViewTransition(() => {
|
|
4881
|
+
flushSync(() => setOpen(false));
|
|
4882
|
+
thumb.style.viewTransitionName = vtName;
|
|
4883
|
+
}).finished.then(() => {
|
|
4884
|
+
thumb.style.viewTransitionName = "";
|
|
4885
|
+
}).catch(() => {
|
|
4886
|
+
thumb.style.viewTransitionName = "";
|
|
4887
|
+
});
|
|
4888
|
+
else setOpen(false);
|
|
4889
|
+
}, [])
|
|
4890
|
+
};
|
|
4891
|
+
}
|
|
4892
|
+
function ImagePreview({ attachment }) {
|
|
4893
|
+
const src = getSourceUrl(attachment.source);
|
|
4894
|
+
const { thumbnailRef, vtName, open, openLightbox, closeLightbox } = useLightbox();
|
|
4895
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("img", {
|
|
4896
|
+
ref: thumbnailRef,
|
|
4897
|
+
src,
|
|
4898
|
+
alt: attachment.filename || "Image attachment",
|
|
4899
|
+
className: "cpk:w-full cpk:h-full cpk:object-cover cpk:cursor-pointer",
|
|
4900
|
+
onClick: openLightbox
|
|
4901
|
+
}), open && /* @__PURE__ */ jsx(Lightbox, {
|
|
4902
|
+
onClose: closeLightbox,
|
|
4903
|
+
children: /* @__PURE__ */ jsx("img", {
|
|
4904
|
+
style: { viewTransitionName: vtName },
|
|
4905
|
+
src,
|
|
4906
|
+
alt: attachment.filename || "Image attachment",
|
|
4907
|
+
className: "cpk:max-w-[90vw] cpk:max-h-[90vh] cpk:object-contain cpk:rounded-lg"
|
|
4908
|
+
})
|
|
4909
|
+
})] });
|
|
4910
|
+
}
|
|
4911
|
+
function AudioPreview({ attachment }) {
|
|
4912
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4913
|
+
className: "cpk:flex cpk:flex-col cpk:gap-1 cpk:w-full",
|
|
4914
|
+
children: [/* @__PURE__ */ jsx("audio", {
|
|
4915
|
+
src: getSourceUrl(attachment.source),
|
|
4916
|
+
controls: true,
|
|
4917
|
+
preload: "metadata",
|
|
4918
|
+
className: "cpk:w-full cpk:h-8"
|
|
4919
|
+
}), attachment.filename && /* @__PURE__ */ jsx("span", {
|
|
4920
|
+
className: "cpk:text-xs cpk:font-medium cpk:overflow-hidden cpk:text-ellipsis cpk:whitespace-nowrap",
|
|
4921
|
+
children: attachment.filename
|
|
4922
|
+
})]
|
|
4923
|
+
});
|
|
4924
|
+
}
|
|
4925
|
+
function VideoPreview({ attachment }) {
|
|
4926
|
+
const src = getSourceUrl(attachment.source);
|
|
4927
|
+
const { thumbnailRef, vtName, open, openLightbox, closeLightbox } = useLightbox();
|
|
4928
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
4929
|
+
/* @__PURE__ */ jsx("div", {
|
|
4930
|
+
ref: thumbnailRef,
|
|
4931
|
+
className: "cpk:w-full cpk:h-full",
|
|
4932
|
+
children: attachment.thumbnail ? /* @__PURE__ */ jsx("img", {
|
|
4933
|
+
src: attachment.thumbnail,
|
|
4934
|
+
alt: attachment.filename || "Video thumbnail",
|
|
4935
|
+
className: "cpk:w-full cpk:h-full cpk:object-cover"
|
|
4936
|
+
}) : /* @__PURE__ */ jsx("video", {
|
|
4937
|
+
src,
|
|
4938
|
+
preload: "metadata",
|
|
4939
|
+
muted: true,
|
|
4940
|
+
className: "cpk:w-full cpk:h-full cpk:object-cover"
|
|
4941
|
+
})
|
|
4942
|
+
}),
|
|
4943
|
+
/* @__PURE__ */ jsx("button", {
|
|
4944
|
+
onClick: openLightbox,
|
|
4945
|
+
className: "cpk:absolute cpk:inset-0 cpk:flex cpk:items-center cpk:justify-center cpk:z-10 cpk:cursor-pointer cpk:bg-black/20 cpk:border-none cpk:p-0",
|
|
4946
|
+
"aria-label": "Play video",
|
|
4947
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
4948
|
+
className: "cpk:w-8 cpk:h-8 cpk:rounded-full cpk:bg-black/60 cpk:flex cpk:items-center cpk:justify-center",
|
|
4949
|
+
children: /* @__PURE__ */ jsx(Play, { className: "cpk:w-4 cpk:h-4 cpk:text-white cpk:ml-0.5" })
|
|
4950
|
+
})
|
|
4951
|
+
}),
|
|
4952
|
+
open && /* @__PURE__ */ jsx(Lightbox, {
|
|
4953
|
+
onClose: closeLightbox,
|
|
4954
|
+
children: /* @__PURE__ */ jsx("video", {
|
|
4955
|
+
style: { viewTransitionName: vtName },
|
|
4956
|
+
src,
|
|
4957
|
+
controls: true,
|
|
4958
|
+
autoPlay: true,
|
|
4959
|
+
className: "cpk:max-w-[90vw] cpk:max-h-[90vh] cpk:rounded-lg"
|
|
4960
|
+
})
|
|
4961
|
+
})
|
|
4962
|
+
] });
|
|
4963
|
+
}
|
|
4964
|
+
function isPdf(mimeType) {
|
|
4965
|
+
return !!mimeType && mimeType.includes("pdf");
|
|
4966
|
+
}
|
|
4967
|
+
function isText(mimeType) {
|
|
4968
|
+
return !!mimeType && mimeType.startsWith("text/");
|
|
4969
|
+
}
|
|
4970
|
+
function canPreviewInBrowser(mimeType) {
|
|
4971
|
+
return isPdf(mimeType) || isText(mimeType);
|
|
4972
|
+
}
|
|
4973
|
+
/**
|
|
4974
|
+
* Convert a base64-encoded data source to a blob: URL that browsers will
|
|
4975
|
+
* render inside an iframe (data: URLs are blocked for PDFs in most browsers).
|
|
4976
|
+
*/
|
|
4977
|
+
function useBlobUrl(attachment) {
|
|
4978
|
+
const [url, setUrl] = useState(null);
|
|
4979
|
+
useEffect(() => {
|
|
4980
|
+
if (attachment.source.type !== "data") return;
|
|
4981
|
+
try {
|
|
4982
|
+
const binary = atob(attachment.source.value);
|
|
4983
|
+
const bytes = new Uint8Array(binary.length);
|
|
4984
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
4985
|
+
const blob = new Blob([bytes], { type: attachment.source.mimeType || "application/octet-stream" });
|
|
4986
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
4987
|
+
setUrl(blobUrl);
|
|
4988
|
+
return () => URL.revokeObjectURL(blobUrl);
|
|
4989
|
+
} catch (error) {
|
|
4990
|
+
console.error("[CopilotKit] Failed to decode attachment data:", error);
|
|
4991
|
+
setUrl(null);
|
|
4992
|
+
}
|
|
4993
|
+
}, [
|
|
4994
|
+
attachment.source.type,
|
|
4995
|
+
attachment.source.value,
|
|
4996
|
+
attachment.source.mimeType
|
|
4997
|
+
]);
|
|
4998
|
+
if (attachment.source.type === "url") return attachment.source.value;
|
|
4999
|
+
return url;
|
|
5000
|
+
}
|
|
5001
|
+
function DocumentLightboxContent({ attachment, vtName }) {
|
|
5002
|
+
const mimeType = attachment.source.mimeType;
|
|
5003
|
+
const blobUrl = useBlobUrl(attachment);
|
|
5004
|
+
if (isPdf(mimeType)) {
|
|
5005
|
+
if (!blobUrl) return null;
|
|
5006
|
+
return /* @__PURE__ */ jsx("iframe", {
|
|
5007
|
+
style: { viewTransitionName: vtName },
|
|
5008
|
+
src: blobUrl,
|
|
5009
|
+
title: attachment.filename || "PDF preview",
|
|
5010
|
+
className: "cpk:w-[90vw] cpk:h-[90vh] cpk:max-w-[1000px] cpk:rounded-lg cpk:bg-white"
|
|
5011
|
+
});
|
|
5012
|
+
}
|
|
5013
|
+
if (isText(mimeType)) {
|
|
5014
|
+
const textContent = attachment.source.type === "data" ? (() => {
|
|
5015
|
+
try {
|
|
5016
|
+
return atob(attachment.source.value);
|
|
5017
|
+
} catch {
|
|
5018
|
+
return attachment.source.value;
|
|
5019
|
+
}
|
|
5020
|
+
})() : null;
|
|
5021
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
5022
|
+
style: { viewTransitionName: vtName },
|
|
5023
|
+
className: "cpk:w-[90vw] cpk:max-w-[800px] cpk:max-h-[90vh] cpk:overflow-auto cpk:rounded-lg cpk:bg-white cpk:dark:bg-gray-900 cpk:p-6",
|
|
5024
|
+
children: [attachment.filename && /* @__PURE__ */ jsx("div", {
|
|
5025
|
+
className: "cpk:text-sm cpk:font-medium cpk:text-gray-500 cpk:dark:text-gray-400 cpk:mb-4 cpk:pb-2 cpk:border-b cpk:border-gray-200 cpk:dark:border-gray-700",
|
|
5026
|
+
children: attachment.filename
|
|
5027
|
+
}), textContent ? /* @__PURE__ */ jsx("pre", {
|
|
5028
|
+
className: "cpk:text-sm cpk:whitespace-pre-wrap cpk:break-words cpk:text-gray-800 cpk:dark:text-gray-200 cpk:font-mono cpk:m-0",
|
|
5029
|
+
children: textContent
|
|
5030
|
+
}) : blobUrl ? /* @__PURE__ */ jsx("iframe", {
|
|
5031
|
+
src: blobUrl,
|
|
5032
|
+
title: attachment.filename || "Text preview",
|
|
5033
|
+
className: "cpk:w-full cpk:h-[80vh] cpk:border-none"
|
|
5034
|
+
}) : null]
|
|
5035
|
+
});
|
|
5036
|
+
}
|
|
5037
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
5038
|
+
style: { viewTransitionName: vtName },
|
|
5039
|
+
className: "cpk:flex cpk:flex-col cpk:items-center cpk:gap-4 cpk:p-8 cpk:rounded-lg cpk:bg-white cpk:dark:bg-gray-900",
|
|
5040
|
+
children: [
|
|
5041
|
+
/* @__PURE__ */ jsx("div", {
|
|
5042
|
+
className: "cpk:w-16 cpk:h-16 cpk:rounded-xl cpk:bg-primary cpk:text-primary-foreground cpk:flex cpk:items-center cpk:justify-center cpk:text-xl cpk:font-bold",
|
|
5043
|
+
children: getDocumentIcon(mimeType ?? "")
|
|
5044
|
+
}),
|
|
5045
|
+
/* @__PURE__ */ jsxs("div", {
|
|
5046
|
+
className: "cpk:text-center",
|
|
5047
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
5048
|
+
className: "cpk:text-base cpk:font-medium cpk:text-gray-800 cpk:dark:text-gray-200",
|
|
5049
|
+
children: attachment.filename || "Document"
|
|
5050
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
5051
|
+
className: "cpk:text-sm cpk:text-gray-500 cpk:dark:text-gray-400 cpk:mt-1",
|
|
5052
|
+
children: [mimeType || "Unknown type", attachment.size != null && ` · ${formatFileSize(attachment.size)}`]
|
|
5053
|
+
})]
|
|
5054
|
+
}),
|
|
5055
|
+
/* @__PURE__ */ jsx("div", {
|
|
5056
|
+
className: "cpk:text-xs cpk:text-gray-400 cpk:dark:text-gray-500",
|
|
5057
|
+
children: "No preview available for this file type"
|
|
5058
|
+
})
|
|
5059
|
+
]
|
|
5060
|
+
});
|
|
5061
|
+
}
|
|
5062
|
+
function DocumentPreview({ attachment }) {
|
|
5063
|
+
const { thumbnailRef, vtName, open, openLightbox, closeLightbox } = useLightbox();
|
|
5064
|
+
const mimeType = attachment.source.mimeType;
|
|
5065
|
+
const previewable = canPreviewInBrowser(mimeType);
|
|
5066
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs("div", {
|
|
5067
|
+
ref: thumbnailRef,
|
|
5068
|
+
className: cn("cpk:flex cpk:items-center cpk:gap-2", previewable && "cpk:cursor-pointer"),
|
|
5069
|
+
onClick: previewable ? openLightbox : void 0,
|
|
5070
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
5071
|
+
className: "cpk:w-8 cpk:h-8 cpk:rounded-md cpk:bg-primary cpk:text-primary-foreground cpk:flex cpk:items-center cpk:justify-center cpk:text-[10px] cpk:font-semibold cpk:shrink-0",
|
|
5072
|
+
children: getDocumentIcon(mimeType ?? "")
|
|
5073
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
5074
|
+
className: "cpk:flex cpk:flex-col cpk:min-w-0",
|
|
5075
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
5076
|
+
className: "cpk:text-xs cpk:font-medium cpk:break-all cpk:leading-tight",
|
|
5077
|
+
children: attachment.filename || "Document"
|
|
5078
|
+
}), attachment.size != null && /* @__PURE__ */ jsx("span", {
|
|
5079
|
+
className: "cpk:text-[11px] cpk:text-muted-foreground",
|
|
5080
|
+
children: formatFileSize(attachment.size)
|
|
5081
|
+
})]
|
|
5082
|
+
})]
|
|
5083
|
+
}), open && /* @__PURE__ */ jsx(Lightbox, {
|
|
5084
|
+
onClose: closeLightbox,
|
|
5085
|
+
children: /* @__PURE__ */ jsx(DocumentLightboxContent, {
|
|
5086
|
+
attachment,
|
|
5087
|
+
vtName
|
|
5088
|
+
})
|
|
5089
|
+
})] });
|
|
5090
|
+
}
|
|
5091
|
+
|
|
4425
5092
|
//#endregion
|
|
4426
5093
|
//#region src/v2/hooks/use-keyboard-height.tsx
|
|
4427
5094
|
/**
|
|
@@ -4466,7 +5133,19 @@ function useKeyboardHeight() {
|
|
|
4466
5133
|
//#endregion
|
|
4467
5134
|
//#region src/v2/components/chat/CopilotChatView.tsx
|
|
4468
5135
|
const FEATHER_HEIGHT = 96;
|
|
4469
|
-
function
|
|
5136
|
+
function DropOverlay() {
|
|
5137
|
+
return /* @__PURE__ */ jsx("div", {
|
|
5138
|
+
className: cn("cpk:absolute cpk:inset-0 cpk:z-50 cpk:pointer-events-none", "cpk:flex cpk:items-center cpk:justify-center", "cpk:bg-primary/5 cpk:backdrop-blur-[2px]", "cpk:border-2 cpk:border-dashed cpk:border-primary/40 cpk:rounded-lg cpk:m-2"),
|
|
5139
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
5140
|
+
className: "cpk:flex cpk:flex-col cpk:items-center cpk:gap-2 cpk:text-primary/70",
|
|
5141
|
+
children: [/* @__PURE__ */ jsx(Upload, { className: "cpk:w-8 cpk:h-8" }), /* @__PURE__ */ jsx("span", {
|
|
5142
|
+
className: "cpk:text-sm cpk:font-medium",
|
|
5143
|
+
children: "Drop files here"
|
|
5144
|
+
})]
|
|
5145
|
+
})
|
|
5146
|
+
});
|
|
5147
|
+
}
|
|
5148
|
+
function CopilotChatView({ messageView, input, scrollView, suggestionView, welcomeScreen, messages = [], autoScroll = true, isRunning = false, suggestions, suggestionLoadingIndexes, onSelectSuggestion, onSubmitMessage, onStop, inputMode, inputValue, onInputChange, onStartTranscribe, onCancelTranscribe, onFinishTranscribe, onFinishTranscribeWithAudio, attachments, onRemoveAttachment, onAddFile, dragOver, onDragOver, onDragLeave, onDrop, disclaimer, children, className, ...props }) {
|
|
4470
5149
|
const inputContainerRef = useRef(null);
|
|
4471
5150
|
const [inputContainerHeight, setInputContainerHeight] = useState(0);
|
|
4472
5151
|
const [isResizing, setIsResizing] = useState(false);
|
|
@@ -4513,6 +5192,7 @@ function CopilotChatView({ messageView, input, scrollView, suggestionView, welco
|
|
|
4513
5192
|
onCancelTranscribe,
|
|
4514
5193
|
onFinishTranscribe,
|
|
4515
5194
|
onFinishTranscribeWithAudio,
|
|
5195
|
+
onAddFile,
|
|
4516
5196
|
positioning: "static",
|
|
4517
5197
|
keyboardHeight: isKeyboardOpen ? keyboardHeight : 0,
|
|
4518
5198
|
containerRef: inputContainerRef,
|
|
@@ -4553,21 +5233,34 @@ function CopilotChatView({ messageView, input, scrollView, suggestionView, welco
|
|
|
4553
5233
|
onCancelTranscribe,
|
|
4554
5234
|
onFinishTranscribe,
|
|
4555
5235
|
onFinishTranscribeWithAudio,
|
|
5236
|
+
onAddFile,
|
|
4556
5237
|
positioning: "static",
|
|
4557
5238
|
showDisclaimer: true,
|
|
4558
5239
|
...disclaimer !== void 0 ? { disclaimer } : {}
|
|
4559
5240
|
});
|
|
4560
|
-
const
|
|
4561
|
-
|
|
5241
|
+
const welcomeScreenSlot = welcomeScreen === true ? void 0 : welcomeScreen;
|
|
5242
|
+
const inputWithAttachments = /* @__PURE__ */ jsxs("div", {
|
|
5243
|
+
className: "cpk:w-full",
|
|
5244
|
+
children: [attachments && attachments.length > 0 && /* @__PURE__ */ jsx(CopilotChatAttachmentQueue, {
|
|
5245
|
+
attachments,
|
|
5246
|
+
onRemoveAttachment: (id) => onRemoveAttachment?.(id),
|
|
5247
|
+
className: "cpk:mb-2"
|
|
5248
|
+
}), BoundInputForWelcome]
|
|
5249
|
+
});
|
|
5250
|
+
const BoundWelcomeScreen = renderSlot(welcomeScreenSlot, CopilotChatView.WelcomeScreen, {
|
|
5251
|
+
input: inputWithAttachments,
|
|
4562
5252
|
suggestionView: BoundSuggestionView ?? /* @__PURE__ */ jsx(Fragment$1, {})
|
|
4563
5253
|
});
|
|
4564
|
-
return /* @__PURE__ */
|
|
5254
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4565
5255
|
"data-copilotkit": true,
|
|
4566
5256
|
"data-testid": "copilot-chat",
|
|
4567
5257
|
"data-copilot-running": isRunning ? "true" : "false",
|
|
4568
|
-
|
|
5258
|
+
onDragOver,
|
|
5259
|
+
onDragLeave,
|
|
5260
|
+
onDrop,
|
|
5261
|
+
className: cn("copilotKitChat cpk:relative cpk:h-full cpk:flex cpk:flex-col", className),
|
|
4569
5262
|
...props,
|
|
4570
|
-
children: BoundWelcomeScreen
|
|
5263
|
+
children: [dragOver && /* @__PURE__ */ jsx(DropOverlay, {}), BoundWelcomeScreen]
|
|
4571
5264
|
});
|
|
4572
5265
|
}
|
|
4573
5266
|
if (children) return /* @__PURE__ */ jsx("div", {
|
|
@@ -4584,39 +5277,66 @@ function CopilotChatView({ messageView, input, scrollView, suggestionView, welco
|
|
|
4584
5277
|
"data-copilotkit": true,
|
|
4585
5278
|
"data-testid": "copilot-chat",
|
|
4586
5279
|
"data-copilot-running": isRunning ? "true" : "false",
|
|
4587
|
-
|
|
5280
|
+
onDragOver,
|
|
5281
|
+
onDragLeave,
|
|
5282
|
+
onDrop,
|
|
5283
|
+
className: cn("copilotKitChat cpk:relative cpk:h-full cpk:flex cpk:flex-col", className),
|
|
4588
5284
|
...props,
|
|
4589
|
-
children: [
|
|
5285
|
+
children: [
|
|
5286
|
+
dragOver && /* @__PURE__ */ jsx(DropOverlay, {}),
|
|
5287
|
+
BoundScrollView,
|
|
5288
|
+
/* @__PURE__ */ jsx("div", {
|
|
5289
|
+
className: "cpk:max-w-3xl cpk:mx-auto cpk:w-full",
|
|
5290
|
+
children: attachments && attachments.length > 0 && /* @__PURE__ */ jsx(CopilotChatAttachmentQueue, {
|
|
5291
|
+
attachments,
|
|
5292
|
+
onRemoveAttachment: (id) => onRemoveAttachment?.(id),
|
|
5293
|
+
className: "cpk:px-4"
|
|
5294
|
+
})
|
|
5295
|
+
}),
|
|
5296
|
+
BoundInput
|
|
5297
|
+
]
|
|
4590
5298
|
});
|
|
4591
5299
|
}
|
|
4592
5300
|
(function(_CopilotChatView) {
|
|
4593
5301
|
const ScrollContent = ({ children, scrollToBottomButton, feather, inputContainerHeight, isResizing }) => {
|
|
4594
|
-
const { isAtBottom, scrollToBottom } = useStickToBottomContext();
|
|
5302
|
+
const { isAtBottom, scrollToBottom, scrollRef } = useStickToBottomContext();
|
|
5303
|
+
const [scrollEl, setScrollEl] = useState(null);
|
|
5304
|
+
useLayoutEffect(() => {
|
|
5305
|
+
setScrollEl(scrollRef.current ?? null);
|
|
5306
|
+
}, []);
|
|
4595
5307
|
const BoundFeather = renderSlot(feather, CopilotChatView.Feather, {});
|
|
4596
|
-
return /* @__PURE__ */
|
|
4597
|
-
|
|
4598
|
-
|
|
4599
|
-
|
|
4600
|
-
|
|
4601
|
-
|
|
4602
|
-
|
|
4603
|
-
|
|
4604
|
-
|
|
4605
|
-
children
|
|
5308
|
+
return /* @__PURE__ */ jsx(ScrollElementContext.Provider, {
|
|
5309
|
+
value: scrollEl,
|
|
5310
|
+
children: /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
5311
|
+
/* @__PURE__ */ jsx(StickToBottom.Content, {
|
|
5312
|
+
className: "cpk:overflow-y-auto cpk:overflow-x-hidden",
|
|
5313
|
+
style: {
|
|
5314
|
+
flex: "1 1 0%",
|
|
5315
|
+
minHeight: 0
|
|
5316
|
+
},
|
|
5317
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
5318
|
+
className: "cpk:px-4 cpk:sm:px-0 cpk:[div[data-sidebar-chat]_&]:px-8 cpk:[div[data-popup-chat]_&]:px-6",
|
|
5319
|
+
children
|
|
5320
|
+
})
|
|
5321
|
+
}),
|
|
5322
|
+
BoundFeather,
|
|
5323
|
+
!isAtBottom && !isResizing && /* @__PURE__ */ jsx("div", {
|
|
5324
|
+
className: "cpk:absolute cpk:inset-x-0 cpk:flex cpk:justify-center cpk:z-30 cpk:pointer-events-none",
|
|
5325
|
+
style: { bottom: `${inputContainerHeight + FEATHER_HEIGHT + 16}px` },
|
|
5326
|
+
children: renderSlot(scrollToBottomButton, CopilotChatView.ScrollToBottomButton, { onClick: () => scrollToBottom() })
|
|
4606
5327
|
})
|
|
4607
|
-
})
|
|
4608
|
-
|
|
4609
|
-
!isAtBottom && !isResizing && /* @__PURE__ */ jsx("div", {
|
|
4610
|
-
className: "cpk:absolute cpk:inset-x-0 cpk:flex cpk:justify-center cpk:z-30 cpk:pointer-events-none",
|
|
4611
|
-
style: { bottom: `${inputContainerHeight + FEATHER_HEIGHT + 16}px` },
|
|
4612
|
-
children: renderSlot(scrollToBottomButton, CopilotChatView.ScrollToBottomButton, { onClick: () => scrollToBottom() })
|
|
4613
|
-
})
|
|
4614
|
-
] });
|
|
5328
|
+
] })
|
|
5329
|
+
});
|
|
4615
5330
|
};
|
|
4616
5331
|
_CopilotChatView.ScrollView = ({ children, autoScroll = true, scrollToBottomButton, feather, inputContainerHeight = 0, isResizing = false, className, ...props }) => {
|
|
4617
5332
|
const [hasMounted, setHasMounted] = useState(false);
|
|
4618
5333
|
const { scrollRef, contentRef, scrollToBottom } = useStickToBottom();
|
|
4619
5334
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
|
5335
|
+
const [nonAutoScrollEl, setNonAutoScrollEl] = useState(null);
|
|
5336
|
+
const nonAutoScrollRefCallback = useCallback((el) => {
|
|
5337
|
+
scrollRef.current = el;
|
|
5338
|
+
setNonAutoScrollEl(el);
|
|
5339
|
+
}, []);
|
|
4620
5340
|
useEffect(() => {
|
|
4621
5341
|
setHasMounted(true);
|
|
4622
5342
|
}, []);
|
|
@@ -4645,23 +5365,26 @@ function CopilotChatView({ messageView, input, scrollView, suggestionView, welco
|
|
|
4645
5365
|
});
|
|
4646
5366
|
if (!autoScroll) {
|
|
4647
5367
|
const BoundFeather = renderSlot(feather, CopilotChatView.Feather, {});
|
|
4648
|
-
return /* @__PURE__ */
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
5368
|
+
return /* @__PURE__ */ jsx(ScrollElementContext.Provider, {
|
|
5369
|
+
value: nonAutoScrollEl,
|
|
5370
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
5371
|
+
ref: nonAutoScrollRefCallback,
|
|
5372
|
+
className: cn("cpk:h-full cpk:max-h-full cpk:flex cpk:flex-col cpk:min-h-0 cpk:overflow-y-auto cpk:overflow-x-hidden cpk:relative", className),
|
|
5373
|
+
...props,
|
|
5374
|
+
children: [
|
|
5375
|
+
/* @__PURE__ */ jsx("div", {
|
|
5376
|
+
ref: contentRef,
|
|
5377
|
+
className: "cpk:px-4 cpk:sm:px-0 cpk:[div[data-sidebar-chat]_&]:px-8 cpk:[div[data-popup-chat]_&]:px-6",
|
|
5378
|
+
children
|
|
5379
|
+
}),
|
|
5380
|
+
BoundFeather,
|
|
5381
|
+
showScrollButton && !isResizing && /* @__PURE__ */ jsx("div", {
|
|
5382
|
+
className: "cpk:absolute cpk:inset-x-0 cpk:flex cpk:justify-center cpk:z-30 cpk:pointer-events-none",
|
|
5383
|
+
style: { bottom: `${inputContainerHeight + FEATHER_HEIGHT + 16}px` },
|
|
5384
|
+
children: renderSlot(scrollToBottomButton, CopilotChatView.ScrollToBottomButton, { onClick: () => scrollToBottom() })
|
|
5385
|
+
})
|
|
5386
|
+
]
|
|
5387
|
+
})
|
|
4665
5388
|
});
|
|
4666
5389
|
}
|
|
4667
5390
|
return /* @__PURE__ */ jsx(StickToBottom, {
|
|
@@ -4853,7 +5576,7 @@ async function transcribeAudio(core, audioBlob, filename = "recording.webm") {
|
|
|
4853
5576
|
|
|
4854
5577
|
//#endregion
|
|
4855
5578
|
//#region src/v2/components/chat/CopilotChat.tsx
|
|
4856
|
-
function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen, onError, ...props }) {
|
|
5579
|
+
function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen, attachments: attachmentsConfig, onError, ...props }) {
|
|
4857
5580
|
const existingConfig = useCopilotChatConfiguration();
|
|
4858
5581
|
const resolvedAgentId = agentId ?? existingConfig?.agentId ?? DEFAULT_AGENT_ID;
|
|
4859
5582
|
const resolvedThreadId = useMemo(() => threadId ?? existingConfig?.threadId ?? randomUUID(), [threadId, existingConfig?.threadId]);
|
|
@@ -4889,6 +5612,7 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
|
|
|
4889
5612
|
const [inputValue, setInputValue] = useState("");
|
|
4890
5613
|
const [transcriptionError, setTranscriptionError] = useState(null);
|
|
4891
5614
|
const [isTranscribing, setIsTranscribing] = useState(false);
|
|
5615
|
+
const { attachments: selectedAttachments, enabled: attachmentsEnabled, dragOver, fileInputRef, containerRef: chatContainerRef, handleFileUpload, handleDragOver, handleDragLeave, handleDrop, removeAttachment, consumeAttachments } = useAttachments({ config: attachmentsConfig });
|
|
4892
5616
|
const isTranscriptionEnabled = copilotkit.audioFileTranscriptionEnabled;
|
|
4893
5617
|
const isMediaRecorderSupported = typeof window !== "undefined" && typeof MediaRecorder !== "undefined";
|
|
4894
5618
|
const { messageView: providedMessageView, suggestionView: providedSuggestionView, onStop: providedStopHandler, ...restProps } = props;
|
|
@@ -4916,7 +5640,31 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
|
|
|
4916
5640
|
resolvedAgentId
|
|
4917
5641
|
]);
|
|
4918
5642
|
const onSubmitInput = useCallback(async (value) => {
|
|
4919
|
-
|
|
5643
|
+
if (selectedAttachments.some((a) => a.status === "uploading")) {
|
|
5644
|
+
console.error("[CopilotKit] Cannot send while attachments are uploading");
|
|
5645
|
+
return;
|
|
5646
|
+
}
|
|
5647
|
+
const readyAttachments = consumeAttachments();
|
|
5648
|
+
if (readyAttachments.length > 0) {
|
|
5649
|
+
const contentParts = [];
|
|
5650
|
+
if (value.trim()) contentParts.push({
|
|
5651
|
+
type: "text",
|
|
5652
|
+
text: value
|
|
5653
|
+
});
|
|
5654
|
+
for (const att of readyAttachments) contentParts.push({
|
|
5655
|
+
type: att.type,
|
|
5656
|
+
source: att.source,
|
|
5657
|
+
metadata: {
|
|
5658
|
+
...att.filename ? { filename: att.filename } : {},
|
|
5659
|
+
...att.metadata
|
|
5660
|
+
}
|
|
5661
|
+
});
|
|
5662
|
+
agent.addMessage({
|
|
5663
|
+
id: randomUUID(),
|
|
5664
|
+
role: "user",
|
|
5665
|
+
content: contentParts
|
|
5666
|
+
});
|
|
5667
|
+
} else agent.addMessage({
|
|
4920
5668
|
id: randomUUID(),
|
|
4921
5669
|
role: "user",
|
|
4922
5670
|
content: value
|
|
@@ -4927,7 +5675,11 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
|
|
|
4927
5675
|
} catch (error) {
|
|
4928
5676
|
console.error("CopilotChat: runAgent failed", error);
|
|
4929
5677
|
}
|
|
4930
|
-
}, [
|
|
5678
|
+
}, [
|
|
5679
|
+
agent,
|
|
5680
|
+
selectedAttachments,
|
|
5681
|
+
consumeAttachments
|
|
5682
|
+
]);
|
|
4931
5683
|
const handleSelectSuggestion = useCallback(async (suggestion) => {
|
|
4932
5684
|
agent.addMessage({
|
|
4933
5685
|
id: randomUUID(),
|
|
@@ -5014,21 +5766,33 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
|
|
|
5014
5766
|
return () => clearTimeout(timer);
|
|
5015
5767
|
}
|
|
5016
5768
|
}, [transcriptionError]);
|
|
5017
|
-
const
|
|
5769
|
+
const stableMessageView = useShallowStableRef(typeof providedMessageView === "string" ? { className: providedMessageView } : providedMessageView);
|
|
5770
|
+
const stableSuggestionView = useShallowStableRef(providedSuggestionView);
|
|
5771
|
+
const handleAddFile = useCallback(() => {
|
|
5772
|
+
setTimeout(() => {
|
|
5773
|
+
fileInputRef.current?.click();
|
|
5774
|
+
}, 100);
|
|
5775
|
+
}, []);
|
|
5776
|
+
const mergedProps = {
|
|
5018
5777
|
isRunning: agent.isRunning,
|
|
5019
5778
|
suggestions: autoSuggestions,
|
|
5020
5779
|
onSelectSuggestion: handleSelectSuggestion,
|
|
5021
|
-
suggestionView:
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
});
|
|
5780
|
+
suggestionView: stableSuggestionView,
|
|
5781
|
+
...restProps
|
|
5782
|
+
};
|
|
5783
|
+
if (stableMessageView !== void 0) mergedProps.messageView = stableMessageView;
|
|
5026
5784
|
const hasMessages = agent.messages.length > 0;
|
|
5027
5785
|
const effectiveStopHandler = agent.isRunning && hasMessages ? providedStopHandler ?? stopCurrentRun : providedStopHandler;
|
|
5028
5786
|
const showTranscription = isTranscriptionEnabled && isMediaRecorderSupported;
|
|
5029
5787
|
const effectiveMode = isTranscribing ? "processing" : transcribeMode;
|
|
5030
|
-
const
|
|
5031
|
-
|
|
5788
|
+
const messages = useMemo(() => [...agent.messages], [agent.messages.map((m) => {
|
|
5789
|
+
const contentKey = typeof m.content === "string" ? m.content.length : Array.isArray(m.content) ? m.content.length : 0;
|
|
5790
|
+
const toolCallsKey = "toolCalls" in m && Array.isArray(m.toolCalls) ? m.toolCalls.map((tc) => `${tc.id}:${tc.function?.arguments?.length ?? 0}`).join(";") : "";
|
|
5791
|
+
return `${m.id}:${m.role}:${contentKey}:${toolCallsKey}`;
|
|
5792
|
+
}).join(",")]);
|
|
5793
|
+
const RenderedChatView = renderSlot(chatView, CopilotChatView, {
|
|
5794
|
+
...mergedProps,
|
|
5795
|
+
messages,
|
|
5032
5796
|
onSubmitMessage: onSubmitInput,
|
|
5033
5797
|
onStop: effectiveStopHandler,
|
|
5034
5798
|
inputMode: effectiveMode,
|
|
@@ -5037,32 +5801,51 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
|
|
|
5037
5801
|
onStartTranscribe: showTranscription ? handleStartTranscribe : void 0,
|
|
5038
5802
|
onCancelTranscribe: showTranscription ? handleCancelTranscribe : void 0,
|
|
5039
5803
|
onFinishTranscribe: showTranscription ? handleFinishTranscribe : void 0,
|
|
5040
|
-
onFinishTranscribeWithAudio: showTranscription ? handleFinishTranscribeWithAudio : void 0
|
|
5041
|
-
|
|
5042
|
-
|
|
5804
|
+
onFinishTranscribeWithAudio: showTranscription ? handleFinishTranscribeWithAudio : void 0,
|
|
5805
|
+
attachments: selectedAttachments,
|
|
5806
|
+
onRemoveAttachment: removeAttachment,
|
|
5807
|
+
onAddFile: attachmentsEnabled ? handleAddFile : void 0,
|
|
5808
|
+
dragOver,
|
|
5809
|
+
onDragOver: handleDragOver,
|
|
5810
|
+
onDragLeave: handleDragLeave,
|
|
5811
|
+
onDrop: handleDrop
|
|
5812
|
+
});
|
|
5813
|
+
return /* @__PURE__ */ jsx(CopilotChatConfigurationProvider, {
|
|
5043
5814
|
agentId: resolvedAgentId,
|
|
5044
5815
|
threadId: resolvedThreadId,
|
|
5045
5816
|
labels,
|
|
5046
5817
|
isModalDefaultOpen,
|
|
5047
|
-
children:
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
|
|
5065
|
-
|
|
5818
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
5819
|
+
ref: chatContainerRef,
|
|
5820
|
+
style: { display: "contents" },
|
|
5821
|
+
children: [
|
|
5822
|
+
attachmentsEnabled && /* @__PURE__ */ jsx("input", {
|
|
5823
|
+
type: "file",
|
|
5824
|
+
multiple: true,
|
|
5825
|
+
ref: fileInputRef,
|
|
5826
|
+
onChange: handleFileUpload,
|
|
5827
|
+
accept: attachmentsConfig?.accept ?? "*/*",
|
|
5828
|
+
style: { display: "none" }
|
|
5829
|
+
}),
|
|
5830
|
+
!isChatLicensed && /* @__PURE__ */ jsx(InlineFeatureWarning, { featureName: "Chat" }),
|
|
5831
|
+
transcriptionError && /* @__PURE__ */ jsx("div", {
|
|
5832
|
+
style: {
|
|
5833
|
+
position: "absolute",
|
|
5834
|
+
bottom: "100px",
|
|
5835
|
+
left: "50%",
|
|
5836
|
+
transform: "translateX(-50%)",
|
|
5837
|
+
backgroundColor: "#ef4444",
|
|
5838
|
+
color: "white",
|
|
5839
|
+
padding: "8px 16px",
|
|
5840
|
+
borderRadius: "8px",
|
|
5841
|
+
fontSize: "14px",
|
|
5842
|
+
zIndex: 50
|
|
5843
|
+
},
|
|
5844
|
+
children: transcriptionError
|
|
5845
|
+
}),
|
|
5846
|
+
RenderedChatView
|
|
5847
|
+
]
|
|
5848
|
+
})
|
|
5066
5849
|
});
|
|
5067
5850
|
}
|
|
5068
5851
|
(function(_CopilotChat) {
|
|
@@ -7643,5 +8426,5 @@ function validateProps(props) {
|
|
|
7643
8426
|
}
|
|
7644
8427
|
|
|
7645
8428
|
//#endregion
|
|
7646
|
-
export {
|
|
7647
|
-
//# sourceMappingURL=copilotkit-
|
|
8429
|
+
export { useRenderToolCall as $, CopilotChatSuggestionView as A, useConfigureSuggestions as B, CopilotChatToggleButton as C, CopilotChatView_default as D, CopilotChat as E, CopilotChatAssistantMessage_default as F, useRenderTool as G, useAgentContext as H, CopilotChatToolCallsView as I, useFrontendTool as J, defineToolCallRenderer as K, useAttachments as L, CopilotChatReasoningMessage_default as M, CopilotChatUserMessage_default as N, CopilotChatAttachmentQueue as O, CopilotChatAttachmentRenderer as P, useAgent as Q, useThreads$1 as R, CopilotModalHeader as S, DefaultOpenIcon as T, useHumanInTheLoop as U, useSuggestions as V, useDefaultRenderTool as W, useRenderCustomMessages as X, useRenderActivityMessage as Y, UseAgentUpdate as Z, WildcardToolCallRender as _, ThreadsProvider as a, MCPAppsActivityRenderer as at, CopilotPopupView as b, CoAgentStateRendersProvider as c, CopilotChatInput_default as ct, shouldShowDevConsole as d, CopilotChatConfigurationProvider as dt, CopilotKitProvider as et, useToast as f, useCopilotChatConfiguration as ft, useCopilotContext as g, CopilotContext as h, ThreadsContext as i, MCPAppsActivityContentSchema as it, CopilotChatSuggestionPill as j, CopilotChatMessageView as k, useCoAgentStateRenders as l, AudioRecorderError as lt, useCopilotMessagesContext as m, defaultCopilotContextCategories as n, CopilotKitCoreReact as nt, useThreads as o, MCPAppsActivityType as ot, CopilotMessagesContext as p, useComponent as q, CoAgentStateRenderBridge as r, createA2UIMessageRenderer as rt, CoAgentStateRendersContext as s, CopilotKitInspector as st, CopilotKit as t, useCopilotKit as tt, useAsyncCallback as u, CopilotChatAudioRecorder as ut, CopilotPopup as v, DefaultCloseIcon as w, CopilotSidebarView as x, CopilotSidebar as y, useInterrupt as z };
|
|
8430
|
+
//# sourceMappingURL=copilotkit-CF9UzB5N.mjs.map
|