MCPcopy Index your code
hub / github.com/TeleBoxOrg/TeleBox_Plugins / testGeminiAPI

Function testGeminiAPI

scripts/test-gemini.js:19–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17
18// 测试 API 调用
19async function testGeminiAPI() {
20 const testPrompt = '请用一句话介绍 TeleBox 项目';
21
22 const postData = JSON.stringify({
23 contents: [{
24 parts: [{
25 text: testPrompt
26 }]
27 }],
28 generationConfig: {
29 temperature: 0.7,
30 maxOutputTokens: 100
31 }
32 });
33
34 return new Promise((resolve) => {
35 const options = {
36 hostname: 'generativelanguage.googleapis.com',
37 port: 443,
38 path: `/v1beta/models/gemini-2.5-flash:generateContent?key=${API_KEY}`,
39 method: 'POST',
40 headers: {
41 'Content-Type': 'application/json',
42 'Content-Length': Buffer.byteLength(postData)
43 },
44 timeout: 15000
45 };
46
47 console.log('\n📡 测试 Gemini API...');
48 console.log('🌐 API 端点:', `https://${options.hostname}${options.path.split('?')[0]}`);
49
50 const startTime = Date.now();
51
52 const req = https.request(options, (res) => {
53 let data = '';
54
55 console.log('📊 响应状态码:', res.statusCode);
56 console.log('📋 响应头:', JSON.stringify(res.headers, null, 2));
57
58 res.on('data', (chunk) => {
59 data += chunk;
60 });
61
62 res.on('end', () => {
63 const duration = Date.now() - startTime;
64 console.log(`⏱️ 响应时间: ${duration}ms\n`);
65
66 try {
67 const response = JSON.parse(data);
68
69 // 检查错误
70 if (response.error) {
71 console.error('❌ API 返回错误:');
72 console.error(' 错误代码:', response.error.code);
73 console.error(' 错误消息:', response.error.message);
74 console.error(' 错误状态:', response.error.status);
75
76 // 诊断常见问题

Callers 1

mainFunction · 0.85

Calls 5

errorMethod · 0.80
warnMethod · 0.80
writeMethod · 0.80
requestMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected