Skip to content

Commit 06765ce

Browse files
committed
feat: add background sound when agent using tools
1 parent f6f5678 commit 06765ce

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

custom/composables/useAgentAudio.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,36 @@ type StreamingAudioState = {
1414
isDone: boolean;
1515
};
1616

17-
const standByAudio = new Audio('./agentAudio/agent-processing.mp3');
17+
let standByAudio: HTMLAudioElement | null = null;
1818

1919
function playStandByAudio() {
20+
if (!standByAudio) {
21+
standByAudio = new Audio(`/plugins/AdminForthAgentPlugin/agentAudio/agent-processing.mp3`);
22+
standByAudio.addEventListener('ended', () => {
23+
if (!standByAudio.paused) {
24+
restartStandByAudio();
25+
}
26+
});
27+
}
2028
standByAudio.currentTime = 0;
2129
standByAudio.play()
2230
}
2331

2432
function stopStandByAudio() {
33+
if (!standByAudio) {
34+
return;
35+
}
2536
standByAudio.pause();
2637
standByAudio.currentTime = 0;
2738
}
2839

2940
function restartStandByAudio() {
30-
standByAudio.currentTime = 0;
41+
if (standByAudio) {
42+
standByAudio.currentTime = 0;
43+
}
3144
playStandByAudio();
3245
}
3346

34-
standByAudio.addEventListener('ended', () => {
35-
if (!standByAudio.paused) {
36-
restartStandByAudio();
37-
}
38-
});
3947

4048
export const useAgentAudio = defineStore('agentAudio', () => {
4149
const agentStore = useAgentStore();
@@ -164,7 +172,7 @@ export const useAgentAudio = defineStore('agentAudio', () => {
164172
}
165173

166174
if (event.type === 'speech-response') {
167-
// stopStandByAudio();
175+
stopStandByAudio();
168176
agentStore.setCurrentChatStatus('ready');
169177
agentStore.addAgentMessage(event.data.response.text);
170178
agentAudioMode.value = 'playingAgentResponse';
@@ -189,7 +197,7 @@ export const useAgentAudio = defineStore('agentAudio', () => {
189197
}
190198

191199
if (event.type === 'data-tool-call') {
192-
// playStandByAudio();
200+
playStandByAudio();
193201
agentStore.addDataToolCallMessage(event.data);
194202
}
195203
}
@@ -330,6 +338,7 @@ export const useAgentAudio = defineStore('agentAudio', () => {
330338
}
331339

332340
function stopCurrentAudioPlayback(dontResetMode = false) {
341+
stopStandByAudio();
333342
bufferedAudioChunks = [];
334343
bufferedAudioMimeType = 'audio/mpeg';
335344
detachStreamingAudio();
File renamed without changes.

0 commit comments

Comments
 (0)