@copilotkit/react-native 2.0.0-rc.5 → 2.0.0-rc.51
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
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useContext,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
9
|
+
import type { ComponentType, ReactNode } from "react";
|
|
10
|
+
import { StyleSheet, Text, View } from "react-native";
|
|
11
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
12
|
+
import { allowedComponents } from "@copilotkit/core/generative-ui";
|
|
13
|
+
import {
|
|
14
|
+
SurfaceView,
|
|
15
|
+
useGenerativeUIRejected,
|
|
16
|
+
useGenerativeUIRuntime,
|
|
17
|
+
useManagerVersion,
|
|
18
|
+
} from "@copilotkit/react-core/generative-ui/native";
|
|
19
|
+
import type {
|
|
20
|
+
GenerativeUI,
|
|
21
|
+
GenerativeUIComponentName,
|
|
22
|
+
} from "@copilotkit/react-core/generative-ui/native";
|
|
23
|
+
import { GenerativeUIProviderPresentContext } from "./GenerativeUIProvider";
|
|
24
|
+
import type { GenerativeUIActivityWrapperProps } from "./GenerativeUIActivity";
|
|
25
|
+
import { useDevelopmentWarnings } from "./internal";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The default wrapper of a named-target surface: the surface, and an alert
|
|
29
|
+
* under it when operations were rejected. It draws no view of its own, so
|
|
30
|
+
* the surface lays out in the view around it (`style`).
|
|
31
|
+
*/
|
|
32
|
+
function DefaultGenerativeUISurfaceWrapper({
|
|
33
|
+
error,
|
|
34
|
+
children,
|
|
35
|
+
}: GenerativeUIActivityWrapperProps) {
|
|
36
|
+
return (
|
|
37
|
+
<>
|
|
38
|
+
{children}
|
|
39
|
+
{error ? (
|
|
40
|
+
<Text accessibilityRole="alert" style={styles.error}>
|
|
41
|
+
Part of this UI couldn't be shown.
|
|
42
|
+
</Text>
|
|
43
|
+
) : null}
|
|
44
|
+
</>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type SurfaceManager = NonNullable<
|
|
49
|
+
ReturnType<typeof useGenerativeUIRuntime>
|
|
50
|
+
>["manager"];
|
|
51
|
+
|
|
52
|
+
/** What the start detection remembers of the surface it saw last. */
|
|
53
|
+
interface SeenSurface {
|
|
54
|
+
key: string;
|
|
55
|
+
status: string;
|
|
56
|
+
serverRejected: number;
|
|
57
|
+
clientRejected: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type GenerativeUISurfaceProps<
|
|
61
|
+
UI extends GenerativeUI<any> = GenerativeUI<any>,
|
|
62
|
+
> = {
|
|
63
|
+
/** Target id the agent renders into. */
|
|
64
|
+
id: string;
|
|
65
|
+
/**
|
|
66
|
+
* Tells the model where this surface is and what to show in it, for
|
|
67
|
+
* example "The home screen, under the greeting."
|
|
68
|
+
*/
|
|
69
|
+
description?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Shown until a surface targets this id, and while the provider has no
|
|
72
|
+
* `ui`.
|
|
73
|
+
*/
|
|
74
|
+
fallback?: ReactNode;
|
|
75
|
+
/**
|
|
76
|
+
* The style of the view around the surface. It applies while the fallback
|
|
77
|
+
* shows, too.
|
|
78
|
+
*/
|
|
79
|
+
style?: StyleProp<ViewStyle>;
|
|
80
|
+
/**
|
|
81
|
+
* Called once each time the agent starts to render into this surface: for
|
|
82
|
+
* each `render_ui` call, on a new surface or as an update, also when React
|
|
83
|
+
* batches several starts into one render. Also called when the surface
|
|
84
|
+
* mounts, its `id` changes, or the active thread changes, while the agent
|
|
85
|
+
* is rendering into it. Not called again for the same call when the
|
|
86
|
+
* component renders again, mounts under `<React.StrictMode>`, or gets a
|
|
87
|
+
* new callback or a new runtime. Use it, for example, to scroll the
|
|
88
|
+
* surface into view.
|
|
89
|
+
*
|
|
90
|
+
* A call is seen as new when the surface goes to "streaming" from another
|
|
91
|
+
* status, or stays "streaming" while its rejected operations are reset
|
|
92
|
+
* (the runtime resets them at the start of each call). When the app never
|
|
93
|
+
* sees the end of one call, for example because a reconnect delivers the
|
|
94
|
+
* next call already streaming and neither call had a rejected operation,
|
|
95
|
+
* the two calls count as one start.
|
|
96
|
+
*
|
|
97
|
+
* It runs after React commits the render. An error it throws is caught
|
|
98
|
+
* and logged with `console.error`, so the surface stays up.
|
|
99
|
+
*/
|
|
100
|
+
onGenerationStart?: () => void;
|
|
101
|
+
/**
|
|
102
|
+
* Replaces the default wrapper around the surface, as on
|
|
103
|
+
* `<GenerativeUIActivity>`. It gets the operations of the latest
|
|
104
|
+
* `render_ui` call that were rejected, by the runtime or on the device,
|
|
105
|
+
* and an `error` when there are any. The default draws the surface and,
|
|
106
|
+
* under it, "Part of this UI couldn't be shown." when some were rejected.
|
|
107
|
+
* The fallback is not wrapped. A surface whose `createSurface` was
|
|
108
|
+
* rejected never exists, so the fallback shows; render the activity
|
|
109
|
+
* message with `<GenerativeUIActivity target>` to show that error.
|
|
110
|
+
*/
|
|
111
|
+
Wrapper?: ComponentType<GenerativeUIActivityWrapperProps>;
|
|
112
|
+
} & (
|
|
113
|
+
| {
|
|
114
|
+
/** The UI of the provider. It types the names in `components`. */
|
|
115
|
+
ui: UI;
|
|
116
|
+
/**
|
|
117
|
+
* The only components that the agent can use in this surface. Left
|
|
118
|
+
* out, every component of the UI is allowed.
|
|
119
|
+
*/
|
|
120
|
+
components?: readonly GenerativeUIComponentName<UI>[];
|
|
121
|
+
}
|
|
122
|
+
| { ui?: undefined; components?: undefined }
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Experimental. A named place in the app where the agent can render UI. The
|
|
127
|
+
* agent sees the target, with its `description`, in each run that starts
|
|
128
|
+
* while it is mounted (the target list is sent at the start of the run).
|
|
129
|
+
*
|
|
130
|
+
* A target follows the thread that is active in the provider: the thread of
|
|
131
|
+
* the last run that started on the CopilotKit core, of any agent, including
|
|
132
|
+
* agents that the provider's `agentIds` leaves out. To show the output of
|
|
133
|
+
* one particular agent run, use `<GenerativeUIActivity>` with that agent's
|
|
134
|
+
* activity message.
|
|
135
|
+
*
|
|
136
|
+
* In development it warns once for each distinct problem of this instance
|
|
137
|
+
* (no `ui` on the provider, a `ui` that is not the provider's, unknown
|
|
138
|
+
* component names), and again for any of them after the provider's catalog
|
|
139
|
+
* changes.
|
|
140
|
+
*
|
|
141
|
+
* It throws when there is no `<GenerativeUIProvider>` above it. Under a
|
|
142
|
+
* provider with no `ui`, it shows its fallback (in development it warns).
|
|
143
|
+
* The view around it, with `style` and the test ID
|
|
144
|
+
* `generative-ui-surface-<id>`, is there in both states.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```tsx
|
|
148
|
+
* <GenerativeUISurface
|
|
149
|
+
* id="insights"
|
|
150
|
+
* ui={bankUI}
|
|
151
|
+
* description="Under the balances. Show spending insights here."
|
|
152
|
+
* components={["SpendingCard", "InsightCard"]}
|
|
153
|
+
* fallback={<InsightsPlaceholder />}
|
|
154
|
+
* />
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export function GenerativeUISurface<
|
|
158
|
+
UI extends GenerativeUI<any> = GenerativeUI<any>,
|
|
159
|
+
>({
|
|
160
|
+
id,
|
|
161
|
+
description,
|
|
162
|
+
ui,
|
|
163
|
+
components,
|
|
164
|
+
fallback = null,
|
|
165
|
+
style,
|
|
166
|
+
onGenerationStart,
|
|
167
|
+
Wrapper = DefaultGenerativeUISurfaceWrapper,
|
|
168
|
+
}: GenerativeUISurfaceProps<UI>) {
|
|
169
|
+
const runtime = useGenerativeUIRuntime();
|
|
170
|
+
const hasProvider = useContext(GenerativeUIProviderPresentContext);
|
|
171
|
+
if (!runtime && !hasProvider) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`<GenerativeUISurface id="${id}"> must be rendered inside a <GenerativeUIProvider> from @copilotkit/react-native/generative-ui.`,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
useManagerVersion(runtime);
|
|
177
|
+
const otherUI = runtime !== null && ui !== undefined && ui !== runtime.ui;
|
|
178
|
+
// A new array with the same names must not register the target again.
|
|
179
|
+
const componentsKey =
|
|
180
|
+
components === undefined ? undefined : JSON.stringify(components);
|
|
181
|
+
const catalog = runtime?.ui;
|
|
182
|
+
const allowed = useMemo(() => {
|
|
183
|
+
if (!catalog || componentsKey === undefined) return undefined;
|
|
184
|
+
return allowedComponents(JSON.parse(componentsKey) as string[], catalog);
|
|
185
|
+
}, [catalog, componentsKey]);
|
|
186
|
+
useDevelopmentWarnings(
|
|
187
|
+
[
|
|
188
|
+
runtime
|
|
189
|
+
? undefined
|
|
190
|
+
: `[generativeUI] <GenerativeUISurface id="${id}"> shows its fallback: the <GenerativeUIProvider> above it has no \`ui\`.`,
|
|
191
|
+
otherUI
|
|
192
|
+
? `[generativeUI] <GenerativeUISurface id="${id}">: the ui prop is not the UI of the provider. The components are checked against the UI of the provider.`
|
|
193
|
+
: undefined,
|
|
194
|
+
allowed && allowed.warning !== ""
|
|
195
|
+
? `[generativeUI] <GenerativeUISurface id="${id}">: ${allowed.warning}.`
|
|
196
|
+
: undefined,
|
|
197
|
+
],
|
|
198
|
+
catalog,
|
|
199
|
+
);
|
|
200
|
+
const allowedNames = allowed?.names;
|
|
201
|
+
useLayoutEffect(() => {
|
|
202
|
+
if (!runtime) return;
|
|
203
|
+
return runtime.manager.registerTarget(id, {
|
|
204
|
+
description,
|
|
205
|
+
components: allowedNames,
|
|
206
|
+
});
|
|
207
|
+
// `allowedNames` changes only with the catalog or the names.
|
|
208
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
209
|
+
}, [runtime, id, description, componentsKey]);
|
|
210
|
+
const surface = runtime?.manager.getSurfaceForTarget(id);
|
|
211
|
+
const { rejected, error } = useGenerativeUIRejected(
|
|
212
|
+
surface?.serverRejected,
|
|
213
|
+
surface?.clientRejected,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
// Starts are detected from the manager's notifications, which come at
|
|
217
|
+
// most once per ingest (only when it changes something), not from
|
|
218
|
+
// renders: React can batch the "ready" of one call and the "streaming" of
|
|
219
|
+
// the next into one render. What was seen last is kept in a ref, so an
|
|
220
|
+
// effect that runs again (StrictMode, a new runtime) does not see the
|
|
221
|
+
// same call as a new start. It is reset only when `id` changes. A new
|
|
222
|
+
// runtime has a new, empty manager that the provider fills again from the
|
|
223
|
+
// messages: "no surface" counts only from the manager that showed the
|
|
224
|
+
// last one, so the rebuilt surface is not a new start either.
|
|
225
|
+
const seen = useRef<{
|
|
226
|
+
id: string;
|
|
227
|
+
manager?: SurfaceManager;
|
|
228
|
+
surface?: SeenSurface;
|
|
229
|
+
}>({ id });
|
|
230
|
+
const pendingStarts = useRef(0);
|
|
231
|
+
const [, setStartTick] = useState(0);
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
if (!runtime) return;
|
|
234
|
+
const { manager } = runtime;
|
|
235
|
+
if (seen.current.id !== id) seen.current = { id };
|
|
236
|
+
const check = () => {
|
|
237
|
+
const current = manager.getSurfaceForTarget(id);
|
|
238
|
+
const last = seen.current.surface;
|
|
239
|
+
if (!current) {
|
|
240
|
+
if (seen.current.manager === manager) seen.current = { id, manager };
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const next: SeenSurface = {
|
|
244
|
+
key: current.key,
|
|
245
|
+
status: current.status,
|
|
246
|
+
serverRejected: current.serverRejected.length,
|
|
247
|
+
clientRejected: current.clientRejected.length,
|
|
248
|
+
};
|
|
249
|
+
seen.current = { id, manager, surface: next };
|
|
250
|
+
if (next.status !== "streaming") return;
|
|
251
|
+
const isStart =
|
|
252
|
+
last === undefined ||
|
|
253
|
+
last.key !== next.key ||
|
|
254
|
+
last.status !== "streaming" ||
|
|
255
|
+
next.serverRejected < last.serverRejected ||
|
|
256
|
+
next.clientRejected < last.clientRejected;
|
|
257
|
+
if (isStart) {
|
|
258
|
+
pendingStarts.current += 1;
|
|
259
|
+
setStartTick((n) => n + 1);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
check();
|
|
263
|
+
return manager.subscribe(check);
|
|
264
|
+
}, [runtime, id]);
|
|
265
|
+
const onStart = useRef(onGenerationStart);
|
|
266
|
+
onStart.current = onGenerationStart;
|
|
267
|
+
// After every commit: calls the callback once for each start seen since.
|
|
268
|
+
useEffect(() => {
|
|
269
|
+
while (pendingStarts.current > 0) {
|
|
270
|
+
pendingStarts.current -= 1;
|
|
271
|
+
const callback = onStart.current;
|
|
272
|
+
if (!callback) continue;
|
|
273
|
+
try {
|
|
274
|
+
callback();
|
|
275
|
+
} catch (callbackError) {
|
|
276
|
+
console.error(
|
|
277
|
+
`[generativeUI] <GenerativeUISurface id="${id}"> onGenerationStart threw:`,
|
|
278
|
+
callbackError,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
return (
|
|
284
|
+
<View style={style} testID={`generative-ui-surface-${id}`}>
|
|
285
|
+
{surface ? (
|
|
286
|
+
<Wrapper
|
|
287
|
+
surfaceId={surface.surfaceId}
|
|
288
|
+
isStreaming={surface.status === "streaming"}
|
|
289
|
+
error={error}
|
|
290
|
+
rejected={rejected}
|
|
291
|
+
>
|
|
292
|
+
<SurfaceView surface={surface} />
|
|
293
|
+
</Wrapper>
|
|
294
|
+
) : (
|
|
295
|
+
fallback
|
|
296
|
+
)}
|
|
297
|
+
</View>
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const styles = StyleSheet.create({
|
|
302
|
+
error: { marginTop: 8, fontSize: 12, color: "#dc2626" },
|
|
303
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from "react-native";
|
|
3
|
+
import type {
|
|
4
|
+
GenerativeUIAccessibilityWrapperProps,
|
|
5
|
+
GenerativeUIHost,
|
|
6
|
+
GenerativeUINodeFallbackProps,
|
|
7
|
+
GenerativeUISurfaceRootProps,
|
|
8
|
+
} from "@copilotkit/react-core/generative-ui/native";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A surface needs no box of its own on React Native: the catalog's
|
|
12
|
+
* components lay themselves out. The web theme is a stylesheet of CSS
|
|
13
|
+
* variables, which React Native has no use for. `GenerativeUIProvider` takes
|
|
14
|
+
* no `theme` and drops one that an untyped caller passes, so the `theme` of
|
|
15
|
+
* these props is always undefined, in a custom host too; style the
|
|
16
|
+
* components of your catalog instead.
|
|
17
|
+
*/
|
|
18
|
+
function NativeSurfaceRoot({ children }: GenerativeUISurfaceRootProps) {
|
|
19
|
+
return <>{children}</>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** aria-live to React Native's `accessibilityLiveRegion`. */
|
|
23
|
+
const LIVE_REGION = {
|
|
24
|
+
off: "none",
|
|
25
|
+
polite: "polite",
|
|
26
|
+
assertive: "assertive",
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Carries the envelope's hidden state and live region to the platform (not
|
|
31
|
+
* its label or description: see below).
|
|
32
|
+
*
|
|
33
|
+
* It is a real native view, on every React Native version the package
|
|
34
|
+
* supports. `display: contents` would keep it out of the layout, but it
|
|
35
|
+
* exists only on React Native 0.77+ with the New Architecture, and even
|
|
36
|
+
* there a contents view is dropped from the native tree, and its
|
|
37
|
+
* accessibility props with it. `collapsable={false}` stops Fabric and the
|
|
38
|
+
* Android view flattener from removing it as a layout-only view for the
|
|
39
|
+
* same reason.
|
|
40
|
+
*
|
|
41
|
+
* Layout: it has no style of its own, and it cannot pass the component's
|
|
42
|
+
* sizing on, because the style of the component's root view is not one of
|
|
43
|
+
* the props it gets. The wrapper, not the component, is the flex item of the
|
|
44
|
+
* parent. A default view is a column that stretches its child across, so
|
|
45
|
+
* the component keeps the parent's width (in a column) and its own height.
|
|
46
|
+
* But a component sized against its parent's main axis loses that size: in
|
|
47
|
+
* a column, `flex: 1`, `flexGrow` or a percentage `height` resolves against
|
|
48
|
+
* the wrapper, which is only as tall as its content, so the component no
|
|
49
|
+
* longer fills the parent; in a row, the same holds for `flex`, `flexGrow`,
|
|
50
|
+
* a percentage `width` and `alignSelf`. A component that must fill its
|
|
51
|
+
* parent applies the envelope itself: give its definition
|
|
52
|
+
* `accessibility: "self"` and spread `useGenerativeUIAccessibility()` on its
|
|
53
|
+
* root view (React Native 0.71+ reads `aria-hidden` and `aria-live`), and
|
|
54
|
+
* the engine draws no wrapper around it. A custom host can also replace
|
|
55
|
+
* this wrapper.
|
|
56
|
+
*
|
|
57
|
+
* Hidden: `accessibilityElementsHidden` hides the subtree from VoiceOver,
|
|
58
|
+
* `importantForAccessibility="no-hide-descendants"` from TalkBack, and
|
|
59
|
+
* `aria-hidden` covers both on React Native 0.71+.
|
|
60
|
+
*
|
|
61
|
+
* Live region: `accessibilityLiveRegion` is Android-only. iOS has no
|
|
62
|
+
* live-region prop, so VoiceOver does not announce updates to the
|
|
63
|
+
* component by itself; a component that must be heard on iOS announces
|
|
64
|
+
* its change with `AccessibilityInfo.announceForAccessibility`.
|
|
65
|
+
*
|
|
66
|
+
* Label and description: the envelope's `aria-label` and
|
|
67
|
+
* `aria-description` are not applied. A label on this wrapper would name
|
|
68
|
+
* nothing on iOS, where a view that is not `accessible` is not an element,
|
|
69
|
+
* and making the wrapper `accessible` would merge the component and every
|
|
70
|
+
* control in it into one element. React Native also has no
|
|
71
|
+
* `aria-description` (its nearest prop is `accessibilityHint`). A component
|
|
72
|
+
* that needs a name or a hint reads them with `accessibility: "self"` and
|
|
73
|
+
* `useGenerativeUIAccessibility()`, and sets `accessibilityLabel` /
|
|
74
|
+
* `aria-label` (React Native 0.71+) and `accessibilityHint` on its own
|
|
75
|
+
* root view. Spread only the props React Native knows: it ignores
|
|
76
|
+
* `aria-description`.
|
|
77
|
+
*/
|
|
78
|
+
function NativeAccessibilityWrapper({
|
|
79
|
+
attributes,
|
|
80
|
+
children,
|
|
81
|
+
}: GenerativeUIAccessibilityWrapperProps) {
|
|
82
|
+
const hidden = attributes["aria-hidden"] === true;
|
|
83
|
+
const live = attributes["aria-live"];
|
|
84
|
+
return (
|
|
85
|
+
<View
|
|
86
|
+
collapsable={false}
|
|
87
|
+
aria-hidden={hidden || undefined}
|
|
88
|
+
accessibilityElementsHidden={hidden || undefined}
|
|
89
|
+
importantForAccessibility={hidden ? "no-hide-descendants" : undefined}
|
|
90
|
+
accessibilityLiveRegion={live ? LIVE_REGION[live] : undefined}
|
|
91
|
+
>
|
|
92
|
+
{children}
|
|
93
|
+
</View>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function NativeNodeFallback({
|
|
98
|
+
message,
|
|
99
|
+
componentId,
|
|
100
|
+
retry,
|
|
101
|
+
}: GenerativeUINodeFallbackProps) {
|
|
102
|
+
return (
|
|
103
|
+
<View
|
|
104
|
+
accessibilityRole="alert"
|
|
105
|
+
testID={`generative-ui-error-${componentId}`}
|
|
106
|
+
style={styles.fallback}
|
|
107
|
+
>
|
|
108
|
+
<Text style={styles.fallbackText}>
|
|
109
|
+
Couldn’t display this part: {message}
|
|
110
|
+
</Text>
|
|
111
|
+
{retry ? (
|
|
112
|
+
<Pressable accessibilityRole="button" onPress={retry}>
|
|
113
|
+
<Text style={[styles.fallbackText, styles.retry]}>Retry</Text>
|
|
114
|
+
</Pressable>
|
|
115
|
+
) : null}
|
|
116
|
+
</View>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The host elements of a surface on React Native. */
|
|
121
|
+
export const nativeGenerativeUIHost: GenerativeUIHost = {
|
|
122
|
+
SurfaceRoot: NativeSurfaceRoot,
|
|
123
|
+
AccessibilityWrapper: NativeAccessibilityWrapper,
|
|
124
|
+
NodeFallback: NativeNodeFallback,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const styles = StyleSheet.create({
|
|
128
|
+
fallback: {
|
|
129
|
+
borderWidth: 1,
|
|
130
|
+
borderColor: "#fca5a5",
|
|
131
|
+
backgroundColor: "#fef2f2",
|
|
132
|
+
borderRadius: 8,
|
|
133
|
+
paddingVertical: 8,
|
|
134
|
+
paddingHorizontal: 12,
|
|
135
|
+
},
|
|
136
|
+
fallbackText: { color: "#991b1b", fontSize: 13 },
|
|
137
|
+
retry: { marginTop: 4, textDecorationLine: "underline" },
|
|
138
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copilotkit/react-native/generative-ui
|
|
3
|
+
*
|
|
4
|
+
* Experimental. Generative UI on React Native: the agent renders the
|
|
5
|
+
* components of your catalog (`createGenerativeUI`) with the `render_ui`
|
|
6
|
+
* tool, and the runtime validates each operation before it streams it to the
|
|
7
|
+
* app. The same catalog API as `@copilotkit/react-core/generative-ui`,
|
|
8
|
+
* with React Native host elements and without the DOM standard catalog.
|
|
9
|
+
*
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import {
|
|
12
|
+
* GenerativeUIActivity,
|
|
13
|
+
* GenerativeUIProvider,
|
|
14
|
+
* createGenerativeUI,
|
|
15
|
+
* } from "@copilotkit/react-native/generative-ui";
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Supported: the tool transport, custom catalogs, state bindings, functions,
|
|
19
|
+
* events, `<GenerativeUIActivity>` and `<GenerativeUISurface>`. Not yet: the
|
|
20
|
+
* markdown transport, the standard catalog and surfaces inside the prebuilt
|
|
21
|
+
* `CopilotChat`. There is no `theme` option (React Native has no CSS
|
|
22
|
+
* variables): `GenerativeUIToolConfig` here is the web config without it.
|
|
23
|
+
*
|
|
24
|
+
* `child()` and `childList()` return zod 4 schemas, so write a catalog that
|
|
25
|
+
* uses them with zod 4.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
// Auto-install polyfills before anything else loads: an app can import this
|
|
29
|
+
// entry before the root one, and @copilotkit/core and @ag-ui/client fail at
|
|
30
|
+
// load time on Hermes without them (scripts/verify-polyfill-barrel.mjs checks
|
|
31
|
+
// that this stays the built entry's first statement).
|
|
32
|
+
import "../polyfills";
|
|
33
|
+
|
|
34
|
+
// The catalog API.
|
|
35
|
+
export {
|
|
36
|
+
createGenerativeUI,
|
|
37
|
+
child,
|
|
38
|
+
childList,
|
|
39
|
+
exportA2UICatalog,
|
|
40
|
+
GenerativeUIError,
|
|
41
|
+
useGenerativeUIAccessibility,
|
|
42
|
+
useGenerativeUIFunction,
|
|
43
|
+
useGenerativeUIRuntime,
|
|
44
|
+
useGenerativeUIState,
|
|
45
|
+
} from "@copilotkit/react-core/generative-ui/native";
|
|
46
|
+
export type {
|
|
47
|
+
CreateGenerativeUIConfig,
|
|
48
|
+
ExportA2UICatalogOptions,
|
|
49
|
+
GenerativeUI,
|
|
50
|
+
GenerativeUIAccessibility,
|
|
51
|
+
GenerativeUIAriaAttributes,
|
|
52
|
+
GenerativeUIComponentDefinition,
|
|
53
|
+
GenerativeUIComponentName,
|
|
54
|
+
GenerativeUIComponentProps,
|
|
55
|
+
GenerativeUIErrorBoundaryConfig,
|
|
56
|
+
GenerativeUIErrorCode,
|
|
57
|
+
GenerativeUIErrorInfo,
|
|
58
|
+
GenerativeUIEventDefinition,
|
|
59
|
+
GenerativeUIEventHandler,
|
|
60
|
+
GenerativeUIFunctionArgs,
|
|
61
|
+
GenerativeUIFunctionDefinition,
|
|
62
|
+
GenerativeUIFunctionInput,
|
|
63
|
+
GenerativeUIFunctionName,
|
|
64
|
+
GenerativeUIAccessibilityWrapperProps,
|
|
65
|
+
GenerativeUIHost,
|
|
66
|
+
GenerativeUIInvokeResult,
|
|
67
|
+
GenerativeUINodeFallbackProps,
|
|
68
|
+
GenerativeUIRuntime,
|
|
69
|
+
GenerativeUISlotKind,
|
|
70
|
+
GenerativeUIStateOf,
|
|
71
|
+
GenerativeUISurfaceRootProps,
|
|
72
|
+
} from "@copilotkit/react-core/generative-ui/native";
|
|
73
|
+
export type { GenerativeUIRejectedOperation } from "@copilotkit/shared";
|
|
74
|
+
|
|
75
|
+
// React Native components.
|
|
76
|
+
export { GenerativeUIProvider } from "./GenerativeUIProvider";
|
|
77
|
+
export type {
|
|
78
|
+
GenerativeUIProviderProps,
|
|
79
|
+
GenerativeUIToolConfig,
|
|
80
|
+
} from "./GenerativeUIProvider";
|
|
81
|
+
export {
|
|
82
|
+
DefaultGenerativeUIActivityWrapper,
|
|
83
|
+
GenerativeUIActivity,
|
|
84
|
+
isGenerativeUIActivity,
|
|
85
|
+
} from "./GenerativeUIActivity";
|
|
86
|
+
export type {
|
|
87
|
+
GenerativeUIActivityMessage,
|
|
88
|
+
GenerativeUIActivityProps,
|
|
89
|
+
GenerativeUIActivityWrapperProps,
|
|
90
|
+
} from "./GenerativeUIActivity";
|
|
91
|
+
export { GenerativeUISurface } from "./GenerativeUISurface";
|
|
92
|
+
export type { GenerativeUISurfaceProps } from "./GenerativeUISurface";
|
|
93
|
+
export { nativeGenerativeUIHost, NativeNodeFallback } from "./host";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers shared by the React Native generative UI components. Not part of
|
|
3
|
+
* the public entry.
|
|
4
|
+
*/
|
|
5
|
+
import { useEffect, useRef } from "react";
|
|
6
|
+
|
|
7
|
+
export const isDevelopment = () => process.env.NODE_ENV !== "production";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Warns in development, once for each distinct message of this component
|
|
11
|
+
* instance: a message that stays the same across renders warns once, and so
|
|
12
|
+
* does one that goes away and comes back (A, B, A warns A once and B once).
|
|
13
|
+
* When `resetKey` changes (a new input, such as another activity message or
|
|
14
|
+
* another catalog), every message can warn once again. Each entry of
|
|
15
|
+
* `messages` is checked on its own; undefined entries are skipped.
|
|
16
|
+
*/
|
|
17
|
+
export function useDevelopmentWarnings(
|
|
18
|
+
messages: ReadonlyArray<string | undefined>,
|
|
19
|
+
resetKey?: unknown,
|
|
20
|
+
) {
|
|
21
|
+
const warned = useRef<{ resetKey: unknown; messages: Set<string> }>({
|
|
22
|
+
resetKey,
|
|
23
|
+
messages: new Set(),
|
|
24
|
+
});
|
|
25
|
+
const key = messages.join("\u0000");
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!isDevelopment()) return;
|
|
28
|
+
if (!Object.is(warned.current.resetKey, resetKey)) {
|
|
29
|
+
warned.current = { resetKey, messages: new Set() };
|
|
30
|
+
}
|
|
31
|
+
for (const message of messages) {
|
|
32
|
+
if (message === undefined || warned.current.messages.has(message)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
warned.current.messages.add(message);
|
|
36
|
+
console.warn(message);
|
|
37
|
+
}
|
|
38
|
+
// `key` stands for `messages`, which is a new array on every render.
|
|
39
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
40
|
+
}, [key, resetKey]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A value for a warning, as JSON when it has a JSON form (a string comes out
|
|
45
|
+
* quoted), so an object does not print as "[object Object]".
|
|
46
|
+
*/
|
|
47
|
+
export function describeValue(value: unknown): string {
|
|
48
|
+
try {
|
|
49
|
+
const json = JSON.stringify(value);
|
|
50
|
+
if (json !== undefined) return json;
|
|
51
|
+
} catch {
|
|
52
|
+
// A cycle or a BigInt: fall through to String().
|
|
53
|
+
}
|
|
54
|
+
return String(value);
|
|
55
|
+
}
|
package/src/headless.ts
CHANGED
|
@@ -39,6 +39,10 @@ export type { CopilotKitNativeProviderProps } from "./CopilotKitProvider";
|
|
|
39
39
|
// Provider props alias (mirrors web's CopilotKitProviderProps)
|
|
40
40
|
export type { CopilotKitNativeProviderProps as CopilotKitProviderProps } from "./CopilotKitProvider";
|
|
41
41
|
|
|
42
|
+
// v1 names, kept as aliases (mirrors web's `CopilotKit` / `CopilotKitProps`).
|
|
43
|
+
// `CopilotKitProvider` is the preferred name.
|
|
44
|
+
export { CopilotKit, type CopilotKitProps } from "./CopilotKitProvider";
|
|
45
|
+
|
|
42
46
|
// Re-export context and hooks from react-core (platform-agnostic)
|
|
43
47
|
export {
|
|
44
48
|
useCopilotKit,
|