File size: 4,497 Bytes
7c012de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1974ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c012de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

/**
 * 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;
}