feat(aiCore): introduce Hub Provider and enhance provider management

- Added a new example file demonstrating the usage of the Hub Provider for routing to multiple underlying providers.
- Implemented the Hub Provider to support model ID parsing and routing based on a specified format.
- Refactored provider management by introducing a Registry Management class for better organization and retrieval of provider instances.
- Updated the Provider Initializer to streamline the initialization and registration of providers, enhancing overall flexibility and usability.
- Removed outdated files related to provider creation and dynamic registration to simplify the codebase.
This commit is contained in:
lizhixuan
2025-08-26 00:31:41 +08:00
parent ead0e22c60
commit 53dcda6942
15 changed files with 788 additions and 803 deletions
+16 -1
View File
@@ -8,7 +8,7 @@
* 3. 暂时保持接口兼容性
*/
import { createExecutor, generateImage, StreamTextParams } from '@cherrystudio/ai-core'
import { createExecutor, generateImage, initializeProvider, StreamTextParams } from '@cherrystudio/ai-core'
import { loggerService } from '@logger'
import { isNotSupportedImageSizeModel } from '@renderer/config/models'
import { addSpan, endSpan } from '@renderer/services/SpanManagerService'
@@ -36,6 +36,21 @@ export default class ModernAiProvider {
// 只保存配置,不预先创建executor
this.config = providerToAiSdkConfig(this.actualProvider)
// 初始化 provider 到全局管理器
try {
initializeProvider(this.config.providerId, this.config.options)
logger.debug('Provider initialized successfully', {
providerId: this.config.providerId,
hasOptions: !!this.config.options
})
} catch (error) {
// 如果 provider 已经初始化过,可能会抛出错误,这里可以忽略
logger.debug('Provider initialization skipped (may already be initialized)', {
providerId: this.config.providerId,
error: error instanceof Error ? error.message : String(error)
})
}
}
public getActualProvider() {
+13 -14
View File
@@ -1,10 +1,9 @@
import { AiCore, getProviderMapping, type ProviderId } from '@cherrystudio/ai-core'
import { AiCore, type ProviderId } from '@cherrystudio/ai-core'
import { Provider } from '@renderer/types'
import { initializeNewProviders } from './providerConfigs'
// TODO
// 初始化新的Provider注册系统
initializeNewProviders()
// initializeNewProviders()
// 静态Provider映射 - 核心providers
const STATIC_PROVIDER_MAPPING: Record<string, ProviderId> = {
@@ -21,24 +20,24 @@ export function getAiSdkProviderId(provider: Provider): ProviderId | 'openai-com
if (staticProviderId) {
return staticProviderId
}
// TODO
// 2. 检查动态注册的provider映射(使用aiCore的函数)
const dynamicProviderId = getProviderMapping(provider.id)
if (dynamicProviderId) {
return dynamicProviderId as ProviderId
}
// const dynamicProviderId = getProviderMapping(provider.id)
// if (dynamicProviderId) {
// return dynamicProviderId as ProviderId
// }
// 3. 检查provider.type的静态映射
const staticProviderType = STATIC_PROVIDER_MAPPING[provider.type]
if (staticProviderType) {
return staticProviderType
}
// TODO
// 4. 检查provider.type的动态映射
const dynamicProviderType = getProviderMapping(provider.type)
if (dynamicProviderType) {
return dynamicProviderType as ProviderId
}
// const dynamicProviderType = getProviderMapping(provider.type)
// if (dynamicProviderType) {
// return dynamicProviderType as ProviderId
// }
// 5. 检查AiCore是否直接支持
if (AiCore.isSupported(provider.id)) {
@@ -1,4 +1,4 @@
import { type ProviderConfig, registerMultipleProviders } from '@cherrystudio/ai-core'
import { type ProviderConfig } from '@cherrystudio/ai-core'
import { loggerService } from '@logger'
const logger = loggerService.withContext('ProviderConfigs')
@@ -43,18 +43,19 @@ export const NEW_PROVIDER_CONFIGS: (ProviderConfig & {
}
] as const
/**
* 初始化新的Providers
* 使用aiCore的动态注册功能
*/
export async function initializeNewProviders(): Promise<void> {
try {
const successCount = registerMultipleProviders(NEW_PROVIDER_CONFIGS)
// TODO
// /**
// * 初始化新的Providers
// * 使用aiCore的动态注册功能
// */
// export async function initializeNewProviders(): Promise<void> {
// try {
// const successCount = registerMultipleProviders(NEW_PROVIDER_CONFIGS)
if (successCount < NEW_PROVIDER_CONFIGS.length) {
logger.warn('Some providers failed to register. Check previous error logs.')
}
} catch (error) {
logger.error('Failed to initialize new providers:', error as Error)
}
}
// if (successCount < NEW_PROVIDER_CONFIGS.length) {
// logger.warn('Some providers failed to register. Check previous error logs.')
// }
// } catch (error) {
// logger.error('Failed to initialize new providers:', error as Error)
// }
// }