docs(ai-chat-composer): update voice interaction demo
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
<template>
|
||||
<div class="demo">
|
||||
<p
|
||||
class="voice-placeholder"
|
||||
:class="{ hidden: voiceState !== 'idle' }"
|
||||
v-text="'长按下方输入框开始语音输入'"
|
||||
/>
|
||||
<ai-chat-composer
|
||||
v-model="draft"
|
||||
:is-generating="generating"
|
||||
@@ -9,6 +14,7 @@
|
||||
@send="sendMessage"
|
||||
@voice-start="onVoiceStart"
|
||||
@voice-stop="onVoiceStop"
|
||||
@voice-keep="onVoiceKeep"
|
||||
@voice-cancel="onVoiceCancel"
|
||||
/>
|
||||
|
||||
@@ -81,15 +87,16 @@ let recognition: SpeechRecognitionLike | undefined;
|
||||
let finalText = "";
|
||||
let mockStepTimer: number | undefined;
|
||||
let mockFinishTimer: number | undefined;
|
||||
let sendOnFinish = true;
|
||||
|
||||
const hint = computed(() => {
|
||||
if (mockEnabled.value) {
|
||||
return "模拟模式:按住输入框,松手发送,按住上滑取消。";
|
||||
return "模拟模式:按住输入框说话,松手发送;上滑到按钮可选择取消或保留。";
|
||||
}
|
||||
if (!supported.value) {
|
||||
return "当前浏览器不支持语音识别,已切换为模拟。";
|
||||
}
|
||||
return "长按输入框说话,松手发送;按住上滑可取消。";
|
||||
return "长按输入框说话,松手发送;上滑到「取消发送」或「保留识别」按钮释放。";
|
||||
});
|
||||
|
||||
/** 获取浏览器实现;Chrome 与 Edge 需要 webkit 前缀。 */
|
||||
@@ -115,6 +122,7 @@ function sendMessage(message: string): void {
|
||||
push("没有识别到内容,请再试一次");
|
||||
return;
|
||||
}
|
||||
draft.value = "";
|
||||
generating.value = true;
|
||||
push(`send("${content}")`);
|
||||
setTimeout(() => {
|
||||
@@ -126,6 +134,7 @@ function sendMessage(message: string): void {
|
||||
function onVoiceStart(): void {
|
||||
voiceText.value = "";
|
||||
finalText = "";
|
||||
sendOnFinish = true;
|
||||
if (mockEnabled.value || !supported.value) {
|
||||
startMock();
|
||||
return;
|
||||
@@ -153,6 +162,16 @@ function onVoiceStart(): void {
|
||||
}
|
||||
|
||||
function onVoiceStop(): void {
|
||||
sendOnFinish = true;
|
||||
stopVoice();
|
||||
}
|
||||
|
||||
function onVoiceKeep(): void {
|
||||
sendOnFinish = false;
|
||||
stopVoice();
|
||||
}
|
||||
|
||||
function stopVoice(): void {
|
||||
if (voiceState.value !== "listening") {
|
||||
return;
|
||||
}
|
||||
@@ -161,23 +180,24 @@ function onVoiceStop(): void {
|
||||
return;
|
||||
}
|
||||
if (!recognition) {
|
||||
resetVoice();
|
||||
finishVoice();
|
||||
return;
|
||||
}
|
||||
voiceState.value = "processing";
|
||||
try {
|
||||
recognition.stop();
|
||||
} catch {
|
||||
finishVoice(voiceText.value);
|
||||
finishVoice();
|
||||
}
|
||||
}
|
||||
|
||||
function onVoiceCancel(): void {
|
||||
push("已取消语音输入");
|
||||
draft.value = "";
|
||||
resetVoice();
|
||||
}
|
||||
|
||||
/** 实时拼接最终结果与临时结果,喂给组件的语音面板。 */
|
||||
/** 实时拼接最终结果与临时结果,组件会同步到输入框。 */
|
||||
function handleResult(event: SpeechResultEvent): void {
|
||||
let interim = "";
|
||||
for (let index = event.resultIndex; index < event.results.length; index += 1) {
|
||||
@@ -204,25 +224,34 @@ function handleError(event: SpeechErrorEvent): void {
|
||||
"no-speech": "没有听到声音,请再试一次"
|
||||
};
|
||||
push(messages[event.error] ?? `语音识别失败:${event.error}`);
|
||||
draft.value = "";
|
||||
resetVoice();
|
||||
}
|
||||
|
||||
function handleEnd(): void {
|
||||
recognition = undefined;
|
||||
voiceState.value = "processing";
|
||||
finishVoice(voiceText.value);
|
||||
finishVoice();
|
||||
}
|
||||
|
||||
function finishVoice(text: string): void {
|
||||
const message = text.trim();
|
||||
function finishVoice(): void {
|
||||
const message = voiceText.value.trim();
|
||||
const shouldSend = sendOnFinish;
|
||||
resetVoice();
|
||||
if (!shouldSend) {
|
||||
if (message) {
|
||||
draft.value = message;
|
||||
}
|
||||
push("已保留识别结果,可继续编辑");
|
||||
return;
|
||||
}
|
||||
sendMessage(message);
|
||||
}
|
||||
|
||||
function startMock(): void {
|
||||
voiceState.value = "listening";
|
||||
voiceText.value = "";
|
||||
let step = 0;
|
||||
voiceText.value = MOCK_STEPS[0];
|
||||
let step = 1;
|
||||
mockStepTimer = window.setInterval(() => {
|
||||
voiceText.value = MOCK_STEPS[Math.min(step, MOCK_STEPS.length - 1)];
|
||||
step += 1;
|
||||
@@ -237,7 +266,7 @@ function stopMock(): void {
|
||||
voiceState.value = "processing";
|
||||
mockFinishTimer = window.setTimeout(() => {
|
||||
mockFinishTimer = undefined;
|
||||
finishVoice(voiceText.value || MOCK_STEPS[0]);
|
||||
finishVoice();
|
||||
}, 600);
|
||||
}
|
||||
|
||||
@@ -266,6 +295,7 @@ function resetVoice(): void {
|
||||
voiceState.value = "idle";
|
||||
voiceText.value = "";
|
||||
finalText = "";
|
||||
sendOnFinish = true;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
@@ -280,6 +310,22 @@ onBeforeUnmount(() => {
|
||||
|
||||
<style lang="less" scoped>
|
||||
.demo {
|
||||
.voice-placeholder {
|
||||
display: flex;
|
||||
height: 5.5rem;
|
||||
margin: 0 0 .5rem;
|
||||
color: var(--vp-c-text-3);
|
||||
font-size: .75rem;
|
||||
border: 1px dashed var(--vp-c-divider);
|
||||
border-radius: 1rem;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
&.hidden {
|
||||
visibility: hidden;
|
||||
}
|
||||
}
|
||||
|
||||
.bar {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
@@ -289,10 +335,12 @@ onBeforeUnmount(() => {
|
||||
|
||||
.mock {
|
||||
gap: .25rem;
|
||||
flex: none;
|
||||
display: inline-flex;
|
||||
font-size: .75rem;
|
||||
cursor: pointer;
|
||||
color: var(--vp-c-text-2);
|
||||
white-space: nowrap;
|
||||
align-items: center;
|
||||
|
||||
input {
|
||||
|
||||
Reference in New Issue
Block a user