content.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useChatWithHistoryContext } from '../context'
  4. import Input from '@/app/components/base/input'
  5. import Textarea from '@/app/components/base/textarea'
  6. import { PortalSelect } from '@/app/components/base/select'
  7. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  8. import { InputVarType } from '@/app/components/workflow/types'
  9. type Props = {
  10. showTip?: boolean
  11. }
  12. const InputsFormContent = ({ showTip }: Props) => {
  13. const { t } = useTranslation()
  14. const {
  15. appParams,
  16. inputsForms,
  17. currentConversationId,
  18. currentConversationItem,
  19. newConversationInputs,
  20. newConversationInputsRef,
  21. handleNewConversationInputsChange,
  22. } = useChatWithHistoryContext()
  23. const inputsFormValue = currentConversationId ? currentConversationItem?.inputs : newConversationInputs
  24. const readonly = !!currentConversationId
  25. const handleFormChange = useCallback((variable: string, value: any) => {
  26. handleNewConversationInputsChange({
  27. ...newConversationInputsRef.current,
  28. [variable]: value,
  29. })
  30. }, [newConversationInputsRef, handleNewConversationInputsChange])
  31. return (
  32. <div className='space-y-4'>
  33. {inputsForms.map(form => (
  34. <div key={form.variable} className='space-y-1'>
  35. <div className='h-6 flex items-center gap-1'>
  36. <div className='text-text-secondary system-md-semibold'>{form.label}</div>
  37. {!form.required && (
  38. <div className='text-text-tertiary system-xs-regular'>{t('appDebug.variableTable.optional')}</div>
  39. )}
  40. </div>
  41. {form.type === InputVarType.textInput && (
  42. <Input
  43. value={inputsFormValue?.[form.variable] || ''}
  44. onChange={e => handleFormChange(form.variable, e.target.value)}
  45. placeholder={form.label}
  46. readOnly={readonly}
  47. disabled={readonly}
  48. />
  49. )}
  50. {form.type === InputVarType.number && (
  51. <Input
  52. type='number'
  53. value={inputsFormValue?.[form.variable] || ''}
  54. onChange={e => handleFormChange(form.variable, e.target.value)}
  55. placeholder={form.label}
  56. readOnly={readonly}
  57. disabled={readonly}
  58. />
  59. )}
  60. {form.type === InputVarType.paragraph && (
  61. <Textarea
  62. value={inputsFormValue?.[form.variable] || ''}
  63. onChange={e => handleFormChange(form.variable, e.target.value)}
  64. placeholder={form.label}
  65. readOnly={readonly}
  66. disabled={readonly}
  67. />
  68. )}
  69. {form.type === InputVarType.select && (
  70. <PortalSelect
  71. popupClassName='w-[200px]'
  72. value={inputsFormValue?.[form.variable]}
  73. items={form.options.map((option: string) => ({ value: option, name: option }))}
  74. onSelect={item => handleFormChange(form.variable, item.value as string)}
  75. placeholder={form.label}
  76. readonly={readonly}
  77. />
  78. )}
  79. {form.type === InputVarType.singleFile && (
  80. <FileUploaderInAttachmentWrapper
  81. value={inputsFormValue?.[form.variable] ? [inputsFormValue?.[form.variable]] : []}
  82. onChange={files => handleFormChange(form.variable, files[0])}
  83. fileConfig={{
  84. allowed_file_types: form.allowed_file_types,
  85. allowed_file_extensions: form.allowed_file_extensions,
  86. allowed_file_upload_methods: form.allowed_file_upload_methods,
  87. number_limits: 1,
  88. fileUploadConfig: (appParams as any).system_parameters,
  89. }}
  90. />
  91. )}
  92. {form.type === InputVarType.multiFiles && (
  93. <FileUploaderInAttachmentWrapper
  94. value={inputsFormValue?.[form.variable] || []}
  95. onChange={files => handleFormChange(form.variable, files)}
  96. fileConfig={{
  97. allowed_file_types: form.allowed_file_types,
  98. allowed_file_extensions: form.allowed_file_extensions,
  99. allowed_file_upload_methods: form.allowed_file_upload_methods,
  100. number_limits: form.max_length,
  101. fileUploadConfig: (appParams as any).system_parameters,
  102. }}
  103. />
  104. )}
  105. </div>
  106. ))}
  107. {showTip && (
  108. <div className='text-text-tertiary system-xs-regular'>{t('share.chat.chatFormTip')}</div>
  109. )}
  110. </div>
  111. )
  112. }
  113. export default InputsFormContent