@copilotkit/react-native 2.0.0-rc.3 → 2.0.0-rc.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-BXpkAl1Y.mjs} +55 -42
  2. package/dist/CopilotKitProvider-BXpkAl1Y.mjs.map +1 -0
  3. package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-DDPbJkHS.cjs} +53 -40
  4. package/dist/CopilotKitProvider-DDPbJkHS.cjs.map +1 -0
  5. package/dist/CopilotKitProvider-F7Pn5kp4.d.cts +65 -0
  6. package/dist/CopilotKitProvider-Khgz5dQo.d.mts +65 -0
  7. package/dist/{Markdown-C_9Re36O.cjs → Markdown-CaYHlD4j.cjs} +8 -8
  8. package/dist/Markdown-CaYHlD4j.cjs.map +1 -0
  9. package/dist/{Markdown-1ips7myr.d.cts → Markdown-DzjnSuu5.d.cts} +3 -3
  10. package/dist/{Markdown-C6AsLHmd.mjs → Markdown-SE9MfY2U.mjs} +4 -4
  11. package/dist/Markdown-SE9MfY2U.mjs.map +1 -0
  12. package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-a-rdG22_.d.mts} +3 -3
  13. package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-B_AKdPAN.mjs} +1 -1
  14. package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-B_AKdPAN.mjs.map} +1 -1
  15. package/dist/components/index.cjs +73 -172
  16. package/dist/components/index.cjs.map +1 -1
  17. package/dist/components/index.d.cts +2 -2
  18. package/dist/components/index.d.mts +2 -2
  19. package/dist/components/index.mjs +75 -174
  20. package/dist/components/index.mjs.map +1 -1
  21. package/dist/headless.cjs +2 -1
  22. package/dist/headless.d.cts +2 -2
  23. package/dist/headless.d.mts +2 -2
  24. package/dist/headless.mjs +2 -1
  25. package/dist/index.cjs +126 -158
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +35 -17
  28. package/dist/index.d.mts +36 -18
  29. package/dist/index.mjs +130 -161
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/polyfills/encoding.cjs +5 -5
  32. package/dist/polyfills/encoding.cjs.map +1 -1
  33. package/dist/polyfills/encoding.mjs +5 -4
  34. package/dist/polyfills/encoding.mjs.map +1 -1
  35. package/dist/polyfills.cjs +2 -1
  36. package/dist/polyfills.cjs.map +1 -1
  37. package/dist/polyfills.mjs +2 -1
  38. package/dist/polyfills.mjs.map +1 -1
  39. package/dist/{streaming-fetch-Dm1oN4f2.mjs → streaming-fetch-CLHG3Z1H.mjs} +1 -1
  40. package/dist/{streaming-fetch-Dm1oN4f2.mjs.map → streaming-fetch-CLHG3Z1H.mjs.map} +1 -1
  41. package/dist/{streaming-fetch-Bm91EsRc.cjs → streaming-fetch-DbDALGya.cjs} +1 -1
  42. package/dist/{streaming-fetch-Bm91EsRc.cjs.map → streaming-fetch-DbDALGya.cjs.map} +1 -1
  43. package/dist/utf8-BIN9t4Oy.mjs +113 -0
  44. package/dist/utf8-BIN9t4Oy.mjs.map +1 -0
  45. package/dist/utf8-Cf3Ssy33.cjs +125 -0
  46. package/dist/utf8-Cf3Ssy33.cjs.map +1 -0
  47. package/package.json +12 -21
  48. package/scripts/verify-polyfill-barrel.mjs +4 -6
  49. package/src/CopilotChat.tsx +114 -93
  50. package/src/CopilotKitProvider.tsx +87 -130
  51. package/src/CopilotPopup.tsx +2 -2
  52. package/src/CopilotSidebar.tsx +7 -5
  53. package/src/__mocks__/react-native.ts +31 -0
  54. package/src/__mocks__/test-copilotkit.tsx +9 -3
  55. package/src/__tests__/attachments-integration.test.tsx +15 -2
  56. package/src/__tests__/copilot-chat-agentid.test.tsx +15 -2
  57. package/src/__tests__/copilot-chat-attachments.test.tsx +15 -2
  58. package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
  59. package/src/__tests__/copilot-chat-threads.test.tsx +232 -0
  60. package/src/__tests__/copilot-popup.test.tsx +15 -2
  61. package/src/__tests__/copilot-sidebar.test.tsx +15 -2
  62. package/src/__tests__/copilotkit-provider.test.tsx +68 -73
  63. package/src/__tests__/headless-integration.test.tsx +11 -1
  64. package/src/__tests__/use-attachments.test.ts +27 -2
  65. package/src/components/CopilotChat.tsx +106 -233
  66. package/src/components/CopilotModal.tsx +1 -1
  67. package/src/components/Markdown.tsx +4 -4
  68. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
  69. package/src/components/__tests__/CopilotChat.test.tsx +12 -4
  70. package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
  71. package/src/components/__tests__/Markdown.code-alias.test.tsx +1 -1
  72. package/src/components/messages/AssistantMessage.tsx +3 -3
  73. package/src/components/messages/TypingIndicator.tsx +4 -4
  74. package/src/hooks/use-attachments.ts +95 -139
  75. package/src/polyfills/__tests__/utf8.test.ts +112 -0
  76. package/src/polyfills/encoding.ts +5 -4
  77. package/src/polyfills/utf8.ts +151 -0
  78. package/src/types.d.ts +0 -13
  79. package/dist/CopilotKitProvider-BWjzCdHN.d.mts +0 -121
  80. package/dist/CopilotKitProvider-Btm10msB.d.cts +0 -121
  81. package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +0 -1
  82. package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +0 -1
  83. package/dist/Markdown-C6AsLHmd.mjs.map +0 -1
  84. package/dist/Markdown-C_9Re36O.cjs.map +0 -1
@@ -0,0 +1,113 @@
1
+ //#region src/polyfills/utf8.ts
2
+ /**
3
+ * UTF-8 `TextEncoder` and `TextDecoder`, for runtimes that have neither.
4
+ *
5
+ * CopilotKit only reads and writes UTF-8, so these classes support only that
6
+ * encoding. The decoder follows the WHATWG UTF-8 decode algorithm, so its
7
+ * output, including U+FFFD for invalid bytes and `{ stream: true }` chunks,
8
+ * matches the built-in `TextDecoder`.
9
+ */
10
+ const REPLACEMENT = 65533;
11
+ var Utf8TextEncoder = class {
12
+ encoding = "utf-8";
13
+ encode(input = "") {
14
+ const bytes = [];
15
+ for (const char of input) {
16
+ let codePoint = char.codePointAt(0);
17
+ if (codePoint >= 55296 && codePoint <= 57343) codePoint = REPLACEMENT;
18
+ if (codePoint < 128) bytes.push(codePoint);
19
+ else if (codePoint < 2048) bytes.push(192 | codePoint >> 6, 128 | codePoint & 63);
20
+ else if (codePoint < 65536) bytes.push(224 | codePoint >> 12, 128 | codePoint >> 6 & 63, 128 | codePoint & 63);
21
+ else bytes.push(240 | codePoint >> 18, 128 | codePoint >> 12 & 63, 128 | codePoint >> 6 & 63, 128 | codePoint & 63);
22
+ }
23
+ return new Uint8Array(bytes);
24
+ }
25
+ };
26
+ var Utf8TextDecoder = class {
27
+ encoding = "utf-8";
28
+ fatal;
29
+ ignoreBOM;
30
+ #codePoint = 0;
31
+ #bytesNeeded = 0;
32
+ #bytesSeen = 0;
33
+ #lowerBoundary = 128;
34
+ #upperBoundary = 191;
35
+ #atStart = true;
36
+ constructor(label = "utf-8", options = {}) {
37
+ const normalized = String(label).trim().toLowerCase();
38
+ if (normalized !== "utf-8" && normalized !== "utf8") throw new RangeError(`The "${label}" encoding is not supported.`);
39
+ this.fatal = options.fatal ?? false;
40
+ this.ignoreBOM = options.ignoreBOM ?? false;
41
+ }
42
+ decode(input, options = {}) {
43
+ const bytes = input === void 0 ? new Uint8Array(0) : ArrayBuffer.isView(input) ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength) : new Uint8Array(input);
44
+ const codePoints = [];
45
+ for (let i = 0; i < bytes.length; i++) {
46
+ const byte = bytes[i];
47
+ if (this.#bytesNeeded === 0) {
48
+ if (byte <= 127) codePoints.push(byte);
49
+ else if (byte >= 194 && byte <= 223) this.#start(1, byte & 31);
50
+ else if (byte >= 224 && byte <= 239) {
51
+ if (byte === 224) this.#lowerBoundary = 160;
52
+ if (byte === 237) this.#upperBoundary = 159;
53
+ this.#start(2, byte & 15);
54
+ } else if (byte >= 240 && byte <= 244) {
55
+ if (byte === 240) this.#lowerBoundary = 144;
56
+ if (byte === 244) this.#upperBoundary = 143;
57
+ this.#start(3, byte & 7);
58
+ } else codePoints.push(this.#error());
59
+ continue;
60
+ }
61
+ if (byte < this.#lowerBoundary || byte > this.#upperBoundary) {
62
+ this.#reset();
63
+ codePoints.push(this.#error());
64
+ i--;
65
+ continue;
66
+ }
67
+ this.#lowerBoundary = 128;
68
+ this.#upperBoundary = 191;
69
+ this.#codePoint = this.#codePoint << 6 | byte & 63;
70
+ if (++this.#bytesSeen === this.#bytesNeeded) {
71
+ codePoints.push(this.#codePoint);
72
+ this.#reset();
73
+ }
74
+ }
75
+ if (!options.stream && this.#bytesNeeded > 0) {
76
+ this.#reset();
77
+ codePoints.push(this.#error());
78
+ }
79
+ if (this.#atStart && codePoints.length > 0) {
80
+ this.#atStart = false;
81
+ if (!this.ignoreBOM && codePoints[0] === 65279) codePoints.shift();
82
+ }
83
+ if (!options.stream) this.#atStart = true;
84
+ return codePointsToString(codePoints);
85
+ }
86
+ #start(bytesNeeded, codePoint) {
87
+ this.#bytesNeeded = bytesNeeded;
88
+ this.#codePoint = codePoint;
89
+ }
90
+ #reset() {
91
+ this.#codePoint = 0;
92
+ this.#bytesNeeded = 0;
93
+ this.#bytesSeen = 0;
94
+ this.#lowerBoundary = 128;
95
+ this.#upperBoundary = 191;
96
+ }
97
+ #error() {
98
+ if (this.fatal) {
99
+ this.#atStart = true;
100
+ throw new TypeError("The encoded data was not valid utf-8.");
101
+ }
102
+ return REPLACEMENT;
103
+ }
104
+ };
105
+ function codePointsToString(codePoints) {
106
+ let out = "";
107
+ for (let i = 0; i < codePoints.length; i += 32768) out += String.fromCodePoint(...codePoints.slice(i, i + 32768));
108
+ return out;
109
+ }
110
+
111
+ //#endregion
112
+ export { Utf8TextEncoder as n, Utf8TextDecoder as t };
113
+ //# sourceMappingURL=utf8-BIN9t4Oy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-BIN9t4Oy.mjs","names":["#bytesNeeded","#start","#lowerBoundary","#upperBoundary","#error","#reset","#codePoint","#bytesSeen","#atStart"],"sources":["../src/polyfills/utf8.ts"],"sourcesContent":["/**\n * UTF-8 `TextEncoder` and `TextDecoder`, for runtimes that have neither.\n *\n * CopilotKit only reads and writes UTF-8, so these classes support only that\n * encoding. The decoder follows the WHATWG UTF-8 decode algorithm, so its\n * output, including U+FFFD for invalid bytes and `{ stream: true }` chunks,\n * matches the built-in `TextDecoder`.\n */\n\nconst REPLACEMENT = 0xfffd;\n\nexport class Utf8TextEncoder {\n readonly encoding = \"utf-8\";\n\n encode(input = \"\"): Uint8Array {\n const bytes: number[] = [];\n for (const char of input) {\n let codePoint = char.codePointAt(0)!;\n // A lone surrogate is not a Unicode scalar value.\n if (codePoint >= 0xd800 && codePoint <= 0xdfff) codePoint = REPLACEMENT;\n if (codePoint < 0x80) bytes.push(codePoint);\n else if (codePoint < 0x800)\n bytes.push(0xc0 | (codePoint >> 6), 0x80 | (codePoint & 0x3f));\n else if (codePoint < 0x10000)\n bytes.push(\n 0xe0 | (codePoint >> 12),\n 0x80 | ((codePoint >> 6) & 0x3f),\n 0x80 | (codePoint & 0x3f),\n );\n else\n bytes.push(\n 0xf0 | (codePoint >> 18),\n 0x80 | ((codePoint >> 12) & 0x3f),\n 0x80 | ((codePoint >> 6) & 0x3f),\n 0x80 | (codePoint & 0x3f),\n );\n }\n return new Uint8Array(bytes);\n }\n}\n\nexport class Utf8TextDecoder {\n readonly encoding = \"utf-8\";\n readonly fatal: boolean;\n readonly ignoreBOM: boolean;\n\n #codePoint = 0;\n #bytesNeeded = 0;\n #bytesSeen = 0;\n #lowerBoundary = 0x80;\n #upperBoundary = 0xbf;\n #atStart = true;\n\n constructor(\n label = \"utf-8\",\n options: { fatal?: boolean; ignoreBOM?: boolean } = {},\n ) {\n const normalized = String(label).trim().toLowerCase();\n if (normalized !== \"utf-8\" && normalized !== \"utf8\") {\n throw new RangeError(`The \"${label}\" encoding is not supported.`);\n }\n this.fatal = options.fatal ?? false;\n this.ignoreBOM = options.ignoreBOM ?? false;\n }\n\n decode(\n input?: ArrayBuffer | ArrayBufferView,\n options: { stream?: boolean } = {},\n ): string {\n const bytes =\n input === undefined\n ? new Uint8Array(0)\n : ArrayBuffer.isView(input)\n ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength)\n : new Uint8Array(input);\n const codePoints: number[] = [];\n\n for (let i = 0; i < bytes.length; i++) {\n const byte = bytes[i];\n if (this.#bytesNeeded === 0) {\n if (byte <= 0x7f) codePoints.push(byte);\n else if (byte >= 0xc2 && byte <= 0xdf) this.#start(1, byte & 0x1f);\n else if (byte >= 0xe0 && byte <= 0xef) {\n if (byte === 0xe0) this.#lowerBoundary = 0xa0;\n if (byte === 0xed) this.#upperBoundary = 0x9f;\n this.#start(2, byte & 0x0f);\n } else if (byte >= 0xf0 && byte <= 0xf4) {\n if (byte === 0xf0) this.#lowerBoundary = 0x90;\n if (byte === 0xf4) this.#upperBoundary = 0x8f;\n this.#start(3, byte & 0x07);\n } else codePoints.push(this.#error());\n continue;\n }\n if (byte < this.#lowerBoundary || byte > this.#upperBoundary) {\n // The sequence ends early. Read this byte again as a new sequence.\n this.#reset();\n codePoints.push(this.#error());\n i--;\n continue;\n }\n this.#lowerBoundary = 0x80;\n this.#upperBoundary = 0xbf;\n this.#codePoint = (this.#codePoint << 6) | (byte & 0x3f);\n if (++this.#bytesSeen === this.#bytesNeeded) {\n codePoints.push(this.#codePoint);\n this.#reset();\n }\n }\n\n if (!options.stream && this.#bytesNeeded > 0) {\n this.#reset();\n codePoints.push(this.#error());\n }\n if (this.#atStart && codePoints.length > 0) {\n this.#atStart = false;\n if (!this.ignoreBOM && codePoints[0] === 0xfeff) codePoints.shift();\n }\n if (!options.stream) this.#atStart = true;\n return codePointsToString(codePoints);\n }\n\n #start(bytesNeeded: number, codePoint: number): void {\n this.#bytesNeeded = bytesNeeded;\n this.#codePoint = codePoint;\n }\n\n #reset(): void {\n this.#codePoint = 0;\n this.#bytesNeeded = 0;\n this.#bytesSeen = 0;\n this.#lowerBoundary = 0x80;\n this.#upperBoundary = 0xbf;\n }\n\n #error(): number {\n if (this.fatal) {\n this.#atStart = true;\n throw new TypeError(\"The encoded data was not valid utf-8.\");\n }\n return REPLACEMENT;\n }\n}\n\nfunction codePointsToString(codePoints: number[]): string {\n let out = \"\";\n // Stay under the engine's limit on function arguments.\n for (let i = 0; i < codePoints.length; i += 0x8000) {\n out += String.fromCodePoint(...codePoints.slice(i, i + 0x8000));\n }\n return out;\n}\n"],"mappings":"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"}
@@ -0,0 +1,125 @@
1
+
2
+ //#region src/polyfills/utf8.ts
3
+ /**
4
+ * UTF-8 `TextEncoder` and `TextDecoder`, for runtimes that have neither.
5
+ *
6
+ * CopilotKit only reads and writes UTF-8, so these classes support only that
7
+ * encoding. The decoder follows the WHATWG UTF-8 decode algorithm, so its
8
+ * output, including U+FFFD for invalid bytes and `{ stream: true }` chunks,
9
+ * matches the built-in `TextDecoder`.
10
+ */
11
+ const REPLACEMENT = 65533;
12
+ var Utf8TextEncoder = class {
13
+ encoding = "utf-8";
14
+ encode(input = "") {
15
+ const bytes = [];
16
+ for (const char of input) {
17
+ let codePoint = char.codePointAt(0);
18
+ if (codePoint >= 55296 && codePoint <= 57343) codePoint = REPLACEMENT;
19
+ if (codePoint < 128) bytes.push(codePoint);
20
+ else if (codePoint < 2048) bytes.push(192 | codePoint >> 6, 128 | codePoint & 63);
21
+ else if (codePoint < 65536) bytes.push(224 | codePoint >> 12, 128 | codePoint >> 6 & 63, 128 | codePoint & 63);
22
+ else bytes.push(240 | codePoint >> 18, 128 | codePoint >> 12 & 63, 128 | codePoint >> 6 & 63, 128 | codePoint & 63);
23
+ }
24
+ return new Uint8Array(bytes);
25
+ }
26
+ };
27
+ var Utf8TextDecoder = class {
28
+ encoding = "utf-8";
29
+ fatal;
30
+ ignoreBOM;
31
+ #codePoint = 0;
32
+ #bytesNeeded = 0;
33
+ #bytesSeen = 0;
34
+ #lowerBoundary = 128;
35
+ #upperBoundary = 191;
36
+ #atStart = true;
37
+ constructor(label = "utf-8", options = {}) {
38
+ const normalized = String(label).trim().toLowerCase();
39
+ if (normalized !== "utf-8" && normalized !== "utf8") throw new RangeError(`The "${label}" encoding is not supported.`);
40
+ this.fatal = options.fatal ?? false;
41
+ this.ignoreBOM = options.ignoreBOM ?? false;
42
+ }
43
+ decode(input, options = {}) {
44
+ const bytes = input === void 0 ? new Uint8Array(0) : ArrayBuffer.isView(input) ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength) : new Uint8Array(input);
45
+ const codePoints = [];
46
+ for (let i = 0; i < bytes.length; i++) {
47
+ const byte = bytes[i];
48
+ if (this.#bytesNeeded === 0) {
49
+ if (byte <= 127) codePoints.push(byte);
50
+ else if (byte >= 194 && byte <= 223) this.#start(1, byte & 31);
51
+ else if (byte >= 224 && byte <= 239) {
52
+ if (byte === 224) this.#lowerBoundary = 160;
53
+ if (byte === 237) this.#upperBoundary = 159;
54
+ this.#start(2, byte & 15);
55
+ } else if (byte >= 240 && byte <= 244) {
56
+ if (byte === 240) this.#lowerBoundary = 144;
57
+ if (byte === 244) this.#upperBoundary = 143;
58
+ this.#start(3, byte & 7);
59
+ } else codePoints.push(this.#error());
60
+ continue;
61
+ }
62
+ if (byte < this.#lowerBoundary || byte > this.#upperBoundary) {
63
+ this.#reset();
64
+ codePoints.push(this.#error());
65
+ i--;
66
+ continue;
67
+ }
68
+ this.#lowerBoundary = 128;
69
+ this.#upperBoundary = 191;
70
+ this.#codePoint = this.#codePoint << 6 | byte & 63;
71
+ if (++this.#bytesSeen === this.#bytesNeeded) {
72
+ codePoints.push(this.#codePoint);
73
+ this.#reset();
74
+ }
75
+ }
76
+ if (!options.stream && this.#bytesNeeded > 0) {
77
+ this.#reset();
78
+ codePoints.push(this.#error());
79
+ }
80
+ if (this.#atStart && codePoints.length > 0) {
81
+ this.#atStart = false;
82
+ if (!this.ignoreBOM && codePoints[0] === 65279) codePoints.shift();
83
+ }
84
+ if (!options.stream) this.#atStart = true;
85
+ return codePointsToString(codePoints);
86
+ }
87
+ #start(bytesNeeded, codePoint) {
88
+ this.#bytesNeeded = bytesNeeded;
89
+ this.#codePoint = codePoint;
90
+ }
91
+ #reset() {
92
+ this.#codePoint = 0;
93
+ this.#bytesNeeded = 0;
94
+ this.#bytesSeen = 0;
95
+ this.#lowerBoundary = 128;
96
+ this.#upperBoundary = 191;
97
+ }
98
+ #error() {
99
+ if (this.fatal) {
100
+ this.#atStart = true;
101
+ throw new TypeError("The encoded data was not valid utf-8.");
102
+ }
103
+ return REPLACEMENT;
104
+ }
105
+ };
106
+ function codePointsToString(codePoints) {
107
+ let out = "";
108
+ for (let i = 0; i < codePoints.length; i += 32768) out += String.fromCodePoint(...codePoints.slice(i, i + 32768));
109
+ return out;
110
+ }
111
+
112
+ //#endregion
113
+ Object.defineProperty(exports, 'Utf8TextDecoder', {
114
+ enumerable: true,
115
+ get: function () {
116
+ return Utf8TextDecoder;
117
+ }
118
+ });
119
+ Object.defineProperty(exports, 'Utf8TextEncoder', {
120
+ enumerable: true,
121
+ get: function () {
122
+ return Utf8TextEncoder;
123
+ }
124
+ });
125
+ //# sourceMappingURL=utf8-Cf3Ssy33.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-Cf3Ssy33.cjs","names":["#bytesNeeded","#start","#lowerBoundary","#upperBoundary","#error","#reset","#codePoint","#bytesSeen","#atStart"],"sources":["../src/polyfills/utf8.ts"],"sourcesContent":["/**\n * UTF-8 `TextEncoder` and `TextDecoder`, for runtimes that have neither.\n *\n * CopilotKit only reads and writes UTF-8, so these classes support only that\n * encoding. The decoder follows the WHATWG UTF-8 decode algorithm, so its\n * output, including U+FFFD for invalid bytes and `{ stream: true }` chunks,\n * matches the built-in `TextDecoder`.\n */\n\nconst REPLACEMENT = 0xfffd;\n\nexport class Utf8TextEncoder {\n readonly encoding = \"utf-8\";\n\n encode(input = \"\"): Uint8Array {\n const bytes: number[] = [];\n for (const char of input) {\n let codePoint = char.codePointAt(0)!;\n // A lone surrogate is not a Unicode scalar value.\n if (codePoint >= 0xd800 && codePoint <= 0xdfff) codePoint = REPLACEMENT;\n if (codePoint < 0x80) bytes.push(codePoint);\n else if (codePoint < 0x800)\n bytes.push(0xc0 | (codePoint >> 6), 0x80 | (codePoint & 0x3f));\n else if (codePoint < 0x10000)\n bytes.push(\n 0xe0 | (codePoint >> 12),\n 0x80 | ((codePoint >> 6) & 0x3f),\n 0x80 | (codePoint & 0x3f),\n );\n else\n bytes.push(\n 0xf0 | (codePoint >> 18),\n 0x80 | ((codePoint >> 12) & 0x3f),\n 0x80 | ((codePoint >> 6) & 0x3f),\n 0x80 | (codePoint & 0x3f),\n );\n }\n return new Uint8Array(bytes);\n }\n}\n\nexport class Utf8TextDecoder {\n readonly encoding = \"utf-8\";\n readonly fatal: boolean;\n readonly ignoreBOM: boolean;\n\n #codePoint = 0;\n #bytesNeeded = 0;\n #bytesSeen = 0;\n #lowerBoundary = 0x80;\n #upperBoundary = 0xbf;\n #atStart = true;\n\n constructor(\n label = \"utf-8\",\n options: { fatal?: boolean; ignoreBOM?: boolean } = {},\n ) {\n const normalized = String(label).trim().toLowerCase();\n if (normalized !== \"utf-8\" && normalized !== \"utf8\") {\n throw new RangeError(`The \"${label}\" encoding is not supported.`);\n }\n this.fatal = options.fatal ?? false;\n this.ignoreBOM = options.ignoreBOM ?? false;\n }\n\n decode(\n input?: ArrayBuffer | ArrayBufferView,\n options: { stream?: boolean } = {},\n ): string {\n const bytes =\n input === undefined\n ? new Uint8Array(0)\n : ArrayBuffer.isView(input)\n ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength)\n : new Uint8Array(input);\n const codePoints: number[] = [];\n\n for (let i = 0; i < bytes.length; i++) {\n const byte = bytes[i];\n if (this.#bytesNeeded === 0) {\n if (byte <= 0x7f) codePoints.push(byte);\n else if (byte >= 0xc2 && byte <= 0xdf) this.#start(1, byte & 0x1f);\n else if (byte >= 0xe0 && byte <= 0xef) {\n if (byte === 0xe0) this.#lowerBoundary = 0xa0;\n if (byte === 0xed) this.#upperBoundary = 0x9f;\n this.#start(2, byte & 0x0f);\n } else if (byte >= 0xf0 && byte <= 0xf4) {\n if (byte === 0xf0) this.#lowerBoundary = 0x90;\n if (byte === 0xf4) this.#upperBoundary = 0x8f;\n this.#start(3, byte & 0x07);\n } else codePoints.push(this.#error());\n continue;\n }\n if (byte < this.#lowerBoundary || byte > this.#upperBoundary) {\n // The sequence ends early. Read this byte again as a new sequence.\n this.#reset();\n codePoints.push(this.#error());\n i--;\n continue;\n }\n this.#lowerBoundary = 0x80;\n this.#upperBoundary = 0xbf;\n this.#codePoint = (this.#codePoint << 6) | (byte & 0x3f);\n if (++this.#bytesSeen === this.#bytesNeeded) {\n codePoints.push(this.#codePoint);\n this.#reset();\n }\n }\n\n if (!options.stream && this.#bytesNeeded > 0) {\n this.#reset();\n codePoints.push(this.#error());\n }\n if (this.#atStart && codePoints.length > 0) {\n this.#atStart = false;\n if (!this.ignoreBOM && codePoints[0] === 0xfeff) codePoints.shift();\n }\n if (!options.stream) this.#atStart = true;\n return codePointsToString(codePoints);\n }\n\n #start(bytesNeeded: number, codePoint: number): void {\n this.#bytesNeeded = bytesNeeded;\n this.#codePoint = codePoint;\n }\n\n #reset(): void {\n this.#codePoint = 0;\n this.#bytesNeeded = 0;\n this.#bytesSeen = 0;\n this.#lowerBoundary = 0x80;\n this.#upperBoundary = 0xbf;\n }\n\n #error(): number {\n if (this.fatal) {\n this.#atStart = true;\n throw new TypeError(\"The encoded data was not valid utf-8.\");\n }\n return REPLACEMENT;\n }\n}\n\nfunction codePointsToString(codePoints: number[]): string {\n let out = \"\";\n // Stay under the engine's limit on function arguments.\n for (let i = 0; i < codePoints.length; i += 0x8000) {\n out += String.fromCodePoint(...codePoints.slice(i, i + 0x8000));\n }\n return out;\n}\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copilotkit/react-native",
3
- "version": "2.0.0-rc.3",
3
+ "version": "2.0.0-rc.30",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "ai",
@@ -12,6 +12,9 @@
12
12
  ],
13
13
  "homepage": "https://github.com/CopilotKit/CopilotKit",
14
14
  "license": "MIT",
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
15
18
  "repository": {
16
19
  "type": "git",
17
20
  "url": "https://github.com/CopilotKit/CopilotKit.git"
@@ -73,12 +76,11 @@
73
76
  "access": "public"
74
77
  },
75
78
  "dependencies": {
76
- "@ag-ui/client": "1.0.2",
77
- "@copilotkit/core": "2.0.0-rc.3",
78
- "@copilotkit/markdown-renderer": "2.0.0-rc.3",
79
- "@copilotkit/react-core": "2.0.0-rc.3",
80
- "@copilotkit/shared": "2.0.0-rc.3",
81
- "text-encoding": "^0.7.0",
79
+ "@ag-ui/client": "1.0.4",
80
+ "@copilotkit/core": "2.0.0-rc.30",
81
+ "@copilotkit/markdown": "2.0.0-rc.30",
82
+ "@copilotkit/react-core": "2.0.0-rc.30",
83
+ "@copilotkit/shared": "2.0.0-rc.30",
82
84
  "web-streams-polyfill": "^4.2.0"
83
85
  },
84
86
  "devDependencies": {
@@ -90,17 +92,15 @@
90
92
  "react-dom": "^19.1.0",
91
93
  "tsdown": "^0.20.3",
92
94
  "typescript": "^5.8.3",
93
- "vitest": "^3.2.4"
95
+ "vitest": "^3.2.4",
96
+ "zod": "^4.2.0"
94
97
  },
95
98
  "peerDependencies": {
96
99
  "@gorhom/bottom-sheet": "^5.0.0",
97
100
  "expo-document-picker": ">=12.0.0",
98
101
  "expo-file-system": ">=17.0.0",
99
102
  "react": "^18 || ^19",
100
- "react-native": ">=0.70",
101
- "react-native-gesture-handler": ">=2.0.0",
102
- "react-native-reanimated": ">=3.0.0",
103
- "zod": ">=3.0.0"
103
+ "react-native": ">=0.70"
104
104
  },
105
105
  "peerDependenciesMeta": {
106
106
  "@gorhom/bottom-sheet": {
@@ -111,15 +111,6 @@
111
111
  },
112
112
  "expo-file-system": {
113
113
  "optional": true
114
- },
115
- "react-native-gesture-handler": {
116
- "optional": true
117
- },
118
- "react-native-reanimated": {
119
- "optional": true
120
- },
121
- "zod": {
122
- "optional": true
123
114
  }
124
115
  },
125
116
  "scripts": {
@@ -17,11 +17,9 @@
17
17
  // natively, so asserting they are merely "defined" afterwards would pass on
18
18
  // an empty barrel; clearing them first is what makes the check honest. It
19
19
  // runs in a child process so the probe cannot poison this one.
20
- // - ESM is checked structurally. It cannot be executed here: the encoding
21
- // polyfill takes a named import from `text-encoding`, which is CommonJS.
22
- // Metro rewrites that to a require() and it works in React Native; bare
23
- // Node ESM refuses it. Asserting the five imports survive the bundler is
24
- // the part this file exists to protect.
20
+ // - ESM is checked structurally: the CJS run already proves that each
21
+ // polyfill installs its globals. Asserting the five imports survive the
22
+ // bundler is the part this file exists to protect.
25
23
  //
26
24
  // Invoked from package.json `build`, so a barrel that installs nothing fails
27
25
  // the build rather than reaching npm.
@@ -73,7 +71,7 @@ export function assertBuilt(distDir) {
73
71
 
74
72
  /**
75
73
  * Which polyfill modules the ESM barrel no longer imports. Pure; the ESM barrel
76
- * is read as text rather than executed (see the header note on text-encoding).
74
+ * is read as text rather than executed (see the header note).
77
75
  */
78
76
  export function missingEsmImports(esmSource) {
79
77
  return GROUPS.filter(
@@ -3,14 +3,26 @@ import React, {
3
3
  useCallback,
4
4
  useContext,
5
5
  useEffect,
6
+ useLayoutEffect,
7
+ useMemo,
6
8
  useRef,
9
+ useState,
7
10
  } from "react";
8
11
  import type { ReactNode } from "react";
9
- import { useAgent } from "@copilotkit/react-core/headless";
12
+ import {
13
+ CopilotChatConfigurationProvider,
14
+ useAgent,
15
+ useCopilotChatConfiguration,
16
+ } from "@copilotkit/react-core/headless";
10
17
  import { useCopilotKit } from "@copilotkit/react-core/context";
11
- import { DEFAULT_AGENT_ID, randomUUID } from "@copilotkit/shared";
12
- import type { InputContent } from "@copilotkit/shared";
18
+ import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
13
19
  import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
20
+ import {
21
+ ɵChatConnection,
22
+ ɵFreshThreadId,
23
+ ɵcreateChatSubmitter,
24
+ ɵsubscribeToAgentErrors,
25
+ } from "@copilotkit/core";
14
26
  import { useAttachments } from "./hooks/use-attachments";
15
27
  import type { NativeAttachmentsConfig } from "./hooks/use-attachments";
16
28
  import type { Attachment } from "@copilotkit/shared";
@@ -35,9 +47,10 @@ export interface CopilotChatContextValue {
35
47
  /** Remove an attachment by ID. */
36
48
  removeAttachment: (id: string) => void;
37
49
  /**
38
- * Submit a message with optional attachments.
39
- * Handles consuming ready attachments, building InputContent[],
40
- * calling agent.addMessage, and running the agent.
50
+ * Submit a message with the ready attachments and run the agent. Waits
51
+ * for the thread's connection and for the run in flight. Blank text, and a
52
+ * send while an attachment uploads, are ignored. Resolves once the run has
53
+ * settled.
41
54
  */
42
55
  submitMessage: (text: string) => Promise<void>;
43
56
  }
@@ -67,7 +80,8 @@ export interface CopilotChatBaseProps {
67
80
  * The agent ID to use for this chat session.
68
81
  * Matches the web SDK's CopilotChat `agentId` prop.
69
82
  *
70
- * Resolution order: `agentId` > `agentName` > `"default"`
83
+ * Resolution order: `agentId` > `agentName` > the agent of the chat (or
84
+ * `CopilotChatConfigurationProvider`) around this one > `"default"`
71
85
  */
72
86
  agentId?: string;
73
87
 
@@ -78,15 +92,17 @@ export interface CopilotChatBaseProps {
78
92
  agentName?: string;
79
93
 
80
94
  /**
81
- * Thread ID for this chat session. When provided, the chat will resume
82
- * the specified thread. Matches the web SDK's CopilotChat `threadId` prop.
95
+ * Thread ID for this chat session. When provided, the chat restores the
96
+ * thread from the backend and reconnects to a run in progress. Without it,
97
+ * the chat uses the thread of the chat around it, or a new thread. Matches
98
+ * the web SDK's CopilotChat `threadId` prop.
83
99
  */
84
100
  threadId?: string;
85
101
 
86
102
  /**
87
103
  * Error handler scoped to this chat's agent. Fires in addition to the
88
- * provider-level onError (does not suppress it). Receives only errors
89
- * whose context.agentId matches this chat's agent.
104
+ * provider-level onError (does not suppress it). Receives errors whose
105
+ * context.agentId matches this chat's agent, and errors with no agentId.
90
106
  */
91
107
  onError?: (event: {
92
108
  error: Error;
@@ -130,6 +146,16 @@ export interface CopilotChatProps extends CopilotChatBaseProps {
130
146
  *
131
147
  * Children can access chat state via `useCopilotChatContext()`.
132
148
  *
149
+ * Hooks inside the chat, such as `useAgent` and `useInterrupt`, use this
150
+ * chat's agent when they do not pass an `agentId`. When the chat has a
151
+ * `threadId`, `useAgent` inside the chat sets that thread on each agent it
152
+ * returns, also on an agent that the hook names with its own `agentId`. Only
153
+ * `useAgent({ agentId, runtimeAgentId, threadId })` keeps a different thread.
154
+ *
155
+ * A chat inside another chat uses the outer chat's agent when it has no
156
+ * `agentId`. When the outer chat has a `threadId` and the inner chat does not,
157
+ * the inner chat uses the outer thread. The web `CopilotChat` does the same.
158
+ *
133
159
  * ```tsx
134
160
  * import { CopilotChat, useCopilotChatContext } from "@copilotkit/react-native";
135
161
  *
@@ -153,7 +179,11 @@ export function CopilotChat({
153
179
  children,
154
180
  ..._rest
155
181
  }: CopilotChatProps) {
156
- const resolvedAgentId = agentId ?? agentName ?? DEFAULT_AGENT_ID;
182
+ // Same precedence as the web CopilotChat: a chat that names no agent takes
183
+ // the agent of the chat configuration around it before "default".
184
+ const chatConfig = useCopilotChatConfiguration();
185
+ const resolvedAgentId =
186
+ agentId ?? agentName ?? chatConfig?.agentId ?? DEFAULT_AGENT_ID;
157
187
 
158
188
  // Deprecation warning (dev only, fires once per mount)
159
189
  const warnedRef = useRef(false);
@@ -172,17 +202,37 @@ export function CopilotChat({
172
202
  }
173
203
  }, [agentName, agentId]);
174
204
 
205
+ // Same thread rules as the web CopilotChat: the `threadId` prop, else the
206
+ // thread of the chat around this one, else a UUID minted for this chat.
207
+ // Only a thread a caller picked is "explicit" and restored from the backend.
208
+ const providedThreadId = threadId ?? chatConfig?.threadId;
209
+ const [freshThreadId] = useState(() => new ɵFreshThreadId());
210
+ const resolvedThreadId = useMemo(
211
+ () => freshThreadId.resolve(providedThreadId),
212
+ [freshThreadId, providedThreadId],
213
+ );
214
+ const hasExplicitThreadId = !!threadId || !!chatConfig?.hasExplicitThreadId;
215
+
175
216
  const { agent } = useAgent({ agentId: resolvedAgentId, throttleMs });
217
+ const { copilotkit } = useCopilotKit();
176
218
 
177
- // Set threadId on the agent when provided
219
+ // Keeps the agent on the thread this chat shows, restores a picked thread
220
+ // with a connect, and empties the agent on a switch to a fresh thread. Core
221
+ // owns the connect's abort and detach, as in the web chats.
222
+ const [connection] = useState(
223
+ () => new ɵChatConnection({ core: copilotkit }),
224
+ );
225
+ const { threads } = connection;
178
226
  useEffect(() => {
179
- if (threadId) {
180
- agent.threadId = threadId;
181
- }
182
- }, [agent, threadId]);
227
+ const handle = connection.open({
228
+ agent,
229
+ threadId: resolvedThreadId,
230
+ explicit: hasExplicitThreadId,
231
+ });
232
+ return () => handle.release();
233
+ }, [connection, agent, resolvedThreadId, hasExplicitThreadId]);
183
234
 
184
235
  // onError subscription -- forward core errors scoped to this chat's agent
185
- const { copilotkit } = useCopilotKit();
186
236
  const onErrorRef = useRef(onError);
187
237
  useEffect(() => {
188
238
  onErrorRef.current = onError;
@@ -190,26 +240,9 @@ export function CopilotChat({
190
240
 
191
241
  useEffect(() => {
192
242
  if (!onErrorRef.current) return;
193
-
194
- const subscription = copilotkit.subscribe({
195
- onError: (event) => {
196
- // Only forward errors that match this chat's agent
197
- if (
198
- event.context?.agentId === resolvedAgentId ||
199
- !event.context?.agentId
200
- ) {
201
- onErrorRef.current?.({
202
- error: event.error,
203
- code: event.code,
204
- context: event.context,
205
- });
206
- }
207
- },
208
- });
209
-
210
- return () => {
211
- subscription.unsubscribe();
212
- };
243
+ return ɵsubscribeToAgentErrors(copilotkit, resolvedAgentId, (event) =>
244
+ onErrorRef.current?.(event),
245
+ );
213
246
  }, [copilotkit, resolvedAgentId]);
214
247
 
215
248
  // Attachments
@@ -220,60 +253,39 @@ export function CopilotChat({
220
253
  removeAttachment,
221
254
  consumeAttachments,
222
255
  } = useAttachments({ config: attachmentsConfig });
256
+ // A send waits for the run in flight, so the submitter re-checks the
257
+ // upload guard against the latest attachments, not this render's.
258
+ const attachmentsRef = useRef(selectedAttachments);
259
+ useLayoutEffect(() => {
260
+ attachmentsRef.current = selectedAttachments;
261
+ });
223
262
 
224
- // Submit handler -- mirrors web CopilotChat.tsx lines 234-288
263
+ // The send side lives in core: the upload guard, the message content, and
264
+ // waiting for the thread's connection and the run in flight.
265
+ const agentRef = useRef(agent);
266
+ useLayoutEffect(() => {
267
+ agentRef.current = agent;
268
+ });
269
+ const [submitter] = useState(() =>
270
+ ɵcreateChatSubmitter({
271
+ core: copilotkit,
272
+ getAgent: () => agentRef.current,
273
+ // This chat renders no input box, so there is no text to clear or put
274
+ // back.
275
+ input: { get: () => "", set: () => {} },
276
+ attachments: {
277
+ isUploading: () =>
278
+ attachmentsRef.current.some((a) => a.status === "uploading"),
279
+ consumeReady: () => consumeAttachments(),
280
+ },
281
+ threads,
282
+ }),
283
+ );
284
+ // Clears the notice timer of a blocked send on unmount.
285
+ useEffect(() => () => submitter.showNotice(null), [submitter]);
225
286
  const submitMessage = useCallback(
226
- async (value: string) => {
227
- // Block if uploads in progress
228
- const hasUploading = selectedAttachments.some(
229
- (a) => a.status === "uploading",
230
- );
231
- if (hasUploading) {
232
- console.error(
233
- "[CopilotKit] Cannot send while attachments are uploading",
234
- );
235
- return;
236
- }
237
-
238
- const readyAttachments = consumeAttachments();
239
-
240
- if (readyAttachments.length > 0) {
241
- const contentParts: InputContent[] = [];
242
- if (value.trim()) {
243
- contentParts.push({ type: "text", text: value });
244
- }
245
- for (const att of readyAttachments) {
246
- contentParts.push({
247
- type: att.type,
248
- source: att.source,
249
- metadata: {
250
- ...(att.filename ? { filename: att.filename } : {}),
251
- ...att.metadata,
252
- },
253
- } as InputContent);
254
- }
255
- agent.addMessage({
256
- id: randomUUID(),
257
- role: "user",
258
- content: contentParts,
259
- });
260
- } else {
261
- agent.addMessage({
262
- id: randomUUID(),
263
- role: "user",
264
- content: value,
265
- });
266
- }
267
-
268
- try {
269
- await copilotkit.runAgent({ agent });
270
- } catch (error) {
271
- console.error("CopilotChat: runAgent failed", error);
272
- }
273
- },
274
- // copilotkit is intentionally excluded -- it is a stable ref that never changes.
275
- // eslint-disable-next-line react-hooks/exhaustive-deps
276
- [agent, selectedAttachments, consumeAttachments],
287
+ (value: string) => submitter.submit(value),
288
+ [submitter],
277
289
  );
278
290
 
279
291
  const contextValue: CopilotChatContextValue = {
@@ -287,9 +299,18 @@ export function CopilotChat({
287
299
  submitMessage,
288
300
  };
289
301
 
302
+ // Publish this chat's agent and thread to the subtree, as the web
303
+ // CopilotChat does. Hooks inside it that name no agent use this chat's
304
+ // agent, which is the outer chat's agent when this chat names none.
290
305
  return (
291
- <CopilotChatCtx.Provider value={contextValue}>
292
- {children}
293
- </CopilotChatCtx.Provider>
306
+ <CopilotChatConfigurationProvider
307
+ agentId={resolvedAgentId}
308
+ threadId={resolvedThreadId}
309
+ hasExplicitThreadId={hasExplicitThreadId}
310
+ >
311
+ <CopilotChatCtx.Provider value={contextValue}>
312
+ {children}
313
+ </CopilotChatCtx.Provider>
314
+ </CopilotChatConfigurationProvider>
294
315
  );
295
316
  }