@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.
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/react-native",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.58.0-canary.thread-id-propagation",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -54,6 +54,10 @@
|
|
|
54
54
|
"import": "./dist/polyfills/location.mjs",
|
|
55
55
|
"require": "./dist/polyfills/location.cjs"
|
|
56
56
|
},
|
|
57
|
+
"./components": {
|
|
58
|
+
"import": "./dist/components/index.mjs",
|
|
59
|
+
"require": "./dist/components/index.cjs"
|
|
60
|
+
},
|
|
57
61
|
"./package.json": "./package.json"
|
|
58
62
|
},
|
|
59
63
|
"publishConfig": {
|
|
@@ -63,9 +67,9 @@
|
|
|
63
67
|
"@ag-ui/client": "0.0.53",
|
|
64
68
|
"text-encoding": "^0.7.0",
|
|
65
69
|
"web-streams-polyfill": "^4.2.0",
|
|
66
|
-
"@copilotkit/core": "1.
|
|
67
|
-
"@copilotkit/
|
|
68
|
-
"@copilotkit/shared": "1.
|
|
70
|
+
"@copilotkit/react-core": "1.58.0-canary.thread-id-propagation",
|
|
71
|
+
"@copilotkit/core": "1.58.0-canary.thread-id-propagation",
|
|
72
|
+
"@copilotkit/shared": "1.58.0-canary.thread-id-propagation"
|
|
69
73
|
},
|
|
70
74
|
"devDependencies": {
|
|
71
75
|
"@testing-library/react": "^16.3.0",
|
|
@@ -78,19 +82,35 @@
|
|
|
78
82
|
"vitest": "^3.2.4"
|
|
79
83
|
},
|
|
80
84
|
"peerDependencies": {
|
|
85
|
+
"@gorhom/bottom-sheet": "^5.0.0",
|
|
81
86
|
"expo-document-picker": ">=12.0.0",
|
|
82
87
|
"expo-file-system": ">=17.0.0",
|
|
83
88
|
"react": "^18 || ^19",
|
|
84
89
|
"react-native": ">=0.70",
|
|
90
|
+
"react-native-gesture-handler": ">=2.0.0",
|
|
91
|
+
"react-native-reanimated": ">=3.0.0",
|
|
92
|
+
"react-native-streamdown": ">=0.1.0",
|
|
85
93
|
"zod": ">=3.0.0"
|
|
86
94
|
},
|
|
87
95
|
"peerDependenciesMeta": {
|
|
96
|
+
"@gorhom/bottom-sheet": {
|
|
97
|
+
"optional": true
|
|
98
|
+
},
|
|
88
99
|
"expo-document-picker": {
|
|
89
100
|
"optional": true
|
|
90
101
|
},
|
|
91
102
|
"expo-file-system": {
|
|
92
103
|
"optional": true
|
|
93
104
|
},
|
|
105
|
+
"react-native-gesture-handler": {
|
|
106
|
+
"optional": true
|
|
107
|
+
},
|
|
108
|
+
"react-native-reanimated": {
|
|
109
|
+
"optional": true
|
|
110
|
+
},
|
|
111
|
+
"react-native-streamdown": {
|
|
112
|
+
"optional": true
|
|
113
|
+
},
|
|
94
114
|
"zod": {
|
|
95
115
|
"optional": true
|
|
96
116
|
}
|
|
@@ -8,6 +8,7 @@ import type { CopilotKitContextValue } from "@copilotkit/react-core/v2/context";
|
|
|
8
8
|
import { CopilotKitCoreReact } from "@copilotkit/react-core/v2/headless";
|
|
9
9
|
import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
|
|
10
10
|
import type { DebugConfig } from "@copilotkit/shared";
|
|
11
|
+
import { RenderToolProvider } from "./hooks/RenderToolContext";
|
|
11
12
|
|
|
12
13
|
export interface CopilotKitNativeProviderProps {
|
|
13
14
|
children: ReactNode;
|
|
@@ -223,7 +224,7 @@ export const CopilotKitProvider: React.FC<CopilotKitNativeProviderProps> = ({
|
|
|
223
224
|
return (
|
|
224
225
|
<CopilotKitContext.Provider value={contextValue}>
|
|
225
226
|
<LicenseContext.Provider value={licenseContextValue}>
|
|
226
|
-
{children}
|
|
227
|
+
<RenderToolProvider>{children}</RenderToolProvider>
|
|
227
228
|
</LicenseContext.Provider>
|
|
228
229
|
</CopilotKitContext.Provider>
|
|
229
230
|
);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fallback module for react-native-streamdown.
|
|
3
|
+
*
|
|
4
|
+
* react-native-streamdown is a peer dependency that isn't installed during
|
|
5
|
+
* monorepo development. This stub satisfies vite's import resolution so
|
|
6
|
+
* that vi.mock() in test files can override it at runtime.
|
|
7
|
+
*/
|
|
8
|
+
import React from "react";
|
|
9
|
+
|
|
10
|
+
export function StreamdownText(props: {
|
|
11
|
+
markdown: string;
|
|
12
|
+
markdownStyle?: Record<string, unknown>;
|
|
13
|
+
streamingAnimation?: boolean;
|
|
14
|
+
}) {
|
|
15
|
+
return React.createElement("div", null, props.markdown);
|
|
16
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stub module for react-native in vitest.
|
|
3
|
+
*
|
|
4
|
+
* The real react-native package uses Flow syntax (`import typeof`) that
|
|
5
|
+
* vite/rollup cannot parse. This stub provides minimal mocks so that
|
|
6
|
+
* modules importing from "react-native" can be resolved during testing.
|
|
7
|
+
* Individual test files can still override with vi.mock("react-native", ...).
|
|
8
|
+
*/
|
|
9
|
+
import React from "react";
|
|
10
|
+
|
|
11
|
+
export const StyleSheet = {
|
|
12
|
+
create: <T extends Record<string, unknown>>(styles: T): T => styles,
|
|
13
|
+
hairlineWidth: 1,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function createMockComponent(name: string) {
|
|
17
|
+
return React.forwardRef(function MockComponent(props: any, ref: any) {
|
|
18
|
+
return React.createElement(name, { ...props, ref });
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const View = createMockComponent("View");
|
|
23
|
+
export const Text = createMockComponent("Text");
|
|
24
|
+
export const TextInput = createMockComponent("TextInput");
|
|
25
|
+
export const TouchableOpacity = createMockComponent("TouchableOpacity");
|
|
26
|
+
export const Pressable = createMockComponent("Pressable");
|
|
27
|
+
export const FlatList = createMockComponent("FlatList");
|
|
28
|
+
export const KeyboardAvoidingView = createMockComponent("KeyboardAvoidingView");
|
|
29
|
+
export const ScrollView = createMockComponent("ScrollView");
|
|
30
|
+
export const ActivityIndicator = createMockComponent("ActivityIndicator");
|
|
31
|
+
export const Image = createMockComponent("Image");
|
|
32
|
+
|
|
33
|
+
export const Platform = {
|
|
34
|
+
OS: "ios" as const,
|
|
35
|
+
select: <T>(obj: { ios?: T; android?: T; default?: T }): T | undefined =>
|
|
36
|
+
obj.ios ?? obj.default,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default {
|
|
40
|
+
StyleSheet,
|
|
41
|
+
View,
|
|
42
|
+
Text,
|
|
43
|
+
TextInput,
|
|
44
|
+
TouchableOpacity,
|
|
45
|
+
Pressable,
|
|
46
|
+
FlatList,
|
|
47
|
+
KeyboardAvoidingView,
|
|
48
|
+
ScrollView,
|
|
49
|
+
ActivityIndicator,
|
|
50
|
+
Image,
|
|
51
|
+
Platform,
|
|
52
|
+
};
|
|
@@ -210,6 +210,22 @@ vi.mock("@copilotkit/react-core/v2/context", () => {
|
|
|
210
210
|
};
|
|
211
211
|
});
|
|
212
212
|
|
|
213
|
+
// Mock @gorhom/bottom-sheet to prevent its CommonJS require("react-native")
|
|
214
|
+
// from bypassing the vite alias and loading the Flow-syntax react-native.
|
|
215
|
+
vi.mock("@gorhom/bottom-sheet", () => {
|
|
216
|
+
const React = require("react");
|
|
217
|
+
return {
|
|
218
|
+
__esModule: true,
|
|
219
|
+
default: React.forwardRef((props: any, ref: any) =>
|
|
220
|
+
React.createElement("mock-bottom-sheet", { ref }, props.children),
|
|
221
|
+
),
|
|
222
|
+
BottomSheetBackdrop: () => null,
|
|
223
|
+
BottomSheetFlatList: "FlatList",
|
|
224
|
+
BottomSheetView: (props: any) =>
|
|
225
|
+
React.createElement("div", null, props.children),
|
|
226
|
+
};
|
|
227
|
+
});
|
|
228
|
+
|
|
213
229
|
vi.mock("@copilotkit/shared", () => ({
|
|
214
230
|
createLicenseContextValue: () => ({
|
|
215
231
|
status: null,
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
FlatList,
|
|
4
|
+
KeyboardAvoidingView,
|
|
5
|
+
Platform,
|
|
6
|
+
Pressable,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
Text,
|
|
9
|
+
TextInput,
|
|
10
|
+
TouchableOpacity,
|
|
11
|
+
View,
|
|
12
|
+
type ListRenderItemInfo,
|
|
13
|
+
type ViewStyle,
|
|
14
|
+
} from "react-native";
|
|
15
|
+
import { useAgent } from "@copilotkit/react-core/v2/headless";
|
|
16
|
+
import { useCopilotKit } from "@copilotkit/react-core/v2/context";
|
|
17
|
+
import { AssistantMessage } from "./messages/AssistantMessage";
|
|
18
|
+
import { UserMessage } from "./messages/UserMessage";
|
|
19
|
+
import { useRenderToolRegistry } from "../hooks/RenderToolContext";
|
|
20
|
+
import type { Message } from "@copilotkit/shared";
|
|
21
|
+
|
|
22
|
+
/** Shape of an assistant message with optional tool calls. */
|
|
23
|
+
interface AssistantMessageShape {
|
|
24
|
+
id: string;
|
|
25
|
+
role: "assistant";
|
|
26
|
+
content?: string;
|
|
27
|
+
toolCalls?: Array<{
|
|
28
|
+
id: string;
|
|
29
|
+
type: "function";
|
|
30
|
+
function: { name: string; arguments: string };
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CopilotChatProps {
|
|
35
|
+
/** Agent ID to connect to. Defaults to 'default'. */
|
|
36
|
+
agentName?: string;
|
|
37
|
+
/** Placeholder text for the input field. */
|
|
38
|
+
placeholder?: string;
|
|
39
|
+
/** Suggestion pills shown in the empty state. */
|
|
40
|
+
initialMessages?: string[];
|
|
41
|
+
/** Title shown when there are no messages. */
|
|
42
|
+
emptyStateTitle?: string;
|
|
43
|
+
/** Subtitle shown when there are no messages. */
|
|
44
|
+
emptyStateSubtitle?: string;
|
|
45
|
+
/** Title for the optional header bar. */
|
|
46
|
+
headerTitle?: string;
|
|
47
|
+
/** Whether to show the header bar. Defaults to true. */
|
|
48
|
+
showHeader?: boolean;
|
|
49
|
+
/** Style override for the outermost container. */
|
|
50
|
+
style?: ViewStyle;
|
|
51
|
+
/** Style override for the message list container. */
|
|
52
|
+
messageContainerStyle?: ViewStyle;
|
|
53
|
+
/** Style override for the input bar container. */
|
|
54
|
+
inputContainerStyle?: ViewStyle;
|
|
55
|
+
/** Callback fired when the user sends a message. */
|
|
56
|
+
onSendMessage?: (text: string) => void;
|
|
57
|
+
/** Custom FlatList component (e.g. BottomSheetFlatList for use inside a bottom sheet). */
|
|
58
|
+
FlatListComponent?: React.ComponentType<any>;
|
|
59
|
+
/** When true, skip the KeyboardAvoidingView wrapper (useful when a parent already handles keyboard). */
|
|
60
|
+
disableKeyboardAvoiding?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ChatListItem {
|
|
64
|
+
id: string;
|
|
65
|
+
type: "user" | "assistant" | "tool-call" | "loading";
|
|
66
|
+
content?: string;
|
|
67
|
+
toolCalls?: Array<{
|
|
68
|
+
id: string;
|
|
69
|
+
type: "function";
|
|
70
|
+
function: { name: string; arguments: string };
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Full-screen chat UI component for React Native.
|
|
76
|
+
*
|
|
77
|
+
* Connects to a CopilotKit agent via `useAgent` and renders messages
|
|
78
|
+
* using platform-appropriate AssistantMessage / UserMessage components.
|
|
79
|
+
*
|
|
80
|
+
* Usage:
|
|
81
|
+
* ```tsx
|
|
82
|
+
* <CopilotChat agentName="my-agent" headerTitle="Assistant" />
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function CopilotChat({
|
|
86
|
+
agentName = "default",
|
|
87
|
+
placeholder = "Type a message...",
|
|
88
|
+
initialMessages = [],
|
|
89
|
+
emptyStateTitle = "How can I help?",
|
|
90
|
+
emptyStateSubtitle = "Ask me anything or try a suggestion below.",
|
|
91
|
+
headerTitle = "Chat",
|
|
92
|
+
showHeader = true,
|
|
93
|
+
style,
|
|
94
|
+
messageContainerStyle,
|
|
95
|
+
inputContainerStyle,
|
|
96
|
+
onSendMessage,
|
|
97
|
+
FlatListComponent = FlatList,
|
|
98
|
+
disableKeyboardAvoiding = false,
|
|
99
|
+
}: CopilotChatProps) {
|
|
100
|
+
const [inputText, setInputText] = useState("");
|
|
101
|
+
const [error, setError] = useState<string | null>(null);
|
|
102
|
+
const flatListRef = useRef<FlatList>(null);
|
|
103
|
+
const messageIdCounter = useRef(0);
|
|
104
|
+
|
|
105
|
+
const { copilotkit, executingToolCallIds } = useCopilotKit();
|
|
106
|
+
const { agent } = useAgent({ agentId: agentName });
|
|
107
|
+
|
|
108
|
+
const messages = agent.messages ?? [];
|
|
109
|
+
const isRunning = agent.isRunning;
|
|
110
|
+
|
|
111
|
+
const toolRenderers = useRenderToolRegistry();
|
|
112
|
+
|
|
113
|
+
// Stable extraData for FlatList to avoid re-creating the object every render
|
|
114
|
+
const extraData = useMemo(
|
|
115
|
+
() => ({ isRunning, executingToolCallIds, toolRenderers }),
|
|
116
|
+
[isRunning, executingToolCallIds, toolRenderers],
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Build flat list items from messages
|
|
120
|
+
const listItems: ChatListItem[] = useMemo(() => {
|
|
121
|
+
const items: ChatListItem[] = [];
|
|
122
|
+
|
|
123
|
+
for (const msg of messages) {
|
|
124
|
+
if (msg.role === "user") {
|
|
125
|
+
items.push({
|
|
126
|
+
id: msg.id,
|
|
127
|
+
type: "user",
|
|
128
|
+
content: typeof msg.content === "string" ? msg.content : "",
|
|
129
|
+
});
|
|
130
|
+
} else if (msg.role === "assistant") {
|
|
131
|
+
const assistantMsg = msg as AssistantMessageShape;
|
|
132
|
+
// Add text content if present
|
|
133
|
+
if (assistantMsg.content) {
|
|
134
|
+
items.push({
|
|
135
|
+
id: msg.id,
|
|
136
|
+
type: "assistant",
|
|
137
|
+
content: assistantMsg.content,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// Add tool calls if present
|
|
141
|
+
if (assistantMsg.toolCalls && assistantMsg.toolCalls.length > 0) {
|
|
142
|
+
for (const tc of assistantMsg.toolCalls) {
|
|
143
|
+
items.push({
|
|
144
|
+
id: `${msg.id}-tc-${tc.id}`,
|
|
145
|
+
type: "tool-call",
|
|
146
|
+
toolCalls: [tc],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Show loading indicator when agent is running and the last message
|
|
154
|
+
// is not already the assistant streaming
|
|
155
|
+
if (isRunning) {
|
|
156
|
+
const lastItem = items[items.length - 1];
|
|
157
|
+
if (!lastItem || lastItem.type !== "assistant") {
|
|
158
|
+
items.push({ id: "__loading__", type: "loading" });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return items;
|
|
163
|
+
}, [messages, isRunning]);
|
|
164
|
+
|
|
165
|
+
// Shared logic for sending a message to the agent
|
|
166
|
+
const sendMessage = useCallback(
|
|
167
|
+
async (text: string) => {
|
|
168
|
+
if (!text || isRunning || !agent) return;
|
|
169
|
+
|
|
170
|
+
setError(null);
|
|
171
|
+
onSendMessage?.(text);
|
|
172
|
+
|
|
173
|
+
const id = `user-${++messageIdCounter.current}`;
|
|
174
|
+
agent.addMessage({
|
|
175
|
+
id,
|
|
176
|
+
role: "user",
|
|
177
|
+
content: text,
|
|
178
|
+
} as Message);
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
await copilotkit.runAgent({ agent });
|
|
182
|
+
} catch (err) {
|
|
183
|
+
const message =
|
|
184
|
+
err instanceof Error ? err.message : "An unexpected error occurred";
|
|
185
|
+
console.error("[CopilotChat] runAgent failed:", err);
|
|
186
|
+
setError(message);
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
[isRunning, agent, copilotkit, onSendMessage],
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
// Send from the input field
|
|
193
|
+
const handleSend = useCallback(async () => {
|
|
194
|
+
const text = inputText.trim();
|
|
195
|
+
if (!text) return;
|
|
196
|
+
setInputText("");
|
|
197
|
+
await sendMessage(text);
|
|
198
|
+
}, [inputText, sendMessage]);
|
|
199
|
+
|
|
200
|
+
// Handle suggestion pill press
|
|
201
|
+
const handleSuggestion = useCallback(
|
|
202
|
+
(text: string) => {
|
|
203
|
+
void sendMessage(text);
|
|
204
|
+
},
|
|
205
|
+
[sendMessage],
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// Auto-scroll when content changes
|
|
209
|
+
const handleContentSizeChange = useCallback(() => {
|
|
210
|
+
flatListRef.current?.scrollToEnd({ animated: true });
|
|
211
|
+
}, []);
|
|
212
|
+
|
|
213
|
+
// Render a single list item
|
|
214
|
+
const renderItem = useCallback(
|
|
215
|
+
({ item }: ListRenderItemInfo<ChatListItem>) => {
|
|
216
|
+
if (item.type === "user") {
|
|
217
|
+
return <UserMessage content={item.content ?? ""} />;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (item.type === "assistant") {
|
|
221
|
+
return (
|
|
222
|
+
<AssistantMessage
|
|
223
|
+
content={item.content ?? ""}
|
|
224
|
+
isLoading={
|
|
225
|
+
isRunning && item.id === listItems[listItems.length - 1]?.id
|
|
226
|
+
}
|
|
227
|
+
/>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (item.type === "tool-call" && item.toolCalls) {
|
|
232
|
+
const tc = item.toolCalls[0];
|
|
233
|
+
const renderer = toolRenderers.get(tc.function.name);
|
|
234
|
+
if (renderer) {
|
|
235
|
+
let args: Record<string, unknown> = {};
|
|
236
|
+
try {
|
|
237
|
+
args = JSON.parse(tc.function.arguments || "{}");
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.warn(
|
|
240
|
+
`[CopilotChat] Failed to parse tool call arguments for ${tc.function.name}:`,
|
|
241
|
+
e,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
const status = executingToolCallIds.has(tc.id)
|
|
245
|
+
? "executing"
|
|
246
|
+
: "complete";
|
|
247
|
+
return renderer({ args, status });
|
|
248
|
+
}
|
|
249
|
+
// Subtle indicator for unregistered tool calls
|
|
250
|
+
return (
|
|
251
|
+
<View style={styles.toolCallIndicator}>
|
|
252
|
+
<Text style={styles.toolCallText}>Called: {tc.function.name}</Text>
|
|
253
|
+
</View>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (item.type === "loading") {
|
|
258
|
+
return <AssistantMessage content="" isLoading />;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return null;
|
|
262
|
+
},
|
|
263
|
+
[isRunning, listItems, toolRenderers, executingToolCallIds],
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const keyExtractor = useCallback((item: ChatListItem) => item.id, []);
|
|
267
|
+
|
|
268
|
+
// Empty state component
|
|
269
|
+
const emptyComponent = useMemo(
|
|
270
|
+
() => (
|
|
271
|
+
<View style={styles.emptyState}>
|
|
272
|
+
<Text style={styles.emptyTitle}>{emptyStateTitle}</Text>
|
|
273
|
+
<Text style={styles.emptySubtitle}>{emptyStateSubtitle}</Text>
|
|
274
|
+
{initialMessages.map((suggestion, i) => (
|
|
275
|
+
<Pressable
|
|
276
|
+
key={`suggestion-${i}`}
|
|
277
|
+
style={styles.suggestionPill}
|
|
278
|
+
onPress={() => handleSuggestion(suggestion)}
|
|
279
|
+
>
|
|
280
|
+
<Text style={styles.suggestionText}>{suggestion}</Text>
|
|
281
|
+
</Pressable>
|
|
282
|
+
))}
|
|
283
|
+
</View>
|
|
284
|
+
),
|
|
285
|
+
[emptyStateTitle, emptyStateSubtitle, initialMessages, handleSuggestion],
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
const sendDisabled = !inputText.trim() || isRunning;
|
|
289
|
+
|
|
290
|
+
const content = (
|
|
291
|
+
<>
|
|
292
|
+
{showHeader && (
|
|
293
|
+
<View style={styles.header}>
|
|
294
|
+
<Text style={styles.headerTitle}>{headerTitle}</Text>
|
|
295
|
+
</View>
|
|
296
|
+
)}
|
|
297
|
+
|
|
298
|
+
<FlatListComponent
|
|
299
|
+
ref={flatListRef}
|
|
300
|
+
data={listItems}
|
|
301
|
+
renderItem={renderItem}
|
|
302
|
+
keyExtractor={keyExtractor}
|
|
303
|
+
extraData={extraData}
|
|
304
|
+
contentContainerStyle={[styles.messageList, messageContainerStyle]}
|
|
305
|
+
onContentSizeChange={handleContentSizeChange}
|
|
306
|
+
ListEmptyComponent={emptyComponent}
|
|
307
|
+
/>
|
|
308
|
+
|
|
309
|
+
{error && (
|
|
310
|
+
<View style={styles.errorContainer} testID="error-message">
|
|
311
|
+
<Text style={styles.errorText}>{error}</Text>
|
|
312
|
+
</View>
|
|
313
|
+
)}
|
|
314
|
+
|
|
315
|
+
<View style={[styles.inputContainer, inputContainerStyle]}>
|
|
316
|
+
<TextInput
|
|
317
|
+
style={styles.input}
|
|
318
|
+
value={inputText}
|
|
319
|
+
onChangeText={setInputText}
|
|
320
|
+
placeholder={placeholder}
|
|
321
|
+
placeholderTextColor="#999"
|
|
322
|
+
multiline
|
|
323
|
+
numberOfLines={4}
|
|
324
|
+
returnKeyType="send"
|
|
325
|
+
onSubmitEditing={handleSend}
|
|
326
|
+
/>
|
|
327
|
+
<TouchableOpacity
|
|
328
|
+
style={[styles.sendButton, sendDisabled && styles.sendButtonDisabled]}
|
|
329
|
+
onPress={handleSend}
|
|
330
|
+
disabled={sendDisabled}
|
|
331
|
+
testID="send-button"
|
|
332
|
+
>
|
|
333
|
+
<Text style={styles.sendButtonIcon}>{"↑"}</Text>
|
|
334
|
+
</TouchableOpacity>
|
|
335
|
+
</View>
|
|
336
|
+
</>
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
if (disableKeyboardAvoiding) {
|
|
340
|
+
return <View style={[styles.container, style]}>{content}</View>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return (
|
|
344
|
+
<KeyboardAvoidingView
|
|
345
|
+
style={[styles.container, style]}
|
|
346
|
+
behavior={Platform.OS === "ios" ? "padding" : "height"}
|
|
347
|
+
keyboardVerticalOffset={0}
|
|
348
|
+
>
|
|
349
|
+
{content}
|
|
350
|
+
</KeyboardAvoidingView>
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const styles = StyleSheet.create({
|
|
355
|
+
container: {
|
|
356
|
+
flex: 1,
|
|
357
|
+
backgroundColor: "#FFFFFF",
|
|
358
|
+
},
|
|
359
|
+
header: {
|
|
360
|
+
height: 56,
|
|
361
|
+
justifyContent: "center",
|
|
362
|
+
alignItems: "center",
|
|
363
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
364
|
+
borderBottomColor: "#E0E0E0",
|
|
365
|
+
backgroundColor: "#FFFFFF",
|
|
366
|
+
},
|
|
367
|
+
headerTitle: {
|
|
368
|
+
fontSize: 17,
|
|
369
|
+
fontWeight: "600",
|
|
370
|
+
color: "#1A1A1A",
|
|
371
|
+
},
|
|
372
|
+
messageList: {
|
|
373
|
+
paddingHorizontal: 16,
|
|
374
|
+
flexGrow: 1,
|
|
375
|
+
},
|
|
376
|
+
inputContainer: {
|
|
377
|
+
flexDirection: "row",
|
|
378
|
+
alignItems: "flex-end",
|
|
379
|
+
paddingHorizontal: 8,
|
|
380
|
+
paddingVertical: 8,
|
|
381
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
382
|
+
borderTopColor: "#E0E0E0",
|
|
383
|
+
backgroundColor: "#FFFFFF",
|
|
384
|
+
},
|
|
385
|
+
input: {
|
|
386
|
+
flex: 1,
|
|
387
|
+
backgroundColor: "#F5F5F5",
|
|
388
|
+
borderRadius: 20,
|
|
389
|
+
paddingHorizontal: 12,
|
|
390
|
+
paddingVertical: 8,
|
|
391
|
+
fontSize: 15,
|
|
392
|
+
maxHeight: 100,
|
|
393
|
+
color: "#1A1A1A",
|
|
394
|
+
},
|
|
395
|
+
sendButton: {
|
|
396
|
+
width: 36,
|
|
397
|
+
height: 36,
|
|
398
|
+
borderRadius: 18,
|
|
399
|
+
backgroundColor: "#0066CC",
|
|
400
|
+
justifyContent: "center",
|
|
401
|
+
alignItems: "center",
|
|
402
|
+
marginLeft: 8,
|
|
403
|
+
},
|
|
404
|
+
sendButtonDisabled: {
|
|
405
|
+
opacity: 0.4,
|
|
406
|
+
},
|
|
407
|
+
sendButtonIcon: {
|
|
408
|
+
color: "#FFFFFF",
|
|
409
|
+
fontSize: 18,
|
|
410
|
+
fontWeight: "700",
|
|
411
|
+
},
|
|
412
|
+
emptyState: {
|
|
413
|
+
flex: 1,
|
|
414
|
+
justifyContent: "center",
|
|
415
|
+
alignItems: "center",
|
|
416
|
+
paddingTop: 100,
|
|
417
|
+
gap: 12,
|
|
418
|
+
},
|
|
419
|
+
emptyTitle: {
|
|
420
|
+
fontSize: 22,
|
|
421
|
+
fontWeight: "700",
|
|
422
|
+
color: "#1A1A1A",
|
|
423
|
+
marginBottom: 4,
|
|
424
|
+
},
|
|
425
|
+
emptySubtitle: {
|
|
426
|
+
fontSize: 15,
|
|
427
|
+
color: "#666666",
|
|
428
|
+
marginBottom: 16,
|
|
429
|
+
},
|
|
430
|
+
suggestionPill: {
|
|
431
|
+
backgroundColor: "#E8F0FE",
|
|
432
|
+
borderRadius: 20,
|
|
433
|
+
paddingHorizontal: 16,
|
|
434
|
+
paddingVertical: 10,
|
|
435
|
+
},
|
|
436
|
+
suggestionText: {
|
|
437
|
+
color: "#0066CC",
|
|
438
|
+
fontWeight: "600",
|
|
439
|
+
fontSize: 14,
|
|
440
|
+
},
|
|
441
|
+
toolCallIndicator: {
|
|
442
|
+
alignSelf: "flex-start",
|
|
443
|
+
backgroundColor: "#F0F0F0",
|
|
444
|
+
borderRadius: 12,
|
|
445
|
+
paddingHorizontal: 12,
|
|
446
|
+
paddingVertical: 6,
|
|
447
|
+
marginBottom: 8,
|
|
448
|
+
},
|
|
449
|
+
toolCallText: {
|
|
450
|
+
fontSize: 12,
|
|
451
|
+
color: "#999999",
|
|
452
|
+
fontStyle: "italic",
|
|
453
|
+
},
|
|
454
|
+
errorContainer: {
|
|
455
|
+
backgroundColor: "#FEE2E2",
|
|
456
|
+
paddingHorizontal: 12,
|
|
457
|
+
paddingVertical: 8,
|
|
458
|
+
marginHorizontal: 8,
|
|
459
|
+
borderRadius: 8,
|
|
460
|
+
},
|
|
461
|
+
errorText: {
|
|
462
|
+
color: "#DC2626",
|
|
463
|
+
fontSize: 13,
|
|
464
|
+
},
|
|
465
|
+
});
|