refactor: Update chat and knowledge base handling for improved flexibility

- Removed validation for empty knowledge_base_ids in useStream, allowing backend to manage this case.
- Adjusted routing in KnowledgeBase.vue to decouple session creation from knowledge base ID.
- Updated getCurrentKbId function to simplify knowledge base ID retrieval from the route.
- Enhanced comments for clarity regarding knowledge base handling in chat components.
This commit is contained in:
wizardchen
2025-11-07 01:06:25 +08:00
parent 4fb12545c1
commit 288dfbd19e
4 changed files with 15 additions and 40 deletions
+4 -7
View File
@@ -47,14 +47,11 @@ export function useStream() {
}
// Validate knowledge_base_ids for agent-chat requests
// Note: knowledge_base_ids can be empty if user hasn't selected any, but we allow it
// The backend will handle the case when no knowledge bases are selected
const isAgentChat = params.url === '/api/v1/agent-chat';
if (isAgentChat && params.method === 'POST') {
if (!params.knowledge_base_ids || params.knowledge_base_ids.length === 0) {
error.value = "请至少选择一个知识库";
stopStream();
return;
}
}
// Removed validation - allow empty knowledge_base_ids array
// The backend should handle this case appropriately
try {
let url =
+5 -4
View File
@@ -538,12 +538,13 @@ const handleLogout = () => {
}
const getCurrentKbId = async (): Promise<string | null> => {
// Session 不再和知识库绑定,从当前路由获取知识库ID(仅用于知识库详情页)
let kbId = (route.params as any)?.kbId as string
// 新的路由格式:/platform/chat/:kbId/:chatid,直接从路由参数获取
if (!kbId && route.name === 'chat' && (route.params as any)?.kbId) {
kbId = (route.params as any).kbId
// 如果当前在知识库详情页,返回知识库ID
if (route.name === 'knowledgeBaseDetail' && kbId) {
return kbId
}
return kbId || null
return null
}
const gotopage = async (path: string) => {
+2 -5
View File
@@ -228,11 +228,8 @@ const sendMsg = async (value) => {
scrollToBottom();
// Always use agent mode with unified architecture
// Get knowledge_base_ids from session's agent_config.knowledge_bases to update SessionAgentConfig
let kbIds = [];
if (sessionData.value?.agent_config?.knowledge_bases?.length > 0) {
kbIds = sessionData.value.agent_config.knowledge_bases;
}
// Get knowledge_base_ids from settings store (selected by user via KnowledgeBaseSelector)
const kbIds = useSettingsStoreInstance.settings.selectedKnowledgeBases || [];
// Validate knowledge_base_ids before sending
if (kbIds.length === 0) {
+4 -24
View File
@@ -179,7 +179,7 @@ const getTitle = (session_id: string, value: string) => {
const now = new Date().toISOString();
let obj = {
title: '新会话',
path: `chat/${kbId.value}/${session_id}`,
path: `chat/${session_id}`,
id: session_id,
isMore: false,
isNoTitle: true,
@@ -189,32 +189,12 @@ const getTitle = (session_id: string, value: string) => {
usemenuStore.updataMenuChildren(obj);
usemenuStore.changeIsFirstSession(true);
usemenuStore.changeFirstQuery(value);
router.push(`/platform/chat/${kbId.value}/${session_id}`);
router.push(`/platform/chat/${session_id}`);
};
async function createNewSession(value: string): Promise<void> {
// 优先使用当前页面的知识库ID
let sessionKbId = kbId.value;
// 如果当前页面没有知识库ID,尝试从localStorage获取设置中的知识库ID
if (!sessionKbId) {
const settingsStr = localStorage.getItem("WeKnora_settings");
if (settingsStr) {
try {
const settings = JSON.parse(settingsStr);
sessionKbId = settings.knowledgeBaseId;
} catch (e) {
console.error("解析设置失败:", e);
}
}
}
if (!sessionKbId) {
MessagePlugin.warning("请先选择一个知识库");
return;
}
createSessions({ knowledge_base_id: sessionKbId }).then(res => {
// Session 不再和知识库绑定,直接创建 Session
createSessions({}).then(res => {
if (res.data && res.data.id) {
getTitle(res.data.id, value);
} else {