use-config.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 {
  115. availableVars,
  116. availableNodesWithParent,
  117. } = useAvailableVarList(id, {
  118. onlyLeafNodeVar: false,
  119. filterVar: filterInputVar,
  120. })
  121. const hasSetBlockStatus = {
  122. history: false,
  123. query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false,
  124. context: false,
  125. }
  126. const handleInstructionChange = useCallback((instruction: string) => {
  127. const newInputs = produce(inputs, (draft) => {
  128. draft.instruction = instruction
  129. })
  130. setInputs(newInputs)
  131. }, [inputs, setInputs])
  132. const handleMemoryChange = useCallback((memory?: Memory) => {
  133. const newInputs = produce(inputs, (draft) => {
  134. draft.memory = memory
  135. })
  136. setInputs(newInputs)
  137. }, [inputs, setInputs])
  138. // single run
  139. const {
  140. isShowSingleRun,
  141. hideSingleRun,
  142. getInputVars,
  143. runningStatus,
  144. handleRun,
  145. handleStop,
  146. runInputData,
  147. setRunInputData,
  148. runResult,
  149. } = useOneStepRun<QuestionClassifierNodeType>({
  150. id,
  151. data: inputs,
  152. defaultRunInputData: {
  153. query: '',
  154. },
  155. })
  156. const query = runInputData.query
  157. const setQuery = useCallback((newQuery: string) => {
  158. setRunInputData({
  159. ...runInputData,
  160. query: newQuery,
  161. })
  162. }, [runInputData, setRunInputData])
  163. const varInputs = getInputVars([inputs.instruction])
  164. const inputVarValues = (() => {
  165. const vars: Record<string, any> = {
  166. query,
  167. }
  168. Object.keys(runInputData)
  169. .forEach((key) => {
  170. vars[key] = runInputData[key]
  171. })
  172. return vars
  173. })()
  174. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  175. setRunInputData(newPayload)
  176. }, [setRunInputData])
  177. const filterVar = useCallback((varPayload: Var) => {
  178. return varPayload.type === VarType.string
  179. }, [])
  180. return {
  181. readOnly,
  182. inputs,
  183. handleModelChanged,
  184. isChatMode,
  185. isChatModel,
  186. handleCompletionParamsChange,
  187. handleQueryVarChange,
  188. filterVar,
  189. handleTopicsChange: handleClassesChange,
  190. hasSetBlockStatus,
  191. availableVars,
  192. availableNodesWithParent,
  193. handleInstructionChange,
  194. varInputs,
  195. inputVarValues,
  196. setInputVarValues,
  197. handleMemoryChange,
  198. isVisionModel,
  199. handleVisionResolutionEnabledChange,
  200. handleVisionResolutionChange,
  201. isShowSingleRun,
  202. hideSingleRun,
  203. runningStatus,
  204. handleRun,
  205. handleStop,
  206. query,
  207. setQuery,
  208. runResult,
  209. }
  210. }
  211. export default useConfig