fix(conflicts): 语音支持
This commit is contained in:
1 parent
7f126cfd52
commit
2a036764e3
4 files changed
+276
-8
No files matched your search
@@ -0,0 +1,156 @@
|
||||
import { ref, computed, onBeforeUnmount, type Ref } from 'vue';
|
||||
|
||||
export type SpeechRecognitionErrorKey =
|
||||
| 'chat.voiceNotSupported'
|
||||
| 'chat.voiceRequiresHttps'
|
||||
| 'chat.voicePermissionDenied'
|
||||
| 'chat.voiceNoSpeech'
|
||||
| 'chat.voiceAudioCapture'
|
||||
| 'chat.voiceNetworkError'
|
||||
| 'chat.voiceServiceNotAllowed'
|
||||
| 'chat.voiceRecognitionFailed';
|
||||
|
||||
type SpeechRecognitionCtor = new () => SpeechRecognition;
|
||||
|
||||
const getSpeechRecognitionCtor = (): SpeechRecognitionCtor | null => {
|
||||
if (typeof window === 'undefined') return null;
|
||||
const w = window as Window & {
|
||||
SpeechRecognition?: SpeechRecognitionCtor;
|
||||
webkitSpeechRecognition?: SpeechRecognitionCtor;
|
||||
};
|
||||
return w.SpeechRecognition || w.webkitSpeechRecognition || null;
|
||||
};
|
||||
|
||||
export const mapLocaleToSpeechLang = (locale: string): string => {
|
||||
const map: Record<string, string> = {
|
||||
'zh-CN': 'zh-CN',
|
||||
'zh-TW': 'zh-TW',
|
||||
en: 'en-US',
|
||||
th: 'th-TH',
|
||||
};
|
||||
return map[locale] || 'zh-CN';
|
||||
};
|
||||
|
||||
const isSecureContextForSpeech = (): boolean => {
|
||||
if (typeof window === 'undefined') return false;
|
||||
if (window.isSecureContext) return true;
|
||||
const host = window.location.hostname;
|
||||
return host === 'localhost' || host === '127.0.0.1';
|
||||
};
|
||||
|
||||
/** 语音不可用时的提示 key;HTTPS/localhost 且浏览器有 API 时返回 null */
|
||||
export const getVoiceUnavailableErrorKey = (): SpeechRecognitionErrorKey | null => {
|
||||
if (!isSecureContextForSpeech()) return 'chat.voiceRequiresHttps';
|
||||
if (!getSpeechRecognitionCtor()) return 'chat.voiceNotSupported';
|
||||
return null;
|
||||
};
|
||||
|
||||
const mapErrorToKey = (error: string): SpeechRecognitionErrorKey => {
|
||||
switch (error) {
|
||||
case 'not-allowed':
|
||||
return 'chat.voicePermissionDenied';
|
||||
case 'service-not-allowed':
|
||||
return 'chat.voiceServiceNotAllowed';
|
||||
case 'no-speech':
|
||||
return 'chat.voiceNoSpeech';
|
||||
case 'audio-capture':
|
||||
return 'chat.voiceAudioCapture';
|
||||
case 'network':
|
||||
return 'chat.voiceNetworkError';
|
||||
default:
|
||||
return 'chat.voiceRecognitionFailed';
|
||||
}
|
||||
};
|
||||
|
||||
export function useSpeechRecognition(options: {
|
||||
lang: Ref<string>;
|
||||
onResult: (text: string) => void;
|
||||
onError: (key: SpeechRecognitionErrorKey) => void;
|
||||
}) {
|
||||
const isListening = ref(false);
|
||||
const isSupported = computed(() => getVoiceUnavailableErrorKey() === null);
|
||||
|
||||
let recognition: SpeechRecognition | null = null;
|
||||
let baseText = '';
|
||||
|
||||
const stop = () => {
|
||||
if (recognition) {
|
||||
try {
|
||||
recognition.abort();
|
||||
} catch {
|
||||
// ignore abort errors
|
||||
}
|
||||
recognition = null;
|
||||
}
|
||||
isListening.value = false;
|
||||
};
|
||||
|
||||
const start = (existingText = '') => {
|
||||
if (isListening.value) return;
|
||||
|
||||
const unavailable = getVoiceUnavailableErrorKey();
|
||||
if (unavailable) {
|
||||
options.onError(unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
const Ctor = getSpeechRecognitionCtor()!;
|
||||
|
||||
stop();
|
||||
baseText = existingText;
|
||||
|
||||
const instance = new Ctor();
|
||||
instance.continuous = true;
|
||||
instance.interimResults = true;
|
||||
instance.lang = mapLocaleToSpeechLang(options.lang.value);
|
||||
|
||||
instance.onstart = () => {
|
||||
isListening.value = true;
|
||||
};
|
||||
|
||||
instance.onresult = (event: SpeechRecognitionEvent) => {
|
||||
let sessionTranscript = '';
|
||||
for (let i = 0; i < event.results.length; i++) {
|
||||
sessionTranscript += event.results[i]?.[0]?.transcript || '';
|
||||
}
|
||||
options.onResult(`${baseText}${sessionTranscript}`);
|
||||
};
|
||||
|
||||
instance.onerror = (event: SpeechRecognitionErrorEvent) => {
|
||||
if (event.error === 'aborted') return;
|
||||
options.onError(mapErrorToKey(event.error));
|
||||
stop();
|
||||
};
|
||||
|
||||
instance.onend = () => {
|
||||
isListening.value = false;
|
||||
recognition = null;
|
||||
};
|
||||
|
||||
recognition = instance;
|
||||
try {
|
||||
recognition.start();
|
||||
} catch {
|
||||
options.onError('chat.voiceRecognitionFailed');
|
||||
stop();
|
||||
}
|
||||
};
|
||||
|
||||
const toggle = (existingText = '') => {
|
||||
if (isListening.value) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
start(existingText);
|
||||
};
|
||||
|
||||
onBeforeUnmount(stop);
|
||||
|
||||
return {
|
||||
isListening,
|
||||
isSupported,
|
||||
start,
|
||||
stop,
|
||||
toggle,
|
||||
};
|
||||
}
|
||||
@@ -1250,6 +1250,16 @@ const messages = {
|
||||
welcomeTitle: 'Hello, I am Hexin AI Digital Human',
|
||||
askAnything: 'Ask me anything...',
|
||||
voiceFeature: 'Voice feature',
|
||||
voiceListening: 'Listening...',
|
||||
voiceStop: 'Stop voice input',
|
||||
voiceNotSupported: 'Voice input is not supported in this browser',
|
||||
voiceRequiresHttps: 'Voice input requires HTTPS or localhost access',
|
||||
voicePermissionDenied: 'Microphone permission denied. Allow it in browser settings',
|
||||
voiceNoSpeech: 'No speech detected. Please try again',
|
||||
voiceAudioCapture: 'Unable to access microphone',
|
||||
voiceNetworkError: 'Voice recognition network error',
|
||||
voiceServiceNotAllowed: 'Voice recognition service is not allowed',
|
||||
voiceRecognitionFailed: 'Voice recognition failed. Please try again',
|
||||
collapseBrowser: 'Collapse Browser',
|
||||
openBrowser: 'Open Browser',
|
||||
kb: {
|
||||
@@ -2703,6 +2713,16 @@ const messages = {
|
||||
welcomeTitle: '你好,我是和信AI数字人',
|
||||
askAnything: '问我任何事...',
|
||||
voiceFeature: '语音功能',
|
||||
voiceListening: '正在聆听…',
|
||||
voiceStop: '停止语音输入',
|
||||
voiceNotSupported: '当前浏览器不支持语音输入',
|
||||
voiceRequiresHttps: '语音输入需要 HTTPS 或 localhost 访问',
|
||||
voicePermissionDenied: '麦克风权限被拒绝,请在浏览器设置中允许',
|
||||
voiceNoSpeech: '未检测到语音,请重试',
|
||||
voiceAudioCapture: '无法访问麦克风',
|
||||
voiceNetworkError: '语音识别网络错误',
|
||||
voiceServiceNotAllowed: '不允许使用语音识别服务',
|
||||
voiceRecognitionFailed: '语音识别失败,请重试',
|
||||
collapseBrowser: '收起浏览器',
|
||||
openBrowser: '打开浏览器',
|
||||
kb: {
|
||||
@@ -4015,6 +4035,16 @@ const messages = {
|
||||
welcomeTitle: '你好,我是和信AI數字人',
|
||||
askAnything: '問我任何事...',
|
||||
voiceFeature: '語音功能',
|
||||
voiceListening: '正在聆聽…',
|
||||
voiceStop: '停止語音輸入',
|
||||
voiceNotSupported: '目前瀏覽器不支援語音輸入',
|
||||
voiceRequiresHttps: '語音輸入需要 HTTPS 或 localhost 存取',
|
||||
voicePermissionDenied: '麥克風權限被拒絕,請在瀏覽器設定中允許',
|
||||
voiceNoSpeech: '未偵測到語音,請重試',
|
||||
voiceAudioCapture: '無法存取麥克風',
|
||||
voiceNetworkError: '語音辨識網路錯誤',
|
||||
voiceServiceNotAllowed: '不允許使用語音辨識服務',
|
||||
voiceRecognitionFailed: '語音辨識失敗,請重試',
|
||||
collapseBrowser: '收起瀏覽器',
|
||||
openBrowser: '打開瀏覽器',
|
||||
kb: {
|
||||
@@ -5244,6 +5274,16 @@ const messages = {
|
||||
welcomeTitle: 'สวัสดี ฉันคือ AI Digital Human ของ Hexin',
|
||||
askAnything: 'ถามฉันได้ทุกเรื่อง...',
|
||||
voiceFeature: 'ฟีเจอร์เสียง',
|
||||
voiceListening: 'กำลังฟัง...',
|
||||
voiceStop: 'หยุดป้อนข้อมูลด้วยเสียง',
|
||||
voiceNotSupported: 'เบราว์เซอร์นี้ไม่รองรับการป้อนข้อมูลด้วยเสียง',
|
||||
voiceRequiresHttps: 'การป้อนข้อมูลด้วยเสียงต้องใช้ HTTPS หรือ localhost',
|
||||
voicePermissionDenied: 'ไม่ได้รับอนุญาตให้ใช้ไมโครโฟน โปรดอนุญาตในการตั้งค่าเบราว์เซอร์',
|
||||
voiceNoSpeech: 'ไม่พบเสียงพูด โปรดลองอีกครั้ง',
|
||||
voiceAudioCapture: 'ไม่สามารถเข้าถึงไมโครโฟนได้',
|
||||
voiceNetworkError: 'เกิดข้อผิดพลาดเครือข่ายในการรู้จำเสียง',
|
||||
voiceServiceNotAllowed: 'ไม่อนุญาตให้ใช้บริการรู้จำเสียง',
|
||||
voiceRecognitionFailed: 'การรู้จำเสียงล้มเหลว โปรดลองอีกครั้ง',
|
||||
collapseBrowser: 'ย่อเบราว์เซอร์',
|
||||
openBrowser: 'เปิดเบราว์เซอร์',
|
||||
kb: {
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
interface SpeechRecognitionAlternative {
|
||||
transcript: string;
|
||||
confidence: number;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionResult {
|
||||
readonly isFinal: boolean;
|
||||
readonly length: number;
|
||||
item(index: number): SpeechRecognitionAlternative;
|
||||
[index: number]: SpeechRecognitionAlternative;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionResultList {
|
||||
readonly length: number;
|
||||
item(index: number): SpeechRecognitionResult;
|
||||
[index: number]: SpeechRecognitionResult;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionEvent extends Event {
|
||||
readonly resultIndex: number;
|
||||
readonly results: SpeechRecognitionResultList;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionErrorEvent extends Event {
|
||||
readonly error: string;
|
||||
readonly message: string;
|
||||
}
|
||||
|
||||
interface SpeechRecognition extends EventTarget {
|
||||
continuous: boolean;
|
||||
interimResults: boolean;
|
||||
lang: string;
|
||||
start(): void;
|
||||
stop(): void;
|
||||
abort(): void;
|
||||
onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
||||
onend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
||||
onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
|
||||
onerror: ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => void) | null;
|
||||
}
|
||||
|
||||
interface Window {
|
||||
SpeechRecognition?: new () => SpeechRecognition;
|
||||
webkitSpeechRecognition?: new () => SpeechRecognition;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* ChatView.vue
|
||||
* 文件用途:单个 Agent 的对话界面
|
||||
@@ -21,6 +21,7 @@ import DeleteConfirmDialog from '../components/DeleteConfirmDialog.vue';
|
||||
import GeneralFeedbackDialog from '../components/GeneralFeedbackDialog.vue';
|
||||
import { useMessage } from '../composables/useMessage';
|
||||
import { useIsMobile } from '../composables/useIsMobile';
|
||||
import { getVoiceUnavailableErrorKey, useSpeechRecognition } from '../composables/useSpeechRecognition';
|
||||
import { useTheme } from '../composables/useTheme';
|
||||
import { getAuthHeaders, getStoredAuthUser, logoutAuth } from '../utils/auth';
|
||||
import { copyTextToClipboard } from '../utils/clipboard';
|
||||
@@ -128,10 +129,6 @@ watch(locale, (newLang) => {
|
||||
localStorage.setItem('hxops_lang', String(newLang));
|
||||
});
|
||||
|
||||
const handleVoiceFeatureClick = () => {
|
||||
msg.info(t('chat.devComingSoon'));
|
||||
};
|
||||
|
||||
const stripReasoningPrelude = (text: string) => {
|
||||
if (!text) return '';
|
||||
let cleaned = text
|
||||
@@ -621,6 +618,34 @@ const copiedIndex = ref<number | null>(null);
|
||||
// 从 useChat 中解构出消息列表、思考状态、会话 ID 与发送函数
|
||||
const { messages, isThinking, conversationId, runs, activeRunId, activateRun, createNewChatView, sendMessage } = useChat();
|
||||
const input = ref('');
|
||||
const {
|
||||
isListening: isVoiceListening,
|
||||
toggle: toggleVoiceInput,
|
||||
stop: stopVoiceInput,
|
||||
} = useSpeechRecognition({
|
||||
lang: locale,
|
||||
onResult: (text) => {
|
||||
input.value = text;
|
||||
},
|
||||
onError: (key) => {
|
||||
msg.warning(t(key));
|
||||
},
|
||||
});
|
||||
|
||||
const handleVoiceFeatureClick = () => {
|
||||
if (isThinking.value) return;
|
||||
const unavailable = getVoiceUnavailableErrorKey();
|
||||
if (unavailable) {
|
||||
msg.warning(t(unavailable));
|
||||
return;
|
||||
}
|
||||
toggleVoiceInput(input.value);
|
||||
};
|
||||
|
||||
watch(isThinking, (thinking) => {
|
||||
if (thinking) stopVoiceInput();
|
||||
});
|
||||
|
||||
const currentAgent = ref<any>(null);
|
||||
const userAgents = ref<Array<{ id: number; name: string; description: string; icon_url?: string; dify_app_type?: 'chatflow' | 'workflow' }>>([]);
|
||||
const showGeneralFeedbackDialog = ref(false);
|
||||
@@ -2781,7 +2806,7 @@ const showChatRating = ref(false);
|
||||
v-model="input"
|
||||
@keydown.enter.prevent="handleSend"
|
||||
rows="1"
|
||||
:placeholder="$t('chat.askAnything')"
|
||||
:placeholder="isVoiceListening ? $t('chat.voiceListening') : $t('chat.askAnything')"
|
||||
class="w-full resize-none border-none bg-transparent px-3 py-2 text-[15px] leading-relaxed text-primary placeholder-slate-400 focus:outline-none focus:ring-0 focus:border-transparent"
|
||||
style="min-height: 44px; max-height: 220px;"
|
||||
:disabled="isThinking"
|
||||
@@ -2795,8 +2820,10 @@ const showChatRating = ref(false);
|
||||
<button
|
||||
@click="handleVoiceFeatureClick"
|
||||
type="button"
|
||||
:title="$t('chat.voiceFeature')"
|
||||
class="text-muted transition-colors hover:text-brand-600"
|
||||
:disabled="isThinking"
|
||||
:title="isVoiceListening ? $t('chat.voiceStop') : $t('chat.voiceFeature')"
|
||||
class="transition-colors disabled:cursor-not-allowed disabled:opacity-50"
|
||||
:class="isVoiceListening ? 'text-red-500 animate-pulse' : 'text-muted hover:text-brand-600'"
|
||||
>
|
||||
<Mic class="h-5 w-5" />
|
||||
</button>
|
||||
|
||||
Reference in new issue
Block a user