diff --git a/server.js b/server.js index 03a047b..44aa0b3 100644 --- a/server.js +++ b/server.js @@ -137,7 +137,8 @@ const scenes = [ tag: 'chat', apiKey: 'bot-20250916100919-w8vxr', // 起床场景的API key openingLines: [ - " 爷爷/奶奶,我来啦!今天您过得怎么样呀?有没有什么好玩的事儿跟我说说呀?", + "我来啦!今天您过得怎么样呀?有没有什么好玩的事儿跟我说说呀?", + "天冷了,您可得多穿点啊!" ] } ]; @@ -487,18 +488,26 @@ io.on('connection', (socket) => { }); // 断开连接 - socket.on('disconnect', () => { + socket.on('disconnect', async () => { console.log('用户断开连接:', socket.id); const client = connectedClients.get(socket.id); if (client) { - // 广播用户离开事件 - socket.broadcast.emit('user-disconnected', { - id: socket.id, - username: client.username - }); + // 广播用户离开事件 + socket.broadcast.emit('user-disconnected', { + id: socket.id, + username: client.username + }); } connectedClients.delete(socket.id); - + + // 清空聊天记录 + try { + await messageHistory.clearHistory(); + console.log('断开连接后已清空 chat_history.json'); + } catch (err) { + console.error('清空聊天记录失败:', err); + } + // 清除活跃用户 if (activeUser === socket.id) { activeUser = null; diff --git a/src/chat_with_audio.js b/src/chat_with_audio.js index c15d002..32b2231 100644 --- a/src/chat_with_audio.js +++ b/src/chat_with_audio.js @@ -197,7 +197,7 @@ async function chatWithAudioStream(userInput) { } // 导出初始化函数,供外部调用 -export { chatWithAudioStream, initializeHistoryMessage, getCurrentHistoryMessage, saveMessage, updateHistoryMessage }; +export { chatWithAudioStream, initializeHistoryMessage, getCurrentHistoryMessage, saveMessage, updateHistoryMessage, prependIntroRole }; // 处理音频播放队列 async function processAudioQueue() { @@ -322,4 +322,19 @@ async function playAudioStreamNode(audioHex) { -// export { chatWithAudioStream, playAudioStream, playAudioStreamNode, initializeHistoryMessage, getCurrentHistoryMessage }; \ No newline at end of file +// export { chatWithAudioStream, playAudioStream, playAudioStreamNode, initializeHistoryMessage, getCurrentHistoryMessage }; + +// 在历史消息顶部插入“我是你的roleName / 好的,roleName。”开场提示 +function prependIntroRole(roleName) { + if (!roleName) return; + const introUser = { role: 'user', content: `我是你的${roleName}` }; + const introAssistant = { role: 'assistant', content: `好的,${roleName}。` }; + const hasIntro = historyMessage.slice(0, 2).some(m => + m.content === introUser.content || m.content === introAssistant.content + ); + if (!hasIntro) { + // 先插助手,再插用户,确保用户消息在最顶部 + historyMessage.unshift(introAssistant); + historyMessage.unshift(introUser); + } +} \ No newline at end of file diff --git a/src/dash.html b/src/dash.html index 3390c7f..5a7d632 100644 --- a/src/dash.html +++ b/src/dash.html @@ -56,7 +56,7 @@
- +
Dashboard diff --git a/src/index.js b/src/index.js index dfe7314..01b1c39 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ console.log('视频文件:'); // WebRTC 音视频通话应用 // import { chatWithAudioStream } from './chat_with_audio.js'; -import { chatWithAudioStream, initializeHistoryMessage, updateHistoryMessage } from './chat_with_audio.js'; +import { chatWithAudioStream, initializeHistoryMessage, updateHistoryMessage, prependIntroRole } from './chat_with_audio.js'; import { AudioProcessor } from './audio_processor.js'; @@ -256,7 +256,11 @@ class WebRTCChat { async initializeHistory() { try { await initializeHistoryMessage(100); - + const params = new URLSearchParams(window.location.search); + const roleName = params.get('roleName'); + if (roleName) { + prependIntroRole(roleName); + } console.log('历史消息初始化完成'); } catch (error) { console.error('历史消息初始化失败:', error); @@ -268,16 +272,22 @@ class WebRTCChat { try { console.log('开始初始化开场白音频...'); + // 获取URL参数中的roleName + const params = new URLSearchParams(window.location.search); + const roleName = params.get('roleName') || ''; + // 获取当前场景的开场白 const response = await fetch('/api/current-scene/opening-line'); const data = await response.json(); if (data.success && data.openingLine) { - console.log(`获取到开场白: ${data.openingLine}`); + // 如果有roleName,则在开场白前加上角色名称 + const finalOpeningLine = roleName ? `${roleName},${data.openingLine}` : data.openingLine; + console.log(`获取到开场白: ${finalOpeningLine}`); // 生成开场白音频 - await this.generateOpeningAudio(data.openingLine); - this.logMessage(`开场白音频已准备就绪: ${data.openingLine}`, 'success'); + await this.generateOpeningAudio(finalOpeningLine); + this.logMessage(`开场白音频已准备就绪: ${finalOpeningLine}`, 'success'); } else { console.warn('未获取到开场白:', data.message); } diff --git a/src/old.html b/src/old.html new file mode 100644 index 0000000..0551c42 --- /dev/null +++ b/src/old.html @@ -0,0 +1,188 @@ + + + + + + Yantootech + + + + + + +
+ +
+
+

选择你的身份

+

在数字世界找到属于你的温暖连接

+
+ +
+ +
+
+ 👨‍💼 +
+

叔叔

+ + +
+ + +
+
+ 👩‍💼 +
+

阿姨

+ + +
+ + +
+
+ 👴🌳 +
+

爷爷

+ + +
+ + +
+
+ 👵🧶 +
+

奶奶

+ + +
+
+ +
+ +
+
+ + + +