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

View File

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

View File

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

View File

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