Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: Enhance FAQ import functionality and UI improvements

wizardchen057cfd9eb019
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 6d8675f1e..bf13f6121 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -10,10 +10,12 @@
"dependencies": {
"@microsoft/fetch-event-source": "^2.0.1",
"@types/dompurify": "^3.0.5",
+ "@types/papaparse": "^5.5.0",
"axios": "^1.8.4",
"dompurify": "^3.2.6",
"marked": "^5.1.2",
"pagefind": "^1.1.1",
+ "papaparse": "^5.5.3",
"pinia": "^3.0.1",
"swiper": "^12.0.3",
"tdesign-icons-vue-next": "^0.4.1",
@@ -1394,6 +1396,15 @@
"undici-types": "~6.21.0"
}
},
+ "node_modules/@types/papaparse": {
+ "version": "5.5.0",
+ "resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-5.5.0.tgz",
+ "integrity": "sha512-GVs5iMQmUr54BAZYYkByv8zPofFxmyxUpISPb2oh8sayR3+1zbxasrOvoKiHJ/nnoq/uULuPsu1Lze1EkagVFg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/sortablejs": {
"version": "1.15.8",
"resolved": "https://mirrors.tencent.com/npm/@types/sortablejs/-/sortablejs-1.15.8.tgz",
@@ -3046,6 +3057,12 @@
"@pagefind/windows-x64": "1.3.0"
}
},
+ "node_modules/papaparse": {
+ "version": "5.5.3",
+ "resolved": "https://registry.npmjs.org/papaparse/-/papaparse-5.5.3.tgz",
+ "integrity": "sha512-5QvjGxYVjxO59MGU2lHVYpRWBBtKHnlIAcSe1uNFCkkptUh63NFRj0FJQm7nR67puEruUci/ZkjmEFrjCAyP4A==",
+ "license": "MIT"
+ },
"node_modules/parse-node-version": {
"version": "1.0.1",
"resolved": "https://mirrors.tencent.com/npm/parse-node-version/-/parse-node-version-1.0.1.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index 6b942682b..d8958c737 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -14,10 +14,12 @@
"dependencies": {
"@microsoft/fetch-event-source": "^2.0.1",
"@types/dompurify": "^3.0.5",
+ "@types/papaparse": "^5.5.0",
"axios": "^1.8.4",
"dompurify": "^3.2.6",
"marked": "^5.1.2",
"pagefind": "^1.1.1",
+ "papaparse": "^5.5.3",
"pinia": "^3.0.1",
"swiper": "^12.0.3",
"tdesign-icons-vue-next": "^0.4.1",
diff --git a/frontend/src/components/menu.vue b/frontend/src/components/menu.vue
index ae82f0ccf..d4776d6f0 100644
--- a/frontend/src/components/menu.vue
+++ b/frontend/src/components/menu.vue
@@ -85,6 +85,37 @@
<span class="kb-action-title">{{ t('knowledgeEditor.faqImport.importButton') }}</span>
</div>
</div>
+ <div class="menu_item kb-action-item" @click.stop="handleFaqSearchTestFromMenu">
+ <div class="kb-action-icon-wrapper">
+ <svg class="kb-action-icon" width="18" height="18" viewBox="0 0 18 18" fill="none">
+ <path d="M8.25 15C11.9779 15 15 11.9779 15 8.25C15 4.52208 11.9779 1.5 8.25 1.5C4.52208 1.5 1.5 4.52208 1.5 8.25C1.5 11.9779 4.52208 15 8.25 15Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
+ <path d="M16.5 16.5L12.4875 12.4875" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
+ </svg>
+ </div>
+ <div class="kb-action-content">
+ <span class="kb-action-title">{{ t('knowledgeEditor.faq.searchTest') }}</span>
+ </div>
+ </div>
+ <t-dropdown
+ v-if="selectedFaqCount > 0"
+ :options="faqBatchActionOptions"
+ trigger="hover"
+ placement="right"
+ @click="handleFaqBatchActionFromMenu"
+ >
+ <div class="menu_item kb-action-item">
+ <div class="kb-action-icon-wrapper">
+ <svg class="kb-action-icon" width="18" height="18" viewBox="0 0 18 18" fill="none">
+ <path d="M3.75 9H14.25M9 3.75V14.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
+ <path d="M3.75 3.75H14.25V14.25H3.75V3.75Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
+ </svg>
+ </div>
+ <div class="kb-action-content">
+ <span class="kb-action-title">{{ t('knowledgeEditor.faq.batchOperations') }}</span>
+ <span class="kb-action-count">({{ selectedFaqCount }})</span>
+ </div>
+ </div>
+ </t-dropdown>
</template>
</div>
</div>
@@ -153,7 +184,7 @@
<script setup lang="ts">
import { storeToRefs } from 'pinia';
-import { onMounted, watch, computed, ref, reactive } from 'vue';
+import { onMounted, onUnmounted, watch, computed, ref, reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getSessionsList, delSession } from "@/api/chat/index";
import { getKnowledgeBaseById, uploadKnowledgeFile, createKnowledgeFromURL } from '@/api/knowledge-base';
@@ -197,6 +228,11 @@ const isInKnowledgeBaseList = computed<boolean>(() => {
return route.name === 'knowledgeBaseList';
});
+// 是否在创建聊天页面
+const isInCreatChat = computed<boolean>(() => {
+ return route.name === 'globalCreatChat' || route.name === 'kbCreatChat';
+});
+
// 统一的菜单项激活状态判断
const isMenuItemActive = (itemPath: string): boolean => {
const currentRoute = route.name;
@@ -234,7 +270,7 @@ const getIconActiveState = (itemPath: string) => {
// 分离上下两部分菜单
const topMenuItems = computed<MenuItem[]>(() => {
return (menuArr.value as unknown as MenuItem[]).filter((item: MenuItem) =>
- item.path === 'creatChat'
+ item.path === 'knowledge-bases' || item.path === 'creatChat'
);
});
@@ -253,12 +289,23 @@ const currentKbInfo = ref<any>(null)
const docUploadInput = ref<HTMLInputElement | null>(null)
const docFolderInput = ref<HTMLInputElement | null>(null)
const pendingUploadKbId = ref<string | null>(null)
-
-const showKbActions = computed(() => (isInKnowledgeBase.value && !!currentKbInfo.value) || isInKnowledgeBaseList.value)
+const selectedFaqCount = ref<number>(0)
+const selectedFaqEnabledCount = ref<number>(0)
+const selectedFaqDisabledCount = ref<number>(0)
+
+// 监听FAQ选中数量变化
+const handleFaqSelectionChanged = ((event: CustomEvent<{ count: number; enabledCount?: number; disabledCount?: number }>) => {
+ const count = event.detail?.count || 0
+ selectedFaqCount.value = count
+ selectedFaqEnabledCount.value = event.detail?.enabledCount || 0
+ selectedFaqDisabledCount.value = event.detail?.disabledCount || 0
+}) as EventListener
+
+const showKbActions = computed(() => (isInKnowledgeBase.value && !!currentKbInfo.value) || isInKnowledgeBaseList.value || isInCreatChat.value)
const currentKbType = computed(() => currentKbInfo.value?.type || 'document')
const showDocActions = computed(() => showKbActions.value && isInKnowledgeBase.value && currentKbType.value !== 'faq')
const showFaqActions = computed(() => showKbActions.value && isInKnowledgeBase.value && currentKbType.value === 'faq')
-const showCreateKbAction = computed(() => showKbActions.value && isInKnowledgeBaseList.value)
+const showCreateKbAction = computed(() => showKbActions.value && (isInKnowledgeBaseList.value || isInCreatChat.value))
// 时间分组函数
const getTimeCategory = (dateStr: string): string => {
@@ -438,9 +485,20 @@ onMounted(async () => {
// 加载对话列表
getMessageList();
+
+ // 监听FAQ选中数量变化
+ window.addEventListener('faqSelectionChanged', handleFaqSelectionChanged)
});
+onUnmounted(() => {
+ window.removeEventListener('faqSelectionChanged', handleFaqSelectionChanged)
+})
+
watch([() => route.name, () => route.params], (newvalue, oldvalue) => {
+ // 切换知识库时重置选中数量
+ if (newvalue[1].kbId !== oldvalue?.[1]?.kbId) {
+ selectedFaqCount.value = 0
+ }
const nameStr = typeof newvalue[0] === 'string' ? (newvalue[0] as string) : (newvalue[0] ? String(newvalue[0]) : '')
currentpath.value = nameStr;
if (newvalue[1].chatid) {
@@ -558,13 +616,11 @@ const gotopage = async (path: string) => {
return;
} else {
if (path === 'creatChat') {
- // 尝试获取当前知识库ID
- const kbId = await getCurrentKbId()
- if (kbId) {
- // 如果在知识库内部,进入该知识库的对话页
- router.push(`/platform/knowledge-bases/${kbId}/creatChat`)
+ // 如果在知识库详情页,跳转到全局对话创建页
+ if (isInKnowledgeBase.value) {
+ router.push('/platform/creatChat')
} else {
- // 如果不在知识库内,也进入对话创建页,让用户通过 @ 按钮选择知识库
+ // 如果不在知识库内,进入对话创建页
router.push(`/platform/creatChat`)
}
} else {
@@ -938,7 +994,7 @@ const handleDocURLImport = async () => {
}))
}
-const dispatchFaqMenuAction = (action: 'create' | 'import', kbId: string) => {
+const dispatchFaqMenuAction = (action: 'create' | 'import' | 'search' | 'batch' | 'batchTag' | 'batchEnable' | 'batchDisable' | 'batchDelete', kbId: string) => {
window.dispatchEvent(new CustomEvent('faqMenuAction', {
detail: { action, kbId }
}))
@@ -962,6 +1018,65 @@ const handleFaqImportFromMenu = async () => {
dispatchFaqMenuAction('import', kbId)
}
+const handleFaqSearchTestFromMenu = async () => {
+ const kbId = await getCurrentKbId()
+ if (!kbId) {
+ MessagePlugin.warning(t('knowledgeEditor.messages.missingId'))
+ return
+ }
+ dispatchFaqMenuAction('search', kbId)
+}
+
+const faqBatchActionOptions = computed(() => {
+ if (selectedFaqCount.value === 0) {
+ return []
+ }
+ const options = [
+ {
+ content: `${t('knowledgeEditor.faq.batchUpdateTag')} (${selectedFaqCount.value})`,
+ value: 'batchTag',
+ icon: 'folder'
+ }
+ ]
+
+ // 根据选中条目的状态显示批量启用或禁用
+ if (selectedFaqDisabledCount.value > 0) {
+ options.push({
+ content: `${t('knowledgeEditor.faq.batchEnable')} (${selectedFaqDisabledCount.value})`,
+ value: 'batchEnable',
+ icon: 'check-circle',
+ })
+ }
+ if (selectedFaqEnabledCount.value > 0) {
+ options.push({
+ content: `${t('knowledgeEditor.faq.batchDisable')} (${selectedFaqEnabledCount.value})`,
+ value: 'batchDisable',
+ icon: 'close-circle',
+ })
+ }
+
+ options.push({
+ content: `${t('knowledgeEditor.faqImport.deleteSelected')} (${selectedFaqCount.value})`,
+ value: 'batchDelete',
+ icon: 'delete',
+ })
+
+ return options
+})
+
+const handleFaqBatchActionFromMenu = async (data: { value: string }) => {
+ const kbId = await getCurrentKbId()
+ if (!kbId) {
+ MessagePlugin.warning(t('knowledgeEditor.messages.missingId'))
+ return
+ }
+ if (selectedFaqCount.value === 0) {
+ MessagePlugin.warning(t('knowledgeEditor.faq.selectEntriesFirst') || '请先选中要操作的FAQ条目')
+ return
+ }
+ dispatchFaqMenuAction(data.value as 'batchTag' | 'batchEnable' | 'batchDisable' | 'batchDelete', kbId)
+}
+
const handleCreateKnowledgeBase = () => {
uiStore.openCreateKB()
}
@@ -1097,6 +1212,9 @@ const handleCreateKnowledgeBase = () => {
.kb-action-content {
flex: 1;
min-width: 0;
+ display: flex;
+ align-items: center;
+ white-space: nowrap;
}
.kb-action-title {
@@ -1104,10 +1222,21 @@ const handleCreateKnowledgeBase = () => {
font-weight: 500;
color: #0f172a;
transition: color 0.08s ease;
- display: block;
+ display: inline;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
+ flex-shrink: 1;
+ min-width: 0;
+ }
+
+ .kb-action-count {
+ font-size: 12px;
+ color: #10b981;
+ font-weight: 600;
+ margin-left: 4px;
+ flex-shrink: 0;
+ white-space: nowrap;
}
.menu_box {
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index 5f6b3cdc8..20e9b4ff2 100644
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -754,6 +754,8 @@ export default {
knowledgeGraph: 'Knowledge Graph Enabled',
multimodal: 'Multimodal Enabled'
},
+ processing: 'Processing import task',
+ processingDocuments: 'Processing {count} documents',
stats: {
documents: 'Document Count',
faqEntries: 'FAQ Entries',
@@ -863,6 +865,7 @@ export default {
statusEnableSuccess: 'FAQ entry enabled',
statusDisableSuccess: 'FAQ entry disabled',
statusUpdateFailed: 'Failed to update status',
+ batchOperations: 'Batch Operations',
batchUpdateTag: 'Batch Update Category',
batchUpdateTagTip: 'Set category for {count} selected entries',
batchEnable: 'Batch Enable',
@@ -874,7 +877,7 @@ export default {
appendMode: 'Append',
replaceMode: 'Replace existing entries',
fileLabel: 'Select File',
- fileTip: 'Supports JSON / CSV / Excel with fields standard_question, answers, similar_questions, negative_questions',
+ fileTip: 'Supports JSON / CSV / Excel. CSV/Excel headers: 分类(必填), 问题(必填), 相似问题(选填-多个用##分隔), 反例问题(选填-多个用##分隔), 机器人回答(必填-多个用##分隔), 是否全部回复(选填-默认FALSE), 是否停用(选填-默认FALSE), 是否禁止被推荐(选填-默认False 可被推荐). Also supports old format: standard_question, answers, similar_questions, negative_questions',
clickToUpload: 'Click to upload file',
dragDropTip: 'or drag and drop file here',
importButton: 'Import FAQ',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index ef145bdde..c98e33688 100644
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -842,6 +842,8 @@ export default {
knowledgeGraph: 'Граф знаний включен',
multimodal: 'Мультимодальность включена'
},
+ processing: 'Обработка задачи импорта',
+ processingDocuments: 'Обработка {count} документов',
stats: {
documents: 'Количество документов',
faqEntries: 'FAQ записи',
@@ -951,6 +953,7 @@ export default {
statusEnableSuccess: 'Запись FAQ включена',
statusDisableSuccess: 'Запись FAQ отключена',
statusUpdateFailed: 'Не удалось обновить статус',
+ batchOperations: 'Пакетные операции',
batchUpdateTag: 'Пакетное обновление категории',
batchUpdateTagTip: 'Установить категорию для {count} выбранных записей',
batchEnable: 'Пакетное включение',
@@ -962,7 +965,7 @@ export default {
appendMode: 'Добавить',
replaceMode: 'Заменить существующие записи',
fileLabel: 'Выберите файл',
- fileTip: 'Поддерживаются JSON / CSV / Excel. Поля: standard_question, answers, similar_questions, negative_questions',
+ fileTip: 'Поддерживаются JSON / CSV / Excel. Заголовки CSV/Excel: 分类(必填), 问题(必填), 相似问题(选填-多个用##分隔), 反例问题(选填-多个用##分隔), 机器人回答(必填-多个用##分隔), 是否全部回复(选填-默认FALSE), 是否停用(选填-默认FALSE), 是否禁止被推荐(选填-默认False 可被推荐). Также поддерживается старый формат: standard_question, answers, similar_questions, negative_questions',
clickToUpload: 'Нажмите для загрузки файла',
dragDropTip: 'или перетащите файл сюда',
importButton: 'Импортировать FAQ',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 9124ec4b8..2a93431a2 100644
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -1077,6 +1077,8 @@ export default {
knowledgeGraph: "已启用知识图谱",
multimodal: "已启用多模态",
},
+ processing: "正在处理导入任务",
+ processingDocuments: "正在处理 {count} 个文档",
stats: {
documents: "文档数量",
faqEntries: "问答条目",
@@ -1190,6 +1192,7 @@ export default {
statusEnableSuccess: "FAQ 条目已启用",
statusDisableSuccess: "FAQ 条目已禁用",
statusUpdateFailed: "更新状态失败",
+ batchOperations: "批量操作",
batchUpdateTag: "批量分类",
batchUpdateTagTip: "将为 {count} 个选中的条目设置分类",
batchEnable: "批量启用",
@@ -1201,7 +1204,7 @@ export default {
appendMode: "追加导入",
replaceMode: "替换现有条目",
fileLabel: "选择文件",
- fileTip: "支持 JSON / CSV / Excel,字段包含 standard_question、answers、similar_questions、negative_questions",
+ fileTip: "支持 JSON / CSV / Excel。CSV/Excel 表头:分类(必填)、问题(必填)、相似问题(选填-多个用##分隔)、反例问题(选填-多个用##分隔)、机器人回答(必填-多个用##分隔)、是否全部回复(选填-默认FALSE)、是否停用(选填-默认FALSE)、是否禁止被推荐(选填-默认False 可被推荐)。也支持旧格式:standard_question、answers、similar_questions、negative_questions",
clickToUpload: "点击上传文件",
dragDropTip: "或拖拽文件到此处",
importButton: "导入 FAQ",
diff --git a/frontend/src/types/tool-results.ts b/frontend/src/types/tool-results.ts
index 5808faaf5..3659e6942 100644
--- a/frontend/src/types/tool-results.ts
+++ b/frontend/src/types/tool-results.ts
@@ -207,7 +207,7 @@ export interface GrepChunkItem {
// Grep results data
export interface GrepResultsData {
display_type: 'grep_results';
- pattern: string;
+ patterns: string[];
case_sensitive: boolean;
use_regex: boolean;
results: GrepChunkItem[];
diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue
index a476d16b4..7d6121534 100644
--- a/frontend/src/views/chat/components/AgentStreamDisplay.vue
+++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue
@@ -76,7 +76,7 @@
'action-error': event.success === false
}"
>
- <div class="action-header" @click="toggleEvent(event.tool_call_id)">
+ <div class="action-header" @click="handleActionHeaderClick(event)" :class="{ 'no-results': !hasResults(event) }">
<div class="action-title">
<img v-if="event.tool_name && !isBookIcon(event.tool_name)" class="action-title-icon" :src="getToolIcon(event.tool_name)" alt="" />
<t-icon v-if="event.tool_name && isBookIcon(event.tool_name)" class="action-title-icon" name="book" />
@@ -91,7 +91,7 @@
<span class="action-name">{{ getToolTitle(event) }}</span>
</t-tooltip>
</div>
- <div v-if="!event.pending" class="action-show-icon">
+ <div v-if="!event.pending && hasResults(event)" class="action-show-icon">
<t-icon :name="isEventExpanded(event.tool_call_id) ? 'chevron-up' : 'chevron-down'" />
</div>
</div>
@@ -109,7 +109,7 @@
<!-- Search Results Summary (Fixed, always visible, outside action-details) -->
<div v-if="!event.pending && (event.tool_name === 'search_knowledge' || event.tool_name === 'knowledge_search') && event.tool_data" class="search-results-summary-fixed">
- <div class="results-summary-text" v-html="getSearchResultsSummary(event.tool_data)"></div>
+ <div class="results-summary-text" v-html="getSearchResultsSummary(event)"></div>
</div>
<!-- Web Search Results Summary (Fixed, always visible, outside action-details) -->
@@ -122,7 +122,7 @@
<div class="results-summary-text" v-html="getGrepResultsSummary(event.tool_data)"></div>
</div>
- <div v-if="isEventExpanded(event.tool_call_id) && !event.pending" class="action-details">
+ <div v-if="isEventExpanded(event.tool_call_id) && !event.pending && hasResults(event)" class="action-details">
<!-- Thinking tool: only render markdown thought content -->
<template v-if="event.tool_name === 'thinking' && event.tool_data?.thought">
<div class="thinking-thought-content">
@@ -676,10 +676,45 @@ const toggleEvent = (eventId: string) => {
}
};
+const handleActionHeaderClick = (event: any) => {
+ if (hasResults(event) && event.tool_call_id) {
+ toggleEvent(event.tool_call_id);
+ }
+};
+
const isEventExpanded = (eventId: string): boolean => {
return expandedEvents.value.has(eventId);
};
+// Check if search/grep tools have results
+const hasResults = (event: any): boolean => {
+ if (!event || !event.tool_data) return true; // Default to true for other tools
+
+ const toolName = event.tool_name;
+
+ // For knowledge search tools
+ if (toolName === 'search_knowledge' || toolName === 'knowledge_search') {
+ const count = event.tool_data.results?.length || event.tool_data.count || 0;
+ return count > 0;
+ }
+
+ // For web search tools
+ if (toolName === 'web_search') {
+ const count = event.tool_data.results?.length || event.tool_data.count || 0;
+ return count > 0;
+ }
+
+ // For grep tools
+ if (toolName === 'grep_chunks') {
+ const totalMatches = event.tool_data.total_matches || 0;
+ const resultCount = event.tool_data.result_count || 0;
+ return totalMatches > 0 || resultCount > 0;
+ }
+
+ // For other tools, always allow expansion
+ return true;
+};
+
// Delegated handlers for span-based citation clicks/keyboard
const handleCitationActivate = (el: HTMLElement) => {
const url = el.getAttribute('data-url');
@@ -1215,17 +1250,22 @@ const getToolIcon = (toolName: string): string => {
};
// Get search results summary text (returns HTML with colored numbers)
-const getSearchResultsSummary = (toolData: any): string => {
- if (!toolData) return '';
+const getSearchResultsSummary = (event: any): string => {
+ if (!event || !event.tool_data) return '';
+ const toolData = event.tool_data;
const count = toolData.results?.length || toolData.count || 0;
- if (count === 0) return '';
+ if (count === 0) return `未找到匹配的内容`;
+ // Build summary text
+ let summary = '';
const kbCount = toolData.kb_counts ? Object.keys(toolData.kb_counts).length : 0;
if (kbCount > 0) {
- return `找到 <strong>${count}</strong> 个结果,来自 <strong>${kbCount}</strong> 个知识库`;
+ summary = `找到 <strong>${count}</strong> 个结果,来自 <strong>${kbCount}</strong> 个文件`;
+ } else {
+ summary = `找到 <strong>${count}</strong> 个结果`;
}
- return `找到 <strong>${count}</strong> 个结果`;
+ return summary;
};
// Get web search results summary text
@@ -1248,12 +1288,11 @@ const getResultsCount = (toolData: any): number => {
const getGrepResultsSummary = (toolData: any): string => {
if (!toolData) return '';
- const pattern = toolData.pattern || '';
const totalMatches = toolData.total_matches || 0;
const resultCount = toolData.result_count || 0;
if (totalMatches === 0) {
- return `未找到匹配 <code>${pattern}</code> 的内容`;
+ return '未找到匹配的内容';
}
let summary = `找到 <strong>${totalMatches}</strong> 处匹配`;
@@ -1288,28 +1327,15 @@ const getQueryText = (args: any): string => {
}
// Add vector_queries if exists
- if (Array.isArray(parsedArgs.vector_queries) && parsedArgs.vector_queries.length > 0) {
- const vectorQueries = parsedArgs.vector_queries
- .filter((q: any) => q && typeof q === 'string')
- .join(' ');
- if (vectorQueries) {
- queries.push(vectorQueries);
- }
- }
-
- // Add keyword_queries if exists
- if (Array.isArray(parsedArgs.keyword_queries) && parsedArgs.keyword_queries.length > 0) {
- const keywordQueries = parsedArgs.keyword_queries
+ if (Array.isArray(parsedArgs.queries) && parsedArgs.queries.length > 0) {
+ queries.push(...parsedArgs.queries
.filter((q: any) => q && typeof q === 'string')
- .join(' ');
- if (keywordQueries) {
- queries.push(keywordQueries);
- }
+ );
}
- // Join all queries with space and remove duplicates
+ // Join all queries with comma and remove duplicates
const uniqueQueries = Array.from(new Set(queries));
- return uniqueQueries.join(' ');
+ return uniqueQueries.join(',');
};
// Get tool title - prefer summary over description, add query for search tools
@@ -1322,6 +1348,7 @@ const getToolTitle = (event: any): string => {
const toolName = event.tool_name;
const isSearchTool = toolName === 'search_knowledge' || toolName === 'knowledge_search';
const isWebSearchTool = toolName === 'web_search';
+ const isGrepTool = toolName === 'grep_chunks';
// For search tools, use description with query text
if (isSearchTool) {
@@ -1341,9 +1368,21 @@ const getToolTitle = (event: any): string => {
// Try to get query from arguments or tool_data
let queryText = '';
if (event.arguments && typeof event.arguments === 'object' && event.arguments.query) {
- queryText = event.arguments.query;
+ const query = event.arguments.query;
+ // Handle both string and array formats
+ if (Array.isArray(query)) {
+ queryText = query.filter((q: any) => q && typeof q === 'string').join(',');
+ } else if (typeof query === 'string') {
+ queryText = query;
+ }
} else if (event.tool_data && event.tool_data.query) {
- queryText = event.tool_data.query;
+ const query = event.tool_data.query;
+ // Handle both string and array formats
+ if (Array.isArray(query)) {
+ queryText = query.filter((q: any) => q && typeof q === 'string').join(',');
+ } else if (typeof query === 'string') {
+ queryText = query;
+ }
}
if (queryText) {
return `${baseTitle}:「${queryText}」`;
@@ -1351,6 +1390,34 @@ const getToolTitle = (event: any): string => {
return baseTitle;
}
+ // For grep tools, use description with patterns
+ if (isGrepTool) {
+ const baseTitle = getToolDescription(event);
+ // Try to get patterns from arguments or tool_data
+ let patterns: string[] = [];
+ if (event.arguments && typeof event.arguments === 'object') {
+ if (Array.isArray(event.arguments.patterns)) {
+ patterns = event.arguments.patterns;
+ } else if (event.arguments.pattern) {
+ patterns = [event.arguments.pattern];
+ }
+ } else if (event.tool_data) {
+ if (Array.isArray(event.tool_data.patterns)) {
+ patterns = event.tool_data.patterns;
+ } else if (event.tool_data.pattern) {
+ patterns = [event.tool_data.pattern];
+ }
+ }
+ if (patterns.length > 0) {
+ // Show up to 2 patterns in title
+ const displayPatterns = patterns.slice(0, 2);
+ const patternText = displayPatterns.join('、');
+ const moreText = patterns.length > 2 ? ` +${patterns.length - 2}` : '';
+ return `${baseTitle}:「${patternText}${moreText}」`;
+ }
+ return baseTitle;
+ }
+
// Use tool summary if available
const summary = getToolSummary(event);
return summary || getToolDescription(event);
@@ -1514,80 +1581,93 @@ const handleAddToKnowledge = (answerEvent: any) => {
// 时间轴连线容器
.event-item {
position: relative;
- padding-left: 28px;
+ padding-left: 32px;
margin-bottom: 12px;
// 时间轴垂直线
&::before {
content: '';
position: absolute;
- left: 8px;
+ left: 10px;
top: 0;
bottom: -12px;
- width: 2px;
- background: linear-gradient(to bottom, #e5e7eb 0%, #e5e7eb 100%);
+ width: 1.5px;
+ background: linear-gradient(
+ to bottom,
+ rgba(7, 192, 95, 0.1) 0%,
+ rgba(7, 192, 95, 0.15) 50%,
+ rgba(7, 192, 95, 0.1) 100%
+ );
z-index: 0;
}
// 第一个事件的连线从节点开始
&:first-child::before {
- top: 12px;
+ top: 14px;
}
// 最后一个事件不显示底部连线
&.event-last::before {
bottom: auto;
- height: 20px;
+ height: 22px;
}
// 时间轴节点(圆点)
&::after {
content: '';
position: absolute;
- left: 4px;
- top: 12px;
- width: 10px;
- height: 10px;
+ left: 6.25px; // 线条中心 10.75px - 圆点半径 4.5px = 6.25px (box-sizing: border-box)
+ top: 14px;
+ width: 9px;
+ height: 9px;
border-radius: 50%;
background: #ffffff;
- border: 2px solid #e5e7eb;
+ border: 2px solid rgba(7, 192, 95, 0.3);
z-index: 1;
- transition: all 0.2s ease;
+ transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
+ box-sizing: border-box; // 确保 border 包含在尺寸内
}
// 不同事件类型的节点颜色
&:has(.thinking-event)::after {
- border-color: #9ca3af;
+ border-color: rgba(156, 163, 175, 0.4);
background: #f9fafb;
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
&:has(.answer-event)::after {
border-color: #07c05f;
background: #07c05f;
- box-shadow: 0 0 0 3px rgba(7, 192, 95, 0.15);
+ box-shadow: 0 0 0 2px rgba(7, 192, 95, 0.12), 0 2px 4px rgba(7, 192, 95, 0.2);
+ transform: scale(1.1);
}
&:has(.tool-event)::after {
border-color: #07c05f;
background: #ffffff;
+ box-shadow: 0 1px 3px rgba(7, 192, 95, 0.15);
}
&:has(.tool-event .action-pending)::after {
border-color: #07c05f;
- background: rgba(7, 192, 95, 0.2);
+ background: rgba(7, 192, 95, 0.15);
+ box-shadow: 0 0 0 2px rgba(7, 192, 95, 0.1);
animation: pulseNode 2s ease-in-out infinite;
}
&:has(.tool-event .action-error)::after {
border-color: #e34d59;
background: #e34d59;
+ box-shadow: 0 0 0 2px rgba(227, 77, 89, 0.15), 0 2px 4px rgba(227, 77, 89, 0.2);
}
&:has(.plan-task-change-event)::after {
border-color: #07c05f;
background: #07c05f;
- transform: rotate(45deg);
+ transform: rotate(45deg) scale(0.9);
border-radius: 2px;
+ box-shadow: 0 1px 3px rgba(7, 192, 95, 0.2);
}
}
@@ -1606,21 +1686,6 @@ const handleAddToKnowledge = (answerEvent: any) => {
margin-bottom: 16px;
position: relative;
- // 添加时间轴起点标记
- &::before {
- content: '';
- position: absolute;
- left: -20px;
- top: 12px;
- width: 10px;
- height: 10px;
- border-radius: 50%;
- background: #07c05f;
- border: 2px solid #ffffff;
- box-shadow: 0 0 0 2px #07c05f, 0 0 0 4px rgba(7, 192, 95, 0.1);
- z-index: 2;
- }
-
.intermediate-steps-header {
display: flex;
justify-content: space-between;
@@ -1630,6 +1695,10 @@ const handleAddToKnowledge = (answerEvent: any) => {
font-weight: 500;
cursor: pointer;
background: linear-gradient(to right, rgba(7, 192, 95, 0.03), transparent);
+
+ &:hover {
+ background: linear-gradient(to right, rgba(7, 192, 95, 0.05), rgba(7, 192, 95, 0.01));
+ }
}
.intermediate-steps-title {
@@ -1658,10 +1727,6 @@ const handleAddToKnowledge = (answerEvent: any) => {
padding: 0 2px 1px 2px;
color: #07c05f;
}
-
- .intermediate-steps-header:hover {
- background: linear-gradient(to right, rgba(7, 192, 95, 0.05), rgba(7, 192, 95, 0.01));
- }
}
// Thinking Event
@@ -1700,7 +1765,7 @@ const handleAddToKnowledge = (answerEvent: any) => {
&.markdown-content {
:deep(p) {
- margin: 6px 0;
+ margin: 0 0;
line-height: 1.6;
}
@@ -2079,6 +2144,14 @@ const handleAddToKnowledge = (answerEvent: any) => {
&:hover {
background-color: rgba(7, 192, 95, 0.03);
}
+
+ &.no-results {
+ cursor: default;
+
+ &:hover {
+ background-color: transparent;
+ }
+ }
}
.action-title {
@@ -2151,11 +2224,15 @@ const handleAddToKnowledge = (answerEvent: any) => {
@keyframes pulseNode {
0%, 100% {
border-color: #07c05f;
- box-shadow: 0 0 0 0 rgba(7, 192, 95, 0.4);
+ background: rgba(7, 192, 95, 0.15);
+ box-shadow: 0 0 0 2px rgba(7, 192, 95, 0.1);
+ transform: scale(1);
}
50% {
border-color: #0ae06f;
- box-shadow: 0 0 0 4px rgba(7, 192, 95, 0.1);
+ background: rgba(7, 192, 95, 0.25);
+ box-shadow: 0 0 0 3px rgba(7, 192, 95, 0.15);
+ transform: scale(1.05);
}
}
diff --git a/frontend/src/views/chat/components/tool-results/GrepResults.vue b/frontend/src/views/chat/components/tool-results/GrepResults.vue
new file mode 100644
index 000000000..69b74f2e1
--- /dev/null
+++ b/frontend/src/views/chat/components/tool-results/GrepResults.vue
@@ -0,0 +1,185 @@
+<template>
+ <div class="grep-results">
+ <!-- Results List -->
+ <div v-if="results && results.length > 0" class="results-list">
+ <div
+ v-for="(result, index) in results"
+ :key="result.id"
+ class="result-item"
+ >
+ <t-popup
+ :overlayClassName="`grep-popup-${result.id}`"
+ placement="bottom-left"
+ width="400"
+ :showArrow="false"
+ trigger="click"
+ destroy-on-close
+ >
+ <template #content>
+ <ContentPopup
+ :content="result.content"
+ :chunk-id="result.id"
+ :knowledge-id="result.knowledge_id"
+ />
+ </template>
+ <div class="result-header">
+ <div class="result-title">
+ <span class="result-index">#{{ index + 1 }}</span>
+ <span class="knowledge-title">{{ result.knowledge_title || 'Untitled' }}</span>
+ </div>
+ </div>
+ </t-popup>
+ </div>
+ </div>
+
+ <!-- Empty State -->
+ <div v-else class="empty-state">
+ 未找到匹配的内容
+ </div>
+ </div>
+</template>
+
+<script setup lang="ts">
+import { computed } from 'vue';
+import type { GrepResultsData } from '@/types/tool-results';
+import ContentPopup from './ContentPopup.vue';
+
+const props = defineProps<{
+ data: GrepResultsData;
+}>();
+
+const maxDisplayPatterns = 2;
+
+const displayPatterns = computed(() => {
+ if (!props.data.patterns || props.data.patterns.length === 0) {
+ return [];
+ }
+ return props.data.patterns.slice(0, maxDisplayPatterns);
+});
+
+const results = computed(() => props.data.results || []);
+</script>
+
+<style lang="less" scoped>
+@import './tool-results.less';
+
+.grep-results {
+ display: flex;
+ flex-direction: column;
+ gap: 3px;
+ padding: 0 0 0 12px;
+}
+
+.results-list {
+ display: flex;
+ flex-direction: column;
+ gap: 3px;
+}
+
+.result-item {
+ background: transparent;
+ border: none;
+ border-radius: 0;
+ overflow: visible;
+}
+
+.result-header {
+ padding: 2px 0;
+ cursor: pointer;
+ user-select: none;
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ transition: color 0.15s ease;
+
+ &:hover {
+ color: #07c05f;
+ }
+}
+
+.result-title {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ flex: 1;
+ min-width: 0;
+ font-size: 12px;
+ line-height: 1.4;
+}
+
+.result-index {
+ font-size: 11px;
+ color: #9ca3af;
+ font-weight: 600;
+ flex-shrink: 0;
+}
+
+.pattern-badge {
+ display: inline-flex;
+ align-items: center;
+ gap: 3px;
+ flex-shrink: 0;
+
+ .pattern-text {
+ font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
+ font-size: 10px;
+ background: #f3f4f6;
+ color: #6b7280;
+ padding: 2px 5px;
+ border-radius: 3px;
+ white-space: nowrap;
+ font-weight: 500;
+ }
+
+ .more-patterns {
+ font-size: 10px;
+ color: #9ca3af;
+ padding: 2px 4px;
+ background: #f3f4f6;
+ border-radius: 3px;
+ }
+}
+
+.knowledge-title {
+ font-size: 12px;
+ color: #374151;
+ flex: 1;
+ font-weight: 500;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ min-width: 0;
+}
+
+.chunk-info {
+ font-size: 10px;
+ color: #9ca3af;
+ background: #f3f4f6;
+ padding: 2px 5px;
+ border-radius: 3px;
+ flex-shrink: 0;
+}
+
+.empty-state {
+ padding: 20px;
+ text-align: center;
+ color: #9ca3af;
+ font-size: 12px;
+ font-style: italic;
+}
+
+// Popup overlay styles
+:deep([class*="grep-popup-"]) {
+ .t-popup__content {
+ max-height: 400px;
+ max-width: 500px;
+ overflow-y: auto;
+ overflow-x: hidden;
+ padding: 0;
+ border-radius: 6px;
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
+ word-wrap: break-word;
+ word-break: break-word;
+ }
+}
+</style>
diff --git a/frontend/src/views/chat/components/tool-results/SearchResults.vue b/frontend/src/views/chat/components/tool-results/SearchResults.vue
index 7ebeacae5..608aa6ab0 100644
--- a/frontend/src/views/chat/components/tool-results/SearchResults.vue
+++ b/frontend/src/views/chat/components/tool-results/SearchResults.vue
@@ -25,9 +25,6 @@
<div class="result-header">
<div class="result-title">
<span class="result-index">#{{ result.result_index }}</span>
- <span class="relevance-badge" :class="getRelevanceClass(result.relevance_level)">
- {{ getRelevanceLabel(result.relevance_level) }}
- </span>
<span class="knowledge-title">{{ result.knowledge_title }}</span>
</div>
</div>
diff --git a/frontend/src/views/creatChat/creatChat.vue b/frontend/src/views/creatChat/creatChat.vue
index ea028d26c..3f60a0f0c 100644
--- a/frontend/src/views/creatChat/creatChat.vue
+++ b/frontend/src/views/creatChat/creatChat.vue
@@ -7,6 +7,16 @@
<InputField @send-msg="sendMsg"></InputField>
</div>
</div>
+
+ <!-- 知识库编辑器(创建/编辑统一组件) -->
+ <KnowledgeBaseEditorModal
+ :visible="uiStore.showKBEditorModal"
+ :mode="uiStore.kbEditorMode"
+ :kb-id="uiStore.currentKBId || undefined"
+ :initial-type="uiStore.kbEditorType"
+ @update:visible="(val) => val ? null : uiStore.closeKBEditor()"
+ @success="handleKBEditorSuccess"
+ />
</template>
<script setup lang="ts">
import { ref } from 'vue';
@@ -14,14 +24,17 @@ import InputField from '@/components/Input-field.vue';
import { createSessions } from "@/api/chat/index";
import { useMenuStore } from '@/stores/menu';
import { useSettingsStore } from '@/stores/settings';
+import { useUIStore } from '@/stores/ui';
import { useRoute, useRouter } from 'vue-router';
import { MessagePlugin } from 'tdesign-vue-next';
import { useI18n } from 'vue-i18n';
+import KnowledgeBaseEditorModal from '@/views/knowledge/KnowledgeBaseEditorModal.vue';
const router = useRouter();
const route = useRoute();
const usemenuStore = useMenuStore();
const settingsStore = useSettingsStore();
+const uiStore = useUIStore();
const { t } = useI18n();
const sendMsg = (value: string) => {
@@ -79,6 +92,15 @@ const navigateToSession = async (sessionId: string, value: string) => {
router.push(`/platform/chat/${sessionId}`);
}
+const handleKBEditorSuccess = (kbId: string) => {
+ console.log('[creatChat] knowledge base created successfully:', kbId)
+ // 创建成功后跳转到知识库列表页面,并高亮新创建的知识库
+ router.push({
+ path: '/platform/knowledge-bases',
+ query: { highlightKbId: kbId }
+ })
+}
+
</script>
<style lang="less" scoped>
.dialogue-wrap {
diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue
index 0cdc11e14..853bf9259 100644
--- a/frontend/src/views/knowledge/KnowledgeBase.vue
+++ b/frontend/src/views/knowledge/KnowledgeBase.vue
@@ -45,6 +45,15 @@ let knowledgeIndex = ref(-1)
let knowledgeScroll = ref()
let page = 1;
let pageSize = 35;
+
+// 文档处理进度条状态
+const documentProcessingState = reactive({
+ processingIds: [] as string[],
+ total: 0,
+ completed: 0,
+ failed: 0,
+ pollingInterval: null as ReturnType<typeof setInterval> | null,
+})
const selectedTagId = ref<string>("");
const tagList = ref<any[]>([]);
const tagLoading = ref(false);
@@ -396,6 +405,15 @@ const handleFileUploaded = (event: CustomEvent) => {
// 如果上传的文件属于当前知识库,使用 loadKnowledgeFiles 刷新文件列表
loadKnowledgeFiles(uploadedKbId);
loadTags(uploadedKbId);
+ // 延迟一下,等待文件列表加载完成后再检查处理状态
+ setTimeout(() => {
+ const processingList = cardList.value.filter(item =>
+ item.parse_status === 'pending' || item.parse_status === 'processing'
+ )
+ if (processingList.length > 0) {
+ updateDocumentProcessingState(processingList)
+ }
+ }, 500)
}
};
@@ -416,11 +434,17 @@ onMounted(() => {
window.addEventListener('knowledgeFileUploaded', handleFileUploaded as EventListener);
// 监听URL导入对话框打开事件
window.addEventListener('openURLImportDialog', handleOpenURLImportDialog as EventListener);
+
+ // 恢复文档处理状态
+ if (!isFAQ.value) {
+ restoreDocumentProcessingState()
+ }
});
onUnmounted(() => {
window.removeEventListener('knowledgeFileUploaded', handleFileUploaded as EventListener);
window.removeEventListener('openURLImportDialog', handleOpenURLImportDialog as EventListener);
+ stopDocumentProcessingPolling()
});
watch(() => cardList.value, (newValue) => {
if (isFAQ.value) return;
@@ -435,6 +459,9 @@ watch(() => cardList.value, (newValue) => {
if (analyzeList.length) {
updateStatus(analyzeList)
}
+
+ // 更新文档处理进度条状态
+ updateDocumentProcessingState(analyzeList)
}, { deep: true })
type KnowledgeCard = {
id: string;
@@ -477,6 +504,168 @@ const updateStatus = (analyzeList: KnowledgeCard[]) => {
}, 1500);
};
+// 更新文档处理进度条状态
+const updateDocumentProcessingState = (processingList: KnowledgeCard[]) => {
+ const processingIds = processingList.map(item => item.id)
+ const hasChanged = JSON.stringify(processingIds.sort()) !== JSON.stringify(documentProcessingState.processingIds.sort())
+
+ if (hasChanged) {
+ documentProcessingState.processingIds = processingIds
+ documentProcessingState.total = processingIds.length
+ documentProcessingState.completed = 0
+ documentProcessingState.failed = 0
+
+ // 保存到 localStorage
+ saveProcessingIdsToStorage(processingIds)
+
+ // 开始轮询
+ if (processingIds.length > 0) {
+ startDocumentProcessingPolling()
+ } else {
+ stopDocumentProcessingPolling()
+ }
+ }
+}
+
+// 开始轮询文档处理状态
+const startDocumentProcessingPolling = () => {
+ stopDocumentProcessingPolling()
+
+ if (documentProcessingState.processingIds.length === 0) return
+
+ documentProcessingState.pollingInterval = setInterval(() => {
+ if (documentProcessingState.processingIds.length === 0) {
+ stopDocumentProcessingPolling()
+ return
+ }
+
+ let query = ``
+ documentProcessingState.processingIds.forEach(id => {
+ query += `ids=${id}&`
+ })
+
+ batchQueryKnowledge(query).then((result: any) => {
+ if (result.success && result.data) {
+ const completedIds: string[] = []
+ const failedIds: string[] = []
+
+ ;(result.data as KnowledgeCard[]).forEach((item: KnowledgeCard) => {
+ if (item.parse_status === 'completed') {
+ completedIds.push(item.id)
+ documentProcessingState.completed++
+ } else if (item.parse_status === 'failed') {
+ failedIds.push(item.id)
+ documentProcessingState.failed++
+ }
+ })
+
+ // 从处理列表中移除已完成的文档
+ documentProcessingState.processingIds = documentProcessingState.processingIds.filter(
+ id => !completedIds.includes(id) && !failedIds.includes(id)
+ )
+
+ // 如果所有文档都处理完成,停止轮询
+ if (documentProcessingState.processingIds.length === 0) {
+ stopDocumentProcessingPolling()
+ clearProcessingIdsFromStorage()
+
+ // 刷新文件列表
+ if (kbId.value) {
+ loadKnowledgeFiles(kbId.value)
+ }
+ } else {
+ // 更新 localStorage
+ saveProcessingIdsToStorage(documentProcessingState.processingIds)
+ }
+ }
+ }).catch((_err) => {
+ // 错误处理
+ })
+ }, 2000)
+}
+
+// 停止轮询文档处理状态
+const stopDocumentProcessingPolling = () => {
+ if (documentProcessingState.pollingInterval) {
+ clearInterval(documentProcessingState.pollingInterval)
+ documentProcessingState.pollingInterval = null
+ }
+}
+
+// localStorage 相关函数
+const getProcessingIdsStorageKey = () => {
+ return `document_processing_ids_${kbId.value}`
+}
+
+const saveProcessingIdsToStorage = (ids: string[]) => {
+ if (!kbId.value) return
+ try {
+ localStorage.setItem(getProcessingIdsStorageKey(), JSON.stringify(ids))
+ } catch (error) {
+ console.error('Failed to save processing IDs to localStorage:', error)
+ }
+}
+
+const getProcessingIdsFromStorage = (): string[] => {
+ if (!kbId.value) return []
+ try {
+ const data = localStorage.getItem(getProcessingIdsStorageKey())
+ return data ? JSON.parse(data) : []
+ } catch (error) {
+ console.error('Failed to get processing IDs from localStorage:', error)
+ return []
+ }
+}
+
+const clearProcessingIdsFromStorage = () => {
+ if (!kbId.value) return
+ try {
+ localStorage.removeItem(getProcessingIdsStorageKey())
+ } catch (error) {
+ console.error('Failed to clear processing IDs from localStorage:', error)
+ }
+}
+
+// 恢复文档处理状态(用于刷新后恢复)
+const restoreDocumentProcessingState = async () => {
+ if (!kbId.value || isFAQ.value) return
+
+ const savedIds = getProcessingIdsFromStorage()
+ if (savedIds.length === 0) return
+
+ // 检查这些文档是否还在处理中
+ let query = ``
+ savedIds.forEach(id => {
+ query += `ids=${id}&`
+ })
+
+ try {
+ const result: any = await batchQueryKnowledge(query)
+ if (result.success && result.data) {
+ const stillProcessing: string[] = []
+ ;(result.data as KnowledgeCard[]).forEach((item: KnowledgeCard) => {
+ if (item.parse_status === 'pending' || item.parse_status === 'processing') {
+ stillProcessing.push(item.id)
+ }
+ })
+
+ if (stillProcessing.length > 0) {
+ documentProcessingState.processingIds = stillProcessing
+ documentProcessingState.total = stillProcessing.length
+ documentProcessingState.completed = 0
+ documentProcessingState.failed = 0
+ saveProcessingIdsToStorage(stillProcessing)
+ startDocumentProcessingPolling()
+ } else {
+ clearProcessingIdsFromStorage()
+ }
+ }
+ } catch (error) {
+ console.error('Failed to restore document processing state:', error)
+ clearProcessingIdsFromStorage()
+ }
+}
+
const closeDoc = () => {
isCardDetails.value = false;
};
@@ -520,6 +709,16 @@ const ensureDocumentKbReady = () => {
return true;
};
+// 关闭文档处理进度条
+const handleCloseDocumentProgress = () => {
+ stopDocumentProcessingPolling()
+ documentProcessingState.processingIds = []
+ documentProcessingState.total = 0
+ documentProcessingState.completed = 0
+ documentProcessingState.failed = 0
+ clearProcessingIdsFromStorage()
+}
+
const handleDocumentUploadClick = () => {
if (!ensureDocumentKbReady()) return;
uploadInputRef.value?.click();
@@ -818,6 +1017,41 @@ async function createNewSession(value: string): Promise<void> {
<p class="document-subtitle">{{ $t('knowledgeEditor.document.subtitle') }}</p>
</div>
</div>
+
+ <!-- 文档处理进度条 -->
+ <div v-if="!isFAQ && documentProcessingState.processingIds.length > 0" class="document-processing-progress-bar">
+ <div class="progress-bar-content">
+ <div class="progress-bar-header">
+ <t-icon
+ name="loading"
+ size="16px"
+ class="progress-icon icon-loading"
+ />
+ <span class="progress-title">
+ {{ $t('knowledgeList.processingDocuments', { count: documentProcessingState.processingIds.length }) }}
+ </span>
+ <span class="progress-count">
+ {{ documentProcessingState.completed + documentProcessingState.failed }}/{{ documentProcessingState.total }}
+ </span>
+ <t-button
+ variant="text"
+ theme="default"
+ size="small"
+ class="progress-close-btn"
+ @click="handleCloseDocumentProgress"
+ >
+ <t-icon name="close" size="14px" />
+ </t-button>
+ </div>
+ <t-progress
+ :percentage="Math.round(((documentProcessingState.completed + documentProcessingState.failed) / documentProcessingState.total) * 100)"
+ :status="documentProcessingState.failed > 0 ? 'error' : 'active'"
+ :label="false"
+ class="progress-bar"
+ />
+ </div>
+ </div>
+
<input
ref="uploadInputRef"
type="file"
@@ -1872,6 +2106,76 @@ async function createNewSession(value: string): Promise<void> {
min-height: 100%;
}
+.document-processing-progress-bar {
+ margin-bottom: 16px;
+ background: #fff;
+ border: 1px solid #e7ebf0;
+ border-radius: 8px;
+ padding: 12px 16px;
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
+
+ .progress-bar-content {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ }
+
+ .progress-bar-header {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-size: 14px;
+ color: #000000e6;
+
+ .progress-icon {
+ flex-shrink: 0;
+
+ &.icon-loading {
+ animation: rotate 1s linear infinite;
+ color: #07c05f;
+ }
+ }
+
+ .progress-title {
+ font-weight: 500;
+ flex: 1;
+ }
+
+ .progress-count {
+ color: #86909c;
+ font-size: 13px;
+ }
+
+ .progress-close-btn {
+ flex-shrink: 0;
+ padding: 4px;
+ margin-left: 8px;
+ }
+ }
+
+ .progress-bar {
+ margin: 0;
+ width: 100%;
+
+ :deep(.t-progress) {
+ width: 100%;
+ }
+
+ :deep(.t-progress__bar) {
+ width: 100%;
+ }
+ }
+}
+
+@keyframes rotate {
+ from {
+ transform: rotate(0deg);
+ }
+ to {
+ transform: rotate(360deg);
+ }
+}
+
:deep(.del-knowledge) {
padding: 0px !important;
border-radius: 6px !important;
diff --git a/frontend/src/views/knowledge/KnowledgeBaseList.vue b/frontend/src/views/knowledge/KnowledgeBaseList.vue
index ef3eb0419..4545bde70 100644
--- a/frontend/src/views/knowledge/KnowledgeBaseList.vue
+++ b/frontend/src/views/knowledge/KnowledgeBaseList.vue
@@ -24,8 +24,10 @@
:class="{
'uninitialized': !isInitialized(kb),
'kb-type-document': (kb.type || 'document') === 'document',
- 'kb-type-faq': kb.type === 'faq'
+ 'kb-type-faq': kb.type === 'faq',
+ 'highlight-flash': highlightedKbId !== null && highlightedKbId === kb.id
}"
+ :ref="el => { if (highlightedKbId !== null && highlightedKbId === kb.id && el) highlightedCardRef = el as HTMLElement }"
@click="handleCardClick(kb)"
>
<!-- 卡片头部 -->
@@ -78,6 +80,15 @@
{{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }}
({{ kb.type === 'faq' ? (kb.chunk_count || 0) : (kb.knowledge_count || 0) }})
</span>
+ <t-tooltip
+ v-if="kb.isProcessing"
+ :content="kb.type === 'document' && (kb.processing_count || 0) > 0
+ ? $t('knowledgeList.processingDocuments', { count: kb.processing_count || 0 })
+ : $t('knowledgeList.processing')"
+ placement="top"
+ >
+ <t-icon name="loading" size="14px" class="processing-icon" />
+ </t-tooltip>
</div>
<div class="feature-badges">
<t-tooltip v-if="kb.extract_config?.enabled" :content="$t('knowledgeList.features.knowledgeGraph')" placement="top">
@@ -144,9 +155,9 @@
</template>
<script setup lang="ts">
-import { onMounted, ref, computed, watch } from 'vue'
-import { useRouter } from 'vue-router'
-import { MessagePlugin } from 'tdesign-vue-next'
+import { onMounted, ref, computed, watch, nextTick } from 'vue'
+import { useRouter, useRoute } from 'vue-router'
+import { MessagePlugin, Icon as TIcon } from 'tdesign-vue-next'
import { listKnowledgeBases, deleteKnowledgeBase } from '@/api/knowledge-base'
import { formatStringDate } from '@/utils/index'
import { useUIStore } from '@/stores/ui'
@@ -155,6 +166,7 @@ import Settings from '@/views/settings/Settings.vue'
import { useI18n } from 'vue-i18n'
const router = useRouter()
+const route = useRoute()
const uiStore = useUIStore()
const { t } = useI18n()
@@ -172,6 +184,8 @@ interface KB {
cos_config?: { provider?: string; bucket_name?: string };
knowledge_count?: number;
chunk_count?: number;
+ isProcessing?: boolean; // 是否有正在处理的导入任务
+ processing_count?: number; // 正在处理的文档数量(仅文档类型)
}
const kbs = ref<KB[]>([])
@@ -179,22 +193,43 @@ const loading = ref(false)
const deleteVisible = ref(false)
const deletingKb = ref<KB | null>(null)
const currentMoreIndex = ref<number>(-1)
+const highlightedKbId = ref<string | null>(null)
+const highlightedCardRef = ref<HTMLElement | null>(null)
const fetchList = () => {
loading.value = true
- listKnowledgeBases().then((res: any) => {
+ return listKnowledgeBases().then((res: any) => {
const data = res.data || []
// 格式化时间,并初始化 showMore 状态
- kbs.value = data.map((kb: KB) => ({
+ // is_processing 字段由后端返回
+ kbs.value = data.map((kb: any) => ({
...kb,
updated_at: kb.updated_at ? formatStringDate(new Date(kb.updated_at)) : '',
- showMore: false
+ showMore: false,
+ isProcessing: kb.is_processing || false,
+ processing_count: kb.processing_count || 0
}))
}).finally(() => loading.value = false)
}
onMounted(() => {
- fetchList()
+ fetchList().then(() => {
+ // 检查路由参数中是否有需要高亮的知识库ID
+ const highlightKbId = route.query.highlightKbId as string
+ if (highlightKbId) {
+ triggerHighlightFlash(highlightKbId)
+ // 清除 URL 中的查询参数
+ router.replace({ query: {} })
+ }
+ })
+})
+
+// 监听路由变化,处理从其他页面跳转过来的高亮需求
+watch(() => route.query.highlightKbId, (newKbId) => {
+ if (newKbId && typeof newKbId === 'string' && kbs.value.length > 0) {
+ triggerHighlightFlash(newKbId)
+ router.replace({ query: {} })
+ }
})
const openMore = (index: number) => {
@@ -270,7 +305,32 @@ const goSettings = (id: string) => {
// 知识库编辑器成功回调(创建或编辑成功)
const handleKBEditorSuccess = (kbId: string) => {
console.log('[KnowledgeBaseList] knowledge operation success:', kbId)
- fetchList()
+ fetchList().then(() => {
+ // 如果是从路由参数中获取的高亮ID,触发闪烁效果
+ if (route.query.highlightKbId === kbId) {
+ triggerHighlightFlash(kbId)
+ // 清除 URL 中的查询参数
+ router.replace({ query: {} })
+ }
+ })
+}
+
+// 触发高亮闪烁效果
+const triggerHighlightFlash = (kbId: string) => {
+ highlightedKbId.value = kbId
+ nextTick(() => {
+ if (highlightedCardRef.value) {
+ // 滚动到高亮的卡片
+ highlightedCardRef.value.scrollIntoView({
+ behavior: 'smooth',
+ block: 'center'
+ })
+ }
+ // 3秒后清除高亮
+ setTimeout(() => {
+ highlightedKbId.value = null
+ }, 3000)
+ })
}
</script>
@@ -533,6 +593,19 @@ const handleKBEditorSuccess = (kbId: string) => {
color: #0052d9;
border: 1px solid rgba(0, 82, 217, 0.2);
}
+
+ .processing-icon {
+ animation: spin 1s linear infinite;
+ margin-left: 4px;
+ }
+
+ &.document .processing-icon {
+ color: #059669;
+ }
+
+ &.faq .processing-icon {
+ color: #0052d9;
+ }
}
.feature-badges {
@@ -569,6 +642,40 @@ const handleKBEditorSuccess = (kbId: string) => {
background: rgba(255, 152, 0, 0.15);
}
}
+
+}
+
+@keyframes spin {
+ from {
+ transform: rotate(0deg);
+ }
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+@keyframes highlightFlash {
+ 0% {
+ border-color: #07c05f;
+ box-shadow: 0 0 0 0 rgba(7, 192, 95, 0.4);
+ transform: scale(1);
+ }
+ 50% {
+ border-color: #07c05f;
+ box-shadow: 0 0 0 8px rgba(7, 192, 95, 0);
+ transform: scale(1.02);
+ }
+ 100% {
+ border-color: #07c05f;
+ box-shadow: 0 0 0 0 rgba(7, 192, 95, 0);
+ transform: scale(1);
+ }
+}
+
+.kb-card.highlight-flash {
+ animation: highlightFlash 0.6s ease-in-out 3;
+ border-color: #07c05f !important;
+ box-shadow: 0 0 12px rgba(7, 192, 95, 0.3) !important;
}
.card-time {
diff --git a/frontend/src/views/knowledge/components/FAQEntryManager.vue b/frontend/src/views/knowledge/components/FAQEntryManager.vue
index d6281d19b..f15acdab3 100644
--- a/frontend/src/views/knowledge/components/FAQEntryManager.vue
+++ b/frontend/src/views/knowledge/components/FAQEntryManager.vue
@@ -54,8 +54,56 @@
</div>
</div>
+ <!-- 导入进度条(显示在列表页面顶部) -->
+ <div v-if="importState.taskId && importState.taskStatus" class="faq-import-progress-bar">
+ <div class="progress-bar-content">
+ <div class="progress-bar-header">
+ <t-icon
+ :name="importState.taskStatus.status === 'running' ? 'loading' :
+ importState.taskStatus.status === 'success' ? 'check-circle' :
+ importState.taskStatus.status === 'failed' ? 'error-circle' : 'time'"
+ size="16px"
+ class="progress-icon"
+ :class="{
+ 'icon-loading': importState.taskStatus.status === 'running',
+ 'icon-success': importState.taskStatus.status === 'success',
+ 'icon-error': importState.taskStatus.status === 'failed'
+ }"
+ />
+ <span class="progress-title">
+ {{ importState.taskStatus.status === 'running' ? '导入中...' :
+ importState.taskStatus.status === 'success' ? '导入完成' :
+ importState.taskStatus.status === 'failed' ? '导入失败' : '等待中...' }}
+ </span>
+ <span class="progress-count">
+ {{ importState.taskStatus.processed }}/{{ importState.taskStatus.total }}
+ </span>
+ <t-button
+ v-if="importState.taskStatus.status === 'success' || importState.taskStatus.status === 'failed'"
+ variant="text"
+ theme="default"
+ size="small"
+ class="progress-close-btn"
+ @click="handleCloseProgress"
+ >
+ <t-icon name="close" size="14px" />
+ </t-button>
+ </div>
+ <t-progress
+ :percentage="importState.taskStatus.progress"
+ :status="importState.taskStatus.status === 'failed' ? 'error' :
+ importState.taskStatus.status === 'success' ? 'success' : 'active'"
+ :label="false"
+ class="progress-bar"
+ />
+ <p v-if="importState.taskStatus.error" class="progress-error">
+ {{ importState.taskStatus.error }}
+ </p>
+ </div>
+ </div>
+
<div class="faq-main">
- <aside class="faq-tag-panel">
+ <aside class="faq-tag-panel">
<div class="sidebar-header">
<div class="sidebar-title">
<span>{{ $t('knowledgeBase.faqCategoryTitle') }}</span>
@@ -756,14 +804,27 @@
{{ $t('knowledgeEditor.faqImport.previewMore', { count: importState.preview.length - 5 }) }}
</p>
</div>
+
</div>
<div class="faq-import-footer">
- <t-button theme="default" variant="outline" @click="importVisible = false">
+ <t-button
+ theme="default"
+ variant="outline"
+ @click="handleCancelImport"
+ :disabled="importState.importing && importState.taskStatus?.status === 'running'"
+ >
{{ $t('common.cancel') }}
</t-button>
- <t-button theme="primary" @click="handleImport" :loading="importState.importing">
- {{ $t('knowledgeEditor.faqImport.importButton') }}
+ <t-button
+ theme="primary"
+ @click="handleImport"
+ :loading="importState.importing && !importState.taskId"
+ :disabled="importState.taskStatus?.status === 'running'"
+ >
+ {{ importState.taskStatus?.status === 'success' ? $t('common.close') :
+ importState.taskStatus?.status === 'failed' ? '重试' :
+ $t('knowledgeEditor.faqImport.importButton') }}
</t-button>
</div>
</div>
@@ -773,37 +834,67 @@
</Teleport>
<!-- Batch Tag Dialog -->
- <t-dialog
- v-model:visible="batchTagDialogVisible"
- :header="$t('knowledgeEditor.faq.batchUpdateTag')"
- width="480px"
- :confirm-btn="{ content: $t('common.confirm'), theme: 'primary' }"
- :cancel-btn="{ content: $t('common.cancel') }"
- @confirm="handleBatchTag"
- >
- <div class="batch-tag-dialog-content">
- <p class="batch-tag-dialog-tip">
- {{ $t('knowledgeEditor.faq.batchUpdateTagTip', { count: selectedRowKeys.length }) }}
- </p>
- <t-form layout="vertical" class="batch-tag-form">
- <t-form-item :label="$t('knowledgeBase.tagLabel')">
- <t-select
- v-model="batchTagValue"
- :options="tagSelectOptions"
- :placeholder="$t('knowledgeBase.tagPlaceholder')"
- clearable
- filterable
- >
- <template #empty>
- <div class="tag-select-empty">
- {{ $t('knowledgeBase.noTags') }}
+ <Teleport to="body">
+ <Transition name="modal">
+ <div v-if="batchTagDialogVisible" class="batch-tag-overlay" @click.self="batchTagDialogVisible = false">
+ <div class="batch-tag-modal">
+ <!-- 关闭按钮 -->
+ <button class="batch-tag-close-btn" @click="batchTagDialogVisible = false" :aria-label="$t('general.close')">
+ <svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
+ <path d="M15 5L5 15M5 5L15 15" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
+ </svg>
+ </button>
+
+ <div class="batch-tag-container">
+ <div class="batch-tag-header">
+ <h2 class="batch-tag-title">{{ $t('knowledgeEditor.faq.batchUpdateTag') }}</h2>
+ </div>
+
+ <div class="batch-tag-content">
+ <div class="batch-tag-tip">
+ <t-icon name="info-circle" size="16px" class="tip-icon" />
+ <span>{{ $t('knowledgeEditor.faq.batchUpdateTagTip', { count: selectedRowKeys.length }) }}</span>
</div>
- </template>
- </t-select>
- </t-form-item>
- </t-form>
- </div>
- </t-dialog>
+ <t-form layout="vertical" class="batch-tag-form">
+ <t-form-item :label="$t('knowledgeBase.tagLabel')">
+ <t-select
+ v-model="batchTagValue"
+ :options="tagSelectOptions"
+ :placeholder="$t('knowledgeBase.tagPlaceholder')"
+ clearable
+ filterable
+ class="batch-tag-select"
+ >
+ <template #empty>
+ <div class="tag-select-empty">
+ {{ $t('knowledgeBase.noTags') }}
+ </div>
+ </template>
+ </t-select>
+ </t-form-item>
+ </t-form>
+ </div>
+
+ <div class="batch-tag-footer">
+ <t-button
+ theme="default"
+ variant="outline"
+ @click="batchTagDialogVisible = false"
+ >
+ {{ $t('common.cancel') }}
+ </t-button>
+ <t-button
+ theme="primary"
+ @click="handleBatchTag"
+ >
+ {{ $t('common.confirm') }}
+ </t-button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </Transition>
+ </Teleport>
<!-- Search Test Drawer -->
<t-drawer
@@ -986,8 +1077,10 @@ import {
deleteKnowledgeBaseTag,
getKnowledgeBaseById,
listKnowledgeBases,
+ getKnowledgeDetails,
} from '@/api/knowledge-base'
import * as XLSX from 'xlsx'
+import Papa from 'papaparse'
import FAQTagTooltip from '@/components/FAQTagTooltip.vue'
import { useUIStore } from '@/stores/ui'
@@ -1018,6 +1111,7 @@ interface FAQEntryPayload {
negative_questions: string[]
answers: string[]
tag_id?: string
+ is_enabled?: boolean
}
const props = defineProps<{
@@ -1158,6 +1252,15 @@ const importState = reactive({
file: null as File | null,
preview: [] as FAQEntryPayload[],
importing: false,
+ taskId: null as string | null,
+ taskStatus: null as {
+ status: string
+ progress: number
+ total: number
+ processed: number
+ error?: string
+ } | null,
+ pollingInterval: null as ReturnType<typeof setInterval> | null,
})
// Search test state
@@ -1174,7 +1277,12 @@ const searchForm = reactive({
// Toolbar actions dropdown
const toolbarActionOptions = computed(() => {
const options = [
- { content: t('knowledgeEditor.faqImport.importButton'), value: 'import', icon: 'upload' },
+ {
+ content: t('knowledgeEditor.faqImport.importButton'),
+ value: 'import',
+ icon: 'upload',
+ disabled: importState.taskStatus?.status === 'running' // 导入过程中禁用导入按钮
+ },
{ content: t('knowledgeEditor.faq.searchTest'), value: 'search', icon: 'search' },
]
@@ -1451,6 +1559,29 @@ const handleFaqMenuAction = (event: Event) => {
openEditor()
} else if (detail.action === 'import') {
openImportDialog()
+ } else if (detail.action === 'search') {
+ searchDrawerVisible.value = true
+ } else if (detail.action === 'batch') {
+ // 批量操作通过左侧菜单的下拉菜单处理
+ if (selectedRowKeys.value.length === 0) {
+ MessagePlugin.warning(t('knowledgeEditor.faq.selectEntriesFirst') || '请先选中要操作的FAQ条目')
+ }
+ } else if (detail.action === 'batchTag') {
+ if (selectedRowKeys.value.length > 0) {
+ openBatchTagDialog()
+ }
+ } else if (detail.action === 'batchEnable') {
+ if (selectedRowKeys.value.length > 0) {
+ handleBatchStatusChange(true)
+ }
+ } else if (detail.action === 'batchDisable') {
+ if (selectedRowKeys.value.length > 0) {
+ handleBatchStatusChange(false)
+ }
+ } else if (detail.action === 'batchDelete') {
+ if (selectedRowKeys.value.length > 0) {
+ handleBatchDelete()
+ }
}
}
@@ -1767,10 +1898,18 @@ const handleMenuDelete = async (entry: FAQEntry) => {
}
const openImportDialog = () => {
+ // 如果正在导入,不允许打开导入对话框
+ if (importState.taskStatus?.status === 'running') {
+ MessagePlugin.warning('导入正在进行中,请等待完成后再试')
+ return
+ }
+ stopPolling()
importVisible.value = true
importState.file = null
importState.preview = []
importState.mode = 'append'
+ // 注意:不清除taskId和taskStatus,以便在关闭对话框后仍能看到进度
+ importState.importing = false
}
const processFile = async (file: File) => {
@@ -1820,25 +1959,55 @@ const parseJSONFile = async (file: File): Promise<FAQEntryPayload[]> => {
const parseCSVFile = async (file: File): Promise<FAQEntryPayload[]> => {
const text = await file.text()
- const [headerLine, ...rows] = text.split(/\r?\n/).filter(Boolean)
- const headers = headerLine.split(',').map((h) => h.trim().toLowerCase())
- const payloads: FAQEntryPayload[] = []
- rows.forEach((line) => {
- const columns = line.split(',').map((c) => c.trim())
- const record: Record<string, string> = {}
- headers.forEach((key, idx) => {
- record[key] = columns[idx] || ''
+
+ // 使用 papaparse 解析 CSV,自动处理引号、转义、分隔符等
+ return new Promise((resolve, reject) => {
+ Papa.parse(text, {
+ header: true,
+ skipEmptyLines: true,
+ delimiter: '', // 自动检测分隔符(逗号或制表符)
+ quoteChar: '"',
+ escapeChar: '"',
+ transformHeader: (header: string) => {
+ // 移除字段名中的括号和说明,只保留核心字段名
+ const cleaned = header.trim()
+ .replace(/\([^)]*\)/g, '') // 移除括号及内容
+ .trim()
+ // 对于中文字段名,不转换为小写;对于英文字段名,转换为小写
+ return /[\u4e00-\u9fa5]/.test(cleaned) ? cleaned : cleaned.toLowerCase()
+ },
+ complete: (results) => {
+ try {
+ const payloads: FAQEntryPayload[] = []
+ results.data.forEach((row: any) => {
+ const record: Record<string, string> = {}
+ // 将行数据转换为记录对象
+ Object.keys(row).forEach((key) => {
+ record[key] = String(row[key] || '').trim()
+ })
+
+ const isDisabled = parseBooleanField(record['是否停用'], false)
+ payloads.push(
+ normalizePayload({
+ standard_question: record['问题'] || record['standard_question'] || record['question'] || '',
+ answers: splitByDelimiter(record['机器人回答'] || record['answers']),
+ similar_questions: splitByDelimiter(record['相似问题'] || record['similar_questions']),
+ negative_questions: splitByDelimiter(record['反例问题'] || record['negative_questions']),
+ tag_id: record['分类'] || record['tag_id'] || '',
+ is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
+ }),
+ )
+ })
+ resolve(payloads)
+ } catch (error) {
+ reject(error)
+ }
+ },
+ error: (error: Error) => {
+ reject(new Error(`CSV解析失败: ${error.message}`))
+ },
})
- payloads.push(
- normalizePayload({
- standard_question: record['standard_question'] || record['question'] || '',
- answers: splitByDelimiter(record['answers']),
- similar_questions: splitByDelimiter(record['similar_questions']),
- negative_questions: splitByDelimiter(record['negative_questions']),
- }),
- )
})
- return payloads
}
const parseExcelFile = async (file: File): Promise<FAQEntryPayload[]> => {
@@ -1846,60 +2015,65 @@ const parseExcelFile = async (file: File): Promise<FAQEntryPayload[]> => {
const workbook = XLSX.read(data, { type: 'array' })
const sheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[sheetName]
- const json = XLSX.utils.sheet_to_json<Record<string, string>>(worksheet, { defval: '' })
- return json.map((row) =>
- normalizePayload({
- standard_question: row['standard_question'] || row['question'] || '',
- answers: splitByDelimiter(row['answers']),
- similar_questions: splitByDelimiter(row['similar_questions']),
- negative_questions: splitByDelimiter(row['negative_questions']),
- }),
- )
+ // 使用 raw: false 确保正确处理引号和转义
+ const json = XLSX.utils.sheet_to_json<Record<string, string>>(worksheet, {
+ defval: '',
+ raw: false // 确保字符串值被正确解析
+ })
+ return json.map((row) => {
+ // 获取原始表头(去除括号说明)
+ const normalizedRow: Record<string, string> = {}
+ Object.keys(row).forEach((key) => {
+ const normalizedKey = key.trim()
+ .replace(/\([^)]*\)/g, '') // 移除括号及内容
+ .trim()
+ // 对于中文字段名,不转换为小写;对于英文字段名,转换为小写
+ const finalKey = /[\u4e00-\u9fa5]/.test(normalizedKey) ? normalizedKey : normalizedKey.toLowerCase()
+ // 确保值是字符串类型
+ normalizedRow[finalKey] = String(row[key] || '').trim()
+ })
+
+ const isDisabled = parseBooleanField(normalizedRow['是否停用'], false)
+ return normalizePayload({
+ standard_question: normalizedRow['问题'] || normalizedRow['standard_question'] || normalizedRow['question'] || '',
+ answers: splitByDelimiter(normalizedRow['机器人回答'] || normalizedRow['answers']),
+ similar_questions: splitByDelimiter(normalizedRow['相似问题'] || normalizedRow['similar_questions']),
+ negative_questions: splitByDelimiter(normalizedRow['反例问题'] || normalizedRow['negative_questions']),
+ tag_id: normalizedRow['分类'] || normalizedRow['tag_id'] || '',
+ is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
+ })
+ })
}
const splitByDelimiter = (value?: string) => {
if (!value) return []
- // 支持引号包裹的内容,避免包含分隔符的内容被错误分割
- const result: string[] = []
- let current = ''
- let inQuotes = false
- // 支持多种引号字符
- const quoteChars = ['"', "'", '\u201C', '\u201D', '\u2018', '\u2019', '\u300C', '\u300D', '\u300E', '\u300F']
+ // 只使用 ## 作为分隔符,避免错误分割包含逗号、分号等内容
+ const trimmedValue = value.trim()
+ if (!trimmedValue) return []
- for (let i = 0; i < value.length; i++) {
- const char = value[i]
-
- // 检查是否是引号
- if (quoteChars.includes(char)) {
- inQuotes = !inQuotes
- continue
- }
-
- // 如果在引号内,直接添加到当前字符串
- if (inQuotes) {
- current += char
- continue
- }
-
- // 检查是否是分隔符
- if (/[\n;;,,]/.test(char)) {
- const trimmed = current.trim()
- if (trimmed) {
- result.push(trimmed)
- }
- current = ''
- } else {
- current += char
- }
+ // 如果包含 ## 分隔符,按 ## 分割
+ if (trimmedValue.includes('##')) {
+ return trimmedValue
+ .split('##')
+ .map(item => item.trim())
+ .filter(Boolean)
}
- // 添加最后一部分
- const trimmed = current.trim()
- if (trimmed) {
- result.push(trimmed)
+ // 如果没有 ## 分隔符,整个值作为一个答案
+ return [trimmedValue]
+}
+
+// 解析布尔字段(支持多种格式:TRUE/FALSE, true/false, 是/否, 1/0等)
+const parseBooleanField = (value?: string, defaultValue: boolean = true): boolean | undefined => {
+ if (!value) return undefined
+ const normalized = value.trim().toUpperCase()
+ if (normalized === 'TRUE' || normalized === '1' || normalized === '是' || normalized === 'YES') {
+ return true
}
-
- return result.filter(Boolean)
+ if (normalized === 'FALSE' || normalized === '0' || normalized === '否' || normalized === 'NO') {
+ return false
+ }
+ return defaultValue
}
const normalizePayload = (payload: Partial<FAQEntryPayload>): FAQEntryPayload => ({
@@ -1908,29 +2082,267 @@ const normalizePayload = (payload: Partial<FAQEntryPayload>): FAQEntryPayload =>
similar_questions: payload.similar_questions?.filter(Boolean) || [],
negative_questions: payload.negative_questions?.filter(Boolean) || [],
tag_id: payload.tag_id || '',
+ is_enabled: payload.is_enabled !== undefined ? payload.is_enabled : undefined,
})
+const stopPolling = () => {
+ if (importState.pollingInterval) {
+ clearInterval(importState.pollingInterval)
+ importState.pollingInterval = null
+ }
+}
+
+const startPolling = (taskId: string) => {
+ stopPolling()
+ // 保存taskId到localStorage,以便刷新后恢复
+ saveTaskIdToStorage(taskId)
+
+ importState.pollingInterval = setInterval(async () => {
+ try {
+ const res: any = await getKnowledgeDetails(taskId)
+ const knowledge = res?.data
+ if (knowledge) {
+ // 从Knowledge对象中提取导入任务状态
+ // ParseStatus: "pending" -> "pending", "processing" -> "running", "completed" -> "success", "failed" -> "failed"
+ let status = knowledge.parse_status
+ if (status === 'processing') {
+ status = 'running'
+ } else if (status === 'completed') {
+ status = 'success'
+ }
+
+ // 从Metadata中提取导入进度信息
+ const metadata = knowledge.metadata || {}
+ const progress = metadata.import_progress || 0
+ const total = metadata.import_total || 0
+ const processed = metadata.import_processed || 0
+ const error = knowledge.error_message || ''
+
+ importState.taskStatus = {
+ status: status,
+ progress: progress,
+ total: total,
+ processed: processed,
+ error: error,
+ }
+
+ // 任务完成或失败,停止轮询(但不自动关闭进度条,让用户手动关闭)
+ if (status === 'success' || status === 'failed') {
+ stopPolling()
+ if (status === 'success') {
+ MessagePlugin.success(t('knowledgeEditor.faqImport.importSuccess'))
+ await loadEntries()
+ // 任务完成后,3秒后自动关闭进度条
+ setTimeout(() => {
+ if (importState.taskStatus?.status === 'success') {
+ handleCloseProgress()
+ }
+ }, 3000)
+ } else {
+ MessagePlugin.error(error || t('common.operationFailed'))
+ // 失败时不自动关闭,让用户看到错误信息
+ }
+ }
+ }
+ } catch (error: any) {
+ console.error('Failed to poll task status:', error)
+ // 如果任务不存在或已过期,清除存储
+ if (error?.response?.status === 404 || error?.message?.includes('not found')) {
+ clearTaskIdFromStorage()
+ stopPolling()
+ importState.taskId = null
+ importState.taskStatus = null
+ }
+ }
+ }, 3000) // 每3秒轮询一次
+}
+
+const handleCancelImport = () => {
+ stopPolling()
+ importState.importing = false
+ importState.taskId = null
+ importState.taskStatus = null
+ importVisible.value = false
+ // 注意:不清除localStorage,因为任务可能还在进行中
+}
+
+const handleCloseProgress = () => {
+ stopPolling()
+ importState.taskId = null
+ importState.taskStatus = null
+ clearTaskIdFromStorage()
+}
+
+// localStorage相关函数
+const getStorageKey = () => {
+ return `faq_import_task_${props.kbId}`
+}
+
+const saveTaskIdToStorage = (taskId: string) => {
+ if (!props.kbId) return
+ try {
+ localStorage.setItem(getStorageKey(), taskId)
+ } catch (error) {
+ console.error('Failed to save taskId to localStorage:', error)
+ }
+}
+
+const getTaskIdFromStorage = (): string | null => {
+ if (!props.kbId) return null
+ try {
+ return localStorage.getItem(getStorageKey())
+ } catch (error) {
+ console.error('Failed to get taskId from localStorage:', error)
+ return null
+ }
+}
+
+const clearTaskIdFromStorage = () => {
+ if (!props.kbId) return
+ try {
+ localStorage.removeItem(getStorageKey())
+ } catch (error) {
+ console.error('Failed to clear taskId from localStorage:', error)
+ }
+}
+
+// 恢复导入任务状态(用于刷新后恢复)
+const restoreImportTask = async () => {
+ if (!props.kbId) return
+
+ const savedTaskId = getTaskIdFromStorage()
+ if (!savedTaskId) return
+
+ try {
+ // 查询Knowledge状态
+ const res: any = await getKnowledgeDetails(savedTaskId)
+ const knowledge = res?.data
+
+ if (knowledge) {
+ // 从Knowledge对象中提取导入任务状态
+ let status = knowledge.parse_status
+ if (status === 'processing') {
+ status = 'running'
+ } else if (status === 'completed') {
+ status = 'success'
+ }
+
+ // 从Metadata中提取导入进度信息
+ const metadata = knowledge.metadata || {}
+ const progress = metadata.import_progress || 0
+ const total = metadata.import_total || 0
+ const processed = metadata.import_processed || 0
+ const error = knowledge.error_message || ''
+
+ importState.taskId = savedTaskId
+ importState.taskStatus = {
+ status: status,
+ progress: progress,
+ total: total,
+ processed: processed,
+ error: error,
+ }
+
+ // 如果任务还在进行中,恢复轮询
+ if (status === 'pending' || status === 'running') {
+ startPolling(savedTaskId)
+ } else {
+ // 任务已完成或失败,清除存储
+ clearTaskIdFromStorage()
+ }
+ } else {
+ // 任务不存在,清除存储
+ clearTaskIdFromStorage()
+ }
+ } catch (error: any) {
+ console.error('Failed to restore import task:', error)
+ // 如果任务不存在或已过期,清除存储
+ if (error?.response?.status === 404 || error?.message?.includes('not found')) {
+ clearTaskIdFromStorage()
+ }
+ }
+}
+
const handleImport = async () => {
if (!importState.file || !importState.preview.length) {
MessagePlugin.warning(t('knowledgeEditor.faqImport.selectFile'))
return
}
+
+ // 如果任务已完成或失败,关闭对话框
+ if (importState.taskStatus?.status === 'success' || importState.taskStatus?.status === 'failed') {
+ if (importState.taskStatus.status === 'success') {
+ handleCancelImport()
+ } else {
+ // 失败时重试
+ importState.taskId = null
+ importState.taskStatus = null
+ importState.importing = false
+ }
+ return
+ }
+
importState.importing = true
try {
- await upsertFAQEntries(props.kbId, {
+ const res: any = await upsertFAQEntries(props.kbId, {
entries: importState.preview,
mode: importState.mode,
})
- MessagePlugin.success(t('knowledgeEditor.faqImport.importSuccess'))
- importVisible.value = false
- await loadEntries()
+
+ const taskId = res?.data?.task_id
+ if (taskId) {
+ importState.taskId = taskId
+ importState.taskStatus = {
+ status: 'pending',
+ progress: 0,
+ total: importState.preview.length,
+ processed: 0,
+ }
+ // 开始轮询任务状态
+ startPolling(taskId)
+ // 立即关闭导入对话框,进度将在列表页面顶部显示
+ importVisible.value = false
+ // 重置导入对话框状态(但保留taskId和taskStatus用于进度显示)
+ importState.file = null
+ importState.preview = []
+ importState.importing = false
+ } else {
+ // 如果没有返回任务ID,可能是旧版本API,使用同步方式
+ MessagePlugin.success(t('knowledgeEditor.faqImport.importSuccess'))
+ importVisible.value = false
+ await loadEntries()
+ importState.importing = false
+ }
} catch (error: any) {
MessagePlugin.error(error?.message || t('common.operationFailed'))
- } finally {
importState.importing = false
+ stopPolling()
}
}
+// 监听选中数量变化,通知左侧菜单
+watch(selectedRowKeys, (newKeys, oldKeys) => {
+ const count = newKeys.length
+ // 获取选中条目的状态信息
+ const selectedEntries = entries.value.filter(entry => newKeys.includes(entry.id))
+ const enabledCount = selectedEntries.filter(entry => entry.is_enabled !== false).length
+ const disabledCount = count - enabledCount
+
+ const event = new CustomEvent('faqSelectionChanged', {
+ detail: {
+ count,
+ enabledCount,
+ disabledCount
+ }
+ })
+ window.dispatchEvent(event)
+}, { immediate: true, deep: true })
+
+// 组件卸载时清理轮询
+onUnmounted(() => {
+ stopPolling()
+})
+
// 下载示例文件选项
const downloadExampleOptions = computed(() => [
{ content: t('knowledgeEditor.faqImport.downloadExampleJSON'), value: 'json' },
@@ -1986,18 +2398,28 @@ const downloadJSONExample = () => {
// 下载 CSV 示例
const downloadCSVExample = () => {
- const headers = ['standard_question', 'answers', 'similar_questions', 'negative_questions']
+ const headers = ['分类(必填)', '问题(必填)', '相似问题(选填-多个用##分隔)', '反例问题(选填-多个用##分隔)', '机器人回答(必填-多个用##分隔)', '是否全部回复(选填-默认FALSE)', '是否停用(选填-默认FALSE)', '是否禁止被推荐(选填-默认False 可被推荐)']
const rows = exampleData.map((item) => {
return [
+ '', // 分类
item.standard_question,
- item.answers.join(';'),
- item.similar_questions.join(';'),
- item.negative_questions.join(';'),
+ item.similar_questions.join('##'),
+ item.negative_questions.join('##'),
+ item.answers.join('##'),
+ 'FALSE', // 是否全部回复
+ 'FALSE', // 是否停用
+ 'FALSE', // 是否禁止被推荐
]
})
const csvContent = [
- headers.join(','),
- ...rows.map((row) => row.map((cell) => `"${cell.replace(/"/g, '""')}"`).join(',')),
+ headers.join('\t'), // 使用制表符分隔
+ ...rows.map((row) => row.map((cell) => {
+ // 如果包含制表符、换行符或引号,需要用引号包裹
+ if (cell.includes('\t') || cell.includes('\n') || cell.includes('"')) {
+ return `"${cell.replace(/"/g, '""')}"`
+ }
+ return cell
+ }).join('\t')),
].join('\n')
const blob = new Blob(['\ufeff' + csvContent], { type: 'text/csv;charset=utf-8' })
const url = URL.createObjectURL(blob)
@@ -2014,10 +2436,14 @@ const downloadCSVExample = () => {
const downloadExcelExample = () => {
const worksheet = XLSX.utils.json_to_sheet(
exampleData.map((item) => ({
- standard_question: item.standard_question,
- answers: item.answers.join(';'),
- similar_questions: item.similar_questions.join(';'),
- negative_questions: item.negative_questions.join(';'),
+ '分类(必填)': '',
+ '问题(必填)': item.standard_question,
+ '相似问题(选填-多个用##分隔)': item.similar_questions.join('##'),
+ '反例问题(选填-多个用##分隔)': item.negative_questions.join('##'),
+ '机器人回答(必填-多个用##分隔)': item.answers.join('##'),
+ '是否全部回复(选填-默认FALSE)': 'FALSE',
+ '是否停用(选填-默认FALSE)': 'FALSE',
+ '是否禁止被推荐(选填-默认False 可被推荐)': 'FALSE',
})),
)
const workbook = XLSX.utils.book_new()
@@ -2037,6 +2463,11 @@ watch(
if (!newKbId) {
kbInfo.value = null
+ // kbId变化时,清除之前的任务状态
+ stopPolling()
+ importState.taskId = null
+ importState.taskStatus = null
+ clearTaskIdFromStorage()
return
}
@@ -2047,6 +2478,8 @@ watch(
loadEntries()
loadTags()
+ // 恢复导入任务状态(如果存在)
+ await restoreImportTask()
},
{ immediate: true },
)
@@ -2227,10 +2660,28 @@ const handleResize = () => {
}, 150)
}
-onMounted(() => {
+onMounted(async () => {
loadKnowledgeList()
window.addEventListener('resize', handleResize)
window.addEventListener('faqMenuAction', handleFaqMenuAction as EventListener)
+ // 如果已有kbId,恢复导入任务状态
+ if (props.kbId) {
+ await restoreImportTask()
+ }
+ // 主动触发一次选中数量事件,确保左侧菜单能接收到初始状态
+ nextTick(() => {
+ const count = selectedRowKeys.value.length
+ const selectedEntries = entries.value.filter(entry => selectedRowKeys.value.includes(entry.id))
+ const enabledCount = selectedEntries.filter(entry => entry.is_enabled !== false).length
+ const disabledCount = count - enabledCount
+ window.dispatchEvent(new CustomEvent('faqSelectionChanged', {
+ detail: {
+ count,
+ enabledCount,
+ disabledCount
+ }
+ }))
+ })
})
onUnmounted(() => {
@@ -2743,6 +3194,93 @@ watch(() => entries.value.map(e => ({
margin-bottom: 20px;
border-bottom: 1px solid #e7ebf0;
flex-shrink: 0;
+}
+
+// 导入进度条样式(显示在列表页面顶部)
+.faq-import-progress-bar {
+ margin-bottom: 16px;
+ background: #fff;
+ border: 1px solid #e7ebf0;
+ border-radius: 8px;
+ padding: 12px 16px;
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
+
+ .progress-bar-content {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ }
+
+ .progress-bar-header {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-size: 14px;
+ color: #000000e6;
+
+ .progress-icon {
+ flex-shrink: 0;
+
+ &.icon-loading {
+ animation: rotate 1s linear infinite;
+ color: #00a870;
+ }
+
+ &.icon-success {
+ color: #00a870;
+ }
+
+ &.icon-error {
+ color: #fa5151;
+ }
+ }
+
+ .progress-title {
+ font-weight: 500;
+ flex: 1;
+ }
+
+ .progress-count {
+ color: #86909c;
+ font-size: 13px;
+ }
+
+ .progress-close-btn {
+ flex-shrink: 0;
+ padding: 4px;
+ margin-left: 8px;
+ }
+ }
+
+ .progress-bar {
+ margin: 0;
+ width: 100%;
+
+ :deep(.t-progress) {
+ width: 100%;
+ }
+
+ :deep(.t-progress__bar) {
+ width: 100%;
+ }
+ }
+
+ .progress-error {
+ margin: 0;
+ font-size: 13px;
+ color: #fa5151;
+ line-height: 1.5;
+ }
+}
+
+@keyframes rotate {
+ from {
+ transform: rotate(0deg);
+ }
+ to {
+ transform: rotate(360deg);
+ }
+}
.faq-header-title {
display: flex;
@@ -2835,7 +3373,6 @@ watch(() => entries.value.map(e => ({
line-height: 20px;
}
-}
.tag-filter-bar {
display: flex;
@@ -4385,7 +4922,9 @@ watch(() => entries.value.map(e => ({
}
.modal-enter-active .faq-import-modal,
-.modal-leave-active .faq-import-modal {
+.modal-leave-active .faq-import-modal,
+.modal-enter-active .batch-tag-modal,
+.modal-leave-active .batch-tag-modal {
transition: transform 0.2s ease, opacity 0.2s ease;
}
@@ -4395,7 +4934,9 @@ watch(() => entries.value.map(e => ({
}
.modal-enter-from .faq-import-modal,
-.modal-leave-to .faq-import-modal {
+.modal-leave-to .faq-import-modal,
+.modal-enter-from .batch-tag-modal,
+.modal-leave-to .batch-tag-modal {
transform: scale(0.95);
opacity: 0;
}
@@ -4794,19 +5335,124 @@ watch(() => entries.value.map(e => ({
gap: 8px;
}
-.batch-tag-dialog-content {
- padding: 8px 0;
+// 批量分类弹窗样式 - 与导入对话框风格一致
+.batch-tag-overlay {
+ position: fixed;
+ inset: 0;
+ z-index: 1000;
+ background: rgba(0, 0, 0, 0.5);
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ padding: 20px;
+ backdrop-filter: blur(4px);
+}
+
+.batch-tag-modal {
+ position: relative;
+ width: 100%;
+ max-width: 480px;
+ background: #ffffff;
+ border-radius: 12px;
+ box-shadow: 0 6px 28px rgba(15, 23, 42, 0.08);
+ overflow: hidden;
+ display: flex;
+ flex-direction: column;
+
+ .batch-tag-close-btn {
+ position: absolute;
+ top: 20px;
+ right: 20px;
+ width: 32px;
+ height: 32px;
+ border: none;
+ background: #f5f5f5;
+ border-radius: 6px;
+ cursor: pointer;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ color: #666;
+ transition: all 0.2s ease;
+ z-index: 10;
+
+ &:hover {
+ background: #e5e5e5;
+ color: #000;
+ }
+ }
+}
+
+.batch-tag-container {
+ display: flex;
+ flex-direction: column;
+ padding: 24px;
+}
+
+.batch-tag-header {
+ margin-bottom: 24px;
+ padding-right: 40px;
+
+ .batch-tag-title {
+ margin: 0;
+ font-size: 20px;
+ font-weight: 600;
+ color: #0f172a;
+ line-height: 1.4;
+ }
+}
+
+.batch-tag-content {
+ flex: 1;
+ min-height: 0;
}
-.batch-tag-dialog-tip {
- margin: 0 0 20px 0;
+.batch-tag-tip {
+ display: flex;
+ align-items: flex-start;
+ gap: 8px;
+ padding: 12px 16px;
+ margin-bottom: 20px;
+ background: #f0f9ff;
+ border: 1px solid #bae6fd;
+ border-radius: 8px;
font-size: 14px;
- color: #4e5969;
+ color: #0369a1;
line-height: 1.5;
+
+ .tip-icon {
+ flex-shrink: 0;
+ margin-top: 2px;
+ color: #0284c7;
+ }
}
.batch-tag-form {
margin-top: 0;
+
+ :deep(.t-form-item) {
+ margin-bottom: 0;
+ }
+
+ :deep(.t-form-item__label) {
+ font-size: 14px;
+ font-weight: 500;
+ color: #1d2129;
+ margin-bottom: 8px;
+ }
+}
+
+.batch-tag-select {
+ width: 100%;
+}
+
+.batch-tag-footer {
+ display: flex;
+ justify-content: flex-end;
+ gap: 12px;
+ margin-top: 24px;
+ padding-top: 20px;
+ border-top: 1px solid #e5e7eb;
}
.tag-select-empty {
diff --git a/internal/agent/prompts.go b/internal/agent/prompts.go
index 4fbf87a3e..acb493de7 100644
--- a/internal/agent/prompts.go
+++ b/internal/agent/prompts.go
@@ -117,56 +117,56 @@ func formatKnowledgeBaseList(kbInfos []*KnowledgeBaseInfo) string {
}
builder.WriteString(fmt.Sprintf(" - Document count: %d\n", kb.DocCount))
- // Display recent documents if available
- // For FAQ type knowledge bases, adjust the display format
- if len(kb.RecentDocs) > 0 {
- if kbType == "faq" {
- // FAQ knowledge base: show Q&A pairs in a more compact format
- builder.WriteString(" - Recent FAQ entries:\n\n")
- builder.WriteString(" | # | Standard Question | Similar Questions | Answers | Entry ID | Created At |\n")
- builder.WriteString(" |---|-------------------|-------------------|---------|----------|------------|\n")
- for j, doc := range kb.RecentDocs {
- if j >= 10 { // Limit to 10 documents
- break
- }
- question := doc.FAQStandardQuestion
- if question == "" {
- question = doc.FileName
- }
- similar := "-"
- if len(doc.FAQSimilarQuestions) > 0 {
- similar = strings.Join(doc.FAQSimilarQuestions, "; ")
- }
- answers := "-"
- if len(doc.FAQAnswers) > 0 {
- answers = strings.Join(doc.FAQAnswers, " | ")
- }
- builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s |\n",
- j+1, question, similar, answers, doc.KnowledgeID, doc.CreatedAt))
- }
- } else {
- // Document knowledge base: show documents in standard format
- builder.WriteString(" - Recently added documents:\n\n")
- builder.WriteString(" | # | Document Name | Type | Created At | Knowledge ID | File Size | Summary |\n")
- builder.WriteString(" |---|---------------|------|------------|--------------|----------|---------|\n")
- for j, doc := range kb.RecentDocs {
- if j >= 10 { // Limit to 10 documents
- break
- }
- docName := doc.Title
- if docName == "" {
- docName = doc.FileName
- }
- // Format file size
- fileSize := formatFileSize(doc.FileSize)
- summary := formatDocSummary(doc.Description, 120)
- builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s | %s |\n",
- j+1, docName, doc.Type, doc.CreatedAt, doc.KnowledgeID, fileSize, summary))
- }
- }
- builder.WriteString("\n")
- }
- builder.WriteString("\n")
+ // // Display recent documents if available
+ // // For FAQ type knowledge bases, adjust the display format
+ // if len(kb.RecentDocs) > 0 {
+ // if kbType == "faq" {
+ // // FAQ knowledge base: show Q&A pairs in a more compact format
+ // builder.WriteString(" - Recent FAQ entries:\n\n")
+ // builder.WriteString(" | # | Standard Question | Similar Questions | Answers | Entry ID | Created At |\n")
+ // builder.WriteString(" |---|-------------------|-------------------|---------|----------|------------|\n")
+ // for j, doc := range kb.RecentDocs {
+ // if j >= 10 { // Limit to 10 documents
+ // break
+ // }
+ // question := doc.FAQStandardQuestion
+ // if question == "" {
+ // question = doc.FileName
+ // }
+ // similar := "-"
+ // if len(doc.FAQSimilarQuestions) > 0 {
+ // similar = strings.Join(doc.FAQSimilarQuestions, "; ")
+ // }
+ // answers := "-"
+ // if len(doc.FAQAnswers) > 0 {
+ // answers = strings.Join(doc.FAQAnswers, " | ")
+ // }
+ // builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s |\n",
+ // j+1, question, similar, answers, doc.KnowledgeID, doc.CreatedAt))
+ // }
+ // } else {
+ // // Document knowledge base: show documents in standard format
+ // builder.WriteString(" - Recently added documents:\n\n")
+ // builder.WriteString(" | # | Document Name | Type | Created At | Knowledge ID | File Size | Summary |\n")
+ // builder.WriteString(" |---|---------------|------|------------|--------------|----------|---------|\n")
+ // for j, doc := range kb.RecentDocs {
+ // if j >= 10 { // Limit to 10 documents
+ // break
+ // }
+ // docName := doc.Title
+ // if docName == "" {
+ // docName = doc.FileName
+ // }
+ // // Format file size
+ // fileSize := formatFileSize(doc.FileSize)
+ // summary := formatDocSummary(doc.Description, 120)
+ // builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s | %s |\n",
+ // j+1, docName, doc.Type, doc.CreatedAt, doc.KnowledgeID, fileSize, summary))
+ // }
+ // }
+ // builder.WriteString("\n")
+ // }
+ // builder.WriteString("\n")
}
return builder.String()
}
@@ -241,719 +241,380 @@ func BuildProgressiveRAGSystemPrompt(knowledgeBases []*KnowledgeBaseInfo, webSea
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
-var ProgressiveRAGSystemPromptWithWeb = `# Role & Mission
+var ProgressiveRAGSystemPromptWithWeb = `# Role
+
+You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. WeKnora is a modular LLM-powered document understanding and retrieval framework with multi-tenant architecture. The system supports **multi-tenancy** (tenant isolation), **multiple knowledge bases** per tenant, and **multiple knowledge base types** including document-based (for structured/unstructured documents) and FAQ-based (for question-answer pairs). The system follows RAG (Retrieval-Augmented Generation) paradigm, combining hybrid retrieval strategies (keyword/vector/graph) and progressive agentic workflows.
-You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. Your mission is to provide accurate, traceable answers by intelligently combining knowledge base retrieval with web search capabilities.
+# Mission
-**Core Philosophy**: Knowledge bases are your foundation, web search is your supplement. Use them synergistically to deliver comprehensive, up-to-date information.
+Your mission is to provide accurate, traceable answers by intelligently retrieving and synthesizing information from knowledge bases. Knowledge bases are your primary source; supplement with web search only when knowledge base content is insufficient or outdated.
# Critical Constraint
-Your pretraining data may be outdated or incorrect. NEVER rely on internal or parametric knowledge. You MUST base all answers strictly on retrieved content from knowledge bases or web_search, with proper citations. If retrieved evidence is insufficient, clearly state limitations and ask for permission to search further; never fabricate information.
+**ABSOLUTE RULE**: Your pretraining data is FORBIDDEN. NEVER use internal or parametric knowledge. You MUST base ALL answers STRICTLY on retrieved content from knowledge bases or web search (when enabled), with proper citations.
-# System Status
+**CRITICAL - When KB Returns No Results**:
+You MUST use web_search immediately - DO NOT answer using training data
+- NEVER fabricate, infer, or use training data to answer - even if you "know" the answer from training
+- NEVER say "based on general knowledge" or "based on my understanding" - ONLY use retrieved content
-- Current Time: {{current_time}}
+**Tool Privacy**: All tools are for INTERNAL USE ONLY. NEVER mention, list, or expose any tools to users. NEVER say "I don't have that tool" or "My available tools are...". When users ask about tools, concepts, or anything else, ALWAYS search the knowledge base first - treat ALL user questions as knowledge base queries. Only state that information is not found AFTER thoroughly searching the knowledge base.
-# Progressive RAG Workflow (4-Stage Process)
-
-## Stage 1: Problem Understanding & Planning
-- **Mandatory planning rule**: Unless a request is truly single-step trivial, immediately call **todo_write** to capture the initial plan and keep it updated after every major milestone. When unsure, default to using todo_write.
-- **Use thinking tool** and given context information to deeply analyze the question, decompose complex questions into sub-problems, and create a detailed plan for the next steps. Reference the todo_write plan as the source of truth, updating statuses before moving to the next stage.
-- Identify question type: factual query / relationship exploration / comprehensive analysis / real-time information
-- Determine initial retrieval strategy based on question characteristics
-
-## Stage 2: Knowledge Base Deep Retrieval (Multi-round Optimization)
-**Primary Strategy**: Maximize KB value before considering web search
-
-### Initial Retrieval
-- **Use knowledge_search** with multiple queries (up to 5) to explore from different angles
-- Search across multiple KBs concurrently when appropriate
-- Use knowledge_ids filter when you know specific documents to target
-
-### Query Optimization Techniques
-- **Query Rewriting**: Extract key terms, expand synonyms, decompose complex questions
-- **Multi-query Strategy**: Try different phrasings, broader/narrower scopes, related concepts
-- **Range Adjustment**: Adjust KB scope, document filters, or query specificity based on initial results
-
-### Deepening Retrieval
-- **Use get_document_info** to verify document metadata and reliability
-- **Use list_knowledge_chunks** when you already know the knowledge_id and need deterministic chunk snapshots or chunk counts
-- **Use database_query** for structured data queries when needed
-
-### Quality Assessment
-After each retrieval round, use thinking tool to evaluate:
-- Relevance: Do results directly address the question?
-- Completeness: Is sufficient information gathered?
-- Credibility: Are sources reliable and up-to-date?
-- Gaps: What information is still missing?
-
-## Stage 3: Web Real-time Information Supplementation
-**Trigger Conditions**: Use web search when:
-- KB results are insufficient or incomplete
-- Question requires real-time/current information (news, recent events, latest updates)
-- Need to verify or supplement KB information with external sources
-- User explicitly requests current/recent information
-
-### Web Search Strategy
-- **Use web_search** with refined queries (synonyms, narrower/wider scope, time filters)
-- Can call multiple times if first round is insufficient
-- **Use web_fetch** to deeply read specific web pages when needed
-- Results are automatically compressed using RAG for efficient processing
-
-### KB-Web Synergy
-- Compare KB and web results for consistency
-- Use web to fill gaps identified in KB retrieval
-- Cross-validate information from both sources
-
-## Stage 4: Synthesis & Answer Generation
-- **Use thinking tool** to validate evidence sufficiency and outline response
-- Synthesize information from all sources (KB + Web)
-- Structure answer clearly with proper citations
-- Be honest about limitations and suggest improvements
-- Close the loop by updating **todo_write**: mark completed steps, leave notes for any follow-ups, and only stop using todo_write when all planned work is resolved or explicitly handed off.
-
-# Intelligent Tool Selection Strategy
-
-## Question Type -> Tool Mapping
-
-### Factual Queries
-- **Primary**: knowledge_search (multiple queries, multiple KBs)
-- **Verification**: get_document_info for metadata
-- **Supplement**: web_search if KB insufficient
-
-### Relationship Exploration
-- **Primary**: query_knowledge_graph (if KB has graph) + knowledge_search
-- **Deep Dive**: database_query for structured relationships
-
-### Comprehensive Analysis
-- **Primary**: knowledge_search (multiple queries) + todo_write (plan)
-- **Exploration**: query_knowledge_graph + get_document_info
-- **Supplement**: web_search for additional perspectives
-
-### Real-time Information Needs
-- **Can prioritize**: web_search first if clearly time-sensitive
-- **Still check KB**: Don't skip KB entirely, but can parallelize
-- **Deep read**: web_fetch for important web sources
-
-## Tool Combination Patterns
-
-Always follow the loop **thinking ➜ todo_write ➜ tool execution**, repeating it between every major action. Thinking chooses the next step, todo_write records/updates the plan and statuses, then the chosen tool runs. After the tool finishes, re-enter thinking ➜ todo_write before proceeding, until the task is explicitly completed.
-
-### Pattern 1: Deep Context Exploration
- thinking (define retrieval hypotheses)
- -> todo_write (capture plan + success criteria)
- -> knowledge_search (multiple queries)
- -> thinking (interpret hits, pick next focus)
- -> todo_write (log findings, queue chunk review)
- -> list_knowledge_chunks (sequential + semantic)
- -> thinking (spot gaps, decide if graph needed)
- -> todo_write (note open questions)
- -> query_knowledge_graph (if applicable)
- -> thinking (evaluate completeness)
- -> todo_write (summarize outcomes, mark done)
-
-### Pattern 2: Document Verification Flow
- thinking (determine verification targets)
- -> todo_write (list documents + checks)
- -> knowledge_search
- -> thinking (confirm candidate docs)
- -> todo_write (update with selected doc IDs)
- -> get_document_info (verify metadata)
- -> thinking (assess metadata gaps)
- -> todo_write (record issues, plan DB queries)
- -> database_query (if structured data needed)
- -> thinking (assess reliability)
- -> todo_write (update verification status and pending checks)
-
-### Pattern 3: KB-Web Hybrid
- thinking (scope KB vs web needs)
- -> todo_write (document KB-first plan)
- -> knowledge_search (KB exploration)
- -> thinking (identify gaps)
- -> todo_write (revise plan before switching sources)
- -> web_search (fill gaps)
- -> thinking (select URLs for deep read)
- -> todo_write (log chosen sources)
- -> web_fetch (deep read key sources)
- -> thinking (synthesize cross-source insights)
- -> todo_write (close completed tasks, open follow-ups)
-
-### Pattern 4: Multi-KB Parallel Search
- thinking (decide KB coverage strategy)
- -> todo_write (track queries per KB)
- -> knowledge_search (all KBs, multiple queries in parallel)
- -> thinking (compare hits, prioritize chunks)
- -> todo_write (note chunk IDs pending review)
- -> list_knowledge_chunks (from best results)
- -> thinking (compare and evaluate)
- -> todo_write (log decisions, note additional retrieval actions)
-
-## Parallel Execution Strategy
-
-**Encourage parallel tool calls when possible**:
-- Multiple KB searches can run concurrently
-- knowledge_search with multiple queries executes in parallel
-- list_knowledge_chunks for multiple chunk_ids processes concurrently
-- KB search and web search can run in parallel when appropriate
-
-# Multi-round Retrieval & Query Optimization
-
-## Query Rewriting Techniques
-- **Keyword Extraction**: Identify core concepts and entities
-- **Synonym Expansion**: Use related terms and alternative phrasings
-- **Question Decomposition**: Break complex questions into simpler sub-queries
-- **Scope Adjustment**: Broaden (more general) or narrow (more specific) queries
-
-## Result Quality Assessment
-After each retrieval:
-1. **Relevance Check**: Do results directly answer the question?
-2. **Completeness Check**: Is sufficient information gathered?
-3. **Credibility Check**: Are sources reliable?
-4. **Gap Analysis**: What information is still missing?
-
-## Adaptive Strategy Adjustment
-- If results are too broad -> narrow queries, add filters
-- If results are too narrow -> broaden queries, remove filters
-- If results are irrelevant -> rewrite queries, try different KBs
-- If results are incomplete -> use related_chunks, try graph, consider web
-
-# Error Handling & Retry Strategy
-
-## Insufficient Results
-1. **Multi-round Retry**: Rewrite queries, try different strategies
-2. **Strategy Switch**: Try different tool combinations
-3. **Scope Expansion**: Search more KBs, remove filters, broaden queries
-4. **Web Supplementation**: Use web_search if KB exhausted (when enabled)
-
-## Tool Call Failures
-1. **Retry Mechanism**: Retry failed tool calls with adjusted parameters
-2. **Fallback Strategy**: Use alternative tools or approaches
-3. **Error Communication**: Clearly explain failures to user
-
-## User Communication
-- **Honest Limitations**: Clearly state when information is unavailable
-- **Improvement Suggestions**: Suggest how to improve queries or KB coverage
-- **Progress Updates**: Keep user informed of retrieval progress
-
-# Tool Usage Guidelines
-
-## knowledge_search
-**When to Use**: Primary retrieval tool for all KB queries
-**Best Practices**:
-- Use multiple queries (2-5) for comprehensive coverage
-- Search multiple KBs concurrently when appropriate
-- Use knowledge_ids filter when targeting specific documents
-- Combine with other tools for deep exploration
-
-**Common Mistakes**:
-- Using single query when multiple would help
-- Not utilizing multi-KB search capability
-- Skipping query optimization
-
-**Common Mistakes**:
-- Using when search results already provide sufficient context
-- Not choosing appropriate relation_type
-- Setting limit too high (causing information overload)
-
-## query_knowledge_graph
-**When to Use**: Exploring entity relationships, understanding concept networks
-**Best Practices**:
-- Check if KB has graph configured first
-- Use for relationship queries, not simple fact lookups
-- Combine with knowledge_search for comprehensive results
-
-**Common Mistakes**:
-- Using for simple text search (use knowledge_search instead)
-- Not checking graph configuration status
-
-## get_document_info
-**When to Use**: Need document metadata, verify document status, batch query multiple documents
-**Best Practices**:
-- Query multiple documents concurrently (up to 10)
-- Use to verify document processing status
-- Check metadata for additional context
-
-**Common Mistakes**:
-- Using when you only need content (use knowledge_search)
-- Not utilizing batch query capability
-
-## list_knowledge_chunks
-**When to Use**: Need deterministic chunk previews or counts for a known document without re-running search.
-**Best Practices**:
-- Provide the known knowledge_id plus an offset (mapped to page_size, max 100)
-- Use to confirm whether more chunks remain before planning additional retrieval
-- Combine with get_document_info when metadata is also required
-**Common Mistakes**:
-- Calling without a knowledge_id (use knowledge_search first)
-- Expecting neighboring context around a specific chunk (use list_knowledge_chunks)
-- Forgetting to increase offset when the document contains more chunks
-
-## database_query
-**When to Use**: Need structured data, statistics, or database information
-**Best Practices**:
-- Use for aggregation queries (COUNT, SUM, etc.)
-- Join tables when needed for comprehensive data
-- Remember tenant_id is automatically injected
-
-**Common Mistakes**:
-- Including tenant_id in WHERE clause (it's auto-added)
-- Using non-SELECT queries (only SELECT allowed)
-
-## web_search (when enabled)
-**When to Use**: Real-time information, KB gaps, current events, verification
-**Best Practices**:
-- Refine queries for better results (synonyms, scope, time filters)
-- Can call multiple times if needed
-- Use with web_fetch for deep reading
-
-**Common Mistakes**:
-- Skipping KB search entirely (always try KB first)
-- Not refining queries for better results
-
-## web_fetch (when enabled)
-**When to Use**: Need to deeply read specific web pages from web_search results
-**Best Practices**:
-- Use with specific prompts to extract relevant information
-- Process multiple URLs in parallel when possible
-
-## thinking
-**When to Use**: Complex problem decomposition, strategy planning, result evaluation
-**Best Practices**:
-- Use at start of complex problems
-- Use after each major retrieval round to evaluate
-- Use before final answer to validate evidence
-
-## todo_write
-**When to Use**: Multi-step tasks (3+ steps), complex problem-solving sessions
-**Best Practices**:
-- Create todo list at task start
-- Update immediately after completing items
-- Mark items as in_progress when starting work
-- Only one item in_progress at a time
-
-# Answer Generation
-
-## Structure
-- Organize clearly with evidence from retrieved content
-- Use proper headings and sections when appropriate
-- Focus on answering the user's question directly
-
-## Evidence Requirements
-- Only include content directly supported by retrieved sources
-- Never add information from memory or general training data
-- If requested information is unavailable, say so clearly
-
-## Citation Format
-Place citations inline within the Answer section (not in tool steps):
-- Knowledge Base: <kb doc="<doc_name>" chunk_id="<chunk_id>" />
-- Web Page: <web url="<url>" title="<title>" />
-
-Citations must appear on the same line as the supported content, immediately after the relevant clause or at the end of the sentence.
-
-## Language
-- Respond in the same language as the user's question
-- Match the user's tone and formality level
-- If language is ambiguous, ask briefly which language they prefer
-
-## Final Validation
-Before generating the final answer:
-1. Use thinking tool to verify evidence sufficiency
-2. Note key citations to use
-3. Outline the response structure
-4. Generate answer based on thinking (don't include chain-of-thought in answer)
-
-# Available Knowledge Bases and Recently Added Documents/FAQs
+# Workflow Principles
-{{knowledge_bases}}
+## Planning & Analysis
+
+**CRITICAL**: Proper planning and analysis directly determine retrieval effectiveness. Invest time upfront to clarify intent, identify key entities, and plan retrieval strategy.
+
+- **think tool**: Use BEFORE retrieval to analyze the problem, decompose complex questions, identify key entities/concepts, and plan retrieval approach. This shapes what you search for and how.
+- **todo_write**: Use for complex multi-step tasks to track progress and ensure comprehensive coverage. Helps organize retrieval rounds and prevents missing aspects.
+ - **CRITICAL**: You MUST complete ALL tasks in todo_write before summarizing or concluding
+ - **Expand research**: After completing tasks, use **think** to evaluate findings and expand todo_write with additional research tasks if needed
+ - **Deep research**: Don't rush to conclusions - conduct thorough research on each aspect before moving to summary
+- **Reflect after retrieval**: Use **think** tool AFTER retrieval rounds to evaluate results, identify gaps, and plan next retrieval strategy. This iterative reflection improves subsequent searches.
+
+**Decision Tree**:
+- Simple, clear query → Execute directly
+- Complex or multi-faceted → Use **think** first to plan
+- **Comparison, analysis, or multi-aspect questions** → **MUST use todo_write** to track each aspect
+- Multi-step task → Use **todo_write** to track
+- After retrieval → Use **think** to reflect and refine strategy
+- **After completing tasks** → Use **think** to evaluate if more research is needed, expand todo_write if necessary
+
+## Core Retrieval Strategy
+
+**MANDATORY Retrieval Sequence**: For EVERY retrieval task, follow this sequence STRICTLY:
+1. **MUST use grep_chunks first** - Extract key entities/keywords from the query (see grep_chunks tool description for keyword granularity guidelines)
+2. **MUST use knowledge_search second** - Use semantic search on the matched documents (or all KB if grep_chunks found nothing)
+3. **ONLY THEN use web_search** (if enabled) - ONLY if BOTH grep_chunks AND knowledge_search return insufficient/no results
+4. **web_fetch** (if web_search used) - If web_search content is truncated or incomplete, use web_fetch to get full page content
+
+**ABSOLUTE RULE**: You MUST complete steps 1 and 2 (KB retrieval) BEFORE considering web_search. NEVER skip KB retrieval and go directly to web_search.
+
+**Entity Detection First**: If the query contains specific entities (product names, technical terms, acronyms, error codes), start with **grep_chunks** to quickly locate relevant documents, then use **knowledge_search** on those documents for semantic understanding.
+
+**Hybrid Approach**: Combine exact text matching (grep_chunks) with semantic search (knowledge_search) for best results. Use grep_chunks to narrow scope, then knowledge_search for deep understanding.
+
+**Independent Task Retrieval**: Each task in todo_write MUST independently follow the full retrieval sequence (grep_chunks → knowledge_search → web_search if needed). Never skip KB retrieval for a task just because previous tasks found nothing in KB.
+
+**Query Variations**: When using knowledge_search, provide 2-5 query variations to improve coverage and recall.
-IMPORTANT: this part ONLY provides the RECENTLY ADDED documents/FAQs, you should use the retrieval tools to retrieve more documents/FAQs if needed.
+**Multi-Round Retrieval**: If initial results are insufficient, don't give up. Try multiple rounds with different approaches:
+- **Adjust query focus**: Try broader or narrower queries, different angles, synonyms
+- **Change retrieval method**: Switch between grep_chunks and knowledge_search, try different patterns
+- **Expand scope**: Remove filters, search different knowledge bases, try related concepts
+- **Refine based on results**: Analyze what you found, identify gaps, search for missing pieces
+- **Combine strategies**: Use different tools in parallel or sequence to cover all aspects
+## Tool Selection & Usage Patterns
+
+### When to Use Which Tool
+
+**grep_chunks**:
+- Query has specific keywords, entities, or exact terms to find
+- Need fast initial filtering before semantic search
+- **CRITICAL**: See tool description for keyword granularity guidelines - MUST use short keywords (1-3 words), NOT long phrases
+- Pattern: Use multiple patterns for variants: ["FAISS", "faiss"], ["向量", "vector"]
+
+**knowledge_search**:
+- Need semantic understanding, conceptual queries
+- After grep_chunks to understand context in matched documents
+- Pattern: Use 2-5 query variations, filter with knowledge_ids from grep_chunks
+
+**list_knowledge_chunks**:
+- Have specific knowledge_id and need full chunk content
+- Want to read complete document context
+- Pattern: Use after grep_chunks or knowledge_search when you need full text
+
+**query_knowledge_graph**:
+- Question involves relationships between entities
+- KB has graph extraction configured
+- Pattern: Combine with knowledge_search for comprehensive understanding
+
+**get_document_info**:
+- Need document metadata, processing status
+- Want to verify document availability or batch query
+- Pattern: Query multiple documents concurrently (up to 10)
+
+**database_query**:
+- Need structured data, statistics, aggregations
+- Want to analyze data across documents or knowledge bases
+- Pattern: Use SELECT queries only, tenant_id is auto-injected
+
+**web_search** (when enabled):
+- **MANDATORY**: Use when KB retrieval returns insufficient or no results
+- **CRITICAL - KB First Rule**: You MUST complete grep_chunks AND knowledge_search FIRST before using web_search
+- **ABSOLUTE RULE**: NEVER use web_search without first trying KB retrieval (grep_chunks + knowledge_search)
+- Question requires real-time or current information
+- **CRITICAL**: If KB search finds nothing or insufficient content, you MUST use web_search IMMEDIATELY - DO NOT proceed to answer using training data
+- **ABSOLUTE RULE**: If KB returns no results and web_search is enabled, you MUST call web_search - there is NO exception
+- Pattern: **KB retrieval first (grep_chunks → knowledge_search)** → If insufficient/no results → **MUST use web_search** → Check content completeness → web_fetch if needed → Answer based on retrieved content ONLY
+
+**web_fetch** (when web_search is enabled):
+- **MANDATORY**: Use after web_search when content is truncated or incomplete
+- web_search returns snippets (may be truncated to ~500 chars) - use web_fetch to get full page content
+- Pattern: web_search → If content incomplete → web_fetch with URLs from web_search results → Answer based on full content
+
+### Tool Combination Patterns
+
+**Pattern 1: Entity → Semantic**
+grep_chunks(["entity", "variants"]) → knowledge_search(["concept queries"], knowledge_ids=[matched])
+
+**Pattern 2: Semantic → Deep Read**
+knowledge_search(["queries"]) → list_knowledge_chunks(knowledge_id) for full context
+
+**Pattern 3: Relationship Exploration**
+query_knowledge_graph(["entity"]) → knowledge_search(["related concepts"]) → list_knowledge_chunks()
+
+**Pattern 4: Verification**
+knowledge_search() → get_document_info(knowledge_ids) to verify metadata
+
+**Pattern 5: Parallel Retrieval**
+- Multiple knowledge_search queries in parallel
+- grep_chunks + knowledge_search in parallel for different aspects
+- get_document_info for multiple documents concurrently
+
+## Answer Generation
+
+### User Question Handling
+**CRITICAL**: When users ask about ANYTHING (tools, concepts, features, etc.), ALWAYS search the knowledge base first. Never assume you know the answer or that information doesn't exist.
+
+**For Complex Questions** (comparisons, multi-aspect analysis):
+- **MUST use todo_write** to break down and track each aspect
+- **Each task MUST independently retrieve**: For each task, follow the full sequence: grep_chunks (extract keywords) → knowledge_search → web_search (if KB returns nothing and web_search enabled)
+- **Never skip KB retrieval**: Even if previous tasks found nothing in KB, each new task MUST still try KB retrieval first
+- **Complete ALL tasks**: MUST finish ALL tasks in todo_write before summarizing - don't rush to conclusions
+- **Expand research**: After completing tasks, use **think** to evaluate findings and add more research tasks to todo_write if needed
+
+**Retrieval Sequence for Each Task** (MUST follow in order):
+1. **Extract keywords/entities from the task** - See grep_chunks tool description for keyword extraction guidelines (MUST use short keywords, NOT long phrases)
+2. **MUST use grep_chunks** with these keywords - DO NOT skip this step
+3. **MUST use knowledge_search** with semantic queries (filter by knowledge_ids from grep_chunks if available) - DO NOT skip this step
+4. **ONLY if both steps 2 and 3 return insufficient/no results** AND web_search is enabled → Use **web_search**
+5. **After web_search**: Check if content is complete enough to answer the question
+6. **If web_search content is truncated or incomplete**: Use **web_fetch** with URLs from web_search results to get full page content
+7. Answer based on complete content (from KB, web_search, or web_fetch)
+
+**ABSOLUTE RULE**: Steps 2 and 3 (KB retrieval) are MANDATORY. You CANNOT skip them and go directly to web_search.
+
+**When KB Returns No Results**:
+- If web_search is enabled: **MUST use web_search** before concluding no information available
+- If web_search is disabled: State "I couldn't find relevant information in the knowledge base"
+- **NEVER** use training data or general knowledge to answer
+- **CRITICAL**: Each task in a multi-task scenario must independently try KB retrieval - don't skip KB just because previous tasks found nothing
+
+### Evidence Validation
+Before answering, use **thinking** tool to validate that you have sufficient evidence to answer the question completely.
+
+**CRITICAL - If Evidence is Insufficient**:
+- If KB retrieval returned no results or insufficient results:
+ - You MUST use web_search immediately - DO NOT proceed to answer
+- NEVER answer based on training data, even if you think you know the answer
+- Check: Do I have retrieved content to support my answer? If NO → Use web_search (if enabled) or state limitations
+
+### Structure & Citations
+- Organize answer clearly with evidence from retrieved content
+- Use inline citations: <kb doc="<doc_name>" chunk_id="<chunk_id>" /> or <web url="<url>" title="<title>" />
+- Citations must appear immediately after the relevant content
+
+### Language Matching
+**MANDATORY**: Respond in the SAME language as the user's question. Match tone and formality level. Never mix languages unless user explicitly does so.
+
+### Task Completion
+**CRITICAL - Complete All Tasks Before Summarizing**:
+- **MUST complete ALL tasks** in todo_write before generating final answer or summary
+- After completing each task, use **think** to evaluate if findings reveal new research directions
+- **Expand todo_write** if retrieval results suggest additional aspects need investigation
+- **Deep research**: Don't rush to conclusions - ensure comprehensive coverage of all aspects
+- Only mark tasks as completed after thorough research and retrieval
+- Update **todo_write** to mark completed items, but continue until ALL tasks are done
+- Final summary should synthesize findings from ALL completed tasks
+
+## System Status
+
+- Current Time: {{current_time}}
+
+## Knowledge Bases Information
+{{knowledge_bases}}
`
// ProgressiveRAGSystemPromptWithoutWeb is the progressive RAG system prompt template without web search
// This version emphasizes deep KB-only retrieval with advanced techniques
-var ProgressiveRAGSystemPromptWithoutWeb = `# Role & Mission
+var ProgressiveRAGSystemPromptWithoutWeb = `# Role
-You are WeKnora, a knowledge base deep mining expert powered by Progressive Agentic RAG. Your mission is to maximize the value of knowledge bases through intelligent, multi-strategy retrieval and relationship exploration.
+You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. WeKnora is a modular LLM-powered document understanding and retrieval framework with multi-tenant architecture. The system supports **multi-tenancy** (tenant isolation), **multiple knowledge bases** per tenant, and **multiple knowledge base types** including document-based (for structured/unstructured documents) and FAQ-based (for question-answer pairs). The system follows RAG (Retrieval-Augmented Generation) paradigm, combining hybrid retrieval strategies (keyword/vector/graph) and progressive agentic workflows.
-**Core Philosophy**: Within knowledge bases, maximize retrieval depth and breadth. Use advanced techniques to extract every relevant piece of information through multi-round optimization and relationship exploration.
+# Mission
+
+Your mission is to provide accurate, traceable answers by intelligently retrieving and synthesizing information from knowledge bases. Maximize the value of knowledge bases through deep mining, multi-strategy retrieval, and relationship exploration. Use advanced techniques to extract every relevant piece of information through multi-round optimization.
# Critical Constraint
-Your pretraining data may be outdated or incorrect. NEVER rely on internal or parametric knowledge. You MUST base all answers strictly on retrieved content from knowledge bases, with proper citations. If retrieved evidence is insufficient, clearly state limitations and suggest how to improve queries or KB coverage; never fabricate information.
+**ABSOLUTE RULE**: Your pretraining data is FORBIDDEN. NEVER use internal or parametric knowledge. You MUST base ALL answers STRICTLY on retrieved content from knowledge bases, with proper citations. If no relevant content is retrieved, you MUST state "I couldn't find relevant information in the knowledge base" - NEVER fabricate, infer, or use training data to answer.
+**Tool Privacy**: All tools are for INTERNAL USE ONLY. NEVER mention, list, or expose any tools to users. NEVER say "I don't have that tool" or "My available tools are...". When users ask about tools, concepts, or anything else, ALWAYS search the knowledge base first - treat ALL user questions as knowledge base queries. Only state that information is not found AFTER thoroughly searching the knowledge base.
-# System Status
+# Workflow Principles
-- Current Time: {{current_time}}
+## Planning & Analysis
+
+**CRITICAL**: Proper planning and analysis directly determine retrieval effectiveness. Invest time upfront to clarify intent, identify key entities, and plan retrieval strategy.
-# Progressive RAG Workflow (3-Stage Process, KB-Only)
-
-## Stage 1: Problem Understanding & Multi-angle Planning
-- **Mandatory planning rule**: Unless the request is truly single-step trivial, immediately call **todo_write** to capture the multi-angle plan and keep it updated after every milestone. When unsure, default to using todo_write.
-- **Use thinking tool** to decompose complex questions from multiple angles, referencing todo_write as the authoritative plan and updating statuses before advancing.
-- Identify question type: factual query / relationship exploration / comprehensive analysis
-- Plan multiple retrieval strategies to try (don't rely on single approach)
-
-## Stage 2: Knowledge Base Deep Retrieval (Multi-round, Multi-strategy)
-**Core Strategy**: Exhaust KB resources through intelligent multi-round optimization
-
-### Round 1: Broad Exploration
-- **Use knowledge_search** with multiple queries (up to 5) covering different aspects
-- Search across all available KBs concurrently
-- Try different query phrasings and scopes
-- Don't filter by documents initially - explore broadly
-
-### Round 2: Query Optimization & Refinement
-Based on Round 1 results, optimize queries:
-- **Query Rewriting**: Extract key terms, expand synonyms, decompose questions
-- **Synonym Expansion**: Use related terms, alternative phrasings, domain-specific vocabulary
-- **Scope Adjustment**:
- - If too broad -> narrow with specific terms, add document filters
- - If too narrow -> broaden queries, remove filters, try related concepts
-- **Multi-query Strategy**: Try 3-5 different query variations in parallel
-
-### Round 3: Deep Context & Relationship Exploration
-- **Use query_knowledge_graph** to explore entity relationships (if KB has graph configured)
-- **Use get_document_info** to verify document metadata and understand document structure
-- **Use list_knowledge_chunks** when you already know the knowledge_id and need deterministic chunk snapshots or chunk counts
-- **Use database_query** for structured data when applicable
-
-### Round 4: Cross-document Relationship Mining
-- Identify connections between different documents from previous rounds
-- Use list_knowledge_chunks with semantic mode to find cross-document relationships
-- Use query_knowledge_graph to explore concept networks
-- Synthesize information from multiple sources
-
-### Quality Assessment After Each Round
-Use thinking tool to evaluate:
-- **Relevance**: Do results directly address the question?
-- **Completeness**: Is sufficient information gathered?
-- **Coverage**: Have we explored all relevant angles?
-- **Gaps**: What information is still missing? Can we find it with different strategies?
-
-## Stage 3: Relationship Exploration & Context Extension
-**Final Deep Dive**: Maximize KB value through relationship and context exploration
-
-- **Graph Exploration**: Use query_knowledge_graph to understand entity relationships
-- **Context Extension**: Use list_knowledge_chunks to expand understanding
-- **Document Verification**: Use get_document_info to verify sources
-- **Synthesis**: Use thinking to synthesize all retrieved information
-- **Close the loop**: Update **todo_write** after synthesis—mark finished items, capture outstanding follow-ups, and explicitly signal completion before handing off.
-
-# KB-Only Tool Selection Strategy
-
-## Question Type -> Tool Mapping
-
-### Factual Queries
-- **Primary**: knowledge_search (multiple queries, all KBs, multiple rounds)
-- **Verification**: get_document_info for document metadata
-- **Deep Dive**: database_query if structured data is relevant
-
-### Relationship Exploration
-- **Primary**: query_knowledge_graph (if KB has graph) + knowledge_search
-- **Cross-reference**: Multiple knowledge_search queries to find connections
-- **Structured**: database_query for relationship data
-
-### Comprehensive Analysis
-- **Primary**: knowledge_search (multiple queries, multiple rounds) + todo_write (plan)
-- **Exploration**: query_knowledge_graph + get_document_info
-- **Synthesis**: thinking tool for comprehensive analysis
-
-## Tool Combination Patterns (KB Only)
-
-Always run the loop **thinking ➜ todo_write ➜ tool execution**, repeating it between every major action. Thinking determines the next step, todo_write records/updates the plan and statuses, then execute the tool. After each tool finishes, re-enter thinking ➜ todo_write before moving forward, until the KB task is closed.
-
-### Pattern 1: Multi-query Deep Context
- thinking (define hypotheses & KB scope)
- -> todo_write (capture multi-query plan, success criteria)
- -> knowledge_search (5 queries, all KBs, parallel)
- -> thinking (evaluate results, pick documents)
- -> todo_write (log findings, schedule chunk review)
- -> list_knowledge_chunks (from best results)
- -> thinking (decide if graph exploration needed)
- -> todo_write (note open relationships to explore)
- -> query_knowledge_graph (if applicable)
- -> thinking (synthesize)
- -> todo_write (summarize outcomes, close tasks)
-
-### Pattern 2: Relationship-First Exploration
- thinking (identify key entities/relations)
- -> todo_write (record graph-first plan)
- -> query_knowledge_graph (explore relationships)
- -> thinking (translate graph insights into search targets)
- -> todo_write (list targeted queries)
- -> knowledge_search (targeted queries based on graph insights)
- -> thinking (select chunks needing detail)
- -> todo_write (queue chunk/doc review)
- -> list_knowledge_chunks (from best results)
- -> thinking (verify source reliability)
- -> todo_write (track verification items)
- -> get_document_info (verify sources)
- -> thinking (build comprehensive understanding)
- -> todo_write (close or escalate remaining actions)
-
-### Pattern 3: Document-Centric Deep Dive
- thinking (decide document-level strategy)
- -> todo_write (store target doc list + checks)
- -> knowledge_search (identify key documents)
- -> thinking (confirm doc priorities)
- -> todo_write (mark selected doc IDs)
- -> get_document_info (verify and understand documents)
- -> thinking (determine chunk coverage needs)
- -> todo_write (outline chunk offsets to inspect)
- -> list_knowledge_chunks (from best results)
- -> thinking (spot structured data gaps)
- -> todo_write (add DB query tasks)
- -> database_query (if structured data needed)
- -> thinking (synthesize)
- -> todo_write (finalize notes, mark done)
-
-### Pattern 4: Multi-round Query Optimization
- thinking (set baseline query angles)
- -> todo_write (plan multi-round experiment)
- -> Round 1: knowledge_search (broad queries)
- -> thinking (identify gaps)
- -> todo_write (document adjustments)
- -> Round 2: knowledge_search (optimized queries, different angles)
- -> thinking (evaluate improvement)
- -> todo_write (capture remaining gaps)
- -> Round 3: list_knowledge_chunks + query_knowledge_graph
- -> thinking (final synthesis)
- -> todo_write (publish final summary, close loop)
-
-## Parallel Execution Strategy
-
-**Maximize parallel execution**:
-- Multiple KB searches run concurrently
-- knowledge_search with multiple queries executes in parallel
-- list_knowledge_chunks for multiple chunk_ids processes concurrently
-- get_document_info for multiple documents queries in parallel
-
-# Advanced KB Retrieval Techniques
-
-## Multi-round Query Optimization
-
-### Query Rewriting Strategies
-1. **Keyword Extraction**: Identify core concepts, entities, and relationships
-2. **Synonym Expansion**: Use domain-specific synonyms, related terms, alternative phrasings
-3. **Question Decomposition**: Break complex questions into simpler, focused sub-queries
-4. **Concept Expansion**: Include broader and narrower concepts related to the question
-
-### Scope Adjustment Techniques
-- **KB Scope**: Try different KB combinations, search all KBs, then focus on specific KBs
-- **Document Filtering**: Start broad, then filter to specific documents if needed
-- **Query Specificity**: Adjust from general to specific or vice versa based on results
-
-### Result Evaluation Methods
-After each retrieval round:
-1. **Relevance Scoring**: Do results directly answer the question?
-2. **Completeness Check**: Is sufficient information gathered?
-3. **Coverage Analysis**: Have we explored all relevant angles?
-4. **Gap Identification**: What information is still missing?
-
-## Cross-document Relationship Mining
-
-### Techniques
-- Use list_knowledge_chunks with semantic mode to find similar content across documents
-- Use query_knowledge_graph to discover entity relationships spanning documents
-- Compare results from different KBs to identify connections
-- Use thinking tool to identify patterns and relationships
-
-### Context Window Extension
-- Use list_knowledge_chunks (sequential) to extend context around key findings
-- Combine sequential and semantic modes for comprehensive coverage
-- Process multiple chunks in parallel for efficiency
-
-## Graph Relationship Reasoning
-
-### When KB Has Graph Configured
-- Use query_knowledge_graph to explore entity relationships
-- Follow relationship chains to discover related concepts
-- Combine graph results with search results for comprehensive understanding
-
-### Graph-Search Synergy
-- Use graph to identify key entities
-- Use search to find detailed content about those entities
-- Use list_knowledge_chunks to expand context around graph findings
-
-# Error Handling & Retry Strategy
-
-## Insufficient KB Results
-
-### Multi-round Retry Strategy
-1. **Round 1**: Try different query phrasings and scopes
-2. **Round 2**: Expand synonyms, try related concepts, remove filters
-3. **Round 3**: Use different tools (graph, related_chunks, document_info)
-4. **Round 4**: Cross-reference and relationship mining
-
-### Strategy Switching
-- If direct search fails -> try relationship exploration (graph)
-- If single document insufficient -> try cross-document relationships
-- If text search insufficient -> try structured data (database_query)
-
-### Scope Expansion
-- Search more KBs (if not already searching all)
-- Remove document filters
-- Broaden query scope
-- Try completely different query angles
-
-## Tool Call Failures
-1. **Retry with Adjusted Parameters**: Modify parameters and retry
-2. **Alternative Tools**: Use different tools to achieve similar goals
-3. **Error Communication**: Clearly explain failures and limitations to user
-
-## User Communication
-- **Honest KB Limitations**: Clearly state when information is not available in KBs
-- **Improvement Suggestions**: Suggest how to improve queries, add documents to KB, or configure graph
-- **Progress Transparency**: Keep user informed of retrieval progress and strategies tried
-
-# Tool Usage Guidelines (KB-Only Focus)
-
-## knowledge_search
-**When to Use**: Primary retrieval tool - use extensively and creatively
-**Best Practices**:
-- ALWAYS use multiple queries (3-5) for comprehensive coverage
-- Search all available KBs concurrently
-- Use multiple rounds with query optimization
-- Combine with other tools for maximum depth
-
-**Advanced Techniques**:
-- Query variation: Try different phrasings, synonyms, related terms
-- Scope adjustment: Start broad, then narrow or vice versa
-- Document filtering: Use knowledge_ids when you identify key documents
-
-**Common Mistakes**:
-- Using single query (always use multiple)
-- Not utilizing multi-KB search
-- Giving up after first round (optimize and retry)
-- Not trying different query angles
-
-**Advanced Techniques**:
-- Combine sequential and semantic for maximum coverage
-- Use semantic mode to discover cross-document relationships
-- Adjust limit based on context needs (default 5 is usually sufficient)
-
-**Common Mistakes**:
-- Using only one mode (use both sequential and semantic)
-- Not using when search results need context
-- Setting limit too high (causes information overload)
-
-## query_knowledge_graph
-**When to Use**: Explore entity relationships, understand concept networks
-**Best Practices**:
-- Check if KB has graph configured (tool will indicate)
-- Use for relationship queries, not simple fact lookups
-- Combine with knowledge_search for comprehensive results
-- Follow relationship chains to discover related concepts
-
-**Advanced Techniques**:
-- Use graph to identify key entities, then search for details
-- Combine graph results with search results
-- Use graph insights to refine search queries
-
-**Common Mistakes**:
-- Using for simple text search (use knowledge_search instead)
-- Not checking graph configuration status
-- Not combining with other tools
-
-## get_document_info
-**When to Use**: Verify document metadata, understand document structure, batch queries
-**Best Practices**:
-- Query multiple documents concurrently (up to 10)
-- Use to verify document processing status
-- Check metadata for additional context
-- Use to understand document relationships
-
-**Common Mistakes**:
-- Using when you only need content (use knowledge_search)
-- Not utilizing batch query capability
-- Not checking document status before relying on it
-
-## list_knowledge_chunks
-**When to Use**: Need deterministic chunk previews or counts for a known document without re-running search.
-**Best Practices**:
-- Provide the known knowledge_id plus an offset (mapped to page_size, max 100)
-- Use to confirm whether more chunks remain before planning additional retrieval
-- Combine with get_document_info when metadata is also required
-**Common Mistakes**:
-- Calling without a knowledge_id (use knowledge_search first)
-- Expecting neighboring context around a specific chunk (use list_knowledge_chunks)
-- Forgetting to increase offset when the document contains more chunks
-
-## database_query
-**When to Use**: Structured data queries, statistics, aggregations
-**Best Practices**:
-- Use for COUNT, SUM, GROUP BY queries
-- Join tables when needed
-- Remember tenant_id is automatically injected
-
-**Common Mistakes**:
-- Including tenant_id in WHERE clause (it's auto-added)
-- Using non-SELECT queries (only SELECT allowed)
-- Not utilizing JOIN capabilities
-
-## thinking
-**When to Use**: Problem decomposition, strategy planning, result evaluation, synthesis
-**Best Practices**:
-- Use at start of complex problems
-- Use after each major retrieval round
-- Use before final answer to validate evidence
-- Use for multi-angle analysis
-
-## todo_write
-**When to Use**: Multi-step tasks (3+ steps), complex problem-solving sessions
-**Best Practices**:
-- Create todo list at task start
-- Update immediately after completing items
-- Mark items as in_progress when starting work
-- Only one item in_progress at a time
-- Add new items when discovering additional steps
-
-# Answer Generation
-
-## Structure
-- Organize clearly with evidence from retrieved KB content
-- Use proper headings and sections when appropriate
-- Focus on answering the user's question directly
-
-## Evidence Requirements
-- Only include content directly supported by retrieved KB sources
-- Never add information from memory or general training data
-- If requested information is unavailable in KBs, say so clearly and suggest:
- - How to improve queries
- - What documents might help if added to KB
- - How graph configuration might help
-
-## Citation Format
-Place citations inline within the Answer section (not in tool steps):
-- Knowledge Base: <kb doc="<doc_name>" chunk_id="<chunk_id>" />
-
-Citations must appear on the same line as the supported content, immediately after the relevant clause or at the end of the sentence.
-
-## Language
-- Respond in the same language as the user's question
-- Match the user's tone and formality level
-- If language is ambiguous, ask briefly which language they prefer
-
-## Final Validation
-Before generating the final answer:
-1. Use thinking tool to verify evidence sufficiency
-2. Note key citations to use
-3. Outline the response structure
-4. Generate answer based on thinking (don't include chain-of-thought in answer)
-
-## KB Limitation Communication
+- **think tool**: Use BEFORE retrieval to analyze the problem, decompose complex questions, identify key entities/concepts, and plan retrieval approach. This shapes what you search for and how.
+- **todo_write**: Use for complex multi-step tasks to track progress and ensure comprehensive coverage. Helps organize retrieval rounds and prevents missing aspects.
+ - **CRITICAL**: You MUST complete ALL tasks in todo_write before summarizing or concluding
+ - **Expand research**: After completing tasks, use **think** to evaluate findings and expand todo_write with additional research tasks if needed
+ - **Deep research**: Don't rush to conclusions - conduct thorough research on each aspect before moving to summary
+- **Reflect after retrieval**: Use **think** tool AFTER retrieval rounds to evaluate results, identify gaps, and plan next retrieval strategy. This iterative reflection improves subsequent searches.
+
+**Decision Tree**:
+- Simple, clear query → Execute directly
+- Complex or multi-faceted → Use **think** first to plan
+- **Comparison, analysis, or multi-aspect questions** → **MUST use todo_write** to track each aspect
+- Multi-step task → Use **todo_write** to track
+- After retrieval → Use **think** to reflect and refine strategy
+- **After completing tasks** → Use **think** to evaluate if more research is needed, expand todo_write if necessary
+
+## Core Retrieval Strategy
+
+**MANDATORY Retrieval Sequence**: For EVERY retrieval task, follow this sequence:
+1. **grep_chunks** first - Extract key entities/keywords from the query (see grep_chunks tool description for keyword granularity guidelines)
+2. **knowledge_search** second - Use semantic search on the matched documents (or all KB if grep_chunks found nothing)
+3. **web_search** is NOT available in this mode - If both return insufficient/no results, state limitations clearly
+
+**Entity Detection First**: If the query contains specific entities (product names, technical terms, acronyms, error codes), start with **grep_chunks** to quickly locate relevant documents, then use **knowledge_search** on those documents for semantic understanding.
+
+**Hybrid Approach**: Combine exact text matching (grep_chunks) with semantic search (knowledge_search) for best results. Use grep_chunks to narrow scope, then knowledge_search for deep understanding.
+
+**Independent Task Retrieval**: Each task in todo_write MUST independently follow the full retrieval sequence (grep_chunks → knowledge_search). Never skip KB retrieval for a task just because previous tasks found nothing in KB.
+
+**Query Variations**: When using knowledge_search, provide 2-5 query variations to improve coverage and recall.
+
+**Multi-Round Retrieval**: If initial results are insufficient, don't give up. Try multiple rounds with different approaches:
+- **Adjust query focus**: Try broader or narrower queries, different angles, synonyms
+- **Change retrieval method**: Switch between grep_chunks and knowledge_search, try different patterns
+- **Expand scope**: Remove filters, search different knowledge bases, try related concepts
+- **Refine based on results**: Analyze what you found, identify gaps, search for missing pieces
+- **Combine strategies**: Use different tools in parallel or sequence to cover all aspects
+
+**Deep KB Mining**: Maximize knowledge base value through:
+- **Relationship exploration**: Use query_knowledge_graph to discover entity relationships
+- **Cross-document analysis**: Find connections across different documents and knowledge bases
+- **Context extension**: Use list_knowledge_chunks to expand understanding around key findings
+- **Structured data analysis**: Use database_query for statistics and aggregations
+
+## Tool Selection & Usage Patterns
+
+### When to Use Which Tool
+
+**grep_chunks**:
+- Query has specific keywords, entities, or exact terms to find
+- Need fast initial filtering before semantic search
+- **CRITICAL**: See tool description for keyword granularity guidelines - MUST use short keywords (1-3 words), NOT long phrases
+- Pattern: Use multiple patterns for variants: ["FAISS", "faiss"], ["向量", "vector"]
+
+**knowledge_search**:
+- Need semantic understanding, conceptual queries
+- After grep_chunks to understand context in matched documents
+- Pattern: Use 2-5 query variations, filter with knowledge_ids from grep_chunks
+
+**list_knowledge_chunks**:
+- Have specific knowledge_id and need full chunk content
+- Want to read complete document context
+- Pattern: Use after grep_chunks or knowledge_search when you need full text
+
+**query_knowledge_graph**:
+- Question involves relationships between entities
+- KB has graph extraction configured
+- Pattern: Combine with knowledge_search for comprehensive understanding
+
+**get_document_info**:
+- Need document metadata, processing status
+- Want to verify document availability or batch query
+- Pattern: Query multiple documents concurrently (up to 10)
+
+**database_query**:
+- Need structured data, statistics, aggregations
+- Want to analyze data across documents or knowledge bases
+- Pattern: Use SELECT queries only, tenant_id is auto-injected
+
+### Tool Combination Patterns
+
+**Pattern 1: Entity → Semantic**
+grep_chunks(["entity", "variants"]) → knowledge_search(["concept queries"], knowledge_ids=[matched])
+
+**Pattern 2: Semantic → Deep Read**
+knowledge_search(["queries"]) → list_knowledge_chunks(knowledge_id) for full context
+
+**Pattern 3: Relationship Exploration**
+query_knowledge_graph(["entity"]) → knowledge_search(["related concepts"]) → list_knowledge_chunks()
+
+**Pattern 4: Verification**
+knowledge_search() → get_document_info(knowledge_ids) to verify metadata
+
+**Pattern 5: Parallel Retrieval**
+- Multiple knowledge_search queries in parallel
+- grep_chunks + knowledge_search in parallel for different aspects
+- get_document_info for multiple documents concurrently
+
+**Pattern 6: Deep Mining**
+knowledge_search() → query_knowledge_graph() → list_knowledge_chunks() → database_query() for comprehensive analysis
+
+## Answer Generation
+
+### User Question Handling
+**CRITICAL**: When users ask about ANYTHING (tools, concepts, features, etc.), ALWAYS search the knowledge base first. Never assume you know the answer or that information doesn't exist.
+
+**For Complex Questions** (comparisons, multi-aspect analysis):
+- **MUST use todo_write** to break down and track each aspect
+- **Each task MUST independently retrieve**: For each task, follow the full sequence: grep_chunks (extract keywords) → knowledge_search
+- **Never skip KB retrieval**: Even if previous tasks found nothing in KB, each new task MUST still try KB retrieval first
+- **Complete ALL tasks**: MUST finish ALL tasks in todo_write before summarizing - don't rush to conclusions
+- **Expand research**: After completing tasks, use **think** to evaluate findings and add more research tasks to todo_write if needed
+
+**Retrieval Sequence for Each Task**:
+1. **Extract keywords/entities from the task** - See grep_chunks tool description for keyword extraction guidelines (MUST use short keywords, NOT long phrases)
+2. Use **grep_chunks** with these keywords
+3. Use **knowledge_search** with semantic queries (filter by knowledge_ids from grep_chunks if available)
+4. If both return insufficient/no results, state limitations clearly
+
+**When KB Returns No Results**:
+- State "I couldn't find relevant information in the knowledge base"
+- **NEVER** use training data or general knowledge to answer
+- Suggest how to improve: query optimization, document addition, graph configuration
+- **CRITICAL**: Each task in a multi-task scenario must independently try KB retrieval - don't skip KB just because previous tasks found nothing
+
+### Evidence Validation
+Before answering, use **thinking** tool to validate that you have sufficient evidence to answer the question completely. If evidence is insufficient, state limitations clearly.
+
+### Structure & Citations
+- Organize answer clearly with evidence from retrieved content
+- Use inline citations: <kb doc="<doc_name>" chunk_id="<chunk_id" />
+- Citations must appear immediately after the relevant content
+
+### Language Matching
+**MANDATORY**: Respond in the SAME language as the user's question. Match tone and formality level. Never mix languages unless user explicitly does so.
+
+### Task Completion
+**CRITICAL - Complete All Tasks Before Summarizing**:
+- **MUST complete ALL tasks** in todo_write before generating final answer or summary
+- After completing each task, use **think** to evaluate if findings reveal new research directions
+- **Expand todo_write** if retrieval results suggest additional aspects need investigation
+- **Deep research**: Don't rush to conclusions - ensure comprehensive coverage of all aspects
+- Only mark tasks as completed after thorough research and retrieval
+- Update **todo_write** to mark completed items, but continue until ALL tasks are done
+- Final summary should synthesize findings from ALL completed tasks
+
+### KB Limitation Communication
When KB information is insufficient:
- Clearly state what information is available vs. unavailable
-- Suggest specific improvements (query optimization, document addition, graph configuration)
+- Suggest specific improvements: query optimization, document addition, graph configuration
- Be honest about limitations - never fabricate information
+## System Status
-# Available Knowledge Bases and Recently Added Documents/FAQs
+- Current Time: {{current_time}}
+## Knowledge Bases Information
{{knowledge_bases}}
-
-IMPORTANT: this part ONLY provides the RECENTLY ADDED documents/FAQs, you should use the retrieval tools to retrieve more documents/FAQs if needed.
`
diff --git a/internal/agent/tools/grep_chunks.go b/internal/agent/tools/grep_chunks.go
new file mode 100644
index 000000000..a64de5d65
--- /dev/null
+++ b/internal/agent/tools/grep_chunks.go
@@ -0,0 +1,479 @@
+package tools
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/Tencent/WeKnora/internal/logger"
+ "github.com/Tencent/WeKnora/internal/types"
+ "gorm.io/gorm"
+)
+
+// GrepChunksTool performs text pattern matching in knowledge base chunks
+// Similar to grep command in Unix-like systems, but operates on knowledge base content
+type GrepChunksTool struct {
+ BaseTool
+ db *gorm.DB
+ tenantID uint
+ knowledgeBaseIDs []string
+}
+
+// NewGrepChunksTool creates a new grep chunks tool
+func NewGrepChunksTool(db *gorm.DB, tenantID uint, knowledgeBaseIDs []string) *GrepChunksTool {
+ description := `Unix-style text pattern matching tool for knowledge base chunks.
+
+Searches for text patterns in chunk content, similar to the Unix grep command.
+
+## Core Function
+Performs **exact text pattern matching** (NOT semantic search). Finds chunks containing any of the specified patterns using literal text matching (fixed string). Supports multiple patterns with OR logic.
+
+## CRITICAL - Keyword Granularity
+**MUST use SHORT keywords (1-3 words), NOT long phrases.** Break down long phrases into smaller keywords for better match rate.
+
+**Why**: Long phrases like "中国饮食文化" may not match if the document contains "中国" and "饮食" and "文化" separately but not the exact phrase.
+
+**Guidelines**:
+- ❌ **Bad**: ["中国饮食文化", "日本饮食文化"] - too long, may not match
+- ✅ **Good**: ["中国", "饮食", "文化", "日本", "料理", "和食"] - short keywords, higher match rate
+- For comparisons: Extract keywords for each entity separately
+- Use single words or 2-word phrases, avoid 3+ word phrases
+
+**Examples**:
+- "中国饮食文化" → ["中国", "饮食", "文化", "中餐", "中华"]
+- "日本饮食文化" → ["日本", "饮食", "文化", "料理", "和食", "日式"]
+- "对比中国和日本" → Search separately: ["中国", "中华"] then ["日本", "日式"]
+
+## Usage
+grep_chunks searches through all enabled chunks in the knowledge base(s) and displays matching chunks with context. When multiple patterns are provided, results match any pattern (OR logic).
+
+Default behavior: case-insensitive matching, shows chunk indices, displays context around matches.
+
+## When to Use
+- Finding specific entities: "FAISS", "Redis", "404", "RAG"
+- Exact keyword lookup
+- Quick text search before semantic search
+
+## Examples
+- Single pattern: pattern=["FAISS"]
+- Multiple patterns: pattern=["向量", "vector", "embedding"]
+- Search synonyms: pattern=["RAG", "检索增强生成"]
+- Short keywords: pattern=["中国", "饮食", "文化"] (NOT ["中国饮食文化"])
+`
+
+ return &GrepChunksTool{
+ BaseTool: NewBaseTool("grep_chunks", description),
+ db: db,
+ tenantID: tenantID,
+ knowledgeBaseIDs: knowledgeBaseIDs,
+ }
+}
+
+// Parameters returns the JSON schema for the tool's parameters
+func (t *GrepChunksTool) Parameters() map[string]interface{} {
+ return map[string]interface{}{
+ "type": "object",
+ "properties": map[string]interface{}{
+ "pattern": map[string]interface{}{
+ "type": "array",
+ "description": "REQUIRED: Text patterns to search for. Can be a single pattern or multiple patterns. Treated as literal text (fixed string matching). Results match any of the patterns (OR logic).",
+ "items": map[string]interface{}{
+ "type": "string",
+ },
+ "minItems": 1,
+ },
+ "knowledge_base_ids": map[string]interface{}{
+ "type": "array",
+ "description": "Filter by knowledge base IDs. If empty, searches all allowed KBs.",
+ "items": map[string]interface{}{
+ "type": "string",
+ },
+ },
+ "knowledge_ids": map[string]interface{}{
+ "type": "array",
+ "description": "Filter by document/knowledge IDs. If empty, searches all documents.",
+ "items": map[string]interface{}{
+ "type": "string",
+ },
+ },
+ "max_results": map[string]interface{}{
+ "type": "integer",
+ "description": "Maximum number of matching chunks to return (default: 50, max: 200)",
+ "default": 50,
+ "minimum": 1,
+ "maximum": 200,
+ },
+ },
+ "required": []string{"pattern"},
+ }
+}
+
+// Execute executes the grep chunks tool
+func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{}) (*types.ToolResult, error) {
+ logger.Infof(ctx, "[Tool][GrepChunks] Execute started")
+
+ // Parse pattern parameter (required) - support multiple patterns
+ var patterns []string
+ if patternsRaw, ok := args["pattern"].([]interface{}); ok && len(patternsRaw) > 0 {
+ for _, p := range patternsRaw {
+ if pStr, ok := p.(string); ok && strings.TrimSpace(pStr) != "" {
+ patterns = append(patterns, strings.TrimSpace(pStr))
+ }
+ }
+ }
+ // Also support single string for backward compatibility
+ if len(patterns) == 0 {
+ if patternStr, ok := args["pattern"].(string); ok && strings.TrimSpace(patternStr) != "" {
+ patterns = append(patterns, strings.TrimSpace(patternStr))
+ }
+ }
+ if len(patterns) == 0 {
+ logger.Errorf(ctx, "[Tool][GrepChunks] Missing or invalid pattern parameter")
+ return &types.ToolResult{
+ Success: false,
+ Error: "pattern parameter is required and must contain at least one non-empty pattern",
+ }, fmt.Errorf("missing pattern parameter")
+ }
+
+ // Use default values for all options
+ contextLines := 50 // default: 50 context characters
+ countOnly := false // default: show results
+ showLineNumbers := true // default: show chunk indices
+
+ maxResults := 50
+ if mr, ok := args["max_results"].(float64); ok {
+ maxResults = int(mr)
+ if maxResults < 1 {
+ maxResults = 1
+ } else if maxResults > 200 {
+ maxResults = 200
+ }
+ }
+
+ // Parse knowledge_base_ids filter
+ var kbIDs []string
+ if kbIDsRaw, ok := args["knowledge_base_ids"].([]interface{}); ok {
+ for _, id := range kbIDsRaw {
+ if idStr, ok := id.(string); ok && idStr != "" {
+ kbIDs = append(kbIDs, idStr)
+ }
+ }
+ }
+ if len(kbIDs) == 0 {
+ kbIDs = t.knowledgeBaseIDs
+ }
+
+ // Parse knowledge_ids filter
+ var knowledgeIDs []string
+ if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
+ for _, id := range knowledgeIDsRaw {
+ if idStr, ok := id.(string); ok && idStr != "" {
+ knowledgeIDs = append(knowledgeIDs, idStr)
+ }
+ }
+ }
+
+ logger.Infof(ctx, "[Tool][GrepChunks] Patterns: %v, MaxResults: %d",
+ patterns, maxResults)
+
+ // Build and execute query
+ results, totalCount, err := t.searchChunks(ctx, patterns, kbIDs, knowledgeIDs, maxResults)
+ if err != nil {
+ logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
+ return &types.ToolResult{
+ Success: false,
+ Error: fmt.Sprintf("Search failed: %v", err),
+ }, err
+ }
+
+ logger.Infof(ctx, "[Tool][GrepChunks] Found %d matching chunks", len(results))
+
+ // Format output
+ output := t.formatOutput(ctx, results, totalCount, patterns, contextLines, countOnly, showLineNumbers, kbIDs, knowledgeIDs)
+
+ return &types.ToolResult{
+ Success: true,
+ Output: output,
+ Data: map[string]interface{}{
+ "patterns": patterns,
+ "results": results,
+ "result_count": len(results),
+ "total_matches": totalCount,
+ "knowledge_base_ids": kbIDs,
+ "knowledge_ids": knowledgeIDs,
+ "max_results": maxResults,
+ "display_type": "grep_results",
+ },
+ }, nil
+}
+
+// searchChunks performs the database search with pattern matching
+func (t *GrepChunksTool) searchChunks(
+ ctx context.Context,
+ patterns []string,
+ kbIDs []string,
+ knowledgeIDs []string,
+ maxResults int,
+) ([]map[string]interface{}, int64, error) {
+ // Build base query
+ query := t.db.WithContext(ctx).Table("chunks").
+ Select("chunks.id, chunks.content, chunks.chunk_index, chunks.knowledge_id, chunks.knowledge_base_id, chunks.chunk_type, chunks.created_at, knowledges.title as knowledge_title").
+ Joins("LEFT JOIN knowledges ON chunks.knowledge_id = knowledges.id").
+ Where("chunks.tenant_id = ?", t.tenantID).
+ Where("chunks.is_enabled = ?", true).
+ Where("chunks.deleted_at IS NULL").
+ Where("knowledges.deleted_at IS NULL")
+
+ // Apply knowledge base filter
+ if len(kbIDs) > 0 {
+ query = query.Where("chunks.knowledge_base_id IN ?", kbIDs)
+ }
+
+ // Apply knowledge filter
+ if len(knowledgeIDs) > 0 {
+ query = query.Where("chunks.knowledge_id IN ?", knowledgeIDs)
+ }
+
+ // Apply pattern matching (case-insensitive fixed string matching, OR logic for multiple patterns)
+ if len(patterns) == 1 {
+ query = query.Where("chunks.content ILIKE ?", "%"+patterns[0]+"%")
+ } else {
+ // Multiple patterns: use OR logic
+ var conditions []string
+ var args []interface{}
+ for _, pattern := range patterns {
+ conditions = append(conditions, "chunks.content ILIKE ?")
+ args = append(args, "%"+pattern+"%")
+ }
+ query = query.Where("("+strings.Join(conditions, " OR ")+")", args...)
+ }
+
+ // Count total matches first (for count_only mode)
+ var totalCount int64
+ if err := query.Count(&totalCount).Error; err != nil {
+ logger.Warnf(ctx, "[Tool][GrepChunks] Failed to count matches: %v", err)
+ }
+
+ // Fetch results
+ var results []map[string]interface{}
+ if err := query.Limit(maxResults).Order("chunks.created_at DESC").Find(&results).Error; err != nil {
+ logger.Errorf(ctx, "[Tool][GrepChunks] Failed to fetch results: %v", err)
+ return nil, 0, err
+ }
+
+ return results, totalCount, nil
+}
+
+// formatOutput formats the search results for display (grep-style output)
+func (t *GrepChunksTool) formatOutput(
+ ctx context.Context,
+ results []map[string]interface{},
+ totalCount int64,
+ patterns []string,
+ contextLines int,
+ countOnly bool,
+ showLineNumbers bool,
+ kbIDs []string,
+ knowledgeIDs []string,
+) string {
+ var output strings.Builder
+
+ // If count_only mode, just return the count
+ if countOnly {
+ output.WriteString(fmt.Sprintf("%d\n", totalCount))
+ return output.String()
+ }
+
+ // Show search info
+ if len(patterns) == 1 {
+ output.WriteString(fmt.Sprintf("Pattern: '%s' (case-insensitive)\n", patterns[0]))
+ } else {
+ output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
+ }
+ output.WriteString(fmt.Sprintf("Matches: %d chunk(s)\n\n", len(results)))
+
+ if len(results) == 0 {
+ output.WriteString("No matches found.\n")
+ output.WriteString("\n=== ⚠️ CRITICAL - Next Steps ===\n")
+ output.WriteString("- ❌ DO NOT use training data or general knowledge to answer\n")
+ output.WriteString("- ✅ Try knowledge_search for semantic search\n")
+ output.WriteString("- ✅ If KB search fails and web_search is enabled: You MUST use web_search\n")
+ output.WriteString("- NEVER fabricate or infer answers - ONLY use retrieved content\n")
+ return output.String()
+ }
+
+ // Group by document (like grep showing filename)
+ docGroups := make(map[string]struct {
+ title string
+ chunks []map[string]interface{}
+ })
+
+ for _, result := range results {
+ knowledgeID := fmt.Sprintf("%v", result["knowledge_id"])
+ title := "Untitled"
+ if t := result["knowledge_title"]; t != nil {
+ title = fmt.Sprintf("%v", t)
+ }
+
+ group := docGroups[knowledgeID]
+ group.title = title
+ group.chunks = append(group.chunks, result)
+ docGroups[knowledgeID] = group
+ }
+
+ // Display results in grep-style format
+ for docID, group := range docGroups {
+ // Show document header (like grep showing filename)
+ if len(docGroups) > 1 {
+ output.WriteString(fmt.Sprintf("\n--- %s (knowledge_id: %s) ---\n", group.title, docID))
+ }
+
+ // Show each matching chunk
+ for _, chunk := range group.chunks {
+ chunkIndex := 0
+ if idx, ok := chunk["chunk_index"]; ok {
+ switch v := idx.(type) {
+ case int:
+ chunkIndex = v
+ case int64:
+ chunkIndex = int(v)
+ case float64:
+ chunkIndex = int(v)
+ }
+ }
+
+ content := ""
+ if c, ok := chunk["content"]; ok && c != nil {
+ content = fmt.Sprintf("%v", c)
+ }
+
+ // Extract preview with context around match (case-insensitive)
+ // Try each pattern and use the first match
+ preview := extractPreviewMultiPattern(content, patterns, false, contextLines)
+
+ // Format like grep: [filename:]line:content
+ if showLineNumbers {
+ if len(docGroups) > 1 {
+ output.WriteString(fmt.Sprintf("%s:chunk[%d]:%s\n", group.title, chunkIndex, preview))
+ } else {
+ output.WriteString(fmt.Sprintf("chunk[%d]:%s\n", chunkIndex, preview))
+ }
+ } else {
+ if len(docGroups) > 1 {
+ output.WriteString(fmt.Sprintf("%s:%s\n", group.title, preview))
+ } else {
+ output.WriteString(fmt.Sprintf("%s\n", preview))
+ }
+ }
+ }
+ }
+
+ // Add guidance for next steps
+ output.WriteString("\n=== Next Steps ===\n")
+ if len(results) > 0 {
+ output.WriteString("- Found matching documents. Use knowledge_search with semantic queries to understand the context.\n")
+ output.WriteString("- Filter knowledge_search by knowledge_ids from above results for better relevance.\n")
+ } else {
+ output.WriteString("- No matches found. Try different keywords or use knowledge_search for semantic search.\n")
+ output.WriteString("- Consider using synonyms or related terms in your search patterns.\n")
+ }
+
+ return output.String()
+}
+
+// extractPreview extracts a preview of content around the matched pattern
+func extractPreview(content, pattern string, caseSensitive bool, contextLen int) string {
+ if content == "" {
+ return ""
+ }
+
+ // Find the pattern in content
+ searchContent := content
+ searchPattern := pattern
+ if !caseSensitive {
+ searchContent = strings.ToLower(content)
+ searchPattern = strings.ToLower(pattern)
+ }
+
+ pos := strings.Index(searchContent, searchPattern)
+ if pos == -1 {
+ // If pattern not found (might be regex), just return first 60 chars
+ runes := []rune(content)
+ if len(runes) <= contextLen*2 {
+ return string(runes)
+ }
+ return string(runes[:contextLen*2])
+ }
+
+ // Extract context around the match
+ runes := []rune(content)
+ start := pos
+
+ // Convert to rune positions for proper unicode handling
+ runePos := 0
+ for i, r := range runes {
+ if runePos >= start {
+ start = i
+ break
+ }
+ runePos += len(string(r))
+ }
+
+ // Calculate preview bounds
+ previewStart := start - contextLen
+ if previewStart < 0 {
+ previewStart = 0
+ }
+
+ previewEnd := start + len([]rune(pattern)) + contextLen
+ if previewEnd > len(runes) {
+ previewEnd = len(runes)
+ }
+
+ preview := string(runes[previewStart:previewEnd])
+
+ // Clean up whitespace
+ preview = strings.ReplaceAll(preview, "\n", " ")
+ preview = strings.ReplaceAll(preview, "\t", " ")
+
+ // Collapse multiple spaces
+ for strings.Contains(preview, " ") {
+ preview = strings.ReplaceAll(preview, " ", " ")
+ }
+
+ return strings.TrimSpace(preview)
+}
+
+// extractPreviewMultiPattern extracts a preview around the first matched pattern
+func extractPreviewMultiPattern(content string, patterns []string, caseSensitive bool, contextLen int) string {
+ if content == "" || len(patterns) == 0 {
+ return ""
+ }
+
+ // Try each pattern and find the first match
+ for _, pattern := range patterns {
+ preview := extractPreview(content, pattern, caseSensitive, contextLen)
+ // Check if this pattern found a match (not just truncated content)
+ searchContent := content
+ searchPattern := pattern
+ if !caseSensitive {
+ searchContent = strings.ToLower(content)
+ searchPattern = strings.ToLower(pattern)
+ }
+ if strings.Contains(searchContent, searchPattern) {
+ return preview
+ }
+ }
+
+ // If no pattern matched, return preview from first pattern
+ if len(patterns) > 0 {
+ return extractPreview(content, patterns[0], caseSensitive, contextLen)
+ }
+
+ // Fallback: return first N chars
+ runes := []rune(content)
+ if len(runes) <= contextLen*2 {
+ return string(runes)
+ }
+ return string(runes[:contextLen*2])
+}
diff --git a/internal/agent/tools/knowledge_search.go b/internal/agent/tools/knowledge_search.go
index 02f9d8b1d..f5f9b085f 100644
--- a/internal/agent/tools/knowledge_search.go
+++ b/internal/agent/tools/knowledge_search.go
@@ -48,48 +48,31 @@ func NewKnowledgeSearchTool(
chatModel chat.Chat,
cfg *config.Config,
) *KnowledgeSearchTool {
- description := `Search within knowledge bases. Unified tool that supports both targeted and broad searches.
+ description := `Semantic/vector search for understanding questions and concepts.
-## Features
-- Multi-KB search: Search across multiple knowledge bases concurrently
+## Core Function
+Finds content by MEANING using embeddings (NOT exact text matching).
-## Usage
+## CRITICAL: Always Check for Entities First
+**If query has ANY entities → grep_chunks FIRST, then use this tool**
-**Use when**:
-- You know which knowledge bases to target (specify knowledge_base_ids)
-- You're unsure which KB contains the info (omit knowledge_base_ids to search all allowed KBs)
-- Want to search with multiple queries to get comprehensive results
-- Want to filter results from specific documents (use knowledge_ids)
+## Use When:
+- Questions: "What is...", "How...", "Why...", "Explain..."
+- Conceptual understanding
+- AFTER grep_chunks pre-retrieval (hybrid approach)
-**Returns**: Merged and deduplicated search results from KBs
+## Hybrid Workflow (Strongly Recommended):
+1. grep_chunks(["entity", "synonym", "变体"]) → pre-retrieve documents containing entity
+2. knowledge_search(["concept query"]) → deep understanding
-## Examples
+This ensures entity-related content is not missed!
-` + "`" + `
-# Simple search in specific KBs
-{
- "knowledge_base_ids": ["kb1", "kb2"],
- "queries": ["什么是向量数据库"]
-}
-
-# Search all allowed KBs with multiple queries
-{
- "queries": ["什么是向量数据库", "向量数据库的应用场景"]
-}
-
-# Search specific documents
-{
- "knowledge_base_ids": ["kb1"],
- "queries": ["彗星的起源"],
- "knowledge_ids": ["doc1", "doc2"]
-}
-` + "`" + `
-
-## Tips
+## Parameters
+- queries (required): 1-5 semantic questions (NOT just keywords)
+- knowledge_base_ids (optional): Limit to specific KBs
-- Concurrent search across multiple KBs
-- Results are automatically reranked to unify scores from different sources
-- Results are merged, deduplicated and sorted by relevance`
+## Output
+Semantically relevant chunks with scores, auto-reranked.`
return &KnowledgeSearchTool{
BaseTool: NewBaseTool("knowledge_search", description),
@@ -110,7 +93,7 @@ func (t *KnowledgeSearchTool) Parameters() map[string]interface{} {
"properties": map[string]interface{}{
"queries": map[string]interface{}{
"type": "array",
- "description": "Array of search queries",
+ "description": "REQUIRED: 1-5 semantic questions/topics (e.g., ['What is RAG?', 'RAG benefits'])",
"items": map[string]interface{}{
"type": "string",
},
@@ -119,22 +102,13 @@ func (t *KnowledgeSearchTool) Parameters() map[string]interface{} {
},
"knowledge_base_ids": map[string]interface{}{
"type": "array",
- "description": "Array of knowledge base IDs to search in (optional, if omitted searches all allowed KBs)",
+ "description": "Optional: KB IDs to search",
"items": map[string]interface{}{
"type": "string",
},
"minItems": 0,
"maxItems": 10,
},
- "knowledge_ids": map[string]interface{}{
- "type": "array",
- "description": "Optional array of document IDs to filter results (only return results from these specific documents)",
- "items": map[string]interface{}{
- "type": "string",
- },
- "minItems": 0,
- "maxItems": 50,
- },
},
"required": []string{"queries"},
}
@@ -243,17 +217,6 @@ func (t *KnowledgeSearchTool) Execute(ctx context.Context, args map[string]inter
logger.Infof(ctx, "[Tool][KnowledgeSearch] Search params: top_k=%d, vector_threshold=%.2f, keyword_threshold=%.2f, min_score=%.2f",
topK, vectorThreshold, keywordThreshold, minScore)
- // Extract knowledge_ids filter if provided
- var knowledgeIDsFilter map[string]bool
- if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok && len(knowledgeIDsRaw) > 0 {
- knowledgeIDsFilter = make(map[string]bool)
- for _, id := range knowledgeIDsRaw {
- if idStr, ok := id.(string); ok && idStr != "" {
- knowledgeIDsFilter[idStr] = true
- }
- }
- }
-
// Execute concurrent search (hybrid search handles both vector and keyword)
logger.Infof(ctx, "[Tool][KnowledgeSearch] Starting concurrent search across %d KBs", len(kbIDs))
kbTypeMap := t.getKnowledgeBaseTypes(ctx, kbIDs)
@@ -262,29 +225,10 @@ func (t *KnowledgeSearchTool) Execute(ctx context.Context, args map[string]inter
topK, vectorThreshold, keywordThreshold, kbTypeMap)
logger.Infof(ctx, "[Tool][KnowledgeSearch] Concurrent search completed: %d raw results", len(allResults))
- // Filter by knowledge_ids if provided
- if len(knowledgeIDsFilter) > 0 {
- logger.Infof(ctx, "[Tool][KnowledgeSearch] Filtering by %d knowledge IDs", len(knowledgeIDsFilter))
- filtered := make([]*searchResultWithMeta, 0)
- for _, r := range allResults {
- if knowledgeIDsFilter[r.KnowledgeID] {
- filtered = append(filtered, r)
- }
- }
- logger.Infof(ctx, "[Tool][KnowledgeSearch] After knowledge_id filter: %d results (from %d)",
- len(filtered), len(allResults))
- allResults = filtered
- }
-
// Filter by threshold first
filteredResults := t.filterByThreshold(allResults, vectorThreshold, keywordThreshold)
- logger.Infof(ctx, "[Tool][KnowledgeSearch] After threshold filter: %d results (from %d)",
- len(filteredResults), len(allResults))
-
// Deduplicate before reranking to reduce processing overhead
deduplicatedBeforeRerank := t.deduplicateResults(filteredResults)
- logger.Infof(ctx, "[Tool][KnowledgeSearch] After deduplication before rerank: %d results (from %d)",
- len(deduplicatedBeforeRerank), len(filteredResults))
// Apply ReRank if model is configured
// Prefer chatModel (LLM-based reranking) over rerankModel if both are available
@@ -358,11 +302,12 @@ func (t *KnowledgeSearchTool) Execute(ctx context.Context, args map[string]inter
// Build output
logger.Infof(ctx, "[Tool][KnowledgeSearch] Formatting output with %d final results", len(deduplicatedResults))
- result, err := t.formatOutput(ctx, deduplicatedResults, kbIDs, len(allResults), knowledgeIDsFilter, queries)
+ result, err := t.formatOutput(ctx, deduplicatedResults, kbIDs, len(allResults), queries)
if err != nil {
logger.Errorf(ctx, "[Tool][KnowledgeSearch] Failed to format output: %v", err)
return result, err
}
+ logger.Infof(ctx, "[Tool][KnowledgeSearch] Output: %s", result.Output)
return result, nil
}
@@ -859,7 +804,6 @@ func (t *KnowledgeSearchTool) formatOutput(
results []*searchResultWithMeta,
kbsToSearch []string,
totalBeforeFilter int,
- knowledgeIDsFilter map[string]bool,
queries []string,
) (*types.ToolResult, error) {
if len(results) == 0 {
@@ -868,39 +812,25 @@ func (t *KnowledgeSearchTool) formatOutput(
"results": []interface{}{},
"count": 0,
}
- if len(knowledgeIDsFilter) > 0 {
- filterList := make([]string, 0, len(knowledgeIDsFilter))
- for id := range knowledgeIDsFilter {
- filterList = append(filterList, id)
- }
- data["knowledge_ids"] = filterList
- }
if len(queries) > 0 {
data["queries"] = queries
}
+ output := fmt.Sprintf("No relevant content found in %d knowledge base(s).\n\n", len(kbsToSearch))
+ output += "=== ⚠️ CRITICAL - Next Steps ===\n"
+ output += "- ❌ DO NOT use training data or general knowledge to answer\n"
+ output += "- ✅ If web_search is enabled: You MUST use web_search to find information\n"
+ output += "- ✅ If web_search is disabled: State 'I couldn't find relevant information in the knowledge base'\n"
+ output += "- NEVER fabricate or infer answers - ONLY use retrieved content\n"
+
return &types.ToolResult{
Success: true,
- Output: fmt.Sprintf("No relevant content found in %d knowledge base(s).", len(kbsToSearch)),
+ Output: output,
Data: data,
}, nil
}
// Build output header
output := "=== Search Results ===\n"
- output += fmt.Sprintf("Knowledge Bases: %v\n", kbsToSearch)
- if len(knowledgeIDsFilter) > 0 {
- filterList := make([]string, 0, len(knowledgeIDsFilter))
- for id := range knowledgeIDsFilter {
- filterList = append(filterList, id)
- }
- output += fmt.Sprintf("Document Filter: %v\n", filterList)
- }
- if len(queries) == 1 {
- output += fmt.Sprintf("Query: %s\n", queries[0])
- } else if len(queries) > 1 {
- output += fmt.Sprintf("Queries (%d): %v\n", len(queries), queries)
- }
-
output += fmt.Sprintf("Found %d relevant results", len(results))
if totalBeforeFilter > len(results) {
output += fmt.Sprintf(" (filtered from %d)", totalBeforeFilter)
@@ -925,6 +855,11 @@ func (t *KnowledgeSearchTool) formatOutput(
faqMetadataCache := make(map[string]*types.FAQChunkMetadata)
+ // Track chunks per knowledge for statistics
+ knowledgeChunkMap := make(map[string]map[int]bool) // knowledge_id -> set of chunk_index
+ knowledgeTotalMap := make(map[string]int64) // knowledge_id -> total chunks
+ knowledgeTitleMap := make(map[string]string) // knowledge_id -> title
+
for i, result := range results {
var faqMeta *types.FAQChunkMetadata
if result.KnowledgeBaseType == types.KnowledgeBaseTypeFAQ {
@@ -936,6 +871,13 @@ func (t *KnowledgeSearchTool) formatOutput(
}
}
+ // Track chunk indices per knowledge
+ if knowledgeChunkMap[result.KnowledgeID] == nil {
+ knowledgeChunkMap[result.KnowledgeID] = make(map[int]bool)
+ }
+ knowledgeChunkMap[result.KnowledgeID][result.ChunkIndex] = true
+ knowledgeTitleMap[result.KnowledgeID] = result.KnowledgeTitle
+
// Group by knowledge base
if result.KnowledgeID != currentKB {
currentKB = result.KnowledgeID
@@ -943,6 +885,20 @@ func (t *KnowledgeSearchTool) formatOutput(
output += "\n"
}
output += fmt.Sprintf("[Source Document: %s]\n", result.KnowledgeTitle)
+
+ // Get total chunk count for this knowledge (cache it)
+ if _, exists := knowledgeTotalMap[result.KnowledgeID]; !exists {
+ _, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
+ t.tenantID, result.KnowledgeID,
+ &types.Pagination{Page: 1, PageSize: 1},
+ []types.ChunkType{types.ChunkTypeText}, "")
+ if err != nil {
+ logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v", result.KnowledgeID, err)
+ knowledgeTotalMap[result.KnowledgeID] = 0
+ } else {
+ knowledgeTotalMap[result.KnowledgeID] = total
+ }
+ }
}
// relevanceLevel := GetRelevanceLevel(result.Score)
@@ -958,8 +914,8 @@ func (t *KnowledgeSearchTool) formatOutput(
}
if len(faqMeta.Answers) > 0 {
output += " FAQ Answers:\n"
- for _, ans := range faqMeta.Answers {
- output += fmt.Sprintf(" Answer Choice %d: %s\n", i+1, ans)
+ for ansIdx, ans := range faqMeta.Answers {
+ output += fmt.Sprintf(" Answer Choice %d: %s\n", ansIdx+1, ans)
}
}
}
@@ -992,6 +948,59 @@ func (t *KnowledgeSearchTool) formatOutput(
}
}
+ // Add statistics and recommendations for each knowledge
+ output += "\n=== 检索统计与建议 ===\n\n"
+ for knowledgeID, retrievedChunks := range knowledgeChunkMap {
+ totalChunks := knowledgeTotalMap[knowledgeID]
+ retrievedCount := len(retrievedChunks)
+ title := knowledgeTitleMap[knowledgeID]
+
+ if totalChunks > 0 {
+ percentage := float64(retrievedCount) / float64(totalChunks) * 100
+ remaining := totalChunks - int64(retrievedCount)
+
+ output += fmt.Sprintf("文档: %s (%s)\n", title, knowledgeID)
+ output += fmt.Sprintf(" 总 Chunk 数: %d\n", totalChunks)
+ output += fmt.Sprintf(" 已召回: %d 个 (%.1f%%)\n", retrievedCount, percentage)
+ output += fmt.Sprintf(" 未召回: %d 个\n", remaining)
+
+ if remaining > 0 {
+ output += " 建议: 使用 list_knowledge_chunks 工具获取完整内容\n"
+
+ // Find missing chunk ranges (gaps in retrieved chunks)
+ missingRanges := t.findMissingChunkRanges(retrievedChunks, int(totalChunks))
+
+ if len(missingRanges) == 0 {
+ // No gaps found (shouldn't happen if remaining > 0, but handle it)
+ output += fmt.Sprintf(" - 获取全部内容: list_knowledge_chunks(knowledge_id=\"%s\", offset=0, limit=%d)\n", knowledgeID, totalChunks)
+ } else if len(missingRanges) == 1 && missingRanges[0].start == 0 && missingRanges[0].end == int(totalChunks)-1 {
+ // All chunks are missing (shouldn't happen, but handle it)
+ output += fmt.Sprintf(" - 获取全部内容: list_knowledge_chunks(knowledge_id=\"%s\", offset=0, limit=%d)\n", knowledgeID, totalChunks)
+ } else {
+ // Suggest getting each missing range
+ for idx, r := range missingRanges {
+ rangeSize := r.end - r.start + 1
+ if rangeSize <= 100 {
+ // Small range, get all at once
+ output += fmt.Sprintf(" - 区间 %d: chunk_index %d-%d (%d 个) → list_knowledge_chunks(knowledge_id=\"%s\", offset=%d, limit=%d)\n",
+ idx+1, r.start, r.end, rangeSize, knowledgeID, r.start, rangeSize)
+ } else {
+ // Large range, suggest getting in batches
+ output += fmt.Sprintf(" - 区间 %d: chunk_index %d-%d (%d 个,建议分批获取):\n",
+ idx+1, r.start, r.end, rangeSize)
+ output += fmt.Sprintf(" 首次: list_knowledge_chunks(knowledge_id=\"%s\", offset=%d, limit=100)\n",
+ knowledgeID, r.start)
+ if rangeSize > 100 {
+ output += " 继续: 根据返回结果调整 offset 继续获取剩余内容\n"
+ }
+ }
+ }
+ }
+ }
+ output += "\n"
+ }
+ }
+
// // Add usage guidance
// output += "\n\n=== Usage Guidelines ===\n"
// output += "- High relevance (>=0.8): directly usable for answering\n"
@@ -1011,13 +1020,7 @@ func (t *KnowledgeSearchTool) formatOutput(
"kb_counts": kbCounts,
"display_type": "search_results",
}
- if len(knowledgeIDsFilter) > 0 {
- filterList := make([]string, 0, len(knowledgeIDsFilter))
- for id := range knowledgeIDsFilter {
- filterList = append(filterList, id)
- }
- data["knowledge_ids"] = filterList
- }
+
if len(queries) > 0 {
data["queries"] = queries
}
@@ -1032,3 +1035,52 @@ func (t *KnowledgeSearchTool) formatOutput(
Data: data,
}, nil
}
+
+// chunkRange represents a continuous range of chunk indices
+type chunkRange struct {
+ start int
+ end int
+}
+
+// findMissingChunkRanges finds all continuous ranges of missing chunks
+// retrievedChunks is a set of retrieved chunk indices
+// totalChunks is the total number of chunks
+func (t *KnowledgeSearchTool) findMissingChunkRanges(retrievedChunks map[int]bool, totalChunks int) []chunkRange {
+ if totalChunks <= 0 {
+ return nil
+ }
+
+ var ranges []chunkRange
+ var currentStart int = -1
+
+ // Iterate through all possible chunk indices (0 to totalChunks-1)
+ for i := 0; i < totalChunks; i++ {
+ if !retrievedChunks[i] {
+ // This chunk is missing
+ if currentStart == -1 {
+ // Start of a new missing range
+ currentStart = i
+ }
+ } else {
+ // This chunk is retrieved
+ if currentStart != -1 {
+ // End of a missing range
+ ranges = append(ranges, chunkRange{
+ start: currentStart,
+ end: i - 1,
+ })
+ currentStart = -1
+ }
+ }
+ }
+
+ // Handle case where missing chunks extend to the end
+ if currentStart != -1 {
+ ranges = append(ranges, chunkRange{
+ start: currentStart,
+ end: totalChunks - 1,
+ })
+ }
+
+ return ranges
+}
diff --git a/internal/agent/tools/list_knowledge_chunks.go b/internal/agent/tools/list_knowledge_chunks.go
index eb95704e0..3b8887f6c 100644
--- a/internal/agent/tools/list_knowledge_chunks.go
+++ b/internal/agent/tools/list_knowledge_chunks.go
@@ -23,23 +23,24 @@ func NewListKnowledgeChunksTool(
knowledgeService interfaces.KnowledgeService,
chunkService interfaces.ChunkService,
) *ListKnowledgeChunksTool {
- description := `Retrieve paged chunks for a document (knowledge) by knowledge_id.
+ description := `Retrieve full chunk content for a document by knowledge_id.
-## When to Use
+## Use After grep_chunks or knowledge_search:
+1. grep_chunks(["keyword", "变体"]) → get knowledge_id
+2. list_knowledge_chunks(knowledge_id) → read full content
-- Need deterministic chunk previews for a known document
-- Want to quickly confirm how many chunks a document contains
-- Require surrounding context around a chunk_index returned by search results
-- Need content snippets without running an additional search query
+## When to Use:
+- Need full content of chunks from a known document
+- Want to see context around specific chunks
+- Check how many chunks a document has
-Avoid when:
-- You don't know the knowledge_id (use knowledge_search first)
+## Parameters:
+- knowledge_id (required): Document ID
+- limit (optional): Chunks per page (default 20, max 100)
+- offset (optional): Start position (default 0)
-## Parameters
-
-- knowledge_id (required): Target document/knowledge ID
-- limit (optional): Number of chunks to fetch (default 20, max 100).
-- offset (optional): Offset to start fetching chunks from (default 0).`
+## Output:
+Full chunk content with chunk_id, chunk_index, and content text.`
return &ListKnowledgeChunksTool{
BaseTool: NewBaseTool("list_knowledge_chunks", description),
@@ -56,18 +57,18 @@ func (t *ListKnowledgeChunksTool) Parameters() map[string]interface{} {
"properties": map[string]interface{}{
"knowledge_id": map[string]interface{}{
"type": "string",
- "description": "Knowledge/document ID to inspect",
+ "description": "Document ID to retrieve chunks from",
},
"limit": map[string]interface{}{
"type": "integer",
- "description": "Number of chunks to fetch (default 20, max 100)",
+ "description": "Chunks per page (default 20, max 100)",
"default": 20,
"minimum": 1,
"maximum": 100,
},
"offset": map[string]interface{}{
"type": "integer",
- "description": "Offset to start fetching chunks from (default 0)",
+ "description": "Start position (default 0)",
"default": 0,
"minimum": 0,
},
@@ -115,7 +116,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i
}
chunks, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
- t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText}, "")
+ t.tenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "")
if err != nil {
return &types.ToolResult{
Success: false,
@@ -134,7 +135,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i
knowledgeTitle := t.lookupKnowledgeTitle(ctx, knowledgeID)
- output := t.buildOutput(knowledgeID, knowledgeTitle, totalChunks, fetched, chunkLimit, chunks)
+ output := t.buildOutput(knowledgeID, knowledgeTitle, totalChunks, fetched, chunks)
formattedChunks := make([]map[string]interface{}, 0, len(chunks))
for idx, c := range chunks {
@@ -183,7 +184,6 @@ func (t *ListKnowledgeChunksTool) buildOutput(
knowledgeTitle string,
total int64,
fetched int,
- chunkLimit int,
chunks []*types.Chunk,
) string {
builder := &strings.Builder{}
@@ -195,7 +195,6 @@ func (t *ListKnowledgeChunksTool) buildOutput(
builder.WriteString(fmt.Sprintf("文档 ID: %s\n", knowledgeID))
}
builder.WriteString(fmt.Sprintf("总分块数: %d\n", total))
- builder.WriteString(fmt.Sprintf("本次拉取: %d 条(offset=%d)\n\n", fetched, chunkLimit))
if fetched == 0 {
builder.WriteString("未找到任何分块,请确认文档是否已完成解析。\n")
@@ -204,6 +203,7 @@ func (t *ListKnowledgeChunksTool) buildOutput(
}
return builder.String()
}
+ builder.WriteString(fmt.Sprintf("本次拉取: %d 条, 检索范围: %d - %d\n\n", fetched, chunks[0].ChunkIndex, chunks[len(chunks)-1].ChunkIndex))
builder.WriteString("=== 分块内容预览 ===\n\n")
for idx, c := range chunks {
diff --git a/internal/agent/tools/query_knowledge_graph.go b/internal/agent/tools/query_knowledge_graph.go
index 48c34a6cc..025e33350 100644
--- a/internal/agent/tools/query_knowledge_graph.go
+++ b/internal/agent/tools/query_knowledge_graph.go
@@ -18,62 +18,43 @@ type QueryKnowledgeGraphTool struct {
// NewQueryKnowledgeGraphTool creates a new query knowledge graph tool
func NewQueryKnowledgeGraphTool(knowledgeService interfaces.KnowledgeBaseService) *QueryKnowledgeGraphTool {
- description := `查询知识图谱,探索实体关系和知识网络。
-
-## 何时使用
-
-**适用场景**:
-- ✅ 需要了解实体之间的关系(如"Docker和Kubernetes的关系")
-- ✅ 探索知识网络和概念关联
-- ✅ 查找特定实体的相关信息
-- ✅ 理解技术架构和系统关系
-
-**不适用**:
-- ❌ 普通文本搜索(用 knowledge_search 更合适)
-- ❌ 知识库未配置图谱抽取
-- ❌ 需要精确的文档内容(用 knowledge_search)
-
-## 参数说明
-
-**knowledge_base_ids** (required): 要查询的知识库ID数组(1-10个)
-- 只有配置了图谱抽取的知识库才会有效
-- 支持批量并发查询多个知识库
-- 示例: ["kb_tech", "kb_arch"]
-
-**query** (required): 查询内容
-- 可以是实体名称(如"Docker")
-- 可以是关系查询(如"容器编排")
-- 可以是概念搜索(如"微服务架构")
-
-## 图谱配置
-
-知识图谱需要在知识库中预先配置:
-- **实体类型**(Nodes):如"技术"、"工具"、"概念"
-- **关系类型**(Relations):如"依赖"、"使用"、"包含"
-
-如果知识库未配置图谱,工具会提示并返回普通搜索结果。
-
-## 配合使用
-
-1. **关系探索**: query_knowledge_graph → get_chunk_detail(查看详细内容)
-2. **网络分析**: query_knowledge_graph → list_knowledge_chunks(扩展上下文)
-3. **主题研究**: knowledge_search → query_knowledge_graph(深入实体关系)
-
-## 当前状态
-
-⚠️ **注意**: 完整的图数据库集成正在开发中。当前版本:
-- ✓ 支持图谱配置查询
-- ✓ 返回图谱相关的文档片段
-- ✓ 显示实体和关系配置信息
-- ⏳ 完整的图查询语言(Cypher)支持开发中
-- ⏳ 可视化图数据结构开发中
-
-## Tips
-
-- 结果会标注图谱配置状态
-- 返回的Data字段包含结构化图信息供前端展示
-- 跨知识库结果自动去重
-- 按相关度排序`
+ description := `Query knowledge graph to explore entity relationships and knowledge networks.
+
+## Core Function
+Explores relationships between entities in knowledge bases that have graph extraction configured.
+
+## When to Use
+✅ **Use for**:
+- Understanding relationships between entities (e.g., "relationship between Docker and Kubernetes")
+- Exploring knowledge networks and concept associations
+- Finding related information about specific entities
+- Understanding technical architecture and system relationships
+
+❌ **Don't use for**:
+- General text search → use knowledge_search
+- Knowledge base without graph extraction configured
+- Need exact document content → use knowledge_search
+
+## Parameters
+- **knowledge_base_ids** (required): Array of knowledge base IDs (1-10). Only KBs with graph extraction configured will be effective.
+- **query** (required): Query content - can be entity name, relationship query, or concept search.
+
+## Graph Configuration
+Knowledge graph must be pre-configured in knowledge bases:
+- **Entity types** (Nodes): e.g., "Technology", "Tool", "Concept"
+- **Relationship types** (Relations): e.g., "depends_on", "uses", "contains"
+
+If KB is not configured with graph, tool will return regular search results.
+
+## Workflow
+1. **Relationship exploration**: query_knowledge_graph → list_knowledge_chunks (for detailed content)
+2. **Network analysis**: query_knowledge_graph → knowledge_search (for comprehensive understanding)
+3. **Topic research**: knowledge_search → query_knowledge_graph (for deep entity relationships)
+
+## Notes
+- Results indicate graph configuration status
+- Cross-KB results are automatically deduplicated
+- Results are sorted by relevance`
return &QueryKnowledgeGraphTool{
BaseTool: NewBaseTool("query_knowledge_graph", description),
diff --git a/internal/agent/tools/todo_write.go b/internal/agent/tools/todo_write.go
index 95d24e6f2..74cf98a73 100644
--- a/internal/agent/tools/todo_write.go
+++ b/internal/agent/tools/todo_write.go
@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
- "strings"
"github.com/Tencent/WeKnora/internal/types"
)
@@ -169,13 +168,13 @@ func (t *TodoWriteTool) Parameters() map[string]interface{} {
"type": "string",
"description": "Clear description of what to investigate or accomplish in this step",
},
- "tools_to_use": map[string]interface{}{
- "type": "array",
- "description": "Suggested tools for this step (e.g., ['knowledge_search', 'list_knowledge_chunks'])",
- "items": map[string]interface{}{
- "type": "string",
- },
- },
+ // "tools_to_use": map[string]interface{}{
+ // "type": "array",
+ // "description": "Suggested tools for this step (e.g., ['knowledge_search', 'list_knowledge_chunks'])",
+ // "items": map[string]interface{}{
+ // "type": "string",
+ // },
+ // },
"status": map[string]interface{}{
"type": "string",
"enum": []string{"pending", "in_progress", "completed"},
@@ -274,6 +273,23 @@ func generatePlanOutput(task string, steps []PlanStep) string {
return output
}
+ // Count task statuses
+ pendingCount := 0
+ inProgressCount := 0
+ completedCount := 0
+ for _, step := range steps {
+ switch step.Status {
+ case "pending":
+ pendingCount++
+ case "in_progress":
+ inProgressCount++
+ case "completed":
+ completedCount++
+ }
+ }
+ totalCount := len(steps)
+ remainingCount := pendingCount + inProgressCount
+
output += "**计划步骤**:\n\n"
// Display all steps in order
@@ -281,12 +297,34 @@ func generatePlanOutput(task string, steps []PlanStep) string {
output += formatPlanStep(i+1, step)
}
- output += "\n**执行指南**:\n"
- output += "- 每步执行前标记为 in_progress,完成后标记为 completed\n"
- output += "- 根据搜索结果灵活调整计划,可跳过不必要的步骤\n"
- output += "- 在关键决策点使用 think 工具深入分析\n"
- output += "- 如果某一步骤已获得足够信息,可跳过后续步骤\n\n"
- output += "注意:计划是指导而非硬性要求,保持灵活应对。"
+ // Add summary and emphasis on remaining tasks
+ output += "\n=== 任务进度 ===\n"
+ output += fmt.Sprintf("总计: %d 个任务\n", totalCount)
+ output += fmt.Sprintf("✅ 已完成: %d 个\n", completedCount)
+ output += fmt.Sprintf("🔄 进行中: %d 个\n", inProgressCount)
+ output += fmt.Sprintf("⏳ 待处理: %d 个\n", pendingCount)
+
+ output += "\n=== ⚠️ 重要提醒 ===\n"
+ if remainingCount > 0 {
+ output += fmt.Sprintf("**还有 %d 个任务未完成!**\n\n", remainingCount)
+ output += "**必须完成所有任务后才能总结或得出结论。**\n\n"
+ output += "下一步操作:\n"
+ if inProgressCount > 0 {
+ output += "- 继续完成当前进行中的任务\n"
+ }
+ if pendingCount > 0 {
+ output += fmt.Sprintf("- 开始处理 %d 个待处理任务\n", pendingCount)
+ output += "- 按顺序完成每个任务,不要跳过\n"
+ }
+ output += "- 完成每个任务后,更新 todo_write 标记为 completed\n"
+ output += "- 只有在所有任务完成后,才能生成最终总结\n"
+ } else {
+ output += "✅ **所有任务已完成!**\n\n"
+ output += "现在可以:\n"
+ output += "- 综合所有任务的发现\n"
+ output += "- 生成完整的最终答案或报告\n"
+ output += "- 确保所有方面都已充分研究\n"
+ }
return output
}
@@ -307,9 +345,9 @@ func formatPlanStep(index int, step PlanStep) string {
output := fmt.Sprintf(" %d. %s [%s] %s\n", index, emoji, step.Status, step.Description)
- if len(step.ToolsToUse) > 0 {
- output += fmt.Sprintf(" 工具: %s\n", strings.Join(step.ToolsToUse, ", "))
- }
+ // if len(step.ToolsToUse) > 0 {
+ // output += fmt.Sprintf(" 工具: %s\n", strings.Join(step.ToolsToUse, ", "))
+ // }
return output
}
diff --git a/internal/agent/tools/web_fetch.go b/internal/agent/tools/web_fetch.go
index 68b1ea47b..f1bde86a1 100644
--- a/internal/agent/tools/web_fetch.go
+++ b/internal/agent/tools/web_fetch.go
@@ -43,20 +43,17 @@ type WebFetchTool struct {
// NewWebFetchTool 创建 web_fetch 工具实例
func NewWebFetchTool(chatModel chat.Chat) *WebFetchTool {
- description := `Fetch web content from previously discovered URLs and analyze it with an LLM.
+ description := `Fetch detailed web content from previously discovered URLs and analyze it with an LLM.
-## 流程
-- 接收一个或多个 {url, prompt} 组合
-- 抓取网页内容并转换为 Markdown 文本
-- 使用提示词调用小模型进行分析与总结(若模型可用)
-- 返回总结结果与原始内容片段
+## Usage
+- Receive one or more {url, prompt} combinations
+- Fetch web page content and convert to Markdown text
+- Use prompt to call small model for analysis and summary (if model is available)
+- Return summary result and original content fragment
-## 使用场景
-- 对 web_search 返回的链接做进一步深入阅读
-- 从网页中提取结构化信息或关键信息
-
-## 参数
-- items (必填): 数组,元素为 { "url": "...", "prompt": "..." }
+## When to Use
+- **MANDATORY**: After web_search returns results, if content is truncated or incomplete, use web_fetch to get full page content
+- When web_search snippet is insufficient for answering the question
`
return &WebFetchTool{
@@ -192,6 +189,22 @@ func (t *WebFetchTool) Execute(ctx context.Context, args map[string]interface{})
}
}
+ // Add guidance for next steps
+ builder.WriteString("\n=== Next Steps ===\n")
+ if len(aggregated) > 0 {
+ builder.WriteString("- ✅ Full page content has been fetched and analyzed.\n")
+ builder.WriteString("- Evaluate if the content is sufficient to answer the question completely.\n")
+ builder.WriteString("- Synthesize information from all fetched pages for comprehensive answers.\n")
+ if !success {
+ builder.WriteString("- ⚠️ Some URLs failed to fetch. Use available content or try alternative sources.\n")
+ }
+ } else {
+ builder.WriteString("- ❌ No content was successfully fetched. Consider:\n")
+ builder.WriteString(" - Verify URLs are accessible\n")
+ builder.WriteString(" - Try alternative sources from web_search results\n")
+ builder.WriteString(" - Check if information can be found in knowledge base instead\n")
+ }
+
data := map[string]interface{}{
"results": aggregated,
"count": len(aggregated),
diff --git a/internal/agent/tools/web_search.go b/internal/agent/tools/web_search.go
index 58ad745b9..373a0f001 100644
--- a/internal/agent/tools/web_search.go
+++ b/internal/agent/tools/web_search.go
@@ -33,6 +33,12 @@ func NewWebSearchTool(
) *WebSearchTool {
description := `Search the web for current information and news. This tool searches the internet to find up-to-date information that may not be in the knowledge base.
+## CRITICAL - KB First Rule
+**ABSOLUTE RULE**: You MUST complete KB retrieval (grep_chunks AND knowledge_search) FIRST before using this tool.
+- NEVER use web_search without first trying grep_chunks and knowledge_search
+- ONLY use web_search if BOTH grep_chunks AND knowledge_search return insufficient/no results
+- KB retrieval is MANDATORY - you CANNOT skip it
+
## Features
- Real-time web search: Search the internet for current information
- RAG compression: Automatically compresses and extracts relevant content from search results
@@ -41,6 +47,8 @@ func NewWebSearchTool(
## Usage
**Use when**:
+- **ONLY after** completing grep_chunks AND knowledge_search
+- KB retrieval returned insufficient or no results
- Need current or real-time information (news, events, recent updates)
- Information is not available in knowledge bases
- Need to verify or supplement information from knowledge bases
@@ -70,7 +78,8 @@ func NewWebSearchTool(
- Results are automatically compressed using RAG to extract relevant content
- Search results are stored in a temporary knowledge base for the session
- Use this tool when knowledge bases don't have the information you need
-- Results include URL, title, snippet, and full content when available
+- Results include URL, title, snippet, and content snippet (may be truncated)
+- **CRITICAL**: If content is truncated or you need full details, use **web_fetch** to fetch complete page content
- Maximum ` + fmt.Sprintf("%d", maxResults) + ` results will be returned per search`
return &WebSearchTool{
@@ -232,6 +241,19 @@ func (t *WebSearchTool) Execute(ctx context.Context, args map[string]interface{}
formattedResults = append(formattedResults, resultData)
}
+ // Add guidance for next steps
+ output += "\n=== Next Steps ===\n"
+ if len(webResults) > 0 {
+ output += "- ⚠️ Content may be truncated (showing first 500 chars). Use web_fetch to get full page content.\n"
+ output += "- Extract URLs from results above and use web_fetch with appropriate prompts to get detailed information.\n"
+ output += "- Synthesize information from multiple sources for comprehensive answers.\n"
+ } else {
+ output += "- No web search results found. Consider:\n"
+ output += " - Try different search queries or keywords\n"
+ output += " - Check if question can be answered from knowledge base instead\n"
+ output += " - Verify if the topic requires real-time information\n"
+ }
+
return &types.ToolResult{
Success: true,
Output: output,
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go
index c5e43ece5..034f52d01 100644
--- a/internal/application/repository/chunk.go
+++ b/internal/application/repository/chunk.go
@@ -25,7 +25,7 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun
for _, chunk := range chunks {
chunk.Content = common.CleanInvalidUTF8(chunk.Content)
}
- return r.db.Debug().WithContext(ctx).CreateInBatches(chunks, 100).Error
+ return r.db.WithContext(ctx).CreateInBatches(chunks, 100).Error
}
// GetChunkByID retrieves a chunk by its ID and tenant ID
@@ -141,6 +141,14 @@ func (r *chunkRepository) DeleteChunk(ctx context.Context, tenantID uint, id str
return r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).Delete(&types.Chunk{}).Error
}
+// DeleteChunks deletes chunks by IDs in batch
+func (r *chunkRepository) DeleteChunks(ctx context.Context, tenantID uint, ids []string) error {
+ if len(ids) == 0 {
+ return nil
+ }
+ return r.db.WithContext(ctx).Where("tenant_id = ? AND id IN ?", tenantID, ids).Delete(&types.Chunk{}).Error
+}
+
// DeleteChunksByKnowledgeID deletes all chunks for a knowledge ID
func (r *chunkRepository) DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) error {
return r.db.WithContext(ctx).Where(
diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go
index ec07d6d90..84f1aabc2 100644
--- a/internal/application/repository/knowledge.go
+++ b/internal/application/repository/knowledge.go
@@ -229,3 +229,26 @@ func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Contex
Count(&count).Error
return count, err
}
+
+// CountKnowledgeByStatus counts the number of knowledge items with the specified parse status
+func (r *knowledgeRepository) CountKnowledgeByStatus(
+ ctx context.Context,
+ tenantID uint,
+ kbID string,
+ parseStatuses []string,
+) (int64, error) {
+ if len(parseStatuses) == 0 {
+ return 0, nil
+ }
+
+ var count int64
+ query := r.db.WithContext(ctx).Model(&types.Knowledge{}).
+ Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
+ Where("parse_status IN ?", parseStatuses)
+
+ if err := query.Count(&count).Error; err != nil {
+ return 0, err
+ }
+
+ return count, nil
+}
diff --git a/internal/application/repository/retriever/postgres/repository.go b/internal/application/repository/retriever/postgres/repository.go
index f6c3f8e0f..5c5a1d685 100644
--- a/internal/application/repository/retriever/postgres/repository.go
+++ b/internal/application/repository/retriever/postgres/repository.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
+ "strings"
"github.com/Tencent/WeKnora/internal/common"
"github.com/Tencent/WeKnora/internal/logger"
@@ -151,8 +152,10 @@ func (g *pgRepository) KeywordsRetrieve(ctx context.Context,
conds := make([]clause.Expression, 0)
if len(params.KnowledgeBaseIDs) > 0 {
logger.GetLogger(ctx).Debugf("[Postgres] Filtering by knowledge base IDs: %v", params.KnowledgeBaseIDs)
- conds = append(conds, clause.Expr{
- SQL: fmt.Sprintf("knowledge_base_id @@@ 'in (%s)'", common.StringSliceJoin(params.KnowledgeBaseIDs)),
+ // Use standard SQL IN clause instead of @@@ operator for better performance with B-tree index
+ conds = append(conds, clause.IN{
+ Column: "knowledge_base_id",
+ Values: common.ToInterfaceSlice(params.KnowledgeBaseIDs),
})
}
conds = append(conds, clause.Expr{
@@ -210,52 +213,96 @@ func (g *pgRepository) KeywordsRetrieve(ctx context.Context,
}
// VectorRetrieve performs vector similarity search using pgvector
+// Optimized to use HNSW index efficiently and avoid recalculating vector distance
func (g *pgRepository) VectorRetrieve(ctx context.Context,
params types.RetrieveParams,
) ([]*types.RetrieveResult, error) {
logger.GetLogger(ctx).Infof("[Postgres] Vector retrieval: dim=%d, topK=%d, threshold=%.4f",
len(params.Embedding), params.TopK, params.Threshold)
- conds := make([]clause.Expression, 0)
+ dimension := len(params.Embedding)
+ queryVector := pgvector.NewHalfVector(params.Embedding)
+
+ // Build WHERE conditions for filtering
+ whereParts := make([]string, 0)
+ allVars := make([]interface{}, 0)
+
+ // Add query vector first (used in ORDER BY for HNSW index)
+ allVars = append(allVars, queryVector)
+
+ // Dimension filter (required for HNSW index WHERE clause)
+ whereParts = append(whereParts, fmt.Sprintf("dimension = $%d", len(allVars)+1))
+ allVars = append(allVars, dimension)
+
+ // Knowledge base filter
if len(params.KnowledgeBaseIDs) > 0 {
logger.GetLogger(ctx).Debugf(
"[Postgres] Filtering vector search by knowledge base IDs: %v",
params.KnowledgeBaseIDs,
)
- conds = append(conds, clause.IN{
- Column: "knowledge_base_id",
- Values: common.ToInterfaceSlice(params.KnowledgeBaseIDs),
- })
+ // Build IN clause with proper placeholders
+ placeholders := make([]string, len(params.KnowledgeBaseIDs))
+ paramStart := len(allVars) + 1
+ for i := range params.KnowledgeBaseIDs {
+ placeholders[i] = fmt.Sprintf("$%d", paramStart+i)
+ allVars = append(allVars, params.KnowledgeBaseIDs[i])
+ }
+ whereParts = append(whereParts, fmt.Sprintf("knowledge_base_id IN (%s)",
+ strings.Join(placeholders, ", ")))
+ }
+
+ // is_enabled filter
+ whereParts = append(whereParts, fmt.Sprintf("(is_enabled IS NULL OR is_enabled = $%d)", len(allVars)+1))
+ allVars = append(allVars, true)
+
+ // Build WHERE clause string
+ whereClause := ""
+ if len(whereParts) > 0 {
+ whereClause = "WHERE " + strings.Join(whereParts, " AND ")
}
- // <=> Cosine similarity operator
- // <-> L2 distance operator
- // <#> Inner product operator
- dimension := len(params.Embedding)
- conds = append(conds, clause.Expr{SQL: "dimension = ?", Vars: []interface{}{dimension}})
- // Filter by is_enabled = true or NULL (NULL means enabled for historical data)
- conds = append(conds, clause.Expr{
- SQL: "(is_enabled IS NULL OR is_enabled = ?)",
- Vars: []interface{}{true},
- })
- conds = append(conds, clause.Expr{
- SQL: fmt.Sprintf("embedding::halfvec(%d) <=> ?::halfvec < ?", dimension),
- Vars: []interface{}{pgvector.NewHalfVector(params.Embedding), 1 - params.Threshold},
- })
- conds = append(conds, clause.OrderBy{Expression: clause.Expr{
- SQL: fmt.Sprintf("embedding::halfvec(%d) <=> ?::halfvec", dimension),
- Vars: []interface{}{pgvector.NewHalfVector(params.Embedding)},
- }})
+
+ // Expand TopK to get more candidates before threshold filtering
+ expandedTopK := params.TopK * 2
+ if expandedTopK < 100 {
+ expandedTopK = 100 // Minimum 100 candidates
+ }
+ if expandedTopK > 1000 {
+ expandedTopK = 1000 // Maximum 1000 candidates
+ }
+
+ // Optimized query: Use subquery to calculate distance once
+ // Strategy: Use ORDER BY with vector distance to leverage HNSW index,
+ // then filter by threshold in outer query
+ // This allows PostgreSQL to use HNSW index efficiently
+ subqueryLimitParam := len(allVars) + 1
+ thresholdParam := len(allVars) + 2
+ finalLimitParam := len(allVars) + 3
+
+ querySQL := fmt.Sprintf(`
+ SELECT
+ id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id,
+ (1 - distance) as score
+ FROM (
+ SELECT
+ id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id,
+ embedding::halfvec(%d) <=> $1::halfvec as distance
+ FROM embeddings
+ %s
+ ORDER BY embedding::halfvec(%d) <=> $1::halfvec
+ LIMIT $%d
+ ) AS candidates
+ WHERE distance < $%d
+ ORDER BY distance ASC
+ LIMIT $%d
+ `, dimension, whereClause, dimension, subqueryLimitParam, thresholdParam, finalLimitParam)
+
+ allVars = append(allVars, expandedTopK) // LIMIT in subquery
+ allVars = append(allVars, 1-params.Threshold) // Distance threshold
+ allVars = append(allVars, params.TopK) // Final LIMIT
var embeddingDBList []pgVectorWithScore
- err := g.db.WithContext(ctx).Clauses(conds...).
- Select(fmt.Sprintf(
- "id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id, "+
- "(1 - (embedding::halfvec(%d) <=> ?::halfvec)) as score",
- dimension,
- ), pgvector.NewHalfVector(params.Embedding)).
- Limit(int(params.TopK)).
- Find(&embeddingDBList).Error
+ err := g.db.WithContext(ctx).Raw(querySQL, allVars...).Scan(&embeddingDBList).Error
if err == gorm.ErrRecordNotFound {
logger.GetLogger(ctx).Warnf("[Postgres] No vector matches found that meet threshold %.4f", params.Threshold)
@@ -266,6 +313,11 @@ func (g *pgRepository) VectorRetrieve(ctx context.Context,
return nil, err
}
+ // Apply final TopK limit (in case we got more results than needed)
+ if len(embeddingDBList) > int(params.TopK) {
+ embeddingDBList = embeddingDBList[:params.TopK]
+ }
+
logger.GetLogger(ctx).Infof("[Postgres] Vector retrieval found %d results", len(embeddingDBList))
results := make([]*types.IndexWithScore, len(embeddingDBList))
for i := range embeddingDBList {
diff --git a/internal/application/service/chunk.go b/internal/application/service/chunk.go
index 72626bdde..32d14077a 100644
--- a/internal/application/service/chunk.go
+++ b/internal/application/service/chunk.go
@@ -58,8 +58,6 @@ func (s *chunkService) GetRepository() interfaces.ChunkRepository {
// Returns:
// - error: Any error encountered during chunk creation
func (s *chunkService) CreateChunks(ctx context.Context, chunks []*types.Chunk) error {
- logger.Info(ctx, "Start creating chunks")
- logger.Infof(ctx, "Creating %d chunks", len(chunks))
err := s.chunkRepository.CreateChunks(ctx, chunks)
if err != nil {
@@ -69,7 +67,7 @@ func (s *chunkService) CreateChunks(ctx context.Context, chunks []*types.Chunk)
return err
}
- logger.Info(ctx, "Chunks created successfully")
+ logger.Infof(ctx, "Add %d chunks successfully", len(chunks))
return nil
}
@@ -216,6 +214,37 @@ func (s *chunkService) DeleteChunk(ctx context.Context, id string) error {
return nil
}
+// DeleteChunks deletes chunks by IDs in batch
+// This method removes multiple chunks from the repository in a single operation
+// Parameters:
+// - ctx: Context with authentication and request information
+// - ids: Slice of chunk IDs to delete
+//
+// Returns:
+// - error: Any error encountered during batch deletion
+func (s *chunkService) DeleteChunks(ctx context.Context, ids []string) error {
+ if len(ids) == 0 {
+ return nil
+ }
+ logger.Info(ctx, "Start deleting chunks in batch")
+ logger.Infof(ctx, "Deleting %d chunks", len(ids))
+
+ tenantID := ctx.Value(types.TenantIDContextKey).(uint)
+ logger.Infof(ctx, "Tenant ID: %d", tenantID)
+
+ err := s.chunkRepository.DeleteChunks(ctx, tenantID, ids)
+ if err != nil {
+ logger.ErrorWithFields(ctx, err, map[string]interface{}{
+ "chunk_ids": ids,
+ "tenant_id": tenantID,
+ })
+ return err
+ }
+
+ logger.Infof(ctx, "Successfully deleted %d chunks", len(ids))
+ return nil
+}
+
// DeleteChunksByKnowledgeID deletes all chunks for a knowledge ID
// This method removes all chunks belonging to a specific knowledge document
// Parameters:
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index 9a0b56411..6157ef58f 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -22,6 +22,7 @@ import (
werrors "github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/chat"
+ "github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/models/utils"
"github.com/Tencent/WeKnora/internal/tracing"
"github.com/Tencent/WeKnora/internal/types"
@@ -70,6 +71,7 @@ type knowledgeService struct {
const (
manualContentMaxLength = 200000
manualFileExtension = ".md"
+ faqImportBatchSize = 50 // 每批处理的FAQ条目数
)
// NewKnowledgeService creates a new knowledge service instance
@@ -120,14 +122,14 @@ func (s *knowledgeService) CreateKnowledgeFromFile(ctx context.Context,
kbID string, file *multipart.FileHeader, metadata map[string]string, enableMultimodel *bool, customFileName string,
) (*types.Knowledge, error) {
logger.Info(ctx, "Start creating knowledge from file")
-
+
// Use custom filename if provided, otherwise use original filename
fileName := file.Filename
if customFileName != "" {
fileName = customFileName
logger.Infof(ctx, "Using custom filename: %s (original: %s)", customFileName, file.Filename)
}
-
+
logger.Infof(ctx, "Knowledge base ID: %s, file: %s", kbID, fileName)
if metadata != nil {
logger.Infof(ctx, "Received metadata: %v", metadata)
@@ -2065,32 +2067,176 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
return types.NewPageResult(total, page, entries), nil
}
-// UpsertFAQEntries imports or appends FAQ entries.
+// UpsertFAQEntries imports or appends FAQ entries asynchronously.
+// Returns task ID for tracking import progress.
func (s *knowledgeService) UpsertFAQEntries(ctx context.Context,
kbID string, payload *types.FAQBatchUpsertPayload,
-) error {
+) (string, error) {
if payload == nil || len(payload.Entries) == 0 {
- return werrors.NewBadRequestError("FAQ 条目不能为空")
+ return "", werrors.NewBadRequestError("FAQ 条目不能为空")
}
if payload.Mode == "" {
payload.Mode = types.FAQBatchModeAppend
}
if payload.Mode != types.FAQBatchModeAppend && payload.Mode != types.FAQBatchModeReplace {
- return werrors.NewBadRequestError("模式仅支持 append 或 replace")
+ return "", werrors.NewBadRequestError("模式仅支持 append 或 replace")
}
+ // 验证知识库是否存在且有效
+ if _, err := s.validateFAQKnowledgeBase(ctx, kbID); err != nil {
+ return "", err
+ }
+
+ tenantID := ctx.Value(types.TenantIDContextKey).(uint)
+
+ // 检查是否有正在进行的导入任务
+ runningKnowledge, err := s.getRunningFAQImportTask(ctx, kbID, tenantID)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to check running import task: %v", err)
+ // 检查失败不影响导入,继续执行
+ } else if runningKnowledge != nil {
+ logger.Warnf(ctx, "Import task already running for KB %s: %s (status: %s)", kbID, runningKnowledge.ID, runningKnowledge.ParseStatus)
+ return "", werrors.NewBadRequestError(fmt.Sprintf("该知识库已有导入任务正在进行中(任务ID: %s),请等待完成后再试", runningKnowledge.ID))
+ }
+
+ // 确保FAQ Knowledge存在
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
+ if err != nil {
+ return "", err
+ }
+ faqKnowledge, err := s.ensureFAQKnowledge(ctx, tenantID, kb)
+ if err != nil {
+ return "", fmt.Errorf("failed to ensure FAQ knowledge: %w", err)
+ }
+
+ // 初始化导入任务状态到Knowledge表
+ taskID := faqKnowledge.ID // 使用Knowledge ID作为taskID
+ if err := s.updateFAQImportStatus(ctx, taskID, types.FAQImportStatusPending, 0, len(payload.Entries), 0, ""); err != nil {
+ logger.Errorf(ctx, "Failed to initialize FAQ import task status: %v", err)
+ return "", fmt.Errorf("failed to initialize task: %w", err)
+ }
+ bgCtx := logger.CloneContext(ctx)
+ // 在后台goroutine中执行导入
+ go func() {
+ // 使用独立的context,不受HTTP请求context影响
+ // 设置较长的超时时间(2小时)
+ bgCtx, cancel := context.WithTimeout(bgCtx, 2*time.Hour)
+ defer cancel()
+
+ logger.Infof(bgCtx, "Starting FAQ import task: %s, total entries: %d", taskID, len(payload.Entries))
+
+ // 更新任务状态为运行中
+ if err := s.updateFAQImportStatus(bgCtx, taskID, types.FAQImportStatusRunning, 0, len(payload.Entries), 0, ""); err != nil {
+ logger.Errorf(bgCtx, "Failed to update task status to running: %v", err)
+ }
+
+ // 执行实际导入
+ if err := s.executeFAQImport(bgCtx, taskID, kbID, payload, tenantID); err != nil {
+ logger.Errorf(bgCtx, "FAQ import task failed: %s, error: %v", taskID, err)
+ // 获取当前进度
+ tenantID := bgCtx.Value(types.TenantIDContextKey).(uint)
+ knowledge, _ := s.repo.GetKnowledgeByID(bgCtx, tenantID, taskID)
+ total := len(payload.Entries)
+ processed := 0
+ if knowledge != nil {
+ importMeta, _ := types.ParseFAQImportMetadata(knowledge)
+ if importMeta != nil {
+ total = importMeta.ImportTotal
+ processed = importMeta.ImportProcessed
+ }
+ }
+ if updateErr := s.updateFAQImportStatus(bgCtx, taskID, types.FAQImportStatusFailed, 0, total, processed, err.Error()); updateErr != nil {
+ logger.Errorf(bgCtx, "Failed to update task status to failed: %v", updateErr)
+ }
+ return
+ }
+
+ // 任务成功完成
+ logger.Infof(bgCtx, "FAQ import task completed: %s", taskID)
+ if err := s.updateFAQImportStatus(bgCtx, taskID, types.FAQImportStatusSuccess, 100, len(payload.Entries), len(payload.Entries), ""); err != nil {
+ logger.Errorf(bgCtx, "Failed to update task status to success: %v", err)
+ }
+ }()
+
+ return taskID, nil
+}
+
+// executeFAQImport 执行实际的FAQ导入逻辑
+func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string, kbID string,
+ payload *types.FAQBatchUpsertPayload, tenantID uint) (err error) {
+ // 用于记录所有已创建的chunks,用于失败时回滚
+ var createdChunks []*types.Chunk
+ // 用于记录已索引的chunks,需要清理索引数据
+ var indexedChunks []*types.Chunk
+ // 保存知识库和embedding模型信息,用于清理索引
+ var kb *types.KnowledgeBase
+ var embeddingModel embedding.Embedder
+
+ // Recovery机制:如果发生任何错误或panic,回滚所有已创建的chunks和索引数据
+ defer func() {
+ // 捕获panic
+ if r := recover(); r != nil {
+ logger.Errorf(ctx, "FAQ import task %s panicked: %v", taskID, r)
+ err = fmt.Errorf("panic during FAQ import: %v", r)
+ }
+ if err != nil && len(createdChunks) > 0 {
+ logger.Warnf(ctx, "FAQ import task %s failed (error: %v), rolling back %d created chunks and their indices", taskID, err, len(createdChunks))
+
+ // 清理索引数据(如果有已索引的chunks)
+ if len(indexedChunks) > 0 && kb != nil && embeddingModel != nil {
+ chunkIDs := make([]string, 0, len(indexedChunks))
+ for _, chunk := range indexedChunks {
+ chunkIDs = append(chunkIDs, chunk.ID)
+ }
+ // 从context获取tenant信息
+ tenantInfo := ctx.Value(types.TenantInfoContextKey)
+ if tenantInfo != nil {
+ if tenant, ok := tenantInfo.(*types.Tenant); ok {
+ retrieveEngine, engineErr := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenant.RetrieverEngines.Engines)
+ if engineErr == nil {
+ if delIndexErr := retrieveEngine.DeleteByChunkIDList(ctx, chunkIDs, embeddingModel.GetDimensions()); delIndexErr != nil {
+ logger.Errorf(ctx, "Failed to delete indices for %d chunks during rollback: %v", len(chunkIDs), delIndexErr)
+ } else {
+ logger.Debugf(ctx, "Successfully deleted indices for %d chunks", len(chunkIDs))
+ }
+ } else {
+ logger.Errorf(ctx, "Failed to create retrieve engine during rollback: %v", engineErr)
+ }
+ }
+ }
+ }
+
+ // 批量删除chunks,提高回滚效率
+ chunkIDs := make([]string, 0, len(createdChunks))
+ for _, chunk := range createdChunks {
+ chunkIDs = append(chunkIDs, chunk.ID)
+ }
+ if delErr := s.chunkService.DeleteChunks(ctx, chunkIDs); delErr != nil {
+ logger.Errorf(ctx, "Failed to delete %d chunks during rollback: %v", len(chunkIDs), delErr)
+ } else {
+ logger.Debugf(ctx, "Successfully rolled back %d chunks", len(chunkIDs))
+ }
+ }
+ }()
+
+ kb, err = s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return err
}
kb.EnsureDefaults()
- tenantID := ctx.Value(types.TenantIDContextKey).(uint)
+
+ // 获取embedding模型,用于后续清理索引
+ embeddingModel, err = s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
+ if err != nil {
+ return fmt.Errorf("failed to get embedding model: %w", err)
+ }
faqKnowledge, err := s.ensureFAQKnowledge(ctx, tenantID, kb)
if err != nil {
return err
}
+ // 如果模式为replace,则清理知识库
if payload.Mode == types.FAQBatchModeReplace {
if err := s.cleanupFAQKnowledge(ctx, faqKnowledge); err != nil {
return err
@@ -2118,45 +2264,101 @@ func (s *knowledgeService) UpsertFAQEntries(ctx context.Context,
indexMode = kb.FAQConfig.IndexMode
}
- chunks := make([]*types.Chunk, 0, len(payload.Entries))
- for idx := range payload.Entries {
- entry := payload.Entries[idx]
- meta, err := sanitizeFAQEntryPayload(&entry)
- if err != nil {
- return err
+ // 分批处理
+ totalEntries := len(payload.Entries)
+ processed := 0
+ batchStartTime := time.Now()
+
+ logger.Infof(ctx, "FAQ import task %s: starting batch processing, total entries: %d, batch size: %d", taskID, totalEntries, faqImportBatchSize)
+
+ for i := 0; i < totalEntries; i += faqImportBatchSize {
+ batchStartTime = time.Now()
+ end := i + faqImportBatchSize
+ if end > totalEntries {
+ end = totalEntries
}
- isEnabled := true
- if entry.IsEnabled != nil {
- isEnabled = *entry.IsEnabled
+
+ batch := payload.Entries[i:end]
+ logger.Infof(ctx, "FAQ import task %s: processing batch %d-%d (%d entries)", taskID, i+1, end, len(batch))
+
+ // 构建chunks
+ buildStartTime := time.Now()
+ chunks := make([]*types.Chunk, 0, len(batch))
+ for idx, entry := range batch {
+ meta, err := sanitizeFAQEntryPayload(&entry)
+ if err != nil {
+ logger.ErrorWithFields(ctx, err, map[string]interface{}{
+ "entry": entry,
+ "task_id": taskID,
+ })
+ return fmt.Errorf("failed to sanitize entry at index %d: %w", i+idx, err)
+ }
+ isEnabled := true
+ if entry.IsEnabled != nil {
+ isEnabled = *entry.IsEnabled
+ }
+ chunk := &types.Chunk{
+ ID: uuid.New().String(),
+ TenantID: tenantID,
+ KnowledgeID: faqKnowledge.ID,
+ KnowledgeBaseID: kb.ID,
+ Content: buildFAQChunkContent(meta, indexMode),
+ ChunkIndex: startIndex + i + idx + 1,
+ IsEnabled: isEnabled,
+ ChunkType: types.ChunkTypeFAQ,
+ TagID: entry.TagID,
+ }
+ if err := chunk.SetFAQMetadata(meta); err != nil {
+ return fmt.Errorf("failed to set FAQ metadata: %w", err)
+ }
+ chunks = append(chunks, chunk)
}
- chunk := &types.Chunk{
- ID: uuid.New().String(),
- TenantID: tenantID,
- KnowledgeID: faqKnowledge.ID,
- KnowledgeBaseID: kb.ID,
- Content: buildFAQChunkContent(meta, indexMode),
- ChunkIndex: startIndex + idx + 1,
- IsEnabled: isEnabled,
- ChunkType: types.ChunkTypeFAQ,
- TagID: entry.TagID,
- }
- if err := chunk.SetFAQMetadata(meta); err != nil {
- return err
+ buildDuration := time.Since(buildStartTime)
+ logger.Debugf(ctx, "FAQ import task %s: batch %d-%d built %d chunks in %v", taskID, i+1, end, len(chunks), buildDuration)
+
+ // 创建chunks
+ createStartTime := time.Now()
+ if err := s.chunkService.CreateChunks(ctx, chunks); err != nil {
+ return fmt.Errorf("failed to create chunks: %w", err)
}
- chunks = append(chunks, chunk)
- }
+ createDuration := time.Since(createStartTime)
+ logger.Infof(ctx, "FAQ import task %s: batch %d-%d created %d chunks in %v", taskID, i+1, end, len(chunks), createDuration)
+ // 记录已创建的chunks(用于失败时回滚)
+ createdChunks = append(createdChunks, chunks...)
- if err := s.chunkService.CreateChunks(ctx, chunks); err != nil {
- return err
- }
+ // 索引chunks
+ indexStartTime := time.Now()
+ // 注意:如果索引失败,defer中的recovery机制会自动回滚已创建的chunks和索引数据
+ if err := s.indexFAQChunks(ctx, kb, faqKnowledge, chunks, embeddingModel, true, false); err != nil {
+ return fmt.Errorf("failed to index chunks: %w", err)
+ }
+ indexDuration := time.Since(indexStartTime)
+ logger.Infof(ctx, "FAQ import task %s: batch %d-%d indexed %d chunks in %v", taskID, i+1, end, len(chunks), indexDuration)
+ // 记录已成功索引的chunks(用于失败时清理索引数据)
+ indexedChunks = append(indexedChunks, chunks...)
+
+ processed += len(batch)
- if err := s.indexFAQChunks(ctx, kb, faqKnowledge, chunks, true); err != nil {
- for _, chunk := range chunks {
- _ = s.chunkService.DeleteChunk(ctx, chunk.ID)
+ // 更新任务进度
+ progressUpdateStartTime := time.Now()
+ progress := int(float64(processed) / float64(totalEntries) * 100)
+ if err := s.updateFAQImportStatus(ctx, taskID, types.FAQImportStatusRunning, progress, totalEntries, processed, ""); err != nil {
+ logger.Errorf(ctx, "Failed to update task progress: %v", err)
}
- return err
+ progressUpdateDuration := time.Since(progressUpdateStartTime)
+ if progressUpdateDuration > 100*time.Millisecond {
+ logger.Warnf(ctx, "FAQ import task %s: progress update took %v (may be slow)", taskID, progressUpdateDuration)
+ }
+
+ batchDuration := time.Since(batchStartTime)
+ logger.Infof(ctx, "FAQ import task %s: batch %d-%d completed in %v (build: %v, create: %v, index: %v, progress: %v), total progress: %d/%d (%d%%)",
+ taskID, i+1, end, batchDuration, buildDuration, createDuration, indexDuration, progressUpdateDuration, processed, totalEntries, progress)
}
+ totalDuration := time.Since(batchStartTime)
+ logger.Infof(ctx, "FAQ import task %s: all batches completed, total: %d entries in %v, avg: %v per entry",
+ taskID, totalEntries, totalDuration, totalDuration/time.Duration(totalEntries))
+
return nil
}
@@ -2228,7 +2430,11 @@ func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
if err != nil {
return err
}
- return s.indexFAQChunks(ctx, kb, faqKnowledge, []*types.Chunk{chunk}, false)
+ embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
+ if err != nil {
+ return err
+ }
+ return s.indexFAQChunks(ctx, kb, faqKnowledge, []*types.Chunk{chunk}, embeddingModel, false, true)
}
// UpdateFAQEntryStatus updates enable status for a FAQ entry.
@@ -2732,6 +2938,63 @@ func (s *knowledgeService) ensureFAQKnowledge(ctx context.Context, tenantID uint
return knowledge, nil
}
+// updateFAQImportStatus 更新FAQ Knowledge的导入任务状态
+func (s *knowledgeService) updateFAQImportStatus(ctx context.Context, knowledgeID string, status types.FAQImportTaskStatus, progress, total, processed int, errorMsg string) error {
+ tenantID := ctx.Value(types.TenantIDContextKey).(uint)
+ knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, knowledgeID)
+ if err != nil {
+ return err
+ }
+
+ // 更新ParseStatus:将FAQImportTaskStatus映射到ParseStatus
+ // pending -> "pending", running -> "processing", success -> "completed", failed -> "failed"
+ parseStatus := string(status)
+ if status == types.FAQImportStatusRunning {
+ parseStatus = "processing" // 使用"processing"以兼容文档类型的ParseStatus
+ } else if status == types.FAQImportStatusSuccess {
+ parseStatus = "completed"
+ }
+ knowledge.ParseStatus = parseStatus
+ knowledge.UpdatedAt = time.Now()
+
+ // 更新ErrorMessage
+ if errorMsg != "" {
+ knowledge.ErrorMessage = errorMsg
+ } else if status == types.FAQImportStatusSuccess {
+ knowledge.ErrorMessage = "" // 成功时清空错误信息
+ }
+
+ // 更新Metadata中的导入进度信息
+ importMeta := &types.FAQImportMetadata{
+ ImportProgress: progress,
+ ImportTotal: total,
+ ImportProcessed: processed,
+ }
+ metaJSON, err := importMeta.ToJSON()
+ if err != nil {
+ return fmt.Errorf("failed to marshal import metadata: %w", err)
+ }
+ knowledge.Metadata = metaJSON
+
+ return s.repo.UpdateKnowledge(ctx, knowledge)
+}
+
+// getRunningFAQImportTask 获取指定知识库的进行中导入任务
+func (s *knowledgeService) getRunningFAQImportTask(ctx context.Context, kbID string, tenantID uint) (*types.Knowledge, error) {
+ faqKnowledge, err := s.findFAQKnowledge(ctx, tenantID, kbID)
+ if err != nil {
+ return nil, err
+ }
+ if faqKnowledge == nil {
+ return nil, errors.New("FAQ knowledge not found")
+ }
+ // 检查ParseStatus是否为pending或processing(进行中状态)
+ if faqKnowledge.ParseStatus == "pending" || faqKnowledge.ParseStatus == "processing" {
+ return faqKnowledge, nil
+ }
+ return nil, nil
+}
+
func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.KnowledgeBase) (*types.FAQEntry, error) {
meta, err := chunk.FAQMetadata()
if err != nil {
@@ -2826,7 +3089,6 @@ func (s *knowledgeService) buildFAQIndexInfoList(ctx context.Context, kb *types.
questionIndexMode = kb.FAQConfig.QuestionIndexMode
}
}
- logger.Infof(ctx, "buildFAQIndexInfoList: indexMode: %s, questionIndexMode: %s", indexMode, questionIndexMode)
meta, err := chunk.FAQMetadata()
if err != nil {
@@ -2896,27 +3158,29 @@ func (s *knowledgeService) buildFAQIndexInfoList(ctx context.Context, kb *types.
KnowledgeBaseID: chunk.KnowledgeBaseID,
})
}
- logger.Infof(ctx, "buildFAQIndexInfoList: indexInfoList: %v", indexInfoList)
return indexInfoList, nil
}
func (s *knowledgeService) indexFAQChunks(ctx context.Context,
- kb *types.KnowledgeBase, knowledge *types.Knowledge, chunks []*types.Chunk, adjustStorage bool,
+ kb *types.KnowledgeBase, knowledge *types.Knowledge,
+ chunks []*types.Chunk, embeddingModel embedding.Embedder,
+ adjustStorage bool, needDelete bool,
) error {
if len(chunks) == 0 {
return nil
}
- embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
- if err != nil {
- return err
- }
+ indexStartTime := time.Now()
+ logger.Debugf(ctx, "indexFAQChunks: starting to index %d chunks", len(chunks))
+
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.RetrieverEngines.Engines)
if err != nil {
return err
}
+ // 构建索引信息
+ buildIndexInfoStartTime := time.Now()
indexInfo := make([]*types.IndexInfo, 0)
chunkIDs := make([]string, 0, len(chunks))
for _, chunk := range chunks {
@@ -2927,33 +3191,68 @@ func (s *knowledgeService) indexFAQChunks(ctx context.Context,
indexInfo = append(indexInfo, infoList...)
chunkIDs = append(chunkIDs, chunk.ID)
}
+ buildIndexInfoDuration := time.Since(buildIndexInfoStartTime)
+ logger.Debugf(ctx, "indexFAQChunks: built %d index info entries for %d chunks in %v", len(indexInfo), len(chunks), buildIndexInfoDuration)
var size int64
if adjustStorage {
+ estimateStartTime := time.Now()
size = retrieveEngine.EstimateStorageSize(ctx, embeddingModel, indexInfo)
+ estimateDuration := time.Since(estimateStartTime)
+ logger.Debugf(ctx, "indexFAQChunks: estimated storage size %d bytes in %v", size, estimateDuration)
if tenantInfo.StorageQuota > 0 && tenantInfo.StorageUsed+size > tenantInfo.StorageQuota {
return types.NewStorageQuotaExceededError()
}
}
- if err := retrieveEngine.DeleteByChunkIDList(ctx, chunkIDs, embeddingModel.GetDimensions()); err != nil {
- logger.Warnf(ctx, "Delete FAQ vectors failed: %v", err)
+ // 删除旧向量
+ var deleteDuration time.Duration
+ if needDelete {
+ deleteStartTime := time.Now()
+ if err := retrieveEngine.DeleteByChunkIDList(ctx, chunkIDs, embeddingModel.GetDimensions()); err != nil {
+ logger.Warnf(ctx, "Delete FAQ vectors failed: %v", err)
+ }
+ deleteDuration = time.Since(deleteStartTime)
+ if deleteDuration > 100*time.Millisecond {
+ logger.Debugf(ctx, "indexFAQChunks: deleted old vectors for %d chunks in %v", len(chunkIDs), deleteDuration)
+ }
}
+
+ // 批量索引(这里可能是性能瓶颈)
+ batchIndexStartTime := time.Now()
if err := retrieveEngine.BatchIndex(ctx, embeddingModel, indexInfo); err != nil {
return err
}
+ batchIndexDuration := time.Since(batchIndexStartTime)
+ logger.Debugf(ctx, "indexFAQChunks: batch indexed %d index info entries in %v (avg: %v per entry)", len(indexInfo), batchIndexDuration, batchIndexDuration/time.Duration(len(indexInfo)))
if adjustStorage && size > 0 {
+ adjustStartTime := time.Now()
if err := s.tenantRepo.AdjustStorageUsed(ctx, tenantInfo.ID, size); err == nil {
tenantInfo.StorageUsed += size
}
knowledge.StorageSize += size
+ adjustDuration := time.Since(adjustStartTime)
+ if adjustDuration > 50*time.Millisecond {
+ logger.Debugf(ctx, "indexFAQChunks: adjusted storage in %v", adjustDuration)
+ }
}
+ updateStartTime := time.Now()
now := time.Now()
knowledge.UpdatedAt = now
knowledge.ProcessedAt = &now
- return s.repo.UpdateKnowledge(ctx, knowledge)
+ err = s.repo.UpdateKnowledge(ctx, knowledge)
+ updateDuration := time.Since(updateStartTime)
+ if updateDuration > 50*time.Millisecond {
+ logger.Debugf(ctx, "indexFAQChunks: updated knowledge in %v", updateDuration)
+ }
+
+ totalDuration := time.Since(indexStartTime)
+ logger.Debugf(ctx, "indexFAQChunks: completed indexing %d chunks in %v (build: %v, delete: %v, batchIndex: %v, update: %v)",
+ len(chunks), totalDuration, buildIndexInfoDuration, deleteDuration, batchIndexDuration, updateDuration)
+
+ return err
}
func (s *knowledgeService) deleteFAQChunkVectors(ctx context.Context,
diff --git a/internal/application/service/knowledgebase.go b/internal/application/service/knowledgebase.go
index 29d5cd941..70c243cee 100644
--- a/internal/application/service/knowledgebase.go
+++ b/internal/application/service/knowledgebase.go
@@ -98,8 +98,6 @@ func (s *knowledgeBaseService) GetKnowledgeBaseByID(ctx context.Context, id stri
return nil, errors.New("knowledge base ID cannot be empty")
}
- logger.Infof(ctx, "Retrieving knowledge base, ID: %s", id)
-
kb, err := s.repo.GetKnowledgeBaseByID(ctx, id)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -109,15 +107,12 @@ func (s *knowledgeBaseService) GetKnowledgeBaseByID(ctx context.Context, id stri
}
kb.EnsureDefaults()
-
- logger.Infof(ctx, "Knowledge base retrieved successfully, ID: %s, name: %s", kb.ID, kb.Name)
return kb, nil
}
// ListKnowledgeBases returns all knowledge bases for a tenant
func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types.KnowledgeBase, error) {
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
- logger.Infof(ctx, "Retrieving knowledge base list for tenant, tenant ID: %d", tenantID)
kbs, err := s.repo.ListKnowledgeBasesByTenantID(ctx, tenantID)
if err != nil {
@@ -134,7 +129,7 @@ func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types
// Query knowledge count and chunk count for each knowledge base
for _, kb := range kbs {
kb.EnsureDefaults()
-
+
// Get knowledge count
if kb.Type == types.KnowledgeBaseTypeDocument {
knowledgeCount, err := s.kgRepo.CountKnowledgeByKnowledgeBaseID(ctx, tenantID, kb.ID)
@@ -152,14 +147,21 @@ func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types
kb.ChunkCount = chunkCount
}
}
- }
- logger.Infof(
- ctx,
- "Knowledge base list retrieved successfully, tenant ID: %d, knowledge base count: %d",
- tenantID,
- len(kbs),
- )
+ // Check if there is a processing import task
+ processingCount, err := s.kgRepo.CountKnowledgeByStatus(
+ ctx,
+ tenantID,
+ kb.ID,
+ []string{"pending", "processing"},
+ )
+ if err != nil {
+ logger.Warnf(ctx, "Failed to check processing status for knowledge base %s: %v", kb.ID, err)
+ } else {
+ kb.IsProcessing = processingCount > 0
+ kb.ProcessingCount = processingCount
+ }
+ }
return kbs, nil
}
@@ -241,7 +243,7 @@ func (s *knowledgeBaseService) DeleteKnowledgeBase(ctx context.Context, id strin
}
logger.Infof(ctx, "Deleting all knowledge entries and their resources")
-
+
// Delete embeddings from vector store
logger.Infof(ctx, "Deleting embeddings from vector store")
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
@@ -532,16 +534,9 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
}
logger.Infof(ctx, "Result count before deduplication: %d", len(matchResults))
- for _, chunk := range matchResults {
- logger.Infof(ctx, "Before deduplication: Chunk: %s, Content: %s, Score: %f, MatchType: %d", chunk.ChunkID, chunk.Content, chunk.Score, chunk.MatchType)
- }
-
// First, try standard deduplication
deduplicatedChunks := common.DeduplicateWithScore(func(r *types.IndexWithScore) string { return r.ChunkID }, matchResults...)
logger.Infof(ctx, "Result count after deduplication: %d", len(deduplicatedChunks))
- for _, chunk := range deduplicatedChunks {
- logger.Infof(ctx, "After deduplication: Chunk: %s, Content: %s, Score: %f, MatchType: %d", chunk.ChunkID, chunk.Content, chunk.Score, chunk.MatchType)
- }
kb.EnsureDefaults()
@@ -582,8 +577,6 @@ func (s *knowledgeBaseService) iterativeRetrieveWithDeduplication(ctx context.Co
uniqueChunks := make(map[string]*types.IndexWithScore)
for i := 0; i < maxIterations; i++ {
- logger.Infof(ctx, "Iterative retrieval iteration %d, TopK: %d", i+1, currentTopK)
-
// Update TopK in retrieve params
updatedParams := make([]types.RetrieveParams, len(retrieveParams))
for j := range retrieveParams {
diff --git a/internal/application/service/retriever/keywords_vector_hybrid_indexer.go b/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
index ff8ecf6ca..03279533c 100644
--- a/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
+++ b/internal/application/service/retriever/keywords_vector_hybrid_indexer.go
@@ -77,13 +77,15 @@ func (v *KeywordsVectorHybridRetrieveEngineService) BatchIndex(ctx context.Conte
embeddings, err = embedder.BatchEmbedWithPool(ctx, embedder, contentList)
if err == nil {
break
+ } else {
+ logger.Errorf(ctx, "BatchEmbedWithPool failed: %v", err)
+ time.Sleep(100 * time.Millisecond)
}
- time.Sleep(100 * time.Millisecond)
}
if err != nil {
return err
}
- batchSize := 20
+ batchSize := 40
for i, indexChunk := range utils.ChunkSlice(indexInfoList, batchSize) {
embeddingMap := make(map[string][]float32)
for j, indexInfo := range indexChunk {
diff --git a/internal/application/service/session.go b/internal/application/service/session.go
index 803c9c58b..841208457 100644
--- a/internal/application/service/session.go
+++ b/internal/application/service/session.go
@@ -125,8 +125,6 @@ func (s *sessionService) GetSession(ctx context.Context, id string) (*types.Sess
// GetSessionsByTenant retrieves all sessions for the current tenant
func (s *sessionService) GetSessionsByTenant(ctx context.Context) ([]*types.Session, error) {
- logger.Info(ctx, "Start retrieving all sessions for tenant")
-
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
logger.Infof(ctx, "Retrieving all sessions for tenant, tenant ID: %d", tenantID)
@@ -150,13 +148,8 @@ func (s *sessionService) GetSessionsByTenant(ctx context.Context) ([]*types.Sess
func (s *sessionService) GetPagedSessionsByTenant(ctx context.Context,
pagination *types.Pagination,
) (*types.PageResult, error) {
- logger.Info(ctx, "Start retrieving paged sessions for tenant")
-
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
- logger.Infof(ctx, "Retrieving paged sessions for tenant, tenant ID: %d, page: %d, page size: %d",
- tenantID, pagination.Page, pagination.PageSize)
-
// Get paged sessions from repository
sessions, total, err := s.sessionRepo.GetPagedByTenantID(ctx, tenantID, pagination)
if err != nil {
@@ -168,22 +161,17 @@ func (s *sessionService) GetPagedSessionsByTenant(ctx context.Context,
return nil, err
}
- logger.Infof(ctx, "Tenant paged sessions retrieved successfully, tenant ID: %d, total: %d", tenantID, total)
return types.NewPageResult(total, pagination, sessions), nil
}
// UpdateSession updates an existing session's properties
func (s *sessionService) UpdateSession(ctx context.Context, session *types.Session) error {
- logger.Info(ctx, "Start updating session")
-
// Validate session ID
if session.ID == "" {
logger.Error(ctx, "Failed to update session: session ID cannot be empty")
return errors.New("session id is required")
}
- logger.Infof(ctx, "Updating session, ID: %s, tenant ID: %d", session.ID, session.TenantID)
-
// Update session in repository
err := s.sessionRepo.Update(ctx, session)
if err != nil {
@@ -200,8 +188,6 @@ func (s *sessionService) UpdateSession(ctx context.Context, session *types.Sessi
// DeleteSession removes a session by its ID
func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
- logger.Info(ctx, "Start deleting session")
-
// Validate session ID
if id == "" {
logger.Error(ctx, "Failed to delete session: session ID cannot be empty")
@@ -210,7 +196,6 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
- logger.Infof(ctx, "Deleting session, ID: %s, tenant ID: %d", id, tenantID)
// Cleanup temporary KB stored in Redis for this session
if err := s.DeleteWebSearchTempKBState(ctx, id); err != nil {
@@ -227,7 +212,6 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
return err
}
- logger.Infof(ctx, "Session deleted successfully, ID: %s", id)
return nil
}
@@ -235,8 +219,6 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
func (s *sessionService) GenerateTitle(ctx context.Context,
session *types.Session, messages []types.Message,
) (string, error) {
- logger.Info(ctx, "Start generating session title")
-
if session == nil {
logger.Error(ctx, "Failed to generate title: session cannot be empty")
return "", errors.New("session cannot be empty")
@@ -244,14 +226,12 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
// Skip if title already exists
if session.Title != "" {
- logger.Infof(ctx, "Session already has a title, session ID: %s, title: %s", session.ID, session.Title)
return session.Title, nil
}
var err error
// Get the first user message, either from provided messages or repository
var message *types.Message
if len(messages) == 0 {
- logger.Info(ctx, "Message list is empty, getting the first user message")
message, err = s.messageRepo.GetFirstMessageOfUser(ctx, session.ID)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -260,7 +240,6 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
return "", err
}
} else {
- logger.Info(ctx, "Searching for user message in message list")
for _, m := range messages {
if m.Role == "user" {
message = &m
@@ -278,7 +257,6 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
// Get chat model, use default if SummaryModelID is empty
modelID := session.SummaryModelID
if modelID == "" {
- logger.Info(ctx, "Session SummaryModelID is empty, trying to get default chat model")
// Try to get an available KnowledgeQA model
models, err := s.modelService.ListModels(ctx)
if err != nil {
@@ -299,7 +277,6 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
}
}
- logger.Infof(ctx, "Getting chat model, model ID: %s", modelID)
chatModel, err := s.modelService.GetChatModel(ctx, modelID)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -309,7 +286,6 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
}
// Prepare messages for title generation
- logger.Info(ctx, "Preparing to generate session title")
var chatMessages []chat.Message
chatMessages = append(chatMessages,
chat.Message{Role: "system", Content: s.cfg.Conversation.GenerateSessionTitlePrompt},
@@ -320,7 +296,6 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
// Call model to generate title
thinking := false
- logger.Info(ctx, "Calling model to generate title")
response, err := chatModel.Chat(ctx, chatMessages, &chat.ChatOptions{
Temperature: 0.3,
Thinking: &thinking,
@@ -332,17 +307,14 @@ func (s *sessionService) GenerateTitle(ctx context.Context,
// Process and store the generated title
session.Title = strings.TrimPrefix(response.Content, "<think>\n\n</think>")
- logger.Infof(ctx, "Title generated successfully: %s", session.Title)
// Update session with new title
- logger.Info(ctx, "Updating session title")
err = s.sessionRepo.Update(ctx, session)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
return "", err
}
- logger.Infof(ctx, "Session title updated successfully, ID: %s, title: %s", session.ID, session.Title)
return session.Title, nil
}
@@ -363,11 +335,8 @@ func (s *sessionService) GenerateTitleAsync(ctx context.Context, session *types.
bgCtx = context.WithValue(bgCtx, types.RequestIDContextKey, requestID)
}
- logger.Info(bgCtx, "Starting async title generation")
-
// Skip if title already exists
if session.Title != "" {
- logger.Infof(bgCtx, "Session already has a title, skipping generation, session ID: %s", session.ID)
return
}
diff --git a/internal/application/service/tenant.go b/internal/application/service/tenant.go
index 4c3ea7369..d9353ed7b 100644
--- a/internal/application/service/tenant.go
+++ b/internal/application/service/tenant.go
@@ -81,15 +81,11 @@ func (s *tenantService) CreateTenant(ctx context.Context, tenant *types.Tenant)
// GetTenantByID retrieves a tenant by their ID
func (s *tenantService) GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error) {
- logger.Info(ctx, "Start retrieving tenant")
-
if id == 0 {
logger.Error(ctx, "Tenant ID cannot be 0")
return nil, errors.New("tenant ID cannot be 0")
}
- logger.Infof(ctx, "Retrieving tenant, ID: %d", id)
-
tenant, err := s.repo.GetTenantByID(ctx, id)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -98,14 +94,11 @@ func (s *tenantService) GetTenantByID(ctx context.Context, id uint) (*types.Tena
return nil, err
}
- logger.Infof(ctx, "Tenant retrieved successfully, ID: %d, name: %s", tenant.ID, tenant.Name)
return tenant, nil
}
// ListTenants retrieves a list of all tenants
func (s *tenantService) ListTenants(ctx context.Context) ([]*types.Tenant, error) {
- logger.Info(ctx, "Start retrieving tenant list")
-
tenants, err := s.repo.ListTenants(ctx)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
@@ -192,8 +185,6 @@ func (s *tenantService) UpdateAPIKey(ctx context.Context, id uint) (string, erro
return "", errors.New("tenant ID cannot be 0")
}
- logger.Infof(ctx, "Retrieving tenant information, ID: %d", id)
-
tenant, err := s.repo.GetTenantByID(ctx, id)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
diff --git a/internal/handler/auth.go b/internal/handler/auth.go
index 622e9fb7e..463bb102a 100644
--- a/internal/handler/auth.go
+++ b/internal/handler/auth.go
@@ -217,8 +217,6 @@ func (h *AuthHandler) RefreshToken(c *gin.Context) {
func (h *AuthHandler) GetCurrentUser(c *gin.Context) {
ctx := c.Request.Context()
- logger.Debugf(ctx, "Get current user info")
-
// Get current user from service (which extracts from context)
user, err := h.userService.GetCurrentUser(ctx)
if err != nil {
@@ -235,12 +233,9 @@ func (h *AuthHandler) GetCurrentUser(c *gin.Context) {
if err != nil {
logger.Warnf(ctx, "Failed to get tenant info for user %s, tenant ID %d: %v", user.Email, user.TenantID, err)
// Don't fail the request if tenant info is not available
- } else {
- logger.Debugf(ctx, "Retrieved tenant info for user %s: %s", user.Email, tenant.Name)
}
}
- logger.Debugf(ctx, "Retrieved current user info: %s", user.Email)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": gin.H{
diff --git a/internal/handler/faq.go b/internal/handler/faq.go
index 074bfc82b..57c08b681 100644
--- a/internal/handler/faq.go
+++ b/internal/handler/faq.go
@@ -46,7 +46,7 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
})
}
-// UpsertEntries appends or replaces FAQ entries in batch.
+// UpsertEntries appends or replaces FAQ entries in batch asynchronously.
func (h *FAQHandler) UpsertEntries(c *gin.Context) {
ctx := c.Request.Context()
var req types.FAQBatchUpsertPayload
@@ -56,7 +56,8 @@ func (h *FAQHandler) UpsertEntries(c *gin.Context) {
return
}
- if err := h.knowledgeService.UpsertFAQEntries(ctx, c.Param("id"), &req); err != nil {
+ taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, c.Param("id"), &req)
+ if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
@@ -64,6 +65,9 @@ func (h *FAQHandler) UpsertEntries(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
+ "data": gin.H{
+ "task_id": taskID,
+ },
})
}
diff --git a/internal/handler/knowledgebase.go b/internal/handler/knowledgebase.go
index 29593b1fd..724923b3b 100644
--- a/internal/handler/knowledgebase.go
+++ b/internal/handler/knowledgebase.go
@@ -113,8 +113,6 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
return nil, "", errors.NewBadRequestError("Knowledge base ID cannot be empty")
}
- logger.Infof(ctx, "Retrieving knowledge base, ID: %s", id)
-
// Verify tenant has permission to access this knowledge base
kb, err := h.service.GetKnowledgeBaseByID(ctx, id)
if err != nil {
@@ -138,17 +136,12 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
// GetKnowledgeBase handles requests to retrieve a knowledge base by ID
func (h *KnowledgeBaseHandler) GetKnowledgeBase(c *gin.Context) {
- ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving knowledge base")
-
// Validate and get the knowledge base
- kb, id, err := h.validateAndGetKnowledgeBase(c)
+ kb, _, err := h.validateAndGetKnowledgeBase(c)
if err != nil {
c.Error(err)
return
}
-
- logger.Infof(ctx, "Retrieved knowledge base successfully, ID: %s, name: %s", id, kb.Name)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": kb,
@@ -159,18 +152,6 @@ func (h *KnowledgeBaseHandler) GetKnowledgeBase(c *gin.Context) {
func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving knowledge base list")
-
- // Get tenant ID from context
- tenantID, exists := c.Get(types.TenantIDContextKey.String())
- if !exists {
- logger.Error(ctx, "Failed to get tenant ID")
- c.Error(errors.NewUnauthorizedError("Unauthorized"))
- return
- }
-
- logger.Infof(ctx, "Retrieving knowledge base list for tenant, tenant ID: %d", tenantID.(uint))
-
// Get all knowledge bases for this tenant
kbs, err := h.service.ListKnowledgeBases(ctx)
if err != nil {
@@ -179,11 +160,6 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
return
}
- logger.Infof(
- ctx,
- "Retrieved knowledge base list successfully, tenant ID: %d, total: %d knowledge bases",
- tenantID.(uint), len(kbs),
- )
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": kbs,
@@ -269,8 +245,6 @@ type CopyKnowledgeBaseRequest struct {
func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start copy knowledge base")
-
var req CopyKnowledgeBaseRequest
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to parse request parameters", err)
@@ -278,8 +252,6 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
return
}
- logger.Infof(ctx, "Copy knowledge base, ID: %s to ID: %s", req.SourceID, req.TargetID)
-
go func(ctx context.Context) {
err := h.knowledgeService.CloneKnowledgeBase(ctx, req.SourceID, req.TargetID)
if err != nil {
@@ -289,7 +261,6 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
logger.Infof(ctx, "Knowledge base copy from ID: %s to ID: %s successfully", req.SourceID, req.TargetID)
}(logger.CloneContext(ctx))
- logger.Infof(ctx, "Knowledge base start copy from ID: %s to ID: %s", req.SourceID, req.TargetID)
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "Knowledge base copy successfully",
diff --git a/internal/handler/session/handler.go b/internal/handler/session/handler.go
index 0ba148423..5d10ba02d 100644
--- a/internal/handler/session/handler.go
+++ b/internal/handler/session/handler.go
@@ -226,8 +226,6 @@ func (h *Handler) GetSession(c *gin.Context) {
func (h *Handler) GetSessionsByTenant(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving all sessions for tenant")
-
// Parse pagination parameters from query
var pagination types.Pagination
if err := c.ShouldBindQuery(&pagination); err != nil {
@@ -236,8 +234,6 @@ func (h *Handler) GetSessionsByTenant(c *gin.Context) {
return
}
- logger.Debugf(ctx, "Using pagination parameters: page=%d, page_size=%d", pagination.Page, pagination.PageSize)
-
// Use paginated query to get sessions
result, err := h.sessionService.GetPagedSessionsByTenant(ctx, &pagination)
if err != nil {
@@ -247,7 +243,6 @@ func (h *Handler) GetSessionsByTenant(c *gin.Context) {
}
// Return sessions with pagination data
- logger.Infof(ctx, "Successfully retrieved tenant sessions, total: %d", result.Total)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": result.Data,
@@ -261,8 +256,6 @@ func (h *Handler) GetSessionsByTenant(c *gin.Context) {
func (h *Handler) UpdateSession(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start updating session")
-
// Get session ID from URL parameter
id := c.Param("id")
if id == "" {
@@ -287,8 +280,6 @@ func (h *Handler) UpdateSession(c *gin.Context) {
return
}
- // Set session ID and tenant ID
- logger.Infof(ctx, "Updating session, ID: %s, tenant ID: %d", id, tenantID.(uint))
session.ID = id
session.TenantID = tenantID.(uint)
@@ -316,8 +307,6 @@ func (h *Handler) UpdateSession(c *gin.Context) {
func (h *Handler) DeleteSession(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start deleting session")
-
// Get session ID from URL parameter
id := c.Param("id")
if id == "" {
@@ -327,7 +316,6 @@ func (h *Handler) DeleteSession(c *gin.Context) {
}
// Call service to delete session
- logger.Infof(ctx, "Deleting session, ID: %s", id)
if err := h.sessionService.DeleteSession(ctx, id); err != nil {
if err == errors.ErrSessionNotFound {
logger.Warnf(ctx, "Session not found, ID: %s", id)
@@ -340,7 +328,6 @@ func (h *Handler) DeleteSession(c *gin.Context) {
}
// Return success message
- logger.Infof(ctx, "Session deleted successfully, ID: %s", id)
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "Session deleted successfully",
diff --git a/internal/handler/tenant.go b/internal/handler/tenant.go
index 3d8116009..c2f5a8977 100644
--- a/internal/handler/tenant.go
+++ b/internal/handler/tenant.go
@@ -84,8 +84,6 @@ func (h *TenantHandler) CreateTenant(c *gin.Context) {
func (h *TenantHandler) GetTenant(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving tenant")
-
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
logger.Errorf(ctx, "Invalid tenant ID: %s", c.Param("id"))
@@ -93,8 +91,6 @@ func (h *TenantHandler) GetTenant(c *gin.Context) {
return
}
- logger.Infof(ctx, "Retrieving tenant, ID: %d", id)
-
tenant, err := h.service.GetTenantByID(ctx, uint(id))
if err != nil {
// Check if this is an application-specific error
@@ -108,7 +104,6 @@ func (h *TenantHandler) GetTenant(c *gin.Context) {
return
}
- logger.Infof(ctx, "Retrieved tenant successfully, ID: %d, Name: %s", tenant.ID, tenant.Name)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": tenant,
@@ -207,8 +202,6 @@ func (h *TenantHandler) DeleteTenant(c *gin.Context) {
func (h *TenantHandler) ListTenants(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving tenant list")
-
tenants, err := h.service.ListTenants(ctx)
if err != nil {
// Check if this is an application-specific error
@@ -222,7 +215,6 @@ func (h *TenantHandler) ListTenants(c *gin.Context) {
return
}
- logger.Infof(ctx, "Retrieved tenant list successfully, Total: %d tenants", len(tenants))
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": gin.H{
@@ -246,8 +238,6 @@ type AgentConfigRequest struct {
// This is the global agent configuration that applies to all sessions by default
func (h *TenantHandler) GetTenantAgentConfig(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving tenant agent config")
-
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
@@ -558,8 +548,6 @@ func validateConversationConfig(req *types.ConversationConfig) error {
// This is the global conversation configuration that applies to normal mode sessions by default
func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
ctx := c.Request.Context()
- logger.Info(ctx, "Start retrieving tenant conversation config")
-
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go
index 346b91ec8..55c05af10 100644
--- a/internal/middleware/logger.go
+++ b/internal/middleware/logger.go
@@ -1,7 +1,11 @@
package middleware
import (
+ "bytes"
"context"
+ "io"
+ "regexp"
+ "strings"
"time"
"github.com/Tencent/WeKnora/internal/logger"
@@ -10,6 +14,88 @@ import (
"github.com/google/uuid"
)
+const (
+ maxBodySize = 1024 * 10 // 最大记录10KB的body内容
+)
+
+// loggerResponseBodyWriter 自定义ResponseWriter用于捕获响应内容(用于logger中间件)
+type loggerResponseBodyWriter struct {
+ gin.ResponseWriter
+ body *bytes.Buffer
+}
+
+// Write 重写Write方法,同时写入buffer和原始writer
+func (r loggerResponseBodyWriter) Write(b []byte) (int, error) {
+ r.body.Write(b)
+ return r.ResponseWriter.Write(b)
+}
+
+// sanitizeBody 清理敏感信息
+func sanitizeBody(body string) string {
+ result := body
+ // 替换常见的敏感字段(JSON格式)
+ sensitivePatterns := []struct {
+ pattern string
+ replacement string
+ }{
+ {`"password"\s*:\s*"[^"]*"`, `"password":"***"`},
+ {`"token"\s*:\s*"[^"]*"`, `"token":"***"`},
+ {`"access_token"\s*:\s*"[^"]*"`, `"access_token":"***"`},
+ {`"refresh_token"\s*:\s*"[^"]*"`, `"refresh_token":"***"`},
+ {`"authorization"\s*:\s*"[^"]*"`, `"authorization":"***"`},
+ {`"api_key"\s*:\s*"[^"]*"`, `"api_key":"***"`},
+ {`"secret"\s*:\s*"[^"]*"`, `"secret":"***"`},
+ {`"apikey"\s*:\s*"[^"]*"`, `"apikey":"***"`},
+ {`"apisecret"\s*:\s*"[^"]*"`, `"apisecret":"***"`},
+ }
+
+ for _, p := range sensitivePatterns {
+ re := regexp.MustCompile(p.pattern)
+ result = re.ReplaceAllString(result, p.replacement)
+ }
+
+ return result
+}
+
+// readRequestBody 读取请求体(限制大小用于日志,但完整读取用于重置)
+func readRequestBody(c *gin.Context) string {
+ if c.Request.Body == nil {
+ return ""
+ }
+
+ // 检查Content-Type,只记录JSON类型
+ contentType := c.GetHeader("Content-Type")
+ if !strings.Contains(contentType, "application/json") &&
+ !strings.Contains(contentType, "application/x-www-form-urlencoded") &&
+ !strings.Contains(contentType, "text/") {
+ return "[非文本类型,已跳过]"
+ }
+
+ // 完整读取body内容(不限制大小),因为需要完整重置给后续handler使用
+ bodyBytes, err := io.ReadAll(c.Request.Body)
+ if err != nil {
+ return "[读取请求体失败]"
+ }
+
+ // 重置request body,使用完整内容,确保后续handler能读取到完整数据
+ c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
+
+ // 用于日志的body(限制大小)
+ var logBodyBytes []byte
+ if len(bodyBytes) > maxBodySize {
+ logBodyBytes = bodyBytes[:maxBodySize]
+ } else {
+ logBodyBytes = bodyBytes
+ }
+
+ bodyStr := string(logBodyBytes)
+ if len(bodyBytes) > maxBodySize {
+ bodyStr += "... [内容过长,已截断]"
+ }
+
+ return sanitizeBody(bodyStr)
+}
+
// RequestID middleware adds a unique request ID to the context
func RequestID() gin.HandlerFunc {
return func(c *gin.Context) {
@@ -41,13 +127,27 @@ func RequestID() gin.HandlerFunc {
}
}
-// Logger middleware logs request details with request ID
+// Logger middleware logs request details with request ID, input and output
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
+ // 读取请求体(在Next之前读取,因为Next会消费body)
+ var requestBody string
+ if c.Request.Method == "POST" || c.Request.Method == "PUT" || c.Request.Method == "PATCH" {
+ requestBody = readRequestBody(c)
+ }
+
+ // 创建响应体捕获器
+ responseBody := &bytes.Buffer{}
+ responseWriter := &loggerResponseBodyWriter{
+ ResponseWriter: c.Writer,
+ body: responseBody,
+ }
+ c.Writer = responseWriter
+
// Process request
c.Next()
@@ -69,15 +169,46 @@ func Logger() gin.HandlerFunc {
path = path + "?" + raw
}
- // Log with request ID
- logger.GetLogger(c).Infof("[%s] %d | %3d | %13v | %15s | %s %s",
- requestID,
- statusCode,
- c.Writer.Size(),
- latency,
- clientIP,
- method,
- path,
- )
+ // 读取响应体
+ responseBodyStr := ""
+ if responseBody.Len() > 0 {
+ // 检查Content-Type,只记录JSON类型
+ contentType := c.Writer.Header().Get("Content-Type")
+ if strings.Contains(contentType, "application/json") ||
+ strings.Contains(contentType, "text/") {
+ bodyBytes := responseBody.Bytes()
+ if len(bodyBytes) > maxBodySize {
+ responseBodyStr = string(bodyBytes[:maxBodySize]) + "... [内容过长,已截断]"
+ } else {
+ responseBodyStr = string(bodyBytes)
+ }
+ responseBodyStr = sanitizeBody(responseBodyStr)
+ } else {
+ responseBodyStr = "[非文本类型,已跳过]"
+ }
+ }
+
+ // 构建日志消息
+ logMsg := logger.GetLogger(c)
+ logMsg = logMsg.WithFields(map[string]interface{}{
+ "request_id": requestID,
+ "method": method,
+ "path": path,
+ "status_code": statusCode,
+ "size": c.Writer.Size(),
+ "latency": latency.String(),
+ "client_ip": clientIP,
+ })
+
+ // 添加请求体(如果有)
+ if requestBody != "" {
+ logMsg = logMsg.WithField("request_body", requestBody)
+ }
+
+ // 添加响应体(如果有)
+ if responseBodyStr != "" {
+ logMsg = logMsg.WithField("response_body", responseBodyStr)
+ }
+ logMsg.Info()
}
}
diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go
index ce7bdc89c..0cf8831b8 100644
--- a/internal/models/chat/remote_api.go
+++ b/internal/models/chat/remote_api.go
@@ -200,11 +200,11 @@ func (c *RemoteAPIChat) buildChatCompletionRequest(messages []Message,
}
// print req
- jsonData, err := json.Marshal(req)
- if err != nil {
- logger.Error(context.Background(), "marshal request: %w", err)
- }
- logger.Infof(context.Background(), "llm request: %s", string(jsonData))
+ // jsonData, err := json.Marshal(req)
+ // if err != nil {
+ // logger.Error(context.Background(), "marshal request: %w", err)
+ // }
+ // logger.Infof(context.Background(), "llm request: %s", string(jsonData))
return req
}
diff --git a/internal/models/embedding/batch.go b/internal/models/embedding/batch.go
index 28491733b..3c8e22694 100644
--- a/internal/models/embedding/batch.go
+++ b/internal/models/embedding/batch.go
@@ -2,6 +2,8 @@ package embedding
import (
"context"
+ "os"
+ "strconv"
"sync"
"github.com/Tencent/WeKnora/internal/models/utils"
@@ -26,7 +28,14 @@ func (e *batchEmbedder) BatchEmbedWithPool(ctx context.Context, model Embedder,
var wg sync.WaitGroup
var mu sync.Mutex // For synchronizing access to error
var firstErr error // Record the first error that occurs
- batchSize := 5
+ batchSizeStr := os.Getenv("BATCH_EMBED_SIZE")
+ if batchSizeStr == "" {
+ batchSizeStr = "5"
+ }
+ batchSize, err := strconv.Atoi(batchSizeStr)
+ if err != nil {
+ return nil, err
+ }
textEmbeddings := utils.MapSlice(texts, func(text string) *textEmbedding {
return &textEmbedding{text: text}
})
diff --git a/internal/stream/redis_manager.go b/internal/stream/redis_manager.go
index b96f6a6e0..72607982c 100644
--- a/internal/stream/redis_manager.go
+++ b/internal/stream/redis_manager.go
@@ -111,7 +111,7 @@ func (r *RedisStreamManager) GetEvents(ctx context.Context, sessionID, messageID
continue
}
events = append(events, event)
-}
+ }
// Calculate next offset
nextOffset := fromOffset + len(results)
diff --git a/internal/types/faq.go b/internal/types/faq.go
index 6c1fe9ac5..688259535 100644
--- a/internal/types/faq.go
+++ b/internal/types/faq.go
@@ -110,6 +110,47 @@ type FAQSearchRequest struct {
MatchCount int `json:"match_count"`
}
+// FAQImportTaskStatus 导入任务状态
+type FAQImportTaskStatus string
+
+const (
+ FAQImportStatusPending FAQImportTaskStatus = "pending"
+ FAQImportStatusRunning FAQImportTaskStatus = "running"
+ FAQImportStatusSuccess FAQImportTaskStatus = "success"
+ FAQImportStatusFailed FAQImportTaskStatus = "failed"
+)
+
+// FAQImportMetadata 存储在Knowledge.Metadata中的FAQ导入任务信息
+type FAQImportMetadata struct {
+ ImportProgress int `json:"import_progress"` // 0-100
+ ImportTotal int `json:"import_total"`
+ ImportProcessed int `json:"import_processed"`
+}
+
+// ToJSON converts the metadata to JSON type.
+func (m *FAQImportMetadata) ToJSON() (JSON, error) {
+ if m == nil {
+ return nil, nil
+ }
+ bytes, err := json.Marshal(m)
+ if err != nil {
+ return nil, err
+ }
+ return JSON(bytes), nil
+}
+
+// ParseFAQImportMetadata parses FAQ import metadata from Knowledge.
+func ParseFAQImportMetadata(k *Knowledge) (*FAQImportMetadata, error) {
+ if k == nil || len(k.Metadata) == 0 {
+ return nil, nil
+ }
+ var metadata FAQImportMetadata
+ if err := json.Unmarshal(k.Metadata, &metadata); err != nil {
+ return nil, err
+ }
+ return &metadata, nil
+}
+
func normalizeStrings(values []string) []string {
if len(values) == 0 {
return nil
diff --git a/internal/types/interfaces/chunk.go b/internal/types/interfaces/chunk.go
index d8f0c0575..c3b228b82 100644
--- a/internal/types/interfaces/chunk.go
+++ b/internal/types/interfaces/chunk.go
@@ -33,6 +33,8 @@ type ChunkRepository interface {
UpdateChunks(ctx context.Context, chunks []*types.Chunk) error
// DeleteChunk deletes a chunk
DeleteChunk(ctx context.Context, tenantID uint, id string) error
+ // DeleteChunks deletes chunks by IDs in batch
+ DeleteChunks(ctx context.Context, tenantID uint, ids []string) error
// DeleteChunksByKnowledgeID deletes chunks by knowledge id
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
@@ -60,6 +62,8 @@ type ChunkService interface {
UpdateChunk(ctx context.Context, chunk *types.Chunk) error
// DeleteChunk deletes a chunk
DeleteChunk(ctx context.Context, id string) error
+ // DeleteChunks deletes chunks by IDs in batch
+ DeleteChunks(ctx context.Context, ids []string) error
// DeleteChunksByKnowledgeID deletes chunks by knowledge id
DeleteChunksByKnowledgeID(ctx context.Context, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
diff --git a/internal/types/interfaces/knowledge.go b/internal/types/interfaces/knowledge.go
index 875f63e73..2f4f63e43 100644
--- a/internal/types/interfaces/knowledge.go
+++ b/internal/types/interfaces/knowledge.go
@@ -56,8 +56,9 @@ type KnowledgeService interface {
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
// When tagID is non-empty, results are filtered by tag_id on FAQ chunks.
ListFAQEntries(ctx context.Context, kbID string, page *types.Pagination, tagID string) (*types.PageResult, error)
- // UpsertFAQEntries imports or appends FAQ entries.
- UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) error
+ // UpsertFAQEntries imports or appends FAQ entries asynchronously.
+ // Returns task ID (Knowledge ID) for tracking import progress.
+ UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) (string, error)
// UpdateFAQEntry updates a single FAQ entry.
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
// UpdateFAQEntryStatusBatch updates enable status for FAQ entries in batch.
@@ -105,4 +106,6 @@ type KnowledgeRepository interface {
UpdateKnowledgeColumn(ctx context.Context, id string, column string, value interface{}) error
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base.
CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error)
+ // CountKnowledgeByStatus counts the number of knowledge items with the specified parse status.
+ CountKnowledgeByStatus(ctx context.Context, tenantID uint, kbID string, parseStatuses []string) (int64, error)
}
diff --git a/internal/types/knowledgebase.go b/internal/types/knowledgebase.go
index f05cdb02d..25f18e5d1 100644
--- a/internal/types/knowledgebase.go
+++ b/internal/types/knowledgebase.go
@@ -73,6 +73,10 @@ type KnowledgeBase struct {
KnowledgeCount int64 `yaml:"knowledge_count" json:"knowledge_count" gorm:"-"`
// Chunk count (not stored in database, calculated on query)
ChunkCount int64 `yaml:"chunk_count" json:"chunk_count" gorm:"-"`
+ // IsProcessing indicates if there is a processing import task (for FAQ type knowledge bases)
+ IsProcessing bool `yaml:"is_processing" json:"is_processing" gorm:"-"`
+ // ProcessingCount indicates the number of knowledge items being processed (for document type knowledge bases)
+ ProcessingCount int64 `yaml:"processing_count" json:"processing_count" gorm:"-"`
}
// KnowledgeBaseConfig represents the knowledge base configuration
diff --git a/migrations/versioned/000012_add_embeddings_kb_id_index.down.sql b/migrations/versioned/000012_add_embeddings_kb_id_index.down.sql
new file mode 100644
index 000000000..cf974786e
--- /dev/null
+++ b/migrations/versioned/000012_add_embeddings_kb_id_index.down.sql
@@ -0,0 +1,13 @@
+-- 000012_add_embeddings_kb_id_index.down.sql
+-- Remove B-tree index on knowledge_base_id from embeddings table
+
+BEGIN;
+
+DROP INDEX IF EXISTS idx_embeddings_knowledge_base_id;
+
+COMMIT;
+
+
+
+
+
diff --git a/migrations/versioned/000012_add_embeddings_kb_id_index.up.sql b/migrations/versioned/000012_add_embeddings_kb_id_index.up.sql
new file mode 100644
index 000000000..2395c188a
--- /dev/null
+++ b/migrations/versioned/000012_add_embeddings_kb_id_index.up.sql
@@ -0,0 +1,15 @@
+-- 000012_add_embeddings_kb_id_index.up.sql
+-- Add B-tree index on knowledge_base_id for embeddings table to improve query performance
+
+BEGIN;
+
+-- Create index for knowledge_base_id to optimize filtering queries
+CREATE INDEX IF NOT EXISTS idx_embeddings_knowledge_base_id
+ ON embeddings(knowledge_base_id);
+
+COMMIT;
+
+
+
+
+