/** * features/chat/page/useChatPageVoice.ts * 用途:封装对话页语音输入按钮行为,将浏览器录音结果回填到输入框。 * 依赖:Vue watch、shared/composables/useAudioTranscription、聊天页 message/i18n 上下文。 * 逻辑:ChatViewContent 触发按钮点击后,本文件先做思考态与浏览器能力校验,再交给转写 composable 录音并调用后端 ASR。 */ import { watch } from 'vue'; import { getVoiceUnavailableErrorKey, useAudioTranscription } from '@/shared/composables/useAudioTranscription'; import type { Ref } from 'vue'; export interface ChatPageVoiceContext { input: Ref; isThinking: Ref; msg: { info: (text: string) => void; warning: (text: string) => void }; t: (key: string) => string; } /** * 绑定聊天页语音输入状态与操作。 * 核心逻辑:复用 useAudioTranscription 完成录音/转写;点击时只拦截思考中、转写中、非 HTTPS 或浏览器不支持的场景。 * @param ctx - 聊天页输入框、思考态、消息提示与 i18n 翻译上下文。 * @returns 语音录制状态、转写状态、点击处理函数与取消函数。 */ export function useChatPageVoice(ctx: ChatPageVoiceContext) { const { isRecording: isVoiceRecording, isTranscribing: isVoiceTranscribing, toggle: toggleVoiceInput, cancel: cancelVoiceInput, } = useAudioTranscription({ onResult: (text) => { ctx.input.value = text; }, onError: (key) => { ctx.msg.warning(ctx.t(key)); }, }); const handleVoiceFeatureClick = () => { if (ctx.isThinking.value || isVoiceTranscribing.value) return; const unavailable = getVoiceUnavailableErrorKey(); if (unavailable) { ctx.msg.warning(ctx.t(unavailable)); return; } toggleVoiceInput(ctx.input.value); }; watch(ctx.isThinking, (thinking) => { if (thinking) cancelVoiceInput(); }); return { isVoiceRecording, isVoiceTranscribing, handleVoiceFeatureClick, cancelVoiceInput, }; }