mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 13:57:10 +00:00
feat: use sqlite instead of lowdb
This commit is contained in:
parent
9c41251164
commit
fd197abb33
22 changed files with 3519 additions and 39 deletions
56
models/chaite/storage/lowdb/history_manager.js
Normal file
56
models/chaite/storage/lowdb/history_manager.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import { AbstractHistoryManager } from 'chaite'
|
||||
|
||||
export class LowDBHistoryManager extends AbstractHistoryManager {
|
||||
/**
|
||||
*
|
||||
* @param { LowDBStorage } storage
|
||||
*/
|
||||
constructor (storage) {
|
||||
super()
|
||||
this.storage = storage
|
||||
/**
|
||||
* 集合
|
||||
* @type {LowDBCollection}
|
||||
*/
|
||||
this.collection = this.storage.collection('history')
|
||||
}
|
||||
|
||||
async saveHistory (message, conversationId) {
|
||||
const historyObj = { ...message, conversationId }
|
||||
if (message.id) {
|
||||
await this.collection.updateById(message.id, historyObj)
|
||||
}
|
||||
await this.collection.insert(historyObj)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param messageId
|
||||
* @param conversationId
|
||||
* @returns {Promise<import('chaite').HistoryMessage[]>}
|
||||
*/
|
||||
async getHistory (messageId, conversationId) {
|
||||
if (messageId) {
|
||||
const messages = []
|
||||
let currentId = messageId
|
||||
while (currentId) {
|
||||
const message = await this.collection.findOne({ id: currentId })
|
||||
if (!message) break
|
||||
messages.unshift(message)
|
||||
currentId = message.parentId
|
||||
}
|
||||
return messages
|
||||
} else if (conversationId) {
|
||||
return this.collection.find({ conversationId })
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
async deleteConversation (conversationId) {
|
||||
await this.collection.delete({ conversationId })
|
||||
}
|
||||
|
||||
async getOneHistory (messageId, conversationId) {
|
||||
return this.collection.findOne({ id: messageId, conversationId })
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue