@copilotkit/react-native 1.83.0 → 2.0.0-rc.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 (67) hide show
  1. package/USAGE.md +2 -2
  2. package/dist/{CopilotKitProvider-BUVBq9LN.d.mts → CopilotKitProvider-BWjzCdHN.d.mts} +29 -2
  3. package/dist/{CopilotKitProvider-BVPr5t83.d.cts → CopilotKitProvider-Btm10msB.d.cts} +29 -2
  4. package/dist/{CopilotKitProvider-CjoPnabW.cjs → CopilotKitProvider-Byw3cqe2.cjs} +13 -10
  5. package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +1 -0
  6. package/dist/{CopilotKitProvider-D8sn7G_C.mjs → CopilotKitProvider-CE4bc6CP.mjs} +9 -5
  7. package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +1 -0
  8. package/dist/Markdown-1ips7myr.d.cts +52 -0
  9. package/dist/Markdown-C6AsLHmd.mjs +47 -0
  10. package/dist/Markdown-C6AsLHmd.mjs.map +1 -0
  11. package/dist/Markdown-C_9Re36O.cjs +60 -0
  12. package/dist/Markdown-C_9Re36O.cjs.map +1 -0
  13. package/dist/Markdown-JsYh7oSA.d.mts +52 -0
  14. package/dist/MarkdownRendererContext-BRNe808d.cjs +72 -0
  15. package/dist/MarkdownRendererContext-BRNe808d.cjs.map +1 -0
  16. package/dist/MarkdownRendererContext-CAnuohIS.mjs +21 -0
  17. package/dist/MarkdownRendererContext-CAnuohIS.mjs.map +1 -0
  18. package/dist/components/index.cjs +20 -106
  19. package/dist/components/index.cjs.map +1 -1
  20. package/dist/components/index.d.cts +1 -46
  21. package/dist/components/index.d.mts +1 -46
  22. package/dist/components/index.mjs +13 -98
  23. package/dist/components/index.mjs.map +1 -1
  24. package/dist/headless.cjs +22 -22
  25. package/dist/headless.d.cts +4 -3
  26. package/dist/headless.d.mts +4 -3
  27. package/dist/headless.mjs +3 -3
  28. package/dist/index.cjs +32 -27
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.cts +3 -2
  31. package/dist/index.d.mts +3 -2
  32. package/dist/index.mjs +6 -4
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/polyfills/encoding.cjs +1 -1
  35. package/dist/polyfills/streams.cjs +1 -1
  36. package/package.json +6 -9
  37. package/scripts/measure-headless.mjs +2 -2
  38. package/src/CopilotChat.tsx +2 -2
  39. package/src/CopilotKitProvider.tsx +17 -4
  40. package/src/__mocks__/test-copilotkit.tsx +3 -3
  41. package/src/__tests__/attachments-integration.test.tsx +2 -2
  42. package/src/__tests__/copilot-chat-agentid.test.tsx +2 -2
  43. package/src/__tests__/copilot-chat-attachments.test.tsx +2 -2
  44. package/src/__tests__/copilot-popup.test.tsx +2 -2
  45. package/src/__tests__/copilot-sidebar.test.tsx +2 -2
  46. package/src/__tests__/copilotkit-provider.test.tsx +2 -2
  47. package/src/__tests__/headless-entry-surface.test.ts +33 -12
  48. package/src/__tests__/headless-integration.test.tsx +4 -2
  49. package/src/__tests__/headless-value-exports.test.ts +2 -2
  50. package/src/components/CopilotChat.tsx +2 -5
  51. package/src/components/Markdown.tsx +62 -100
  52. package/src/components/MarkdownRendererContext.tsx +40 -0
  53. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +2 -2
  54. package/src/components/__tests__/CopilotChat.test.tsx +2 -2
  55. package/src/components/__tests__/Markdown.code-alias.test.tsx +59 -0
  56. package/src/components/__tests__/Markdown.test.tsx +6 -75
  57. package/src/components/messages/AssistantMessage.tsx +22 -1
  58. package/src/components/messages/__tests__/AssistantMessage.provider-renderer.test.tsx +73 -0
  59. package/src/components/messages/__tests__/markdown-renderer-config.test.tsx +66 -0
  60. package/src/headless.ts +8 -8
  61. package/src/index.ts +19 -2
  62. package/tsdown.config.ts +0 -1
  63. package/vitest.config.mjs +0 -7
  64. package/dist/CopilotKitProvider-CjoPnabW.cjs.map +0 -1
  65. package/dist/CopilotKitProvider-D8sn7G_C.mjs.map +0 -1
  66. package/dist/chunk-C0xms8kb.cjs +0 -34
  67. package/src/__mocks__/react-native-streamdown.ts +0 -16
@@ -7,7 +7,7 @@ import path from "node:path";
7
7
  import type * as HeadlessEntry from "../headless";
8
8
  // Same reason, and additionally: the identity assertion below needs the module
9
9
  // this entry re-exports FROM, to compare bindings rather than merely count them.
10
- import type * as CoreHeadlessEntry from "@copilotkit/react-core/v2/headless";
10
+ import type * as CoreHeadlessEntry from "@copilotkit/react-core/headless";
11
11
 
12
12
  /**
13
13
  * Guards the `@copilotkit/react-native/headless` entry (src/headless.ts).
@@ -21,7 +21,7 @@ import type * as CoreHeadlessEntry from "@copilotkit/react-core/v2/headless";
21
21
  * (nothing in a normal typecheck/test catches it, because those are optional
22
22
  * peers). This walks the relative-import graph and fails if that happens.
23
23
  *
24
- * Mirrors the export-surface guard added for @copilotkit/react-core/v2/headless
24
+ * Mirrors the export-surface guard added for @copilotkit/react-core/headless
25
25
  * (PR #5883).
26
26
  *
27
27
  * ─── Why the walker looks the way it does ──────────────────────────────────
@@ -31,7 +31,7 @@ import type * as CoreHeadlessEntry from "@copilotkit/react-core/v2/headless";
31
31
  * to close them and each is covered by a test:
32
32
  *
33
33
  * 1. It only saw `import … from "x"`. A lazy optional-peer
34
- * `require("@copilotkit/react-core/v2")` or `await import(…)` — which Metro
34
+ * `require("@copilotkit/react-core")` or `await import(…)` — which Metro
35
35
  * follows and bundles just the same — was invisible. Now static, bare
36
36
  * side-effect, dynamic `import()` and `require()` are all extracted, and a
37
37
  * loader whose argument is NOT a literal is reported as unanalyzable rather
@@ -41,7 +41,7 @@ import type * as CoreHeadlessEntry from "@copilotkit/react-core/v2/headless";
41
41
  * `…/headless`, `…/polyfills` and its five subpaths) that NO source file
42
42
  * imports (half the reported bare-specifier set), purely from JSDoc
43
43
  * examples. In the other direction, a "don't do this: import from
44
- * @copilotkit/react-core/v2" counter-example written in a doc comment failed
44
+ * @copilotkit/react-core" counter-example written in a doc comment failed
45
45
  * the build. Comments are stripped before matching now.
46
46
  * 3. `resolveLocal` returned null for an edge it could not resolve and the
47
47
  * caller dropped it, so an unresolvable specifier read as "clean" while
@@ -85,10 +85,10 @@ const FORBIDDEN_LOCAL = [
85
85
  ];
86
86
 
87
87
  // The one react-core entry this package's hooks come from.
88
- const CORE_HEADLESS = "@copilotkit/react-core/v2/headless";
88
+ const CORE_HEADLESS = "@copilotkit/react-core/headless";
89
89
 
90
90
  // ─── #4893 bundle guard ──────────────────────────────────────────────────────
91
- // @copilotkit/react-core/v2 (the "fat" entry) re-exports from a monolithic chunk
91
+ // @copilotkit/react-core (the "fat" entry) re-exports from a monolithic chunk
92
92
  // that pulls the chat-message rendering stack: streamdown -> shiki (~5.5 MB of
93
93
  // grammars + themes), plus mermaid, cytoscape and katex. Metro does not
94
94
  // tree-shake, so ONE import of that specifier from anywhere in this package puts
@@ -96,7 +96,7 @@ const CORE_HEADLESS = "@copilotkit/react-core/v2/headless";
96
96
  // hooks into /v2/headless precisely so this package never needs the fat entry.
97
97
  const ALLOWED_REACT_CORE_ENTRIES = [
98
98
  CORE_HEADLESS,
99
- "@copilotkit/react-core/v2/context",
99
+ "@copilotkit/react-core/context",
100
100
  ];
101
101
 
102
102
  /**
@@ -140,6 +140,21 @@ const FORBIDDEN_REGISTRY_APIS: readonly (readonly [
140
140
  ],
141
141
  ];
142
142
 
143
+ // Deliberate exemptions from FORBIDDEN_REGISTRY_APIS: one file, one identifier,
144
+ // and the reason it is not a tool/renderer registry. File paths are relative to
145
+ // src/ and use forward slashes.
146
+ const REGISTRY_API_EXEMPTIONS: readonly (readonly [
147
+ file: string,
148
+ identifier: string,
149
+ why: string,
150
+ ])[] = [
151
+ [
152
+ "components/MarkdownRendererContext.tsx",
153
+ "createContext",
154
+ "holds the provider-level `markdownRenderer` (a component, or config for the built-in markdown renderer). It stores no tools and no tool-call renderers",
155
+ ],
156
+ ];
157
+
143
158
  // Heavy modules that must never appear as a direct import from this package.
144
159
  // (Transitive leakage through react-core's own entry is covered separately by
145
160
  // packages/react-core/scripts/assert-headless-purity.mjs — this walker cannot
@@ -170,7 +185,7 @@ const REQUIRED_HEADLESS_MODULES = [
170
185
  "CopilotKitProvider.tsx",
171
186
  "polyfills.ts",
172
187
  ];
173
- const REQUIRED_HEADLESS_BARE = ["@copilotkit/react-core/v2/headless"];
188
+ const REQUIRED_HEADLESS_BARE = ["@copilotkit/react-core/headless"];
174
189
 
175
190
  interface PkgManifest {
176
191
  dependencies?: Record<string, string>;
@@ -196,7 +211,7 @@ interface PkgManifest {
196
211
  *
197
212
  * What this set does NOT police is WHICH ENTRY of an allowed package is imported:
198
213
  * `@copilotkit/react-core` is a legitimate dependency, so the fat
199
- * `@copilotkit/react-core/v2` passes here and is caught by the dedicated
214
+ * `@copilotkit/react-core` passes here and is caught by the dedicated
200
215
  * ALLOWED_REACT_CORE_ENTRIES test (which is package-wide, i.e. stricter). Keep
201
216
  * both — this one is the catch-all for UNENUMERATED packages, that one is the
202
217
  * #4893 entry ban.
@@ -521,7 +536,13 @@ describe("@copilotkit/react-native/headless entry", () => {
521
536
  // modules in it name these APIs in prose to explain why they do NOT call
522
537
  // them. A raw text match would fail on the documentation.
523
538
  const code = stripComments(fs.readFileSync(file, "utf8"));
539
+ const relFile = path.relative(srcDir, file).split(path.sep).join("/");
524
540
  for (const [identifier, why] of FORBIDDEN_REGISTRY_APIS) {
541
+ const exempt = REGISTRY_API_EXEMPTIONS.some(
542
+ ([exemptFile, exemptIdentifier]) =>
543
+ exemptFile === relFile && exemptIdentifier === identifier,
544
+ );
545
+ if (exempt) continue;
525
546
  if (new RegExp(`\\b${identifier}\\b`).test(code)) {
526
547
  offenders.push(
527
548
  `${path.relative(srcDir, file)}: ${identifier} — ${why}`,
@@ -584,7 +605,7 @@ describe("@copilotkit/react-native/headless entry", () => {
584
605
  // hit rather than a second traversal.
585
606
  [mod, coreHeadless] = await Promise.all([
586
607
  import("../headless"),
587
- import("@copilotkit/react-core/v2/headless"),
608
+ import("@copilotkit/react-core/headless"),
588
609
  ]);
589
610
  }, IMPORT_BUDGET_MS);
590
611
 
@@ -640,7 +661,7 @@ describe("@copilotkit/react-native/headless entry", () => {
640
661
  expect(
641
662
  mod.useFrontendTool,
642
663
  "`useFrontendTool` on the RN headless entry is NOT the binding from " +
643
- "@copilotkit/react-core/v2/headless. RN must own no implementation of " +
664
+ "@copilotkit/react-core/headless. RN must own no implementation of " +
644
665
  "it: re-export core's, or a local one will drift from it silently " +
645
666
  "under a name consumers already trust.",
646
667
  ).toBe(coreHeadless.useFrontendTool);
@@ -727,7 +748,7 @@ describe("@copilotkit/react-native/headless entry", () => {
727
748
  expect(
728
749
  offenders,
729
750
  `must import only ${ALLOWED_REACT_CORE_ENTRIES.join(" or ")} — found: ${offenders.join(", ")}. ` +
730
- `Importing @copilotkit/react-core/v2 drags the shiki/mermaid/katex render stack into every RN bundle (#4893).`,
751
+ `Importing @copilotkit/react-core drags the shiki/mermaid/katex render stack into every RN bundle (#4893).`,
731
752
  ).toEqual([]);
732
753
  },
733
754
  );
@@ -107,7 +107,7 @@ function createMockCore() {
107
107
 
108
108
  let mockCoreInstance: ReturnType<typeof createMockCore>;
109
109
 
110
- vi.mock("@copilotkit/react-core/v2/headless", () => {
110
+ vi.mock("@copilotkit/react-core/headless", () => {
111
111
  // Regular function (not arrow) so it's new-able
112
112
  function CopilotKitCoreReact(this: any, ...args: any[]) {
113
113
  hoisted.MockCoreConstructor(...args);
@@ -188,7 +188,7 @@ vi.mock("@copilotkit/react-core/v2/headless", () => {
188
188
  };
189
189
  });
190
190
 
191
- vi.mock("@copilotkit/react-core/v2/context", () => {
191
+ vi.mock("@copilotkit/react-core/context", () => {
192
192
  const _React = require("react");
193
193
  return {
194
194
  CopilotKitContext: hoisted.RealContext,
@@ -245,6 +245,8 @@ vi.mock("@copilotkit/shared", async (importOriginal) => ({
245
245
  checkFeature: () => true,
246
246
  getLimit: () => null,
247
247
  }),
248
+ // Required by @copilotkit/core (transitively imported via Markdown.tsx)
249
+ RUNTIME_MODE_SSE: "sse",
248
250
  }));
249
251
 
250
252
  // Import after mocks
@@ -46,7 +46,7 @@ import type { RenderToolProps } from "../headless";
46
46
  *
47
47
  * ─── Why the guard has to live here, in react-native ─────────────────────────
48
48
  *
49
- * `packages/react-core/src/v2/__tests__/headless-type-exports.test-d.ts` cannot
49
+ * `packages/react-core/src/__tests__/headless-type-exports.test-d.ts` cannot
50
50
  * cover this, structurally: it asserts against react-core's entry surface, not
51
51
  * React Native's. Proof — react-core's own `UseAgentUpdate` was ALREADY a correct
52
52
  * value export at the time RN's was wrong. Export kind is a property of the
@@ -104,7 +104,7 @@ const RUNTIME_VALUE_EXPORTS: readonly ValueExport[] = [
104
104
  name: "UseAgentUpdate",
105
105
  typeOf: "object",
106
106
  member: "OnMessagesChanged",
107
- declaredAs: "export enum, @copilotkit/react-core/v2/headless",
107
+ declaredAs: "export enum, @copilotkit/react-core/headless",
108
108
  },
109
109
  {
110
110
  name: "AbstractAgent",
@@ -11,11 +11,8 @@ import {
11
11
  View,
12
12
  } from "react-native";
13
13
  import type { ListRenderItemInfo, ViewStyle } from "react-native";
14
- import {
15
- useAgent,
16
- useRenderToolCall,
17
- } from "@copilotkit/react-core/v2/headless";
18
- import { useCopilotKit } from "@copilotkit/react-core/v2/context";
14
+ import { useAgent, useRenderToolCall } from "@copilotkit/react-core/headless";
15
+ import { useCopilotKit } from "@copilotkit/react-core/context";
19
16
  import { AssistantMessage } from "./messages/AssistantMessage";
20
17
  import { UserMessage } from "./messages/UserMessage";
21
18
  import type { Message } from "@copilotkit/shared";
@@ -1,124 +1,86 @@
1
- import React, { useMemo } from "react";
2
- import { StreamdownText } from "react-native-streamdown";
1
+ import React from "react";
2
+ import {
3
+ StreamingMarkdownRenderer,
4
+ defaultMarkdownStyles as _streamingDefaultStyles,
5
+ } from "@copilotkit/markdown-renderer/react-native";
6
+ import type { MarkdownStyle as StreamingMarkdownStyle } from "@copilotkit/markdown-renderer/react-native";
7
+ import { warnUnsupportedRichSyntaxOnce } from "@copilotkit/markdown-renderer";
3
8
 
4
9
  /**
5
- * Style object accepted by `react-native-enriched-markdown` (and therefore
6
- * `react-native-streamdown`). Each key targets a markdown element; the
7
- * available properties vary per element — see the enriched-markdown style
8
- * reference for the full list.
10
+ * Style map for CopilotMarkdown. Each key maps a node type to a React Native
11
+ * style object. Extends the streaming renderer's style keys with a `code`
12
+ * shorthand kept for backward compatibility (`list` already exists on the
13
+ * base style and needs no alias).
14
+ *
15
+ * @public
9
16
  */
10
- export type MarkdownStyle = Record<string, Record<string, unknown>>;
17
+ export type MarkdownStyle = StreamingMarkdownStyle & {
18
+ /** Alias for `inlineCode`; kept for backward compatibility. */
19
+ code?: Record<string, unknown>;
20
+ };
11
21
 
12
22
  /**
13
- * Props for the CopilotMarkdown component.
23
+ * Default styles that ship with CopilotMarkdown.
24
+ *
25
+ * @public
14
26
  */
27
+ export const defaultMarkdownStyles: MarkdownStyle = {
28
+ ..._streamingDefaultStyles,
29
+ // Back-compat alias: `code` = same as `inlineCode`
30
+ code: _streamingDefaultStyles.inlineCode,
31
+ };
32
+
15
33
  export interface CopilotMarkdownProps {
16
- /** Markdown string to render. */
17
34
  content: string;
18
- /** Optional style overrides merged on top of the defaults. */
19
35
  style?: MarkdownStyle;
20
- /** Whether to enable the streaming fade-in animation (default: true). */
36
+ /**
37
+ * Toggles `Intl.Segmenter`-based token segmentation in the parser. Defaults
38
+ * to `false` (Hermes-safe: no Segmenter needed). Note: the React Native
39
+ * renderer renders text directly and does not perform per-token fade-in
40
+ * animation, so this currently has no visual effect — it is accepted for
41
+ * backward compatibility.
42
+ */
21
43
  streamingAnimation?: boolean;
22
44
  }
23
45
 
24
46
  /**
25
- * Default markdown styles tuned for chat bubble display.
26
- *
27
- * Exported so consumers can spread and extend:
28
- * ```ts
29
- * import { defaultMarkdownStyles } from "@copilotkit/react-native";
30
- * const custom = { ...defaultMarkdownStyles, h1: { fontSize: 28 } };
31
- * ```
32
- */
33
- export const defaultMarkdownStyles: MarkdownStyle = {
34
- paragraph: {
35
- fontSize: 16,
36
- lineHeight: 24,
37
- color: "#1a1a1a",
38
- marginTop: 4,
39
- marginBottom: 4,
40
- },
41
- h1: {
42
- fontSize: 24,
43
- fontWeight: "bold",
44
- marginTop: 12,
45
- marginBottom: 8,
46
- color: "#111111",
47
- },
48
- h2: {
49
- fontSize: 20,
50
- fontWeight: "bold",
51
- marginTop: 10,
52
- marginBottom: 6,
53
- color: "#111111",
54
- },
55
- h3: {
56
- fontSize: 18,
57
- fontWeight: "600",
58
- marginTop: 8,
59
- marginBottom: 4,
60
- color: "#222222",
61
- },
62
- strong: {
63
- fontWeight: "bold",
64
- },
65
- em: {
66
- fontStyle: "italic",
67
- },
68
- link: {
69
- color: "#0066cc",
70
- underline: true,
71
- },
72
- blockquote: {
73
- backgroundColor: "#f5f5f5",
74
- borderWidth: 4,
75
- borderColor: "#cccccc",
76
- gapWidth: 12,
77
- },
78
- code: {
79
- backgroundColor: "#f0f0f0",
80
- fontFamily: "monospace",
81
- fontSize: 14,
82
- },
83
- codeBlock: {
84
- backgroundColor: "#f0f0f0",
85
- borderRadius: 8,
86
- padding: 12,
87
- fontFamily: "monospace",
88
- fontSize: 14,
89
- },
90
- list: {
91
- marginTop: 4,
92
- marginBottom: 4,
93
- },
94
- };
95
-
96
- /**
97
- * Renders markdown content using `react-native-streamdown` with
98
- * pre-configured styles suited for CopilotKit chat bubbles.
47
+ * GFM markdown renderer for React Native. Delegates to the shared
48
+ * `StreamingMarkdownRenderer` from `@copilotkit/markdown-renderer/react-native`.
99
49
  *
100
- * `react-native-streamdown` processes incomplete streaming markdown in the
101
- * background, rendering incrementally without visual glitches — ideal for
102
- * displaying LLM output as it arrives.
50
+ * Public API is backward-compatible with the previous basic renderer:
51
+ * - `CopilotMarkdownProps` — same shape.
52
+ * - `MarkdownStyle` / `defaultMarkdownStyles` — same exports.
53
+ * - `streamingAnimation` maps to `animate` (defaults `false` = Hermes-safe).
103
54
  *
104
- * Custom styles are merged on top of the defaults so callers only need
105
- * to override what they want to change.
55
+ * @public
106
56
  */
107
57
  export function CopilotMarkdown({
108
58
  content,
109
59
  style,
110
- streamingAnimation = true,
111
- }: CopilotMarkdownProps) {
112
- const mergedStyles = useMemo(() => {
113
- if (!style) return defaultMarkdownStyles;
114
- return { ...defaultMarkdownStyles, ...style };
115
- }, [style]);
60
+ streamingAnimation = false,
61
+ }: CopilotMarkdownProps): React.ReactElement | null {
62
+ // Translate the back-compat `code` alias onto `inlineCode` — the key the
63
+ // streaming renderer actually reads. Without this, a caller migrating from
64
+ // the previous renderer who passes `style={{ code: ... }}` would silently
65
+ // lose inline-code styling. An explicit `inlineCode` wins over the alias.
66
+ // Dev-only: nudge upgraders from the bundled Streamdown default when their
67
+ // content needs math/syntax highlighting the built-in renderer doesn't do.
68
+ warnUnsupportedRichSyntaxOnce(content);
116
69
 
70
+ let resolvedStyle = style as StreamingMarkdownStyle | undefined;
71
+ if (style?.code !== undefined) {
72
+ const { code, ...rest } = style;
73
+ resolvedStyle = {
74
+ ...(rest as StreamingMarkdownStyle),
75
+ inlineCode: (rest as StreamingMarkdownStyle).inlineCode ?? code,
76
+ };
77
+ }
117
78
  return (
118
- <StreamdownText
119
- markdown={content}
120
- markdownStyle={mergedStyles}
121
- streamingAnimation={streamingAnimation}
79
+ <StreamingMarkdownRenderer
80
+ content={content}
81
+ isComplete={true}
82
+ style={resolvedStyle}
83
+ animate={streamingAnimation}
122
84
  />
123
85
  );
124
86
  }
@@ -0,0 +1,40 @@
1
+ import React, { createContext, useContext } from "react";
2
+ import type { MarkdownStyle } from "./Markdown";
3
+
4
+ export interface NativeMarkdownRendererProps {
5
+ content: string;
6
+ isStreaming?: boolean;
7
+ }
8
+
9
+ export type NativeMarkdownRenderer =
10
+ React.ComponentType<NativeMarkdownRendererProps>;
11
+
12
+ /** Config for the built-in RN renderer (pass instead of a component). */
13
+ export interface DefaultMarkdownRendererProps {
14
+ style?: MarkdownStyle;
15
+ animate?: boolean;
16
+ }
17
+ export type MarkdownRendererValue =
18
+ | NativeMarkdownRenderer
19
+ | DefaultMarkdownRendererProps;
20
+
21
+ /** A component is a function or an object carrying React's `$$typeof` (forwardRef/memo). */
22
+ export function isNativeComponentRenderer(
23
+ value: unknown,
24
+ ): value is NativeMarkdownRenderer {
25
+ if (typeof value === "function") return true;
26
+ return !!value && typeof value === "object" && "$$typeof" in value;
27
+ }
28
+
29
+ const Ctx = createContext<MarkdownRendererValue | undefined>(undefined);
30
+
31
+ export const MarkdownRendererProvider: React.FC<{
32
+ renderer?: MarkdownRendererValue;
33
+ children: React.ReactNode;
34
+ }> = ({ renderer, children }) => (
35
+ <Ctx.Provider value={renderer}>{children}</Ctx.Provider>
36
+ );
37
+
38
+ export function useMarkdownRenderer(): MarkdownRendererValue | undefined {
39
+ return useContext(Ctx);
40
+ }
@@ -22,12 +22,12 @@ const hoisted = vi.hoisted(() => {
22
22
  // rendering against a REAL useRenderToolCall lives in
23
23
  // CopilotChatToolCalls.test.tsx; here useRenderToolCall resolves no renderer so
24
24
  // tool calls fall through to the "Called: <name>" placeholder.
25
- vi.mock("@copilotkit/react-core/v2/headless", () => ({
25
+ vi.mock("@copilotkit/react-core/headless", () => ({
26
26
  useAgent: vi.fn(() => ({ agent: hoisted.mockAgent })),
27
27
  useRenderToolCall: vi.fn(() => () => null),
28
28
  }));
29
29
 
30
- vi.mock("@copilotkit/react-core/v2/context", () => ({
30
+ vi.mock("@copilotkit/react-core/context", () => ({
31
31
  useCopilotKit: vi.fn(() => ({
32
32
  copilotkit: { runAgent: hoisted.mockRunAgent },
33
33
  executingToolCallIds: hoisted.mockExecutingToolCallIds,
@@ -21,12 +21,12 @@ const hoisted = vi.hoisted(() => {
21
21
  // behavior. Tool-call rendering (which drives a REAL useRenderToolCall) lives
22
22
  // in CopilotChatToolCalls.test.tsx; here useRenderToolCall is a stub that
23
23
  // resolves no renderer, so tool calls fall through to the placeholder.
24
- vi.mock("@copilotkit/react-core/v2/headless", () => ({
24
+ vi.mock("@copilotkit/react-core/headless", () => ({
25
25
  useAgent: vi.fn(() => ({ agent: hoisted.mockAgent })),
26
26
  useRenderToolCall: vi.fn(() => () => null),
27
27
  }));
28
28
 
29
- vi.mock("@copilotkit/react-core/v2/context", () => ({
29
+ vi.mock("@copilotkit/react-core/context", () => ({
30
30
  useCopilotKit: vi.fn(() => ({
31
31
  copilotkit: { runAgent: hoisted.mockRunAgent },
32
32
  executingToolCallIds: new Set<string>(),
@@ -0,0 +1,59 @@
1
+ import React from "react";
2
+ import { render } from "@testing-library/react";
3
+ import { describe, it, expect, vi, beforeEach } from "vitest";
4
+
5
+ vi.mock("react-native", () => ({
6
+ StyleSheet: {
7
+ create: <T extends Record<string, any>>(styles: T): T => styles,
8
+ flatten: (style: any) => style,
9
+ },
10
+ View: "View",
11
+ Text: "Text",
12
+ }));
13
+
14
+ // Capture the props handed to the underlying streaming renderer so we can
15
+ // assert how CopilotMarkdown translates its public style map.
16
+ const capturedProps: any[] = [];
17
+ vi.mock("@copilotkit/markdown-renderer/react-native", () => ({
18
+ StreamingMarkdownRenderer: (props: any) => {
19
+ capturedProps.push(props);
20
+ return null;
21
+ },
22
+ defaultMarkdownStyles: { inlineCode: { color: "default" } },
23
+ }));
24
+
25
+ import { CopilotMarkdown } from "../Markdown";
26
+
27
+ describe("CopilotMarkdown back-compat `code` style alias", () => {
28
+ beforeEach(() => {
29
+ capturedProps.length = 0;
30
+ });
31
+
32
+ it("maps the back-compat `code` alias onto `inlineCode` (which the renderer reads)", () => {
33
+ render(
34
+ <CopilotMarkdown content={"`x`"} style={{ code: { color: "red" } }} />,
35
+ );
36
+ // Regression: the renderer reads `inlineCode`, not `code`. Without the
37
+ // alias translation, `style={{ code }}` silently lost inline-code styling.
38
+ expect(capturedProps[0].style.inlineCode).toEqual({ color: "red" });
39
+ // The alias key itself is not forwarded.
40
+ expect(capturedProps[0].style.code).toBeUndefined();
41
+ });
42
+
43
+ it("lets an explicit `inlineCode` win over the `code` alias", () => {
44
+ render(
45
+ <CopilotMarkdown
46
+ content={"`x`"}
47
+ style={{ code: { color: "red" }, inlineCode: { color: "blue" } }}
48
+ />,
49
+ );
50
+ expect(capturedProps[0].style.inlineCode).toEqual({ color: "blue" });
51
+ });
52
+
53
+ it("passes style through untouched when no `code` alias is present", () => {
54
+ render(
55
+ <CopilotMarkdown content={"`x`"} style={{ paragraph: { margin: 1 } }} />,
56
+ );
57
+ expect(capturedProps[0].style).toEqual({ paragraph: { margin: 1 } });
58
+ });
59
+ });
@@ -1,10 +1,7 @@
1
1
  import React from "react";
2
2
  import { render } from "@testing-library/react";
3
- import { describe, it, expect, vi, beforeEach } from "vitest";
3
+ import { describe, it, expect, vi } from "vitest";
4
4
 
5
- // ─── Mocks ────────────────────────────────────────────────────────────────────
6
-
7
- // Mock react-native since we're in jsdom
8
5
  vi.mock("react-native", () => ({
9
6
  StyleSheet: {
10
7
  create: <T extends Record<string, any>>(styles: T): T => styles,
@@ -14,93 +11,27 @@ vi.mock("react-native", () => ({
14
11
  Text: "Text",
15
12
  }));
16
13
 
17
- // Capture the props passed to StreamdownText
18
- let lastStreamdownProps: any = null;
19
-
20
- vi.mock("react-native-streamdown", () => ({
21
- StreamdownText: function MockStreamdownText(props: any) {
22
- lastStreamdownProps = props;
23
- return React.createElement(
24
- "div",
25
- { "data-testid": "markdown" },
26
- props.markdown,
27
- );
28
- },
29
- }));
30
-
31
- // Import after mocks
32
14
  import { CopilotMarkdown, defaultMarkdownStyles } from "../Markdown";
33
15
 
34
- // ─── Tests ────────────────────────────────────────────────────────────────────
35
-
36
- describe("CopilotMarkdown", () => {
37
- beforeEach(() => {
38
- lastStreamdownProps = null;
39
- });
40
-
16
+ describe("CopilotMarkdown (basic renderer)", () => {
41
17
  it("renders without crashing", () => {
42
18
  const { container } = render(<CopilotMarkdown content="Hello world" />);
43
19
  expect(container).toBeTruthy();
44
20
  });
45
21
 
46
- it("passes content as markdown prop to StreamdownText", () => {
47
- render(<CopilotMarkdown content="# Title" />);
48
- expect(lastStreamdownProps).not.toBeNull();
49
- expect(lastStreamdownProps.markdown).toBe("# Title");
50
- });
51
-
52
- it("uses default styles when no custom style is provided", () => {
53
- render(<CopilotMarkdown content="test" />);
54
- expect(lastStreamdownProps.markdownStyle).toBe(defaultMarkdownStyles);
55
- });
56
-
57
- it("merges custom styles with defaults", () => {
58
- const customStyle = { paragraph: { fontSize: 20, color: "#000" } };
59
- render(<CopilotMarkdown content="test" style={customStyle} />);
60
-
61
- // Custom should override the paragraph style
62
- expect(lastStreamdownProps.markdownStyle.paragraph).toEqual({
63
- fontSize: 20,
64
- color: "#000",
65
- });
66
- // Other defaults should still be present
67
- expect(lastStreamdownProps.markdownStyle.h1).toEqual(
68
- defaultMarkdownStyles.h1,
69
- );
70
- expect(lastStreamdownProps.markdownStyle.codeBlock).toEqual(
71
- defaultMarkdownStyles.codeBlock,
72
- );
22
+ it("renders text content", () => {
23
+ const { container } = render(<CopilotMarkdown content="# Title" />);
24
+ expect(container.textContent).toContain("Title");
73
25
  });
74
26
 
75
27
  it("renders safely with empty content", () => {
76
28
  const { container } = render(<CopilotMarkdown content="" />);
77
29
  expect(container).toBeTruthy();
78
- expect(lastStreamdownProps.markdown).toBe("");
79
30
  });
80
31
 
81
- it("enables streamingAnimation by default", () => {
82
- render(<CopilotMarkdown content="test" />);
83
- expect(lastStreamdownProps.streamingAnimation).toBe(true);
84
- });
85
-
86
- it("allows disabling streamingAnimation", () => {
87
- render(<CopilotMarkdown content="test" streamingAnimation={false} />);
88
- expect(lastStreamdownProps.streamingAnimation).toBe(false);
89
- });
90
- });
91
-
92
- describe("defaultMarkdownStyles", () => {
93
- it("exports a style object with expected keys", () => {
32
+ it("exports default styles with expected keys", () => {
94
33
  expect(defaultMarkdownStyles.paragraph).toBeDefined();
95
34
  expect(defaultMarkdownStyles.h1).toBeDefined();
96
- expect(defaultMarkdownStyles.h2).toBeDefined();
97
- expect(defaultMarkdownStyles.h3).toBeDefined();
98
- expect(defaultMarkdownStyles.strong).toBeDefined();
99
- expect(defaultMarkdownStyles.em).toBeDefined();
100
- expect(defaultMarkdownStyles.link).toBeDefined();
101
- expect(defaultMarkdownStyles.blockquote).toBeDefined();
102
- expect(defaultMarkdownStyles.code).toBeDefined();
103
35
  expect(defaultMarkdownStyles.codeBlock).toBeDefined();
104
- expect(defaultMarkdownStyles.list).toBeDefined();
105
36
  });
106
37
  });
@@ -1,6 +1,10 @@
1
1
  import React from "react";
2
2
  import { View, Text, StyleSheet, type ViewStyle } from "react-native";
3
3
  import { CopilotMarkdown } from "../Markdown";
4
+ import {
5
+ useMarkdownRenderer,
6
+ isNativeComponentRenderer,
7
+ } from "../MarkdownRendererContext";
4
8
  import { TypingIndicator } from "./TypingIndicator";
5
9
  import { formatTimestamp } from "./utils";
6
10
 
@@ -35,10 +39,27 @@ export function AssistantMessage({
35
39
  timestamp,
36
40
  style,
37
41
  }: AssistantMessageProps) {
42
+ // Honor a provider-level markdown renderer (CopilotKitProvider's
43
+ // `markdownRenderer` prop), falling back to the built-in CopilotMarkdown.
44
+ // This is the React Native arm of the slot -> provider -> built-in
45
+ // resolution order documented in the migration guide.
46
+ const provided = useMarkdownRenderer();
38
47
  return (
39
48
  <View style={[styles.container, style]}>
40
49
  <View style={styles.bubble}>
41
- {content ? <CopilotMarkdown content={content} /> : null}
50
+ {content ? (
51
+ isNativeComponentRenderer(provided) ? (
52
+ React.createElement(provided, { content, isStreaming: isLoading })
53
+ ) : provided ? (
54
+ <CopilotMarkdown
55
+ content={content}
56
+ style={provided.style}
57
+ streamingAnimation={provided.animate}
58
+ />
59
+ ) : (
60
+ <CopilotMarkdown content={content} />
61
+ )
62
+ ) : null}
42
63
  {isLoading ? <TypingIndicator /> : null}
43
64
  </View>
44
65
  {timestamp && (