You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 8.0KB

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