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

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