diff --git a/docs/WeKnora.md b/docs/WeKnora.md index d5d2caf5..f0de040d 100644 --- a/docs/WeKnora.md +++ b/docs/WeKnora.md @@ -305,6 +305,17 @@ Reranker(重排器)是目前RAG领域中非常先进的技术,它们在工 未知情况处理: 如果文档中没有足够的信息来回答问题,请告知“根据所掌握的资料,无法回答这个问题” 引用要求:在回答时,如果引用了某个文档内容,请在句子末尾加上文档编号 +--- + +## 手工知识在线编辑 + +平台的知识库页面新增“上传文档 / 在线编辑”双入口,支持直接在浏览器中撰写并维护 Markdown 知识: + +- 草稿模式用于暂存内容,草稿不会参与检索。 +- 发布操作会自动触发向量化与索引构建。 +- 已发布的 Markdown 知识可再次打开编辑并重新发布。 +- 在对话页面的助手回答末尾提供“添加到知识库”工具,可一键带入当前问答到编辑器中确认后保存。 + diff --git a/frontend/src/App.vue b/frontend/src/App.vue index e7d4d2bf..6559a7d0 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,8 +1,10 @@ + diff --git a/frontend/src/components/menu.vue b/frontend/src/components/menu.vue index a4f68b20..d3867416 100644 --- a/frontend/src/components/menu.vue +++ b/frontend/src/components/menu.vue @@ -39,13 +39,20 @@ {{ kb.name }} - -
- -
+ +
+ +
+
+
+ + + 添加到知识库 + +
@@ -190,8 +196,30 @@ import { marked } from 'marked'; import DOMPurify from 'dompurify'; import ToolResultRenderer from './ToolResultRenderer.vue'; import { getChunkByIdOnly } from '@/api/knowledge-base'; +import { MessagePlugin } from 'tdesign-vue-next'; +import { useUIStore } from '@/stores/ui'; const router = useRouter(); +const uiStore = useUIStore(); + +const TOOL_NAME_I18N: Record = { + search_knowledge: '知识库检索', + knowledge_search: '知识库检索', + web_search: '网络搜索', + web_fetch: '网页抓取', + get_document_info: '获取文档信息', + get_related_chunks: '查找相关片段', + get_related_documents: '查找相关文档', + get_document_content: '获取文档内容', + todo_write: '计划管理', + knowledge_graph_extract: '知识图谱抽取', + thinking: '思考', +}; + +const getLocalizedToolName = (toolName?: string | null): string => { + if (!toolName) return '工具'; + return TOOL_NAME_I18N[toolName] || toolName; +}; // 根元素引用 const rootElement = ref(null); @@ -276,6 +304,7 @@ interface SessionData { const props = defineProps<{ session: SessionData; + userQuery?: string; }>(); // Configure marked for security @@ -1164,7 +1193,8 @@ const getQueryText = (args: any): string => { // Get tool title - prefer summary over description, add query for search tools const getToolTitle = (event: any): string => { if (event.pending) { - return `正在调用 ${event.tool_name}...`; + const localizedName = getLocalizedToolName(event.tool_name); + return `正在调用 ${localizedName}...`; } const toolName = event.tool_name; @@ -1207,7 +1237,8 @@ const getToolTitle = (event: any): string => { // Tool description const getToolDescription = (event: any): string => { if (event.pending) { - return `正在调用 ${event.tool_name}...`; + const localizedName = getLocalizedToolName(event.tool_name); + return `正在调用 ${localizedName}...`; } const success = event.success === true; @@ -1224,7 +1255,8 @@ const getToolDescription = (event: any): string => { } else if (toolName === 'todo_write') { return success ? '更新任务列表' : '更新任务列表失败'; } else { - return success ? `调用 ${toolName}` : `调用 ${toolName} 失败`; + const localizedName = getLocalizedToolName(toolName); + return success ? `调用 ${localizedName}` : `调用 ${localizedName} 失败`; } }; @@ -1255,6 +1287,44 @@ const formatJSON = (obj: any): string => { return String(obj); } }; + +const buildManualMarkdown = (question: string, answer: string): string => { + const safeQuestion = question?.trim() || '(无提问内容)'; + const safeAnswer = answer?.trim() || '(无回答内容)'; + return `# 用户提问\n${safeQuestion}\n\n# 助手回答\n${safeAnswer}\n`; +}; + +const formatManualTitle = (question: string): string => { + if (!question) { + return '会话摘录'; + } + const condensed = question.replace(/\s+/g, ' ').trim(); + if (!condensed) { + return '会话摘录'; + } + return condensed.length > 40 ? `${condensed.slice(0, 40)}...` : condensed; +}; + +const handleAddToKnowledge = (answerEvent: any) => { + const answerContent = (answerEvent?.content || '').trim(); + if (!answerContent) { + MessagePlugin.warning('当前回答为空,无法保存到知识库'); + return; + } + + const question = (props.userQuery || '').trim(); + const manualContent = buildManualMarkdown(question, answerContent); + const manualTitle = formatManualTitle(question); + + uiStore.openManualEditor({ + mode: 'create', + title: manualTitle, + content: manualContent, + status: 'draft', + }); + + MessagePlugin.info('已打开编辑器,请选择知识库后保存'); +}; + diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index eb763e59..01cbb961 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -7,7 +7,7 @@
-
@@ -60,6 +60,17 @@ const loading = ref(false); let fullContent = ref('') let userquery = ref('') const scrollContainer = ref(null) + +const getUserQuery = (index) => { + if (index <= 0) { + return ''; + } + const previous = messagesList[index - 1]; + if (previous && previous.role === 'user') { + return previous.content || ''; + } + return ''; +}; watch([() => route.params], (newvalue) => { isFirstEnter.value = true; if (newvalue[0].chatid) { @@ -538,28 +549,53 @@ const handleAgentChunk = (data) => { break; case 'tool_call': - // Store pending tool call to pair with result later - if (data.data && data.data.tool_name) { - console.log('[Tool Call]', data.data.tool_name); + // Store or update pending tool call to pair with result later + if (data.data && (data.data.tool_name || data.data.tool_call_id)) { + const incomingToolName = data.data.tool_name; + const incomingArguments = data.data.arguments; if (!message.agentEventStream) message.agentEventStream = []; if (!message._pendingToolCalls) message._pendingToolCalls = new Map(); - const toolCallId = data.data.tool_call_id || (data.data.tool_name + '_' + Date.now()); + const toolCallId = data.data.tool_call_id || (incomingToolName ? (incomingToolName + '_' + Date.now()) : null); + if (!toolCallId) { + console.warn('[Tool Call] Received event without identifiable tool_call_id:', data.data); + break; + } - // Create tool call event - const toolCallEvent = { - type: 'tool_call', + console.log('[Tool Call]', { tool_call_id: toolCallId, - tool_name: data.data.tool_name, - arguments: data.data.arguments, - timestamp: Date.now(), - pending: true - }; + tool_name: incomingToolName, + has_arguments: Boolean(incomingArguments) + }); - // Add to event stream - message.agentEventStream.push(toolCallEvent); - message._pendingToolCalls.set(toolCallId, toolCallEvent); + let toolCallEvent = message._pendingToolCalls.get(toolCallId); + if (!toolCallEvent) { + toolCallEvent = message.agentEventStream.find( + (event) => event.type === 'tool_call' && event.tool_call_id === toolCallId + ); + } + + if (toolCallEvent) { + if (incomingToolName) toolCallEvent.tool_name = incomingToolName; + if (incomingArguments) toolCallEvent.arguments = incomingArguments; + toolCallEvent.pending = true; + if (!toolCallEvent.timestamp) { + toolCallEvent.timestamp = Date.now(); + } + message._pendingToolCalls.set(toolCallId, toolCallEvent); + } else { + const newToolCallEvent = { + type: 'tool_call', + tool_call_id: toolCallId, + tool_name: incomingToolName, + arguments: incomingArguments, + timestamp: Date.now(), + pending: true + }; + message.agentEventStream.push(newToolCallEvent); + message._pendingToolCalls.set(toolCallId, newToolCallEvent); + } } break; diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue index aa88a10f..f6fa6b50 100644 --- a/frontend/src/views/knowledge/KnowledgeBase.vue +++ b/frontend/src/views/knowledge/KnowledgeBase.vue @@ -8,7 +8,6 @@ import EmptyKnowledge from '@/components/empty-knowledge.vue'; import { getSessionsList, createSessions, generateSessionsTitle } from "@/api/chat/index"; import { useMenuStore } from '@/stores/menu'; import { useUIStore } from '@/stores/ui'; -import { MessagePlugin } from 'tdesign-vue-next'; import KnowledgeBaseEditorModal from './KnowledgeBaseEditorModal.vue'; const usemenuStore = useMenuStore(); const uiStore = useUIStore(); @@ -53,15 +52,18 @@ const loadKnowledgeFiles = async (kbIdValue: string) => { } const cardList_ = data.map((item: any) => { - item["file_name"] = item.file_name.substring( - 0, - item.file_name.lastIndexOf(".") - ); + const rawName = item.file_name || item.title || item.source || '未命名文档' + const dotIndex = rawName.lastIndexOf('.') + const displayName = dotIndex > 0 ? rawName.substring(0, dotIndex) : rawName + const fileTypeSource = item.file_type || (item.type === 'manual' ? 'MANUAL' : '') return { ...item, + original_file_name: item.file_name, + display_name: displayName, + file_name: displayName, updated_at: formatStringDate(new Date(item.updated_at)), isMore: false, - file_type: item.file_type.toLocaleUpperCase(), + file_type: fileTypeSource ? String(fileTypeSource).toLocaleUpperCase() : '', }; }); @@ -113,7 +115,22 @@ watch(() => cardList.value, (newValue) => { updateStatus(analyzeList) } }, { deep: true }) -type KnowledgeCard = { id: string; parse_status: string; description?: string; file_name?: string; updated_at?: string; file_type?: string; isMore?: boolean }; +type KnowledgeCard = { + id: string; + knowledge_base_id?: string; + parse_status: string; + description?: string; + file_name?: string; + original_file_name?: string; + display_name?: string; + title?: string; + type?: string; + updated_at?: string; + file_type?: string; + isMore?: boolean; + metadata?: any; + error_message?: string; +}; const updateStatus = (analyzeList: KnowledgeCard[]) => { let query = ``; for (let i = 0; i < analyzeList.length; i++) { @@ -152,6 +169,24 @@ const delCard = (index: number, item: KnowledgeCard) => { delDialog.value = true; }; +const manualEditorSuccess = ({ kbId: savedKbId }: { kbId: string; knowledgeId: string; status: 'draft' | 'publish' }) => { + if (savedKbId === kbId.value) { + loadKnowledgeFiles(savedKbId); + } +}; + +const handleManualEdit = (index: number, item: KnowledgeCard) => { + if (cardList.value[index]) { + cardList.value[index].isMore = false; + } + uiStore.openManualEditor({ + mode: 'edit', + kbId: item.knowledge_base_id || kbId.value, + knowledgeId: item.id, + onSuccess: manualEditorSuccess, + }); +}; + const handleScroll = () => { const element = knowledgeScroll.value; if (element) { @@ -225,26 +260,52 @@ async function createNewSession(value: string): Promise {
{{ item.file_name }} -
-
- - {{ - item.parse_status == "failed" ? "解析失败" : "解析中..." - }} +
+ + 解析中...
-
+
+ + 解析失败 +
+
+ 草稿 + 暂存内容,未参与检索 +
+
{{ item.description }}
@@ -292,16 +353,13 @@ async function createNewSession(value: string): Promise { } .card-more .t-popup__content { - width: 160px; - height: 40px; - line-height: 30px; - padding-left: 14px; - cursor: pointer; + width: 180px; + padding: 6px 0; margin-top: 4px !important; color: #000000e6; } .card-more .t-popup__content:hover { - color: #FA5151 !important; + color: inherit !important; }