Claude Code commited on
Commit
4bc39b4
·
1 Parent(s): 654c4be

Claude Code: Add brain import with try/except fallback and hello endpoint

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -2,6 +2,7 @@
2
  """
3
  Minimal HuggingClaw - Cain
4
  FastAPI with lazy imports to avoid startup blocking.
 
5
  """
6
  import os
7
  import sys
@@ -14,6 +15,19 @@ print(">>> CAIN: Python version:", sys.version.split()[0], flush=True)
14
  print(">>> CAIN: Working directory:", os.getcwd(), flush=True)
15
  print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'), flush=True)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Lazy import FastAPI to avoid blocking init
18
  def get_app():
19
  """Create FastAPI app with imports inside function."""
@@ -69,6 +83,20 @@ def get_app():
69
  "active_agents": 1 # Cain is always active
70
  }
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  @app.get("/metrics")
73
  async def metrics():
74
  """System metrics endpoint with CPU, memory, and disk usage."""
 
2
  """
3
  Minimal HuggingClaw - Cain
4
  FastAPI with lazy imports to avoid startup blocking.
5
+ Brain imports are wrapped in try/except for fallback mode.
6
  """
7
  import os
8
  import sys
 
15
  print(">>> CAIN: Working directory:", os.getcwd(), flush=True)
16
  print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'), flush=True)
17
 
18
+ # Try to import brain modules - fallback to simple mode if unavailable
19
+ BRAIN_AVAILABLE = False
20
+ brain = None
21
+ try:
22
+ print(">>> CAIN: Attempting to import brain...", flush=True)
23
+ from openclaw.openclaw.agents import brain_minimal
24
+ brain = brain_minimal
25
+ BRAIN_AVAILABLE = True
26
+ print(">>> CAIN: Brain imported successfully", flush=True)
27
+ except ImportError as e:
28
+ print(f">>> CAIN: Brain import failed: {e} - using fallback mode", flush=True)
29
+ BRAIN_AVAILABLE = False
30
+
31
  # Lazy import FastAPI to avoid blocking init
32
  def get_app():
33
  """Create FastAPI app with imports inside function."""
 
83
  "active_agents": 1 # Cain is always active
84
  }
85
 
86
+ @app.get("/hello")
87
+ async def hello():
88
+ """Hello World endpoint - uses brain if available, fallback otherwise."""
89
+ if BRAIN_AVAILABLE and brain:
90
+ try:
91
+ # Try to use brain for a dynamic response
92
+ result = await brain.think_async("Hello") if hasattr(brain, 'think_async') else None
93
+ if result:
94
+ return {"message": result, "brain": "active"}
95
+ except Exception as e:
96
+ logger.warning(f"Brain call failed: {e}")
97
+ # Fallback response
98
+ return {"message": "Hello World from Cain!", "brain": "fallback"}
99
+
100
  @app.get("/metrics")
101
  async def metrics():
102
  """System metrics endpoint with CPU, memory, and disk usage."""