import fs from 'fs' import lodash from 'lodash' import { execSync } from 'child_process' const defaultConfig = { blockWords: ['屏蔽词1', '屏蔽词b'], promptBlockWords: ['屏蔽词1', '屏蔽词b'], imgOcr: true, defaultUsePicture: false, autoUsePicture: true, autoUsePictureThreshold: 1200, conversationPreserveTime: 0, toggleMode: 'at', quoteReply: true, showQRCode: true, cacheUrl: 'https://content.alcedogroup.com', cacheEntry: false, apiKey: '', openAiBaseUrl: 'https://api.openai.com', drawCD: 30, model: '', temperature: 0.8, api: 'https://chatgpt.duti.tech/api/conversation', apiBaseUrl: 'https://chatgpt.duti.tech/api', plus: false, promptPrefixOverride: 'Your answer shouldn\'t be too verbose. Prefer to answer in Chinese.', assistantLabel: 'ChatGPT', thinkingTips: true, username: '', password: '', UA: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36', headless: false, chromePath: '', '2captchaToken': '', proxy: '', debug: true, defaultTimeoutMs: 120000, chromeTimeoutMS: 120000, version: 'v2.0.12', toneStyle: 'balanced' // or creative, precise } const _path = process.cwd() let config = {} if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/config/config.json`)) { const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/config.json`) const data = fs.readFileSync(fullPath) if (data) { try { config = JSON.parse(data) } catch (e) { logger.error('chatgpt插件读取配置文件出错,请检查config/config.json格式,将忽略用户配置转为使用默认配置', e) logger.warn('chatgpt插件即将使用默认配置') } } } else if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/config/config.js`)) { // 旧版本的config.js,读取其内容,生成config.json,然后删掉config.js const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/config.js`) config = (await import(`file://${fullPath}`)).default try { logger.warn('[ChatGPT-Plugin]发现旧版本config.js文件,正在读取其内容并转换为新版本config.json文件') // 读取其内容,生成config.json fs.writeFileSync(`${_path}/plugins/chatgpt-plugin/config/config.json`, JSON.stringify(config, null, 2)) // 删掉config.js fs.unlinkSync(`${_path}/plugins/chatgpt-plugin/config/config.js`) logger.info('[ChatGPT-Plugin]配置文件转换处理完成') } catch (err) { logger.error('[ChatGPT-Plugin]转换旧版配置文件失败,建议手动清理旧版config.js文件,并转为使用新版config.json格式', err) } } else if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/config/index.js`)) { // 兼容旧版本 const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/index.js`) config = (await import(`file://${fullPath}`)).Config try { logger.warn('[ChatGPT-Plugin]发现旧版本config.js文件,正在读取其内容并转换为新版本config.json文件') // 读取其内容,生成config.json fs.writeFileSync(`${_path}/plugins/chatgpt-plugin/config/config.json`, JSON.stringify(config, null, 2)) // index.js fs.unlinkSync(`${_path}/plugins/chatgpt-plugin/config/index.js`) logger.info('[ChatGPT-Plugin]配置文件转换处理完成') } catch (err) { logger.error('[ChatGPT-Plugin]转换旧版配置文件失败,建议手动清理旧版index.js文件,并转为使用新版config.json格式', err) } } config = Object.assign({}, defaultConfig, config) // const latestTag = execSync(`cd ${_path}/plugins/chatgpt-plugin && git describe --tags --abbrev=0`).toString().trim() // config.version = latestTag export const Config = new Proxy(config, { set (target, property, value) { target[property] = value const change = lodash.transform(target, function (result, value, key) { if (!lodash.isEqual(value, defaultConfig[key])) { result[key] = value } }) try { fs.writeFileSync(`${_path}/plugins/chatgpt-plugin/config/config.json`, JSON.stringify(change, null, 2), { flag: 'w' }) } catch (err) { logger.error(err) return false } return true } })