this repo has no description
1/**
2 * Health check module for ADHD Support Agent
3 *
4 * Checks the health of all critical dependencies:
5 * - Letta API server
6 * - LiteLLM proxy
7 * - Database (optional for M0, will be enabled in M2)
8 *
9 * Returns 200 if all services are healthy, 503 if any are down.
10 */
11
12import { config } from './config';
13
14export interface HealthCheckResult {
15 healthy: boolean;
16 checks: {
17 db: boolean;
18 letta: boolean;
19 litellm: boolean;
20 };
21}
22
23/**
24 * Perform health checks on all critical services
25 *
26 * @returns Response with health status (200 if healthy, 503 if unhealthy)
27 */
28export async function healthCheck(): Promise<Response> {
29 const checks = {
30 db: false,
31 letta: false,
32 litellm: false,
33 };
34
35 // DB: Optional for M0 (database module doesn't exist yet)
36 // Will be enabled in M2 when src/db/index.ts exists
37 // For now, we skip the DB check entirely - it will be implemented in M2
38 checks.db = true;
39
40 // Letta: Check health endpoint (fast, doesn't query agents)
41 try {
42 const res = await fetch(`${config.LETTA_BASE_URL}/v1/health/`, {
43 method: 'GET',
44 signal: AbortSignal.timeout(5000), // 5s timeout
45 });
46 checks.letta = res.ok;
47 } catch (error) {
48 console.error('Letta health check failed:', error);
49 checks.letta = false;
50 }
51
52 // LiteLLM: Check health endpoint
53 try {
54 const res = await fetch(`${config.LITELLM_URL}/health`, {
55 method: 'GET',
56 signal: AbortSignal.timeout(5000), // 5s timeout
57 });
58 checks.litellm = res.ok;
59 } catch (error) {
60 console.error('LiteLLM health check failed:', error);
61 checks.litellm = false;
62 }
63
64 // Overall health: all checks must pass
65 const healthy = Object.values(checks).every(Boolean);
66
67 const result: HealthCheckResult = {
68 healthy,
69 checks,
70 };
71
72 return new Response(JSON.stringify(result), {
73 status: healthy ? 200 : 503,
74 headers: {
75 'Content-Type': 'application/json',
76 },
77 });
78}
79
80/**
81 * Simplified version for M0 that only checks if the server is running
82 * Can be used before dependencies are fully set up
83 *
84 * @returns Response indicating server is alive
85 */
86export function simpleHealthCheck(): Response {
87 return new Response(
88 JSON.stringify({
89 healthy: true,
90 checks: {
91 server: true,
92 },
93 message: 'Server is running (M0 - basic health check)',
94 }),
95 {
96 status: 200,
97 headers: {
98 'Content-Type': 'application/json',
99 },
100 }
101 );
102}