fix: 很多功能

This commit is contained in:
ikechan8370 2025-03-20 22:43:39 +08:00
parent a711ec13d7
commit 66905640e4
15 changed files with 242 additions and 29 deletions

View file

@ -22,7 +22,10 @@ export class LowDBChannelStorage extends ChaiteStorage {
*/
async getItem (key) {
const obj = await this.collection.findOne({ id: key })
return new Channel({}).fromString(JSON.stringify(obj))
if (!obj) {
return null
}
return new Channel(obj)
}
/**
@ -32,7 +35,7 @@ export class LowDBChannelStorage extends ChaiteStorage {
* @returns {Promise<string>}
*/
async setItem (id, channel) {
if (id) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, channel)
return id
}

View file

@ -28,7 +28,7 @@ export class LowDBChatPresetsStorage extends ChaiteStorage {
if (!obj) {
return null
}
return new ChatPreset({}).fromString(JSON.stringify(obj))
return new ChatPreset(obj)
}
/**
@ -38,7 +38,7 @@ export class LowDBChatPresetsStorage extends ChaiteStorage {
* @returns {Promise<string>}
*/
async setItem (id, preset) {
if (id) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, preset)
return id
}

View file

@ -1,4 +1,15 @@
import { Chaite, ChannelsManager, ChatPresetManager, DefaultChannelLoadBalancer, GeminiClient, OpenAIClient, ProcessorsManager, RAGManager, ToolManager } from 'chaite'
import {
Chaite,
ChannelsManager,
ChatPresetManager,
DefaultChannelLoadBalancer,
GeminiClient,
OpenAIClient,
ProcessorsManager,
RAGManager,
ToolManager,
ToolsGroupManager
} from 'chaite'
import ChatGPTConfig from '../../config/config.js'
import { LowDBChannelStorage } from './channel_storage.js'
import { LowDBChatPresetsStorage } from './chat_preset_storage.js'
@ -11,6 +22,8 @@ import { VectraVectorDatabase } from './vector_database.js'
import ChatGPTStorage from '../storage.js'
import path from 'path'
import fs from 'fs'
import { migrateDatabase } from '../../utils/initDB.js'
import { LowDBToolsGroupDTOsStorage } from './tool_groups_storage.js'
/**
* 认证以便共享上传
@ -117,14 +130,16 @@ export async function initChaite () {
}
const processorsManager = await ProcessorsManager.init(processorsDir, new LowDBProcessorsStorage(ChatGPTStorage))
const chatPresetManager = await ChatPresetManager.init(new LowDBChatPresetsStorage(ChatGPTStorage))
const toolsGroupManager = await ToolsGroupManager.init(new LowDBToolsGroupDTOsStorage(ChatGPTStorage))
const userModeSelector = new ChatGPTUserModeSelector()
const userStateStorage = new LowDBUserStateStorage(ChatGPTStorage)
const historyManager = new LowDBHistoryManager(ChatGPTStorage)
let chaite = Chaite.init(channelsManager, toolsManager, processorsManager, chatPresetManager,
let chaite = Chaite.init(channelsManager, toolsManager, processorsManager, chatPresetManager, toolsGroupManager,
userModeSelector, userStateStorage, historyManager, logger)
logger.info('Chaite 初始化完成')
chaite.setCloudService(ChatGPTConfig.chaite.cloudBaseUrl)
logger.info('Chaite.Cloud 初始化完成')
await migrateDatabase()
ChatGPTConfig.chaite.cloudApiKey && await chaite.auth(ChatGPTConfig.chaite.cloudApiKey)
await initRagManager(ChatGPTConfig.llm.embeddingModel, ChatGPTConfig.llm.dimensions)
if (!ChatGPTConfig.chaite.authKey) {

View file

@ -25,7 +25,10 @@ export class LowDBProcessorsStorage extends ChaiteStorage {
*/
async getItem (key) {
const obj = await this.collection.findOne({ id: key })
return new ProcessorDTO({}).fromString(JSON.stringify(obj))
if (!obj) {
return null
}
return new ProcessorDTO(obj)
}
/**
@ -35,7 +38,7 @@ export class LowDBProcessorsStorage extends ChaiteStorage {
* @returns {Promise<string>}
*/
async setItem (id, processor) {
if (id) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, processor)
return id
}

View file

@ -0,0 +1,70 @@
import { ChaiteStorage, ToolsGroupDTO } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').ToolsGroupDTO>}
*/
export class LowDBToolsGroupDTOsStorage extends ChaiteStorage {
/**
*
* @param { LowDBStorage } storage
*/
constructor (storage) {
super()
this.storage = storage
/**
* 集合
* @type {LowDBCollection}
*/
this.collection = this.storage.collection('tool_groups')
}
/**
*
* @param key
* @returns {Promise<import('chaite').ToolsGroupDTO>}
*/
async getItem (key) {
const obj = await this.collection.findOne({ id: key })
if (!obj) {
return null
}
return new ToolsGroupDTO(obj)
}
/**
*
* @param {string} id
* @param {import('chaite').ToolsGroupDTO} preset
* @returns {Promise<string>}
*/
async setItem (id, preset) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, preset)
return id
}
const result = await this.collection.insert(preset)
return result.id
}
/**
*
* @param {string} key
* @returns {Promise<void>}
*/
async removeItem (key) {
await this.collection.deleteById(key)
}
/**
*
* @returns {Promise<import('chaite').ToolsGroupDTO[]>}
*/
async listItems () {
const list = await this.collection.findAll()
return list.map(item => new ToolsGroupDTO({}).fromString(JSON.stringify(item)))
}
async clear () {
await this.collection.deleteAll()
}
}

View file

@ -25,7 +25,10 @@ export class LowDBToolsStorage extends ChaiteStorage {
*/
async getItem (key) {
const obj = await this.collection.findOne({ id: key })
return new ToolDTO({}).fromString(JSON.stringify(obj))
if (!obj) {
return null
}
return new ToolDTO(obj)
}
/**
@ -35,7 +38,7 @@ export class LowDBToolsStorage extends ChaiteStorage {
* @returns {Promise<string>}
*/
async setItem (id, tools) {
if (id) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, tools)
return id
}

View file

@ -52,11 +52,9 @@ export class LowDBUserStateStorage extends ChaiteStorage {
* @returns {Promise<string>}
*/
async setItem (id, state) {
if (id) {
if (await this.getItem(id)) {
await this.collection.updateById(id, state)
return id
}
if (id && await this.getItem(id)) {
await this.collection.updateById(id, state)
return id
}
state.id = id
const result = await this.collection.insert(state)

View file

@ -348,7 +348,7 @@ export class LowDBCollection {
}
}
const dataDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.dataDir)
export const dataDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.dataDir)
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true })
}