Compare commits
8 Commits
dev-exampl
...
re-add-dow
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c27e8566fb | ||
|
|
511f1336da | ||
|
|
25ea3b8e98 | ||
|
|
b1ec67ea42 | ||
|
|
f1bb5fa4c5 | ||
|
|
44d8596872 | ||
|
|
32d84c85ea | ||
|
|
d839e4661c |
@@ -1,6 +1,7 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useRecoilValue } from 'recoil';
|
import { useRecoilValue } from 'recoil';
|
||||||
import type { TMessage } from 'librechat-data-provider';
|
import type { TMessage } from 'librechat-data-provider';
|
||||||
|
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '~/components/ui';
|
||||||
import { VolumeIcon, VolumeMuteIcon, Spinner } from '~/components/svg';
|
import { VolumeIcon, VolumeMuteIcon, Spinner } from '~/components/svg';
|
||||||
import { useLocalize, useTextToSpeech } from '~/hooks';
|
import { useLocalize, useTextToSpeech } from '~/hooks';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
@@ -14,32 +15,77 @@ type THoverButtons = {
|
|||||||
export default function MessageAudio({ index, message, isLast }: THoverButtons) {
|
export default function MessageAudio({ index, message, isLast }: THoverButtons) {
|
||||||
const localize = useLocalize();
|
const localize = useLocalize();
|
||||||
const playbackRate = useRecoilValue(store.playbackRate);
|
const playbackRate = useRecoilValue(store.playbackRate);
|
||||||
|
const [audioText, setAudioText] = useState<string>(localize('com_ui_info_read_aloud'));
|
||||||
|
const [tooltipOpen, setTooltipOpen] = useState(false);
|
||||||
|
const [wasLongPress, setWasLongPress] = useState(false);
|
||||||
|
|
||||||
const { toggleSpeech, isSpeaking, isLoading, audioRef } = useTextToSpeech(message, isLast, index);
|
const { toggleSpeech, isSpeaking, isLoading, audioRef } = useTextToSpeech(message, isLast, index);
|
||||||
|
|
||||||
|
const isMouseDownRef = useRef(false);
|
||||||
|
const timerRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
const counterRef = useRef(0);
|
||||||
|
|
||||||
const renderIcon = (size: string) => {
|
const renderIcon = (size: string) => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <Spinner size={size} />;
|
return <Spinner size={size} />;
|
||||||
}
|
}
|
||||||
|
return isSpeaking ? <VolumeMuteIcon size={size} /> : <VolumeIcon size={size} />;
|
||||||
|
};
|
||||||
|
|
||||||
if (isSpeaking) {
|
const handleMouseDown = () => {
|
||||||
return <VolumeMuteIcon size={size} />;
|
setWasLongPress(false);
|
||||||
|
setTooltipOpen(true);
|
||||||
|
if (isMouseDownRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
isMouseDownRef.current = true;
|
||||||
|
counterRef.current = 2;
|
||||||
|
setAudioText(localize('com_ui_hold_mouse_download', counterRef.current.toString()));
|
||||||
|
timerRef.current = setInterval(() => {
|
||||||
|
counterRef.current--;
|
||||||
|
if (counterRef.current >= 0) {
|
||||||
|
setAudioText(localize('com_ui_hold_mouse_download', counterRef.current.toString()));
|
||||||
|
}
|
||||||
|
if (isMouseDownRef.current && counterRef.current === 0) {
|
||||||
|
setAudioText(localize('com_ui_downloading'));
|
||||||
|
toggleSpeech(true);
|
||||||
|
}
|
||||||
|
if (counterRef.current < 0 && timerRef.current) {
|
||||||
|
clearInterval(timerRef.current);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
window.addEventListener('mouseup', handleMouseUp);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMouseUp = () => {
|
||||||
|
if (counterRef.current > 0) {
|
||||||
|
toggleSpeech(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <VolumeIcon size={size} />;
|
if (counterRef.current === 0) {
|
||||||
|
setWasLongPress(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
setTooltipOpen(false);
|
||||||
|
isMouseDownRef.current = false;
|
||||||
|
if (timerRef.current) {
|
||||||
|
clearInterval(timerRef.current);
|
||||||
|
timerRef.current = null;
|
||||||
|
setAudioText(localize('com_ui_info_read_aloud'));
|
||||||
|
}
|
||||||
|
|
||||||
|
window.removeEventListener('mouseup', handleMouseUp);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const messageAudio = document.getElementById(
|
const messageAudio = document.getElementById(
|
||||||
`audio-${message.messageId}`,
|
`audio-${message.messageId}`,
|
||||||
) as HTMLAudioElement | null;
|
) as HTMLAudioElement | null;
|
||||||
if (!messageAudio) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
playbackRate &&
|
|
||||||
playbackRate > 0 &&
|
|
||||||
messageAudio &&
|
messageAudio &&
|
||||||
|
playbackRate !== null &&
|
||||||
|
playbackRate > 0 &&
|
||||||
messageAudio.playbackRate !== playbackRate
|
messageAudio.playbackRate !== playbackRate
|
||||||
) {
|
) {
|
||||||
messageAudio.playbackRate = playbackRate;
|
messageAudio.playbackRate = playbackRate;
|
||||||
@@ -47,48 +93,60 @@ export default function MessageAudio({ index, message, isLast }: THoverButtons)
|
|||||||
}, [audioRef, isSpeaking, playbackRate, message.messageId]);
|
}, [audioRef, isSpeaking, playbackRate, message.messageId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<TooltipProvider>
|
||||||
<button
|
<>
|
||||||
className="hover-button rounded-md p-1 pl-0 text-gray-400 hover:bg-gray-100 hover:text-gray-500 dark:text-gray-400/70 dark:hover:bg-gray-700 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:group-hover:visible md:group-[.final-completion]:visible"
|
<Tooltip open={tooltipOpen}>
|
||||||
// onMouseDownCapture={() => {
|
<TooltipTrigger asChild>
|
||||||
// if (audioRef.current) {
|
<button
|
||||||
// audioRef.current.muted = false;
|
className="hover-button rounded-md p-1 pl-0 text-gray-400 hover:text-gray-950 dark:text-gray-400/70 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:group-hover:visible md:group-[.final-completion]:visible"
|
||||||
// }
|
onMouseDownCapture={handleMouseDown}
|
||||||
// handleMouseDown();
|
onMouseUpCapture={handleMouseUp}
|
||||||
// }}
|
onMouseEnter={() => setTooltipOpen(true)}
|
||||||
// onMouseUpCapture={() => {
|
onMouseLeave={() => setTooltipOpen(false)}
|
||||||
// if (audioRef.current) {
|
onClickCapture={() => {
|
||||||
// audioRef.current.muted = false;
|
if (!wasLongPress && audioRef.current) {
|
||||||
// }
|
audioRef.current.muted = false;
|
||||||
// handleMouseUp();
|
toggleSpeech(false);
|
||||||
// }}
|
}
|
||||||
onClickCapture={() => {
|
}}
|
||||||
if (audioRef.current) {
|
type="button"
|
||||||
audioRef.current.muted = false;
|
>
|
||||||
}
|
{renderIcon('19')}
|
||||||
toggleSpeech();
|
</button>
|
||||||
}}
|
</TooltipTrigger>
|
||||||
type="button"
|
<TooltipContent side="bottom" sideOffset={0}>
|
||||||
title={isSpeaking ? localize('com_ui_stop') : localize('com_ui_read_aloud')}
|
<div className="space-y-2">
|
||||||
>
|
{isSpeaking ? (
|
||||||
{renderIcon('19')}
|
<p className="text-center text-sm text-gray-600 dark:text-gray-300">
|
||||||
</button>
|
{localize('com_ui_stop')}
|
||||||
<audio
|
</p>
|
||||||
ref={audioRef}
|
) : (
|
||||||
controls
|
<p className="text-center text-sm text-gray-600 dark:text-gray-300">
|
||||||
controlsList="nodownload nofullscreen noremoteplayback"
|
{localize('com_ui_read_aloud')}
|
||||||
style={{
|
<br />
|
||||||
position: 'absolute',
|
{audioText}
|
||||||
overflow: 'hidden',
|
</p>
|
||||||
display: 'none',
|
)}
|
||||||
height: '0px',
|
</div>
|
||||||
width: '0px',
|
</TooltipContent>
|
||||||
}}
|
</Tooltip>
|
||||||
src={audioRef.current?.src || undefined}
|
<audio
|
||||||
id={`audio-${message.messageId}`}
|
ref={audioRef}
|
||||||
muted
|
controls
|
||||||
autoPlay
|
controlsList="nodownload nofullscreen noremoteplayback"
|
||||||
/>
|
style={{
|
||||||
</>
|
position: 'absolute',
|
||||||
|
overflow: 'hidden',
|
||||||
|
display: 'none',
|
||||||
|
height: '0px',
|
||||||
|
width: '0px',
|
||||||
|
}}
|
||||||
|
src={audioRef.current?.src || undefined}
|
||||||
|
id={`audio-${message.messageId}`}
|
||||||
|
muted
|
||||||
|
autoPlay
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
</TooltipProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ const TooltipContent = React.forwardRef<
|
|||||||
>
|
>
|
||||||
<span className="flex items-center whitespace-pre-wrap px-2 py-1 text-center text-sm font-medium normal-case text-white">
|
<span className="flex items-center whitespace-pre-wrap px-2 py-1 text-center text-sm font-medium normal-case text-white">
|
||||||
{children}
|
{children}
|
||||||
<TooltipArrow className="TooltipArrow" />
|
<TooltipArrow className="TooltipArrow border-gray-700" />
|
||||||
</span>
|
</span>
|
||||||
</TooltipPrimitive.Content>
|
</TooltipPrimitive.Content>
|
||||||
</TooltipPortal>
|
</TooltipPortal>
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ export default function useCustomAudioRef({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleEnded = () => {
|
const handleEnded = () => {
|
||||||
setIsPlaying(false);
|
setIsPlaying(false);
|
||||||
console.log('message audio ended');
|
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
URL.revokeObjectURL(audioRef.current.src);
|
URL.revokeObjectURL(audioRef.current.src);
|
||||||
}
|
}
|
||||||
@@ -17,12 +16,10 @@ export default function useCustomAudioRef({
|
|||||||
|
|
||||||
const handleStart = () => {
|
const handleStart = () => {
|
||||||
setIsPlaying(true);
|
setIsPlaying(true);
|
||||||
console.log('message audio started');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePause = () => {
|
const handlePause = () => {
|
||||||
setIsPlaying(false);
|
setIsPlaying(false);
|
||||||
console.log('message audio paused');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const audioElement = audioRef.current;
|
const audioElement = audioRef.current;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ const useTextToSpeech = (message: TMessage, isLast: boolean, index = 0) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleSpeech = () => {
|
const toggleSpeech = (download: boolean) => {
|
||||||
if (isSpeaking) {
|
if (isSpeaking) {
|
||||||
console.log('canceling message audio speech');
|
console.log('canceling message audio speech');
|
||||||
cancelSpeech();
|
cancelSpeech();
|
||||||
@@ -59,7 +59,7 @@ const useTextToSpeech = (message: TMessage, isLast: boolean, index = 0) => {
|
|||||||
const messageContent = message?.content ?? message?.text ?? '';
|
const messageContent = message?.content ?? message?.text ?? '';
|
||||||
const parsedMessage =
|
const parsedMessage =
|
||||||
typeof messageContent === 'string' ? messageContent : parseTextParts(messageContent);
|
typeof messageContent === 'string' ? messageContent : parseTextParts(messageContent);
|
||||||
generateSpeech(parsedMessage, false);
|
generateSpeech(parsedMessage, download);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ function useTextToSpeechExternal(messageId: string, isLast: boolean, index = 0)
|
|||||||
});
|
});
|
||||||
|
|
||||||
newAudio.onended = () => {
|
newAudio.onended = () => {
|
||||||
console.log('Cached message audio ended');
|
|
||||||
URL.revokeObjectURL(blobUrl);
|
URL.revokeObjectURL(blobUrl);
|
||||||
setIsSpeaking(false);
|
setIsSpeaking(false);
|
||||||
};
|
};
|
||||||
@@ -100,7 +99,9 @@ function useTextToSpeechExternal(messageId: string, isLast: boolean, index = 0)
|
|||||||
const blobUrl = URL.createObjectURL(audioBlob);
|
const blobUrl = URL.createObjectURL(audioBlob);
|
||||||
if (downloadFile) {
|
if (downloadFile) {
|
||||||
downloadAudio(blobUrl);
|
downloadAudio(blobUrl);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
autoPlayAudio(blobUrl);
|
autoPlayAudio(blobUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast({
|
showToast({
|
||||||
|
|||||||
@@ -293,6 +293,9 @@ export default {
|
|||||||
com_ui_use_micrphone: 'Use microphone',
|
com_ui_use_micrphone: 'Use microphone',
|
||||||
com_ui_min_tags: 'Cannot remove more values, a minimum of {0} are required.',
|
com_ui_min_tags: 'Cannot remove more values, a minimum of {0} are required.',
|
||||||
com_ui_max_tags: 'Maximum number allowed is {0}, using latest values.',
|
com_ui_max_tags: 'Maximum number allowed is {0}, using latest values.',
|
||||||
|
com_ui_hold_mouse_download: 'Hold for {0} more seconds to download the audio',
|
||||||
|
com_ui_info_read_aloud: 'Hold click 3 seconds to download',
|
||||||
|
com_ui_downloading: 'Downloading...',
|
||||||
com_ui_bookmarks: 'Bookmarks',
|
com_ui_bookmarks: 'Bookmarks',
|
||||||
com_ui_bookmarks_rebuild: 'Rebuild',
|
com_ui_bookmarks_rebuild: 'Rebuild',
|
||||||
com_ui_bookmarks_new: 'New Bookmark',
|
com_ui_bookmarks_new: 'New Bookmark',
|
||||||
|
|||||||
Reference in New Issue
Block a user