mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
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')
|
|
let example = []
|
|
try {
|
|
if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/prompts/${name}_example.json`)) {
|
|
example = fs.readFileSync(`${_path}/plugins/chatgpt-plugin/prompts/${name}_example.json`, 'utf8')
|
|
example = JSON.parse(example)
|
|
}
|
|
} catch (err) {
|
|
logger.debug(err)
|
|
}
|
|
prompts.push({
|
|
name,
|
|
content,
|
|
example
|
|
})
|
|
})
|
|
}
|
|
}
|
|
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, examples) {
|
|
const _path = process.cwd()
|
|
mkdirs(`${_path}/plugins/chatgpt-plugin/prompts`)
|
|
let filePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}.txt`
|
|
fs.writeFileSync(filePath, content)
|
|
if (examples) {
|
|
let examplePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}_example.json`
|
|
fs.writeFileSync(examplePath, JSON.stringify(examples))
|
|
}
|
|
}
|
|
|
|
export function deleteOnePrompt (name) {
|
|
const _path = process.cwd()
|
|
mkdirs(`${_path}/plugins/chatgpt-plugin/prompts`)
|
|
let filePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}.txt`
|
|
fs.unlinkSync(filePath)
|
|
try {
|
|
let examplePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}_example.json`
|
|
fs.unlinkSync(examplePath)
|
|
} catch (err) {}
|
|
}
|