feat: 多设定管理与切换功能

This commit is contained in:
ikechan8370 2023-03-14 20:18:10 +08:00
parent 2d899db572
commit c7a53a4996
5 changed files with 249 additions and 3 deletions

View file

@ -60,7 +60,7 @@ const defaultConfig = {
initiativeChatGroups: [],
enableDraw: true,
helloPrompt: '写一段话让大家来找我聊天。类似于“有人找我聊天吗?"这种风格轻松随意一点控制在20个字以内',
version: 'v2.1.6'
version: 'v2.1.7'
}
const _path = process.cwd()
let config = {}

42
utils/prompts.js Normal file
View file

@ -0,0 +1,42 @@
import _ from 'lodash'
import fs from 'fs'
import {mkdirs} from "./common.js";
export function readPrompts () {
const _path = process.cwd()
let prompts = []
if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/prompts`)) {
if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/prompts`)) {
const files = fs.readdirSync(`${_path}/plugins/chatgpt-plugin/prompts`)
const txtFiles = files.filter(file => file.endsWith('.txt'))
txtFiles.forEach(txtFile => {
let name = _.trimEnd(txtFile, '.txt')
const content = fs.readFileSync(`${_path}/plugins/chatgpt-plugin/prompts/${txtFile}`, 'utf8')
prompts.push({
name,
content
})
})
}
}
return prompts
}
export function getPromptByName (name) {
if (!name) {
return null
}
let prompts = readPrompts()
let hits = prompts.filter(p => p.name.trim() === name.trim())
if (hits && hits.length > 0) {
return hits[0]
} else {
return null
}
}
export function saveOnePrompt (name, content) {
const _path = process.cwd()
mkdirs(`${_path}/plugins/chatgpt-plugin/prompts`)
let filePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}.txt`
fs.writeFileSync(filePath, content)
}