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:
@@ -47,14 +47,11 @@ export function useStream() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate knowledge_base_ids for agent-chat requests
|
// 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';
|
const isAgentChat = params.url === '/api/v1/agent-chat';
|
||||||
if (isAgentChat && params.method === 'POST') {
|
// Removed validation - allow empty knowledge_base_ids array
|
||||||
if (!params.knowledge_base_ids || params.knowledge_base_ids.length === 0) {
|
// The backend should handle this case appropriately
|
||||||
error.value = "请至少选择一个知识库";
|
|
||||||
stopStream();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let url =
|
let url =
|
||||||
|
|||||||
@@ -538,12 +538,13 @@ const handleLogout = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getCurrentKbId = async (): Promise<string | null> => {
|
const getCurrentKbId = async (): Promise<string | null> => {
|
||||||
|
// Session 不再和知识库绑定,从当前路由获取知识库ID(仅用于知识库详情页)
|
||||||
let kbId = (route.params as any)?.kbId as string
|
let kbId = (route.params as any)?.kbId as string
|
||||||
// 新的路由格式:/platform/chat/:kbId/:chatid,直接从路由参数获取
|
// 如果当前在知识库详情页,返回知识库ID
|
||||||
if (!kbId && route.name === 'chat' && (route.params as any)?.kbId) {
|
if (route.name === 'knowledgeBaseDetail' && kbId) {
|
||||||
kbId = (route.params as any).kbId
|
return kbId
|
||||||
}
|
}
|
||||||
return kbId || null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const gotopage = async (path: string) => {
|
const gotopage = async (path: string) => {
|
||||||
|
|||||||
@@ -228,11 +228,8 @@ const sendMsg = async (value) => {
|
|||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
|
|
||||||
// Always use agent mode with unified architecture
|
// Always use agent mode with unified architecture
|
||||||
// Get knowledge_base_ids from session's agent_config.knowledge_bases to update SessionAgentConfig
|
// Get knowledge_base_ids from settings store (selected by user via KnowledgeBaseSelector)
|
||||||
let kbIds = [];
|
const kbIds = useSettingsStoreInstance.settings.selectedKnowledgeBases || [];
|
||||||
if (sessionData.value?.agent_config?.knowledge_bases?.length > 0) {
|
|
||||||
kbIds = sessionData.value.agent_config.knowledge_bases;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate knowledge_base_ids before sending
|
// Validate knowledge_base_ids before sending
|
||||||
if (kbIds.length === 0) {
|
if (kbIds.length === 0) {
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ const getTitle = (session_id: string, value: string) => {
|
|||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
let obj = {
|
let obj = {
|
||||||
title: '新会话',
|
title: '新会话',
|
||||||
path: `chat/${kbId.value}/${session_id}`,
|
path: `chat/${session_id}`,
|
||||||
id: session_id,
|
id: session_id,
|
||||||
isMore: false,
|
isMore: false,
|
||||||
isNoTitle: true,
|
isNoTitle: true,
|
||||||
@@ -189,32 +189,12 @@ const getTitle = (session_id: string, value: string) => {
|
|||||||
usemenuStore.updataMenuChildren(obj);
|
usemenuStore.updataMenuChildren(obj);
|
||||||
usemenuStore.changeIsFirstSession(true);
|
usemenuStore.changeIsFirstSession(true);
|
||||||
usemenuStore.changeFirstQuery(value);
|
usemenuStore.changeFirstQuery(value);
|
||||||
router.push(`/platform/chat/${kbId.value}/${session_id}`);
|
router.push(`/platform/chat/${session_id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function createNewSession(value: string): Promise<void> {
|
async function createNewSession(value: string): Promise<void> {
|
||||||
// 优先使用当前页面的知识库ID
|
// Session 不再和知识库绑定,直接创建 Session
|
||||||
let sessionKbId = kbId.value;
|
createSessions({}).then(res => {
|
||||||
|
|
||||||
// 如果当前页面没有知识库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 => {
|
|
||||||
if (res.data && res.data.id) {
|
if (res.data && res.data.id) {
|
||||||
getTitle(res.data.id, value);
|
getTitle(res.data.id, value);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user