diff --git a/camel/toolkits/browser_toolkit.py b/camel/toolkits/browser_toolkit.py index 89f95e3070..2677a00ef7 100644 --- a/camel/toolkits/browser_toolkit.py +++ b/camel/toolkits/browser_toolkit.py @@ -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): @@ -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 @@ -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), + ]