@copilotkit/react-native 2.0.0-rc.37 → 2.0.0-rc.39

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.
Files changed (82) hide show
  1. package/README.md +334 -2
  2. package/USAGE.md +382 -21
  3. package/dist/{CopilotKitProvider-F7Pn5kp4.d.cts → CopilotKitProvider-Cbfrm_7u.d.mts} +2 -2
  4. package/dist/{CopilotKitProvider-DDPbJkHS.cjs → CopilotKitProvider-CvJYytjn.cjs} +4 -3
  5. package/dist/{CopilotKitProvider-DDPbJkHS.cjs.map → CopilotKitProvider-CvJYytjn.cjs.map} +1 -1
  6. package/dist/{CopilotKitProvider-Khgz5dQo.d.mts → CopilotKitProvider-Dv3ISMKf.d.cts} +2 -2
  7. package/dist/{CopilotKitProvider-BXpkAl1Y.mjs → CopilotKitProvider-oTdmylSY.mjs} +2 -2
  8. package/dist/{CopilotKitProvider-BXpkAl1Y.mjs.map → CopilotKitProvider-oTdmylSY.mjs.map} +1 -1
  9. package/dist/{Markdown-CaYHlD4j.cjs → Markdown-DLopAh5W.cjs} +3 -3
  10. package/dist/{Markdown-CaYHlD4j.cjs.map → Markdown-DLopAh5W.cjs.map} +1 -1
  11. package/dist/{Markdown-a-rdG22_.d.mts → Markdown-Du-b_Qpr.d.mts} +1 -1
  12. package/dist/{Markdown-DzjnSuu5.d.cts → Markdown-UXZdDjzs.d.cts} +1 -1
  13. package/dist/{Markdown-SE9MfY2U.mjs → Markdown-VxtPJ_yh.mjs} +1 -1
  14. package/dist/{Markdown-SE9MfY2U.mjs.map → Markdown-VxtPJ_yh.mjs.map} +1 -1
  15. package/dist/{MarkdownRendererContext-B_AKdPAN.mjs → MarkdownRendererContext-EobUsERg.mjs} +1 -1
  16. package/dist/{MarkdownRendererContext-B_AKdPAN.mjs.map → MarkdownRendererContext-EobUsERg.mjs.map} +1 -1
  17. package/dist/MarkdownRendererContext-cDEHvbw9.cjs +40 -0
  18. package/dist/{MarkdownRendererContext-BRNe808d.cjs.map → MarkdownRendererContext-cDEHvbw9.cjs.map} +1 -1
  19. package/dist/chunk-C0xms8kb.cjs +34 -0
  20. package/dist/components/index.cjs +6 -4
  21. package/dist/components/index.cjs.map +1 -1
  22. package/dist/components/index.d.cts +1 -1
  23. package/dist/components/index.d.mts +1 -1
  24. package/dist/components/index.mjs +3 -2
  25. package/dist/components/index.mjs.map +1 -1
  26. package/dist/generative-ui/index.cjs +612 -0
  27. package/dist/generative-ui/index.cjs.map +1 -0
  28. package/dist/generative-ui/index.d.cts +341 -0
  29. package/dist/generative-ui/index.d.mts +341 -0
  30. package/dist/generative-ui/index.mjs +549 -0
  31. package/dist/generative-ui/index.mjs.map +1 -0
  32. package/dist/headless.cjs +2 -3
  33. package/dist/headless.d.cts +2 -2
  34. package/dist/headless.d.mts +2 -2
  35. package/dist/headless.mjs +1 -2
  36. package/dist/index.cjs +8 -7
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +3 -3
  39. package/dist/index.d.mts +2 -2
  40. package/dist/index.mjs +4 -4
  41. package/dist/polyfills/encoding.cjs +113 -3
  42. package/dist/polyfills/encoding.cjs.map +1 -1
  43. package/dist/polyfills/encoding.d.cts +31 -1
  44. package/dist/polyfills/encoding.d.mts +31 -1
  45. package/dist/polyfills/encoding.mjs +109 -3
  46. package/dist/polyfills/encoding.mjs.map +1 -1
  47. package/dist/polyfills/streams.cjs +1 -1
  48. package/dist/polyfills.cjs +218 -3
  49. package/dist/polyfills.cjs.map +1 -1
  50. package/dist/polyfills.mjs +217 -2
  51. package/dist/polyfills.mjs.map +1 -1
  52. package/package.json +13 -5
  53. package/project.json +6 -1
  54. package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
  55. package/scripts/verify-polyfill-barrel.mjs +154 -5
  56. package/src/__tests__/generative-ui-harness.tsx +243 -0
  57. package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
  58. package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
  59. package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
  60. package/src/__tests__/generative-ui.test.tsx +1000 -0
  61. package/src/__tests__/headless-entry-surface.test.ts +979 -60
  62. package/src/components/index.ts +6 -0
  63. package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
  64. package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
  65. package/src/generative-ui/GenerativeUISurface.tsx +303 -0
  66. package/src/generative-ui/host.tsx +138 -0
  67. package/src/generative-ui/index.ts +93 -0
  68. package/src/generative-ui/internal.ts +55 -0
  69. package/src/index.ts +8 -3
  70. package/src/polyfills/__tests__/{utf8.test.ts → encoding-utf8.test.ts} +1 -1
  71. package/src/polyfills/encoding.ts +156 -1
  72. package/tsdown.config.ts +1 -0
  73. package/dist/MarkdownRendererContext-BRNe808d.cjs +0 -72
  74. package/dist/streaming-fetch-CLHG3Z1H.mjs +0 -219
  75. package/dist/streaming-fetch-CLHG3Z1H.mjs.map +0 -1
  76. package/dist/streaming-fetch-DbDALGya.cjs +0 -225
  77. package/dist/streaming-fetch-DbDALGya.cjs.map +0 -1
  78. package/dist/utf8-BIN9t4Oy.mjs +0 -113
  79. package/dist/utf8-BIN9t4Oy.mjs.map +0 -1
  80. package/dist/utf8-Cf3Ssy33.cjs +0 -125
  81. package/dist/utf8-Cf3Ssy33.cjs.map +0 -1
  82. package/src/polyfills/utf8.ts +0 -151
@@ -0,0 +1,612 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_chunk = require('../chunk-C0xms8kb.cjs');
3
+ require('../polyfills.cjs');
4
+ let react = require("react");
5
+ react = require_chunk.__toESM(react);
6
+ let _copilotkit_react_core_context = require("@copilotkit/react-core/context");
7
+ let _copilotkit_shared = require("@copilotkit/shared");
8
+ let react_jsx_runtime = require("react/jsx-runtime");
9
+ let react_native = require("react-native");
10
+ let _copilotkit_react_core_generative_ui_native = require("@copilotkit/react-core/generative-ui/native");
11
+ let _copilotkit_core_generative_ui = require("@copilotkit/core/generative-ui");
12
+
13
+ //#region src/generative-ui/host.tsx
14
+ /**
15
+ * A surface needs no box of its own on React Native: the catalog's
16
+ * components lay themselves out. The web theme is a stylesheet of CSS
17
+ * variables, which React Native has no use for. `GenerativeUIProvider` takes
18
+ * no `theme` and drops one that an untyped caller passes, so the `theme` of
19
+ * these props is always undefined, in a custom host too; style the
20
+ * components of your catalog instead.
21
+ */
22
+ function NativeSurfaceRoot({ children }) {
23
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children });
24
+ }
25
+ /** aria-live to React Native's `accessibilityLiveRegion`. */
26
+ const LIVE_REGION = {
27
+ off: "none",
28
+ polite: "polite",
29
+ assertive: "assertive"
30
+ };
31
+ /**
32
+ * Carries the envelope's hidden state and live region to the platform (not
33
+ * its label or description: see below).
34
+ *
35
+ * It is a real native view, on every React Native version the package
36
+ * supports. `display: contents` would keep it out of the layout, but it
37
+ * exists only on React Native 0.77+ with the New Architecture, and even
38
+ * there a contents view is dropped from the native tree, and its
39
+ * accessibility props with it. `collapsable={false}` stops Fabric and the
40
+ * Android view flattener from removing it as a layout-only view for the
41
+ * same reason.
42
+ *
43
+ * Layout: it has no style of its own, and it cannot pass the component's
44
+ * sizing on, because the style of the component's root view is not one of
45
+ * the props it gets. The wrapper, not the component, is the flex item of the
46
+ * parent. A default view is a column that stretches its child across, so
47
+ * the component keeps the parent's width (in a column) and its own height.
48
+ * But a component sized against its parent's main axis loses that size: in
49
+ * a column, `flex: 1`, `flexGrow` or a percentage `height` resolves against
50
+ * the wrapper, which is only as tall as its content, so the component no
51
+ * longer fills the parent; in a row, the same holds for `flex`, `flexGrow`,
52
+ * a percentage `width` and `alignSelf`. A component that must fill its
53
+ * parent applies the envelope itself: give its definition
54
+ * `accessibility: "self"` and spread `useGenerativeUIAccessibility()` on its
55
+ * root view (React Native 0.71+ reads `aria-hidden` and `aria-live`), and
56
+ * the engine draws no wrapper around it. A custom host can also replace
57
+ * this wrapper.
58
+ *
59
+ * Hidden: `accessibilityElementsHidden` hides the subtree from VoiceOver,
60
+ * `importantForAccessibility="no-hide-descendants"` from TalkBack, and
61
+ * `aria-hidden` covers both on React Native 0.71+.
62
+ *
63
+ * Live region: `accessibilityLiveRegion` is Android-only. iOS has no
64
+ * live-region prop, so VoiceOver does not announce updates to the
65
+ * component by itself; a component that must be heard on iOS announces
66
+ * its change with `AccessibilityInfo.announceForAccessibility`.
67
+ *
68
+ * Label and description: the envelope's `aria-label` and
69
+ * `aria-description` are not applied. A label on this wrapper would name
70
+ * nothing on iOS, where a view that is not `accessible` is not an element,
71
+ * and making the wrapper `accessible` would merge the component and every
72
+ * control in it into one element. React Native also has no
73
+ * `aria-description` (its nearest prop is `accessibilityHint`). A component
74
+ * that needs a name or a hint reads them with `accessibility: "self"` and
75
+ * `useGenerativeUIAccessibility()`, and sets `accessibilityLabel` /
76
+ * `aria-label` (React Native 0.71+) and `accessibilityHint` on its own
77
+ * root view. Spread only the props React Native knows: it ignores
78
+ * `aria-description`.
79
+ */
80
+ function NativeAccessibilityWrapper({ attributes, children }) {
81
+ const hidden = attributes["aria-hidden"] === true;
82
+ const live = attributes["aria-live"];
83
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.View, {
84
+ collapsable: false,
85
+ "aria-hidden": hidden || void 0,
86
+ accessibilityElementsHidden: hidden || void 0,
87
+ importantForAccessibility: hidden ? "no-hide-descendants" : void 0,
88
+ accessibilityLiveRegion: live ? LIVE_REGION[live] : void 0,
89
+ children
90
+ });
91
+ }
92
+ function NativeNodeFallback({ message, componentId, retry }) {
93
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
94
+ accessibilityRole: "alert",
95
+ testID: `generative-ui-error-${componentId}`,
96
+ style: styles$2.fallback,
97
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
98
+ style: styles$2.fallbackText,
99
+ children: ["Couldn’t display this part: ", message]
100
+ }), retry ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Pressable, {
101
+ accessibilityRole: "button",
102
+ onPress: retry,
103
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
104
+ style: [styles$2.fallbackText, styles$2.retry],
105
+ children: "Retry"
106
+ })
107
+ }) : null]
108
+ });
109
+ }
110
+ /** The host elements of a surface on React Native. */
111
+ const nativeGenerativeUIHost = {
112
+ SurfaceRoot: NativeSurfaceRoot,
113
+ AccessibilityWrapper: NativeAccessibilityWrapper,
114
+ NodeFallback: NativeNodeFallback
115
+ };
116
+ const styles$2 = react_native.StyleSheet.create({
117
+ fallback: {
118
+ borderWidth: 1,
119
+ borderColor: "#fca5a5",
120
+ backgroundColor: "#fef2f2",
121
+ borderRadius: 8,
122
+ paddingVertical: 8,
123
+ paddingHorizontal: 12
124
+ },
125
+ fallbackText: {
126
+ color: "#991b1b",
127
+ fontSize: 13
128
+ },
129
+ retry: {
130
+ marginTop: 4,
131
+ textDecorationLine: "underline"
132
+ }
133
+ });
134
+
135
+ //#endregion
136
+ //#region src/generative-ui/internal.ts
137
+ /**
138
+ * Helpers shared by the React Native generative UI components. Not part of
139
+ * the public entry.
140
+ */
141
+ const isDevelopment = () => process.env.NODE_ENV !== "production";
142
+ /**
143
+ * Warns in development, once for each distinct message of this component
144
+ * instance: a message that stays the same across renders warns once, and so
145
+ * does one that goes away and comes back (A, B, A warns A once and B once).
146
+ * When `resetKey` changes (a new input, such as another activity message or
147
+ * another catalog), every message can warn once again. Each entry of
148
+ * `messages` is checked on its own; undefined entries are skipped.
149
+ */
150
+ function useDevelopmentWarnings(messages, resetKey) {
151
+ const warned = (0, react.useRef)({
152
+ resetKey,
153
+ messages: /* @__PURE__ */ new Set()
154
+ });
155
+ (0, react.useEffect)(() => {
156
+ if (!isDevelopment()) return;
157
+ if (!Object.is(warned.current.resetKey, resetKey)) warned.current = {
158
+ resetKey,
159
+ messages: /* @__PURE__ */ new Set()
160
+ };
161
+ for (const message of messages) {
162
+ if (message === void 0 || warned.current.messages.has(message)) continue;
163
+ warned.current.messages.add(message);
164
+ console.warn(message);
165
+ }
166
+ }, [messages.join("\0"), resetKey]);
167
+ }
168
+ /**
169
+ * A value for a warning, as JSON when it has a JSON form (a string comes out
170
+ * quoted), so an object does not print as "[object Object]".
171
+ */
172
+ function describeValue(value) {
173
+ try {
174
+ const json = JSON.stringify(value);
175
+ if (json !== void 0) return json;
176
+ } catch {}
177
+ return String(value);
178
+ }
179
+
180
+ //#endregion
181
+ //#region src/generative-ui/GenerativeUIProvider.tsx
182
+ /**
183
+ * The number of mounted providers with a `ui`, by CopilotKit core. Each one
184
+ * sets the generative UI of every run on its core, so two of them override
185
+ * each other.
186
+ */
187
+ const mountedRoots = /* @__PURE__ */ new WeakMap();
188
+ /**
189
+ * Whether a `GenerativeUIProvider` is above, so the components below can tell
190
+ * "no provider" from "a provider without `ui`" (the runtime is null in both).
191
+ */
192
+ const GenerativeUIProviderPresentContext = (0, react.createContext)(false);
193
+ /**
194
+ * Experimental. Turns on generative UI for the agents of the enclosing
195
+ * `CopilotKitProvider`: the agent renders the components of `ui` (from
196
+ * `createGenerativeUI`) with the `render_ui` tool. Place it inside
197
+ * `CopilotKitProvider`. It applies to every agent run on that CopilotKit
198
+ * core, wherever the run starts in the component tree; limit it to some
199
+ * agents with `agentIds`. Show what the agent
200
+ * renders with `<GenerativeUIActivity>` (any activity message, for example in
201
+ * your own chat list or on a home screen) or `<GenerativeUISurface id>` (a
202
+ * named place in your app).
203
+ *
204
+ * The runtime needs no setup: `CopilotRuntime` adds the `render_ui` tool to
205
+ * the run when the app sends a catalog with at least one component. A
206
+ * catalog with no components gets no `render_ui` tool.
207
+ *
208
+ * Mount exactly one `GenerativeUIProvider` with a `ui` for each
209
+ * `CopilotKitProvider`, above every screen that shows generative UI, rather
210
+ * than one per screen. Each one sets the catalog and the named targets of
211
+ * every run on the core, so two of them override each other, and each
212
+ * shows only the surfaces built for its own catalog. In development, the
213
+ * second one mounted warns.
214
+ *
215
+ * It ingests every generative UI activity message in the messages of the
216
+ * agents it watches. A message whose content is malformed is skipped (in
217
+ * development it warns once for each distinct issue), as
218
+ * `<GenerativeUIActivity>` skips it.
219
+ *
220
+ * @example
221
+ * ```tsx
222
+ * const ui = createGenerativeUI({
223
+ * id: "bank",
224
+ * components: {
225
+ * BalanceCard: {
226
+ * description: "An account balance",
227
+ * props: z.object({ accountId: z.string() }),
228
+ * component: BalanceCard,
229
+ * },
230
+ * },
231
+ * });
232
+ *
233
+ * <CopilotKitProvider runtimeUrl={RUNTIME_URL}>
234
+ * <GenerativeUIProvider ui={ui}>
235
+ * <App />
236
+ * </GenerativeUIProvider>
237
+ * </CopilotKitProvider>
238
+ * ```
239
+ */
240
+ function GenerativeUIProvider({ children, host = nativeGenerativeUIHost, ...props }) {
241
+ const { theme, transport: requestedTransport, ...config } = props;
242
+ const hasTheme = theme !== void 0;
243
+ const otherTransport = requestedTransport === void 0 || requestedTransport === "tool" ? void 0 : describeValue(requestedTransport);
244
+ const hasUI = config.ui !== void 0 && config.ui !== null;
245
+ useDevelopmentWarnings([
246
+ hasTheme ? "[generativeUI] <GenerativeUIProvider> ignores `theme`: React Native has no CSS variables. Style the components of your catalog instead." : void 0,
247
+ otherTransport !== void 0 ? `[generativeUI] <GenerativeUIProvider> ignores transport ${otherTransport}: React Native supports only the tool transport.` : void 0,
248
+ hasUI ? void 0 : "[generativeUI] <GenerativeUIProvider> has no `ui`, so generative UI is off: pass the result of createGenerativeUI."
249
+ ]);
250
+ const { copilotkit } = (0, _copilotkit_react_core_context.useCopilotKit)();
251
+ const runtime = (0, _copilotkit_react_core_generative_ui_native.useGenerativeUIRoot)(copilotkit, {
252
+ ...config,
253
+ transport: "tool"
254
+ });
255
+ const isRoot = runtime !== null;
256
+ const warnedShared = (0, react.useRef)(false);
257
+ (0, react.useEffect)(() => {
258
+ if (!isRoot) return;
259
+ const count = (mountedRoots.get(copilotkit) ?? 0) + 1;
260
+ mountedRoots.set(copilotkit, count);
261
+ if (count > 1 && !warnedShared.current && isDevelopment()) {
262
+ warnedShared.current = true;
263
+ console.warn("[generativeUI] more than one <GenerativeUIProvider> with a `ui` is mounted under one CopilotKitProvider. Each one sets the catalog and the targets of every agent run on that core, so they override each other. Mount one <GenerativeUIProvider> for each CopilotKitProvider, above every screen that shows generative UI.");
264
+ }
265
+ return () => {
266
+ mountedRoots.set(copilotkit, (mountedRoots.get(copilotkit) ?? 1) - 1);
267
+ };
268
+ }, [copilotkit, isRoot]);
269
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUIProviderPresentContext.Provider, {
270
+ value: true,
271
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_react_core_generative_ui_native.GenerativeUIRuntimeContext.Provider, {
272
+ value: runtime,
273
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_copilotkit_react_core_generative_ui_native.GenerativeUIHostContext.Provider, {
274
+ value: host,
275
+ children: [runtime ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_react_core_generative_ui_native.GenerativeUIToolCallRenderer, {}) : null, children]
276
+ })
277
+ })
278
+ });
279
+ }
280
+
281
+ //#endregion
282
+ //#region src/generative-ui/GenerativeUIActivity.tsx
283
+ /**
284
+ * True for the activity messages that carry a generated surface. A type
285
+ * guard, so `agent.messages.filter(isGenerativeUIActivity)` gives activity
286
+ * messages. It does not check the content: `<GenerativeUIActivity>` does.
287
+ */
288
+ function isGenerativeUIActivity(message) {
289
+ return message.role === "activity" && message.activityType === _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE;
290
+ }
291
+ /**
292
+ * The default wrapper: the surface, and an error line under it when
293
+ * operations were rejected.
294
+ *
295
+ * It is the flex item of the parent, so it grows (`flexGrow: 1`) to fill
296
+ * the free space of a parent with a fixed size, and the stretch default of
297
+ * `alignItems` gives it the parent's cross size. A catalog root that sizes
298
+ * itself against its parent (`flex: 1`, `flexGrow` or a percentage height)
299
+ * then fills the parent, for example a screen that shows one surface.
300
+ * In a list it does not: a `FlatList` cell and the content of a
301
+ * `ScrollView` are as tall as their content, so there is no free space and
302
+ * each row keeps its own height. Siblings in a parent with a fixed size, or
303
+ * in a `ScrollView` whose `contentContainerStyle` grows, share its free
304
+ * space. Pass a `Wrapper` for any other layout.
305
+ */
306
+ function DefaultGenerativeUIActivityWrapper({ error, children }) {
307
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
308
+ style: styles$1.wrapper,
309
+ children: [children, error ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
310
+ accessibilityRole: "alert",
311
+ style: styles$1.error,
312
+ children: "Part of this UI couldn't be shown."
313
+ }) : null]
314
+ });
315
+ }
316
+ /**
317
+ * Experimental. Shows the surface that an agent rendered with `render_ui`,
318
+ * from its activity message. Put it wherever the app shows the agent's
319
+ * output: in a chat list (one per activity message), or on a screen with no
320
+ * chat at all, for example a home screen that an agent composes.
321
+ *
322
+ * Like the web chat, it shows only surfaces whose target is `"chat"`. A
323
+ * surface aimed at a `<GenerativeUISurface id>` is drawn there (see
324
+ * `target`).
325
+ *
326
+ * It renders nothing when:
327
+ * - the message is not a generative UI activity;
328
+ * - its content is malformed (in development it warns with the issues);
329
+ * - there is no `<GenerativeUIProvider>` above it, or the provider has no
330
+ * `ui` (in development it warns);
331
+ * - the agent is undefined or has no `threadId` yet, since the surface
332
+ * belongs to a thread (in development it warns);
333
+ * - the surface's target is not `target`;
334
+ * - the agent deleted the surface (`deleteSurface`).
335
+ *
336
+ * When the agent's `createSurface` was rejected, the Wrapper renders with
337
+ * `children` set to `null`, and only the error shows.
338
+ *
339
+ * Each instance warns once for each distinct problem, also when a problem
340
+ * goes away and comes back. A `message` with another id warns again.
341
+ *
342
+ * The provider ingests every activity message of `agent.messages` itself,
343
+ * with the same checks: a malformed one is skipped there too (in
344
+ * development it warns), so it cannot break the agent's run.
345
+ *
346
+ * @example
347
+ * ```tsx
348
+ * const { agent } = useAgent({ agentId: "home" });
349
+ * return agent.messages
350
+ * .filter(isGenerativeUIActivity)
351
+ * .map((message) => (
352
+ * <GenerativeUIActivity key={message.id} message={message} agent={agent} />
353
+ * ));
354
+ * ```
355
+ *
356
+ * Render every activity message, not only the latest: the latest can be a
357
+ * surface of a named target, or a deleted surface, and render nothing. For a
358
+ * placeholder until the agent renders, use `<GenerativeUISurface id
359
+ * fallback>`, which knows whether its target has a surface.
360
+ */
361
+ function GenerativeUIActivity({ message, agent, Wrapper = DefaultGenerativeUIActivityWrapper, target = _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET }) {
362
+ const runtime = (0, _copilotkit_react_core_generative_ui_native.useGenerativeUIRuntime)();
363
+ const hasProvider = (0, react.useContext)(GenerativeUIProviderPresentContext);
364
+ const isSurface = isGenerativeUIActivity(message);
365
+ const rawContent = isSurface ? message.content : void 0;
366
+ const parsed = (0, react.useMemo)(() => isSurface ? (0, _copilotkit_react_core_generative_ui_native.parseGenerativeUISurfaceContent)(rawContent) : void 0, [isSurface, rawContent]);
367
+ const content = parsed?.content;
368
+ const threadId = agent?.threadId;
369
+ const agentId = agent?.agentId;
370
+ let problem;
371
+ if (isSurface) {
372
+ if (!runtime) problem = hasProvider ? "the <GenerativeUIProvider> above it has no `ui`." : "there is no <GenerativeUIProvider> above it.";
373
+ else if (parsed?.issues) problem = `the content of activity message ${JSON.stringify(message.id ?? "")} is malformed: ${parsed.issues.join("; ")}.`;
374
+ else if (!agent) problem = "the agent is undefined.";
375
+ else if (!threadId) problem = "the agent has no threadId yet.";
376
+ }
377
+ useDevelopmentWarnings([problem === void 0 ? void 0 : `[generativeUI] <GenerativeUIActivity> renders nothing: ${problem}`], message.id);
378
+ const { surface, rejected, error } = (0, _copilotkit_react_core_generative_ui_native.useGenerativeUIActivitySurface)({
379
+ runtime,
380
+ threadId,
381
+ agentId,
382
+ content,
383
+ target
384
+ });
385
+ if (!content || !surface) return null;
386
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
387
+ surfaceId: content.surfaceId,
388
+ isStreaming: content.status === "streaming",
389
+ error,
390
+ rejected,
391
+ children: surface.failed ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_react_core_generative_ui_native.SurfaceView, { surface })
392
+ });
393
+ }
394
+ const styles$1 = react_native.StyleSheet.create({
395
+ wrapper: { flexGrow: 1 },
396
+ error: {
397
+ marginTop: 8,
398
+ fontSize: 12,
399
+ color: "#dc2626"
400
+ }
401
+ });
402
+
403
+ //#endregion
404
+ //#region src/generative-ui/GenerativeUISurface.tsx
405
+ /**
406
+ * The default wrapper of a named-target surface: the surface, and an alert
407
+ * under it when operations were rejected. It draws no view of its own, so
408
+ * the surface lays out in the view around it (`style`).
409
+ */
410
+ function DefaultGenerativeUISurfaceWrapper({ error, children }) {
411
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [children, error ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
412
+ accessibilityRole: "alert",
413
+ style: styles.error,
414
+ children: "Part of this UI couldn't be shown."
415
+ }) : null] });
416
+ }
417
+ /**
418
+ * Experimental. A named place in the app where the agent can render UI. The
419
+ * agent sees the target, with its `description`, in each run that starts
420
+ * while it is mounted (the target list is sent at the start of the run).
421
+ *
422
+ * A target follows the thread that is active in the provider: the thread of
423
+ * the last run that started on the CopilotKit core, of any agent, including
424
+ * agents that the provider's `agentIds` leaves out. To show the output of
425
+ * one particular agent run, use `<GenerativeUIActivity>` with that agent's
426
+ * activity message.
427
+ *
428
+ * In development it warns once for each distinct problem of this instance
429
+ * (no `ui` on the provider, a `ui` that is not the provider's, unknown
430
+ * component names), and again for any of them after the provider's catalog
431
+ * changes.
432
+ *
433
+ * It throws when there is no `<GenerativeUIProvider>` above it. Under a
434
+ * provider with no `ui`, it shows its fallback (in development it warns).
435
+ * The view around it, with `style` and the test ID
436
+ * `generative-ui-surface-<id>`, is there in both states.
437
+ *
438
+ * @example
439
+ * ```tsx
440
+ * <GenerativeUISurface
441
+ * id="insights"
442
+ * ui={bankUI}
443
+ * description="Under the balances. Show spending insights here."
444
+ * components={["SpendingCard", "InsightCard"]}
445
+ * fallback={<InsightsPlaceholder />}
446
+ * />
447
+ * ```
448
+ */
449
+ function GenerativeUISurface({ id, description, ui, components, fallback = null, style, onGenerationStart, Wrapper = DefaultGenerativeUISurfaceWrapper }) {
450
+ const runtime = (0, _copilotkit_react_core_generative_ui_native.useGenerativeUIRuntime)();
451
+ const hasProvider = (0, react.useContext)(GenerativeUIProviderPresentContext);
452
+ if (!runtime && !hasProvider) throw new Error(`<GenerativeUISurface id="${id}"> must be rendered inside a <GenerativeUIProvider> from @copilotkit/react-native/generative-ui.`);
453
+ (0, _copilotkit_react_core_generative_ui_native.useManagerVersion)(runtime);
454
+ const otherUI = runtime !== null && ui !== void 0 && ui !== runtime.ui;
455
+ const componentsKey = components === void 0 ? void 0 : JSON.stringify(components);
456
+ const catalog = runtime?.ui;
457
+ const allowed = (0, react.useMemo)(() => {
458
+ if (!catalog || componentsKey === void 0) return void 0;
459
+ return (0, _copilotkit_core_generative_ui.allowedComponents)(JSON.parse(componentsKey), catalog);
460
+ }, [catalog, componentsKey]);
461
+ useDevelopmentWarnings([
462
+ runtime ? void 0 : `[generativeUI] <GenerativeUISurface id="${id}"> shows its fallback: the <GenerativeUIProvider> above it has no \`ui\`.`,
463
+ otherUI ? `[generativeUI] <GenerativeUISurface id="${id}">: the ui prop is not the UI of the provider. The components are checked against the UI of the provider.` : void 0,
464
+ allowed && allowed.warning !== "" ? `[generativeUI] <GenerativeUISurface id="${id}">: ${allowed.warning}.` : void 0
465
+ ], catalog);
466
+ const allowedNames = allowed?.names;
467
+ (0, react.useLayoutEffect)(() => {
468
+ if (!runtime) return;
469
+ return runtime.manager.registerTarget(id, {
470
+ description,
471
+ components: allowedNames
472
+ });
473
+ }, [
474
+ runtime,
475
+ id,
476
+ description,
477
+ componentsKey
478
+ ]);
479
+ const surface = runtime?.manager.getSurfaceForTarget(id);
480
+ const { rejected, error } = (0, _copilotkit_react_core_generative_ui_native.useGenerativeUIRejected)(surface?.serverRejected, surface?.clientRejected);
481
+ const seen = (0, react.useRef)({ id });
482
+ const pendingStarts = (0, react.useRef)(0);
483
+ const [, setStartTick] = (0, react.useState)(0);
484
+ (0, react.useEffect)(() => {
485
+ if (!runtime) return;
486
+ const { manager } = runtime;
487
+ if (seen.current.id !== id) seen.current = { id };
488
+ const check = () => {
489
+ const current = manager.getSurfaceForTarget(id);
490
+ const last = seen.current.surface;
491
+ if (!current) {
492
+ if (seen.current.manager === manager) seen.current = {
493
+ id,
494
+ manager
495
+ };
496
+ return;
497
+ }
498
+ const next = {
499
+ key: current.key,
500
+ status: current.status,
501
+ serverRejected: current.serverRejected.length,
502
+ clientRejected: current.clientRejected.length
503
+ };
504
+ seen.current = {
505
+ id,
506
+ manager,
507
+ surface: next
508
+ };
509
+ if (next.status !== "streaming") return;
510
+ if (last === void 0 || last.key !== next.key || last.status !== "streaming" || next.serverRejected < last.serverRejected || next.clientRejected < last.clientRejected) {
511
+ pendingStarts.current += 1;
512
+ setStartTick((n) => n + 1);
513
+ }
514
+ };
515
+ check();
516
+ return manager.subscribe(check);
517
+ }, [runtime, id]);
518
+ const onStart = (0, react.useRef)(onGenerationStart);
519
+ onStart.current = onGenerationStart;
520
+ (0, react.useEffect)(() => {
521
+ while (pendingStarts.current > 0) {
522
+ pendingStarts.current -= 1;
523
+ const callback = onStart.current;
524
+ if (!callback) continue;
525
+ try {
526
+ callback();
527
+ } catch (callbackError) {
528
+ console.error(`[generativeUI] <GenerativeUISurface id="${id}"> onGenerationStart threw:`, callbackError);
529
+ }
530
+ }
531
+ });
532
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.View, {
533
+ style,
534
+ testID: `generative-ui-surface-${id}`,
535
+ children: surface ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
536
+ surfaceId: surface.surfaceId,
537
+ isStreaming: surface.status === "streaming",
538
+ error,
539
+ rejected,
540
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_react_core_generative_ui_native.SurfaceView, { surface })
541
+ }) : fallback
542
+ });
543
+ }
544
+ const styles = react_native.StyleSheet.create({ error: {
545
+ marginTop: 8,
546
+ fontSize: 12,
547
+ color: "#dc2626"
548
+ } });
549
+
550
+ //#endregion
551
+ exports.DefaultGenerativeUIActivityWrapper = DefaultGenerativeUIActivityWrapper;
552
+ exports.GenerativeUIActivity = GenerativeUIActivity;
553
+ Object.defineProperty(exports, 'GenerativeUIError', {
554
+ enumerable: true,
555
+ get: function () {
556
+ return _copilotkit_react_core_generative_ui_native.GenerativeUIError;
557
+ }
558
+ });
559
+ exports.GenerativeUIProvider = GenerativeUIProvider;
560
+ exports.GenerativeUISurface = GenerativeUISurface;
561
+ exports.NativeNodeFallback = NativeNodeFallback;
562
+ Object.defineProperty(exports, 'child', {
563
+ enumerable: true,
564
+ get: function () {
565
+ return _copilotkit_react_core_generative_ui_native.child;
566
+ }
567
+ });
568
+ Object.defineProperty(exports, 'childList', {
569
+ enumerable: true,
570
+ get: function () {
571
+ return _copilotkit_react_core_generative_ui_native.childList;
572
+ }
573
+ });
574
+ Object.defineProperty(exports, 'createGenerativeUI', {
575
+ enumerable: true,
576
+ get: function () {
577
+ return _copilotkit_react_core_generative_ui_native.createGenerativeUI;
578
+ }
579
+ });
580
+ Object.defineProperty(exports, 'exportA2UICatalog', {
581
+ enumerable: true,
582
+ get: function () {
583
+ return _copilotkit_react_core_generative_ui_native.exportA2UICatalog;
584
+ }
585
+ });
586
+ exports.isGenerativeUIActivity = isGenerativeUIActivity;
587
+ exports.nativeGenerativeUIHost = nativeGenerativeUIHost;
588
+ Object.defineProperty(exports, 'useGenerativeUIAccessibility', {
589
+ enumerable: true,
590
+ get: function () {
591
+ return _copilotkit_react_core_generative_ui_native.useGenerativeUIAccessibility;
592
+ }
593
+ });
594
+ Object.defineProperty(exports, 'useGenerativeUIFunction', {
595
+ enumerable: true,
596
+ get: function () {
597
+ return _copilotkit_react_core_generative_ui_native.useGenerativeUIFunction;
598
+ }
599
+ });
600
+ Object.defineProperty(exports, 'useGenerativeUIRuntime', {
601
+ enumerable: true,
602
+ get: function () {
603
+ return _copilotkit_react_core_generative_ui_native.useGenerativeUIRuntime;
604
+ }
605
+ });
606
+ Object.defineProperty(exports, 'useGenerativeUIState', {
607
+ enumerable: true,
608
+ get: function () {
609
+ return _copilotkit_react_core_generative_ui_native.useGenerativeUIState;
610
+ }
611
+ });
612
+ //# sourceMappingURL=index.cjs.map