feat: add silent mode to shell

This commit is contained in:
WJG 2024-02-28 01:21:30 +08:00
parent 413a80cbf8
commit 120a8cce77
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
8 changed files with 53 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "mi-gpt", "name": "mi-gpt",
"version": "1.0.0", "version": "1.2.0",
"type": "module", "type": "module",
"description": "Seamlessly integrate your XiaoAI speaker and Mi Home devices with ChatGPT for an enhanced smart home experience.", "description": "Seamlessly integrate your XiaoAI speaker and Mi Home devices with ChatGPT for an enhanced smart home experience.",
"license": "MIT", "license": "MIT",

View File

@ -7,7 +7,7 @@ generator client {
datasource db { datasource db {
provider = "sqlite" provider = "sqlite"
url = "file:../.mi-gpt.db" url = "file:app.db"
} }
model User { model User {

View File

@ -35,7 +35,7 @@ export class MiGPT {
} }
async start() { async start() {
await initDB(".mi-gpt.db"); await initDB();
const main = () => { const main = () => {
console.log(kBannerASCII); console.log(kBannerASCII);
return this.ai.run(); return this.ai.run();

View File

@ -19,6 +19,10 @@ export class ConversationManager {
this.config = config; this.config = config;
} }
async init() {
return this.get();
}
async get(): Promise<Partial<IBotConfig & { memory: MemoryManager }>> { async get(): Promise<Partial<IBotConfig & { memory: MemoryManager }>> {
const config = await this.update(); const config = await this.update();
if (!config) { if (!config) {

View File

@ -80,8 +80,9 @@ export class MyBot {
return this.speaker.stop(); return this.speaker.stop();
} }
run() { async run() {
this.speaker.askAI = (msg) => this.ask(msg); this.speaker.askAI = (msg) => this.ask(msg);
await this.manager.init();
return this.speaker.run(); return this.speaker.run();
} }

View File

@ -27,14 +27,19 @@ export function getSkipWithCursor(skip: number, cursorId: any) {
}; };
} }
export async function initDB(dbPath: string) { export async function initDB() {
if (!exists(dbPath)) {
const isExternal = exists("node_modules/mi-gpt/prisma"); 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 withSchema = isExternal const withSchema = isExternal
? "--schema node_modules/mi-gpt/prisma/schema.prisma" ? "--schema node_modules/mi-gpt/prisma/schema.prisma"
: ""; : "";
await deleteFile(".bot.json"); 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); const success = exists(dbPath);
kDBLogger.assert(success, "初始化数据库失败!"); kDBLogger.assert(success, "初始化数据库失败!");

View File

@ -1,10 +1,44 @@
import { exec as execSync } from "child_process"; import { exec as execSync, spawn } from "child_process";
import { promisify } from "util"; import { promisify } from "util";
import { isNotEmpty } from "./is";
const exec = promisify(execSync); const exec = promisify(execSync);
interface StdIO {
stdout: string;
stderr: string;
}
export class Shell { 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); return exec(command);
} }