chore: update the agent model trigger

pull/13639/head
Yi 1 year ago
parent 316c418a01
commit 0c62e000cd

@ -1,6 +1,7 @@
import type { FC } from 'react' import type { FC } from 'react'
import { useEffect, useMemo, useState } from 'react' import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useQuery } from '@tanstack/react-query'
import type { import type {
ModelItem, ModelItem,
ModelProvider, ModelProvider,
@ -9,7 +10,6 @@ import {
CustomConfigurationStatusEnum, CustomConfigurationStatusEnum,
ModelTypeEnum, ModelTypeEnum,
} from '../declarations' } from '../declarations'
import type { PluginInfoFromMarketPlace } from '@/app/components/plugins/types'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins' import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
import ConfigurationButton from './configuration-button' import ConfigurationButton from './configuration-button'
import Loading from '@/app/components/base/loading' import Loading from '@/app/components/base/loading'
@ -66,43 +66,44 @@ const AgentModelTrigger: FC<AgentModelTriggerProps> = ({
needsConfiguration, needsConfiguration,
} }
}, [modelProviders, providerName]) }, [modelProviders, providerName])
const [pluginInfo, setPluginInfo] = useState<PluginInfoFromMarketPlace | null>(null)
const [isPluginChecked, setIsPluginChecked] = useState(false)
const [installed, setInstalled] = useState(false) const [installed, setInstalled] = useState(false)
const [inModelList, setInModelList] = useState(false)
const invalidateInstalledPluginList = useInvalidateInstalledPluginList() const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
const handleOpenModal = useModelModalHandler() const handleOpenModal = useModelModalHandler()
useEffect(() => { const { data: inModelList = false } = useQuery({
(async () => { queryKey: ['modelInList', currentProvider?.provider, modelId],
if (modelId && currentProvider) { queryFn: async () => {
try { if (!modelId || !currentProvider) return false
const modelsData = await fetchModelProviderModelList(`/workspaces/current/model-providers/${currentProvider?.provider}/models`) try {
if (modelId && modelsData.data.find(item => item.model === modelId)) const modelsData = await fetchModelProviderModelList(`/workspaces/current/model-providers/${currentProvider?.provider}/models`)
setInModelList(true) return !!modelId && !!modelsData.data.find(item => item.model === modelId)
}
catch (error) {
// pass
}
} }
if (providerName) { catch (error) {
const parts = providerName.split('/') return false
const org = parts[0]
const name = parts[1]
try {
const pluginInfo = await fetchPluginInfoFromMarketPlace({ org, name })
if (pluginInfo.data.plugin.category === PluginType.model)
setPluginInfo(pluginInfo.data.plugin)
}
catch (error) {
// pass
}
} }
setIsPluginChecked(true) },
})() enabled: !!modelId && !!currentProvider,
}, [providerName, modelId, currentProvider]) })
if (modelId && !isPluginChecked) const { data: pluginInfo, isLoading: isPluginLoading } = useQuery({
queryKey: ['pluginInfo', providerName],
queryFn: async () => {
if (!providerName) return null
const parts = providerName.split('/')
const org = parts[0]
const name = parts[1]
try {
const response = await fetchPluginInfoFromMarketPlace({ org, name })
return response.data.plugin.category === PluginType.model ? response.data.plugin : null
}
catch (error) {
return null
}
},
enabled: !!providerName,
})
if (modelId && isPluginLoading)
return <Loading /> return <Loading />
return ( return (

Loading…
Cancel
Save