Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Web Speech API #661

Open
wants to merge 4 commits into
base: v2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api/elevenlabs/speech/route.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const runtime = 'edge';
export { elevenLabsHandler as POST } from '~/modules/elevenlabs/elevenlabs.server';
export { elevenLabsHandler as POST } from '~/modules/tts/vendors/elevenlabs/elevenlabs.server';
5 changes: 3 additions & 2 deletions pages/info/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { ROUTE_APP_CHAT, ROUTE_INDEX } from '~/common/app.routes';
import { Release } from '~/common/app.release';

// capabilities access
import { useCapabilityBrowserSpeechRecognition, useCapabilityElevenLabs, useCapabilityTextToImage } from '~/common/components/useCapabilities';
import { useCapabilityBrowserSpeechRecognition, useCapabilityTextToImage } from '~/common/components/useCapabilities';
import { useTTSCapability } from '~/modules/tts/tts.client.hooks';

// stores access
import { getLLMsDebugInfo } from '~/common/stores/llms/store-llms';
Expand Down Expand Up @@ -95,7 +96,7 @@ function AppDebug() {
const cProduct = {
capabilities: {
mic: useCapabilityBrowserSpeechRecognition(),
elevenLabs: useCapabilityElevenLabs(),
elevenLabs: useTTSCapability(),
textToImage: useCapabilityTextToImage(),
},
models: getLLMsDebugInfo(),
Expand Down
6 changes: 4 additions & 2 deletions src/apps/call/CallWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import WarningRoundedIcon from '@mui/icons-material/WarningRounded';
import { animationColorRainbow } from '~/common/util/animUtils';
import { navigateBack } from '~/common/app.routes';
import { optimaOpenPreferences } from '~/common/layout/optima/useOptima';
import { useCapabilityBrowserSpeechRecognition, useCapabilityElevenLabs } from '~/common/components/useCapabilities';
import { useCapabilityBrowserSpeechRecognition } from '~/common/components/useCapabilities';
import { useTTSCapability } from '~/modules/tts/tts.client.hooks';
import { useChatStore } from '~/common/stores/chat/store-chats';
import { useUICounter } from '~/common/state/store-ui';



function StatusCard(props: { icon: React.JSX.Element, hasIssue: boolean, text: string, button?: React.JSX.Element }) {
return (
<Card sx={{ width: '100%' }}>
Expand Down Expand Up @@ -45,7 +47,7 @@ export function CallWizard(props: { strict?: boolean, conversationId: string | n

// external state
const recognition = useCapabilityBrowserSpeechRecognition();
const synthesis = useCapabilityElevenLabs();
const synthesis = useTTSCapability();
const chatIsEmpty = useChatStore(state => {
if (!props.conversationId)
return false;
Expand Down
29 changes: 19 additions & 10 deletions src/apps/call/Telephone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { ScrollToBottom } from '~/common/scroll-to-bottom/ScrollToBottom';
import { ScrollToBottomButton } from '~/common/scroll-to-bottom/ScrollToBottomButton';
import { useChatLLMDropdown } from '../chat/components/layout-bar/useLLMDropdown';

import { EXPERIMENTAL_speakTextStream } from '~/modules/elevenlabs/elevenlabs.client';
import { EXPERIMENTAL_speakTextStream } from '~/modules/tts/tts.client';
import { SystemPurposeId, SystemPurposes } from '../../data';
import { llmStreamingChatGenerate, VChatMessageIn } from '~/modules/llms/llm.client';
import { useElevenLabsVoiceDropdown } from '~/modules/elevenlabs/useElevenLabsVoiceDropdown';
import { TTSSetting } from '~/modules/tts/tts.setting';

import type { OptimaBarControlMethods } from '~/common/layout/optima/bar/OptimaBarDropdown';
import { AudioPlayer } from '~/common/util/audio/AudioPlayer';
Expand All @@ -39,6 +39,7 @@ import { CallStatus } from './components/CallStatus';
import { useAppCallStore } from './state/store-app-call';



function CallMenuItems(props: {
pushToTalk: boolean,
setPushToTalk: (pushToTalk: boolean) => void,
Expand All @@ -48,8 +49,7 @@ function CallMenuItems(props: {

// external state
const { grayUI, toggleGrayUI } = useAppCallStore();
const { voicesDropdown } = useElevenLabsVoiceDropdown(false, !props.override);


const handlePushToTalkToggle = () => props.setPushToTalk(!props.pushToTalk);

const handleChangeVoiceToggle = () => props.setOverride(!props.override);
Expand All @@ -68,10 +68,10 @@ function CallMenuItems(props: {
<Switch checked={props.override} onChange={handleChangeVoiceToggle} sx={{ ml: 'auto' }} />
</MenuItem>

<MenuItem>
<ListItemDecorator>{' '}</ListItemDecorator>
{voicesDropdown}
<MenuItem sx={{flexWrap: 'wrap'}}>
<TTSSetting />
</MenuItem>


<ListDivider />

Expand Down Expand Up @@ -245,13 +245,22 @@ export function Telephone(props: {
// perform completion
responseAbortController.current = new AbortController();
let finalText = '';
let currentSentence = '';
let error: any | null = null;
setPersonaTextInterim('💭...');
llmStreamingChatGenerate(chatLLMId, callPrompt, 'call', callMessages[0].id, null, null, responseAbortController.current.signal, ({ textSoFar }) => {
const text = textSoFar?.trim();
if (text) {
finalText = text;
setPersonaTextInterim(text);

// Maintain and say the current sentence
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this.

if (/[.,!?]$/.test(text)) {
currentSentence = text.substring(finalText?.length)
finalText = text
if (currentSentence?.length >= 1)
void EXPERIMENTAL_speakTextStream(currentSentence, personaVoiceId);
}
currentSentence = text.substring(finalText?.length) // to be added to the final text
}
}).catch((err: DOMException) => {
if (err?.name !== 'AbortError')
Expand All @@ -261,8 +270,8 @@ export function Telephone(props: {
if (finalText || error)
setCallMessages(messages => [...messages, createDMessageTextContent('assistant', finalText + (error ? ` (ERROR: ${error.message || error.toString()})` : ''))]); // [state] append assistant:call_response
// fire/forget
if (finalText?.length >= 1)
void EXPERIMENTAL_speakTextStream(finalText, personaVoiceId);
if (currentSentence?.length >= 1)
void EXPERIMENTAL_speakTextStream(currentSentence, personaVoiceId);
});

return () => {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chat/AppChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FlattenerModal } from '~/modules/aifn/flatten/FlattenerModal';
import { TradeConfig, TradeModal } from '~/modules/trade/TradeModal';
import { downloadSingleChat, importConversationsFromFilesAtRest, openConversationsAtRestPicker } from '~/modules/trade/trade.client';
import { imaginePromptFromTextOrThrow } from '~/modules/aifn/imagine/imaginePromptFromText';
import { speakText } from '~/modules/elevenlabs/elevenlabs.client';
import { speakText } from '~/modules/tts/tts.client';
import { useAreBeamsOpen } from '~/modules/beam/store-beam.hooks';
import { useCapabilityTextToImage } from '~/modules/t2i/t2i.client';

Expand Down
5 changes: 3 additions & 2 deletions src/apps/chat/components/ChatMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getConversation, useChatStore } from '~/common/stores/chat/store-chats'
import { openFileForAttaching } from '~/common/components/ButtonAttachFiles';
import { optimaOpenPreferences } from '~/common/layout/optima/useOptima';
import { useBrowserTranslationWarning } from '~/common/components/useIsBrowserTranslating';
import { useCapabilityElevenLabs } from '~/common/components/useCapabilities';
import { useTTSCapability } from '~/modules/tts/tts.client.hooks';
import { useChatOverlayStore } from '~/common/chat-overlay/store-perchat_vanilla';
import { useScrollToBottom } from '~/common/scroll-to-bottom/useScrollToBottom';

Expand All @@ -30,6 +30,7 @@ import { PersonaSelector } from './persona-selector/PersonaSelector';
import { useChatAutoSuggestHTMLUI, useChatShowSystemMessages } from '../store-app-chat';



const stableNoMessages: DMessage[] = [];

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ export function ChatMessageList(props: {
_composerInReferenceToCount: state.inReferenceTo?.length ?? 0,
ephemerals: state.ephemerals?.length ? state.ephemerals : null,
})));
const { mayWork: isSpeakable } = useCapabilityElevenLabs();
const { mayWork: isSpeakable } = useTTSCapability();

// derived state
const { conversationHandler, conversationId, capabilityHasT2I, onConversationBranch, onConversationExecuteHistory, onTextDiagram, onTextImagine, onTextSpeak } = props;
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chat/editors/persona/PersonaChatMessageSpeak.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { speakText } from '~/modules/elevenlabs/elevenlabs.client';
import { speakText } from '~/modules/tts/tts.client';

import { isTextContentFragment } from '~/common/stores/chat/chat.fragments';

Expand Down
10 changes: 10 additions & 0 deletions src/apps/chat/store-app-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { persist } from 'zustand/middleware';
import { useShallow } from 'zustand/react/shallow';

import type { DLLMId } from '~/common/stores/llms/llms.types';
import { ASREngineKey, ASREngineList } from '~/modules/asr/asr.client';


export type ChatAutoSpeakType = 'off' | 'firstLine' | 'all';
Expand Down Expand Up @@ -51,6 +52,9 @@ interface AppChatStore {
micTimeoutMs: number;
setMicTimeoutMs: (micTimeoutMs: number) => void;

ASREngine: ASREngineKey;
setASREngine: (ASREngine: ASREngineKey) => void;

showPersonaIcons: boolean;
setShowPersonaIcons: (showPersonaIcons: boolean) => void;

Expand Down Expand Up @@ -114,6 +118,9 @@ const useAppChatStore = create<AppChatStore>()(persist(
micTimeoutMs: 2000,
setMicTimeoutMs: (micTimeoutMs: number) => _set({ micTimeoutMs }),

ASREngine: ASREngineList[0].key,
setASREngine: (ASREngine: ASREngineKey) => _set({ ASREngine }),

showPersonaIcons: true,
setShowPersonaIcons: (showPersonaIcons: boolean) => _set({ showPersonaIcons }),

Expand Down Expand Up @@ -198,6 +205,9 @@ export const useChatMicTimeoutMsValue = (): number =>
export const useChatMicTimeoutMs = (): [number, (micTimeoutMs: number) => void] =>
useAppChatStore(useShallow(state => [state.micTimeoutMs, state.setMicTimeoutMs]));

export const useASREngine = (): [ASREngineKey, (ASREngine: ASREngineKey) => void] =>
useAppChatStore(useShallow(state => [state.ASREngine, state.setASREngine]));

export const useChatDrawerFilters = () => {
const values = useAppChatStore(useShallow(state => ({
filterHasDocFragments: state.filterHasDocFragments,
Expand Down
10 changes: 7 additions & 3 deletions src/apps/settings-modal/SettingsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import WarningRoundedIcon from '@mui/icons-material/WarningRounded';

import { BrowseSettings } from '~/modules/browse/BrowseSettings';
import { DallESettings } from '~/modules/t2i/dalle/DallESettings';
import { ElevenlabsSettings } from '~/modules/elevenlabs/ElevenlabsSettings';
import { GoogleSearchSettings } from '~/modules/google/GoogleSearchSettings';
import { ProdiaSettings } from '~/modules/t2i/prodia/ProdiaSettings';
import { T2ISettings } from '~/modules/t2i/T2ISettings';
Expand All @@ -22,6 +21,9 @@ import { AppChatSettingsAI } from './AppChatSettingsAI';
import { AppChatSettingsUI } from './settings-ui/AppChatSettingsUI';
import { UxLabsSettings } from './UxLabsSettings';
import { VoiceSettings } from './VoiceSettings';
import { useTTSEngine } from '~/modules/tts/useTTSStore';
import { TTSSetting } from '~/modules/tts/tts.setting';
import { getName as getTTSEngineName } from '~/modules/tts/tts.client';


// styled <AccordionGroup variant='plain'> into a Topics component
Expand Down Expand Up @@ -122,6 +124,8 @@ export function SettingsModal(props: {
// external state
const isMobile = useIsMobile();

const [TTSEngine] = useTTSEngine()

// handlers

const { setTab } = props;
Expand Down Expand Up @@ -193,8 +197,8 @@ export function SettingsModal(props: {
<Topic icon='🎙️' title='Voice settings'>
<VoiceSettings />
</Topic>
<Topic icon='📢' title='ElevenLabs API'>
<ElevenlabsSettings />
<Topic icon='📢' title={getTTSEngineName()}>
<TTSSetting />
</Topic>
</Topics>
</TabPanel>
Expand Down
Loading