import { useMemo } from 'react' import { useTranslation } from 'react-i18next' import SystemModelSelector from './system-model-selector' import ProviderAddedCard, { UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST } from './provider-added-card' import ProviderCard from './provider-card' import type { CustomConfigurationModelFixedFields, ModelProvider, } from './declarations' import { ConfigurationMethodEnum, CustomConfigurationStatusEnum, ModelTypeEnum, } from './declarations' import { useDefaultModel, useUpdateModelList, useUpdateModelProviders, } from './hooks' import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback' import { useProviderContext } from '@/context/provider-context' import { useModalContextSelector } from '@/context/modal-context' import { useEventEmitterContextContext } from '@/context/event-emitter' const ModelProviderPage = () => { const { t } = useTranslation() const { eventEmitter } = useEventEmitterContextContext() const updateModelProviders = useUpdateModelProviders() const updateModelList = useUpdateModelList() const { data: textGenerationDefaultModel } = useDefaultModel(ModelTypeEnum.textGeneration) const { data: embeddingsDefaultModel } = useDefaultModel(ModelTypeEnum.textEmbedding) const { data: rerankDefaultModel } = useDefaultModel(ModelTypeEnum.rerank) const { data: speech2textDefaultModel } = useDefaultModel(ModelTypeEnum.speech2text) const { data: ttsDefaultModel } = useDefaultModel(ModelTypeEnum.tts) const { modelProviders: providers } = useProviderContext() const setShowModelModal = useModalContextSelector(state => state.setShowModelModal) const defaultModelNotConfigured = !textGenerationDefaultModel && !embeddingsDefaultModel && !speech2textDefaultModel && !rerankDefaultModel && !ttsDefaultModel const [configuredProviders, notConfiguredProviders] = useMemo(() => { const configuredProviders: ModelProvider[] = [] const notConfiguredProviders: ModelProvider[] = [] providers.forEach((provider) => { if ( provider.custom_configuration.status === CustomConfigurationStatusEnum.active || ( provider.system_configuration.enabled === true && provider.system_configuration.quota_configurations.find(item => item.quota_type === provider.system_configuration.current_quota_type) ) ) configuredProviders.push(provider) else notConfiguredProviders.push(provider) }) return [configuredProviders, notConfiguredProviders] }, [providers]) const handleOpenModal = ( provider: ModelProvider, configurateMethod: ConfigurationMethodEnum, CustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields, ) => { setShowModelModal({ payload: { currentProvider: provider, currentConfigurationMethod: configurateMethod, currentCustomConfigurationModelFixedFields: CustomConfigurationModelFixedFields, }, onSaveCallback: () => { updateModelProviders() if (configurateMethod === ConfigurationMethodEnum.predefinedModel) { provider.supported_model_types.forEach((type) => { updateModelList(type) }) } if (configurateMethod === ConfigurationMethodEnum.customizableModel && provider.custom_configuration.status === CustomConfigurationStatusEnum.active) { eventEmitter?.emit({ type: UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST, payload: provider.provider, } as any) if (CustomConfigurationModelFixedFields?.__model_type) updateModelList(CustomConfigurationModelFixedFields?.__model_type) } }, }) } return (
{ defaultModelNotConfigured ? (
{t('common.modelProvider.notConfigured')}
) :
{t('common.modelProvider.models')}
}
{ !!configuredProviders?.length && (
{ configuredProviders?.map(provider => ( handleOpenModal(provider, configurateMethod, currentCustomConfigurationModelFixedFields)} /> )) }
) } { !!notConfiguredProviders?.length && ( <>
+ {t('common.modelProvider.addMoreModelProvider')}
{ notConfiguredProviders?.map(provider => ( handleOpenModal(provider, configurateMethod)} /> )) }
) }
) } export default ModelProviderPage