feat:完成dify知识库文档基础CRUD模块

This commit is contained in:
PingChuan
2025-11-30 21:28:49 +08:00
parent d85010bada
commit 754ec2c7b5
21 changed files with 1142 additions and 706 deletions
+15 -49
View File
@@ -1,13 +1,13 @@
/**
* Dify 服务端基础 API 模块
* Dify Chat 服务端 API 模块
*
* 提供 Node.js 服务端调用 FastAPI 后端的基础功能
* 包括配置管理和基础请求函数
* Dify 的 API_KEY 和 APP_ID 由 FastAPI 后端管理,前端只负责转发请求
*
* 调用链路:
* Remix Server → FastAPI /dify/* → Dify
* Remix Server → FastAPI /dify_chat/* → Dify
*
* @module api/dify/client.server
* @module api/dify-chat/client.server
*/
import { API_BASE_URL } from '~/config/api-config';
@@ -17,45 +17,25 @@ import { API_BASE_URL } from '~/config/api-config';
// ============================================================================
/**
* 获取环境变量的服务端函数
* Dify Chat API 代理地址
* 通过 FastAPI 后端的 /dify_chat 路由代理访问 Dify
* Dify 的认证(API_KEY)由 FastAPI 后端处理
*/
const getServerEnvVar = (name: string, defaultValue: string = ''): string => {
return process.env[name] || defaultValue;
};
/**
* Dify API 客户端配置
* 通过 FastAPI 后端的 /dify 路由代理访问 Dify
*/
const DIFY_CONFIG = {
API_URL: `${API_BASE_URL}/dify_chat`,
API_KEY: getServerEnvVar('NEXT_PUBLIC_APP_KEY', ''),
APP_ID: (() => {
const rawAppId = getServerEnvVar('NEXT_PUBLIC_APP_ID', '');
const match = rawAppId.match(/\/app\/([a-f0-9-]{36})/);
return match ? match[1] : rawAppId;
})(),
};
console.log('[Dify Server] 配置:', {
apiUrl: DIFY_CONFIG.API_URL,
apiBaseUrl: API_BASE_URL,
appId: DIFY_CONFIG.APP_ID,
configComplete: !!(DIFY_CONFIG.API_URL && DIFY_CONFIG.APP_ID)
});
const DIFY_CHAT_API_URL = `${API_BASE_URL}/dify_chat`;
// ============================================================================
// 基础请求函数
// ============================================================================
/**
* Dify API 基础请求函数
* Dify Chat API 基础请求函数
*
* 使用 JWT 认证通过 FastAPI 代理访问 Dify
* 使用用户 JWT 认证通过 FastAPI 代理访问 Dify
* FastAPI 后端会验证 JWT 并添加 Dify API_KEY
*
* @param endpoint - API 端点路径
* @param options - fetch 请求选项
* @param jwt - JWT 认证令牌
* @param jwt - 用户 JWT 认证令牌
* @returns Response 对象
*/
export async function difyFetch(
@@ -63,7 +43,7 @@ export async function difyFetch(
options: RequestInit = {},
jwt?: string
): Promise<Response> {
const url = `${DIFY_CONFIG.API_URL}/${endpoint.replace(/^\//, '')}`;
const url = `${DIFY_CHAT_API_URL}/${endpoint.replace(/^\//, '')}`;
const headers: HeadersInit = {
'Content-Type': 'application/json',
@@ -73,7 +53,7 @@ export async function difyFetch(
if (jwt) {
(headers as Record<string, string>)['Authorization'] = `Bearer ${jwt}`;
} else {
console.warn('[Dify Server] 没有提供 JWT转发fastapi请求可能失败');
console.warn('[Dify Chat] 没有提供 JWTFastAPI 请求可能失败');
}
const response = await fetch(url, {
@@ -83,7 +63,7 @@ export async function difyFetch(
if (!response.ok) {
const errorText = await response.text();
console.error('[Dify Server] API 转发错误:', {
console.error('[Dify Chat] API 转发错误:', {
status: response.status,
statusText: response.statusText,
error: errorText
@@ -99,19 +79,5 @@ export async function difyFetch(
return response;
}
// ============================================================================
// 工具函数
// ============================================================================
/**
* Dify 工具函数
*/
export const difyUtils = {
/**
* 获取 Dify 配置
*/
getConfig: () => DIFY_CONFIG,
};
// 重新导出 chat 模块的 difyClient
export { difyClient } from './chat';