您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-config.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }, [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. }, [defaultConfig])
  103. const handleClassesChange = useCallback((newClasses: any) => {
  104. const newInputs = produce(inputs, (draft) => {
  105. draft.classes = newClasses
  106. draft._targetBranches = newClasses
  107. })
  108. setInputs(newInputs)
  109. }, [inputs, setInputs])
  110. const filterInputVar = useCallback((varPayload: Var) => {
  111. return [VarType.number, VarType.string].includes(varPayload.type)
  112. }, [])
  113. const filterVisionInputVar = useCallback((varPayload: Var) => {
  114. return [VarType.file, VarType.arrayFile].includes(varPayload.type)
  115. }, [])
  116. const {
  117. availableVars,
  118. availableNodesWithParent,
  119. } = useAvailableVarList(id, {
  120. onlyLeafNodeVar: false,
  121. filterVar: filterInputVar,
  122. })
  123. const {
  124. availableVars: availableVisionVars,
  125. } = useAvailableVarList(id, {
  126. onlyLeafNodeVar: false,
  127. filterVar: filterVisionInputVar,
  128. })
  129. const hasSetBlockStatus = {
  130. history: false,
  131. query: isChatMode ? checkHasQueryBlock(inputs.instruction) : false,
  132. context: false,
  133. }
  134. const handleInstructionChange = useCallback((instruction: string) => {
  135. const newInputs = produce(inputs, (draft) => {
  136. draft.instruction = instruction
  137. })
  138. setInputs(newInputs)
  139. }, [inputs, setInputs])
  140. const handleMemoryChange = useCallback((memory?: Memory) => {
  141. const newInputs = produce(inputs, (draft) => {
  142. draft.memory = memory
  143. })
  144. setInputs(newInputs)
  145. }, [inputs, setInputs])
  146. const filterVar = useCallback((varPayload: Var) => {
  147. return varPayload.type === VarType.string
  148. }, [])
  149. const handleSortTopic = useCallback((newTopics: (Topic & { id: string })[]) => {
  150. const newInputs = produce(inputs, (draft) => {
  151. draft.classes = newTopics.filter(Boolean).map(item => ({
  152. id: item.id,
  153. name: item.name,
  154. }))
  155. })
  156. setInputs(newInputs)
  157. updateNodeInternals(id)
  158. }, [id, inputs, setInputs, updateNodeInternals])
  159. return {
  160. readOnly,
  161. inputs,
  162. handleModelChanged,
  163. isChatMode,
  164. isChatModel,
  165. handleCompletionParamsChange,
  166. handleQueryVarChange,
  167. filterVar,
  168. handleTopicsChange: handleClassesChange,
  169. hasSetBlockStatus,
  170. availableVars,
  171. availableNodesWithParent,
  172. availableVisionVars,
  173. handleInstructionChange,
  174. handleMemoryChange,
  175. isVisionModel,
  176. handleVisionResolutionEnabledChange,
  177. handleVisionResolutionChange,
  178. handleSortTopic,
  179. }
  180. }
  181. export default useConfig