|
10 | 10 | import com.xiaozhi.dialogue.tts.factory.TtsServiceFactory; |
11 | 11 | import com.xiaozhi.entity.SysConfig; |
12 | 12 | import com.xiaozhi.entity.SysDevice; |
| 13 | +import com.xiaozhi.event.ChatSessionCloseEvent; |
13 | 14 | import com.xiaozhi.utils.AudioUtils; |
14 | 15 | import com.xiaozhi.utils.EmojiUtils; |
15 | 16 | import com.xiaozhi.utils.EmojiUtils.EmoSentence; |
16 | 17 | import jakarta.annotation.Resource; |
17 | 18 | import org.slf4j.Logger; |
18 | 19 | import org.slf4j.LoggerFactory; |
| 20 | +import org.springframework.context.ApplicationListener; |
19 | 21 | import org.springframework.stereotype.Service; |
20 | 22 | import org.springframework.util.ObjectUtils; |
21 | 23 | import org.springframework.util.StringUtils; |
|
33 | 35 | * 负责处理语音识别和对话生成的业务逻辑 |
34 | 36 | */ |
35 | 37 | @Service |
36 | | -public class DialogueService { |
| 38 | +public class DialogueService implements ApplicationListener<ChatSessionCloseEvent> { |
37 | 39 | private static final Logger logger = LoggerFactory.getLogger(DialogueService.class); |
38 | 40 | private static final DecimalFormat df = new DecimalFormat("0.00"); |
39 | 41 | private static final long TIMEOUT_MS = 5000; |
@@ -84,6 +86,20 @@ public class DialogueService { |
84 | 86 | private final Map<String, Semaphore> sessionSemaphores = new ConcurrentHashMap<>(); |
85 | 87 | private final Map<String, PriorityBlockingQueue<TtsTask>> sessionTaskQueues = new ConcurrentHashMap<>(); |
86 | 88 |
|
| 89 | + @Override |
| 90 | + public void onApplicationEvent(ChatSessionCloseEvent event) { |
| 91 | + ChatSession chatSession = event.getSession(); |
| 92 | + if(chatSession != null) { |
| 93 | + // clean up dialogue audio paths and responses |
| 94 | + if (StringUtils.hasText(chatSession.getDialogueId())) { |
| 95 | + String dialogueId = chatSession.getDialogueId(); |
| 96 | + dialogueAudioPaths.remove(dialogueId); |
| 97 | + dialogueResponses.remove(dialogueId); |
| 98 | + } |
| 99 | + cleanupSession(chatSession.getSessionId()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
87 | 103 | /** |
88 | 104 | * 句子对象,用于跟踪每个句子的处理状态 |
89 | 105 | */ |
|
0 commit comments