docs(ai-chat-composer): update voice interaction demo
This commit is contained in:
@@ -12,9 +12,9 @@ demo-preview=../demos/ai-chat-composer/basic.vue
|
|||||||
|
|
||||||
## 语音输入
|
## 语音输入
|
||||||
|
|
||||||
输入框为空且未聚焦时,长按 300ms 进入语音面板;松手发送,按住上滑可取消。组件只负责手势与状态展示,识别能力由业务侧接入。
|
输入框为空且未聚焦时,长按 300ms 进入语音输入:识别文本直接写进输入框,卡片显示主题色呼吸边框,发送按钮变为「松开发送」。松手发送;按住上滑到「取消发送」或「保留识别」按钮后释放,执行对应操作。组件只负责手势与状态展示,识别能力由业务侧接入。
|
||||||
|
|
||||||
:::preview 长按语音输入 || 长按输入框说话,松手发送,按住上滑取消;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」
|
:::preview 长按语音输入 || 长按输入框说话,识别文本实时进入输入框;松手发送,上滑到按钮可取消或只保留文本;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」
|
||||||
|
|
||||||
demo-preview=../demos/ai-chat-composer/voice.vue
|
demo-preview=../demos/ai-chat-composer/voice.vue
|
||||||
|
|
||||||
@@ -23,23 +23,28 @@ demo-preview=../demos/ai-chat-composer/voice.vue
|
|||||||
### 交互约定
|
### 交互约定
|
||||||
|
|
||||||
- 短按(小于 300ms)仍然聚焦文本域;输入框有内容或已聚焦时不接管手势,避免影响编辑与选择
|
- 短按(小于 300ms)仍然聚焦文本域;输入框有内容或已聚焦时不接管手势,避免影响编辑与选择
|
||||||
- 触发后显示语音面板:`voiceText` 有值时优先展示,否则按 `voiceState` 显示 `voiceListeningText` 或 `voiceProcessingText`
|
- 触发后整个输入卡片显示主题色呼吸边框,上方浮出「取消发送」和「保留识别」两个投放区
|
||||||
- 按住上滑超过 48px 后松手触发 `voice-cancel`,面板切换为 `voiceCancelHint`
|
- `voiceText` 的实时结果直接同步到输入框,不再单独占用语音面板
|
||||||
|
- 按住时发送按钮文案切换为 `voiceSendHint`,默认「松开发送」
|
||||||
|
- 松手时指针在「取消发送」上:触发 `voice-cancel`,组件清空本次识别文本
|
||||||
|
- 松手时指针在「保留识别」上:触发 `voice-keep`,停止识别并把文本留在输入框,不发送
|
||||||
|
- 松手时指针不在按钮上:触发 `voice-stop`,由业务侧停止识别、取最终结果并决定发送
|
||||||
|
- 指针被系统取消时同样触发 `voice-cancel`
|
||||||
- `voiceEnabled` 默认关闭,需要显式开启
|
- `voiceEnabled` 默认关闭,需要显式开启
|
||||||
|
|
||||||
`voiceState` 由业务侧驱动:
|
`voiceState` 由业务侧驱动:
|
||||||
|
|
||||||
| 状态 | 含义 |
|
| 状态 | 含义 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `idle` | 未在识别,面板关闭 |
|
| `idle` | 未在识别,语音操作区关闭 |
|
||||||
| `listening` | 正在聆听,实时回写 `voiceText` |
|
| `listening` | 正在聆听,实时回写 `voiceText`,组件同步到输入框 |
|
||||||
| `processing` | 已松手,等待最终识别结果 |
|
| `processing` | 已松手或选择保留,等待最终识别结果 |
|
||||||
|
|
||||||
识别完成或失败后回到 `idle`,面板随之关闭。识别结果不会自动写入输入框或发送,由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceSendHint`、`voiceCancelHint`、`voiceListeningText`、`voiceProcessingText`。
|
识别完成或失败后回到 `idle`,语音操作区随之关闭。组件不会自动发送;正常松手由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceSendHint`、`voiceCancelHint`、`voiceKeepHint`、`voiceCancelText`、`voiceKeepText`、`voiceListeningText`、`voiceProcessingText`。
|
||||||
|
|
||||||
### 组件边界与兼容性
|
### 组件边界与兼容性
|
||||||
|
|
||||||
`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。
|
`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-keep`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。
|
||||||
|
|
||||||
Web Speech API 的实际兼容性(2026-11):
|
Web Speech API 的实际兼容性(2026-11):
|
||||||
|
|
||||||
@@ -68,17 +73,22 @@ function onVoiceStart() {
|
|||||||
|
|
||||||
function onVoiceStop() {
|
function onVoiceStop() {
|
||||||
voiceState.value = "processing";
|
voiceState.value = "processing";
|
||||||
// 停止录音,等待最终结果
|
// 停止录音,等待最终结果;拿到结果后发送
|
||||||
|
}
|
||||||
|
|
||||||
|
function onVoiceKeep() {
|
||||||
|
voiceState.value = "processing";
|
||||||
|
// 停止录音但不发送,最终结果留在输入框
|
||||||
}
|
}
|
||||||
|
|
||||||
function onVoiceCancel() {
|
function onVoiceCancel() {
|
||||||
voiceState.value = "idle";
|
voiceState.value = "idle";
|
||||||
voiceText.value = "";
|
voiceText.value = "";
|
||||||
// 中断录音或识别
|
// 中断录音或识别,丢弃本次结果
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`voiceText` 会实时渲染在语音面板中;面板最多展示两行,长文本建议只传最近一段。识别不可用时退回文字输入,不要阻塞发送。
|
`voiceText` 会实时同步到输入框;识别不可用时退回文字输入,不要阻塞发送。
|
||||||
|
|
||||||
## 何时使用
|
## 何时使用
|
||||||
|
|
||||||
@@ -86,7 +96,7 @@ function onVoiceCancel() {
|
|||||||
|
|
||||||
## 输入区与操作按钮
|
## 输入区与操作按钮
|
||||||
|
|
||||||
文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示纯文本发送或停止按钮。
|
文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示纯文本发送或停止按钮。按住语音时发送按钮文案切换为 `voiceSendHint`。
|
||||||
|
|
||||||
| 状态 | 按钮 |
|
| 状态 | 按钮 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
@@ -104,8 +114,9 @@ function onVoiceCancel() {
|
|||||||
- `new-conversation`:清空输入框并请求业务侧开始一个新会话
|
- `new-conversation`:清空输入框并请求业务侧开始一个新会话
|
||||||
- `add-attachment`:请求业务侧添加附件
|
- `add-attachment`:请求业务侧添加附件
|
||||||
- `voice-start`:长按达到阈值,可以开始录音或启动识别
|
- `voice-start`:长按达到阈值,可以开始录音或启动识别
|
||||||
- `voice-stop`:松手且未处于取消区域,可以停止识别并取最终结果
|
- `voice-stop`:松手且不在操作按钮上,可以停止识别并取最终结果
|
||||||
- `voice-cancel`:上滑后松手,或指针被系统取消,应丢弃本次语音
|
- `voice-keep`:松手在「保留识别」上,停止识别,文本留在输入框但不发送
|
||||||
|
- `voice-cancel`:松手在「取消发送」上,或指针被系统取消;组件会清空输入框,业务侧应丢弃本次语音
|
||||||
|
|
||||||
输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。
|
输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="demo">
|
<div class="demo">
|
||||||
|
<p
|
||||||
|
class="voice-placeholder"
|
||||||
|
:class="{ hidden: voiceState !== 'idle' }"
|
||||||
|
v-text="'长按下方输入框开始语音输入'"
|
||||||
|
/>
|
||||||
<ai-chat-composer
|
<ai-chat-composer
|
||||||
v-model="draft"
|
v-model="draft"
|
||||||
:is-generating="generating"
|
:is-generating="generating"
|
||||||
@@ -9,6 +14,7 @@
|
|||||||
@send="sendMessage"
|
@send="sendMessage"
|
||||||
@voice-start="onVoiceStart"
|
@voice-start="onVoiceStart"
|
||||||
@voice-stop="onVoiceStop"
|
@voice-stop="onVoiceStop"
|
||||||
|
@voice-keep="onVoiceKeep"
|
||||||
@voice-cancel="onVoiceCancel"
|
@voice-cancel="onVoiceCancel"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -81,15 +87,16 @@ let recognition: SpeechRecognitionLike | undefined;
|
|||||||
let finalText = "";
|
let finalText = "";
|
||||||
let mockStepTimer: number | undefined;
|
let mockStepTimer: number | undefined;
|
||||||
let mockFinishTimer: number | undefined;
|
let mockFinishTimer: number | undefined;
|
||||||
|
let sendOnFinish = true;
|
||||||
|
|
||||||
const hint = computed(() => {
|
const hint = computed(() => {
|
||||||
if (mockEnabled.value) {
|
if (mockEnabled.value) {
|
||||||
return "模拟模式:按住输入框,松手发送,按住上滑取消。";
|
return "模拟模式:按住输入框说话,松手发送;上滑到按钮可选择取消或保留。";
|
||||||
}
|
}
|
||||||
if (!supported.value) {
|
if (!supported.value) {
|
||||||
return "当前浏览器不支持语音识别,已切换为模拟。";
|
return "当前浏览器不支持语音识别,已切换为模拟。";
|
||||||
}
|
}
|
||||||
return "长按输入框说话,松手发送;按住上滑可取消。";
|
return "长按输入框说话,松手发送;上滑到「取消发送」或「保留识别」按钮释放。";
|
||||||
});
|
});
|
||||||
|
|
||||||
/** 获取浏览器实现;Chrome 与 Edge 需要 webkit 前缀。 */
|
/** 获取浏览器实现;Chrome 与 Edge 需要 webkit 前缀。 */
|
||||||
@@ -115,6 +122,7 @@ function sendMessage(message: string): void {
|
|||||||
push("没有识别到内容,请再试一次");
|
push("没有识别到内容,请再试一次");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
draft.value = "";
|
||||||
generating.value = true;
|
generating.value = true;
|
||||||
push(`send("${content}")`);
|
push(`send("${content}")`);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -126,6 +134,7 @@ function sendMessage(message: string): void {
|
|||||||
function onVoiceStart(): void {
|
function onVoiceStart(): void {
|
||||||
voiceText.value = "";
|
voiceText.value = "";
|
||||||
finalText = "";
|
finalText = "";
|
||||||
|
sendOnFinish = true;
|
||||||
if (mockEnabled.value || !supported.value) {
|
if (mockEnabled.value || !supported.value) {
|
||||||
startMock();
|
startMock();
|
||||||
return;
|
return;
|
||||||
@@ -153,6 +162,16 @@ function onVoiceStart(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onVoiceStop(): void {
|
function onVoiceStop(): void {
|
||||||
|
sendOnFinish = true;
|
||||||
|
stopVoice();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onVoiceKeep(): void {
|
||||||
|
sendOnFinish = false;
|
||||||
|
stopVoice();
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopVoice(): void {
|
||||||
if (voiceState.value !== "listening") {
|
if (voiceState.value !== "listening") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -161,23 +180,24 @@ function onVoiceStop(): void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!recognition) {
|
if (!recognition) {
|
||||||
resetVoice();
|
finishVoice();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
voiceState.value = "processing";
|
voiceState.value = "processing";
|
||||||
try {
|
try {
|
||||||
recognition.stop();
|
recognition.stop();
|
||||||
} catch {
|
} catch {
|
||||||
finishVoice(voiceText.value);
|
finishVoice();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onVoiceCancel(): void {
|
function onVoiceCancel(): void {
|
||||||
push("已取消语音输入");
|
push("已取消语音输入");
|
||||||
|
draft.value = "";
|
||||||
resetVoice();
|
resetVoice();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 实时拼接最终结果与临时结果,喂给组件的语音面板。 */
|
/** 实时拼接最终结果与临时结果,组件会同步到输入框。 */
|
||||||
function handleResult(event: SpeechResultEvent): void {
|
function handleResult(event: SpeechResultEvent): void {
|
||||||
let interim = "";
|
let interim = "";
|
||||||
for (let index = event.resultIndex; index < event.results.length; index += 1) {
|
for (let index = event.resultIndex; index < event.results.length; index += 1) {
|
||||||
@@ -204,25 +224,34 @@ function handleError(event: SpeechErrorEvent): void {
|
|||||||
"no-speech": "没有听到声音,请再试一次"
|
"no-speech": "没有听到声音,请再试一次"
|
||||||
};
|
};
|
||||||
push(messages[event.error] ?? `语音识别失败:${event.error}`);
|
push(messages[event.error] ?? `语音识别失败:${event.error}`);
|
||||||
|
draft.value = "";
|
||||||
resetVoice();
|
resetVoice();
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleEnd(): void {
|
function handleEnd(): void {
|
||||||
recognition = undefined;
|
recognition = undefined;
|
||||||
voiceState.value = "processing";
|
voiceState.value = "processing";
|
||||||
finishVoice(voiceText.value);
|
finishVoice();
|
||||||
}
|
}
|
||||||
|
|
||||||
function finishVoice(text: string): void {
|
function finishVoice(): void {
|
||||||
const message = text.trim();
|
const message = voiceText.value.trim();
|
||||||
|
const shouldSend = sendOnFinish;
|
||||||
resetVoice();
|
resetVoice();
|
||||||
|
if (!shouldSend) {
|
||||||
|
if (message) {
|
||||||
|
draft.value = message;
|
||||||
|
}
|
||||||
|
push("已保留识别结果,可继续编辑");
|
||||||
|
return;
|
||||||
|
}
|
||||||
sendMessage(message);
|
sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function startMock(): void {
|
function startMock(): void {
|
||||||
voiceState.value = "listening";
|
voiceState.value = "listening";
|
||||||
voiceText.value = "";
|
voiceText.value = MOCK_STEPS[0];
|
||||||
let step = 0;
|
let step = 1;
|
||||||
mockStepTimer = window.setInterval(() => {
|
mockStepTimer = window.setInterval(() => {
|
||||||
voiceText.value = MOCK_STEPS[Math.min(step, MOCK_STEPS.length - 1)];
|
voiceText.value = MOCK_STEPS[Math.min(step, MOCK_STEPS.length - 1)];
|
||||||
step += 1;
|
step += 1;
|
||||||
@@ -237,7 +266,7 @@ function stopMock(): void {
|
|||||||
voiceState.value = "processing";
|
voiceState.value = "processing";
|
||||||
mockFinishTimer = window.setTimeout(() => {
|
mockFinishTimer = window.setTimeout(() => {
|
||||||
mockFinishTimer = undefined;
|
mockFinishTimer = undefined;
|
||||||
finishVoice(voiceText.value || MOCK_STEPS[0]);
|
finishVoice();
|
||||||
}, 600);
|
}, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,6 +295,7 @@ function resetVoice(): void {
|
|||||||
voiceState.value = "idle";
|
voiceState.value = "idle";
|
||||||
voiceText.value = "";
|
voiceText.value = "";
|
||||||
finalText = "";
|
finalText = "";
|
||||||
|
sendOnFinish = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
@@ -280,6 +310,22 @@ onBeforeUnmount(() => {
|
|||||||
|
|
||||||
<style lang="less" scoped>
|
<style lang="less" scoped>
|
||||||
.demo {
|
.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 {
|
.bar {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: .5rem;
|
gap: .5rem;
|
||||||
@@ -289,10 +335,12 @@ onBeforeUnmount(() => {
|
|||||||
|
|
||||||
.mock {
|
.mock {
|
||||||
gap: .25rem;
|
gap: .25rem;
|
||||||
|
flex: none;
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
font-size: .75rem;
|
font-size: .75rem;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
color: var(--vp-c-text-2);
|
color: var(--vp-c-text-2);
|
||||||
|
white-space: nowrap;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
input {
|
input {
|
||||||
|
|||||||
Reference in New Issue
Block a user