@copilotkit/react-native 2.0.0-rc.5 → 2.0.0-rc.50
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 +334 -2
- package/USAGE.md +382 -21
- package/dist/CopilotKitProvider-CBVQLdzn.d.cts +77 -0
- package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-CCBBonxr.mjs} +63 -43
- package/dist/CopilotKitProvider-CCBBonxr.mjs.map +1 -0
- package/dist/CopilotKitProvider-CtQg_0S-.d.mts +77 -0
- package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-QdcQrrSA.cjs} +69 -42
- package/dist/CopilotKitProvider-QdcQrrSA.cjs.map +1 -0
- package/dist/{Markdown-C_9Re36O.cjs → Markdown-DLopAh5W.cjs} +10 -10
- package/dist/Markdown-DLopAh5W.cjs.map +1 -0
- package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-Du-b_Qpr.d.mts} +3 -3
- package/dist/{Markdown-1ips7myr.d.cts → Markdown-UXZdDjzs.d.cts} +3 -3
- package/dist/{Markdown-C6AsLHmd.mjs → Markdown-VxtPJ_yh.mjs} +4 -4
- package/dist/Markdown-VxtPJ_yh.mjs.map +1 -0
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-EobUsERg.mjs} +1 -1
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-EobUsERg.mjs.map} +1 -1
- package/dist/MarkdownRendererContext-cDEHvbw9.cjs +40 -0
- package/dist/{MarkdownRendererContext-BRNe808d.cjs.map → MarkdownRendererContext-cDEHvbw9.cjs.map} +1 -1
- package/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/components/index.cjs +78 -175
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +76 -174
- package/dist/components/index.mjs.map +1 -1
- package/dist/generative-ui/index.cjs +612 -0
- package/dist/generative-ui/index.cjs.map +1 -0
- package/dist/generative-ui/index.d.cts +341 -0
- package/dist/generative-ui/index.d.mts +341 -0
- package/dist/generative-ui/index.mjs +549 -0
- package/dist/generative-ui/index.mjs.map +1 -0
- package/dist/headless.cjs +3 -2
- package/dist/headless.d.cts +3 -3
- package/dist/headless.d.mts +3 -3
- package/dist/headless.mjs +2 -2
- package/dist/index.cjs +132 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -19
- package/dist/index.d.mts +37 -19
- package/dist/index.mjs +131 -162
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +115 -5
- package/dist/polyfills/encoding.cjs.map +1 -1
- package/dist/polyfills/encoding.d.cts +31 -1
- package/dist/polyfills/encoding.d.mts +31 -1
- package/dist/polyfills/encoding.mjs +113 -6
- package/dist/polyfills/encoding.mjs.map +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/dist/polyfills.cjs +218 -2
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +217 -1
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +51 -21
- package/project.json +6 -1
- package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
- package/scripts/verify-polyfill-barrel.mjs +158 -11
- package/src/CopilotChat.tsx +114 -93
- package/src/CopilotKitProvider.tsx +101 -130
- package/src/CopilotPopup.tsx +2 -2
- package/src/CopilotSidebar.tsx +7 -5
- package/src/__mocks__/react-native.ts +31 -0
- package/src/__mocks__/test-copilotkit.tsx +9 -3
- package/src/__tests__/attachments-integration.test.tsx +15 -2
- package/src/__tests__/copilot-chat-agentid.test.tsx +15 -2
- package/src/__tests__/copilot-chat-attachments.test.tsx +15 -2
- package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
- package/src/__tests__/copilot-chat-threads.test.tsx +232 -0
- package/src/__tests__/copilot-popup.test.tsx +15 -2
- package/src/__tests__/copilot-sidebar.test.tsx +15 -2
- package/src/__tests__/copilotkit-alias-exports.test.ts +16 -0
- package/src/__tests__/copilotkit-provider.test.tsx +68 -73
- package/src/__tests__/generative-ui-harness.tsx +243 -0
- package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
- package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
- package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
- package/src/__tests__/generative-ui.test.tsx +1000 -0
- package/src/__tests__/headless-entry-surface.test.ts +979 -61
- package/src/__tests__/headless-integration.test.tsx +11 -1
- package/src/__tests__/use-attachments.test.ts +27 -2
- package/src/components/CopilotChat.tsx +106 -233
- package/src/components/CopilotModal.tsx +1 -1
- package/src/components/Markdown.tsx +4 -4
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
- package/src/components/__tests__/CopilotChat.test.tsx +12 -4
- package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
- package/src/components/__tests__/Markdown.code-alias.test.tsx +1 -1
- package/src/components/index.ts +6 -0
- package/src/components/messages/AssistantMessage.tsx +3 -3
- package/src/components/messages/TypingIndicator.tsx +4 -4
- package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
- package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
- package/src/generative-ui/GenerativeUISurface.tsx +303 -0
- package/src/generative-ui/host.tsx +138 -0
- package/src/generative-ui/index.ts +93 -0
- package/src/generative-ui/internal.ts +55 -0
- package/src/headless.ts +4 -0
- package/src/hooks/use-attachments.ts +95 -139
- package/src/index.ts +10 -4
- package/src/polyfills/__tests__/encoding-utf8.test.ts +112 -0
- package/src/polyfills/encoding.ts +160 -4
- package/src/types.d.ts +0 -13
- package/tsdown.config.ts +1 -0
- package/dist/CopilotKitProvider-BWjzCdHN.d.mts +0 -121
- package/dist/CopilotKitProvider-Btm10msB.d.cts +0 -121
- package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +0 -1
- package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +0 -1
- package/dist/Markdown-C6AsLHmd.mjs.map +0 -1
- package/dist/Markdown-C_9Re36O.cjs.map +0 -1
- package/dist/MarkdownRendererContext-BRNe808d.cjs +0 -72
- package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
- package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
- package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
- package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
package/USAGE.md
CHANGED
|
@@ -2,23 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
## Prerequisites
|
|
4
4
|
|
|
5
|
-
Install
|
|
5
|
+
Install the peer dependencies the Quick Start below uses:
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install react react-native @gorhom/bottom-sheet react-native-gesture-handler react-native-reanimated
|
|
8
|
+
npm install react react-native zod @gorhom/bottom-sheet react-native-gesture-handler react-native-reanimated expo-document-picker expo-file-system
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
`zod` 4.2 or later is what you write tool parameters and generative UI catalogs with. `@copilotkit/react-core` depends on zod 4, and the `child()` and `childList()` helpers return zod 4 schemas.
|
|
12
|
+
|
|
13
|
+
The other packages are optional peer dependencies. The default entry (`@copilotkit/react-native`) re-exports `useAttachments`, which imports `expo-document-picker` and `expo-file-system`, so Metro needs both whenever an app imports from it. `@gorhom/bottom-sheet`, `react-native-gesture-handler` and `react-native-reanimated` are used by the prebuilt components in `@copilotkit/react-native/components`. An app that only imports `@copilotkit/react-native/headless` or `/generative-ui` needs none of them (see "Headless Import Path" below). `CopilotMarkdown` uses a built-in basic-GFM renderer — no extra markdown library install required.
|
|
12
14
|
|
|
13
15
|
## Quick Start
|
|
14
16
|
|
|
17
|
+
The provider and the hooks come from `@copilotkit/react-native`. The prebuilt
|
|
18
|
+
chat UI comes from `@copilotkit/react-native/components`.
|
|
19
|
+
|
|
15
20
|
```tsx
|
|
16
21
|
import "@copilotkit/react-native/polyfills";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
CopilotChat,
|
|
20
|
-
useFrontendTool,
|
|
21
|
-
} from "@copilotkit/react-native";
|
|
22
|
+
import { CopilotKitProvider, useFrontendTool } from "@copilotkit/react-native";
|
|
23
|
+
import { CopilotChat } from "@copilotkit/react-native/components";
|
|
22
24
|
import { z } from "zod";
|
|
23
25
|
|
|
24
26
|
function App() {
|
|
@@ -35,7 +37,8 @@ function ChatScreen() {
|
|
|
35
37
|
name: "showWeather",
|
|
36
38
|
description: "Show weather info",
|
|
37
39
|
parameters: z.object({ city: z.string() }),
|
|
38
|
-
|
|
40
|
+
// `args` is partial while the agent is still writing the call.
|
|
41
|
+
render: ({ args }) => <WeatherCard city={args.city ?? ""} />,
|
|
39
42
|
});
|
|
40
43
|
|
|
41
44
|
return <CopilotChat placeholder="Ask anything..." />;
|
|
@@ -44,22 +47,31 @@ function ChatScreen() {
|
|
|
44
47
|
|
|
45
48
|
## Available Components
|
|
46
49
|
|
|
50
|
+
The components in this section are the prebuilt UI, and they are exported only
|
|
51
|
+
by `@copilotkit/react-native/components`. The package root exports a
|
|
52
|
+
`CopilotChat` and a `CopilotModal` too, but those are headless: they render
|
|
53
|
+
no UI and take no `placeholder` or `headerTitle` (see
|
|
54
|
+
[Headless chat components](#headless-chat-components)).
|
|
55
|
+
|
|
47
56
|
### CopilotChat
|
|
48
57
|
|
|
49
58
|
Inline chat panel. Renders a message list with an input bar.
|
|
50
59
|
|
|
51
60
|
```tsx
|
|
52
|
-
import { CopilotChat } from "@copilotkit/react-native";
|
|
61
|
+
import { CopilotChat } from "@copilotkit/react-native/components";
|
|
53
62
|
|
|
54
63
|
<CopilotChat placeholder="Type a message..." />;
|
|
55
64
|
```
|
|
56
65
|
|
|
57
66
|
### CopilotModal
|
|
58
67
|
|
|
59
|
-
Modal chat overlay. Open/close programmatically via a ref.
|
|
68
|
+
Modal chat overlay (a bottom sheet). Open/close programmatically via a ref.
|
|
60
69
|
|
|
61
70
|
```tsx
|
|
62
|
-
import {
|
|
71
|
+
import {
|
|
72
|
+
CopilotModal,
|
|
73
|
+
type CopilotModalRef,
|
|
74
|
+
} from "@copilotkit/react-native/components";
|
|
63
75
|
import { useRef } from "react";
|
|
64
76
|
|
|
65
77
|
const modalRef = useRef<CopilotModalRef>(null);
|
|
@@ -75,7 +87,7 @@ modalRef.current?.open();
|
|
|
75
87
|
Renders Markdown text with sensible React Native styling.
|
|
76
88
|
|
|
77
89
|
```tsx
|
|
78
|
-
import { CopilotMarkdown } from "@copilotkit/react-native";
|
|
90
|
+
import { CopilotMarkdown } from "@copilotkit/react-native/components";
|
|
79
91
|
|
|
80
92
|
<CopilotMarkdown content="**Hello** from CopilotKit!" />;
|
|
81
93
|
```
|
|
@@ -85,7 +97,10 @@ import { CopilotMarkdown } from "@copilotkit/react-native";
|
|
|
85
97
|
Individual message bubbles. Useful when building a custom chat UI.
|
|
86
98
|
|
|
87
99
|
```tsx
|
|
88
|
-
import {
|
|
100
|
+
import {
|
|
101
|
+
AssistantMessage,
|
|
102
|
+
UserMessage,
|
|
103
|
+
} from "@copilotkit/react-native/components";
|
|
89
104
|
|
|
90
105
|
<UserMessage content="What's the weather?" />
|
|
91
106
|
<AssistantMessage content="It's sunny!" isLoading={false} />
|
|
@@ -108,7 +123,7 @@ useFrontendTool({
|
|
|
108
123
|
name: "showChart",
|
|
109
124
|
description: "Display a chart",
|
|
110
125
|
parameters: z.object({ data: z.record(z.unknown()) }),
|
|
111
|
-
render: ({ args }) => <ChartView data={args.data} />,
|
|
126
|
+
render: ({ args }) => <ChartView data={args.data ?? {}} />,
|
|
112
127
|
});
|
|
113
128
|
```
|
|
114
129
|
|
|
@@ -177,12 +192,34 @@ untyped, so it still compiles there).
|
|
|
177
192
|
See the [`useRenderTool` reference](https://docs.copilotkit.ai/reference/react-native/hooks/useRenderTool)
|
|
178
193
|
for the full migration table.
|
|
179
194
|
|
|
180
|
-
##
|
|
195
|
+
## Headless chat components
|
|
181
196
|
|
|
182
|
-
|
|
197
|
+
`CopilotChat` and `CopilotModal` from the package root render no UI. They wire
|
|
198
|
+
up the agent and render their children, which read the chat state with
|
|
199
|
+
`useCopilotChatContext()`:
|
|
183
200
|
|
|
184
201
|
```tsx
|
|
185
|
-
import {
|
|
202
|
+
import { Pressable, Text, View } from "react-native";
|
|
203
|
+
import { CopilotChat, useCopilotChatContext } from "@copilotkit/react-native";
|
|
204
|
+
|
|
205
|
+
function MyChatUI() {
|
|
206
|
+
const { messages, isRunning, submitMessage } = useCopilotChatContext();
|
|
207
|
+
return (
|
|
208
|
+
<View>
|
|
209
|
+
<Text>{`${messages.length} messages`}</Text>
|
|
210
|
+
<Pressable
|
|
211
|
+
disabled={isRunning}
|
|
212
|
+
onPress={() => void submitMessage("Hello!")}
|
|
213
|
+
>
|
|
214
|
+
<Text>Say hello</Text>
|
|
215
|
+
</Pressable>
|
|
216
|
+
</View>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
<CopilotChat agentId="my-agent">
|
|
221
|
+
<MyChatUI />
|
|
222
|
+
</CopilotChat>;
|
|
186
223
|
```
|
|
187
224
|
|
|
188
225
|
## Headless Import Path (custom UI, no chat/attachment native deps)
|
|
@@ -200,9 +237,10 @@ import {
|
|
|
200
237
|
```
|
|
201
238
|
|
|
202
239
|
The default barrel (`@copilotkit/react-native`) statically re-exports the
|
|
203
|
-
|
|
204
|
-
`CopilotPopup
|
|
205
|
-
|
|
240
|
+
chat components (`CopilotChat` / `CopilotModal` / `CopilotSidebar` /
|
|
241
|
+
`CopilotPopup`) and `useAttachments` (which imports `expo-document-picker` +
|
|
242
|
+
`expo-file-system`). The bottom-sheet modal that needs `@gorhom/bottom-sheet`
|
|
243
|
+
lives in `@copilotkit/react-native/components`. Even though those are
|
|
206
244
|
optional peer dependencies, the static re-export forces Metro to resolve them at
|
|
207
245
|
bundle time — so a headless consumer previously had to install every chat and
|
|
208
246
|
attachment native dep, or stub them in `metro.config.js`, to get past
|
|
@@ -214,3 +252,326 @@ the render-tool registry, and the core/AG-UI types — none of the chat UI or
|
|
|
214
252
|
`metro.config.js` stub workaround is no longer needed. Polyfills are still
|
|
215
253
|
auto-installed, so no separate `import "@copilotkit/react-native/polyfills"` is
|
|
216
254
|
required.
|
|
255
|
+
|
|
256
|
+
## Generative UI (experimental)
|
|
257
|
+
|
|
258
|
+
`@copilotkit/react-native/generative-ui` lets the agent render a catalog of your
|
|
259
|
+
own native components with the `render_ui` tool. Write the catalog with `zod`
|
|
260
|
+
4.2 or later: `child()` and `childList()` return zod 4 schemas. The runtime needs no change by default: `CopilotKitRuntime` adds the
|
|
261
|
+
tool when the app sends a catalog with at least one component, unless the
|
|
262
|
+
server sets `generativeUI: false` or limits it with
|
|
263
|
+
`generativeUI: { agents: [...] }`. This example needs a `home` agent on the
|
|
264
|
+
runtime: once the runtime has connected, `useAgent` throws without it.
|
|
265
|
+
|
|
266
|
+
```tsx
|
|
267
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
268
|
+
import {
|
|
269
|
+
ActivityIndicator,
|
|
270
|
+
Pressable,
|
|
271
|
+
ScrollView,
|
|
272
|
+
Text,
|
|
273
|
+
View,
|
|
274
|
+
} from "react-native";
|
|
275
|
+
import { z } from "zod";
|
|
276
|
+
import {
|
|
277
|
+
CopilotKitCoreErrorCode,
|
|
278
|
+
CopilotKitCoreRuntimeConnectionStatus,
|
|
279
|
+
CopilotKitProvider,
|
|
280
|
+
useAgent,
|
|
281
|
+
useAgentContext,
|
|
282
|
+
useCopilotKit,
|
|
283
|
+
} from "@copilotkit/react-native/headless";
|
|
284
|
+
import {
|
|
285
|
+
GenerativeUIActivity,
|
|
286
|
+
GenerativeUIProvider,
|
|
287
|
+
createGenerativeUI,
|
|
288
|
+
isGenerativeUIActivity,
|
|
289
|
+
useGenerativeUIRuntime,
|
|
290
|
+
type GenerativeUIActivityMessage,
|
|
291
|
+
} from "@copilotkit/react-native/generative-ui";
|
|
292
|
+
|
|
293
|
+
const ui = createGenerativeUI({
|
|
294
|
+
id: "acme/bank@1",
|
|
295
|
+
components: {
|
|
296
|
+
BalanceCard: {
|
|
297
|
+
component: ({ account, amount }: { account: string; amount: number }) => (
|
|
298
|
+
<View>
|
|
299
|
+
<Text>{account}</Text>
|
|
300
|
+
<Text>{amount}</Text>
|
|
301
|
+
</View>
|
|
302
|
+
),
|
|
303
|
+
description:
|
|
304
|
+
"The balance of one account. Copy the values from the context.",
|
|
305
|
+
props: z.object({ account: z.string(), amount: z.number() }),
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
type CopilotKit = ReturnType<typeof useCopilotKit>["copilotkit"];
|
|
311
|
+
type Agent = ReturnType<typeof useAgent>["agent"];
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* The errors that end a run. A failed tool call is not one of them
|
|
315
|
+
* (TOOL_HANDLER_FAILED, TOOL_ARGUMENT_PARSE_FAILED): its error goes back to
|
|
316
|
+
* the model as the result of the call, and the run goes on.
|
|
317
|
+
*/
|
|
318
|
+
const RUN_FAILURES: ReadonlySet<CopilotKitCoreErrorCode> = new Set([
|
|
319
|
+
CopilotKitCoreErrorCode.AGENT_RUN_FAILED,
|
|
320
|
+
CopilotKitCoreErrorCode.AGENT_RUN_FAILED_EVENT,
|
|
321
|
+
CopilotKitCoreErrorCode.AGENT_RUN_ERROR_EVENT,
|
|
322
|
+
CopilotKitCoreErrorCode.AGENT_THREAD_LOCKED,
|
|
323
|
+
]);
|
|
324
|
+
|
|
325
|
+
// Date.now() alone repeats within a millisecond; with a counter, ids do not.
|
|
326
|
+
let sent = 0;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Sends one user message and runs the agent. Resolves with the run's error,
|
|
330
|
+
* or undefined; it never rejects. It counts every run failure of this agent
|
|
331
|
+
* while it waits, so start one run of an agent at a time.
|
|
332
|
+
*/
|
|
333
|
+
async function runTurn(copilotkit: CopilotKit, agent: Agent, content: string) {
|
|
334
|
+
const before = agent.messages.map((message) => message.id);
|
|
335
|
+
const { threadId } = agent;
|
|
336
|
+
let failure: Error | undefined;
|
|
337
|
+
const subscription = copilotkit.subscribe({
|
|
338
|
+
onError: ({ error, code, context }) => {
|
|
339
|
+
if (context.agentId === agent.agentId && RUN_FAILURES.has(code)) {
|
|
340
|
+
failure ??= error;
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
try {
|
|
345
|
+
sent += 1;
|
|
346
|
+
agent.addMessage({
|
|
347
|
+
id: `user-${Date.now()}-${sent}`,
|
|
348
|
+
role: "user",
|
|
349
|
+
content,
|
|
350
|
+
});
|
|
351
|
+
await copilotkit.runAgent({ agent });
|
|
352
|
+
} catch (error) {
|
|
353
|
+
failure ??= error instanceof Error ? error : new Error(String(error));
|
|
354
|
+
} finally {
|
|
355
|
+
subscription.unsubscribe();
|
|
356
|
+
}
|
|
357
|
+
try {
|
|
358
|
+
if (failure) takeBack(agent, before, threadId);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
console.error("Could not take the failed turn back:", error);
|
|
361
|
+
}
|
|
362
|
+
return failure;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Removes the messages added since `before` (their ids), such as the user
|
|
367
|
+
* message and a partial reply. It keeps the surfaces: the provider keeps
|
|
368
|
+
* every surface that a run rendered, and the model still sees it, so the
|
|
369
|
+
* screen shows it too.
|
|
370
|
+
*/
|
|
371
|
+
function takeBack(
|
|
372
|
+
agent: Agent,
|
|
373
|
+
before: readonly string[],
|
|
374
|
+
threadId: string | undefined,
|
|
375
|
+
) {
|
|
376
|
+
// The agent moved to another thread: those messages are not this turn's.
|
|
377
|
+
if (agent.threadId !== threadId) return;
|
|
378
|
+
const kept = new Set(before);
|
|
379
|
+
agent.setMessages(
|
|
380
|
+
agent.messages.filter(
|
|
381
|
+
(message) => kept.has(message.id) || isGenerativeUIActivity(message),
|
|
382
|
+
),
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* An activity message that GenerativeUIActivity draws: its surface is aimed at
|
|
388
|
+
* the chat (the target it shows) and the agent has not deleted it.
|
|
389
|
+
*/
|
|
390
|
+
function isChatSurface(message: GenerativeUIActivityMessage) {
|
|
391
|
+
if (!isGenerativeUIActivity(message)) return false;
|
|
392
|
+
const content = message.content as {
|
|
393
|
+
target?: unknown;
|
|
394
|
+
operations?: unknown;
|
|
395
|
+
} | null;
|
|
396
|
+
const operations = Array.isArray(content?.operations)
|
|
397
|
+
? content.operations
|
|
398
|
+
: [];
|
|
399
|
+
return (
|
|
400
|
+
content?.target === "chat" &&
|
|
401
|
+
!operations.some(
|
|
402
|
+
(op) => typeof op === "object" && op !== null && "deleteSurface" in op,
|
|
403
|
+
)
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* The chat surfaces that this device built, each message id with its operations,
|
|
409
|
+
* to tell whether a run rendered or updated one here. The operations alone: each
|
|
410
|
+
* call of the agent also refills the list of rejected operations, so a call whose
|
|
411
|
+
* updates were all rejected changes the content but not the surface. A surface
|
|
412
|
+
* whose createSurface the app rejected (`failed`) does not count: the app checks
|
|
413
|
+
* the operations again, prop values included, and GenerativeUIActivity then shows
|
|
414
|
+
* only an error line.
|
|
415
|
+
*/
|
|
416
|
+
function chatSurfaces(
|
|
417
|
+
messages: readonly GenerativeUIActivityMessage[],
|
|
418
|
+
failed: (surfaceId: string) => boolean,
|
|
419
|
+
) {
|
|
420
|
+
const built = new Map<string | undefined, string>();
|
|
421
|
+
for (const message of messages.filter(isChatSurface)) {
|
|
422
|
+
const { surfaceId, operations } = message.content as {
|
|
423
|
+
surfaceId?: unknown;
|
|
424
|
+
operations?: unknown;
|
|
425
|
+
};
|
|
426
|
+
if (typeof surfaceId === "string" && failed(surfaceId)) continue;
|
|
427
|
+
built.set(message.id, JSON.stringify(operations));
|
|
428
|
+
}
|
|
429
|
+
return built;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Your app's data. The agent sees only what you give it with useAgentContext.
|
|
433
|
+
const accounts = [
|
|
434
|
+
{ account: "Chequing", amount: 3482.1 },
|
|
435
|
+
{ account: "Savings", amount: 6150 },
|
|
436
|
+
];
|
|
437
|
+
|
|
438
|
+
function Home() {
|
|
439
|
+
const { copilotkit } = useCopilotKit();
|
|
440
|
+
// Until the runtime connects, `agent` is a stand-in (`isReady` is false). The
|
|
441
|
+
// registered agent replaces it, so a run started on the stand-in is lost.
|
|
442
|
+
// Once the runtime has connected, useAgent throws if it has no `home` agent.
|
|
443
|
+
const { agent, isReady } = useAgent({ agentId: "home" });
|
|
444
|
+
const runtime = useGenerativeUIRuntime();
|
|
445
|
+
// The thread it last checked: a ref, so StrictMode's second effect run sees it.
|
|
446
|
+
const checkedThread = useRef<string | undefined>(undefined);
|
|
447
|
+
const composing = useRef(false); // a ref, so a second tap is ignored at once
|
|
448
|
+
const [checked, setChecked] = useState(false);
|
|
449
|
+
const [busy, setBusy] = useState(false);
|
|
450
|
+
const [error, setError] = useState<string>();
|
|
451
|
+
|
|
452
|
+
useAgentContext({
|
|
453
|
+
description:
|
|
454
|
+
"The user's accounts. Show only these, with these exact balances.",
|
|
455
|
+
value: accounts,
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const failed = useCallback(
|
|
459
|
+
(surfaceId: string) =>
|
|
460
|
+
!!runtime?.manager.getFailedSurface(agent.threadId, surfaceId),
|
|
461
|
+
[runtime, agent],
|
|
462
|
+
);
|
|
463
|
+
|
|
464
|
+
const compose = useCallback(async () => {
|
|
465
|
+
// A stand-in agent (not ready) would lose the run.
|
|
466
|
+
if (composing.current || !isReady) return;
|
|
467
|
+
composing.current = true;
|
|
468
|
+
setBusy(true);
|
|
469
|
+
setError(undefined);
|
|
470
|
+
try {
|
|
471
|
+
const before = agent.messages.map((message) => message.id);
|
|
472
|
+
const { threadId } = agent;
|
|
473
|
+
const shownBefore = chatSurfaces(agent.messages, failed);
|
|
474
|
+
const failure = await runTurn(copilotkit, agent, "Show my balances.");
|
|
475
|
+
const shownAfter = chatSurfaces(agent.messages, failed);
|
|
476
|
+
if (failure) {
|
|
477
|
+
// runTurn took the prompt back, so a retry sends it once.
|
|
478
|
+
setError(failure.message);
|
|
479
|
+
} else if (
|
|
480
|
+
![...shownAfter].some(
|
|
481
|
+
([id, content]) => shownBefore.get(id) !== content,
|
|
482
|
+
)
|
|
483
|
+
) {
|
|
484
|
+
// This run rendered or updated nothing here. Take its prompt back too.
|
|
485
|
+
takeBack(agent, before, threadId);
|
|
486
|
+
setError("The agent rendered nothing for this screen.");
|
|
487
|
+
}
|
|
488
|
+
} catch (reason) {
|
|
489
|
+
setError(reason instanceof Error ? reason.message : String(reason));
|
|
490
|
+
} finally {
|
|
491
|
+
composing.current = false;
|
|
492
|
+
setBusy(false);
|
|
493
|
+
}
|
|
494
|
+
}, [agent, copilotkit, failed, isReady]);
|
|
495
|
+
|
|
496
|
+
// The agent and its messages live in the core and outlive this screen. Run only
|
|
497
|
+
// when the registered agent has no surface here yet and is not running, so a
|
|
498
|
+
// screen that mounts again shows what it has instead of composing it again.
|
|
499
|
+
// Check again when the agent moves to another thread, once it is not running
|
|
500
|
+
// and no compose is under way (`busy` reruns this when one ends).
|
|
501
|
+
useEffect(() => {
|
|
502
|
+
if (
|
|
503
|
+
!isReady ||
|
|
504
|
+
busy ||
|
|
505
|
+
composing.current ||
|
|
506
|
+
agent.isRunning ||
|
|
507
|
+
checkedThread.current === agent.threadId
|
|
508
|
+
) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
checkedThread.current = agent.threadId;
|
|
512
|
+
setChecked(true);
|
|
513
|
+
setError(undefined);
|
|
514
|
+
if (chatSurfaces(agent.messages, failed).size === 0) {
|
|
515
|
+
void compose();
|
|
516
|
+
}
|
|
517
|
+
}, [isReady, busy, agent, agent.threadId, agent.isRunning, compose, failed]);
|
|
518
|
+
|
|
519
|
+
const surfaces = agent.messages.filter(isChatSurface);
|
|
520
|
+
const built = chatSurfaces(agent.messages, failed);
|
|
521
|
+
const unreachable =
|
|
522
|
+
copilotkit.runtimeConnectionStatus ===
|
|
523
|
+
CopilotKitCoreRuntimeConnectionStatus.Error;
|
|
524
|
+
const waiting = !isReady || !checked || busy || agent.isRunning;
|
|
525
|
+
|
|
526
|
+
return (
|
|
527
|
+
<ScrollView contentContainerStyle={{ padding: 16 }}>
|
|
528
|
+
{surfaces.map((message) => (
|
|
529
|
+
<GenerativeUIActivity
|
|
530
|
+
key={message.id}
|
|
531
|
+
message={message}
|
|
532
|
+
agent={agent}
|
|
533
|
+
/>
|
|
534
|
+
))}
|
|
535
|
+
{unreachable ? (
|
|
536
|
+
<Text accessibilityRole="alert">
|
|
537
|
+
Can't reach the agent. Check the runtime URL.
|
|
538
|
+
</Text>
|
|
539
|
+
) : waiting ? (
|
|
540
|
+
<ActivityIndicator />
|
|
541
|
+
) : error || built.size === 0 ? (
|
|
542
|
+
<Pressable accessibilityRole="button" onPress={() => void compose()}>
|
|
543
|
+
<Text>
|
|
544
|
+
Couldn't load your balances{error ? ` (${error})` : ""}. Tap to try
|
|
545
|
+
again.
|
|
546
|
+
</Text>
|
|
547
|
+
</Pressable>
|
|
548
|
+
) : null}
|
|
549
|
+
</ScrollView>
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export default function App() {
|
|
554
|
+
return (
|
|
555
|
+
<CopilotKitProvider runtimeUrl="https://your-server/api/copilotkit">
|
|
556
|
+
<GenerativeUIProvider ui={ui}>
|
|
557
|
+
<Home />
|
|
558
|
+
</GenerativeUIProvider>
|
|
559
|
+
</CopilotKitProvider>
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
`GenerativeUIActivity` renders the surface of one activity message, in your own
|
|
565
|
+
message list or on a screen with no chat. Like the web chat, it shows only
|
|
566
|
+
surfaces aimed at the chat; pass `target` to show another target. It renders
|
|
567
|
+
nothing (and warns in development) without a `GenerativeUIProvider`, under a
|
|
568
|
+
provider without `ui`, or while the agent has no `threadId`. The example shows
|
|
569
|
+
a spinner, an unreachable runtime and a retry with the reason. A failed run's
|
|
570
|
+
prompt is taken back, but the surfaces it rendered stay: the provider and the
|
|
571
|
+
model keep them. The docs add a chat list.
|
|
572
|
+
`GenerativeUISurface` marks a named place in the app; `style` applies while its
|
|
573
|
+
fallback shows too. Tool transport only; there is no standard catalog, no
|
|
574
|
+
`theme` prop (a `theme` passed from JavaScript is dropped with a development
|
|
575
|
+
warning) and no `openUrl` (it fails with "openUrl is not available on this
|
|
576
|
+
platform"), and the prebuilt `CopilotChat` does not draw surfaces yet. See the
|
|
577
|
+
[React Native docs](https://docs.copilotkit.ai/react-native#generative-ui-with-your-own-components).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { r as MarkdownStyle } from "./Markdown-UXZdDjzs.cjs";
|
|
2
|
+
import React, { ReactNode } from "react";
|
|
3
|
+
import { ɵCopilotKitProviderBaseOptions } from "@copilotkit/core";
|
|
4
|
+
|
|
5
|
+
//#region src/components/MarkdownRendererContext.d.ts
|
|
6
|
+
interface NativeMarkdownRendererProps {
|
|
7
|
+
content: string;
|
|
8
|
+
isStreaming?: boolean;
|
|
9
|
+
}
|
|
10
|
+
type NativeMarkdownRenderer = React.ComponentType<NativeMarkdownRendererProps>;
|
|
11
|
+
/** Config for the built-in RN renderer (pass instead of a component). */
|
|
12
|
+
interface DefaultMarkdownRendererProps {
|
|
13
|
+
style?: MarkdownStyle;
|
|
14
|
+
animate?: boolean;
|
|
15
|
+
}
|
|
16
|
+
type MarkdownRendererValue = NativeMarkdownRenderer | DefaultMarkdownRendererProps;
|
|
17
|
+
declare const MarkdownRendererProvider: React.FC<{
|
|
18
|
+
renderer?: MarkdownRendererValue;
|
|
19
|
+
children: React.ReactNode;
|
|
20
|
+
}>;
|
|
21
|
+
declare function useMarkdownRenderer(): MarkdownRendererValue | undefined;
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/CopilotKitProvider.d.ts
|
|
24
|
+
interface CopilotKitNativeProviderProps extends Pick<ɵCopilotKitProviderBaseOptions, "credentials" | "messageFilter" | "useSingleEndpoint" | "properties" | "onError" | "debug" | "defaultThrottleMs"> {
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
/** URL of the CopilotKit runtime endpoint */
|
|
27
|
+
runtimeUrl: string;
|
|
28
|
+
/** Custom headers sent with every request */
|
|
29
|
+
headers?: Record<string, string> | (() => Record<string, string>);
|
|
30
|
+
/**
|
|
31
|
+
* Custom markdown renderer for assistant messages. Either a component (escape
|
|
32
|
+
* hatch — receives `content` and an optional `isStreaming` flag) used instead
|
|
33
|
+
* of the built-in renderer, or a `{ style, animate }` config object to
|
|
34
|
+
* configure the built-in React Native renderer (the `CopilotMarkdown` used by
|
|
35
|
+
* the default `AssistantMessage`) without writing a wrapper.
|
|
36
|
+
*/
|
|
37
|
+
markdownRenderer?: MarkdownRendererValue;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* CopilotKit provider for React Native.
|
|
41
|
+
*
|
|
42
|
+
* A lightweight alternative to the web CopilotKitProvider that avoids
|
|
43
|
+
* web-only dependencies (DOM, CSS, Radix UI, Lit, etc).
|
|
44
|
+
*
|
|
45
|
+
* Polyfills are auto-imported when `@copilotkit/react-native` is loaded,
|
|
46
|
+
* so a separate `import "@copilotkit/react-native/polyfills"` is no longer
|
|
47
|
+
* required (though it remains available for advanced use).
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* ```tsx
|
|
51
|
+
* import { CopilotKitProvider } from "@copilotkit/react-native";
|
|
52
|
+
*
|
|
53
|
+
* function App() {
|
|
54
|
+
* return (
|
|
55
|
+
* <CopilotKitProvider runtimeUrl="https://your-runtime/api/copilotkit">
|
|
56
|
+
* <ChatScreen />
|
|
57
|
+
* </CopilotKitProvider>
|
|
58
|
+
* );
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare const CopilotKitProvider: React.FC<CopilotKitNativeProviderProps>;
|
|
63
|
+
/**
|
|
64
|
+
* Alias of {@link CopilotKitProvider}: the same component under its v1 name,
|
|
65
|
+
* so `<CopilotKit>` keeps working. `CopilotKitProvider` is the preferred name.
|
|
66
|
+
*
|
|
67
|
+
* @alias
|
|
68
|
+
*/
|
|
69
|
+
declare const CopilotKit: React.FC<CopilotKitNativeProviderProps>;
|
|
70
|
+
/**
|
|
71
|
+
* Alias of {@link CopilotKitNativeProviderProps}, the props of
|
|
72
|
+
* {@link CopilotKit}. Prefer `CopilotKitProviderProps` in new code.
|
|
73
|
+
*/
|
|
74
|
+
type CopilotKitProps = CopilotKitNativeProviderProps;
|
|
75
|
+
//#endregion
|
|
76
|
+
export { MarkdownRendererProvider as a, useMarkdownRenderer as c, CopilotKitProvider as i, CopilotKitNativeProviderProps as n, NativeMarkdownRenderer as o, CopilotKitProps as r, NativeMarkdownRendererProps as s, CopilotKit as t };
|
|
77
|
+
//# sourceMappingURL=CopilotKitProvider-CBVQLdzn.d.cts.map
|