feat: integrate dynamic options fetching in VarReferencePicker and related components

pull/21425/head
Yeuoly 11 months ago
parent e9887adcf6
commit f0626f44f6

@ -17,7 +17,8 @@ import VarReferencePopup from './var-reference-popup'
import { getNodeInfoById, isConversationVar, isENV, isSystemVar, varTypeToStructType } from './utils' import { getNodeInfoById, isConversationVar, isENV, isSystemVar, varTypeToStructType } from './utils'
import ConstantField from './constant-field' import ConstantField from './constant-field'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import type { Node, NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types' import type { Node, NodeOutPutVar, ToolWithProvider, ValueSelector, Var } from '@/app/components/workflow/types'
import type { CredentialFormSchemaSelect } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { type CredentialFormSchema, type FormOption, FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { type CredentialFormSchema, type FormOption, FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { BlockEnum } from '@/app/components/workflow/types' import { BlockEnum } from '@/app/components/workflow/types'
import { VarBlockIcon } from '@/app/components/workflow/block-icon' import { VarBlockIcon } from '@/app/components/workflow/block-icon'
@ -41,6 +42,8 @@ import Tooltip from '@/app/components/base/tooltip'
import { isExceptionVariable } from '@/app/components/workflow/utils' import { isExceptionVariable } from '@/app/components/workflow/utils'
import VarFullPathPanel from './var-full-path-panel' import VarFullPathPanel from './var-full-path-panel'
import { noop } from 'lodash-es' import { noop } from 'lodash-es'
import { useFetchDynamicOptions } from '@/service/use-plugins'
import type { Tool } from '@/app/components/tools/types'
const TRIGGER_DEFAULT_WIDTH = 227 const TRIGGER_DEFAULT_WIDTH = 227
@ -69,7 +72,8 @@ type Props = {
minWidth?: number minWidth?: number
popupFor?: 'assigned' | 'toAssigned' popupFor?: 'assigned' | 'toAssigned'
zIndex?: number zIndex?: number
isLoading?: boolean currentTool?: Tool
currentProvider?: ToolWithProvider
} }
const DEFAULT_VALUE_SELECTOR: Props['value'] = [] const DEFAULT_VALUE_SELECTOR: Props['value'] = []
@ -99,6 +103,8 @@ const VarReferencePicker: FC<Props> = ({
minWidth, minWidth,
popupFor, popupFor,
zIndex, zIndex,
currentTool,
currentProvider,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const store = useStoreApi() const store = useStoreApi()
@ -321,33 +327,24 @@ const VarReferencePicker: FC<Props> = ({
const [dynamicOptions, setDynamicOptions] = useState<FormOption[] | null>(null) const [dynamicOptions, setDynamicOptions] = useState<FormOption[] | null>(null)
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const handleOpenDynamicSelect = useCallback((open: boolean) => { const { mutateAsync: fetchDynamicOptions } = useFetchDynamicOptions(
if (schema?.type !== FormTypeEnum.dynamicSelect || !open || isLoading) currentProvider?.plugin_id || '', currentProvider?.name || '', currentTool?.name || '', (schema as CredentialFormSchemaSelect).variable,
return )
if (dynamicOptions) const handleFetchDynamicOptions = async () => {
if (schema?.type !== FormTypeEnum.dynamicSelect || !currentTool || !currentProvider)
return return
setIsLoading(true) setIsLoading(true)
// load options try {
setTimeout(() => { const data = await fetchDynamicOptions()
setDynamicOptions(data?.data?.options || [])
}
finally {
setIsLoading(false) setIsLoading(false)
setDynamicOptions([{ }
label: { }
en_US: 'Option 1', useEffect(() => {
zh_Hans: '选项1', handleFetchDynamicOptions()
}, }, [currentTool, currentProvider, schema])
value: 'option1',
show_on: [],
}, {
label: {
en_US: 'Option 2',
zh_Hans: '选项2',
},
value: 'option2',
show_on: [],
}])
}, 1000)
}, [isLoading])
const schemaWithDynamicSelect = useMemo(() => { const schemaWithDynamicSelect = useMemo(() => {
if (schema?.type !== FormTypeEnum.dynamicSelect) if (schema?.type !== FormTypeEnum.dynamicSelect)
@ -414,7 +411,6 @@ const VarReferencePicker: FC<Props> = ({
onChange={onChange as ((value: string | number, varKindType: VarKindType, varInfo?: Var) => void)} onChange={onChange as ((value: string | number, varKindType: VarKindType, varInfo?: Var) => void)}
schema={schemaWithDynamicSelect as CredentialFormSchema} schema={schemaWithDynamicSelect as CredentialFormSchema}
readonly={readonly} readonly={readonly}
onOpenChange={handleOpenDynamicSelect}
isLoading={isLoading} isLoading={isLoading}
/> />
) )

@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next'
import type { ToolVarInputs } from '../types' import type { ToolVarInputs } from '../types'
import { VarType as VarKindType } from '../types' import { VarType as VarKindType } from '../types'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import type { ValueSelector, Var } from '@/app/components/workflow/types' import type { ToolWithProvider, ValueSelector, Var } from '@/app/components/workflow/types'
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
@ -17,6 +17,7 @@ import { VarType } from '@/app/components/workflow/types'
import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector' import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector' import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
import { noop } from 'lodash-es' import { noop } from 'lodash-es'
import type { Tool } from '@/app/components/tools/types'
type Props = { type Props = {
readOnly: boolean readOnly: boolean
@ -27,6 +28,8 @@ type Props = {
onOpen?: (index: number) => void onOpen?: (index: number) => void
isSupportConstantValue?: boolean isSupportConstantValue?: boolean
filterVar?: (payload: Var, valueSelector: ValueSelector) => boolean filterVar?: (payload: Var, valueSelector: ValueSelector) => boolean
currentTool?: Tool
currentProvider?: ToolWithProvider
} }
const InputVarList: FC<Props> = ({ const InputVarList: FC<Props> = ({
@ -38,6 +41,8 @@ const InputVarList: FC<Props> = ({
onOpen = noop, onOpen = noop,
isSupportConstantValue, isSupportConstantValue,
filterVar, filterVar,
currentTool,
currentProvider,
}) => { }) => {
const language = useLanguage() const language = useLanguage()
const { t } = useTranslation() const { t } = useTranslation()
@ -207,6 +212,8 @@ const InputVarList: FC<Props> = ({
filterVar={isNumber ? filterVar : undefined} filterVar={isNumber ? filterVar : undefined}
availableVars={isSelect ? availableVars : undefined} availableVars={isSelect ? availableVars : undefined}
schema={schema} schema={schema}
currentTool={currentTool}
currentProvider={currentProvider}
/> />
)} )}
{isFile && ( {isFile && (

@ -42,6 +42,7 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
isLoading, isLoading,
outputSchema, outputSchema,
hasObjectOutput, hasObjectOutput,
currTool,
} = useConfig(id, data) } = useConfig(id, data)
if (isLoading) { if (isLoading) {
@ -80,6 +81,8 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
filterVar={filterVar} filterVar={filterVar}
isSupportConstantValue isSupportConstantValue
onOpen={handleOnVarOpen} onOpen={handleOnVarOpen}
currentProvider={currCollection}
currentTool={currTool}
/> />
</Field> </Field>
)} )}

@ -1,5 +1,6 @@
import { useCallback, useEffect } from 'react' import { useCallback, useEffect } from 'react'
import type { import type {
FormOption,
ModelProvider, ModelProvider,
} from '@/app/components/header/account-setting/model-provider-page/declarations' } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { fetchModelProviderModelList } from '@/service/common' import { fetchModelProviderModelList } from '@/service/common'
@ -571,3 +572,16 @@ export const usePluginInfo = (providerName?: string) => {
enabled: !!providerName, enabled: !!providerName,
}) })
} }
export const useFetchDynamicOptions = (plugin_id: string, provider: string, action: string, parameter: string) => {
return useMutation({
mutationFn: () => get<{ data: { options: FormOption[] } }>('/workspaces/current/plugin/parameters/dynamic-options', {
params: {
plugin_id,
provider,
action,
parameter,
},
}),
})
}

Loading…
Cancel
Save