mirror of
https://github.com/chaos-zhu/easynode.git
synced 2026-05-10 15:27:17 +08:00
82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
const path = require('path')
|
|
const decryptAndExecuteAsync = require('../utils/decrypt-file')
|
|
const { AIConfigDB, ChatHistoryDB } = require('../utils/db-class')
|
|
|
|
const aiConfigDB = new AIConfigDB().getInstance()
|
|
const chatHistoryDB = new ChatHistoryDB().getInstance()
|
|
|
|
async function getAIConfig({ res }) {
|
|
try {
|
|
const config = await aiConfigDB.findOneAsync({})
|
|
if (!config) {
|
|
return res.success({ data: {} })
|
|
}
|
|
res.success({ data: config })
|
|
} catch (error) {
|
|
res.fail({ msg: '获取配置失败' })
|
|
}
|
|
}
|
|
|
|
async function getAIModels({ res, request }) {
|
|
let { getAIModels } = (await decryptAndExecuteAsync(path.join(__dirname, 'plus.js'))) || {}
|
|
if (getAIModels) {
|
|
await getAIModels({ res, request })
|
|
} else {
|
|
return res.fail({ data: false, msg: 'Plus专属功能!' })
|
|
}
|
|
}
|
|
|
|
async function saveAIConfig({ res, request }) {
|
|
let { saveAIConfig } = (await decryptAndExecuteAsync(path.join(__dirname, 'plus.js'))) || {}
|
|
if (saveAIConfig) {
|
|
await saveAIConfig({ res, request })
|
|
} else {
|
|
return res.fail({ data: false, msg: 'Plus专属功能!' })
|
|
}
|
|
}
|
|
|
|
async function getChatHistory({ res }) {
|
|
const chatHistory = await chatHistoryDB.findAsync({})
|
|
const newChatHistory = chatHistory.map(item => {
|
|
item.id = item._id
|
|
delete item._id
|
|
return item
|
|
}).sort((a, b) => b.createdAt - a.createdAt)
|
|
res.success({ data: newChatHistory || [] })
|
|
}
|
|
|
|
async function saveChatHistory({ res, request }) {
|
|
const chatRecord = request.body
|
|
const { id = '', chatList } = chatRecord
|
|
if (!chatList) return res.fail({ data: false, msg: '参数错误' })
|
|
let updateChat = chatRecord
|
|
if (id) {
|
|
chatRecord.updatedAt = Date.now()
|
|
await chatHistoryDB.updateAsync({ _id: id }, chatRecord)
|
|
} else {
|
|
chatRecord.createdAt = Date.now()
|
|
delete chatRecord.id
|
|
const result = await chatHistoryDB.insertAsync(chatRecord)
|
|
updateChat = result
|
|
updateChat.id = result._id
|
|
delete updateChat._id
|
|
}
|
|
res.success({ data: { updateChat } })
|
|
}
|
|
|
|
async function removeChatHistory({ res, request }) {
|
|
let { params: { id } } = request
|
|
if (!id) return res.fail({ data: false, msg: '参数错误' })
|
|
await chatHistoryDB.removeAsync({ _id: id })
|
|
res.success({ data: true })
|
|
}
|
|
|
|
module.exports = {
|
|
getAIConfig,
|
|
saveAIConfig,
|
|
getAIModels,
|
|
getChatHistory,
|
|
saveChatHistory,
|
|
removeChatHistory
|
|
}
|