Skip to content

Commit 5d9a7e7

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: enable persistent browser sessions in the computer use sample
The computer_use sample now supports launching with a `user_data_dir` to maintain browser state across runs. The sample agent is updated to use a shared temporary directory for the browser profile, preserving login sessions and other data. PiperOrigin-RevId: 823749082
1 parent f8a9672 commit 5d9a7e7

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

contributing/samples/computer_use/agent.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
import tempfile
17+
1518
from google.adk import Agent
16-
from google.adk.models.google_llm import Gemini
1719
from google.adk.tools.computer_use.computer_use_toolset import ComputerUseToolset
18-
from typing_extensions import override
1920

2021
from .playwright import PlaywrightComputer
2122

23+
# Define user_data_dir path
24+
profile_name = 'browser_profile_for_adk'
25+
profile_path = os.path.join(tempfile.gettempdir(), profile_name)
26+
os.makedirs(profile_path, exist_ok=True)
27+
28+
computer_with_profile = PlaywrightComputer(
29+
screen_size=(1280, 936),
30+
user_data_dir=profile_path,
31+
)
32+
33+
# Create agent with the toolset using the new computer instance
2234
root_agent = Agent(
2335
model='gemini-2.5-computer-use-preview-10-2025',
2436
name='hello_world_agent',
2537
description=(
2638
'computer use agent that can operate a browser on a computer to finish'
2739
' user tasks'
2840
),
29-
instruction="""
30-
you are a computer use agent
31-
""",
32-
tools=[
33-
ComputerUseToolset(computer=PlaywrightComputer(screen_size=(1280, 936)))
34-
],
41+
instruction=""" you are a computer use agent """,
42+
tools=[ComputerUseToolset(computer=computer_with_profile)],
3543
)

contributing/samples/computer_use/playwright.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import asyncio
1515
import time
1616
from typing import Literal
17+
from typing import Optional
1718

1819
from google.adk.tools.computer_use.base_computer import BaseComputer
1920
from google.adk.tools.computer_use.base_computer import ComputerEnvironment
@@ -79,29 +80,61 @@ def __init__(
7980
initial_url: str = "https://www.google.com",
8081
search_engine_url: str = "https://www.google.com",
8182
highlight_mouse: bool = False,
83+
user_data_dir: Optional[str] = None,
8284
):
8385
self._initial_url = initial_url
8486
self._screen_size = screen_size
8587
self._search_engine_url = search_engine_url
8688
self._highlight_mouse = highlight_mouse
89+
self._user_data_dir = user_data_dir
8790

8891
@override
8992
async def initialize(self):
9093
print("Creating session...")
9194
self._playwright = await async_playwright().start()
92-
self._browser = await self._playwright.chromium.launch(
93-
args=["--disable-blink-features=AutomationControlled"],
94-
headless=False,
95-
)
96-
self._context = await self._browser.new_context(
97-
viewport={
98-
"width": self._screen_size[0],
99-
"height": self._screen_size[1],
100-
}
101-
)
102-
self._page = await self._context.new_page()
103-
await self._page.goto(self._initial_url)
10495

96+
# Define common arguments for both launch types
97+
browser_args = [
98+
"--disable-blink-features=AutomationControlled",
99+
"--disable-gpu",
100+
]
101+
102+
if self._user_data_dir:
103+
termcolor.cprint(
104+
f"Starting playwright with persistent profile: {self._user_data_dir}",
105+
color="yellow",
106+
attrs=["bold"],
107+
)
108+
# Use a persistent context if user_data_dir is provided
109+
self._context = await self._playwright.chromium.launch_persistent_context(
110+
self._user_data_dir,
111+
headless=False,
112+
args=browser_args,
113+
)
114+
self._browser = self._context.browser
115+
else:
116+
termcolor.cprint(
117+
"Starting playwright with a temporary profile.",
118+
color="yellow",
119+
attrs=["bold"],
120+
)
121+
# Launch a temporary browser instance if user_data_dir is not provided
122+
self._browser = await self._playwright.chromium.launch(
123+
args=browser_args,
124+
headless=False,
125+
)
126+
self._context = await self._browser.new_context()
127+
128+
if not self._context.pages:
129+
self._page = await self._context.new_page()
130+
await self._page.goto(self._initial_url)
131+
else:
132+
self._page = self._context.pages[0] # Use existing page if any
133+
134+
await self._page.set_viewport_size({
135+
"width": self._screen_size[0],
136+
"height": self._screen_size[1],
137+
})
105138
termcolor.cprint(
106139
f"Started local playwright.",
107140
color="green",

0 commit comments

Comments
 (0)