fix: stabilize review detail and collabora loading

This commit is contained in:
wren
2026-05-08 10:59:04 +08:00
parent 7fdb7386ee
commit 3da2a8d088
20 changed files with 319 additions and 284 deletions
+26 -53
View File
@@ -1,98 +1,71 @@
/**
* Dify Chat 服务端 API 模块
* RAG Chat 服务端 API 模块
*
* 提供 Node.js 服务端调用 FastAPI 后端的基础功能
* Dify 的 API_KEY 和 APP_ID 由 FastAPI 后端管理,前端只负责转发请求
*
* 调用链路:
* Remix Server → FastAPI /dify_chat/* → Dify
* 提供 Node.js 服务端调用 FastAPI 后端的基础功能
* 现已改为走自有 RAG 接口:
* Remix Server -> FastAPI /api/v3/rag/*
*
* @module api/dify-chat/client.server
*/
import { API_BASE_URL } from '~/config/api-config';
// ============================================================================
// 配置
// ============================================================================
const RAG_API_ROOT = `${API_BASE_URL}/v3/rag`;
/**
* Dify Chat API 代理地址
* 通过 FastAPI 后端的 /dify_chat 路由代理访问 Dify
* Dify 的认证(API_KEY)由 FastAPI 后端处理
*/
const DIFY_CHAT_API_URL = `${API_BASE_URL}/dify_chat`;
// ============================================================================
// 基础请求函数
// ============================================================================
/**
* Dify Fetch 请求选项
*/
export interface DifyFetchOptions extends RequestInit {
/** 对话应用 ID,用于切换不同的 Dify 应用 */
appId?: string;
}
/**
* Dify Chat API 基础请求函数
*
* 使用用户 JWT 认证通过 FastAPI 代理访问 Dify
* FastAPI 后端会验证 JWT 并添加 Dify API_KEY
*
* @param endpoint - API 端点路径
* @param options - fetch 请求选项(可包含 appId
* @param jwt - 用户 JWT 认证令牌
* @returns Response 对象
*/
export async function difyFetch(
endpoint: string,
options: DifyFetchOptions = {},
jwt?: string
): Promise<Response> {
const { appId, ...fetchOptions } = options;
const url = `${DIFY_CHAT_API_URL}/${endpoint.replace(/^\//, '')}`;
function buildHeaders(fetchOptions: RequestInit, jwt?: string): HeadersInit {
const headers: HeadersInit = {
'Content-Type': 'application/json',
...fetchOptions.headers,
};
if (jwt) {
(headers as Record<string, string>)['Authorization'] = `Bearer ${jwt}`;
(headers as Record<string, string>).Authorization = `Bearer ${jwt}`;
} else {
console.warn('[Dify Chat] 没有提供 JWTFastAPI 请求可能失败');
console.warn('[RAG Chat] 没有提供 JWTFastAPI 请求可能失败');
}
// 如果指定了应用 ID,添加 X-Dify-App-Id 请求头
return headers;
}
export async function difyFetch(
endpoint: string,
options: DifyFetchOptions = {},
jwt?: string
): Promise<Response> {
const { appId, ...fetchOptions } = options;
const cleanEndpoint = endpoint.replace(/^\//, '');
let url = `${RAG_API_ROOT}/${cleanEndpoint}`;
if (appId) {
(headers as Record<string, string>)['X-Dify-App-Id'] = appId;
console.log('[Dify Chat] 使用应用 ID:', appId);
const separator = url.includes('?') ? '&' : '?';
url = `${url}${separator}appId=${encodeURIComponent(appId)}`;
}
const response = await fetch(url, {
...fetchOptions,
headers,
headers: buildHeaders(fetchOptions, jwt),
});
if (!response.ok) {
const errorText = await response.text();
console.error('[Dify Chat] API 转发错误:', {
console.error('[RAG Chat] API 转发错误:', {
status: response.status,
statusText: response.statusText,
error: errorText
error: errorText,
});
if (response.status === 401) {
throw new Error('JWT认证失败,请重新登录');
}
throw new Error(`Dify API Error: ${response.status} ${response.statusText}`);
throw new Error(`RAG API Error: ${response.status} ${response.statusText}`);
}
return response;
}
// 重新导出 chat 模块的 difyClient
export { difyClient } from './chat';