@copilotkit/react-native 1.57.3 → 1.58.0

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,106 @@
1
+ import React from "react";
2
+ import { render } from "@testing-library/react";
3
+ import { describe, it, expect, vi, beforeEach } from "vitest";
4
+
5
+ // ─── Mocks ────────────────────────────────────────────────────────────────────
6
+
7
+ // Mock react-native since we're in jsdom
8
+ vi.mock("react-native", () => ({
9
+ StyleSheet: {
10
+ create: <T extends Record<string, any>>(styles: T): T => styles,
11
+ flatten: (style: any) => style,
12
+ },
13
+ View: "View",
14
+ Text: "Text",
15
+ }));
16
+
17
+ // Capture the props passed to StreamdownText
18
+ let lastStreamdownProps: any = null;
19
+
20
+ vi.mock("react-native-streamdown", () => ({
21
+ StreamdownText: function MockStreamdownText(props: any) {
22
+ lastStreamdownProps = props;
23
+ return React.createElement(
24
+ "div",
25
+ { "data-testid": "markdown" },
26
+ props.markdown,
27
+ );
28
+ },
29
+ }));
30
+
31
+ // Import after mocks
32
+ import { CopilotMarkdown, defaultMarkdownStyles } from "../Markdown";
33
+
34
+ // ─── Tests ────────────────────────────────────────────────────────────────────
35
+
36
+ describe("CopilotMarkdown", () => {
37
+ beforeEach(() => {
38
+ lastStreamdownProps = null;
39
+ });
40
+
41
+ it("renders without crashing", () => {
42
+ const { container } = render(<CopilotMarkdown content="Hello world" />);
43
+ expect(container).toBeTruthy();
44
+ });
45
+
46
+ it("passes content as markdown prop to StreamdownText", () => {
47
+ render(<CopilotMarkdown content="# Title" />);
48
+ expect(lastStreamdownProps).not.toBeNull();
49
+ expect(lastStreamdownProps.markdown).toBe("# Title");
50
+ });
51
+
52
+ it("uses default styles when no custom style is provided", () => {
53
+ render(<CopilotMarkdown content="test" />);
54
+ expect(lastStreamdownProps.markdownStyle).toBe(defaultMarkdownStyles);
55
+ });
56
+
57
+ it("merges custom styles with defaults", () => {
58
+ const customStyle = { paragraph: { fontSize: 20, color: "#000" } };
59
+ render(<CopilotMarkdown content="test" style={customStyle} />);
60
+
61
+ // Custom should override the paragraph style
62
+ expect(lastStreamdownProps.markdownStyle.paragraph).toEqual({
63
+ fontSize: 20,
64
+ color: "#000",
65
+ });
66
+ // Other defaults should still be present
67
+ expect(lastStreamdownProps.markdownStyle.h1).toEqual(
68
+ defaultMarkdownStyles.h1,
69
+ );
70
+ expect(lastStreamdownProps.markdownStyle.codeBlock).toEqual(
71
+ defaultMarkdownStyles.codeBlock,
72
+ );
73
+ });
74
+
75
+ it("renders safely with empty content", () => {
76
+ const { container } = render(<CopilotMarkdown content="" />);
77
+ expect(container).toBeTruthy();
78
+ expect(lastStreamdownProps.markdown).toBe("");
79
+ });
80
+
81
+ it("enables streamingAnimation by default", () => {
82
+ render(<CopilotMarkdown content="test" />);
83
+ expect(lastStreamdownProps.streamingAnimation).toBe(true);
84
+ });
85
+
86
+ it("allows disabling streamingAnimation", () => {
87
+ render(<CopilotMarkdown content="test" streamingAnimation={false} />);
88
+ expect(lastStreamdownProps.streamingAnimation).toBe(false);
89
+ });
90
+ });
91
+
92
+ describe("defaultMarkdownStyles", () => {
93
+ it("exports a style object with expected keys", () => {
94
+ expect(defaultMarkdownStyles.paragraph).toBeDefined();
95
+ expect(defaultMarkdownStyles.h1).toBeDefined();
96
+ expect(defaultMarkdownStyles.h2).toBeDefined();
97
+ expect(defaultMarkdownStyles.h3).toBeDefined();
98
+ expect(defaultMarkdownStyles.strong).toBeDefined();
99
+ expect(defaultMarkdownStyles.em).toBeDefined();
100
+ expect(defaultMarkdownStyles.link).toBeDefined();
101
+ expect(defaultMarkdownStyles.blockquote).toBeDefined();
102
+ expect(defaultMarkdownStyles.code).toBeDefined();
103
+ expect(defaultMarkdownStyles.codeBlock).toBeDefined();
104
+ expect(defaultMarkdownStyles.list).toBeDefined();
105
+ });
106
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Component barrel — importable via `@copilotkit/react-native/components`.
3
+ */
4
+
5
+ export { CopilotChat } from "./CopilotChat";
6
+ export type { CopilotChatProps } from "./CopilotChat";
7
+
8
+ export { CopilotModal } from "./CopilotModal";
9
+ export type { CopilotModalProps, CopilotModalRef } from "./CopilotModal";
10
+
11
+ export { CopilotMarkdown, defaultMarkdownStyles } from "./Markdown";
12
+ export type { CopilotMarkdownProps } from "./Markdown";
13
+
14
+ export { AssistantMessage } from "./messages/AssistantMessage";
15
+ export type { AssistantMessageProps } from "./messages/AssistantMessage";
16
+
17
+ export { UserMessage } from "./messages/UserMessage";
18
+ export type { UserMessageProps } from "./messages/UserMessage";
@@ -0,0 +1,73 @@
1
+ import React from "react";
2
+ import { View, Text, StyleSheet, type ViewStyle } from "react-native";
3
+ import { CopilotMarkdown } from "../Markdown";
4
+ import { TypingIndicator } from "./TypingIndicator";
5
+ import { formatTimestamp } from "./utils";
6
+
7
+ // ─── Colors ──────────────────────────────────────────────────────────────────
8
+ const ASSISTANT_BUBBLE_BG = "#F0F0F0";
9
+ const ASSISTANT_TEXT_COLOR = "#1A1A1A";
10
+ const TIMESTAMP_COLOR = "#999999";
11
+
12
+ /**
13
+ * Props for the AssistantMessage component.
14
+ */
15
+ export interface AssistantMessageProps {
16
+ /** Markdown content to render inside the bubble */
17
+ content: string;
18
+ /** When true, shows a typing indicator instead of content */
19
+ isLoading?: boolean;
20
+ /** Optional timestamp displayed below the bubble */
21
+ timestamp?: Date;
22
+ /** Optional style override for the outer container */
23
+ style?: ViewStyle;
24
+ }
25
+
26
+ /**
27
+ * Left-aligned chat bubble for AI assistant responses.
28
+ *
29
+ * Renders markdown content via `CopilotMarkdown` and shows an animated
30
+ * typing indicator when `isLoading` is true.
31
+ */
32
+ export function AssistantMessage({
33
+ content,
34
+ isLoading = false,
35
+ timestamp,
36
+ style,
37
+ }: AssistantMessageProps) {
38
+ return (
39
+ <View style={[styles.container, style]}>
40
+ <View style={styles.bubble}>
41
+ {content ? <CopilotMarkdown content={content} /> : null}
42
+ {isLoading ? <TypingIndicator /> : null}
43
+ </View>
44
+ {timestamp && (
45
+ <Text style={styles.timestamp}>{formatTimestamp(timestamp)}</Text>
46
+ )}
47
+ </View>
48
+ );
49
+ }
50
+
51
+ const styles = StyleSheet.create({
52
+ container: {
53
+ alignItems: "flex-start",
54
+ marginVertical: 4,
55
+ paddingHorizontal: 12,
56
+ },
57
+ bubble: {
58
+ backgroundColor: ASSISTANT_BUBBLE_BG,
59
+ borderTopLeftRadius: 16,
60
+ borderTopRightRadius: 16,
61
+ borderBottomRightRadius: 16,
62
+ borderBottomLeftRadius: 4,
63
+ paddingHorizontal: 12,
64
+ paddingVertical: 8,
65
+ maxWidth: "80%",
66
+ },
67
+ timestamp: {
68
+ color: TIMESTAMP_COLOR,
69
+ fontSize: 11,
70
+ marginTop: 2,
71
+ marginLeft: 4,
72
+ },
73
+ });
@@ -0,0 +1,103 @@
1
+ import React, { useEffect, useRef } from "react";
2
+ import { View, Animated, StyleSheet, type ViewStyle } from "react-native";
3
+
4
+ /**
5
+ * Props for the TypingIndicator component.
6
+ */
7
+ export interface TypingIndicatorProps {
8
+ /** Optional style override for the container */
9
+ style?: ViewStyle;
10
+ }
11
+
12
+ const DOT_SIZE = 6;
13
+ const DOT_SPACING = 4;
14
+ const ANIMATION_DURATION = 400;
15
+
16
+ /**
17
+ * Three animated dots that pulse in sequence, suitable for embedding
18
+ * inside an AssistantMessage to indicate the AI is still generating.
19
+ *
20
+ * Uses React Native's built-in `Animated` API (no Reanimated dependency).
21
+ */
22
+ export function TypingIndicator({ style }: TypingIndicatorProps) {
23
+ const dot1 = useRef(new Animated.Value(0)).current;
24
+ const dot2 = useRef(new Animated.Value(0)).current;
25
+ const dot3 = useRef(new Animated.Value(0)).current;
26
+
27
+ useEffect(() => {
28
+ const createPulse = (dot: Animated.Value, delay: number) =>
29
+ Animated.sequence([
30
+ Animated.delay(delay),
31
+ Animated.loop(
32
+ Animated.sequence([
33
+ Animated.timing(dot, {
34
+ toValue: 1,
35
+ duration: ANIMATION_DURATION,
36
+ useNativeDriver: true,
37
+ }),
38
+ Animated.timing(dot, {
39
+ toValue: 0,
40
+ duration: ANIMATION_DURATION,
41
+ useNativeDriver: true,
42
+ }),
43
+ ]),
44
+ ),
45
+ ]);
46
+
47
+ const animation = Animated.parallel([
48
+ createPulse(dot1, 0),
49
+ createPulse(dot2, ANIMATION_DURATION * 0.33),
50
+ createPulse(dot3, ANIMATION_DURATION * 0.66),
51
+ ]);
52
+
53
+ animation.start();
54
+
55
+ return () => {
56
+ animation.stop();
57
+ };
58
+ }, [dot1, dot2, dot3]);
59
+
60
+ const dotStyle = (animatedValue: Animated.Value) => ({
61
+ ...styles.dot,
62
+ opacity: animatedValue.interpolate({
63
+ inputRange: [0, 1],
64
+ outputRange: [0.3, 1],
65
+ }),
66
+ transform: [
67
+ {
68
+ scale: animatedValue.interpolate({
69
+ inputRange: [0, 1],
70
+ outputRange: [0.8, 1.2],
71
+ }),
72
+ },
73
+ ],
74
+ });
75
+
76
+ return (
77
+ <View
78
+ style={[styles.container, style]}
79
+ accessibilityLabel="Typing indicator"
80
+ accessibilityRole="text"
81
+ >
82
+ <Animated.View style={dotStyle(dot1)} />
83
+ <Animated.View style={dotStyle(dot2)} />
84
+ <Animated.View style={dotStyle(dot3)} />
85
+ </View>
86
+ );
87
+ }
88
+
89
+ const styles = StyleSheet.create({
90
+ container: {
91
+ flexDirection: "row",
92
+ alignItems: "center",
93
+ paddingVertical: 4,
94
+ paddingHorizontal: 2,
95
+ },
96
+ dot: {
97
+ width: DOT_SIZE,
98
+ height: DOT_SIZE,
99
+ borderRadius: DOT_SIZE / 2,
100
+ backgroundColor: "#999999",
101
+ marginHorizontal: DOT_SPACING / 2,
102
+ },
103
+ });
@@ -0,0 +1,68 @@
1
+ import React from "react";
2
+ import { View, Text, StyleSheet, type ViewStyle } from "react-native";
3
+ import { formatTimestamp } from "./utils";
4
+
5
+ // ─── Colors ──────────────────────────────────────────────────────────────────
6
+ const USER_BUBBLE_BG = "#0066CC";
7
+ const USER_TEXT_COLOR = "#FFFFFF";
8
+ const TIMESTAMP_COLOR = "#999999";
9
+
10
+ /**
11
+ * Props for the UserMessage component.
12
+ */
13
+ export interface UserMessageProps {
14
+ /** Plain text content to display */
15
+ content: string;
16
+ /** Optional timestamp displayed below the bubble */
17
+ timestamp?: Date;
18
+ /** Optional style override for the outer container */
19
+ style?: ViewStyle;
20
+ }
21
+
22
+ /**
23
+ * Right-aligned chat bubble for user messages.
24
+ *
25
+ * Renders plain text (no markdown) with a primary-color background
26
+ * and white text. Optionally displays a subtle timestamp below.
27
+ */
28
+ export function UserMessage({ content, timestamp, style }: UserMessageProps) {
29
+ return (
30
+ <View style={[styles.container, style]}>
31
+ <View style={styles.bubble}>
32
+ <Text style={styles.text}>{content}</Text>
33
+ </View>
34
+ {timestamp && (
35
+ <Text style={styles.timestamp}>{formatTimestamp(timestamp)}</Text>
36
+ )}
37
+ </View>
38
+ );
39
+ }
40
+
41
+ const styles = StyleSheet.create({
42
+ container: {
43
+ alignItems: "flex-end",
44
+ marginVertical: 4,
45
+ paddingHorizontal: 12,
46
+ },
47
+ bubble: {
48
+ backgroundColor: USER_BUBBLE_BG,
49
+ borderTopLeftRadius: 16,
50
+ borderTopRightRadius: 16,
51
+ borderBottomLeftRadius: 16,
52
+ borderBottomRightRadius: 4,
53
+ paddingHorizontal: 12,
54
+ paddingVertical: 8,
55
+ maxWidth: "80%",
56
+ },
57
+ text: {
58
+ color: USER_TEXT_COLOR,
59
+ fontSize: 16,
60
+ lineHeight: 22,
61
+ },
62
+ timestamp: {
63
+ color: TIMESTAMP_COLOR,
64
+ fontSize: 11,
65
+ marginTop: 2,
66
+ marginRight: 4,
67
+ },
68
+ });
@@ -0,0 +1,179 @@
1
+ import React from "react";
2
+ import { render } from "@testing-library/react";
3
+ import { describe, it, expect, vi } from "vitest";
4
+
5
+ // ─── Mock react-native ───────────────────────────────────────────────────────
6
+ vi.mock("react-native", () => {
7
+ const React = require("react");
8
+
9
+ const View = React.forwardRef(
10
+ (
11
+ {
12
+ children,
13
+ style,
14
+ testID,
15
+ accessibilityLabel,
16
+ accessibilityRole,
17
+ ...rest
18
+ }: any,
19
+ ref: any,
20
+ ) =>
21
+ React.createElement(
22
+ "div",
23
+ {
24
+ ref,
25
+ style,
26
+ "data-testid": testID,
27
+ "aria-label": accessibilityLabel,
28
+ role: accessibilityRole,
29
+ ...rest,
30
+ },
31
+ children,
32
+ ),
33
+ );
34
+ View.displayName = "View";
35
+
36
+ const Text = React.forwardRef(({ children, style, ...rest }: any, ref: any) =>
37
+ React.createElement("span", { ref, style, ...rest }, children),
38
+ );
39
+ Text.displayName = "Text";
40
+
41
+ const AnimatedValue = class {
42
+ _value: number;
43
+ constructor(value: number) {
44
+ this._value = value;
45
+ }
46
+ interpolate({ outputRange }: any) {
47
+ return outputRange[0];
48
+ }
49
+ };
50
+
51
+ const AnimatedView = React.forwardRef(
52
+ ({ children, style, ...rest }: any, ref: any) =>
53
+ React.createElement("div", { ref, style, ...rest }, children),
54
+ );
55
+ AnimatedView.displayName = "Animated.View";
56
+
57
+ const timing = () => ({ start: vi.fn(), stop: vi.fn() });
58
+ const sequence = () => ({ start: vi.fn(), stop: vi.fn() });
59
+ const loop = () => ({ start: vi.fn(), stop: vi.fn() });
60
+ const parallel = () => ({ start: vi.fn(), stop: vi.fn() });
61
+ const delay = () => ({ start: vi.fn(), stop: vi.fn() });
62
+
63
+ return {
64
+ View,
65
+ Text,
66
+ Animated: {
67
+ Value: AnimatedValue,
68
+ View: AnimatedView,
69
+ timing,
70
+ sequence,
71
+ loop,
72
+ parallel,
73
+ delay,
74
+ },
75
+ StyleSheet: {
76
+ create: (styles: any) => styles,
77
+ flatten: (style: any) =>
78
+ Array.isArray(style) ? Object.assign({}, ...style) : style || {},
79
+ },
80
+ };
81
+ });
82
+
83
+ // ─── Mock Markdown ────────────────────────────────────────────────────────────
84
+ vi.mock("../../Markdown", () => ({
85
+ CopilotMarkdown: ({ content }: { content: string }) => {
86
+ const React = require("react");
87
+ return React.createElement(
88
+ "div",
89
+ { "data-testid": "copilot-markdown" },
90
+ content,
91
+ );
92
+ },
93
+ }));
94
+
95
+ import { AssistantMessage } from "../AssistantMessage";
96
+ import { UserMessage } from "../UserMessage";
97
+ import { TypingIndicator } from "../TypingIndicator";
98
+
99
+ // ─── Tests ───────────────────────────────────────────────────────────────────
100
+
101
+ describe("AssistantMessage edge cases", () => {
102
+ it("renders empty content without crashing", () => {
103
+ const { container } = render(<AssistantMessage content="" />);
104
+ expect(container).toBeTruthy();
105
+ });
106
+
107
+ it("shows both markdown content AND typing indicator when both content and isLoading are provided", () => {
108
+ // When content is truthy AND isLoading is true, BOTH should render
109
+ // Looking at the source: content is rendered when truthy, isLoading adds typing indicator
110
+ const { queryByTestId, queryByLabelText } = render(
111
+ <AssistantMessage content="Thinking..." isLoading />,
112
+ );
113
+
114
+ expect(queryByTestId("copilot-markdown")).toBeTruthy();
115
+ expect(queryByLabelText("Typing indicator")).toBeTruthy();
116
+ });
117
+
118
+ it("does not render markdown when content is empty", () => {
119
+ const { queryByTestId } = render(<AssistantMessage content="" />);
120
+
121
+ // Empty content = falsy = CopilotMarkdown should not render
122
+ expect(queryByTestId("copilot-markdown")).toBeNull();
123
+ });
124
+
125
+ it("does not render typing indicator when isLoading is false", () => {
126
+ const { queryByLabelText } = render(
127
+ <AssistantMessage content="Hello" isLoading={false} />,
128
+ );
129
+
130
+ expect(queryByLabelText("Typing indicator")).toBeNull();
131
+ });
132
+
133
+ it("accepts style override", () => {
134
+ const { container } = render(
135
+ <AssistantMessage content="styled" style={{ marginTop: 20 }} />,
136
+ );
137
+
138
+ expect(container).toBeTruthy();
139
+ });
140
+ });
141
+
142
+ describe("UserMessage edge cases", () => {
143
+ it("renders empty content without crashing", () => {
144
+ const { container } = render(<UserMessage content="" />);
145
+ expect(container).toBeTruthy();
146
+ });
147
+
148
+ it("renders long content without crashing", () => {
149
+ const longText = "A".repeat(5000);
150
+ const { container } = render(<UserMessage content={longText} />);
151
+ expect(container.textContent).toContain("A".repeat(100));
152
+ });
153
+
154
+ it("accepts style override", () => {
155
+ const { container } = render(
156
+ <UserMessage content="styled" style={{ marginBottom: 10 }} />,
157
+ );
158
+
159
+ expect(container).toBeTruthy();
160
+ });
161
+ });
162
+
163
+ describe("TypingIndicator edge cases", () => {
164
+ it("accepts style override", () => {
165
+ const { getByLabelText } = render(
166
+ <TypingIndicator style={{ paddingVertical: 10 }} />,
167
+ );
168
+
169
+ expect(getByLabelText("Typing indicator")).toBeTruthy();
170
+ });
171
+
172
+ it("has correct accessibility attributes", () => {
173
+ const { getByLabelText } = render(<TypingIndicator />);
174
+
175
+ const indicator = getByLabelText("Typing indicator");
176
+ expect(indicator).toBeTruthy();
177
+ expect(indicator.getAttribute("role")).toBe("text");
178
+ });
179
+ });