feat: Enhance chat and knowledge base functionalities with multi-KB support

- Updated agentChat API to accept optional knowledge_base_ids and agent_enabled parameters.
- Introduced getSession API to retrieve session details.
- Modified useStream to validate knowledge_base_ids for agent-chat requests and prepare POST body accordingly.
- Enhanced createKnowledgeBase API to include additional configuration options.
- Refactored chat routing to remove knowledge base ID from the chat path, allowing for dynamic knowledge base selection.
- Improved session management by preventing duplicate session entries in the menu store.
- Updated UI components for better session handling and display.
- Added context management for agent conversations to improve state persistence across sessions.
This commit is contained in:
wizardchen
2025-11-06 21:12:26 +08:00
parent 602e8a3b88
commit 266b9a5943
41 changed files with 3188 additions and 1746 deletions
+15 -2
View File
@@ -19,8 +19,17 @@ export async function knowledgeChat(data: { session_id: string; query: string; }
}
// Agent chat with streaming support
export async function agentChat(data: { session_id: string; query: string; }) {
return postChat(`/api/v1/agent-chat/${data.session_id}`, { query: data.query });
export async function agentChat(data: {
session_id: string;
query: string;
knowledge_base_ids?: string[];
agent_enabled: boolean;
}) {
return postChat(`/api/v1/agent-chat/${data.session_id}`, {
query: data.query,
knowledge_base_ids: data.knowledge_base_ids,
agent_enabled: data.agent_enabled
});
}
export async function getMessageList(data: { session_id: string; limit: number, created_at: string }) {
@@ -33,4 +42,8 @@ export async function getMessageList(data: { session_id: string; limit: number,
export async function delSession(session_id: string) {
return del(`/api/v1/sessions/${session_id}`);
}
export async function getSession(session_id: string) {
return get(`/api/v1/sessions/${session_id}`);
}
+24 -2
View File
@@ -28,7 +28,7 @@ export function useStream() {
let renderTimer: number | null = null
// 启动流式请求
const startStream = async (params: { session_id: any; query: any; method: string; url: string }) => {
const startStream = async (params: { session_id: any; query: any; knowledge_base_ids?: string[]; agent_enabled?: boolean; method: string; url: string }) => {
// 重置状态
output.value = '';
error.value = null;
@@ -46,11 +46,33 @@ export function useStream() {
return;
}
// Validate knowledge_base_ids for agent-chat requests
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;
}
}
try {
let url =
params.method == "POST"
? `${apiUrl}${params.url}/${params.session_id}`
: `${apiUrl}${params.url}/${params.session_id}?message_id=${params.query}`;
// Prepare POST body with required fields for agent-chat
// knowledge_base_ids array and agent_enabled can update Session's SessionAgentConfig
const postBody: any = {
query: params.query,
agent_enabled: params.agent_enabled !== undefined ? params.agent_enabled : true
};
// Always include knowledge_base_ids for agent-chat (already validated above)
if (params.knowledge_base_ids !== undefined && params.knowledge_base_ids.length > 0) {
postBody.knowledge_base_ids = params.knowledge_base_ids;
}
await fetchEventSource(url, {
method: params.method,
headers: {
@@ -60,7 +82,7 @@ export function useStream() {
},
body:
params.method == "POST"
? JSON.stringify({ query: params.query })
? JSON.stringify(postBody)
: null,
signal: controller.signal,
openWhenHidden: true,
+12 -1
View File
@@ -5,7 +5,18 @@ export function listKnowledgeBases() {
return get(`/api/v1/knowledge-bases`);
}
export function createKnowledgeBase(data: { name: string; description?: string; chunking_config?: any }) {
export function createKnowledgeBase(data: {
name: string;
description?: string;
chunking_config?: any;
embedding_model_id?: string;
summary_model_id?: string;
rerank_model_id?: string;
vlm_model_id?: string;
vlm_config?: any;
cos_config?: any;
extract_config?: any;
}) {
return post(`/api/v1/knowledge-bases`, data);
}