@copilotkit/react-core 1.57.0 → 1.57.1-canary.1778272612

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 (86) hide show
  1. package/dist/{copilotkit-CPe2-340.mjs → copilotkit-3XTEoVQO.mjs} +1367 -1200
  2. package/dist/copilotkit-3XTEoVQO.mjs.map +1 -0
  3. package/dist/{copilotkit-DFaI4j2r.d.mts → copilotkit-BCJ2yvV6.d.mts} +68 -8
  4. package/dist/copilotkit-BCJ2yvV6.d.mts.map +1 -0
  5. package/dist/{copilotkit-Dg4r4Gi_.d.cts → copilotkit-CBbSvze0.d.cts} +68 -8
  6. package/dist/copilotkit-CBbSvze0.d.cts.map +1 -0
  7. package/dist/{copilotkit-DGbvw8n2.cjs → copilotkit-Dnj9pi4m.cjs} +1369 -1196
  8. package/dist/copilotkit-Dnj9pi4m.cjs.map +1 -0
  9. package/dist/index.cjs +2 -5
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.mts +1 -1
  13. package/dist/index.mjs +2 -5
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/index.umd.js +733 -610
  16. package/dist/index.umd.js.map +1 -1
  17. package/dist/v2/context.cjs +135 -0
  18. package/dist/v2/context.cjs.map +1 -0
  19. package/dist/v2/context.d.cts +148 -0
  20. package/dist/v2/context.d.cts.map +1 -0
  21. package/dist/v2/context.d.mts +148 -0
  22. package/dist/v2/context.d.mts.map +1 -0
  23. package/dist/v2/context.mjs +129 -0
  24. package/dist/v2/context.mjs.map +1 -0
  25. package/dist/v2/headless.cjs +1043 -0
  26. package/dist/v2/headless.cjs.map +1 -0
  27. package/dist/v2/headless.d.cts +605 -0
  28. package/dist/v2/headless.d.cts.map +1 -0
  29. package/dist/v2/headless.d.mts +512 -0
  30. package/dist/v2/headless.d.mts.map +1 -0
  31. package/dist/v2/headless.mjs +997 -0
  32. package/dist/v2/headless.mjs.map +1 -0
  33. package/dist/v2/index.cjs +2 -1
  34. package/dist/v2/index.css +1 -1
  35. package/dist/v2/index.d.cts +2 -2
  36. package/dist/v2/index.d.mts +2 -2
  37. package/dist/v2/index.mjs +2 -2
  38. package/dist/v2/index.umd.js +1385 -1204
  39. package/dist/v2/index.umd.js.map +1 -1
  40. package/package.json +14 -6
  41. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +5 -6
  42. package/src/hooks/use-copilot-chat_internal.ts +0 -1
  43. package/src/v2/components/chat/CopilotChat.tsx +2 -1
  44. package/src/v2/components/chat/CopilotChatMessageView.tsx +24 -9
  45. package/src/v2/components/chat/CopilotChatView.tsx +2 -2
  46. package/src/v2/components/chat/CopilotSidebar.tsx +5 -1
  47. package/src/v2/components/chat/CopilotSidebarView.tsx +24 -10
  48. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +1 -3
  49. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +29 -25
  50. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +159 -0
  51. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +5 -60
  52. package/src/v2/components/index.ts +1 -0
  53. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +286 -0
  54. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +464 -0
  55. package/src/v2/components/intelligence-indicator/index.ts +2 -0
  56. package/src/v2/context.ts +62 -0
  57. package/src/v2/headless.ts +42 -0
  58. package/src/v2/hooks/__tests__/standard-schema.test.tsx +2 -2
  59. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +3 -3
  60. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +3 -3
  61. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +85 -85
  62. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +2 -2
  63. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +2 -2
  64. package/src/v2/hooks/__tests__/use-threads.test.tsx +2 -2
  65. package/src/v2/hooks/__tests__/zod-regression.test.tsx +2 -2
  66. package/src/v2/hooks/use-agent-context.tsx +1 -1
  67. package/src/v2/hooks/use-agent.tsx +9 -118
  68. package/src/v2/hooks/use-configure-suggestions.tsx +1 -1
  69. package/src/v2/hooks/use-default-render-tool.tsx +18 -1
  70. package/src/v2/hooks/use-frontend-tool.tsx +2 -2
  71. package/src/v2/hooks/use-human-in-the-loop.tsx +1 -1
  72. package/src/v2/hooks/use-interrupt.tsx +1 -1
  73. package/src/v2/hooks/use-render-activity-message.tsx +3 -11
  74. package/src/v2/hooks/use-render-custom-messages.tsx +1 -6
  75. package/src/v2/hooks/use-render-tool-call.tsx +36 -6
  76. package/src/v2/hooks/use-render-tool.tsx +2 -2
  77. package/src/v2/hooks/use-suggestions.tsx +1 -1
  78. package/src/v2/hooks/use-threads.tsx +1 -1
  79. package/src/v2/providers/CopilotKitProvider.tsx +19 -59
  80. package/src/v2/styles/globals.css +118 -0
  81. package/tsdown.config.ts +75 -0
  82. package/dist/copilotkit-CPe2-340.mjs.map +0 -1
  83. package/dist/copilotkit-DFaI4j2r.d.mts.map +0 -1
  84. package/dist/copilotkit-DGbvw8n2.cjs.map +0 -1
  85. package/dist/copilotkit-Dg4r4Gi_.d.cts.map +0 -1
  86. package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +0 -333
@@ -0,0 +1,464 @@
1
+ import React from "react";
2
+ import { afterEach, describe, expect, it } from "vitest";
3
+ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
4
+ import { EventType } from "@ag-ui/client";
5
+ import type { BaseEvent, RunAgentInput } from "@ag-ui/client";
6
+ import type { Observable } from "rxjs";
7
+ import type { Subject } from "rxjs";
8
+ import { takeWhile } from "rxjs/operators";
9
+ import {
10
+ MockStepwiseAgent,
11
+ runStartedEvent,
12
+ runFinishedEvent,
13
+ textMessageStartEvent,
14
+ textMessageContentEvent,
15
+ textMessageEndEvent,
16
+ toolCallChunkEvent,
17
+ toolCallResultEvent,
18
+ } from "../../../__tests__/utils/test-helpers";
19
+ import { CopilotChat } from "../../chat/CopilotChat";
20
+ import { CopilotKitProvider } from "../../../providers/CopilotKitProvider";
21
+ import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
22
+ import { useCopilotKit } from "../../../providers/CopilotKitProvider";
23
+ import { useAgent } from "../../../hooks/use-agent";
24
+
25
+ /**
26
+ * Mock agent with accurate per-run `isRunning` lifecycle. The shared
27
+ * `MockStepwiseAgent` returns the un-terminating `subject.asObservable()`
28
+ * for backward compatibility, so emitting `RUN_FINISHED` on it doesn't
29
+ * complete the rxjs pipeline → `isRunning` never flips back. The
30
+ * `takeWhile(..., true)` here makes the per-run observable terminate on
31
+ * RUN_FINISHED / RUN_ERROR (the `true` is "inclusive" — emit the
32
+ * terminal event before completing). That terminates AG-UI's run
33
+ * pipeline like a real agent so the indicator sees the falling edge.
34
+ */
35
+ class IsRunningAccurateMockAgent extends MockStepwiseAgent {
36
+ override run(_input: RunAgentInput): Observable<BaseEvent> {
37
+ return (this as unknown as { subject: Subject<BaseEvent> }).subject.pipe(
38
+ takeWhile(
39
+ (event) =>
40
+ event.type !== EventType.RUN_FINISHED &&
41
+ event.type !== EventType.RUN_ERROR,
42
+ true,
43
+ ),
44
+ );
45
+ }
46
+ }
47
+
48
+ const PILL_TESTID_RE = /^cpk-intelligence-pill-/;
49
+
50
+ const expectPillCount = (n: number): void => {
51
+ expect(screen.queryAllByTestId(PILL_TESTID_RE).length).toBe(n);
52
+ };
53
+
54
+ const expectPillOn = (messageId: string): void => {
55
+ expect(
56
+ screen.getByTestId(`cpk-intelligence-pill-${messageId}`).textContent,
57
+ ).toContain("Using CopilotKit Intelligence");
58
+ };
59
+
60
+ const expectNoPillOn = (messageId: string): void => {
61
+ expect(screen.queryByTestId(`cpk-intelligence-pill-${messageId}`)).toBeNull();
62
+ };
63
+
64
+ const expectNoPillAnywhere = (): void => {
65
+ expect(screen.queryAllByTestId(PILL_TESTID_RE).length).toBe(0);
66
+ };
67
+
68
+ const emitAssistantMessageWithToolCalls = (
69
+ agent: MockStepwiseAgent,
70
+ messageId: string,
71
+ toolCalls: Array<{ id: string; name?: string; arg: string }>,
72
+ ): void => {
73
+ agent.emit(textMessageStartEvent(messageId));
74
+ agent.emit(textMessageEndEvent(messageId));
75
+ for (const tc of toolCalls) {
76
+ agent.emit(
77
+ toolCallChunkEvent({
78
+ toolCallId: tc.id,
79
+ toolCallName: tc.name ?? "copilotkit_knowledge_base_shell",
80
+ parentMessageId: messageId,
81
+ delta: tc.arg,
82
+ }),
83
+ );
84
+ }
85
+ };
86
+
87
+ const emitToolResult = (
88
+ agent: MockStepwiseAgent,
89
+ toolCallId: string,
90
+ resultMessageId: string,
91
+ content = "ok",
92
+ ): void => {
93
+ agent.emit(
94
+ toolCallResultEvent({ toolCallId, messageId: resultMessageId, content }),
95
+ );
96
+ };
97
+
98
+ const emitAssistantProse = (
99
+ agent: MockStepwiseAgent,
100
+ messageId: string,
101
+ text: string,
102
+ ): void => {
103
+ agent.emit(textMessageStartEvent(messageId));
104
+ agent.emit(textMessageContentEvent(messageId, text));
105
+ agent.emit(textMessageEndEvent(messageId));
106
+ };
107
+
108
+ const startRun = (agent: MockStepwiseAgent): void => {
109
+ agent.emit(runStartedEvent());
110
+ };
111
+
112
+ /**
113
+ * Test harness: drives `copilotkit.runAgent` from a click and force-sets
114
+ * `copilotkit.intelligence` on the live core. Apps observe the
115
+ * intelligence-set-up state via a real `/info` round trip; tests bypass
116
+ * that plumbing by writing directly into the registry's private slot.
117
+ */
118
+ const RunAgentHarness: React.FC<{ withIntelligence: boolean }> = ({
119
+ withIntelligence,
120
+ }) => {
121
+ const { copilotkit } = useCopilotKit();
122
+ const { agent } = useAgent();
123
+
124
+ React.useEffect(() => {
125
+ const reg = (
126
+ copilotkit as unknown as {
127
+ agentRegistry: { _intelligence?: { wsUrl: string } };
128
+ }
129
+ ).agentRegistry;
130
+ if (withIntelligence) {
131
+ reg._intelligence = { wsUrl: "wss://test/intelligence" };
132
+ } else {
133
+ reg._intelligence = undefined;
134
+ }
135
+ }, [copilotkit, withIntelligence]);
136
+
137
+ const handleClick = React.useCallback(() => {
138
+ if (!agent) return;
139
+ copilotkit.runAgent({ agent }).catch((err: unknown) => {
140
+ console.error("[test] copilotkit.runAgent rejected:", err);
141
+ throw err;
142
+ });
143
+ }, [copilotkit, agent]);
144
+
145
+ return (
146
+ <button data-testid="trigger-run" onClick={handleClick}>
147
+ run
148
+ </button>
149
+ );
150
+ };
151
+
152
+ interface RenderOptions {
153
+ withIntelligence?: boolean;
154
+ }
155
+
156
+ const renderForIndicator = (
157
+ agent: MockStepwiseAgent,
158
+ options: RenderOptions = {},
159
+ ): void => {
160
+ const { withIntelligence = true } = options;
161
+
162
+ // No `renderCustomMessages` prop is passed — the indicator
163
+ // auto-mounts when intelligence is configured.
164
+ render(
165
+ <CopilotKitProvider agents__unsafe_dev_only={{ default: agent }}>
166
+ <CopilotChatConfigurationProvider agentId="default" threadId="t">
167
+ <RunAgentHarness withIntelligence={withIntelligence} />
168
+ <div style={{ height: 400 }}>
169
+ <CopilotChat welcomeScreen={false} />
170
+ </div>
171
+ </CopilotChatConfigurationProvider>
172
+ </CopilotKitProvider>,
173
+ );
174
+ };
175
+
176
+ const triggerRun = async (agent: MockStepwiseAgent): Promise<void> => {
177
+ await waitFor(() => expect(agent.isRunning).toBe(false));
178
+ fireEvent.click(screen.getByTestId("trigger-run"));
179
+ await waitFor(() => expect(agent.isRunning).toBe(true));
180
+ };
181
+
182
+ /** Phase machine fades over up to ~1780 ms (500 + 800 + 480). */
183
+ const FADE_OUT_TIMEOUT_MS = 2500;
184
+
185
+ describe('IntelligenceIndicator — "Using CopilotKit Intelligence" (auto-mounted)', () => {
186
+ const activeAgents: IsRunningAccurateMockAgent[] = [];
187
+ const makeAgent = (): IsRunningAccurateMockAgent => {
188
+ const agent = new IsRunningAccurateMockAgent();
189
+ activeAgents.push(agent);
190
+ return agent;
191
+ };
192
+ afterEach(() => {
193
+ while (activeAgents.length) {
194
+ const agent = activeAgents.pop()!;
195
+ try {
196
+ agent.complete();
197
+ } catch (err) {
198
+ console.error("[test] agent.complete() threw during cleanup:", err);
199
+ }
200
+ }
201
+ });
202
+
203
+ /**
204
+ * Walks through the canonical scenario:
205
+ *
206
+ * RUN A
207
+ * m_a1 (assistant) → toolCall bash#1, bash#2
208
+ * RUN B
209
+ * m_b1 (assistant) → toolCall bash
210
+ * m_b2 (assistant) → toolCall bash#1, bash#2
211
+ *
212
+ * The pill must render only on the last message of the latest
213
+ * in-flight run — never on multiple messages, never on stale runs,
214
+ * never on a non-last message of the current run.
215
+ */
216
+ it("renders only on the last message of the latest in-flight run", async () => {
217
+ const agent = makeAgent();
218
+ renderForIndicator(agent);
219
+ await screen.findByTestId("trigger-run");
220
+
221
+ expectNoPillAnywhere();
222
+
223
+ // ─── Run A: m_a1 with two tool calls → pill on m_a1 ───────────────
224
+ await triggerRun(agent);
225
+ startRun(agent);
226
+ emitAssistantMessageWithToolCalls(agent, "m_a1", [
227
+ { id: "tc_a1_1", arg: '{"cmd":"ls"}' },
228
+ { id: "tc_a1_2", arg: '{"cmd":"pwd"}' },
229
+ ]);
230
+ await waitFor(() => expectPillOn("m_a1"));
231
+ expectPillCount(1);
232
+
233
+ agent.emit(runFinishedEvent());
234
+
235
+ // ─── Run B: m_b1 (one bash) ──────────────────────────────────────
236
+ await triggerRun(agent);
237
+ startRun(agent);
238
+ emitAssistantMessageWithToolCalls(agent, "m_b1", [
239
+ { id: "tc_b1", arg: '{"cmd":"echo b1"}' },
240
+ ]);
241
+ await waitFor(() => expectPillOn("m_b1"));
242
+ // m_a1's pill may briefly remain in fade-out; lock the post-debounce
243
+ // state.
244
+ await waitFor(() => expectNoPillOn("m_a1"), {
245
+ timeout: FADE_OUT_TIMEOUT_MS,
246
+ });
247
+ expectPillCount(1);
248
+
249
+ // ─── m_b2 streams in — pill moves to m_b2, m_b1 loses pill ────────
250
+ emitAssistantMessageWithToolCalls(agent, "m_b2", [
251
+ { id: "tc_b2_1", arg: '{"cmd":"echo b2-1"}' },
252
+ { id: "tc_b2_2", arg: '{"cmd":"echo b2-2"}' },
253
+ ]);
254
+ await waitFor(() => expectPillOn("m_b2"));
255
+ expectNoPillOn("m_b1");
256
+ expectNoPillOn("m_a1");
257
+ expectPillCount(1);
258
+
259
+ // ─── Run B finishes — pill fades, eventually no pill anywhere ─────
260
+ agent.emit(runFinishedEvent());
261
+ await waitFor(() => expectNoPillAnywhere(), {
262
+ timeout: FADE_OUT_TIMEOUT_MS,
263
+ });
264
+ });
265
+
266
+ // ─── Per-condition focused tests ────────────────────────────────────
267
+
268
+ it("condition (last-in-run): never renders on a non-last message of the run", async () => {
269
+ const agent = makeAgent();
270
+ renderForIndicator(agent);
271
+ await screen.findByTestId("trigger-run");
272
+
273
+ await triggerRun(agent);
274
+ startRun(agent);
275
+ emitAssistantMessageWithToolCalls(agent, "m_first", [
276
+ { id: "tc_first", arg: "{}" },
277
+ ]);
278
+ // Wait for m_first to render the pill while it is still the last
279
+ // message in the run. Without this gate, both messages would land
280
+ // synchronously and m_first's renderer would never have been
281
+ // invoked while it was the last — turning a reactive assertion
282
+ // into a first-render correctness test by accident.
283
+ await waitFor(() => expectPillOn("m_first"));
284
+
285
+ emitAssistantMessageWithToolCalls(agent, "m_second", [
286
+ { id: "tc_second", arg: "{}" },
287
+ ]);
288
+
289
+ await waitFor(() => expectPillOn("m_second"));
290
+ expectNoPillOn("m_first");
291
+ expectPillCount(1);
292
+ });
293
+
294
+ it("condition (in-flight): pill clears after the run finishes", async () => {
295
+ const agent = makeAgent();
296
+ renderForIndicator(agent);
297
+ await screen.findByTestId("trigger-run");
298
+
299
+ await triggerRun(agent);
300
+ startRun(agent);
301
+ emitAssistantMessageWithToolCalls(agent, "m_only", [
302
+ { id: "tc", arg: "{}" },
303
+ ]);
304
+ await waitFor(() => expectPillOn("m_only"));
305
+
306
+ agent.emit(runFinishedEvent());
307
+ await waitFor(() => expectNoPillOn("m_only"), {
308
+ timeout: FADE_OUT_TIMEOUT_MS,
309
+ });
310
+ });
311
+
312
+ it("condition (latest-run): never renders on a stale run after a newer run starts", async () => {
313
+ const agent = makeAgent();
314
+ renderForIndicator(agent);
315
+ await screen.findByTestId("trigger-run");
316
+
317
+ await triggerRun(agent);
318
+ startRun(agent);
319
+ emitAssistantMessageWithToolCalls(agent, "m_run1", [
320
+ { id: "tc_run1", arg: "{}" },
321
+ ]);
322
+ await waitFor(() => expectPillOn("m_run1"));
323
+ agent.emit(runFinishedEvent());
324
+
325
+ await triggerRun(agent);
326
+ startRun(agent);
327
+ emitAssistantMessageWithToolCalls(agent, "m_run2", [
328
+ { id: "tc_run2", arg: "{}" },
329
+ ]);
330
+ await waitFor(() => expectPillOn("m_run2"));
331
+ expectNoPillOn("m_run1");
332
+ });
333
+
334
+ it("condition (tool-match): only renders when a configured tool name matches", async () => {
335
+ const agent = makeAgent();
336
+ renderForIndicator(agent);
337
+ await screen.findByTestId("trigger-run");
338
+
339
+ await triggerRun(agent);
340
+ startRun(agent);
341
+ // First message has only a non-matching tool call; no pill.
342
+ emitAssistantMessageWithToolCalls(agent, "m_no_match", [
343
+ { id: "tc_no_match", name: "fetch", arg: "{}" },
344
+ ]);
345
+ await new Promise((r) => setTimeout(r, 80));
346
+ expectNoPillOn("m_no_match");
347
+
348
+ // Second message has a bash call — pill should appear on it.
349
+ emitAssistantMessageWithToolCalls(agent, "m_match", [
350
+ { id: "tc_match", name: "copilotkit_knowledge_base_shell", arg: "{}" },
351
+ ]);
352
+ await waitFor(() => expectPillOn("m_match"));
353
+ expectPillCount(1);
354
+ });
355
+
356
+ it("intelligence gate: does not render when copilotkit.intelligence is undefined", async () => {
357
+ const agent = makeAgent();
358
+ renderForIndicator(agent, { withIntelligence: false });
359
+ await screen.findByTestId("trigger-run");
360
+
361
+ await triggerRun(agent);
362
+ startRun(agent);
363
+ emitAssistantMessageWithToolCalls(agent, "m1", [{ id: "tc1", arg: "{}" }]);
364
+
365
+ // Without the gate, the pill would be visible by now.
366
+ await new Promise((r) => setTimeout(r, 80));
367
+ expectNoPillAnywhere();
368
+ });
369
+
370
+ it("auto-registration: no renderCustomMessages prop is required", async () => {
371
+ // This is the explicit assertion that the indicator auto-mounts.
372
+ // `renderForIndicator` does not pass `renderCustomMessages`, yet
373
+ // the pill renders solely because intelligence is configured.
374
+ const agent = makeAgent();
375
+ renderForIndicator(agent);
376
+ await screen.findByTestId("trigger-run");
377
+
378
+ await triggerRun(agent);
379
+ startRun(agent);
380
+ emitAssistantMessageWithToolCalls(agent, "m1", [{ id: "tc1", arg: "{}" }]);
381
+
382
+ await waitFor(() => expectPillOn("m1"));
383
+ expectPillCount(1);
384
+ });
385
+
386
+ // ─── New gate: tool-call pending past grace window ──────────────────
387
+
388
+ it("replay-flash suppression: no pill when tool result arrives within the grace window", async () => {
389
+ // Models a `connectAgent` history replay: the tool call and its
390
+ // matching `tool`-role result arrive in the same tick, well below
391
+ // PENDING_THRESHOLD_MS. The pill timer should be cancelled before
392
+ // it fires, so nothing renders.
393
+ const agent = makeAgent();
394
+ renderForIndicator(agent);
395
+ await screen.findByTestId("trigger-run");
396
+
397
+ await triggerRun(agent);
398
+ startRun(agent);
399
+ emitAssistantMessageWithToolCalls(agent, "m_replay", [
400
+ { id: "tc_replay", arg: '{"cmd":"ls"}' },
401
+ ]);
402
+ emitToolResult(agent, "tc_replay", "tr_replay");
403
+
404
+ // Wait past the grace window — pill should never appear.
405
+ await new Promise((r) => setTimeout(r, 200));
406
+ expectNoPillAnywhere();
407
+ });
408
+
409
+ it("multi-step: pill stays continuously across tool-result interleaving", async () => {
410
+ // Tool result arrives, then a second assistant message with bash.
411
+ // The pill must not show the completed/fade animation between
412
+ // calls — it stays on m_step1 until m_step2 takes over the slot.
413
+ const agent = makeAgent();
414
+ renderForIndicator(agent);
415
+ await screen.findByTestId("trigger-run");
416
+
417
+ await triggerRun(agent);
418
+ startRun(agent);
419
+ emitAssistantMessageWithToolCalls(agent, "m_step1", [
420
+ { id: "tc_step1", arg: '{"cmd":"ls"}' },
421
+ ]);
422
+ await waitFor(() => expectPillOn("m_step1"));
423
+
424
+ // Tool result lands. agent.isRunning is still true, no real
425
+ // follow-up yet → pill stays on m_step1 in spinner.
426
+ emitToolResult(agent, "tc_step1", "tr_step1");
427
+ await new Promise((r) => setTimeout(r, 150));
428
+ expectPillOn("m_step1");
429
+ expectPillCount(1);
430
+
431
+ // Second assistant message with bash arrives. Slot moves; old
432
+ // instance returns null without a fade animation.
433
+ emitAssistantMessageWithToolCalls(agent, "m_step2", [
434
+ { id: "tc_step2", arg: '{"cmd":"pwd"}' },
435
+ ]);
436
+ await waitFor(() => expectPillOn("m_step2"));
437
+ expectNoPillOn("m_step1");
438
+ expectPillCount(1);
439
+ });
440
+
441
+ it("real-followup exit: prose assistant message after the tool flow clears the pill", async () => {
442
+ // After the tool result the agent emits a final prose message —
443
+ // that's a "real follow-up" and should immediately exit the
444
+ // spinner (without waiting on isRunning), advancing to check
445
+ // → fading → hidden.
446
+ const agent = makeAgent();
447
+ renderForIndicator(agent);
448
+ await screen.findByTestId("trigger-run");
449
+
450
+ await triggerRun(agent);
451
+ startRun(agent);
452
+ emitAssistantMessageWithToolCalls(agent, "m_tool", [
453
+ { id: "tc_only", arg: '{"cmd":"ls"}' },
454
+ ]);
455
+ await waitFor(() => expectPillOn("m_tool"));
456
+
457
+ emitToolResult(agent, "tc_only", "tr_only");
458
+ emitAssistantProse(agent, "m_prose", "All done.");
459
+
460
+ await waitFor(() => expectNoPillAnywhere(), {
461
+ timeout: FADE_OUT_TIMEOUT_MS,
462
+ });
463
+ });
464
+ });
@@ -0,0 +1,2 @@
1
+ export { IntelligenceIndicator } from "./IntelligenceIndicator";
2
+ export type { IntelligenceIndicatorProps } from "./IntelligenceIndicator";
@@ -0,0 +1,62 @@
1
+ "use client";
2
+
3
+ import { createContext, useContext, useEffect, useReducer } from "react";
4
+ import { CopilotKitCoreReact } from "./lib/react-core";
5
+ import type { CopilotKitCoreReactConfig } from "./lib/react-core";
6
+ import type { LicenseContextValue } from "@copilotkit/shared";
7
+
8
+ // Re-export so headless.ts (and consumers) reference the same type declaration.
9
+ export { CopilotKitCoreReact };
10
+ export type { CopilotKitCoreReactConfig };
11
+
12
+ export interface CopilotKitContextValue {
13
+ copilotkit: CopilotKitCoreReact;
14
+ /**
15
+ * Set of tool call IDs currently being executed.
16
+ * This is tracked at the provider level to ensure tool execution events
17
+ * are captured even before child components mount.
18
+ */
19
+ executingToolCallIds: ReadonlySet<string>;
20
+ }
21
+
22
+ export const EMPTY_SET: ReadonlySet<string> = new Set();
23
+
24
+ export const CopilotKitContext = createContext<CopilotKitContextValue | null>(
25
+ null,
26
+ );
27
+
28
+ export const useCopilotKit = (): CopilotKitContextValue => {
29
+ const context = useContext(CopilotKitContext);
30
+ const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
31
+
32
+ if (!context) {
33
+ throw new Error("useCopilotKit must be used within CopilotKitProvider");
34
+ }
35
+ useEffect(() => {
36
+ const subscription = context.copilotkit.subscribe({
37
+ onRuntimeConnectionStatusChanged: () => {
38
+ forceUpdate();
39
+ },
40
+ });
41
+ return () => {
42
+ subscription.unsubscribe();
43
+ };
44
+ // eslint-disable-next-line react-hooks/exhaustive-deps
45
+ }, []);
46
+
47
+ return context;
48
+ };
49
+
50
+ // License context — shared between web and RN providers.
51
+ // Default is permissive (all features allowed) — providers override via createLicenseContextValue.
52
+ // Inlined here to avoid a runtime import from @copilotkit/shared, which pulls in
53
+ // Node-only deps (jose) that break React Native's Metro bundler.
54
+ export const LicenseContext = createContext<LicenseContextValue>({
55
+ status: null,
56
+ license: null,
57
+ checkFeature: () => true,
58
+ getLimit: () => null,
59
+ } as LicenseContextValue);
60
+
61
+ export const useLicenseContext = (): LicenseContextValue =>
62
+ useContext(LicenseContext);
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Headless (platform-agnostic) exports from @copilotkit/react-core/v2.
3
+ *
4
+ * No CSS, no web UI components, no DOM dependencies.
5
+ * Used by @copilotkit/react-native.
6
+ */
7
+
8
+ // Re-export from context (which is external in this build) so the .d.ts
9
+ // references the same type declaration. This avoids a nominal type mismatch
10
+ // caused by private class members being declared in two separate .d.ts files.
11
+ export { CopilotKitCoreReact } from "./context";
12
+ export type { CopilotKitCoreReactConfig } from "./context";
13
+
14
+ // Chat configuration provider (no UI, just context)
15
+ export {
16
+ CopilotChatConfigurationProvider,
17
+ useCopilotChatConfiguration,
18
+ CopilotChatDefaultLabels,
19
+ type CopilotChatLabels,
20
+ type CopilotChatConfigurationValue,
21
+ type CopilotChatConfigurationProviderProps,
22
+ } from "./providers/CopilotChatConfigurationProvider";
23
+
24
+ // Platform-agnostic hooks
25
+ export { useAgent, type UseAgentUpdate } from "./hooks/use-agent";
26
+ export { useFrontendTool } from "./hooks/use-frontend-tool";
27
+ export { useComponent } from "./hooks/use-component";
28
+ export { useHumanInTheLoop } from "./hooks/use-human-in-the-loop";
29
+ export { useInterrupt, type UseInterruptConfig } from "./hooks/use-interrupt";
30
+ export { useSuggestions } from "./hooks/use-suggestions";
31
+ export { useConfigureSuggestions } from "./hooks/use-configure-suggestions";
32
+ export {
33
+ useAgentContext,
34
+ type AgentContextInput,
35
+ type JsonSerializable,
36
+ } from "./hooks/use-agent-context";
37
+ export {
38
+ useThreads,
39
+ type Thread,
40
+ type UseThreadsInput,
41
+ type UseThreadsResult,
42
+ } from "./hooks/use-threads";
@@ -6,12 +6,12 @@ import * as v from "valibot";
6
6
  import { type } from "arktype";
7
7
  import { useRenderTool, type RenderToolProps } from "../use-render-tool";
8
8
  import { useComponent } from "../use-component";
9
- import { useCopilotKit } from "../../providers/CopilotKitProvider";
9
+ import { useCopilotKit } from "../../context";
10
10
  import { useFrontendTool } from "../use-frontend-tool";
11
11
  import type { ReactToolCallRenderer } from "../../types/react-tool-call-renderer";
12
12
  import type { StandardSchemaV1 } from "@copilotkit/shared";
13
13
 
14
- vi.mock("../../providers/CopilotKitProvider", () => ({
14
+ vi.mock("../../context", () => ({
15
15
  useCopilotKit: vi.fn(),
16
16
  }));
17
17
 
@@ -2,10 +2,10 @@ import React, { useState } from "react";
2
2
  import { render, act } from "@testing-library/react";
3
3
  import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
4
4
  import { useAgentContext, type AgentContextInput } from "../use-agent-context";
5
- import { useCopilotKit } from "../../providers/CopilotKitProvider";
5
+ import { useCopilotKit } from "../../context";
6
6
 
7
- // Mock the CopilotKitProvider
8
- vi.mock("../../providers/CopilotKitProvider", () => ({
7
+ // Mock the CopilotKit context
8
+ vi.mock("../../context", () => ({
9
9
  useCopilotKit: vi.fn(),
10
10
  }));
11
11
 
@@ -2,13 +2,13 @@ import React from "react";
2
2
  import { render } from "@testing-library/react";
3
3
  import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
4
4
  import { AbstractAgent } from "@ag-ui/client";
5
- import { useCopilotKit } from "../../providers/CopilotKitProvider";
5
+ import { useCopilotKit } from "../../context";
6
6
  import { MockStepwiseAgent } from "../../__tests__/utils/test-helpers";
7
7
  import { useAgent } from "../use-agent";
8
8
  import { CopilotKitCoreRuntimeConnectionStatus } from "@copilotkit/core";
9
9
 
10
- // Mock the CopilotKitProvider to control copilotkit state directly
11
- vi.mock("../../providers/CopilotKitProvider", () => ({
10
+ // Mock the CopilotKit context to control copilotkit state directly
11
+ vi.mock("../../context", () => ({
12
12
  useCopilotKit: vi.fn(),
13
13
  }));
14
14