fix: 修复小爱回复无法终止的问题

This commit is contained in:
WJG 2024-06-03 09:30:26 +08:00
parent f4866f8729
commit 025ebe476d
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
4 changed files with 24 additions and 3 deletions

View File

@ -110,6 +110,14 @@ export class BaseSpeaker {
audioBeep?: string;
responding = false;
/**
*
*
*
*/
checkIfHasNewMsg() {
return { hasNewMsg: () => false, noNewMsg: () => true };
}
async response(options: {
tts?: TTSProvider;
text?: string;
@ -118,6 +126,7 @@ export class BaseSpeaker {
speaker?: string;
keepAlive?: boolean;
playSFX?: boolean;
hasNewMsg?: () => boolean;
}) {
let {
text,
@ -127,6 +136,7 @@ export class BaseSpeaker {
keepAlive = false,
tts = this.tts,
} = options ?? {};
options.hasNewMsg ??= this.checkIfHasNewMsg().hasNewMsg;
const doubaoTTS = process.env.TTS_DOUBAO;
if (!doubaoTTS) {
@ -203,6 +213,7 @@ export class BaseSpeaker {
speaker?: string;
keepAlive?: boolean;
playSFX?: boolean;
hasNewMsg?: () => boolean;
}) {
let {
text,
@ -214,6 +225,14 @@ export class BaseSpeaker {
speaker = this._defaultSpeaker,
} = options ?? {};
const hasNewMsg = () => {
const flag = options.hasNewMsg?.();
if (this.debug) {
this.logger.debug("checkIfHasNewMsg:" + flag);
}
return flag;
};
const ttsText = text?.replace(/\n\s*\n/g, "\n")?.trim();
const ttsNotXiaoai = !stream && !!text && !audio && tts !== "xiaoai";
playSFX = ttsNotXiaoai && playSFX;
@ -258,6 +277,7 @@ export class BaseSpeaker {
playing = { ...playing, ...res };
}
if (
hasNewMsg() ||
!this.responding || // 有新消息
(playing.status === "playing" && playing.media_type) // 小爱自己开始播放音乐
) {

View File

@ -10,11 +10,11 @@ import { testMiGPT } from "./migpt";
async function main() {
// println(kBannerASCII);
// testDB();
testSpeaker();
// testSpeaker();
// testOpenAI();
// testMyBot();
// testLog();
// testMiGPT();
testMiGPT();
}
main();

View File

@ -18,6 +18,7 @@ export async function testMiGPT() {
userId: process.env.MI_USER!,
password: process.env.MI_PASS!,
did: process.env.MI_DID,
debug: true,
},
bot: {
name: "傻妞",

View File

@ -11,7 +11,7 @@ export async function testSpeaker() {
debug: true,
});
await speaker.initMiServices();
await testAISpeakerStatus(speaker);
// await testAISpeakerStatus(speaker);
// await testSpeakerResponse(speaker);
await testSpeakerStreamResponse(speaker);
// await testSpeakerGetMessages(speaker);