fix: error message do not show in tts
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { pcmToWav } from '@/utils/pcm-to-wav';
|
import { pcmToWav } from '@/utils/pcm-to-wav';
|
||||||
import { useCallback, useRef, useState } from 'react';
|
import { useMemoizedFn } from 'ahooks';
|
||||||
|
import { useRef, useState } from 'react';
|
||||||
import { textToSpeech } from '../../apis';
|
import { textToSpeech } from '../../apis';
|
||||||
import { extractErrorMessage } from '../../config';
|
import { extractErrorMessage } from '../../config';
|
||||||
|
|
||||||
@@ -51,75 +52,72 @@ export const useNonStreamTTS = (params?: UseNonStreamTTSParams) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const generate = useCallback(
|
const generate = useMemoizedFn(async (ttsParams: TTSParams) => {
|
||||||
async (ttsParams: TTSParams) => {
|
try {
|
||||||
try {
|
setLoading(true);
|
||||||
setLoading(true);
|
setError(null);
|
||||||
setError(null);
|
|
||||||
|
|
||||||
// Abort previous request if exists
|
// Abort previous request if exists
|
||||||
controllerRef.current?.abort();
|
controllerRef.current?.abort();
|
||||||
controllerRef.current = new AbortController();
|
controllerRef.current = new AbortController();
|
||||||
const signal = controllerRef.current.signal;
|
const signal = controllerRef.current.signal;
|
||||||
|
|
||||||
const res: any = await textToSpeech({
|
const res: any = await textToSpeech({
|
||||||
data: ttsParams,
|
data: ttsParams,
|
||||||
signal
|
signal
|
||||||
|
});
|
||||||
|
if (!res?.audioBlob || res?.error) {
|
||||||
|
const errorMessage = extractErrorMessage(res);
|
||||||
|
setError({
|
||||||
|
error: true,
|
||||||
|
errorMessage
|
||||||
});
|
});
|
||||||
|
params?.onError?.(errorMessage);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if ((res?.status_code && res?.status_code !== 200) || res?.error) {
|
let result = {
|
||||||
const errorMessage = extractErrorMessage(res);
|
url: '',
|
||||||
setError({
|
type: ''
|
||||||
error: true,
|
};
|
||||||
errorMessage
|
|
||||||
});
|
|
||||||
params?.onError?.(errorMessage);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = {
|
if (res.audioBlob?.type?.indexOf('audio') === -1) {
|
||||||
|
result = {
|
||||||
url: '',
|
url: '',
|
||||||
type: ''
|
type: ''
|
||||||
};
|
};
|
||||||
|
} else if (
|
||||||
if (res.audioBlob?.type?.indexOf('audio') === -1) {
|
ttsParams.response_format === 'pcm' ||
|
||||||
result = {
|
isPCMFormat(res.audioBlob)
|
||||||
url: '',
|
) {
|
||||||
type: ''
|
result = await convertPCMToWav(res.audioBlob);
|
||||||
};
|
} else {
|
||||||
} else if (
|
result = await generateAudioUrl(res.audioBlob);
|
||||||
ttsParams.response_format === 'pcm' ||
|
|
||||||
isPCMFormat(res.audioBlob)
|
|
||||||
) {
|
|
||||||
result = await convertPCMToWav(res.audioBlob);
|
|
||||||
} else {
|
|
||||||
result = await generateAudioUrl(res.audioBlob);
|
|
||||||
}
|
|
||||||
|
|
||||||
params?.onSuccess?.(result);
|
|
||||||
return res;
|
|
||||||
} catch (err: any) {
|
|
||||||
const res = err?.response?.data;
|
|
||||||
if (res?.error) {
|
|
||||||
const errorMessage = extractErrorMessage(res);
|
|
||||||
setError({
|
|
||||||
error: true,
|
|
||||||
errorMessage
|
|
||||||
});
|
|
||||||
params?.onError?.(errorMessage);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
[params]
|
|
||||||
);
|
|
||||||
|
|
||||||
const abort = useCallback(() => {
|
params?.onSuccess?.(result);
|
||||||
|
return res;
|
||||||
|
} catch (err: any) {
|
||||||
|
console.log('TTS error', err);
|
||||||
|
const res = err?.response?.data;
|
||||||
|
if (res?.error) {
|
||||||
|
const errorMessage = extractErrorMessage(res);
|
||||||
|
setError({
|
||||||
|
error: true,
|
||||||
|
errorMessage
|
||||||
|
});
|
||||||
|
params?.onError?.(errorMessage);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const abort = useMemoizedFn(() => {
|
||||||
controllerRef.current?.abort();
|
controllerRef.current?.abort();
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, []);
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
generate,
|
generate,
|
||||||
|
|||||||
@@ -224,6 +224,7 @@ const GroundTTS: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
setPlayingStream(false);
|
setPlayingStream(false);
|
||||||
|
console.error('Error generating TTS:', error);
|
||||||
setTokenResult({
|
setTokenResult({
|
||||||
error: true,
|
error: true,
|
||||||
errorMessage: error?.message || 'Unknown error'
|
errorMessage: error?.message || 'Unknown error'
|
||||||
|
|||||||
Reference in New Issue
Block a user