@copilotkit/react-native 1.57.4 → 1.58.0-canary.thread-id-propagation

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 (48) hide show
  1. package/USAGE.md +116 -0
  2. package/dist/RenderToolContext-CBNKcJqz.cjs +126 -0
  3. package/dist/RenderToolContext-CBNKcJqz.cjs.map +1 -0
  4. package/dist/RenderToolContext-CszHyes6.mjs +75 -0
  5. package/dist/RenderToolContext-CszHyes6.mjs.map +1 -0
  6. package/dist/components/index.cjs +722 -0
  7. package/dist/components/index.cjs.map +1 -0
  8. package/dist/components/index.d.cts +203 -0
  9. package/dist/components/index.d.mts +203 -0
  10. package/dist/components/index.mjs +714 -0
  11. package/dist/components/index.mjs.map +1 -0
  12. package/dist/index.cjs +53 -38
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.cts +102 -6
  15. package/dist/index.d.mts +100 -4
  16. package/dist/index.mjs +49 -3
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/polyfills/encoding.cjs +1 -1
  19. package/dist/polyfills/streams.cjs +1 -1
  20. package/package.json +24 -4
  21. package/src/CopilotKitProvider.tsx +2 -1
  22. package/src/__mocks__/react-native-streamdown.ts +16 -0
  23. package/src/__mocks__/react-native.ts +52 -0
  24. package/src/__tests__/headless-integration.test.tsx +16 -0
  25. package/src/components/CopilotChat.tsx +465 -0
  26. package/src/components/CopilotModal.tsx +210 -0
  27. package/src/components/Markdown.tsx +124 -0
  28. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +418 -0
  29. package/src/components/__tests__/CopilotChat.test.tsx +390 -0
  30. package/src/components/__tests__/CopilotModal.test.tsx +257 -0
  31. package/src/components/__tests__/Markdown.test.tsx +106 -0
  32. package/src/components/index.ts +18 -0
  33. package/src/components/messages/AssistantMessage.tsx +73 -0
  34. package/src/components/messages/TypingIndicator.tsx +103 -0
  35. package/src/components/messages/UserMessage.tsx +68 -0
  36. package/src/components/messages/__tests__/messages-edge-cases.test.tsx +179 -0
  37. package/src/components/messages/__tests__/messages.test.tsx +189 -0
  38. package/src/components/messages/__tests__/utils.test.ts +39 -0
  39. package/src/components/messages/index.ts +8 -0
  40. package/src/components/messages/utils.ts +11 -0
  41. package/src/hooks/RenderToolContext.tsx +150 -0
  42. package/src/hooks/__tests__/RenderToolContext.test.tsx +264 -0
  43. package/src/hooks/__tests__/useRenderTool.test.tsx +334 -0
  44. package/src/hooks/index.ts +9 -0
  45. package/src/hooks/useRenderTool.ts +96 -0
  46. package/src/index.ts +10 -3
  47. package/tsdown.config.ts +9 -0
  48. package/vitest.config.mjs +10 -0
@@ -0,0 +1,210 @@
1
+ /**
2
+ * CopilotModal — a bottom-sheet chat overlay for React Native.
3
+ *
4
+ * Mobile equivalent of CopilotPopup on web. Wraps CopilotChat inside
5
+ * @gorhom/bottom-sheet so the chat can slide up over any screen.
6
+ */
7
+ import React, {
8
+ forwardRef,
9
+ useCallback,
10
+ useEffect,
11
+ useImperativeHandle,
12
+ useMemo,
13
+ useRef,
14
+ } from "react";
15
+ import { StyleSheet, View } from "react-native";
16
+ import BottomSheet, {
17
+ BottomSheetBackdrop,
18
+ BottomSheetFlatList,
19
+ BottomSheetView,
20
+ } from "@gorhom/bottom-sheet";
21
+ import type { BottomSheetBackdropProps } from "@gorhom/bottom-sheet";
22
+ import { CopilotChat } from "./CopilotChat";
23
+ import type { CopilotChatProps } from "./CopilotChat";
24
+
25
+ // ---------------------------------------------------------------------------
26
+ // Types
27
+ // ---------------------------------------------------------------------------
28
+
29
+ export interface CopilotModalProps {
30
+ /** Controlled visibility — when true the sheet opens, when false it closes. */
31
+ visible?: boolean;
32
+
33
+ /** Called when the sheet is dismissed (via backdrop tap, swipe-down, or close()). */
34
+ onDismiss?: () => void;
35
+
36
+ /**
37
+ * Bottom-sheet snap points.
38
+ * @default ['50%', '90%']
39
+ */
40
+ snapPoints?: (string | number)[];
41
+
42
+ /**
43
+ * Which snap-point index to open at.
44
+ * @default 0
45
+ */
46
+ initialSnapIndex?: number;
47
+
48
+ /**
49
+ * Whether closing the sheet fires onDismiss.
50
+ * @default true
51
+ */
52
+ enableDismissOnClose?: boolean;
53
+
54
+ /**
55
+ * Backdrop opacity when the sheet is open.
56
+ * @default 0.5
57
+ */
58
+ backdropOpacity?: number;
59
+
60
+ // -- Pass-through to CopilotChat ------------------------------------------
61
+
62
+ /** Which agent to connect to. */
63
+ agentName?: string;
64
+
65
+ /** Input placeholder text. */
66
+ placeholder?: string;
67
+
68
+ /** Seed messages shown on first render. */
69
+ initialMessages?: string[];
70
+
71
+ /** Title shown in the CopilotChat header area. */
72
+ headerTitle?: string;
73
+ }
74
+
75
+ /** Imperative handle exposed via ref. */
76
+ export interface CopilotModalRef {
77
+ /** Programmatically open the bottom sheet. */
78
+ open: () => void;
79
+ /** Programmatically close the bottom sheet. */
80
+ close: () => void;
81
+ }
82
+
83
+ // ---------------------------------------------------------------------------
84
+ // Component
85
+ // ---------------------------------------------------------------------------
86
+
87
+ export const CopilotModal = forwardRef<CopilotModalRef, CopilotModalProps>(
88
+ function CopilotModal(
89
+ {
90
+ visible,
91
+ onDismiss,
92
+ snapPoints: snapPointsProp,
93
+ initialSnapIndex = 0,
94
+ enableDismissOnClose = true,
95
+ backdropOpacity = 0.5,
96
+ agentName,
97
+ placeholder,
98
+ initialMessages,
99
+ headerTitle,
100
+ },
101
+ ref,
102
+ ) {
103
+ const bottomSheetRef = useRef<BottomSheet>(null);
104
+
105
+ // Stable snap-points array
106
+ const snapPoints = useMemo(
107
+ () => snapPointsProp ?? ["50%", "90%"],
108
+ [snapPointsProp],
109
+ );
110
+
111
+ // ── Imperative API ────────────────────────────────────────────────────
112
+ useImperativeHandle(
113
+ ref,
114
+ () => ({
115
+ open() {
116
+ bottomSheetRef.current?.snapToIndex(initialSnapIndex);
117
+ },
118
+ close() {
119
+ bottomSheetRef.current?.close();
120
+ },
121
+ }),
122
+ [initialSnapIndex],
123
+ );
124
+
125
+ // ── Controlled visibility ─────────────────────────────────────────────
126
+ useEffect(() => {
127
+ if (visible === undefined) return;
128
+ if (visible) {
129
+ bottomSheetRef.current?.snapToIndex(initialSnapIndex);
130
+ } else {
131
+ bottomSheetRef.current?.close();
132
+ }
133
+ }, [visible, initialSnapIndex]);
134
+
135
+ // ── Backdrop renderer ─────────────────────────────────────────────────
136
+ const renderBackdrop = useCallback(
137
+ (props: BottomSheetBackdropProps) => (
138
+ <BottomSheetBackdrop
139
+ {...props}
140
+ disappearsOnIndex={-1}
141
+ appearsOnIndex={0}
142
+ opacity={backdropOpacity}
143
+ pressBehavior="close"
144
+ />
145
+ ),
146
+ [backdropOpacity],
147
+ );
148
+
149
+ // ── Sheet close handler ───────────────────────────────────────────────
150
+ const handleClose = useCallback(() => {
151
+ if (enableDismissOnClose) {
152
+ onDismiss?.();
153
+ }
154
+ }, [enableDismissOnClose, onDismiss]);
155
+
156
+ // ── Build CopilotChat props ───────────────────────────────────────────
157
+ const chatProps = useMemo(() => {
158
+ const props: Partial<CopilotChatProps> = {};
159
+ if (agentName !== undefined) props.agentName = agentName;
160
+ if (placeholder !== undefined) props.placeholder = placeholder;
161
+ if (initialMessages !== undefined)
162
+ props.initialMessages = initialMessages;
163
+ if (headerTitle !== undefined) props.headerTitle = headerTitle;
164
+ return props;
165
+ }, [agentName, placeholder, initialMessages, headerTitle]);
166
+
167
+ return (
168
+ <BottomSheet
169
+ ref={bottomSheetRef}
170
+ index={-1}
171
+ snapPoints={snapPoints}
172
+ enablePanDownToClose
173
+ backdropComponent={renderBackdrop}
174
+ onClose={handleClose}
175
+ keyboardBehavior="interactive"
176
+ keyboardBlurBehavior="restore"
177
+ backgroundStyle={styles.sheetBackground}
178
+ handleIndicatorStyle={styles.handleIndicator}
179
+ >
180
+ <BottomSheetView style={styles.contentContainer}>
181
+ <CopilotChat
182
+ {...chatProps}
183
+ FlatListComponent={BottomSheetFlatList}
184
+ disableKeyboardAvoiding
185
+ />
186
+ </BottomSheetView>
187
+ </BottomSheet>
188
+ );
189
+ },
190
+ );
191
+
192
+ // ---------------------------------------------------------------------------
193
+ // Styles
194
+ // ---------------------------------------------------------------------------
195
+
196
+ const styles = StyleSheet.create({
197
+ sheetBackground: {
198
+ backgroundColor: "#FFFFFF",
199
+ borderTopLeftRadius: 16,
200
+ borderTopRightRadius: 16,
201
+ },
202
+ handleIndicator: {
203
+ width: 40,
204
+ height: 4,
205
+ backgroundColor: "#DDDDDD",
206
+ },
207
+ contentContainer: {
208
+ flex: 1,
209
+ },
210
+ });
@@ -0,0 +1,124 @@
1
+ import React, { useMemo } from "react";
2
+ import { StreamdownText } from "react-native-streamdown";
3
+
4
+ /**
5
+ * Style object accepted by `react-native-enriched-markdown` (and therefore
6
+ * `react-native-streamdown`). Each key targets a markdown element; the
7
+ * available properties vary per element — see the enriched-markdown style
8
+ * reference for the full list.
9
+ */
10
+ export type MarkdownStyle = Record<string, Record<string, unknown>>;
11
+
12
+ /**
13
+ * Props for the CopilotMarkdown component.
14
+ */
15
+ export interface CopilotMarkdownProps {
16
+ /** Markdown string to render. */
17
+ content: string;
18
+ /** Optional style overrides merged on top of the defaults. */
19
+ style?: MarkdownStyle;
20
+ /** Whether to enable the streaming fade-in animation (default: true). */
21
+ streamingAnimation?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Default markdown styles tuned for chat bubble display.
26
+ *
27
+ * Exported so consumers can spread and extend:
28
+ * ```ts
29
+ * import { defaultMarkdownStyles } from "@copilotkit/react-native";
30
+ * const custom = { ...defaultMarkdownStyles, h1: { fontSize: 28 } };
31
+ * ```
32
+ */
33
+ export const defaultMarkdownStyles: MarkdownStyle = {
34
+ paragraph: {
35
+ fontSize: 16,
36
+ lineHeight: 24,
37
+ color: "#1a1a1a",
38
+ marginTop: 4,
39
+ marginBottom: 4,
40
+ },
41
+ h1: {
42
+ fontSize: 24,
43
+ fontWeight: "bold",
44
+ marginTop: 12,
45
+ marginBottom: 8,
46
+ color: "#111111",
47
+ },
48
+ h2: {
49
+ fontSize: 20,
50
+ fontWeight: "bold",
51
+ marginTop: 10,
52
+ marginBottom: 6,
53
+ color: "#111111",
54
+ },
55
+ h3: {
56
+ fontSize: 18,
57
+ fontWeight: "600",
58
+ marginTop: 8,
59
+ marginBottom: 4,
60
+ color: "#222222",
61
+ },
62
+ strong: {
63
+ fontWeight: "bold",
64
+ },
65
+ em: {
66
+ fontStyle: "italic",
67
+ },
68
+ link: {
69
+ color: "#0066cc",
70
+ underline: true,
71
+ },
72
+ blockquote: {
73
+ backgroundColor: "#f5f5f5",
74
+ borderWidth: 4,
75
+ borderColor: "#cccccc",
76
+ gapWidth: 12,
77
+ },
78
+ code: {
79
+ backgroundColor: "#f0f0f0",
80
+ fontFamily: "monospace",
81
+ fontSize: 14,
82
+ },
83
+ codeBlock: {
84
+ backgroundColor: "#f0f0f0",
85
+ borderRadius: 8,
86
+ padding: 12,
87
+ fontFamily: "monospace",
88
+ fontSize: 14,
89
+ },
90
+ list: {
91
+ marginTop: 4,
92
+ marginBottom: 4,
93
+ },
94
+ };
95
+
96
+ /**
97
+ * Renders markdown content using `react-native-streamdown` with
98
+ * pre-configured styles suited for CopilotKit chat bubbles.
99
+ *
100
+ * `react-native-streamdown` processes incomplete streaming markdown in the
101
+ * background, rendering incrementally without visual glitches — ideal for
102
+ * displaying LLM output as it arrives.
103
+ *
104
+ * Custom styles are merged on top of the defaults so callers only need
105
+ * to override what they want to change.
106
+ */
107
+ export function CopilotMarkdown({
108
+ content,
109
+ style,
110
+ streamingAnimation = true,
111
+ }: CopilotMarkdownProps) {
112
+ const mergedStyles = useMemo(() => {
113
+ if (!style) return defaultMarkdownStyles;
114
+ return { ...defaultMarkdownStyles, ...style };
115
+ }, [style]);
116
+
117
+ return (
118
+ <StreamdownText
119
+ markdown={content}
120
+ markdownStyle={mergedStyles}
121
+ streamingAnimation={streamingAnimation}
122
+ />
123
+ );
124
+ }