@copilotkit/react-core 1.56.0 → 1.56.2-canary.pin-to-send

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.
Files changed (60) hide show
  1. package/dist/{copilotkit-BebqQrYT.mjs → copilotkit-BBYbekCa.mjs} +265 -76
  2. package/dist/copilotkit-BBYbekCa.mjs.map +1 -0
  3. package/dist/{copilotkit-Cvb6WpAX.cjs → copilotkit-D5JT2Pu3.cjs} +264 -75
  4. package/dist/copilotkit-D5JT2Pu3.cjs.map +1 -0
  5. package/dist/{copilotkit-f2Uq0RwG.d.mts → copilotkit-DArT2Iuw.d.mts} +71 -18
  6. package/dist/copilotkit-DArT2Iuw.d.mts.map +1 -0
  7. package/dist/{copilotkit-Dv8zU8_U.d.cts → copilotkit-KEc28l8G.d.cts} +71 -18
  8. package/dist/copilotkit-KEc28l8G.d.cts.map +1 -0
  9. package/dist/index.cjs +1 -1
  10. package/dist/index.d.cts +1 -1
  11. package/dist/index.d.mts +1 -1
  12. package/dist/index.mjs +1 -1
  13. package/dist/index.umd.js +30 -46
  14. package/dist/index.umd.js.map +1 -1
  15. package/dist/v2/index.cjs +1 -1
  16. package/dist/v2/index.css +1 -1
  17. package/dist/v2/index.d.cts +2 -2
  18. package/dist/v2/index.d.mts +2 -2
  19. package/dist/v2/index.mjs +1 -1
  20. package/dist/v2/index.umd.js +264 -79
  21. package/dist/v2/index.umd.js.map +1 -1
  22. package/package.json +6 -6
  23. package/src/components/CopilotListeners.tsx +15 -4
  24. package/src/components/__tests__/CopilotListeners.test.tsx +38 -0
  25. package/src/v2/components/chat/CopilotChat.tsx +80 -4
  26. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +4 -4
  27. package/src/v2/components/chat/CopilotChatInput.tsx +43 -2
  28. package/src/v2/components/chat/CopilotChatView.tsx +206 -11
  29. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +66 -0
  30. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +300 -2
  31. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +72 -0
  32. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +38 -0
  33. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +56 -0
  34. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +94 -0
  35. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -1
  36. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +37 -0
  37. package/src/v2/components/chat/index.ts +2 -0
  38. package/src/v2/components/chat/last-user-message-context.ts +21 -0
  39. package/src/v2/components/chat/normalize-auto-scroll.ts +17 -0
  40. package/src/v2/components/license-warning-banner.tsx +20 -1
  41. package/src/v2/components/ui/button.tsx +12 -11
  42. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +6 -0
  43. package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +6 -0
  44. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +76 -50
  45. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +219 -0
  46. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +55 -0
  47. package/src/v2/hooks/__tests__/use-threads.test.tsx +68 -0
  48. package/src/v2/hooks/use-agent.tsx +34 -77
  49. package/src/v2/hooks/use-pin-to-send.ts +94 -0
  50. package/src/v2/hooks/use-render-custom-messages.tsx +1 -1
  51. package/src/v2/hooks/use-render-tool-call.tsx +3 -0
  52. package/src/v2/hooks/use-render-tool.tsx +3 -0
  53. package/src/v2/hooks/use-threads.tsx +55 -12
  54. package/src/v2/providers/CopilotKitProvider.tsx +2 -11
  55. package/src/v2/types/defineToolCallRenderer.ts +3 -0
  56. package/src/v2/types/react-tool-call-renderer.ts +3 -0
  57. package/dist/copilotkit-BebqQrYT.mjs.map +0 -1
  58. package/dist/copilotkit-Cvb6WpAX.cjs.map +0 -1
  59. package/dist/copilotkit-Dv8zU8_U.d.cts.map +0 -1
  60. package/dist/copilotkit-f2Uq0RwG.d.mts.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
- const hasSuggestions = Array.isArray(suggestions) && suggestions.length > 0;
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
- const shouldShowWelcomeScreen = isEmpty && !welcomeScreenDisabled;
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 = true,
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
- const { scrollRef, contentRef, scrollToBottom } = useStickToBottom();
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 from useStickToBottom; safe to omit.
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 (autoScroll) return; // Skip for autoscroll mode
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, autoScroll]);
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
- // When autoScroll is false, we don't use StickToBottom
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
+ });