@copilotkit/react-core 1.54.1 → 1.55.0-next.7

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 (183) hide show
  1. package/CHANGELOG.md +117 -116
  2. package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
  3. package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
  4. package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
  5. package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
  6. package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
  7. package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
  8. package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
  9. package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
  10. package/dist/index.cjs +27 -28
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +3 -3
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +3 -3
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +4 -5
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/index.umd.js +1941 -35
  19. package/dist/index.umd.js.map +1 -1
  20. package/dist/v2/index.cjs +77 -7
  21. package/dist/v2/index.css +1 -2
  22. package/dist/v2/index.d.cts +6 -4
  23. package/dist/v2/index.d.mts +6 -4
  24. package/dist/v2/index.mjs +7 -4
  25. package/dist/v2/index.umd.js +5725 -24
  26. package/dist/v2/index.umd.js.map +1 -1
  27. package/package.json +37 -9
  28. package/scripts/scope-preflight.mjs +101 -0
  29. package/src/components/CopilotListeners.tsx +2 -6
  30. package/src/components/copilot-provider/copilot-messages.tsx +1 -1
  31. package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
  32. package/src/components/copilot-provider/copilotkit.tsx +4 -4
  33. package/src/context/copilot-messages-context.tsx +1 -1
  34. package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
  35. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
  36. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
  37. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
  38. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
  39. package/src/hooks/use-agent-nodename.ts +1 -1
  40. package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
  41. package/src/hooks/use-coagent.ts +1 -1
  42. package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
  43. package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
  44. package/src/hooks/use-copilot-chat_internal.ts +2 -2
  45. package/src/hooks/use-copilot-readable.ts +1 -1
  46. package/src/hooks/use-frontend-tool.ts +2 -2
  47. package/src/hooks/use-human-in-the-loop.ts +2 -2
  48. package/src/hooks/use-langgraph-interrupt.ts +2 -5
  49. package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
  50. package/src/hooks/use-render-tool-call.ts +1 -1
  51. package/src/lib/copilot-task.ts +1 -1
  52. package/src/setupTests.ts +18 -14
  53. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
  54. package/src/v2/__tests__/globalSetup.ts +14 -0
  55. package/src/v2/__tests__/setup.ts +93 -0
  56. package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
  57. package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
  58. package/src/v2/components/CopilotKitInspector.tsx +50 -0
  59. package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
  60. package/src/v2/components/WildcardToolCallRender.tsx +86 -0
  61. package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
  62. package/src/v2/components/chat/CopilotChat.tsx +431 -0
  63. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
  64. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
  65. package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
  66. package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
  67. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
  68. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
  69. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
  70. package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
  71. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
  72. package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
  73. package/src/v2/components/chat/CopilotChatView.tsx +598 -0
  74. package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
  75. package/src/v2/components/chat/CopilotPopup.tsx +81 -0
  76. package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
  77. package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
  78. package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
  79. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
  80. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
  81. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
  82. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
  83. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
  84. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
  85. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
  86. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
  87. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
  88. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
  89. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
  90. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
  91. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
  92. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
  93. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
  94. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
  95. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
  96. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
  97. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
  98. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
  99. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
  100. package/src/v2/components/chat/__tests__/setup.ts +1 -0
  101. package/src/v2/components/chat/index.ts +79 -0
  102. package/src/v2/components/index.ts +7 -0
  103. package/src/v2/components/license-warning-banner.tsx +198 -0
  104. package/src/v2/components/ui/button.tsx +123 -0
  105. package/src/v2/components/ui/dropdown-menu.tsx +258 -0
  106. package/src/v2/components/ui/tooltip.tsx +60 -0
  107. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
  108. package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
  109. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
  110. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
  111. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
  112. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
  113. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
  114. package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
  115. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
  116. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
  117. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
  118. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
  119. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
  120. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
  121. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
  122. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
  123. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
  124. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
  125. package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
  126. package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
  127. package/src/v2/hooks/index.ts +18 -0
  128. package/src/v2/hooks/use-agent-context.tsx +45 -0
  129. package/src/v2/hooks/use-agent.tsx +155 -0
  130. package/src/v2/hooks/use-component.tsx +89 -0
  131. package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
  132. package/src/v2/hooks/use-default-render-tool.tsx +254 -0
  133. package/src/v2/hooks/use-frontend-tool.tsx +43 -0
  134. package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
  135. package/src/v2/hooks/use-interrupt.tsx +305 -0
  136. package/src/v2/hooks/use-keyboard-height.tsx +67 -0
  137. package/src/v2/hooks/use-render-activity-message.tsx +73 -0
  138. package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
  139. package/src/v2/hooks/use-render-tool-call.tsx +175 -0
  140. package/src/v2/hooks/use-render-tool.tsx +181 -0
  141. package/src/v2/hooks/use-suggestions.tsx +91 -0
  142. package/src/v2/hooks/use-threads.tsx +256 -0
  143. package/src/v2/hooks/useKatexStyles.ts +27 -0
  144. package/src/v2/index.css +1 -1
  145. package/src/v2/index.ts +18 -2
  146. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
  147. package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
  148. package/src/v2/lib/react-core.ts +156 -0
  149. package/src/v2/lib/slots.tsx +143 -0
  150. package/src/v2/lib/transcription-client.ts +184 -0
  151. package/src/v2/lib/utils.ts +8 -0
  152. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
  153. package/src/v2/providers/CopilotKitProvider.tsx +600 -0
  154. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
  155. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
  156. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
  157. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
  158. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
  159. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
  160. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
  161. package/src/v2/providers/index.ts +14 -0
  162. package/src/v2/styles/globals.css +230 -0
  163. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
  164. package/src/v2/types/defineToolCallRenderer.ts +65 -0
  165. package/src/v2/types/frontend-tool.ts +8 -0
  166. package/src/v2/types/human-in-the-loop.ts +33 -0
  167. package/src/v2/types/index.ts +7 -0
  168. package/src/v2/types/interrupt.ts +15 -0
  169. package/src/v2/types/react-activity-message-renderer.ts +27 -0
  170. package/src/v2/types/react-custom-message-renderer.ts +17 -0
  171. package/src/v2/types/react-tool-call-renderer.ts +32 -0
  172. package/tsdown.config.ts +34 -10
  173. package/vitest.config.mjs +4 -3
  174. package/LICENSE +0 -21
  175. package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
  176. package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
  177. package/dist/copilotkit-C94ayZbs.cjs +0 -2161
  178. package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
  179. package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
  180. package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
  181. package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
  182. package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
  183. package/dist/v2/index.css.map +0 -1
@@ -0,0 +1,252 @@
1
+ import { ReasoningMessage, Message } from "@ag-ui/core";
2
+ import { useState, useEffect, useRef } from "react";
3
+ import { ChevronRight } from "lucide-react";
4
+ import { twMerge } from "tailwind-merge";
5
+ import { Streamdown } from "streamdown";
6
+ import { WithSlots, renderSlot } from "../../lib/slots";
7
+
8
+ export type CopilotChatReasoningMessageProps = WithSlots<
9
+ {
10
+ header: typeof CopilotChatReasoningMessage.Header;
11
+ contentView: typeof CopilotChatReasoningMessage.Content;
12
+ toggle: typeof CopilotChatReasoningMessage.Toggle;
13
+ },
14
+ {
15
+ message: ReasoningMessage;
16
+ messages?: Message[];
17
+ isRunning?: boolean;
18
+ } & React.HTMLAttributes<HTMLDivElement>
19
+ >;
20
+
21
+ /**
22
+ * Formats an elapsed duration (in seconds) to a human-readable string.
23
+ */
24
+ function formatDuration(seconds: number): string {
25
+ if (seconds < 1) return "a few seconds";
26
+ if (seconds < 60) return `${Math.round(seconds)} seconds`;
27
+ const mins = Math.floor(seconds / 60);
28
+ const secs = Math.round(seconds % 60);
29
+ if (secs === 0) return `${mins} minute${mins > 1 ? "s" : ""}`;
30
+ return `${mins}m ${secs}s`;
31
+ }
32
+
33
+ export function CopilotChatReasoningMessage({
34
+ message,
35
+ messages,
36
+ isRunning,
37
+ header,
38
+ contentView,
39
+ toggle,
40
+ children,
41
+ className,
42
+ ...props
43
+ }: CopilotChatReasoningMessageProps) {
44
+ const isLatest = messages?.[messages.length - 1]?.id === message.id;
45
+ const isStreaming = !!(isRunning && isLatest);
46
+ const hasContent = !!(message.content && message.content.length > 0);
47
+
48
+ // Track elapsed time while streaming
49
+ const startTimeRef = useRef<number | null>(null);
50
+ const [elapsed, setElapsed] = useState(0);
51
+
52
+ useEffect(() => {
53
+ if (isStreaming && startTimeRef.current === null) {
54
+ startTimeRef.current = Date.now();
55
+ }
56
+
57
+ if (!isStreaming && startTimeRef.current !== null) {
58
+ // Final snapshot of elapsed time
59
+ setElapsed((Date.now() - startTimeRef.current) / 1000);
60
+ return;
61
+ }
62
+
63
+ if (!isStreaming) return;
64
+
65
+ // Tick every second while streaming
66
+ const timer = setInterval(() => {
67
+ if (startTimeRef.current !== null) {
68
+ setElapsed((Date.now() - startTimeRef.current) / 1000);
69
+ }
70
+ }, 1000);
71
+ return () => clearInterval(timer);
72
+ }, [isStreaming]);
73
+
74
+ // Default to open while streaming, auto-collapse when streaming ends
75
+ const [isOpen, setIsOpen] = useState(isStreaming);
76
+
77
+ useEffect(() => {
78
+ if (isStreaming) {
79
+ setIsOpen(true);
80
+ } else {
81
+ // Auto-collapse when reasoning finishes
82
+ setIsOpen(false);
83
+ }
84
+ }, [isStreaming]);
85
+
86
+ const label = isStreaming
87
+ ? "Thinking…"
88
+ : `Thought for ${formatDuration(elapsed)}`;
89
+
90
+ const boundHeader = renderSlot(header, CopilotChatReasoningMessage.Header, {
91
+ isOpen,
92
+ label,
93
+ hasContent,
94
+ isStreaming,
95
+ onClick: hasContent ? () => setIsOpen((prev) => !prev) : undefined,
96
+ });
97
+
98
+ const boundContent = renderSlot(
99
+ contentView,
100
+ CopilotChatReasoningMessage.Content,
101
+ {
102
+ isStreaming,
103
+ hasContent,
104
+ children: message.content,
105
+ },
106
+ );
107
+
108
+ const boundToggle = renderSlot(toggle, CopilotChatReasoningMessage.Toggle, {
109
+ isOpen,
110
+ children: boundContent,
111
+ });
112
+
113
+ if (children) {
114
+ return (
115
+ <div data-copilotkit style={{ display: "contents" }}>
116
+ {children({
117
+ header: boundHeader,
118
+ contentView: boundContent,
119
+ toggle: boundToggle,
120
+ message,
121
+ messages,
122
+ isRunning,
123
+ })}
124
+ </div>
125
+ );
126
+ }
127
+
128
+ return (
129
+ <div
130
+ className={twMerge("cpk:my-1", className)}
131
+ data-message-id={message.id}
132
+ {...props}
133
+ >
134
+ {boundHeader}
135
+ {boundToggle}
136
+ </div>
137
+ );
138
+ }
139
+
140
+ export namespace CopilotChatReasoningMessage {
141
+ export const Header: React.FC<
142
+ React.ButtonHTMLAttributes<HTMLButtonElement> & {
143
+ isOpen?: boolean;
144
+ label?: string;
145
+ hasContent?: boolean;
146
+ isStreaming?: boolean;
147
+ }
148
+ > = ({
149
+ isOpen,
150
+ label = "Thoughts",
151
+ hasContent,
152
+ isStreaming,
153
+ className,
154
+ children: headerChildren,
155
+ ...headerProps
156
+ }) => {
157
+ const isExpandable = !!hasContent;
158
+
159
+ return (
160
+ <button
161
+ type="button"
162
+ className={twMerge(
163
+ "cpk:inline-flex cpk:items-center cpk:gap-1 cpk:py-1 cpk:text-sm cpk:text-muted-foreground cpk:transition-colors cpk:select-none",
164
+ isExpandable
165
+ ? "cpk:hover:text-foreground cpk:cursor-pointer"
166
+ : "cpk:cursor-default",
167
+ className,
168
+ )}
169
+ aria-expanded={isExpandable ? isOpen : undefined}
170
+ {...headerProps}
171
+ >
172
+ <span className="cpk:font-medium">{label}</span>
173
+ {isStreaming && !hasContent && (
174
+ <span className="cpk:inline-flex cpk:items-center cpk:ml-1">
175
+ <span className="cpk:w-1.5 cpk:h-1.5 cpk:rounded-full cpk:bg-muted-foreground cpk:animate-pulse" />
176
+ </span>
177
+ )}
178
+ {headerChildren}
179
+ {isExpandable && (
180
+ <ChevronRight
181
+ className={twMerge(
182
+ "cpk:size-3.5 cpk:shrink-0 cpk:transition-transform cpk:duration-200",
183
+ isOpen && "cpk:rotate-90",
184
+ )}
185
+ />
186
+ )}
187
+ </button>
188
+ );
189
+ };
190
+
191
+ export const Content: React.FC<
192
+ React.HTMLAttributes<HTMLDivElement> & {
193
+ isStreaming?: boolean;
194
+ hasContent?: boolean;
195
+ }
196
+ > = ({
197
+ isStreaming,
198
+ hasContent,
199
+ className,
200
+ children: contentChildren,
201
+ ...contentProps
202
+ }) => {
203
+ // Don't render the content area at all when there's nothing to show
204
+ if (!hasContent && !isStreaming) return null;
205
+
206
+ return (
207
+ <div
208
+ className={twMerge("cpk:pb-2 cpk:pt-1", className)}
209
+ {...contentProps}
210
+ >
211
+ <div className="cpk:text-sm cpk:text-muted-foreground">
212
+ <Streamdown>
213
+ {typeof contentChildren === "string" ? contentChildren : ""}
214
+ </Streamdown>
215
+ {isStreaming && hasContent && (
216
+ <span className="cpk:inline-flex cpk:items-center cpk:ml-1 cpk:align-middle">
217
+ <span className="cpk:w-2 cpk:h-2 cpk:rounded-full cpk:bg-muted-foreground cpk:animate-pulse-cursor" />
218
+ </span>
219
+ )}
220
+ </div>
221
+ </div>
222
+ );
223
+ };
224
+
225
+ export const Toggle: React.FC<
226
+ React.HTMLAttributes<HTMLDivElement> & {
227
+ isOpen?: boolean;
228
+ }
229
+ > = ({ isOpen, className, children: toggleChildren, ...toggleProps }) => {
230
+ return (
231
+ <div
232
+ className={twMerge(
233
+ "cpk:grid cpk:transition-[grid-template-rows] cpk:duration-200 cpk:ease-in-out",
234
+ className,
235
+ )}
236
+ style={{ gridTemplateRows: isOpen ? "1fr" : "0fr" }}
237
+ {...toggleProps}
238
+ >
239
+ <div className="cpk:overflow-hidden">{toggleChildren}</div>
240
+ </div>
241
+ );
242
+ };
243
+ }
244
+
245
+ CopilotChatReasoningMessage.Header.displayName =
246
+ "CopilotChatReasoningMessage.Header";
247
+ CopilotChatReasoningMessage.Content.displayName =
248
+ "CopilotChatReasoningMessage.Content";
249
+ CopilotChatReasoningMessage.Toggle.displayName =
250
+ "CopilotChatReasoningMessage.Toggle";
251
+
252
+ export default CopilotChatReasoningMessage;
@@ -0,0 +1,59 @@
1
+ import React from "react";
2
+ import { Loader2 } from "lucide-react";
3
+ import { cn } from "../../lib/utils";
4
+
5
+ export interface CopilotChatSuggestionPillProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
6
+ /** Optional icon to render on the left side when not loading. */
7
+ icon?: React.ReactNode;
8
+ /** Whether the pill should display a loading spinner. */
9
+ isLoading?: boolean;
10
+ }
11
+
12
+ const baseClasses =
13
+ "group cpk:inline-flex cpk:h-7 cpk:sm:h-8 cpk:items-center cpk:gap-1 cpk:sm:gap-1.5 cpk:rounded-full cpk:border cpk:border-border/60 cpk:bg-background cpk:px-2.5 cpk:sm:px-3 cpk:text-[11px] cpk:sm:text-xs cpk:leading-none cpk:text-foreground cpk:transition-colors cpk:cursor-pointer cpk:hover:bg-accent/60 cpk:hover:text-foreground cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-ring cpk:focus-visible:ring-offset-2 cpk:focus-visible:ring-offset-background cpk:disabled:cursor-not-allowed cpk:disabled:text-muted-foreground cpk:disabled:hover:bg-background cpk:disabled:hover:text-muted-foreground cpk:pointer-events-auto";
14
+
15
+ const labelClasses = "cpk:whitespace-nowrap cpk:font-medium cpk:leading-none";
16
+
17
+ export const CopilotChatSuggestionPill = React.forwardRef<
18
+ HTMLButtonElement,
19
+ CopilotChatSuggestionPillProps
20
+ >(function CopilotChatSuggestionPill(
21
+ { className, children, icon, isLoading, type, ...props },
22
+ ref,
23
+ ) {
24
+ const showIcon = !isLoading && icon;
25
+
26
+ return (
27
+ <button
28
+ ref={ref}
29
+ data-copilotkit
30
+ data-testid="copilot-suggestion"
31
+ data-slot="suggestion-pill"
32
+ className={cn(baseClasses, className)}
33
+ type={type ?? "button"}
34
+ aria-busy={isLoading || undefined}
35
+ disabled={isLoading || props.disabled}
36
+ {...props}
37
+ >
38
+ {isLoading ? (
39
+ <span className="cpk:flex cpk:h-3.5 cpk:sm:h-4 cpk:w-3.5 cpk:sm:w-4 cpk:items-center cpk:justify-center cpk:text-muted-foreground">
40
+ <Loader2
41
+ className="cpk:h-3.5 cpk:sm:h-4 cpk:w-3.5 cpk:sm:w-4 cpk:animate-spin"
42
+ aria-hidden="true"
43
+ />
44
+ </span>
45
+ ) : (
46
+ showIcon && (
47
+ <span className="cpk:flex cpk:h-3.5 cpk:sm:h-4 cpk:w-3.5 cpk:sm:w-4 cpk:items-center cpk:justify-center cpk:text-muted-foreground">
48
+ {icon}
49
+ </span>
50
+ )
51
+ )}
52
+ <span className={labelClasses}>{children}</span>
53
+ </button>
54
+ );
55
+ });
56
+
57
+ CopilotChatSuggestionPill.displayName = "CopilotChatSuggestionPill";
58
+
59
+ export default CopilotChatSuggestionPill;
@@ -0,0 +1,133 @@
1
+ import React from "react";
2
+ import { Suggestion } from "@copilotkit/core";
3
+ import { renderSlot, WithSlots } from "../../lib/slots";
4
+ import { cn } from "../../lib/utils";
5
+ import CopilotChatSuggestionPill, {
6
+ CopilotChatSuggestionPillProps,
7
+ } from "./CopilotChatSuggestionPill";
8
+
9
+ const DefaultContainer = React.forwardRef<
10
+ HTMLDivElement,
11
+ React.HTMLAttributes<HTMLDivElement>
12
+ >(function DefaultContainer({ className, ...props }, ref) {
13
+ return (
14
+ <div
15
+ ref={ref}
16
+ data-copilotkit
17
+ data-testid="copilot-suggestions"
18
+ className={cn(
19
+ "cpk:flex cpk:flex-wrap cpk:items-center cpk:gap-1.5 cpk:sm:gap-2 cpk:pl-0 cpk:pr-4 cpk:sm:px-0 cpk:pointer-events-none",
20
+ className,
21
+ )}
22
+ {...props}
23
+ />
24
+ );
25
+ });
26
+
27
+ export type CopilotChatSuggestionViewProps = WithSlots<
28
+ {
29
+ container: typeof DefaultContainer;
30
+ suggestion: typeof CopilotChatSuggestionPill;
31
+ },
32
+ {
33
+ suggestions: Suggestion[];
34
+ onSelectSuggestion?: (suggestion: Suggestion, index: number) => void;
35
+ loadingIndexes?: ReadonlyArray<number>;
36
+ } & React.HTMLAttributes<HTMLDivElement>
37
+ >;
38
+
39
+ export const CopilotChatSuggestionView = React.forwardRef<
40
+ HTMLDivElement,
41
+ CopilotChatSuggestionViewProps
42
+ >(function CopilotChatSuggestionView(
43
+ {
44
+ suggestions,
45
+ onSelectSuggestion,
46
+ loadingIndexes,
47
+ container,
48
+ suggestion: suggestionSlot,
49
+ className,
50
+ children,
51
+ ...restProps
52
+ },
53
+ ref,
54
+ ) {
55
+ const loadingSet = React.useMemo(() => {
56
+ if (!loadingIndexes || loadingIndexes.length === 0) {
57
+ return new Set<number>();
58
+ }
59
+ return new Set(loadingIndexes);
60
+ }, [loadingIndexes]);
61
+
62
+ const ContainerElement = renderSlot(container, DefaultContainer, {
63
+ ref,
64
+ className,
65
+ ...restProps,
66
+ });
67
+
68
+ const suggestionElements = suggestions.map((suggestion, index) => {
69
+ const isLoading = loadingSet.has(index) || suggestion.isLoading === true;
70
+ const pill = renderSlot<
71
+ typeof CopilotChatSuggestionPill,
72
+ CopilotChatSuggestionPillProps
73
+ >(suggestionSlot, CopilotChatSuggestionPill, {
74
+ children: suggestion.title,
75
+ isLoading,
76
+ type: "button",
77
+ onClick: () => onSelectSuggestion?.(suggestion, index),
78
+ });
79
+
80
+ return React.cloneElement(pill, {
81
+ key: `${suggestion.title}-${index}`,
82
+ });
83
+ });
84
+
85
+ const boundContainer = React.cloneElement(
86
+ ContainerElement,
87
+ undefined,
88
+ suggestionElements,
89
+ );
90
+
91
+ if (typeof children === "function") {
92
+ const sampleSuggestion = renderSlot<
93
+ typeof CopilotChatSuggestionPill,
94
+ CopilotChatSuggestionPillProps
95
+ >(suggestionSlot, CopilotChatSuggestionPill, {
96
+ children: suggestions[0]?.title ?? "",
97
+ isLoading:
98
+ suggestions.length > 0
99
+ ? loadingSet.has(0) || suggestions[0]?.isLoading === true
100
+ : false,
101
+ type: "button",
102
+ });
103
+
104
+ return (
105
+ <div data-copilotkit style={{ display: "contents" }}>
106
+ {children({
107
+ container: boundContainer,
108
+ suggestion: sampleSuggestion,
109
+ suggestions,
110
+ onSelectSuggestion,
111
+ loadingIndexes,
112
+ className,
113
+ ...restProps,
114
+ })}
115
+ </div>
116
+ );
117
+ }
118
+
119
+ if (children) {
120
+ return (
121
+ <div data-copilotkit style={{ display: "contents" }}>
122
+ {boundContainer}
123
+ {children}
124
+ </div>
125
+ );
126
+ }
127
+
128
+ return boundContainer;
129
+ });
130
+
131
+ CopilotChatSuggestionView.displayName = "CopilotChatSuggestionView";
132
+
133
+ export default CopilotChatSuggestionView;
@@ -0,0 +1,171 @@
1
+ import React, { useState, MouseEvent } from "react";
2
+ import { MessageCircle, X } from "lucide-react";
3
+
4
+ import { renderSlot, SlotValue } from "../../lib/slots";
5
+ import { cn } from "../../lib/utils";
6
+ import {
7
+ CopilotChatDefaultLabels,
8
+ useCopilotChatConfiguration,
9
+ } from "../../providers/CopilotChatConfigurationProvider";
10
+
11
+ const DefaultOpenIcon: React.FC<React.SVGProps<SVGSVGElement>> = ({
12
+ className,
13
+ ...props
14
+ }) => (
15
+ <MessageCircle
16
+ className={cn("cpk:h-6 cpk:w-6", className)}
17
+ strokeWidth={1.75}
18
+ fill="currentColor"
19
+ {...props}
20
+ />
21
+ );
22
+
23
+ const DefaultCloseIcon: React.FC<React.SVGProps<SVGSVGElement>> = ({
24
+ className,
25
+ ...props
26
+ }) => (
27
+ <X
28
+ className={cn("cpk:h-6 cpk:w-6", className)}
29
+ strokeWidth={1.75}
30
+ {...props}
31
+ />
32
+ );
33
+
34
+ DefaultOpenIcon.displayName = "CopilotChatToggleButton.OpenIcon";
35
+ DefaultCloseIcon.displayName = "CopilotChatToggleButton.CloseIcon";
36
+
37
+ export interface CopilotChatToggleButtonProps extends Omit<
38
+ React.ButtonHTMLAttributes<HTMLButtonElement>,
39
+ "children"
40
+ > {
41
+ /** Optional slot override for the chat (closed) icon. */
42
+ openIcon?: SlotValue<typeof DefaultOpenIcon>;
43
+ /** Optional slot override for the close icon. */
44
+ closeIcon?: SlotValue<typeof DefaultCloseIcon>;
45
+ }
46
+
47
+ const ICON_TRANSITION_STYLE: React.CSSProperties = Object.freeze({
48
+ transition:
49
+ "opacity 120ms ease-out, transform 260ms cubic-bezier(0.22, 1, 0.36, 1)",
50
+ });
51
+
52
+ const ICON_WRAPPER_BASE =
53
+ "cpk:pointer-events-none cpk:absolute cpk:inset-0 cpk:flex cpk:items-center cpk:justify-center cpk:will-change-transform";
54
+
55
+ const BUTTON_BASE_CLASSES = cn(
56
+ "copilotKitButton",
57
+ "cpk:fixed cpk:bottom-6 cpk:right-6 cpk:z-[1100] cpk:flex cpk:h-14 cpk:w-14 cpk:items-center cpk:justify-center",
58
+ "cpk:rounded-full cpk:border cpk:border-primary cpk:bg-primary cpk:text-primary-foreground",
59
+ "cpk:shadow-sm cpk:transition-all cpk:duration-200 cpk:ease-out",
60
+ "cpk:hover:scale-[1.04] cpk:hover:shadow-md",
61
+ "cpk:cursor-pointer",
62
+ "cpk:active:scale-[0.96]",
63
+ "cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-primary/50 cpk:focus-visible:ring-offset-2 cpk:focus-visible:ring-offset-background",
64
+ "cpk:disabled:pointer-events-none cpk:disabled:opacity-60",
65
+ );
66
+
67
+ export const CopilotChatToggleButton = React.forwardRef<
68
+ HTMLButtonElement,
69
+ CopilotChatToggleButtonProps
70
+ >(function CopilotChatToggleButton(
71
+ { openIcon, closeIcon, className, ...buttonProps },
72
+ ref,
73
+ ) {
74
+ const { onClick, type, disabled, ...restProps } = buttonProps;
75
+
76
+ const configuration = useCopilotChatConfiguration();
77
+ const labels = configuration?.labels ?? CopilotChatDefaultLabels;
78
+
79
+ const [fallbackOpen, setFallbackOpen] = useState(false);
80
+
81
+ const isOpen = configuration?.isModalOpen ?? fallbackOpen;
82
+ const setModalOpen = configuration?.setModalOpen ?? setFallbackOpen;
83
+
84
+ const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
85
+ if (disabled) {
86
+ return;
87
+ }
88
+
89
+ if (onClick) {
90
+ onClick(event);
91
+ }
92
+
93
+ if (event.defaultPrevented) {
94
+ return;
95
+ }
96
+
97
+ const nextOpen = !isOpen;
98
+ setModalOpen(nextOpen);
99
+ };
100
+
101
+ const renderedOpenIcon = renderSlot(openIcon, DefaultOpenIcon, {
102
+ className: "cpk:h-6 cpk:w-6",
103
+ "aria-hidden": true,
104
+ focusable: false,
105
+ });
106
+
107
+ const renderedCloseIcon = renderSlot(closeIcon, DefaultCloseIcon, {
108
+ className: "cpk:h-6 cpk:w-6",
109
+ "aria-hidden": true,
110
+ focusable: false,
111
+ });
112
+
113
+ const openIconElement = (
114
+ <span
115
+ aria-hidden="true"
116
+ data-slot="chat-toggle-button-open-icon"
117
+ className={ICON_WRAPPER_BASE}
118
+ style={{
119
+ ...ICON_TRANSITION_STYLE,
120
+ opacity: isOpen ? 0 : 1,
121
+ transform: `scale(${isOpen ? 0.75 : 1}) rotate(${isOpen ? 90 : 0}deg)`,
122
+ }}
123
+ >
124
+ {renderedOpenIcon}
125
+ </span>
126
+ );
127
+
128
+ const closeIconElement = (
129
+ <span
130
+ aria-hidden="true"
131
+ data-slot="chat-toggle-button-close-icon"
132
+ className={ICON_WRAPPER_BASE}
133
+ style={{
134
+ ...ICON_TRANSITION_STYLE,
135
+ opacity: isOpen ? 1 : 0,
136
+ transform: `scale(${isOpen ? 1 : 0.75}) rotate(${isOpen ? 0 : -90}deg)`,
137
+ }}
138
+ >
139
+ {renderedCloseIcon}
140
+ </span>
141
+ );
142
+
143
+ return (
144
+ <button
145
+ ref={ref}
146
+ type={type ?? "button"}
147
+ data-copilotkit
148
+ data-testid="copilot-chat-toggle"
149
+ data-slot="chat-toggle-button"
150
+ data-state={isOpen ? "open" : "closed"}
151
+ className={cn(BUTTON_BASE_CLASSES, className)}
152
+ aria-label={
153
+ isOpen ? labels.chatToggleCloseLabel : labels.chatToggleOpenLabel
154
+ }
155
+ aria-pressed={isOpen}
156
+ disabled={disabled}
157
+ onClick={handleClick}
158
+ {...restProps}
159
+ >
160
+ {openIconElement}
161
+ {closeIconElement}
162
+ </button>
163
+ );
164
+ });
165
+ CopilotChatToggleButton.displayName = "CopilotChatToggleButton";
166
+ export default CopilotChatToggleButton;
167
+
168
+ export {
169
+ DefaultOpenIcon as CopilotChatToggleButtonOpenIcon,
170
+ DefaultCloseIcon as CopilotChatToggleButtonCloseIcon,
171
+ };
@@ -0,0 +1,40 @@
1
+ import { useRenderToolCall } from "../../hooks";
2
+ import type { AssistantMessage, Message, ToolMessage } from "@ag-ui/core";
3
+ import React from "react";
4
+
5
+ export type CopilotChatToolCallsViewProps = {
6
+ message: AssistantMessage;
7
+ messages?: Message[];
8
+ };
9
+
10
+ export function CopilotChatToolCallsView({
11
+ message,
12
+ messages = [],
13
+ }: CopilotChatToolCallsViewProps) {
14
+ const renderToolCall = useRenderToolCall();
15
+
16
+ if (!message.toolCalls || message.toolCalls.length === 0) {
17
+ return null;
18
+ }
19
+
20
+ return (
21
+ <>
22
+ {message.toolCalls.map((toolCall) => {
23
+ const toolMessage = messages.find(
24
+ (m) => m.role === "tool" && m.toolCallId === toolCall.id,
25
+ ) as ToolMessage | undefined;
26
+
27
+ return (
28
+ <React.Fragment key={toolCall.id}>
29
+ {renderToolCall({
30
+ toolCall,
31
+ toolMessage,
32
+ })}
33
+ </React.Fragment>
34
+ );
35
+ })}
36
+ </>
37
+ );
38
+ }
39
+
40
+ export default CopilotChatToolCallsView;