@copilotkit/react-core 1.62.0 → 1.62.2-canary.1783457132

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.
@@ -3650,12 +3650,13 @@ const CopilotKitProvider = ({ children, runtimeUrl, headers: headersProp = EMPTY
3650
3650
  }
3651
3651
  const copilotkit = copilotkitRef.current;
3652
3652
  (0, react.useEffect)(() => {
3653
- setRuntimeA2UIEnabled(copilotkit.a2uiEnabled);
3654
- const subscription = copilotkit.subscribe({ onRuntimeConnectionStatusChanged: () => {
3653
+ const syncRuntimeInfo = () => {
3655
3654
  setRuntimeA2UIEnabled(copilotkit.a2uiEnabled);
3656
3655
  setRuntimeOpenGenUIEnabled(copilotkit.openGenerativeUIEnabled);
3657
3656
  setRuntimeLicenseStatus(copilotkit.licenseStatus);
3658
- } });
3657
+ };
3658
+ const subscription = copilotkit.subscribe({ onRuntimeConnectionStatusChanged: syncRuntimeInfo });
3659
+ syncRuntimeInfo();
3659
3660
  return () => {
3660
3661
  subscription.unsubscribe();
3661
3662
  };
@@ -5193,6 +5194,70 @@ function useThreads$1({ agentId, includeArchived, limit, enabled = true }) {
5193
5194
  };
5194
5195
  }
5195
5196
 
5197
+ //#endregion
5198
+ //#region src/v2/hooks/use-memories.tsx
5199
+ function useMemoryStoreSelector(store, selector) {
5200
+ return (0, react.useSyncExternalStore)((0, react.useCallback)((onStoreChange) => {
5201
+ const subscription = store.select(selector).subscribe(onStoreChange);
5202
+ return () => subscription.unsubscribe();
5203
+ }, [store, selector]), () => selector(store.getState()), () => selector(store.getServerState()));
5204
+ }
5205
+ /**
5206
+ * React hook for listing and managing platform memories.
5207
+ *
5208
+ * Reads the memory store owned and wired by `CopilotKitCore`. On mount the
5209
+ * hook exposes the live list plus stable `addMemory` / `updateMemory` /
5210
+ * `removeMemory` / `refresh` callbacks. Mutations are server-authoritative:
5211
+ * each resolves once the platform confirms the operation and rejects with an
5212
+ * `Error` on failure.
5213
+ *
5214
+ * Realtime updates are automatic: the core's memory store opens its own
5215
+ * `user_meta:memories:<joinCode>` channel and applies `memory_metadata` deltas
5216
+ * to the list. You can still call `refresh()` to re-pull the REST snapshot on
5217
+ * demand.
5218
+ *
5219
+ * @returns Memory list state and stable mutation callbacks.
5220
+ *
5221
+ * @example
5222
+ * ```tsx
5223
+ * import { useMemories } from "@copilotkit/react-core";
5224
+ *
5225
+ * function MemoryList() {
5226
+ * const { memories, isLoading, isAvailable, addMemory, removeMemory } =
5227
+ * useMemories();
5228
+ *
5229
+ * if (!isAvailable) return null;
5230
+ * if (isLoading) return <p>Loading…</p>;
5231
+ *
5232
+ * return (
5233
+ * <ul>
5234
+ * {memories.map((m) => (
5235
+ * <li key={m.id}>
5236
+ * {m.content}
5237
+ * <button onClick={() => removeMemory(m.id)}>Delete</button>
5238
+ * </li>
5239
+ * ))}
5240
+ * </ul>
5241
+ * );
5242
+ * }
5243
+ * ```
5244
+ */
5245
+ function useMemories() {
5246
+ const { copilotkit } = useCopilotKit();
5247
+ const store = copilotkit.getMemoryStore();
5248
+ return {
5249
+ memories: useMemoryStoreSelector(store, _copilotkit_core.ɵselectMemories),
5250
+ isLoading: useMemoryStoreSelector(store, _copilotkit_core.ɵselectMemoriesIsLoading),
5251
+ error: useMemoryStoreSelector(store, _copilotkit_core.ɵselectMemoriesError),
5252
+ isAvailable: useMemoryStoreSelector(store, _copilotkit_core.ɵselectMemoriesAvailable),
5253
+ realtimeStatus: useMemoryStoreSelector(store, _copilotkit_core.ɵselectMemoriesRealtimeStatus),
5254
+ refresh: (0, react.useCallback)(() => store.refresh(), [store]),
5255
+ addMemory: (0, react.useCallback)((input) => store.addMemory(input), [store]),
5256
+ updateMemory: (0, react.useCallback)((id, changes) => store.updateMemory(id, changes), [store]),
5257
+ removeMemory: (0, react.useCallback)((id) => store.removeMemory(id), [store])
5258
+ };
5259
+ }
5260
+
5196
5261
  //#endregion
5197
5262
  //#region src/v2/lib/record-annotation.ts
5198
5263
  /**
@@ -7955,9 +8020,57 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
7955
8020
  const { messageView: providedMessageView, suggestionView: providedSuggestionView, onStop: providedStopHandler, ...restProps } = props;
7956
8021
  const [lastConnectedThreadId, setLastConnectedThreadId] = (0, react.useState)(null);
7957
8022
  const isConnecting = hasExplicitThreadId && lastConnectedThreadId !== resolvedThreadId;
8023
+ const activeConnectCountRef = (0, react.useRef)(0);
8024
+ const pendingRunActivityReconnectRef = (0, react.useRef)(false);
8025
+ const runActivityReconnectGenerationRef = (0, react.useRef)(0);
8026
+ const activeLocalRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
8027
+ const recentlyLocalRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
8028
+ const activeWakeRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
8029
+ const recentlyWakeRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
8030
+ const pendingWakeRunIdRef = (0, react.useRef)(void 0);
8031
+ const startRunActivityReconnectRef = (0, react.useRef)(null);
8032
+ const runtimeStatus = copilotkit.runtimeConnectionStatus === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Connected ? "Connected" : copilotkit.runtimeConnectionStatus;
8033
+ const hasNativeIntelligenceRunActivity = hasExplicitThreadId && runtimeStatus === "Connected" && !!copilotkit.intelligence?.wsUrl && copilotkit.threadEndpoints?.realtimeMetadata === true;
8034
+ const [standaloneRunActivityStore] = (0, react.useState)(() => (0, _copilotkit_core.ɵcreateThreadStore)({ fetch: globalThis.fetch }));
7958
8035
  const previousThreadIdRef = (0, react.useRef)(null);
7959
8036
  const hasExplicitThreadIdRef = (0, react.useRef)(hasExplicitThreadId);
7960
8037
  hasExplicitThreadIdRef.current = hasExplicitThreadId;
8038
+ const rememberRecentlyLocalRunId = (0, react.useCallback)((runId) => {
8039
+ const existingTimeout = recentlyLocalRunIdsRef.current.get(runId);
8040
+ if (existingTimeout) clearTimeout(existingTimeout);
8041
+ const timeout = setTimeout(() => {
8042
+ recentlyLocalRunIdsRef.current.delete(runId);
8043
+ }, 3e4);
8044
+ recentlyLocalRunIdsRef.current.set(runId, timeout);
8045
+ }, []);
8046
+ const rememberRecentlyWakeRunId = (0, react.useCallback)((runId) => {
8047
+ const existingTimeout = recentlyWakeRunIdsRef.current.get(runId);
8048
+ if (existingTimeout) clearTimeout(existingTimeout);
8049
+ const timeout = setTimeout(() => {
8050
+ recentlyWakeRunIdsRef.current.delete(runId);
8051
+ }, 3e4);
8052
+ recentlyWakeRunIdsRef.current.set(runId, timeout);
8053
+ }, []);
8054
+ const isLocalActiveRunActivity = (0, react.useCallback)((notification) => {
8055
+ if (notification.agentId && notification.agentId !== resolvedAgentId) return false;
8056
+ if (!notification.runId || !activeLocalRunIdsRef.current.has(notification.runId) && !recentlyLocalRunIdsRef.current.has(notification.runId)) return false;
8057
+ const eventType = notification.eventType.toUpperCase();
8058
+ return eventType === "RUN_STARTED" || eventType === "RUN_FINISHED" || eventType === "RUN_ERROR";
8059
+ }, [resolvedAgentId]);
8060
+ (0, react.useEffect)(() => {
8061
+ const recentlyLocalRunIds = recentlyLocalRunIdsRef.current;
8062
+ const recentlyWakeRunIds = recentlyWakeRunIdsRef.current;
8063
+ return () => {
8064
+ recentlyLocalRunIds.forEach((timeout) => {
8065
+ clearTimeout(timeout);
8066
+ });
8067
+ recentlyLocalRunIds.clear();
8068
+ recentlyWakeRunIds.forEach((timeout) => {
8069
+ clearTimeout(timeout);
8070
+ });
8071
+ recentlyWakeRunIds.clear();
8072
+ };
8073
+ }, []);
7961
8074
  (0, react.useEffect)(() => {
7962
8075
  const threadChanged = previousThreadIdRef.current !== resolvedThreadId;
7963
8076
  previousThreadIdRef.current = resolvedThreadId;
@@ -7970,6 +8083,7 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
7970
8083
  const connectAbortController = new AbortController();
7971
8084
  if (agent instanceof _ag_ui_client.HttpAgent) agent.abortController = connectAbortController;
7972
8085
  const connect = async (agentToConnect) => {
8086
+ activeConnectCountRef.current += 1;
7973
8087
  try {
7974
8088
  await copilotkit.connectAgent({ agent: agentToConnect });
7975
8089
  } catch (error) {
@@ -7980,6 +8094,14 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
7980
8094
  if (!detached) setLastConnectedThreadId(resolvedThreadId);
7981
8095
  });
7982
8096
  else if (!hasExplicitThreadIdRef.current) agentToConnect.setMessages([]);
8097
+ activeConnectCountRef.current = Math.max(0, activeConnectCountRef.current - 1);
8098
+ if (!detached && activeConnectCountRef.current === 0) {
8099
+ const startReconnect = startRunActivityReconnectRef.current;
8100
+ if (pendingRunActivityReconnectRef.current && startReconnect) {
8101
+ pendingRunActivityReconnectRef.current = false;
8102
+ startReconnect(runActivityReconnectGenerationRef.current);
8103
+ }
8104
+ }
7983
8105
  }
7984
8106
  };
7985
8107
  connect(agent);
@@ -7994,6 +8116,119 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
7994
8116
  resolvedAgentId,
7995
8117
  hasExplicitThreadId
7996
8118
  ]);
8119
+ (0, react.useEffect)(() => {
8120
+ if (!hasNativeIntelligenceRunActivity) return;
8121
+ const registeredThreadStore = copilotkit.getThreadStore(resolvedAgentId);
8122
+ const threadStore = registeredThreadStore ?? standaloneRunActivityStore;
8123
+ if (!threadStore?.subscribeToRunActivity) return;
8124
+ const ownsStandaloneStore = registeredThreadStore === void 0;
8125
+ if (ownsStandaloneStore) {
8126
+ threadStore.start();
8127
+ const context = copilotkit.runtimeUrl ? {
8128
+ runtimeUrl: copilotkit.runtimeUrl,
8129
+ headers: { ...copilotkit.headers },
8130
+ wsUrl: copilotkit.intelligence?.wsUrl,
8131
+ agentId: resolvedAgentId
8132
+ } : null;
8133
+ threadStore.setContext(context);
8134
+ }
8135
+ const generation = runActivityReconnectGenerationRef.current + 1;
8136
+ runActivityReconnectGenerationRef.current = generation;
8137
+ let detached = false;
8138
+ let wakeReconnectActive = false;
8139
+ let pendingAgentIdleDrain = null;
8140
+ const hasActiveAgentRun = () => activeLocalRunIdsRef.current.size > 0 || agent.isRunning;
8141
+ const scheduleAgentIdleDrain = () => {
8142
+ if (pendingAgentIdleDrain !== null) return;
8143
+ pendingAgentIdleDrain = setTimeout(() => {
8144
+ pendingAgentIdleDrain = null;
8145
+ if (detached || runActivityReconnectGenerationRef.current !== generation || !pendingRunActivityReconnectRef.current) return;
8146
+ if (hasActiveAgentRun()) {
8147
+ scheduleAgentIdleDrain();
8148
+ return;
8149
+ }
8150
+ startRunActivityReconnectRef.current?.(generation);
8151
+ }, 10);
8152
+ };
8153
+ const connect = async () => {
8154
+ activeConnectCountRef.current += 1;
8155
+ wakeReconnectActive = true;
8156
+ const wakeRunId = pendingWakeRunIdRef.current;
8157
+ pendingWakeRunIdRef.current = void 0;
8158
+ if (wakeRunId) activeWakeRunIdsRef.current.add(wakeRunId);
8159
+ let didConnect = false;
8160
+ try {
8161
+ await copilotkit.connectAgent({ agent });
8162
+ didConnect = true;
8163
+ } catch (error) {
8164
+ if (!detached) console.error("CopilotChat: run activity reconnect failed", error);
8165
+ } finally {
8166
+ if (wakeRunId) {
8167
+ activeWakeRunIdsRef.current.delete(wakeRunId);
8168
+ if (didConnect) rememberRecentlyWakeRunId(wakeRunId);
8169
+ }
8170
+ activeConnectCountRef.current = Math.max(0, activeConnectCountRef.current - 1);
8171
+ wakeReconnectActive = false;
8172
+ if (!detached && runActivityReconnectGenerationRef.current === generation && activeConnectCountRef.current === 0 && pendingRunActivityReconnectRef.current) {
8173
+ pendingRunActivityReconnectRef.current = false;
8174
+ connect();
8175
+ }
8176
+ }
8177
+ };
8178
+ startRunActivityReconnectRef.current = (requestedGeneration) => {
8179
+ if (detached || requestedGeneration !== generation || runActivityReconnectGenerationRef.current !== generation) return;
8180
+ if (hasActiveAgentRun()) {
8181
+ pendingRunActivityReconnectRef.current = true;
8182
+ scheduleAgentIdleDrain();
8183
+ return;
8184
+ }
8185
+ if (activeConnectCountRef.current > 0) {
8186
+ if (!wakeReconnectActive) pendingRunActivityReconnectRef.current = true;
8187
+ return;
8188
+ }
8189
+ pendingRunActivityReconnectRef.current = false;
8190
+ connect();
8191
+ };
8192
+ const subscription = threadStore.subscribeToRunActivity((notification) => {
8193
+ if (notification.threadId !== resolvedThreadId) return;
8194
+ if (notification.agentId && notification.agentId !== resolvedAgentId) return;
8195
+ if (isLocalActiveRunActivity(notification)) return;
8196
+ if (notification.runId && (activeWakeRunIdsRef.current.has(notification.runId) || recentlyWakeRunIdsRef.current.has(notification.runId))) return;
8197
+ pendingWakeRunIdRef.current = notification.runId;
8198
+ startRunActivityReconnectRef.current?.(generation);
8199
+ });
8200
+ return () => {
8201
+ detached = true;
8202
+ pendingRunActivityReconnectRef.current = false;
8203
+ pendingWakeRunIdRef.current = void 0;
8204
+ if (pendingAgentIdleDrain !== null) {
8205
+ clearTimeout(pendingAgentIdleDrain);
8206
+ pendingAgentIdleDrain = null;
8207
+ }
8208
+ if (startRunActivityReconnectRef.current) startRunActivityReconnectRef.current = null;
8209
+ if (wakeReconnectActive) agent.detachActiveRun().catch(() => {});
8210
+ activeWakeRunIdsRef.current.clear();
8211
+ subscription.unsubscribe();
8212
+ if (ownsStandaloneStore) {
8213
+ threadStore.setContext(null);
8214
+ threadStore.stop();
8215
+ }
8216
+ };
8217
+ }, [
8218
+ agent,
8219
+ resolvedAgentId,
8220
+ resolvedThreadId,
8221
+ hasExplicitThreadId,
8222
+ hasNativeIntelligenceRunActivity,
8223
+ copilotkit.runtimeConnectionStatus,
8224
+ copilotkit.runtimeUrl,
8225
+ copilotkit.headers,
8226
+ copilotkit.intelligence?.wsUrl,
8227
+ copilotkit.threadEndpoints?.realtimeMetadata,
8228
+ standaloneRunActivityStore,
8229
+ isLocalActiveRunActivity,
8230
+ rememberRecentlyWakeRunId
8231
+ ]);
7997
8232
  const waitForActiveRunToSettle = (0, react.useCallback)(async () => {
7998
8233
  const maybeAware = agent;
7999
8234
  const activeRunCompletionPromise = (0, _copilotkit_core.isRunCompletionAware)(maybeAware) ? maybeAware.activeRunCompletionPromise : void 0;
@@ -8042,15 +8277,34 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
8042
8277
  role: "user",
8043
8278
  content: value
8044
8279
  });
8280
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
8281
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
8045
8282
  try {
8046
- await copilotkit.runAgent({ agent });
8283
+ await copilotkit.runAgent({
8284
+ agent,
8285
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
8286
+ });
8047
8287
  } catch (error) {
8048
8288
  console.error("CopilotChat: runAgent failed", error);
8289
+ } finally {
8290
+ if (localRunId) {
8291
+ activeLocalRunIdsRef.current.delete(localRunId);
8292
+ rememberRecentlyLocalRunId(localRunId);
8293
+ }
8294
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
8295
+ const startReconnect = startRunActivityReconnectRef.current;
8296
+ if (startReconnect) {
8297
+ pendingRunActivityReconnectRef.current = false;
8298
+ startReconnect(runActivityReconnectGenerationRef.current);
8299
+ }
8300
+ }
8049
8301
  }
8050
8302
  }, [
8051
8303
  agent,
8052
8304
  consumeAttachments,
8053
- waitForActiveRunToSettle
8305
+ waitForActiveRunToSettle,
8306
+ hasNativeIntelligenceRunActivity,
8307
+ rememberRecentlyLocalRunId
8054
8308
  ]);
8055
8309
  const handleSelectSuggestion = (0, react.useCallback)(async (suggestion) => {
8056
8310
  await waitForActiveRunToSettle();
@@ -8059,12 +8313,34 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
8059
8313
  role: "user",
8060
8314
  content: suggestion.message
8061
8315
  });
8316
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
8317
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
8062
8318
  try {
8063
- await copilotkit.runAgent({ agent });
8319
+ await copilotkit.runAgent({
8320
+ agent,
8321
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
8322
+ });
8064
8323
  } catch (error) {
8065
8324
  console.error("CopilotChat: runAgent failed after selecting suggestion", error);
8325
+ } finally {
8326
+ if (localRunId) {
8327
+ activeLocalRunIdsRef.current.delete(localRunId);
8328
+ rememberRecentlyLocalRunId(localRunId);
8329
+ }
8330
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
8331
+ const startReconnect = startRunActivityReconnectRef.current;
8332
+ if (startReconnect) {
8333
+ pendingRunActivityReconnectRef.current = false;
8334
+ startReconnect(runActivityReconnectGenerationRef.current);
8335
+ }
8336
+ }
8066
8337
  }
8067
- }, [agent, waitForActiveRunToSettle]);
8338
+ }, [
8339
+ agent,
8340
+ waitForActiveRunToSettle,
8341
+ hasNativeIntelligenceRunActivity,
8342
+ rememberRecentlyLocalRunId
8343
+ ]);
8068
8344
  const stopCurrentRun = (0, react.useCallback)(() => {
8069
8345
  try {
8070
8346
  copilotkit.stopAgent({ agent });
@@ -11706,6 +11982,12 @@ Object.defineProperty(exports, 'useLearningContainersInCurrentThread', {
11706
11982
  return useLearningContainersInCurrentThread;
11707
11983
  }
11708
11984
  });
11985
+ Object.defineProperty(exports, 'useMemories', {
11986
+ enumerable: true,
11987
+ get: function () {
11988
+ return useMemories;
11989
+ }
11990
+ });
11709
11991
  Object.defineProperty(exports, 'useRenderActivityMessage', {
11710
11992
  enumerable: true,
11711
11993
  get: function () {
@@ -11760,4 +12042,4 @@ Object.defineProperty(exports, 'useToast', {
11760
12042
  return useToast;
11761
12043
  }
11762
12044
  });
11763
- //# sourceMappingURL=copilotkit-CdpDZi3i.cjs.map
12045
+ //# sourceMappingURL=copilotkit-Ces-OkSR.cjs.map