diff --git a/.gitignore b/.gitignore index d7397c62..dcf2b70b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,4 @@ test/data/ /**/kritor/src/generated/ /pnpm-lock.yaml /test/zhin.db/ -/test/zhin.config.yml +/test/config/zhin.config.yml diff --git a/core/package.json b/core/package.json index 8051a87f..5910af84 100644 --- a/core/package.json +++ b/core/package.json @@ -27,12 +27,14 @@ "directory": "core" }, "dependencies": { - "axios": "^1.6.1", "jiti": "^1.21.0", "log4js": "^6.9.1", "tsconfig-paths": "^4.2.0", "yaml": "^2.4.5" }, + "peerDependencies": { + "@zhinjs/shared": "workspace:^" + }, "devDependencies": { "dtsc": "^2.3.0" } diff --git a/core/src/adapter.ts b/core/src/adapter.ts index 122d1d70..6b46e96c 100644 --- a/core/src/adapter.ts +++ b/core/src/adapter.ts @@ -3,9 +3,8 @@ import { EventEmitter } from 'events'; import { Message } from './message'; import path from 'path'; import { getLogger, Logger } from 'log4js'; -import { Dict } from './types'; +import { Dict } from '@zhinjs/shared'; import { WORK_DIR } from './constans'; -import { Prompt } from './prompt'; import { Schema } from './schema'; export type AdapterBot = A extends Adapter ? B : unknown; diff --git a/core/src/app.ts b/core/src/app.ts index 05329436..2b569d5c 100644 --- a/core/src/app.ts +++ b/core/src/app.ts @@ -3,13 +3,15 @@ import { Logger, getLogger } from 'log4js'; import { Middleware } from './middleware'; import { Plugin, PluginMap } from './plugin'; import { Bot, LogLevel } from './types'; -import { loadModule, remove } from './utils'; -import { APP_KEY, REQUIRED_KEY, WORK_DIR } from './constans'; +import { loadModule } from './utils'; +import { remove } from '@zhinjs/shared'; +import { APP_KEY, CONFIG_DIR, REQUIRED_KEY, WORK_DIR } from './constans'; import path from 'path'; import { Adapter, AdapterBot, AdapterReceive } from './adapter'; import { Message } from './message'; import process from 'process'; import { Config } from './config'; +import * as fs from 'fs'; export function defineConfig(config: Partial): Partial; export function defineConfig( @@ -28,11 +30,12 @@ export class App extends EventEmitter { middlewares: Middleware[] = []; plugins: PluginMap = new PluginMap(); renders: Message.Render[] = []; - constructor(key?: string) { + constructor() { super(); this.handleMessage = this.handleMessage.bind(this); + if (!fs.existsSync(CONFIG_DIR)) fs.mkdirSync(CONFIG_DIR); this.on('message', this.handleMessage); - this.config = new Config(process.env.ZHIN_CONFIG || 'zhin.config', App.defaultConfig); + this.config = new Config(process.env.ZHIN_CONFIG || 'zhin.config', App.defaultConfig); this.logger.level = this.config.log_level; return new Proxy(this, { get(target: App, key) { @@ -41,8 +44,9 @@ export class App extends EventEmitter { }, }); } - registerRender(render: Message.Render) { - this.renders.push(render); + registerRender(render: Message.Render, insertBefore?: boolean) { + if (insertBefore) this.renders.unshift(render); + else this.renders.push(render); return () => remove(this.renders, render); } getAdapterSchema(name: string) { @@ -400,8 +404,8 @@ export interface App extends App.Services { removeAllListeners(event: S & Exclude): this; } -export function createApp(key?: string) { - return new App(key); +export function createApp() { + return new App(); } export namespace App { diff --git a/core/src/command.ts b/core/src/command.ts index c674dc6c..858bd074 100644 --- a/core/src/command.ts +++ b/core/src/command.ts @@ -1,5 +1,6 @@ -import { deepClone, findLastIndex, isEmpty, trimQuote } from './utils'; -import { Bot, Dict } from './types'; +import { findLastIndex, trimQuote } from './utils'; +import { deepClone, Dict, isEmpty } from '@zhinjs/shared'; +import { Bot } from './types'; import { Adapter } from './adapter'; import { Message, segment } from './message'; import { Prompt } from './prompt'; diff --git a/core/src/config.ts b/core/src/config.ts index b7a33c9c..edf0096e 100644 --- a/core/src/config.ts +++ b/core/src/config.ts @@ -1,27 +1,27 @@ import * as fs from 'fs'; import * as path from 'path'; import * as yaml from 'yaml'; -import { WORK_DIR } from './constans'; +import { CONFIG_DIR } from './constans'; import { App } from './app'; -export class Config { +export class Config { public static exts: string[] = ['.json', '.yaml', '.yml']; filename: string = ''; #type: Config.Type = Config.Type.YAML; - private _data: App.Config; + private _data: T; get data() { return this._data; } - constructor(name: string, defaultValue?: App.Config) { + constructor(name: string, defaultValue?: T) { try { this.filename = this.#resolveByName(name); } catch (e) { if (!defaultValue) throw e; const ext = path.extname(name); - if (!Config.exts.includes(ext)) this.filename = path.join(WORK_DIR, `${name}${this.#resolveExt()}`); + if (!Config.exts.includes(ext)) this.filename = path.join(CONFIG_DIR, `${name}${this.#resolveExt()}`); this.#saveConfig(defaultValue); } this._data = this.#loadConfig(); - return new Proxy(this._data, { + return new Proxy(this._data, { get: (target, p, receiver) => { if (Reflect.has(this, p)) return Reflect.get(this, p, receiver); return this.#proxied(target, p, receiver); @@ -38,7 +38,7 @@ export class Config { this.#saveConfig(); return result; }, - }) as unknown as Config; + }) as unknown as Config; } #resolveByName(name: string): string { if (!Config.exts.includes(path.extname(name))) { @@ -49,7 +49,7 @@ export class Config { } throw new Error(`未找到配置文件${name}`); } - name = path.resolve(WORK_DIR, name); + name = path.resolve(CONFIG_DIR, name); if (!fs.existsSync(name)) { throw new Error(`未找到配置文件${name}`); } @@ -78,7 +78,7 @@ export class Config { throw new Error(`不支持的配置文件类型${this.#type}`); } } - #saveConfig(data: App.Config = this._data) { + #saveConfig(data: T = this._data) { switch (this.#type) { case Config.Type.JSON: return fs.writeFileSync(this.filename, JSON.stringify(data, null, 2)); diff --git a/core/src/constans.ts b/core/src/constans.ts index 7c6fb861..097eb03b 100644 --- a/core/src/constans.ts +++ b/core/src/constans.ts @@ -1,5 +1,6 @@ import * as os from 'os'; import process from 'process'; +import path from 'path'; export const APP_KEY = Symbol('AppKey'); export const REQUIRED_KEY = Symbol('RequiredServices'); @@ -10,3 +11,4 @@ export const isMobile = !isLinux && !isMac && !isWin; export const HOME_DIR = os.homedir(); export const TEMP_DIR = os.tmpdir(); export const WORK_DIR = process.env.PWD || process.cwd(); +export const CONFIG_DIR = path.join(WORK_DIR, 'config'); diff --git a/core/src/index.ts b/core/src/index.ts index d23c1f4f..87cee3a1 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -1,6 +1,5 @@ export * from './adapter'; export * from './message'; -export { default as axios } from 'axios'; export * as yaml from 'yaml'; export * from './types'; export * from './utils'; diff --git a/core/src/message.ts b/core/src/message.ts index 8f9285a9..4442afbd 100644 --- a/core/src/message.ts +++ b/core/src/message.ts @@ -1,4 +1,5 @@ -import { Bot, Dict } from './types'; +import { Bot } from './types'; +import { Dict } from '@zhinjs/shared'; import { Prompt } from './prompt'; import { Adapter, AdapterReceive } from './adapter'; export interface MessageBase { @@ -130,8 +131,8 @@ segment.image = (file: string, type = 'png') => ` `