inputs-panel.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useNodes } from 'reactflow'
  8. import FormItem from '../nodes/_base/components/before-run-form/form-item'
  9. import {
  10. BlockEnum,
  11. InputVarType,
  12. WorkflowRunningStatus,
  13. } from '../types'
  14. import {
  15. useStore,
  16. useWorkflowStore,
  17. } from '../store'
  18. import { useWorkflowRun } from '../hooks'
  19. import type { StartNodeType } from '../nodes/start/types'
  20. import { TransferMethod } from '../../base/text-generation/types'
  21. import Button from '@/app/components/base/button'
  22. import { useFeatures } from '@/app/components/base/features/hooks'
  23. import {
  24. getProcessedInputs,
  25. } from '@/app/components/base/chat/chat/utils'
  26. import { useCheckInputsForms } from '@/app/components/base/chat/chat/check-input-forms-hooks'
  27. type Props = {
  28. onRun: () => void
  29. }
  30. const InputsPanel = ({ onRun }: Props) => {
  31. const { t } = useTranslation()
  32. const workflowStore = useWorkflowStore()
  33. const fileSettings = useFeatures(s => s.features.file)
  34. const nodes = useNodes<StartNodeType>()
  35. const inputs = useStore(s => s.inputs)
  36. const files = useStore(s => s.files)
  37. const workflowRunningData = useStore(s => s.workflowRunningData)
  38. const {
  39. handleRun,
  40. } = useWorkflowRun()
  41. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  42. const startVariables = startNode?.data.variables
  43. const { checkInputsForm } = useCheckInputsForms()
  44. const variables = useMemo(() => {
  45. const data = startVariables || []
  46. if (fileSettings?.image?.enabled) {
  47. return [
  48. ...data,
  49. {
  50. type: InputVarType.files,
  51. variable: '__image',
  52. required: false,
  53. label: 'files',
  54. },
  55. ]
  56. }
  57. return data
  58. }, [fileSettings?.image?.enabled, startVariables])
  59. const handleValueChange = (variable: string, v: any) => {
  60. const {
  61. inputs,
  62. setInputs,
  63. } = workflowStore.getState()
  64. if (variable === '__image') {
  65. workflowStore.setState({
  66. files: v,
  67. })
  68. }
  69. else {
  70. setInputs({
  71. ...inputs,
  72. [variable]: v,
  73. })
  74. }
  75. }
  76. const doRun = useCallback(() => {
  77. if (!checkInputsForm(inputs, variables as any))
  78. return
  79. onRun()
  80. handleRun({ inputs: getProcessedInputs(inputs, variables as any), files })
  81. }, [files, handleRun, inputs, onRun, variables, checkInputsForm])
  82. const canRun = useMemo(() => {
  83. if (files?.some(item => (item.transfer_method as any) === TransferMethod.local_file && !item.upload_file_id))
  84. return false
  85. return true
  86. }, [files])
  87. return (
  88. <>
  89. <div className='pt-3 px-4 pb-2'>
  90. {
  91. variables.map((variable, index) => (
  92. <div
  93. key={variable.variable}
  94. className='mb-2 last-of-type:mb-0'
  95. >
  96. <FormItem
  97. autoFocus={index === 0}
  98. className='!block'
  99. payload={variable}
  100. value={inputs[variable.variable]}
  101. onChange={v => handleValueChange(variable.variable, v)}
  102. />
  103. </div>
  104. ))
  105. }
  106. </div>
  107. <div className='flex items-center justify-between px-4 py-2'>
  108. <Button
  109. variant='primary'
  110. disabled={!canRun || workflowRunningData?.result?.status === WorkflowRunningStatus.Running}
  111. className='w-full'
  112. onClick={doRun}
  113. >
  114. {t('workflow.singleRun.startRun')}
  115. </Button>
  116. </div>
  117. </>
  118. )
  119. }
  120. export default memo(InputsPanel)