@copilotkit/react-native 1.57.1 → 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.
- package/README.md +11 -1
- package/dist/index.cjs +752 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +394 -6
- package/dist/index.d.mts +394 -6
- package/dist/index.mjs +725 -14
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills.cjs +86 -73
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +86 -73
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +21 -5
- package/src/CopilotChat.tsx +293 -0
- package/src/CopilotKitProvider.tsx +55 -13
- package/src/CopilotModal.tsx +32 -0
- package/src/CopilotPopup.tsx +359 -0
- package/src/CopilotSidebar.tsx +357 -0
- package/src/__tests__/__mocks__/expo-document-picker.ts +13 -0
- package/src/__tests__/__mocks__/expo-file-system.ts +12 -0
- package/src/__tests__/attachments-integration.test.tsx +336 -0
- package/src/__tests__/copilot-chat-agentid.test.tsx +305 -0
- package/src/__tests__/copilot-chat-attachments.test.tsx +259 -0
- package/src/__tests__/copilot-popup.test.tsx +558 -0
- package/src/__tests__/copilot-sidebar.test.tsx +500 -0
- package/src/__tests__/copilotkit-provider.test.tsx +3 -0
- package/src/__tests__/headless-integration.test.tsx +55 -1
- package/src/__tests__/streaming-fetch.test.ts +84 -44
- package/src/__tests__/use-attachments.test.ts +410 -0
- package/src/hooks/use-attachments.ts +274 -0
- package/src/index.ts +77 -3
- package/src/polyfills.ts +3 -2
- package/src/streaming-fetch.ts +135 -104
- package/tsdown.config.ts +1 -0
- package/vitest.config.mjs +19 -0
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import "./polyfills.mjs";
|
|
2
|
+
import React, { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { CopilotKitContext, CopilotKitContext as CopilotKitContext$1, LicenseContext, useCopilotKit, useCopilotKit as useCopilotKit$1, useLicenseContext } from "@copilotkit/react-core/v2/context";
|
|
4
|
+
import { CopilotChatDefaultLabels, CopilotKitCoreReact, defineToolCallRenderer, useAgent, useAgent as useAgent$1, useAgentContext, useCapabilities, useComponent, useConfigureSuggestions, useFrontendTool, useHumanInTheLoop, useInterrupt, useRenderTool, useSuggestions, useThreads } from "@copilotkit/react-core/v2/headless";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { DEFAULT_AGENT_ID, formatFileSize, getModalityFromMimeType, randomUUID } from "@copilotkit/shared";
|
|
7
|
+
import * as DocumentPicker from "expo-document-picker";
|
|
8
|
+
import * as FileSystem from "expo-file-system";
|
|
9
|
+
import { Animated, Modal, Pressable, StyleSheet, Text, TouchableOpacity, View, useWindowDimensions } from "react-native";
|
|
5
10
|
|
|
6
11
|
//#region src/CopilotKitProvider.tsx
|
|
7
12
|
/**
|
|
@@ -10,9 +15,12 @@ import { jsx } from "react/jsx-runtime";
|
|
|
10
15
|
* A lightweight alternative to the web CopilotKitProvider that avoids
|
|
11
16
|
* web-only dependencies (DOM, CSS, Radix UI, Lit, etc).
|
|
12
17
|
*
|
|
18
|
+
* Polyfills are auto-imported when `@copilotkit/react-native` is loaded,
|
|
19
|
+
* so a separate `import "@copilotkit/react-native/polyfills"` is no longer
|
|
20
|
+
* required (though it remains available for advanced use).
|
|
21
|
+
*
|
|
13
22
|
* Usage:
|
|
14
23
|
* ```tsx
|
|
15
|
-
* import "@copilotkit/react-native/polyfills";
|
|
16
24
|
* import { CopilotKitProvider } from "@copilotkit/react-native";
|
|
17
25
|
*
|
|
18
26
|
* function App() {
|
|
@@ -24,29 +32,42 @@ import { jsx } from "react/jsx-runtime";
|
|
|
24
32
|
* }
|
|
25
33
|
* ```
|
|
26
34
|
*/
|
|
27
|
-
const CopilotKitProvider = ({ children, runtimeUrl, headers, useSingleEndpoint, properties, onError }) => {
|
|
28
|
-
const
|
|
35
|
+
const CopilotKitProvider = ({ children, runtimeUrl, headers: headersProp, credentials, useSingleEndpoint, properties, onError, debug, defaultThrottleMs }) => {
|
|
36
|
+
const resolvedHeaders = typeof headersProp === "function" ? headersProp() : headersProp;
|
|
37
|
+
const stableHeaders = useMemo(() => resolvedHeaders ?? {}, [JSON.stringify(resolvedHeaders)]);
|
|
29
38
|
const stableProperties = useMemo(() => properties ?? {}, [JSON.stringify(properties)]);
|
|
30
39
|
const copilotkitRef = useRef(null);
|
|
31
|
-
if (copilotkitRef.current === null)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
if (copilotkitRef.current === null) {
|
|
41
|
+
copilotkitRef.current = new CopilotKitCoreReact({
|
|
42
|
+
runtimeUrl,
|
|
43
|
+
runtimeTransport: useSingleEndpoint === true ? "single" : useSingleEndpoint === false ? "rest" : "auto",
|
|
44
|
+
headers: stableHeaders,
|
|
45
|
+
credentials,
|
|
46
|
+
properties: stableProperties,
|
|
47
|
+
debug
|
|
48
|
+
});
|
|
49
|
+
if (defaultThrottleMs !== void 0) copilotkitRef.current.setDefaultThrottleMs(defaultThrottleMs);
|
|
50
|
+
}
|
|
37
51
|
const copilotkit = copilotkitRef.current;
|
|
38
52
|
useEffect(() => {
|
|
39
53
|
copilotkit.setRuntimeUrl(runtimeUrl);
|
|
40
54
|
copilotkit.setRuntimeTransport(useSingleEndpoint === true ? "single" : useSingleEndpoint === false ? "rest" : "auto");
|
|
41
55
|
copilotkit.setHeaders(stableHeaders);
|
|
56
|
+
copilotkit.setCredentials(credentials);
|
|
42
57
|
copilotkit.setProperties(stableProperties);
|
|
58
|
+
copilotkit.setDebug(debug);
|
|
43
59
|
}, [
|
|
44
60
|
runtimeUrl,
|
|
45
61
|
useSingleEndpoint,
|
|
46
62
|
stableHeaders,
|
|
63
|
+
credentials,
|
|
47
64
|
stableProperties,
|
|
65
|
+
debug,
|
|
48
66
|
copilotkit
|
|
49
67
|
]);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
copilotkit.setDefaultThrottleMs(defaultThrottleMs);
|
|
70
|
+
}, [copilotkit, defaultThrottleMs]);
|
|
50
71
|
const [executingToolCallIds, setExecutingToolCallIds] = useState(() => /* @__PURE__ */ new Set());
|
|
51
72
|
const onErrorRef = useRef(onError);
|
|
52
73
|
useEffect(() => {
|
|
@@ -97,5 +118,695 @@ const CopilotKitProvider = ({ children, runtimeUrl, headers, useSingleEndpoint,
|
|
|
97
118
|
};
|
|
98
119
|
|
|
99
120
|
//#endregion
|
|
100
|
-
|
|
121
|
+
//#region src/hooks/use-attachments.ts
|
|
122
|
+
const DEFAULT_MAX_SIZE = 20 * 1024 * 1024;
|
|
123
|
+
/**
|
|
124
|
+
* React Native hook that manages file attachment state -- picking, uploading,
|
|
125
|
+
* and lifecycle. All returned callbacks are referentially stable via useCallback.
|
|
126
|
+
*
|
|
127
|
+
* This is the RN counterpart of the web `useAttachments` hook from
|
|
128
|
+
* `@copilotkit/react-core`. It replaces web APIs (FileReader, DragEvent,
|
|
129
|
+
* HTMLInputElement) with Expo modules (expo-document-picker, expo-file-system).
|
|
130
|
+
*/
|
|
131
|
+
function useAttachments({ config }) {
|
|
132
|
+
const enabled = config?.enabled ?? false;
|
|
133
|
+
const [attachments, setAttachments] = useState([]);
|
|
134
|
+
const configRef = useRef(config);
|
|
135
|
+
configRef.current = config;
|
|
136
|
+
const attachmentsRef = useRef([]);
|
|
137
|
+
attachmentsRef.current = attachments;
|
|
138
|
+
/**
|
|
139
|
+
* Simple MIME accept filter for NativeFileInput.
|
|
140
|
+
* Handles wildcards like "image/*" and exact types like "application/pdf".
|
|
141
|
+
* Comma-separated lists are supported.
|
|
142
|
+
*/
|
|
143
|
+
const matchesAccept = useCallback((file, accept) => {
|
|
144
|
+
if (!accept || accept === "*/*") return true;
|
|
145
|
+
return accept.split(",").map((f) => f.trim()).some((filter) => {
|
|
146
|
+
if (filter.startsWith(".")) return file.name.toLowerCase().endsWith(filter.toLowerCase());
|
|
147
|
+
if (filter.endsWith("/*")) {
|
|
148
|
+
const prefix = filter.slice(0, -2);
|
|
149
|
+
return file.mimeType.startsWith(prefix + "/");
|
|
150
|
+
}
|
|
151
|
+
return file.mimeType === filter;
|
|
152
|
+
});
|
|
153
|
+
}, []);
|
|
154
|
+
const processFiles = useCallback(async (files) => {
|
|
155
|
+
const cfg = configRef.current;
|
|
156
|
+
const accept = cfg?.accept ?? "*/*";
|
|
157
|
+
const maxSize = cfg?.maxSize ?? DEFAULT_MAX_SIZE;
|
|
158
|
+
const rejectedFiles = files.filter((f) => !matchesAccept(f, accept));
|
|
159
|
+
for (const file of rejectedFiles) cfg?.onUploadFailed?.({
|
|
160
|
+
reason: "invalid-type",
|
|
161
|
+
file,
|
|
162
|
+
message: `File "${file.name}" is not accepted. Supported types: ${accept}`
|
|
163
|
+
});
|
|
164
|
+
const validFiles = files.filter((f) => matchesAccept(f, accept));
|
|
165
|
+
for (const file of validFiles) {
|
|
166
|
+
if (file.size > maxSize) {
|
|
167
|
+
cfg?.onUploadFailed?.({
|
|
168
|
+
reason: "file-too-large",
|
|
169
|
+
file,
|
|
170
|
+
message: `File "${file.name}" exceeds the maximum size of ${formatFileSize(maxSize)}`
|
|
171
|
+
});
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const modality = getModalityFromMimeType(file.mimeType);
|
|
175
|
+
const placeholderId = randomUUID();
|
|
176
|
+
const placeholder = {
|
|
177
|
+
id: placeholderId,
|
|
178
|
+
type: modality,
|
|
179
|
+
source: {
|
|
180
|
+
type: "data",
|
|
181
|
+
value: "",
|
|
182
|
+
mimeType: file.mimeType
|
|
183
|
+
},
|
|
184
|
+
filename: file.name,
|
|
185
|
+
size: file.size,
|
|
186
|
+
status: "uploading"
|
|
187
|
+
};
|
|
188
|
+
setAttachments((prev) => [...prev, placeholder]);
|
|
189
|
+
try {
|
|
190
|
+
let source;
|
|
191
|
+
let uploadMetadata;
|
|
192
|
+
if (cfg?.onUpload) {
|
|
193
|
+
const { metadata: meta, ...uploadSource } = await cfg.onUpload(file);
|
|
194
|
+
source = uploadSource;
|
|
195
|
+
uploadMetadata = meta;
|
|
196
|
+
} else source = {
|
|
197
|
+
type: "data",
|
|
198
|
+
value: await FileSystem.readAsStringAsync(file.uri, { encoding: FileSystem.EncodingType.Base64 }),
|
|
199
|
+
mimeType: file.mimeType
|
|
200
|
+
};
|
|
201
|
+
setAttachments((prev) => prev.map((att) => att.id === placeholderId ? {
|
|
202
|
+
...att,
|
|
203
|
+
source,
|
|
204
|
+
status: "ready",
|
|
205
|
+
metadata: uploadMetadata
|
|
206
|
+
} : att));
|
|
207
|
+
} catch (error) {
|
|
208
|
+
setAttachments((prev) => prev.filter((att) => att.id !== placeholderId));
|
|
209
|
+
console.error(`[CopilotKit] Failed to upload "${file.name}":`, error);
|
|
210
|
+
cfg?.onUploadFailed?.({
|
|
211
|
+
reason: "upload-failed",
|
|
212
|
+
file,
|
|
213
|
+
message: error instanceof Error ? error.message : `Failed to upload "${file.name}"`
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}, [matchesAccept]);
|
|
218
|
+
return {
|
|
219
|
+
attachments,
|
|
220
|
+
enabled,
|
|
221
|
+
openPicker: useCallback(async () => {
|
|
222
|
+
const typeArray = (configRef.current?.accept ?? "*/*").split(",").map((t) => t.trim()).filter(Boolean);
|
|
223
|
+
try {
|
|
224
|
+
const result = await DocumentPicker.getDocumentAsync({
|
|
225
|
+
type: typeArray.length > 0 ? typeArray : ["*/*"],
|
|
226
|
+
copyToCacheDirectory: true,
|
|
227
|
+
multiple: true
|
|
228
|
+
});
|
|
229
|
+
if (result.canceled || !result.assets?.length) return;
|
|
230
|
+
await processFiles(result.assets.map((asset) => ({
|
|
231
|
+
uri: asset.uri,
|
|
232
|
+
name: asset.name ?? "unknown",
|
|
233
|
+
size: asset.size ?? 0,
|
|
234
|
+
mimeType: asset.mimeType ?? "application/octet-stream"
|
|
235
|
+
})));
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.error("[CopilotKit] Document picker error:", error);
|
|
238
|
+
}
|
|
239
|
+
}, [processFiles]),
|
|
240
|
+
processFiles,
|
|
241
|
+
removeAttachment: useCallback((id) => {
|
|
242
|
+
setAttachments((prev) => prev.filter((a) => a.id !== id));
|
|
243
|
+
}, []),
|
|
244
|
+
consumeAttachments: useCallback(() => {
|
|
245
|
+
const ready = attachmentsRef.current.filter((a) => a.status === "ready");
|
|
246
|
+
if (ready.length === 0) return ready;
|
|
247
|
+
setAttachments((prev) => prev.filter((a) => a.status !== "ready"));
|
|
248
|
+
return ready;
|
|
249
|
+
}, [])
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
//#endregion
|
|
254
|
+
//#region src/CopilotChat.tsx
|
|
255
|
+
const CopilotChatCtx = createContext(null);
|
|
256
|
+
/**
|
|
257
|
+
* Hook to access the CopilotChat context from child components.
|
|
258
|
+
* Must be called inside a `<CopilotChat>` component tree.
|
|
259
|
+
*/
|
|
260
|
+
function useCopilotChatContext() {
|
|
261
|
+
const ctx = useContext(CopilotChatCtx);
|
|
262
|
+
if (!ctx) throw new Error("useCopilotChatContext must be used within a <CopilotChat> component");
|
|
263
|
+
return ctx;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Headless CopilotChat component for React Native.
|
|
267
|
+
*
|
|
268
|
+
* Wires up the `useAgent` hook with `agentId` resolution and renders children.
|
|
269
|
+
* Unlike the web SDK's CopilotChat, this component does not render any UI
|
|
270
|
+
* elements -- consumers provide their own React Native views.
|
|
271
|
+
*
|
|
272
|
+
* Children can access chat state via `useCopilotChatContext()`.
|
|
273
|
+
*
|
|
274
|
+
* ```tsx
|
|
275
|
+
* import { CopilotChat, useCopilotChatContext } from "@copilotkit/react-native";
|
|
276
|
+
*
|
|
277
|
+
* function MyChatUI() {
|
|
278
|
+
* const { messages, submitMessage, attachments, openPicker } = useCopilotChatContext();
|
|
279
|
+
* // ... render your UI
|
|
280
|
+
* }
|
|
281
|
+
*
|
|
282
|
+
* <CopilotChat agentId="my-agent" attachments={{ enabled: true }}>
|
|
283
|
+
* <MyChatUI />
|
|
284
|
+
* </CopilotChat>
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
function CopilotChat({ agentId, agentName, threadId, onError, throttleMs, attachments: attachmentsConfig, children, ..._rest }) {
|
|
288
|
+
const resolvedAgentId = agentId ?? agentName ?? DEFAULT_AGENT_ID;
|
|
289
|
+
const warnedRef = useRef(false);
|
|
290
|
+
useEffect(() => {
|
|
291
|
+
if (agentName !== void 0 && agentId === void 0 && !warnedRef.current) {
|
|
292
|
+
warnedRef.current = true;
|
|
293
|
+
if (typeof __DEV__ === "undefined" || __DEV__) console.warn("[CopilotKit] agentName is deprecated, use agentId instead");
|
|
294
|
+
}
|
|
295
|
+
}, [agentName, agentId]);
|
|
296
|
+
const { agent } = useAgent$1({
|
|
297
|
+
agentId: resolvedAgentId,
|
|
298
|
+
throttleMs
|
|
299
|
+
});
|
|
300
|
+
useEffect(() => {
|
|
301
|
+
if (threadId) agent.threadId = threadId;
|
|
302
|
+
}, [agent, threadId]);
|
|
303
|
+
const { copilotkit } = useCopilotKit$1();
|
|
304
|
+
const onErrorRef = useRef(onError);
|
|
305
|
+
useEffect(() => {
|
|
306
|
+
onErrorRef.current = onError;
|
|
307
|
+
}, [onError]);
|
|
308
|
+
useEffect(() => {
|
|
309
|
+
if (!onErrorRef.current) return;
|
|
310
|
+
const subscription = copilotkit.subscribe({ onError: (event) => {
|
|
311
|
+
if (event.context?.agentId === resolvedAgentId || !event.context?.agentId) onErrorRef.current?.({
|
|
312
|
+
error: event.error,
|
|
313
|
+
code: event.code,
|
|
314
|
+
context: event.context
|
|
315
|
+
});
|
|
316
|
+
} });
|
|
317
|
+
return () => {
|
|
318
|
+
subscription.unsubscribe();
|
|
319
|
+
};
|
|
320
|
+
}, [copilotkit, resolvedAgentId]);
|
|
321
|
+
const { attachments: selectedAttachments, enabled: attachmentsEnabled, openPicker, removeAttachment, consumeAttachments } = useAttachments({ config: attachmentsConfig });
|
|
322
|
+
const submitMessage = useCallback(async (value) => {
|
|
323
|
+
if (selectedAttachments.some((a) => a.status === "uploading")) {
|
|
324
|
+
console.error("[CopilotKit] Cannot send while attachments are uploading");
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const readyAttachments = consumeAttachments();
|
|
328
|
+
if (readyAttachments.length > 0) {
|
|
329
|
+
const contentParts = [];
|
|
330
|
+
if (value.trim()) contentParts.push({
|
|
331
|
+
type: "text",
|
|
332
|
+
text: value
|
|
333
|
+
});
|
|
334
|
+
for (const att of readyAttachments) contentParts.push({
|
|
335
|
+
type: att.type,
|
|
336
|
+
source: att.source,
|
|
337
|
+
metadata: {
|
|
338
|
+
...att.filename ? { filename: att.filename } : {},
|
|
339
|
+
...att.metadata
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
agent.addMessage({
|
|
343
|
+
id: randomUUID(),
|
|
344
|
+
role: "user",
|
|
345
|
+
content: contentParts
|
|
346
|
+
});
|
|
347
|
+
} else agent.addMessage({
|
|
348
|
+
id: randomUUID(),
|
|
349
|
+
role: "user",
|
|
350
|
+
content: value
|
|
351
|
+
});
|
|
352
|
+
try {
|
|
353
|
+
await copilotkit.runAgent({ agent });
|
|
354
|
+
} catch (error) {
|
|
355
|
+
console.error("CopilotChat: runAgent failed", error);
|
|
356
|
+
}
|
|
357
|
+
}, [
|
|
358
|
+
agent,
|
|
359
|
+
selectedAttachments,
|
|
360
|
+
consumeAttachments
|
|
361
|
+
]);
|
|
362
|
+
const contextValue = {
|
|
363
|
+
agent,
|
|
364
|
+
isRunning: agent.isRunning,
|
|
365
|
+
messages: agent.messages,
|
|
366
|
+
attachments: selectedAttachments,
|
|
367
|
+
attachmentsEnabled,
|
|
368
|
+
openPicker,
|
|
369
|
+
removeAttachment,
|
|
370
|
+
submitMessage
|
|
371
|
+
};
|
|
372
|
+
return /* @__PURE__ */ jsx(CopilotChatCtx.Provider, {
|
|
373
|
+
value: contextValue,
|
|
374
|
+
children
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/CopilotModal.tsx
|
|
380
|
+
/**
|
|
381
|
+
* Headless CopilotModal component for React Native.
|
|
382
|
+
*
|
|
383
|
+
* A thin wrapper around CopilotChat that mirrors the web SDK's CopilotModal
|
|
384
|
+
* API surface. On React Native, modal presentation is handled by the consumer
|
|
385
|
+
* (e.g. React Native's `Modal` component) -- this component only provides
|
|
386
|
+
* the agent wiring and prop resolution.
|
|
387
|
+
*
|
|
388
|
+
* ```tsx
|
|
389
|
+
* import { CopilotModal } from "@copilotkit/react-native";
|
|
390
|
+
* import { Modal } from "react-native";
|
|
391
|
+
*
|
|
392
|
+
* <Modal visible={isOpen}>
|
|
393
|
+
* <CopilotModal agentId="my-agent">
|
|
394
|
+
* <MyChatUI />
|
|
395
|
+
* </CopilotModal>
|
|
396
|
+
* </Modal>
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
function CopilotModal({ children, ...props }) {
|
|
400
|
+
return /* @__PURE__ */ jsx(CopilotChat, {
|
|
401
|
+
...props,
|
|
402
|
+
children
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
//#endregion
|
|
407
|
+
//#region src/CopilotSidebar.tsx
|
|
408
|
+
const ANIMATION_DURATION_MS = 300;
|
|
409
|
+
const DEFAULT_HEADER_TITLE = "Copilot";
|
|
410
|
+
const BACKDROP_OPACITY = .4;
|
|
411
|
+
const FAB_SIZE = 56;
|
|
412
|
+
/**
|
|
413
|
+
* CopilotSidebar -- a slide-in drawer from the right edge of the screen
|
|
414
|
+
* that wraps CopilotChat for React Native.
|
|
415
|
+
*
|
|
416
|
+
* ```tsx
|
|
417
|
+
* import { CopilotSidebar } from "@copilotkit/react-native";
|
|
418
|
+
*
|
|
419
|
+
* const ref = useRef<CopilotSidebarHandle>(null);
|
|
420
|
+
*
|
|
421
|
+
* <CopilotSidebar
|
|
422
|
+
* ref={ref}
|
|
423
|
+
* agentId="my-agent"
|
|
424
|
+
* headerTitle="Assistant"
|
|
425
|
+
* defaultOpen={false}
|
|
426
|
+
* />
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
const CopilotSidebar = forwardRef(function CopilotSidebar({ agentId, agentName, threadId, onError, throttleMs, defaultOpen = false, width: widthProp, headerTitle = DEFAULT_HEADER_TITLE, showToggleButton = true, onOpen, onClose, style, children, ...rest }, ref) {
|
|
430
|
+
const { width: screenWidth } = useWindowDimensions();
|
|
431
|
+
const drawerWidth = resolveWidth(widthProp, screenWidth);
|
|
432
|
+
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
433
|
+
const slideAnim = useRef(new Animated.Value(defaultOpen ? 0 : drawerWidth)).current;
|
|
434
|
+
useEffect(() => {
|
|
435
|
+
if (!isOpen) slideAnim.setValue(drawerWidth);
|
|
436
|
+
}, [
|
|
437
|
+
drawerWidth,
|
|
438
|
+
isOpen,
|
|
439
|
+
slideAnim
|
|
440
|
+
]);
|
|
441
|
+
const animateTo = useCallback((toValue, cb) => {
|
|
442
|
+
Animated.timing(slideAnim, {
|
|
443
|
+
toValue,
|
|
444
|
+
duration: ANIMATION_DURATION_MS,
|
|
445
|
+
useNativeDriver: true
|
|
446
|
+
}).start(({ finished }) => {
|
|
447
|
+
if (finished) cb?.();
|
|
448
|
+
});
|
|
449
|
+
}, [slideAnim]);
|
|
450
|
+
const open = useCallback(() => {
|
|
451
|
+
setIsOpen(true);
|
|
452
|
+
animateTo(0, onOpen);
|
|
453
|
+
}, [animateTo, onOpen]);
|
|
454
|
+
const close = useCallback(() => {
|
|
455
|
+
animateTo(drawerWidth, () => {
|
|
456
|
+
setIsOpen(false);
|
|
457
|
+
onClose?.();
|
|
458
|
+
});
|
|
459
|
+
}, [
|
|
460
|
+
animateTo,
|
|
461
|
+
drawerWidth,
|
|
462
|
+
onClose
|
|
463
|
+
]);
|
|
464
|
+
const toggle = useCallback(() => {
|
|
465
|
+
if (isOpen) close();
|
|
466
|
+
else open();
|
|
467
|
+
}, [
|
|
468
|
+
isOpen,
|
|
469
|
+
open,
|
|
470
|
+
close
|
|
471
|
+
]);
|
|
472
|
+
useImperativeHandle(ref, () => ({
|
|
473
|
+
open,
|
|
474
|
+
close,
|
|
475
|
+
toggle
|
|
476
|
+
}), [
|
|
477
|
+
open,
|
|
478
|
+
close,
|
|
479
|
+
toggle
|
|
480
|
+
]);
|
|
481
|
+
const onOpenRef = useRef(onOpen);
|
|
482
|
+
const onCloseRef = useRef(onClose);
|
|
483
|
+
useEffect(() => {
|
|
484
|
+
onOpenRef.current = onOpen;
|
|
485
|
+
}, [onOpen]);
|
|
486
|
+
useEffect(() => {
|
|
487
|
+
onCloseRef.current = onClose;
|
|
488
|
+
}, [onClose]);
|
|
489
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
490
|
+
isOpen && /* @__PURE__ */ jsx(Pressable, {
|
|
491
|
+
style: styles$1.backdrop,
|
|
492
|
+
onPress: close,
|
|
493
|
+
accessibilityRole: "button",
|
|
494
|
+
accessibilityLabel: "Close sidebar",
|
|
495
|
+
testID: "copilot-sidebar-backdrop"
|
|
496
|
+
}),
|
|
497
|
+
isOpen && /* @__PURE__ */ jsxs(Animated.View, {
|
|
498
|
+
style: [
|
|
499
|
+
styles$1.drawer,
|
|
500
|
+
{
|
|
501
|
+
width: drawerWidth,
|
|
502
|
+
transform: [{ translateX: slideAnim }]
|
|
503
|
+
},
|
|
504
|
+
style
|
|
505
|
+
],
|
|
506
|
+
testID: "copilot-sidebar-drawer",
|
|
507
|
+
children: [/* @__PURE__ */ jsxs(View, {
|
|
508
|
+
style: styles$1.header,
|
|
509
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
510
|
+
style: styles$1.headerTitle,
|
|
511
|
+
children: headerTitle
|
|
512
|
+
}), /* @__PURE__ */ jsx(Pressable, {
|
|
513
|
+
onPress: close,
|
|
514
|
+
accessibilityRole: "button",
|
|
515
|
+
accessibilityLabel: "Close",
|
|
516
|
+
hitSlop: 8,
|
|
517
|
+
testID: "copilot-sidebar-close",
|
|
518
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
519
|
+
style: styles$1.closeButton,
|
|
520
|
+
children: "✕"
|
|
521
|
+
})
|
|
522
|
+
})]
|
|
523
|
+
}), /* @__PURE__ */ jsx(View, {
|
|
524
|
+
style: styles$1.chatContainer,
|
|
525
|
+
children: /* @__PURE__ */ jsx(CopilotChat, {
|
|
526
|
+
agentId,
|
|
527
|
+
agentName,
|
|
528
|
+
threadId,
|
|
529
|
+
onError,
|
|
530
|
+
throttleMs,
|
|
531
|
+
...rest,
|
|
532
|
+
children
|
|
533
|
+
})
|
|
534
|
+
})]
|
|
535
|
+
}),
|
|
536
|
+
showToggleButton && !isOpen && /* @__PURE__ */ jsx(Pressable, {
|
|
537
|
+
style: styles$1.fab,
|
|
538
|
+
onPress: open,
|
|
539
|
+
accessibilityRole: "button",
|
|
540
|
+
accessibilityLabel: "Open sidebar",
|
|
541
|
+
testID: "copilot-sidebar-fab",
|
|
542
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
543
|
+
style: styles$1.fabIcon,
|
|
544
|
+
children: "💬"
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
] });
|
|
548
|
+
});
|
|
549
|
+
function resolveWidth(widthProp, screenWidth) {
|
|
550
|
+
if (widthProp === void 0) return Math.round(screenWidth * .85);
|
|
551
|
+
if (typeof widthProp === "number") return widthProp;
|
|
552
|
+
const pctMatch = String(widthProp).match(/^(\d+(?:\.\d+)?)%$/);
|
|
553
|
+
if (pctMatch) return Math.round(screenWidth * (parseFloat(pctMatch[1]) / 100));
|
|
554
|
+
const parsed = parseFloat(widthProp);
|
|
555
|
+
return isNaN(parsed) ? Math.round(screenWidth * .85) : parsed;
|
|
556
|
+
}
|
|
557
|
+
const styles$1 = StyleSheet.create({
|
|
558
|
+
backdrop: {
|
|
559
|
+
...StyleSheet.absoluteFillObject,
|
|
560
|
+
backgroundColor: `rgba(0, 0, 0, ${BACKDROP_OPACITY})`,
|
|
561
|
+
zIndex: 999
|
|
562
|
+
},
|
|
563
|
+
drawer: {
|
|
564
|
+
position: "absolute",
|
|
565
|
+
top: 0,
|
|
566
|
+
right: 0,
|
|
567
|
+
bottom: 0,
|
|
568
|
+
backgroundColor: "#ffffff",
|
|
569
|
+
zIndex: 1e3,
|
|
570
|
+
shadowColor: "#000",
|
|
571
|
+
shadowOffset: {
|
|
572
|
+
width: -2,
|
|
573
|
+
height: 0
|
|
574
|
+
},
|
|
575
|
+
shadowOpacity: .25,
|
|
576
|
+
shadowRadius: 8,
|
|
577
|
+
elevation: 16
|
|
578
|
+
},
|
|
579
|
+
header: {
|
|
580
|
+
flexDirection: "row",
|
|
581
|
+
alignItems: "center",
|
|
582
|
+
justifyContent: "space-between",
|
|
583
|
+
paddingHorizontal: 16,
|
|
584
|
+
paddingVertical: 12,
|
|
585
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
586
|
+
borderBottomColor: "#e0e0e0"
|
|
587
|
+
},
|
|
588
|
+
headerTitle: {
|
|
589
|
+
fontSize: 18,
|
|
590
|
+
fontWeight: "600",
|
|
591
|
+
color: "#1a1a1a"
|
|
592
|
+
},
|
|
593
|
+
closeButton: {
|
|
594
|
+
fontSize: 20,
|
|
595
|
+
color: "#666",
|
|
596
|
+
padding: 4
|
|
597
|
+
},
|
|
598
|
+
chatContainer: { flex: 1 },
|
|
599
|
+
fab: {
|
|
600
|
+
position: "absolute",
|
|
601
|
+
bottom: 24,
|
|
602
|
+
right: 24,
|
|
603
|
+
width: FAB_SIZE,
|
|
604
|
+
height: FAB_SIZE,
|
|
605
|
+
borderRadius: FAB_SIZE / 2,
|
|
606
|
+
backgroundColor: "#007AFF",
|
|
607
|
+
alignItems: "center",
|
|
608
|
+
justifyContent: "center",
|
|
609
|
+
shadowColor: "#000",
|
|
610
|
+
shadowOffset: {
|
|
611
|
+
width: 0,
|
|
612
|
+
height: 2
|
|
613
|
+
},
|
|
614
|
+
shadowOpacity: .3,
|
|
615
|
+
shadowRadius: 4,
|
|
616
|
+
elevation: 8,
|
|
617
|
+
zIndex: 998
|
|
618
|
+
},
|
|
619
|
+
fabIcon: { fontSize: 24 }
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
//#endregion
|
|
623
|
+
//#region src/CopilotPopup.tsx
|
|
624
|
+
/**
|
|
625
|
+
* CopilotPopup for React Native.
|
|
626
|
+
*
|
|
627
|
+
* A floating action button (FAB) that opens a modal chat overlay.
|
|
628
|
+
* The popup appears as a card floating above content with rounded corners,
|
|
629
|
+
* a shadow, and a semi-transparent backdrop.
|
|
630
|
+
*
|
|
631
|
+
* ```tsx
|
|
632
|
+
* import { CopilotPopup } from "@copilotkit/react-native";
|
|
633
|
+
*
|
|
634
|
+
* const popupRef = useRef<CopilotPopupHandle>(null);
|
|
635
|
+
*
|
|
636
|
+
* <CopilotPopup
|
|
637
|
+
* ref={popupRef}
|
|
638
|
+
* agentId="my-agent"
|
|
639
|
+
* headerTitle="Chat"
|
|
640
|
+
* defaultOpen={false}
|
|
641
|
+
* />
|
|
642
|
+
* ```
|
|
643
|
+
*/
|
|
644
|
+
const CopilotPopup = forwardRef(function CopilotPopup({ agentId, agentName, threadId, throttleMs, defaultOpen = false, height = "60%", onError, headerTitle = "CopilotKit", attachments: attachmentsConfig, children, onOpen, onClose, dismissOnBackdropPress = true, showToggleButton = true, style }, ref) {
|
|
645
|
+
const [visible, setVisible] = useState(defaultOpen);
|
|
646
|
+
const { height: screenHeight } = useWindowDimensions();
|
|
647
|
+
const onOpenRef = useRef(onOpen);
|
|
648
|
+
const onCloseRef = useRef(onClose);
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
onOpenRef.current = onOpen;
|
|
651
|
+
}, [onOpen]);
|
|
652
|
+
useEffect(() => {
|
|
653
|
+
onCloseRef.current = onClose;
|
|
654
|
+
}, [onClose]);
|
|
655
|
+
const handleOpen = useCallback(() => {
|
|
656
|
+
setVisible(true);
|
|
657
|
+
onOpenRef.current?.();
|
|
658
|
+
}, []);
|
|
659
|
+
const handleClose = useCallback(() => {
|
|
660
|
+
setVisible(false);
|
|
661
|
+
onCloseRef.current?.();
|
|
662
|
+
}, []);
|
|
663
|
+
const handleToggle = useCallback(() => {
|
|
664
|
+
setVisible((prev) => {
|
|
665
|
+
const next = !prev;
|
|
666
|
+
if (next) onOpenRef.current?.();
|
|
667
|
+
else onCloseRef.current?.();
|
|
668
|
+
return next;
|
|
669
|
+
});
|
|
670
|
+
}, []);
|
|
671
|
+
useImperativeHandle(ref, () => ({
|
|
672
|
+
open: handleOpen,
|
|
673
|
+
close: handleClose,
|
|
674
|
+
toggle: handleToggle
|
|
675
|
+
}), [
|
|
676
|
+
handleOpen,
|
|
677
|
+
handleClose,
|
|
678
|
+
handleToggle
|
|
679
|
+
]);
|
|
680
|
+
const resolvedHeight = typeof height === "string" && height.endsWith("%") ? parseFloat(height) / 100 * screenHeight : typeof height === "number" ? height : .6 * screenHeight;
|
|
681
|
+
const chatOnError = onError ? (event) => onError(event.error) : void 0;
|
|
682
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [showToggleButton && !visible && /* @__PURE__ */ jsx(TouchableOpacity, {
|
|
683
|
+
testID: "copilot-popup-fab",
|
|
684
|
+
style: styles.fab,
|
|
685
|
+
onPress: handleToggle,
|
|
686
|
+
activeOpacity: .8,
|
|
687
|
+
accessibilityLabel: "Open chat",
|
|
688
|
+
accessibilityRole: "button",
|
|
689
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
690
|
+
style: styles.fabIcon,
|
|
691
|
+
children: "💬"
|
|
692
|
+
})
|
|
693
|
+
}), /* @__PURE__ */ jsx(Modal, {
|
|
694
|
+
testID: "copilot-popup-modal",
|
|
695
|
+
visible,
|
|
696
|
+
transparent: true,
|
|
697
|
+
animationType: "slide",
|
|
698
|
+
onRequestClose: handleClose,
|
|
699
|
+
children: /* @__PURE__ */ jsx(Pressable, {
|
|
700
|
+
testID: "copilot-popup-backdrop",
|
|
701
|
+
style: styles.backdrop,
|
|
702
|
+
onPress: dismissOnBackdropPress ? handleClose : void 0,
|
|
703
|
+
children: /* @__PURE__ */ jsxs(Pressable, {
|
|
704
|
+
testID: "copilot-popup-card",
|
|
705
|
+
style: [
|
|
706
|
+
styles.card,
|
|
707
|
+
{ height: resolvedHeight },
|
|
708
|
+
style
|
|
709
|
+
],
|
|
710
|
+
onPress: () => {},
|
|
711
|
+
children: [/* @__PURE__ */ jsxs(View, {
|
|
712
|
+
style: styles.header,
|
|
713
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
714
|
+
style: styles.headerTitle,
|
|
715
|
+
children: headerTitle
|
|
716
|
+
}), /* @__PURE__ */ jsx(TouchableOpacity, {
|
|
717
|
+
testID: "copilot-popup-close",
|
|
718
|
+
onPress: handleClose,
|
|
719
|
+
hitSlop: {
|
|
720
|
+
top: 8,
|
|
721
|
+
bottom: 8,
|
|
722
|
+
left: 8,
|
|
723
|
+
right: 8
|
|
724
|
+
},
|
|
725
|
+
accessibilityLabel: "Close chat",
|
|
726
|
+
accessibilityRole: "button",
|
|
727
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
728
|
+
style: styles.closeButton,
|
|
729
|
+
children: "✕"
|
|
730
|
+
})
|
|
731
|
+
})]
|
|
732
|
+
}), /* @__PURE__ */ jsx(View, {
|
|
733
|
+
style: styles.chatContainer,
|
|
734
|
+
children: /* @__PURE__ */ jsx(CopilotChat, {
|
|
735
|
+
agentId,
|
|
736
|
+
agentName,
|
|
737
|
+
threadId,
|
|
738
|
+
throttleMs,
|
|
739
|
+
onError: chatOnError,
|
|
740
|
+
attachments: attachmentsConfig,
|
|
741
|
+
children
|
|
742
|
+
})
|
|
743
|
+
})]
|
|
744
|
+
})
|
|
745
|
+
})
|
|
746
|
+
})] });
|
|
747
|
+
});
|
|
748
|
+
const styles = StyleSheet.create({
|
|
749
|
+
fab: {
|
|
750
|
+
position: "absolute",
|
|
751
|
+
bottom: 24,
|
|
752
|
+
right: 24,
|
|
753
|
+
width: 56,
|
|
754
|
+
height: 56,
|
|
755
|
+
borderRadius: 28,
|
|
756
|
+
backgroundColor: "#6366f1",
|
|
757
|
+
alignItems: "center",
|
|
758
|
+
justifyContent: "center",
|
|
759
|
+
elevation: 6,
|
|
760
|
+
shadowColor: "#000",
|
|
761
|
+
shadowOffset: {
|
|
762
|
+
width: 0,
|
|
763
|
+
height: 3
|
|
764
|
+
},
|
|
765
|
+
shadowOpacity: .27,
|
|
766
|
+
shadowRadius: 4.65
|
|
767
|
+
},
|
|
768
|
+
fabIcon: { fontSize: 24 },
|
|
769
|
+
backdrop: {
|
|
770
|
+
flex: 1,
|
|
771
|
+
backgroundColor: "rgba(0, 0, 0, 0.4)",
|
|
772
|
+
justifyContent: "flex-end"
|
|
773
|
+
},
|
|
774
|
+
card: {
|
|
775
|
+
backgroundColor: "#ffffff",
|
|
776
|
+
borderTopLeftRadius: 16,
|
|
777
|
+
borderTopRightRadius: 16,
|
|
778
|
+
overflow: "hidden",
|
|
779
|
+
elevation: 10,
|
|
780
|
+
shadowColor: "#000",
|
|
781
|
+
shadowOffset: {
|
|
782
|
+
width: 0,
|
|
783
|
+
height: -3
|
|
784
|
+
},
|
|
785
|
+
shadowOpacity: .25,
|
|
786
|
+
shadowRadius: 8
|
|
787
|
+
},
|
|
788
|
+
header: {
|
|
789
|
+
flexDirection: "row",
|
|
790
|
+
alignItems: "center",
|
|
791
|
+
justifyContent: "space-between",
|
|
792
|
+
paddingHorizontal: 16,
|
|
793
|
+
paddingVertical: 12,
|
|
794
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
795
|
+
borderBottomColor: "#e5e7eb"
|
|
796
|
+
},
|
|
797
|
+
headerTitle: {
|
|
798
|
+
fontSize: 17,
|
|
799
|
+
fontWeight: "600",
|
|
800
|
+
color: "#111827"
|
|
801
|
+
},
|
|
802
|
+
closeButton: {
|
|
803
|
+
fontSize: 18,
|
|
804
|
+
color: "#6b7280",
|
|
805
|
+
fontWeight: "500"
|
|
806
|
+
},
|
|
807
|
+
chatContainer: { flex: 1 }
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
//#endregion
|
|
811
|
+
export { CopilotChat, CopilotChatDefaultLabels, CopilotKitContext, CopilotKitProvider, CopilotModal, CopilotPopup, CopilotSidebar, defineToolCallRenderer, useAgent, useAgentContext, useAttachments, useCapabilities, useComponent, useConfigureSuggestions, useCopilotChatContext, useCopilotKit, useFrontendTool, useHumanInTheLoop, useInterrupt, useLicenseContext, useRenderTool, useSuggestions, useThreads };
|
|
101
812
|
//# sourceMappingURL=index.mjs.map
|