You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-single-run-form-params.ts 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import type { MutableRefObject } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  4. import type { InputVar, Var, Variable } from '@/app/components/workflow/types'
  5. import { InputVarType, VarType } from '@/app/components/workflow/types'
  6. import type { QuestionClassifierNodeType } from './types'
  7. import useNodeCrud from '../_base/hooks/use-node-crud'
  8. import { useCallback } from 'react'
  9. import useConfigVision from '../../hooks/use-config-vision'
  10. import { noop } from 'lodash-es'
  11. import { findVariableWhenOnLLMVision } from '../utils'
  12. import useAvailableVarList from '../_base/hooks/use-available-var-list'
  13. const i18nPrefix = 'workflow.nodes.questionClassifiers'
  14. type Params = {
  15. id: string,
  16. payload: QuestionClassifierNodeType,
  17. runInputData: Record<string, any>
  18. runInputDataRef: MutableRefObject<Record<string, any>>
  19. getInputVars: (textList: string[]) => InputVar[]
  20. setRunInputData: (data: Record<string, any>) => void
  21. toVarInputs: (variables: Variable[]) => InputVar[]
  22. }
  23. const useSingleRunFormParams = ({
  24. id,
  25. payload,
  26. runInputData,
  27. runInputDataRef,
  28. getInputVars,
  29. setRunInputData,
  30. }: Params) => {
  31. const { t } = useTranslation()
  32. const { inputs } = useNodeCrud<QuestionClassifierNodeType>(id, payload)
  33. const model = inputs.model
  34. const {
  35. isVisionModel,
  36. } = useConfigVision(model, {
  37. payload: inputs.vision,
  38. onChange: noop,
  39. })
  40. const visionFiles = runInputData['#files#']
  41. const setVisionFiles = useCallback((newFiles: any[]) => {
  42. setRunInputData?.({
  43. ...runInputDataRef.current,
  44. '#files#': newFiles,
  45. })
  46. }, [runInputDataRef, setRunInputData])
  47. const varInputs = getInputVars([inputs.instruction])
  48. const inputVarValues = (() => {
  49. const vars: Record<string, any> = {}
  50. Object.keys(runInputData)
  51. .filter(key => !['#files#'].includes(key))
  52. .forEach((key) => {
  53. vars[key] = runInputData[key]
  54. })
  55. return vars
  56. })()
  57. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  58. const newVars = {
  59. ...newPayload,
  60. '#files#': runInputDataRef.current['#files#'],
  61. }
  62. setRunInputData?.(newVars)
  63. }, [runInputDataRef, setRunInputData])
  64. const filterVisionInputVar = useCallback((varPayload: Var) => {
  65. return [VarType.file, VarType.arrayFile].includes(varPayload.type)
  66. }, [])
  67. const {
  68. availableVars: availableVisionVars,
  69. } = useAvailableVarList(id, {
  70. onlyLeafNodeVar: false,
  71. filterVar: filterVisionInputVar,
  72. })
  73. const forms = (() => {
  74. const forms: FormProps[] = []
  75. forms.push(
  76. {
  77. label: t('workflow.nodes.llm.singleRun.variable')!,
  78. inputs: [{
  79. label: t(`${i18nPrefix}.inputVars`)!,
  80. variable: 'query',
  81. type: InputVarType.paragraph,
  82. required: true,
  83. }, ...varInputs],
  84. values: inputVarValues,
  85. onChange: setInputVarValues,
  86. },
  87. )
  88. if (isVisionModel && payload.vision?.enabled && payload.vision?.configs?.variable_selector) {
  89. const currentVariable = findVariableWhenOnLLMVision(payload.vision.configs.variable_selector, availableVisionVars)
  90. forms.push(
  91. {
  92. label: t('workflow.nodes.llm.vision')!,
  93. inputs: [{
  94. label: currentVariable?.variable as any,
  95. variable: '#files#',
  96. type: currentVariable?.formType as any,
  97. required: false,
  98. }],
  99. values: { '#files#': visionFiles },
  100. onChange: keyValue => setVisionFiles(keyValue['#files#']),
  101. },
  102. )
  103. }
  104. return forms
  105. })()
  106. const getDependentVars = () => {
  107. const promptVars = varInputs.map(item => item.variable.slice(1, -1).split('.'))
  108. const vars = [payload.query_variable_selector, ...promptVars]
  109. if (isVisionModel && payload.vision?.enabled && payload.vision?.configs?.variable_selector) {
  110. const visionVar = payload.vision.configs.variable_selector
  111. vars.push(visionVar)
  112. }
  113. return vars
  114. }
  115. const getDependentVar = (variable: string) => {
  116. if(variable === 'query')
  117. return payload.query_variable_selector
  118. if(variable === '#files#')
  119. return payload.vision.configs?.variable_selector
  120. return false
  121. }
  122. return {
  123. forms,
  124. getDependentVars,
  125. getDependentVar,
  126. }
  127. }
  128. export default useSingleRunFormParams