"use client"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { getEmbeddingUsage } from "@/features/knowledge/api/engines"; import type { EmbeddingUsage } from "@/features/knowledge/model/types"; export default function EmbeddingModelUsage({ profileId, modelId, }: { profileId: string; modelId: string; }) { const { t } = useTranslation(); const [usage, setUsage] = useState(null); const [error, setError] = useState(false); const [attempt, setAttempt] = useState(0); const requestKey = JSON.stringify([profileId, modelId, attempt]); const [resolvedKey, setResolvedKey] = useState(requestKey); if (resolvedKey !== requestKey) { setResolvedKey(requestKey); setUsage(null); setError(false); } useEffect(() => { let cancelled = false; void getEmbeddingUsage() .then((rows) => { if (!cancelled) setUsage( rows.filter( (row) => row.profile_id === profileId && row.model_id === modelId, ), ); }) .catch(() => { if (!cancelled) setError(true); }); return () => { cancelled = true; }; }, [profileId, modelId, attempt]); return (

{t("Knowledge bases using this model")} {usage && ` (${usage.length})`}

{error ? (

{t("Failed to load embedding model usage")}

{t("This list shows knowledge base references, not the model connection status.")}

) : !usage ? (

{t("Loading...")}

) : usage.length === 0 ? (

{t("No knowledge bases use this model.")}

) : ( <>

{t( "Deleting this model is allowed. These knowledge bases will need another embedding model before they can be used again. Their documents and indexes will be preserved.", )}

)}
); }