@@ -38,71 +38,71 @@ class LivePageProxy:
38
38
A proxy object that dynamically delegates all operations to the current active page.
39
39
This mimics the behavior of the JavaScript Proxy in the original implementation.
40
40
"""
41
-
41
+
42
42
def __init__ (self , stagehand_instance ):
43
43
# Use object.__setattr__ to avoid infinite recursion
44
- object .__setattr__ (self , ' _stagehand' , stagehand_instance )
45
-
44
+ object .__setattr__ (self , " _stagehand" , stagehand_instance )
45
+
46
46
def __getattr__ (self , name ):
47
47
"""Delegate all attribute access to the current active page."""
48
- stagehand = object .__getattribute__ (self , ' _stagehand' )
49
-
48
+ stagehand = object .__getattribute__ (self , " _stagehand" )
49
+
50
50
# 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 :
52
52
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 :
54
54
active_page = stagehand ._original_page
55
55
else :
56
56
raise RuntimeError ("No active page available" )
57
-
57
+
58
58
# Get the attribute from the active page
59
59
attr = getattr (active_page , name )
60
-
60
+
61
61
# If it's a method, bind it to the active page
62
62
if callable (attr ):
63
63
return attr
64
-
64
+
65
65
return attr
66
-
66
+
67
67
def __setattr__ (self , name , value ):
68
68
"""Delegate all attribute setting to the current active page."""
69
- if name .startswith ('_' ):
69
+ if name .startswith ("_" ):
70
70
# Internal attributes are set on the proxy itself
71
71
object .__setattr__ (self , name , value )
72
72
else :
73
- stagehand = object .__getattribute__ (self , ' _stagehand' )
74
-
73
+ stagehand = object .__getattribute__ (self , " _stagehand" )
74
+
75
75
# 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 :
77
77
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 :
79
79
active_page = stagehand ._original_page
80
80
else :
81
81
raise RuntimeError ("No active page available" )
82
-
82
+
83
83
# Set the attribute on the active page
84
84
setattr (active_page , name , value )
85
-
85
+
86
86
def __dir__ (self ):
87
87
"""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 :
91
91
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 :
93
93
active_page = stagehand ._original_page
94
94
else :
95
95
return []
96
-
96
+
97
97
return dir (active_page )
98
-
98
+
99
99
def __repr__ (self ):
100
100
"""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 :
104
104
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 :
106
106
return f"<LivePageProxy -> { repr (stagehand ._original_page )} >"
107
107
else :
108
108
return "<LivePageProxy -> No active page>"
@@ -705,29 +705,28 @@ def _handle_llm_metrics(
705
705
def _set_active_page (self , stagehand_page : StagehandPage ):
706
706
"""
707
707
Internal method called by StagehandContext to update the active page.
708
-
708
+
709
709
Args:
710
710
stagehand_page: The StagehandPage to set as active
711
711
"""
712
712
self ._active_page = stagehand_page
713
713
714
-
715
714
@property
716
715
def page (self ) -> Optional [StagehandPage ]:
717
716
"""
718
717
Get the current active page. This property returns a live proxy that
719
718
always points to the currently focused page when multiple tabs are open.
720
-
719
+
721
720
Returns:
722
721
A LivePageProxy that delegates to the active StagehandPage or None if not initialized
723
722
"""
724
723
if not self ._initialized :
725
724
return None
726
-
725
+
727
726
# Create the live page proxy if it doesn't exist
728
727
if not self ._live_page_proxy :
729
728
self ._live_page_proxy = LivePageProxy (self )
730
-
729
+
731
730
return self ._live_page_proxy
732
731
733
732
0 commit comments