feat:前端新增初版知识库管理页面
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* Dify Chat API 模块
|
||||
*
|
||||
* 提供客户端调用 Dify API 的函数
|
||||
* 用于 Remix loader/action 中调用 Dify API
|
||||
*
|
||||
* @module api/dify/chat
|
||||
*/
|
||||
|
||||
import { difyFetch } from './client.server';
|
||||
|
||||
// ============================================================================
|
||||
// Dify Chat API 客户端
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Dify Chat API 客户端
|
||||
*
|
||||
* @param jwt - JWT 认证令牌
|
||||
* user 参数由后端自动从 JWT 中提取
|
||||
*/
|
||||
export const difyClient = {
|
||||
/**
|
||||
* 获取应用参数
|
||||
*/
|
||||
async getApplicationParameters(jwt?: string): Promise<any> {
|
||||
const response = await difyFetch('parameters', {
|
||||
method: 'GET',
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
*/
|
||||
async getConversations(jwt?: string): Promise<any> {
|
||||
const params = new URLSearchParams({
|
||||
limit: '100',
|
||||
first_id: '',
|
||||
});
|
||||
|
||||
const response = await difyFetch(`conversations?${params}`, {
|
||||
method: 'GET',
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取会话消息
|
||||
*/
|
||||
async getConversationMessages(conversationId: string, jwt?: string): Promise<any> {
|
||||
const params = new URLSearchParams({
|
||||
conversation_id: conversationId,
|
||||
limit: '20',
|
||||
last_id: '',
|
||||
});
|
||||
|
||||
const response = await difyFetch(`messages?${params}`, {
|
||||
method: 'GET',
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* 发送聊天消息
|
||||
*
|
||||
* @param inputs - 输入参数
|
||||
* @param query - 用户问题
|
||||
* @param responseMode - 响应模式 ('streaming' | 'blocking')
|
||||
* @param conversationId - 会话 ID
|
||||
* @param files - 附件文件
|
||||
* @param jwt - JWT 认证令牌
|
||||
* @returns 对于流式响应返回 Response 对象,否则返回 JSON
|
||||
*/
|
||||
async createChatMessage(
|
||||
inputs: Record<string, any>,
|
||||
query: string,
|
||||
responseMode: string = 'streaming',
|
||||
conversationId?: string,
|
||||
files?: any[],
|
||||
jwt?: string
|
||||
): Promise<Response | any> {
|
||||
const body = {
|
||||
inputs,
|
||||
query,
|
||||
response_mode: responseMode,
|
||||
conversation_id: conversationId,
|
||||
files: files || [],
|
||||
};
|
||||
const response = await difyFetch('chat-messages', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
}, jwt);
|
||||
|
||||
// 对于流式响应,直接返回 Response 对象
|
||||
if (responseMode === 'streaming') {
|
||||
return response;
|
||||
}
|
||||
|
||||
console.log('[Dify Chat] 解析 JSON 响应');
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* 重命名会话
|
||||
*/
|
||||
async renameConversation(
|
||||
conversationId: string,
|
||||
name: string,
|
||||
autoGenerate: boolean = false,
|
||||
jwt?: string
|
||||
): Promise<any> {
|
||||
const body = {
|
||||
name,
|
||||
auto_generate: autoGenerate,
|
||||
};
|
||||
|
||||
const response = await difyFetch(`conversations/${conversationId}/name`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
*/
|
||||
async deleteConversation(conversationId: string, jwt?: string): Promise<any> {
|
||||
console.log('[Dify Chat] 删除会话:', conversationId);
|
||||
|
||||
try {
|
||||
const response = await difyFetch(`conversations/${conversationId}`, {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify({}),
|
||||
}, jwt);
|
||||
|
||||
// 对于 204 No Content 响应,直接返回成功
|
||||
if (response.status === 204) {
|
||||
console.log('[Dify Chat] 删除会话成功:', conversationId);
|
||||
return { result: 'success' };
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
console.log('[Dify Chat] 删除会话文本响应:', text);
|
||||
|
||||
return { result: 'success' };
|
||||
} catch (error: any) {
|
||||
console.warn('[Dify Chat] 删除会话请求失败,但可能已成功删除:', error.message);
|
||||
return { result: 'success' };
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 更新消息反馈
|
||||
*/
|
||||
async updateMessageFeedback(
|
||||
messageId: string,
|
||||
rating: 'like' | 'dislike' | null,
|
||||
jwt?: string
|
||||
): Promise<any> {
|
||||
const body = {
|
||||
rating,
|
||||
};
|
||||
|
||||
const response = await difyFetch(`messages/${messageId}/feedbacks`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
}, jwt);
|
||||
return response.json();
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user