index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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={item.config as any}
  96. />
  97. )}
  98. {item.type === 'file-list' && (
  99. <FileUploaderInAttachmentWrapper
  100. onChange={(files) => { onInputsChange({ ...inputs, [item.key]: getProcessedFiles(files) }) }}
  101. fileConfig={item.config as any}
  102. />
  103. )}
  104. </div>
  105. </div>
  106. ))}
  107. {
  108. visionConfig?.enabled && (
  109. <div className="w-full mt-4">
  110. <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div>
  111. <div className='mt-2'>
  112. <TextGenerationImageUploader
  113. settings={visionConfig}
  114. onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
  115. type: 'image',
  116. transfer_method: fileItem.type,
  117. url: fileItem.url,
  118. upload_file_id: fileItem.fileId,
  119. })))}
  120. />
  121. </div>
  122. </div>
  123. )
  124. }
  125. {promptConfig.prompt_variables.length > 0 && (
  126. <div className='mt-4 h-[1px] bg-gray-100'></div>
  127. )}
  128. <div className='w-full mt-4'>
  129. <div className="flex items-center justify-between">
  130. <Button
  131. onClick={onClear}
  132. disabled={false}
  133. >
  134. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  135. </Button>
  136. <Button
  137. type='submit'
  138. variant="primary"
  139. disabled={false}
  140. >
  141. <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
  142. <span className='text-[13px]'>{t('share.generation.run')}</span>
  143. </Button>
  144. </div>
  145. </div>
  146. </form>
  147. </section>
  148. </div>
  149. )
  150. }
  151. export default React.memo(RunOnce)