diff --git a/models/chaite/channel_storage.js b/models/chaite/channel_storage.js index a1a6126..2fe8ea9 100644 --- a/models/chaite/channel_storage.js +++ b/models/chaite/channel_storage.js @@ -61,6 +61,43 @@ export class LowDBChannelStorage extends ChaiteStorage { return list.map(item => new Channel({}).fromString(JSON.stringify(item))) } + /** + * + * @param {Record} filter + * @returns {Promise} + */ + async listItemsByEqFilter (filter) { + const allList = await this.listItems() + return allList.filter(item => { + for (const key in filter) { + if (item[key] !== filter[key]) { + return false + } + } + return true + }) + } + + /** + * + * @param {Array<{ + * field: string; + * values: unknown[]; + * }>} query + * @returns {Promise} + */ + async listItemsByInQuery (query) { + const allList = await this.listItems() + return allList.filter(item => { + for (const { field, values } of query) { + if (!values.includes(item[field])) { + return false + } + } + return true + }) + } + async clear () { await this.collection.deleteAll() } diff --git a/models/chaite/chat_preset_storage.js b/models/chaite/chat_preset_storage.js index d1b85fb..49788c6 100644 --- a/models/chaite/chat_preset_storage.js +++ b/models/chaite/chat_preset_storage.js @@ -64,6 +64,43 @@ export class LowDBChatPresetsStorage extends ChaiteStorage { return list.map(item => new ChatPreset({}).fromString(JSON.stringify(item))) } + /** + * + * @param {Record} filter + * @returns {Promise} + */ + async listItemsByEqFilter (filter) { + const allList = await this.listItems() + return allList.filter(item => { + for (const key in filter) { + if (item[key] !== filter[key]) { + return false + } + } + return true + }) + } + + /** + * + * @param {Array<{ + * field: string; + * values: unknown[]; + * }>} query + * @returns {Promise} + */ + async listItemsByInQuery (query) { + const allList = await this.listItems() + return allList.filter(item => { + for (const { field, values } of query) { + if (!values.includes(item[field])) { + return false + } + } + return true + }) + } + async clear () { await this.collection.deleteAll() } diff --git a/models/chaite/cloud.js b/models/chaite/cloud.js index 5f9bd2c..b5de1f3 100644 --- a/models/chaite/cloud.js +++ b/models/chaite/cloud.js @@ -19,7 +19,7 @@ import { ChatGPTUserModeSelector } from './user_mode_selector.js' import { LowDBUserStateStorage } from './user_state_storage.js' import { LowDBHistoryManager } from './history_manager.js' import { VectraVectorDatabase } from './vector_database.js' -import ChatGPTStorage, {ChatGPTHistoryStorage} from '../storage.js' +import ChatGPTStorage, { ChatGPTHistoryStorage } from '../storage.js' import path from 'path' import fs from 'fs' import { migrateDatabase } from '../../utils/initDB.js' @@ -28,11 +28,15 @@ import { LowDBToolsGroupDTOsStorage } from './tool_groups_storage.js' /** * 认证,以便共享上传 * @param apiKey - * @returns {Promise | null} + * @returns {Promise} */ export async function authCloud (apiKey = ChatGPTConfig.chaite.cloudApiKey) { - await Chaite.getInstance().auth(apiKey) - return Chaite.getInstance().getToolsManager().cloudService.getUser() + try { + await Chaite.getInstance().auth(apiKey) + return Chaite.getInstance().getToolsManager().cloudService.getUser() + } catch (err) { + + } } /** @@ -141,7 +145,10 @@ export async function initChaite () { chaite.setCloudService(ChatGPTConfig.chaite.cloudBaseUrl) logger.info('Chaite.Cloud 初始化完成') await migrateDatabase() - ChatGPTConfig.chaite.cloudApiKey && await chaite.auth(ChatGPTConfig.chaite.cloudApiKey) + if (ChatGPTConfig.chaite.cloudApiKey) { + const user = await authCloud(ChatGPTConfig.chaite.cloudApiKey) + logger.info(`Chaite.Cloud 认证成功, 当前用户${user.username || user.email} (${user.user_id})`) + } await initRagManager(ChatGPTConfig.llm.embeddingModel, ChatGPTConfig.llm.dimensions) if (!ChatGPTConfig.chaite.authKey) { ChatGPTConfig.chaite.authKey = Chaite.getInstance().getFrontendAuthHandler().generateToken(0, true) diff --git a/models/chaite/processors_storage.js b/models/chaite/processors_storage.js index dd55a47..684d98c 100644 --- a/models/chaite/processors_storage.js +++ b/models/chaite/processors_storage.js @@ -64,6 +64,43 @@ export class LowDBProcessorsStorage extends ChaiteStorage { return list.map(item => new ProcessorDTO({}).fromString(JSON.stringify(item))) } + /** + * + * @param {Record} filter + * @returns {Promise} + */ + async listItemsByEqFilter (filter) { + const allList = await this.listItems() + return allList.filter(item => { + for (const key in filter) { + if (item[key] !== filter[key]) { + return false + } + } + return true + }) + } + + /** + * + * @param {Array<{ + * field: string; + * values: unknown[]; + * }>} query + * @returns {Promise} + */ + async listItemsByInQuery (query) { + const allList = await this.listItems() + return allList.filter(item => { + for (const { field, values } of query) { + if (!values.includes(item[field])) { + return false + } + } + return true + }) + } + async clear () { await this.collection.deleteAll() } diff --git a/models/chaite/tools_storage.js b/models/chaite/tools_storage.js index 9f4ed05..f76246c 100644 --- a/models/chaite/tools_storage.js +++ b/models/chaite/tools_storage.js @@ -64,6 +64,43 @@ export class LowDBToolsStorage extends ChaiteStorage { return list.map(item => new ToolDTO({}).fromString(JSON.stringify(item))) } + /** + * + * @param {Record} filter + * @returns {Promise} + */ + async listItemsByEqFilter (filter) { + const allList = await this.listItems() + return allList.filter(item => { + for (const key in filter) { + if (item[key] !== filter[key]) { + return false + } + } + return true + }) + } + + /** + * + * @param {Array<{ + * field: string; + * values: unknown[]; + * }>} query + * @returns {Promise} + */ + async listItemsByInQuery (query) { + const allList = await this.listItems() + return allList.filter(item => { + for (const { field, values } of query) { + if (!values.includes(item[field])) { + return false + } + } + return true + }) + } + async clear () { await this.collection.deleteAll() } diff --git a/package.json b/package.json index 3f563ae..a18c6d7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "module", "author": "ikechan8370", "dependencies": { - "chaite": "^1.3.8", + "chaite": "^1.4.0", "js-yaml": "^4.1.0", "keyv": "^5.3.1", "keyv-file": "^5.1.2", diff --git a/utils/command.js b/utils/command.js index 3002f51..94a3088 100644 --- a/utils/command.js +++ b/utils/command.js @@ -47,6 +47,70 @@ export function createCRUDCommandRules (cmdPrefix, name, variable, detail = true e.reply(`${name}不存在`) } } + rules.push({ + reg: cmdPrefix + `上传${name}(.*)`, + fnc: `upload${upperVariable}` + }) + this[`upload${upperVariable}`] = async function (e) { + const match = e.msg.match(new RegExp(cmdPrefix + `上传${name}(.*)`)) + if (match) { + const id = match[1].trim() + console.log(id) + const instance = await manager.getInstanceT(id) + if (instance) { + const result = await manager.shareToCloud(id) + if (result) { + e.reply(`上传成功,云端${name}ID为:${result}`) + } else { + e.reply('上传失败') + } + } else { + e.reply(`${name}不存在`) + } + } + } + rules.push({ + reg: cmdPrefix + `浏览云端${name}(.*?)(页码(\\d+))?$`, + fnc: `listCloud${upperVariable}` + }) + this[`listCloud${upperVariable}`] = async function (e) { + const match = e.msg.match(new RegExp(cmdPrefix + `浏览云端${name}(.*?)(页码(\\d+))?$`)) + if (match) { + const query = match[1].trim() + const page = match[3] ? parseInt(match[3]) : 1 // 如果没有指定页码,默认为第1页 + + const result = await manager?.listFromCloud({}, query, { + page, + pageSize: 10, + searchFields: ['name', 'description'] + }) + + logger.debug(result) + if (result.items && result.items.length > 0) { + const msgs = result.items.map(i => i.toFormatedString(!e.isGroup)) + + // 构建分页信息 + const { currentPage, totalPages, totalItems, pageSize } = result.pagination + const pageInfo = `云端${name}查询结果 - 第${currentPage}/${totalPages}页,共${totalItems}条,每页${pageSize}条` + + // 添加翻页提示 + let pageHint = '' + if (result.pagination.hasNextPage) { + pageHint += `\n发送 ${cmdPrefix}浏览云端${name}${query || ''}页码${currentPage + 1} 查看下一页` + } + if (result.pagination.hasPreviousPage) { + pageHint += `\n发送 ${cmdPrefix}浏览云端${name}${query || ''}页码${currentPage - 1} 查看上一页` + } + + const forwardedMsg = await common.makeForwardMsg(e, msgs, pageInfo + pageHint) + e.reply(forwardedMsg) + } else { + e.reply(`未找到相关的云端${name}或页码超出范围`) + } + } else { + e.reply(`格式错误,正确格式:${cmdPrefix}浏览云端${name}[关键词][页码数字]`) + } + } } // todo // 定义对应的函数 @@ -72,10 +136,10 @@ export function createCRUDCommandRules (cmdPrefix, name, variable, detail = true if (manager) { this.reply(`暂不支持添加${name},请使用后台管理面板添加`) } else { + const id = e.msg.replace(new RegExp(cmdPrefix + `(添加|新增)${name}`), '') if (variable in ChatGPTConfig.llm) { /** @type {string[]} */ const list = ChatGPTConfig.llm[variable] - const id = e.msg.replace(new RegExp(cmdPrefix + `(添加|新增)${name}`), '') if (list.indexOf(id) > 0) { e.reply(`${name}已存在`) } else {