import React, { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import { usePathname, useRouter } from 'next/navigation' import produce from 'immer' import { RiEqualizer2Line, RiExternalLinkLine } from '@remixicon/react' import { MessageFast } from '@/app/components/base/icons/src/vender/features' import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card' import Button from '@/app/components/base/button' import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks' import type { OnFeaturesChange } from '@/app/components/base/features/types' import useAnnotationConfig from '@/app/components/base/features/new-feature-panel/annotation-reply/use-annotation-config' import ConfigParamModal from '@/app/components/base/features/new-feature-panel/annotation-reply/config-param-modal' import AnnotationFullModal from '@/app/components/billing/annotation-full/modal' import { ANNOTATION_DEFAULT } from '@/config' type Props = { disabled?: boolean onChange?: OnFeaturesChange } const AnnotationReply = ({ disabled, onChange, }: Props) => { const { t } = useTranslation() const router = useRouter() const pathname = usePathname() const matched = pathname.match(/\/app\/([^/]+)/) const appId = (matched?.length && matched[1]) ? matched[1] : '' const featuresStore = useFeaturesStore() const annotationReply = useFeatures(s => s.features.annotationReply) const updateAnnotationReply = useCallback((newConfig: any) => { const { features, setFeatures, } = featuresStore!.getState() const newFeatures = produce(features, (draft) => { draft.annotationReply = newConfig }) setFeatures(newFeatures) if (onChange) onChange(newFeatures) }, [featuresStore, onChange]) const { handleEnableAnnotation, handleDisableAnnotation, isShowAnnotationConfigInit, setIsShowAnnotationConfigInit, isShowAnnotationFullModal, setIsShowAnnotationFullModal, } = useAnnotationConfig({ appId, annotationConfig: annotationReply as any || { id: '', enabled: false, score_threshold: ANNOTATION_DEFAULT.score_threshold, embedding_model: { embedding_provider_name: '', embedding_model_name: '', }, }, setAnnotationConfig: updateAnnotationReply, }) const handleSwitch = useCallback((enabled: boolean) => { if (enabled) setIsShowAnnotationConfigInit(true) else handleDisableAnnotation(annotationReply?.embedding_model as any) }, [annotationReply?.embedding_model, handleDisableAnnotation, setIsShowAnnotationConfigInit]) const [isHovering, setIsHovering] = useState(false) return ( <> } title={t('appDebug.feature.annotation.title')} value={!!annotationReply?.enabled} onChange={state => handleSwitch(state)} onMouseEnter={() => setIsHovering(true)} onMouseLeave={() => setIsHovering(false)} disabled={disabled} > <> {!annotationReply?.enabled && ( {t('appDebug.feature.annotation.description')} )} {!!annotationReply?.enabled && ( <> {!isHovering && ( {t('appDebug.feature.annotation.scoreThreshold.title')} {annotationReply.score_threshold || '-'} {t('common.modelProvider.embeddingModel.key')} {annotationReply.embedding_model?.embedding_model_name} )} {isHovering && ( setIsShowAnnotationConfigInit(true)} disabled={disabled}> {t('common.operation.params')} { router.push(`/app/${appId}/annotations`) }}> {t('appDebug.feature.annotation.cacheManagement')} )} > )} > { setIsShowAnnotationConfigInit(false) // showChooseFeatureTrue() }} onSave={async (embeddingModel, score) => { await handleEnableAnnotation(embeddingModel, score) setIsShowAnnotationConfigInit(false) }} annotationConfig={annotationReply as any} /> {isShowAnnotationFullModal && ( setIsShowAnnotationFullModal(false)} /> )} > ) } export default AnnotationReply