@@ -38,71 +38,71 @@ class LivePageProxy:
3838 A proxy object that dynamically delegates all operations to the current active page.
3939 This mimics the behavior of the JavaScript Proxy in the original implementation.
4040 """
41-
41+
4242 def __init__ (self , stagehand_instance ):
4343 # Use object.__setattr__ to avoid infinite recursion
44- object .__setattr__ (self , ' _stagehand' , stagehand_instance )
45-
44+ object .__setattr__ (self , " _stagehand" , stagehand_instance )
45+
4646 def __getattr__ (self , name ):
4747 """Delegate all attribute access to the current active page."""
48- stagehand = object .__getattribute__ (self , ' _stagehand' )
49-
48+ stagehand = object .__getattribute__ (self , " _stagehand" )
49+
5050 # Get the current active page
51- if hasattr (stagehand , ' _active_page' ) and stagehand ._active_page :
51+ if hasattr (stagehand , " _active_page" ) and stagehand ._active_page :
5252 active_page = stagehand ._active_page
53- elif hasattr (stagehand , ' _original_page' ) and stagehand ._original_page :
53+ elif hasattr (stagehand , " _original_page" ) and stagehand ._original_page :
5454 active_page = stagehand ._original_page
5555 else :
5656 raise RuntimeError ("No active page available" )
57-
57+
5858 # Get the attribute from the active page
5959 attr = getattr (active_page , name )
60-
60+
6161 # If it's a method, bind it to the active page
6262 if callable (attr ):
6363 return attr
64-
64+
6565 return attr
66-
66+
6767 def __setattr__ (self , name , value ):
6868 """Delegate all attribute setting to the current active page."""
69- if name .startswith ('_' ):
69+ if name .startswith ("_" ):
7070 # Internal attributes are set on the proxy itself
7171 object .__setattr__ (self , name , value )
7272 else :
73- stagehand = object .__getattribute__ (self , ' _stagehand' )
74-
73+ stagehand = object .__getattribute__ (self , " _stagehand" )
74+
7575 # Get the current active page
76- if hasattr (stagehand , ' _active_page' ) and stagehand ._active_page :
76+ if hasattr (stagehand , " _active_page" ) and stagehand ._active_page :
7777 active_page = stagehand ._active_page
78- elif hasattr (stagehand , ' _original_page' ) and stagehand ._original_page :
78+ elif hasattr (stagehand , " _original_page" ) and stagehand ._original_page :
7979 active_page = stagehand ._original_page
8080 else :
8181 raise RuntimeError ("No active page available" )
82-
82+
8383 # Set the attribute on the active page
8484 setattr (active_page , name , value )
85-
85+
8686 def __dir__ (self ):
8787 """Return attributes of the current active page."""
88- stagehand = object .__getattribute__ (self , ' _stagehand' )
89-
90- if hasattr (stagehand , ' _active_page' ) and stagehand ._active_page :
88+ stagehand = object .__getattribute__ (self , " _stagehand" )
89+
90+ if hasattr (stagehand , " _active_page" ) and stagehand ._active_page :
9191 active_page = stagehand ._active_page
92- elif hasattr (stagehand , ' _original_page' ) and stagehand ._original_page :
92+ elif hasattr (stagehand , " _original_page" ) and stagehand ._original_page :
9393 active_page = stagehand ._original_page
9494 else :
9595 return []
96-
96+
9797 return dir (active_page )
98-
98+
9999 def __repr__ (self ):
100100 """Return representation of the current active page."""
101- stagehand = object .__getattribute__ (self , ' _stagehand' )
102-
103- if hasattr (stagehand , ' _active_page' ) and stagehand ._active_page :
101+ stagehand = object .__getattribute__ (self , " _stagehand" )
102+
103+ if hasattr (stagehand , " _active_page" ) and stagehand ._active_page :
104104 return f"<LivePageProxy -> { repr (stagehand ._active_page )} >"
105- elif hasattr (stagehand , ' _original_page' ) and stagehand ._original_page :
105+ elif hasattr (stagehand , " _original_page" ) and stagehand ._original_page :
106106 return f"<LivePageProxy -> { repr (stagehand ._original_page )} >"
107107 else :
108108 return "<LivePageProxy -> No active page>"
@@ -705,29 +705,28 @@ def _handle_llm_metrics(
705705 def _set_active_page (self , stagehand_page : StagehandPage ):
706706 """
707707 Internal method called by StagehandContext to update the active page.
708-
708+
709709 Args:
710710 stagehand_page: The StagehandPage to set as active
711711 """
712712 self ._active_page = stagehand_page
713713
714-
715714 @property
716715 def page (self ) -> Optional [StagehandPage ]:
717716 """
718717 Get the current active page. This property returns a live proxy that
719718 always points to the currently focused page when multiple tabs are open.
720-
719+
721720 Returns:
722721 A LivePageProxy that delegates to the active StagehandPage or None if not initialized
723722 """
724723 if not self ._initialized :
725724 return None
726-
725+
727726 # Create the live page proxy if it doesn't exist
728727 if not self ._live_page_proxy :
729728 self ._live_page_proxy = LivePageProxy (self )
730-
729+
731730 return self ._live_page_proxy
732731
733732
0 commit comments