'use client'; import { useState } from 'react'; interface SearchResult { id: string; content: string; metadata: Record; type: string; similarity: number; } interface SemanticSearchProps { onResultSelect?: (result: SearchResult) => void; placeholder?: string; } export default function SemanticSearch({ onResultSelect, placeholder = 'Семантический поиск по базе знаний...' }: SemanticSearchProps) { const [query, setQuery] = useState(''); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const [type, setType] = useState('all'); const handleSearch = async () => { if (!query.trim()) { return; } setLoading(true); try { const response = await fetch('/api/knowledge/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query, type: type === 'all' ? undefined : type, limit: 10, threshold: 0.7, }), }); const data = await response.json(); setResults(data.results || []); } catch (error) { // Ошибка уже обработана в API setResults([]); } finally { setLoading(false); } }; return (
setQuery(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleSearch()} placeholder={placeholder} style={{ flex: 1, padding: '8px 12px', border: '1px solid #ccc', borderRadius: '4px', fontSize: '14px', }} />
{results.length > 0 && (
Найдено: {results.length} результатов
{results.map((result) => (
onResultSelect?.(result)} style={{ padding: '12px', backgroundColor: '#f5f5f5', borderRadius: '4px', cursor: onResultSelect ? 'pointer' : 'default', borderLeft: `4px solid ${ result.type === 'risk' ? '#f44336' : result.type === 'audit' ? '#ff9800' : result.type === 'aircraft' ? '#2196f3' : '#4caf50' }`, }} >
{result.type} Релевантность: {Math.round(result.similarity * 100)}%
{result.content.substring(0, 200)} {result.content.length > 200 && '...'}
))}
)}
); }