index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
  66. />
  67. )}
  68. {item.type === 'paragraph' && (
  69. <textarea
  70. 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 "
  71. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  72. value={inputs[item.key]}
  73. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  74. />
  75. )}
  76. </div>
  77. </div>
  78. ))}
  79. {
  80. visionConfig?.enabled && (
  81. <div className="w-full mt-4">
  82. <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div>
  83. <div className='mt-2'>
  84. <TextGenerationImageUploader
  85. settings={visionConfig}
  86. onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
  87. type: 'image',
  88. transfer_method: fileItem.type,
  89. url: fileItem.url,
  90. upload_file_id: fileItem.fileId,
  91. })))}
  92. />
  93. </div>
  94. </div>
  95. )
  96. }
  97. {promptConfig.prompt_variables.length > 0 && (
  98. <div className='mt-4 h-[1px] bg-gray-100'></div>
  99. )}
  100. <div className='w-full mt-4'>
  101. <div className="flex items-center justify-between">
  102. <Button
  103. className='!h-8 !p-3'
  104. onClick={onClear}
  105. disabled={false}
  106. >
  107. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  108. </Button>
  109. <Button
  110. type="primary"
  111. className='!h-8 !pl-3 !pr-4'
  112. onClick={onSend}
  113. disabled={false}
  114. >
  115. <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
  116. <span className='text-[13px]'>{t('share.generation.run')}</span>
  117. </Button>
  118. </div>
  119. </div>
  120. </form>
  121. </section>
  122. </div>
  123. )
  124. }
  125. export default React.memo(RunOnce)