@copilotkit/react-native 1.57.1 → 1.57.2
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 +11 -1
- package/dist/index.cjs +752 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +394 -6
- package/dist/index.d.mts +394 -6
- package/dist/index.mjs +725 -14
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills.cjs +86 -73
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +86 -73
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +21 -5
- package/src/CopilotChat.tsx +293 -0
- package/src/CopilotKitProvider.tsx +55 -13
- package/src/CopilotModal.tsx +32 -0
- package/src/CopilotPopup.tsx +359 -0
- package/src/CopilotSidebar.tsx +357 -0
- package/src/__tests__/__mocks__/expo-document-picker.ts +13 -0
- package/src/__tests__/__mocks__/expo-file-system.ts +12 -0
- package/src/__tests__/attachments-integration.test.tsx +336 -0
- package/src/__tests__/copilot-chat-agentid.test.tsx +305 -0
- package/src/__tests__/copilot-chat-attachments.test.tsx +259 -0
- package/src/__tests__/copilot-popup.test.tsx +558 -0
- package/src/__tests__/copilot-sidebar.test.tsx +500 -0
- package/src/__tests__/copilotkit-provider.test.tsx +3 -0
- package/src/__tests__/headless-integration.test.tsx +55 -1
- package/src/__tests__/streaming-fetch.test.ts +84 -44
- package/src/__tests__/use-attachments.test.ts +410 -0
- package/src/hooks/use-attachments.ts +274 -0
- package/src/index.ts +77 -3
- package/src/polyfills.ts +3 -2
- package/src/streaming-fetch.ts +135 -104
- package/tsdown.config.ts +1 -0
- package/vitest.config.mjs +19 -0
|
@@ -26,7 +26,21 @@ class MockXHR {
|
|
|
26
26
|
|
|
27
27
|
let mockXhr: MockXHR;
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Flush pending setTimeout(fn, 0) callbacks and microtasks.
|
|
31
|
+
* The streaming-fetch polyfill defers all XHR callbacks via setTimeout(fn, 0)
|
|
32
|
+
* to ensure they run on the JS thread in React Native. In tests we need to
|
|
33
|
+
* flush these timers after each simulated XHR event.
|
|
34
|
+
*
|
|
35
|
+
* We flush multiple ticks to ensure all chained timers and microtasks settle.
|
|
36
|
+
*/
|
|
37
|
+
async function flushTimers() {
|
|
38
|
+
for (let i = 0; i < 3; i++) {
|
|
39
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function simulateHeaders(
|
|
30
44
|
xhr: MockXHR,
|
|
31
45
|
status: number,
|
|
32
46
|
headers = "content-type: text/plain\r\n",
|
|
@@ -37,24 +51,29 @@ function simulateHeaders(
|
|
|
37
51
|
xhr.statusText = statusText;
|
|
38
52
|
xhr.getAllResponseHeaders.mockReturnValue(headers);
|
|
39
53
|
xhr.onreadystatechange?.();
|
|
54
|
+
await flushTimers();
|
|
40
55
|
}
|
|
41
56
|
|
|
42
|
-
function simulateProgress(xhr: MockXHR, text: string) {
|
|
57
|
+
async function simulateProgress(xhr: MockXHR, text: string) {
|
|
43
58
|
xhr.responseText = text;
|
|
44
59
|
xhr.onprogress?.();
|
|
60
|
+
await flushTimers();
|
|
45
61
|
}
|
|
46
62
|
|
|
47
|
-
function simulateLoad(xhr: MockXHR) {
|
|
63
|
+
async function simulateLoad(xhr: MockXHR) {
|
|
48
64
|
xhr.readyState = 4;
|
|
49
65
|
xhr.onload?.();
|
|
66
|
+
await flushTimers();
|
|
50
67
|
}
|
|
51
68
|
|
|
52
|
-
function simulateError(xhr: MockXHR) {
|
|
69
|
+
async function simulateError(xhr: MockXHR) {
|
|
53
70
|
xhr.onerror?.();
|
|
71
|
+
await flushTimers();
|
|
54
72
|
}
|
|
55
73
|
|
|
56
|
-
function simulateTimeout(xhr: MockXHR) {
|
|
74
|
+
async function simulateTimeout(xhr: MockXHR) {
|
|
57
75
|
xhr.ontimeout?.();
|
|
76
|
+
await flushTimers();
|
|
58
77
|
}
|
|
59
78
|
|
|
60
79
|
// ─── Globals save/restore ─────────────────────────────────────────────────────
|
|
@@ -69,7 +88,10 @@ beforeEach(() => {
|
|
|
69
88
|
savedResponse = globalThis.Response;
|
|
70
89
|
|
|
71
90
|
mockXhr = new MockXHR();
|
|
72
|
-
|
|
91
|
+
// Must use a regular function (not arrow) so it can be called with `new`
|
|
92
|
+
(globalThis as any).XMLHttpRequest = vi.fn(function () {
|
|
93
|
+
return mockXhr;
|
|
94
|
+
});
|
|
73
95
|
|
|
74
96
|
// Make feature detection fail so the polyfill installs
|
|
75
97
|
(globalThis as any).Response = class {
|
|
@@ -88,6 +110,8 @@ afterEach(() => {
|
|
|
88
110
|
async function install() {
|
|
89
111
|
// Reset module cache so installStreamingFetch runs fresh
|
|
90
112
|
vi.resetModules();
|
|
113
|
+
// Ensure __DEV__ survives module reset (React Native global)
|
|
114
|
+
(globalThis as any).__DEV__ = true;
|
|
91
115
|
const mod = await import("../streaming-fetch");
|
|
92
116
|
mod.installStreamingFetch();
|
|
93
117
|
}
|
|
@@ -208,7 +232,7 @@ describe("installStreamingFetch", () => {
|
|
|
208
232
|
describe("response resolution", () => {
|
|
209
233
|
it("resolves when headers arrive with non-zero status", async () => {
|
|
210
234
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
211
|
-
simulateHeaders(xhr, 200);
|
|
235
|
+
await simulateHeaders(xhr, 200);
|
|
212
236
|
const resp = await fetchPromise;
|
|
213
237
|
expect(resp.status).toBe(200);
|
|
214
238
|
});
|
|
@@ -217,7 +241,7 @@ describe("installStreamingFetch", () => {
|
|
|
217
241
|
const { fetchPromise, xhr } = await fetchAndCapture(
|
|
218
242
|
"https://api.test/not-found",
|
|
219
243
|
);
|
|
220
|
-
simulateHeaders(xhr, 404, "", "Not Found");
|
|
244
|
+
await simulateHeaders(xhr, 404, "", "Not Found");
|
|
221
245
|
const resp = await fetchPromise;
|
|
222
246
|
expect(resp.ok).toBe(false);
|
|
223
247
|
expect(resp.status).toBe(404);
|
|
@@ -227,7 +251,7 @@ describe("installStreamingFetch", () => {
|
|
|
227
251
|
|
|
228
252
|
it("parses response headers into a Headers object", async () => {
|
|
229
253
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
230
|
-
simulateHeaders(
|
|
254
|
+
await simulateHeaders(
|
|
231
255
|
xhr,
|
|
232
256
|
200,
|
|
233
257
|
"content-type: application/json\r\nx-request-id: abc123\r\n",
|
|
@@ -239,7 +263,7 @@ describe("installStreamingFetch", () => {
|
|
|
239
263
|
|
|
240
264
|
it("provides a ReadableStream body on the response", async () => {
|
|
241
265
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
242
|
-
simulateHeaders(xhr, 200);
|
|
266
|
+
await simulateHeaders(xhr, 200);
|
|
243
267
|
const resp = await fetchPromise;
|
|
244
268
|
expect(resp.body).toBeInstanceOf(ReadableStream);
|
|
245
269
|
});
|
|
@@ -250,27 +274,27 @@ describe("installStreamingFetch", () => {
|
|
|
250
274
|
describe("streaming chunks", () => {
|
|
251
275
|
it("delivers chunks incrementally as XHR fires onprogress", async () => {
|
|
252
276
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
253
|
-
simulateHeaders(xhr, 200);
|
|
277
|
+
await simulateHeaders(xhr, 200);
|
|
254
278
|
const resp = await fetchPromise;
|
|
255
279
|
const reader = resp.body!.getReader();
|
|
256
280
|
|
|
257
|
-
simulateProgress(xhr, "chunk1");
|
|
281
|
+
await simulateProgress(xhr, "chunk1");
|
|
258
282
|
const { value: c1 } = await reader.read();
|
|
259
283
|
expect(new TextDecoder().decode(c1)).toBe("chunk1");
|
|
260
284
|
|
|
261
|
-
simulateProgress(xhr, "chunk1chunk2");
|
|
285
|
+
await simulateProgress(xhr, "chunk1chunk2");
|
|
262
286
|
const { value: c2 } = await reader.read();
|
|
263
287
|
expect(new TextDecoder().decode(c2)).toBe("chunk2");
|
|
264
288
|
});
|
|
265
289
|
|
|
266
290
|
it("closes stream on onload after delivering final chunks", async () => {
|
|
267
291
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
268
|
-
simulateHeaders(xhr, 200);
|
|
292
|
+
await simulateHeaders(xhr, 200);
|
|
269
293
|
const resp = await fetchPromise;
|
|
270
294
|
const reader = resp.body!.getReader();
|
|
271
295
|
|
|
272
296
|
xhr.responseText = "all data";
|
|
273
|
-
simulateLoad(xhr);
|
|
297
|
+
await simulateLoad(xhr);
|
|
274
298
|
|
|
275
299
|
const { value, done } = await reader.read();
|
|
276
300
|
expect(new TextDecoder().decode(value)).toBe("all data");
|
|
@@ -280,11 +304,11 @@ describe("installStreamingFetch", () => {
|
|
|
280
304
|
|
|
281
305
|
it("encodes chunks as Uint8Array", async () => {
|
|
282
306
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
283
|
-
simulateHeaders(xhr, 200);
|
|
307
|
+
await simulateHeaders(xhr, 200);
|
|
284
308
|
const resp = await fetchPromise;
|
|
285
309
|
const reader = resp.body!.getReader();
|
|
286
310
|
|
|
287
|
-
simulateProgress(xhr, "hello");
|
|
311
|
+
await simulateProgress(xhr, "hello");
|
|
288
312
|
const { value } = await reader.read();
|
|
289
313
|
// Cross-realm safe check (jsdom TextEncoder may produce a different Uint8Array)
|
|
290
314
|
expect(ArrayBuffer.isView(value)).toBe(true);
|
|
@@ -297,22 +321,22 @@ describe("installStreamingFetch", () => {
|
|
|
297
321
|
describe("convenience methods", () => {
|
|
298
322
|
it("text() returns full response text after XHR completes", async () => {
|
|
299
323
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
300
|
-
simulateHeaders(xhr, 200);
|
|
324
|
+
await simulateHeaders(xhr, 200);
|
|
301
325
|
const resp = await fetchPromise;
|
|
302
326
|
|
|
303
327
|
xhr.responseText = "hello world";
|
|
304
|
-
simulateLoad(xhr);
|
|
328
|
+
await simulateLoad(xhr);
|
|
305
329
|
|
|
306
330
|
expect(await resp.text()).toBe("hello world");
|
|
307
331
|
});
|
|
308
332
|
|
|
309
333
|
it("json() parses full response text as JSON", async () => {
|
|
310
334
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
311
|
-
simulateHeaders(xhr, 200);
|
|
335
|
+
await simulateHeaders(xhr, 200);
|
|
312
336
|
const resp = await fetchPromise;
|
|
313
337
|
|
|
314
338
|
xhr.responseText = '{"key":"value"}';
|
|
315
|
-
simulateLoad(xhr);
|
|
339
|
+
await simulateLoad(xhr);
|
|
316
340
|
|
|
317
341
|
expect(await resp.json()).toEqual({ key: "value" });
|
|
318
342
|
});
|
|
@@ -322,11 +346,11 @@ describe("installStreamingFetch", () => {
|
|
|
322
346
|
"https://api.test/bad",
|
|
323
347
|
{ method: "POST" },
|
|
324
348
|
);
|
|
325
|
-
simulateHeaders(xhr, 200);
|
|
349
|
+
await simulateHeaders(xhr, 200);
|
|
326
350
|
const resp = await fetchPromise;
|
|
327
351
|
|
|
328
352
|
xhr.responseText = "not json";
|
|
329
|
-
simulateLoad(xhr);
|
|
353
|
+
await simulateLoad(xhr);
|
|
330
354
|
|
|
331
355
|
await expect(resp.json()).rejects.toThrow(TypeError);
|
|
332
356
|
await expect(resp.json()).rejects.toThrow(/api\.test\/bad/);
|
|
@@ -334,26 +358,26 @@ describe("installStreamingFetch", () => {
|
|
|
334
358
|
|
|
335
359
|
it("clone() always throws", async () => {
|
|
336
360
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
337
|
-
simulateHeaders(xhr, 200);
|
|
361
|
+
await simulateHeaders(xhr, 200);
|
|
338
362
|
const resp = await fetchPromise;
|
|
339
363
|
expect(() => resp.clone()).toThrow(/not supported/);
|
|
340
364
|
});
|
|
341
365
|
|
|
342
366
|
it("formData() always throws", async () => {
|
|
343
367
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
344
|
-
simulateHeaders(xhr, 200);
|
|
368
|
+
await simulateHeaders(xhr, 200);
|
|
345
369
|
const resp = await fetchPromise;
|
|
346
370
|
await expect(resp.formData()).rejects.toThrow(/not supported/);
|
|
347
371
|
});
|
|
348
372
|
|
|
349
373
|
it("marks bodyUsed after calling text()", async () => {
|
|
350
374
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
351
|
-
simulateHeaders(xhr, 200);
|
|
375
|
+
await simulateHeaders(xhr, 200);
|
|
352
376
|
const resp = await fetchPromise;
|
|
353
377
|
expect(resp.bodyUsed).toBe(false);
|
|
354
378
|
|
|
355
379
|
xhr.responseText = "data";
|
|
356
|
-
simulateLoad(xhr);
|
|
380
|
+
await simulateLoad(xhr);
|
|
357
381
|
await resp.text();
|
|
358
382
|
|
|
359
383
|
expect(resp.bodyUsed).toBe(true);
|
|
@@ -391,7 +415,7 @@ describe("installStreamingFetch", () => {
|
|
|
391
415
|
signal: controller.signal,
|
|
392
416
|
});
|
|
393
417
|
|
|
394
|
-
simulateHeaders(xhr, 200);
|
|
418
|
+
await simulateHeaders(xhr, 200);
|
|
395
419
|
const resp = await fetchPromise;
|
|
396
420
|
const reader = resp.body!.getReader();
|
|
397
421
|
|
|
@@ -403,7 +427,7 @@ describe("installStreamingFetch", () => {
|
|
|
403
427
|
|
|
404
428
|
it("cancelling the ReadableStream aborts the XHR", async () => {
|
|
405
429
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
406
|
-
simulateHeaders(xhr, 200);
|
|
430
|
+
await simulateHeaders(xhr, 200);
|
|
407
431
|
const resp = await fetchPromise;
|
|
408
432
|
const reader = resp.body!.getReader();
|
|
409
433
|
|
|
@@ -419,11 +443,11 @@ describe("installStreamingFetch", () => {
|
|
|
419
443
|
const { fetchPromise, xhr } = await fetchAndCapture("https://api.test", {
|
|
420
444
|
signal: controller.signal,
|
|
421
445
|
});
|
|
422
|
-
simulateHeaders(xhr, 200);
|
|
446
|
+
await simulateHeaders(xhr, 200);
|
|
423
447
|
await fetchPromise;
|
|
424
448
|
|
|
425
449
|
xhr.responseText = "done";
|
|
426
|
-
simulateLoad(xhr);
|
|
450
|
+
await simulateLoad(xhr);
|
|
427
451
|
|
|
428
452
|
expect(removeSpy).toHaveBeenCalledWith("abort", expect.any(Function));
|
|
429
453
|
});
|
|
@@ -434,43 +458,59 @@ describe("installStreamingFetch", () => {
|
|
|
434
458
|
describe("error handling", () => {
|
|
435
459
|
it("rejects with TypeError on XHR onerror", async () => {
|
|
436
460
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
437
|
-
|
|
438
|
-
|
|
461
|
+
// Attach rejection handler BEFORE triggering error to avoid
|
|
462
|
+
// Node's unhandled rejection warning (setTimeout defers the rejection)
|
|
463
|
+
const rejection = expect(fetchPromise).rejects.toThrow(
|
|
464
|
+
"Network request failed",
|
|
465
|
+
);
|
|
466
|
+
await simulateError(xhr);
|
|
467
|
+
await rejection;
|
|
439
468
|
});
|
|
440
469
|
|
|
441
470
|
it("rejects with TypeError on XHR ontimeout", async () => {
|
|
442
471
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
443
|
-
|
|
444
|
-
|
|
472
|
+
const rejection = expect(fetchPromise).rejects.toThrow(
|
|
473
|
+
"Network request timed out",
|
|
474
|
+
);
|
|
475
|
+
await simulateTimeout(xhr);
|
|
476
|
+
await rejection;
|
|
445
477
|
});
|
|
446
478
|
|
|
447
479
|
it("rejects with descriptive error on readyState=4 with status=0 (CORS/DNS)", async () => {
|
|
448
480
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
481
|
+
const rejection = expect(fetchPromise).rejects.toThrow(/CORS failure/);
|
|
449
482
|
xhr.readyState = 4;
|
|
450
483
|
xhr.status = 0;
|
|
451
484
|
xhr.onreadystatechange?.();
|
|
452
|
-
await
|
|
485
|
+
await flushTimers();
|
|
486
|
+
await rejection;
|
|
453
487
|
});
|
|
454
488
|
|
|
455
489
|
it("errors stream and rejects text() when onerror fires after headers", async () => {
|
|
456
490
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
457
|
-
simulateHeaders(xhr, 200);
|
|
491
|
+
await simulateHeaders(xhr, 200);
|
|
458
492
|
const resp = await fetchPromise;
|
|
459
493
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
494
|
+
// text() rejection handler must be attached before triggering error
|
|
495
|
+
const textRejection = expect(resp.text()).rejects.toThrow(
|
|
496
|
+
"Network request failed",
|
|
497
|
+
);
|
|
498
|
+
await simulateError(xhr);
|
|
499
|
+
await textRejection;
|
|
463
500
|
});
|
|
464
501
|
|
|
465
502
|
it("does not double-reject (settled guard)", async () => {
|
|
466
503
|
const { fetchPromise, xhr } = await fetchAndCapture();
|
|
467
504
|
|
|
468
|
-
//
|
|
469
|
-
|
|
470
|
-
|
|
505
|
+
// Attach rejection handler before triggering error
|
|
506
|
+
const rejection = expect(fetchPromise).rejects.toThrow(
|
|
507
|
+
"Network request failed",
|
|
508
|
+
);
|
|
509
|
+
await simulateError(xhr);
|
|
510
|
+
await rejection;
|
|
471
511
|
|
|
472
512
|
// Second error should not throw unhandled rejection
|
|
473
|
-
|
|
513
|
+
await simulateTimeout(xhr);
|
|
474
514
|
});
|
|
475
515
|
});
|
|
476
516
|
|