Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

use-config.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, Topic } from './types'
  13. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  14. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  15. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  16. import { checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
  17. import { useUpdateNodeInternals } from 'reactflow'
  18. const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
  19. const updateNodeInternals = useUpdateNodeInternals()
  20. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  21. const isChatMode = useIsChatMode()
  22. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  23. const { getBeforeNodesInSameBranch } = useWorkflow()
  24. const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start)
  25. const startNodeId = startNode?.id
  26. const { inputs, setInputs } = useNodeCrud<QuestionClassifierNodeType>(id, payload)
  27. const inputRef = useRef(inputs)
  28. useEffect(() => {
  29. inputRef.current = inputs
  30. }, [inputs])
  31. const [modelChanged, setModelChanged] = useState(false)
  32. const {
  33. currentProvider,
  34. currentModel,
  35. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
  36. const model = inputs.model
  37. const modelMode = inputs.model?.mode
  38. const isChatModel = modelMode === 'chat'
  39. const {
  40. isVisionModel,
  41. handleVisionResolutionEnabledChange,
  42. handleVisionResolutionChange,
  43. handleModelChanged: handleVisionConfigAfterModelChanged,
  44. } = useConfigVision(model, {
  45. payload: inputs.vision,
  46. onChange: (newPayload) => {
  47. const newInputs = produce(inputs, (draft) => {
  48. draft.vision = newPayload
  49. })
  50. setInputs(newInputs)
  51. },
  52. })
  53. const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
  54. const newInputs = produce(inputRef.current, (draft) => {
  55. draft.model.provider = model.provider
  56. draft.model.name = model.modelId
  57. draft.model.mode = model.mode!
  58. })
  59. setInputs(newInputs)
  60. setModelChanged(true)
  61. }, [setInputs])
  62. useEffect(() => {
  63. if (currentProvider?.provider && currentModel?.model && !model.provider) {
  64. handleModelChanged({
  65. provider: currentProvider?.provider,
  66. modelId: currentModel?.model,
  67. mode: currentModel?.model_properties?.mode as string,
  68. })
  69. }
  70. }, [model.provider, currentProvider, currentModel, handleModelChanged])
  71. const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => {
  72. const newInputs = produce(inputs, (draft) => {
  73. draft.model.completion_params = newParams
  74. })
  75. setInputs(newInputs)
  76. }, [inputs, setInputs])
  77. // change to vision model to set vision enabled, else disabled
  78. useEffect(() => {
  79. if (!modelChanged)
  80. return
  81. setModelChanged(false)
  82. handleVisionConfigAfterModelChanged()
  83. // eslint-disable-next-line react-hooks/exhaustive-deps
  84. }, [isVisionModel, modelChanged])
  85. const handleQueryVarChange = useCallback((newVar: ValueSelector | string) => {
  86. const newInputs = produce(inputs, (draft) => {
  87. draft.query_variable_selector = newVar as ValueSelector
  88. })
  89. setInputs(newInputs)
  90. }, [inputs, setInputs])
  91. useEffect(() => {
  92. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  93. if (isReady) {
  94. let query_variable_selector: ValueSelector = []
  95. if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId)
  96. query_variable_selector = [startNodeId, 'sys.query']
  97. setInputs({
  98. ...inputs,
  99. ...defaultConfig,
  100. query_variable_selector: inputs.query_variable_selector.length > 0 ? inputs.query_variable_selector : query_variable_selector,
  101. })
  102. }
  103. // eslint-disable-next-line react-hooks/exhaustive-deps
  104. }, [defaultConfig])
  105. const handleClassesChange = useCallback((newClasses: any) => {
  106. const newInputs = produce(inputs, (draft) => {
  107. draft.classes = newClasses
  108. draft._targetBranches = newClasses
  109. })
  110. setInputs(newInputs)
  111. }, [inputs, setInputs])
  112. const filterInputVar = useCallback((varPayload: Var) => {
  113. return [VarType.number, VarType.string].includes(varPayload.type)
  114. }, [])
  115. const filterVisionInputVar = useCallback((varPayload: Var) => {
  116. return [VarType.file, VarType.arrayFile].includes(varPayload.type)
  117. }, [])
  118. const {
  119. availableVars,
  120. availableNodesWithParent,
  121. } = useAvailableVarList(id, {
  122. onlyLeafNodeVar: false,
  123. filterVar: filterInputVar,
  124. })
  125. const {
  126. availableVars: availableVisionVars,
  127. } = useAvailableVarList(id, {
  128. onlyLeafNodeVar: false,
  129. filterVar: filterVisionInputVar,
  130. })
  131. const hasSetBlockStatus = {
  132. history: false,
  133. query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false,
  134. context: false,
  135. }
  136. const handleInstructionChange = useCallback((instruction: string) => {
  137. const newInputs = produce(inputs, (draft) => {
  138. draft.instruction = instruction
  139. })
  140. setInputs(newInputs)
  141. }, [inputs, setInputs])
  142. const handleMemoryChange = useCallback((memory?: Memory) => {
  143. const newInputs = produce(inputs, (draft) => {
  144. draft.memory = memory
  145. })
  146. setInputs(newInputs)
  147. }, [inputs, setInputs])
  148. const filterVar = useCallback((varPayload: Var) => {
  149. return varPayload.type === VarType.string
  150. }, [])
  151. const handleSortTopic = useCallback((newTopics: (Topic & { id: string })[]) => {
  152. const newInputs = produce(inputs, (draft) => {
  153. draft.classes = newTopics.filter(Boolean).map(item => ({
  154. id: item.id,
  155. name: item.name,
  156. }))
  157. })
  158. setInputs(newInputs)
  159. updateNodeInternals(id)
  160. }, [id, inputs, setInputs, updateNodeInternals])
  161. return {
  162. readOnly,
  163. inputs,
  164. handleModelChanged,
  165. isChatMode,
  166. isChatModel,
  167. handleCompletionParamsChange,
  168. handleQueryVarChange,
  169. filterVar,
  170. handleTopicsChange: handleClassesChange,
  171. hasSetBlockStatus,
  172. availableVars,
  173. availableNodesWithParent,
  174. availableVisionVars,
  175. handleInstructionChange,
  176. handleMemoryChange,
  177. isVisionModel,
  178. handleVisionResolutionEnabledChange,
  179. handleVisionResolutionChange,
  180. handleSortTopic,
  181. }
  182. }
  183. export default useConfig