Spaces:
Sleeping
Sleeping
File size: 5,118 Bytes
3f0377e 921a78a 3f0377e 921a78a 3f0377e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | let currentState = 'idle';
function setState(newState, options = {}) {
if (currentState === newState) {
return;
}
const oldState = currentState;
currentState = newState;
micContainer.classList.remove('recording', 'thinking', 'speaking', 'disconnected');
switch(newState) {
case 'idle':
hideAgentOutput();
if (typeof stopSpeaking === 'function') {
stopSpeaking();
}
if (options.clearCards !== false) {
clearAllCards();
}
break;
case 'recording':
micContainer.classList.add('recording');
if (!options.keepOutput) {
hideAgentOutput();
}
if (!options.keepCards) {
clearAllCards();
}
transcript.textContent = '聆聽中...';
transcript.className = 'voice-transcript provisional';
break;
case 'thinking':
micContainer.classList.add('thinking');
hideAgentOutput();
if (typeof stopSpeaking === 'function') {
stopSpeaking();
}
break;
case 'speaking':
micContainer.classList.add('speaking');
if (options.outputText) {
typewriterEffect(options.outputText, 40, options.enableTTS);
}
break;
case 'disconnected':
micContainer.classList.add('disconnected');
hideAgentOutput();
if (typeof stopSpeaking === 'function') {
stopSpeaking();
}
clearAllCards();
break;
default:
console.error(`❌ 未知狀態: ${newState}`);
}
}
function applyEmotion(emotion) {
const validEmotions = ['neutral', 'happy', 'sad', 'angry', 'fear', 'surprise'];
if (!validEmotions.includes(emotion)) {
emotion = 'neutral';
}
background.className = `voice-immersive-background emotion-${emotion} active`;
emotionIndicator.textContent = `當前情緒: ${emotionEmojis[emotion]}`;
}
function showErrorNotification(message) {
console.error('🚨 錯誤:', message);
setState('speaking', {
outputText: `抱歉,發生錯誤:${message}`,
enableTTS: false
});
setTimeout(() => setState('idle'), 3000);
}
let isThinking = false;
let isDisconnected = false;
let isRecording = false;
let isSpeaking = false;
function initAgentControls() {
micContainer.addEventListener('click', async () => {
if (currentState === 'recording') {
isRecording = false;
if (typeof stopRealAudioAnalysis === 'function') {
stopRealAudioAnalysis();
}
if (wsManager && typeof wsManager.stopRecording === 'function') {
wsManager.stopRecording();
}
setState('thinking');
return;
}
if (currentState === 'idle' || currentState === 'disconnected' || currentState === 'speaking') {
if (currentState === 'speaking' && typeof stopSpeaking === 'function') {
stopSpeaking();
}
isRecording = true;
setState('recording', {
keepOutput: true, // 保留前次 Agent 回應
keepCards: true // 保留前次工具卡片
});
if (typeof startRealAudioAnalysis === 'function') {
await startRealAudioAnalysis();
}
if (wsManager && typeof wsManager.startRecording === 'function') {
const success = await wsManager.startRecording();
if (!success) {
console.error('❌ 錄音啟動失敗');
setState('idle');
isRecording = false;
if (typeof stopRealAudioAnalysis === 'function') {
stopRealAudioAnalysis();
}
}
} else {
console.error('❌ WebSocket 管理器未初始化');
setState('idle');
isRecording = false;
if (typeof stopRealAudioAnalysis === 'function') {
stopRealAudioAnalysis();
}
}
}
});
document.getElementById('toggle-recording').addEventListener('click', async () => {
isRecording = !isRecording;
if (isRecording) {
setState('recording');
await startRealAudioAnalysis();
} else {
setState('idle');
stopRealAudioAnalysis();
}
});
document.getElementById('toggle-thinking').addEventListener('click', () => {
isThinking = !isThinking;
if (isThinking) {
setState('thinking');
} else {
setState('idle', {clearCards: false}); // 保留工具卡片
}
});
document.getElementById('toggle-speaking').addEventListener('click', () => {
isSpeaking = !isSpeaking;
if (isSpeaking) {
clearAllCards();
setTimeout(() => addToolCard('weather'), 300);
const responseText = '根據目前的天氣資料,台北今天氣溫約 23°C,天氣晴朗,濕度 65%。建議您外出時可以穿著輕便舒適的衣物,並記得攜帶太陽眼鏡。';
setState('speaking', {outputText: responseText});
} else {
setState('idle', {clearCards: false}); // 保留工具卡片
}
});
document.getElementById('toggle-disconnected').addEventListener('click', () => {
isDisconnected = !isDisconnected;
if (isDisconnected) {
setState('disconnected');
} else {
setState('idle');
}
});
}
|