index.tsx 6.6 KB

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