index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. PlayIcon,
  6. } from '@heroicons/react/24/solid'
  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 { DEFAULT_VALUE_MAX_LEN } from '@/config'
  12. import TextGenerationImageUploader from '@/app/components/base/image-uploader/text-generation-image-uploader'
  13. import type { VisionFile, VisionSettings } from '@/types/app'
  14. export type IRunOnceProps = {
  15. siteInfo: SiteInfo
  16. promptConfig: PromptConfig
  17. inputs: Record<string, any>
  18. onInputsChange: (inputs: Record<string, any>) => void
  19. onSend: () => void
  20. visionConfig: VisionSettings
  21. onVisionFilesChange: (files: VisionFile[]) => void
  22. }
  23. const RunOnce: FC<IRunOnceProps> = ({
  24. promptConfig,
  25. inputs,
  26. onInputsChange,
  27. onSend,
  28. visionConfig,
  29. onVisionFilesChange,
  30. }) => {
  31. const { t } = useTranslation()
  32. const onClear = () => {
  33. const newInputs: Record<string, any> = {}
  34. promptConfig.prompt_variables.forEach((item) => {
  35. newInputs[item.key] = ''
  36. })
  37. onInputsChange(newInputs)
  38. }
  39. return (
  40. <div className="">
  41. <section>
  42. {/* input form */}
  43. <form>
  44. {promptConfig.prompt_variables.map(item => (
  45. <div className='w-full mt-4' key={item.key}>
  46. <label className='text-gray-900 text-sm font-medium'>{item.name}</label>
  47. <div className='mt-2'>
  48. {item.type === 'select' && (
  49. <Select
  50. className='w-full'
  51. defaultValue={inputs[item.key]}
  52. onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }}
  53. items={(item.options || []).map(i => ({ name: i, value: i }))}
  54. allowSearch={false}
  55. bgClassName='bg-gray-50'
  56. />
  57. )}
  58. {item.type === 'string' && (
  59. <input
  60. type="text"
  61. className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
  62. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  63. value={inputs[item.key]}
  64. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  65. onKeyDown={(e) => {
  66. if (e.key === 'Enter') {
  67. e.preventDefault()
  68. onSend()
  69. }
  70. }}
  71. maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
  72. />
  73. )}
  74. {item.type === 'paragraph' && (
  75. <textarea
  76. className="block w-full h-[104px] p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
  77. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  78. value={inputs[item.key]}
  79. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  80. />
  81. )}
  82. {item.type === 'number' && (
  83. <input
  84. type="number"
  85. className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "
  86. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  87. value={inputs[item.key]}
  88. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  89. />
  90. )}
  91. </div>
  92. </div>
  93. ))}
  94. {
  95. visionConfig?.enabled && (
  96. <div className="w-full mt-4">
  97. <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div>
  98. <div className='mt-2'>
  99. <TextGenerationImageUploader
  100. settings={visionConfig}
  101. onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
  102. type: 'image',
  103. transfer_method: fileItem.type,
  104. url: fileItem.url,
  105. upload_file_id: fileItem.fileId,
  106. })))}
  107. />
  108. </div>
  109. </div>
  110. )
  111. }
  112. {promptConfig.prompt_variables.length > 0 && (
  113. <div className='mt-4 h-[1px] bg-gray-100'></div>
  114. )}
  115. <div className='w-full mt-4'>
  116. <div className="flex items-center justify-between">
  117. <Button
  118. onClick={onClear}
  119. disabled={false}
  120. >
  121. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  122. </Button>
  123. <Button
  124. variant="primary"
  125. onClick={onSend}
  126. disabled={false}
  127. >
  128. <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
  129. <span className='text-[13px]'>{t('share.generation.run')}</span>
  130. </Button>
  131. </div>
  132. </div>
  133. </form>
  134. </section>
  135. </div>
  136. )
  137. }
  138. export default React.memo(RunOnce)