cf6e9c2421
根本问题:客户端代码直接调用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>
75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import { CHAT_CONFIG } from '../config/chat';
|
||
|
||
/**
|
||
* 测试Dify API连接(通过Remix API routes代理)
|
||
* 这个文件用于测试前端调用Remix API routes,再由服务端转发到Dify
|
||
*/
|
||
export const testDifyConnection = async () => {
|
||
console.log('🔧 API Configuration:', {
|
||
apiUrl: CHAT_CONFIG.API_URL,
|
||
note: '客户端现在调用Remix API routes,不再直接调用Dify',
|
||
});
|
||
|
||
if (!CHAT_CONFIG.API_URL) {
|
||
console.error('❌ API配置不完整');
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
// 测试获取应用参数(通过Remix API route)
|
||
console.log('📋 测试获取应用参数...');
|
||
const response = await fetch(`${CHAT_CONFIG.API_URL}/parameters`, {
|
||
method: 'GET',
|
||
credentials: 'include', // 携带cookie以通过JWT认证
|
||
});
|
||
|
||
if (!response.ok) {
|
||
console.error('❌ 获取应用参数失败:', response.status, response.statusText);
|
||
const errorText = await response.text();
|
||
console.error('错误详情:', errorText);
|
||
return false;
|
||
}
|
||
|
||
const data = await response.json();
|
||
console.log('✅ 成功获取应用参数:', data);
|
||
|
||
// 测试获取会话列表(通过Remix API route)
|
||
console.log('💬 测试获取会话列表...');
|
||
const params = new URLSearchParams({
|
||
limit: '10',
|
||
// 不再传递user参数,服务端会从JWT自动提取
|
||
});
|
||
|
||
const conversationsResponse = await fetch(`${CHAT_CONFIG.API_URL}/conversations?${params}`, {
|
||
method: 'GET',
|
||
credentials: 'include', // 携带cookie
|
||
});
|
||
|
||
if (!conversationsResponse.ok) {
|
||
console.error('❌ 获取会话列表失败:', conversationsResponse.status, conversationsResponse.statusText);
|
||
const errorText = await conversationsResponse.text();
|
||
console.error('错误详情:', errorText);
|
||
return false;
|
||
}
|
||
|
||
const conversationsData = await conversationsResponse.json();
|
||
console.log('✅ 成功获取会话列表:', conversationsData);
|
||
|
||
return true;
|
||
|
||
} catch (error) {
|
||
console.error('❌ 测试API连接时发生错误:', error);
|
||
return false;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 在浏览器控制台中可以调用这个函数进行测试
|
||
* 使用方法:
|
||
* 1. 打开浏览器控制台
|
||
* 2. 输入: window.testDify()
|
||
* 3. 查看输出结果
|
||
*/
|
||
if (typeof window !== 'undefined') {
|
||
(window as any).testDify = testDifyConnection;
|
||
}
|