index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import type { FC, FormEvent } from 'react'
  2. import React, { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. PlayIcon,
  6. } from '@heroicons/react/24/solid'
  7. import Select from '@/app/components/base/select'
  8. import type { SiteInfo } from '@/models/share'
  9. import type { PromptConfig } from '@/models/debug'
  10. import Button from '@/app/components/base/button'
  11. import Textarea from '@/app/components/base/textarea'
  12. import { DEFAULT_VALUE_MAX_LEN } from '@/config'
  13. import TextGenerationImageUploader from '@/app/components/base/image-uploader/text-generation-image-uploader'
  14. import type { VisionFile, VisionSettings } from '@/types/app'
  15. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  16. import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
  17. export type IRunOnceProps = {
  18. siteInfo: SiteInfo
  19. promptConfig: PromptConfig
  20. inputs: Record<string, any>
  21. inputsRef: React.MutableRefObject<Record<string, any>>
  22. onInputsChange: (inputs: Record<string, any>) => void
  23. onSend: () => void
  24. visionConfig: VisionSettings
  25. onVisionFilesChange: (files: VisionFile[]) => void
  26. }
  27. const RunOnce: FC<IRunOnceProps> = ({
  28. promptConfig,
  29. inputs,
  30. inputsRef,
  31. onInputsChange,
  32. onSend,
  33. visionConfig,
  34. onVisionFilesChange,
  35. }) => {
  36. const { t } = useTranslation()
  37. const onClear = () => {
  38. const newInputs: Record<string, any> = {}
  39. promptConfig.prompt_variables.forEach((item) => {
  40. newInputs[item.key] = ''
  41. })
  42. onInputsChange(newInputs)
  43. }
  44. const onSubmit = (e: FormEvent<HTMLFormElement>) => {
  45. e.preventDefault()
  46. onSend()
  47. }
  48. const handleInputsChange = useCallback((newInputs: Record<string, any>) => {
  49. onInputsChange(newInputs)
  50. inputsRef.current = newInputs
  51. }, [onInputsChange, inputsRef])
  52. return (
  53. <div className="">
  54. <section>
  55. {/* input form */}
  56. <form onSubmit={onSubmit}>
  57. {promptConfig.prompt_variables.map(item => (
  58. <div className='w-full mt-4' key={item.key}>
  59. <label className='text-gray-900 text-sm font-medium'>{item.name}</label>
  60. <div className='mt-2'>
  61. {item.type === 'select' && (
  62. <Select
  63. className='w-full'
  64. defaultValue={inputs[item.key]}
  65. onSelect={(i) => { handleInputsChange({ ...inputsRef.current, [item.key]: i.value }) }}
  66. items={(item.options || []).map(i => ({ name: i, value: i }))}
  67. allowSearch={false}
  68. bgClassName='bg-gray-50'
  69. />
  70. )}
  71. {item.type === 'string' && (
  72. <input
  73. type="text"
  74. className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
  75. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  76. value={inputs[item.key]}
  77. onChange={(e) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  78. maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
  79. />
  80. )}
  81. {item.type === 'paragraph' && (
  82. <Textarea
  83. className='h-[104px] sm:text-xs'
  84. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  85. value={inputs[item.key]}
  86. onChange={(e) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  87. />
  88. )}
  89. {item.type === 'number' && (
  90. <input
  91. type="number"
  92. className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
  93. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  94. value={inputs[item.key]}
  95. onChange={(e) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  96. />
  97. )}
  98. {item.type === 'file' && (
  99. <FileUploaderInAttachmentWrapper
  100. onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files)[0] }) }}
  101. fileConfig={{
  102. ...item.config,
  103. fileUploadConfig: (visionConfig as any).fileUploadConfig,
  104. }}
  105. />
  106. )}
  107. {item.type === 'file-list' && (
  108. <FileUploaderInAttachmentWrapper
  109. onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files) }) }}
  110. fileConfig={{
  111. ...item.config,
  112. fileUploadConfig: (visionConfig as any).fileUploadConfig,
  113. }}
  114. />
  115. )}
  116. </div>
  117. </div>
  118. ))}
  119. {
  120. visionConfig?.enabled && (
  121. <div className="w-full mt-4">
  122. <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div>
  123. <div className='mt-2'>
  124. <TextGenerationImageUploader
  125. settings={visionConfig}
  126. onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
  127. type: 'image',
  128. transfer_method: fileItem.type,
  129. url: fileItem.url,
  130. upload_file_id: fileItem.fileId,
  131. })))}
  132. />
  133. </div>
  134. </div>
  135. )
  136. }
  137. {promptConfig.prompt_variables.length > 0 && (
  138. <div className='mt-4 h-[1px] bg-gray-100'></div>
  139. )}
  140. <div className='w-full mt-4'>
  141. <div className="flex items-center justify-between">
  142. <Button
  143. onClick={onClear}
  144. disabled={false}
  145. >
  146. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  147. </Button>
  148. <Button
  149. type='submit'
  150. variant="primary"
  151. disabled={false}
  152. >
  153. <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
  154. <span className='text-[13px]'>{t('share.generation.run')}</span>
  155. </Button>
  156. </div>
  157. </div>
  158. </form>
  159. </section>
  160. </div>
  161. )
  162. }
  163. export default React.memo(RunOnce)