@copilotkit/react-native 1.57.3 → 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.
- package/USAGE.md +116 -0
- package/dist/RenderToolContext-CBNKcJqz.cjs +126 -0
- package/dist/RenderToolContext-CBNKcJqz.cjs.map +1 -0
- package/dist/RenderToolContext-CszHyes6.mjs +75 -0
- package/dist/RenderToolContext-CszHyes6.mjs.map +1 -0
- package/dist/components/index.cjs +722 -0
- package/dist/components/index.cjs.map +1 -0
- package/dist/components/index.d.cts +203 -0
- package/dist/components/index.d.mts +203 -0
- package/dist/components/index.mjs +714 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/index.cjs +53 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +102 -6
- package/dist/index.d.mts +100 -4
- package/dist/index.mjs +49 -3
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/package.json +24 -4
- package/src/CopilotKitProvider.tsx +2 -1
- package/src/__mocks__/react-native-streamdown.ts +16 -0
- package/src/__mocks__/react-native.ts +52 -0
- package/src/__tests__/headless-integration.test.tsx +16 -0
- package/src/components/CopilotChat.tsx +465 -0
- package/src/components/CopilotModal.tsx +210 -0
- package/src/components/Markdown.tsx +124 -0
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +418 -0
- package/src/components/__tests__/CopilotChat.test.tsx +390 -0
- package/src/components/__tests__/CopilotModal.test.tsx +257 -0
- package/src/components/__tests__/Markdown.test.tsx +106 -0
- package/src/components/index.ts +18 -0
- package/src/components/messages/AssistantMessage.tsx +73 -0
- package/src/components/messages/TypingIndicator.tsx +103 -0
- package/src/components/messages/UserMessage.tsx +68 -0
- package/src/components/messages/__tests__/messages-edge-cases.test.tsx +179 -0
- package/src/components/messages/__tests__/messages.test.tsx +189 -0
- package/src/components/messages/__tests__/utils.test.ts +39 -0
- package/src/components/messages/index.ts +8 -0
- package/src/components/messages/utils.ts +11 -0
- package/src/hooks/RenderToolContext.tsx +150 -0
- package/src/hooks/__tests__/RenderToolContext.test.tsx +264 -0
- package/src/hooks/__tests__/useRenderTool.test.tsx +334 -0
- package/src/hooks/index.ts +9 -0
- package/src/hooks/useRenderTool.ts +96 -0
- package/src/index.ts +10 -3
- package/tsdown.config.ts +9 -0
- package/vitest.config.mjs +10 -0
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
import { r as useRenderToolRegistry } from "../RenderToolContext-CszHyes6.mjs";
|
|
2
|
+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { useCopilotKit } from "@copilotkit/react-core/v2/context";
|
|
4
|
+
import { useAgent } from "@copilotkit/react-core/v2/headless";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { Animated, FlatList, KeyboardAvoidingView, Platform, Pressable, StyleSheet, Text, TextInput, TouchableOpacity, View } from "react-native";
|
|
7
|
+
import { StreamdownText } from "react-native-streamdown";
|
|
8
|
+
import BottomSheet, { BottomSheetBackdrop, BottomSheetFlatList, BottomSheetView } from "@gorhom/bottom-sheet";
|
|
9
|
+
|
|
10
|
+
//#region src/components/Markdown.tsx
|
|
11
|
+
/**
|
|
12
|
+
* Default markdown styles tuned for chat bubble display.
|
|
13
|
+
*
|
|
14
|
+
* Exported so consumers can spread and extend:
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { defaultMarkdownStyles } from "@copilotkit/react-native";
|
|
17
|
+
* const custom = { ...defaultMarkdownStyles, h1: { fontSize: 28 } };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
const defaultMarkdownStyles = {
|
|
21
|
+
paragraph: {
|
|
22
|
+
fontSize: 16,
|
|
23
|
+
lineHeight: 24,
|
|
24
|
+
color: "#1a1a1a",
|
|
25
|
+
marginTop: 4,
|
|
26
|
+
marginBottom: 4
|
|
27
|
+
},
|
|
28
|
+
h1: {
|
|
29
|
+
fontSize: 24,
|
|
30
|
+
fontWeight: "bold",
|
|
31
|
+
marginTop: 12,
|
|
32
|
+
marginBottom: 8,
|
|
33
|
+
color: "#111111"
|
|
34
|
+
},
|
|
35
|
+
h2: {
|
|
36
|
+
fontSize: 20,
|
|
37
|
+
fontWeight: "bold",
|
|
38
|
+
marginTop: 10,
|
|
39
|
+
marginBottom: 6,
|
|
40
|
+
color: "#111111"
|
|
41
|
+
},
|
|
42
|
+
h3: {
|
|
43
|
+
fontSize: 18,
|
|
44
|
+
fontWeight: "600",
|
|
45
|
+
marginTop: 8,
|
|
46
|
+
marginBottom: 4,
|
|
47
|
+
color: "#222222"
|
|
48
|
+
},
|
|
49
|
+
strong: { fontWeight: "bold" },
|
|
50
|
+
em: { fontStyle: "italic" },
|
|
51
|
+
link: {
|
|
52
|
+
color: "#0066cc",
|
|
53
|
+
underline: true
|
|
54
|
+
},
|
|
55
|
+
blockquote: {
|
|
56
|
+
backgroundColor: "#f5f5f5",
|
|
57
|
+
borderWidth: 4,
|
|
58
|
+
borderColor: "#cccccc",
|
|
59
|
+
gapWidth: 12
|
|
60
|
+
},
|
|
61
|
+
code: {
|
|
62
|
+
backgroundColor: "#f0f0f0",
|
|
63
|
+
fontFamily: "monospace",
|
|
64
|
+
fontSize: 14
|
|
65
|
+
},
|
|
66
|
+
codeBlock: {
|
|
67
|
+
backgroundColor: "#f0f0f0",
|
|
68
|
+
borderRadius: 8,
|
|
69
|
+
padding: 12,
|
|
70
|
+
fontFamily: "monospace",
|
|
71
|
+
fontSize: 14
|
|
72
|
+
},
|
|
73
|
+
list: {
|
|
74
|
+
marginTop: 4,
|
|
75
|
+
marginBottom: 4
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Renders markdown content using `react-native-streamdown` with
|
|
80
|
+
* pre-configured styles suited for CopilotKit chat bubbles.
|
|
81
|
+
*
|
|
82
|
+
* `react-native-streamdown` processes incomplete streaming markdown in the
|
|
83
|
+
* background, rendering incrementally without visual glitches — ideal for
|
|
84
|
+
* displaying LLM output as it arrives.
|
|
85
|
+
*
|
|
86
|
+
* Custom styles are merged on top of the defaults so callers only need
|
|
87
|
+
* to override what they want to change.
|
|
88
|
+
*/
|
|
89
|
+
function CopilotMarkdown({ content, style, streamingAnimation = true }) {
|
|
90
|
+
return /* @__PURE__ */ jsx(StreamdownText, {
|
|
91
|
+
markdown: content,
|
|
92
|
+
markdownStyle: useMemo(() => {
|
|
93
|
+
if (!style) return defaultMarkdownStyles;
|
|
94
|
+
return {
|
|
95
|
+
...defaultMarkdownStyles,
|
|
96
|
+
...style
|
|
97
|
+
};
|
|
98
|
+
}, [style]),
|
|
99
|
+
streamingAnimation
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/components/messages/TypingIndicator.tsx
|
|
105
|
+
const DOT_SIZE = 6;
|
|
106
|
+
const DOT_SPACING = 4;
|
|
107
|
+
const ANIMATION_DURATION = 400;
|
|
108
|
+
/**
|
|
109
|
+
* Three animated dots that pulse in sequence, suitable for embedding
|
|
110
|
+
* inside an AssistantMessage to indicate the AI is still generating.
|
|
111
|
+
*
|
|
112
|
+
* Uses React Native's built-in `Animated` API (no Reanimated dependency).
|
|
113
|
+
*/
|
|
114
|
+
function TypingIndicator({ style }) {
|
|
115
|
+
const dot1 = useRef(new Animated.Value(0)).current;
|
|
116
|
+
const dot2 = useRef(new Animated.Value(0)).current;
|
|
117
|
+
const dot3 = useRef(new Animated.Value(0)).current;
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const createPulse = (dot, delay) => Animated.sequence([Animated.delay(delay), Animated.loop(Animated.sequence([Animated.timing(dot, {
|
|
120
|
+
toValue: 1,
|
|
121
|
+
duration: ANIMATION_DURATION,
|
|
122
|
+
useNativeDriver: true
|
|
123
|
+
}), Animated.timing(dot, {
|
|
124
|
+
toValue: 0,
|
|
125
|
+
duration: ANIMATION_DURATION,
|
|
126
|
+
useNativeDriver: true
|
|
127
|
+
})]))]);
|
|
128
|
+
const animation = Animated.parallel([
|
|
129
|
+
createPulse(dot1, 0),
|
|
130
|
+
createPulse(dot2, ANIMATION_DURATION * .33),
|
|
131
|
+
createPulse(dot3, ANIMATION_DURATION * .66)
|
|
132
|
+
]);
|
|
133
|
+
animation.start();
|
|
134
|
+
return () => {
|
|
135
|
+
animation.stop();
|
|
136
|
+
};
|
|
137
|
+
}, [
|
|
138
|
+
dot1,
|
|
139
|
+
dot2,
|
|
140
|
+
dot3
|
|
141
|
+
]);
|
|
142
|
+
const dotStyle = (animatedValue) => ({
|
|
143
|
+
...styles$4.dot,
|
|
144
|
+
opacity: animatedValue.interpolate({
|
|
145
|
+
inputRange: [0, 1],
|
|
146
|
+
outputRange: [.3, 1]
|
|
147
|
+
}),
|
|
148
|
+
transform: [{ scale: animatedValue.interpolate({
|
|
149
|
+
inputRange: [0, 1],
|
|
150
|
+
outputRange: [.8, 1.2]
|
|
151
|
+
}) }]
|
|
152
|
+
});
|
|
153
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
154
|
+
style: [styles$4.container, style],
|
|
155
|
+
accessibilityLabel: "Typing indicator",
|
|
156
|
+
accessibilityRole: "text",
|
|
157
|
+
children: [
|
|
158
|
+
/* @__PURE__ */ jsx(Animated.View, { style: dotStyle(dot1) }),
|
|
159
|
+
/* @__PURE__ */ jsx(Animated.View, { style: dotStyle(dot2) }),
|
|
160
|
+
/* @__PURE__ */ jsx(Animated.View, { style: dotStyle(dot3) })
|
|
161
|
+
]
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const styles$4 = StyleSheet.create({
|
|
165
|
+
container: {
|
|
166
|
+
flexDirection: "row",
|
|
167
|
+
alignItems: "center",
|
|
168
|
+
paddingVertical: 4,
|
|
169
|
+
paddingHorizontal: 2
|
|
170
|
+
},
|
|
171
|
+
dot: {
|
|
172
|
+
width: DOT_SIZE,
|
|
173
|
+
height: DOT_SIZE,
|
|
174
|
+
borderRadius: DOT_SIZE / 2,
|
|
175
|
+
backgroundColor: "#999999",
|
|
176
|
+
marginHorizontal: DOT_SPACING / 2
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/components/messages/utils.ts
|
|
182
|
+
/**
|
|
183
|
+
* Format a Date as a 12-hour time string (e.g. "2:05 PM").
|
|
184
|
+
*/
|
|
185
|
+
function formatTimestamp(date) {
|
|
186
|
+
const hours = date.getHours();
|
|
187
|
+
const minutes = date.getMinutes();
|
|
188
|
+
const ampm = hours >= 12 ? "PM" : "AM";
|
|
189
|
+
return `${hours % 12 || 12}:${minutes.toString().padStart(2, "0")} ${ampm}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/components/messages/AssistantMessage.tsx
|
|
194
|
+
const ASSISTANT_BUBBLE_BG = "#F0F0F0";
|
|
195
|
+
const TIMESTAMP_COLOR$1 = "#999999";
|
|
196
|
+
/**
|
|
197
|
+
* Left-aligned chat bubble for AI assistant responses.
|
|
198
|
+
*
|
|
199
|
+
* Renders markdown content via `CopilotMarkdown` and shows an animated
|
|
200
|
+
* typing indicator when `isLoading` is true.
|
|
201
|
+
*/
|
|
202
|
+
function AssistantMessage({ content, isLoading = false, timestamp, style }) {
|
|
203
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
204
|
+
style: [styles$3.container, style],
|
|
205
|
+
children: [/* @__PURE__ */ jsxs(View, {
|
|
206
|
+
style: styles$3.bubble,
|
|
207
|
+
children: [content ? /* @__PURE__ */ jsx(CopilotMarkdown, { content }) : null, isLoading ? /* @__PURE__ */ jsx(TypingIndicator, {}) : null]
|
|
208
|
+
}), timestamp && /* @__PURE__ */ jsx(Text, {
|
|
209
|
+
style: styles$3.timestamp,
|
|
210
|
+
children: formatTimestamp(timestamp)
|
|
211
|
+
})]
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const styles$3 = StyleSheet.create({
|
|
215
|
+
container: {
|
|
216
|
+
alignItems: "flex-start",
|
|
217
|
+
marginVertical: 4,
|
|
218
|
+
paddingHorizontal: 12
|
|
219
|
+
},
|
|
220
|
+
bubble: {
|
|
221
|
+
backgroundColor: ASSISTANT_BUBBLE_BG,
|
|
222
|
+
borderTopLeftRadius: 16,
|
|
223
|
+
borderTopRightRadius: 16,
|
|
224
|
+
borderBottomRightRadius: 16,
|
|
225
|
+
borderBottomLeftRadius: 4,
|
|
226
|
+
paddingHorizontal: 12,
|
|
227
|
+
paddingVertical: 8,
|
|
228
|
+
maxWidth: "80%"
|
|
229
|
+
},
|
|
230
|
+
timestamp: {
|
|
231
|
+
color: TIMESTAMP_COLOR$1,
|
|
232
|
+
fontSize: 11,
|
|
233
|
+
marginTop: 2,
|
|
234
|
+
marginLeft: 4
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/components/messages/UserMessage.tsx
|
|
240
|
+
const USER_BUBBLE_BG = "#0066CC";
|
|
241
|
+
const USER_TEXT_COLOR = "#FFFFFF";
|
|
242
|
+
const TIMESTAMP_COLOR = "#999999";
|
|
243
|
+
/**
|
|
244
|
+
* Right-aligned chat bubble for user messages.
|
|
245
|
+
*
|
|
246
|
+
* Renders plain text (no markdown) with a primary-color background
|
|
247
|
+
* and white text. Optionally displays a subtle timestamp below.
|
|
248
|
+
*/
|
|
249
|
+
function UserMessage({ content, timestamp, style }) {
|
|
250
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
251
|
+
style: [styles$2.container, style],
|
|
252
|
+
children: [/* @__PURE__ */ jsx(View, {
|
|
253
|
+
style: styles$2.bubble,
|
|
254
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
255
|
+
style: styles$2.text,
|
|
256
|
+
children: content
|
|
257
|
+
})
|
|
258
|
+
}), timestamp && /* @__PURE__ */ jsx(Text, {
|
|
259
|
+
style: styles$2.timestamp,
|
|
260
|
+
children: formatTimestamp(timestamp)
|
|
261
|
+
})]
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const styles$2 = StyleSheet.create({
|
|
265
|
+
container: {
|
|
266
|
+
alignItems: "flex-end",
|
|
267
|
+
marginVertical: 4,
|
|
268
|
+
paddingHorizontal: 12
|
|
269
|
+
},
|
|
270
|
+
bubble: {
|
|
271
|
+
backgroundColor: USER_BUBBLE_BG,
|
|
272
|
+
borderTopLeftRadius: 16,
|
|
273
|
+
borderTopRightRadius: 16,
|
|
274
|
+
borderBottomLeftRadius: 16,
|
|
275
|
+
borderBottomRightRadius: 4,
|
|
276
|
+
paddingHorizontal: 12,
|
|
277
|
+
paddingVertical: 8,
|
|
278
|
+
maxWidth: "80%"
|
|
279
|
+
},
|
|
280
|
+
text: {
|
|
281
|
+
color: USER_TEXT_COLOR,
|
|
282
|
+
fontSize: 16,
|
|
283
|
+
lineHeight: 22
|
|
284
|
+
},
|
|
285
|
+
timestamp: {
|
|
286
|
+
color: TIMESTAMP_COLOR,
|
|
287
|
+
fontSize: 11,
|
|
288
|
+
marginTop: 2,
|
|
289
|
+
marginRight: 4
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/components/CopilotChat.tsx
|
|
295
|
+
/**
|
|
296
|
+
* Full-screen chat UI component for React Native.
|
|
297
|
+
*
|
|
298
|
+
* Connects to a CopilotKit agent via `useAgent` and renders messages
|
|
299
|
+
* using platform-appropriate AssistantMessage / UserMessage components.
|
|
300
|
+
*
|
|
301
|
+
* Usage:
|
|
302
|
+
* ```tsx
|
|
303
|
+
* <CopilotChat agentName="my-agent" headerTitle="Assistant" />
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
function CopilotChat({ agentName = "default", placeholder = "Type a message...", initialMessages = [], emptyStateTitle = "How can I help?", emptyStateSubtitle = "Ask me anything or try a suggestion below.", headerTitle = "Chat", showHeader = true, style, messageContainerStyle, inputContainerStyle, onSendMessage, FlatListComponent = FlatList, disableKeyboardAvoiding = false }) {
|
|
307
|
+
const [inputText, setInputText] = useState("");
|
|
308
|
+
const [error, setError] = useState(null);
|
|
309
|
+
const flatListRef = useRef(null);
|
|
310
|
+
const messageIdCounter = useRef(0);
|
|
311
|
+
const { copilotkit, executingToolCallIds } = useCopilotKit();
|
|
312
|
+
const { agent } = useAgent({ agentId: agentName });
|
|
313
|
+
const messages = agent.messages ?? [];
|
|
314
|
+
const isRunning = agent.isRunning;
|
|
315
|
+
const toolRenderers = useRenderToolRegistry();
|
|
316
|
+
const extraData = useMemo(() => ({
|
|
317
|
+
isRunning,
|
|
318
|
+
executingToolCallIds,
|
|
319
|
+
toolRenderers
|
|
320
|
+
}), [
|
|
321
|
+
isRunning,
|
|
322
|
+
executingToolCallIds,
|
|
323
|
+
toolRenderers
|
|
324
|
+
]);
|
|
325
|
+
const listItems = useMemo(() => {
|
|
326
|
+
const items = [];
|
|
327
|
+
for (const msg of messages) if (msg.role === "user") items.push({
|
|
328
|
+
id: msg.id,
|
|
329
|
+
type: "user",
|
|
330
|
+
content: typeof msg.content === "string" ? msg.content : ""
|
|
331
|
+
});
|
|
332
|
+
else if (msg.role === "assistant") {
|
|
333
|
+
const assistantMsg = msg;
|
|
334
|
+
if (assistantMsg.content) items.push({
|
|
335
|
+
id: msg.id,
|
|
336
|
+
type: "assistant",
|
|
337
|
+
content: assistantMsg.content
|
|
338
|
+
});
|
|
339
|
+
if (assistantMsg.toolCalls && assistantMsg.toolCalls.length > 0) for (const tc of assistantMsg.toolCalls) items.push({
|
|
340
|
+
id: `${msg.id}-tc-${tc.id}`,
|
|
341
|
+
type: "tool-call",
|
|
342
|
+
toolCalls: [tc]
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
if (isRunning) {
|
|
346
|
+
const lastItem = items[items.length - 1];
|
|
347
|
+
if (!lastItem || lastItem.type !== "assistant") items.push({
|
|
348
|
+
id: "__loading__",
|
|
349
|
+
type: "loading"
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
return items;
|
|
353
|
+
}, [messages, isRunning]);
|
|
354
|
+
const sendMessage = useCallback(async (text) => {
|
|
355
|
+
if (!text || isRunning || !agent) return;
|
|
356
|
+
setError(null);
|
|
357
|
+
onSendMessage?.(text);
|
|
358
|
+
const id = `user-${++messageIdCounter.current}`;
|
|
359
|
+
agent.addMessage({
|
|
360
|
+
id,
|
|
361
|
+
role: "user",
|
|
362
|
+
content: text
|
|
363
|
+
});
|
|
364
|
+
try {
|
|
365
|
+
await copilotkit.runAgent({ agent });
|
|
366
|
+
} catch (err) {
|
|
367
|
+
const message = err instanceof Error ? err.message : "An unexpected error occurred";
|
|
368
|
+
console.error("[CopilotChat] runAgent failed:", err);
|
|
369
|
+
setError(message);
|
|
370
|
+
}
|
|
371
|
+
}, [
|
|
372
|
+
isRunning,
|
|
373
|
+
agent,
|
|
374
|
+
copilotkit,
|
|
375
|
+
onSendMessage
|
|
376
|
+
]);
|
|
377
|
+
const handleSend = useCallback(async () => {
|
|
378
|
+
const text = inputText.trim();
|
|
379
|
+
if (!text) return;
|
|
380
|
+
setInputText("");
|
|
381
|
+
await sendMessage(text);
|
|
382
|
+
}, [inputText, sendMessage]);
|
|
383
|
+
const handleSuggestion = useCallback((text) => {
|
|
384
|
+
sendMessage(text);
|
|
385
|
+
}, [sendMessage]);
|
|
386
|
+
const handleContentSizeChange = useCallback(() => {
|
|
387
|
+
flatListRef.current?.scrollToEnd({ animated: true });
|
|
388
|
+
}, []);
|
|
389
|
+
const renderItem = useCallback(({ item }) => {
|
|
390
|
+
if (item.type === "user") return /* @__PURE__ */ jsx(UserMessage, { content: item.content ?? "" });
|
|
391
|
+
if (item.type === "assistant") return /* @__PURE__ */ jsx(AssistantMessage, {
|
|
392
|
+
content: item.content ?? "",
|
|
393
|
+
isLoading: isRunning && item.id === listItems[listItems.length - 1]?.id
|
|
394
|
+
});
|
|
395
|
+
if (item.type === "tool-call" && item.toolCalls) {
|
|
396
|
+
const tc = item.toolCalls[0];
|
|
397
|
+
const renderer = toolRenderers.get(tc.function.name);
|
|
398
|
+
if (renderer) {
|
|
399
|
+
let args = {};
|
|
400
|
+
try {
|
|
401
|
+
args = JSON.parse(tc.function.arguments || "{}");
|
|
402
|
+
} catch (e) {
|
|
403
|
+
console.warn(`[CopilotChat] Failed to parse tool call arguments for ${tc.function.name}:`, e);
|
|
404
|
+
}
|
|
405
|
+
const status = executingToolCallIds.has(tc.id) ? "executing" : "complete";
|
|
406
|
+
return renderer({
|
|
407
|
+
args,
|
|
408
|
+
status
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
return /* @__PURE__ */ jsx(View, {
|
|
412
|
+
style: styles$1.toolCallIndicator,
|
|
413
|
+
children: /* @__PURE__ */ jsxs(Text, {
|
|
414
|
+
style: styles$1.toolCallText,
|
|
415
|
+
children: ["Called: ", tc.function.name]
|
|
416
|
+
})
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
if (item.type === "loading") return /* @__PURE__ */ jsx(AssistantMessage, {
|
|
420
|
+
content: "",
|
|
421
|
+
isLoading: true
|
|
422
|
+
});
|
|
423
|
+
return null;
|
|
424
|
+
}, [
|
|
425
|
+
isRunning,
|
|
426
|
+
listItems,
|
|
427
|
+
toolRenderers,
|
|
428
|
+
executingToolCallIds
|
|
429
|
+
]);
|
|
430
|
+
const keyExtractor = useCallback((item) => item.id, []);
|
|
431
|
+
const emptyComponent = useMemo(() => /* @__PURE__ */ jsxs(View, {
|
|
432
|
+
style: styles$1.emptyState,
|
|
433
|
+
children: [
|
|
434
|
+
/* @__PURE__ */ jsx(Text, {
|
|
435
|
+
style: styles$1.emptyTitle,
|
|
436
|
+
children: emptyStateTitle
|
|
437
|
+
}),
|
|
438
|
+
/* @__PURE__ */ jsx(Text, {
|
|
439
|
+
style: styles$1.emptySubtitle,
|
|
440
|
+
children: emptyStateSubtitle
|
|
441
|
+
}),
|
|
442
|
+
initialMessages.map((suggestion, i) => /* @__PURE__ */ jsx(Pressable, {
|
|
443
|
+
style: styles$1.suggestionPill,
|
|
444
|
+
onPress: () => handleSuggestion(suggestion),
|
|
445
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
446
|
+
style: styles$1.suggestionText,
|
|
447
|
+
children: suggestion
|
|
448
|
+
})
|
|
449
|
+
}, `suggestion-${i}`))
|
|
450
|
+
]
|
|
451
|
+
}), [
|
|
452
|
+
emptyStateTitle,
|
|
453
|
+
emptyStateSubtitle,
|
|
454
|
+
initialMessages,
|
|
455
|
+
handleSuggestion
|
|
456
|
+
]);
|
|
457
|
+
const sendDisabled = !inputText.trim() || isRunning;
|
|
458
|
+
const content = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
459
|
+
showHeader && /* @__PURE__ */ jsx(View, {
|
|
460
|
+
style: styles$1.header,
|
|
461
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
462
|
+
style: styles$1.headerTitle,
|
|
463
|
+
children: headerTitle
|
|
464
|
+
})
|
|
465
|
+
}),
|
|
466
|
+
/* @__PURE__ */ jsx(FlatListComponent, {
|
|
467
|
+
ref: flatListRef,
|
|
468
|
+
data: listItems,
|
|
469
|
+
renderItem,
|
|
470
|
+
keyExtractor,
|
|
471
|
+
extraData,
|
|
472
|
+
contentContainerStyle: [styles$1.messageList, messageContainerStyle],
|
|
473
|
+
onContentSizeChange: handleContentSizeChange,
|
|
474
|
+
ListEmptyComponent: emptyComponent
|
|
475
|
+
}),
|
|
476
|
+
error && /* @__PURE__ */ jsx(View, {
|
|
477
|
+
style: styles$1.errorContainer,
|
|
478
|
+
testID: "error-message",
|
|
479
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
480
|
+
style: styles$1.errorText,
|
|
481
|
+
children: error
|
|
482
|
+
})
|
|
483
|
+
}),
|
|
484
|
+
/* @__PURE__ */ jsxs(View, {
|
|
485
|
+
style: [styles$1.inputContainer, inputContainerStyle],
|
|
486
|
+
children: [/* @__PURE__ */ jsx(TextInput, {
|
|
487
|
+
style: styles$1.input,
|
|
488
|
+
value: inputText,
|
|
489
|
+
onChangeText: setInputText,
|
|
490
|
+
placeholder,
|
|
491
|
+
placeholderTextColor: "#999",
|
|
492
|
+
multiline: true,
|
|
493
|
+
numberOfLines: 4,
|
|
494
|
+
returnKeyType: "send",
|
|
495
|
+
onSubmitEditing: handleSend
|
|
496
|
+
}), /* @__PURE__ */ jsx(TouchableOpacity, {
|
|
497
|
+
style: [styles$1.sendButton, sendDisabled && styles$1.sendButtonDisabled],
|
|
498
|
+
onPress: handleSend,
|
|
499
|
+
disabled: sendDisabled,
|
|
500
|
+
testID: "send-button",
|
|
501
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
502
|
+
style: styles$1.sendButtonIcon,
|
|
503
|
+
children: "↑"
|
|
504
|
+
})
|
|
505
|
+
})]
|
|
506
|
+
})
|
|
507
|
+
] });
|
|
508
|
+
if (disableKeyboardAvoiding) return /* @__PURE__ */ jsx(View, {
|
|
509
|
+
style: [styles$1.container, style],
|
|
510
|
+
children: content
|
|
511
|
+
});
|
|
512
|
+
return /* @__PURE__ */ jsx(KeyboardAvoidingView, {
|
|
513
|
+
style: [styles$1.container, style],
|
|
514
|
+
behavior: Platform.OS === "ios" ? "padding" : "height",
|
|
515
|
+
keyboardVerticalOffset: 0,
|
|
516
|
+
children: content
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
const styles$1 = StyleSheet.create({
|
|
520
|
+
container: {
|
|
521
|
+
flex: 1,
|
|
522
|
+
backgroundColor: "#FFFFFF"
|
|
523
|
+
},
|
|
524
|
+
header: {
|
|
525
|
+
height: 56,
|
|
526
|
+
justifyContent: "center",
|
|
527
|
+
alignItems: "center",
|
|
528
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
529
|
+
borderBottomColor: "#E0E0E0",
|
|
530
|
+
backgroundColor: "#FFFFFF"
|
|
531
|
+
},
|
|
532
|
+
headerTitle: {
|
|
533
|
+
fontSize: 17,
|
|
534
|
+
fontWeight: "600",
|
|
535
|
+
color: "#1A1A1A"
|
|
536
|
+
},
|
|
537
|
+
messageList: {
|
|
538
|
+
paddingHorizontal: 16,
|
|
539
|
+
flexGrow: 1
|
|
540
|
+
},
|
|
541
|
+
inputContainer: {
|
|
542
|
+
flexDirection: "row",
|
|
543
|
+
alignItems: "flex-end",
|
|
544
|
+
paddingHorizontal: 8,
|
|
545
|
+
paddingVertical: 8,
|
|
546
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
547
|
+
borderTopColor: "#E0E0E0",
|
|
548
|
+
backgroundColor: "#FFFFFF"
|
|
549
|
+
},
|
|
550
|
+
input: {
|
|
551
|
+
flex: 1,
|
|
552
|
+
backgroundColor: "#F5F5F5",
|
|
553
|
+
borderRadius: 20,
|
|
554
|
+
paddingHorizontal: 12,
|
|
555
|
+
paddingVertical: 8,
|
|
556
|
+
fontSize: 15,
|
|
557
|
+
maxHeight: 100,
|
|
558
|
+
color: "#1A1A1A"
|
|
559
|
+
},
|
|
560
|
+
sendButton: {
|
|
561
|
+
width: 36,
|
|
562
|
+
height: 36,
|
|
563
|
+
borderRadius: 18,
|
|
564
|
+
backgroundColor: "#0066CC",
|
|
565
|
+
justifyContent: "center",
|
|
566
|
+
alignItems: "center",
|
|
567
|
+
marginLeft: 8
|
|
568
|
+
},
|
|
569
|
+
sendButtonDisabled: { opacity: .4 },
|
|
570
|
+
sendButtonIcon: {
|
|
571
|
+
color: "#FFFFFF",
|
|
572
|
+
fontSize: 18,
|
|
573
|
+
fontWeight: "700"
|
|
574
|
+
},
|
|
575
|
+
emptyState: {
|
|
576
|
+
flex: 1,
|
|
577
|
+
justifyContent: "center",
|
|
578
|
+
alignItems: "center",
|
|
579
|
+
paddingTop: 100,
|
|
580
|
+
gap: 12
|
|
581
|
+
},
|
|
582
|
+
emptyTitle: {
|
|
583
|
+
fontSize: 22,
|
|
584
|
+
fontWeight: "700",
|
|
585
|
+
color: "#1A1A1A",
|
|
586
|
+
marginBottom: 4
|
|
587
|
+
},
|
|
588
|
+
emptySubtitle: {
|
|
589
|
+
fontSize: 15,
|
|
590
|
+
color: "#666666",
|
|
591
|
+
marginBottom: 16
|
|
592
|
+
},
|
|
593
|
+
suggestionPill: {
|
|
594
|
+
backgroundColor: "#E8F0FE",
|
|
595
|
+
borderRadius: 20,
|
|
596
|
+
paddingHorizontal: 16,
|
|
597
|
+
paddingVertical: 10
|
|
598
|
+
},
|
|
599
|
+
suggestionText: {
|
|
600
|
+
color: "#0066CC",
|
|
601
|
+
fontWeight: "600",
|
|
602
|
+
fontSize: 14
|
|
603
|
+
},
|
|
604
|
+
toolCallIndicator: {
|
|
605
|
+
alignSelf: "flex-start",
|
|
606
|
+
backgroundColor: "#F0F0F0",
|
|
607
|
+
borderRadius: 12,
|
|
608
|
+
paddingHorizontal: 12,
|
|
609
|
+
paddingVertical: 6,
|
|
610
|
+
marginBottom: 8
|
|
611
|
+
},
|
|
612
|
+
toolCallText: {
|
|
613
|
+
fontSize: 12,
|
|
614
|
+
color: "#999999",
|
|
615
|
+
fontStyle: "italic"
|
|
616
|
+
},
|
|
617
|
+
errorContainer: {
|
|
618
|
+
backgroundColor: "#FEE2E2",
|
|
619
|
+
paddingHorizontal: 12,
|
|
620
|
+
paddingVertical: 8,
|
|
621
|
+
marginHorizontal: 8,
|
|
622
|
+
borderRadius: 8
|
|
623
|
+
},
|
|
624
|
+
errorText: {
|
|
625
|
+
color: "#DC2626",
|
|
626
|
+
fontSize: 13
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
//#endregion
|
|
631
|
+
//#region src/components/CopilotModal.tsx
|
|
632
|
+
/**
|
|
633
|
+
* CopilotModal — a bottom-sheet chat overlay for React Native.
|
|
634
|
+
*
|
|
635
|
+
* Mobile equivalent of CopilotPopup on web. Wraps CopilotChat inside
|
|
636
|
+
* @gorhom/bottom-sheet so the chat can slide up over any screen.
|
|
637
|
+
*/
|
|
638
|
+
const CopilotModal = forwardRef(function CopilotModal({ visible, onDismiss, snapPoints: snapPointsProp, initialSnapIndex = 0, enableDismissOnClose = true, backdropOpacity = .5, agentName, placeholder, initialMessages, headerTitle }, ref) {
|
|
639
|
+
const bottomSheetRef = useRef(null);
|
|
640
|
+
const snapPoints = useMemo(() => snapPointsProp ?? ["50%", "90%"], [snapPointsProp]);
|
|
641
|
+
useImperativeHandle(ref, () => ({
|
|
642
|
+
open() {
|
|
643
|
+
bottomSheetRef.current?.snapToIndex(initialSnapIndex);
|
|
644
|
+
},
|
|
645
|
+
close() {
|
|
646
|
+
bottomSheetRef.current?.close();
|
|
647
|
+
}
|
|
648
|
+
}), [initialSnapIndex]);
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
if (visible === void 0) return;
|
|
651
|
+
if (visible) bottomSheetRef.current?.snapToIndex(initialSnapIndex);
|
|
652
|
+
else bottomSheetRef.current?.close();
|
|
653
|
+
}, [visible, initialSnapIndex]);
|
|
654
|
+
const renderBackdrop = useCallback((props) => /* @__PURE__ */ jsx(BottomSheetBackdrop, {
|
|
655
|
+
...props,
|
|
656
|
+
disappearsOnIndex: -1,
|
|
657
|
+
appearsOnIndex: 0,
|
|
658
|
+
opacity: backdropOpacity,
|
|
659
|
+
pressBehavior: "close"
|
|
660
|
+
}), [backdropOpacity]);
|
|
661
|
+
const handleClose = useCallback(() => {
|
|
662
|
+
if (enableDismissOnClose) onDismiss?.();
|
|
663
|
+
}, [enableDismissOnClose, onDismiss]);
|
|
664
|
+
const chatProps = useMemo(() => {
|
|
665
|
+
const props = {};
|
|
666
|
+
if (agentName !== void 0) props.agentName = agentName;
|
|
667
|
+
if (placeholder !== void 0) props.placeholder = placeholder;
|
|
668
|
+
if (initialMessages !== void 0) props.initialMessages = initialMessages;
|
|
669
|
+
if (headerTitle !== void 0) props.headerTitle = headerTitle;
|
|
670
|
+
return props;
|
|
671
|
+
}, [
|
|
672
|
+
agentName,
|
|
673
|
+
placeholder,
|
|
674
|
+
initialMessages,
|
|
675
|
+
headerTitle
|
|
676
|
+
]);
|
|
677
|
+
return /* @__PURE__ */ jsx(BottomSheet, {
|
|
678
|
+
ref: bottomSheetRef,
|
|
679
|
+
index: -1,
|
|
680
|
+
snapPoints,
|
|
681
|
+
enablePanDownToClose: true,
|
|
682
|
+
backdropComponent: renderBackdrop,
|
|
683
|
+
onClose: handleClose,
|
|
684
|
+
keyboardBehavior: "interactive",
|
|
685
|
+
keyboardBlurBehavior: "restore",
|
|
686
|
+
backgroundStyle: styles.sheetBackground,
|
|
687
|
+
handleIndicatorStyle: styles.handleIndicator,
|
|
688
|
+
children: /* @__PURE__ */ jsx(BottomSheetView, {
|
|
689
|
+
style: styles.contentContainer,
|
|
690
|
+
children: /* @__PURE__ */ jsx(CopilotChat, {
|
|
691
|
+
...chatProps,
|
|
692
|
+
FlatListComponent: BottomSheetFlatList,
|
|
693
|
+
disableKeyboardAvoiding: true
|
|
694
|
+
})
|
|
695
|
+
})
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
const styles = StyleSheet.create({
|
|
699
|
+
sheetBackground: {
|
|
700
|
+
backgroundColor: "#FFFFFF",
|
|
701
|
+
borderTopLeftRadius: 16,
|
|
702
|
+
borderTopRightRadius: 16
|
|
703
|
+
},
|
|
704
|
+
handleIndicator: {
|
|
705
|
+
width: 40,
|
|
706
|
+
height: 4,
|
|
707
|
+
backgroundColor: "#DDDDDD"
|
|
708
|
+
},
|
|
709
|
+
contentContainer: { flex: 1 }
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
//#endregion
|
|
713
|
+
export { AssistantMessage, CopilotChat, CopilotMarkdown, CopilotModal, UserMessage, defaultMarkdownStyles };
|
|
714
|
+
//# sourceMappingURL=index.mjs.map
|