@copilotkitnext/react 1.52.2-next.2 → 1.52.2-next.4

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 (79) hide show
  1. package/dist/components/chat/CopilotChat.cjs +32 -8
  2. package/dist/components/chat/CopilotChat.cjs.map +1 -1
  3. package/dist/components/chat/CopilotChat.d.cts +12 -0
  4. package/dist/components/chat/CopilotChat.d.cts.map +1 -1
  5. package/dist/components/chat/CopilotChat.d.mts +12 -0
  6. package/dist/components/chat/CopilotChat.d.mts.map +1 -1
  7. package/dist/components/chat/CopilotChat.mjs +34 -10
  8. package/dist/components/chat/CopilotChat.mjs.map +1 -1
  9. package/dist/components/chat/CopilotChatAssistantMessage.cjs +1 -1
  10. package/dist/components/chat/CopilotChatAssistantMessage.cjs.map +1 -1
  11. package/dist/components/chat/CopilotChatAssistantMessage.d.cts.map +1 -1
  12. package/dist/components/chat/CopilotChatAssistantMessage.d.mts.map +1 -1
  13. package/dist/components/chat/CopilotChatAssistantMessage.mjs +1 -1
  14. package/dist/components/chat/CopilotChatAssistantMessage.mjs.map +1 -1
  15. package/dist/components/chat/CopilotChatInput.cjs +1 -1
  16. package/dist/components/chat/CopilotChatInput.cjs.map +1 -1
  17. package/dist/components/chat/CopilotChatInput.d.cts.map +1 -1
  18. package/dist/components/chat/CopilotChatInput.d.mts.map +1 -1
  19. package/dist/components/chat/CopilotChatInput.mjs +1 -1
  20. package/dist/components/chat/CopilotChatInput.mjs.map +1 -1
  21. package/dist/components/chat/CopilotChatMessageView.cjs +1 -1
  22. package/dist/components/chat/CopilotChatMessageView.cjs.map +1 -1
  23. package/dist/components/chat/CopilotChatMessageView.mjs +1 -1
  24. package/dist/components/chat/CopilotChatMessageView.mjs.map +1 -1
  25. package/dist/components/chat/CopilotChatToggleButton.cjs +1 -1
  26. package/dist/components/chat/CopilotChatToggleButton.cjs.map +1 -1
  27. package/dist/components/chat/CopilotChatToggleButton.d.cts.map +1 -1
  28. package/dist/components/chat/CopilotChatToggleButton.d.mts.map +1 -1
  29. package/dist/components/chat/CopilotChatToggleButton.mjs +1 -1
  30. package/dist/components/chat/CopilotChatToggleButton.mjs.map +1 -1
  31. package/dist/components/chat/CopilotChatUserMessage.cjs +1 -1
  32. package/dist/components/chat/CopilotChatUserMessage.cjs.map +1 -1
  33. package/dist/components/chat/CopilotChatUserMessage.mjs +1 -1
  34. package/dist/components/chat/CopilotChatUserMessage.mjs.map +1 -1
  35. package/dist/components/chat/CopilotChatView.cjs +2 -2
  36. package/dist/components/chat/CopilotChatView.cjs.map +1 -1
  37. package/dist/components/chat/CopilotChatView.mjs +2 -2
  38. package/dist/components/chat/CopilotChatView.mjs.map +1 -1
  39. package/dist/components/chat/CopilotModalHeader.cjs +1 -1
  40. package/dist/components/chat/CopilotModalHeader.cjs.map +1 -1
  41. package/dist/components/chat/CopilotModalHeader.d.cts.map +1 -1
  42. package/dist/components/chat/CopilotModalHeader.d.mts.map +1 -1
  43. package/dist/components/chat/CopilotModalHeader.mjs +1 -1
  44. package/dist/components/chat/CopilotModalHeader.mjs.map +1 -1
  45. package/dist/components/chat/CopilotPopupView.cjs +1 -1
  46. package/dist/components/chat/CopilotPopupView.cjs.map +1 -1
  47. package/dist/components/chat/CopilotPopupView.d.cts.map +1 -1
  48. package/dist/components/chat/CopilotPopupView.d.mts.map +1 -1
  49. package/dist/components/chat/CopilotPopupView.mjs +1 -1
  50. package/dist/components/chat/CopilotPopupView.mjs.map +1 -1
  51. package/dist/components/chat/CopilotSidebarView.cjs +1 -1
  52. package/dist/components/chat/CopilotSidebarView.cjs.map +1 -1
  53. package/dist/components/chat/CopilotSidebarView.d.cts.map +1 -1
  54. package/dist/components/chat/CopilotSidebarView.d.mts.map +1 -1
  55. package/dist/components/chat/CopilotSidebarView.mjs +1 -1
  56. package/dist/components/chat/CopilotSidebarView.mjs.map +1 -1
  57. package/dist/hooks/use-agent.cjs +21 -3
  58. package/dist/hooks/use-agent.cjs.map +1 -1
  59. package/dist/hooks/use-agent.mjs +22 -4
  60. package/dist/hooks/use-agent.mjs.map +1 -1
  61. package/dist/index.umd.js +132 -46
  62. package/dist/index.umd.js.map +1 -1
  63. package/dist/lib/react-core.cjs +6 -0
  64. package/dist/lib/react-core.cjs.map +1 -1
  65. package/dist/lib/react-core.d.cts +2 -0
  66. package/dist/lib/react-core.d.cts.map +1 -1
  67. package/dist/lib/react-core.d.mts +2 -0
  68. package/dist/lib/react-core.d.mts.map +1 -1
  69. package/dist/lib/react-core.mjs +6 -0
  70. package/dist/lib/react-core.mjs.map +1 -1
  71. package/dist/providers/CopilotKitProvider.cjs +58 -25
  72. package/dist/providers/CopilotKitProvider.cjs.map +1 -1
  73. package/dist/providers/CopilotKitProvider.d.cts +10 -0
  74. package/dist/providers/CopilotKitProvider.d.cts.map +1 -1
  75. package/dist/providers/CopilotKitProvider.d.mts +10 -0
  76. package/dist/providers/CopilotKitProvider.d.mts.map +1 -1
  77. package/dist/providers/CopilotKitProvider.mjs +58 -25
  78. package/dist/providers/CopilotKitProvider.mjs.map +1 -1
  79. package/package.json +8 -8
package/dist/index.umd.js CHANGED
@@ -1017,7 +1017,7 @@ _radix_ui_react_dropdown_menu = __toESM(_radix_ui_react_dropdown_menu);
1017
1017
  }) : null;
1018
1018
  const inputPill = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1019
1019
  "data-testid": "copilot-chat-input",
1020
- className: (0, tailwind_merge.twMerge)("cpk:flex cpk:w-full cpk:flex-col cpk:items-center cpk:justify-center", "cpk:cursor-text", "cpk:overflow-visible cpk:bg-clip-padding cpk:contain-inline-size", "cpk:bg-white cpk:dark:bg-[#303030]", "cpk:shadow-[0_4px_4px_0_#0000000a,0_0_1px_0_#0000009e] cpk:rounded-[28px]"),
1020
+ className: (0, tailwind_merge.twMerge)("copilotKitInput", "cpk:flex cpk:w-full cpk:flex-col cpk:items-center cpk:justify-center", "cpk:cursor-text", "cpk:overflow-visible cpk:bg-clip-padding cpk:contain-inline-size", "cpk:bg-white cpk:dark:bg-[#303030]", "cpk:shadow-[0_4px_4px_0_#0000000a,0_0_1px_0_#0000009e] cpk:rounded-[28px]"),
1021
1021
  onClick: handleContainerClick,
1022
1022
  "data-layout": isExpanded ? "expanded" : "compact",
1023
1023
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
@@ -1817,6 +1817,12 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
1817
1817
  get renderToolCalls() {
1818
1818
  return this._renderToolCalls;
1819
1819
  }
1820
+ setRenderActivityMessages(renderers) {
1821
+ this._renderActivityMessages = renderers;
1822
+ }
1823
+ setRenderCustomMessages(renderers) {
1824
+ this._renderCustomMessages = renderers;
1825
+ }
1820
1826
  setRenderToolCalls(renderToolCalls) {
1821
1827
  this._renderToolCalls = renderToolCalls;
1822
1828
  this.notifySubscribers((subscriber) => {
@@ -1863,7 +1869,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
1863
1869
  }, [value, warningMessage]);
1864
1870
  return value;
1865
1871
  }
1866
- const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, publicApiKey, publicLicenseKey, properties = {}, agents__unsafe_dev_only: agents = {}, selfManagedAgents = {}, renderToolCalls, renderActivityMessages, renderCustomMessages, frontendTools, humanInTheLoop, showDevConsole = false, useSingleEndpoint = false }) => {
1872
+ const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, publicApiKey, publicLicenseKey, properties = {}, agents__unsafe_dev_only: agents = {}, selfManagedAgents = {}, renderToolCalls, renderActivityMessages, renderCustomMessages, frontendTools, humanInTheLoop, showDevConsole = false, useSingleEndpoint = false, onError }) => {
1867
1873
  const [shouldRenderInspector, setShouldRenderInspector] = (0, react.useState)(false);
1868
1874
  (0, react.useEffect)(() => {
1869
1875
  if (typeof window === "undefined") return;
@@ -1971,26 +1977,20 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
1971
1977
  frontendToolsList,
1972
1978
  processedHumanInTheLoopTools
1973
1979
  ]);
1974
- const copilotkit = (0, react.useMemo)(() => {
1975
- return new CopilotKitCoreReact({
1976
- runtimeUrl: chatApiEndpoint,
1977
- runtimeTransport: useSingleEndpoint ? "single" : "rest",
1978
- headers: mergedHeaders,
1979
- credentials,
1980
- properties,
1981
- agents__unsafe_dev_only: mergedAgents,
1982
- tools: allTools,
1983
- renderToolCalls: allRenderToolCalls,
1984
- renderActivityMessages: allActivityRenderers,
1985
- renderCustomMessages: renderCustomMessagesList
1986
- });
1987
- }, [
1988
- allTools,
1989
- allRenderToolCalls,
1990
- allActivityRenderers,
1991
- renderCustomMessagesList,
1992
- useSingleEndpoint
1993
- ]);
1980
+ const copilotkitRef = (0, react.useRef)(null);
1981
+ if (copilotkitRef.current === null) copilotkitRef.current = new CopilotKitCoreReact({
1982
+ runtimeUrl: chatApiEndpoint,
1983
+ runtimeTransport: useSingleEndpoint ? "single" : "rest",
1984
+ headers: mergedHeaders,
1985
+ credentials,
1986
+ properties,
1987
+ agents__unsafe_dev_only: mergedAgents,
1988
+ tools: allTools,
1989
+ renderToolCalls: allRenderToolCalls,
1990
+ renderActivityMessages: allActivityRenderers,
1991
+ renderCustomMessages: renderCustomMessagesList
1992
+ });
1993
+ const copilotkit = copilotkitRef.current;
1994
1994
  const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
1995
1995
  (0, react.useEffect)(() => {
1996
1996
  const subscription = copilotkit.subscribe({ onRenderToolCallsChanged: () => {
@@ -2024,6 +2024,24 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2024
2024
  subscription.unsubscribe();
2025
2025
  };
2026
2026
  }, [copilotkit]);
2027
+ const onErrorRef = (0, react.useRef)(onError);
2028
+ (0, react.useEffect)(() => {
2029
+ onErrorRef.current = onError;
2030
+ }, [onError]);
2031
+ (0, react.useEffect)(() => {
2032
+ if (!onErrorRef.current) return;
2033
+ const subscription = copilotkit.subscribe({ onError: (event) => {
2034
+ var _onErrorRef$current;
2035
+ (_onErrorRef$current = onErrorRef.current) === null || _onErrorRef$current === void 0 || _onErrorRef$current.call(onErrorRef, {
2036
+ error: event.error,
2037
+ code: event.code,
2038
+ context: event.context
2039
+ });
2040
+ } });
2041
+ return () => {
2042
+ subscription.unsubscribe();
2043
+ };
2044
+ }, [copilotkit]);
2027
2045
  (0, react.useEffect)(() => {
2028
2046
  copilotkit.setRuntimeUrl(chatApiEndpoint);
2029
2047
  copilotkit.setRuntimeTransport(useSingleEndpoint ? "single" : "rest");
@@ -2032,6 +2050,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2032
2050
  copilotkit.setProperties(properties);
2033
2051
  copilotkit.setAgents__unsafe_dev_only(mergedAgents);
2034
2052
  }, [
2053
+ copilotkit,
2035
2054
  chatApiEndpoint,
2036
2055
  mergedHeaders,
2037
2056
  credentials,
@@ -2039,11 +2058,32 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2039
2058
  mergedAgents,
2040
2059
  useSingleEndpoint
2041
2060
  ]);
2061
+ const didMountRef = (0, react.useRef)(false);
2062
+ (0, react.useEffect)(() => {
2063
+ if (!didMountRef.current) return;
2064
+ copilotkit.setTools(allTools);
2065
+ }, [copilotkit, allTools]);
2066
+ (0, react.useEffect)(() => {
2067
+ if (!didMountRef.current) return;
2068
+ copilotkit.setRenderToolCalls(allRenderToolCalls);
2069
+ }, [copilotkit, allRenderToolCalls]);
2070
+ (0, react.useEffect)(() => {
2071
+ if (!didMountRef.current) return;
2072
+ copilotkit.setRenderActivityMessages(allActivityRenderers);
2073
+ }, [copilotkit, allActivityRenderers]);
2074
+ (0, react.useEffect)(() => {
2075
+ if (!didMountRef.current) return;
2076
+ copilotkit.setRenderCustomMessages(renderCustomMessagesList);
2077
+ }, [copilotkit, renderCustomMessagesList]);
2078
+ (0, react.useEffect)(() => {
2079
+ didMountRef.current = true;
2080
+ }, []);
2081
+ const contextValue = (0, react.useMemo)(() => ({
2082
+ copilotkit,
2083
+ executingToolCallIds
2084
+ }), [copilotkit, executingToolCallIds]);
2042
2085
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(CopilotKitContext.Provider, {
2043
- value: {
2044
- copilotkit,
2045
- executingToolCallIds
2046
- },
2086
+ value: contextValue,
2047
2087
  children: [children, shouldRenderInspector ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CopilotKitInspector, { core: copilotkit }) : null]
2048
2088
  });
2049
2089
  };
@@ -2708,13 +2748,32 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2708
2748
  const { copilotkit } = useCopilotKit();
2709
2749
  const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
2710
2750
  const updateFlags = (0, react.useMemo)(() => updates !== null && updates !== void 0 ? updates : ALL_UPDATES, [JSON.stringify(updates)]);
2751
+ const provisionalAgentCache = (0, react.useRef)(/* @__PURE__ */ new Map());
2711
2752
  const agent = (0, react.useMemo)(() => {
2712
2753
  var _copilotkit$agents;
2713
2754
  const existing = copilotkit.getAgent(agentId);
2714
- if (existing) return existing;
2755
+ if (existing) {
2756
+ provisionalAgentCache.current.delete(agentId);
2757
+ return existing;
2758
+ }
2715
2759
  const isRuntimeConfigured = copilotkit.runtimeUrl !== void 0;
2716
2760
  const status = copilotkit.runtimeConnectionStatus;
2717
2761
  if (isRuntimeConfigured && (status === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Disconnected || status === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Connecting)) {
2762
+ const cached = provisionalAgentCache.current.get(agentId);
2763
+ if (cached) {
2764
+ cached.headers = { ...copilotkit.headers };
2765
+ return cached;
2766
+ }
2767
+ const provisional = new _copilotkitnext_core.ProxiedCopilotRuntimeAgent({
2768
+ runtimeUrl: copilotkit.runtimeUrl,
2769
+ agentId,
2770
+ transport: copilotkit.runtimeTransport
2771
+ });
2772
+ provisional.headers = { ...copilotkit.headers };
2773
+ provisionalAgentCache.current.set(agentId, provisional);
2774
+ return provisional;
2775
+ }
2776
+ if (isRuntimeConfigured && status === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Error) {
2718
2777
  const provisional = new _copilotkitnext_core.ProxiedCopilotRuntimeAgent({
2719
2778
  runtimeUrl: copilotkit.runtimeUrl,
2720
2779
  agentId,
@@ -2732,8 +2791,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2732
2791
  copilotkit.runtimeConnectionStatus,
2733
2792
  copilotkit.runtimeUrl,
2734
2793
  copilotkit.runtimeTransport,
2735
- JSON.stringify(copilotkit.headers),
2736
- copilotkit
2794
+ JSON.stringify(copilotkit.headers)
2737
2795
  ]);
2738
2796
  (0, react.useEffect)(() => {
2739
2797
  if (updateFlags.length === 0) return;
@@ -3218,7 +3276,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
3218
3276
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3219
3277
  "data-copilotkit": true,
3220
3278
  "data-testid": "copilot-assistant-message",
3221
- className: (0, tailwind_merge.twMerge)(className),
3279
+ className: (0, tailwind_merge.twMerge)("copilotKitMessage copilotKitAssistantMessage", className),
3222
3280
  ...props,
3223
3281
  "data-message-id": message.id,
3224
3282
  children: [
@@ -3385,7 +3443,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
3385
3443
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3386
3444
  "data-copilotkit": true,
3387
3445
  "data-testid": "copilot-user-message",
3388
- className: (0, tailwind_merge.twMerge)("cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10", className),
3446
+ className: (0, tailwind_merge.twMerge)("copilotKitMessage copilotKitUserMessage cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10", className),
3389
3447
  "data-message-id": message.id,
3390
3448
  ...props,
3391
3449
  children: [BoundMessageRenderer, BoundToolbar]
@@ -3940,7 +3998,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
3940
3998
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3941
3999
  "data-copilotkit": true,
3942
4000
  "data-testid": "copilot-message-list",
3943
- className: (0, tailwind_merge.twMerge)("cpk:flex cpk:flex-col", className),
4001
+ className: (0, tailwind_merge.twMerge)("copilotKitMessages cpk:flex cpk:flex-col", className),
3944
4002
  ...props,
3945
4003
  children: [
3946
4004
  messageElements,
@@ -4103,7 +4161,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4103
4161
  "data-copilotkit": true,
4104
4162
  "data-testid": "copilot-chat",
4105
4163
  "data-copilot-running": isRunning ? "true" : "false",
4106
- className: (0, tailwind_merge.twMerge)("cpk:relative cpk:h-full cpk:flex cpk:flex-col", className),
4164
+ className: (0, tailwind_merge.twMerge)("copilotKitChat cpk:relative cpk:h-full cpk:flex cpk:flex-col", className),
4107
4165
  ...props,
4108
4166
  children: BoundWelcomeScreen
4109
4167
  });
@@ -4122,7 +4180,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4122
4180
  "data-copilotkit": true,
4123
4181
  "data-testid": "copilot-chat",
4124
4182
  "data-copilot-running": isRunning ? "true" : "false",
4125
- className: (0, tailwind_merge.twMerge)("cpk:relative cpk:h-full", className),
4183
+ className: (0, tailwind_merge.twMerge)("copilotKitChat cpk:relative cpk:h-full", className),
4126
4184
  ...props,
4127
4185
  children: [BoundScrollView, BoundInput]
4128
4186
  });
@@ -4395,7 +4453,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4395
4453
 
4396
4454
  //#endregion
4397
4455
  //#region src/components/chat/CopilotChat.tsx
4398
- function CopilotChat({ agentId, threadId, labels, chatView, ...props }) {
4456
+ function CopilotChat({ agentId, threadId, labels, chatView, onError, ...props }) {
4399
4457
  var _ref;
4400
4458
  const existingConfig = useCopilotChatConfiguration();
4401
4459
  const resolvedAgentId = (_ref = agentId !== null && agentId !== void 0 ? agentId : existingConfig === null || existingConfig === void 0 ? void 0 : existingConfig.agentId) !== null && _ref !== void 0 ? _ref : _copilotkitnext_shared.DEFAULT_AGENT_ID;
@@ -4406,6 +4464,27 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4406
4464
  const { agent } = useAgent({ agentId: resolvedAgentId });
4407
4465
  const { copilotkit } = useCopilotKit();
4408
4466
  const { suggestions: autoSuggestions } = useSuggestions({ agentId: resolvedAgentId });
4467
+ const onErrorRef = (0, react.useRef)(onError);
4468
+ (0, react.useEffect)(() => {
4469
+ onErrorRef.current = onError;
4470
+ }, [onError]);
4471
+ (0, react.useEffect)(() => {
4472
+ if (!onErrorRef.current) return;
4473
+ const subscription = copilotkit.subscribe({ onError: (event) => {
4474
+ var _event$context, _event$context2;
4475
+ if (((_event$context = event.context) === null || _event$context === void 0 ? void 0 : _event$context.agentId) === resolvedAgentId || !((_event$context2 = event.context) === null || _event$context2 === void 0 ? void 0 : _event$context2.agentId)) {
4476
+ var _onErrorRef$current;
4477
+ (_onErrorRef$current = onErrorRef.current) === null || _onErrorRef$current === void 0 || _onErrorRef$current.call(onErrorRef, {
4478
+ error: event.error,
4479
+ code: event.code,
4480
+ context: event.context
4481
+ });
4482
+ }
4483
+ } });
4484
+ return () => {
4485
+ subscription.unsubscribe();
4486
+ };
4487
+ }, [copilotkit, resolvedAgentId]);
4409
4488
  const [transcribeMode, setTranscribeMode] = (0, react.useState)("input");
4410
4489
  const [inputValue, setInputValue] = (0, react.useState)("");
4411
4490
  const [transcriptionError, setTranscriptionError] = (0, react.useState)(null);
@@ -4414,20 +4493,27 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4414
4493
  const isMediaRecorderSupported = typeof window !== "undefined" && typeof MediaRecorder !== "undefined";
4415
4494
  const { messageView: providedMessageView, suggestionView: providedSuggestionView, onStop: providedStopHandler, ...restProps } = props;
4416
4495
  (0, react.useEffect)(() => {
4496
+ let detached = false;
4497
+ const connectAbortController = new AbortController();
4498
+ if (agent instanceof _ag_ui_client.HttpAgent) agent.abortController = connectAbortController;
4417
4499
  const connect = async (agent) => {
4418
4500
  try {
4419
4501
  await copilotkit.connectAgent({ agent });
4420
4502
  } catch (error) {
4421
- if (error instanceof _ag_ui_client.AGUIConnectNotImplementedError) {} else throw error;
4503
+ if (detached) return;
4504
+ console.error("CopilotChat: connectAgent failed", error);
4422
4505
  }
4423
4506
  };
4424
4507
  agent.threadId = resolvedThreadId;
4425
4508
  connect(agent);
4426
- return () => {};
4509
+ return () => {
4510
+ detached = true;
4511
+ connectAbortController.abort();
4512
+ agent.detachActiveRun();
4513
+ };
4427
4514
  }, [
4428
4515
  resolvedThreadId,
4429
4516
  agent,
4430
- copilotkit,
4431
4517
  resolvedAgentId
4432
4518
  ]);
4433
4519
  const onSubmitInput = (0, react.useCallback)(async (value) => {
@@ -4442,7 +4528,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4442
4528
  } catch (error) {
4443
4529
  console.error("CopilotChat: runAgent failed", error);
4444
4530
  }
4445
- }, [agent, copilotkit]);
4531
+ }, [agent]);
4446
4532
  const handleSelectSuggestion = (0, react.useCallback)(async (suggestion) => {
4447
4533
  agent.addMessage({
4448
4534
  id: (0, _copilotkitnext_shared.randomUUID)(),
@@ -4454,7 +4540,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4454
4540
  } catch (error) {
4455
4541
  console.error("CopilotChat: runAgent failed after selecting suggestion", error);
4456
4542
  }
4457
- }, [agent, copilotkit]);
4543
+ }, [agent]);
4458
4544
  const stopCurrentRun = (0, react.useCallback)(() => {
4459
4545
  try {
4460
4546
  copilotkit.stopAgent({ agent });
@@ -4466,7 +4552,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4466
4552
  console.error("CopilotChat: abortRun fallback failed", abortError);
4467
4553
  }
4468
4554
  }
4469
- }, [agent, copilotkit]);
4555
+ }, [agent]);
4470
4556
  const handleStartTranscribe = (0, react.useCallback)(() => {
4471
4557
  setTranscriptionError(null);
4472
4558
  setTranscribeMode("transcribe");
@@ -4520,7 +4606,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4520
4606
  } finally {
4521
4607
  setIsTranscribing(false);
4522
4608
  }
4523
- }, [copilotkit]);
4609
+ }, []);
4524
4610
  (0, react.useEffect)(() => {
4525
4611
  if (transcriptionError) {
4526
4612
  const timer = setTimeout(() => {
@@ -4596,7 +4682,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4596
4682
  DefaultCloseIcon.displayName = "CopilotChatToggleButton.CloseIcon";
4597
4683
  const ICON_TRANSITION_STYLE = Object.freeze({ transition: "opacity 120ms ease-out, transform 260ms cubic-bezier(0.22, 1, 0.36, 1)" });
4598
4684
  const ICON_WRAPPER_BASE = "cpk:pointer-events-none cpk:absolute cpk:inset-0 cpk:flex cpk:items-center cpk:justify-center cpk:will-change-transform";
4599
- const BUTTON_BASE_CLASSES = cn("cpk:fixed cpk:bottom-6 cpk:right-6 cpk:z-[1100] cpk:flex cpk:h-14 cpk:w-14 cpk:items-center cpk:justify-center", "cpk:rounded-full cpk:border cpk:border-primary cpk:bg-primary cpk:text-primary-foreground", "cpk:shadow-sm cpk:transition-all cpk:duration-200 cpk:ease-out", "cpk:hover:scale-[1.04] cpk:hover:shadow-md", "cpk:cursor-pointer", "cpk:active:scale-[0.96]", "cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-primary/50 cpk:focus-visible:ring-offset-2 cpk:focus-visible:ring-offset-background", "cpk:disabled:pointer-events-none cpk:disabled:opacity-60");
4685
+ const BUTTON_BASE_CLASSES = cn("copilotKitButton", "cpk:fixed cpk:bottom-6 cpk:right-6 cpk:z-[1100] cpk:flex cpk:h-14 cpk:w-14 cpk:items-center cpk:justify-center", "cpk:rounded-full cpk:border cpk:border-primary cpk:bg-primary cpk:text-primary-foreground", "cpk:shadow-sm cpk:transition-all cpk:duration-200 cpk:ease-out", "cpk:hover:scale-[1.04] cpk:hover:shadow-md", "cpk:cursor-pointer", "cpk:active:scale-[0.96]", "cpk:focus-visible:outline-none cpk:focus-visible:ring-2 cpk:focus-visible:ring-primary/50 cpk:focus-visible:ring-offset-2 cpk:focus-visible:ring-offset-background", "cpk:disabled:pointer-events-none cpk:disabled:opacity-60");
4600
4686
  const CopilotChatToggleButton = react.default.forwardRef(function CopilotChatToggleButton({ openIcon, closeIcon, className, ...buttonProps }, ref) {
4601
4687
  var _configuration$labels, _configuration$isModa, _configuration$setMod;
4602
4688
  const { onClick, type, disabled, ...restProps } = buttonProps;
@@ -4683,7 +4769,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4683
4769
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("header", {
4684
4770
  "data-testid": "copilot-modal-header",
4685
4771
  "data-slot": "copilot-modal-header",
4686
- className: cn("cpk:flex cpk:items-center cpk:justify-between cpk:border-b cpk:border-border cpk:px-4 cpk:py-4", "cpk:bg-background/95 cpk:backdrop-blur cpk:supports-[backdrop-filter]:bg-background/80", className),
4772
+ className: cn("copilotKitHeader", "cpk:flex cpk:items-center cpk:justify-between cpk:border-b cpk:border-border cpk:px-4 cpk:py-4", "cpk:bg-background/95 cpk:backdrop-blur cpk:supports-[backdrop-filter]:bg-background/80", className),
4687
4773
  ...rest,
4688
4774
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
4689
4775
  className: "cpk:flex cpk:w-full cpk:items-center cpk:gap-2",
@@ -4796,7 +4882,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4796
4882
  "data-copilotkit": true,
4797
4883
  "data-testid": "copilot-sidebar",
4798
4884
  "data-copilot-sidebar": true,
4799
- className: cn("cpk:fixed cpk:right-0 cpk:top-0 cpk:z-[1200] cpk:flex", "cpk:h-[100vh] cpk:h-[100dvh] cpk:max-h-screen", "cpk:w-full", "cpk:border-l cpk:border-border cpk:bg-background cpk:text-foreground cpk:shadow-xl", "cpk:transition-transform cpk:duration-300 cpk:ease-out", isSidebarOpen ? "cpk:translate-x-0" : "cpk:translate-x-full cpk:pointer-events-none"),
4885
+ className: cn("copilotKitSidebar copilotKitWindow", "cpk:fixed cpk:right-0 cpk:top-0 cpk:z-[1200] cpk:flex", "cpk:h-[100vh] cpk:h-[100dvh] cpk:max-h-screen", "cpk:w-full", "cpk:border-l cpk:border-border cpk:bg-background cpk:text-foreground cpk:shadow-xl", "cpk:transition-transform cpk:duration-300 cpk:ease-out", isSidebarOpen ? "cpk:translate-x-0" : "cpk:translate-x-full cpk:pointer-events-none"),
4800
4886
  style: {
4801
4887
  ["--sidebar-width"]: widthToCss(sidebarWidth),
4802
4888
  paddingTop: "env(safe-area-inset-top)",
@@ -4960,7 +5046,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
4960
5046
  "aria-label": labels.modalHeaderTitle,
4961
5047
  "data-testid": "copilot-popup",
4962
5048
  "data-copilot-popup": true,
4963
- className: cn("cpk:relative cpk:flex cpk:h-full cpk:w-full cpk:flex-col cpk:overflow-hidden cpk:bg-background cpk:text-foreground", "cpk:origin-bottom cpk:focus:outline-none cpk:transform-gpu cpk:transition-transform cpk:transition-opacity cpk:duration-200 cpk:ease-out", "cpk:md:transition-transform cpk:md:transition-opacity", "cpk:rounded-none cpk:border cpk:border-border/0 cpk:shadow-none cpk:ring-0", "cpk:md:h-[var(--copilot-popup-height)] cpk:md:w-[var(--copilot-popup-width)]", "cpk:md:max-h-[var(--copilot-popup-max-height)] cpk:md:max-w-[var(--copilot-popup-max-width)]", "cpk:md:origin-bottom-right cpk:md:rounded-2xl cpk:md:border-border cpk:md:shadow-xl cpk:md:ring-1 cpk:md:ring-border/40", popupAnimationClass),
5049
+ className: cn("copilotKitPopup copilotKitWindow", "cpk:relative cpk:flex cpk:h-full cpk:w-full cpk:flex-col cpk:overflow-hidden cpk:bg-background cpk:text-foreground", "cpk:origin-bottom cpk:focus:outline-none cpk:transform-gpu cpk:transition-transform cpk:transition-opacity cpk:duration-200 cpk:ease-out", "cpk:md:transition-transform cpk:md:transition-opacity", "cpk:rounded-none cpk:border cpk:border-border/0 cpk:shadow-none cpk:ring-0", "cpk:md:h-[var(--copilot-popup-height)] cpk:md:w-[var(--copilot-popup-width)]", "cpk:md:max-h-[var(--copilot-popup-max-height)] cpk:md:max-w-[var(--copilot-popup-max-width)]", "cpk:md:origin-bottom-right cpk:md:rounded-2xl cpk:md:border-border cpk:md:shadow-xl cpk:md:ring-1 cpk:md:ring-border/40", popupAnimationClass),
4964
5050
  style: popupStyle,
4965
5051
  children: [headerElement, /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
4966
5052
  className: "cpk:flex-1 cpk:overflow-hidden",