inputs-panel.tsx 3.0 KB

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