KnowledgeBridge / server /api-health-check.ts
fazeel007's picture
Fix health check to prevent restart loop
e1974ad
/**
* API Health Check Utility
* Verifies that all external API keys are working correctly
*/
import { nebiusClient } from './nebius-client';
import { modalClient } from './modal-client';
interface APIHealthStatus {
service: string;
status: 'healthy' | 'error' | 'missing_key';
message: string;
responseTime?: number;
}
export async function checkAPIHealth(): Promise<APIHealthStatus[]> {
const results: APIHealthStatus[] = [];
// Check Nebius API
try {
if (!process.env.NEBIUS_API_KEY) {
results.push({
service: 'Nebius AI',
status: 'missing_key',
message: 'NEBIUS_API_KEY environment variable not set'
});
} else {
const startTime = Date.now();
await nebiusClient.createEmbeddings({
input: "test",
model: "text-embedding-3-small"
});
const responseTime = Date.now() - startTime;
results.push({
service: 'Nebius AI',
status: 'healthy',
message: 'Successfully generated test embeddings',
responseTime
});
}
} catch (error) {
results.push({
service: 'Nebius AI',
status: 'error',
message: `API Error: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
// Check Modal API
try {
if (!process.env.MODAL_TOKEN_ID || !process.env.MODAL_TOKEN_SECRET) {
results.push({
service: 'Modal',
status: 'missing_key',
message: 'MODAL_TOKEN_ID or MODAL_TOKEN_SECRET environment variable not set'
});
} else {
const startTime = Date.now();
// Try to get a non-existent task status (should return 404 but proves API is accessible)
try {
await modalClient.getTaskStatus('health-check-test');
} catch (error) {
// Expected 404 error means API is accessible
if (error instanceof Error && error.message.includes('404')) {
const responseTime = Date.now() - startTime;
results.push({
service: 'Modal',
status: 'healthy',
message: 'API accessible and responding',
responseTime
});
} else {
throw error;
}
}
}
} catch (error) {
results.push({
service: 'Modal',
status: 'error',
message: `API Error: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
// Check Nebius Chat Completions (DeepSeek model)
try {
if (!process.env.NEBIUS_API_KEY) {
results.push({
service: 'Nebius Chat (DeepSeek)',
status: 'missing_key',
message: 'NEBIUS_API_KEY environment variable not set'
});
} else {
const startTime = Date.now();
await nebiusClient.createChatCompletion({
model: "deepseek-ai/DeepSeek-R1-0528",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 5
});
const responseTime = Date.now() - startTime;
results.push({
service: 'Nebius Chat (DeepSeek)',
status: 'healthy',
message: 'Successfully completed test chat with DeepSeek model',
responseTime
});
}
} catch (error) {
results.push({
service: 'Nebius Chat (DeepSeek)',
status: 'error',
message: `API Error: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
// Check GitHub API
try {
if (!process.env.GITHUB_TOKEN) {
results.push({
service: 'GitHub',
status: 'missing_key',
message: 'GITHUB_TOKEN environment variable not set'
});
} else {
const startTime = Date.now();
const response = await fetch('https://api.github.com/user', {
headers: {
'Authorization': `token ${process.env.GITHUB_TOKEN}`,
'User-Agent': 'Knowledge-Base-Browser'
}
});
const responseTime = Date.now() - startTime;
if (response.ok) {
results.push({
service: 'GitHub',
status: 'healthy',
message: 'Successfully authenticated with GitHub API',
responseTime
});
} else {
results.push({
service: 'GitHub',
status: 'error',
message: `HTTP ${response.status}: ${response.statusText}`
});
}
}
} catch (error) {
results.push({
service: 'GitHub',
status: 'error',
message: `API Error: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
return results;
}