diff --git a/backend/agent/api.py b/backend/agent/api.py index 009ad9c9..24f4bafa 100644 --- a/backend/agent/api.py +++ b/backend/agent/api.py @@ -204,11 +204,12 @@ async def stop_agent_run(agent_run_id: str, error_message: Optional[str] = None) # Update the agent run status in the database update_success = await update_agent_run_status( - client, agent_run_id, final_status, error=error_message, responses=all_responses + client, agent_run_id, final_status, error=error_message ) if not update_success: logger.error(f"Failed to update database status for stopped/failed run {agent_run_id}") + raise HTTPException(status_code=500, detail="Failed to update agent run status in database") # Send STOP signal to the global control channel global_control_channel = f"agent_run:{agent_run_id}:control" diff --git a/backend/agent/tools/web_search_tool.py b/backend/agent/tools/web_search_tool.py index 321d480a..92fee67b 100644 --- a/backend/agent/tools/web_search_tool.py +++ b/backend/agent/tools/web_search_tool.py @@ -197,26 +197,25 @@ class SandboxWebSearchTool(SandboxToolsBase): logging.info(f"Processing {len(url_list)} URLs: {url_list}") - # Process each URL and collect results - results = [] - for url in url_list: - try: - # Add protocol if missing - if not (url.startswith('http://') or url.startswith('https://')): - url = 'https://' + url - logging.info(f"Added https:// protocol to URL: {url}") - - # Scrape this URL - result = await self._scrape_single_url(url) - results.append(result) - - except Exception as e: - logging.error(f"Error processing URL {url}: {str(e)}") - results.append({ - "url": url, + # Process each URL concurrently and collect results + tasks = [self._scrape_single_url(url) for url in url_list] + results = await asyncio.gather(*tasks, return_exceptions=True) + + # Process results, handling exceptions + processed_results = [] + for i, result in enumerate(results): + if isinstance(result, Exception): + logging.error(f"Error processing URL {url_list[i]}: {str(result)}") + processed_results.append({ + "url": url_list[i], "success": False, - "error": str(e) + "error": str(result) }) + else: + processed_results.append(result) + + results = processed_results + # Summarize results successful = sum(1 for r in results if r.get("success", False)) @@ -255,6 +254,12 @@ class SandboxWebSearchTool(SandboxToolsBase): """ Helper function to scrape a single URL and return the result information. """ + + # # Add protocol if missing + # if not (url.startswith('http://') or url.startswith('https://')): + # url = 'https://' + url + # logging.info(f"Added https:// protocol to URL: {url}") + logging.info(f"Scraping single URL: {url}") try: @@ -272,7 +277,7 @@ class SandboxWebSearchTool(SandboxToolsBase): # Use longer timeout and retry logic for more reliability max_retries = 3 - timeout_seconds = 120 + timeout_seconds = 30 retry_count = 0 while retry_count < max_retries: diff --git a/backend/run_agent_background.py b/backend/run_agent_background.py index 1bf14051..1d4ffba8 100644 --- a/backend/run_agent_background.py +++ b/backend/run_agent_background.py @@ -227,7 +227,7 @@ async def run_agent_background( all_responses = [json.loads(r) for r in all_responses_json] # Update DB status - await update_agent_run_status(client, agent_run_id, final_status, error=error_message, responses=all_responses) + await update_agent_run_status(client, agent_run_id, final_status, error=error_message) # Publish final control signal (END_STREAM or ERROR) control_signal = "END_STREAM" if final_status == "completed" else "ERROR" if final_status == "failed" else "STOP" @@ -264,7 +264,7 @@ async def run_agent_background( all_responses = [error_response] # Use the error message we tried to push # Update DB status - await update_agent_run_status(client, agent_run_id, "failed", error=f"{error_message}\n{traceback_str}", responses=all_responses) + await update_agent_run_status(client, agent_run_id, "failed", error=f"{error_message}\n{traceback_str}") # Publish ERROR signal try: @@ -347,7 +347,6 @@ async def update_agent_run_status( agent_run_id: str, status: str, error: Optional[str] = None, - responses: Optional[list[any]] = None # Expects parsed list of dicts ) -> bool: """ Centralized function to update agent run status. @@ -362,9 +361,7 @@ async def update_agent_run_status( if error: update_data["error"] = error - if responses: - # Ensure responses are stored correctly as JSONB - update_data["responses"] = responses + # Retry up to 3 times for retry in range(3):