@superinterface/react 2.2.0 → 2.4.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 CHANGED
@@ -2858,36 +2858,71 @@ var unsupportedNames = [
2858
2858
  ];
2859
2859
  var isHtmlAudioSupported = !unsupportedNames.includes(((_detect = detect()) === null || _detect === void 0 ? void 0 : _detect.name) || "");
2860
2860
  // src/hooks/audioThreads/useMessageAudio/index.ts
2861
+ var SPLIT_SENTENCE_REGEX = /[^\.\?!]+[\.\?!]/g;
2862
+ var FULL_SENTENCE_REGEX = /^\s*[A-Z].*[.?!]$/;
2863
+ var getMessageSentences = function(param) {
2864
+ var messageId = param.messageId, input2 = param.input;
2865
+ var sentences = input2.match(SPLIT_SENTENCE_REGEX) || [];
2866
+ return sentences.map(function(sentence) {
2867
+ return {
2868
+ messageId: messageId,
2869
+ sentence: sentence
2870
+ };
2871
+ });
2872
+ };
2861
2873
  var useMessageAudio = function(param) {
2862
2874
  var onEnd = param.onEnd;
2863
- var _useState4 = _sliced_to_array(useState4([]), 2), playedMessages = _useState4[0], setPlayedMessages = _useState4[1];
2875
+ var _useState4 = _sliced_to_array(useState4([]), 2), playedMessageSentences = _useState4[0], setPlayedMessageSentences = _useState4[1];
2864
2876
  var audioPlayer = useAudioPlayer2();
2865
2877
  var superinterfaceContext = useSuperinterfaceContext();
2878
+ var _useState41 = _sliced_to_array(useState4(false), 2), isPlaying = _useState41[0], setIsPlaying = _useState41[1];
2866
2879
  var latestMessageProps = useLatestMessage();
2867
2880
  useEffect5(function() {
2881
+ if (isPlaying) return;
2868
2882
  if (audioPlayer.playing) return;
2869
2883
  if (!latestMessageProps.latestMessage) return;
2870
2884
  if (latestMessageProps.latestMessage.role !== "assistant") return;
2871
- if (playedMessages.find(function(pm) {
2872
- return pm.id === latestMessageProps.latestMessage.id || isOptimistic({
2873
- id: pm.id
2874
- }) && pm.content === latestMessageProps.latestMessage.content;
2875
- })) return;
2876
- if (playedMessages.includes(latestMessageProps.latestMessage)) return;
2877
2885
  var input2 = input({
2878
2886
  message: latestMessageProps.latestMessage
2879
2887
  });
2880
2888
  if (!input2) return;
2881
- setPlayedMessages(function(prev) {
2889
+ var messageSentences = getMessageSentences({
2890
+ messageId: latestMessageProps.latestMessage.id,
2891
+ input: input2
2892
+ });
2893
+ var unplayedMessageSentences = messageSentences.filter(function(ms) {
2894
+ return !playedMessageSentences.find(function(pms) {
2895
+ return pms.messageId === ms.messageId && pms.sentence === ms.sentence;
2896
+ });
2897
+ });
2898
+ var firstUnplayedMessageSentence = unplayedMessageSentences[0];
2899
+ if (!firstUnplayedMessageSentence) {
2900
+ return;
2901
+ }
2902
+ var isFullSentence = FULL_SENTENCE_REGEX.test(firstUnplayedMessageSentence.sentence);
2903
+ if (!isFullSentence) return;
2904
+ setIsPlaying(true);
2905
+ setPlayedMessageSentences(function(prev) {
2882
2906
  return _to_consumable_array(prev).concat([
2883
- latestMessageProps.latestMessage
2907
+ firstUnplayedMessageSentence
2884
2908
  ]);
2885
2909
  });
2886
- audioPlayer.load("".concat(superinterfaceContext.baseUrl, "/tts?input=").concat(input2), {
2910
+ var searchParams = new URLSearchParams(_object_spread({
2911
+ input: firstUnplayedMessageSentence.sentence
2912
+ }, isHtmlAudioSupported && superinterfaceContext.publicApiKey ? {
2913
+ publicApiKey: superinterfaceContext.publicApiKey
2914
+ } : {}));
2915
+ audioPlayer.load("".concat(superinterfaceContext.baseUrl, "/tts?").concat(searchParams), _object_spread({
2887
2916
  format: "mp3",
2888
2917
  autoplay: true,
2889
2918
  html5: isHtmlAudioSupported,
2890
- onend: onEnd,
2919
+ onend: function() {
2920
+ setIsPlaying(false);
2921
+ if (unplayedMessageSentences.length === 1 && latestMessageProps.latestMessage.status !== "in_progress") {
2922
+ onEnd();
2923
+ }
2924
+ }
2925
+ }, isHtmlAudioSupported ? {} : {
2891
2926
  xhr: _object_spread_props(_object_spread({}, superinterfaceContext.publicApiKey ? {
2892
2927
  headers: {
2893
2928
  Authorization: "Bearer ".concat(superinterfaceContext.publicApiKey)
@@ -2895,16 +2930,17 @@ var useMessageAudio = function(param) {
2895
2930
  } : {}), {
2896
2931
  withCredentials: true
2897
2932
  })
2898
- });
2933
+ }));
2899
2934
  }, [
2935
+ isPlaying,
2900
2936
  superinterfaceContext,
2901
2937
  latestMessageProps,
2902
2938
  audioPlayer,
2903
- playedMessages,
2939
+ playedMessageSentences,
2904
2940
  onEnd
2905
2941
  ]);
2906
2942
  var isInited = useRef5(false);
2907
- var _useState41 = _sliced_to_array(useState4(null), 2), audioEngine = _useState41[0], setAudioEngine = _useState41[1];
2943
+ var _useState42 = _sliced_to_array(useState4(null), 2), audioEngine = _useState42[0], setAudioEngine = _useState42[1];
2908
2944
  useEffect5(function() {
2909
2945
  if (!audioPlayer.playing) return;
2910
2946
  if (isInited.current) return;