feature-modal.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import produce from 'immer'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. useFeatures,
  8. useFeaturesStore,
  9. } from '../hooks'
  10. import type { OnFeaturesChange } from '../types'
  11. import FeatureGroup from './feature-group'
  12. import FeatureItem from './feature-item'
  13. import Modal from '@/app/components/base/modal'
  14. import SuggestedQuestionsAfterAnswerIcon from '@/app/components/app/configuration/base/icons/suggested-questions-after-answer-icon'
  15. import { Microphone01, Speaker } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  16. import { Citations } from '@/app/components/base/icons/src/vender/solid/editor'
  17. import { FileSearch02 } from '@/app/components/base/icons/src/vender/solid/files'
  18. import { MessageHeartCircle } from '@/app/components/base/icons/src/vender/solid/communication'
  19. import { FeatureEnum } from '@/app/components/base/features/types'
  20. import { useDefaultModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  21. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  22. export type FeatureModalProps = {
  23. onChange?: OnFeaturesChange
  24. }
  25. const FeatureModal: FC<FeatureModalProps> = ({
  26. onChange,
  27. }) => {
  28. const { t } = useTranslation()
  29. const { data: speech2textDefaultModel } = useDefaultModel(ModelTypeEnum.speech2text)
  30. const { data: text2speechDefaultModel } = useDefaultModel(ModelTypeEnum.tts)
  31. const featuresStore = useFeaturesStore()
  32. const setShowFeaturesModal = useFeatures(s => s.setShowFeaturesModal)
  33. const features = useFeatures(s => s.features)
  34. const handleCancelModal = useCallback(() => {
  35. setShowFeaturesModal(false)
  36. }, [setShowFeaturesModal])
  37. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  38. const {
  39. features,
  40. setFeatures,
  41. } = featuresStore!.getState()
  42. const newFeatures = produce(features, (draft) => {
  43. draft[type] = {
  44. ...draft[type],
  45. enabled,
  46. }
  47. })
  48. setFeatures(newFeatures)
  49. if (onChange)
  50. onChange(newFeatures)
  51. }, [featuresStore, onChange])
  52. return (
  53. <Modal
  54. isShow
  55. onClose={handleCancelModal}
  56. className='w-[400px]'
  57. title={t('appDebug.operation.addFeature')}
  58. closable
  59. overflowVisible
  60. >
  61. <div className='pt-5 pb-10'>
  62. {/* Chat Feature */}
  63. <FeatureGroup
  64. title={t('appDebug.feature.groupChat.title')}
  65. description={t('appDebug.feature.groupChat.description') as string}
  66. >
  67. <>
  68. <FeatureItem
  69. icon={<MessageHeartCircle className='w-4 h-4 text-[#DD2590]' />}
  70. previewImgClassName='openingStatementPreview'
  71. title={t('appDebug.feature.conversationOpener.title')}
  72. description={t('appDebug.feature.conversationOpener.description')}
  73. value={!!features.opening?.enabled}
  74. onChange={handleChange}
  75. type={FeatureEnum.opening}
  76. />
  77. <FeatureItem
  78. icon={<SuggestedQuestionsAfterAnswerIcon />}
  79. previewImgClassName='suggestedQuestionsAfterAnswerPreview'
  80. title={t('appDebug.feature.suggestedQuestionsAfterAnswer.title')}
  81. description={t('appDebug.feature.suggestedQuestionsAfterAnswer.description')}
  82. value={!!features.suggested?.enabled}
  83. onChange={handleChange}
  84. type={FeatureEnum.suggested}
  85. />
  86. {
  87. !!text2speechDefaultModel && (
  88. <FeatureItem
  89. icon={<Speaker className='w-4 h-4 text-[#7839EE]' />}
  90. previewImgClassName='textToSpeechPreview'
  91. title={t('appDebug.feature.textToSpeech.title')}
  92. description={t('appDebug.feature.textToSpeech.description')}
  93. value={!!features.text2speech?.enabled}
  94. onChange={handleChange}
  95. type={FeatureEnum.text2speech}
  96. />
  97. )
  98. }
  99. {
  100. !!speech2textDefaultModel && (
  101. <FeatureItem
  102. icon={<Microphone01 className='w-4 h-4 text-[#7839EE]' />}
  103. previewImgClassName='speechToTextPreview'
  104. title={t('appDebug.feature.speechToText.title')}
  105. description={t('appDebug.feature.speechToText.description')}
  106. value={!!features.speech2text?.enabled}
  107. onChange={handleChange}
  108. type={FeatureEnum.speech2text}
  109. />
  110. )
  111. }
  112. <FeatureItem
  113. icon={<Citations className='w-4 h-4 text-[#FD853A]' />}
  114. previewImgClassName='citationPreview'
  115. title={t('appDebug.feature.citation.title')}
  116. description={t('appDebug.feature.citation.description')}
  117. value={!!features.citation?.enabled}
  118. onChange={handleChange}
  119. type={FeatureEnum.citation}
  120. />
  121. </>
  122. </FeatureGroup>
  123. <FeatureGroup title={t('appDebug.feature.toolbox.title')}>
  124. <>
  125. <FeatureItem
  126. icon={<FileSearch02 className='w-4 h-4 text-[#039855]' />}
  127. previewImgClassName=''
  128. title={t('appDebug.feature.moderation.title')}
  129. description={t('appDebug.feature.moderation.description')}
  130. value={!!features.moderation?.enabled}
  131. onChange={handleChange}
  132. type={FeatureEnum.moderation}
  133. />
  134. </>
  135. </FeatureGroup>
  136. </div>
  137. </Modal>
  138. )
  139. }
  140. export default React.memo(FeatureModal)