Commit dff91c47 authored by ali's avatar ali

fix: 异常处理

parent 52ba82c4
......@@ -509,9 +509,9 @@ export class PhotoRole extends EventEmitter {
}
async destroy() {
this._liveTaskQueue.length = 0;
await this._stopLive(this.sessionId);
this._liveStatus = 'closing';
this._liveTaskQueue.length = 0
await this._stopLive(this.sessionId)
this._liveStatus = 'closing'
// this.answerArgs = null;
// this._webRTCContainer && document.body.removeChild(this._webRTCContainer)
this._rtc?.stopPlay()
......
......@@ -49,6 +49,13 @@ onMounted(() => {
document.body.style.overflow = 'hidden'
})
const errorSnackbar = ref<boolean>(false)
const errorMsg = ref<string>('');
const showError = (msg: string) => {
errorSnackbar.value = true;
errorMsg.value = msg;
}
function loadImg(): Promise<HTMLImageElement> {
const img = new Image()
img.src = url
......@@ -371,7 +378,7 @@ async function endAudioInput() {
}
inputContext.asrPartial = ''
inputContext.answerArray.length = 0
await photoRole?.destroy();
await photoRole?.destroy()
}
const checkSteps = async () => {
......@@ -411,15 +418,21 @@ async function onQ(question: string) {
microphoneState.value = 'loading'
const ws = await initLLMSocket()
inputContext.ws = ws
const { pose, stepResolve, stepReject } = createStep()
const messageTimeout = setTimeout(async () => {
showError('llm:timeout!')
await endAudioInput()
microphoneState.value = 'waitInput'
}, 5000);
let sliceAnswer = ''
let answer = ''
let isTime = true
let sliceAnswerLength = 10
inputContext.ws = ws
inputContext.answerArray.length = 0
inputContext.steps.length = 0
const { pose, stepResolve, stepReject } = createStep()
inputContext.steps.push(pose)
photoRole!.answerArgs = new PhotoAnswer()
......@@ -446,6 +459,7 @@ async function onQ(question: string) {
photoRole!.answerArgs!._typingAnswer.push(answer)
isTime && console.time('sliceAnswer')
isTime = false
clearTimeout(messageTimeout);
const textArr = text.split('')
for (let i = 0; i < textArr.length; i++) {
......@@ -461,6 +475,10 @@ async function onQ(question: string) {
}
}
} catch (error) {
showError('llm:'+ error)
endAudioInput().then(() => {
microphoneState.value = 'waitInput'
})
stepReject(JSON.stringify(error))
}
}
......@@ -507,6 +525,10 @@ async function runTTSTask() {
})
}
} catch (error) {
showError('tts:'+ error)
endAudioInput().then(() => {
microphoneState.value = 'waitInput'
})
stepReject(JSON.stringify(error))
}
......@@ -552,7 +574,14 @@ async function down() {
await endAudioInput()
}
startVoskWsAudioInput()
try {
await startVoskWsAudioInput()
} catch (error) {
showError('start:'+ error)
endAudioInput().then(() => {
microphoneState.value = 'waitInput'
})
}
}
</script>
......@@ -578,10 +607,7 @@ async function down() {
color="#fff"
variant="elevated"
size="x-large"
:disabled="
microphoneState === 'loading' ||
microphoneState === 'disabled'
"
:disabled="microphoneState === 'loading' || microphoneState === 'disabled'"
@pointerdown="down"
>
<v-icon v-if="microphoneState === 'waitInput'" icon="mdi-microphone"></v-icon>
......@@ -604,6 +630,24 @@ async function down() {
</template>
</v-btn>
</div>
<v-snackbar
v-model="errorSnackbar"
multi-line
:timeout="3000"
>
{{ errorMsg }}
<template #actions>
<v-btn
color="red"
variant="text"
@click="errorSnackbar = false"
>
Close
</v-btn>
</template>
</v-snackbar>
</template>
<style scoped>
.voice {
......
......@@ -47,6 +47,13 @@ router.beforeEach((g) => {
if (!g.query.url) return router.push('/error')
})
const errorSnackbar = ref<boolean>(false)
const errorMsg = ref<string>('');
const showError = (msg: string) => {
errorSnackbar.value = true;
errorMsg.value = msg;
}
async function initVosk({
result,
partialResult
......@@ -160,11 +167,11 @@ async function startVoskWsAudioInput() {
if (microphoneState.value === 'loading') return
if (microphoneState.value === 'input') {
endAudioInput()
await endAudioInput()
return
}
initVoskWS()
await initVoskWS()
sampleRate = 16000
const mediaStream = await navigator.mediaDevices.getUserMedia({
audio: {
......@@ -315,8 +322,10 @@ async function onQ(question: string) {
if (!role) return
microphoneState.value = 'loading'
if (await qNLP(question)) {
loadVideo(url)
try {
const nlpUrl = await qNLP(question)
if (nlpUrl) {
loadVideo(nlpUrl)
microphoneState.value = 'reply'
const videoEle = videos[1].value
videoEle!.loop = false
......@@ -328,12 +337,22 @@ async function onQ(question: string) {
}
return
}
} catch (error) {
microphoneState.value = 'input'
showError(`nlp:${error}`)
return
}
// 视频链接匹配不上,直接走大模型
const ws = await initLLMSocket()
const answerArray: string[] = []
const messageTimeout = setTimeout(() => {
showError('llm:timeout!')
endAudioInput()
microphoneState.value = 'waitInput'
}, 5000);
let sliceAnswer = ''
let answer = ''
const answerArray: string[] = []
let isTime = true
let sliceAnswerLength = 10
inputContext.ws = ws
......@@ -365,6 +384,7 @@ async function onQ(question: string) {
answer += text
isTime && console.time('sliceAnswer')
isTime = false
clearTimeout(messageTimeout);
const textArr = text.split('')
for (let i = 0; i < textArr.length; i++) {
......@@ -380,8 +400,9 @@ async function onQ(question: string) {
}
}
} catch (error) {
console.log('返回答案错误 -----> ' + JSON.stringify(error))
microphoneState.value = 'input'
console.error(error);
showError(`message:${error}`)
microphoneState.value = 'waitInput'
}
}
......@@ -423,7 +444,8 @@ async function runTTSTask(tasks: string[]) {
runAudioPlay()
}
} catch (error) {
microphoneState.value = 'input'
showError(`tts:${error}`)
microphoneState.value = 'waitInput'
console.error(error)
}
......@@ -470,12 +492,16 @@ async function xfTTS(text: string) {
console.log('----------------> tts:', res)
}
function down() {
async function down() {
if (microphoneState.value === 'reply') {
endAudioInput()
}
startVoskWsAudioInput()
try {
await startVoskWsAudioInput()
} catch (error) {
showError(`${error}`)
}
}
</script>
......@@ -533,6 +559,24 @@ function down() {
{{ item.q }}
</v-chip>
</div>
<v-snackbar
v-model="errorSnackbar"
multi-line
:timeout="3000"
>
{{ errorMsg }}
<template #actions>
<v-btn
color="red"
variant="text"
@click="errorSnackbar = false"
>
Close
</v-btn>
</template>
</v-snackbar>
</template>
<style scoped>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment