Skip to content

A programming framework for agentic AI

WebSurfer add close method (#4727)

Hussein Mozannar7d0f485bd288
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/_multimodal_web_surfer.py b/python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/_multimodal_web_surfer.py
index 26907ca0b..16eeab9bb 100644
--- a/python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/_multimodal_web_surfer.py
+++ b/python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/_multimodal_web_surfer.py
@@ -68,7 +68,7 @@ class MultimodalWebSurfer(BaseChatAgent):
When :meth:`on_messages` or :meth:`on_messages_stream` is called, the following occurs:
- 1) If this is the first call, the browser is initialized and the page is loaded. This is done in :meth:`_lazy_init`.
+ 1) If this is the first call, the browser is initialized and the page is loaded. This is done in :meth:`_lazy_init`. The browser is only closed when :meth:`close` is called.
2) The method :meth:`_generate_reply` is called, which then creates the final response as below.
3) The agent takes a screenshot of the page, extracts the interactive elements, and prepares a set-of-mark screenshot with bounding boxes around the interactive elements.
4) The agent makes a call to the :attr:`model_client` with the SOM screenshot, history of messages, and the list of available tools.
@@ -131,6 +131,8 @@ class MultimodalWebSurfer(BaseChatAgent):
# Run the team and stream messages to the console
stream = agent_team.run_stream(task="Navigate to the AutoGen readme on GitHub.")
await Console(stream)
+ # Close the browser controlled by the agent
+ await web_surfer_agent.close()
asyncio.run(main())
@@ -283,6 +285,21 @@ class MultimodalWebSurfer(BaseChatAgent):
await self._set_debug_dir(self.debug_dir)
self.did_lazy_init = True
+ async def close(self) -> None:
+ """
+ Close the browser and the page.
+ Should be called when the agent is no longer needed.
+ """
+ if self._page is not None:
+ await self._page.close()
+ self._page = None
+ if self._context is not None:
+ await self._context.close()
+ self._context = None
+ if self._playwright is not None:
+ await self._playwright.stop()
+ self._playwright = None
+
async def _set_debug_dir(self, debug_dir: str | None) -> None:
assert self._page is not None
if self.debug_dir is None:
diff --git a/python/packages/autogen-ext/tests/test_websurfer_agent.py b/python/packages/autogen-ext/tests/test_websurfer_agent.py
index dfc863057..d8a36e4d9 100644
--- a/python/packages/autogen-ext/tests/test_websurfer_agent.py
+++ b/python/packages/autogen-ext/tests/test_websurfer_agent.py
@@ -140,7 +140,7 @@ async def test_run_websurfer(monkeypatch: pytest.MonkeyPatch) -> None:
result.messages[2] # type: ignore
.content[0] # type: ignore
.startswith( # type: ignore
- "I am waiting a short period of time before taking further action.\n\n Here is a screenshot of the webpage: [Search - Microsoft Bing](https://www.bing.com/)"
+ "I am waiting a short period of time before taking further action.\n\n Here is a screenshot of the webpage:"
)
) # type: ignore
url_after_sleep = agent._page.url # type: ignore
diff --git a/python/packages/autogen-studio/autogenstudio/database/component_factory.py b/python/packages/autogen-studio/autogenstudio/database/component_factory.py
index 418782354..2c42cf807 100644
--- a/python/packages/autogen-studio/autogenstudio/database/component_factory.py
+++ b/python/packages/autogen-studio/autogenstudio/database/component_factory.py
@@ -362,8 +362,8 @@ class ComponentFactory:
name=config.name,
model_client=model_client,
headless=config.headless if config.headless is not None else True,
- debug_dir=config.logs_dir if config.logs_dir is not None else "logs",
- downloads_folder=config.logs_dir if config.logs_dir is not None else "logs",
+ debug_dir=config.logs_dir if config.logs_dir is not None else None,
+ downloads_folder=config.logs_dir if config.logs_dir is not None else None,
to_save_screenshots=config.to_save_screenshots if config.to_save_screenshots is not None else False,
use_ocr=config.use_ocr if config.use_ocr is not None else False,
animate_actions=config.animate_actions if config.animate_actions is not None else False,
diff --git a/python/packages/autogen-studio/autogenstudio/datamodel/types.py b/python/packages/autogen-studio/autogenstudio/datamodel/types.py
index 45a88306f..eb02fb121 100644
--- a/python/packages/autogen-studio/autogenstudio/datamodel/types.py
+++ b/python/packages/autogen-studio/autogenstudio/datamodel/types.py
@@ -114,7 +114,7 @@ class MultimodalWebSurferAgentConfig(BaseAgentConfig):
agent_type: AgentTypes = AgentTypes.MULTIMODAL_WEBSURFER
model_client: ModelConfig
headless: bool = True
- logs_dir: str = "logs"
+ logs_dir: str = None
to_save_screenshots: bool = False
use_ocr: bool = False
animate_actions: bool = False
diff --git a/python/packages/autogen-studio/autogenstudio/teammanager.py b/python/packages/autogen-studio/autogenstudio/teammanager.py
index bc3e01577..fdad55fb4 100644
--- a/python/packages/autogen-studio/autogenstudio/teammanager.py
+++ b/python/packages/autogen-studio/autogenstudio/teammanager.py
@@ -44,6 +44,11 @@ class TeamManager:
else:
yield message
+ # close agent resources
+ for agent in team._participants:
+ if hasattr(agent, "close"):
+ await agent.close()
+
except Exception as e:
raise e
@@ -60,4 +65,9 @@ class TeamManager:
team = await self._create_team(team_config, input_func)
result = await team.run(task=task, cancellation_token=cancellation_token)
+ # close agent resources
+ for agent in team._participants:
+ if hasattr(agent, "close"):
+ await agent.close()
+
return self._create_result(result, start_time)