chatgpt-plugin/models/chaite/history_manager.js
2025-03-18 22:11:31 +08:00

56 lines
1.4 KiB
JavaScript

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 })
}
}