@xinghunm/ai-chat 0.1.0 → 0.2.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/dist/index.js +12 -81
- package/dist/index.mjs +12 -81
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -733,21 +733,9 @@ var useChatMessageReveal = (message) => {
|
|
|
733
733
|
const isAssistantStreaming = message.role === "assistant" && message.status === "streaming";
|
|
734
734
|
const targetContent = message.content || "";
|
|
735
735
|
const targetUnits = (0, import_react5.useMemo)(() => Array.from(targetContent), [targetContent]);
|
|
736
|
-
const resetSnapshot = (0, import_react5.useMemo)(
|
|
737
|
-
() => ({
|
|
738
|
-
messageId: message.id,
|
|
739
|
-
initialTargetUnitCount: targetUnits.length,
|
|
740
|
-
initialBatchedTargetUnitCount: isAssistantStreaming ? 0 : targetUnits.length
|
|
741
|
-
}),
|
|
742
|
-
[isAssistantStreaming, message.id, targetUnits.length]
|
|
743
|
-
);
|
|
744
736
|
const pendingTargetUnitCountRef = (0, import_react5.useRef)(targetUnits.length);
|
|
745
737
|
const batchedTargetUnitCountRef = (0, import_react5.useRef)(isAssistantStreaming ? 0 : targetUnits.length);
|
|
746
738
|
const inputBatchTimeoutRef = (0, import_react5.useRef)(null);
|
|
747
|
-
const commitAnimationFrameRef = (0, import_react5.useRef)(null);
|
|
748
|
-
const freshBlockActivationFrameRef = (0, import_react5.useRef)(null);
|
|
749
|
-
const displayedUnitSyncFrameRef = (0, import_react5.useRef)(null);
|
|
750
|
-
const resetAnimationFrameRef = (0, import_react5.useRef)(null);
|
|
751
739
|
const [batchedTargetUnitCount, setBatchedTargetUnitCount] = (0, import_react5.useState)(
|
|
752
740
|
() => isAssistantStreaming ? 0 : targetUnits.length
|
|
753
741
|
);
|
|
@@ -771,54 +759,18 @@ var useChatMessageReveal = (message) => {
|
|
|
771
759
|
},
|
|
772
760
|
[isAssistantStreaming, message.role]
|
|
773
761
|
);
|
|
774
|
-
const scheduleBatchedTargetUnitCountCommit = (0, import_react5.useCallback)(
|
|
775
|
-
(nextTargetUnitCount) => {
|
|
776
|
-
if (commitAnimationFrameRef.current !== null) {
|
|
777
|
-
window.cancelAnimationFrame(commitAnimationFrameRef.current);
|
|
778
|
-
}
|
|
779
|
-
commitAnimationFrameRef.current = window.requestAnimationFrame(() => {
|
|
780
|
-
commitAnimationFrameRef.current = null;
|
|
781
|
-
commitBatchedTargetUnitCount(nextTargetUnitCount);
|
|
782
|
-
});
|
|
783
|
-
},
|
|
784
|
-
[commitBatchedTargetUnitCount]
|
|
785
|
-
);
|
|
786
762
|
(0, import_react5.useEffect)(() => {
|
|
787
|
-
pendingTargetUnitCountRef.current =
|
|
788
|
-
batchedTargetUnitCountRef.current =
|
|
763
|
+
pendingTargetUnitCountRef.current = targetUnits.length;
|
|
764
|
+
batchedTargetUnitCountRef.current = isAssistantStreaming ? 0 : targetUnits.length;
|
|
765
|
+
setBatchedTargetUnitCount(batchedTargetUnitCountRef.current);
|
|
766
|
+
setDisplayedUnitCount(isAssistantStreaming ? 0 : targetUnits.length);
|
|
789
767
|
lastDisplayedBlockCountRef.current = 0;
|
|
790
768
|
if (inputBatchTimeoutRef.current !== null) {
|
|
791
769
|
window.clearTimeout(inputBatchTimeoutRef.current);
|
|
792
770
|
inputBatchTimeoutRef.current = null;
|
|
793
771
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
commitAnimationFrameRef.current = null;
|
|
797
|
-
}
|
|
798
|
-
if (freshBlockActivationFrameRef.current !== null) {
|
|
799
|
-
window.cancelAnimationFrame(freshBlockActivationFrameRef.current);
|
|
800
|
-
freshBlockActivationFrameRef.current = null;
|
|
801
|
-
}
|
|
802
|
-
if (displayedUnitSyncFrameRef.current !== null) {
|
|
803
|
-
window.cancelAnimationFrame(displayedUnitSyncFrameRef.current);
|
|
804
|
-
displayedUnitSyncFrameRef.current = null;
|
|
805
|
-
}
|
|
806
|
-
if (resetAnimationFrameRef.current !== null) {
|
|
807
|
-
window.cancelAnimationFrame(resetAnimationFrameRef.current);
|
|
808
|
-
}
|
|
809
|
-
resetAnimationFrameRef.current = window.requestAnimationFrame(() => {
|
|
810
|
-
resetAnimationFrameRef.current = null;
|
|
811
|
-
setBatchedTargetUnitCount(resetSnapshot.initialBatchedTargetUnitCount);
|
|
812
|
-
setDisplayedUnitCount(resetSnapshot.initialBatchedTargetUnitCount);
|
|
813
|
-
setIsFreshBlockActive(false);
|
|
814
|
-
});
|
|
815
|
-
return () => {
|
|
816
|
-
if (resetAnimationFrameRef.current !== null) {
|
|
817
|
-
window.cancelAnimationFrame(resetAnimationFrameRef.current);
|
|
818
|
-
resetAnimationFrameRef.current = null;
|
|
819
|
-
}
|
|
820
|
-
};
|
|
821
|
-
}, [resetSnapshot]);
|
|
772
|
+
setIsFreshBlockActive(false);
|
|
773
|
+
}, [message.id, isAssistantStreaming, targetUnits.length]);
|
|
822
774
|
(0, import_react5.useEffect)(() => {
|
|
823
775
|
pendingTargetUnitCountRef.current = targetUnits.length;
|
|
824
776
|
if (message.role !== "assistant" || !isAssistantStreaming) {
|
|
@@ -826,14 +778,14 @@ var useChatMessageReveal = (message) => {
|
|
|
826
778
|
window.clearTimeout(inputBatchTimeoutRef.current);
|
|
827
779
|
inputBatchTimeoutRef.current = null;
|
|
828
780
|
}
|
|
829
|
-
|
|
781
|
+
commitBatchedTargetUnitCount(targetUnits.length);
|
|
830
782
|
return;
|
|
831
783
|
}
|
|
832
784
|
if (targetUnits.length <= batchedTargetUnitCountRef.current) {
|
|
833
785
|
return;
|
|
834
786
|
}
|
|
835
787
|
if (batchedTargetUnitCountRef.current === 0) {
|
|
836
|
-
|
|
788
|
+
commitBatchedTargetUnitCount(targetUnits.length);
|
|
837
789
|
return;
|
|
838
790
|
}
|
|
839
791
|
if (inputBatchTimeoutRef.current !== null) {
|
|
@@ -849,13 +801,7 @@ var useChatMessageReveal = (message) => {
|
|
|
849
801
|
inputBatchTimeoutRef.current = null;
|
|
850
802
|
}
|
|
851
803
|
};
|
|
852
|
-
}, [
|
|
853
|
-
commitBatchedTargetUnitCount,
|
|
854
|
-
isAssistantStreaming,
|
|
855
|
-
message.role,
|
|
856
|
-
scheduleBatchedTargetUnitCountCommit,
|
|
857
|
-
targetUnits.length
|
|
858
|
-
]);
|
|
804
|
+
}, [commitBatchedTargetUnitCount, isAssistantStreaming, message.role, targetUnits.length]);
|
|
859
805
|
const displayedContent = (0, import_react5.useMemo)(
|
|
860
806
|
() => targetUnits.slice(0, displayedUnitCount).join(""),
|
|
861
807
|
[displayedUnitCount, targetUnits]
|
|
@@ -867,18 +813,11 @@ var useChatMessageReveal = (message) => {
|
|
|
867
813
|
if (!hasNewDisplayedBlock) {
|
|
868
814
|
return;
|
|
869
815
|
}
|
|
870
|
-
|
|
871
|
-
freshBlockActivationFrameRef.current = null;
|
|
872
|
-
setIsFreshBlockActive(true);
|
|
873
|
-
});
|
|
816
|
+
setIsFreshBlockActive(true);
|
|
874
817
|
const timer = window.setTimeout(() => {
|
|
875
818
|
setIsFreshBlockActive(false);
|
|
876
819
|
}, STREAM_FRESH_BLOCK_SETTLE_MS);
|
|
877
820
|
return () => {
|
|
878
|
-
if (freshBlockActivationFrameRef.current !== null) {
|
|
879
|
-
window.cancelAnimationFrame(freshBlockActivationFrameRef.current);
|
|
880
|
-
freshBlockActivationFrameRef.current = null;
|
|
881
|
-
}
|
|
882
821
|
window.clearTimeout(timer);
|
|
883
822
|
};
|
|
884
823
|
}, [contentBlocks.length, message.role]);
|
|
@@ -886,17 +825,9 @@ var useChatMessageReveal = (message) => {
|
|
|
886
825
|
const shouldAnimateReveal = message.role === "assistant" && displayedUnitCount < batchedTargetUnitCount && (isAssistantStreaming || displayedUnitCount > 0);
|
|
887
826
|
if (!shouldAnimateReveal) {
|
|
888
827
|
if (displayedUnitCount !== batchedTargetUnitCount) {
|
|
889
|
-
|
|
890
|
-
displayedUnitSyncFrameRef.current = null;
|
|
891
|
-
setDisplayedUnitCount(batchedTargetUnitCount);
|
|
892
|
-
});
|
|
828
|
+
setDisplayedUnitCount(batchedTargetUnitCount);
|
|
893
829
|
}
|
|
894
|
-
return
|
|
895
|
-
if (displayedUnitSyncFrameRef.current !== null) {
|
|
896
|
-
window.cancelAnimationFrame(displayedUnitSyncFrameRef.current);
|
|
897
|
-
displayedUnitSyncFrameRef.current = null;
|
|
898
|
-
}
|
|
899
|
-
};
|
|
830
|
+
return;
|
|
900
831
|
}
|
|
901
832
|
const timer = window.setInterval(() => {
|
|
902
833
|
setDisplayedUnitCount((current) => {
|
package/dist/index.mjs
CHANGED
|
@@ -687,21 +687,9 @@ var useChatMessageReveal = (message) => {
|
|
|
687
687
|
const isAssistantStreaming = message.role === "assistant" && message.status === "streaming";
|
|
688
688
|
const targetContent = message.content || "";
|
|
689
689
|
const targetUnits = useMemo2(() => Array.from(targetContent), [targetContent]);
|
|
690
|
-
const resetSnapshot = useMemo2(
|
|
691
|
-
() => ({
|
|
692
|
-
messageId: message.id,
|
|
693
|
-
initialTargetUnitCount: targetUnits.length,
|
|
694
|
-
initialBatchedTargetUnitCount: isAssistantStreaming ? 0 : targetUnits.length
|
|
695
|
-
}),
|
|
696
|
-
[isAssistantStreaming, message.id, targetUnits.length]
|
|
697
|
-
);
|
|
698
690
|
const pendingTargetUnitCountRef = useRef2(targetUnits.length);
|
|
699
691
|
const batchedTargetUnitCountRef = useRef2(isAssistantStreaming ? 0 : targetUnits.length);
|
|
700
692
|
const inputBatchTimeoutRef = useRef2(null);
|
|
701
|
-
const commitAnimationFrameRef = useRef2(null);
|
|
702
|
-
const freshBlockActivationFrameRef = useRef2(null);
|
|
703
|
-
const displayedUnitSyncFrameRef = useRef2(null);
|
|
704
|
-
const resetAnimationFrameRef = useRef2(null);
|
|
705
693
|
const [batchedTargetUnitCount, setBatchedTargetUnitCount] = useState2(
|
|
706
694
|
() => isAssistantStreaming ? 0 : targetUnits.length
|
|
707
695
|
);
|
|
@@ -725,54 +713,18 @@ var useChatMessageReveal = (message) => {
|
|
|
725
713
|
},
|
|
726
714
|
[isAssistantStreaming, message.role]
|
|
727
715
|
);
|
|
728
|
-
const scheduleBatchedTargetUnitCountCommit = useCallback(
|
|
729
|
-
(nextTargetUnitCount) => {
|
|
730
|
-
if (commitAnimationFrameRef.current !== null) {
|
|
731
|
-
window.cancelAnimationFrame(commitAnimationFrameRef.current);
|
|
732
|
-
}
|
|
733
|
-
commitAnimationFrameRef.current = window.requestAnimationFrame(() => {
|
|
734
|
-
commitAnimationFrameRef.current = null;
|
|
735
|
-
commitBatchedTargetUnitCount(nextTargetUnitCount);
|
|
736
|
-
});
|
|
737
|
-
},
|
|
738
|
-
[commitBatchedTargetUnitCount]
|
|
739
|
-
);
|
|
740
716
|
useEffect(() => {
|
|
741
|
-
pendingTargetUnitCountRef.current =
|
|
742
|
-
batchedTargetUnitCountRef.current =
|
|
717
|
+
pendingTargetUnitCountRef.current = targetUnits.length;
|
|
718
|
+
batchedTargetUnitCountRef.current = isAssistantStreaming ? 0 : targetUnits.length;
|
|
719
|
+
setBatchedTargetUnitCount(batchedTargetUnitCountRef.current);
|
|
720
|
+
setDisplayedUnitCount(isAssistantStreaming ? 0 : targetUnits.length);
|
|
743
721
|
lastDisplayedBlockCountRef.current = 0;
|
|
744
722
|
if (inputBatchTimeoutRef.current !== null) {
|
|
745
723
|
window.clearTimeout(inputBatchTimeoutRef.current);
|
|
746
724
|
inputBatchTimeoutRef.current = null;
|
|
747
725
|
}
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
commitAnimationFrameRef.current = null;
|
|
751
|
-
}
|
|
752
|
-
if (freshBlockActivationFrameRef.current !== null) {
|
|
753
|
-
window.cancelAnimationFrame(freshBlockActivationFrameRef.current);
|
|
754
|
-
freshBlockActivationFrameRef.current = null;
|
|
755
|
-
}
|
|
756
|
-
if (displayedUnitSyncFrameRef.current !== null) {
|
|
757
|
-
window.cancelAnimationFrame(displayedUnitSyncFrameRef.current);
|
|
758
|
-
displayedUnitSyncFrameRef.current = null;
|
|
759
|
-
}
|
|
760
|
-
if (resetAnimationFrameRef.current !== null) {
|
|
761
|
-
window.cancelAnimationFrame(resetAnimationFrameRef.current);
|
|
762
|
-
}
|
|
763
|
-
resetAnimationFrameRef.current = window.requestAnimationFrame(() => {
|
|
764
|
-
resetAnimationFrameRef.current = null;
|
|
765
|
-
setBatchedTargetUnitCount(resetSnapshot.initialBatchedTargetUnitCount);
|
|
766
|
-
setDisplayedUnitCount(resetSnapshot.initialBatchedTargetUnitCount);
|
|
767
|
-
setIsFreshBlockActive(false);
|
|
768
|
-
});
|
|
769
|
-
return () => {
|
|
770
|
-
if (resetAnimationFrameRef.current !== null) {
|
|
771
|
-
window.cancelAnimationFrame(resetAnimationFrameRef.current);
|
|
772
|
-
resetAnimationFrameRef.current = null;
|
|
773
|
-
}
|
|
774
|
-
};
|
|
775
|
-
}, [resetSnapshot]);
|
|
726
|
+
setIsFreshBlockActive(false);
|
|
727
|
+
}, [message.id, isAssistantStreaming, targetUnits.length]);
|
|
776
728
|
useEffect(() => {
|
|
777
729
|
pendingTargetUnitCountRef.current = targetUnits.length;
|
|
778
730
|
if (message.role !== "assistant" || !isAssistantStreaming) {
|
|
@@ -780,14 +732,14 @@ var useChatMessageReveal = (message) => {
|
|
|
780
732
|
window.clearTimeout(inputBatchTimeoutRef.current);
|
|
781
733
|
inputBatchTimeoutRef.current = null;
|
|
782
734
|
}
|
|
783
|
-
|
|
735
|
+
commitBatchedTargetUnitCount(targetUnits.length);
|
|
784
736
|
return;
|
|
785
737
|
}
|
|
786
738
|
if (targetUnits.length <= batchedTargetUnitCountRef.current) {
|
|
787
739
|
return;
|
|
788
740
|
}
|
|
789
741
|
if (batchedTargetUnitCountRef.current === 0) {
|
|
790
|
-
|
|
742
|
+
commitBatchedTargetUnitCount(targetUnits.length);
|
|
791
743
|
return;
|
|
792
744
|
}
|
|
793
745
|
if (inputBatchTimeoutRef.current !== null) {
|
|
@@ -803,13 +755,7 @@ var useChatMessageReveal = (message) => {
|
|
|
803
755
|
inputBatchTimeoutRef.current = null;
|
|
804
756
|
}
|
|
805
757
|
};
|
|
806
|
-
}, [
|
|
807
|
-
commitBatchedTargetUnitCount,
|
|
808
|
-
isAssistantStreaming,
|
|
809
|
-
message.role,
|
|
810
|
-
scheduleBatchedTargetUnitCountCommit,
|
|
811
|
-
targetUnits.length
|
|
812
|
-
]);
|
|
758
|
+
}, [commitBatchedTargetUnitCount, isAssistantStreaming, message.role, targetUnits.length]);
|
|
813
759
|
const displayedContent = useMemo2(
|
|
814
760
|
() => targetUnits.slice(0, displayedUnitCount).join(""),
|
|
815
761
|
[displayedUnitCount, targetUnits]
|
|
@@ -821,18 +767,11 @@ var useChatMessageReveal = (message) => {
|
|
|
821
767
|
if (!hasNewDisplayedBlock) {
|
|
822
768
|
return;
|
|
823
769
|
}
|
|
824
|
-
|
|
825
|
-
freshBlockActivationFrameRef.current = null;
|
|
826
|
-
setIsFreshBlockActive(true);
|
|
827
|
-
});
|
|
770
|
+
setIsFreshBlockActive(true);
|
|
828
771
|
const timer = window.setTimeout(() => {
|
|
829
772
|
setIsFreshBlockActive(false);
|
|
830
773
|
}, STREAM_FRESH_BLOCK_SETTLE_MS);
|
|
831
774
|
return () => {
|
|
832
|
-
if (freshBlockActivationFrameRef.current !== null) {
|
|
833
|
-
window.cancelAnimationFrame(freshBlockActivationFrameRef.current);
|
|
834
|
-
freshBlockActivationFrameRef.current = null;
|
|
835
|
-
}
|
|
836
775
|
window.clearTimeout(timer);
|
|
837
776
|
};
|
|
838
777
|
}, [contentBlocks.length, message.role]);
|
|
@@ -840,17 +779,9 @@ var useChatMessageReveal = (message) => {
|
|
|
840
779
|
const shouldAnimateReveal = message.role === "assistant" && displayedUnitCount < batchedTargetUnitCount && (isAssistantStreaming || displayedUnitCount > 0);
|
|
841
780
|
if (!shouldAnimateReveal) {
|
|
842
781
|
if (displayedUnitCount !== batchedTargetUnitCount) {
|
|
843
|
-
|
|
844
|
-
displayedUnitSyncFrameRef.current = null;
|
|
845
|
-
setDisplayedUnitCount(batchedTargetUnitCount);
|
|
846
|
-
});
|
|
782
|
+
setDisplayedUnitCount(batchedTargetUnitCount);
|
|
847
783
|
}
|
|
848
|
-
return
|
|
849
|
-
if (displayedUnitSyncFrameRef.current !== null) {
|
|
850
|
-
window.cancelAnimationFrame(displayedUnitSyncFrameRef.current);
|
|
851
|
-
displayedUnitSyncFrameRef.current = null;
|
|
852
|
-
}
|
|
853
|
-
};
|
|
784
|
+
return;
|
|
854
785
|
}
|
|
855
786
|
const timer = window.setInterval(() => {
|
|
856
787
|
setDisplayedUnitCount((current) => {
|