feat(ai): refine hold-to-talk voice actions

This commit is contained in:
Timi
2026-11-26 12:31:30 +08:00
parent 68e44caaa4
commit 46c000c76c
+217 -53
View File
@@ -1,5 +1,5 @@
<template> <template>
<div class="composer"> <div class="composer" :class="{ 'voice-active': voiceActive }">
<div v-if="props.lastError" class="error-bar"> <div v-if="props.lastError" class="error-bar">
<slot name="error" :error="props.lastError" :can-retry="props.canRetry"> <slot name="error" :error="props.lastError" :can-retry="props.canRetry">
<span class="error-text" v-text="props.lastError" /> <span class="error-text" v-text="props.lastError" />
@@ -13,6 +13,24 @@
</slot> </slot>
</div> </div>
<t-form class="form glass-white" @submit="submitMessage"> <t-form class="form glass-white" @submit="submitMessage">
<div v-if="voiceActionsVisible" class="voice-actions" role="group" aria-label="语音操作">
<div class="voice-status" aria-live="polite">
<div class="voice-wave" aria-hidden="true">
<span v-for="index in 5" :key="index" :style="`--i: ${index}`" />
</div>
<span class="voice-status-text" v-text="voiceStatusText" />
</div>
<div class="voice-buttons">
<div ref="cancelActionRef" class="voice-action cancel" :class="{ active: willCancel }">
<t-icon name="close" size="small" />
<span v-text="props.voiceCancelText" />
</div>
<div ref="keepActionRef" class="voice-action keep" :class="{ active: willKeep }">
<t-icon name="check" size="small" />
<span v-text="props.voiceKeepText" />
</div>
</div>
</div>
<div class="input-wrap"> <div class="input-wrap">
<t-textarea <t-textarea
ref="textareaRef" ref="textareaRef"
@@ -34,13 +52,6 @@
@pointercancel="handleVoicePointerCancel" @pointercancel="handleVoicePointerCancel"
@contextmenu.prevent @contextmenu.prevent
/> />
<div v-if="voicePanelVisible" class="voice-panel" :class="{ cancel: willCancel }">
<div class="voice-wave" aria-hidden="true">
<span v-for="index in 5" :key="index" :style="`--i: ${index}`" />
</div>
<p class="voice-text" v-text="voicePanelText" />
<p class="voice-hint" v-text="willCancel ? props.voiceCancelHint : props.voiceSendHint" />
</div>
</div> </div>
<div class="actions"> <div class="actions">
<div class="tools"> <div class="tools">
@@ -101,7 +112,7 @@
size="extra-small" size="extra-small"
theme="primary" theme="primary"
:disabled="props.disabled" :disabled="props.disabled"
:content="props.sendText" :content="sendButtonText"
/> />
</slot> </slot>
</div> </div>
@@ -110,7 +121,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, onBeforeUnmount, ref, useTemplateRef } from "vue"; import { computed, onBeforeUnmount, ref, useTemplateRef, watch } from "vue";
defineOptions({ defineOptions({
name: "AiChatComposer" name: "AiChatComposer"
@@ -132,10 +143,16 @@ export interface Props {
voiceState?: "idle" | "listening" | "processing"; voiceState?: "idle" | "listening" | "processing";
/** 语音中间结果 */ /** 语音中间结果 */
voiceText?: string; voiceText?: string;
/** 松开发送提示 */ /** 松开发送提示,同时作为按住时的发送按钮文案 */
voiceSendHint?: string; voiceSendHint?: string;
/** 上滑取消提示 */ /** 上滑到取消按钮时的提示 */
voiceCancelHint?: string; voiceCancelHint?: string;
/** 上滑到保留按钮时的提示 */
voiceKeepHint?: string;
/** 取消发送按钮文案 */
voiceCancelText?: string;
/** 保留识别按钮文案 */
voiceKeepText?: string;
/** 正在聆听提示 */ /** 正在聆听提示 */
voiceListeningText?: string; voiceListeningText?: string;
/** 正在识别提示 */ /** 正在识别提示 */
@@ -156,6 +173,9 @@ const props = withDefaults(defineProps<Props>(), {
voiceText: "", voiceText: "",
voiceSendHint: "松开发送", voiceSendHint: "松开发送",
voiceCancelHint: "松开取消", voiceCancelHint: "松开取消",
voiceKeepHint: "松开保留",
voiceCancelText: "取消发送",
voiceKeepText: "保留识别",
voiceListeningText: "正在听你说…", voiceListeningText: "正在听你说…",
voiceProcessingText: "正在识别…" voiceProcessingText: "正在识别…"
}); });
@@ -169,42 +189,73 @@ const emit = defineEmits<{
"add-attachment": []; "add-attachment": [];
"voice-start": []; "voice-start": [];
"voice-stop": []; "voice-stop": [];
"voice-keep": [];
"voice-cancel": []; "voice-cancel": [];
}>(); }>();
/** 长按触发语音的阈值,短按仍然聚焦输入 */ /** 长按触发语音的阈值,短按仍然聚焦输入 */
const VOICE_HOLD_DELAY = 300; const VOICE_HOLD_DELAY = 300;
/** 上滑取消的位移阈值 */
const VOICE_CANCEL_DISTANCE = 48;
/** 未触发语音时,手指移动超过该距离视为滑动 */ /** 未触发语音时,手指移动超过该距离视为滑动 */
const VOICE_MOVE_TOLERANCE = 12; const VOICE_MOVE_TOLERANCE = 12;
/** 按钮命中区域外扩,避免边缘难以对准;需小于按钮间距,防止两个按钮重叠 */
const VOICE_ACTION_HIT_PADDING = 4;
type VoiceAction = "cancel" | "keep" | "send";
const textareaRef = useTemplateRef<{ focus: () => void }>("textareaRef"); const textareaRef = useTemplateRef<{ focus: () => void }>("textareaRef");
const cancelActionRef = useTemplateRef<HTMLElement>("cancelActionRef");
const keepActionRef = useTemplateRef<HTMLElement>("keepActionRef");
const inputFocused = ref(false); const inputFocused = ref(false);
const voicePressing = ref(false); const voicePressing = ref(false);
const voiceTriggered = ref(false); const voiceTriggered = ref(false);
const willCancel = ref(false); const willCancel = ref(false);
const willKeep = ref(false);
const voiceSessionActive = ref(false);
const voiceKeepPending = ref(false);
let voiceTimer: number | undefined; let voiceTimer: number | undefined;
let voiceStartY = 0; let voiceStartY = 0;
let voicePointerId: number | undefined; let voicePointerId: number | undefined;
/** 输入框为空且未聚焦时才接管长按,避免影响选择和编辑 */ /** 输入框为空且未聚焦时才接管长按;识别中保持覆盖层以持续接收指针事件 */
const voiceHitVisible = computed(() => props.voiceEnabled const voiceHitVisible = computed(() => props.voiceEnabled
&& !props.disabled && !props.disabled
&& !props.isGenerating && !props.isGenerating
&& !draft.value.trim() && (voicePressing.value || (!draft.value.trim() && !inputFocused.value && props.voiceState === "idle")));
&& !inputFocused.value
&& (props.voiceState === "idle" || voicePressing.value));
const voicePanelVisible = computed(() => props.voiceEnabled && (voiceTriggered.value || props.voiceState !== "idle")); const voiceActive = computed(() => voicePressing.value && voiceTriggered.value);
const voicePanelText = computed(() => { const voiceActionsVisible = computed(() => voiceActive.value);
if (props.voiceText) {
return props.voiceText; const voiceStatusText = computed(() => {
if (willCancel.value) {
return props.voiceCancelHint;
}
if (willKeep.value) {
return props.voiceKeepHint;
} }
return props.voiceState === "processing" ? props.voiceProcessingText : props.voiceListeningText; return props.voiceState === "processing" ? props.voiceProcessingText : props.voiceListeningText;
}); });
const sendButtonText = computed(() => voiceActive.value ? props.voiceSendHint : props.sendText);
/** ASR 结果直接写进输入框;取消后停止同步,保留后忽略调用方清空 voiceText 的动作 */
watch(() => props.voiceText, (text) => {
if (!voiceSessionActive.value) {
return;
}
if (voiceKeepPending.value && !text) {
return;
}
draft.value = text;
});
watch(() => props.voiceState, (state) => {
if (state === "idle" && voiceSessionActive.value) {
voiceSessionActive.value = false;
voiceKeepPending.value = false;
}
});
function startNewConversation(): void { function startNewConversation(): void {
if (props.disabled || props.isGenerating) { if (props.disabled || props.isGenerating) {
return; return;
@@ -239,6 +290,9 @@ function handleVoicePointerDown(event: PointerEvent): void {
voicePressing.value = true; voicePressing.value = true;
voiceTriggered.value = false; voiceTriggered.value = false;
willCancel.value = false; willCancel.value = false;
willKeep.value = false;
voiceSessionActive.value = false;
voiceKeepPending.value = false;
voiceStartY = event.clientY; voiceStartY = event.clientY;
voicePointerId = event.pointerId; voicePointerId = event.pointerId;
event.preventDefault(); event.preventDefault();
@@ -252,6 +306,9 @@ function handleVoicePointerDown(event: PointerEvent): void {
return; return;
} }
voiceTriggered.value = true; voiceTriggered.value = true;
voiceSessionActive.value = true;
voiceKeepPending.value = false;
draft.value = "";
navigator.vibrate?.(15); navigator.vibrate?.(15);
emit("voice-start"); emit("voice-start");
}, VOICE_HOLD_DELAY); }, VOICE_HOLD_DELAY);
@@ -267,7 +324,30 @@ function handleVoicePointerMove(event: PointerEvent): void {
} }
return; return;
} }
willCancel.value = voiceStartY - event.clientY > VOICE_CANCEL_DISTANCE; const action = resolveVoiceAction(event);
willCancel.value = action === "cancel";
willKeep.value = action === "keep";
}
function resolveVoiceAction(event: PointerEvent): VoiceAction {
if (isPointInsideAction(event, cancelActionRef.value)) {
return "cancel";
}
if (isPointInsideAction(event, keepActionRef.value)) {
return "keep";
}
return "send";
}
function isPointInsideAction(event: PointerEvent, element: HTMLElement | null): boolean {
if (!element) {
return false;
}
const rect = element.getBoundingClientRect();
return event.clientX >= rect.left - VOICE_ACTION_HIT_PADDING
&& event.clientX <= rect.right + VOICE_ACTION_HIT_PADDING
&& event.clientY >= rect.top - VOICE_ACTION_HIT_PADDING
&& event.clientY <= rect.bottom + VOICE_ACTION_HIT_PADDING;
} }
function handleVoicePointerUp(event: PointerEvent): void { function handleVoicePointerUp(event: PointerEvent): void {
@@ -275,14 +355,19 @@ function handleVoicePointerUp(event: PointerEvent): void {
return; return;
} }
const triggered = voiceTriggered.value; const triggered = voiceTriggered.value;
const cancel = willCancel.value; const action = resolveVoiceAction(event);
resetVoicePress(); resetVoicePress();
if (!triggered) { if (!triggered) {
textareaRef.value?.focus(); textareaRef.value?.focus();
return; return;
} }
if (cancel) { if (action === "cancel") {
emit("voice-cancel"); cancelVoice();
return;
}
if (action === "keep") {
voiceKeepPending.value = true;
emit("voice-keep");
return; return;
} }
emit("voice-stop"); emit("voice-stop");
@@ -295,10 +380,17 @@ function handleVoicePointerCancel(): void {
const triggered = voiceTriggered.value; const triggered = voiceTriggered.value;
resetVoicePress(); resetVoicePress();
if (triggered) { if (triggered) {
emit("voice-cancel"); cancelVoice();
} }
} }
function cancelVoice(): void {
voiceSessionActive.value = false;
voiceKeepPending.value = false;
draft.value = "";
emit("voice-cancel");
}
function resetVoicePress(): void { function resetVoicePress(): void {
if (voiceTimer !== undefined) { if (voiceTimer !== undefined) {
window.clearTimeout(voiceTimer); window.clearTimeout(voiceTimer);
@@ -307,6 +399,7 @@ function resetVoicePress(): void {
voicePressing.value = false; voicePressing.value = false;
voiceTriggered.value = false; voiceTriggered.value = false;
willCancel.value = false; willCancel.value = false;
willKeep.value = false;
voicePointerId = undefined; voicePointerId = undefined;
} }
@@ -352,7 +445,9 @@ onBeforeUnmount(() => {
margin: .5rem 0 0; margin: .5rem 0 0;
display: flex; display: flex;
padding: .5rem; padding: .5rem;
position: relative;
box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .12); box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .12);
transition: border-color .2s, box-shadow .2s;
align-items: stretch; align-items: stretch;
border-radius: 1.25rem; border-radius: 1.25rem;
flex-direction: column; flex-direction: column;
@@ -378,29 +473,44 @@ onBeforeUnmount(() => {
-webkit-tap-highlight-color: transparent; -webkit-tap-highlight-color: transparent;
} }
.voice-panel { .voice-actions {
inset: 0; gap: .375rem;
gap: .25rem; left: 50%;
width: 100%;
z-index: 3;
display: flex; display: flex;
z-index: 2; padding: .5rem;
padding: .5rem .75rem;
position: absolute; position: absolute;
box-sizing: border-box;
bottom: calc(100% + .5rem);
user-select: none; user-select: none;
border-radius: .75rem;
pointer-events: none; pointer-events: none;
border: 1px solid var(--td-brand-color);
background: var(--td-bg-color-container); background: var(--td-bg-color-container);
align-items: center; box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .16);
animation: voice-actions-in .16s ease-out;
border-radius: 1rem;
flex-direction: column; flex-direction: column;
justify-content: center; 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 { .voice-wave {
gap: .1875rem; gap: .1875rem;
height: 1.25rem; height: .875rem;
display: flex; display: flex;
align-items: center; align-items: center;
span { span {
width: .1875rem; width: .125rem;
height: 30%; height: 30%;
border-radius: .125rem; border-radius: .125rem;
background: var(--td-brand-color); background: var(--td-brand-color);
@@ -409,32 +519,52 @@ onBeforeUnmount(() => {
} }
} }
.voice-text { .voice-status-text {
min-width: 0;
overflow: hidden; overflow: hidden;
margin: .375rem 0 0; white-space: nowrap;
font-size: .8125rem; text-overflow: ellipsis;
text-align: center;
line-height: 1.4;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
} }
.voice-hint { .voice-buttons {
margin: .25rem 0 0; gap: .5rem;
color: var(--td-text-color-secondary); display: flex;
font-size: .6875rem;
} }
&.cancel { .voice-action {
.voice-text, gap: .25rem;
.voice-hint { 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); color: var(--td-error-color);
} }
.voice-wave span { &.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); background: var(--td-error-color);
} }
&.keep.active {
border-color: var(--td-brand-color);
background: var(--td-brand-color);
}
} }
} }
@@ -458,6 +588,11 @@ onBeforeUnmount(() => {
} }
} }
} }
&.voice-active .form {
border-color: var(--td-brand-color);
animation: voice-breathe 1.8s ease-in-out infinite;
}
} }
@keyframes voice-wave { @keyframes voice-wave {
@@ -473,6 +608,35 @@ onBeforeUnmount(() => {
} }
} }
@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);
}
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);
}
}
@keyframes voice-actions-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.composer.voice-active .form,
.voice-actions,
.voice-actions .voice-wave span {
animation: none;
}
}
:global(html.theme-dark) { :global(html.theme-dark) {
.error-bar { .error-bar {
color: var(--ai-chat-error-color-dark, #fda29b); color: var(--ai-chat-error-color-dark, #fda29b);