@copilotkit/react-core 1.77.0 → 1.77.1

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.
@@ -1655,7 +1655,7 @@ function useRenderToolCall() {
1655
1655
 
1656
1656
  //#endregion
1657
1657
  //#region package.json
1658
- var version = "1.77.0";
1658
+ var version = "1.77.1";
1659
1659
 
1660
1660
  //#endregion
1661
1661
  //#region src/v2/components/CopilotKitInspector.tsx
@@ -3412,7 +3412,6 @@ const DEFAULT_DESIGN_SKILL = `When generating UI with generateSandboxedUi, follo
3412
3412
  - Use CSS Grid or Flexbox for layout. Ensure the UI looks good at any width.
3413
3413
  - Minimal transitions (150ms) for hover/focus states only. No decorative animations.
3414
3414
  - Keep the UI focused and dense — avoid excessive padding. Use compact spacing (8–12px gaps, 10–14px padding in controls).`;
3415
- const GENERATE_SANDBOXED_UI_DESCRIPTION = "Generate sandboxed UI. IMPORTANT: The generated code runs in a sandboxed iframe WITHOUT same-origin access. Do NOT use localStorage, sessionStorage, document.cookie, IndexedDB, or fetch/XMLHttpRequest to same-origin URLs. To communicate with the host application, use Websandbox.connection.remote.<functionName>(args) which returns a Promise.\n\nYou CAN use external libraries from CDNs by including <script> or <link> tags in the HTML <head> (e.g., Chart.js, D3, Three.js, x-data-spreadsheet, etc.). CDN resources load normally inside the sandbox.\n\nPARAMETER ORDER IS CRITICAL — generate parameters in exactly this order:\n1. initialHeight + placeholderMessages (shown to user while generating)\n2. css (all styles FIRST — the user sees a placeholder until CSS is complete)\n3. html (streams in live — the user watches the UI build as HTML is generated)\n4. jsFunctions (reusable helper functions)\n5. jsExpressions (applied one-by-one — the user sees each expression take effect)";
3416
3415
  function useStableArrayProp(prop, warningMessage, isMeaningfulChange) {
3417
3416
  const empty = (0, react.useMemo)(() => [], []);
3418
3417
  const value = prop ?? empty;
@@ -3605,7 +3604,7 @@ const CopilotKitProvider = ({ children, runtimeUrl, headers: headersProp = EMPTY
3605
3604
  if (!openGenUIActive) return [];
3606
3605
  return [{
3607
3606
  name: "generateSandboxedUi",
3608
- description: GENERATE_SANDBOXED_UI_DESCRIPTION,
3607
+ description: _copilotkit_shared.GENERATE_SANDBOXED_UI_DESCRIPTION,
3609
3608
  parameters: GenerateSandboxedUiArgsSchema,
3610
3609
  handler: async () => "UI generated",
3611
3610
  followUp: true,
@@ -7684,8 +7683,23 @@ function deduplicateMessages(messages) {
7684
7683
  }
7685
7684
  return [...acc.values()];
7686
7685
  }
7686
+ /** A row holding a single message, rendered as it would be ungrouped. */
7687
+ function messageRow(message) {
7688
+ return {
7689
+ type: "message",
7690
+ message
7691
+ };
7692
+ }
7693
+ /** A row holding a group of messages, rendered inside `wrapper`. */
7694
+ function messageGroup(group) {
7695
+ return {
7696
+ type: "group",
7697
+ ...group
7698
+ };
7699
+ }
7700
+ const GROUP_ROW_KEY_PREFIX = "copilotkit-group:";
7687
7701
  const VIRTUALIZE_THRESHOLD = 50;
7688
- function CopilotChatMessageView({ messages = [], assistantMessage, userMessage, reasoningMessage, cursor, intelligenceIndicator, isRunning = false, transformMessages, children, className, ...props }) {
7702
+ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage, reasoningMessage, cursor, intelligenceIndicator, isRunning = false, transformMessages, groupMessages, children, className, ...props }) {
7689
7703
  const isPinnedToBottom = (0, react.useContext)(ScrollPinnedContext);
7690
7704
  const renderCustomMessage = useRenderCustomMessages();
7691
7705
  const { renderActivityMessage } = useRenderActivityMessage();
@@ -7718,20 +7732,43 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7718
7732
  return copilotkit.getStateByRun(config.agentId, config.threadId, resolvedRunId);
7719
7733
  };
7720
7734
  const deduplicatedMessages = (0, react.useMemo)(() => deduplicateMessages(messages), [messages]);
7721
- const renderedMessages = (0, react.useMemo)(() => transformMessages ? transformMessages(deduplicatedMessages) : deduplicatedMessages, [deduplicatedMessages, transformMessages]);
7735
+ const transformedMessages = (0, react.useMemo)(() => transformMessages ? transformMessages(deduplicatedMessages) : deduplicatedMessages, [deduplicatedMessages, transformMessages]);
7736
+ const rows = (0, react.useMemo)(() => groupMessages ? groupMessages(transformedMessages) : transformedMessages.map(messageRow), [transformedMessages, groupMessages]);
7737
+ const renderedMessages = (0, react.useMemo)(() => groupMessages ? rows.flatMap((row) => row.type === "group" ? row.messages : [row.message]) : transformedMessages, [
7738
+ rows,
7739
+ groupMessages,
7740
+ transformedMessages
7741
+ ]);
7722
7742
  const latestRenderedId = renderedMessages[renderedMessages.length - 1]?.id;
7723
7743
  const transformDuplicateId = (0, react.useMemo)(() => {
7724
- if (process.env.NODE_ENV === "production" || !transformMessages) return;
7744
+ if (process.env.NODE_ENV === "production" || !transformMessages && !groupMessages) return;
7725
7745
  const seen = /* @__PURE__ */ new Set();
7726
7746
  for (const message of renderedMessages) {
7727
7747
  if (seen.has(message.id)) return message.id;
7728
7748
  seen.add(message.id);
7729
7749
  }
7730
- }, [renderedMessages, transformMessages]);
7750
+ }, [
7751
+ renderedMessages,
7752
+ transformMessages,
7753
+ groupMessages
7754
+ ]);
7731
7755
  (0, react.useEffect)(() => {
7732
7756
  if (transformDuplicateId === void 0) return;
7733
- console.warn(`[CopilotKit] CopilotChatMessageView: \`transformMessages\` returned more than one message with id "${transformDuplicateId}". Return each id at most once; a message you create needs its own id, stable across renders.`);
7734
- }, [transformDuplicateId]);
7757
+ console.warn(groupMessages ? `[CopilotKit] CopilotChatMessageView: the message with id "${transformDuplicateId}" renders more than once. \`groupMessages\` should place each message in at most one row, and \`transformMessages\` return each id at most once.` : `[CopilotKit] CopilotChatMessageView: \`transformMessages\` returned more than one message with id "${transformDuplicateId}". Return each id at most once; a message you create needs its own id, stable across renders.`);
7758
+ }, [transformDuplicateId, groupMessages]);
7759
+ const duplicateGroupKey = (0, react.useMemo)(() => {
7760
+ if (process.env.NODE_ENV === "production" || !groupMessages) return;
7761
+ const seen = /* @__PURE__ */ new Set();
7762
+ for (const row of rows) {
7763
+ if (row.type !== "group") continue;
7764
+ if (seen.has(row.key)) return row.key;
7765
+ seen.add(row.key);
7766
+ }
7767
+ }, [rows, groupMessages]);
7768
+ (0, react.useEffect)(() => {
7769
+ if (duplicateGroupKey === void 0) return;
7770
+ console.warn(`[CopilotKit] CopilotChatMessageView: \`groupMessages\` returned more than one group with key "${duplicateGroupKey}". Give each group its own key, stable across renders.`);
7771
+ }, [duplicateGroupKey]);
7735
7772
  const rowKeyStoreRef = (0, react.useRef)(null);
7736
7773
  rowKeyStoreRef.current ??= (0, _copilotkit_shared.createRowKeyStore)();
7737
7774
  const rowKeyStore = rowKeyStoreRef.current;
@@ -7739,6 +7776,31 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7739
7776
  (0, react.useLayoutEffect)(() => {
7740
7777
  (0, _copilotkit_shared.commitRowKeyStore)(rowKeyStore, renderedMessages);
7741
7778
  }, [rowKeyStore, renderedMessages]);
7779
+ const stableGroupKey = (key) => rowRenderKeys.get(key) ?? key;
7780
+ const groupStateRef = (0, react.useRef)(/* @__PURE__ */ new Map());
7781
+ const groupSettersRef = (0, react.useRef)(/* @__PURE__ */ new Map());
7782
+ const groupStateThreadRef = (0, react.useRef)(config?.threadId);
7783
+ if (groupStateThreadRef.current !== config?.threadId) {
7784
+ groupStateThreadRef.current = config?.threadId;
7785
+ groupStateRef.current = /* @__PURE__ */ new Map();
7786
+ groupSettersRef.current = /* @__PURE__ */ new Map();
7787
+ }
7788
+ const groupSetter = (key) => {
7789
+ let setter = groupSettersRef.current.get(key);
7790
+ if (!setter) {
7791
+ setter = (next) => {
7792
+ groupStateRef.current.set(key, next);
7793
+ forceUpdate();
7794
+ };
7795
+ groupSettersRef.current.set(key, setter);
7796
+ }
7797
+ return setter;
7798
+ };
7799
+ (0, react.useLayoutEffect)(() => {
7800
+ const live = /* @__PURE__ */ new Set();
7801
+ for (const row of rows) if (row.type === "group") live.add(stableGroupKey(row.key));
7802
+ for (const store of [groupStateRef.current, groupSettersRef.current]) for (const key of store.keys()) if (!live.has(key)) store.delete(key);
7803
+ }, [rows, rowRenderKeys]);
7742
7804
  if (process.env.NODE_ENV === "development" && deduplicatedMessages.length < messages.length) console.warn(`CopilotChatMessageView: Merged ${messages.length - deduplicatedMessages.length} message(s) with duplicate IDs.`);
7743
7805
  const { Component: AssistantComponent, slotProps: assistantSlotProps } = (0, react.useMemo)(() => resolveSlotComponent(assistantMessage, CopilotChatAssistantMessage_default), [assistantMessage]);
7744
7806
  const agentId = config?.agentId;
@@ -7778,7 +7840,7 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7778
7840
  (0, react.useEffect)(() => {
7779
7841
  if (process.env.NODE_ENV === "production" || !childrenDisabledVirtualization || warnedChildrenRef.current) return;
7780
7842
  warnedChildrenRef.current = true;
7781
- console.warn(`[CopilotKit] CopilotChatMessageView: the \`children\` render prop disables virtualization, so all ${renderedMessages.length} messages are mounted. ` + (transformMessages ? "`transformMessages` keeps virtualization on by itself; drop `children` to use it." : "To reshape the list and keep virtualization, use `transformMessages` instead."));
7843
+ console.warn(`[CopilotKit] CopilotChatMessageView: the \`children\` render prop disables virtualization, so all ${renderedMessages.length} messages are mounted. ` + (transformMessages || groupMessages ? "`transformMessages` and `groupMessages` keep virtualization on by themselves; drop `children` to use them." : "To reshape the list and keep virtualization, use `transformMessages` instead, or `groupMessages` to render several messages as one row."));
7782
7844
  }, [
7783
7845
  childrenDisabledVirtualization,
7784
7846
  renderedMessages.length,
@@ -7818,7 +7880,7 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7818
7880
  return item.start < (instance.scrollOffset ?? 0) + scrollAdjustments;
7819
7881
  }, []);
7820
7882
  const virtualizer = (0, _tanstack_react_virtual.useVirtualizer)({
7821
- count: shouldVirtualize ? renderedMessages.length : 0,
7883
+ count: shouldVirtualize ? rows.length : 0,
7822
7884
  getScrollElement: () => scrollElement,
7823
7885
  estimateSize: estimateRowSize,
7824
7886
  overscan: 5,
@@ -7831,8 +7893,8 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7831
7893
  isPinnedToBottomRef.current = isPinnedToBottom;
7832
7894
  virtualizer.shouldAdjustScrollPositionOnItemSizeChange = shouldAdjustScrollOnResize;
7833
7895
  (0, react.useLayoutEffect)(() => {
7834
- if (!shouldVirtualize || !renderedMessages.length) return;
7835
- virtualizer.scrollToIndex(renderedMessages.length - 1, { align: "end" });
7896
+ if (!shouldVirtualize || !rows.length) return;
7897
+ virtualizer.scrollToIndex(rows.length - 1, { align: "end" });
7836
7898
  }, [shouldVirtualize, firstMessageId]);
7837
7899
  const intelligenceTurnAnchors = (0, react.useMemo)(() => getIntelligenceTurnAnchors(deduplicatedMessages, renderedMessages), [deduplicatedMessages, renderedMessages]);
7838
7900
  const renderMessageBlock = (message) => {
@@ -7884,7 +7946,20 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7884
7946
  }, `intelligence-${intelligenceTurnId}`));
7885
7947
  return elements.filter(Boolean);
7886
7948
  };
7887
- const messageElements = shouldVirtualize ? [] : renderedMessages.flatMap(renderMessageBlock);
7949
+ const rowKey = (row) => row.type === "group" ? `${GROUP_ROW_KEY_PREFIX}${stableGroupKey(row.key)}` : rowRenderKeys.get(row.message.id) ?? row.message.id;
7950
+ const renderRow = (row) => {
7951
+ if (row.type === "message") return renderMessageBlock(row.message);
7952
+ const Wrapper = row.wrapper;
7953
+ const stateKey = stableGroupKey(row.key);
7954
+ return [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
7955
+ groupKey: row.key,
7956
+ messages: row.messages,
7957
+ state: groupStateRef.current.get(stateKey),
7958
+ setState: groupSetter(stateKey),
7959
+ children: row.messages.flatMap(renderMessageBlock)
7960
+ }, rowKey(row))];
7961
+ };
7962
+ const messageElements = shouldVirtualize ? [] : rows.flatMap(renderRow);
7888
7963
  if (children) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
7889
7964
  "data-copilotkit": true,
7890
7965
  style: { display: "contents" },
@@ -7909,7 +7984,7 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7909
7984
  position: "relative"
7910
7985
  },
7911
7986
  children: virtualizer.getVirtualItems().map((virtualItem) => {
7912
- const message = renderedMessages[virtualItem.index];
7987
+ const row = rows[virtualItem.index];
7913
7988
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
7914
7989
  "data-index": virtualItem.index,
7915
7990
  ref: virtualizer.measureElement,
@@ -7920,8 +7995,8 @@ function CopilotChatMessageView({ messages = [], assistantMessage, userMessage,
7920
7995
  width: "100%",
7921
7996
  transform: `translateY(${virtualItem.start}px)`
7922
7997
  },
7923
- children: renderMessageBlock(message)
7924
- }, rowRenderKeys.get(message.id) ?? message.id);
7998
+ children: renderRow(row)
7999
+ }, rowKey(row));
7925
8000
  })
7926
8001
  }) : messageElements,
7927
8002
  interruptElement,
@@ -8989,6 +9064,29 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
8989
9064
  const [lastConnectedThreadId, setLastConnectedThreadId] = (0, react.useState)(null);
8990
9065
  const isConnecting = hasExplicitThreadId && lastConnectedThreadId !== resolvedThreadId;
8991
9066
  const activeConnectCountRef = (0, react.useRef)(0);
9067
+ const pendingConnectionsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
9068
+ const failedConnectionsRef = (0, react.useRef)(/* @__PURE__ */ new WeakSet());
9069
+ const submissionQueueRef = (0, react.useRef)(Promise.resolve());
9070
+ const trackConnection = (0, react.useCallback)((connectingAgent, connect) => {
9071
+ const selection = previousThreadRef.current;
9072
+ const connectingThreadId = connectingAgent.threadId;
9073
+ let failed = false;
9074
+ const subscription = copilotkit.subscribe({ onError: (event) => {
9075
+ if (selection?.active && previousThreadRef.current === selection && event.context?.agentId === connectingAgent.agentId && (!event.context.threadId || event.context.threadId === connectingThreadId) && (event.code === _copilotkit_core.CopilotKitCoreErrorCode.AGENT_CONNECT_FAILED || event.code === _copilotkit_core.CopilotKitCoreErrorCode.AGENT_RUN_FAILED_EVENT)) failed = true;
9076
+ } });
9077
+ const pending = connect().catch((error) => {
9078
+ failed = true;
9079
+ console.error("CopilotChat: connection failed", error);
9080
+ }).finally(() => {
9081
+ subscription.unsubscribe();
9082
+ if (pendingConnectionsRef.current.get(connectingAgent) === pending) {
9083
+ pendingConnectionsRef.current.delete(connectingAgent);
9084
+ if (selection?.active && previousThreadRef.current === selection) if (failed) failedConnectionsRef.current.add(connectingAgent);
9085
+ else failedConnectionsRef.current.delete(connectingAgent);
9086
+ }
9087
+ });
9088
+ pendingConnectionsRef.current.set(connectingAgent, pending);
9089
+ }, [copilotkit]);
8992
9090
  const pendingRunActivityReconnectRef = (0, react.useRef)(false);
8993
9091
  const runActivityReconnectGenerationRef = (0, react.useRef)(0);
8994
9092
  const activeLocalRunIdsRef = (0, react.useRef)(/* @__PURE__ */ new Set());
@@ -9048,9 +9146,12 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9048
9146
  const selection = {
9049
9147
  threadId: resolvedThreadId,
9050
9148
  inspectorRequestId,
9051
- agent
9149
+ agent,
9150
+ active: true
9052
9151
  };
9053
9152
  previousThreadRef.current = selection;
9153
+ submissionQueueRef.current = Promise.resolve();
9154
+ failedConnectionsRef.current.delete(agent);
9054
9155
  const discardedThreadId = previousThread?.agent === agent ? previousThread.threadId : agent.threadId;
9055
9156
  if (inspectorTransition) if (typeof copilotkit.stopAgent === "function") copilotkit.stopAgent({ agent });
9056
9157
  else agent.abortRun?.();
@@ -9088,6 +9189,7 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9088
9189
  }
9089
9190
  return () => {
9090
9191
  active = false;
9192
+ selection.active = false;
9091
9193
  };
9092
9194
  }
9093
9195
  let detached = false;
@@ -9101,8 +9203,8 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9101
9203
  await copilotkit.connectAgent({ agent: agentToConnect });
9102
9204
  } catch (error) {
9103
9205
  if (detached) return;
9104
- console.error("CopilotChat: connectAgent failed", error);
9105
9206
  if (inspectorRequestId) failInspectorOverride(inspectorRequestId);
9207
+ throw error;
9106
9208
  } finally {
9107
9209
  if (!detached) (typeof requestAnimationFrame === "function" ? requestAnimationFrame : (cb) => setTimeout(cb, 16))(() => {
9108
9210
  if (!detached) setLastConnectedThreadId(resolvedThreadId);
@@ -9118,8 +9220,9 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9118
9220
  }
9119
9221
  }
9120
9222
  };
9121
- connect(agent);
9223
+ trackConnection(agent, () => connect(agent));
9122
9224
  return () => {
9225
+ selection.active = false;
9123
9226
  detached = true;
9124
9227
  connectAbortController.abort();
9125
9228
  const detach = agent.detachActiveRun().catch(() => {});
@@ -9182,7 +9285,7 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9182
9285
  await copilotkit.connectAgent({ agent });
9183
9286
  didConnect = true;
9184
9287
  } catch (error) {
9185
- if (!detached) console.error("CopilotChat: run activity reconnect failed", error);
9288
+ if (!detached) throw error;
9186
9289
  } finally {
9187
9290
  if (wakeRunId) {
9188
9291
  activeWakeRunIdsRef.current.delete(wakeRunId);
@@ -9192,7 +9295,7 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9192
9295
  wakeReconnectActive = false;
9193
9296
  if (!detached && runActivityReconnectGenerationRef.current === generation && activeConnectCountRef.current === 0 && pendingRunActivityReconnectRef.current) {
9194
9297
  pendingRunActivityReconnectRef.current = false;
9195
- connect();
9298
+ trackConnection(agent, connect);
9196
9299
  }
9197
9300
  }
9198
9301
  };
@@ -9208,7 +9311,7 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9208
9311
  return;
9209
9312
  }
9210
9313
  pendingRunActivityReconnectRef.current = false;
9211
- connect();
9314
+ trackConnection(agent, connect);
9212
9315
  };
9213
9316
  const subscription = threadStore.subscribeToRunActivity((notification) => {
9214
9317
  if (notification.threadId !== resolvedThreadId) return;
@@ -9251,6 +9354,18 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9251
9354
  rememberRecentlyWakeRunId
9252
9355
  ]);
9253
9356
  const waitForActiveRunToSettle = (0, react.useCallback)(async () => {
9357
+ const selection = previousThreadRef.current;
9358
+ const isCurrentSelection = () => selection?.active && previousThreadRef.current === selection;
9359
+ let connection = pendingConnectionsRef.current.get(agent);
9360
+ while (connection && isCurrentSelection()) {
9361
+ await connection;
9362
+ connection = pendingConnectionsRef.current.get(agent);
9363
+ }
9364
+ if (!isCurrentSelection()) return false;
9365
+ if (failedConnectionsRef.current.has(agent)) {
9366
+ setTranscriptionError("Could not load saved conversation. Reopen the thread and try again.");
9367
+ return "failed";
9368
+ }
9254
9369
  const maybeAware = agent;
9255
9370
  const activeRunCompletionPromise = (0, _copilotkit_core.isRunCompletionAware)(maybeAware) ? maybeAware.activeRunCompletionPromise : void 0;
9256
9371
  if (agent.isRunning && activeRunCompletionPromise) try {
@@ -9258,7 +9373,18 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9258
9373
  } catch (error) {
9259
9374
  console.error("CopilotChat: in-flight run rejected while queuing send", error);
9260
9375
  }
9376
+ return isCurrentSelection();
9261
9377
  }, [agent]);
9378
+ const enqueueSubmission = (0, react.useCallback)((submit) => {
9379
+ const selection = previousThreadRef.current;
9380
+ const pending = submissionQueueRef.current.then(async () => {
9381
+ if (selection?.active && previousThreadRef.current === selection) await submit();
9382
+ }).catch((error) => {
9383
+ console.error("CopilotChat: queued submission failed", error);
9384
+ });
9385
+ submissionQueueRef.current = pending;
9386
+ return pending;
9387
+ }, []);
9262
9388
  const onSubmitInput = (0, react.useCallback)(async (value) => {
9263
9389
  if (selectedAttachmentsRef.current.some((a) => a.status === "uploading")) {
9264
9390
  console.error("[CopilotKit] Cannot send while attachments are uploading (pre-await guard)");
@@ -9266,92 +9392,102 @@ function CopilotChat({ agentId, threadId, labels, inspectorTools, chatView, isMo
9266
9392
  return;
9267
9393
  }
9268
9394
  setInputValue("");
9269
- await waitForActiveRunToSettle();
9270
- if (selectedAttachmentsRef.current.some((a) => a.status === "uploading")) {
9271
- console.error("[CopilotKit] Cannot send while attachments are uploading (post-await re-check)");
9272
- setTranscriptionError("Cannot send while attachments are uploading.");
9273
- setInputValue(value);
9274
- return;
9275
- }
9276
- const readyAttachments = consumeAttachments();
9277
- if (readyAttachments.length > 0) {
9278
- const contentParts = [];
9279
- if (value.trim()) contentParts.push({
9280
- type: "text",
9281
- text: value
9282
- });
9283
- for (const att of readyAttachments) contentParts.push((0, _copilotkit_shared.createAttachmentContent)(att));
9284
- agent.addMessage({
9395
+ await enqueueSubmission(async () => {
9396
+ const ready = await waitForActiveRunToSettle();
9397
+ if (ready !== true) {
9398
+ if (ready === "failed") setInputValue(value);
9399
+ return;
9400
+ }
9401
+ if (selectedAttachmentsRef.current.some((a) => a.status === "uploading")) {
9402
+ console.error("[CopilotKit] Cannot send while attachments are uploading (post-await re-check)");
9403
+ setTranscriptionError("Cannot send while attachments are uploading.");
9404
+ setInputValue(value);
9405
+ return;
9406
+ }
9407
+ const readyAttachments = consumeAttachments();
9408
+ if (readyAttachments.length > 0) {
9409
+ const contentParts = [];
9410
+ if (value.trim()) contentParts.push({
9411
+ type: "text",
9412
+ text: value
9413
+ });
9414
+ for (const att of readyAttachments) contentParts.push((0, _copilotkit_shared.createAttachmentContent)(att));
9415
+ agent.addMessage({
9416
+ id: (0, _copilotkit_shared.randomUUID)(),
9417
+ role: "user",
9418
+ content: contentParts
9419
+ });
9420
+ } else agent.addMessage({
9285
9421
  id: (0, _copilotkit_shared.randomUUID)(),
9286
9422
  role: "user",
9287
- content: contentParts
9288
- });
9289
- } else agent.addMessage({
9290
- id: (0, _copilotkit_shared.randomUUID)(),
9291
- role: "user",
9292
- content: value
9293
- });
9294
- const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
9295
- if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
9296
- try {
9297
- await copilotkit.runAgent({
9298
- agent,
9299
- ...localRunId !== void 0 ? { runId: localRunId } : {}
9423
+ content: value
9300
9424
  });
9301
- } catch (error) {
9302
- console.error("CopilotChat: runAgent failed", error);
9303
- } finally {
9304
- if (localRunId) {
9305
- activeLocalRunIdsRef.current.delete(localRunId);
9306
- rememberRecentlyLocalRunId(localRunId);
9307
- }
9308
- if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
9309
- const startReconnect = startRunActivityReconnectRef.current;
9310
- if (startReconnect) {
9311
- pendingRunActivityReconnectRef.current = false;
9312
- startReconnect(runActivityReconnectGenerationRef.current);
9425
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
9426
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
9427
+ try {
9428
+ await copilotkit.runAgent({
9429
+ agent,
9430
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
9431
+ });
9432
+ } catch (error) {
9433
+ console.error("CopilotChat: runAgent failed", error);
9434
+ } finally {
9435
+ if (localRunId) {
9436
+ activeLocalRunIdsRef.current.delete(localRunId);
9437
+ rememberRecentlyLocalRunId(localRunId);
9438
+ }
9439
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
9440
+ const startReconnect = startRunActivityReconnectRef.current;
9441
+ if (startReconnect) {
9442
+ pendingRunActivityReconnectRef.current = false;
9443
+ startReconnect(runActivityReconnectGenerationRef.current);
9444
+ }
9313
9445
  }
9314
9446
  }
9315
- }
9447
+ });
9316
9448
  }, [
9317
9449
  agent,
9318
9450
  consumeAttachments,
9319
9451
  waitForActiveRunToSettle,
9452
+ enqueueSubmission,
9320
9453
  hasNativeIntelligenceRunActivity,
9321
9454
  rememberRecentlyLocalRunId
9322
9455
  ]);
9323
9456
  const handleSelectSuggestion = (0, react.useCallback)(async (suggestion) => {
9324
- await waitForActiveRunToSettle();
9325
- agent.addMessage({
9326
- id: (0, _copilotkit_shared.randomUUID)(),
9327
- role: "user",
9328
- content: suggestion.message
9329
- });
9330
- const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
9331
- if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
9332
- try {
9333
- await copilotkit.runAgent({
9334
- agent,
9335
- ...localRunId !== void 0 ? { runId: localRunId } : {}
9457
+ await enqueueSubmission(async () => {
9458
+ if (await waitForActiveRunToSettle() !== true) return;
9459
+ agent.addMessage({
9460
+ id: (0, _copilotkit_shared.randomUUID)(),
9461
+ role: "user",
9462
+ content: suggestion.message
9336
9463
  });
9337
- } catch (error) {
9338
- console.error("CopilotChat: runAgent failed after selecting suggestion", error);
9339
- } finally {
9340
- if (localRunId) {
9341
- activeLocalRunIdsRef.current.delete(localRunId);
9342
- rememberRecentlyLocalRunId(localRunId);
9343
- }
9344
- if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
9345
- const startReconnect = startRunActivityReconnectRef.current;
9346
- if (startReconnect) {
9347
- pendingRunActivityReconnectRef.current = false;
9348
- startReconnect(runActivityReconnectGenerationRef.current);
9464
+ const localRunId = hasNativeIntelligenceRunActivity ? (0, _copilotkit_shared.randomUUID)() : void 0;
9465
+ if (localRunId) activeLocalRunIdsRef.current.add(localRunId);
9466
+ try {
9467
+ await copilotkit.runAgent({
9468
+ agent,
9469
+ ...localRunId !== void 0 ? { runId: localRunId } : {}
9470
+ });
9471
+ } catch (error) {
9472
+ console.error("CopilotChat: runAgent failed after selecting suggestion", error);
9473
+ } finally {
9474
+ if (localRunId) {
9475
+ activeLocalRunIdsRef.current.delete(localRunId);
9476
+ rememberRecentlyLocalRunId(localRunId);
9477
+ }
9478
+ if (pendingRunActivityReconnectRef.current && activeLocalRunIdsRef.current.size === 0 && activeConnectCountRef.current === 0) {
9479
+ const startReconnect = startRunActivityReconnectRef.current;
9480
+ if (startReconnect) {
9481
+ pendingRunActivityReconnectRef.current = false;
9482
+ startReconnect(runActivityReconnectGenerationRef.current);
9483
+ }
9349
9484
  }
9350
9485
  }
9351
- }
9486
+ });
9352
9487
  }, [
9353
9488
  agent,
9354
9489
  waitForActiveRunToSettle,
9490
+ enqueueSubmission,
9355
9491
  hasNativeIntelligenceRunActivity,
9356
9492
  rememberRecentlyLocalRunId
9357
9493
  ]);
@@ -13020,6 +13156,18 @@ Object.defineProperty(exports, 'getIntelligenceTurnAnchors', {
13020
13156
  return getIntelligenceTurnAnchors;
13021
13157
  }
13022
13158
  });
13159
+ Object.defineProperty(exports, 'messageGroup', {
13160
+ enumerable: true,
13161
+ get: function () {
13162
+ return messageGroup;
13163
+ }
13164
+ });
13165
+ Object.defineProperty(exports, 'messageRow', {
13166
+ enumerable: true,
13167
+ get: function () {
13168
+ return messageRow;
13169
+ }
13170
+ });
13023
13171
  Object.defineProperty(exports, 'shouldShowDevConsole', {
13024
13172
  enumerable: true,
13025
13173
  get: function () {
@@ -13206,4 +13354,4 @@ Object.defineProperty(exports, 'useToast', {
13206
13354
  return useToast;
13207
13355
  }
13208
13356
  });
13209
- //# sourceMappingURL=copilotkit-CFPkj9B9.cjs.map
13357
+ //# sourceMappingURL=copilotkit-BG1d4-qU.cjs.map