use-config.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { useCallback, useEffect, useRef, useState } from 'react'
  2. import produce from 'immer'
  3. import { BlockEnum, VarType } from '../../types'
  4. import type { Memory, ValueSelector, Var } from '../../types'
  5. import {
  6. useIsChatMode, useNodesReadOnly,
  7. useWorkflow,
  8. } from '../../hooks'
  9. import { useStore } from '../../store'
  10. import useAvailableVarList from '../_base/hooks/use-available-var-list'
  11. import useConfigVision from '../../hooks/use-config-vision'
  12. import type { QuestionClassifierNodeType } from './types'
  13. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  14. import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
  15. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  16. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import { checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
  18. const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
  19. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  20. const isChatMode = useIsChatMode()
  21. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  22. const { getBeforeNodesInSameBranch } = useWorkflow()
  23. const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start)
  24. const startNodeId = startNode?.id
  25. const { inputs, setInputs } = useNodeCrud<QuestionClassifierNodeType>(id, payload)
  26. const inputRef = useRef(inputs)
  27. useEffect(() => {
  28. inputRef.current = inputs
  29. }, [inputs])
  30. const [modelChanged, setModelChanged] = useState(false)
  31. const {
  32. currentProvider,
  33. currentModel,
  34. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
  35. const model = inputs.model
  36. const modelMode = inputs.model?.mode
  37. const isChatModel = modelMode === 'chat'
  38. const {
  39. isVisionModel,
  40. handleVisionResolutionEnabledChange,
  41. handleVisionResolutionChange,
  42. handleModelChanged: handleVisionConfigAfterModelChanged,
  43. } = useConfigVision(model, {
  44. payload: inputs.vision,
  45. onChange: (newPayload) => {
  46. const newInputs = produce(inputs, (draft) => {
  47. draft.vision = newPayload
  48. })
  49. setInputs(newInputs)
  50. },
  51. })
  52. const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
  53. const newInputs = produce(inputRef.current, (draft) => {
  54. draft.model.provider = model.provider
  55. draft.model.name = model.modelId
  56. draft.model.mode = model.mode!
  57. })
  58. setInputs(newInputs)
  59. setModelChanged(true)
  60. }, [setInputs])
  61. useEffect(() => {
  62. if (currentProvider?.provider && currentModel?.model && !model.provider) {
  63. handleModelChanged({
  64. provider: currentProvider?.provider,
  65. modelId: currentModel?.model,
  66. mode: currentModel?.model_properties?.mode as string,
  67. })
  68. }
  69. }, [model.provider, currentProvider, currentModel, handleModelChanged])
  70. const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => {
  71. const newInputs = produce(inputs, (draft) => {
  72. draft.model.completion_params = newParams
  73. })
  74. setInputs(newInputs)
  75. }, [inputs, setInputs])
  76. // change to vision model to set vision enabled, else disabled
  77. useEffect(() => {
  78. if (!modelChanged)
  79. return
  80. setModelChanged(false)
  81. handleVisionConfigAfterModelChanged()
  82. // eslint-disable-next-line react-hooks/exhaustive-deps
  83. }, [isVisionModel, modelChanged])
  84. const handleQueryVarChange = useCallback((newVar: ValueSelector | string) => {
  85. const newInputs = produce(inputs, (draft) => {
  86. draft.query_variable_selector = newVar as ValueSelector
  87. })
  88. setInputs(newInputs)
  89. }, [inputs, setInputs])
  90. useEffect(() => {
  91. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  92. if (isReady) {
  93. let query_variable_selector: ValueSelector = []
  94. if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId)
  95. query_variable_selector = [startNodeId, 'sys.query']
  96. setInputs({
  97. ...inputs,
  98. ...defaultConfig,
  99. query_variable_selector: inputs.query_variable_selector.length > 0 ? inputs.query_variable_selector : query_variable_selector,
  100. })
  101. }
  102. // eslint-disable-next-line react-hooks/exhaustive-deps
  103. }, [defaultConfig])
  104. const handleClassesChange = useCallback((newClasses: any) => {
  105. const newInputs = produce(inputs, (draft) => {
  106. draft.classes = newClasses
  107. draft._targetBranches = newClasses
  108. })
  109. setInputs(newInputs)
  110. }, [inputs, setInputs])
  111. const filterInputVar = useCallback((varPayload: Var) => {
  112. return [VarType.number, VarType.string].includes(varPayload.type)
  113. }, [])
  114. const filterVisionInputVar = useCallback((varPayload: Var) => {
  115. return [VarType.file, VarType.arrayFile].includes(varPayload.type)
  116. }, [])
  117. const {
  118. availableVars,
  119. availableNodesWithParent,
  120. } = useAvailableVarList(id, {
  121. onlyLeafNodeVar: false,
  122. filterVar: filterInputVar,
  123. })
  124. const {
  125. availableVars: availableVisionVars,
  126. } = useAvailableVarList(id, {
  127. onlyLeafNodeVar: false,
  128. filterVar: filterVisionInputVar,
  129. })
  130. const hasSetBlockStatus = {
  131. history: false,
  132. query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false,
  133. context: false,
  134. }
  135. const handleInstructionChange = useCallback((instruction: string) => {
  136. const newInputs = produce(inputs, (draft) => {
  137. draft.instruction = instruction
  138. })
  139. setInputs(newInputs)
  140. }, [inputs, setInputs])
  141. const handleMemoryChange = useCallback((memory?: Memory) => {
  142. const newInputs = produce(inputs, (draft) => {
  143. draft.memory = memory
  144. })
  145. setInputs(newInputs)
  146. }, [inputs, setInputs])
  147. // single run
  148. const {
  149. isShowSingleRun,
  150. hideSingleRun,
  151. getInputVars,
  152. runningStatus,
  153. handleRun,
  154. handleStop,
  155. runInputData,
  156. runInputDataRef,
  157. setRunInputData,
  158. runResult,
  159. } = useOneStepRun<QuestionClassifierNodeType>({
  160. id,
  161. data: inputs,
  162. defaultRunInputData: {
  163. 'query': '',
  164. '#files#': [],
  165. },
  166. })
  167. const query = runInputData.query
  168. const setQuery = useCallback((newQuery: string) => {
  169. setRunInputData({
  170. ...runInputData,
  171. query: newQuery,
  172. })
  173. }, [runInputData, setRunInputData])
  174. const varInputs = getInputVars([inputs.instruction])
  175. const inputVarValues = (() => {
  176. const vars: Record<string, any> = {
  177. query,
  178. }
  179. Object.keys(runInputData)
  180. .forEach((key) => {
  181. vars[key] = runInputData[key]
  182. })
  183. return vars
  184. })()
  185. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  186. setRunInputData(newPayload)
  187. }, [setRunInputData])
  188. const visionFiles = runInputData['#files#']
  189. const setVisionFiles = useCallback((newFiles: any[]) => {
  190. setRunInputData({
  191. ...runInputDataRef.current,
  192. '#files#': newFiles,
  193. })
  194. }, [runInputDataRef, setRunInputData])
  195. const filterVar = useCallback((varPayload: Var) => {
  196. return varPayload.type === VarType.string
  197. }, [])
  198. return {
  199. readOnly,
  200. inputs,
  201. handleModelChanged,
  202. isChatMode,
  203. isChatModel,
  204. handleCompletionParamsChange,
  205. handleQueryVarChange,
  206. filterVar,
  207. handleTopicsChange: handleClassesChange,
  208. hasSetBlockStatus,
  209. availableVars,
  210. availableNodesWithParent,
  211. availableVisionVars,
  212. handleInstructionChange,
  213. varInputs,
  214. inputVarValues,
  215. setInputVarValues,
  216. handleMemoryChange,
  217. isVisionModel,
  218. handleVisionResolutionEnabledChange,
  219. handleVisionResolutionChange,
  220. isShowSingleRun,
  221. hideSingleRun,
  222. runningStatus,
  223. handleRun,
  224. handleStop,
  225. query,
  226. setQuery,
  227. runResult,
  228. visionFiles,
  229. setVisionFiles,
  230. }
  231. }
  232. export default useConfig