do not save empty audio file

This commit is contained in:
lloydzhou 2024-11-08 13:43:13 +08:00
parent 50e63109a3
commit a4c9eaf6cd

View File

@ -100,7 +100,7 @@ export function RealtimeChat({
}); });
} }
} }
await clientRef.current.generateResponse(); // await clientRef.current.generateResponse();
} catch (error) { } catch (error) {
console.error("Set message failed:", error); console.error("Set message failed:", error);
} }
@ -156,6 +156,7 @@ export function RealtimeChat({
chatStore.updateTargetSession(session, (session) => { chatStore.updateTargetSession(session, (session) => {
session.messages = session.messages.concat([botMessage]); session.messages = session.messages.concat([botMessage]);
}); });
let hasAudio = false;
for await (const content of item) { for await (const content of item) {
if (content.type === "text") { if (content.type === "text") {
for await (const text of content.textChunks()) { for await (const text of content.textChunks()) {
@ -170,6 +171,7 @@ export function RealtimeChat({
const audioTask = async () => { const audioTask = async () => {
audioHandlerRef.current?.startStreamingPlayback(); audioHandlerRef.current?.startStreamingPlayback();
for await (const audio of content.audioChunks()) { for await (const audio of content.audioChunks()) {
hasAudio = true;
audioHandlerRef.current?.playChunk(audio); audioHandlerRef.current?.playChunk(audio);
} }
}; };
@ -180,15 +182,17 @@ export function RealtimeChat({
session.messages = session.messages.concat(); session.messages = session.messages.concat();
}); });
} }
// upload audio get audio_url if (hasAudio) {
const blob = audioHandlerRef.current?.savePlayFile(); // upload audio get audio_url
uploadImage(blob!).then((audio_url) => { const blob = audioHandlerRef.current?.savePlayFile();
botMessage.audio_url = audio_url; uploadImage(blob!).then((audio_url) => {
// update text and audio_url botMessage.audio_url = audio_url;
chatStore.updateTargetSession(session, (session) => { // update text and audio_url
session.messages = session.messages.concat(); chatStore.updateTargetSession(session, (session) => {
session.messages = session.messages.concat();
});
}); });
}); }
} }
} }
}; };
@ -275,7 +279,7 @@ export function RealtimeChat({
const data = audioHandlerRef.current.getByteFrequencyData(); const data = audioHandlerRef.current.getByteFrequencyData();
console.log("getByteFrequencyData", data); console.log("getByteFrequencyData", data);
} }
}, 100); }, 1000);
return () => { return () => {
if (isRecording) { if (isRecording) {