Update app.py
Browse filesmodified code runner wrapper function
app.py
CHANGED
|
@@ -2043,9 +2043,8 @@ def code_runner_wrapper(code_or_obj) -> str:
|
|
| 2043 |
"""
|
| 2044 |
Wrapper for CodeRunnerAgent that uses async execution with warm pool.
|
| 2045 |
|
| 2046 |
-
|
| 2047 |
-
|
| 2048 |
-
checks and provides appropriate feedback during startup.
|
| 2049 |
|
| 2050 |
Args:
|
| 2051 |
code_or_obj: The code string or object to be executed
|
|
@@ -2055,23 +2054,27 @@ def code_runner_wrapper(code_or_obj) -> str:
|
|
| 2055 |
"""
|
| 2056 |
try:
|
| 2057 |
import asyncio
|
| 2058 |
-
|
| 2059 |
-
|
| 2060 |
-
|
| 2061 |
-
|
|
|
|
|
|
|
| 2062 |
user_message = pool_status.get("user_message", "")
|
| 2063 |
if pool_status.get("status") == "warming_up":
|
| 2064 |
return f"{user_message}\n\nPlease try again in a moment once the environment is ready."
|
| 2065 |
-
|
| 2066 |
-
|
| 2067 |
-
|
| 2068 |
-
|
| 2069 |
-
|
| 2070 |
-
return result
|
| 2071 |
except CodeExecutionError as e:
|
| 2072 |
error_msg = str(e)
|
| 2073 |
if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():
|
| 2074 |
-
return
|
|
|
|
|
|
|
|
|
|
| 2075 |
return error_msg
|
| 2076 |
except Exception as e:
|
| 2077 |
logger.error(f"Code runner wrapper error: {e}")
|
|
|
|
| 2043 |
"""
|
| 2044 |
Wrapper for CodeRunnerAgent that uses async execution with warm pool.
|
| 2045 |
|
| 2046 |
+
Ensures a sandbox is spawned if not already present, waits for readiness,
|
| 2047 |
+
and then executes the code. Provides user-friendly error messages.
|
|
|
|
| 2048 |
|
| 2049 |
Args:
|
| 2050 |
code_or_obj: The code string or object to be executed
|
|
|
|
| 2054 |
"""
|
| 2055 |
try:
|
| 2056 |
import asyncio
|
| 2057 |
+
|
| 2058 |
+
async def ensure_and_run():
|
| 2059 |
+
# Ensure the sandbox pool is initialized and ready
|
| 2060 |
+
await code_runner._ensure_pool_initialized()
|
| 2061 |
+
# Wait for at least one sandbox to be available
|
| 2062 |
+
pool_status = await get_sandbox_pool_status()
|
| 2063 |
user_message = pool_status.get("user_message", "")
|
| 2064 |
if pool_status.get("status") == "warming_up":
|
| 2065 |
return f"{user_message}\n\nPlease try again in a moment once the environment is ready."
|
| 2066 |
+
# Run the code in the sandbox
|
| 2067 |
+
return await code_runner.run_code_async(code_or_obj)
|
| 2068 |
+
|
| 2069 |
+
return asyncio.run(ensure_and_run())
|
| 2070 |
+
|
|
|
|
| 2071 |
except CodeExecutionError as e:
|
| 2072 |
error_msg = str(e)
|
| 2073 |
if "Failed to get sandbox" in error_msg or "timeout" in error_msg.lower():
|
| 2074 |
+
return (
|
| 2075 |
+
"π The code execution environment is still starting up. Please wait a moment and try again.\n\n"
|
| 2076 |
+
"This is normal for the first execution after startup (can take 1-2 minutes)."
|
| 2077 |
+
)
|
| 2078 |
return error_msg
|
| 2079 |
except Exception as e:
|
| 2080 |
logger.error(f"Code runner wrapper error: {e}")
|