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. handleRunSetting,
  36. } = useWorkflowRun()
  37. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  38. const startVariables = startNode?.data.variables
  39. const variables = useMemo(() => {
  40. const data = startVariables || []
  41. if (fileSettings?.image?.enabled) {
  42. return [
  43. ...data,
  44. {
  45. type: InputVarType.files,
  46. variable: '__image',
  47. required: true,
  48. label: 'files',
  49. },
  50. ]
  51. }
  52. return data
  53. }, [fileSettings?.image?.enabled, startVariables])
  54. const handleValueChange = (variable: string, v: any) => {
  55. if (variable === '__image') {
  56. workflowStore.setState({
  57. files: v,
  58. })
  59. }
  60. else {
  61. workflowStore.getState().setInputs({
  62. ...inputs,
  63. [variable]: v,
  64. })
  65. }
  66. }
  67. const doRun = () => {
  68. onRun()
  69. handleRunSetting()
  70. handleRun({ inputs, files })
  71. }
  72. const canRun = (() => {
  73. if (files?.some(item => (item.transfer_method as any) === TransferMethod.local_file && !item.upload_file_id))
  74. return false
  75. return true
  76. })()
  77. return (
  78. <>
  79. <div className='px-4 pb-2'>
  80. {
  81. variables.map(variable => (
  82. <div
  83. key={variable.variable}
  84. className='mb-2 last-of-type:mb-0'
  85. >
  86. <FormItem
  87. className='!block'
  88. payload={variable}
  89. value={inputs[variable.variable]}
  90. onChange={v => handleValueChange(variable.variable, v)}
  91. />
  92. </div>
  93. ))
  94. }
  95. </div>
  96. <div className='flex items-center justify-between px-4 py-2'>
  97. <Button
  98. type='primary'
  99. disabled={!canRun || workflowRunningData?.result?.status === WorkflowRunningStatus.Running}
  100. className='py-0 w-full h-8 rounded-lg text-[13px] font-medium'
  101. onClick={doRun}
  102. >
  103. {t('workflow.singleRun.startRun')}
  104. </Button>
  105. </div>
  106. </>
  107. )
  108. }
  109. export default memo(InputsPanel)