Update app.py
Browse filesmodified code runner wrapper function
2042 unmodified lines2043204420452046204720482049205020513 unmodified lines205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772042 unmodified lines"""Wrapper for CodeRunnerAgent that uses async execution with warm pool.Provides a simplified interface to the code runner with automatic sandboxpool management and user-friendly error messages. Handles warm-up statuschecks and provides appropriate feedback during startup.Args:code_or_obj: The code string or object to be executed3 unmodified lines"""try:import asyncio# First check sandbox pool status to provide user feedbacktry:pool_status = asyncio.run(get_sandbox_pool_status())user_message = pool_status.get("user_message", "")if pool_status.get("status") == "warming_up":return f"{user_message}\n\nPlease try again in a moment once the environment is ready."except Exception:pass # Continue with execution even if status check fails# Use async execution to leverage the warm sandbox poolresult = asyncio.run(code_runner.run_code_async(code_or_obj))return resultexcept CodeExecutionError as e:error_msg = str(e)if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():return "๐ The code execution environment is still starting up. Please wait a moment and try again.\n\nThis is normal for the first execution after startup (can take 1-2 minutes)."return error_msgexcept Exception as e:logger.error(f"Code runner wrapper error: {e}")2042 unmodified lines204320442045204620472048204920503 unmodified lines2054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802042 unmodified lines"""Wrapper for CodeRunnerAgent that uses async execution with warm pool.Ensures a sandbox is spawned if not already present, waits for readiness,and then executes the code. Provides user-friendly error messages.Args:code_or_obj: The code string or object to be executed3 unmodified lines"""try:import asyncioasync def ensure_and_run():# Ensure the sandbox pool is initialized and readyawait code_runner._ensure_pool_initialized()# Wait for at least one sandbox to be availablepool_status = await get_sandbox_pool_status()user_message = pool_status.get("user_message", "")if pool_status.get("status") == "warming_up":return f"{user_message}\n\nPlease try again in a moment once the environment is ready."# Run the code in the sandboxreturn await code_runner.run_code_async(code_or_obj)return asyncio.run(ensure_and_run())except CodeExecutionError as e:error_msg = str(e)if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():return ("๐ The code execution environment is still starting up. Please wait a moment and try again.\n\n""This is normal for the first execution after startup (can take 1-2 minutes).")return error_msgexcept Exception as e:logger.error(f"Code runner wrapper error: {e}")
app.py+17-14