1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+ import logging
1415import termcolor
1516import time
1617import os
1920 Computer ,
2021 EnvState ,
2122)
23+ import playwright .sync_api
2224from playwright .sync_api import sync_playwright
2325from typing import Literal
2426
@@ -85,6 +87,16 @@ def __init__(
8587 self ._search_engine_url = search_engine_url
8688 self ._highlight_mouse = highlight_mouse
8789
90+ def _handle_new_page (self , new_page : playwright .sync_api .Page ):
91+ """The Computer Use model only supports a single tab at the moment.
92+
93+ Some websites, however, try to open links in a new tab.
94+ For those situations, we intercept the page-opening behavior, and instead overwrite the current page.
95+ """
96+ new_url = new_page .url
97+ new_page .close ()
98+ self ._page .goto (new_url )
99+
88100 def __enter__ (self ):
89101 print ("Creating session..." )
90102 self ._playwright = sync_playwright ().start ()
@@ -101,6 +113,8 @@ def __enter__(self):
101113 self ._page = self ._context .new_page ()
102114 self ._page .goto (self ._initial_url )
103115
116+ self ._context .on ("page" , self ._handle_new_page )
117+
104118 termcolor .cprint (
105119 f"Started local playwright." ,
106120 color = "green" ,
@@ -282,7 +296,9 @@ def current_state(self) -> EnvState:
282296 return EnvState (screenshot = screenshot_bytes , url = self ._page .url )
283297
284298 def screen_size (self ) -> tuple [int , int ]:
285- return self ._screen_size
299+ viewport_size = self ._page .viewport_size
300+ assert viewport_size is not None , "Failed to get viewport size."
301+ return viewport_size ["width" ], viewport_size ["height" ]
286302
287303 def highlight_mouse (self , x : int , y : int ):
288304 if not self ._highlight_mouse :
0 commit comments