@copilotkit/react-core 2.0.0-rc.27 → 2.0.0-rc.29

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/index.umd.js CHANGED
@@ -1358,7 +1358,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
1358
1358
 
1359
1359
  //#endregion
1360
1360
  //#region package.json
1361
- var version$1 = "2.0.0-rc.27";
1361
+ var version$1 = "2.0.0-rc.29";
1362
1362
 
1363
1363
  //#endregion
1364
1364
  //#region src/components/CopilotKitInspector.tsx
@@ -10530,6 +10530,11 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10530
10530
  (0, _copilotkit_shared.commitRowKeyStore)(rowKeyStore, renderedMessages);
10531
10531
  }, [rowKeyStore, renderedMessages]);
10532
10532
  const stableGroupKey = (key) => rowRenderKeys.get(key) ?? key;
10533
+ const rowKey = (row) => row.type === "group" ? `${GROUP_ROW_KEY_PREFIX}${stableGroupKey(row.key)}` : rowRenderKeys.get(row.message.id) ?? row.message.id;
10534
+ const rowKeysRef = (0, react.useRef)([]);
10535
+ const nextRowKeys = rows.map(rowKey);
10536
+ if (nextRowKeys.length !== rowKeysRef.current.length || nextRowKeys.some((key, index) => key !== rowKeysRef.current[index])) rowKeysRef.current = nextRowKeys;
10537
+ const rowKeys = rowKeysRef.current;
10533
10538
  const groupStateRef = (0, react.useRef)(/* @__PURE__ */ new Map());
10534
10539
  const groupSettersRef = (0, react.useRef)(/* @__PURE__ */ new Map());
10535
10540
  const groupStateThreadRef = (0, react.useRef)(config?.threadId);
@@ -10620,21 +10625,47 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10620
10625
  });
10621
10626
  const resizeAnchorRef = react.default.useRef(null);
10622
10627
  const restoringVisibilityRef = react.default.useRef(false);
10623
- const firstMessageId = deduplicatedMessages[0]?.id;
10624
- const measuredThreadRef = react.default.useRef(firstMessageId);
10625
- if (measuredThreadRef.current !== firstMessageId) {
10626
- measuredThreadRef.current = firstMessageId;
10627
- resizeAnchorRef.current = null;
10628
- measuredRef.current = {
10629
- total: 0,
10630
- sizes: /* @__PURE__ */ new Map()
10631
- };
10628
+ const isPinnedToBottomRef = react.default.useRef(isPinnedToBottom);
10629
+ const virtualizerRef = react.default.useRef(null);
10630
+ const history = react.default.useRef({
10631
+ messages: deduplicatedMessages,
10632
+ threadId,
10633
+ generation: 0
10634
+ }).current;
10635
+ if (history.messages !== deduplicatedMessages) {
10636
+ if (history.messages[0]?.id !== deduplicatedMessages[0]?.id) {
10637
+ const previousIds = new Set(history.messages.map((m) => m.id));
10638
+ if (history.threadId === threadId && deduplicatedMessages.some((m) => previousIds.has(m.id))) keepReadingRowThroughHistoryChange();
10639
+ else {
10640
+ history.generation += 1;
10641
+ resizeAnchorRef.current = null;
10642
+ measuredRef.current = {
10643
+ total: 0,
10644
+ sizes: /* @__PURE__ */ new Map()
10645
+ };
10646
+ }
10647
+ }
10648
+ history.messages = deduplicatedMessages;
10649
+ history.threadId = threadId;
10650
+ }
10651
+ const historyGeneration = history.generation;
10652
+ function keepReadingRowThroughHistoryChange() {
10653
+ const instance = virtualizerRef.current;
10654
+ const element = instance?.scrollElement;
10655
+ if (!instance || !element || instance.options.count === 0 || element.clientHeight === 0 || isPinnedToBottomRef.current || restoringVisibilityRef.current) return;
10656
+ const offset = element.scrollTop;
10657
+ const item = instance.getVirtualItemForOffset(offset);
10658
+ const index = item ? rowKeys.indexOf(String(item.key)) : -1;
10659
+ resizeAnchorRef.current = item && index >= 0 ? {
10660
+ index,
10661
+ offset: offset - item.start,
10662
+ ready: true
10663
+ } : null;
10632
10664
  }
10633
10665
  const estimateRowSize = react.default.useCallback(() => {
10634
10666
  const { total, sizes } = measuredRef.current;
10635
10667
  return sizes.size > 0 ? Math.max(1, Math.round(total / sizes.size)) : 100;
10636
10668
  }, []);
10637
- const isPinnedToBottomRef = react.default.useRef(isPinnedToBottom);
10638
10669
  const measuredWidthRef = react.default.useRef(null);
10639
10670
  const captureResizeAnchor = react.default.useCallback((instance) => {
10640
10671
  const width = instance.scrollElement?.clientWidth;
@@ -10658,12 +10689,13 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10658
10689
  captureResizeAnchor(instance);
10659
10690
  const height = el?.getBoundingClientRect().height ?? 0;
10660
10691
  const index = Number(el?.dataset?.index);
10661
- if (height > 0 && Number.isInteger(index)) {
10692
+ const key = Number.isInteger(index) ? instance.options.getItemKey(index) : void 0;
10693
+ if (height > 0 && key !== void 0) {
10662
10694
  const { total, sizes } = measuredRef.current;
10663
- measuredRef.current.total = total - (sizes.get(index) ?? 0) + height;
10664
- sizes.set(index, height);
10695
+ measuredRef.current.total = total - (sizes.get(key) ?? 0) + height;
10696
+ sizes.set(key, height);
10665
10697
  }
10666
- return height > 0 ? height : measuredRef.current.sizes.get(index) ?? 100;
10698
+ return height > 0 ? height : key !== void 0 && measuredRef.current.sizes.get(key) || 100;
10667
10699
  }, [captureResizeAnchor]);
10668
10700
  const shouldAdjustScrollOnResize = react.default.useCallback((item, _delta, instance) => {
10669
10701
  if (isPinnedToBottomRef.current || resizeAnchorRef.current || restoringVisibilityRef.current) return false;
@@ -10724,8 +10756,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10724
10756
  resizeAnchorRef.current = null;
10725
10757
  };
10726
10758
  }, [captureResizeAnchor, measureRowElement]);
10759
+ const getItemKey = react.default.useCallback((index) => rowKeys[index] ?? index, [rowKeys]);
10727
10760
  const virtualizer = (0, _tanstack_react_virtual.useVirtualizer)({
10728
10761
  count: shouldVirtualize ? rows.length : 0,
10762
+ getItemKey,
10729
10763
  getScrollElement: () => shouldVirtualize ? scrollElement : null,
10730
10764
  observeElementRect: observeScrollRect,
10731
10765
  estimateSize: estimateRowSize,
@@ -10736,6 +10770,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10736
10770
  height: 600
10737
10771
  }
10738
10772
  });
10773
+ virtualizerRef.current = virtualizer;
10739
10774
  isPinnedToBottomRef.current = isPinnedToBottom;
10740
10775
  virtualizer.shouldAdjustScrollPositionOnItemSizeChange = shouldAdjustScrollOnResize;
10741
10776
  (0, react.useLayoutEffect)(() => {
@@ -10751,18 +10786,18 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10751
10786
  });
10752
10787
  return () => cancelAnimationFrame(frame);
10753
10788
  });
10754
- const lastEndAlignedFirstMessageRef = react.default.useRef(void 0);
10789
+ const lastEndAlignedGenerationRef = react.default.useRef(void 0);
10755
10790
  (0, react.useLayoutEffect)(() => {
10756
10791
  if (!shouldVirtualize || !rows.length) return;
10757
- if (lastEndAlignedFirstMessageRef.current === firstMessageId) return;
10758
- lastEndAlignedFirstMessageRef.current = firstMessageId;
10792
+ if (lastEndAlignedGenerationRef.current === historyGeneration) return;
10793
+ lastEndAlignedGenerationRef.current = historyGeneration;
10759
10794
  virtualizer.scrollToIndex(rows.length - 1, { align: "end" });
10760
- }, [shouldVirtualize, firstMessageId]);
10795
+ }, [shouldVirtualize, historyGeneration]);
10761
10796
  const intelligenceTurnAnchors = (0, react.useMemo)(() => getIntelligenceTurnAnchors(deduplicatedMessages, renderedMessages), [deduplicatedMessages, renderedMessages]);
10762
10797
  const renderMessageBlock = (message) => {
10763
10798
  const elements = [];
10764
10799
  const stateSnapshot = renderCustomMessage ? getStateSnapshotForMessage(message.id) : void 0;
10765
- const rowKey = rowRenderKeys.get(message.id) ?? message.id;
10800
+ const messageKey = rowRenderKeys.get(message.id) ?? message.id;
10766
10801
  const turn = turnByFirstMessage.get(message.id);
10767
10802
  if (turn) elements.push(renderWorkSummary(turn.key, turn.calls));
10768
10803
  if (renderCustomMessage) elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedCustomMessage, {
@@ -10770,7 +10805,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10770
10805
  position: "before",
10771
10806
  renderCustomMessage,
10772
10807
  stateSnapshot
10773
- }, `${rowKey}-custom-before`));
10808
+ }, `${messageKey}-custom-before`));
10774
10809
  if (message.role === "assistant") elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedAssistantMessage, {
10775
10810
  message,
10776
10811
  messages,
@@ -10778,16 +10813,16 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10778
10813
  isLatest: message.id === latestRenderedId,
10779
10814
  AssistantMessageComponent: AssistantComponent,
10780
10815
  slotProps: assistantSlotPropsWithFeedback
10781
- }, rowKey));
10816
+ }, messageKey));
10782
10817
  else if (message.role === "user") elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedUserMessage, {
10783
10818
  message,
10784
10819
  UserMessageComponent: UserComponent,
10785
10820
  slotProps: userSlotProps
10786
- }, rowKey));
10821
+ }, messageKey));
10787
10822
  else if (message.role === "activity") elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedActivityMessage, {
10788
10823
  message,
10789
10824
  renderActivityMessage
10790
- }, rowKey));
10825
+ }, messageKey));
10791
10826
  else if (message.role === "reasoning") {
10792
10827
  const reasoning = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedReasoningMessage, {
10793
10828
  message,
@@ -10796,18 +10831,18 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10796
10831
  isLatest: message.id === latestRenderedId,
10797
10832
  ReasoningMessageComponent: ReasoningComponent,
10798
10833
  slotProps: reasoningSlotProps
10799
- }, rowKey);
10834
+ }, messageKey);
10800
10835
  elements.push(toolCallDisplay === "turn" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Collapsible, {
10801
10836
  open: workOpen.get(message.id) ?? true,
10802
10837
  children: reasoning
10803
- }, rowKey) : reasoning);
10838
+ }, messageKey) : reasoning);
10804
10839
  }
10805
10840
  if (renderCustomMessage) elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedCustomMessage, {
10806
10841
  message,
10807
10842
  position: "after",
10808
10843
  renderCustomMessage,
10809
10844
  stateSnapshot
10810
- }, `${rowKey}-custom-after`));
10845
+ }, `${messageKey}-custom-after`));
10811
10846
  const intelligenceTurnId = showIntelligenceIndicator ? intelligenceTurnAnchors.get(message.id) : void 0;
10812
10847
  if (intelligenceTurnId !== void 0) elements.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(IntelligenceIndicator, {
10813
10848
  message,
@@ -10819,7 +10854,6 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10819
10854
  }, `intelligence-${intelligenceTurnId}`));
10820
10855
  return elements.filter(Boolean);
10821
10856
  };
10822
- const rowKey = (row) => row.type === "group" ? `${GROUP_ROW_KEY_PREFIX}${stableGroupKey(row.key)}` : rowRenderKeys.get(row.message.id) ?? row.message.id;
10823
10857
  const renderRow = (row) => {
10824
10858
  if (row.type === "message") return renderMessageBlock(row.message);
10825
10859
  const Wrapper = row.wrapper;
@@ -10832,8 +10866,6 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10832
10866
  children: row.messages.flatMap(renderMessageBlock)
10833
10867
  }, rowKey(row))];
10834
10868
  };
10835
- const rowKeysRef = (0, react.useRef)([]);
10836
- rowKeysRef.current = rows.map(rowKey);
10837
10869
  const virtualModeRef = (0, react.useRef)(shouldVirtualize);
10838
10870
  virtualModeRef.current = shouldVirtualize;
10839
10871
  (0, react.useEffect)(() => {
@@ -10935,7 +10967,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
10935
10967
  }, [
10936
10968
  scrollElementFromCtx,
10937
10969
  virtualizer,
10938
- firstMessageId
10970
+ historyGeneration
10939
10971
  ]);
10940
10972
  const messageElements = shouldVirtualize ? [] : rows.flatMap(renderRow);
10941
10973
  if (children) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MessageFeedbackScopeContext.Provider, {