fix: 对接

This commit is contained in:
ikechan8370 2025-03-16 22:13:52 +08:00
parent 89ab58b3d7
commit eee1285e2f
11 changed files with 221 additions and 26 deletions

View file

@ -1,4 +1,4 @@
import { ChaiteStorage } from 'chaite'
import { ChaiteStorage, Channel } from 'chaite'
export class LowDBChannelStorage extends ChaiteStorage {
/**
@ -21,7 +21,8 @@ export class LowDBChannelStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').Channel>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
const obj = await this.collection.findOne({ id: key })
return new Channel({}).fromString(JSON.stringify(obj))
}
/**
@ -53,7 +54,8 @@ export class LowDBChannelStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').Channel[]>}
*/
async listItems () {
return this.collection.findAll()
const list = await this.collection.findAll()
return list.map(item => new Channel({}).fromString(JSON.stringify(item)))
}
async clear () {

View file

@ -1,4 +1,4 @@
import { ChaiteStorage } from 'chaite'
import { ChaiteStorage, ChatPreset } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').ChatPreset>}
@ -24,7 +24,11 @@ export class LowDBChatPresetsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').ChatPreset>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
const obj = await this.collection.findOne({ id: key })
if (!obj) {
return null
}
return new ChatPreset({}).fromString(JSON.stringify(obj))
}
/**
@ -56,7 +60,8 @@ export class LowDBChatPresetsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').ChatPreset[]>}
*/
async listItems () {
return this.collection.findAll()
const list = await this.collection.findAll()
return list.map(item => new ChatPreset({}).fromString(JSON.stringify(item)))
}
async clear () {

View file

@ -126,7 +126,11 @@ export async function initChaite () {
chaite.setCloudService(ChatGPTConfig.chaite.cloudBaseUrl)
logger.info('Chaite.Cloud 初始化完成')
ChatGPTConfig.chaite.cloudApiKey && await chaite.auth(ChatGPTConfig.chaite.cloudApiKey)
await initRagManager(ChatGPTConfig.llm.embeddingModel, ChatGPTConfig.dimensions)
await initRagManager(ChatGPTConfig.llm.embeddingModel, ChatGPTConfig.llm.dimensions)
if (!ChatGPTConfig.chaite.authKey) {
ChatGPTConfig.chaite.authKey = Chaite.getInstance().getFrontendAuthHandler().generateToken(0, true)
}
chaite.getGlobalConfig().setAuthKey(ChatGPTConfig.chaite.authKey)
// 监听Chaite配置变化同步需要同步的配置
chaite.on('config-change', obj => {
const { key, newVal, oldVal } = obj
@ -139,19 +143,33 @@ export async function initChaite () {
chaite.setUpdateConfigCallback(config => {
logger.debug('chatgpt-plugin config updated')
Object.keys(config).forEach(key => {
ChatGPTConfig[key] = config[key]
// 回传部分需要同步的配置,以防不一致
if (key === 'serverAuthKey') {
chaite.getGlobalConfig().setAuthKey(config[key])
if (typeof config[key] === 'object' && config[key] !== null && ChatGPTConfig[key]) {
deepMerge(ChatGPTConfig[key], config[key])
} else {
ChatGPTConfig[key] = config[key]
}
})
// 回传部分需要同步的配置,以防不一致
chaite.getGlobalConfig().setAuthKey(ChatGPTConfig.chaite.authKey)
})
// 授予Chaite获取插件配置的能力以便通过api放出
chaite.setGetConfig(async () => {
return ChatGPTConfig
})
logger.info('Chaite.RAGManager 初始化完成')
const token = chaite.getFrontendAuthHandler().generateToken()
logger.info(token)
chaite.runApiServer()
}
function deepMerge (target, source) {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
if (typeof source[key] === 'object' && source[key] !== null && target[key]) {
// 如果是对象且目标属性存在,递归合并
deepMerge(target[key], source[key])
} else {
// 否则直接赋值
target[key] = source[key]
}
}
}
}

View file

@ -1,4 +1,4 @@
import { ChaiteStorage } from 'chaite'
import { ChaiteStorage, ProcessorDTO } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').Processor>}
@ -24,7 +24,8 @@ export class LowDBProcessorsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').Processor>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
const obj = await this.collection.findOne({ id: key })
return new ProcessorDTO({}).fromString(JSON.stringify(obj))
}
/**
@ -56,7 +57,8 @@ export class LowDBProcessorsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').Processor[]>}
*/
async listItems () {
return this.collection.findAll()
const list = await this.collection.findAll()
return list.map(item => new ProcessorDTO({}).fromString(JSON.stringify(item)))
}
async clear () {

View file

@ -1,4 +1,4 @@
import { ChaiteStorage } from 'chaite'
import { ChaiteStorage, ToolDTO } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').ToolDTO>}
@ -24,7 +24,8 @@ export class LowDBToolsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').ToolDTO>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
const obj = await this.collection.findOne({ id: key })
return new ToolDTO({}).fromString(JSON.stringify(obj))
}
/**
@ -56,7 +57,8 @@ export class LowDBToolsStorage extends ChaiteStorage {
* @returns {Promise<import('chaite').ToolDTO[]>}
*/
async listItems () {
return this.collection.findAll()
const list = await this.collection.findAll()
return list.map(item => new ToolDTO({}).fromString(JSON.stringify(item)))
}
async clear () {