From e5a2889b8a734e98a20a054975a5bc2057a86744 Mon Sep 17 00:00:00 2001 From: WJG Date: Tue, 5 Mar 2024 21:08:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=9B=B4=E6=96=B0=20?= =?UTF-8?q?bot/master=20=E4=BA=BA=E8=AE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++ TODO.md | 4 +-- package.json | 4 +-- src/index.ts | 5 +-- src/services/bot/index.ts | 55 ++++++++++++++++++++++++++++++++- src/services/speaker/speaker.ts | 2 +- yarn.lock | 8 ++--- 7 files changed, 67 insertions(+), 14 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e4e77a6..86c1a13 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,5 +23,8 @@ }, "[prisma]": { "editor.defaultFormatter": "Prisma.prisma" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" } } diff --git a/TODO.md b/TODO.md index 347f850..7780761 100644 --- a/TODO.md +++ b/TODO.md @@ -4,5 +4,5 @@ - ✅ Update long/short memories - ✅ Logger - ✅ Npm export -- ✅ Docker -- 更新人设:你是[xxx]你[爱/喜欢][xxx] \ No newline at end of file +- ✅ 更新人设:你是[xxx]你[xxx] +- Docker diff --git a/package.json b/package.json index b47686e..3e9b32e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "mi-gpt", "version": "1.0.0", "type": "module", - "description": "Seamlessly integrate your XiaoAI speaker and Mi Home devices with ChatGPT for an enhanced smart home experience.", + "description": "将小爱音箱接入 ChatGPT 和豆包,改造成你的专属语音助手。", "license": "MIT", "author": { "name": "Del Wang", @@ -29,7 +29,7 @@ "axios": "^1.6.5", "fs-extra": "^11.2.0", "https-proxy-agent": "^7.0.4", - "mi-service-lite": "^2.1.0", + "mi-service-lite": "^2.2.0", "openai": "^4.28.0", "prisma": "^5.8.1" }, diff --git a/src/index.ts b/src/index.ts index 48d1f7a..6fbd07c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { Logger } from "./utils/log"; import { deleteFile } from "./utils/io"; export type MiGPTConfig = Omit & { - speaker: AISpeakerConfig; + speaker: Omit; }; export class MiGPT { @@ -29,9 +29,6 @@ export class MiGPT { "如果需要切换设备或账号,请先使用 MiGPT.reset() 重置实例。" ); } else { - if (config.bot?.name && !config.speaker.name) { - config.speaker.name = config.bot?.name; - } MiGPT.instance = new MiGPT({ ...config, fromCreate: true }); } return MiGPT.instance; diff --git a/src/services/bot/index.ts b/src/services/bot/index.ts index ef48437..75bb0a6 100644 --- a/src/services/bot/index.ts +++ b/src/services/bot/index.ts @@ -78,6 +78,56 @@ export class MyBot { constructor(config: MyBotConfig) { this.speaker = config.speaker; this.manager = new ConversationManager(config); + // 更新 bot 人设命令 + // 比如:你是蔡徐坤,喜欢唱跳rap。 + this.speaker.addCommand({ + match: (msg) => + /.*你是(?[^你]*)你(?.*)/.exec(msg.text) != null, + run: async (msg) => { + const res = /.*你是(?[^你]*)你(?.*)/.exec(msg.text)!; + const name = res[1]; + const profile = res[2]; + const config = await this.manager.update({ + bot: { name, profile }, + }); + if (config) { + this.speaker.name = config?.bot.name; + await this.speaker.response({ + text: `我是${name},很高兴认识你!`, + keepAlive: this.speaker.keepAlive, + }); + } else { + await this.speaker.response({ + text: `召唤${name}失败,请稍后再试吧!`, + keepAlive: this.speaker.keepAlive, + }); + } + }, + }); + this.speaker.addCommand({ + match: (msg) => + /.*我是(?[^我]*)我(?.*)/.exec(msg.text) != null, + run: async (msg) => { + const res = /.*我是(?[^我]*)我(?.*)/.exec(msg.text)!; + const name = res[1]; + const profile = res[2]; + const config = await this.manager.update({ + bot: { name, profile }, + }); + if (config) { + this.speaker.name = config?.bot.name; + await this.speaker.response({ + text: `好的主人,我记住了!`, + keepAlive: this.speaker.keepAlive, + }); + } else { + await this.speaker.response({ + text: `哎呀出错了,请稍后再试吧!`, + keepAlive: this.speaker.keepAlive, + }); + } + }, + }); } stop() { @@ -86,7 +136,10 @@ export class MyBot { async run() { this.speaker.askAI = (msg) => this.ask(msg); - await this.manager.init(); + const { bot } = await this.manager.init(); + if (bot) { + this.speaker.name = bot.name; + } return this.speaker.run(); } diff --git a/src/services/speaker/speaker.ts b/src/services/speaker/speaker.ts index 7f403a9..482bb30 100644 --- a/src/services/speaker/speaker.ts +++ b/src/services/speaker/speaker.ts @@ -110,7 +110,7 @@ export class Speaker extends BaseSpeaker { } addCommand(command: SpeakerCommand) { - this.commands.push(command); + this._commands.push(command); } async onMessage(msg: QueryMessage) { diff --git a/yarn.lock b/yarn.lock index 2ed3097..d6b90c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -927,10 +927,10 @@ merge2@^1.3.0, merge2@^1.4.1: resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae" integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg== -mi-service-lite@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/mi-service-lite/-/mi-service-lite-2.1.0.tgz#a05d85340f18d3ee5278afc180294273235533c5" - integrity sha512-yy7q/7sR36PiE+0kIXgGDQ8ZUCrnLpMUxqtpmdIQgKBsyCaxBx0QNEcscwY+2ejx3WLwbB+ObMy6yR9fznx6hg== +mi-service-lite@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/mi-service-lite/-/mi-service-lite-2.2.0.tgz#30bcebaa8faeb83d1606be0c2ef552a4621dc722" + integrity sha512-rcepSHGA4yUOjI9Tim/1kgu6BzMGeblMl+Nm5aevlRoXw7vv/x8fGx3jiwMV+LX6aDyY7NABEBbjwdPc98wlGA== dependencies: axios "^1.6.5" pako "^2.1.0"