@copilotkit/react-native 1.67.0 → 1.68.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.
- package/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/components/index.cjs +146 -33
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.mjs +144 -32
- package/dist/components/index.mjs.map +1 -1
- package/dist/headless.cjs +40 -4
- package/dist/headless.d.cts +3 -3
- package/dist/headless.d.mts +3 -3
- package/dist/headless.mjs +5 -4
- package/dist/index.cjs +43 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -4
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/dist/useRenderTool-BBxnOOeJ.d.mts +161 -0
- package/dist/useRenderTool-BWo11PCL.d.cts +161 -0
- package/dist/{useRenderTool-DLavcsVB.cjs → useRenderTool-CKEPq7MQ.cjs} +20 -34
- package/dist/useRenderTool-CKEPq7MQ.cjs.map +1 -0
- package/dist/{useRenderTool-DUqpS97B.mjs → useRenderTool-l7M-I8rT.mjs} +18 -33
- package/dist/useRenderTool-l7M-I8rT.mjs.map +1 -0
- package/package.json +9 -6
- package/scripts/__tests__/fixtures/stray-react-dom-headless.js +16 -0
- package/scripts/__tests__/fixtures/tiny-headless.js +10 -0
- package/scripts/__tests__/fixtures/warning-headless.js +13 -0
- package/scripts/__tests__/measure-headless.test.mjs +275 -0
- package/scripts/measure-headless.mjs +310 -0
- package/src/CopilotKitProvider.tsx +1 -2
- package/src/__mocks__/test-copilotkit.tsx +116 -0
- package/src/__mocks__/tool-fixtures.ts +50 -0
- package/src/__tests__/headless-entry-surface.test.ts +464 -46
- package/src/__tests__/headless-integration.test.tsx +12 -1
- package/src/__tests__/headless-value-exports.test.ts +369 -0
- package/src/components/CopilotChat.tsx +218 -32
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -126
- package/src/components/__tests__/CopilotChat.test.tsx +5 -49
- package/src/components/__tests__/CopilotChatToolCalls.test.tsx +468 -0
- package/src/headless.ts +36 -14
- package/src/hooks/__tests__/render-tool-call.integration.test.tsx +181 -0
- package/src/hooks/__tests__/useRenderTool.test.tsx +232 -267
- package/src/hooks/index.ts +1 -7
- package/src/hooks/render-tool-types.ts +55 -0
- package/src/hooks/useRenderTool.ts +29 -57
- package/src/index.ts +13 -5
- package/dist/RenderToolContext-CBNKcJqz.cjs +0 -126
- package/dist/RenderToolContext-CBNKcJqz.cjs.map +0 -1
- package/dist/RenderToolContext-D8M-JhD1.mjs +0 -75
- package/dist/RenderToolContext-D8M-JhD1.mjs.map +0 -1
- package/dist/useRenderTool-9YDIfZ0D.d.mts +0 -164
- package/dist/useRenderTool-BugVTDoM.d.cts +0 -164
- package/dist/useRenderTool-DLavcsVB.cjs.map +0 -1
- package/dist/useRenderTool-DUqpS97B.mjs.map +0 -1
- package/src/hooks/RenderToolContext.tsx +0 -150
- 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
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
// ───
|
|
40
|
+
// ─── Registration basics ──────────────────────────────────────────────────────
|
|
100
41
|
|
|
101
42
|
describe("useRenderTool", () => {
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
111
|
-
const mockSchema = { "~standard": { vendor: "test", version: 1 } };
|
|
112
|
-
|
|
113
|
-
function TestComponent() {
|
|
47
|
+
function Probe() {
|
|
114
48
|
useRenderTool({
|
|
115
|
-
name: "
|
|
116
|
-
description: "
|
|
117
|
-
parameters:
|
|
118
|
-
render: (
|
|
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
|
-
<
|
|
127
|
-
<
|
|
128
|
-
|
|
58
|
+
<TestCopilotKit messages={[]}>
|
|
59
|
+
<CaptureCore into={coreRef} />
|
|
60
|
+
<Probe />
|
|
61
|
+
</TestCopilotKit>,
|
|
129
62
|
);
|
|
130
63
|
|
|
131
|
-
|
|
132
|
-
expect(
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
|
78
|
+
function Probe() {
|
|
148
79
|
useRenderTool({
|
|
149
|
-
name: "
|
|
80
|
+
name: "showWeather",
|
|
150
81
|
description: "Show weather",
|
|
151
|
-
parameters:
|
|
152
|
-
render:
|
|
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
|
-
<
|
|
164
|
-
<
|
|
165
|
-
<
|
|
166
|
-
</
|
|
89
|
+
<TestCopilotKit messages={[]}>
|
|
90
|
+
<CaptureCore into={coreRef} />
|
|
91
|
+
<Probe />
|
|
92
|
+
</TestCopilotKit>,
|
|
167
93
|
);
|
|
168
94
|
|
|
169
|
-
expect(
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
|
114
|
+
function Probe() {
|
|
180
115
|
useRenderTool({
|
|
181
|
-
name: "
|
|
182
|
-
description: "
|
|
183
|
-
parameters:
|
|
184
|
-
render: (
|
|
185
|
-
|
|
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
|
-
<
|
|
197
|
-
<
|
|
198
|
-
<
|
|
199
|
-
</
|
|
126
|
+
<TestCopilotKit messages={[]}>
|
|
127
|
+
<CaptureCore into={coreRef} />
|
|
128
|
+
<Probe />
|
|
129
|
+
</TestCopilotKit>,
|
|
200
130
|
);
|
|
201
131
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
210
|
-
|
|
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
|
|
154
|
+
function Probe() {
|
|
213
155
|
useRenderTool({
|
|
214
|
-
name: "
|
|
215
|
-
description: "
|
|
216
|
-
parameters:
|
|
217
|
-
render: () =>
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
239
|
-
function TestComponent() {
|
|
240
|
-
useRenderToolRegistry();
|
|
241
|
-
return null;
|
|
242
|
-
}
|
|
172
|
+
const core = coreRef.current!;
|
|
243
173
|
|
|
244
|
-
|
|
174
|
+
// Reachable for the agent it was scoped to …
|
|
175
|
+
expect(
|
|
176
|
+
core.getTool({ toolName: "escalate", agentId: "support" }),
|
|
177
|
+
).toBeDefined();
|
|
245
178
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
-
|
|
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
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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
|
-
|
|
251
|
+
await waitFor(() => expect(container.textContent).toBe("before"));
|
|
282
252
|
|
|
283
|
-
// Re-render without the ToolRegistrar
|
|
284
253
|
rerender(
|
|
285
|
-
<
|
|
286
|
-
<
|
|
287
|
-
</
|
|
254
|
+
<TestCopilotKit messages={[]}>
|
|
255
|
+
<LabelProbe label="after" deps={["after"]} />
|
|
256
|
+
</TestCopilotKit>,
|
|
288
257
|
);
|
|
289
258
|
|
|
290
|
-
|
|
259
|
+
await waitFor(() => expect(container.textContent).toBe("after"));
|
|
291
260
|
});
|
|
292
261
|
|
|
293
|
-
it("
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
|
|
323
|
-
<
|
|
324
|
-
<
|
|
325
|
-
<
|
|
326
|
-
|
|
327
|
-
</TestProviders>,
|
|
283
|
+
rerender(
|
|
284
|
+
<TestCopilotKit messages={[]}>
|
|
285
|
+
<CaptureCore into={coreRef} />
|
|
286
|
+
<LabelProbe label="after" deps={freshNonSerialisableDeps()} />
|
|
287
|
+
</TestCopilotKit>,
|
|
328
288
|
);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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
|
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -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;
|