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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import type { ChangeEvent, FC, FormEvent } from 'react'
  2. import { useEffect } 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 onClear = () => {
  44. const newInputs: Record<string, any> = {}
  45. promptConfig.prompt_variables.forEach((item) => {
  46. if (item.type === 'text-input' || item.type === 'paragraph')
  47. newInputs[item.key] = ''
  48. else
  49. newInputs[item.key] = undefined
  50. })
  51. onInputsChange(newInputs)
  52. }
  53. const onSubmit = (e: FormEvent<HTMLFormElement>) => {
  54. e.preventDefault()
  55. onSend()
  56. }
  57. const handleInputsChange = useCallback((newInputs: Record<string, any>) => {
  58. onInputsChange(newInputs)
  59. inputsRef.current = newInputs
  60. }, [onInputsChange, inputsRef])
  61. useEffect(() => {
  62. const newInputs: Record<string, any> = {}
  63. promptConfig.prompt_variables.forEach((item) => {
  64. if (item.type === 'text-input' || item.type === 'paragraph')
  65. newInputs[item.key] = ''
  66. else
  67. newInputs[item.key] = undefined
  68. })
  69. onInputsChange(newInputs)
  70. }, [promptConfig.prompt_variables, onInputsChange])
  71. return (
  72. <div className="">
  73. <section>
  74. {/* input form */}
  75. <form onSubmit={onSubmit}>
  76. {(inputs === null || inputs === undefined || Object.keys(inputs).length === 0) ? null
  77. : promptConfig.prompt_variables.map(item => (
  78. <div className='mt-4 w-full' key={item.key}>
  79. <label className='system-md-semibold flex h-6 items-center text-text-secondary'>{item.name}</label>
  80. <div className='mt-1'>
  81. {item.type === 'select' && (
  82. <Select
  83. className='w-full'
  84. defaultValue={inputs[item.key]}
  85. onSelect={(i) => { handleInputsChange({ ...inputsRef.current, [item.key]: i.value }) }}
  86. items={(item.options || []).map(i => ({ name: i, value: i }))}
  87. allowSearch={false}
  88. />
  89. )}
  90. {item.type === 'string' && (
  91. <Input
  92. type="text"
  93. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  94. value={inputs[item.key]}
  95. onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  96. maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
  97. />
  98. )}
  99. {item.type === 'paragraph' && (
  100. <Textarea
  101. className='h-[104px] sm:text-xs'
  102. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  103. value={inputs[item.key]}
  104. onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  105. />
  106. )}
  107. {item.type === 'number' && (
  108. <Input
  109. type="number"
  110. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  111. value={inputs[item.key]}
  112. onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }}
  113. />
  114. )}
  115. {item.type === 'file' && (
  116. <FileUploaderInAttachmentWrapper
  117. onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files)[0] }) }}
  118. fileConfig={{
  119. ...item.config,
  120. fileUploadConfig: (visionConfig as any).fileUploadConfig,
  121. }}
  122. />
  123. )}
  124. {item.type === 'file-list' && (
  125. <FileUploaderInAttachmentWrapper
  126. onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files) }) }}
  127. fileConfig={{
  128. ...item.config,
  129. fileUploadConfig: (visionConfig as any).fileUploadConfig,
  130. }}
  131. />
  132. )}
  133. </div>
  134. </div>
  135. ))}
  136. {
  137. visionConfig?.enabled && (
  138. <div className="mt-4 w-full">
  139. <div className="system-md-semibold flex h-6 items-center text-text-secondary">{t('common.imageUploader.imageUpload')}</div>
  140. <div className='mt-1'>
  141. <TextGenerationImageUploader
  142. settings={visionConfig}
  143. onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
  144. type: 'image',
  145. transfer_method: fileItem.type,
  146. url: fileItem.url,
  147. upload_file_id: fileItem.fileId,
  148. })))}
  149. />
  150. </div>
  151. </div>
  152. )
  153. }
  154. <div className='mb-3 mt-6 w-full'>
  155. <div className="flex items-center justify-between gap-2">
  156. <Button
  157. onClick={onClear}
  158. disabled={false}
  159. >
  160. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  161. </Button>
  162. <Button
  163. className={cn(!isPC && 'grow')}
  164. type='submit'
  165. variant="primary"
  166. disabled={false}
  167. >
  168. <RiPlayLargeLine className="mr-1 h-4 w-4 shrink-0" aria-hidden="true" />
  169. <span className='text-[13px]'>{t('share.generation.run')}</span>
  170. </Button>
  171. </div>
  172. </div>
  173. </form>
  174. </section>
  175. </div>
  176. )
  177. }
  178. export default React.memo(RunOnce)