@copilotkit/react-native 1.57.1-canary.1778272612 → 1.57.2

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.
@@ -0,0 +1,359 @@
1
+ // NOTE: This component needs to be exported from index.ts
2
+ // e.g. export { CopilotPopup } from "./CopilotPopup";
3
+ // export type { CopilotPopupProps, CopilotPopupHandle } from "./CopilotPopup";
4
+
5
+ import React, {
6
+ forwardRef,
7
+ useCallback,
8
+ useEffect,
9
+ useImperativeHandle,
10
+ useRef,
11
+ useState,
12
+ } from "react";
13
+ import type { ReactNode } from "react";
14
+ import {
15
+ Modal,
16
+ Pressable,
17
+ StyleSheet,
18
+ Text,
19
+ TouchableOpacity,
20
+ View,
21
+ useWindowDimensions,
22
+ } from "react-native";
23
+ import type { ViewStyle } from "react-native";
24
+ import { CopilotChat } from "./CopilotChat";
25
+ import type { NativeAttachmentsConfig } from "./hooks/use-attachments";
26
+ import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
27
+
28
+ export interface CopilotPopupProps {
29
+ /**
30
+ * The agent ID to use for this chat session.
31
+ * Passed through to CopilotChat.
32
+ */
33
+ agentId?: string;
34
+
35
+ /**
36
+ * @deprecated Use `agentId` instead.
37
+ */
38
+ agentName?: string;
39
+
40
+ /**
41
+ * Thread ID for this chat session.
42
+ */
43
+ threadId?: string;
44
+
45
+ /**
46
+ * Throttle interval (ms) for re-renders.
47
+ */
48
+ throttleMs?: number;
49
+
50
+ /**
51
+ * Whether the popup starts in the open state.
52
+ * @default false
53
+ */
54
+ defaultOpen?: boolean;
55
+
56
+ /**
57
+ * Height of the popup card. Accepts a number (points) or a percentage
58
+ * string (e.g. "60%") relative to the screen height.
59
+ * @default "60%"
60
+ */
61
+ height?: number | string;
62
+
63
+ /**
64
+ * Error handler scoped to this popup's chat agent.
65
+ */
66
+ onError?: (error: Error) => void;
67
+
68
+ /**
69
+ * Title displayed in the popup header bar.
70
+ * @default "CopilotKit"
71
+ */
72
+ headerTitle?: string;
73
+
74
+ /**
75
+ * Enable multimodal file attachments. Forwarded to the internal CopilotChat.
76
+ * Children access attachment state via `useCopilotChatContext()`.
77
+ */
78
+ attachments?: NativeAttachmentsConfig;
79
+
80
+ /**
81
+ * Optional children rendered below the CopilotChat content
82
+ * inside the popup card.
83
+ */
84
+ children?: ReactNode;
85
+
86
+ /**
87
+ * Callback fired when the popup opens.
88
+ */
89
+ onOpen?: () => void;
90
+
91
+ /**
92
+ * Callback fired when the popup closes.
93
+ */
94
+ onClose?: () => void;
95
+
96
+ /**
97
+ * Whether tapping the semi-transparent backdrop dismisses the popup.
98
+ * Equivalent to web SDK's `clickOutsideToClose`.
99
+ * @default true
100
+ */
101
+ dismissOnBackdropPress?: boolean;
102
+
103
+ /**
104
+ * Whether to show the floating action button (FAB) that toggles the popup.
105
+ * @default true
106
+ */
107
+ showToggleButton?: boolean;
108
+
109
+ /**
110
+ * Custom styles applied to the popup card container.
111
+ */
112
+ style?: ViewStyle;
113
+ }
114
+
115
+ /**
116
+ * Imperative handle exposed via ref for controlling the popup programmatically.
117
+ */
118
+ export interface CopilotPopupHandle {
119
+ open: () => void;
120
+ close: () => void;
121
+ toggle: () => void;
122
+ }
123
+
124
+ /**
125
+ * CopilotPopup for React Native.
126
+ *
127
+ * A floating action button (FAB) that opens a modal chat overlay.
128
+ * The popup appears as a card floating above content with rounded corners,
129
+ * a shadow, and a semi-transparent backdrop.
130
+ *
131
+ * ```tsx
132
+ * import { CopilotPopup } from "@copilotkit/react-native";
133
+ *
134
+ * const popupRef = useRef<CopilotPopupHandle>(null);
135
+ *
136
+ * <CopilotPopup
137
+ * ref={popupRef}
138
+ * agentId="my-agent"
139
+ * headerTitle="Chat"
140
+ * defaultOpen={false}
141
+ * />
142
+ * ```
143
+ */
144
+ export const CopilotPopup = forwardRef<CopilotPopupHandle, CopilotPopupProps>(
145
+ function CopilotPopup(
146
+ {
147
+ agentId,
148
+ agentName,
149
+ threadId,
150
+ throttleMs,
151
+ defaultOpen = false,
152
+ height = "60%",
153
+ onError,
154
+ headerTitle = "CopilotKit",
155
+ attachments: attachmentsConfig,
156
+ children,
157
+ onOpen,
158
+ onClose,
159
+ dismissOnBackdropPress = true,
160
+ showToggleButton = true,
161
+ style,
162
+ }: CopilotPopupProps,
163
+ ref: React.Ref<CopilotPopupHandle>,
164
+ ) {
165
+ const [visible, setVisible] = useState(defaultOpen);
166
+ const { height: screenHeight } = useWindowDimensions();
167
+
168
+ // Stable refs for callbacks to avoid effect churn
169
+ const onOpenRef = useRef(onOpen);
170
+ const onCloseRef = useRef(onClose);
171
+ useEffect(() => {
172
+ onOpenRef.current = onOpen;
173
+ }, [onOpen]);
174
+ useEffect(() => {
175
+ onCloseRef.current = onClose;
176
+ }, [onClose]);
177
+
178
+ const handleOpen = useCallback(() => {
179
+ setVisible(true);
180
+ onOpenRef.current?.();
181
+ }, []);
182
+
183
+ const handleClose = useCallback(() => {
184
+ setVisible(false);
185
+ onCloseRef.current?.();
186
+ }, []);
187
+
188
+ const handleToggle = useCallback(() => {
189
+ setVisible((prev) => {
190
+ const next = !prev;
191
+ if (next) {
192
+ onOpenRef.current?.();
193
+ } else {
194
+ onCloseRef.current?.();
195
+ }
196
+ return next;
197
+ });
198
+ }, []);
199
+
200
+ // Expose imperative methods
201
+ useImperativeHandle(
202
+ ref,
203
+ () => ({
204
+ open: handleOpen,
205
+ close: handleClose,
206
+ toggle: handleToggle,
207
+ }),
208
+ [handleOpen, handleClose, handleToggle],
209
+ );
210
+
211
+ // Resolve popup height
212
+ const resolvedHeight =
213
+ typeof height === "string" && height.endsWith("%")
214
+ ? (parseFloat(height) / 100) * screenHeight
215
+ : typeof height === "number"
216
+ ? height
217
+ : 0.6 * screenHeight;
218
+
219
+ // Wrap onError to match CopilotChat's expected signature
220
+ const chatOnError = onError
221
+ ? (event: {
222
+ error: Error;
223
+ code: CopilotKitCoreErrorCode;
224
+ context: Record<string, any>;
225
+ }) => onError(event.error)
226
+ : undefined;
227
+
228
+ return (
229
+ <>
230
+ {/* Floating Action Button */}
231
+ {showToggleButton && !visible && (
232
+ <TouchableOpacity
233
+ testID="copilot-popup-fab"
234
+ style={styles.fab}
235
+ onPress={handleToggle}
236
+ activeOpacity={0.8}
237
+ accessibilityLabel="Open chat"
238
+ accessibilityRole="button"
239
+ >
240
+ <Text style={styles.fabIcon}>💬</Text>
241
+ </TouchableOpacity>
242
+ )}
243
+
244
+ {/* Modal Overlay */}
245
+ <Modal
246
+ testID="copilot-popup-modal"
247
+ visible={visible}
248
+ transparent
249
+ animationType="slide"
250
+ onRequestClose={handleClose}
251
+ >
252
+ {/* Backdrop */}
253
+ <Pressable
254
+ testID="copilot-popup-backdrop"
255
+ style={styles.backdrop}
256
+ onPress={dismissOnBackdropPress ? handleClose : undefined}
257
+ >
258
+ {/* Card — stop propagation so tapping the card doesn't dismiss */}
259
+ <Pressable
260
+ testID="copilot-popup-card"
261
+ style={[styles.card, { height: resolvedHeight }, style]}
262
+ onPress={() => {
263
+ // Prevent backdrop press from firing
264
+ }}
265
+ >
266
+ {/* Header */}
267
+ <View style={styles.header}>
268
+ <Text style={styles.headerTitle}>{headerTitle}</Text>
269
+ <TouchableOpacity
270
+ testID="copilot-popup-close"
271
+ onPress={handleClose}
272
+ hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
273
+ accessibilityLabel="Close chat"
274
+ accessibilityRole="button"
275
+ >
276
+ <Text style={styles.closeButton}>✕</Text>
277
+ </TouchableOpacity>
278
+ </View>
279
+
280
+ {/* Chat Content */}
281
+ <View style={styles.chatContainer}>
282
+ <CopilotChat
283
+ agentId={agentId}
284
+ agentName={agentName}
285
+ threadId={threadId}
286
+ throttleMs={throttleMs}
287
+ onError={chatOnError}
288
+ attachments={attachmentsConfig}
289
+ >
290
+ {children}
291
+ </CopilotChat>
292
+ </View>
293
+ </Pressable>
294
+ </Pressable>
295
+ </Modal>
296
+ </>
297
+ );
298
+ },
299
+ );
300
+
301
+ const styles = StyleSheet.create({
302
+ fab: {
303
+ position: "absolute",
304
+ bottom: 24,
305
+ right: 24,
306
+ width: 56,
307
+ height: 56,
308
+ borderRadius: 28,
309
+ backgroundColor: "#6366f1",
310
+ alignItems: "center",
311
+ justifyContent: "center",
312
+ elevation: 6,
313
+ shadowColor: "#000",
314
+ shadowOffset: { width: 0, height: 3 },
315
+ shadowOpacity: 0.27,
316
+ shadowRadius: 4.65,
317
+ },
318
+ fabIcon: {
319
+ fontSize: 24,
320
+ },
321
+ backdrop: {
322
+ flex: 1,
323
+ backgroundColor: "rgba(0, 0, 0, 0.4)",
324
+ justifyContent: "flex-end",
325
+ },
326
+ card: {
327
+ backgroundColor: "#ffffff",
328
+ borderTopLeftRadius: 16,
329
+ borderTopRightRadius: 16,
330
+ overflow: "hidden",
331
+ elevation: 10,
332
+ shadowColor: "#000",
333
+ shadowOffset: { width: 0, height: -3 },
334
+ shadowOpacity: 0.25,
335
+ shadowRadius: 8,
336
+ },
337
+ header: {
338
+ flexDirection: "row",
339
+ alignItems: "center",
340
+ justifyContent: "space-between",
341
+ paddingHorizontal: 16,
342
+ paddingVertical: 12,
343
+ borderBottomWidth: StyleSheet.hairlineWidth,
344
+ borderBottomColor: "#e5e7eb",
345
+ },
346
+ headerTitle: {
347
+ fontSize: 17,
348
+ fontWeight: "600",
349
+ color: "#111827",
350
+ },
351
+ closeButton: {
352
+ fontSize: 18,
353
+ color: "#6b7280",
354
+ fontWeight: "500",
355
+ },
356
+ chatContainer: {
357
+ flex: 1,
358
+ },
359
+ });
@@ -0,0 +1,357 @@
1
+ import React, {
2
+ forwardRef,
3
+ useCallback,
4
+ useEffect,
5
+ useImperativeHandle,
6
+ useRef,
7
+ useState,
8
+ } from "react";
9
+ import type { ReactNode } from "react";
10
+ import {
11
+ Animated,
12
+ Dimensions,
13
+ Pressable,
14
+ StyleSheet,
15
+ Text,
16
+ useWindowDimensions,
17
+ View,
18
+ } from "react-native";
19
+ import type { ViewStyle } from "react-native";
20
+ import { CopilotChat } from "./CopilotChat";
21
+ import type { CopilotChatProps } from "./CopilotChat";
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // Types
25
+ // ---------------------------------------------------------------------------
26
+
27
+ export interface CopilotSidebarHandle {
28
+ /** Slide the drawer open. */
29
+ open(): void;
30
+ /** Slide the drawer closed. */
31
+ close(): void;
32
+ /** Toggle the drawer open/closed. */
33
+ toggle(): void;
34
+ }
35
+
36
+ export interface CopilotSidebarProps extends Omit<
37
+ CopilotChatProps,
38
+ "children"
39
+ > {
40
+ /**
41
+ * Start the drawer in the open position.
42
+ * @default false
43
+ */
44
+ defaultOpen?: boolean;
45
+
46
+ /**
47
+ * Width of the drawer panel. Accepts a number (points) or a percentage
48
+ * string (e.g. `"85%"`). Defaults to 85% of the screen width.
49
+ */
50
+ width?: number | string;
51
+
52
+ /**
53
+ * Title displayed in the drawer header bar.
54
+ * @default "Copilot"
55
+ */
56
+ headerTitle?: string;
57
+
58
+ /**
59
+ * Show a floating action button to toggle the drawer.
60
+ * @default true
61
+ */
62
+ showToggleButton?: boolean;
63
+
64
+ /** Called after the drawer finishes opening. */
65
+ onOpen?: () => void;
66
+
67
+ /** Called after the drawer finishes closing. */
68
+ onClose?: () => void;
69
+
70
+ /** Custom style applied to the drawer container. */
71
+ style?: ViewStyle;
72
+
73
+ /** Content rendered inside the drawer below the chat area. */
74
+ children?: ReactNode;
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // Constants
79
+ // ---------------------------------------------------------------------------
80
+
81
+ const ANIMATION_DURATION_MS = 300;
82
+ const DEFAULT_HEADER_TITLE = "Copilot";
83
+ const BACKDROP_OPACITY = 0.4;
84
+ const FAB_SIZE = 56;
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Component
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /**
91
+ * CopilotSidebar -- a slide-in drawer from the right edge of the screen
92
+ * that wraps CopilotChat for React Native.
93
+ *
94
+ * ```tsx
95
+ * import { CopilotSidebar } from "@copilotkit/react-native";
96
+ *
97
+ * const ref = useRef<CopilotSidebarHandle>(null);
98
+ *
99
+ * <CopilotSidebar
100
+ * ref={ref}
101
+ * agentId="my-agent"
102
+ * headerTitle="Assistant"
103
+ * defaultOpen={false}
104
+ * />
105
+ * ```
106
+ */
107
+ export const CopilotSidebar = forwardRef<
108
+ CopilotSidebarHandle,
109
+ CopilotSidebarProps
110
+ >(function CopilotSidebar(
111
+ {
112
+ agentId,
113
+ agentName,
114
+ threadId,
115
+ onError,
116
+ throttleMs,
117
+ defaultOpen = false,
118
+ width: widthProp,
119
+ headerTitle = DEFAULT_HEADER_TITLE,
120
+ showToggleButton = true,
121
+ onOpen,
122
+ onClose,
123
+ style,
124
+ children,
125
+ ...rest
126
+ },
127
+ ref,
128
+ ) {
129
+ const { width: screenWidth } = useWindowDimensions();
130
+
131
+ // Resolve drawer width ---------------------------------------------------
132
+ const drawerWidth = resolveWidth(widthProp, screenWidth);
133
+
134
+ // Animation & open state -------------------------------------------------
135
+ const [isOpen, setIsOpen] = useState(defaultOpen);
136
+ const slideAnim = useRef(
137
+ new Animated.Value(defaultOpen ? 0 : drawerWidth),
138
+ ).current;
139
+
140
+ // Keep the animated value in sync when drawerWidth changes while closed
141
+ useEffect(() => {
142
+ if (!isOpen) {
143
+ slideAnim.setValue(drawerWidth);
144
+ }
145
+ }, [drawerWidth, isOpen, slideAnim]);
146
+
147
+ const animateTo = useCallback(
148
+ (toValue: number, cb?: () => void) => {
149
+ Animated.timing(slideAnim, {
150
+ toValue,
151
+ duration: ANIMATION_DURATION_MS,
152
+ useNativeDriver: true,
153
+ }).start(({ finished }) => {
154
+ if (finished) cb?.();
155
+ });
156
+ },
157
+ [slideAnim],
158
+ );
159
+
160
+ const open = useCallback(() => {
161
+ setIsOpen(true);
162
+ animateTo(0, onOpen);
163
+ }, [animateTo, onOpen]);
164
+
165
+ const close = useCallback(() => {
166
+ animateTo(drawerWidth, () => {
167
+ setIsOpen(false);
168
+ onClose?.();
169
+ });
170
+ }, [animateTo, drawerWidth, onClose]);
171
+
172
+ const toggle = useCallback(() => {
173
+ if (isOpen) {
174
+ close();
175
+ } else {
176
+ open();
177
+ }
178
+ }, [isOpen, open, close]);
179
+
180
+ useImperativeHandle(ref, () => ({ open, close, toggle }), [
181
+ open,
182
+ close,
183
+ toggle,
184
+ ]);
185
+
186
+ // Callbacks stored in refs for stable animation closures -----------------
187
+ const onOpenRef = useRef(onOpen);
188
+ const onCloseRef = useRef(onClose);
189
+ useEffect(() => {
190
+ onOpenRef.current = onOpen;
191
+ }, [onOpen]);
192
+ useEffect(() => {
193
+ onCloseRef.current = onClose;
194
+ }, [onClose]);
195
+
196
+ // -----------------------------------------------------------------------
197
+ // Render
198
+ // -----------------------------------------------------------------------
199
+ return (
200
+ <>
201
+ {/* Backdrop */}
202
+ {isOpen && (
203
+ <Pressable
204
+ style={styles.backdrop}
205
+ onPress={close}
206
+ accessibilityRole="button"
207
+ accessibilityLabel="Close sidebar"
208
+ testID="copilot-sidebar-backdrop"
209
+ />
210
+ )}
211
+
212
+ {/* Drawer */}
213
+ {isOpen && (
214
+ <Animated.View
215
+ style={[
216
+ styles.drawer,
217
+ { width: drawerWidth, transform: [{ translateX: slideAnim }] },
218
+ style,
219
+ ]}
220
+ testID="copilot-sidebar-drawer"
221
+ >
222
+ {/* Header */}
223
+ <View style={styles.header}>
224
+ <Text style={styles.headerTitle}>{headerTitle}</Text>
225
+ <Pressable
226
+ onPress={close}
227
+ accessibilityRole="button"
228
+ accessibilityLabel="Close"
229
+ hitSlop={8}
230
+ testID="copilot-sidebar-close"
231
+ >
232
+ <Text style={styles.closeButton}>{"✕"}</Text>
233
+ </Pressable>
234
+ </View>
235
+
236
+ {/* Chat area */}
237
+ <View style={styles.chatContainer}>
238
+ <CopilotChat
239
+ agentId={agentId}
240
+ agentName={agentName}
241
+ threadId={threadId}
242
+ onError={onError}
243
+ throttleMs={throttleMs}
244
+ {...rest}
245
+ >
246
+ {children}
247
+ </CopilotChat>
248
+ </View>
249
+ </Animated.View>
250
+ )}
251
+
252
+ {/* Floating action button */}
253
+ {showToggleButton && !isOpen && (
254
+ <Pressable
255
+ style={styles.fab}
256
+ onPress={open}
257
+ accessibilityRole="button"
258
+ accessibilityLabel="Open sidebar"
259
+ testID="copilot-sidebar-fab"
260
+ >
261
+ <Text style={styles.fabIcon}>{"💬"}</Text>
262
+ </Pressable>
263
+ )}
264
+ </>
265
+ );
266
+ });
267
+
268
+ // ---------------------------------------------------------------------------
269
+ // Helpers
270
+ // ---------------------------------------------------------------------------
271
+
272
+ function resolveWidth(
273
+ widthProp: number | string | undefined,
274
+ screenWidth: number,
275
+ ): number {
276
+ if (widthProp === undefined) {
277
+ return Math.round(screenWidth * 0.85);
278
+ }
279
+ if (typeof widthProp === "number") {
280
+ return widthProp;
281
+ }
282
+ // Percentage string, e.g. "85%"
283
+ const pctMatch = String(widthProp).match(/^(\d+(?:\.\d+)?)%$/);
284
+ if (pctMatch) {
285
+ return Math.round(screenWidth * (parseFloat(pctMatch[1]) / 100));
286
+ }
287
+ // Fallback: try parsing as number
288
+ const parsed = parseFloat(widthProp);
289
+ return isNaN(parsed) ? Math.round(screenWidth * 0.85) : parsed;
290
+ }
291
+
292
+ // ---------------------------------------------------------------------------
293
+ // Styles
294
+ // ---------------------------------------------------------------------------
295
+
296
+ const styles = StyleSheet.create({
297
+ backdrop: {
298
+ ...StyleSheet.absoluteFillObject,
299
+ backgroundColor: `rgba(0, 0, 0, ${BACKDROP_OPACITY})`,
300
+ zIndex: 999,
301
+ },
302
+ drawer: {
303
+ position: "absolute",
304
+ top: 0,
305
+ right: 0,
306
+ bottom: 0,
307
+ backgroundColor: "#ffffff",
308
+ zIndex: 1000,
309
+ shadowColor: "#000",
310
+ shadowOffset: { width: -2, height: 0 },
311
+ shadowOpacity: 0.25,
312
+ shadowRadius: 8,
313
+ elevation: 16,
314
+ },
315
+ header: {
316
+ flexDirection: "row",
317
+ alignItems: "center",
318
+ justifyContent: "space-between",
319
+ paddingHorizontal: 16,
320
+ paddingVertical: 12,
321
+ borderBottomWidth: StyleSheet.hairlineWidth,
322
+ borderBottomColor: "#e0e0e0",
323
+ },
324
+ headerTitle: {
325
+ fontSize: 18,
326
+ fontWeight: "600",
327
+ color: "#1a1a1a",
328
+ },
329
+ closeButton: {
330
+ fontSize: 20,
331
+ color: "#666",
332
+ padding: 4,
333
+ },
334
+ chatContainer: {
335
+ flex: 1,
336
+ },
337
+ fab: {
338
+ position: "absolute",
339
+ bottom: 24,
340
+ right: 24,
341
+ width: FAB_SIZE,
342
+ height: FAB_SIZE,
343
+ borderRadius: FAB_SIZE / 2,
344
+ backgroundColor: "#007AFF",
345
+ alignItems: "center",
346
+ justifyContent: "center",
347
+ shadowColor: "#000",
348
+ shadowOffset: { width: 0, height: 2 },
349
+ shadowOpacity: 0.3,
350
+ shadowRadius: 4,
351
+ elevation: 8,
352
+ zIndex: 998,
353
+ },
354
+ fabIcon: {
355
+ fontSize: 24,
356
+ },
357
+ });