@superinterface/react 2.3.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.cjs CHANGED
@@ -2982,33 +2982,57 @@ var unsupportedNames = [
2982
2982
  ];
2983
2983
  var isHtmlAudioSupported = !unsupportedNames.includes(((_this = (0, import_detect_browser.detect)()) === null || _this === void 0 ? void 0 : _this.name) || "");
2984
2984
  // src/hooks/audioThreads/useMessageAudio/index.ts
2985
+ var SPLIT_SENTENCE_REGEX = /[^\.\?!]+[\.\?!]/g;
2986
+ var FULL_SENTENCE_REGEX = /^\s*[A-Z].*[.?!]$/;
2987
+ var getMessageSentences = function(param) {
2988
+ var messageId = param.messageId, input2 = param.input;
2989
+ var sentences = input2.match(SPLIT_SENTENCE_REGEX) || [];
2990
+ return sentences.map(function(sentence) {
2991
+ return {
2992
+ messageId: messageId,
2993
+ sentence: sentence
2994
+ };
2995
+ });
2996
+ };
2985
2997
  var useMessageAudio = function(param) {
2986
2998
  var onEnd = param.onEnd;
2987
- var _ref = _sliced_to_array((0, import_react40.useState)([]), 2), playedMessages = _ref[0], setPlayedMessages = _ref[1];
2999
+ var _ref = _sliced_to_array((0, import_react40.useState)([]), 2), playedMessageSentences = _ref[0], setPlayedMessageSentences = _ref[1];
2988
3000
  var audioPlayer = (0, import_react_use_audio_player2.useAudioPlayer)();
2989
3001
  var superinterfaceContext = useSuperinterfaceContext();
3002
+ var _ref1 = _sliced_to_array((0, import_react40.useState)(false), 2), isPlaying = _ref1[0], setIsPlaying = _ref1[1];
2990
3003
  var latestMessageProps = useLatestMessage();
2991
3004
  (0, import_react40.useEffect)(function() {
3005
+ if (isPlaying) return;
2992
3006
  if (audioPlayer.playing) return;
2993
3007
  if (!latestMessageProps.latestMessage) return;
2994
3008
  if (latestMessageProps.latestMessage.role !== "assistant") return;
2995
- if (playedMessages.find(function(pm) {
2996
- return pm.id === latestMessageProps.latestMessage.id || isOptimistic({
2997
- id: pm.id
2998
- }) && pm.content === latestMessageProps.latestMessage.content;
2999
- })) return;
3000
- if (playedMessages.includes(latestMessageProps.latestMessage)) return;
3001
3009
  var input2 = input({
3002
3010
  message: latestMessageProps.latestMessage
3003
3011
  });
3004
3012
  if (!input2) return;
3005
- setPlayedMessages(function(prev) {
3013
+ var messageSentences = getMessageSentences({
3014
+ messageId: latestMessageProps.latestMessage.id,
3015
+ input: input2
3016
+ });
3017
+ var unplayedMessageSentences = messageSentences.filter(function(ms) {
3018
+ return !playedMessageSentences.find(function(pms) {
3019
+ return pms.messageId === ms.messageId && pms.sentence === ms.sentence;
3020
+ });
3021
+ });
3022
+ var firstUnplayedMessageSentence = unplayedMessageSentences[0];
3023
+ if (!firstUnplayedMessageSentence) {
3024
+ return;
3025
+ }
3026
+ var isFullSentence = FULL_SENTENCE_REGEX.test(firstUnplayedMessageSentence.sentence);
3027
+ if (!isFullSentence) return;
3028
+ setIsPlaying(true);
3029
+ setPlayedMessageSentences(function(prev) {
3006
3030
  return _to_consumable_array(prev).concat([
3007
- latestMessageProps.latestMessage
3031
+ firstUnplayedMessageSentence
3008
3032
  ]);
3009
3033
  });
3010
3034
  var searchParams = new URLSearchParams(_object_spread({
3011
- input: input2
3035
+ input: firstUnplayedMessageSentence.sentence
3012
3036
  }, isHtmlAudioSupported && superinterfaceContext.publicApiKey ? {
3013
3037
  publicApiKey: superinterfaceContext.publicApiKey
3014
3038
  } : {}));
@@ -3016,7 +3040,12 @@ var useMessageAudio = function(param) {
3016
3040
  format: "mp3",
3017
3041
  autoplay: true,
3018
3042
  html5: isHtmlAudioSupported,
3019
- onend: onEnd
3043
+ onend: function() {
3044
+ setIsPlaying(false);
3045
+ if (unplayedMessageSentences.length === 1 && latestMessageProps.latestMessage.status !== "in_progress") {
3046
+ onEnd();
3047
+ }
3048
+ }
3020
3049
  }, isHtmlAudioSupported ? {} : {
3021
3050
  xhr: _object_spread_props(_object_spread({}, superinterfaceContext.publicApiKey ? {
3022
3051
  headers: {
@@ -3027,14 +3056,15 @@ var useMessageAudio = function(param) {
3027
3056
  })
3028
3057
  }));
3029
3058
  }, [
3059
+ isPlaying,
3030
3060
  superinterfaceContext,
3031
3061
  latestMessageProps,
3032
3062
  audioPlayer,
3033
- playedMessages,
3063
+ playedMessageSentences,
3034
3064
  onEnd
3035
3065
  ]);
3036
3066
  var isInited = (0, import_react40.useRef)(false);
3037
- var _ref1 = _sliced_to_array((0, import_react40.useState)(null), 2), audioEngine = _ref1[0], setAudioEngine = _ref1[1];
3067
+ var _ref2 = _sliced_to_array((0, import_react40.useState)(null), 2), audioEngine = _ref2[0], setAudioEngine = _ref2[1];
3038
3068
  (0, import_react40.useEffect)(function() {
3039
3069
  if (!audioPlayer.playing) return;
3040
3070
  if (isInited.current) return;