@paymanai/payman-ask-sdk 1.2.12 → 1.2.13

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.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useChat, useVoice } from '@paymanai/payman-typescript-ask-sdk';
2
2
  export { cancelUserAction, generateId, resendUserAction, streamWorkflowEvents, submitUserAction, useChat, useVoice } from '@paymanai/payman-typescript-ask-sdk';
3
- import { createContext, useContext, useState, useRef, useMemo, useEffect, useCallback } from 'react';
3
+ import { createContext, useContext, useState, useRef, useMemo, useEffect, useCallback, useLayoutEffect } from 'react';
4
4
  import { clsx } from 'clsx';
5
5
  import { twMerge } from 'tailwind-merge';
6
6
  import { motion, AnimatePresence } from 'framer-motion';
@@ -765,6 +765,7 @@ function MessageRowSkeleton({
765
765
  );
766
766
  }
767
767
  var SCROLL_THRESHOLD = 150;
768
+ var LOAD_MORE_THRESHOLD = 80;
768
769
  function MessageList({
769
770
  messages,
770
771
  isLoading = false,
@@ -784,12 +785,17 @@ function MessageList({
784
785
  streamingStepsText,
785
786
  completedStepsText,
786
787
  onExecutionTraceClick,
787
- className
788
+ className,
789
+ onLoadMoreMessages,
790
+ isLoadingMoreMessages = false,
791
+ hasMoreMessages = false
788
792
  }) {
789
793
  const scrollRef = useRef(null);
790
794
  const isNearBottomRef = useRef(true);
791
795
  const [showScrollBtn, setShowScrollBtn] = useState(false);
792
796
  const prevMessageCountRef = useRef(messages.length);
797
+ const scrollHeightBeforePrependRef = useRef(null);
798
+ const firstMessageIdRef = useRef(messages[0]?.id);
793
799
  const getDistanceFromBottom = useCallback(() => {
794
800
  const el = scrollRef.current;
795
801
  if (!el) return 0;
@@ -801,11 +807,27 @@ function MessageList({
801
807
  el.scrollTo({ top: el.scrollHeight, behavior });
802
808
  }, []);
803
809
  const handleScroll = useCallback(() => {
810
+ const el = scrollRef.current;
811
+ if (!el) return;
804
812
  const distance = getDistanceFromBottom();
805
813
  const nearBottom = distance <= SCROLL_THRESHOLD;
806
814
  isNearBottomRef.current = nearBottom;
807
815
  setShowScrollBtn(!nearBottom);
808
- }, [getDistanceFromBottom]);
816
+ if (el.scrollTop <= LOAD_MORE_THRESHOLD && hasMoreMessages && !isLoadingMoreMessages && onLoadMoreMessages) {
817
+ scrollHeightBeforePrependRef.current = el.scrollHeight;
818
+ onLoadMoreMessages();
819
+ }
820
+ }, [getDistanceFromBottom, hasMoreMessages, isLoadingMoreMessages, onLoadMoreMessages]);
821
+ useLayoutEffect(() => {
822
+ const el = scrollRef.current;
823
+ const prevHeight = scrollHeightBeforePrependRef.current;
824
+ const newFirstId = messages[0]?.id;
825
+ if (el && prevHeight !== null && newFirstId !== firstMessageIdRef.current) {
826
+ el.scrollTop = el.scrollHeight - prevHeight;
827
+ scrollHeightBeforePrependRef.current = null;
828
+ }
829
+ firstMessageIdRef.current = newFirstId;
830
+ }, [messages]);
809
831
  useEffect(() => {
810
832
  const prevCount = prevMessageCountRef.current;
811
833
  prevMessageCountRef.current = messages.length;
@@ -916,34 +938,37 @@ function MessageList({
916
938
  className
917
939
  ),
918
940
  children: [
919
- /* @__PURE__ */ jsx(
941
+ /* @__PURE__ */ jsxs(
920
942
  "div",
921
943
  {
922
944
  className: cn(
923
945
  "space-y-4",
924
946
  layout === "centered" ? "max-w-2xl mx-auto px-4 py-6" : "p-4"
925
947
  ),
926
- children: messages.map((message) => /* @__PURE__ */ jsx(
927
- MessageRow,
928
- {
929
- message,
930
- stage,
931
- animated,
932
- showAgentName,
933
- agentName,
934
- showAvatars,
935
- showUserAvatar,
936
- showAssistantAvatar,
937
- layout,
938
- showTimestamps,
939
- showExecutionSteps,
940
- showStreamingDot,
941
- streamingStepsText,
942
- completedStepsText,
943
- onExecutionTraceClick
944
- },
945
- message.id
946
- ))
948
+ children: [
949
+ isLoadingMoreMessages && /* @__PURE__ */ jsx("div", { className: "flex justify-center py-2", children: /* @__PURE__ */ jsx("div", { className: "w-5 h-5 rounded-full border-2 border-muted-foreground/30 border-t-muted-foreground animate-spin" }) }),
950
+ messages.map((message) => /* @__PURE__ */ jsx(
951
+ MessageRow,
952
+ {
953
+ message,
954
+ stage,
955
+ animated,
956
+ showAgentName,
957
+ agentName,
958
+ showAvatars,
959
+ showUserAvatar,
960
+ showAssistantAvatar,
961
+ layout,
962
+ showTimestamps,
963
+ showExecutionSteps,
964
+ showStreamingDot,
965
+ streamingStepsText,
966
+ completedStepsText,
967
+ onExecutionTraceClick
968
+ },
969
+ message.id
970
+ ))
971
+ ]
947
972
  }
948
973
  ),
949
974
  showScrollBtn && /* @__PURE__ */ jsx("div", { className: "sticky bottom-0 z-20 flex justify-center pb-3 -mt-11", children: /* @__PURE__ */ jsx(
@@ -1423,7 +1448,10 @@ function PaymanChat({
1423
1448
  callbacks = {},
1424
1449
  className,
1425
1450
  style,
1426
- children
1451
+ children,
1452
+ onLoadMoreMessages,
1453
+ isLoadingMoreMessages = false,
1454
+ hasMoreMessages = false
1427
1455
  }) {
1428
1456
  const [inputValue, setInputValue] = useState("");
1429
1457
  const prevInputValueRef = useRef(inputValue);
@@ -1434,6 +1462,7 @@ function PaymanChat({
1434
1462
  isWaitingForResponse,
1435
1463
  resetSession,
1436
1464
  clearMessages,
1465
+ prependMessages,
1437
1466
  cancelStream,
1438
1467
  getSessionId,
1439
1468
  getMessages
@@ -1470,6 +1499,7 @@ function PaymanChat({
1470
1499
  () => ({
1471
1500
  resetSession,
1472
1501
  clearMessages,
1502
+ prependMessages,
1473
1503
  cancelStream,
1474
1504
  getSessionId,
1475
1505
  getMessages,
@@ -1478,6 +1508,7 @@ function PaymanChat({
1478
1508
  [
1479
1509
  resetSession,
1480
1510
  clearMessages,
1511
+ prependMessages,
1481
1512
  cancelStream,
1482
1513
  getSessionId,
1483
1514
  getMessages,
@@ -1595,7 +1626,10 @@ function PaymanChat({
1595
1626
  showStreamingDot,
1596
1627
  streamingStepsText,
1597
1628
  completedStepsText,
1598
- onExecutionTraceClick
1629
+ onExecutionTraceClick,
1630
+ onLoadMoreMessages,
1631
+ isLoadingMoreMessages,
1632
+ hasMoreMessages
1599
1633
  }
1600
1634
  ),
1601
1635
  hasAskPermission && /* @__PURE__ */ jsx(