KiWA001 commited on
Commit
7477e13
·
1 Parent(s): b544391

fix: safe services initialization and robust health check

Browse files
Files changed (3) hide show
  1. main.py +13 -2
  2. services.py +20 -3
  3. v1_router.py +3 -0
main.py CHANGED
@@ -282,6 +282,9 @@ async def root():
282
  )
283
  async def list_models():
284
  """List all available AI models across all providers."""
 
 
 
285
  models = engine.get_all_models()
286
  return ModelsResponse(
287
  models=models,
@@ -297,9 +300,17 @@ async def list_models():
297
  async def health_check():
298
  """
299
  Run health checks on all providers.
300
-
301
- Returns the status of each provider and overall system health.
302
  """
 
 
 
 
 
 
 
 
 
 
303
  logger.info("Running health checks...")
304
  results = await engine.health_check_all()
305
 
 
282
  )
283
  async def list_models():
284
  """List all available AI models across all providers."""
285
+ if not engine:
286
+ return ModelsResponse(models=[], total=0)
287
+
288
  models = engine.get_all_models()
289
  return ModelsResponse(
290
  models=models,
 
300
  async def health_check():
301
  """
302
  Run health checks on all providers.
 
 
303
  """
304
+ if not engine:
305
+ return HealthResponse(
306
+ status="unhealthy",
307
+ version="2.0.0",
308
+ uptime=0, # TODO: Track uptime
309
+ providers={},
310
+ error="AI Engine failed to initialize (check logs)"
311
+ )
312
+
313
+ # Get provider health
314
  logger.info("Running health checks...")
315
  results = await engine.health_check_all()
316
 
services.py CHANGED
@@ -1,6 +1,23 @@
1
  from engine import AIEngine
2
  from search_engine import SearchEngine
3
 
4
- # Singleton instances to be shared across modules
5
- engine = AIEngine()
6
- search_engine = SearchEngine()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from engine import AIEngine
2
  from search_engine import SearchEngine
3
 
4
+ import logging
5
+ import traceback
6
+
7
+ logger = logging.getLogger("kai_api.services")
8
+
9
+ # Singleton instances placeholder
10
+ engine = None
11
+ search_engine = None
12
+
13
+ try:
14
+ engine = AIEngine()
15
+ search_engine = SearchEngine()
16
+ logger.info("✅ Services initialized successfully")
17
+ except Exception as e:
18
+ logger.error(f"❌ Failed to initialize services: {e}")
19
+ logger.error(traceback.format_exc())
20
+ # We don't raise here to allow the app to start (and report error via /health)
21
+ # But wait, if engine is None, v1_router will crash when accessed.
22
+ # We should define a dummy engine? Or handle None in routers.
23
+
v1_router.py CHANGED
@@ -184,6 +184,9 @@ async def chat_completions(
184
  provider = request.provider or "auto"
185
 
186
  try:
 
 
 
187
  result = await engine.chat(
188
  prompt=user_prompt,
189
  model=request.model,
 
184
  provider = request.provider or "auto"
185
 
186
  try:
187
+ if not engine:
188
+ raise HTTPException(status_code=503, detail="AI Engine is not initialized (Startup Error)")
189
+
190
  result = await engine.chat(
191
  prompt=user_prompt,
192
  model=request.model,