misc: polish reset MiGPT logic

This commit is contained in:
WJG 2024-02-28 22:36:51 +08:00
parent 303663440d
commit e427f3337a
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
2 changed files with 16 additions and 6 deletions

View File

@ -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<MyBotConfig, "speaker"> & {
speaker: AISpeakerConfig;
@ -10,8 +11,13 @@ export type MiGPTConfig = Omit<MyBotConfig, "speaker"> & {
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) {

View File

@ -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,