index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. export type IRunOnceProps = {
  13. siteInfo: SiteInfo
  14. promptConfig: PromptConfig
  15. inputs: Record<string, any>
  16. onInputsChange: (inputs: Record<string, any>) => void
  17. onSend: () => void
  18. }
  19. const RunOnce: FC<IRunOnceProps> = ({
  20. promptConfig,
  21. inputs,
  22. onInputsChange,
  23. onSend,
  24. }) => {
  25. const { t } = useTranslation()
  26. const onClear = () => {
  27. const newInputs: Record<string, any> = {}
  28. promptConfig.prompt_variables.forEach((item) => {
  29. newInputs[item.key] = ''
  30. })
  31. onInputsChange(newInputs)
  32. }
  33. return (
  34. <div className="">
  35. <section>
  36. {/* input form */}
  37. <form>
  38. {promptConfig.prompt_variables.map(item => (
  39. <div className='w-full mt-4' key={item.key}>
  40. <label className='text-gray-900 text-sm font-medium'>{item.name}</label>
  41. <div className='mt-2'>
  42. {item.type === 'select' && (
  43. <Select
  44. className='w-full'
  45. defaultValue={inputs[item.key]}
  46. onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }}
  47. items={(item.options || []).map(i => ({ name: i, value: i }))}
  48. allowSearch={false}
  49. bgClassName='bg-gray-50'
  50. />
  51. )}
  52. {item.type === 'string' && (
  53. <input
  54. type="text"
  55. 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 "
  56. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  57. value={inputs[item.key]}
  58. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  59. maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}
  60. />
  61. )}
  62. {item.type === 'paragraph' && (
  63. <textarea
  64. 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 "
  65. placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  66. value={inputs[item.key]}
  67. onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}
  68. />
  69. )}
  70. </div>
  71. </div>
  72. ))}
  73. {promptConfig.prompt_variables.length > 0 && (
  74. <div className='mt-4 h-[1px] bg-gray-100'></div>
  75. )}
  76. <div className='w-full mt-4'>
  77. <div className="flex items-center justify-between">
  78. <Button
  79. className='!h-8 !p-3'
  80. onClick={onClear}
  81. disabled={false}
  82. >
  83. <span className='text-[13px]'>{t('common.operation.clear')}</span>
  84. </Button>
  85. <Button
  86. type="primary"
  87. className='!h-8 !pl-3 !pr-4'
  88. onClick={onSend}
  89. disabled={false}
  90. >
  91. <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />
  92. <span className='text-[13px]'>{t('share.generation.run')}</span>
  93. </Button>
  94. </div>
  95. </div>
  96. </form>
  97. </section>
  98. </div>
  99. )
  100. }
  101. export default React.memo(RunOnce)