mirror of
https://github.com/idootop/mi-gpt.git
synced 2025-04-07 17:03:41 +00:00
feat: add silent mode to shell
This commit is contained in:
parent
413a80cbf8
commit
120a8cce77
|
@ -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",
|
||||
|
|
|
@ -7,7 +7,7 @@ generator client {
|
|||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:../.mi-gpt.db"
|
||||
url = "file:app.db"
|
||||
}
|
||||
|
||||
model User {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -19,6 +19,10 @@ export class ConversationManager {
|
|||
this.config = config;
|
||||
}
|
||||
|
||||
async init() {
|
||||
return this.get();
|
||||
}
|
||||
|
||||
async get(): Promise<Partial<IBotConfig & { memory: MemoryManager }>> {
|
||||
const config = await this.update();
|
||||
if (!config) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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, "初始化数据库失败!");
|
||||
|
|
|
@ -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<StdIO>((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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user