配置初始化模块

待优化...
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 * @example
* fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录 * fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录
*/ */
async createDir(path = '', root = '', includeFile = false) { createDir(path = '', root = '', includeFile = false) {
root = getRoot(root); root = getRoot(root);
let pathList = path.split('/'); let pathList = path.split('/');
let nowPath = root; let nowPath = root;
@ -50,7 +50,7 @@ let fc = {
* @example * @example
* const config = fc.readJSON("config.json", "root") * const config = fc.readJSON("config.json", "root")
*/ */
async readJSON(file = '', root = '') { readJSON(file = '', root = '') {
root = getRoot(root); root = getRoot(root);
if (fs.existsSync(`${root}/${file}`)) { if (fs.existsSync(`${root}/${file}`)) {
try { try {
@ -62,7 +62,7 @@ let fc = {
return {}; return {};
}, },
async statSync(file = '', root = '') { statSync(file = '', root = '') {
root = getRoot(root); root = getRoot(root);
try { try {
return fs.statSync(`${root}/${file}`); return fs.statSync(`${root}/${file}`);
@ -82,8 +82,8 @@ let fc = {
* @example * @example
* fc.writeJSON("config.json", {key: "value"}, "root", 4) * fc.writeJSON("config.json", {key: "value"}, "root", 4)
*/ */
async writeJSON(file, data, root = '', space = 4) { writeJSON(file, data, root = '', space = 4) {
await fc.createDir(file, root, true); fc.createDir(file, root, true);
root = getRoot(root); root = getRoot(root);
try { try {
fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space)); fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space));
@ -108,8 +108,8 @@ let fc = {
* @example * @example
* fc.safewriteJSON("config.json", {newKey: "value"}) * fc.safewriteJSON("config.json", {newKey: "value"})
*/ */
async safeWriteJSON(file, data, root = '', space = 4) { safeWriteJSON(file, data, root = '', space = 4) {
await fc.createDir(file, root, true); fc.createDir(file, root, true);
root = getRoot(root); root = getRoot(root);
const filePath = `${root}/${file}`; const filePath = `${root}/${file}`;
@ -146,7 +146,7 @@ let fc = {
* const merged = fc.deepMerge({a: 1}, {b: {c: 2}}) * const merged = fc.deepMerge({a: 1}, {b: {c: 2}})
* // 返回 {a: 1, b: {c: 2}} * // 返回 {a: 1, b: {c: 2}}
*/ */
async deepMerge(target, source) { deepMerge(target, source) {
for (const key in source) { for (const key in source) {
if (source.hasOwnProperty(key)) { if (source.hasOwnProperty(key)) {
if ( if (
@ -155,7 +155,7 @@ let fc = {
target[key] && target[key] &&
typeof target[key] === 'object' typeof target[key] === 'object'
) { ) {
await this.deepMerge(target[key], source[key]); this.deepMerge(target[key], source[key]);
} else { } else {
target[key] = source[key]; target[key] = source[key];
} }
@ -176,7 +176,7 @@ let fc = {
* @example * @example
* const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules") * const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules")
*/ */
async readDirRecursive(directory, extension, excludeDir) { readDirRecursive(directory, extension, excludeDir) {
let files = fs.readdirSync(directory); let files = fs.readdirSync(directory);
let jsFiles = files.filter( let jsFiles = files.filter(
@ -213,7 +213,7 @@ let fc = {
* const obj = { a: 1, b: [2, 3] }; * const obj = { a: 1, b: [2, 3] };
* const cloned = fc.deepClone(obj); * const cloned = fc.deepClone(obj);
*/ */
async deepClone(source) { deepClone(source) {
const cache = new WeakMap(); const cache = new WeakMap();
const clone = (value) => { 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 path from 'path';
import url from 'url'; import url from 'url';
import fs from 'fs';
const __filename = url.fileURLToPath(import.meta.url); const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@ -10,12 +11,16 @@ const Path = {
apps: path.join(rootDir, 'apps'), apps: path.join(rootDir, 'apps'),
components: path.join(rootDir, 'components'), components: path.join(rootDir, 'components'),
defaultConfig: path.join(rootDir, 'config/default.json'), defaultConfig: path.join(rootDir, 'config/default.json'),
config: path.join(rootDir, './../data/crystelf'), config: path.resolve(rootDir, '../../data/crystelf'),
constants: path.join(rootDir, 'constants'), constants: path.join(rootDir, 'constants'),
lib: path.join(rootDir, 'lib'), lib: path.join(rootDir, 'lib'),
models: path.join(rootDir, 'models'), models: path.join(rootDir, 'models'),
index: path.join(rootDir, 'index.js'), index: path.join(rootDir, 'index.js'),
pkg: path.join(rootDir, 'package.json'), 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; export default Path;

View File

@ -9,8 +9,10 @@ logger.info(
chalk.rgb(134, 142, 204)(`crystelf-plugin ${Version.ver} 初始化 ~ by ${Version.author}`) chalk.rgb(134, 142, 204)(`crystelf-plugin ${Version.ver} 初始化 ~ by ${Version.author}`)
); );
updater.checkAndUpdate(); updater.checkAndUpdate().catch((err) => {
crystelfInit.CSH(); logger.err(err);
});
await crystelfInit.CSH();
const appPath = Path.apps; const appPath = Path.apps;
const jsFiles = fc.readDirRecursive(appPath, 'js'); 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 fc from '../../components/json.js';
import path from 'path'; import path from 'path';
import fs from 'fs';
let configCache = null;
let lastModified = 0;
const configPath = Path.config; const configPath = Path.config;
const configFile = path.join(configPath, 'config.json'); const configFile = path.join(configPath, 'config.json');
const configControl = { const configDir = 'data/crystelf/config.json';
async getConfig() {
let configCache = {};
let lastModified = 0;
function init() {
try { try {
const stats = await fc.statSync(configFile, 'root'); if (!fs.existsSync(configPath)) {
if (!configCache || stats.mtimeMs > lastModified) { fs.mkdirSync(configPath, { recursive: true });
configCache = await fc.readJSON(configFile, 'root'); 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; lastModified = stats.mtimeMs;
if (configCache.debug) {
logger.info('crystelf-plugin 日志模块初始化成功..');
} }
return configCache;
} catch (err) { } catch (err) {
console.error('读取配置失败:', err); logger.warn('crystelf-plugin 初始化配置失败,使用空配置..', err);
return {}; configCache = {};
} }
}
const configControl = {
async init() {
init();
}, },
async updateConfig(updater) {
try { get(key) {
const config = this.getConfig(); return key ? configCache[key] : configCache;
configCache = { ...config, ...updater }; },
fc.safeWriteJSON(configFile, configCache, 'root', 4);
} catch (err) { async set(key, value) {
console.error('更新配置失败:', err); 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; export default configControl;

View File

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