index.tsx 6.4 KB

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