Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions camel/toolkits/browser_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ def back(self):
self._wait_for_load()

def close(self):
self.browser.close()
if self.browser and self.browser is not None:
self.browser.close()

# ruff: noqa: E501
def show_interactive_elements(self):
Expand Down Expand Up @@ -1028,6 +1029,27 @@ def _reset(self):
self.history = []
os.makedirs(self.browser.cache_dir, exist_ok=True)

def terminate_browser(self):
r"""Reset and Terminate Playwright Instance.

returns:
Tuple[bool, str]: A tuple containing a boolean indicating
whether the action was successful, and the message to
be returned.
"""
try:
self._reset()
if self.browser and self.browser is not None:
self.browser.close()
if self.browser.playwright and self.browser.playwright is not None:
self.browser.playwright.stop()
return (
True,
"Successfully terminated browser and playwright instances.",
)
except Exception as e:
return (False, f"Error while terminating browser: {e}.")

def _initialize_agent(self) -> Tuple["ChatAgent", "ChatAgent"]:
r"""Initialize the agent."""
from camel.agents import ChatAgent
Expand Down Expand Up @@ -1500,4 +1522,7 @@ def browse_url(
return simulation_result

def get_tools(self) -> List[FunctionTool]:
return [FunctionTool(self.browse_url)]
return [
FunctionTool(self.browse_url),
FunctionTool(self.terminate_browser),
]