配置初始化模块

待优化...
This commit is contained in:
Jerry 2025-04-23 17:37:08 +08:00
parent ed4bc5be47
commit ddc4060caf
6 changed files with 117 additions and 39 deletions

View File

@ -25,7 +25,7 @@ let fc = {
* @example
* fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录
*/
async createDir(path = '', root = '', includeFile = false) {
createDir(path = '', root = '', includeFile = false) {
root = getRoot(root);
let pathList = path.split('/');
let nowPath = root;
@ -50,7 +50,7 @@ let fc = {
* @example
* const config = fc.readJSON("config.json", "root")
*/
async readJSON(file = '', root = '') {
readJSON(file = '', root = '') {
root = getRoot(root);
if (fs.existsSync(`${root}/${file}`)) {
try {
@ -62,7 +62,7 @@ let fc = {
return {};
},
async statSync(file = '', root = '') {
statSync(file = '', root = '') {
root = getRoot(root);
try {
return fs.statSync(`${root}/${file}`);
@ -82,8 +82,8 @@ let fc = {
* @example
* fc.writeJSON("config.json", {key: "value"}, "root", 4)
*/
async writeJSON(file, data, root = '', space = 4) {
await fc.createDir(file, root, true);
writeJSON(file, data, root = '', space = 4) {
fc.createDir(file, root, true);
root = getRoot(root);
try {
fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space));
@ -108,8 +108,8 @@ let fc = {
* @example
* fc.safewriteJSON("config.json", {newKey: "value"})
*/
async safeWriteJSON(file, data, root = '', space = 4) {
await fc.createDir(file, root, true);
safeWriteJSON(file, data, root = '', space = 4) {
fc.createDir(file, root, true);
root = getRoot(root);
const filePath = `${root}/${file}`;
@ -146,7 +146,7 @@ let fc = {
* const merged = fc.deepMerge({a: 1}, {b: {c: 2}})
* // 返回 {a: 1, b: {c: 2}}
*/
async deepMerge(target, source) {
deepMerge(target, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
if (
@ -155,7 +155,7 @@ let fc = {
target[key] &&
typeof target[key] === 'object'
) {
await this.deepMerge(target[key], source[key]);
this.deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
@ -176,7 +176,7 @@ let fc = {
* @example
* const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules")
*/
async readDirRecursive(directory, extension, excludeDir) {
readDirRecursive(directory, extension, excludeDir) {
let files = fs.readdirSync(directory);
let jsFiles = files.filter(
@ -213,7 +213,7 @@ let fc = {
* const obj = { a: 1, b: [2, 3] };
* const cloned = fc.deepClone(obj);
*/
async deepClone(source) {
deepClone(source) {
const cache = new WeakMap();
const clone = (value) => {

View File

@ -0,0 +1,33 @@
{
"debug": true,
"core": true,
"mode": "deepseek",
"model_type": "deepseek-ai/DeepSeek-V3",
"model_list": {
"deepseek-ai/DeepSeek-V3": "V3",
"deepseek-ai/DeepSeek-R1": "R1",
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B": "L8b",
"Qwen/Qwen2.5-7B-Instruct": "Q7b",
"internlm/internlm2_5-7b-chat": "I7b",
"THUDM/glm-4-9b-chat": "G9b",
"deepseek-ai/DeepSeek-R1-Distill-Qwen-14B": "Q14b",
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B": "L70b",
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B": "Q32b"
},
"default_history_length": 3,
"default_max_length": 3,
"default_temperature": 1,
"nickName": "寄气人",
"checkChat": {
"rdNum": 2,
"masterReply": true,
"userId": [
114514
],
"blackGroups": [
114,
514
]
},
"max_message_length": 100
}

View File

@ -1,5 +1,6 @@
import path from 'path';
import url from 'url';
import fs from 'fs';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@ -10,12 +11,16 @@ const Path = {
apps: path.join(rootDir, 'apps'),
components: path.join(rootDir, 'components'),
defaultConfig: path.join(rootDir, 'config/default.json'),
config: path.join(rootDir, './../data/crystelf'),
config: path.resolve(rootDir, '../../data/crystelf'),
constants: path.join(rootDir, 'constants'),
lib: path.join(rootDir, 'lib'),
models: path.join(rootDir, 'models'),
index: path.join(rootDir, 'index.js'),
pkg: path.join(rootDir, 'package.json'),
yunzai: path.join(rootDir, '../../'),
};
const configFile = fs.readFileSync(Path.defaultConfig, 'utf8');
export const defaultConfig = JSON.parse(configFile);
export default Path;

View File

@ -9,8 +9,10 @@ logger.info(
chalk.rgb(134, 142, 204)(`crystelf-plugin ${Version.ver} 初始化 ~ by ${Version.author}`)
);
updater.checkAndUpdate();
crystelfInit.CSH();
updater.checkAndUpdate().catch((err) => {
logger.err(err);
});
await crystelfInit.CSH();
const appPath = Path.apps;
const jsFiles = fc.readDirRecursive(appPath, 'js');

View File

@ -1,33 +1,71 @@
import Path from '../../constants/path.js';
import Path, { defaultConfig } from '../../constants/path.js';
import fc from '../../components/json.js';
import path from 'path';
import fs from 'fs';
let configCache = null;
let lastModified = 0;
const configPath = Path.config;
const configFile = path.join(configPath, 'config.json');
const configControl = {
async getConfig() {
const configDir = 'data/crystelf/config.json';
let configCache = {};
let lastModified = 0;
function init() {
try {
const stats = await fc.statSync(configFile, 'root');
if (!configCache || stats.mtimeMs > lastModified) {
configCache = await fc.readJSON(configFile, 'root');
if (!fs.existsSync(configPath)) {
fs.mkdirSync(configPath, { recursive: true });
logger.mark(`crystelf 配置文件夹创建成功,位于 ${configPath}..`);
}
if (!fs.existsSync(configFile)) {
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 4), 'utf8');
logger.mark('crystelf 配置文件创建成功..');
} else {
const cfgFile = fs.readFileSync(configFile, 'utf8');
const loadedConfig = JSON.parse(cfgFile);
const cfg = { ...defaultConfig, ...loadedConfig };
if (JSON.stringify(cfg) !== JSON.stringify(loadedConfig)) {
fs.writeFileSync(configFile, JSON.stringify(cfg, null, 4), 'utf8');
logger.mark('crystelf 配置文件已更新,补充配置项..');
}
}
const stats = fc.statSync(configDir, 'root');
configCache = fc.readJSON(configDir, 'root');
lastModified = stats.mtimeMs;
if (configCache.debug) {
logger.info('crystelf-plugin 日志模块初始化成功..');
}
return configCache;
} catch (err) {
console.error('读取配置失败:', err);
return {};
logger.warn('crystelf-plugin 初始化配置失败,使用空配置..', err);
configCache = {};
}
}
const configControl = {
async init() {
init();
},
async updateConfig(updater) {
try {
const config = this.getConfig();
configCache = { ...config, ...updater };
fc.safeWriteJSON(configFile, configCache, 'root', 4);
} catch (err) {
console.error('更新配置失败:', err);
}
get(key) {
return key ? configCache[key] : configCache;
},
async set(key, value) {
configCache[key] = value;
return fc.safeWriteJSON(configDir, configCache, 'root', 4);
},
async save() {
return fc.safeWriteJSON(configDir, configCache, 'root', 4);
},
async reload() {
await init();
return true;
},
};
export default configControl;

View File

@ -1,8 +1,8 @@
import Path from '../../constants/path.js';
import configControl from '../config/configControl.js';
export const crystelfInit = {
CSH: () => {
logger.info(Path.root);
async CSH() {
await configControl.init();
logger.mark('crystelf 完成初始化');
},
};