紧急修复:客户端改为调用Remix API routes,不再直接调用Dify API
根本问题:客户端代码直接调用Dify API(12980端口),绕过了服务端代理 修改内容: 1. app/config/api-config.ts - 添加独立的 difyBaseUrl 配置(指向外网 nas.7bm.co:8000) - 导出 DIFY_BASE_URL 供服务端使用 2. app/config/chat.ts - 移除直接Dify API配置(NEXT_PUBLIC_API_URL, APP_ID, API_KEY) - 移除 generateUserId 函数 - API_URL 改为 '/api'(指向Remix API routes) 3. app/services/api.client.ts - 所有fetch调用改为相对路径 /api/* - 移除所有 Authorization 头(服务端自动处理JWT) - 移除所有 user 参数传递(服务端从JWT提取) - credentials 改为 'include' 以携带cookie 4. app/services/dify-client.server.ts - 使用 DIFY_BASE_URL 替代 API_BASE_URL 5. app/utils/dify-test.client.ts - 测试函数改为调用Remix API routes 调用链路: 客户端 → /api/* → Remix API routes → dify-client.server.ts → FastAPI /dify → Dify 解决问题: - ✅ 不再直接调用 nas.7bm.co:12980(Dify端口) - ✅ 统一通过 nas.7bm.co:8000/dify(FastAPI代理) - ✅ 所有请求都经过JWT认证 - ✅ user字段由后端自动管理 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,33 +1,26 @@
|
||||
import { CHAT_CONFIG } from '../config/chat';
|
||||
|
||||
/**
|
||||
* 测试Dify API连接
|
||||
* 这个文件可以用来调试和测试前端直接调用Dify API的功能
|
||||
* 测试Dify API连接(通过Remix API routes代理)
|
||||
* 这个文件用于测试前端调用Remix API routes,再由服务端转发到Dify
|
||||
*/
|
||||
export const testDifyConnection = async () => {
|
||||
console.log('🔧 Dify Configuration:', {
|
||||
console.log('🔧 API Configuration:', {
|
||||
apiUrl: CHAT_CONFIG.API_URL,
|
||||
appId: CHAT_CONFIG.APP_ID,
|
||||
hasApiKey: !!CHAT_CONFIG.API_KEY,
|
||||
apiKeyPreview: CHAT_CONFIG.API_KEY ? `${CHAT_CONFIG.API_KEY.substring(0, 10)}...` : 'No API Key',
|
||||
note: '客户端现在调用Remix API routes,不再直接调用Dify',
|
||||
});
|
||||
|
||||
if (!CHAT_CONFIG.API_URL || !CHAT_CONFIG.APP_ID || !CHAT_CONFIG.API_KEY) {
|
||||
console.error('❌ Dify配置不完整,请检查环境变量');
|
||||
if (!CHAT_CONFIG.API_URL) {
|
||||
console.error('❌ API配置不完整');
|
||||
return false;
|
||||
}
|
||||
|
||||
const user = CHAT_CONFIG.generateUserId();
|
||||
console.log('👤 Generated User ID:', user);
|
||||
|
||||
try {
|
||||
// 测试获取应用参数
|
||||
// 测试获取应用参数(通过Remix API route)
|
||||
console.log('📋 测试获取应用参数...');
|
||||
const response = await fetch(`${CHAT_CONFIG.API_URL}/parameters`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${CHAT_CONFIG.API_KEY}`,
|
||||
},
|
||||
credentials: 'include', // 携带cookie以通过JWT认证
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -40,19 +33,16 @@ export const testDifyConnection = async () => {
|
||||
const data = await response.json();
|
||||
console.log('✅ 成功获取应用参数:', data);
|
||||
|
||||
// 测试获取会话列表
|
||||
// 测试获取会话列表(通过Remix API route)
|
||||
console.log('💬 测试获取会话列表...');
|
||||
const params = new URLSearchParams({
|
||||
user,
|
||||
limit: '10',
|
||||
first_id: '',
|
||||
// 不再传递user参数,服务端会从JWT自动提取
|
||||
});
|
||||
|
||||
const conversationsResponse = await fetch(`${CHAT_CONFIG.API_URL}/conversations?${params}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${CHAT_CONFIG.API_KEY}`,
|
||||
},
|
||||
credentials: 'include', // 携带cookie
|
||||
});
|
||||
|
||||
if (!conversationsResponse.ok) {
|
||||
@@ -68,7 +58,7 @@ export const testDifyConnection = async () => {
|
||||
return true;
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 测试Dify连接时发生错误:', error);
|
||||
console.error('❌ 测试API连接时发生错误:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user