|
@@ -219,6 +219,19 @@ const { isRecording, currentTranscription, tempTranscription, startRecording, st
|
|
|
// 当前播放的音频对象
|
|
// 当前播放的音频对象
|
|
|
const currentAudio = ref(null)
|
|
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 = () => {
|
|
const stopAudio = () => {
|
|
|
if (currentAudio.value) {
|
|
if (currentAudio.value) {
|
|
@@ -331,6 +344,13 @@ watch(currentTranscription, async (newVal, oldVal) => {
|
|
|
|
|
|
|
|
// 发送到后端处理
|
|
// 发送到后端处理
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 停止当前的音频播放
|
|
|
|
|
+ stopCurrentOutput()
|
|
|
|
|
+
|
|
|
|
|
+ // 增加请求ID,标记这是最新的请求
|
|
|
|
|
+ currentRequestId.value++
|
|
|
|
|
+ const thisRequestId = currentRequestId.value
|
|
|
|
|
+
|
|
|
const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
|
|
const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
|
|
|
const response = await fetch('http://localhost:8080/talk/message', {
|
|
const response = await fetch('http://localhost:8080/talk/message', {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
@@ -340,7 +360,8 @@ watch(currentTranscription, async (newVal, oldVal) => {
|
|
|
agentId: selectedAgent.value,
|
|
agentId: selectedAgent.value,
|
|
|
agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
|
|
agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
|
|
|
ttsVcnList: ttsVcnList.value,
|
|
ttsVcnList: ttsVcnList.value,
|
|
|
- conversationId: currentConversationId.value
|
|
|
|
|
|
|
+ conversationId: currentConversationId.value,
|
|
|
|
|
+ requestId: thisRequestId
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -351,16 +372,21 @@ watch(currentTranscription, async (newVal, oldVal) => {
|
|
|
currentConversationId.value = data.conversationId
|
|
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) {
|
|
} catch (error) {
|
|
|
console.error('发送消息失败:', error)
|
|
console.error('发送消息失败:', error)
|
|
@@ -486,6 +512,13 @@ const sendTextMessage = async () => {
|
|
|
|
|
|
|
|
// 发送到后端处理
|
|
// 发送到后端处理
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 停止当前的音频播放
|
|
|
|
|
+ stopCurrentOutput()
|
|
|
|
|
+
|
|
|
|
|
+ // 增加请求ID,标记这是最新的请求
|
|
|
|
|
+ currentRequestId.value++
|
|
|
|
|
+ const thisRequestId = currentRequestId.value
|
|
|
|
|
+
|
|
|
const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
|
|
const selectedAgentData = agents.value.find(a => a.id === selectedAgent.value)
|
|
|
const response = await fetch('http://localhost:8080/talk/message', {
|
|
const response = await fetch('http://localhost:8080/talk/message', {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
@@ -495,7 +528,8 @@ const sendTextMessage = async () => {
|
|
|
agentId: selectedAgent.value,
|
|
agentId: selectedAgent.value,
|
|
|
agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
|
|
agentGender: selectedAgentData?.gender === 'male' ? '0' : '1',
|
|
|
ttsVcnList: ttsVcnList.value,
|
|
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)
|
|
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) {
|
|
} catch (error) {
|
|
|
console.error('发送消息失败:', error)
|
|
console.error('发送消息失败:', error)
|