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
This commit is contained in:
61
keep-notes/hooks/use-auto-tagging.ts
Normal file
61
keep-notes/hooks/use-auto-tagging.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
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([]),
|
||||
};
|
||||
}
|
||||
17
keep-notes/hooks/use-debounce.ts
Normal file
17
keep-notes/hooks/use-debounce.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function useDebounce<T>(value: T, delay: number): T {
|
||||
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setDebouncedValue(value);
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [value, delay]);
|
||||
|
||||
return debouncedValue;
|
||||
}
|
||||
Reference in New Issue
Block a user