feat: chaite 初始化逻辑

This commit is contained in:
ikechan8370 2025-03-10 23:08:23 +08:00
parent 88312cdf38
commit fbcf4e6c08
16 changed files with 549 additions and 1269 deletions

View file

@ -0,0 +1,66 @@
import ChatGPTStorage from '../storage.js'
import { ChaiteStorage } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').UserState>}
*/
export class LowDBUserStateStorage extends ChaiteStorage {
/**
*
* @param {LowDBStorage} storage
*/
constructor (storage = ChatGPTStorage) {
super()
this.storage = storage
/**
* 集合
* @type {LowDBCollection}
*/
this.collection = this.storage.collection('user_states')
}
/**
*
* @param {string} key
* @returns {Promise<import('chaite').UserState>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
}
/**
*
* @param {string} id
* @param {import('chaite').UserState} state
* @returns {Promise<string>}
*/
async setItem (id, state) {
if (id) {
await this.collection.updateById(id, state)
return id
}
const result = await this.collection.insert(state)
return result.id
}
/**
*
* @param {string} key
* @returns {Promise<void>}
*/
async removeItem (key) {
await this.collection.deleteById(key)
}
/**
*
* @returns {Promise<import('chaite').UserState[]>}
*/
async listItems () {
return this.collection.findAll()
}
async clear () {
await this.collection.deleteAll()
}
}