mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-19 19:59:10 +08:00
chore: vision logic hooks
This commit is contained in:
parent
6c9a6b99e0
commit
ef93d60534
77
web/app/components/workflow/hooks/use-config-vision.ts
Normal file
77
web/app/components/workflow/hooks/use-config-vision.ts
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import produce from 'immer'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
import type { ModelConfig, VisionSetting } from '@/app/components/workflow/types'
|
||||||
|
import { useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||||
|
import {
|
||||||
|
ModelFeatureEnum,
|
||||||
|
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
|
import { Resolution } from '@/types/app'
|
||||||
|
|
||||||
|
type Payload = {
|
||||||
|
enabled: boolean
|
||||||
|
configs?: VisionSetting
|
||||||
|
}
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
payload: Payload
|
||||||
|
onChange: (payload: Payload) => void
|
||||||
|
}
|
||||||
|
const useConfigVision = (model: ModelConfig, {
|
||||||
|
payload,
|
||||||
|
onChange,
|
||||||
|
}: Params) => {
|
||||||
|
const {
|
||||||
|
currentModel: currModel,
|
||||||
|
} = useTextGenerationCurrentProviderAndModelAndModelList(
|
||||||
|
{
|
||||||
|
provider: model.provider,
|
||||||
|
model: model.name,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
const getIsVisionModel = useCallback(() => {
|
||||||
|
return !!currModel?.features?.includes(ModelFeatureEnum.vision)
|
||||||
|
}, [currModel])
|
||||||
|
|
||||||
|
const isVisionModel = getIsVisionModel()
|
||||||
|
|
||||||
|
const handleVisionResolutionEnabledChange = useCallback((enabled: boolean) => {
|
||||||
|
const newPayload = produce(payload, (draft) => {
|
||||||
|
draft.enabled = enabled
|
||||||
|
})
|
||||||
|
onChange(newPayload)
|
||||||
|
}, [onChange, payload])
|
||||||
|
|
||||||
|
const handleVisionResolutionChange = useCallback((config: VisionSetting) => {
|
||||||
|
const newPayload = produce(payload, (draft) => {
|
||||||
|
draft.configs = config
|
||||||
|
})
|
||||||
|
onChange(newPayload)
|
||||||
|
}, [onChange, payload])
|
||||||
|
|
||||||
|
const handleModelChanged = useCallback(() => {
|
||||||
|
const isVisionModel = getIsVisionModel()
|
||||||
|
if (!isVisionModel) {
|
||||||
|
handleVisionResolutionEnabledChange(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (payload.enabled) {
|
||||||
|
onChange({
|
||||||
|
enabled: true,
|
||||||
|
configs: {
|
||||||
|
detail: Resolution.high,
|
||||||
|
valueSelector: [],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [getIsVisionModel, handleVisionResolutionEnabledChange, onChange, payload.enabled])
|
||||||
|
|
||||||
|
return {
|
||||||
|
isVisionModel,
|
||||||
|
handleVisionResolutionEnabledChange,
|
||||||
|
handleVisionResolutionChange,
|
||||||
|
handleModelChanged,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useConfigVision
|
@ -1,18 +1,17 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import { EditionType, VarType } from '../../types'
|
import { EditionType, VarType } from '../../types'
|
||||||
import type { Memory, PromptItem, ValueSelector, Var, Variable, VisionSetting } from '../../types'
|
import type { Memory, PromptItem, ValueSelector, Var, Variable } from '../../types'
|
||||||
import { useStore } from '../../store'
|
import { useStore } from '../../store'
|
||||||
import {
|
import {
|
||||||
useIsChatMode,
|
useIsChatMode,
|
||||||
useNodesReadOnly,
|
useNodesReadOnly,
|
||||||
} from '../../hooks'
|
} from '../../hooks'
|
||||||
import useAvailableVarList from '../_base/hooks/use-available-var-list'
|
import useAvailableVarList from '../_base/hooks/use-available-var-list'
|
||||||
|
import useConfigVision from '../../hooks/use-config-vision'
|
||||||
import type { LLMNodeType } from './types'
|
import type { LLMNodeType } from './types'
|
||||||
import { Resolution } from '@/types/app'
|
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||||
import { useModelListAndDefaultModelAndCurrentProviderAndModel, useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
|
||||||
import {
|
import {
|
||||||
ModelFeatureEnum,
|
|
||||||
ModelTypeEnum,
|
ModelTypeEnum,
|
||||||
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||||
@ -109,6 +108,21 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
|||||||
currentModel,
|
currentModel,
|
||||||
} = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
|
} = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
|
||||||
|
|
||||||
|
const {
|
||||||
|
isVisionModel,
|
||||||
|
handleVisionResolutionEnabledChange,
|
||||||
|
handleVisionResolutionChange,
|
||||||
|
handleModelChanged: handleVisionConfigAfterModelChanged,
|
||||||
|
} = useConfigVision(model, {
|
||||||
|
payload: inputs.vision,
|
||||||
|
onChange: (newPayload) => {
|
||||||
|
const newInputs = produce(inputs, (draft) => {
|
||||||
|
draft.vision = newPayload
|
||||||
|
})
|
||||||
|
setInputs(newInputs)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
|
const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
|
||||||
const newInputs = produce(inputRef.current, (draft) => {
|
const newInputs = produce(inputRef.current, (draft) => {
|
||||||
draft.model.provider = model.provider
|
draft.model.provider = model.provider
|
||||||
@ -139,43 +153,12 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
|||||||
setInputs(newInputs)
|
setInputs(newInputs)
|
||||||
}, [inputs, setInputs])
|
}, [inputs, setInputs])
|
||||||
|
|
||||||
const {
|
|
||||||
currentModel: currModel,
|
|
||||||
} = useTextGenerationCurrentProviderAndModelAndModelList(
|
|
||||||
{
|
|
||||||
provider: model.provider,
|
|
||||||
model: model.name,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
const isVisionModel = !!currModel?.features?.includes(ModelFeatureEnum.vision)
|
|
||||||
// change to vision model to set vision enabled, else disabled
|
// change to vision model to set vision enabled, else disabled
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!modelChanged)
|
if (!modelChanged)
|
||||||
return
|
return
|
||||||
setModelChanged(false)
|
setModelChanged(false)
|
||||||
if (!isVisionModel) {
|
handleVisionConfigAfterModelChanged()
|
||||||
const newInputs = produce(inputs, (draft) => {
|
|
||||||
draft.vision = {
|
|
||||||
enabled: false,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
setInputs(newInputs)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!inputs.vision?.enabled) {
|
|
||||||
const newInputs = produce(inputs, (draft) => {
|
|
||||||
if (!draft.vision?.enabled) {
|
|
||||||
draft.vision = {
|
|
||||||
enabled: true,
|
|
||||||
configs: {
|
|
||||||
detail: Resolution.high,
|
|
||||||
valueSelector: [],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
setInputs(newInputs)
|
|
||||||
}
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [isVisionModel, modelChanged])
|
}, [isVisionModel, modelChanged])
|
||||||
|
|
||||||
@ -294,27 +277,6 @@ const useConfig = (id: string, payload: LLMNodeType) => {
|
|||||||
setInputs(newInputs)
|
setInputs(newInputs)
|
||||||
}, [inputs, setInputs])
|
}, [inputs, setInputs])
|
||||||
|
|
||||||
const handleVisionResolutionEnabledChange = useCallback((enabled: boolean) => {
|
|
||||||
const newInputs = produce(inputs, (draft) => {
|
|
||||||
if (!draft.vision) {
|
|
||||||
draft.vision = {
|
|
||||||
enabled,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
draft.vision.enabled = enabled
|
|
||||||
}
|
|
||||||
})
|
|
||||||
setInputs(newInputs)
|
|
||||||
}, [inputs, setInputs])
|
|
||||||
|
|
||||||
const handleVisionResolutionChange = useCallback((config: VisionSetting) => {
|
|
||||||
const newInputs = produce(inputs, (draft) => {
|
|
||||||
draft.vision.configs = config
|
|
||||||
})
|
|
||||||
setInputs(newInputs)
|
|
||||||
}, [inputs, setInputs])
|
|
||||||
|
|
||||||
const filterInputVar = useCallback((varPayload: Var) => {
|
const filterInputVar = useCallback((varPayload: Var) => {
|
||||||
return [VarType.number, VarType.string, VarType.secret].includes(varPayload.type)
|
return [VarType.number, VarType.string, VarType.secret].includes(varPayload.type)
|
||||||
}, [])
|
}, [])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user