Răsfoiți Sursa

对话流式修改

Zhangbw 2 luni în urmă
părinte
comite
8ff33a9946
7 a modificat fișierele cu 110 adăugiri și 21 ștergeri
  1. 10 0
      .idea/.gitignore
  2. 9 0
      .idea/AI-TALK-WEB.iml
  3. 8 0
      .idea/compiler.xml
  4. 9 0
      .idea/misc.xml
  5. 8 0
      .idea/modules.xml
  6. 6 0
      .idea/vcs.xml
  7. 60 21
      src/CustomerService.vue

+ 10 - 0
.idea/.gitignore

@@ -0,0 +1,10 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 已忽略包含查询文件的默认文件夹
+/queries/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/

+ 9 - 0
.idea/AI-TALK-WEB.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/compiler.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <annotationProcessing>
+      <profile default="true" name="Default" enabled="true" />
+    </annotationProcessing>
+  </component>
+</project>

+ 9 - 0
.idea/misc.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="MavenRunner">
+    <option name="jreName" value="17" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/AI-TALK-WEB.iml" filepath="$PROJECT_DIR$/.idea/AI-TALK-WEB.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 60 - 21
src/CustomerService.vue

@@ -219,6 +219,19 @@ const { isRecording, currentTranscription, tempTranscription, startRecording, st
 // 当前播放的音频对象
 const currentAudio = ref(null)
 
+// 当前请求的序列号,用于标识最新的请求
+const currentRequestId = ref(0)
+
+// 停止当前的音频播放和输出
+const stopCurrentOutput = () => {
+  // 停止音频播放
+  if (currentAudio.value) {
+    currentAudio.value.pause()
+    currentAudio.value.currentTime = 0
+    currentAudio.value = null
+  }
+}
+
 // 停止音频播放
 const stopAudio = () => {
   if (currentAudio.value) {
@@ -331,6 +344,13 @@ watch(currentTranscription, async (newVal, oldVal) => {
 
       // 发送到后端处理
       try {
+        // 停止当前的音频播放
+        stopCurrentOutput()
+
+        // 增加请求ID,标记这是最新的请求
+        currentRequestId.value++
+        const thisRequestId = currentRequestId.value
+
         const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
         const response = await fetch('http://localhost:8080/talk/message', {
           method: 'POST',
@@ -340,7 +360,8 @@ watch(currentTranscription, async (newVal, oldVal) => {
             agentId: selectedAgent.value,
             agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
             ttsVcnList: ttsVcnList.value,
-            conversationId: currentConversationId.value
+            conversationId: currentConversationId.value,
+            requestId: thisRequestId
           })
         })
 
@@ -351,16 +372,21 @@ watch(currentTranscription, async (newVal, oldVal) => {
           currentConversationId.value = data.conversationId
         }
 
-        // 添加客服回复
-        chatHistory.value.push({
-          type: 'agent',
-          content: data.reply
-        })
-        scrollToBottom()
-
-        // 播放语音
-        if (data.audio) {
-          playAudio(data.audio)
+        // 只有当这是最新的请求时,才显示回复和播放音频
+        if (thisRequestId === currentRequestId.value) {
+          // 添加客服回复
+          chatHistory.value.push({
+            type: 'agent',
+            content: data.reply
+          })
+          scrollToBottom()
+
+          // 播放语音
+          if (data.audio) {
+            playAudio(data.audio)
+          }
+        } else {
+          console.log('忽略旧请求的回复')
         }
       } catch (error) {
         console.error('发送消息失败:', error)
@@ -486,6 +512,13 @@ const sendTextMessage = async () => {
 
   // 发送到后端处理
   try {
+    // 停止当前的音频播放
+    stopCurrentOutput()
+
+    // 增加请求ID,标记这是最新的请求
+    currentRequestId.value++
+    const thisRequestId = currentRequestId.value
+
     const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
     const response = await fetch('http://localhost:8080/talk/message', {
       method: 'POST',
@@ -495,7 +528,8 @@ const sendTextMessage = async () => {
         agentId: selectedAgent.value,
         agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
         ttsVcnList: ttsVcnList.value,
-        conversationId: currentConversationId.value
+        conversationId: currentConversationId.value,
+        requestId: thisRequestId
       })
     })
 
@@ -507,16 +541,21 @@ const sendTextMessage = async () => {
     }
     console.log('解析后的audio字段长度:', data.audio ? data.audio.length : 0)
 
-    // 添加客服回复
-    chatHistory.value.push({
-      type: 'agent',
-      content: data.reply
-    })
-    scrollToBottom()
+    // 只有当这是最新的请求时,才显示回复和播放音频
+    if (thisRequestId === currentRequestId.value) {
+      // 添加客服回复
+      chatHistory.value.push({
+        type: 'agent',
+        content: data.reply
+      })
+      scrollToBottom()
 
-    // 播放语音
-    if (data.audio) {
-      playAudio(data.audio)
+      // 播放语音
+      if (data.audio) {
+        playAudio(data.audio)
+      }
+    } else {
+      console.log('忽略旧请求的回复')
     }
   } catch (error) {
     console.error('发送消息失败:', error)