@copilotkit/react-native 1.84.2-canary.openbot-intel-fixes → 1.86.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/README.md +341 -2
- package/USAGE.md +388 -21
- package/dist/{CopilotKitProvider-DBq4AhRP.d.cts → CopilotKitProvider-CQ-DpvY1.d.cts} +2 -2
- package/dist/{CopilotKitProvider-D8sn7G_C.mjs → CopilotKitProvider-CXQGv39c.mjs} +1 -1
- package/dist/{CopilotKitProvider-D8sn7G_C.mjs.map → CopilotKitProvider-CXQGv39c.mjs.map} +1 -1
- package/dist/{CopilotKitProvider-fW21Gb7T.d.mts → CopilotKitProvider-Dc1Vbv7K.d.mts} +1 -1
- package/dist/{CopilotKitProvider-CjoPnabW.cjs → CopilotKitProvider-zSfbWwIg.cjs} +1 -1
- package/dist/{CopilotKitProvider-CjoPnabW.cjs.map → CopilotKitProvider-zSfbWwIg.cjs.map} +1 -1
- package/dist/components/index.cjs +1 -0
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.mjs +1 -0
- package/dist/components/index.mjs.map +1 -1
- package/dist/generative-ui/index.cjs +612 -0
- package/dist/generative-ui/index.cjs.map +1 -0
- package/dist/generative-ui/index.d.cts +341 -0
- package/dist/generative-ui/index.d.mts +341 -0
- package/dist/generative-ui/index.mjs +549 -0
- package/dist/generative-ui/index.mjs.map +1 -0
- package/dist/headless.cjs +1 -1
- package/dist/headless.d.cts +1 -1
- package/dist/headless.d.mts +1 -1
- package/dist/headless.mjs +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills.cjs +218 -2
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +217 -1
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +13 -5
- package/project.json +6 -1
- package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
- package/scripts/verify-polyfill-barrel.mjs +154 -5
- package/src/__tests__/generative-ui-harness.tsx +243 -0
- package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
- package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
- package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
- package/src/__tests__/generative-ui.test.tsx +1000 -0
- package/src/__tests__/headless-entry-surface.test.ts +964 -34
- package/src/components/index.ts +6 -0
- package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
- package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
- package/src/generative-ui/GenerativeUISurface.tsx +303 -0
- package/src/generative-ui/host.tsx +138 -0
- package/src/generative-ui/index.ts +99 -0
- package/src/generative-ui/internal.ts +55 -0
- package/src/index.ts +8 -3
- package/tsdown.config.ts +1 -0
- package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
- package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
- package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
- package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wiring of the React Native generative UI components on the paths a
|
|
3
|
+
* happy-path mount leaves out: the provider's own ingest of
|
|
4
|
+
* `agent.messages` (malformed content included), `<React.StrictMode>`, the
|
|
5
|
+
* production build, and the warn-once contract.
|
|
6
|
+
*/
|
|
7
|
+
import React from "react";
|
|
8
|
+
import type { ReactNode } from "react";
|
|
9
|
+
import { act, render, screen } from "@testing-library/react";
|
|
10
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
11
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
12
|
+
import { COPILOTKIT_FORWARDED_PROPS_KEY } from "@copilotkit/shared";
|
|
13
|
+
import type {
|
|
14
|
+
A2UIOperation,
|
|
15
|
+
GenerativeUIRejectedOperation,
|
|
16
|
+
GenerativeUISurfaceContent,
|
|
17
|
+
} from "@copilotkit/shared";
|
|
18
|
+
import { useCopilotKit } from "@copilotkit/react-core/v2/context";
|
|
19
|
+
import { TestCopilotKit } from "../__mocks__/test-copilotkit";
|
|
20
|
+
|
|
21
|
+
vi.mock("react-native", async () =>
|
|
22
|
+
(await import("./generative-ui-rn-mock")).createReactNativeMock(),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
GenerativeUIActivity,
|
|
27
|
+
GenerativeUIProvider,
|
|
28
|
+
GenerativeUISurface,
|
|
29
|
+
NativeNodeFallback,
|
|
30
|
+
createGenerativeUI,
|
|
31
|
+
useGenerativeUIRuntime,
|
|
32
|
+
} from "../generative-ui";
|
|
33
|
+
import type {
|
|
34
|
+
GenerativeUIActivityProps,
|
|
35
|
+
GenerativeUIActivityWrapperProps,
|
|
36
|
+
GenerativeUIProviderProps,
|
|
37
|
+
} from "../generative-ui";
|
|
38
|
+
import {
|
|
39
|
+
Balance,
|
|
40
|
+
Harness,
|
|
41
|
+
RunnableAgent,
|
|
42
|
+
S,
|
|
43
|
+
activity,
|
|
44
|
+
create,
|
|
45
|
+
inProduction,
|
|
46
|
+
newAgentRef,
|
|
47
|
+
nodes,
|
|
48
|
+
startRun,
|
|
49
|
+
ui,
|
|
50
|
+
warningsWith,
|
|
51
|
+
} from "./generative-ui-harness";
|
|
52
|
+
import type { CopilotKitCore, HarnessProps } from "./generative-ui-harness";
|
|
53
|
+
|
|
54
|
+
afterEach(() => {
|
|
55
|
+
vi.restoreAllMocks();
|
|
56
|
+
vi.unstubAllEnvs();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
type Manager = NonNullable<
|
|
60
|
+
ReturnType<typeof useGenerativeUIRuntime>
|
|
61
|
+
>["manager"];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Mounts `children` in the harness. Hands back the agent, the core, the
|
|
65
|
+
* surface manager, and rerender/unmount with the same harness options.
|
|
66
|
+
*/
|
|
67
|
+
function mount(
|
|
68
|
+
children: (agent: AbstractAgent) => ReactNode,
|
|
69
|
+
options: Omit<HarnessProps, "children" | "agentRef" | "coreRef"> = {},
|
|
70
|
+
) {
|
|
71
|
+
const agentRef = newAgentRef();
|
|
72
|
+
const coreRef = {
|
|
73
|
+
current: null,
|
|
74
|
+
} as React.MutableRefObject<CopilotKitCore | null>;
|
|
75
|
+
const box: { manager?: Manager } = {};
|
|
76
|
+
function Grab() {
|
|
77
|
+
box.manager = useGenerativeUIRuntime()?.manager;
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const view = (
|
|
81
|
+
c: (agent: AbstractAgent) => ReactNode,
|
|
82
|
+
o: typeof options = options,
|
|
83
|
+
) => (
|
|
84
|
+
<Harness {...o} agentRef={agentRef} coreRef={coreRef}>
|
|
85
|
+
{(agent) => (
|
|
86
|
+
<>
|
|
87
|
+
<Grab />
|
|
88
|
+
{c(agent)}
|
|
89
|
+
</>
|
|
90
|
+
)}
|
|
91
|
+
</Harness>
|
|
92
|
+
);
|
|
93
|
+
const utils = render(view(children));
|
|
94
|
+
return {
|
|
95
|
+
agent: () => agentRef.current!,
|
|
96
|
+
core: () => coreRef.current!,
|
|
97
|
+
manager: () => box.manager!,
|
|
98
|
+
run: () => startRun(coreRef.current!, agentRef.current!),
|
|
99
|
+
rerender: (
|
|
100
|
+
c: (agent: AbstractAgent) => ReactNode,
|
|
101
|
+
o: typeof options = options,
|
|
102
|
+
) => utils.rerender(view(c, o)),
|
|
103
|
+
unmount: utils.unmount,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ready = (label: string, target = "chat") =>
|
|
108
|
+
activity(
|
|
109
|
+
[create, nodes({ id: "root", component: "Balance", label, amount: 1 })],
|
|
110
|
+
"ready",
|
|
111
|
+
target,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
/** The content of a named-target surface. */
|
|
115
|
+
const insights = (
|
|
116
|
+
status: GenerativeUISurfaceContent["status"],
|
|
117
|
+
label = "Insight",
|
|
118
|
+
surfaceId = S,
|
|
119
|
+
rejected: GenerativeUIRejectedOperation[] = [],
|
|
120
|
+
): GenerativeUISurfaceContent => ({
|
|
121
|
+
surfaceId,
|
|
122
|
+
target: "insights",
|
|
123
|
+
catalogId: "bank",
|
|
124
|
+
status,
|
|
125
|
+
rejected,
|
|
126
|
+
operations: [
|
|
127
|
+
{ version: "1.0", createSurface: { surfaceId, catalogId: "bank" } },
|
|
128
|
+
{
|
|
129
|
+
version: "1.0",
|
|
130
|
+
updateComponents: {
|
|
131
|
+
surfaceId,
|
|
132
|
+
components: [{ id: "root", component: "Balance", label, amount: 7 }],
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("the provider's ingest of agent.messages", () => {
|
|
139
|
+
it.each([false, true])(
|
|
140
|
+
"ingests a valid activity message from agent.messages at the run start (strict: %s)",
|
|
141
|
+
async (strict) => {
|
|
142
|
+
const message = ready("Seeded");
|
|
143
|
+
const h = mount(() => null, { messages: [message], strict });
|
|
144
|
+
await h.run();
|
|
145
|
+
expect(h.manager().getSurface(h.agent().threadId!, S)).toBeDefined();
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
it.each([
|
|
150
|
+
["null", null],
|
|
151
|
+
["a string", "text"],
|
|
152
|
+
["no surfaceId", { ...ready("x").content, surfaceId: undefined }],
|
|
153
|
+
["an unknown status", { ...ready("x").content, status: "done" }],
|
|
154
|
+
[
|
|
155
|
+
"a NaN rejected index",
|
|
156
|
+
{ ...ready("x").content, rejected: [{ index: Number.NaN, reason: "r" }] },
|
|
157
|
+
],
|
|
158
|
+
])(
|
|
159
|
+
"skips content that is %s without throwing, warns once in development, and still ingests the valid messages after it",
|
|
160
|
+
async (_label, content) => {
|
|
161
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
162
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
163
|
+
const bad = { ...ready("x"), id: "bad", content };
|
|
164
|
+
const good = {
|
|
165
|
+
...activity(
|
|
166
|
+
[
|
|
167
|
+
{
|
|
168
|
+
version: "1.0",
|
|
169
|
+
createSurface: { surfaceId: "s2", catalogId: "bank" },
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
"ready",
|
|
173
|
+
),
|
|
174
|
+
id: "good",
|
|
175
|
+
content: {
|
|
176
|
+
...activity([], "ready").content,
|
|
177
|
+
surfaceId: "s2",
|
|
178
|
+
operations: [
|
|
179
|
+
{
|
|
180
|
+
version: "1.0",
|
|
181
|
+
createSurface: { surfaceId: "s2", catalogId: "bank" },
|
|
182
|
+
} as A2UIOperation,
|
|
183
|
+
],
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
const h = mount(() => null, { messages: [bad, good] });
|
|
187
|
+
await h.run();
|
|
188
|
+
// Core logs a throwing run-start subscriber with console.error.
|
|
189
|
+
expect(error).not.toHaveBeenCalled();
|
|
190
|
+
expect(h.manager().getSurface(h.agent().threadId!, "s2")).toBeDefined();
|
|
191
|
+
expect(warningsWith(warn, "malformed")).toHaveLength(1);
|
|
192
|
+
// A later change of the messages ingests them again: no new warning
|
|
193
|
+
// for the same issue.
|
|
194
|
+
act(() => {
|
|
195
|
+
h.agent().setMessages([
|
|
196
|
+
bad,
|
|
197
|
+
good,
|
|
198
|
+
] as unknown as AbstractAgent["messages"]);
|
|
199
|
+
});
|
|
200
|
+
expect(error).not.toHaveBeenCalled();
|
|
201
|
+
expect(warningsWith(warn, "malformed")).toHaveLength(1);
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
it("a malformed message that arrives after the run start does not throw from the agent's subscription", async () => {
|
|
206
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
207
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
208
|
+
const h = mount(() => null);
|
|
209
|
+
await h.run();
|
|
210
|
+
expect(() =>
|
|
211
|
+
act(() => {
|
|
212
|
+
h.agent().setMessages([
|
|
213
|
+
{ ...ready("x"), content: null },
|
|
214
|
+
] as unknown as AbstractAgent["messages"]);
|
|
215
|
+
}),
|
|
216
|
+
).not.toThrow();
|
|
217
|
+
// The agent logs a throwing subscriber with console.error.
|
|
218
|
+
expect(error).not.toHaveBeenCalled();
|
|
219
|
+
expect(warningsWith(warn, "malformed")).toHaveLength(1);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("<GenerativeUIActivity> renders nothing for a null content that is also in agent.messages", async () => {
|
|
223
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
224
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
225
|
+
const message = { ...ready("x"), content: null };
|
|
226
|
+
const Wrapper = vi.fn(() => null);
|
|
227
|
+
const h = mount(
|
|
228
|
+
(agent) => (
|
|
229
|
+
<GenerativeUIActivity
|
|
230
|
+
message={message}
|
|
231
|
+
agent={agent}
|
|
232
|
+
Wrapper={Wrapper}
|
|
233
|
+
/>
|
|
234
|
+
),
|
|
235
|
+
{ messages: [message], strict: true },
|
|
236
|
+
);
|
|
237
|
+
await h.run();
|
|
238
|
+
expect(error).not.toHaveBeenCalled();
|
|
239
|
+
expect(Wrapper).not.toHaveBeenCalled();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
/** A named-target surface with `onStart` as its onGenerationStart. */
|
|
244
|
+
const surface = (onStart: () => void) => () => (
|
|
245
|
+
<GenerativeUISurface id="insights" onGenerationStart={onStart} />
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
describe("onGenerationStart", () => {
|
|
249
|
+
it("is called once for each render_ui start, also when React batches several starts", () => {
|
|
250
|
+
const onStart = vi.fn();
|
|
251
|
+
const h = mount(surface(onStart));
|
|
252
|
+
act(() => {
|
|
253
|
+
h.manager().setActiveThread("t1");
|
|
254
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
255
|
+
h.manager().ingest("t1", "default", insights("ready"));
|
|
256
|
+
h.manager().ingest("t1", "default", insights("streaming", "Second"));
|
|
257
|
+
});
|
|
258
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("is called once, not twice, when it mounts under StrictMode while the agent renders", () => {
|
|
262
|
+
const onStart = vi.fn();
|
|
263
|
+
const h = mount(() => null, { strict: true });
|
|
264
|
+
act(() => {
|
|
265
|
+
h.manager().setActiveThread("t1");
|
|
266
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
267
|
+
});
|
|
268
|
+
h.rerender(surface(onStart));
|
|
269
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("is not called again when the runtime changes during the same render_ui call", () => {
|
|
273
|
+
const onStart = vi.fn();
|
|
274
|
+
const h = mount(surface(onStart));
|
|
275
|
+
act(() => {
|
|
276
|
+
h.manager().setActiveThread("t1");
|
|
277
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
278
|
+
});
|
|
279
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
280
|
+
// A new catalog object gives a new runtime and manager. The provider
|
|
281
|
+
// builds the same surface again from the messages: not a new start.
|
|
282
|
+
const ui2 = createGenerativeUI({
|
|
283
|
+
id: "bank",
|
|
284
|
+
components: { Balance: { component: Balance } },
|
|
285
|
+
});
|
|
286
|
+
h.rerender(surface(onStart), {
|
|
287
|
+
providerProps: { ui: ui2 } as Partial<GenerativeUIProviderProps>,
|
|
288
|
+
});
|
|
289
|
+
act(() => {
|
|
290
|
+
h.manager().setActiveThread("t1");
|
|
291
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
292
|
+
});
|
|
293
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("counts a new call when one update goes from streaming to streaming and the rejected list was reset", () => {
|
|
297
|
+
const onStart = vi.fn();
|
|
298
|
+
const h = mount(surface(onStart));
|
|
299
|
+
act(() => {
|
|
300
|
+
h.manager().setActiveThread("t1");
|
|
301
|
+
h.manager().ingest(
|
|
302
|
+
"t1",
|
|
303
|
+
"default",
|
|
304
|
+
insights("streaming", "First", S, [{ index: 5, reason: "bad" }]),
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
308
|
+
// The "ready" of the first call was never seen; the runtime resets
|
|
309
|
+
// `rejected` to [] at the start of each call.
|
|
310
|
+
act(() => {
|
|
311
|
+
h.manager().ingest("t1", "default", insights("streaming", "Second"));
|
|
312
|
+
});
|
|
313
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("contains a throwing callback: the surface and its siblings stay, and the error is reported", () => {
|
|
317
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
318
|
+
const onStart = vi.fn(() => {
|
|
319
|
+
throw new Error("scroll failed");
|
|
320
|
+
});
|
|
321
|
+
const h = mount(() => (
|
|
322
|
+
<>
|
|
323
|
+
<span>sibling</span>
|
|
324
|
+
<GenerativeUISurface id="insights" onGenerationStart={onStart} />
|
|
325
|
+
</>
|
|
326
|
+
));
|
|
327
|
+
act(() => {
|
|
328
|
+
h.manager().setActiveThread("t1");
|
|
329
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
330
|
+
});
|
|
331
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
332
|
+
expect(screen.getByText("sibling")).toBeTruthy();
|
|
333
|
+
expect(screen.getByTestId("balance-Insight")).toBeTruthy();
|
|
334
|
+
expect(warningsWith(error, "onGenerationStart threw")).toHaveLength(1);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("does not call a new callback identity on a re-render within the same call, and calls the latest one on the next start", () => {
|
|
338
|
+
const first = vi.fn();
|
|
339
|
+
const second = vi.fn();
|
|
340
|
+
const h = mount(surface(first));
|
|
341
|
+
act(() => {
|
|
342
|
+
h.manager().setActiveThread("t1");
|
|
343
|
+
h.manager().ingest("t1", "default", insights("streaming"));
|
|
344
|
+
});
|
|
345
|
+
h.rerender(surface(second));
|
|
346
|
+
expect(first).toHaveBeenCalledTimes(1);
|
|
347
|
+
expect(second).not.toHaveBeenCalled();
|
|
348
|
+
act(() => {
|
|
349
|
+
h.manager().ingest("t1", "default", insights("ready"));
|
|
350
|
+
h.manager().ingest("t1", "default", insights("streaming", "Next"));
|
|
351
|
+
});
|
|
352
|
+
expect(first).toHaveBeenCalledTimes(1);
|
|
353
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe("warn once for each distinct cause", () => {
|
|
358
|
+
const bare = (agent: GenerativeUIActivityProps["agent"], id = "m1") => (
|
|
359
|
+
<GenerativeUIActivity message={{ ...ready("Home"), id }} agent={agent} />
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
it("<GenerativeUIActivity>: A, B, A warns A once and B once", () => {
|
|
363
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
364
|
+
const h = mount(() => bare(undefined));
|
|
365
|
+
h.rerender(() => bare({ agentId: "a1" }));
|
|
366
|
+
h.rerender(() => bare(undefined));
|
|
367
|
+
expect(warningsWith(warn, "the agent is undefined")).toHaveLength(1);
|
|
368
|
+
expect(warningsWith(warn, "has no threadId")).toHaveLength(1);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("<GenerativeUIActivity>: warns again when the message changes to another message", () => {
|
|
372
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
373
|
+
const h = mount(() => bare({ agentId: "a1" }, "m1"));
|
|
374
|
+
h.rerender(() => bare({ agentId: "a1" }, "m2"));
|
|
375
|
+
expect(warningsWith(warn, "has no threadId")).toHaveLength(2);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("<GenerativeUISurface>: unknown components A, B, A warns once; a new ui warns again", () => {
|
|
379
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
380
|
+
const view = (names: string[]) => () => (
|
|
381
|
+
<GenerativeUISurface
|
|
382
|
+
id="insights"
|
|
383
|
+
ui={ui}
|
|
384
|
+
components={names as unknown as ["Balance"]}
|
|
385
|
+
/>
|
|
386
|
+
);
|
|
387
|
+
const h = mount(view(["Balance", "Nope"]));
|
|
388
|
+
h.rerender(view(["Balance"]));
|
|
389
|
+
h.rerender(view(["Balance", "Nope"]));
|
|
390
|
+
expect(warningsWith(warn, "Nope")).toHaveLength(1);
|
|
391
|
+
const ui2 = createGenerativeUI({
|
|
392
|
+
id: "bank",
|
|
393
|
+
components: { Balance: { component: Balance } },
|
|
394
|
+
});
|
|
395
|
+
h.rerender(view(["Balance", "Nope"]), {
|
|
396
|
+
providerProps: { ui: ui2 } as Partial<GenerativeUIProviderProps>,
|
|
397
|
+
});
|
|
398
|
+
expect(warningsWith(warn, "Nope")).toHaveLength(2);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
describe("two providers on one CopilotKit core", () => {
|
|
403
|
+
function TwoProviders({ strict }: { strict?: boolean }) {
|
|
404
|
+
const agentRef = newAgentRef();
|
|
405
|
+
const tree = (
|
|
406
|
+
<TestCopilotKit messages={[]} agentRef={agentRef}>
|
|
407
|
+
<GenerativeUIProvider ui={ui}>
|
|
408
|
+
<GenerativeUIProvider ui={ui}>{null}</GenerativeUIProvider>
|
|
409
|
+
</GenerativeUIProvider>
|
|
410
|
+
</TestCopilotKit>
|
|
411
|
+
);
|
|
412
|
+
return strict ? <React.StrictMode>{tree}</React.StrictMode> : tree;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
it("warns in development", () => {
|
|
416
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
417
|
+
render(<TwoProviders />);
|
|
418
|
+
expect(
|
|
419
|
+
warningsWith(warn, "more than one <GenerativeUIProvider>"),
|
|
420
|
+
).toHaveLength(1);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("does not warn for one provider under StrictMode, or for providers on separate cores", () => {
|
|
424
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
425
|
+
mount(() => null, { strict: true });
|
|
426
|
+
mount(() => null);
|
|
427
|
+
expect(warningsWith(warn, "more than one <GenerativeUIProvider>")).toEqual(
|
|
428
|
+
[],
|
|
429
|
+
);
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
describe("<GenerativeUISurface> rejected operations", () => {
|
|
434
|
+
it("shows the default alert for a named-target surface with rejected operations", () => {
|
|
435
|
+
const h = mount(() => <GenerativeUISurface id="insights" />);
|
|
436
|
+
act(() => {
|
|
437
|
+
h.manager().setActiveThread("t1");
|
|
438
|
+
h.manager().ingest(
|
|
439
|
+
"t1",
|
|
440
|
+
"default",
|
|
441
|
+
insights("ready", "Insight", S, [{ index: 3, reason: "unknown" }]),
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
expect(screen.getByTestId("balance-Insight")).toBeTruthy();
|
|
445
|
+
expect(screen.getByRole("alert").textContent).toBe(
|
|
446
|
+
"Part of this UI couldn't be shown.",
|
|
447
|
+
);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it("gives a custom Wrapper the server and device rejections together", () => {
|
|
451
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
452
|
+
const Wrapper = vi.fn(({ children }: GenerativeUIActivityWrapperProps) => (
|
|
453
|
+
<>{children}</>
|
|
454
|
+
));
|
|
455
|
+
const h = mount(() => (
|
|
456
|
+
<GenerativeUISurface id="insights" Wrapper={Wrapper} />
|
|
457
|
+
));
|
|
458
|
+
const content = insights("streaming", "Insight", S, [
|
|
459
|
+
{ index: 9, reason: "server" },
|
|
460
|
+
]);
|
|
461
|
+
act(() => {
|
|
462
|
+
h.manager().setActiveThread("t1");
|
|
463
|
+
h.manager().ingest("t1", "default", {
|
|
464
|
+
...content,
|
|
465
|
+
operations: [
|
|
466
|
+
...content.operations,
|
|
467
|
+
nodes({
|
|
468
|
+
id: "bad",
|
|
469
|
+
component: "Balance",
|
|
470
|
+
label: "b",
|
|
471
|
+
amount: "x" as never,
|
|
472
|
+
}),
|
|
473
|
+
],
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
const props = Wrapper.mock.calls.at(-1)![0];
|
|
477
|
+
expect(props.rejected.map((r) => r.index)).toEqual([9, 2]);
|
|
478
|
+
expect(props.error?.message).toBe("2 UI update(s) could not be shown");
|
|
479
|
+
// The list is the error's cause, not enumerable, as on the web.
|
|
480
|
+
expect((props.error as Error & { cause?: unknown }).cause).toEqual(
|
|
481
|
+
props.rejected,
|
|
482
|
+
);
|
|
483
|
+
expect(
|
|
484
|
+
Object.prototype.propertyIsEnumerable.call(props.error, "cause"),
|
|
485
|
+
).toBe(false);
|
|
486
|
+
expect(props.isStreaming).toBe(true);
|
|
487
|
+
expect(props.surfaceId).toBe(S);
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
describe("the transport warning", () => {
|
|
492
|
+
it("names a non-string transport readably, not as [object Object]", () => {
|
|
493
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
494
|
+
mount(() => null, {
|
|
495
|
+
providerProps: {
|
|
496
|
+
transport: { kind: "markdown" },
|
|
497
|
+
} as unknown as Partial<GenerativeUIProviderProps>,
|
|
498
|
+
});
|
|
499
|
+
const warnings = warningsWith(warn, "ignores transport");
|
|
500
|
+
expect(warnings).toHaveLength(1);
|
|
501
|
+
expect(warnings[0]).not.toContain("[object Object]");
|
|
502
|
+
expect(warnings[0]).toContain('{"kind":"markdown"}');
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it("stays silent for an explicit tool transport", () => {
|
|
506
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
507
|
+
mount(() => null, {
|
|
508
|
+
providerProps: {
|
|
509
|
+
transport: "tool",
|
|
510
|
+
} as Partial<GenerativeUIProviderProps>,
|
|
511
|
+
});
|
|
512
|
+
expect(warn).not.toHaveBeenCalled();
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
describe("production", () => {
|
|
517
|
+
it("prints no warning for any of the development warnings", async () => {
|
|
518
|
+
await inProduction(async () => {
|
|
519
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
520
|
+
const noUI = {
|
|
521
|
+
ui: undefined,
|
|
522
|
+
} as unknown as Partial<GenerativeUIProviderProps>;
|
|
523
|
+
const untyped = {
|
|
524
|
+
theme: { primary: "#f00" },
|
|
525
|
+
transport: "markdown",
|
|
526
|
+
} as unknown as Partial<GenerativeUIProviderProps>;
|
|
527
|
+
const malformed = { ...ready("x"), content: null };
|
|
528
|
+
// Provider: theme, transport, malformed agent.messages.
|
|
529
|
+
const h = mount(
|
|
530
|
+
(agent) => (
|
|
531
|
+
<>
|
|
532
|
+
{/* Activity: malformed, undefined agent, no threadId. */}
|
|
533
|
+
<GenerativeUIActivity message={malformed} agent={agent} />
|
|
534
|
+
<GenerativeUIActivity message={ready("a")} agent={undefined} />
|
|
535
|
+
<GenerativeUIActivity
|
|
536
|
+
message={ready("b")}
|
|
537
|
+
agent={{ agentId: "x" }}
|
|
538
|
+
/>
|
|
539
|
+
{/* Surface: other ui, unknown components. */}
|
|
540
|
+
<GenerativeUISurface
|
|
541
|
+
id="insights"
|
|
542
|
+
ui={createGenerativeUI({
|
|
543
|
+
id: "other",
|
|
544
|
+
components: { Balance: { component: Balance } },
|
|
545
|
+
})}
|
|
546
|
+
components={["Nope"] as unknown as ["Balance"]}
|
|
547
|
+
/>
|
|
548
|
+
</>
|
|
549
|
+
),
|
|
550
|
+
{ providerProps: untyped, messages: [malformed], strict: true },
|
|
551
|
+
);
|
|
552
|
+
await h.run();
|
|
553
|
+
// Provider without ui, Surface under it, Activity under it.
|
|
554
|
+
mount(
|
|
555
|
+
(agent) => (
|
|
556
|
+
<>
|
|
557
|
+
<GenerativeUISurface id="insights" />
|
|
558
|
+
<GenerativeUIActivity message={ready("c")} agent={agent} />
|
|
559
|
+
</>
|
|
560
|
+
),
|
|
561
|
+
{ providerProps: noUI },
|
|
562
|
+
);
|
|
563
|
+
// Activity with no provider.
|
|
564
|
+
render(
|
|
565
|
+
<GenerativeUIActivity message={ready("d")} agent={{ threadId: "t" }} />,
|
|
566
|
+
);
|
|
567
|
+
// Two providers on one core.
|
|
568
|
+
render(
|
|
569
|
+
<TestCopilotKit messages={[]}>
|
|
570
|
+
<GenerativeUIProvider ui={ui}>
|
|
571
|
+
<GenerativeUIProvider ui={ui}>{null}</GenerativeUIProvider>
|
|
572
|
+
</GenerativeUIProvider>
|
|
573
|
+
</TestCopilotKit>,
|
|
574
|
+
);
|
|
575
|
+
expect(warn.mock.calls.map((c) => String(c[0]))).toEqual([]);
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
describe("<GenerativeUISurface> targets", () => {
|
|
581
|
+
it("warns in development when its ui is not the provider's", () => {
|
|
582
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
583
|
+
const other = createGenerativeUI({
|
|
584
|
+
id: "other",
|
|
585
|
+
components: { Balance: { component: Balance } },
|
|
586
|
+
});
|
|
587
|
+
mount(() => <GenerativeUISurface id="insights" ui={other} />);
|
|
588
|
+
expect(
|
|
589
|
+
warningsWith(warn, "the ui prop is not the UI of the provider"),
|
|
590
|
+
).toHaveLength(1);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("registers the new target when id changes, and again when description changes", () => {
|
|
594
|
+
const h = mount(() => (
|
|
595
|
+
<GenerativeUISurface id="insights" description="One" />
|
|
596
|
+
));
|
|
597
|
+
expect(h.manager().getMountedTargets()).toEqual([
|
|
598
|
+
{ id: "insights", description: "One", components: undefined },
|
|
599
|
+
]);
|
|
600
|
+
h.rerender(() => <GenerativeUISurface id="offers" description="One" />);
|
|
601
|
+
expect(h.manager().getMountedTargets()).toEqual([
|
|
602
|
+
{ id: "offers", description: "One", components: undefined },
|
|
603
|
+
]);
|
|
604
|
+
h.rerender(() => <GenerativeUISurface id="offers" description="Two" />);
|
|
605
|
+
expect(h.manager().getMountedTargets()).toEqual([
|
|
606
|
+
{ id: "offers", description: "Two", components: undefined },
|
|
607
|
+
]);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it.each([
|
|
611
|
+
["an empty list", []],
|
|
612
|
+
["only unknown names", ["Nope"]],
|
|
613
|
+
])(
|
|
614
|
+
"with %s, registers a target that allows no component",
|
|
615
|
+
(_label, names) => {
|
|
616
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
617
|
+
const h = mount(() => (
|
|
618
|
+
<GenerativeUISurface
|
|
619
|
+
id="insights"
|
|
620
|
+
ui={ui}
|
|
621
|
+
components={names as unknown as ["Balance"]}
|
|
622
|
+
/>
|
|
623
|
+
));
|
|
624
|
+
expect(h.manager().getMountedTargets()[0].components).toEqual([]);
|
|
625
|
+
},
|
|
626
|
+
);
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
describe("<GenerativeUIActivity> wiring", () => {
|
|
630
|
+
it("passes isStreaming true while the agent writes the surface", () => {
|
|
631
|
+
const Wrapper = vi.fn(({ children }: GenerativeUIActivityWrapperProps) => (
|
|
632
|
+
<>{children}</>
|
|
633
|
+
));
|
|
634
|
+
const message = activity(
|
|
635
|
+
[
|
|
636
|
+
create,
|
|
637
|
+
nodes({ id: "root", component: "Balance", label: "S", amount: 1 }),
|
|
638
|
+
],
|
|
639
|
+
"streaming",
|
|
640
|
+
);
|
|
641
|
+
mount((agent) => (
|
|
642
|
+
<GenerativeUIActivity message={message} agent={agent} Wrapper={Wrapper} />
|
|
643
|
+
));
|
|
644
|
+
expect(Wrapper.mock.calls.at(-1)![0].isStreaming).toBe(true);
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it("renders the surface once the agent's threadId arrives after mount", () => {
|
|
648
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
649
|
+
const view = (agent: GenerativeUIActivityProps["agent"]) => () => (
|
|
650
|
+
<GenerativeUIActivity message={ready("Late")} agent={agent} />
|
|
651
|
+
);
|
|
652
|
+
const h = mount(view({ agentId: "default" }));
|
|
653
|
+
expect(screen.queryByTestId("balance-Late")).toBeNull();
|
|
654
|
+
h.rerender(view({ agentId: "default", threadId: "t-late" }));
|
|
655
|
+
expect(screen.getByTestId("balance-Late")).toBeTruthy();
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
it("two activities for one surface both render it, from one manager surface", () => {
|
|
659
|
+
const message = ready("Twice");
|
|
660
|
+
const h = mount((agent) => (
|
|
661
|
+
<>
|
|
662
|
+
<GenerativeUIActivity message={message} agent={agent} />
|
|
663
|
+
<GenerativeUIActivity message={message} agent={agent} />
|
|
664
|
+
</>
|
|
665
|
+
));
|
|
666
|
+
expect(screen.getAllByTestId("balance-Twice")).toHaveLength(2);
|
|
667
|
+
expect(h.manager().getSurface(h.agent().threadId!, S)).toBeDefined();
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it.each([false, true])(
|
|
671
|
+
"renders under StrictMode (strict: %s) and from the provider path",
|
|
672
|
+
async (strict) => {
|
|
673
|
+
const message = ready("Strict");
|
|
674
|
+
const h = mount(
|
|
675
|
+
(agent) => <GenerativeUIActivity message={message} agent={agent} />,
|
|
676
|
+
{ messages: [message], strict },
|
|
677
|
+
);
|
|
678
|
+
await h.run();
|
|
679
|
+
expect(screen.getAllByTestId("balance-Strict")).toHaveLength(1);
|
|
680
|
+
},
|
|
681
|
+
);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
describe("agentIds", () => {
|
|
685
|
+
it("sends the catalog only to the agents it names", async () => {
|
|
686
|
+
const agent = new RunnableAgent();
|
|
687
|
+
const other = new RunnableAgent();
|
|
688
|
+
const coreRef = {
|
|
689
|
+
current: null,
|
|
690
|
+
} as React.MutableRefObject<CopilotKitCore | null>;
|
|
691
|
+
function Probe() {
|
|
692
|
+
coreRef.current = useCopilotKit().copilotkit;
|
|
693
|
+
return null;
|
|
694
|
+
}
|
|
695
|
+
render(
|
|
696
|
+
<TestCopilotKit messages={[]} agent={agent} agents={{ other }}>
|
|
697
|
+
<Probe />
|
|
698
|
+
<GenerativeUIProvider ui={ui} agentIds={["other"]}>
|
|
699
|
+
{null}
|
|
700
|
+
</GenerativeUIProvider>
|
|
701
|
+
</TestCopilotKit>,
|
|
702
|
+
);
|
|
703
|
+
await startRun(coreRef.current!, agent);
|
|
704
|
+
await startRun(coreRef.current!, other);
|
|
705
|
+
const uiOf = (a: RunnableAgent) =>
|
|
706
|
+
(a.inputs[0]?.forwardedProps as Record<string, { ui?: unknown }>)?.[
|
|
707
|
+
COPILOTKIT_FORWARDED_PROPS_KEY
|
|
708
|
+
]?.ui;
|
|
709
|
+
expect(uiOf(agent)).toBeUndefined();
|
|
710
|
+
expect(uiOf(other)).toBeDefined();
|
|
711
|
+
});
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
describe("NativeNodeFallback", () => {
|
|
715
|
+
it("draws no Retry button when there is no retry", () => {
|
|
716
|
+
render(<NativeNodeFallback message="kaboom" componentId="root" />);
|
|
717
|
+
expect(screen.getByTestId("generative-ui-error-root")).toBeTruthy();
|
|
718
|
+
expect(screen.queryByText("Retry")).toBeNull();
|
|
719
|
+
});
|
|
720
|
+
});
|