// 全局变量 let allKnowledge = []; let availableTags = []; let currentPage = 1; let pageSize = 200; let totalPages = 1; let totalCount = 0; let isSearchMode = false; let selectedIds = new Set(); let _allTools = []; let _activeCategory = 'all'; // 加载 Tags async function loadTags() { const res = await fetch('/api/knowledge/meta/tags'); const data = await res.json(); availableTags = data.tags; renderTagsFilter(); } function renderTagsFilter() { const container = document.getElementById('tagsFilterContainer'); if (availableTags.length === 0) { container.innerHTML = '
暂无 tags
'; return; } container.innerHTML = availableTags.map(tag => `` ).join(''); } // 加载知识列表 async function loadKnowledge(page = 1) { const params = new URLSearchParams(); params.append('page', page); params.append('page_size', pageSize); const selectedTypes = Array.from(document.querySelectorAll('.type-filter:checked')).map(el => el.value); if (selectedTypes.length > 0) { params.append('types', selectedTypes.join(',')); } const selectedTags = Array.from(document.querySelectorAll('.tag-filter:checked')).map(el => el.value); if (selectedTags.length > 0) { params.append('tags', selectedTags.join(',')); } const ownerFilter = document.getElementById('ownerFilter').value.trim(); if (ownerFilter) { params.append('owner', ownerFilter); } const scopesFilter = document.getElementById('scopesFilter').value.trim(); if (scopesFilter) { params.append('scopes', scopesFilter); } const selectedStatus = Array.from(document.querySelectorAll('.status-filter:checked')).map(el => el.value); if (selectedStatus.length > 0) { params.append('status', selectedStatus.join(',')); } try { const res = await fetch(`/api/knowledge?${params.toString()}`); if (!res.ok) { console.error('加载失败:', res.status, res.statusText); document.getElementById('knowledgeList').innerHTML = '加载失败,请刷新页面重试
'; return; } const data = await res.json(); allKnowledge = data.results || []; currentPage = data.pagination.page; totalPages = data.pagination.total_pages; totalCount = data.pagination.total; renderKnowledge(allKnowledge); updatePagination(); } catch (error) { console.error('加载错误:', error); document.getElementById('knowledgeList').innerHTML = '加载错误: ' + error.message + '
'; } } function applyFilters() { currentPage = 1; loadKnowledge(currentPage); } function goToPage(page) { if (page < 1 || page > totalPages) return; loadKnowledge(page); } function updatePagination() { const paginationDiv = document.getElementById('pagination'); const pageInfo = document.getElementById('pageInfo'); const prevBtn = document.getElementById('prevBtn'); const nextBtn = document.getElementById('nextBtn'); if (totalPages <= 1) { paginationDiv.classList.add('hidden'); } else { paginationDiv.classList.remove('hidden'); pageInfo.textContent = `第 ${currentPage} / ${totalPages} 页 (共 ${totalCount} 条)`; prevBtn.disabled = currentPage === 1; nextBtn.disabled = currentPage === totalPages; } } // 搜索功能 async function performSearch() { const query = document.getElementById('searchInput').value.trim(); if (!query) { alert('请输入搜索内容'); return; } isSearchMode = true; const statusDiv = document.getElementById('searchStatus'); statusDiv.textContent = '搜索中...'; statusDiv.classList.remove('hidden'); try { const params = new URLSearchParams(); params.append('q', query); params.append('top_k', '20'); params.append('min_score', '1'); const selectedTypes = Array.from(document.querySelectorAll('.type-filter:checked')).map(el => el.value); if (selectedTypes.length > 0) { params.append('types', selectedTypes.join(',')); } const ownerFilter = document.getElementById('ownerFilter').value.trim(); if (ownerFilter) { params.append('owner', ownerFilter); } const res = await fetch(`/api/knowledge/search?${params.toString()}`); if (!res.ok) { throw new Error(`搜索失败: ${res.status}`); } const data = await res.json(); allKnowledge = data.results || []; statusDiv.textContent = `找到 ${allKnowledge.length} 条相关知识${data.reranked ? ' (已智能排序)' : ''}`; renderKnowledge(allKnowledge); document.getElementById('pagination').classList.add('hidden'); } catch (error) { console.error('搜索错误:', error); statusDiv.textContent = '搜索失败: ' + error.message; statusDiv.classList.add('text-red-500'); } } function clearSearch() { document.getElementById('searchInput').value = ''; document.getElementById('searchStatus').classList.add('hidden'); document.getElementById('searchStatus').classList.remove('text-red-500'); isSearchMode = false; currentPage = 1; loadKnowledge(currentPage); } // 渲染知识列表 function renderKnowledge(list) { const container = document.getElementById('knowledgeList'); if (list.length === 0) { container.innerHTML = '暂无知识
'; return; } container.innerHTML = list.map(k => { let types = []; if (Array.isArray(k.types)) { types = k.types; } else if (typeof k.types === 'string') { if (k.types.startsWith('[')) { try { types = JSON.parse(k.types); } catch (e) { console.error('解析types失败:', k.types, e); types = [k.types]; } } else { types = [k.types]; } } const eval_data = k.eval || {}; const isChecked = selectedIds.has(k.id); const statusColor = { 'approved': 'bg-green-100 text-green-800', 'checked': 'bg-blue-100 text-blue-800', 'rejected': 'bg-red-100 text-red-800', 'pending': 'bg-yellow-100 text-yellow-800', 'processing': 'bg-orange-100 text-orange-800', }; const statusClass = statusColor[k.status] || 'bg-gray-100 text-gray-800'; const statusLabel = k.status || 'approved'; const toolIds = (k.resource_ids || []).filter(id => id.startsWith('tools/')); const toolTagsHtml = toolIds.length > 0 ? `${escapeHtml(k.content.substring(0, 150))}${k.content.length > 150 ? '...' : ''}
加载失败
'; } } function renderCategoryTabs() { const cats = ['all', ...new Set(_allTools.map(t => t.metadata && t.metadata.category ? t.metadata.category : 'other'))]; document.getElementById('toolCategoryTabs').innerHTML = cats.map(cat => { const isActive = cat === _activeCategory; const activeClass = 'bg-indigo-600 text-white border-indigo-600'; const inactiveClass = 'bg-white text-gray-600 border-gray-300 hover:border-indigo-400'; return ``; }).join(''); } function renderToolList(category) { _activeCategory = category; document.querySelectorAll('.tool-cat-tab').forEach(btn => { const isCurrent = btn.id === `tab_${category}`; btn.className = `tool-cat-tab px-4 py-1.5 rounded-full text-sm font-medium border transition ${ isCurrent ? 'bg-indigo-600 text-white border-indigo-600' : 'bg-white text-gray-600 border-gray-300 hover:border-indigo-400' }`; }); const filtered = category === 'all' ? _allTools : _allTools.filter(t => (t.metadata && t.metadata.category ? t.metadata.category : 'other') === category); const listHtml = filtered.length === 0 ? '该分类下暂无工具
' : filtered.map(t => `加载详情中...
加载中...
'; try { const res = await fetch(`/api/knowledge/${encodeURIComponent(id)}`); if (!res.ok) { contentEl.innerHTML = '知识未找到
'; return; } const k = await res.json(); const statusColor = { 'approved': 'bg-green-100 text-green-800', 'checked': 'bg-blue-100 text-blue-800', 'rejected': 'bg-red-100 text-red-800', 'pending': 'bg-yellow-100 text-yellow-800', }; const types = Array.isArray(k.types) ? k.types : []; const tags = k.tags || {}; const tagKeys = Object.keys(tags); contentEl.innerHTML = `加载失败
'; console.error(err); } } function closeKnowledgeDetailModal() { document.getElementById('knowledgeDetailModal').classList.add('hidden'); } function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; }