diff --git a/package.json b/package.json index e527e69..e8e914e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mi-gpt", - "version": "1.0.0", + "version": "1.2.0", "type": "module", "description": "Seamlessly integrate your XiaoAI speaker and Mi Home devices with ChatGPT for an enhanced smart home experience.", "license": "MIT", diff --git a/prisma/migrations/20240227153046_init/migration.sql b/prisma/migrations/20240227161545_init/migration.sql similarity index 100% rename from prisma/migrations/20240227153046_init/migration.sql rename to prisma/migrations/20240227161545_init/migration.sql diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 300d100..a7cf715 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,7 +7,7 @@ generator client { datasource db { provider = "sqlite" - url = "file:../.mi-gpt.db" + url = "file:app.db" } model User { diff --git a/src/index.ts b/src/index.ts index 88820eb..914eec5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export class MiGPT { } async start() { - await initDB(".mi-gpt.db"); + await initDB(); const main = () => { console.log(kBannerASCII); return this.ai.run(); diff --git a/src/services/bot/conversation.ts b/src/services/bot/conversation.ts index 43718f2..76026f2 100644 --- a/src/services/bot/conversation.ts +++ b/src/services/bot/conversation.ts @@ -19,6 +19,10 @@ export class ConversationManager { this.config = config; } + async init() { + return this.get(); + } + async get(): Promise> { const config = await this.update(); if (!config) { diff --git a/src/services/bot/index.ts b/src/services/bot/index.ts index 6789507..8114d74 100644 --- a/src/services/bot/index.ts +++ b/src/services/bot/index.ts @@ -80,8 +80,9 @@ export class MyBot { return this.speaker.stop(); } - run() { + async run() { this.speaker.askAI = (msg) => this.ask(msg); + await this.manager.init(); return this.speaker.run(); } diff --git a/src/services/db/index.ts b/src/services/db/index.ts index a22c022..dee5c3a 100644 --- a/src/services/db/index.ts +++ b/src/services/db/index.ts @@ -27,14 +27,19 @@ export function getSkipWithCursor(skip: number, cursorId: any) { }; } -export async function initDB(dbPath: string) { +export async function initDB() { + const isExternal = exists("node_modules/mi-gpt/prisma"); + const dbPath = isExternal + ? "node_modules/mi-gpt/prisma/app.db" + : "prisma/app.db"; if (!exists(dbPath)) { - const isExternal = exists("node_modules/mi-gpt/prisma"); const withSchema = isExternal ? "--schema node_modules/mi-gpt/prisma/schema.prisma" : ""; await deleteFile(".bot.json"); - await Shell.run(`npx prisma migrate dev --name init ${withSchema}`); + await Shell.run(`npx prisma migrate dev --name init ${withSchema}`, { + silent: true, + }); } const success = exists(dbPath); kDBLogger.assert(success, "初始化数据库失败!"); diff --git a/src/utils/shell.ts b/src/utils/shell.ts index a25638c..36d1f40 100644 --- a/src/utils/shell.ts +++ b/src/utils/shell.ts @@ -1,10 +1,44 @@ -import { exec as execSync } from "child_process"; +import { exec as execSync, spawn } from "child_process"; import { promisify } from "util"; +import { isNotEmpty } from "./is"; const exec = promisify(execSync); +interface StdIO { + stdout: string; + stderr: string; +} + export class Shell { - static async run(command: string) { + static async run(command: string, options?: { silent?: boolean }) { + const { silent } = options ?? {}; + if (silent) { + return new Promise((resolve) => { + const commands = command.split(" ").filter((e) => isNotEmpty(e.trim())); + const bin = commands[0]; + const [, ...args] = commands; + let res: StdIO = { + stdout: "", + stderr: "", + }; + try { + const ps = spawn(bin, args, { + stdio: "ignore", + }); + ps.stdout?.on("data", (data) => { + res.stdout += data; + }); + ps.stderr?.on("data", (data) => { + res.stderr += data; + }); + ps.on("close", () => { + resolve(res); + }); + } catch { + resolve(res); + } + }); + } return exec(command); }