chore: avoid unnecessary loading for the model selector in agent node

pull/12547/head
Yi 1 year ago
parent dfe69a9c13
commit 3bdaf2dcae

@ -67,42 +67,71 @@ const AgentModelTrigger: FC<AgentModelTriggerProps> = ({
} }
}, [modelProviders, providerName]) }, [modelProviders, providerName])
const [pluginInfo, setPluginInfo] = useState<PluginInfoFromMarketPlace | null>(null) const [pluginInfo, setPluginInfo] = useState<PluginInfoFromMarketPlace | null>(null)
const [isPluginChecked, setIsPluginChecked] = useState(false) const [isPluginChecked, setIsPluginChecked] = useState(!!modelProvider)
const [installed, setInstalled] = useState(false) const [installed, setInstalled] = useState(false)
const [inModelList, setInModelList] = useState(false) const [inModelList, setInModelList] = useState(false)
const invalidateInstalledPluginList = useInvalidateInstalledPluginList() const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
const handleOpenModal = useModelModalHandler() const handleOpenModal = useModelModalHandler()
const checkPluginInfo = useMemo(async () => {
if (!providerName || !modelId)
return null
const parts = providerName.split('/')
try {
const pluginInfo = await fetchPluginInfoFromMarketPlace({
org: parts[0],
name: parts[1],
})
if (pluginInfo.data.plugin.category === PluginType.model)
return pluginInfo.data.plugin
}
catch (error) {
// pass
}
return null
}, [providerName, modelId])
const checkModelList = useMemo(async () => {
if (!modelId || !currentProvider)
return false
try {
const modelsData = await fetchModelProviderModelList(
`/workspaces/current/model-providers/${currentProvider?.provider}/models`,
)
return !!modelsData.data.find(item => item.model === modelId)
}
catch (error) {
// pass
}
return false
}, [modelId, currentProvider])
useEffect(() => { useEffect(() => {
(async () => { let isSubscribed = true
if (modelId && currentProvider) {
try { const initializeChecks = async () => {
const modelsData = await fetchModelProviderModelList(`/workspaces/current/model-providers/${currentProvider?.provider}/models`) if (!isPluginChecked) {
if (modelId && modelsData.data.find(item => item.model === modelId)) const [pluginResult, modelListResult] = await Promise.all([
setInModelList(true) checkPluginInfo,
} checkModelList,
catch (error) { ])
// pass
} if (isSubscribed) {
} if (pluginResult)
if (providerName) { setPluginInfo(pluginResult)
const parts = providerName.split('/') setInModelList(modelListResult)
const org = parts[0] setIsPluginChecked(true)
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)
} }
else { }
setIsPluginChecked(true)
} initializeChecks()
})()
}, [providerName, modelId, currentProvider]) return () => {
isSubscribed = false
}
}, [checkPluginInfo, checkModelList, isPluginChecked, modelId, currentProvider])
if (modelId && !isPluginChecked) if (modelId && !isPluginChecked)
return <Loading /> return <Loading />

Loading…
Cancel
Save