Keep/keep-notes/hooks/use-auto-tagging.ts
sepehr 3c4b9d6176 feat(ai): implement intelligent auto-tagging system
- Added multi-provider AI infrastructure (OpenAI/Ollama)
- Implemented real-time tag suggestions with debounced analysis
- Created AI diagnostics and database maintenance tools in Settings
- Added automated garbage collection for orphan labels
- Refined UX with deterministic color hashing and interactive ghost tags
2026-01-08 22:59:52 +01:00

62 lines
1.8 KiB
TypeScript

import { useState, useEffect } from 'react';
import { useDebounce } from './use-debounce';
import { TagSuggestion } from '@/lib/ai/types';
interface UseAutoTaggingProps {
content: string;
enabled?: boolean;
}
export function useAutoTagging({ content, enabled = true }: UseAutoTaggingProps) {
const [suggestions, setSuggestions] = useState<TagSuggestion[]>([]);
const [isAnalyzing, setIsAnalyzing] = useState(false);
const [error, setError] = useState<string | null>(null);
// Debounce le contenu de 1.5s
const debouncedContent = useDebounce(content, 1500);
useEffect(() => {
// console.log('AutoTagging Effect:', { enabled, contentLength: debouncedContent?.length });
if (!enabled || !debouncedContent || debouncedContent.length < 10) {
setSuggestions([]);
return;
}
const analyzeContent = async () => {
console.log('🚀 Triggering AI analysis for:', debouncedContent.substring(0, 20) + '...');
setIsAnalyzing(true);
setError(null);
try {
const response = await fetch('/api/ai/tags', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content: debouncedContent }),
});
if (!response.ok) {
throw new Error('Erreur lors de l\'analyse');
}
const data = await response.json();
console.log('✅ AI Response:', data);
setSuggestions(data.tags || []);
} catch (err) {
console.error('❌ Auto-tagging error:', err);
setError('Impossible de générer des suggestions');
} finally {
setIsAnalyzing(false);
}
};
analyzeContent();
}, [debouncedContent, enabled]);
return {
suggestions,
isAnalyzing,
error,
clearSuggestions: () => setSuggestions([]),
};
}