@copilotkit/react-native 2.0.0-rc.10 → 2.0.0-rc.11

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 (45) hide show
  1. package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-DbI3aQam.cjs} +1 -1
  2. package/dist/{CopilotKitProvider-Byw3cqe2.cjs.map → CopilotKitProvider-DbI3aQam.cjs.map} +1 -1
  3. package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-DlP73sLt.mjs} +2 -2
  4. package/dist/{CopilotKitProvider-CE4bc6CP.mjs.map → CopilotKitProvider-DlP73sLt.mjs.map} +1 -1
  5. package/dist/{CopilotKitProvider-BWjzCdHN.d.mts → CopilotKitProvider-DzDWD_Vp.d.mts} +2 -2
  6. package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-Bl3qS5hQ.d.mts} +1 -1
  7. package/dist/{Markdown-C_9Re36O.cjs → Markdown-C4dvKQXJ.cjs} +1 -1
  8. package/dist/{Markdown-C_9Re36O.cjs.map → Markdown-C4dvKQXJ.cjs.map} +1 -1
  9. package/dist/{Markdown-C6AsLHmd.mjs → Markdown-CICvatMs.mjs} +1 -1
  10. package/dist/{Markdown-C6AsLHmd.mjs.map → Markdown-CICvatMs.mjs.map} +1 -1
  11. package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-B_AKdPAN.mjs} +1 -1
  12. package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-B_AKdPAN.mjs.map} +1 -1
  13. package/dist/components/index.cjs +1 -1
  14. package/dist/components/index.d.mts +1 -1
  15. package/dist/components/index.mjs +2 -2
  16. package/dist/headless.cjs +2 -1
  17. package/dist/headless.d.mts +2 -2
  18. package/dist/headless.mjs +2 -1
  19. package/dist/index.cjs +3 -2
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.mts +2 -2
  22. package/dist/index.mjs +4 -3
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/polyfills/encoding.cjs +5 -5
  25. package/dist/polyfills/encoding.cjs.map +1 -1
  26. package/dist/polyfills/encoding.mjs +5 -4
  27. package/dist/polyfills/encoding.mjs.map +1 -1
  28. package/dist/polyfills.cjs +2 -1
  29. package/dist/polyfills.cjs.map +1 -1
  30. package/dist/polyfills.mjs +2 -1
  31. package/dist/polyfills.mjs.map +1 -1
  32. package/dist/{streaming-fetch-Dm1oN4f2.mjs → streaming-fetch-CLHG3Z1H.mjs} +1 -1
  33. package/dist/{streaming-fetch-Dm1oN4f2.mjs.map → streaming-fetch-CLHG3Z1H.mjs.map} +1 -1
  34. package/dist/{streaming-fetch-Bm91EsRc.cjs → streaming-fetch-DbDALGya.cjs} +1 -1
  35. package/dist/{streaming-fetch-Bm91EsRc.cjs.map → streaming-fetch-DbDALGya.cjs.map} +1 -1
  36. package/dist/utf8-BIN9t4Oy.mjs +113 -0
  37. package/dist/utf8-BIN9t4Oy.mjs.map +1 -0
  38. package/dist/utf8-Cf3Ssy33.cjs +125 -0
  39. package/dist/utf8-Cf3Ssy33.cjs.map +1 -0
  40. package/package.json +6 -15
  41. package/scripts/verify-polyfill-barrel.mjs +4 -6
  42. package/src/polyfills/__tests__/utf8.test.ts +112 -0
  43. package/src/polyfills/encoding.ts +5 -4
  44. package/src/polyfills/utf8.ts +151 -0
  45. package/src/types.d.ts +0 -13
@@ -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":";;;;;;;;;AASA,MAAM,cAAc;AAEpB,IAAa,kBAAb,MAA6B;CAC3B,AAAS,WAAW;CAEpB,OAAO,QAAQ,IAAgB;EAC7B,MAAM,QAAkB,EAAE;AAC1B,OAAK,MAAM,QAAQ,OAAO;GACxB,IAAI,YAAY,KAAK,YAAY,EAAE;AAEnC,OAAI,aAAa,SAAU,aAAa,MAAQ,aAAY;AAC5D,OAAI,YAAY,IAAM,OAAM,KAAK,UAAU;YAClC,YAAY,KACnB,OAAM,KAAK,MAAQ,aAAa,GAAI,MAAQ,YAAY,GAAM;YACvD,YAAY,MACnB,OAAM,KACJ,MAAQ,aAAa,IACrB,MAAS,aAAa,IAAK,IAC3B,MAAQ,YAAY,GACrB;OAED,OAAM,KACJ,MAAQ,aAAa,IACrB,MAAS,aAAa,KAAM,IAC5B,MAAS,aAAa,IAAK,IAC3B,MAAQ,YAAY,GACrB;;AAEL,SAAO,IAAI,WAAW,MAAM;;;AAIhC,IAAa,kBAAb,MAA6B;CAC3B,AAAS,WAAW;CACpB,AAAS;CACT,AAAS;CAET,aAAa;CACb,eAAe;CACf,aAAa;CACb,iBAAiB;CACjB,iBAAiB;CACjB,WAAW;CAEX,YACE,QAAQ,SACR,UAAoD,EAAE,EACtD;EACA,MAAM,aAAa,OAAO,MAAM,CAAC,MAAM,CAAC,aAAa;AACrD,MAAI,eAAe,WAAW,eAAe,OAC3C,OAAM,IAAI,WAAW,QAAQ,MAAM,8BAA8B;AAEnE,OAAK,QAAQ,QAAQ,SAAS;AAC9B,OAAK,YAAY,QAAQ,aAAa;;CAGxC,OACE,OACA,UAAgC,EAAE,EAC1B;EACR,MAAM,QACJ,UAAU,SACN,IAAI,WAAW,EAAE,GACjB,YAAY,OAAO,MAAM,GACvB,IAAI,WAAW,MAAM,QAAQ,MAAM,YAAY,MAAM,WAAW,GAChE,IAAI,WAAW,MAAM;EAC7B,MAAM,aAAuB,EAAE;AAE/B,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;GACrC,MAAM,OAAO,MAAM;AACnB,OAAI,MAAKA,gBAAiB,GAAG;AAC3B,QAAI,QAAQ,IAAM,YAAW,KAAK,KAAK;aAC9B,QAAQ,OAAQ,QAAQ,IAAM,OAAKC,MAAO,GAAG,OAAO,GAAK;aACzD,QAAQ,OAAQ,QAAQ,KAAM;AACrC,SAAI,SAAS,IAAM,OAAKC,gBAAiB;AACzC,SAAI,SAAS,IAAM,OAAKC,gBAAiB;AACzC,WAAKF,MAAO,GAAG,OAAO,GAAK;eAClB,QAAQ,OAAQ,QAAQ,KAAM;AACvC,SAAI,SAAS,IAAM,OAAKC,gBAAiB;AACzC,SAAI,SAAS,IAAM,OAAKC,gBAAiB;AACzC,WAAKF,MAAO,GAAG,OAAO,EAAK;UACtB,YAAW,KAAK,MAAKG,OAAQ,CAAC;AACrC;;AAEF,OAAI,OAAO,MAAKF,iBAAkB,OAAO,MAAKC,eAAgB;AAE5D,UAAKE,OAAQ;AACb,eAAW,KAAK,MAAKD,OAAQ,CAAC;AAC9B;AACA;;AAEF,SAAKF,gBAAiB;AACtB,SAAKC,gBAAiB;AACtB,SAAKG,YAAc,MAAKA,aAAc,IAAM,OAAO;AACnD,OAAI,EAAE,MAAKC,cAAe,MAAKP,aAAc;AAC3C,eAAW,KAAK,MAAKM,UAAW;AAChC,UAAKD,OAAQ;;;AAIjB,MAAI,CAAC,QAAQ,UAAU,MAAKL,cAAe,GAAG;AAC5C,SAAKK,OAAQ;AACb,cAAW,KAAK,MAAKD,OAAQ,CAAC;;AAEhC,MAAI,MAAKI,WAAY,WAAW,SAAS,GAAG;AAC1C,SAAKA,UAAW;AAChB,OAAI,CAAC,KAAK,aAAa,WAAW,OAAO,MAAQ,YAAW,OAAO;;AAErE,MAAI,CAAC,QAAQ,OAAQ,OAAKA,UAAW;AACrC,SAAO,mBAAmB,WAAW;;CAGvC,OAAO,aAAqB,WAAyB;AACnD,QAAKR,cAAe;AACpB,QAAKM,YAAa;;CAGpB,SAAe;AACb,QAAKA,YAAa;AAClB,QAAKN,cAAe;AACpB,QAAKO,YAAa;AAClB,QAAKL,gBAAiB;AACtB,QAAKC,gBAAiB;;CAGxB,SAAiB;AACf,MAAI,KAAK,OAAO;AACd,SAAKK,UAAW;AAChB,SAAM,IAAI,UAAU,wCAAwC;;AAE9D,SAAO;;;AAIX,SAAS,mBAAmB,YAA8B;CACxD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,KAAK,MAC1C,QAAO,OAAO,cAAc,GAAG,WAAW,MAAM,GAAG,IAAI,MAAO,CAAC;AAEjE,QAAO"}
@@ -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.10",
3
+ "version": "2.0.0-rc.11",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "ai",
@@ -77,11 +77,10 @@
77
77
  },
78
78
  "dependencies": {
79
79
  "@ag-ui/client": "1.0.2",
80
- "@copilotkit/core": "2.0.0-rc.10",
81
- "@copilotkit/markdown-renderer": "2.0.0-rc.10",
82
- "@copilotkit/react-core": "2.0.0-rc.10",
83
- "@copilotkit/shared": "2.0.0-rc.10",
84
- "text-encoding": "^0.7.0",
80
+ "@copilotkit/core": "2.0.0-rc.11",
81
+ "@copilotkit/markdown-renderer": "2.0.0-rc.11",
82
+ "@copilotkit/react-core": "2.0.0-rc.11",
83
+ "@copilotkit/shared": "2.0.0-rc.11",
85
84
  "web-streams-polyfill": "^4.2.0"
86
85
  },
87
86
  "devDependencies": {
@@ -101,9 +100,7 @@
101
100
  "expo-document-picker": ">=12.0.0",
102
101
  "expo-file-system": ">=17.0.0",
103
102
  "react": "^18 || ^19",
104
- "react-native": ">=0.70",
105
- "react-native-gesture-handler": ">=2.0.0",
106
- "react-native-reanimated": ">=3.0.0"
103
+ "react-native": ">=0.70"
107
104
  },
108
105
  "peerDependenciesMeta": {
109
106
  "@gorhom/bottom-sheet": {
@@ -114,12 +111,6 @@
114
111
  },
115
112
  "expo-file-system": {
116
113
  "optional": true
117
- },
118
- "react-native-gesture-handler": {
119
- "optional": true
120
- },
121
- "react-native-reanimated": {
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(
@@ -0,0 +1,112 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { Utf8TextDecoder, Utf8TextEncoder } from "../utf8";
3
+
4
+ // Node's built-in classes are the reference: the polyfill must give the same
5
+ // output on the runtimes that lack them.
6
+ const NativeEncoder = globalThis.TextEncoder;
7
+ const NativeDecoder = globalThis.TextDecoder;
8
+
9
+ const strings = [
10
+ "",
11
+ "hello",
12
+ "Grüße, 東京, emoji 🚀🧪",
13
+ "\u0000\u007f\u0080߿ࠀ￿",
14
+ "🚀 pair, \ud800 lone high, \udc00 lone low",
15
+ "starts with a BOM",
16
+ ];
17
+
18
+ const byteSequences: number[][] = [
19
+ [0xe2, 0x82, 0xac],
20
+ [0xc3],
21
+ [0xc0, 0x80],
22
+ [0xed, 0xa0, 0x80],
23
+ [0xf4, 0x90, 0x80, 0x80],
24
+ [0xf0, 0x9f, 0x9a],
25
+ [0xe0, 0x80, 0x41],
26
+ [0x80, 0xbf, 0x41],
27
+ [0xff, 0xfe, 0x41],
28
+ [0xef, 0xbb, 0xbf, 0x41],
29
+ [0xf0, 0x9f, 0x98, 0x80, 0xe2, 0x82],
30
+ ];
31
+
32
+ function splits(bytes: Uint8Array): Uint8Array[][] {
33
+ const out: Uint8Array[][] = [[bytes]];
34
+ for (let at = 0; at <= bytes.length; at++) {
35
+ out.push([bytes.subarray(0, at), bytes.subarray(at)]);
36
+ }
37
+ out.push([...bytes].map((byte) => new Uint8Array([byte])));
38
+ return out;
39
+ }
40
+
41
+ function streamDecode(
42
+ decoder: {
43
+ decode: (input?: Uint8Array, options?: { stream?: boolean }) => string;
44
+ },
45
+ chunks: Uint8Array[],
46
+ ): string {
47
+ return (
48
+ chunks.map((chunk) => decoder.decode(chunk, { stream: true })).join("") +
49
+ decoder.decode()
50
+ );
51
+ }
52
+
53
+ describe("Utf8TextEncoder", () => {
54
+ it.each(strings)("encodes %j like the built-in encoder", (input) => {
55
+ expect([...new Utf8TextEncoder().encode(input)]).toEqual([
56
+ ...new NativeEncoder().encode(input),
57
+ ]);
58
+ });
59
+ });
60
+
61
+ describe("Utf8TextDecoder", () => {
62
+ const inputs = [
63
+ ...strings.map((input) => new NativeEncoder().encode(input)),
64
+ ...byteSequences.map((bytes) => new Uint8Array(bytes)),
65
+ ];
66
+
67
+ it.each(inputs.map((bytes) => [[...bytes].join(","), bytes] as const))(
68
+ "decodes [%s] like the built-in decoder, in every chunk split",
69
+ (_, bytes) => {
70
+ for (const chunks of splits(bytes)) {
71
+ for (const ignoreBOM of [false, true]) {
72
+ expect(
73
+ streamDecode(new Utf8TextDecoder("utf-8", { ignoreBOM }), chunks),
74
+ ).toBe(
75
+ streamDecode(new NativeDecoder("utf-8", { ignoreBOM }), chunks),
76
+ );
77
+ }
78
+ }
79
+ },
80
+ );
81
+
82
+ it("throws on invalid bytes when fatal is set", () => {
83
+ for (const bytes of byteSequences.map((b) => new Uint8Array(b))) {
84
+ const native = () =>
85
+ new NativeDecoder("utf-8", { fatal: true }).decode(bytes);
86
+ const polyfill = () =>
87
+ new Utf8TextDecoder("utf-8", { fatal: true }).decode(bytes);
88
+ let nativeThrew = false;
89
+ try {
90
+ native();
91
+ } catch {
92
+ nativeThrew = true;
93
+ }
94
+ if (nativeThrew) expect(polyfill).toThrow(TypeError);
95
+ else expect(polyfill()).toBe(native());
96
+ }
97
+ });
98
+
99
+ it("decodes an ArrayBuffer and a view into a larger buffer", () => {
100
+ const buffer = new NativeEncoder().encode("xx東京xx").buffer;
101
+ expect(new Utf8TextDecoder().decode(buffer)).toBe("xx東京xx");
102
+ expect(new Utf8TextDecoder().decode(new Uint8Array(buffer, 2, 6))).toBe(
103
+ "東京",
104
+ );
105
+ expect(new Utf8TextDecoder().decode()).toBe("");
106
+ });
107
+
108
+ it("supports only UTF-8", () => {
109
+ expect(new Utf8TextDecoder("UTF8").encoding).toBe("utf-8");
110
+ expect(() => new Utf8TextDecoder("latin1")).toThrow(RangeError);
111
+ });
112
+ });
@@ -1,20 +1,21 @@
1
1
  /**
2
2
  * Polyfill: TextEncoder, TextDecoder
3
3
  *
4
- * Required for stream chunk processing in CopilotKit.
4
+ * Required for stream chunk processing in CopilotKit. The polyfill supports
5
+ * UTF-8, the only encoding that CopilotKit uses.
5
6
  * Skipped if the globals are already defined.
6
7
  *
7
8
  * Usage:
8
9
  * import "@copilotkit/react-native/polyfills/encoding";
9
10
  */
10
11
 
11
- import { TextEncoder, TextDecoder } from "text-encoding";
12
+ import { Utf8TextDecoder, Utf8TextEncoder } from "./utf8";
12
13
 
13
14
  const g = globalThis as Record<string, unknown>;
14
15
 
15
16
  if (typeof g.TextEncoder === "undefined") {
16
- g.TextEncoder = TextEncoder;
17
+ g.TextEncoder = Utf8TextEncoder;
17
18
  }
18
19
  if (typeof g.TextDecoder === "undefined") {
19
- g.TextDecoder = TextDecoder;
20
+ g.TextDecoder = Utf8TextDecoder;
20
21
  }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * UTF-8 `TextEncoder` and `TextDecoder`, for runtimes that have neither.
3
+ *
4
+ * CopilotKit only reads and writes UTF-8, so these classes support only that
5
+ * encoding. The decoder follows the WHATWG UTF-8 decode algorithm, so its
6
+ * output, including U+FFFD for invalid bytes and `{ stream: true }` chunks,
7
+ * matches the built-in `TextDecoder`.
8
+ */
9
+
10
+ const REPLACEMENT = 0xfffd;
11
+
12
+ export class Utf8TextEncoder {
13
+ readonly encoding = "utf-8";
14
+
15
+ encode(input = ""): Uint8Array {
16
+ const bytes: number[] = [];
17
+ for (const char of input) {
18
+ let codePoint = char.codePointAt(0)!;
19
+ // A lone surrogate is not a Unicode scalar value.
20
+ if (codePoint >= 0xd800 && codePoint <= 0xdfff) codePoint = REPLACEMENT;
21
+ if (codePoint < 0x80) bytes.push(codePoint);
22
+ else if (codePoint < 0x800)
23
+ bytes.push(0xc0 | (codePoint >> 6), 0x80 | (codePoint & 0x3f));
24
+ else if (codePoint < 0x10000)
25
+ bytes.push(
26
+ 0xe0 | (codePoint >> 12),
27
+ 0x80 | ((codePoint >> 6) & 0x3f),
28
+ 0x80 | (codePoint & 0x3f),
29
+ );
30
+ else
31
+ bytes.push(
32
+ 0xf0 | (codePoint >> 18),
33
+ 0x80 | ((codePoint >> 12) & 0x3f),
34
+ 0x80 | ((codePoint >> 6) & 0x3f),
35
+ 0x80 | (codePoint & 0x3f),
36
+ );
37
+ }
38
+ return new Uint8Array(bytes);
39
+ }
40
+ }
41
+
42
+ export class Utf8TextDecoder {
43
+ readonly encoding = "utf-8";
44
+ readonly fatal: boolean;
45
+ readonly ignoreBOM: boolean;
46
+
47
+ #codePoint = 0;
48
+ #bytesNeeded = 0;
49
+ #bytesSeen = 0;
50
+ #lowerBoundary = 0x80;
51
+ #upperBoundary = 0xbf;
52
+ #atStart = true;
53
+
54
+ constructor(
55
+ label = "utf-8",
56
+ options: { fatal?: boolean; ignoreBOM?: boolean } = {},
57
+ ) {
58
+ const normalized = String(label).trim().toLowerCase();
59
+ if (normalized !== "utf-8" && normalized !== "utf8") {
60
+ throw new RangeError(`The "${label}" encoding is not supported.`);
61
+ }
62
+ this.fatal = options.fatal ?? false;
63
+ this.ignoreBOM = options.ignoreBOM ?? false;
64
+ }
65
+
66
+ decode(
67
+ input?: ArrayBuffer | ArrayBufferView,
68
+ options: { stream?: boolean } = {},
69
+ ): string {
70
+ const bytes =
71
+ input === undefined
72
+ ? new Uint8Array(0)
73
+ : ArrayBuffer.isView(input)
74
+ ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
75
+ : new Uint8Array(input);
76
+ const codePoints: number[] = [];
77
+
78
+ for (let i = 0; i < bytes.length; i++) {
79
+ const byte = bytes[i];
80
+ if (this.#bytesNeeded === 0) {
81
+ if (byte <= 0x7f) codePoints.push(byte);
82
+ else if (byte >= 0xc2 && byte <= 0xdf) this.#start(1, byte & 0x1f);
83
+ else if (byte >= 0xe0 && byte <= 0xef) {
84
+ if (byte === 0xe0) this.#lowerBoundary = 0xa0;
85
+ if (byte === 0xed) this.#upperBoundary = 0x9f;
86
+ this.#start(2, byte & 0x0f);
87
+ } else if (byte >= 0xf0 && byte <= 0xf4) {
88
+ if (byte === 0xf0) this.#lowerBoundary = 0x90;
89
+ if (byte === 0xf4) this.#upperBoundary = 0x8f;
90
+ this.#start(3, byte & 0x07);
91
+ } else codePoints.push(this.#error());
92
+ continue;
93
+ }
94
+ if (byte < this.#lowerBoundary || byte > this.#upperBoundary) {
95
+ // The sequence ends early. Read this byte again as a new sequence.
96
+ this.#reset();
97
+ codePoints.push(this.#error());
98
+ i--;
99
+ continue;
100
+ }
101
+ this.#lowerBoundary = 0x80;
102
+ this.#upperBoundary = 0xbf;
103
+ this.#codePoint = (this.#codePoint << 6) | (byte & 0x3f);
104
+ if (++this.#bytesSeen === this.#bytesNeeded) {
105
+ codePoints.push(this.#codePoint);
106
+ this.#reset();
107
+ }
108
+ }
109
+
110
+ if (!options.stream && this.#bytesNeeded > 0) {
111
+ this.#reset();
112
+ codePoints.push(this.#error());
113
+ }
114
+ if (this.#atStart && codePoints.length > 0) {
115
+ this.#atStart = false;
116
+ if (!this.ignoreBOM && codePoints[0] === 0xfeff) codePoints.shift();
117
+ }
118
+ if (!options.stream) this.#atStart = true;
119
+ return codePointsToString(codePoints);
120
+ }
121
+
122
+ #start(bytesNeeded: number, codePoint: number): void {
123
+ this.#bytesNeeded = bytesNeeded;
124
+ this.#codePoint = codePoint;
125
+ }
126
+
127
+ #reset(): void {
128
+ this.#codePoint = 0;
129
+ this.#bytesNeeded = 0;
130
+ this.#bytesSeen = 0;
131
+ this.#lowerBoundary = 0x80;
132
+ this.#upperBoundary = 0xbf;
133
+ }
134
+
135
+ #error(): number {
136
+ if (this.fatal) {
137
+ this.#atStart = true;
138
+ throw new TypeError("The encoded data was not valid utf-8.");
139
+ }
140
+ return REPLACEMENT;
141
+ }
142
+ }
143
+
144
+ function codePointsToString(codePoints: number[]): string {
145
+ let out = "";
146
+ // Stay under the engine's limit on function arguments.
147
+ for (let i = 0; i < codePoints.length; i += 0x8000) {
148
+ out += String.fromCodePoint(...codePoints.slice(i, i + 0x8000));
149
+ }
150
+ return out;
151
+ }
package/src/types.d.ts CHANGED
@@ -8,16 +8,3 @@ declare const __DEV__: boolean;
8
8
  // (`g`) instead of repeating `(globalThis as any)` on every line — same pattern as
9
9
  // packages/angular/src/test-setup.ts. Files without imports add `export {}` so
10
10
  // TypeScript treats them as modules with isolated scope.
11
-
12
- declare module "text-encoding" {
13
- export class TextEncoder {
14
- encode(input?: string): Uint8Array;
15
- }
16
- export class TextDecoder {
17
- constructor(label?: string, options?: { fatal?: boolean });
18
- decode(
19
- input?: ArrayBufferView | ArrayBuffer,
20
- options?: { stream?: boolean },
21
- ): string;
22
- }
23
- }