diff --git a/package.json b/package.json index f4d7d57..a51ae1d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "scripts": { "build": "npm run clean && bun bun.config.mjs", "dev": "bun run --watch bun.config.mjs", - "test": "tsx test/**/*.ts", "clean": "rm -rf dist", "pub": "envision pack -p -u" }, diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml deleted file mode 100644 index efc037a..0000000 --- a/pnpm-workspace.yaml +++ /dev/null @@ -1,2 +0,0 @@ -onlyBuiltDependencies: - - esbuild diff --git a/src/provider/chat-adapter/cnb.ts b/src/provider/chat-adapter/cnb.ts index c4389eb..240124a 100644 --- a/src/provider/chat-adapter/cnb.ts +++ b/src/provider/chat-adapter/cnb.ts @@ -6,7 +6,7 @@ export class CNBChat extends BaseChat { repo: string; constructor(options: CNBOptions & { repo: string }) { const baseURL = CNBChat.BASE_URL.replace('{repo}', options.repo); - super({ model: "hunyuan-a13b", ...(options as BaseChatOptions), baseURL: baseURL }); + super({ model: "CNB-Models", ...(options as BaseChatOptions), baseURL: baseURL }); } async query(params: CNBQueryParam): Promise> { const url = this.baseURL.replace('/ai', '/knowledge/base/query'); diff --git a/src/provider/index.ts b/src/provider/index.ts index b39df7c..dafec72 100644 --- a/src/provider/index.ts +++ b/src/provider/index.ts @@ -2,6 +2,4 @@ export * from './chat.ts'; export * from './knowledge.ts'; -export * from './utils/index.ts'; - export { AIUtils } from './core/utils/index.ts'; \ No newline at end of file diff --git a/src/provider/utils/ai-config-type.ts b/src/provider/utils/ai-config-type.ts deleted file mode 100644 index a4ed0c6..0000000 --- a/src/provider/utils/ai-config-type.ts +++ /dev/null @@ -1,52 +0,0 @@ -import type { Permission } from '@kevisual/permission'; - -export type AIModel = { - /** - * 提供商 - */ - provider: string; - /** - * 模型名称 - */ - model: string; - /** - * 模型组 - */ - group: string; - /** - * 每日请求频率限制 - */ - dayLimit?: number; - /** - * 总的token限制 - */ - tokenLimit?: number; -}; -export type SecretKey = { - /** - * 组 - */ - group: string; - /** - * API密钥 - */ - apiKey: string; - /** - * 解密密钥 - */ - decryptKey?: string; -}; - -export type AIConfig = { - title?: string; - description?: string; - models: AIModel[]; - secretKeys: SecretKey[]; - permission?: Permission; - filter?: { - objectKey: string; - type: 'array' | 'object'; - operate: 'removeAttribute' | 'remove'; - attribute: string[]; - }[]; -}; diff --git a/src/provider/utils/index.ts b/src/provider/utils/index.ts deleted file mode 100644 index 626238c..0000000 --- a/src/provider/utils/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './ai-config-type.ts'; -export * from './parse-config.ts'; diff --git a/src/provider/utils/parse-config.ts b/src/provider/utils/parse-config.ts deleted file mode 100644 index cb4df60..0000000 --- a/src/provider/utils/parse-config.ts +++ /dev/null @@ -1,192 +0,0 @@ -import AES from 'crypto-js/aes.js'; -import Utf8 from 'crypto-js/enc-utf8.js'; -import type { AIConfig } from './ai-config-type.js'; -const CryptoJS = { AES, enc: { Utf8 } }; -// 加密函数 -export function encryptAES(plainText: string, secretKey: string) { - return CryptoJS.AES.encrypt(plainText, secretKey).toString(); -} - -// 解密函数 -export function decryptAES(cipherText: string, secretKey: string) { - const bytes = CryptoJS.AES.decrypt(cipherText, secretKey); - return bytes.toString(CryptoJS.enc.Utf8); -} - -type AIModel = { - /** - * 提供商 - */ - provider: string; - /** - * 模型名称 - */ - model: string; - /** - * 模型组 - */ - group: string; - /** - * 每日请求频率限制 - */ - dayLimit?: number; - /** - * 总的token限制 - */ - tokenLimit?: number; -}; - -type SecretKey = { - /** - * 组 - */ - group: string; - /** - * API密钥 - */ - apiKey: string; - /** - * 解密密钥 - */ - decryptKey?: string; -}; - -export type GetProviderOpts = { - model: string; - group: string; - decryptKey?: string; -}; -export type ProviderResult = { - provider: string; - model: string; - group: string; - apiKey: string; - dayLimit?: number; - tokenLimit?: number; - baseURL?: string; - /** - * 解密密钥 - */ - decryptKey?: string; -}; - -export class AIConfigParser { - private config: AIConfig; - result: ProviderResult; - constructor(config: AIConfig) { - this.config = config; - } - /** - * 获取模型配置 - * @param opts - * @returns - */ - getProvider(opts: GetProviderOpts): ProviderResult { - const { model, group, decryptKey } = opts; - const modelConfig = this.config.models.find((m) => m.model === model && m.group === group); - const groupConfig = this.config.secretKeys.find((m) => m.group === group); - if (!modelConfig) { - throw new Error(`在模型组 ${group} 中未找到模型 ${model}`); - } - const mergeConfig = { - ...modelConfig, - ...groupConfig, - decryptKey: decryptKey || groupConfig?.decryptKey, - }; - // 验证模型配置 - if (!mergeConfig.provider) { - throw new Error(`模型 ${model} 未配置提供商`); - } - if (!mergeConfig.model) { - throw new Error(`模型 ${model} 未配置模型名称`); - } - if (!mergeConfig.apiKey) { - throw new Error(`组 ${group} 未配置 API 密钥`); - } - if (!mergeConfig.group) { - throw new Error(`组 ${group} 未配置`); - } - this.result = mergeConfig; - return mergeConfig; - } - /** - * 获取解密密钥 - * @param opts - * @returns - */ - async getSecretKey(opts?: { - getCache?: (key: string) => Promise; - setCache?: (key: string, value: string) => Promise; - providerResult?: ProviderResult; - }) { - const { getCache, setCache, providerResult } = opts || {}; - const { apiKey, decryptKey, group = '', model } = providerResult || this.result; - const cacheKey = `${group}--${model}`; - if (!decryptKey) { - return apiKey; - } - if (getCache) { - const cache = await getCache(cacheKey); - if (cache) { - return cache; - } - } - const secretKey = decryptAES(apiKey, decryptKey); - if (setCache) { - await setCache(cacheKey, secretKey); - } - return secretKey; - } - /** - * 加密 - * @param plainText - * @param secretKey - * @returns - */ - encrypt(plainText: string, secretKey: string) { - return encryptAES(plainText, secretKey); - } - /** - * 解密 - * @param cipherText - * @param secretKey - * @returns - */ - decrypt(cipherText: string, secretKey: string) { - return decryptAES(cipherText, secretKey); - } - - /** - * 获取模型配置 - * @returns - */ - getSelectOpts() { - const { models, secretKeys = [] } = this.config; - - return models.map((model) => { - const selectOpts = secretKeys.find((m) => m.group === model.group); - return { - ...model, - ...selectOpts, - }; - }); - } - getConfig(keepSecret?: boolean, config?: AIConfig) { - const chatConfig = config ?? this.config; - if (keepSecret) { - return chatConfig; - } - // 过滤掉secret中的所有apiKey,移除掉并返回chatConfig - const { secretKeys = [], ...rest } = chatConfig || {}; - return { - ...rest, - secretKeys: secretKeys.map((item) => { - return { - ...item, - apiKey: undefined, - decryptKey: undefined, - }; - }), - }; - } -}