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.5KB

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