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

inputs-panel.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 { inputs } = useStore(s => ({
  34. inputs: s.inputs,
  35. setInputs: s.setInputs,
  36. }))
  37. const fileSettings = useFeatures(s => s.features.file)
  38. const nodes = useNodes<StartNodeType>()
  39. const files = useStore(s => s.files)
  40. const workflowRunningData = useStore(s => s.workflowRunningData)
  41. const {
  42. handleRun,
  43. } = useWorkflowRun()
  44. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  45. const startVariables = startNode?.data.variables
  46. const { checkInputsForm } = useCheckInputsForms()
  47. const initialInputs = { ...inputs }
  48. if (startVariables) {
  49. startVariables.forEach((variable) => {
  50. if (variable.default)
  51. initialInputs[variable.variable] = variable.default
  52. })
  53. }
  54. const variables = useMemo(() => {
  55. const data = startVariables || []
  56. if (fileSettings?.image?.enabled) {
  57. return [
  58. ...data,
  59. {
  60. type: InputVarType.files,
  61. variable: '__image',
  62. required: false,
  63. label: 'files',
  64. },
  65. ]
  66. }
  67. return data
  68. }, [fileSettings?.image?.enabled, startVariables])
  69. const handleValueChange = (variable: string, v: any) => {
  70. const {
  71. inputs,
  72. setInputs,
  73. } = workflowStore.getState()
  74. if (variable === '__image') {
  75. workflowStore.setState({
  76. files: v,
  77. })
  78. }
  79. else {
  80. setInputs({
  81. ...inputs,
  82. [variable]: v,
  83. })
  84. }
  85. }
  86. const doRun = useCallback(() => {
  87. if (!checkInputsForm(initialInputs, variables as any))
  88. return
  89. onRun()
  90. handleRun({ inputs: getProcessedInputs(initialInputs, variables as any), files })
  91. }, [files, handleRun, initialInputs, onRun, variables, checkInputsForm])
  92. const canRun = useMemo(() => {
  93. if (files?.some(item => (item.transfer_method as any) === TransferMethod.local_file && !item.upload_file_id))
  94. return false
  95. return true
  96. }, [files])
  97. return (
  98. <>
  99. <div className='px-4 pb-2 pt-3'>
  100. {
  101. variables.map((variable, index) => (
  102. <div
  103. key={variable.variable}
  104. className='mb-2 last-of-type:mb-0'
  105. >
  106. <FormItem
  107. autoFocus={index === 0}
  108. className='!block'
  109. payload={variable}
  110. value={initialInputs[variable.variable]}
  111. onChange={v => handleValueChange(variable.variable, v)}
  112. />
  113. </div>
  114. ))
  115. }
  116. </div>
  117. <div className='flex items-center justify-between px-4 py-2'>
  118. <Button
  119. variant='primary'
  120. disabled={!canRun || workflowRunningData?.result?.status === WorkflowRunningStatus.Running}
  121. className='w-full'
  122. onClick={doRun}
  123. >
  124. {t('workflow.singleRun.startRun')}
  125. </Button>
  126. </div>
  127. </>
  128. )
  129. }
  130. export default memo(InputsPanel)