85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
import { CHAT_CONFIG } from '../config/chat';
|
|
|
|
/**
|
|
* 测试Dify API连接
|
|
* 这个文件可以用来调试和测试前端直接调用Dify API的功能
|
|
*/
|
|
export const testDifyConnection = async () => {
|
|
console.log('🔧 Dify 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',
|
|
});
|
|
|
|
if (!CHAT_CONFIG.API_URL || !CHAT_CONFIG.APP_ID || !CHAT_CONFIG.API_KEY) {
|
|
console.error('❌ Dify配置不完整,请检查环境变量');
|
|
return false;
|
|
}
|
|
|
|
const user = CHAT_CONFIG.generateUserId();
|
|
console.log('👤 Generated User ID:', user);
|
|
|
|
try {
|
|
// 测试获取应用参数
|
|
console.log('📋 测试获取应用参数...');
|
|
const response = await fetch(`${CHAT_CONFIG.API_URL}/parameters`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${CHAT_CONFIG.API_KEY}`,
|
|
},
|
|
});
|
|
|
|
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);
|
|
|
|
// 测试获取会话列表
|
|
console.log('💬 测试获取会话列表...');
|
|
const params = new URLSearchParams({
|
|
user,
|
|
limit: '10',
|
|
first_id: '',
|
|
});
|
|
|
|
const conversationsResponse = await fetch(`${CHAT_CONFIG.API_URL}/conversations?${params}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${CHAT_CONFIG.API_KEY}`,
|
|
},
|
|
});
|
|
|
|
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('❌ 测试Dify连接时发生错误:', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 在浏览器控制台中可以调用这个函数进行测试
|
|
* 使用方法:
|
|
* 1. 打开浏览器控制台
|
|
* 2. 输入: window.testDify()
|
|
* 3. 查看输出结果
|
|
*/
|
|
if (typeof window !== 'undefined') {
|
|
(window as any).testDify = testDifyConnection;
|
|
}
|