Claude Code Claude Opus 4.6 commited on
Commit
ef21765
·
1 Parent(s): f7ea576

Claude Code: Fix import path resolution in error_handlers.py

Browse files

- Add sys.path setup at module level for agents imports
- Simplify handle_brain_response to not rely on openclaw import order
- Ensures 'from agents import brain_minimal' works regardless of when openclaw is imported

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. error_handlers.py +14 -3
error_handlers.py CHANGED
@@ -5,12 +5,24 @@ Provides specific exception handlers for common operations.
5
  """
6
  import json
7
  import os
 
8
  from datetime import datetime
9
  from pathlib import Path
10
  from typing import Dict, Any, Optional
11
  from fastapi import HTTPException, Request
12
  from fastapi.responses import JSONResponse
13
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
16
  """
@@ -146,9 +158,8 @@ def handle_brain_response(message: str) -> str:
146
  AttributeError: If required brain methods are missing.
147
  """
148
  try:
149
- # Import openclaw first - it sets up sys.path to include .openclaw
150
- # Then we can import agents directly (same pattern as app.py)
151
- import openclaw
152
  from agents import brain_minimal
153
 
154
  # Verify brain has required method before calling
 
5
  """
6
  import json
7
  import os
8
+ import sys
9
  from datetime import datetime
10
  from pathlib import Path
11
  from typing import Dict, Any, Optional
12
  from fastapi import HTTPException, Request
13
  from fastapi.responses import JSONResponse
14
 
15
+ # CRITICAL: Set up sys.path for agents imports at module load time
16
+ # This ensures `from agents import brain_minimal` works regardless of import order
17
+ _openclaw_package_dir = Path(__file__).parent / "openclaw"
18
+ _openclaw_internal_dir = _openclaw_package_dir / ".openclaw"
19
+
20
+ # Add openclaw parent dir and .openclaw to sys.path for agents imports
21
+ if str(_openclaw_package_dir) not in sys.path:
22
+ sys.path.insert(0, str(_openclaw_package_dir))
23
+ if str(_openclaw_internal_dir) not in sys.path:
24
+ sys.path.insert(0, str(_openclaw_internal_dir))
25
+
26
 
27
  async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
28
  """
 
158
  AttributeError: If required brain methods are missing.
159
  """
160
  try:
161
+ # sys.path is already set up at module level (see top of file)
162
+ # Import agents directly - no need to import openclaw first
 
163
  from agents import brain_minimal
164
 
165
  # Verify brain has required method before calling