@copilotkit/react-core 1.54.1-next.3 → 1.54.1-next.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "version": "1.54.1-next.3",
13
+ "version": "1.54.1-next.5",
14
14
  "sideEffects": [
15
15
  "**/*.css"
16
16
  ],
@@ -55,14 +55,14 @@
55
55
  "tsconfig": "1.4.12"
56
56
  },
57
57
  "dependencies": {
58
- "@ag-ui/client": "^0.0.47",
58
+ "@ag-ui/client": "^0.0.48",
59
59
  "@scarf/scarf": "^1.3.0",
60
60
  "react-markdown": "^8.0.7",
61
61
  "untruncate-json": "^0.0.1",
62
- "@copilotkit/runtime-client-gql": "1.54.1-next.3",
63
- "@copilotkit/shared": "1.54.1-next.3",
64
- "@copilotkitnext/core": "1.54.1-next.3",
65
- "@copilotkitnext/react": "1.54.1-next.3"
62
+ "@copilotkit/runtime-client-gql": "1.54.1-next.5",
63
+ "@copilotkit/shared": "1.54.1-next.5",
64
+ "@copilotkitnext/core": "1.54.1-next.5",
65
+ "@copilotkitnext/react": "1.54.1-next.5"
66
66
  },
67
67
  "keywords": [
68
68
  "copilotkit",
@@ -35,6 +35,7 @@ const mockAgent = {
35
35
  addMessage: vi.fn(),
36
36
  abortRun: vi.fn(),
37
37
  runAgent: vi.fn(),
38
+ detachActiveRun: vi.fn().mockResolvedValue(undefined),
38
39
  };
39
40
 
40
41
  let lastSubscriber: TestAgentSubscriber | null = null;
@@ -28,6 +28,7 @@ const mockAgent: Record<string, unknown> = {
28
28
  addMessage: vi.fn(),
29
29
  abortRun: vi.fn(),
30
30
  runAgent: vi.fn(),
31
+ detachActiveRun: vi.fn().mockResolvedValue(undefined),
31
32
  threadId: undefined as string | undefined,
32
33
  };
33
34
 
@@ -26,7 +26,11 @@ import {
26
26
  CopilotKitCoreRuntimeConnectionStatus,
27
27
  } from "@copilotkitnext/core";
28
28
  import { useLazyToolRenderer } from "./use-lazy-tool-renderer";
29
- import { AbstractAgent, AGUIConnectNotImplementedError } from "@ag-ui/client";
29
+ import {
30
+ AbstractAgent,
31
+ AGUIConnectNotImplementedError,
32
+ HttpAgent,
33
+ } from "@ag-ui/client";
30
34
  import {
31
35
  CoAgentStateRenderBridge,
32
36
  type CoAgentStateRenderBridgeProps,
@@ -336,12 +340,28 @@ export function useCopilotChatInternal({
336
340
  const { agent } = useAgent({ agentId: resolvedAgentId });
337
341
 
338
342
  useEffect(() => {
343
+ let detached = false;
344
+
345
+ // Create a fresh AbortController so we can cancel the HTTP request on cleanup.
346
+ // Mirrors the V2 CopilotChat pattern: HttpAgent uses abortController.signal in
347
+ // its fetch config. connectAgent() does NOT create a new AbortController
348
+ // automatically, so we must set one before connecting.
349
+ const connectAbortController = new AbortController();
350
+ if (agent instanceof HttpAgent) {
351
+ agent.abortController = connectAbortController;
352
+ }
353
+
339
354
  const connect = async (agent: AbstractAgent) => {
340
355
  setAgentAvailable(false);
341
356
  try {
342
357
  await copilotkit.connectAgent({ agent });
343
- setAgentAvailable(true);
358
+ // Guard against setting state after cleanup (e.g. React StrictMode unmount)
359
+ if (!detached) {
360
+ setAgentAvailable(true);
361
+ }
344
362
  } catch (error) {
363
+ // Ignore errors from aborted connections (e.g. React StrictMode cleanup)
364
+ if (detached) return;
345
365
  if (error instanceof AGUIConnectNotImplementedError) {
346
366
  // connect not implemented, ignore
347
367
  } else {
@@ -360,7 +380,14 @@ export function useCopilotChatInternal({
360
380
  agent.threadId = existingConfig.threadId;
361
381
  connect(agent);
362
382
  }
363
- return () => {};
383
+ return () => {
384
+ // Abort the HTTP request and detach the active run.
385
+ // This is critical for React StrictMode which unmounts+remounts in dev,
386
+ // preventing duplicate /connect requests from reaching the server.
387
+ detached = true;
388
+ connectAbortController.abort();
389
+ agent?.detachActiveRun();
390
+ };
364
391
  }, [
365
392
  existingConfig?.threadId,
366
393
  agent,