react-optimistic-chat 1.2.1 → 2.1.0
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/LICENSE +23 -0
- package/README.md +845 -0
- package/dist/index.d.mts +54 -45
- package/dist/index.d.ts +54 -45
- package/dist/index.js +173 -74
- package/dist/index.mjs +173 -74
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -54,14 +54,14 @@ __export(index_exports, {
|
|
|
54
54
|
LoadingSpinner: () => LoadingSpinner,
|
|
55
55
|
SendingDots: () => SendingDots,
|
|
56
56
|
useBrowserSpeechRecognition: () => useBrowserSpeechRecognition,
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
useChat: () => useChat,
|
|
58
|
+
useVoiceChat: () => useVoiceChat
|
|
59
59
|
});
|
|
60
60
|
module.exports = __toCommonJS(index_exports);
|
|
61
61
|
|
|
62
62
|
// src/components/indicators/LoadingSpinner.tsx
|
|
63
63
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
64
|
-
function LoadingSpinner({ size }) {
|
|
64
|
+
function LoadingSpinner({ size = "md" }) {
|
|
65
65
|
const sizeMap = {
|
|
66
66
|
xs: 24,
|
|
67
67
|
sm: 32,
|
|
@@ -531,20 +531,30 @@ function ChatContainer(props) {
|
|
|
531
531
|
disableVoice,
|
|
532
532
|
placeholder,
|
|
533
533
|
inputClassName,
|
|
534
|
+
fetchNextPage,
|
|
535
|
+
hasNextPage,
|
|
536
|
+
isFetchingNextPage,
|
|
534
537
|
className
|
|
535
538
|
} = props;
|
|
536
539
|
const mappedMessages = typeof props.messageMapper === "function" ? props.messages.map(props.messageMapper) : messages;
|
|
537
540
|
(0, import_react5.useEffect)(() => {
|
|
538
541
|
const el = scrollRef.current;
|
|
539
542
|
if (!el) return;
|
|
540
|
-
|
|
541
|
-
const handleScroll = () => {
|
|
543
|
+
const handleScroll = async () => {
|
|
542
544
|
const isBottom = el.scrollTop + el.clientHeight >= el.scrollHeight - 10;
|
|
543
545
|
setIsAtBottom(isBottom);
|
|
546
|
+
if (el.scrollTop === 0 && hasNextPage && !isFetchingNextPage && fetchNextPage) {
|
|
547
|
+
const prevScrollHeight = el.scrollHeight;
|
|
548
|
+
await fetchNextPage();
|
|
549
|
+
requestAnimationFrame(() => {
|
|
550
|
+
const newScrollHeight = el.scrollHeight;
|
|
551
|
+
el.scrollTop = newScrollHeight - prevScrollHeight;
|
|
552
|
+
});
|
|
553
|
+
}
|
|
544
554
|
};
|
|
545
555
|
el.addEventListener("scroll", handleScroll);
|
|
546
556
|
return () => el.removeEventListener("scroll", handleScroll);
|
|
547
|
-
}, []);
|
|
557
|
+
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);
|
|
548
558
|
(0, import_react5.useEffect)(() => {
|
|
549
559
|
const el = scrollRef.current;
|
|
550
560
|
if (!el) return;
|
|
@@ -572,17 +582,20 @@ function ChatContainer(props) {
|
|
|
572
582
|
flex flex-col ${className || ""}
|
|
573
583
|
`,
|
|
574
584
|
children: [
|
|
575
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.
|
|
585
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
|
|
576
586
|
"div",
|
|
577
587
|
{
|
|
578
588
|
ref: scrollRef,
|
|
579
589
|
className: `flex-1 overflow-y-auto chatContainer-scroll p-2`,
|
|
580
|
-
children:
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
590
|
+
children: [
|
|
591
|
+
hasNextPage && isFetchingNextPage && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "flex justify-center py-2", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(LoadingSpinner, { size: "sm" }) }),
|
|
592
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
593
|
+
ChatList,
|
|
594
|
+
__spreadValues(__spreadValues(__spreadValues({
|
|
595
|
+
messages: mappedMessages
|
|
596
|
+
}, messageRenderer && { messageRenderer }), loadingRenderer && { loadingRenderer }), listClassName && { className: listClassName })
|
|
597
|
+
)
|
|
598
|
+
]
|
|
586
599
|
}
|
|
587
600
|
),
|
|
588
601
|
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex-shrink-0 relative", children: [
|
|
@@ -625,12 +638,26 @@ function ChatContainer(props) {
|
|
|
625
638
|
) });
|
|
626
639
|
}
|
|
627
640
|
|
|
628
|
-
// src/hooks/
|
|
641
|
+
// src/hooks/useChat.ts
|
|
629
642
|
var import_react_query = require("@tanstack/react-query");
|
|
630
643
|
var import_react6 = require("react");
|
|
631
|
-
function
|
|
644
|
+
function splitRawToMessage(raw, mapped) {
|
|
645
|
+
const custom = {};
|
|
646
|
+
const mappedValues = new Set(Object.values(mapped));
|
|
647
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
648
|
+
if (!mappedValues.has(value)) {
|
|
649
|
+
custom[key] = value;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return __spreadProps(__spreadValues({}, mapped), {
|
|
653
|
+
custom
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
function useChat({
|
|
632
657
|
queryKey,
|
|
633
658
|
queryFn,
|
|
659
|
+
initialPageParam,
|
|
660
|
+
getNextPageParam,
|
|
634
661
|
mutationFn,
|
|
635
662
|
map,
|
|
636
663
|
onError,
|
|
@@ -640,17 +667,26 @@ function useOptimisticChat({
|
|
|
640
667
|
const [isPending, setIsPending] = (0, import_react6.useState)(false);
|
|
641
668
|
const queryClient = (0, import_react_query.useQueryClient)();
|
|
642
669
|
const {
|
|
643
|
-
data
|
|
644
|
-
isLoading: isInitialLoading
|
|
645
|
-
|
|
670
|
+
data,
|
|
671
|
+
isLoading: isInitialLoading,
|
|
672
|
+
fetchNextPage,
|
|
673
|
+
hasNextPage,
|
|
674
|
+
isFetchingNextPage
|
|
675
|
+
} = (0, import_react_query.useInfiniteQuery)({
|
|
646
676
|
queryKey,
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
677
|
+
initialPageParam,
|
|
678
|
+
queryFn: async ({ pageParam }) => {
|
|
679
|
+
const raw = await queryFn(pageParam);
|
|
680
|
+
return raw.map((r) => {
|
|
681
|
+
const mapped = map(r);
|
|
682
|
+
return splitRawToMessage(r, mapped);
|
|
683
|
+
});
|
|
650
684
|
},
|
|
685
|
+
getNextPageParam,
|
|
651
686
|
staleTime,
|
|
652
687
|
gcTime
|
|
653
688
|
});
|
|
689
|
+
const messages = data ? [...data.pages].reverse().flat() : [];
|
|
654
690
|
const mutation = (0, import_react_query.useMutation)({
|
|
655
691
|
mutationFn,
|
|
656
692
|
// (content: string) => Promise<TMutationRaw>
|
|
@@ -661,38 +697,47 @@ function useOptimisticChat({
|
|
|
661
697
|
await queryClient.cancelQueries({ queryKey });
|
|
662
698
|
}
|
|
663
699
|
queryClient.setQueryData(queryKey, (old) => {
|
|
664
|
-
|
|
665
|
-
return
|
|
666
|
-
|
|
667
|
-
|
|
700
|
+
var _a;
|
|
701
|
+
if (!old) return old;
|
|
702
|
+
const pages = [...old.pages];
|
|
703
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
704
|
+
pages[0] = [
|
|
705
|
+
...firstPage,
|
|
668
706
|
{
|
|
669
707
|
id: crypto.randomUUID(),
|
|
670
708
|
role: "USER",
|
|
671
|
-
content
|
|
709
|
+
content,
|
|
710
|
+
custom: {}
|
|
672
711
|
},
|
|
673
|
-
// AI placeholder 추가
|
|
674
712
|
{
|
|
675
713
|
id: crypto.randomUUID(),
|
|
676
714
|
role: "AI",
|
|
677
715
|
content: "",
|
|
678
|
-
isLoading: true
|
|
716
|
+
isLoading: true,
|
|
717
|
+
custom: {}
|
|
679
718
|
}
|
|
680
719
|
];
|
|
720
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
721
|
+
pages
|
|
722
|
+
});
|
|
681
723
|
});
|
|
682
724
|
return prev ? { prev } : {};
|
|
683
725
|
},
|
|
684
726
|
onSuccess: (rawAiResponse) => {
|
|
685
|
-
const
|
|
727
|
+
const mapped = map(rawAiResponse);
|
|
728
|
+
const aiMessage = splitRawToMessage(rawAiResponse, mapped);
|
|
686
729
|
queryClient.setQueryData(queryKey, (old) => {
|
|
687
|
-
if (!old
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
const
|
|
691
|
-
|
|
692
|
-
next[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, next[lastIndex]), aiMessage), {
|
|
730
|
+
if (!old) return old;
|
|
731
|
+
const pages = [...old.pages];
|
|
732
|
+
const firstPage = [...pages[0]];
|
|
733
|
+
const lastIndex = firstPage.length - 1;
|
|
734
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, firstPage[lastIndex]), aiMessage), {
|
|
693
735
|
isLoading: false
|
|
694
736
|
});
|
|
695
|
-
|
|
737
|
+
pages[0] = firstPage;
|
|
738
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
739
|
+
pages
|
|
740
|
+
});
|
|
696
741
|
});
|
|
697
742
|
setIsPending(false);
|
|
698
743
|
},
|
|
@@ -715,17 +760,35 @@ function useOptimisticChat({
|
|
|
715
760
|
// (content: string) => void
|
|
716
761
|
isPending,
|
|
717
762
|
// 사용자가 채팅 전송 후 AI 응답이 올 때까지의 로딩
|
|
718
|
-
isInitialLoading
|
|
763
|
+
isInitialLoading,
|
|
719
764
|
// 초기 로딩 상태
|
|
765
|
+
// infinite query용
|
|
766
|
+
fetchNextPage,
|
|
767
|
+
hasNextPage,
|
|
768
|
+
isFetchingNextPage
|
|
720
769
|
};
|
|
721
770
|
}
|
|
722
771
|
|
|
723
|
-
// src/hooks/
|
|
772
|
+
// src/hooks/useVoiceChat.ts
|
|
724
773
|
var import_react_query2 = require("@tanstack/react-query");
|
|
725
774
|
var import_react7 = require("react");
|
|
726
|
-
function
|
|
775
|
+
function splitRawToMessage2(raw, mapped) {
|
|
776
|
+
const custom = {};
|
|
777
|
+
const mappedValues = new Set(Object.values(mapped));
|
|
778
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
779
|
+
if (!mappedValues.has(value)) {
|
|
780
|
+
custom[key] = value;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return __spreadProps(__spreadValues({}, mapped), {
|
|
784
|
+
custom
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
function useVoiceChat({
|
|
727
788
|
queryKey,
|
|
728
789
|
queryFn,
|
|
790
|
+
initialPageParam,
|
|
791
|
+
getNextPageParam,
|
|
729
792
|
mutationFn,
|
|
730
793
|
map,
|
|
731
794
|
voice,
|
|
@@ -738,17 +801,26 @@ function useVoiceOptimisticChat({
|
|
|
738
801
|
const currentTextRef = (0, import_react7.useRef)("");
|
|
739
802
|
const rollbackRef = (0, import_react7.useRef)(void 0);
|
|
740
803
|
const {
|
|
741
|
-
data
|
|
742
|
-
isLoading: isInitialLoading
|
|
743
|
-
|
|
804
|
+
data,
|
|
805
|
+
isLoading: isInitialLoading,
|
|
806
|
+
fetchNextPage,
|
|
807
|
+
hasNextPage,
|
|
808
|
+
isFetchingNextPage
|
|
809
|
+
} = (0, import_react_query2.useInfiniteQuery)({
|
|
744
810
|
queryKey,
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
811
|
+
initialPageParam,
|
|
812
|
+
queryFn: async ({ pageParam }) => {
|
|
813
|
+
const raw = await queryFn(pageParam);
|
|
814
|
+
return raw.map((r) => {
|
|
815
|
+
const mapped = map(r);
|
|
816
|
+
return splitRawToMessage2(r, mapped);
|
|
817
|
+
});
|
|
748
818
|
},
|
|
819
|
+
getNextPageParam,
|
|
749
820
|
staleTime,
|
|
750
821
|
gcTime
|
|
751
822
|
});
|
|
823
|
+
const messages = data ? data.pages.map((page) => [...page]).reverse().flat() : [];
|
|
752
824
|
const mutation = (0, import_react_query2.useMutation)({
|
|
753
825
|
mutationFn,
|
|
754
826
|
// (content: string) => Promise<TMutationRaw>
|
|
@@ -759,32 +831,41 @@ function useVoiceOptimisticChat({
|
|
|
759
831
|
await queryClient.cancelQueries({ queryKey });
|
|
760
832
|
}
|
|
761
833
|
queryClient.setQueryData(queryKey, (old) => {
|
|
762
|
-
|
|
763
|
-
return
|
|
764
|
-
|
|
765
|
-
|
|
834
|
+
var _a;
|
|
835
|
+
if (!old) return old;
|
|
836
|
+
const pages = [...old.pages];
|
|
837
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
838
|
+
pages[0] = [
|
|
839
|
+
...firstPage,
|
|
766
840
|
{
|
|
767
841
|
id: crypto.randomUUID(),
|
|
768
842
|
role: "AI",
|
|
769
843
|
content: "",
|
|
770
|
-
isLoading: true
|
|
844
|
+
isLoading: true,
|
|
845
|
+
custom: {}
|
|
771
846
|
}
|
|
772
847
|
];
|
|
848
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
849
|
+
pages
|
|
850
|
+
});
|
|
773
851
|
});
|
|
774
852
|
return prev ? { prev } : {};
|
|
775
853
|
},
|
|
776
854
|
onSuccess: (rawAiResponse) => {
|
|
777
|
-
const
|
|
855
|
+
const mapped = map(rawAiResponse);
|
|
856
|
+
const aiMessage = splitRawToMessage2(rawAiResponse, mapped);
|
|
778
857
|
queryClient.setQueryData(queryKey, (old) => {
|
|
779
|
-
if (!old
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
const
|
|
783
|
-
|
|
784
|
-
next[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, next[lastIndex]), aiMessage), {
|
|
858
|
+
if (!old) return old;
|
|
859
|
+
const pages = [...old.pages];
|
|
860
|
+
const firstPage = [...pages[0]];
|
|
861
|
+
const lastIndex = firstPage.length - 1;
|
|
862
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, firstPage[lastIndex]), aiMessage), {
|
|
785
863
|
isLoading: false
|
|
786
864
|
});
|
|
787
|
-
|
|
865
|
+
pages[0] = firstPage;
|
|
866
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
867
|
+
pages
|
|
868
|
+
});
|
|
788
869
|
});
|
|
789
870
|
setIsPending(false);
|
|
790
871
|
},
|
|
@@ -803,14 +884,24 @@ function useVoiceOptimisticChat({
|
|
|
803
884
|
if (prev) {
|
|
804
885
|
await queryClient.cancelQueries({ queryKey });
|
|
805
886
|
}
|
|
806
|
-
queryClient.setQueryData(queryKey, (old) =>
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
887
|
+
queryClient.setQueryData(queryKey, (old) => {
|
|
888
|
+
var _a;
|
|
889
|
+
if (!old) return old;
|
|
890
|
+
const pages = [...old.pages];
|
|
891
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
892
|
+
pages[0] = [
|
|
893
|
+
...firstPage,
|
|
894
|
+
{
|
|
895
|
+
id: crypto.randomUUID(),
|
|
896
|
+
role: "USER",
|
|
897
|
+
content: "",
|
|
898
|
+
custom: {}
|
|
899
|
+
}
|
|
900
|
+
];
|
|
901
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
902
|
+
pages
|
|
903
|
+
});
|
|
904
|
+
});
|
|
814
905
|
voice.start();
|
|
815
906
|
};
|
|
816
907
|
const onTranscript = (text) => {
|
|
@@ -818,13 +909,17 @@ function useVoiceOptimisticChat({
|
|
|
818
909
|
queryClient.setQueryData(queryKey, (old) => {
|
|
819
910
|
var _a;
|
|
820
911
|
if (!old) return old;
|
|
821
|
-
const
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
|
|
912
|
+
const pages = [...old.pages];
|
|
913
|
+
const firstPage = [...pages[0]];
|
|
914
|
+
const lastIndex = firstPage.length - 1;
|
|
915
|
+
if (((_a = firstPage[lastIndex]) == null ? void 0 : _a.role) !== "USER") return old;
|
|
916
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues({}, firstPage[lastIndex]), {
|
|
825
917
|
content: text
|
|
826
918
|
});
|
|
827
|
-
|
|
919
|
+
pages[0] = firstPage;
|
|
920
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
921
|
+
pages
|
|
922
|
+
});
|
|
828
923
|
});
|
|
829
924
|
};
|
|
830
925
|
(0, import_react7.useEffect)(() => {
|
|
@@ -850,8 +945,12 @@ function useVoiceOptimisticChat({
|
|
|
850
945
|
// 초기 로딩 상태
|
|
851
946
|
startRecording,
|
|
852
947
|
// 음성 인식 시작 함수
|
|
853
|
-
stopRecording
|
|
948
|
+
stopRecording,
|
|
854
949
|
// 음성 인식 종료 함수
|
|
950
|
+
// infinite query용
|
|
951
|
+
fetchNextPage,
|
|
952
|
+
hasNextPage,
|
|
953
|
+
isFetchingNextPage
|
|
855
954
|
};
|
|
856
955
|
}
|
|
857
956
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -863,6 +962,6 @@ function useVoiceOptimisticChat({
|
|
|
863
962
|
LoadingSpinner,
|
|
864
963
|
SendingDots,
|
|
865
964
|
useBrowserSpeechRecognition,
|
|
866
|
-
|
|
867
|
-
|
|
965
|
+
useChat,
|
|
966
|
+
useVoiceChat
|
|
868
967
|
});
|