@copilotkit/react-native 2.0.0-rc.4 → 2.0.0-rc.40

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 (111) hide show
  1. package/README.md +334 -2
  2. package/USAGE.md +382 -21
  3. package/dist/CopilotKitProvider-Cbfrm_7u.d.mts +65 -0
  4. package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-CvJYytjn.cjs} +56 -42
  5. package/dist/CopilotKitProvider-CvJYytjn.cjs.map +1 -0
  6. package/dist/CopilotKitProvider-Dv3ISMKf.d.cts +65 -0
  7. package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-oTdmylSY.mjs} +55 -42
  8. package/dist/CopilotKitProvider-oTdmylSY.mjs.map +1 -0
  9. package/dist/{Markdown-C_9Re36O.cjs → Markdown-DLopAh5W.cjs} +10 -10
  10. package/dist/Markdown-DLopAh5W.cjs.map +1 -0
  11. package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-Du-b_Qpr.d.mts} +3 -3
  12. package/dist/{Markdown-1ips7myr.d.cts → Markdown-UXZdDjzs.d.cts} +3 -3
  13. package/dist/{Markdown-C6AsLHmd.mjs → Markdown-VxtPJ_yh.mjs} +4 -4
  14. package/dist/Markdown-VxtPJ_yh.mjs.map +1 -0
  15. package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-EobUsERg.mjs} +1 -1
  16. package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-EobUsERg.mjs.map} +1 -1
  17. package/dist/MarkdownRendererContext-cDEHvbw9.cjs +40 -0
  18. package/dist/{MarkdownRendererContext-BRNe808d.cjs.map → MarkdownRendererContext-cDEHvbw9.cjs.map} +1 -1
  19. package/dist/chunk-C0xms8kb.cjs +34 -0
  20. package/dist/components/index.cjs +78 -175
  21. package/dist/components/index.cjs.map +1 -1
  22. package/dist/components/index.d.cts +2 -2
  23. package/dist/components/index.d.mts +2 -2
  24. package/dist/components/index.mjs +76 -174
  25. package/dist/components/index.mjs.map +1 -1
  26. package/dist/generative-ui/index.cjs +612 -0
  27. package/dist/generative-ui/index.cjs.map +1 -0
  28. package/dist/generative-ui/index.d.cts +341 -0
  29. package/dist/generative-ui/index.d.mts +341 -0
  30. package/dist/generative-ui/index.mjs +549 -0
  31. package/dist/generative-ui/index.mjs.map +1 -0
  32. package/dist/headless.cjs +2 -2
  33. package/dist/headless.d.cts +2 -2
  34. package/dist/headless.d.mts +2 -2
  35. package/dist/headless.mjs +1 -1
  36. package/dist/index.cjs +131 -162
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +36 -18
  39. package/dist/index.d.mts +36 -18
  40. package/dist/index.mjs +130 -161
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/polyfills/encoding.cjs +115 -5
  43. package/dist/polyfills/encoding.cjs.map +1 -1
  44. package/dist/polyfills/encoding.d.cts +31 -1
  45. package/dist/polyfills/encoding.d.mts +31 -1
  46. package/dist/polyfills/encoding.mjs +113 -6
  47. package/dist/polyfills/encoding.mjs.map +1 -1
  48. package/dist/polyfills/streams.cjs +1 -1
  49. package/dist/polyfills.cjs +218 -2
  50. package/dist/polyfills.cjs.map +1 -1
  51. package/dist/polyfills.mjs +217 -1
  52. package/dist/polyfills.mjs.map +1 -1
  53. package/package.json +51 -21
  54. package/project.json +6 -1
  55. package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
  56. package/scripts/verify-polyfill-barrel.mjs +158 -11
  57. package/src/CopilotChat.tsx +114 -93
  58. package/src/CopilotKitProvider.tsx +87 -130
  59. package/src/CopilotPopup.tsx +2 -2
  60. package/src/CopilotSidebar.tsx +7 -5
  61. package/src/__mocks__/react-native.ts +31 -0
  62. package/src/__mocks__/test-copilotkit.tsx +9 -3
  63. package/src/__tests__/attachments-integration.test.tsx +15 -2
  64. package/src/__tests__/copilot-chat-agentid.test.tsx +15 -2
  65. package/src/__tests__/copilot-chat-attachments.test.tsx +15 -2
  66. package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
  67. package/src/__tests__/copilot-chat-threads.test.tsx +232 -0
  68. package/src/__tests__/copilot-popup.test.tsx +15 -2
  69. package/src/__tests__/copilot-sidebar.test.tsx +15 -2
  70. package/src/__tests__/copilotkit-provider.test.tsx +68 -73
  71. package/src/__tests__/generative-ui-harness.tsx +243 -0
  72. package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
  73. package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
  74. package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
  75. package/src/__tests__/generative-ui.test.tsx +1000 -0
  76. package/src/__tests__/headless-entry-surface.test.ts +979 -60
  77. package/src/__tests__/headless-integration.test.tsx +11 -1
  78. package/src/__tests__/use-attachments.test.ts +27 -2
  79. package/src/components/CopilotChat.tsx +106 -233
  80. package/src/components/CopilotModal.tsx +1 -1
  81. package/src/components/Markdown.tsx +4 -4
  82. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
  83. package/src/components/__tests__/CopilotChat.test.tsx +12 -4
  84. package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
  85. package/src/components/__tests__/Markdown.code-alias.test.tsx +1 -1
  86. package/src/components/index.ts +6 -0
  87. package/src/components/messages/AssistantMessage.tsx +3 -3
  88. package/src/components/messages/TypingIndicator.tsx +4 -4
  89. package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
  90. package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
  91. package/src/generative-ui/GenerativeUISurface.tsx +303 -0
  92. package/src/generative-ui/host.tsx +138 -0
  93. package/src/generative-ui/index.ts +93 -0
  94. package/src/generative-ui/internal.ts +55 -0
  95. package/src/hooks/use-attachments.ts +95 -139
  96. package/src/index.ts +8 -3
  97. package/src/polyfills/__tests__/encoding-utf8.test.ts +112 -0
  98. package/src/polyfills/encoding.ts +160 -4
  99. package/src/types.d.ts +0 -13
  100. package/tsdown.config.ts +1 -0
  101. package/dist/CopilotKitProvider-BWjzCdHN.d.mts +0 -121
  102. package/dist/CopilotKitProvider-Btm10msB.d.cts +0 -121
  103. package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +0 -1
  104. package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +0 -1
  105. package/dist/Markdown-C6AsLHmd.mjs.map +0 -1
  106. package/dist/Markdown-C_9Re36O.cjs.map +0 -1
  107. package/dist/MarkdownRendererContext-BRNe808d.cjs +0 -72
  108. package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
  109. package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
  110. package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
  111. package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copilotkit/react-native",
3
- "version": "2.0.0-rc.4",
3
+ "version": "2.0.0-rc.40",
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"
@@ -20,16 +23,51 @@
20
23
  "sideEffects": [
21
24
  "./src/index.*",
22
25
  "./src/headless.*",
26
+ "./src/components/index.*",
27
+ "./src/generative-ui/index.*",
23
28
  "./src/polyfills.*",
24
29
  "./src/polyfills/**/*",
25
30
  "./dist/index.*",
26
31
  "./dist/headless.*",
32
+ "./dist/components/index.*",
33
+ "./dist/generative-ui/index.*",
27
34
  "./dist/polyfills.*",
28
35
  "./dist/polyfills/**/*"
29
36
  ],
30
37
  "main": "./dist/index.cjs",
31
38
  "module": "./dist/index.mjs",
32
39
  "types": "./dist/index.d.cts",
40
+ "typesVersions": {
41
+ "*": {
42
+ "headless": [
43
+ "./dist/headless.d.cts"
44
+ ],
45
+ "generative-ui": [
46
+ "./dist/generative-ui/index.d.cts"
47
+ ],
48
+ "polyfills": [
49
+ "./dist/polyfills.d.cts"
50
+ ],
51
+ "polyfills/streams": [
52
+ "./dist/polyfills/streams.d.cts"
53
+ ],
54
+ "polyfills/encoding": [
55
+ "./dist/polyfills/encoding.d.cts"
56
+ ],
57
+ "polyfills/crypto": [
58
+ "./dist/polyfills/crypto.d.cts"
59
+ ],
60
+ "polyfills/dom": [
61
+ "./dist/polyfills/dom.d.cts"
62
+ ],
63
+ "polyfills/location": [
64
+ "./dist/polyfills/location.d.cts"
65
+ ],
66
+ "components": [
67
+ "./dist/components/index.d.cts"
68
+ ]
69
+ }
70
+ },
33
71
  "exports": {
34
72
  ".": {
35
73
  "import": "./dist/index.mjs",
@@ -39,6 +77,10 @@
39
77
  "import": "./dist/headless.mjs",
40
78
  "require": "./dist/headless.cjs"
41
79
  },
80
+ "./generative-ui": {
81
+ "import": "./dist/generative-ui/index.mjs",
82
+ "require": "./dist/generative-ui/index.cjs"
83
+ },
42
84
  "./polyfills": {
43
85
  "import": "./dist/polyfills.mjs",
44
86
  "require": "./dist/polyfills.cjs"
@@ -73,12 +115,11 @@
73
115
  "access": "public"
74
116
  },
75
117
  "dependencies": {
76
- "@ag-ui/client": "1.0.2",
77
- "@copilotkit/core": "2.0.0-rc.4",
78
- "@copilotkit/markdown-renderer": "2.0.0-rc.4",
79
- "@copilotkit/react-core": "2.0.0-rc.4",
80
- "@copilotkit/shared": "2.0.0-rc.4",
81
- "text-encoding": "^0.7.0",
118
+ "@ag-ui/client": "1.0.4",
119
+ "@copilotkit/core": "2.0.0-rc.40",
120
+ "@copilotkit/markdown": "2.0.0-rc.40",
121
+ "@copilotkit/react-core": "2.0.0-rc.40",
122
+ "@copilotkit/shared": "2.0.0-rc.40",
82
123
  "web-streams-polyfill": "^4.2.0"
83
124
  },
84
125
  "devDependencies": {
@@ -90,17 +131,15 @@
90
131
  "react-dom": "^19.1.0",
91
132
  "tsdown": "^0.20.3",
92
133
  "typescript": "^5.8.3",
93
- "vitest": "^3.2.4"
134
+ "vitest": "^3.2.4",
135
+ "zod": "^4.2.0"
94
136
  },
95
137
  "peerDependencies": {
96
138
  "@gorhom/bottom-sheet": "^5.0.0",
97
139
  "expo-document-picker": ">=12.0.0",
98
140
  "expo-file-system": ">=17.0.0",
99
141
  "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"
142
+ "react-native": ">=0.70"
104
143
  },
105
144
  "peerDependenciesMeta": {
106
145
  "@gorhom/bottom-sheet": {
@@ -111,15 +150,6 @@
111
150
  },
112
151
  "expo-file-system": {
113
152
  "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
153
  }
124
154
  },
125
155
  "scripts": {
package/project.json CHANGED
@@ -15,7 +15,12 @@
15
15
  "outputs": ["{projectRoot}/dist/**"]
16
16
  },
17
17
  "test": {
18
- "inputs": ["test", "{projectRoot}/scripts/**"]
18
+ "inputs": [
19
+ "test",
20
+ "{projectRoot}/scripts/**",
21
+ "^production",
22
+ { "dependentTasksOutputFiles": "**/*", "transitive": true }
23
+ ]
19
24
  }
20
25
  }
21
26
  }
@@ -20,6 +20,9 @@ import path from "node:path";
20
20
  import {
21
21
  assertBuilt,
22
22
  BUILD_COMMAND,
23
+ checkEntryOrder,
24
+ firstCjsSpecifier,
25
+ firstEsmSpecifier,
23
26
  GROUPS,
24
27
  isEntrypoint,
25
28
  missingEsmImports,
@@ -32,6 +35,61 @@ const emptyBarrel = path.join(fixtureDir, "dist-empty-barrel");
32
35
  const completeBarrel = path.join(fixtureDir, "dist-complete-barrel");
33
36
  const scriptPath = path.join(here, "..", "verify-polyfill-barrel.mjs");
34
37
 
38
+ // The CLI reads package.json next to dist/. Only the polyfill subpath, so the
39
+ // barrel fixtures are checked on their own.
40
+ const BARREL_ONLY_MANIFEST = JSON.stringify({
41
+ exports: {
42
+ "./polyfills": {
43
+ import: "./dist/polyfills.mjs",
44
+ require: "./dist/polyfills.cjs",
45
+ },
46
+ },
47
+ });
48
+
49
+ /**
50
+ * A package root with one `./generative-ui` code entry built as `esm` / `cjs`,
51
+ * beside the complete barrel.
52
+ */
53
+ function packageWithEntry({ esm, cjs }) {
54
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "rn-entry-order-"));
55
+ fs.cpSync(completeBarrel, path.join(root, "dist"), { recursive: true });
56
+ fs.mkdirSync(path.join(root, "dist", "generative-ui"));
57
+ fs.writeFileSync(path.join(root, "dist", "generative-ui", "index.mjs"), esm);
58
+ fs.writeFileSync(path.join(root, "dist", "generative-ui", "index.cjs"), cjs);
59
+ fs.writeFileSync(
60
+ path.join(root, "package.json"),
61
+ JSON.stringify({
62
+ exports: {
63
+ "./generative-ui": {
64
+ import: "./dist/generative-ui/index.mjs",
65
+ require: "./dist/generative-ui/index.cjs",
66
+ },
67
+ "./polyfills": {
68
+ import: "./dist/polyfills.mjs",
69
+ require: "./dist/polyfills.cjs",
70
+ },
71
+ "./package.json": "./package.json",
72
+ },
73
+ }),
74
+ );
75
+ return root;
76
+ }
77
+
78
+ // What tsdown emitted for the generative UI entry before it imported the
79
+ // polyfills: core-loading modules first, no polyfill barrel at all.
80
+ const UNPOLYFILLED_ESM =
81
+ 'import React from "react";\n' +
82
+ 'import { useCopilotKit } from "@copilotkit/react-core/context";\n';
83
+ const UNPOLYFILLED_CJS =
84
+ "Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n" +
85
+ "const require_chunk = require('../chunk-C0xms8kb.cjs');\n" +
86
+ 'let react = require("react");\n';
87
+ const POLYFILLED_ESM = 'import "../polyfills.mjs";\n' + UNPOLYFILLED_ESM;
88
+ const POLYFILLED_CJS = UNPOLYFILLED_CJS.replace(
89
+ 'let react = require("react");',
90
+ "require('../polyfills.cjs');\nlet react = require(\"react\");",
91
+ );
92
+
35
93
  describe("verifyBarrel", () => {
36
94
  it("rejects the tree-shaken barrel that shipped in 1.69.2", () => {
37
95
  const report = verifyBarrel(emptyBarrel);
@@ -96,6 +154,73 @@ describe("verifyBarrel", () => {
96
154
  });
97
155
  });
98
156
 
157
+ describe("checkEntryOrder", () => {
158
+ it("rejects a published entry that loads CopilotKit before the polyfills", () => {
159
+ const root = packageWithEntry({
160
+ esm: UNPOLYFILLED_ESM,
161
+ cjs: UNPOLYFILLED_CJS,
162
+ });
163
+ const results = checkEntryOrder(root);
164
+ // Both formats, and nothing for the polyfill subpath or the manifest.
165
+ assert.deepEqual(
166
+ results.map(({ file, first, ok }) => ({ file, first, ok })),
167
+ [
168
+ { file: "./dist/generative-ui/index.mjs", first: "react", ok: false },
169
+ { file: "./dist/generative-ui/index.cjs", first: "react", ok: false },
170
+ ],
171
+ );
172
+ fs.rmSync(root, { recursive: true, force: true });
173
+ });
174
+
175
+ it("accepts an entry whose first load is the polyfill barrel", () => {
176
+ const root = packageWithEntry({
177
+ esm: POLYFILLED_ESM,
178
+ cjs: POLYFILLED_CJS,
179
+ });
180
+ const results = checkEntryOrder(root);
181
+ assert.equal(results.length, 2);
182
+ for (const result of results) assert.equal(result.ok, true, result.file);
183
+ fs.rmSync(root, { recursive: true, force: true });
184
+ });
185
+
186
+ it("rejects an entry whose polyfill import is present but not first", () => {
187
+ // The root entry's real regression: the bundler hoisted a shared chunk
188
+ // that loads @copilotkit/react-core above the import of the polyfills.
189
+ const root = packageWithEntry({
190
+ esm: UNPOLYFILLED_ESM + 'import "../polyfills.mjs";\n',
191
+ cjs: UNPOLYFILLED_CJS + "require('../polyfills.cjs');\n",
192
+ });
193
+ for (const result of checkEntryOrder(root)) {
194
+ assert.equal(result.ok, false, result.file);
195
+ }
196
+ fs.rmSync(root, { recursive: true, force: true });
197
+ });
198
+
199
+ it("reports an entry that was not built instead of skipping it", () => {
200
+ const root = packageWithEntry({ esm: POLYFILLED_ESM, cjs: POLYFILLED_CJS });
201
+ fs.rmSync(path.join(root, "dist", "generative-ui", "index.cjs"));
202
+ const missing = checkEntryOrder(root).find((r) => r.file.endsWith(".cjs"));
203
+ assert.equal(missing.ok, false);
204
+ assert.equal(missing.error, "not built");
205
+ fs.rmSync(root, { recursive: true, force: true });
206
+ });
207
+ });
208
+
209
+ describe("firstEsmSpecifier / firstCjsSpecifier", () => {
210
+ it("reads the first module, past comments and rolldown's runtime chunk", () => {
211
+ assert.equal(
212
+ firstEsmSpecifier(
213
+ '//#region x\nimport "./polyfills.mjs";\nimport a from "a";',
214
+ ),
215
+ "./polyfills.mjs",
216
+ );
217
+ assert.equal(firstEsmSpecifier('export * from "./x.mjs";'), "./x.mjs");
218
+ assert.equal(firstEsmSpecifier("const a = 1;"), null);
219
+ assert.equal(firstCjsSpecifier(POLYFILLED_CJS), "../polyfills.cjs");
220
+ assert.equal(firstCjsSpecifier(UNPOLYFILLED_CJS), "react");
221
+ });
222
+ });
223
+
99
224
  describe("missingEsmImports", () => {
100
225
  it("names every group the ESM barrel no longer imports", () => {
101
226
  assert.deepEqual(
@@ -157,6 +282,7 @@ describe("cli", () => {
157
282
  fs.mkdirSync(path.join(fakeRoot, "scripts"));
158
283
  fs.cpSync(scriptPath, path.join(fakeRoot, "scripts", "verify.mjs"));
159
284
  fs.cpSync(emptyBarrel, path.join(fakeRoot, "dist"), { recursive: true });
285
+ fs.writeFileSync(path.join(fakeRoot, "package.json"), BARREL_ONLY_MANIFEST);
160
286
 
161
287
  const result = spawnSync(
162
288
  process.execPath,
@@ -174,6 +300,7 @@ describe("cli", () => {
174
300
  fs.mkdirSync(path.join(fakeRoot, "scripts"));
175
301
  fs.cpSync(scriptPath, path.join(fakeRoot, "scripts", "verify.mjs"));
176
302
  fs.cpSync(completeBarrel, path.join(fakeRoot, "dist"), { recursive: true });
303
+ fs.writeFileSync(path.join(fakeRoot, "package.json"), BARREL_ONLY_MANIFEST);
177
304
 
178
305
  const result = spawnSync(
179
306
  process.execPath,
@@ -184,4 +311,27 @@ describe("cli", () => {
184
311
  assert.equal(JSON.parse(result.stdout).ok, true);
185
312
  fs.rmSync(fakeRoot, { recursive: true, force: true });
186
313
  });
314
+
315
+ it("fails the build when a published entry loads CopilotKit before the polyfills", () => {
316
+ const fakeRoot = packageWithEntry({
317
+ esm: UNPOLYFILLED_ESM,
318
+ cjs: UNPOLYFILLED_CJS,
319
+ });
320
+ fs.mkdirSync(path.join(fakeRoot, "scripts"));
321
+ fs.cpSync(scriptPath, path.join(fakeRoot, "scripts", "verify.mjs"));
322
+
323
+ const result = spawnSync(
324
+ process.execPath,
325
+ [path.join(fakeRoot, "scripts", "verify.mjs")],
326
+ { encoding: "utf8" },
327
+ );
328
+ assert.equal(result.status, 1);
329
+ assert.match(
330
+ result.stdout,
331
+ /FAIL {2}\.\/dist\/generative-ui\/index\.mjs +first loads react/,
332
+ );
333
+ assert.match(result.stderr, /expected \.\.\/polyfills\.cjs/);
334
+ assert.match(result.stderr, /sideEffects/);
335
+ fs.rmSync(fakeRoot, { recursive: true, force: true });
336
+ });
187
337
  });
@@ -17,11 +17,17 @@
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.
23
+ //
24
+ // Then a third check, over every published code entry in package.json
25
+ // `exports` (all but the polyfill subpaths): the built import and require
26
+ // files must load the polyfill barrel before anything else. An app can import
27
+ // any entry first, and @copilotkit/core and @ag-ui/client fail at load time on
28
+ // Hermes without the polyfills. It reads dist/ because the bundler decides the
29
+ // emitted order: it hoisted a shared provider chunk above the root entry's
30
+ // `export * from "./headless"`, so the source order alone proved nothing.
25
31
  //
26
32
  // Invoked from package.json `build`, so a barrel that installs nothing fails
27
33
  // the build rather than reaching npm.
@@ -73,7 +79,7 @@ export function assertBuilt(distDir) {
73
79
 
74
80
  /**
75
81
  * 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).
82
+ * is read as text rather than executed (see the header note).
77
83
  */
78
84
  export function missingEsmImports(esmSource) {
79
85
  return GROUPS.filter(
@@ -138,7 +144,111 @@ export function verifyBarrel(distDir) {
138
144
  return { ok, cjs, esmMissing };
139
145
  }
140
146
 
141
- export function formatReport({ cjs, esmMissing }) {
147
+ /**
148
+ * Export subpaths that are the polyfills themselves, or carry no code. Every
149
+ * other published entry loads @copilotkit/core or @ag-ui/client, which fail at
150
+ * load time on Hermes without the polyfills, so each must install them first:
151
+ * an app can import any entry before the root one.
152
+ */
153
+ const isPolyfillOrManifestExport = (key) =>
154
+ key === "./package.json" ||
155
+ key === "./polyfills" ||
156
+ key.startsWith("./polyfills/");
157
+
158
+ /** The first module an ESM file imports or re-exports, in order. */
159
+ export function firstEsmSpecifier(source) {
160
+ const m =
161
+ /^\s*(?:import\s*(?:[^;"'`]*?\bfrom\s*)?|export\s[^;"'`]*?\bfrom\s*)["'`]([^"'`]+)["'`]/m.exec(
162
+ source.replace(/\/\*[\s\S]*?\*\/|^\s*\/\/[^\n]*$/gm, ""),
163
+ );
164
+ return m ? m[1] : null;
165
+ }
166
+
167
+ /**
168
+ * The first module a CJS file requires, in order, skipping the bundler's own
169
+ * runtime helper chunk (rolldown's `chunk-*.cjs`: `__toESM` and friends, which
170
+ * requires nothing and touches no global).
171
+ */
172
+ export function firstCjsSpecifier(source) {
173
+ for (const m of source.matchAll(/\brequire\s*\(\s*(["'])([^"']+)\1\s*\)/g)) {
174
+ if (/(?:^|\/)chunk-[\w-]+\.cjs$/.test(m[2])) continue;
175
+ return m[2];
176
+ }
177
+ return null;
178
+ }
179
+
180
+ /**
181
+ * For every published code entry (from package.json `exports`), whether its
182
+ * BUILT import and require targets load the polyfill barrel before anything
183
+ * else. Checked in dist/, because the bundler decides the emitted order: a
184
+ * shared chunk can be hoisted above a re-export that installs the polyfills.
185
+ */
186
+ export function checkEntryOrder(packageDir) {
187
+ const manifestPath = path.join(packageDir, "package.json");
188
+ if (!existsSync(manifestPath)) {
189
+ throw new Error(`No package.json at ${manifestPath}.`);
190
+ }
191
+ const { exports: exportsMap } = JSON.parse(
192
+ readFileSync(manifestPath, "utf8"),
193
+ );
194
+ const results = [];
195
+ for (const [key, value] of Object.entries(exportsMap ?? {})) {
196
+ if (isPolyfillOrManifestExport(key)) continue;
197
+ const targets =
198
+ typeof value === "string" ? { import: value } : (value ?? {});
199
+ for (const [condition, target] of Object.entries(targets)) {
200
+ if (condition !== "import" && condition !== "require") continue;
201
+ const file = path.join(packageDir, target);
202
+ const cjs = file.endsWith(".cjs");
203
+ let expected = path
204
+ .relative(
205
+ path.dirname(file),
206
+ path.join(
207
+ packageDir,
208
+ "dist",
209
+ cjs ? "polyfills.cjs" : "polyfills.mjs",
210
+ ),
211
+ )
212
+ .split(path.sep)
213
+ .join("/");
214
+ if (!expected.startsWith(".")) expected = `./${expected}`;
215
+ if (!existsSync(file)) {
216
+ results.push({
217
+ entry: key,
218
+ file: target,
219
+ expected,
220
+ first: null,
221
+ ok: false,
222
+ error: "not built",
223
+ });
224
+ continue;
225
+ }
226
+ const source = readFileSync(file, "utf8");
227
+ const first = cjs ? firstCjsSpecifier(source) : firstEsmSpecifier(source);
228
+ results.push({
229
+ entry: key,
230
+ file: target,
231
+ expected,
232
+ first,
233
+ ok: first === expected,
234
+ });
235
+ }
236
+ }
237
+ return results;
238
+ }
239
+
240
+ /** The barrel check plus the entry-order check, for a built package. */
241
+ export function verifyPackage(packageDir) {
242
+ const barrel = verifyBarrel(path.join(packageDir, "dist"));
243
+ const entries = checkEntryOrder(packageDir);
244
+ return {
245
+ ...barrel,
246
+ entries,
247
+ ok: barrel.ok && entries.every((entry) => entry.ok),
248
+ };
249
+ }
250
+
251
+ export function formatReport({ cjs, esmMissing, entries = [] }) {
142
252
  const lines = cjs.map(
143
253
  (entry) =>
144
254
  `${entry.missing.length === 0 ? "PASS" : "FAIL"} cjs ${entry.group.padEnd(9)} ` +
@@ -150,10 +260,41 @@ export function formatReport({ cjs, esmMissing }) {
150
260
  `import "./polyfills/${group.name}.mjs"`,
151
261
  );
152
262
  }
263
+ for (const entry of entries) {
264
+ lines.push(
265
+ `${entry.ok ? "PASS" : "FAIL"} ${entry.file.padEnd(32)} ` +
266
+ `first loads ${entry.first ?? `(${entry.error ?? "nothing"})`}`,
267
+ );
268
+ }
153
269
  return lines.join("\n");
154
270
  }
155
271
 
156
- export function failureMessage({ cjs, esmMissing }) {
272
+ export function failureMessage({ cjs, esmMissing, entries = [] }) {
273
+ const misordered = entries.filter((entry) => !entry.ok);
274
+ if (
275
+ misordered.length &&
276
+ cjs.every((entry) => entry.missing.length === 0) &&
277
+ esmMissing.length === 0
278
+ ) {
279
+ return (
280
+ `A published entry does not install the polyfills before anything else.\n` +
281
+ misordered
282
+ .map(
283
+ (entry) =>
284
+ ` ${entry.file} (${entry.entry}) first loads ` +
285
+ `${entry.first ?? `(${entry.error ?? "nothing"})`}, expected ${entry.expected}`,
286
+ )
287
+ .join("\n") +
288
+ `\n\nAn app can import any entry before the root one, and on Hermes\n` +
289
+ `@copilotkit/core and @ag-ui/client fail at load time without the polyfills.\n` +
290
+ `Make \`import "./polyfills"\` (relative) the entry's first import, and list\n` +
291
+ `the entry's src and dist paths in package.json "sideEffects".`
292
+ );
293
+ }
294
+ return failureMessageForBarrel({ cjs, esmMissing }, misordered);
295
+ }
296
+
297
+ function failureMessageForBarrel({ cjs, esmMissing }, misordered) {
157
298
  const dropped = [
158
299
  ...cjs.flatMap((entry) => entry.missing),
159
300
  ...esmMissing.map((name) => `import "./polyfills/${name}.mjs"`),
@@ -163,7 +304,10 @@ export function failureMessage({ cjs, esmMissing }) {
163
304
  `Missing: ${dropped.join(", ")}\n\n` +
164
305
  `The side-effect imports in src/polyfills.ts were tree-shaken away. Check that\n` +
165
306
  `the "sideEffects" globs in package.json match this package's SOURCE paths —\n` +
166
- `globs that only match ./dist/** tell the bundler the src files are pure.`
307
+ `globs that only match ./dist/** tell the bundler the src files are pure.` +
308
+ (misordered.length
309
+ ? `\n\nAlso misordered: ${misordered.map((entry) => entry.file).join(", ")}`
310
+ : "")
167
311
  );
168
312
  }
169
313
 
@@ -196,7 +340,7 @@ if (isEntrypoint(import.meta.url, process.argv[1])) {
196
340
  const jsonMode = process.argv.includes("--json");
197
341
  let report;
198
342
  try {
199
- report = verifyBarrel(path.join(packageRoot, "dist"));
343
+ report = verifyPackage(packageRoot);
200
344
  } catch (error) {
201
345
  if (jsonMode) {
202
346
  process.stdout.write(
@@ -214,7 +358,10 @@ if (isEntrypoint(import.meta.url, process.argv[1])) {
214
358
  process.stdout.write(formatReport(report) + "\n");
215
359
  if (!report.ok) process.stderr.write(`\n${failureMessage(report)}\n`);
216
360
  else
217
- process.stdout.write(`\nAll ${GROUPS.length} polyfill groups install.\n`);
361
+ process.stdout.write(
362
+ `\nAll ${GROUPS.length} polyfill groups install, and all ` +
363
+ `${report.entries.length} published entry files load them first.\n`,
364
+ );
218
365
  }
219
366
  process.exit(report.ok ? 0 : 1);
220
367
  }