|
| 1 | +import https from 'https'; |
| 2 | +import Atoma from '@atoma-agents/sui-agent/src/config/atoma'; |
| 3 | +import type { ChatResponse } from '@atoma-agents/sui-agent/src/@types/interface'; |
| 4 | + |
| 5 | +// Function to check API availability |
| 6 | +const checkApiAvailability = (): Promise<boolean> => { |
| 7 | + return new Promise((resolve) => { |
| 8 | + const req = https.get('https://api.atoma.network/health', (res) => { |
| 9 | + resolve(res.statusCode === 200); |
| 10 | + }); |
| 11 | + |
| 12 | + req.on('error', () => { |
| 13 | + resolve(false); |
| 14 | + }); |
| 15 | + |
| 16 | + req.end(); |
| 17 | + }); |
| 18 | +}; |
| 19 | + |
| 20 | +// Function to create a timeout promise |
| 21 | +const timeoutPromise = (ms: number): Promise<never> => |
| 22 | + new Promise((_, reject) => setTimeout(() => reject(new Error('Request timed out')), ms)); |
| 23 | + |
| 24 | +interface AtomaError { |
| 25 | + statusCode: number; |
| 26 | + body: string; |
| 27 | + contentType: string; |
| 28 | + rawResponse: unknown; |
| 29 | +} |
| 30 | + |
| 31 | +// Atoma SDK health check |
| 32 | +export const checkAtomaSDK = async (bearerAuth: string): Promise<void> => { |
| 33 | + const atomaSDK = new Atoma(bearerAuth); |
| 34 | + |
| 35 | + try { |
| 36 | + console.log('\n=== Atoma SDK Diagnostic Check ==='); |
| 37 | + console.log(`Bearer Token length: ${bearerAuth.length} characters`); |
| 38 | + console.log( |
| 39 | + `Bearer Token: ${bearerAuth.substring(0, 4)}...${bearerAuth.substring(bearerAuth.length - 4)}` |
| 40 | + ); |
| 41 | + |
| 42 | + const apiAvailable = await checkApiAvailability(); |
| 43 | + console.log(`API Health Check: ${apiAvailable ? 'OK' : 'Failed'}`); |
| 44 | + |
| 45 | + if (!apiAvailable) { |
| 46 | + throw new Error('API endpoint is not available'); |
| 47 | + } |
| 48 | + |
| 49 | + console.log('\nAttempting to connect to Atoma API...'); |
| 50 | + |
| 51 | + const result = (await Promise.race([ |
| 52 | + atomaSDK.atomaChat([ |
| 53 | + { |
| 54 | + role: 'user', |
| 55 | + content: 'Hi, are you there?' |
| 56 | + } |
| 57 | + ]), |
| 58 | + timeoutPromise(30000) |
| 59 | + ])) as ChatResponse; |
| 60 | + |
| 61 | + console.log('=== Chat Completion Response ==='); |
| 62 | + console.log(`Timestamp: ${new Date().toISOString()}`); |
| 63 | + console.log(`Model: ${result.model}`); |
| 64 | + console.log('\nResponse Content:'); |
| 65 | + result.choices.forEach((choice, index: number) => { |
| 66 | + console.log(`Choice ${index + 1}:`); |
| 67 | + console.log(` Role: ${choice.message.role}`); |
| 68 | + console.log(` Content: ${choice.message.content}`); |
| 69 | + }); |
| 70 | + console.log('\nAtoma SDK Check Complete ✅'); |
| 71 | + } catch (error) { |
| 72 | + console.error('\n=== Atoma SDK Check Error ==='); |
| 73 | + if (error && typeof error === 'object' && 'rawResponse' in error) { |
| 74 | + const atomaError = error as AtomaError; |
| 75 | + console.error(`Status Code: ${atomaError.statusCode}`); |
| 76 | + console.error(`Response Body: ${atomaError.body}`); |
| 77 | + console.error(`Content Type: ${atomaError.contentType}`); |
| 78 | + |
| 79 | + switch (atomaError.statusCode) { |
| 80 | + case 402: |
| 81 | + console.error('\nBalance Error:'); |
| 82 | + console.error('Your account has insufficient balance to make this request.'); |
| 83 | + console.error('\nSuggested actions:'); |
| 84 | + console.error('1. Check your account balance at https://atoma.network'); |
| 85 | + console.error('2. Add credits to your account'); |
| 86 | + console.error('3. Consider using a different model with lower cost'); |
| 87 | + console.error('4. Contact support if you believe this is an error'); |
| 88 | + break; |
| 89 | + |
| 90 | + case 500: |
| 91 | + console.error('\nPossible issues:'); |
| 92 | + console.error('1. Invalid or expired bearer token'); |
| 93 | + console.error('2. Server-side issue with the model'); |
| 94 | + console.error('3. Rate limiting or quota exceeded'); |
| 95 | + console.error('\nSuggested actions:'); |
| 96 | + console.error('- Verify your bearer token is valid'); |
| 97 | + console.error('- Try a different model'); |
| 98 | + console.error('- Check your API usage quota'); |
| 99 | + console.error('- Contact support if the issue persists'); |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + console.error('\nFull Error Stack:', error); |
| 104 | + console.error('\nAtoma SDK Check Failed ❌'); |
| 105 | + } |
| 106 | +}; |
0 commit comments