feat: 添加对话应用选择和知识库切换功能
- 新增对话应用管理模块(dify-chat-apps),支持获取和切换对话应用 - 优化对话应用切换后自动刷新会话列表功能 - 知识库管理页面新增下拉选择器,支持切换不同知识库 - API 层支持 app_id 参数传递,实现多应用会话隔离 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -32,8 +32,11 @@ export const difyClient = {
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
*
|
||||
* @param jwt - JWT 认证令牌
|
||||
* @param appId - 对话应用 ID(可选,用于获取特定应用的会话列表)
|
||||
*/
|
||||
async getConversations(jwt?: string): Promise<any> {
|
||||
async getConversations(jwt?: string, appId?: string): Promise<any> {
|
||||
const params = new URLSearchParams({
|
||||
limit: '100',
|
||||
first_id: '',
|
||||
@@ -41,6 +44,7 @@ export const difyClient = {
|
||||
|
||||
const response = await difyFetch(`conversations?${params}`, {
|
||||
method: 'GET',
|
||||
appId, // 传递应用 ID,会在请求头中添加 X-Dify-App-Id
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
@@ -70,6 +74,7 @@ export const difyClient = {
|
||||
* @param conversationId - 会话 ID
|
||||
* @param files - 附件文件
|
||||
* @param jwt - JWT 认证令牌
|
||||
* @param appId - 对话应用 ID(可选,用于切换不同的 Dify 应用)
|
||||
* @returns 对于流式响应返回 Response 对象,否则返回 JSON
|
||||
*/
|
||||
async createChatMessage(
|
||||
@@ -78,7 +83,8 @@ export const difyClient = {
|
||||
responseMode: string = 'streaming',
|
||||
conversationId?: string,
|
||||
files?: any[],
|
||||
jwt?: string
|
||||
jwt?: string,
|
||||
appId?: string
|
||||
): Promise<Response | any> {
|
||||
const body = {
|
||||
inputs,
|
||||
@@ -90,6 +96,7 @@ export const difyClient = {
|
||||
const response = await difyFetch('chat-messages', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
appId, // 传递应用 ID,会在请求头中添加 X-Dify-App-Id
|
||||
}, jwt);
|
||||
|
||||
// 对于流式响应,直接返回 Response 对象
|
||||
|
||||
@@ -27,6 +27,14 @@ const DIFY_CHAT_API_URL = `${API_BASE_URL}/dify_chat`;
|
||||
// 基础请求函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Dify Fetch 请求选项
|
||||
*/
|
||||
export interface DifyFetchOptions extends RequestInit {
|
||||
/** 对话应用 ID,用于切换不同的 Dify 应用 */
|
||||
appId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dify Chat API 基础请求函数
|
||||
*
|
||||
@@ -34,20 +42,21 @@ const DIFY_CHAT_API_URL = `${API_BASE_URL}/dify_chat`;
|
||||
* FastAPI 后端会验证 JWT 并添加 Dify API_KEY
|
||||
*
|
||||
* @param endpoint - API 端点路径
|
||||
* @param options - fetch 请求选项
|
||||
* @param options - fetch 请求选项(可包含 appId)
|
||||
* @param jwt - 用户 JWT 认证令牌
|
||||
* @returns Response 对象
|
||||
*/
|
||||
export async function difyFetch(
|
||||
endpoint: string,
|
||||
options: RequestInit = {},
|
||||
options: DifyFetchOptions = {},
|
||||
jwt?: string
|
||||
): Promise<Response> {
|
||||
const { appId, ...fetchOptions } = options;
|
||||
const url = `${DIFY_CHAT_API_URL}/${endpoint.replace(/^\//, '')}`;
|
||||
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
...fetchOptions.headers,
|
||||
};
|
||||
|
||||
if (jwt) {
|
||||
@@ -56,8 +65,14 @@ export async function difyFetch(
|
||||
console.warn('[Dify Chat] 没有提供 JWT,FastAPI 请求可能失败');
|
||||
}
|
||||
|
||||
// 如果指定了应用 ID,添加 X-Dify-App-Id 请求头
|
||||
if (appId) {
|
||||
(headers as Record<string, string>)['X-Dify-App-Id'] = appId;
|
||||
console.log('[Dify Chat] 使用应用 ID:', appId);
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
...fetchOptions,
|
||||
headers,
|
||||
});
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ const baseOptions: RequestInit = {
|
||||
/**
|
||||
* 获取用户的会话列表
|
||||
*
|
||||
* @param appId - 对话应用 ID(可选,用于获取特定应用的会话列表)
|
||||
* @returns 包含会话列表的响应对象
|
||||
* @throws {Error} 当获取会话列表失败时抛出错误
|
||||
*
|
||||
@@ -59,15 +60,23 @@ const baseOptions: RequestInit = {
|
||||
* const response = await fetchConversations();
|
||||
* const conversations = response.data;
|
||||
* console.log('会话数量:', conversations.length);
|
||||
*
|
||||
* // 获取指定应用的会话列表
|
||||
* const appConversations = await fetchConversations('app-123');
|
||||
* ```
|
||||
*/
|
||||
export async function fetchConversations(): Promise<ConversationsResponse> {
|
||||
export async function fetchConversations(appId?: string): Promise<ConversationsResponse> {
|
||||
const params = new URLSearchParams({
|
||||
limit: '100',
|
||||
});
|
||||
|
||||
// 如果指定了 appId,添加到查询参数中
|
||||
if (appId) {
|
||||
params.append('app_id', appId);
|
||||
}
|
||||
|
||||
const url = `${API_URL}/conversations?${params}`;
|
||||
console.log('📋 [Dify Client] 获取会话列表:', { url });
|
||||
console.log('📋 [Dify Client] 获取会话列表:', { url, appId });
|
||||
|
||||
try {
|
||||
const response = await axios.get<ConversationsResponse>(url, {
|
||||
|
||||
@@ -521,6 +521,8 @@ export interface SendMessageParams {
|
||||
conversation_id?: string | null;
|
||||
files?: VisionFile[];
|
||||
response_mode?: 'streaming' | 'blocking';
|
||||
/** 对话应用 ID,用于切换不同的 Dify 应用 */
|
||||
app_id?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user