@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.
- 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
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, screen, act } from "@testing-library/react";
|
|
3
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
6
|
+
|
|
7
|
+
// Local FlatList that actually invokes renderItem — the shared stub in
|
|
8
|
+
// src/__mocks__/react-native.ts passes it through as an inert prop, so the
|
|
9
|
+
// tool-call branch would never execute. Mirrors CopilotChat.test.tsx:64.
|
|
10
|
+
vi.mock("react-native", async () => {
|
|
11
|
+
const actual = await vi.importActual<any>("../../__mocks__/react-native");
|
|
12
|
+
return {
|
|
13
|
+
...actual,
|
|
14
|
+
FlatList: ({ data, renderItem, ListEmptyComponent, keyExtractor }: any) => {
|
|
15
|
+
if (!data?.length)
|
|
16
|
+
return React.createElement(
|
|
17
|
+
"div",
|
|
18
|
+
{ "data-testid": "flatlist" },
|
|
19
|
+
ListEmptyComponent,
|
|
20
|
+
);
|
|
21
|
+
return React.createElement(
|
|
22
|
+
"div",
|
|
23
|
+
{ "data-testid": "flatlist" },
|
|
24
|
+
data.map((item: any, index: number) =>
|
|
25
|
+
React.createElement(
|
|
26
|
+
"div",
|
|
27
|
+
{ key: keyExtractor?.(item) ?? index },
|
|
28
|
+
renderItem({ item, index, separators: {} }),
|
|
29
|
+
),
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
import { CopilotChat } from "../CopilotChat";
|
|
37
|
+
import { useRenderTool } from "../../hooks/useRenderTool";
|
|
38
|
+
import { TestCopilotKit } from "../../__mocks__/test-copilotkit";
|
|
39
|
+
import { assistantToolCall, toolMessage } from "../../__mocks__/tool-fixtures";
|
|
40
|
+
|
|
41
|
+
// Reports status + args only. Assertions about the RESULT must NOT use this one:
|
|
42
|
+
// its output cannot distinguish "the correlated result reached the renderer" from
|
|
43
|
+
// "status flipped for some other reason", which is how a corrupted tool-message
|
|
44
|
+
// content passed unnoticed. Use `ReportRegistrar` below for those.
|
|
45
|
+
function Registrar() {
|
|
46
|
+
useRenderTool({
|
|
47
|
+
name: "showPlaces",
|
|
48
|
+
description: "Show places",
|
|
49
|
+
parameters: z.object({ title: z.string() }),
|
|
50
|
+
// Tolerates partial props — that is the contract while streaming.
|
|
51
|
+
render: ({ args, status }) => (
|
|
52
|
+
<div data-testid="places">{`${status}:${(args as { title?: string }).title ?? ""}`}</div>
|
|
53
|
+
),
|
|
54
|
+
});
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Reports status, args AND result together, so one assertion observes the whole
|
|
59
|
+
// output of the `toolCallId -> ToolMessage` correlation: the status it derived,
|
|
60
|
+
// the args it parsed, and the result content it actually handed the renderer.
|
|
61
|
+
// `undefined` is spelled out rather than stringified, so an absent result stays
|
|
62
|
+
// distinguishable from an empty-string one.
|
|
63
|
+
function ReportRegistrar() {
|
|
64
|
+
useRenderTool({
|
|
65
|
+
name: "reportPlaces",
|
|
66
|
+
description: "Report places",
|
|
67
|
+
parameters: z.object({ title: z.string() }),
|
|
68
|
+
render: ({ args, status, result }) => (
|
|
69
|
+
<div data-testid="report">
|
|
70
|
+
{`${status}:${(args as { title?: string }).title ?? ""}|${
|
|
71
|
+
result === undefined ? "<no result>" : result
|
|
72
|
+
}`}
|
|
73
|
+
</div>
|
|
74
|
+
),
|
|
75
|
+
});
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Renders the raw args object it receives, so tests can assert exactly what the
|
|
80
|
+
// renderer got for degenerate argument strings (empty / unparseable).
|
|
81
|
+
function ArgsRegistrar() {
|
|
82
|
+
useRenderTool({
|
|
83
|
+
name: "echoArgs",
|
|
84
|
+
description: "Echo args",
|
|
85
|
+
parameters: z.object({}),
|
|
86
|
+
render: ({ args }) => <div data-testid="args">{JSON.stringify(args)}</div>,
|
|
87
|
+
});
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Renders the raw `result` it receives, so tests can assert exactly what the
|
|
92
|
+
// renderer got for a tool message whose content is not the `string` the
|
|
93
|
+
// renderer contract declares.
|
|
94
|
+
function ResultRegistrar() {
|
|
95
|
+
useRenderTool({
|
|
96
|
+
name: "echoResult",
|
|
97
|
+
description: "Echo result",
|
|
98
|
+
parameters: z.object({}),
|
|
99
|
+
render: ({ status, result }) => (
|
|
100
|
+
<div data-testid="result">{`${status}|${String(result)}`}</div>
|
|
101
|
+
),
|
|
102
|
+
});
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// All four call the same shared factory, so every fixture here agrees on the
|
|
107
|
+
// tool-call id the tool results below are correlated against.
|
|
108
|
+
const assistantWithCall = (args: string) =>
|
|
109
|
+
assistantToolCall("showPlaces", args);
|
|
110
|
+
const assistantEchoArgs = (args: string) => assistantToolCall("echoArgs", args);
|
|
111
|
+
const assistantEchoResult = assistantToolCall("echoResult", "{}");
|
|
112
|
+
const assistantReportCall = (args: string) =>
|
|
113
|
+
assistantToolCall("reportPlaces", args);
|
|
114
|
+
|
|
115
|
+
// The one fixture here that cannot be the typed `toolMessage()` factory: the
|
|
116
|
+
// "tool-result content" suite drives content the `ToolMessage` type FORBIDS
|
|
117
|
+
// (arrays, objects, null, circular) through `toolResultContent`, and no
|
|
118
|
+
// properly-typed fixture can express that. Its string cases share this helper for
|
|
119
|
+
// uniformity within that suite; every tool result OUTSIDE it is typed.
|
|
120
|
+
const toolResult = (content: unknown) => ({
|
|
121
|
+
id: "m2",
|
|
122
|
+
role: "tool",
|
|
123
|
+
toolCallId: "tc1",
|
|
124
|
+
content,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("CopilotChat tool-call rendering", () => {
|
|
128
|
+
it("paints PARTIAL args with status inProgress while the call streams", () => {
|
|
129
|
+
// '{"title":"Roof' is invalid JSON — the model is mid-write. The old code
|
|
130
|
+
// JSON.parse'd it, warned, and fell back to {}, so nothing painted until
|
|
131
|
+
// the call completed.
|
|
132
|
+
render(
|
|
133
|
+
<TestCopilotKit messages={[assistantWithCall('{"title":"Roof')]}>
|
|
134
|
+
<Registrar />
|
|
135
|
+
<CopilotChat />
|
|
136
|
+
</TestCopilotKit>,
|
|
137
|
+
);
|
|
138
|
+
expect(screen.getByTestId("places").textContent).toBe("inProgress:Roof");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("reports complete and passes the result through when a tool message exists", () => {
|
|
142
|
+
render(
|
|
143
|
+
<TestCopilotKit
|
|
144
|
+
messages={[
|
|
145
|
+
assistantReportCall('{"title":"Rooftop"}'),
|
|
146
|
+
toolMessage("tc1", "ok"),
|
|
147
|
+
]}
|
|
148
|
+
>
|
|
149
|
+
<ReportRegistrar />
|
|
150
|
+
<CopilotChat />
|
|
151
|
+
</TestCopilotKit>,
|
|
152
|
+
);
|
|
153
|
+
// Reads the RESULT, not only the status. Asserting `complete` alone is
|
|
154
|
+
// satisfied by ANY tool message reaching `renderToolCall`, so it stayed
|
|
155
|
+
// green when the correlated content was replaced with a constant.
|
|
156
|
+
expect(screen.getByTestId("report").textContent).toBe(
|
|
157
|
+
"complete:Rooftop|ok",
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("does NOT complete a tool call that no tool message is correlated to", () => {
|
|
162
|
+
// The other direction of the same map. Every positive fixture in this file
|
|
163
|
+
// matches on id, so a lookup that ignores the key — keyed on the tool
|
|
164
|
+
// message's own id, or falling back to "any tool message we have" — reads as
|
|
165
|
+
// a correct correlation. Here the only tool result belongs to a DIFFERENT
|
|
166
|
+
// call, so a leak is visible as both a completed status and a stray result.
|
|
167
|
+
render(
|
|
168
|
+
<TestCopilotKit
|
|
169
|
+
messages={[
|
|
170
|
+
assistantReportCall('{"title":"Rooftop"}'),
|
|
171
|
+
toolMessage("tc-someone-else", "leaked"),
|
|
172
|
+
]}
|
|
173
|
+
>
|
|
174
|
+
<ReportRegistrar />
|
|
175
|
+
<CopilotChat />
|
|
176
|
+
</TestCopilotKit>,
|
|
177
|
+
);
|
|
178
|
+
expect(screen.getByTestId("report").textContent).toBe(
|
|
179
|
+
"inProgress:Rooftop|<no result>",
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("keeps rendering a tool call after the registering component unmounts", () => {
|
|
184
|
+
// The chat-history regression: RN's old Map deleted the renderer on unmount,
|
|
185
|
+
// so navigating away degraded earlier tool calls to the placeholder. Core
|
|
186
|
+
// deliberately keeps renderer entries.
|
|
187
|
+
const { rerender } = render(
|
|
188
|
+
<TestCopilotKit messages={[assistantWithCall('{"title":"Rooftop"}')]}>
|
|
189
|
+
<Registrar />
|
|
190
|
+
<CopilotChat />
|
|
191
|
+
</TestCopilotKit>,
|
|
192
|
+
);
|
|
193
|
+
expect(screen.getByTestId("places")).toBeTruthy();
|
|
194
|
+
|
|
195
|
+
rerender(
|
|
196
|
+
<TestCopilotKit messages={[assistantWithCall('{"title":"Rooftop"}')]}>
|
|
197
|
+
<CopilotChat />
|
|
198
|
+
</TestCopilotKit>,
|
|
199
|
+
);
|
|
200
|
+
expect(screen.getByTestId("places")).toBeTruthy();
|
|
201
|
+
expect(screen.queryByText("Called: showPlaces")).toBeNull();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("reports executing status when the call id is in executingToolCallIds and has no result", () => {
|
|
205
|
+
// No tool message + id in executingToolCallIds → core derives "executing"
|
|
206
|
+
// (executing sits between inProgress and complete in the status ladder).
|
|
207
|
+
render(
|
|
208
|
+
<TestCopilotKit
|
|
209
|
+
messages={[assistantWithCall('{"title":"Rooftop"}')]}
|
|
210
|
+
executingToolCallIds={new Set(["tc1"])}
|
|
211
|
+
>
|
|
212
|
+
<Registrar />
|
|
213
|
+
<CopilotChat />
|
|
214
|
+
</TestCopilotKit>,
|
|
215
|
+
);
|
|
216
|
+
expect(screen.getByTestId("places").textContent).toBe("executing:Rooftop");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("yields an empty args object for empty-string arguments without throwing", () => {
|
|
220
|
+
render(
|
|
221
|
+
<TestCopilotKit messages={[assistantEchoArgs("")]}>
|
|
222
|
+
<ArgsRegistrar />
|
|
223
|
+
<CopilotChat />
|
|
224
|
+
</TestCopilotKit>,
|
|
225
|
+
);
|
|
226
|
+
expect(screen.getByTestId("args").textContent).toBe("{}");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("yields an empty args object for unrepairable JSON arguments without throwing", () => {
|
|
230
|
+
render(
|
|
231
|
+
<TestCopilotKit messages={[assistantEchoArgs("not json at all")]}>
|
|
232
|
+
<ArgsRegistrar />
|
|
233
|
+
<CopilotChat />
|
|
234
|
+
</TestCopilotKit>,
|
|
235
|
+
);
|
|
236
|
+
expect(screen.getByTestId("args").textContent).toBe("{}");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("falls back to the placeholder for an unregistered tool", () => {
|
|
240
|
+
render(
|
|
241
|
+
<TestCopilotKit
|
|
242
|
+
messages={[assistantToolCall("notRegistered", "{}", "tc9")]}
|
|
243
|
+
>
|
|
244
|
+
<CopilotChat />
|
|
245
|
+
</TestCopilotKit>,
|
|
246
|
+
);
|
|
247
|
+
expect(screen.getByText("Called: notRegistered")).toBeTruthy();
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* How PRODUCTION changes `agent.messages`, and why it needs its own suite.
|
|
253
|
+
*
|
|
254
|
+
* Core NEVER reassigns `agent.messages`. Tool results are inserted with
|
|
255
|
+
* `agent.messages.splice(insertAt, 0, toolMessage)` (packages/core, run-handler),
|
|
256
|
+
* new messages are appended by `AbstractAgent.addMessage` (a `push`), and the
|
|
257
|
+
* AG-UI apply pipeline mutates the SAME array for the whole run. `useAgent`
|
|
258
|
+
* then re-renders with a bare `forceUpdate()` — it does not hand down a new
|
|
259
|
+
* array either.
|
|
260
|
+
*
|
|
261
|
+
* The array's IDENTITY therefore stays constant while its CONTENT changes. Any
|
|
262
|
+
* `useMemo(..., [messages])` in the chat is invalidated by neither, so it
|
|
263
|
+
* freezes at whatever the first render of the run saw.
|
|
264
|
+
*
|
|
265
|
+
* The rest of this file (and every other RN chat suite) drives messages by
|
|
266
|
+
* re-rendering `TestCopilotKit` with a NEW array, which does change identity —
|
|
267
|
+
* so those tests pass whether or not the memo dependencies are correct. These
|
|
268
|
+
* assertions deliberately mutate in place instead, via the same public
|
|
269
|
+
* `addMessage` that core's own paths bottom out in.
|
|
270
|
+
*/
|
|
271
|
+
describe("CopilotChat against in-place message mutation", () => {
|
|
272
|
+
// The object-content test below spies on console.warn; without this the spy
|
|
273
|
+
// would outlive it and swallow warnings from later tests in this file.
|
|
274
|
+
afterEach(() => {
|
|
275
|
+
vi.restoreAllMocks();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("reports complete and passes the result once a tool result is pushed in place", async () => {
|
|
279
|
+
// A plain box, not React.createRef — TestCopilotKit only writes to it.
|
|
280
|
+
const agentRef: React.MutableRefObject<AbstractAgent | null> = {
|
|
281
|
+
current: null,
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
render(
|
|
285
|
+
<TestCopilotKit
|
|
286
|
+
messages={[assistantReportCall('{"title":"Rooftop"}')]}
|
|
287
|
+
agentRef={agentRef}
|
|
288
|
+
>
|
|
289
|
+
<ReportRegistrar />
|
|
290
|
+
<CopilotChat />
|
|
291
|
+
</TestCopilotKit>,
|
|
292
|
+
);
|
|
293
|
+
// No tool message yet, and the id is not executing → core derives inProgress,
|
|
294
|
+
// and the renderer is handed no result at all.
|
|
295
|
+
expect(screen.getByTestId("report").textContent).toBe(
|
|
296
|
+
"inProgress:Rooftop|<no result>",
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
await act(async () => {
|
|
300
|
+
// A real `ToolMessage`, so `addMessage` needs no cast and the id the
|
|
301
|
+
// correlation keys on cannot go missing.
|
|
302
|
+
agentRef.current!.addMessage(toolMessage("tc1", "ok"));
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// The toolCallId → ToolMessage correlation must see the pushed message AND
|
|
306
|
+
// carry its content through — asserting the status alone would pass even if
|
|
307
|
+
// the map delivered a different message's result.
|
|
308
|
+
expect(screen.getByTestId("report").textContent).toBe(
|
|
309
|
+
"complete:Rooftop|ok",
|
|
310
|
+
);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("renders a tool call that is pushed in place after mount", async () => {
|
|
314
|
+
// A plain box, not React.createRef — TestCopilotKit only writes to it.
|
|
315
|
+
const agentRef: React.MutableRefObject<AbstractAgent | null> = {
|
|
316
|
+
current: null,
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
render(
|
|
320
|
+
<TestCopilotKit
|
|
321
|
+
messages={[{ id: "u1", role: "user", content: "find me a bar" }]}
|
|
322
|
+
agentRef={agentRef}
|
|
323
|
+
>
|
|
324
|
+
<Registrar />
|
|
325
|
+
<CopilotChat />
|
|
326
|
+
</TestCopilotKit>,
|
|
327
|
+
);
|
|
328
|
+
expect(screen.queryByTestId("places")).toBeNull();
|
|
329
|
+
|
|
330
|
+
await act(async () => {
|
|
331
|
+
// Typed `AssistantMessage` from the shared factory — assignable to
|
|
332
|
+
// `Message` on its own, so the old double cast is gone.
|
|
333
|
+
agentRef.current!.addMessage(assistantWithCall('{"title":"Rooftop"}'));
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// The flat-list items must see the pushed assistant message.
|
|
337
|
+
expect(screen.getByTestId("places").textContent).toBe("inProgress:Rooftop");
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Pins the OBJECT branch of `messagesFingerprint`'s content key.
|
|
342
|
+
*
|
|
343
|
+
* A length-based key is a constant 0 for every object, so before that branch
|
|
344
|
+
* existed any two objects fingerprinted identically: replacing a message's
|
|
345
|
+
* object content in place left `messagesKey` byte-identical, the
|
|
346
|
+
* `toolCallId -> ToolMessage` memo never recomputed, and the renderer kept the
|
|
347
|
+
* result serialized from the FIRST object it saw.
|
|
348
|
+
*
|
|
349
|
+
* This asserts through the renderer rather than calling the fingerprint
|
|
350
|
+
* directly, so it fails for the reason a user would notice (a stale `result`)
|
|
351
|
+
* instead of merely restating the implementation.
|
|
352
|
+
*
|
|
353
|
+
* Re-rendering the provider is safe here, unlike in the `agentRef` tests above:
|
|
354
|
+
* the mutation is to a message OBJECT inside the array this test owns, and the
|
|
355
|
+
* SAME array reference is passed again, so the provider's per-render re-seed is
|
|
356
|
+
* a no-op rather than a replacement. Nothing else in the fingerprint moves —
|
|
357
|
+
* same ids, roles, `toolCallId` and tool calls — so the object branch is the
|
|
358
|
+
* only thing that can invalidate the memo.
|
|
359
|
+
*/
|
|
360
|
+
it("re-renders a tool result when object content is replaced in place", () => {
|
|
361
|
+
// Non-string tool content is warned about by design; silence it so this
|
|
362
|
+
// assertion is about the memo, not about the warning.
|
|
363
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
364
|
+
|
|
365
|
+
const messages: Array<Record<string, unknown>> = [
|
|
366
|
+
assistantEchoResult,
|
|
367
|
+
{ id: "m2", role: "tool", toolCallId: "tc1", content: { step: "one" } },
|
|
368
|
+
];
|
|
369
|
+
// A FACTORY, not a stored element: re-rendering the identical element
|
|
370
|
+
// reference lets React bail out of the subtree entirely, which would make
|
|
371
|
+
// this test pass for the wrong reason (nothing re-rendered at all).
|
|
372
|
+
const tree = () => (
|
|
373
|
+
<TestCopilotKit messages={messages}>
|
|
374
|
+
<ResultRegistrar />
|
|
375
|
+
<CopilotChat />
|
|
376
|
+
</TestCopilotKit>
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const { rerender } = render(tree());
|
|
380
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
381
|
+
'complete|{"step":"one"}',
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
// Same id, same role, same toolCallId — only the object content differs, and
|
|
385
|
+
// it serializes to the same LENGTH as well, so a key that measured either the
|
|
386
|
+
// type or the size alone would still miss this.
|
|
387
|
+
messages[1].content = { step: "two" };
|
|
388
|
+
rerender(tree());
|
|
389
|
+
|
|
390
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
391
|
+
'complete|{"step":"two"}',
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe("CopilotChat tool-result content", () => {
|
|
397
|
+
afterEach(() => {
|
|
398
|
+
vi.restoreAllMocks();
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
const renderWithResult = (content: unknown) =>
|
|
402
|
+
render(
|
|
403
|
+
<TestCopilotKit messages={[assistantEchoResult, toolResult(content)]}>
|
|
404
|
+
<ResultRegistrar />
|
|
405
|
+
<CopilotChat />
|
|
406
|
+
</TestCopilotKit>,
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
it("passes a string result through verbatim without warning", () => {
|
|
410
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
411
|
+
renderWithResult('{"ok":true}');
|
|
412
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
413
|
+
'complete|{"ok":true}',
|
|
414
|
+
);
|
|
415
|
+
expect(warn).not.toHaveBeenCalled();
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("keeps a genuinely EMPTY string result empty and does not warn", () => {
|
|
419
|
+
// "" is a legitimate tool result. It must stay silent, so a warning is
|
|
420
|
+
// reserved for content the code could not represent.
|
|
421
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
422
|
+
renderWithResult("");
|
|
423
|
+
expect(screen.getByTestId("result").textContent).toBe("complete|");
|
|
424
|
+
expect(warn).not.toHaveBeenCalled();
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it("serialises ARRAY tool content instead of collapsing it to an empty result", () => {
|
|
428
|
+
// The silent-failure regression: array content (structured / attachment
|
|
429
|
+
// parts) was coerced to "", which a renderer cannot distinguish from a tool
|
|
430
|
+
// that genuinely returned nothing.
|
|
431
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
432
|
+
renderWithResult([{ type: "text", text: "hello" }]);
|
|
433
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
434
|
+
'complete|[{"type":"text","text":"hello"}]',
|
|
435
|
+
);
|
|
436
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
437
|
+
expect(String(warn.mock.calls[0]?.[0])).toContain("tc1");
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it("serialises OBJECT tool content instead of collapsing it to an empty result", () => {
|
|
441
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
442
|
+
renderWithResult({ status: "ok" });
|
|
443
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
444
|
+
'complete|{"status":"ok"}',
|
|
445
|
+
);
|
|
446
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("warns for NULL tool content even though it has nothing to serialise", () => {
|
|
450
|
+
// Nothing is lost by rendering "", but the message is still malformed, so
|
|
451
|
+
// the failure must be audible rather than silent.
|
|
452
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
453
|
+
renderWithResult(null);
|
|
454
|
+
expect(screen.getByTestId("result").textContent).toBe("complete|");
|
|
455
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it("does not throw on tool content that cannot be JSON-serialised", () => {
|
|
459
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
460
|
+
const circular: Record<string, unknown> = {};
|
|
461
|
+
circular.self = circular;
|
|
462
|
+
expect(() => renderWithResult(circular)).not.toThrow();
|
|
463
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
464
|
+
"complete|[object Object]",
|
|
465
|
+
);
|
|
466
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
467
|
+
});
|
|
468
|
+
});
|
package/src/headless.ts
CHANGED
|
@@ -58,7 +58,11 @@ export {
|
|
|
58
58
|
useCapabilities,
|
|
59
59
|
defineToolCallRenderer,
|
|
60
60
|
CopilotChatDefaultLabels,
|
|
61
|
-
type
|
|
61
|
+
// Runtime enum, not a type: it is passed BY VALUE to useAgent's `updates`
|
|
62
|
+
// option (`updates: [UseAgentUpdate.OnMessagesChanged]`). `export type` would
|
|
63
|
+
// strip the runtime binding and leave consumers unable to name a member.
|
|
64
|
+
// react-core's own headless entry exports it as a value for the same reason.
|
|
65
|
+
UseAgentUpdate,
|
|
62
66
|
type UseInterruptConfig,
|
|
63
67
|
type AgentContextInput,
|
|
64
68
|
type JsonSerializable,
|
|
@@ -80,31 +84,49 @@ export {
|
|
|
80
84
|
type RenderToolCompleteProps,
|
|
81
85
|
} from "@copilotkit/react-core/v2/headless";
|
|
82
86
|
|
|
83
|
-
// Re-export core
|
|
84
|
-
|
|
87
|
+
// Re-export core runtime ENUMS as values. Each of these is `export enum` in
|
|
88
|
+
// @copilotkit/core, and each is the type of a prop or field a consumer has to
|
|
89
|
+
// branch on — `status === ToolCallStatus.Executing`,
|
|
90
|
+
// `status === CopilotKitCoreRuntimeConnectionStatus.Connected`. An enum-typed
|
|
91
|
+
// field rejects a bare string literal, so a type-only re-export leaves the
|
|
92
|
+
// member names unreachable and the field impossible to compare against.
|
|
93
|
+
export {
|
|
94
|
+
ToolCallStatus,
|
|
85
95
|
CopilotKitCoreRuntimeConnectionStatus,
|
|
86
96
|
CopilotKitCoreErrorCode,
|
|
87
|
-
Suggestion,
|
|
88
|
-
FrontendTool,
|
|
89
|
-
ToolCallStatus,
|
|
90
97
|
} from "@copilotkit/core";
|
|
91
98
|
|
|
99
|
+
// Re-export core types commonly needed
|
|
100
|
+
export type { Suggestion, FrontendTool } from "@copilotkit/core";
|
|
101
|
+
|
|
102
|
+
// AbstractAgent is a runtime CLASS, not a type: it is the AG-UI extension point
|
|
103
|
+
// consumers subclass (`class MyAgent extends AbstractAgent`) and test with
|
|
104
|
+
// `instanceof`. @ag-ui/client is a dependency of this package rather than a peer,
|
|
105
|
+
// so a consumer cannot reliably import the class from there directly — a
|
|
106
|
+
// type-only re-export left the extension point unreachable from RN.
|
|
107
|
+
export { AbstractAgent } from "@ag-ui/client";
|
|
108
|
+
|
|
92
109
|
// Re-export AG-UI types for consumer convenience (matches web SDK surface)
|
|
93
110
|
export type {
|
|
94
111
|
Message,
|
|
95
112
|
AssistantMessage as AssistantMessageType,
|
|
96
113
|
ToolCall,
|
|
97
114
|
ToolMessage,
|
|
98
|
-
AbstractAgent,
|
|
99
115
|
AgentCapabilities,
|
|
100
116
|
} from "@ag-ui/client";
|
|
101
117
|
|
|
102
|
-
// Render tool
|
|
103
|
-
//
|
|
118
|
+
// Render tool registration (React Native flavour: RN-typed render function,
|
|
119
|
+
// registered into react-core's canonical registry — no local registry).
|
|
104
120
|
export { useRenderTool } from "./hooks/useRenderTool";
|
|
105
121
|
export type { UseRenderToolOptions } from "./hooks/useRenderTool";
|
|
106
|
-
export {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
} from "./hooks/
|
|
110
|
-
|
|
122
|
+
export type {
|
|
123
|
+
RenderToolProps,
|
|
124
|
+
RenderToolFunction,
|
|
125
|
+
} from "./hooks/render-tool-types";
|
|
126
|
+
|
|
127
|
+
// Render tool consumption. react-core's hook is platform-agnostic — it pulls no
|
|
128
|
+
// DOM and no chat-UI stack, and it returns ReactElement | null, which is exactly
|
|
129
|
+
// what FlatList's renderItem requires. Use it to render a registered component
|
|
130
|
+
// on any surface, chat or not.
|
|
131
|
+
export { useRenderToolCall } from "@copilotkit/react-core/v2/headless";
|
|
132
|
+
export type { ReactToolCallRenderer } from "@copilotkit/react-core/v2/headless";
|