Use the browser to interact with the internet to complete the task. Args: task (str): The task to complete. id (str): Unique identifier for this call to avoid file conflicts in concurrent calls.
(self, task: str, id: str, **kwargs)
| 58 | logger.info(f"| Browser tool base directory: {self.base_dir}") |
| 59 | |
| 60 | async def _call_agent(self, task: str, id: str, **kwargs) -> ToolResponse: |
| 61 | """Use the browser to interact with the internet to complete the task. |
| 62 | |
| 63 | Args: |
| 64 | task (str): The task to complete. |
| 65 | id (str): Unique identifier for this call to avoid file conflicts in concurrent calls. |
| 66 | """ |
| 67 | agent = None |
| 68 | try: |
| 69 | try: |
| 70 | # Create unique subdirectory for this call to avoid file conflicts in concurrent calls |
| 71 | save_dir = os.path.join(self.base_dir, id) if self.base_dir else None |
| 72 | if save_dir: |
| 73 | os.makedirs(save_dir, exist_ok=True) |
| 74 | |
| 75 | # Use unique paths for each call to avoid file conflicts |
| 76 | gif_path = os.path.join(save_dir, "browser.gif") if save_dir else None |
| 77 | logs_path = os.path.join(save_dir, "browser.log") if save_dir else None |
| 78 | |
| 79 | agent = Agent( |
| 80 | task=task, |
| 81 | llm=ChatOpenAI( |
| 82 | model=self.model_name.split("/")[-1], |
| 83 | base_url=os.getenv("OPENROUTER_API_BASE"), |
| 84 | api_key=os.getenv("OPENROUTER_API_KEY"), |
| 85 | ), |
| 86 | page_extraction_llm=ChatOpenAI( |
| 87 | model=self.model_name.split("/")[-1], |
| 88 | base_url=os.getenv("OPENROUTER_API_BASE"), |
| 89 | api_key=os.getenv("OPENROUTER_API_KEY"), |
| 90 | ), |
| 91 | file_system_path=save_dir if save_dir else None, |
| 92 | generate_gif=gif_path, |
| 93 | save_conversation_path=logs_path, |
| 94 | max_steps=50, |
| 95 | verbose=True, |
| 96 | ) |
| 97 | except Exception as e: |
| 98 | return ToolResponse(success=False, message=f"Error creating browser agent: {str(e)}") |
| 99 | |
| 100 | history = await agent.run() |
| 101 | |
| 102 | try: |
| 103 | if hasattr(history, "extracted_content"): |
| 104 | contents = history.extracted_content() |
| 105 | res = "\n".join(contents) if contents else "No extracted content found" |
| 106 | elif hasattr(history, "final_result"): |
| 107 | res = history.final_result() or "No final result available" |
| 108 | elif hasattr(history, "history") and history.history: |
| 109 | last_step = history.history[-1] |
| 110 | res = str(getattr(last_step, "action_results", last_step)) |
| 111 | else: |
| 112 | res = "Task completed but no specific results available" |
| 113 | except Exception as e: |
| 114 | res = ToolResponse(success=False, message=f"Task completed but error extracting results: {str(e)}") |
| 115 | |
| 116 | # Close the agent |
| 117 | agent.close() |
no test coverage detected