@copilotkit/react-core 1.56.1 → 1.56.2-canary.test-welcome-screen
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/dist/{copilotkit-CSJw5BG8.cjs → copilotkit-By2G6-Zx.cjs} +250 -63
- package/dist/copilotkit-By2G6-Zx.cjs.map +1 -0
- package/dist/{copilotkit-CCbxm6JM.d.mts → copilotkit-DFaI4j2r.d.mts} +64 -18
- package/dist/copilotkit-DFaI4j2r.d.mts.map +1 -0
- package/dist/{copilotkit-BtP7w7cT.d.cts → copilotkit-Dg4r4Gi_.d.cts} +64 -18
- package/dist/copilotkit-Dg4r4Gi_.d.cts.map +1 -0
- package/dist/{copilotkit-Cj2ZIxVr.mjs → copilotkit-PzJlPKcU.mjs} +251 -64
- package/dist/copilotkit-PzJlPKcU.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.umd.js +31 -44
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +1 -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 +1 -1
- package/dist/v2/index.umd.js +249 -66
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +6 -6
- package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +107 -0
- package/src/components/copilot-provider/copilotkit.tsx +6 -1
- package/src/context/__tests__/threads-context.test.tsx +116 -3
- package/src/context/threads-context.tsx +18 -1
- package/src/v2/components/chat/CopilotChat.tsx +91 -4
- package/src/v2/components/chat/CopilotChatInput.tsx +22 -0
- package/src/v2/components/chat/CopilotChatView.tsx +206 -11
- package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +66 -0
- package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +186 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +300 -2
- package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +56 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +94 -0
- package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -1
- package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +37 -0
- package/src/v2/components/chat/index.ts +2 -0
- package/src/v2/components/chat/last-user-message-context.ts +21 -0
- package/src/v2/components/chat/normalize-auto-scroll.ts +17 -0
- package/src/v2/components/license-warning-banner.tsx +20 -1
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +6 -0
- package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +6 -0
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +76 -50
- package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +219 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +68 -0
- package/src/v2/hooks/use-agent.tsx +34 -77
- package/src/v2/hooks/use-pin-to-send.ts +94 -0
- package/src/v2/hooks/use-threads.tsx +55 -12
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +29 -1
- package/src/v2/providers/CopilotKitProvider.tsx +2 -11
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +106 -0
- package/dist/copilotkit-BtP7w7cT.d.cts.map +0 -1
- package/dist/copilotkit-CCbxm6JM.d.mts.map +0 -1
- package/dist/copilotkit-CSJw5BG8.cjs.map +0 -1
- package/dist/copilotkit-Cj2ZIxVr.mjs.map +0 -1
|
@@ -33,10 +33,34 @@ import {
|
|
|
33
33
|
CopilotChatDefaultLabels,
|
|
34
34
|
} from "../../providers/CopilotChatConfigurationProvider";
|
|
35
35
|
import { useKeyboardHeight } from "../../hooks/use-keyboard-height";
|
|
36
|
+
import { normalizeAutoScroll } from "./normalize-auto-scroll";
|
|
37
|
+
import type { AutoScrollMode } from "./normalize-auto-scroll";
|
|
38
|
+
import { usePinToSend } from "../../hooks/use-pin-to-send";
|
|
36
39
|
|
|
37
40
|
// Height of the feather gradient overlay (h-24 = 6rem = 96px)
|
|
38
41
|
const FEATHER_HEIGHT = 96;
|
|
39
42
|
|
|
43
|
+
// Pin-to-send uses a softer, shorter feather than pin-to-bottom so readable
|
|
44
|
+
// content isn't obscured (h-12 = 3rem = 48px).
|
|
45
|
+
const PIN_TO_SEND_FEATHER_HEIGHT = 48;
|
|
46
|
+
|
|
47
|
+
const PinToSendSoftFeather: React.FC<React.HTMLAttributes<HTMLDivElement>> = ({
|
|
48
|
+
className,
|
|
49
|
+
style,
|
|
50
|
+
...props
|
|
51
|
+
}) => (
|
|
52
|
+
<div
|
|
53
|
+
className={cn(
|
|
54
|
+
"cpk:absolute cpk:bottom-0 cpk:left-0 cpk:right-4 cpk:h-12 cpk:pointer-events-none cpk:z-10 cpk:bg-gradient-to-t",
|
|
55
|
+
"cpk:from-white cpk:to-transparent",
|
|
56
|
+
"cpk:dark:from-[rgb(33,33,33)]",
|
|
57
|
+
className,
|
|
58
|
+
)}
|
|
59
|
+
style={style}
|
|
60
|
+
{...props}
|
|
61
|
+
/>
|
|
62
|
+
);
|
|
63
|
+
|
|
40
64
|
// Forward declaration for WelcomeScreen component type
|
|
41
65
|
export type WelcomeScreenProps = WithSlots<
|
|
42
66
|
{
|
|
@@ -57,7 +81,7 @@ export type CopilotChatViewProps = WithSlots<
|
|
|
57
81
|
},
|
|
58
82
|
{
|
|
59
83
|
messages?: Message[];
|
|
60
|
-
autoScroll?: boolean;
|
|
84
|
+
autoScroll?: AutoScrollMode | boolean;
|
|
61
85
|
isRunning?: boolean;
|
|
62
86
|
suggestions?: Suggestion[];
|
|
63
87
|
suggestionLoadingIndexes?: ReadonlyArray<number>;
|
|
@@ -81,6 +105,21 @@ export type CopilotChatViewProps = WithSlots<
|
|
|
81
105
|
onDragOver?: (e: React.DragEvent) => void;
|
|
82
106
|
onDragLeave?: (e: React.DragEvent) => void;
|
|
83
107
|
onDrop?: (e: React.DragEvent) => void;
|
|
108
|
+
/**
|
|
109
|
+
* When `true`, suppresses the welcome screen while a thread's initial
|
|
110
|
+
* connect is in flight. Prevents the "How can I help you today?" flash
|
|
111
|
+
* that would otherwise appear between mounting an empty cloned agent and
|
|
112
|
+
* the bootstrap messages arriving from /connect.
|
|
113
|
+
*/
|
|
114
|
+
isConnecting?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* When `true`, the caller has explicitly picked a thread (via `threadId`
|
|
117
|
+
* prop or `CopilotChatConfigurationProvider`). Suppresses the welcome
|
|
118
|
+
* screen unconditionally — a caller-managed thread targets a specific
|
|
119
|
+
* conversation and should render its messages (or an empty panel during
|
|
120
|
+
* connect) rather than a generic "start a new chat" greeting.
|
|
121
|
+
*/
|
|
122
|
+
hasExplicitThreadId?: boolean;
|
|
84
123
|
/**
|
|
85
124
|
* @deprecated Use the `input` slot's `disclaimer` prop instead:
|
|
86
125
|
* ```tsx
|
|
@@ -139,6 +178,8 @@ export function CopilotChatView({
|
|
|
139
178
|
onDragOver,
|
|
140
179
|
onDragLeave,
|
|
141
180
|
onDrop,
|
|
181
|
+
isConnecting = false,
|
|
182
|
+
hasExplicitThreadId = false,
|
|
142
183
|
// Deprecated — forwarded to input slot
|
|
143
184
|
disclaimer,
|
|
144
185
|
children,
|
|
@@ -219,10 +260,22 @@ export function CopilotChatView({
|
|
|
219
260
|
keyboardHeight: isKeyboardOpen ? keyboardHeight : 0,
|
|
220
261
|
containerRef: inputContainerRef,
|
|
221
262
|
showDisclaimer: true,
|
|
263
|
+
// This input is the last flex child of the chat column, so it sits at
|
|
264
|
+
// the bottom where the license banner would overlap. The welcome-screen
|
|
265
|
+
// input (below) intentionally omits this flag.
|
|
266
|
+
bottomAnchored: true,
|
|
222
267
|
...(disclaimer !== undefined ? { disclaimer } : {}),
|
|
223
268
|
} as CopilotChatInputProps);
|
|
224
269
|
|
|
225
|
-
|
|
270
|
+
// Hide suggestions while a thread is connecting or a run is in flight.
|
|
271
|
+
// Otherwise, mid-replay (bootstrap stream from /connect) or mid-run, the
|
|
272
|
+
// suggestions would render against a still-assembling message tree and
|
|
273
|
+
// visibly jump as each final text chunk reflows the layout.
|
|
274
|
+
const hasSuggestions =
|
|
275
|
+
!isConnecting &&
|
|
276
|
+
!isRunning &&
|
|
277
|
+
Array.isArray(suggestions) &&
|
|
278
|
+
suggestions.length > 0;
|
|
226
279
|
const BoundSuggestionView = hasSuggestions
|
|
227
280
|
? renderSlot(suggestionView, CopilotChatSuggestionView, {
|
|
228
281
|
suggestions,
|
|
@@ -258,7 +311,13 @@ export function CopilotChatView({
|
|
|
258
311
|
const isEmpty = messages.length === 0;
|
|
259
312
|
// Type assertion needed because TypeScript doesn't fully propagate `| boolean` through WithSlots
|
|
260
313
|
const welcomeScreenDisabled = (welcomeScreen as unknown) === false;
|
|
261
|
-
|
|
314
|
+
// Suppress the welcome screen (1) while the initial connect is in flight
|
|
315
|
+
// and (2) whenever the caller has picked a specific thread. The caller-
|
|
316
|
+
// managed case targets a conversation directly, so the generic welcome
|
|
317
|
+
// greeting is never the right thing to show — even for a thread that
|
|
318
|
+
// happens to have no messages yet.
|
|
319
|
+
const shouldShowWelcomeScreen =
|
|
320
|
+
isEmpty && !welcomeScreenDisabled && !isConnecting && !hasExplicitThreadId;
|
|
262
321
|
|
|
263
322
|
if (shouldShowWelcomeScreen) {
|
|
264
323
|
// Create a separate input for welcome screen with static positioning and disclaimer visible
|
|
@@ -442,9 +501,114 @@ export namespace CopilotChatView {
|
|
|
442
501
|
);
|
|
443
502
|
};
|
|
444
503
|
|
|
504
|
+
// Internal component for pin-to-send scroll behavior — not exported on CopilotChatView.
|
|
505
|
+
const PinToSendScrollContainer: React.FC<
|
|
506
|
+
React.HTMLAttributes<HTMLDivElement> & {
|
|
507
|
+
scrollRef: React.MutableRefObject<HTMLElement | null>;
|
|
508
|
+
contentRef: React.MutableRefObject<HTMLElement | null>;
|
|
509
|
+
scrollToBottom: () => void;
|
|
510
|
+
scrollToBottomButton?: SlotValue<
|
|
511
|
+
React.FC<React.ButtonHTMLAttributes<HTMLButtonElement>>
|
|
512
|
+
>;
|
|
513
|
+
feather?: SlotValue<React.FC<React.HTMLAttributes<HTMLDivElement>>>;
|
|
514
|
+
inputContainerHeight: number;
|
|
515
|
+
isResizing: boolean;
|
|
516
|
+
nonAutoScrollEl: HTMLElement | null;
|
|
517
|
+
nonAutoScrollRefCallback: (el: HTMLElement | null) => void;
|
|
518
|
+
showScrollButton: boolean;
|
|
519
|
+
}
|
|
520
|
+
> = ({
|
|
521
|
+
children,
|
|
522
|
+
scrollRef,
|
|
523
|
+
contentRef,
|
|
524
|
+
scrollToBottom,
|
|
525
|
+
scrollToBottomButton,
|
|
526
|
+
feather,
|
|
527
|
+
inputContainerHeight,
|
|
528
|
+
isResizing,
|
|
529
|
+
nonAutoScrollEl,
|
|
530
|
+
nonAutoScrollRefCallback,
|
|
531
|
+
showScrollButton,
|
|
532
|
+
className,
|
|
533
|
+
...props
|
|
534
|
+
}) => {
|
|
535
|
+
const spacerRef = useRef<HTMLDivElement>(null);
|
|
536
|
+
|
|
537
|
+
usePinToSend({
|
|
538
|
+
scrollRef,
|
|
539
|
+
contentRef,
|
|
540
|
+
spacerRef,
|
|
541
|
+
topOffset: 16,
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
// Pin-to-send uses a SOFTER feather than pin-to-bottom:
|
|
545
|
+
// - default: h-24 + from-white via-white to-transparent (fully opaque
|
|
546
|
+
// bottom half, aggressive). Good for streaming-to-bottom where
|
|
547
|
+
// the edge is always churning.
|
|
548
|
+
// - pin-to-send: h-12 + from-white to-transparent (gradual fade,
|
|
549
|
+
// no opaque midline). Gives a visual soft edge above the input
|
|
550
|
+
// without obscuring otherwise-readable content.
|
|
551
|
+
// Consumers can still override with the `feather` slot.
|
|
552
|
+
const BoundFeather = renderSlot(feather, PinToSendSoftFeather, {});
|
|
553
|
+
|
|
554
|
+
// Feather and scroll-to-bottom button live OUTSIDE the scroll container.
|
|
555
|
+
// `position: absolute` children of an `overflow: auto` element are
|
|
556
|
+
// positioned relative to the scroll *content*, which means they scroll
|
|
557
|
+
// away with it. Placing them as siblings of the scroll container
|
|
558
|
+
// (inside a `relative` wrapper) keeps them pinned to the viewport bottom.
|
|
559
|
+
return (
|
|
560
|
+
<ScrollElementContext.Provider value={nonAutoScrollEl}>
|
|
561
|
+
<div
|
|
562
|
+
className={cn(
|
|
563
|
+
"cpk:h-full cpk:max-h-full cpk:flex cpk:flex-col cpk:min-h-0 cpk:relative",
|
|
564
|
+
className,
|
|
565
|
+
)}
|
|
566
|
+
>
|
|
567
|
+
<div
|
|
568
|
+
ref={nonAutoScrollRefCallback}
|
|
569
|
+
className="cpk:flex-1 cpk:min-h-0 cpk:overflow-y-auto cpk:overflow-x-hidden"
|
|
570
|
+
{...props}
|
|
571
|
+
>
|
|
572
|
+
<div
|
|
573
|
+
ref={contentRef}
|
|
574
|
+
className="cpk:px-4 cpk:sm:px-0 cpk:[div[data-sidebar-chat]_&]:px-8 cpk:[div[data-popup-chat]_&]:px-6"
|
|
575
|
+
>
|
|
576
|
+
{children}
|
|
577
|
+
</div>
|
|
578
|
+
<div
|
|
579
|
+
ref={spacerRef}
|
|
580
|
+
data-pin-to-send-spacer
|
|
581
|
+
aria-hidden="true"
|
|
582
|
+
style={{ height: 0, flex: "0 0 auto" }}
|
|
583
|
+
/>
|
|
584
|
+
</div>
|
|
585
|
+
{/* Soft feather — pinned to wrapper bottom */}
|
|
586
|
+
{BoundFeather}
|
|
587
|
+
{/* Scroll to bottom button */}
|
|
588
|
+
{showScrollButton && !isResizing && (
|
|
589
|
+
<div
|
|
590
|
+
className="cpk:absolute cpk:inset-x-0 cpk:flex cpk:justify-center cpk:z-30 cpk:pointer-events-none"
|
|
591
|
+
style={{
|
|
592
|
+
bottom: `${inputContainerHeight + PIN_TO_SEND_FEATHER_HEIGHT + 16}px`,
|
|
593
|
+
}}
|
|
594
|
+
>
|
|
595
|
+
{renderSlot(
|
|
596
|
+
scrollToBottomButton,
|
|
597
|
+
CopilotChatView.ScrollToBottomButton,
|
|
598
|
+
{
|
|
599
|
+
onClick: () => scrollToBottom(),
|
|
600
|
+
},
|
|
601
|
+
)}
|
|
602
|
+
</div>
|
|
603
|
+
)}
|
|
604
|
+
</div>
|
|
605
|
+
</ScrollElementContext.Provider>
|
|
606
|
+
);
|
|
607
|
+
};
|
|
608
|
+
|
|
445
609
|
export const ScrollView: React.FC<
|
|
446
610
|
React.HTMLAttributes<HTMLDivElement> & {
|
|
447
|
-
autoScroll?: boolean;
|
|
611
|
+
autoScroll?: AutoScrollMode | boolean;
|
|
448
612
|
scrollToBottomButton?: SlotValue<
|
|
449
613
|
React.FC<React.ButtonHTMLAttributes<HTMLButtonElement>>
|
|
450
614
|
>;
|
|
@@ -454,7 +618,7 @@ export namespace CopilotChatView {
|
|
|
454
618
|
}
|
|
455
619
|
> = ({
|
|
456
620
|
children,
|
|
457
|
-
autoScroll =
|
|
621
|
+
autoScroll = "pin-to-bottom",
|
|
458
622
|
scrollToBottomButton,
|
|
459
623
|
feather,
|
|
460
624
|
inputContainerHeight = 0,
|
|
@@ -462,8 +626,18 @@ export namespace CopilotChatView {
|
|
|
462
626
|
className,
|
|
463
627
|
...props
|
|
464
628
|
}) => {
|
|
629
|
+
const mode = normalizeAutoScroll(autoScroll);
|
|
465
630
|
const [hasMounted, setHasMounted] = useState(false);
|
|
466
|
-
|
|
631
|
+
// Plain refs for the "none" and "pin-to-send" paths. Do NOT use
|
|
632
|
+
// useStickToBottom() here — its internal effects would attach scroll-following
|
|
633
|
+
// behavior to these refs and fight pin-to-send. The "pin-to-bottom" path
|
|
634
|
+
// gets its refs via <StickToBottom> below, scoped to that branch only.
|
|
635
|
+
const scrollRef = useRef<HTMLElement | null>(null);
|
|
636
|
+
const contentRef = useRef<HTMLElement | null>(null);
|
|
637
|
+
const scrollToBottom = useCallback(() => {
|
|
638
|
+
const el = scrollRef.current;
|
|
639
|
+
if (el) el.scrollTo({ top: el.scrollHeight, behavior: "smooth" });
|
|
640
|
+
}, []);
|
|
467
641
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
|
468
642
|
// Tracks the scroll container element for the non-autoScroll path so the
|
|
469
643
|
// context value is reactive (element state, not a ref).
|
|
@@ -478,7 +652,7 @@ export namespace CopilotChatView {
|
|
|
478
652
|
scrollRef.current = el;
|
|
479
653
|
setNonAutoScrollEl(el);
|
|
480
654
|
},
|
|
481
|
-
// scrollRef is a stable object
|
|
655
|
+
// scrollRef is a stable ref object; safe to omit.
|
|
482
656
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
483
657
|
[],
|
|
484
658
|
);
|
|
@@ -489,7 +663,7 @@ export namespace CopilotChatView {
|
|
|
489
663
|
|
|
490
664
|
// Monitor scroll position for non-autoscroll mode
|
|
491
665
|
useEffect(() => {
|
|
492
|
-
if (
|
|
666
|
+
if (mode === "pin-to-bottom") return; // Skip for autoscroll mode
|
|
493
667
|
|
|
494
668
|
const scrollElement = scrollRef.current;
|
|
495
669
|
if (!scrollElement) return;
|
|
@@ -514,7 +688,7 @@ export namespace CopilotChatView {
|
|
|
514
688
|
scrollElement.removeEventListener("scroll", checkScroll);
|
|
515
689
|
resizeObserver.disconnect();
|
|
516
690
|
};
|
|
517
|
-
}, [scrollRef,
|
|
691
|
+
}, [scrollRef, mode]);
|
|
518
692
|
|
|
519
693
|
if (!hasMounted) {
|
|
520
694
|
return (
|
|
@@ -526,8 +700,7 @@ export namespace CopilotChatView {
|
|
|
526
700
|
);
|
|
527
701
|
}
|
|
528
702
|
|
|
529
|
-
|
|
530
|
-
if (!autoScroll) {
|
|
703
|
+
if (mode === "none") {
|
|
531
704
|
const BoundFeather = renderSlot(feather, CopilotChatView.Feather, {});
|
|
532
705
|
|
|
533
706
|
return (
|
|
@@ -574,6 +747,28 @@ export namespace CopilotChatView {
|
|
|
574
747
|
);
|
|
575
748
|
}
|
|
576
749
|
|
|
750
|
+
if (mode === "pin-to-send") {
|
|
751
|
+
return (
|
|
752
|
+
<PinToSendScrollContainer
|
|
753
|
+
scrollRef={scrollRef}
|
|
754
|
+
contentRef={contentRef}
|
|
755
|
+
scrollToBottom={scrollToBottom}
|
|
756
|
+
scrollToBottomButton={scrollToBottomButton}
|
|
757
|
+
feather={feather}
|
|
758
|
+
inputContainerHeight={inputContainerHeight}
|
|
759
|
+
isResizing={isResizing}
|
|
760
|
+
nonAutoScrollEl={nonAutoScrollEl}
|
|
761
|
+
nonAutoScrollRefCallback={nonAutoScrollRefCallback}
|
|
762
|
+
showScrollButton={showScrollButton}
|
|
763
|
+
className={className}
|
|
764
|
+
{...props}
|
|
765
|
+
>
|
|
766
|
+
{children}
|
|
767
|
+
</PinToSendScrollContainer>
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// mode === "pin-to-bottom" (default)
|
|
577
772
|
return (
|
|
578
773
|
<StickToBottom
|
|
579
774
|
className={cn(
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { describe, it, expect, vi } from "vitest";
|
|
3
|
+
import { render } from "@testing-library/react";
|
|
4
|
+
import { EMPTY, Observable } from "rxjs";
|
|
5
|
+
import { type BaseEvent, type RunAgentInput } from "@ag-ui/client";
|
|
6
|
+
import { MockStepwiseAgent } from "../../../__tests__/utils/test-helpers";
|
|
7
|
+
import { CopilotKitProvider } from "../../../providers/CopilotKitProvider";
|
|
8
|
+
import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
|
|
9
|
+
import { CopilotChat } from "../CopilotChat";
|
|
10
|
+
|
|
11
|
+
describe("CopilotChat avoids /connect for locally-generated threadIds (ENT-314)", () => {
|
|
12
|
+
function buildAgentWithConnectSpy(): {
|
|
13
|
+
agent: MockStepwiseAgent;
|
|
14
|
+
connectSpy: ReturnType<typeof vi.fn>;
|
|
15
|
+
} {
|
|
16
|
+
const connectSpy = vi.fn();
|
|
17
|
+
class SpyAgent extends MockStepwiseAgent {
|
|
18
|
+
connect(input: RunAgentInput): Observable<BaseEvent> {
|
|
19
|
+
connectSpy(input);
|
|
20
|
+
return EMPTY;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return { agent: new SpyAgent(), connectSpy };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
it("does not call connect() when no threadId is supplied", async () => {
|
|
27
|
+
const { agent, connectSpy } = buildAgentWithConnectSpy();
|
|
28
|
+
|
|
29
|
+
render(
|
|
30
|
+
<CopilotKitProvider agents__unsafe_dev_only={{ default: agent }}>
|
|
31
|
+
<CopilotChat welcomeScreen={false} />
|
|
32
|
+
</CopilotKitProvider>,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
36
|
+
expect(connectSpy).not.toHaveBeenCalled();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("calls connect() when a threadId is supplied via props", async () => {
|
|
40
|
+
const { agent, connectSpy } = buildAgentWithConnectSpy();
|
|
41
|
+
|
|
42
|
+
render(
|
|
43
|
+
<CopilotKitProvider agents__unsafe_dev_only={{ default: agent }}>
|
|
44
|
+
<CopilotChat welcomeScreen={false} threadId="user-thread-abc" />
|
|
45
|
+
</CopilotKitProvider>,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
49
|
+
expect(connectSpy).toHaveBeenCalled();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("calls connect() when a threadId is supplied via configuration provider", async () => {
|
|
53
|
+
const { agent, connectSpy } = buildAgentWithConnectSpy();
|
|
54
|
+
|
|
55
|
+
render(
|
|
56
|
+
<CopilotKitProvider agents__unsafe_dev_only={{ default: agent }}>
|
|
57
|
+
<CopilotChatConfigurationProvider threadId="config-thread-xyz">
|
|
58
|
+
<CopilotChat welcomeScreen={false} />
|
|
59
|
+
</CopilotChatConfigurationProvider>
|
|
60
|
+
</CopilotKitProvider>,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
64
|
+
expect(connectSpy).toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
4
|
+
import { CopilotKitProvider } from "../../../providers/CopilotKitProvider";
|
|
5
|
+
import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
|
|
6
|
+
import { CopilotChat } from "../CopilotChat";
|
|
7
|
+
import { MockStepwiseAgent } from "../../../__tests__/utils/test-helpers";
|
|
8
|
+
import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Mock agent that records every connectAgent() invocation and resolves
|
|
12
|
+
* immediately with an empty run result. Tracking lives on the class so
|
|
13
|
+
* per-thread clones (from useAgent's WeakMap) share the counter.
|
|
14
|
+
*/
|
|
15
|
+
class TrackingAgent extends MockStepwiseAgent {
|
|
16
|
+
static connectCalls: Array<{
|
|
17
|
+
threadId: string | undefined;
|
|
18
|
+
agentId: string | undefined;
|
|
19
|
+
}> = [];
|
|
20
|
+
|
|
21
|
+
static reset() {
|
|
22
|
+
TrackingAgent.connectCalls = [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async connectAgent(
|
|
26
|
+
_params: unknown,
|
|
27
|
+
_subscriber: unknown,
|
|
28
|
+
): Promise<{ result: unknown; newMessages: [] }> {
|
|
29
|
+
TrackingAgent.connectCalls.push({
|
|
30
|
+
threadId: this.threadId,
|
|
31
|
+
agentId: this.agentId,
|
|
32
|
+
});
|
|
33
|
+
return { result: undefined, newMessages: [] };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function renderWithKit(ui: React.ReactNode, agent: TrackingAgent) {
|
|
38
|
+
return render(
|
|
39
|
+
<CopilotKitProvider agents__unsafe_dev_only={{ [DEFAULT_AGENT_ID]: agent }}>
|
|
40
|
+
<div style={{ height: 400 }}>{ui}</div>
|
|
41
|
+
</CopilotKitProvider>,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Regression coverage for fix/welcome-not-showing-at-all.
|
|
47
|
+
*
|
|
48
|
+
* The underlying bug: the v1 <CopilotKit> wrapper pipes a ThreadsProvider-
|
|
49
|
+
* minted UUID through to CopilotChatConfigurationProvider as `threadId`.
|
|
50
|
+
* CopilotChat previously treated any non-empty providedThreadId as "caller
|
|
51
|
+
* supplied a real backend thread" and (a) fired /connect (→ 404 for an
|
|
52
|
+
* auto-minted UUID) and (b) suppressed the welcome screen forever. The
|
|
53
|
+
* fix threads an `hasExplicitThreadId` signal through the provider chain;
|
|
54
|
+
* these tests pin the contract that /connect and welcome-screen gating
|
|
55
|
+
* now follow that signal rather than `!!threadId`.
|
|
56
|
+
*/
|
|
57
|
+
describe("CopilotChat welcome / connect integration", () => {
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
TrackingAgent.reset();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("v1 bridge scenario (config provider marks threadId as non-explicit)", () => {
|
|
63
|
+
it("does not call connectAgent and shows the welcome screen", async () => {
|
|
64
|
+
const agent = new TrackingAgent();
|
|
65
|
+
agent.agentId = DEFAULT_AGENT_ID;
|
|
66
|
+
|
|
67
|
+
renderWithKit(
|
|
68
|
+
<CopilotChatConfigurationProvider
|
|
69
|
+
threadId="auto-minted-uuid"
|
|
70
|
+
hasExplicitThreadId={false}
|
|
71
|
+
>
|
|
72
|
+
<CopilotChat />
|
|
73
|
+
</CopilotChatConfigurationProvider>,
|
|
74
|
+
agent,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Give the connect-effect a chance to misfire.
|
|
78
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
79
|
+
|
|
80
|
+
expect(TrackingAgent.connectCalls).toHaveLength(0);
|
|
81
|
+
expect(screen.getByTestId("copilot-welcome-screen")).toBeDefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("plain CopilotChat (no threadId anywhere)", () => {
|
|
86
|
+
it("does not call connectAgent and shows the welcome screen", async () => {
|
|
87
|
+
const agent = new TrackingAgent();
|
|
88
|
+
agent.agentId = DEFAULT_AGENT_ID;
|
|
89
|
+
|
|
90
|
+
renderWithKit(<CopilotChat />, agent);
|
|
91
|
+
|
|
92
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
93
|
+
|
|
94
|
+
expect(TrackingAgent.connectCalls).toHaveLength(0);
|
|
95
|
+
expect(screen.getByTestId("copilot-welcome-screen")).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("explicit threadId via CopilotChat prop", () => {
|
|
100
|
+
it("calls connectAgent with that threadId and suppresses the welcome screen", async () => {
|
|
101
|
+
const agent = new TrackingAgent();
|
|
102
|
+
agent.agentId = DEFAULT_AGENT_ID;
|
|
103
|
+
|
|
104
|
+
renderWithKit(<CopilotChat threadId="real-thread" />, agent);
|
|
105
|
+
|
|
106
|
+
await waitFor(() => {
|
|
107
|
+
expect(TrackingAgent.connectCalls.length).toBeGreaterThan(0);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// The per-thread clone carries threadId; agentId is the default.
|
|
111
|
+
expect(
|
|
112
|
+
TrackingAgent.connectCalls.some((c) => c.threadId === "real-thread"),
|
|
113
|
+
).toBe(true);
|
|
114
|
+
|
|
115
|
+
// Welcome screen is suppressed even after connect resolves, because the
|
|
116
|
+
// thread was caller-picked (hasExplicitThreadId=true).
|
|
117
|
+
expect(screen.queryByTestId("copilot-welcome-screen")).toBeNull();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("explicit threadId via wrapping CopilotChatConfigurationProvider", () => {
|
|
122
|
+
it("inherits explicitness from the provider and connects", async () => {
|
|
123
|
+
const agent = new TrackingAgent();
|
|
124
|
+
agent.agentId = DEFAULT_AGENT_ID;
|
|
125
|
+
|
|
126
|
+
renderWithKit(
|
|
127
|
+
<CopilotChatConfigurationProvider threadId="from-config">
|
|
128
|
+
<CopilotChat />
|
|
129
|
+
</CopilotChatConfigurationProvider>,
|
|
130
|
+
agent,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
await waitFor(() => {
|
|
134
|
+
expect(TrackingAgent.connectCalls.length).toBeGreaterThan(0);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expect(
|
|
138
|
+
TrackingAgent.connectCalls.some((c) => c.threadId === "from-config"),
|
|
139
|
+
).toBe(true);
|
|
140
|
+
expect(screen.queryByTestId("copilot-welcome-screen")).toBeNull();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("thread switch between two explicit threads", () => {
|
|
145
|
+
it("keeps the welcome screen hidden across the switch", async () => {
|
|
146
|
+
const agent = new TrackingAgent();
|
|
147
|
+
agent.agentId = DEFAULT_AGENT_ID;
|
|
148
|
+
|
|
149
|
+
const { rerender } = renderWithKit(
|
|
150
|
+
<CopilotChat threadId="thread-a" />,
|
|
151
|
+
agent,
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
await waitFor(() => {
|
|
155
|
+
expect(
|
|
156
|
+
TrackingAgent.connectCalls.some((c) => c.threadId === "thread-a"),
|
|
157
|
+
).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
// After thread-a's connect resolves, welcome must still be hidden
|
|
160
|
+
// because the thread is caller-picked.
|
|
161
|
+
expect(screen.queryByTestId("copilot-welcome-screen")).toBeNull();
|
|
162
|
+
|
|
163
|
+
rerender(
|
|
164
|
+
<CopilotKitProvider
|
|
165
|
+
agents__unsafe_dev_only={{ [DEFAULT_AGENT_ID]: agent }}
|
|
166
|
+
>
|
|
167
|
+
<div style={{ height: 400 }}>
|
|
168
|
+
<CopilotChat threadId="thread-b" />
|
|
169
|
+
</div>
|
|
170
|
+
</CopilotKitProvider>,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// During the switch (lastConnected="thread-a" !== "thread-b") isConnecting
|
|
174
|
+
// is true — welcome must not flash.
|
|
175
|
+
expect(screen.queryByTestId("copilot-welcome-screen")).toBeNull();
|
|
176
|
+
|
|
177
|
+
await waitFor(() => {
|
|
178
|
+
expect(
|
|
179
|
+
TrackingAgent.connectCalls.some((c) => c.threadId === "thread-b"),
|
|
180
|
+
).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
// And after thread-b's connect resolves.
|
|
183
|
+
expect(screen.queryByTestId("copilot-welcome-screen")).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|