index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React, { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { usePathname, useRouter } from 'next/navigation'
  4. import produce from 'immer'
  5. import { RiEqualizer2Line, RiExternalLinkLine } from '@remixicon/react'
  6. import { MessageFast } from '@/app/components/base/icons/src/vender/features'
  7. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  8. import Button from '@/app/components/base/button'
  9. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  10. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  11. import useAnnotationConfig from '@/app/components/base/features/new-feature-panel/annotation-reply/use-annotation-config'
  12. import ConfigParamModal from '@/app/components/base/features/new-feature-panel/annotation-reply/config-param-modal'
  13. import AnnotationFullModal from '@/app/components/billing/annotation-full/modal'
  14. import { ANNOTATION_DEFAULT } from '@/config'
  15. type Props = {
  16. disabled?: boolean
  17. onChange?: OnFeaturesChange
  18. }
  19. const AnnotationReply = ({
  20. disabled,
  21. onChange,
  22. }: Props) => {
  23. const { t } = useTranslation()
  24. const router = useRouter()
  25. const pathname = usePathname()
  26. const matched = pathname.match(/\/app\/([^/]+)/)
  27. const appId = (matched?.length && matched[1]) ? matched[1] : ''
  28. const featuresStore = useFeaturesStore()
  29. const annotationReply = useFeatures(s => s.features.annotationReply)
  30. const updateAnnotationReply = useCallback((newConfig: any) => {
  31. const {
  32. features,
  33. setFeatures,
  34. } = featuresStore!.getState()
  35. const newFeatures = produce(features, (draft) => {
  36. draft.annotationReply = newConfig
  37. })
  38. setFeatures(newFeatures)
  39. if (onChange)
  40. onChange(newFeatures)
  41. }, [featuresStore, onChange])
  42. const {
  43. handleEnableAnnotation,
  44. handleDisableAnnotation,
  45. isShowAnnotationConfigInit,
  46. setIsShowAnnotationConfigInit,
  47. isShowAnnotationFullModal,
  48. setIsShowAnnotationFullModal,
  49. } = useAnnotationConfig({
  50. appId,
  51. annotationConfig: annotationReply as any || {
  52. id: '',
  53. enabled: false,
  54. score_threshold: ANNOTATION_DEFAULT.score_threshold,
  55. embedding_model: {
  56. embedding_provider_name: '',
  57. embedding_model_name: '',
  58. },
  59. },
  60. setAnnotationConfig: updateAnnotationReply,
  61. })
  62. const handleSwitch = useCallback((enabled: boolean) => {
  63. if (enabled)
  64. setIsShowAnnotationConfigInit(true)
  65. else
  66. handleDisableAnnotation(annotationReply?.embedding_model as any)
  67. }, [annotationReply?.embedding_model, handleDisableAnnotation, setIsShowAnnotationConfigInit])
  68. const [isHovering, setIsHovering] = useState(false)
  69. return (
  70. <>
  71. <FeatureCard
  72. icon={
  73. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-util-colors-indigo-indigo-600'>
  74. <MessageFast className='w-4 h-4 text-text-primary-on-surface' />
  75. </div>
  76. }
  77. title={t('appDebug.feature.annotation.title')}
  78. value={!!annotationReply?.enabled}
  79. onChange={state => handleSwitch(state)}
  80. onMouseEnter={() => setIsHovering(true)}
  81. onMouseLeave={() => setIsHovering(false)}
  82. disabled={disabled}
  83. >
  84. <>
  85. {!annotationReply?.enabled && (
  86. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.annotation.description')}</div>
  87. )}
  88. {!!annotationReply?.enabled && (
  89. <>
  90. {!isHovering && (
  91. <div className='pt-0.5 flex items-center gap-4'>
  92. <div className=''>
  93. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.annotation.scoreThreshold.title')}</div>
  94. <div className='text-text-secondary system-xs-regular'>{annotationReply.score_threshold || '-'}</div>
  95. </div>
  96. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  97. <div className=''>
  98. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('common.modelProvider.embeddingModel.key')}</div>
  99. <div className='text-text-secondary system-xs-regular'>{annotationReply.embedding_model?.embedding_model_name}</div>
  100. </div>
  101. </div>
  102. )}
  103. {isHovering && (
  104. <div className='flex items-center justify-between'>
  105. <Button className='w-[178px]' onClick={() => setIsShowAnnotationConfigInit(true)} disabled={disabled}>
  106. <RiEqualizer2Line className='mr-1 w-4 h-4' />
  107. {t('common.operation.params')}
  108. </Button>
  109. <Button className='w-[178px]' onClick={() => {
  110. router.push(`/app/${appId}/annotations`)
  111. }}>
  112. <RiExternalLinkLine className='mr-1 w-4 h-4' />
  113. {t('appDebug.feature.annotation.cacheManagement')}
  114. </Button>
  115. </div>
  116. )}
  117. </>
  118. )}
  119. </>
  120. </FeatureCard>
  121. <ConfigParamModal
  122. appId={appId}
  123. isInit
  124. isShow={isShowAnnotationConfigInit}
  125. onHide={() => {
  126. setIsShowAnnotationConfigInit(false)
  127. // showChooseFeatureTrue()
  128. }}
  129. onSave={async (embeddingModel, score) => {
  130. await handleEnableAnnotation(embeddingModel, score)
  131. setIsShowAnnotationConfigInit(false)
  132. }}
  133. annotationConfig={annotationReply as any}
  134. />
  135. {isShowAnnotationFullModal && (
  136. <AnnotationFullModal
  137. show={isShowAnnotationFullModal}
  138. onHide={() => setIsShowAnnotationFullModal(false)}
  139. />
  140. )}
  141. </>
  142. )
  143. }
  144. export default AnnotationReply