@copilotkit/react-native 1.85.0 → 1.87.0

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 (55) hide show
  1. package/README.md +341 -2
  2. package/USAGE.md +388 -21
  3. package/dist/{CopilotKitProvider-DBq4AhRP.d.cts → CopilotKitProvider-CQ-DpvY1.d.cts} +2 -2
  4. package/dist/{CopilotKitProvider-D8sn7G_C.mjs → CopilotKitProvider-CXQGv39c.mjs} +1 -1
  5. package/dist/{CopilotKitProvider-D8sn7G_C.mjs.map → CopilotKitProvider-CXQGv39c.mjs.map} +1 -1
  6. package/dist/{CopilotKitProvider-fW21Gb7T.d.mts → CopilotKitProvider-Dc1Vbv7K.d.mts} +1 -1
  7. package/dist/{CopilotKitProvider-CjoPnabW.cjs → CopilotKitProvider-zSfbWwIg.cjs} +1 -1
  8. package/dist/{CopilotKitProvider-CjoPnabW.cjs.map → CopilotKitProvider-zSfbWwIg.cjs.map} +1 -1
  9. package/dist/components/index.cjs +1 -0
  10. package/dist/components/index.cjs.map +1 -1
  11. package/dist/components/index.mjs +1 -0
  12. package/dist/components/index.mjs.map +1 -1
  13. package/dist/generative-ui/index.cjs +612 -0
  14. package/dist/generative-ui/index.cjs.map +1 -0
  15. package/dist/generative-ui/index.d.cts +341 -0
  16. package/dist/generative-ui/index.d.mts +341 -0
  17. package/dist/generative-ui/index.mjs +549 -0
  18. package/dist/generative-ui/index.mjs.map +1 -0
  19. package/dist/headless.cjs +1 -1
  20. package/dist/headless.d.cts +1 -1
  21. package/dist/headless.d.mts +1 -1
  22. package/dist/headless.mjs +1 -1
  23. package/dist/index.cjs +2 -1
  24. package/dist/index.cjs.map +1 -1
  25. package/dist/index.d.cts +2 -2
  26. package/dist/index.d.mts +1 -1
  27. package/dist/index.mjs +2 -1
  28. package/dist/index.mjs.map +1 -1
  29. package/dist/polyfills.cjs +218 -2
  30. package/dist/polyfills.cjs.map +1 -1
  31. package/dist/polyfills.mjs +217 -1
  32. package/dist/polyfills.mjs.map +1 -1
  33. package/package.json +12 -4
  34. package/project.json +6 -1
  35. package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
  36. package/scripts/verify-polyfill-barrel.mjs +154 -5
  37. package/src/__tests__/generative-ui-harness.tsx +243 -0
  38. package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
  39. package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
  40. package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
  41. package/src/__tests__/generative-ui.test.tsx +1000 -0
  42. package/src/__tests__/headless-entry-surface.test.ts +964 -34
  43. package/src/components/index.ts +6 -0
  44. package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
  45. package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
  46. package/src/generative-ui/GenerativeUISurface.tsx +303 -0
  47. package/src/generative-ui/host.tsx +138 -0
  48. package/src/generative-ui/index.ts +99 -0
  49. package/src/generative-ui/internal.ts +55 -0
  50. package/src/index.ts +8 -3
  51. package/tsdown.config.ts +1 -0
  52. package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
  53. package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
  54. package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
  55. package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
@@ -0,0 +1,106 @@
1
+ import type React from "react";
2
+ import { expectTypeOf } from "vitest";
3
+ import type { Message } from "@ag-ui/client";
4
+ import {
5
+ createGenerativeUI,
6
+ GenerativeUIProvider,
7
+ GenerativeUISurface,
8
+ isGenerativeUIActivity,
9
+ } from "../generative-ui";
10
+ import type {
11
+ GenerativeUIAccessibilityWrapperProps,
12
+ GenerativeUIActivityWrapperProps,
13
+ GenerativeUIHost,
14
+ GenerativeUINodeFallbackProps,
15
+ GenerativeUIProviderProps,
16
+ GenerativeUIRejectedOperation,
17
+ GenerativeUIRuntime,
18
+ GenerativeUISurfaceProps,
19
+ GenerativeUISurfaceRootProps,
20
+ GenerativeUIToolConfig,
21
+ } from "../generative-ui";
22
+
23
+ /**
24
+ * Type-level guard for the `@copilotkit/react-native/generative-ui` entry.
25
+ *
26
+ * A `.test-d.tsx` file, like `frontend-tool-render.test-d.tsx`: `tsc --noEmit`
27
+ * (`nx run @copilotkit/react-native:check-types`) compiles it, so every
28
+ * `@ts-expect-error` below is load-bearing, and the `.test-d.` infix keeps it
29
+ * out of vitest's suite glob.
30
+ */
31
+
32
+ const ui = createGenerativeUI({ id: "props-test", components: {} });
33
+
34
+ // ─── `theme` is not a React Native prop ──────────────────────────────────────
35
+ // The web `theme` sets `--a2ui-*` CSS variables. React Native has no CSS
36
+ // variables, so the RN host never applies it; accepting it would drop the
37
+ // value silently.
38
+
39
+ expectTypeOf<GenerativeUIProviderProps>().not.toHaveProperty("theme");
40
+ // The config type this entry exports is the React Native one, without theme.
41
+ expectTypeOf<GenerativeUIToolConfig>().not.toHaveProperty("theme");
42
+ expectTypeOf<GenerativeUIToolConfig>().toHaveProperty("agentIds");
43
+
44
+ export const withTheme = (
45
+ // @ts-expect-error `theme` is not accepted on React Native.
46
+ <GenerativeUIProvider ui={ui} theme={{}}>
47
+ {null}
48
+ </GenerativeUIProvider>
49
+ );
50
+
51
+ export const withoutTheme = (
52
+ <GenerativeUIProvider ui={ui} agentIds={["agent"]}>
53
+ {null}
54
+ </GenerativeUIProvider>
55
+ );
56
+
57
+ // ─── The types a custom `host` and a custom Wrapper need are exported ────────
58
+
59
+ expectTypeOf<GenerativeUIActivityWrapperProps["rejected"]>().toEqualTypeOf<
60
+ GenerativeUIRejectedOperation[]
61
+ >();
62
+ expectTypeOf<
63
+ React.ComponentProps<GenerativeUIHost["SurfaceRoot"]>
64
+ >().toEqualTypeOf<GenerativeUISurfaceRootProps>();
65
+ expectTypeOf<
66
+ React.ComponentProps<GenerativeUIHost["AccessibilityWrapper"]>
67
+ >().toEqualTypeOf<GenerativeUIAccessibilityWrapperProps>();
68
+ expectTypeOf<
69
+ React.ComponentProps<GenerativeUIHost["NodeFallback"]>
70
+ >().toEqualTypeOf<GenerativeUINodeFallbackProps>();
71
+ expectTypeOf<GenerativeUIRuntime>().toBeObject();
72
+
73
+ // ─── isGenerativeUIActivity is a type guard ──────────────────────────────────
74
+
75
+ declare const messages: Message[];
76
+ const activities = messages.filter(isGenerativeUIActivity);
77
+ expectTypeOf(activities[0]!.role).toEqualTypeOf<"activity">();
78
+ expectTypeOf(
79
+ activities[0]!.activityType,
80
+ ).toEqualTypeOf<"generative-ui-surface">();
81
+
82
+ // ─── GenerativeUISurface: `components` is typed by `ui` ──────────────────────
83
+
84
+ const shopUI = createGenerativeUI({
85
+ id: "surface-props-test",
86
+ components: { Card: { component: () => null } },
87
+ });
88
+
89
+ export const typedComponents = (
90
+ <GenerativeUISurface id="a" ui={shopUI} components={["Card"]} />
91
+ );
92
+
93
+ export const unknownComponent = (
94
+ // @ts-expect-error "Nope" is not a component of shopUI.
95
+ <GenerativeUISurface id="a" ui={shopUI} components={["Nope"]} />
96
+ );
97
+
98
+ export const componentsWithoutUI = (
99
+ // @ts-expect-error `components` needs `ui`, which types its names.
100
+ <GenerativeUISurface id="a" components={["Card"]} />
101
+ );
102
+
103
+ // The Wrapper of a named-target surface takes the Activity's Wrapper props.
104
+ expectTypeOf<NonNullable<GenerativeUISurfaceProps["Wrapper"]>>().toEqualTypeOf<
105
+ React.ComponentType<GenerativeUIActivityWrapperProps>
106
+ >();
@@ -0,0 +1,54 @@
1
+ /**
2
+ * React Native host components as DOM elements, so the generative UI tests
3
+ * can query them. The layout and accessibility props a native view would
4
+ * receive are written out as data attributes, so the tests can see what
5
+ * reaches the platform.
6
+ *
7
+ * Use it as the factory of `vi.mock("react-native", ...)`. It imports
8
+ * nothing that imports react-native, so the factory cannot recurse.
9
+ */
10
+ import React from "react";
11
+
12
+ const host =
13
+ (tag: string) =>
14
+ ({
15
+ children,
16
+ testID,
17
+ style,
18
+ onPress,
19
+ accessibilityRole,
20
+ collapsable,
21
+ accessibilityElementsHidden,
22
+ importantForAccessibility,
23
+ accessibilityLiveRegion,
24
+ "aria-hidden": ariaHidden,
25
+ "aria-live": ariaLive,
26
+ }: any) =>
27
+ React.createElement(
28
+ tag,
29
+ {
30
+ "data-testid": testID,
31
+ "data-style": JSON.stringify(style ?? null),
32
+ "data-collapsable": JSON.stringify(collapsable),
33
+ "data-accessibility-elements-hidden": JSON.stringify(
34
+ accessibilityElementsHidden,
35
+ ),
36
+ "data-important-for-accessibility": importantForAccessibility,
37
+ "data-accessibility-live-region": accessibilityLiveRegion,
38
+ "data-aria-hidden": JSON.stringify(ariaHidden),
39
+ "data-aria-live": ariaLive,
40
+ role: accessibilityRole,
41
+ onClick: onPress,
42
+ },
43
+ children,
44
+ );
45
+
46
+ export function createReactNativeMock() {
47
+ return {
48
+ View: host("div"),
49
+ Text: host("span"),
50
+ Pressable: host("button"),
51
+ StyleSheet: { create: (s: unknown) => s, hairlineWidth: 1 },
52
+ Platform: { OS: "ios", select: (o: any) => o.ios ?? o.default },
53
+ };
54
+ }