Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.tsx 5.9KB

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