@copilotkit/react-native 1.67.1 → 1.68.1

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 (56) hide show
  1. package/dist/chunk-C0xms8kb.cjs +34 -0
  2. package/dist/components/index.cjs +146 -33
  3. package/dist/components/index.cjs.map +1 -1
  4. package/dist/components/index.mjs +144 -32
  5. package/dist/components/index.mjs.map +1 -1
  6. package/dist/headless.cjs +40 -4
  7. package/dist/headless.d.cts +3 -3
  8. package/dist/headless.d.mts +3 -3
  9. package/dist/headless.mjs +5 -4
  10. package/dist/index.cjs +43 -7
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +3 -3
  13. package/dist/index.d.mts +4 -4
  14. package/dist/index.mjs +3 -4
  15. package/dist/index.mjs.map +1 -1
  16. package/dist/polyfills/encoding.cjs +1 -1
  17. package/dist/polyfills/streams.cjs +1 -1
  18. package/dist/useRenderTool-BBxnOOeJ.d.mts +161 -0
  19. package/dist/useRenderTool-BWo11PCL.d.cts +161 -0
  20. package/dist/{useRenderTool-DLavcsVB.cjs → useRenderTool-CKEPq7MQ.cjs} +20 -34
  21. package/dist/useRenderTool-CKEPq7MQ.cjs.map +1 -0
  22. package/dist/{useRenderTool-DUqpS97B.mjs → useRenderTool-l7M-I8rT.mjs} +18 -33
  23. package/dist/useRenderTool-l7M-I8rT.mjs.map +1 -0
  24. package/package.json +9 -6
  25. package/scripts/__tests__/fixtures/stray-react-dom-headless.js +16 -0
  26. package/scripts/__tests__/fixtures/tiny-headless.js +10 -0
  27. package/scripts/__tests__/fixtures/warning-headless.js +13 -0
  28. package/scripts/__tests__/measure-headless.test.mjs +275 -0
  29. package/scripts/measure-headless.mjs +310 -0
  30. package/src/CopilotKitProvider.tsx +1 -2
  31. package/src/__mocks__/test-copilotkit.tsx +116 -0
  32. package/src/__mocks__/tool-fixtures.ts +50 -0
  33. package/src/__tests__/headless-entry-surface.test.ts +464 -46
  34. package/src/__tests__/headless-integration.test.tsx +12 -1
  35. package/src/__tests__/headless-value-exports.test.ts +369 -0
  36. package/src/components/CopilotChat.tsx +218 -32
  37. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -126
  38. package/src/components/__tests__/CopilotChat.test.tsx +5 -49
  39. package/src/components/__tests__/CopilotChatToolCalls.test.tsx +468 -0
  40. package/src/headless.ts +36 -14
  41. package/src/hooks/__tests__/render-tool-call.integration.test.tsx +181 -0
  42. package/src/hooks/__tests__/useRenderTool.test.tsx +232 -267
  43. package/src/hooks/index.ts +1 -7
  44. package/src/hooks/render-tool-types.ts +55 -0
  45. package/src/hooks/useRenderTool.ts +29 -57
  46. package/src/index.ts +13 -5
  47. package/dist/RenderToolContext-CBNKcJqz.cjs +0 -126
  48. package/dist/RenderToolContext-CBNKcJqz.cjs.map +0 -1
  49. package/dist/RenderToolContext-D8M-JhD1.mjs +0 -75
  50. package/dist/RenderToolContext-D8M-JhD1.mjs.map +0 -1
  51. package/dist/useRenderTool-9YDIfZ0D.d.mts +0 -164
  52. package/dist/useRenderTool-BugVTDoM.d.cts +0 -164
  53. package/dist/useRenderTool-DLavcsVB.cjs.map +0 -1
  54. package/dist/useRenderTool-DUqpS97B.mjs.map +0 -1
  55. package/src/hooks/RenderToolContext.tsx +0 -150
  56. package/src/hooks/__tests__/RenderToolContext.test.tsx +0 -264
@@ -1,334 +1,299 @@
1
1
  import React from "react";
2
- import { render } from "@testing-library/react";
3
- import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
4
-
5
- // ─── Mocks ────────────────────────────────────────────────────────────────────
6
-
7
- const hoisted = vi.hoisted(() => {
8
- const _React = require("react");
9
- return {
10
- RealContext: _React.createContext(null),
11
- mockAddTool: vi.fn(),
12
- mockRemoveTool: vi.fn(),
13
- mockGetTool: vi.fn(() => undefined),
14
- mockAddHookRenderToolCall: vi.fn(),
15
- mockSubscribe: vi.fn(() => ({ unsubscribe: vi.fn() })),
16
- };
17
- });
18
-
19
- vi.mock("@copilotkit/react-core/v2/headless", () => {
20
- return {
21
- useFrontendTool: vi.fn((_tool: any, _deps?: any) => {
22
- // Require context — mirrors real behavior
23
- const ctx = require("react").useContext(hoisted.RealContext);
24
- if (!ctx) {
25
- throw new Error("useCopilotKit must be used within CopilotKitProvider");
26
- }
27
- // Simulate addTool call
28
- hoisted.mockAddTool(_tool);
29
- }),
30
- CopilotKitCoreReact: function CopilotKitCoreReact() {},
31
- CopilotChatConfigurationProvider: ({ children }: any) => children,
32
- useCopilotChatConfiguration: () => null,
33
- CopilotChatDefaultLabels: {},
34
- };
35
- });
2
+ import { act, render, waitFor } from "@testing-library/react";
3
+ import { describe, it, expect, vi } from "vitest";
4
+ import { z } from "zod";
36
5
 
37
- vi.mock("@copilotkit/react-core/v2/context", () => {
38
- const _React = require("react");
39
- return {
40
- CopilotKitContext: hoisted.RealContext,
41
- LicenseContext: _React.createContext({
42
- status: null,
43
- license: null,
44
- checkFeature: () => true,
45
- getLimit: () => null,
46
- }),
47
- useCopilotKit: () => {
48
- const ctx = _React.useContext(hoisted.RealContext);
49
- if (!ctx) {
50
- throw new Error("useCopilotKit must be used within CopilotKitProvider");
51
- }
52
- return ctx;
53
- },
54
- useLicenseContext: () => ({
55
- status: null,
56
- license: null,
57
- checkFeature: () => true,
58
- getLimit: () => null,
59
- }),
60
- };
61
- });
62
-
63
- vi.mock("@copilotkit/shared", () => ({
64
- createLicenseContextValue: () => ({
65
- status: null,
66
- license: null,
67
- checkFeature: () => true,
68
- getLimit: () => null,
69
- }),
70
- }));
71
-
72
- // Import after mocks
73
6
  import { useRenderTool } from "../useRenderTool";
74
- import {
75
- RenderToolProvider,
76
- useRenderToolRegistry,
77
- } from "../RenderToolContext";
78
-
79
- // Minimal wrapper that provides both CopilotKit context and RenderToolProvider
80
- function TestProviders({ children }: { children: React.ReactNode }) {
81
- const mockCtx = {
82
- copilotkit: {
83
- addTool: hoisted.mockAddTool,
84
- removeTool: hoisted.mockRemoveTool,
85
- getTool: hoisted.mockGetTool,
86
- addHookRenderToolCall: hoisted.mockAddHookRenderToolCall,
87
- subscribe: hoisted.mockSubscribe,
88
- },
89
- executingToolCallIds: new Set<string>(),
90
- };
91
-
92
- return (
93
- <hoisted.RealContext.Provider value={mockCtx as any}>
94
- <RenderToolProvider>{children}</RenderToolProvider>
95
- </hoisted.RealContext.Provider>
96
- );
7
+ import { useCopilotKit, useRenderToolCall } from "../../headless";
8
+ import type { CopilotKitContextValue } from "../../headless";
9
+ import { TestCopilotKit } from "../../__mocks__/test-copilotkit";
10
+
11
+ /**
12
+ * `useRenderTool` is a THIN FORWARDER: every field it accepts is handed to
13
+ * react-core's `useFrontendTool`, and every behaviour it claims is really that
14
+ * hook's behaviour. So this suite must not mock `useFrontendTool` — an earlier
15
+ * version did, and the double it substituted only modelled `name`/`render`.
16
+ * Deleting the `deps`, `handler` and `agentId` forwarding from the hook left
17
+ * that suite fully green: it could not detect a regression in any of the three
18
+ * things the hook exists to forward.
19
+ *
20
+ * Everything here therefore drives a REAL `CopilotKitCoreReact` through
21
+ * `TestCopilotKit` (as the sibling `render-tool-call.integration.test.tsx`
22
+ * does) and asserts on core's own observable behaviour — what `runTool`
23
+ * executes, what `getTool` resolves, what actually paints — rather than on a
24
+ * mock's call arguments.
25
+ */
26
+
27
+ type Core = CopilotKitContextValue["copilotkit"];
28
+
29
+ /** Publishes the live core instance so a test can drive it directly. */
30
+ function CaptureCore({
31
+ into,
32
+ }: {
33
+ into: { current: Core | null };
34
+ }): React.ReactElement | null {
35
+ const { copilotkit } = useCopilotKit();
36
+ into.current = copilotkit;
37
+ return null;
97
38
  }
98
39
 
99
- // ─── Tests ────────────────────────────────────────────────────────────────────
40
+ // ─── Registration basics ──────────────────────────────────────────────────────
100
41
 
101
42
  describe("useRenderTool", () => {
102
- beforeEach(() => {
103
- vi.clearAllMocks();
104
- });
105
-
106
- afterEach(() => {
107
- vi.restoreAllMocks();
108
- });
43
+ it("registers the tool with react-core, name/description/schema intact", () => {
44
+ const coreRef: { current: Core | null } = { current: null };
45
+ const schema = z.object({ query: z.string() });
109
46
 
110
- it("registers a tool via useFrontendTool", () => {
111
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
112
-
113
- function TestComponent() {
47
+ function Probe() {
114
48
  useRenderTool({
115
- name: "test-render-tool",
116
- description: "A tool with render",
117
- parameters: mockSchema as any,
118
- render: ({ args, status }) =>
119
- React.createElement("View", null, `${status}`),
120
- handler: async () => "done",
49
+ name: "searchLedger",
50
+ description: "Search the ledger",
51
+ parameters: schema,
52
+ render: () => null,
121
53
  });
122
54
  return null;
123
55
  }
124
56
 
125
57
  render(
126
- <TestProviders>
127
- <TestComponent />
128
- </TestProviders>,
58
+ <TestCopilotKit messages={[]}>
59
+ <CaptureCore into={coreRef} />
60
+ <Probe />
61
+ </TestCopilotKit>,
129
62
  );
130
63
 
131
- // useFrontendTool should have been called with the tool config
132
- expect(hoisted.mockAddTool).toHaveBeenCalledWith(
133
- expect.objectContaining({
134
- name: "test-render-tool",
135
- description: "A tool with render",
136
- }),
137
- );
64
+ const tool = coreRef.current!.getTool({ toolName: "searchLedger" });
65
+ expect(tool).toBeDefined();
66
+ expect(tool!.description).toBe("Search the ledger");
67
+ expect(tool!.parameters).toBe(schema);
138
68
  });
69
+ });
139
70
 
140
- it("stores render function in the registry", () => {
141
- let registry: Map<string, any> | null = null;
142
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
143
-
144
- const renderFn = ({ args, status }: any) =>
145
- React.createElement("View", null, `${status}`);
71
+ describe("useRenderTool registry target", () => {
72
+ it("registers its renderer into core's renderToolCalls, not a local map", () => {
73
+ // The whole point of the convergence: one registry. react-core's
74
+ // useRenderToolCall reads copilotkit.renderToolCalls, so a renderer that
75
+ // does not land there renders nowhere outside RN's own chat.
76
+ const coreRef: { current: Core | null } = { current: null };
146
77
 
147
- function ToolRegistrar() {
78
+ function Probe() {
148
79
  useRenderTool({
149
- name: "weather-tool",
80
+ name: "showWeather",
150
81
  description: "Show weather",
151
- parameters: mockSchema as any,
152
- render: renderFn,
82
+ parameters: z.object({ city: z.string() }),
83
+ render: () => null,
153
84
  });
154
85
  return null;
155
86
  }
156
87
 
157
- function RegistryReader() {
158
- registry = useRenderToolRegistry();
159
- return null;
160
- }
161
-
162
88
  render(
163
- <TestProviders>
164
- <ToolRegistrar />
165
- <RegistryReader />
166
- </TestProviders>,
89
+ <TestCopilotKit messages={[]}>
90
+ <CaptureCore into={coreRef} />
91
+ <Probe />
92
+ </TestCopilotKit>,
167
93
  );
168
94
 
169
- expect(registry).not.toBeNull();
170
- expect(registry!.has("weather-tool")).toBe(true);
171
- // The stored function is a stable wrapper, not the original
172
- expect(typeof registry!.get("weather-tool")).toBe("function");
95
+ expect(coreRef.current!.renderToolCalls.map((r) => r.name)).toContain(
96
+ "showWeather",
97
+ );
173
98
  });
99
+ });
174
100
 
175
- it("render function in registry produces expected output", () => {
176
- let registry: Map<string, any> | null = null;
177
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
101
+ // ─── Forwarded concern: handler ───────────────────────────────────────────────
102
+
103
+ describe("useRenderTool forwards handler", () => {
104
+ it("runs the caller's handler when core executes the tool", async () => {
105
+ // Observed through core's real execution path (`runTool` → the same
106
+ // `executeToolHandler` an LLM-driven turn uses), not by inspecting the
107
+ // registered object: a handler that is registered but never reached is
108
+ // indistinguishable from a missing one to the app.
109
+ const coreRef: { current: Core | null } = { current: null };
110
+ const handler = vi.fn(
111
+ async (args: { city: string }) => `sunny in ${args.city}`,
112
+ );
178
113
 
179
- function ToolRegistrar() {
114
+ function Probe() {
180
115
  useRenderTool({
181
- name: "greeting-tool",
182
- description: "Greet someone",
183
- parameters: mockSchema as any,
184
- render: ({ args, status }) =>
185
- React.createElement("Text", null, `Hello ${status}`),
116
+ name: "getWeather",
117
+ description: "Get weather",
118
+ parameters: z.object({ city: z.string() }),
119
+ render: () => null,
120
+ handler,
186
121
  });
187
122
  return null;
188
123
  }
189
124
 
190
- function RegistryReader() {
191
- registry = useRenderToolRegistry();
192
- return null;
193
- }
194
-
195
125
  render(
196
- <TestProviders>
197
- <ToolRegistrar />
198
- <RegistryReader />
199
- </TestProviders>,
126
+ <TestCopilotKit messages={[]}>
127
+ <CaptureCore into={coreRef} />
128
+ <Probe />
129
+ </TestCopilotKit>,
200
130
  );
201
131
 
202
- const renderFn = registry!.get("greeting-tool");
203
- const element = renderFn({ args: {}, status: "executing" });
204
- expect(element).not.toBeNull();
205
- expect(element.type).toBe("Text");
206
- expect(element.props.children).toBe("Hello executing");
132
+ let outcome: Awaited<ReturnType<Core["runTool"]>> | undefined;
133
+ await act(async () => {
134
+ outcome = await coreRef.current!.runTool({
135
+ name: "getWeather",
136
+ parameters: { city: "Berlin" },
137
+ });
138
+ });
139
+
140
+ expect(handler).toHaveBeenCalledTimes(1);
141
+ expect(handler.mock.calls[0]![0]).toEqual({ city: "Berlin" });
142
+ // Core writes the handler's return value into the tool result. Drop the
143
+ // forwarding and this is the empty string a handler-less tool produces.
144
+ expect(outcome!.result).toBe("sunny in Berlin");
207
145
  });
146
+ });
147
+
148
+ // ─── Forwarded concern: agentId ───────────────────────────────────────────────
208
149
 
209
- it("throws when useRenderTool is called outside RenderToolProvider", () => {
210
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
150
+ describe("useRenderTool forwards agentId", () => {
151
+ it("scopes the tool and its renderer to that agent, not globally", () => {
152
+ const coreRef: { current: Core | null } = { current: null };
211
153
 
212
- function TestComponent() {
154
+ function Probe() {
213
155
  useRenderTool({
214
- name: "orphan-tool",
215
- description: "No provider",
216
- parameters: mockSchema as any,
217
- render: () => React.createElement("View"),
156
+ name: "escalate",
157
+ description: "Escalate to a human",
158
+ parameters: z.object({ reason: z.string() }),
159
+ render: () => null,
160
+ agentId: "support",
218
161
  });
219
162
  return null;
220
163
  }
221
164
 
222
- // Need CopilotKit context but no RenderToolProvider
223
- const spy = vi.spyOn(console, "error").mockImplementation(() => {});
224
-
225
- expect(() => {
226
- render(
227
- <hoisted.RealContext.Provider
228
- value={{ copilotkit: {}, executingToolCallIds: new Set() } as any}
229
- >
230
- <TestComponent />
231
- </hoisted.RealContext.Provider>,
232
- );
233
- }).toThrow("useRenderTool must be used within a RenderToolProvider");
234
-
235
- spy.mockRestore();
236
- });
165
+ render(
166
+ <TestCopilotKit messages={[]}>
167
+ <CaptureCore into={coreRef} />
168
+ <Probe />
169
+ </TestCopilotKit>,
170
+ );
237
171
 
238
- it("throws when useRenderToolRegistry is called outside RenderToolProvider", () => {
239
- function TestComponent() {
240
- useRenderToolRegistry();
241
- return null;
242
- }
172
+ const core = coreRef.current!;
243
173
 
244
- const spy = vi.spyOn(console, "error").mockImplementation(() => {});
174
+ // Reachable for the agent it was scoped to …
175
+ expect(
176
+ core.getTool({ toolName: "escalate", agentId: "support" }),
177
+ ).toBeDefined();
245
178
 
246
- expect(() => {
247
- render(<TestComponent />);
248
- }).toThrow(
249
- "useRenderToolRegistry must be used within a RenderToolProvider",
250
- );
179
+ // … and NOT as a global tool. This is the assertion that bites: core's
180
+ // `getTool` falls back to global tools only when `tool.agentId` is unset,
181
+ // so a dropped agentId turns a scoped tool into one every agent can call.
182
+ expect(core.getTool({ toolName: "escalate" })).toBeUndefined();
251
183
 
252
- spy.mockRestore();
184
+ // The renderer half is keyed `${agentId}:${name}`, which is what lets a
185
+ // scoped renderer coexist with a global one of the same name.
186
+ const renderer = core.renderToolCalls.find((r) => r.name === "escalate");
187
+ expect(renderer).toBeDefined();
188
+ expect(renderer!.agentId).toBe("support");
253
189
  });
190
+ });
254
191
 
255
- it("unregisters the render function on unmount", () => {
256
- let registry: Map<string, any> | null = null;
257
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
258
-
259
- function ToolRegistrar() {
260
- useRenderTool({
261
- name: "ephemeral-tool",
262
- description: "Will unmount",
263
- parameters: mockSchema as any,
264
- render: () => React.createElement("View"),
265
- });
266
- return null;
267
- }
268
-
269
- function RegistryReader() {
270
- registry = useRenderToolRegistry();
271
- return null;
272
- }
192
+ // ─── Forwarded concern: deps ──────────────────────────────────────────────────
193
+
194
+ /**
195
+ * Paints whatever `label` the renderer captured at registration time, through
196
+ * react-core's real `useRenderToolCall`. The render closure is deliberately
197
+ * stale-able: `useFrontendTool` captures it in an effect, so the painted text
198
+ * only changes when that effect re-runs.
199
+ */
200
+ function LabelProbe({
201
+ label,
202
+ deps,
203
+ }: {
204
+ label: string;
205
+ deps: ReadonlyArray<unknown>;
206
+ }) {
207
+ useRenderTool(
208
+ {
209
+ name: "showLabel",
210
+ description: "Show a label",
211
+ parameters: z.object({}),
212
+ render: () => <>{label}</>,
213
+ },
214
+ deps,
215
+ );
216
+ const renderToolCall = useRenderToolCall();
217
+ return (
218
+ <>
219
+ {renderToolCall({
220
+ toolCall: {
221
+ id: "tc-label",
222
+ type: "function",
223
+ function: { name: "showLabel", arguments: "{}" },
224
+ },
225
+ })}
226
+ </>
227
+ );
228
+ }
273
229
 
274
- const { rerender } = render(
275
- <TestProviders>
276
- <ToolRegistrar />
277
- <RegistryReader />
278
- </TestProviders>,
230
+ /**
231
+ * A dep array holding a FRESH function identity on every call. `JSON.stringify`
232
+ * flattens it to the constant `"[null]"`, so `useFrontendTool` can never see it
233
+ * change however often the caller re-renders.
234
+ */
235
+ const freshNonSerialisableDeps = (): ReadonlyArray<unknown> => [
236
+ () => "callback",
237
+ ];
238
+
239
+ describe("useRenderTool forwards deps", () => {
240
+ it("re-registers the render closure when a dep changes", async () => {
241
+ // The hook's own JSDoc promises exactly this: values a render closes over
242
+ // must be passed in `deps` or the chat keeps painting the stale closure.
243
+ // Without the forwarding, `useFrontendTool` sees an empty dep array, its
244
+ // effect never re-runs, and "before" is painted forever.
245
+ const { container, rerender } = render(
246
+ <TestCopilotKit messages={[]}>
247
+ <LabelProbe label="before" deps={["before"]} />
248
+ </TestCopilotKit>,
279
249
  );
280
250
 
281
- expect(registry!.has("ephemeral-tool")).toBe(true);
251
+ await waitFor(() => expect(container.textContent).toBe("before"));
282
252
 
283
- // Re-render without the ToolRegistrar
284
253
  rerender(
285
- <TestProviders>
286
- <RegistryReader />
287
- </TestProviders>,
254
+ <TestCopilotKit messages={[]}>
255
+ <LabelProbe label="after" deps={["after"]} />
256
+ </TestCopilotKit>,
288
257
  );
289
258
 
290
- expect(registry!.has("ephemeral-tool")).toBe(false);
259
+ await waitFor(() => expect(container.textContent).toBe("after"));
291
260
  });
292
261
 
293
- it("supports multiple tools registered simultaneously", () => {
294
- let registry: Map<string, any> | null = null;
295
- const mockSchema = { "~standard": { vendor: "test", version: 1 } };
296
-
297
- function ToolA() {
298
- useRenderTool({
299
- name: "tool-a",
300
- description: "Tool A",
301
- parameters: mockSchema as any,
302
- render: () => React.createElement("View"),
303
- });
304
- return null;
305
- }
306
-
307
- function ToolB() {
308
- useRenderTool({
309
- name: "tool-b",
310
- description: "Tool B",
311
- parameters: mockSchema as any,
312
- render: () => React.createElement("View"),
313
- });
314
- return null;
315
- }
262
+ it("PINS the limitation: a non-serialisable dep can never re-register", async () => {
263
+ // `useFrontendTool` compares deps with `JSON.stringify(extraDeps)`, so a
264
+ // function / Map / Set / private-field class instance collapses to a
265
+ // CONSTANT and cannot trigger re-registration however often its identity
266
+ // changes. This is a documented sharp edge, not a bug to fix here — it is
267
+ // pinned so that changing the comparator (e.g. to reference equality) fails
268
+ // loudly and forces the hook's JSDoc to be updated with it.
269
+ const coreRef: { current: Core | null } = { current: null };
270
+
271
+ const { container, rerender } = render(
272
+ <TestCopilotKit messages={[]}>
273
+ <CaptureCore into={coreRef} />
274
+ <LabelProbe label="before" deps={freshNonSerialisableDeps()} />
275
+ </TestCopilotKit>,
276
+ );
316
277
 
317
- function RegistryReader() {
318
- registry = useRenderToolRegistry();
319
- return null;
320
- }
278
+ await waitFor(() => expect(container.textContent).toBe("before"));
279
+ const registeredBefore = coreRef.current!.renderToolCalls.find(
280
+ (r) => r.name === "showLabel",
281
+ )!.render;
321
282
 
322
- render(
323
- <TestProviders>
324
- <ToolA />
325
- <ToolB />
326
- <RegistryReader />
327
- </TestProviders>,
283
+ rerender(
284
+ <TestCopilotKit messages={[]}>
285
+ <CaptureCore into={coreRef} />
286
+ <LabelProbe label="after" deps={freshNonSerialisableDeps()} />
287
+ </TestCopilotKit>,
328
288
  );
329
-
330
- expect(registry!.has("tool-a")).toBe(true);
331
- expect(registry!.has("tool-b")).toBe(true);
332
- expect(registry!.size).toBe(2);
289
+ // Let any pending effect + subscriber notification settle, so this asserts
290
+ // "nothing happened" rather than "nothing has happened yet".
291
+ await act(async () => {});
292
+
293
+ expect(
294
+ coreRef.current!.renderToolCalls.find((r) => r.name === "showLabel")!
295
+ .render,
296
+ ).toBe(registeredBefore);
297
+ expect(container.textContent).toBe("before");
333
298
  });
334
299
  });
@@ -1,9 +1,3 @@
1
1
  export { useRenderTool } from "./useRenderTool";
2
2
  export type { UseRenderToolOptions } from "./useRenderTool";
3
-
4
- export { RenderToolProvider, useRenderToolRegistry } from "./RenderToolContext";
5
- export type {
6
- RenderToolProps,
7
- RenderToolFunction,
8
- RenderToolRegistry,
9
- } from "./RenderToolContext";
3
+ export type { RenderToolProps, RenderToolFunction } from "./render-tool-types";
@@ -0,0 +1,55 @@
1
+ import type React from "react";
2
+ import type { ReactToolCallRenderer } from "@copilotkit/react-core/v2/headless";
3
+
4
+ /**
5
+ * Props a React Native render function receives for a tool call.
6
+ *
7
+ * DERIVED from react-core's canonical `ReactToolCallRenderer` contract rather
8
+ * than declared separately. This is deliberate: RN previously declared its own
9
+ * shape and drifted from that contract — RN's `status` was a two-member
10
+ * `"executing" | "complete"` union with no in-progress state, RN omitted
11
+ * `name`/`toolCallId` entirely, and RN's `args` was unconditionally the full
12
+ * `T`, promising complete arguments even before they had finished streaming.
13
+ *
14
+ * What the derivation buys, precisely: RN's props cannot drift from
15
+ * `ReactToolCallRenderer` — the contract every registered renderer is
16
+ * actually invoked against, and the type this alias reads through
17
+ * `React.ComponentProps`. Change that contract and RN's public type changes
18
+ * with it, so `check-types` names every RN renderer the change breaks.
19
+ *
20
+ * What it does NOT buy: parity with the type react-core *publicly exports*
21
+ * under this same name. Web's `RenderToolProps<S>`
22
+ * (react-core `src/v2/hooks/use-render-tool.tsx`) is a separate union, generic
23
+ * over a schema rather than over the parsed args, carrying arguments under
24
+ * `parameters` (not `args`) and declaring `status` as the string literals
25
+ * `"inProgress"` / `"executing"` / `"complete"` rather than as `ToolCallStatus`
26
+ * members. Both divergences are live today and nothing type-checks them shut:
27
+ * the two types share no structural relation, and the one place they meet —
28
+ * react-core's own bridge, which spreads the enum-typed renderer props into
29
+ * the literal-typed slot — compiles because a string-enum member is
30
+ * assignable to its own literal type (not the reverse). Web's `status` is a
31
+ * widening of the canonical contract, not a derivation from it. RN's entry
32
+ * point also re-exports web's three `RenderTool*Props` arms, so both shapes
33
+ * ship under confusingly similar names.
34
+ *
35
+ * `status` is the discriminant of a three-arm union, typed as the
36
+ * `ToolCallStatus` enum (`@copilotkit/core`), so narrow with the enum members
37
+ * rather than the raw strings: `args` is `Partial<T>` only on
38
+ * `ToolCallStatus.InProgress`, and `result` is a string only on
39
+ * `ToolCallStatus.Complete`.
40
+ */
41
+ export type RenderToolProps<T = Record<string, unknown>> = React.ComponentProps<
42
+ ReactToolCallRenderer<T>["render"]
43
+ >;
44
+
45
+ /**
46
+ * A render function returning a React Native element.
47
+ *
48
+ * This is the one place RN legitimately narrows `ReactToolCallRenderer`:
49
+ * `FlatList`'s `renderItem` cannot render strings or portals, so RN requires
50
+ * `ReactElement | null` where that contract permits any `ReactNode`. The PROPS
51
+ * come from it unchanged; only the return type is platform-specific.
52
+ */
53
+ export type RenderToolFunction<T = Record<string, unknown>> = (
54
+ props: RenderToolProps<T>,
55
+ ) => React.ReactElement | null;