@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.cjs CHANGED
@@ -2982,36 +2982,71 @@ 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
- audioPlayer.load("".concat(superinterfaceContext.baseUrl, "/tts?input=").concat(input2), {
3034
+ var searchParams = new URLSearchParams(_object_spread({
3035
+ input: firstUnplayedMessageSentence.sentence
3036
+ }, isHtmlAudioSupported && superinterfaceContext.publicApiKey ? {
3037
+ publicApiKey: superinterfaceContext.publicApiKey
3038
+ } : {}));
3039
+ audioPlayer.load("".concat(superinterfaceContext.baseUrl, "/tts?").concat(searchParams), _object_spread({
3011
3040
  format: "mp3",
3012
3041
  autoplay: true,
3013
3042
  html5: isHtmlAudioSupported,
3014
- onend: onEnd,
3043
+ onend: function() {
3044
+ setIsPlaying(false);
3045
+ if (unplayedMessageSentences.length === 1 && latestMessageProps.latestMessage.status !== "in_progress") {
3046
+ onEnd();
3047
+ }
3048
+ }
3049
+ }, isHtmlAudioSupported ? {} : {
3015
3050
  xhr: _object_spread_props(_object_spread({}, superinterfaceContext.publicApiKey ? {
3016
3051
  headers: {
3017
3052
  Authorization: "Bearer ".concat(superinterfaceContext.publicApiKey)
@@ -3019,16 +3054,17 @@ var useMessageAudio = function(param) {
3019
3054
  } : {}), {
3020
3055
  withCredentials: true
3021
3056
  })
3022
- });
3057
+ }));
3023
3058
  }, [
3059
+ isPlaying,
3024
3060
  superinterfaceContext,
3025
3061
  latestMessageProps,
3026
3062
  audioPlayer,
3027
- playedMessages,
3063
+ playedMessageSentences,
3028
3064
  onEnd
3029
3065
  ]);
3030
3066
  var isInited = (0, import_react40.useRef)(false);
3031
- 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];
3032
3068
  (0, import_react40.useEffect)(function() {
3033
3069
  if (!audioPlayer.playing) return;
3034
3070
  if (isInited.current) return;