@copilotkit/react-core 1.62.0 → 1.62.2

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/dist/v2/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
- const require_copilotkit = require('../copilotkit-CdpDZi3i.cjs');
4
+ const require_copilotkit = require('../copilotkit-lHheGQWt.cjs');
5
5
  require("./index.css");
6
6
  let _copilotkit_a2ui_renderer = require("@copilotkit/a2ui-renderer");
7
7
 
package/dist/v2/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { $ as UseAgentUpdate, A as CopilotChatMessageView, B as CopilotChatAssistantMessage_default, C as CopilotModalHeader, Ct as CopilotChatConfigurationProvider, D as CopilotChat, E as DefaultOpenIcon, F as CopilotChatSuggestionView, G as useLearnFromUserActionInCurrentThread, H as useLearningContainersInCurrentThread, I as CopilotChatSuggestionPill, J as useInterrupt, K as useLearnFromUserAction, L as CopilotChatReasoningMessage_default, M as IntelligenceIndicator, N as getIntelligenceTurnAnchors, O as CopilotChatView_default, P as IntelligenceIndicatorView, Q as useCapabilities, R as CopilotChatUserMessage_default, S as CopilotSidebarView, St as CopilotChatAudioRecorder, T as DefaultCloseIcon, U as useLearningContainers, V as CopilotChatToolCallsView, W as useAttachments, X as useSuggestions, Y as useConfigureSuggestions, Z as useAgentContext, _ as WildcardToolCallRender, _t as useRenderToolCall, at as useFrontendTool, b as CopilotSidebar, bt as CopilotChatInput_default, ct as CopilotKitProvider, dt as SandboxFunctionsContext, et as useAgent, ft as useSandboxFunctions, gt as CopilotKitInspector, ht as MCPAppsActivityType, it as useComponent, j as INTELLIGENCE_TURN_HEAD, k as CopilotChatAttachmentQueue, lt as defineToolCallRenderer, mt as MCPAppsActivityRenderer, nt as useDefaultRenderTool, ot as useRenderActivityMessage, pt as MCPAppsActivityContentSchema, q as useThreads, rt as useRenderTool, st as useRenderCustomMessages, t as CopilotKit, tt as useHumanInTheLoop, ut as createA2UIMessageRenderer, v as CopilotThreadsDrawer, vt as useCopilotKit, w as CopilotChatToggleButton, wt as useCopilotChatConfiguration, x as CopilotPopupView, xt as AudioRecorderError, y as CopilotPopup, yt as CopilotKitCoreReact, z as CopilotChatAttachmentRenderer } from "../copilotkit-BtRkFsNR.mjs";
3
+ import { $ as UseAgentUpdate, A as CopilotChatMessageView, B as CopilotChatAssistantMessage_default, C as CopilotModalHeader, Ct as CopilotChatConfigurationProvider, D as CopilotChat, E as DefaultOpenIcon, F as CopilotChatSuggestionView, G as useLearnFromUserActionInCurrentThread, H as useLearningContainersInCurrentThread, I as CopilotChatSuggestionPill, J as useInterrupt, K as useLearnFromUserAction, L as CopilotChatReasoningMessage_default, M as IntelligenceIndicator, N as getIntelligenceTurnAnchors, O as CopilotChatView_default, P as IntelligenceIndicatorView, Q as useCapabilities, R as CopilotChatUserMessage_default, S as CopilotSidebarView, St as CopilotChatAudioRecorder, T as DefaultCloseIcon, U as useLearningContainers, V as CopilotChatToolCallsView, W as useAttachments, X as useSuggestions, Y as useConfigureSuggestions, Z as useAgentContext, _ as WildcardToolCallRender, _t as useRenderToolCall, at as useFrontendTool, b as CopilotSidebar, bt as CopilotChatInput_default, ct as CopilotKitProvider, dt as SandboxFunctionsContext, et as useAgent, ft as useSandboxFunctions, gt as CopilotKitInspector, ht as MCPAppsActivityType, it as useComponent, j as INTELLIGENCE_TURN_HEAD, k as CopilotChatAttachmentQueue, lt as defineToolCallRenderer, mt as MCPAppsActivityRenderer, nt as useDefaultRenderTool, ot as useRenderActivityMessage, pt as MCPAppsActivityContentSchema, q as useThreads, rt as useRenderTool, st as useRenderCustomMessages, t as CopilotKit, tt as useHumanInTheLoop, ut as createA2UIMessageRenderer, v as CopilotThreadsDrawer, vt as useCopilotKit, w as CopilotChatToggleButton, wt as useCopilotChatConfiguration, x as CopilotPopupView, xt as AudioRecorderError, y as CopilotPopup, yt as CopilotKitCoreReact, z as CopilotChatAttachmentRenderer } from "../copilotkit-CmcMFc8o.mjs";
4
4
  import "./index.css";
5
5
  import { defaultTheme as a2uiDefaultTheme } from "@copilotkit/a2ui-renderer";
6
6
 
@@ -3635,12 +3635,13 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
3635
3635
  }
3636
3636
  const copilotkit = copilotkitRef.current;
3637
3637
  (0, react.useEffect)(() => {
3638
- setRuntimeA2UIEnabled(copilotkit.a2uiEnabled);
3639
- const subscription = copilotkit.subscribe({ onRuntimeConnectionStatusChanged: () => {
3638
+ const syncRuntimeInfo = () => {
3640
3639
  setRuntimeA2UIEnabled(copilotkit.a2uiEnabled);
3641
3640
  setRuntimeOpenGenUIEnabled(copilotkit.openGenerativeUIEnabled);
3642
3641
  setRuntimeLicenseStatus(copilotkit.licenseStatus);
3643
- } });
3642
+ };
3643
+ const subscription = copilotkit.subscribe({ onRuntimeConnectionStatusChanged: syncRuntimeInfo });
3644
+ syncRuntimeInfo();
3644
3645
  return () => {
3645
3646
  subscription.unsubscribe();
3646
3647
  };
@@ -7940,9 +7941,57 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
7940
7941
  const { messageView: providedMessageView, suggestionView: providedSuggestionView, onStop: providedStopHandler, ...restProps } = props;
7941
7942
  const [lastConnectedThreadId, setLastConnectedThreadId] = (0, react.useState)(null);
7942
7943
  const isConnecting = hasExplicitThreadId && lastConnectedThreadId !== resolvedThreadId;
7944
+ const activeConnectCountRef = (0, react.useRef)(0);
7945
+ const pendingRunActivityReconnectRef = (0, react.useRef)(false);
7946
+ const runActivityReconnectGenerationRef = (0, react.useRef)(0);
7947
+ const activeLocalRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
7948
+ const recentlyLocalRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
7949
+ const activeWakeRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
7950
+ const recentlyWakeRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
7951
+ const pendingWakeRunIdRef = (0, react.useRef)(void 0);
7952
+ const startRunActivityReconnectRef = (0, react.useRef)(null);
7953
+ const runtimeStatus = copilotkit.runtimeConnectionStatus === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Connected ? "Connected" : copilotkit.runtimeConnectionStatus;
7954
+ const hasNativeIntelligenceRunActivity = hasExplicitThreadId && runtimeStatus === "Connected" && !!copilotkit.intelligence?.wsUrl && copilotkit.threadEndpoints?.realtimeMetadata === true;
7955
+ const [standaloneRunActivityStore] = (0, react.useState)(() => (0, _copilotkit_core.ɵcreateThreadStore)({ fetch: globalThis.fetch }));
7943
7956
  const previousThreadIdRef = (0, react.useRef)(null);
7944
7957
  const hasExplicitThreadIdRef = (0, react.useRef)(hasExplicitThreadId);
7945
7958
  hasExplicitThreadIdRef.current = hasExplicitThreadId;
7959
+ const rememberRecentlyLocalRunId = (0, react.useCallback)((runId) => {
7960
+ const existingTimeout = recentlyLocalRunIdsRef.current.get(runId);
7961
+ if (existingTimeout) clearTimeout(existingTimeout);
7962
+ const timeout = setTimeout(() => {
7963
+ recentlyLocalRunIdsRef.current.delete(runId);
7964
+ }, 3e4);
7965
+ recentlyLocalRunIdsRef.current.set(runId, timeout);
7966
+ }, []);
7967
+ const rememberRecentlyWakeRunId = (0, react.useCallback)((runId) => {
7968
+ const existingTimeout = recentlyWakeRunIdsRef.current.get(runId);
7969
+ if (existingTimeout) clearTimeout(existingTimeout);
7970
+ const timeout = setTimeout(() => {
7971
+ recentlyWakeRunIdsRef.current.delete(runId);
7972
+ }, 3e4);
7973
+ recentlyWakeRunIdsRef.current.set(runId, timeout);
7974
+ }, []);
7975
+ const isLocalActiveRunActivity = (0, react.useCallback)((notification) => {
7976
+ if (notification.agentId && notification.agentId !== resolvedAgentId) return false;
7977
+ if (!notification.runId || !activeLocalRunIdsRef.current.has(notification.runId) && !recentlyLocalRunIdsRef.current.has(notification.runId)) return false;
7978
+ const eventType = notification.eventType.toUpperCase();
7979
+ return eventType === "RUN_STARTED" || eventType === "RUN_FINISHED" || eventType === "RUN_ERROR";
7980
+ }, [resolvedAgentId]);
7981
+ (0, react.useEffect)(() => {
7982
+ const recentlyLocalRunIds = recentlyLocalRunIdsRef.current;
7983
+ const recentlyWakeRunIds = recentlyWakeRunIdsRef.current;
7984
+ return () => {
7985
+ recentlyLocalRunIds.forEach((timeout) => {
7986
+ clearTimeout(timeout);
7987
+ });
7988
+ recentlyLocalRunIds.clear();
7989
+ recentlyWakeRunIds.forEach((timeout) => {
7990
+ clearTimeout(timeout);
7991
+ });
7992
+ recentlyWakeRunIds.clear();
7993
+ };
7994
+ }, []);
7946
7995
  (0, react.useEffect)(() => {
7947
7996
  const threadChanged = previousThreadIdRef.current !== resolvedThreadId;
7948
7997
  previousThreadIdRef.current = resolvedThreadId;
@@ -7955,6 +8004,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
7955
8004
  const connectAbortController = new AbortController();
7956
8005
  if (agent instanceof _ag_ui_client.HttpAgent) agent.abortController = connectAbortController;
7957
8006
  const connect = async (agentToConnect) => {
8007
+ activeConnectCountRef.current += 1;
7958
8008
  try {
7959
8009
  await copilotkit.connectAgent({ agent: agentToConnect });
7960
8010
  } catch (error) {
@@ -7965,6 +8015,14 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
7965
8015
  if (!detached) setLastConnectedThreadId(resolvedThreadId);
7966
8016
  });
7967
8017
  else if (!hasExplicitThreadIdRef.current) agentToConnect.setMessages([]);
8018
+ activeConnectCountRef.current = Math.max(0, activeConnectCountRef.current - 1);
8019
+ if (!detached && activeConnectCountRef.current === 0) {
8020
+ const startReconnect = startRunActivityReconnectRef.current;
8021
+ if (pendingRunActivityReconnectRef.current && startReconnect) {
8022
+ pendingRunActivityReconnectRef.current = false;
8023
+ startReconnect(runActivityReconnectGenerationRef.current);
8024
+ }
8025
+ }
7968
8026
  }
7969
8027
  };
7970
8028
  connect(agent);
@@ -7979,6 +8037,119 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
7979
8037
  resolvedAgentId,
7980
8038
  hasExplicitThreadId
7981
8039
  ]);
8040
+ (0, react.useEffect)(() => {
8041
+ if (!hasNativeIntelligenceRunActivity) return;
8042
+ const registeredThreadStore = copilotkit.getThreadStore(resolvedAgentId);
8043
+ const threadStore = registeredThreadStore ?? standaloneRunActivityStore;
8044
+ if (!threadStore?.subscribeToRunActivity) return;
8045
+ const ownsStandaloneStore = registeredThreadStore === void 0;
8046
+ if (ownsStandaloneStore) {
8047
+ threadStore.start();
8048
+ const context = copilotkit.runtimeUrl ? {
8049
+ runtimeUrl: copilotkit.runtimeUrl,
8050
+ headers: { ...copilotkit.headers },
8051
+ wsUrl: copilotkit.intelligence?.wsUrl,
8052
+ agentId: resolvedAgentId
8053
+ } : null;
8054
+ threadStore.setContext(context);
8055
+ }
8056
+ const generation = runActivityReconnectGenerationRef.current + 1;
8057
+ runActivityReconnectGenerationRef.current = generation;
8058
+ let detached = false;
8059
+ let wakeReconnectActive = false;
8060
+ let pendingAgentIdleDrain = null;
8061
+ const hasActiveAgentRun = () => activeLocalRunIdsRef.current.size > 0 || agent.isRunning;
8062
+ const scheduleAgentIdleDrain = () => {
8063
+ if (pendingAgentIdleDrain !== null) return;
8064
+ pendingAgentIdleDrain = setTimeout(() => {
8065
+ pendingAgentIdleDrain = null;
8066
+ if (detached || runActivityReconnectGenerationRef.current !== generation || !pendingRunActivityReconnectRef.current) return;
8067
+ if (hasActiveAgentRun()) {
8068
+ scheduleAgentIdleDrain();
8069
+ return;
8070
+ }
8071
+ startRunActivityReconnectRef.current?.(generation);
8072
+ }, 10);
8073
+ };
8074
+ const connect = async () => {
8075
+ activeConnectCountRef.current += 1;
8076
+ wakeReconnectActive = true;
8077
+ const wakeRunId = pendingWakeRunIdRef.current;
8078
+ pendingWakeRunIdRef.current = void 0;
8079
+ if (wakeRunId) activeWakeRunIdsRef.current.add(wakeRunId);
8080
+ let didConnect = false;
8081
+ try {
8082
+ await copilotkit.connectAgent({ agent });
8083
+ didConnect = true;
8084
+ } catch (error) {
8085
+ if (!detached) console.error("CopilotChat: run activity reconnect failed", error);
8086
+ } finally {
8087
+ if (wakeRunId) {
8088
+ activeWakeRunIdsRef.current.delete(wakeRunId);
8089
+ if (didConnect) rememberRecentlyWakeRunId(wakeRunId);
8090
+ }
8091
+ activeConnectCountRef.current = Math.max(0, activeConnectCountRef.current - 1);
8092
+ wakeReconnectActive = false;
8093
+ if (!detached && runActivityReconnectGenerationRef.current === generation && activeConnectCountRef.current === 0 && pendingRunActivityReconnectRef.current) {
8094
+ pendingRunActivityReconnectRef.current = false;
8095
+ connect();
8096
+ }
8097
+ }
8098
+ };
8099
+ startRunActivityReconnectRef.current = (requestedGeneration) => {
8100
+ if (detached || requestedGeneration !== generation || runActivityReconnectGenerationRef.current !== generation) return;
8101
+ if (hasActiveAgentRun()) {
8102
+ pendingRunActivityReconnectRef.current = true;
8103
+ scheduleAgentIdleDrain();
8104
+ return;
8105
+ }
8106
+ if (activeConnectCountRef.current > 0) {
8107
+ if (!wakeReconnectActive) pendingRunActivityReconnectRef.current = true;
8108
+ return;
8109
+ }
8110
+ pendingRunActivityReconnectRef.current = false;
8111
+ connect();
8112
+ };
8113
+ const subscription = threadStore.subscribeToRunActivity((notification) => {
8114
+ if (notification.threadId !== resolvedThreadId) return;
8115
+ if (notification.agentId && notification.agentId !== resolvedAgentId) return;
8116
+ if (isLocalActiveRunActivity(notification)) return;
8117
+ if (notification.runId && (activeWakeRunIdsRef.current.has(notification.runId) || recentlyWakeRunIdsRef.current.has(notification.runId))) return;
8118
+ pendingWakeRunIdRef.current = notification.runId;
8119
+ startRunActivityReconnectRef.current?.(generation);
8120
+ });
8121
+ return () => {
8122
+ detached = true;
8123
+ pendingRunActivityReconnectRef.current = false;
8124
+ pendingWakeRunIdRef.current = void 0;
8125
+ if (pendingAgentIdleDrain !== null) {
8126
+ clearTimeout(pendingAgentIdleDrain);
8127
+ pendingAgentIdleDrain = null;
8128
+ }
8129
+ if (startRunActivityReconnectRef.current) startRunActivityReconnectRef.current = null;
8130
+ if (wakeReconnectActive) agent.detachActiveRun().catch(() => {});
8131
+ activeWakeRunIdsRef.current.clear();
8132
+ subscription.unsubscribe();
8133
+ if (ownsStandaloneStore) {
8134
+ threadStore.setContext(null);
8135
+ threadStore.stop();
8136
+ }
8137
+ };
8138
+ }, [
8139
+ agent,
8140
+ resolvedAgentId,
8141
+ resolvedThreadId,
8142
+ hasExplicitThreadId,
8143
+ hasNativeIntelligenceRunActivity,
8144
+ copilotkit.runtimeConnectionStatus,
8145
+ copilotkit.runtimeUrl,
8146
+ copilotkit.headers,
8147
+ copilotkit.intelligence?.wsUrl,
8148
+ copilotkit.threadEndpoints?.realtimeMetadata,
8149
+ standaloneRunActivityStore,
8150
+ isLocalActiveRunActivity,
8151
+ rememberRecentlyWakeRunId
8152
+ ]);
7982
8153
  const waitForActiveRunToSettle = (0, react.useCallback)(async () => {
7983
8154
  const maybeAware = agent;
7984
8155
  const activeRunCompletionPromise = (0, _copilotkit_core.isRunCompletionAware)(maybeAware) ? maybeAware.activeRunCompletionPromise : void 0;
@@ -8027,15 +8198,34 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
8027
8198
  role: "user",
8028
8199
  content: value
8029
8200
  });
8201
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
8202
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
8030
8203
  try {
8031
- await copilotkit.runAgent({ agent });
8204
+ await copilotkit.runAgent({
8205
+ agent,
8206
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
8207
+ });
8032
8208
  } catch (error) {
8033
8209
  console.error("CopilotChat: runAgent failed", error);
8210
+ } finally {
8211
+ if (localRunId) {
8212
+ activeLocalRunIdsRef.current.delete(localRunId);
8213
+ rememberRecentlyLocalRunId(localRunId);
8214
+ }
8215
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
8216
+ const startReconnect = startRunActivityReconnectRef.current;
8217
+ if (startReconnect) {
8218
+ pendingRunActivityReconnectRef.current = false;
8219
+ startReconnect(runActivityReconnectGenerationRef.current);
8220
+ }
8221
+ }
8034
8222
  }
8035
8223
  }, [
8036
8224
  agent,
8037
8225
  consumeAttachments,
8038
- waitForActiveRunToSettle
8226
+ waitForActiveRunToSettle,
8227
+ hasNativeIntelligenceRunActivity,
8228
+ rememberRecentlyLocalRunId
8039
8229
  ]);
8040
8230
  const handleSelectSuggestion = (0, react.useCallback)(async (suggestion) => {
8041
8231
  await waitForActiveRunToSettle();
@@ -8044,12 +8234,34 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
8044
8234
  role: "user",
8045
8235
  content: suggestion.message
8046
8236
  });
8237
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
8238
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
8047
8239
  try {
8048
- await copilotkit.runAgent({ agent });
8240
+ await copilotkit.runAgent({
8241
+ agent,
8242
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
8243
+ });
8049
8244
  } catch (error) {
8050
8245
  console.error("CopilotChat: runAgent failed after selecting suggestion", error);
8246
+ } finally {
8247
+ if (localRunId) {
8248
+ activeLocalRunIdsRef.current.delete(localRunId);
8249
+ rememberRecentlyLocalRunId(localRunId);
8250
+ }
8251
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
8252
+ const startReconnect = startRunActivityReconnectRef.current;
8253
+ if (startReconnect) {
8254
+ pendingRunActivityReconnectRef.current = false;
8255
+ startReconnect(runActivityReconnectGenerationRef.current);
8256
+ }
8257
+ }
8051
8258
  }
8052
- }, [agent, waitForActiveRunToSettle]);
8259
+ }, [
8260
+ agent,
8261
+ waitForActiveRunToSettle,
8262
+ hasNativeIntelligenceRunActivity,
8263
+ rememberRecentlyLocalRunId
8264
+ ]);
8053
8265
  const stopCurrentRun = (0, react.useCallback)(() => {
8054
8266
  try {
8055
8267
  copilotkit.stopAgent({ agent });