@@ -649,58 +649,76 @@ def debug(
649
649
self .log (message , level = 2 , category = category , auxiliary = auxiliary )
650
650
651
651
652
- # Create a synchronous wrapper for the async default_log_handler
653
652
def sync_log_handler (log_data : dict [str , Any ]) -> None :
654
653
"""
655
- Synchronous wrapper for log handling, doesn't require awaiting.
656
- This avoids the coroutine never awaited warnings.
654
+ Enhanced log handler for messages from the Stagehand server.
655
+ Uses Rich for pretty printing and JSON formatting.
656
+
657
+ The log_data structure from the server is:
658
+ {
659
+ "message": { // This is the actual LogLine object
660
+ "message": "...",
661
+ "level": 0|1|2,
662
+ "category": "...",
663
+ "auxiliary": {...}
664
+ },
665
+ "status": "running"
666
+ }
657
667
"""
658
- # Extract relevant data from the log message
659
- level = log_data .get ("level" , 1 )
660
- if isinstance (level , str ):
661
- level = {"error" : 0 , "info" : 1 , "warning" : 2 , "debug" : 2 }.get (level .lower (), 1 )
662
-
663
- message = log_data .get ("message" , "" )
664
- category = log_data .get ("category" , "" )
665
- auxiliary = {}
666
-
667
- # Process auxiliary data if present
668
- if "auxiliary" in log_data :
669
- auxiliary = log_data .get ("auxiliary" , {})
670
-
671
- # Convert string representation to actual object if needed
672
- if isinstance (auxiliary , str ) and (
673
- auxiliary .startswith ("{" ) or auxiliary .startswith ("{'" )
674
- ):
675
- try :
676
- import ast
668
+ try :
669
+ # Extract the actual LogLine object from the nested structure
670
+ log_line = log_data .get ("message" , {})
671
+
672
+ # Handle case where log_data might directly be the LogLine (fallback)
673
+ if not isinstance (log_line , dict ) or not log_line :
674
+ # If message field is not a dict or is empty, treat log_data as the LogLine
675
+ log_line = log_data
676
+
677
+ # Extract data from the LogLine object
678
+ level = log_line .get ("level" , 1 )
679
+ message = log_line .get ("message" , "" )
680
+ category = log_line .get ("category" , "" )
681
+ auxiliary = log_line .get ("auxiliary" , {})
682
+
683
+ # Handle level conversion if it's a string
684
+ if isinstance (level , str ):
685
+ level = {"error" : 0 , "info" : 1 , "warning" : 1 , "warn" : 1 , "debug" : 2 }.get (
686
+ level .lower (), 1
687
+ )
677
688
678
- auxiliary = ast .literal_eval (auxiliary )
679
- except (SyntaxError , ValueError ):
680
- # If parsing fails, keep as string
681
- pass
689
+ # Ensure level is within valid range
690
+ level = max (0 , min (2 , int (level ))) if level is not None else 1
682
691
683
- # Create a temporary logger to handle
684
- temp_logger = StagehandLogger (verbose = 2 , use_rich = True , external_logger = None )
692
+ # Handle cases where message might be a complex object
693
+ if isinstance (message , dict ):
694
+ # If message is a dict, convert to string for display
695
+ if "message" in message :
696
+ # Handle nested message structure
697
+ actual_message = message .get ("message" , "" )
698
+ if not level and "level" in message :
699
+ level = message .get ("level" , 1 )
700
+ if not category and "category" in message :
701
+ category = message .get ("category" , "" )
702
+ message = actual_message
703
+ else :
704
+ # Convert dict to JSON string
705
+ message = json .dumps (message , indent = 2 )
706
+
707
+ # Create a temporary logger to handle the message
708
+ temp_logger = StagehandLogger (verbose = 2 , use_rich = True , external_logger = None )
685
709
686
- try :
687
710
# Use the logger to format and display the message
688
- temp_logger .log (message , level = level , category = category , auxiliary = auxiliary )
711
+ temp_logger .log (message , level = level , auxiliary = auxiliary )
712
+
689
713
except Exception as e :
690
714
# Fall back to basic logging if formatting fails
691
715
print (f"Error formatting log: { str (e )} " )
692
- print (f"Original message: { message } " )
693
- if category :
694
- print (f"Category: { category } " )
695
- if auxiliary :
696
- print (f"Auxiliary data: { auxiliary } " )
716
+ print (f"Original log_data: { log_data } " )
697
717
698
718
699
719
async def default_log_handler (log_data : dict [str , Any ]) -> None :
700
720
"""
701
- Enhanced default handler for log messages from the Stagehand server.
702
- Uses Rich for pretty printing and JSON formatting.
703
-
704
- This is an async function but calls the synchronous implementation.
721
+ Default handler for log messages from the Stagehand server.
722
+ This is just a wrapper around sync_log_handler for backward compatibility.
705
723
"""
706
724
sync_log_handler (log_data )
0 commit comments