From 9d2faf3440768706a516d1c68cdaa73b900a3c0e Mon Sep 17 00:00:00 2001 From: Timi Date: Thu, 26 Nov 2026 15:51:26 +0800 Subject: [PATCH] feat(ai): refine voice input layout and cancellation --- .../theme/containers/DemoPreview.vue | 7 + docs/components/ai-chat-composer.md | 58 +- docs/demos/ai-chat-composer/voice.vue | 26 +- src/components/ai/AiChatComposer.vue | 544 +++++++++--------- 4 files changed, 302 insertions(+), 333 deletions(-) diff --git a/docs/.vitepress/theme/containers/DemoPreview.vue b/docs/.vitepress/theme/containers/DemoPreview.vue index fd18c6d..8ef160c 100644 --- a/docs/.vitepress/theme/containers/DemoPreview.vue +++ b/docs/.vitepress/theme/containers/DemoPreview.vue @@ -209,6 +209,13 @@ onUnmounted(() => { // 文档预览里的过渡组件就是用来验证转场的,因此在预览范围内恢复原定时长。 @media (prefers-reduced-motion: reduce) { .tui-demo { + // 语音输入的呼吸边框是交互反馈的一部分,预览中恢复动画 + :deep(.composer.voice-active .form::after) { + animation-delay: 0s !important; + animation-duration: 1.6s !important; + animation-iteration-count: infinite !important; + } + :deep(.page-transition .push-left-enter-active) { transition-duration: 520ms, 200ms !important; } diff --git a/docs/components/ai-chat-composer.md b/docs/components/ai-chat-composer.md index f5d119a..2bbe2f0 100644 --- a/docs/components/ai-chat-composer.md +++ b/docs/components/ai-chat-composer.md @@ -12,9 +12,9 @@ demo-preview=../demos/ai-chat-composer/basic.vue ## 语音输入 -输入框为空且未聚焦时,长按 300ms 进入语音输入:识别文本直接写进输入框,卡片显示主题色呼吸边框,发送按钮变为「松开发送」。松手发送;按住上滑到「取消发送」或「保留识别」按钮后释放,执行对应操作。组件只负责手势与状态展示,识别能力由业务侧接入。 +开启 `voice-enabled` 后,输入框为空时右侧发送按钮会变成语音按钮(图标 `voice-wave`,文案「语音」)。点击后输入区切换为「按住说话」,右侧按钮变为键盘按钮(图标 `keyboard-1`,文案「输入」);点击键盘按钮回到文本输入。组件只负责手势与状态展示,识别能力由业务侧接入。 -:::preview 长按语音输入 || 长按输入框说话,识别文本实时进入输入框;松手发送,上滑到按钮可取消或只保留文本;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」 +:::preview 语音输入 || 点击语音按钮进入按住说话,识别文本实时显示在整个控件上方;松手发送,移出按住按钮后松开取消;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」 demo-preview=../demos/ai-chat-composer/voice.vue @@ -22,29 +22,30 @@ demo-preview=../demos/ai-chat-composer/voice.vue ### 交互约定 -- 短按(小于 300ms)仍然聚焦文本域;输入框有内容或已聚焦时不接管手势,避免影响编辑与选择 -- 触发后整个输入卡片显示主题色呼吸边框,上方浮出「取消发送」和「保留识别」两个投放区 -- `voiceText` 的实时结果直接同步到输入框,不再单独占用语音面板 -- 按住时发送按钮文案切换为 `voiceSendHint`,默认「松开发送」 -- 松手时指针在「取消发送」上:触发 `voice-cancel`,组件清空本次识别文本 -- 松手时指针在「保留识别」上:触发 `voice-keep`,停止识别并把文本留在输入框,不发送 -- 松手时指针不在按钮上:触发 `voice-stop`,由业务侧停止识别、取最终结果并决定发送 +- 输入框有内容时右侧仍是发送按钮;只有内容为空且 `voiceState` 为 `idle` 时才显示语音入口 +- 点击语音按钮进入语音模式,输入区替换为「按住说话」按钮;按住立即触发 `voice-start`,没有长按等待 +- 按住期间整个输入卡片显示主题色呼吸边框,识别文本实时浮在整个输入卡片上方,最多两行 +- 按住时按钮文案使用 `voiceSendHint`,默认「松开发送」;指针移出按住按钮后改用 `voiceCancelHint`,默认「松开取消」 +- 松手时指针仍在按住按钮内:触发 `voice-stop`,由业务侧停止识别、取最终结果并发送 +- 松手时指针已移出按住按钮:触发 `voice-cancel`,业务侧应中断识别并丢弃本次结果 - 指针被系统取消时同样触发 `voice-cancel` +- 语音模式右侧固定为键盘按钮(图标 `keyboard-1`,文案「输入」),识别中和按住时禁用;点击回到文本输入 +- 文本输入与按住说话共用同一输入区高度,切换模式时组件不跳动;文本域按内容换行,最少 3 行、最多 6 行,超出后在文本域内滚动 - `voiceEnabled` 默认关闭,需要显式开启 `voiceState` 由业务侧驱动: | 状态 | 含义 | | --- | --- | -| `idle` | 未在识别,语音操作区关闭 | -| `listening` | 正在聆听,实时回写 `voiceText`,组件同步到输入框 | -| `processing` | 已松手或选择保留,等待最终识别结果 | +| `idle` | 未在识别,可以切换模式或按住说话 | +| `listening` | 正在聆听,实时回写 `voiceText`,组件显示在整个控件上方 | +| `processing` | 已松手,等待最终识别结果 | -识别完成或失败后回到 `idle`,语音操作区随之关闭。组件不会自动发送;正常松手由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceSendHint`、`voiceCancelHint`、`voiceKeepHint`、`voiceCancelText`、`voiceKeepText`、`voiceListeningText`、`voiceProcessingText`。 +识别完成或失败后回到 `idle`。组件不会自动发送;正常松手由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceHoldText`、`voiceSendHint`、`voiceCancelHint`、`voiceListeningText`、`voiceProcessingText`。 ### 组件边界与兼容性 -`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-keep`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。 +`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。 Web Speech API 的实际兼容性(2026-11): @@ -58,7 +59,7 @@ Web Speech API 的实际兼容性(2026-11): | Firefox 桌面 160+ | 有实现,但生态较新,建议实测;Firefox Android 不支持 | | SafariViewController / iOS App 内嵌页 | Web Speech API 不可用或不可靠,改用原生桥接 | -Web Speech API 还需要 HTTPS(localhost 除外)和麦克风授权;音频会交给浏览器厂商的服务,隐私敏感或企业内网场景不建议使用。iOS 对用户手势要求更严格,如果 `SpeechRecognition.start()` 在 300ms 长按回调里被拒绝,需要在 `pointerdown` 阶段提前启动,或改成点击说话。 +Web Speech API 还需要 HTTPS(localhost 除外)和麦克风授权;音频会交给浏览器厂商的服务,隐私敏感或企业内网场景不建议使用。组件在 `pointerdown` 时立即触发 `voice-start`,iOS 上应在这个事件里同步调用 `SpeechRecognition.start()`;等异步连接建立后再启动容易被用户手势策略拒绝。 生产环境建议把识别放到服务端流式 ASR(WebSocket)或原生语音插件。业务侧只需要按组件契约驱动状态: @@ -76,11 +77,6 @@ function onVoiceStop() { // 停止录音,等待最终结果;拿到结果后发送 } -function onVoiceKeep() { - voiceState.value = "processing"; - // 停止录音但不发送,最终结果留在输入框 -} - function onVoiceCancel() { voiceState.value = "idle"; voiceText.value = ""; @@ -88,7 +84,7 @@ function onVoiceCancel() { } ``` -`voiceText` 会实时同步到输入框;识别不可用时退回文字输入,不要阻塞发送。 +`voiceText` 只在按住说话区上方展示,不回写 `v-model`;语音结果由业务侧在 `voice-stop` 后取出并发送。识别不可用时退回文字输入,不要阻塞发送。 ## 何时使用 @@ -96,12 +92,15 @@ function onVoiceCancel() { ## 输入区与操作按钮 -文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示纯文本发送或停止按钮。按住语音时发送按钮文案切换为 `voiceSendHint`。 +文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示语音、键盘、发送或停止按钮。 | 状态 | 按钮 | | --- | --- | -| 空闲且未禁用 | 新开会话、添加附件、发送(`sendText`) | -| `isGenerating` 为真 | 停止(`stopText`,危险色) | +| 空闲、输入框为空且启用 `voiceEnabled` | 语音(`voice-wave`),点击进入语音模式 | +| 语音模式且未在识别 | 键盘(`keyboard-1`),点击回到文本输入 | +| 语音模式正在识别或处理 | 键盘按钮禁用 | +| 文本模式且有内容 | 发送(`sendText`) | +| `isGenerating` 为真 | 停止(`stopText`,危险色),优先于语音模式 | | 有 `lastError` 且 `canRetry` | 错误条 + 重试(`retryText`) | 生成中或 `disabled` 为真时输入框不可编辑,新开会话、添加附件和 `send` 事件不会触发。 @@ -113,16 +112,15 @@ function onVoiceCancel() { - `retry`:点错误条上的重试 - `new-conversation`:清空输入框并请求业务侧开始一个新会话 - `add-attachment`:请求业务侧添加附件 -- `voice-start`:长按达到阈值,可以开始录音或启动识别 -- `voice-stop`:松手且不在操作按钮上,可以停止识别并取最终结果 -- `voice-keep`:松手在「保留识别」上,停止识别,文本留在输入框但不发送 -- `voice-cancel`:松手在「取消发送」上,或指针被系统取消;组件会清空输入框,业务侧应丢弃本次语音 +- `voice-start`:按住「按住说话」按钮时立即触发,可以开始录音或启动识别 +- `voice-stop`:松手且不在取消区,可以停止识别并取最终结果 +- `voice-cancel`:松手时指针已移出按住按钮,或指针被系统取消;业务侧应中断识别并丢弃本次结果 -输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。 +输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。语音识别结果只通过 `voiceText` 展示,不写入 `v-model`。 ## 自定义插槽 -`send`、`stop`、`error` 三个插槽可以替换对应区域的渲染,都带作用域参数(`disabled`、`onSend`、`onStop`、`error`、`canRetry`)。 +`send`、`stop`、`error` 三个插槽可以替换对应区域的渲染,都带作用域参数(`disabled`、`onSend`、`onStop`、`error`、`canRetry`)。`send` 插槽只用于文本发送;语音入口和键盘切换按钮由组件内置。 ## API diff --git a/docs/demos/ai-chat-composer/voice.vue b/docs/demos/ai-chat-composer/voice.vue index f686f97..e4eae33 100644 --- a/docs/demos/ai-chat-composer/voice.vue +++ b/docs/demos/ai-chat-composer/voice.vue @@ -3,7 +3,7 @@

@@ -87,16 +86,15 @@ 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 前缀。 */ @@ -134,7 +132,6 @@ function sendMessage(message: string): void { function onVoiceStart(): void { voiceText.value = ""; finalText = ""; - sendOnFinish = true; if (mockEnabled.value || !supported.value) { startMock(); return; @@ -162,12 +159,6 @@ function onVoiceStart(): void { } function onVoiceStop(): void { - sendOnFinish = true; - stopVoice(); -} - -function onVoiceKeep(): void { - sendOnFinish = false; stopVoice(); } @@ -197,7 +188,7 @@ function onVoiceCancel(): void { resetVoice(); } -/** 实时拼接最终结果与临时结果,组件会同步到输入框。 */ +/** 实时拼接最终结果与临时结果,组件在按住说话区上方展示。 */ function handleResult(event: SpeechResultEvent): void { let interim = ""; for (let index = event.resultIndex; index < event.results.length; index += 1) { @@ -236,15 +227,7 @@ function handleEnd(): void { function finishVoice(): void { const message = voiceText.value.trim(); - const shouldSend = sendOnFinish; resetVoice(); - if (!shouldSend) { - if (message) { - draft.value = message; - } - push("已保留识别结果,可继续编辑"); - return; - } sendMessage(message); } @@ -295,7 +278,6 @@ function resetVoice(): void { voiceState.value = "idle"; voiceText.value = ""; finalText = ""; - sendOnFinish = true; } onMounted(() => { diff --git a/src/components/ai/AiChatComposer.vue b/src/components/ai/AiChatComposer.vue index 1e1fe8a..5e6603f 100644 --- a/src/components/ai/AiChatComposer.vue +++ b/src/components/ai/AiChatComposer.vue @@ -1,5 +1,5 @@ @@ -453,9 +428,12 @@ onBeforeUnmount(() => { flex-direction: column; .input-wrap { + gap: .375rem; + display: flex; position: relative; flex: 1; min-width: 0; + flex-direction: column; } .input { @@ -463,115 +441,118 @@ onBeforeUnmount(() => { width: 100%; padding: 0 .25rem; min-width: 0; + + &.voice-input-hidden { + visibility: hidden; + } } - .voice-hit { - inset: 0; - z-index: 1; - position: absolute; - touch-action: none; - -webkit-tap-highlight-color: transparent; - } - - .voice-actions { - gap: .375rem; + .voice-transcript { left: 50%; + gap: .5rem; width: 100%; z-index: 3; display: flex; - padding: .5rem; + padding: .5rem .75rem; position: absolute; box-sizing: border-box; bottom: calc(100% + .5rem); - user-select: none; + align-items: center; pointer-events: none; border: 1px solid var(--td-brand-color); background: var(--td-bg-color-container); box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .16); - animation: voice-actions-in .16s ease-out; + animation: voice-transcript-in .16s ease-out; border-radius: 1rem; - flex-direction: column; transform: translateX(-50%); + } - .voice-status { - gap: .375rem; - display: flex; - min-height: 1.25rem; - color: var(--td-text-color-secondary); - font-size: .75rem; - align-items: center; - justify-content: center; + .voice-wave { + gap: .1875rem; + height: 1rem; + display: flex; + flex: none; + align-items: center; + + span { + width: .125rem; + height: 100%; + border-radius: .125rem; + background: var(--td-brand-color); + animation: voice-wave .9s ease-in-out infinite; + animation-delay: calc(var(--i) * .1s); + transform: scaleY(.3); + transform-origin: center; + } + } + + .voice-text, + .voice-placeholder { + margin: 0; + flex: 1; + min-width: 0; + word-break: break-word; + } + + .voice-text { + display: -webkit-box; + overflow: hidden; + color: var(--td-text-color-primary); + font-size: .875rem; + line-height: 1.4; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; + } + + .voice-placeholder { + overflow: hidden; + color: var(--td-text-color-placeholder); + font-size: .75rem; + white-space: nowrap; + text-overflow: ellipsis; + } + + .voice-hold { + inset: 0; + gap: .375rem; + z-index: 1; + display: flex; + padding: 0 .75rem; + position: absolute; + color: var(--td-brand-color); + font-size: .875rem; + font-weight: 500; + transition: color .15s, border-color .15s; + border: 0; + border-bottom: 1px solid var(--td-component-border); + background: transparent; + border-radius: 0; + align-items: center; + justify-content: center; + touch-action: none; + user-select: none; + -webkit-tap-highlight-color: transparent; + + &.active { + color: var(--td-brand-color); + border-bottom-color: var(--td-brand-color); } - .voice-wave { - gap: .1875rem; - height: .875rem; - display: flex; - align-items: center; - - span { - width: .125rem; - height: 30%; - border-radius: .125rem; - background: var(--td-brand-color); - animation: voice-wave .9s ease-in-out infinite; - animation-delay: calc(var(--i) * .1s); - } + &.cancel { + color: var(--td-error-color); + border-bottom-color: var(--td-error-color); } - .voice-status-text { - min-width: 0; - overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; - } - - .voice-buttons { - gap: .5rem; - display: flex; - } - - .voice-action { - gap: .25rem; - flex: 1; - display: flex; - padding: .4375rem .5rem; - font-size: .75rem; - transition: color .15s, background .15s, border-color .15s, transform .15s; - border: 1px solid var(--td-component-border); - border-radius: .625rem; - align-items: center; - justify-content: center; - - &.cancel { - color: var(--td-error-color); - } - - &.keep { - color: var(--td-brand-color); - } - - &.active { - color: #fff; - transform: translateY(-.0625rem); - } - - &.cancel.active { - border-color: var(--td-error-color); - background: var(--td-error-color); - } - - &.keep.active { - border-color: var(--td-brand-color); - background: var(--td-brand-color); - } + &:disabled { + opacity: .55; } } .actions { - --td-button-border-radius: .75rem; + --td-button-border-radius: 1rem; gap: .5rem; + min-height: 2rem; margin-top: .375rem; display: flex; align-items: center; @@ -582,44 +563,53 @@ onBeforeUnmount(() => { display: flex; align-items: center; } - - .send { - padding: 0 1rem; - } } } &.voice-active .form { border-color: var(--td-brand-color); - animation: voice-breathe 1.8s ease-in-out infinite; - } -} -@keyframes voice-wave { - 0%, - 100% { - height: 25%; - opacity: .5; - } - - 50% { - height: 100%; - opacity: 1; + &::after { + content: ""; + inset: -.125rem; + border: 1px solid var(--td-brand-color); + position: absolute; + animation: voice-breathe 1s ease-in-out 0s infinite; + will-change: transform, opacity; + border-radius: inherit; + pointer-events: none; + transform-origin: center; + } } } @keyframes voice-breathe { 0%, 100% { - box-shadow: 0 0 0 .125rem var(--td-brand-color-light, rgba(0, 82, 217, .16)), 0 .5rem 1.5rem rgba(15, 23, 42, .12); + opacity: .6; + transform: scaleX(1) scaleY(1); } 50% { - box-shadow: 0 0 0 .375rem var(--td-brand-color-light, rgba(0, 82, 217, .16)), 0 .5rem 1.5rem rgba(15, 23, 42, .12); + opacity: .2; + transform: scaleX(1.02) scaleY(1.08); } } -@keyframes voice-actions-in { +@keyframes voice-wave { + 0%, + 100% { + opacity: .5; + transform: scaleY(.3); + } + + 50% { + opacity: 1; + transform: scaleY(1); + } +} + +@keyframes voice-transcript-in { from { opacity: 0; } @@ -629,14 +619,6 @@ onBeforeUnmount(() => { } } -@media (prefers-reduced-motion: reduce) { - .composer.voice-active .form, - .voice-actions, - .voice-actions .voice-wave span { - animation: none; - } -} - :global(html.theme-dark) { .error-bar { color: var(--ai-chat-error-color-dark, #fda29b);