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

Add verbal commands (send, clear, stop) #72

Open
wants to merge 4 commits into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 23 additions & 32 deletions src/components/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,15 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
case 'Amazon Polly':
language = speech.pollyLanguage;
voiceName = speech.pollyVoice;
region = existEnvironmentVariable('AWS_REGION')
? getEnvironmentVariable('AWS_REGION')
: key.awsRegion;
accessKeyId = existEnvironmentVariable('AWS_ACCESS_KEY_ID')
? getEnvironmentVariable('AWS_ACCESS_KEY_ID')
: key.awsKeyId;
secretAccessKey = existEnvironmentVariable('AWS_ACCESS_KEY')
? getEnvironmentVariable('AWS_ACCESS_KEY')
: key.awsKey;
region = getEnvironmentVariable('AWS_REGION', key.awsRegion);
accessKeyId = getEnvironmentVariable('AWS_ACCESS_KEY_ID', key.awsKeyId);
secretAccessKey = getEnvironmentVariable('AWS_ACCESS_KEY', key.awsKey);
break;
case 'Azure TTS':
language = speech.azureLanguage;
voiceName = speech.azureVoice;
region = existEnvironmentVariable('AZURE_REGION')
? getEnvironmentVariable('AZURE_REGION')
: key.azureRegion;
secretAccessKey = existEnvironmentVariable('AZURE_KEY')
? getEnvironmentVariable('AZURE_KEY')
: key.azureKey;
region = getEnvironmentVariable('AZURE_REGION', key.azureRegion);
secretAccessKey = getEnvironmentVariable('AZURE_KEY', key.azureKey);
break;
}

Expand Down Expand Up @@ -188,12 +178,24 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
});
};

// user spoke, speech->text
useEffect(() => {
if (transcript.length !== 0 && transcript !== 'undefined') {
setInput(prevInput => prevInput + ' ' + transcript);
let trimmed = transcript.trim();
if (trimmed === 'clear') {
setInput('');
setTranscript('');
} else if (trimmed === 'stop') {
stopRecording();
} else if (trimmed === 'send') {
handleSend();
} else {
setInput(prevInput => prevInput + ' ' + transcript);
}
}
}, [transcript]);

// gpt responded, text->speech
useEffect(() => {
if (response.length !== 0 && response !== 'undefined') {
setSendMessages(false);
Expand All @@ -217,15 +219,9 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
conversationsToSent = conversationsToSent.slice(chat.maxMessages * -1);
conversationsToSent.unshift({ role: 'system', content: chat.systemRole });
console.log(conversationsToSent);
const openaiApiKey = existEnvironmentVariable('OPENAI_API_KEY')
? getEnvironmentVariable('OPENAI_API_KEY')
: key.openaiApiKey;
const openaiApiHost = existEnvironmentVariable('OPENAI_HOST')
? getEnvironmentVariable('OPENAI_HOST')
: key.openaiHost;
const openaiApiModel = existEnvironmentVariable('OPENAI_MODEL')
? getEnvironmentVariable('OPENAI_MODEL')
: key.openaiModel;
const openaiApiKey = getEnvironmentVariable('OPENAI_API_KEY', key.openaiApiKey);
const openaiApiHost = getEnvironmentVariable('OPENAI_HOST', key.openaiHost);
const openaiApiModel = getEnvironmentVariable('OPENAI_MODEL', key.openaiModel);

if (existEnvironmentVariable('ACCESS_CODE')) {
const accessCode = getEnvironmentVariable('ACCESS_CODE');
Expand Down Expand Up @@ -301,7 +297,6 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
};
setSendMessages(true);
chatDB.chat.add(input_json);

setInput('');
if (!isMobile) {
focusInput();
Expand Down Expand Up @@ -467,14 +462,10 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
{voice.service == 'Azure Speech to Text' && (
<AzureSpeechToText
subscriptionKey={
existEnvironmentVariable('AZURE_KEY')
? getEnvironmentVariable('AZURE_KEY')
: key.azureKey
getEnvironmentVariable('AZURE_KEY', key.azureKey)
}
region={
existEnvironmentVariable('AZURE_REGION')
? getEnvironmentVariable('AZURE_REGION')
: key.azureRegion
getEnvironmentVariable('AZURE_REGION', key.azureRegion)
}
language={voice.azureLanguage}
isListening={isListening}
Expand Down
15 changes: 10 additions & 5 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ export function absolutifyLink(rel: string): string {
return anchor.href;
}

export function getEnvironmentVariable(name: string): string {
const environment_variable_name = 'VITE_' + name;
return import.meta.env[environment_variable_name];
export function getEnvironmentKey(name: string): string {
return 'VITE_' + name;
}

export function getEnvironmentVariable(name: string, backup: string): string {
const environment_variable_name = getEnvironmentKey(name);
return existEnvironmentVariable(name) ? import.meta.env[environment_variable_name] : backup;
}

export function existEnvironmentVariable(name: string): boolean {
const environment_variable_name = 'VITE_' + name;
return import.meta.env[environment_variable_name] !== 'REPLACE_WITH_YOUR_OWN';
const environment_variable_name = getEnvironmentKey(name);
return import.meta.env[environment_variable_name] !== 'REPLACE_WITH_YOUR_OWN' &&
import.meta.env[environment_variable_name] !== undefined;
}

export function getFormatDateTime(isoDateTime: string): any {
Expand Down