diff --git a/src/index.ts b/src/index.ts index 2b2a85d..8102e15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,9 @@ import { AISpeaker, AISpeakerConfig } from "./services/speaker/ai"; import { MyBot, MyBotConfig } from "./services/bot"; -import { initDB, runWithDB } from "./services/db"; +import { getDBInfo, initDB, runWithDB } from "./services/db"; import { kBannerASCII } from "./utils/string"; import { Logger } from "./utils/log"; +import { deleteFile } from "./utils/io"; export type MiGPTConfig = Omit & { speaker: AISpeakerConfig; @@ -10,8 +11,13 @@ export type MiGPTConfig = Omit & { export class MiGPT { static instance: MiGPT | null; - static reset() { + static async reset() { MiGPT.instance = null; + const { dbPath } = getDBInfo(); + await deleteFile(dbPath); + await deleteFile(".mi.json"); + await deleteFile(".bot.json"); + MiGPT.logger.log("MiGPT 已重置,请使用 MiGPT.create() 重新创建实例。"); } static logger = Logger.create({ tag: "MiGPT" }); static create(config: MiGPTConfig) { diff --git a/src/services/db/index.ts b/src/services/db/index.ts index dee5c3a..4f6368a 100644 --- a/src/services/db/index.ts +++ b/src/services/db/index.ts @@ -27,15 +27,19 @@ export function getSkipWithCursor(skip: number, cursorId: any) { }; } -export async function initDB() { +export function getDBInfo() { const isExternal = exists("node_modules/mi-gpt/prisma"); const dbPath = isExternal ? "node_modules/mi-gpt/prisma/app.db" : "prisma/app.db"; + const schemaPath = isExternal ? "node_modules/mi-gpt" : ""; + const withSchema = `--schema ${schemaPath}/prisma/schema.prisma`; + return { dbPath, isExternal, withSchema }; +} + +export async function initDB() { + const { dbPath, withSchema } = getDBInfo(); if (!exists(dbPath)) { - 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}`, { silent: true,