Various small SDK improvements (#3916)

This commit is contained in:
Stanislav Novosad
2025-11-06 09:30:38 -07:00
committed by GitHub
parent e37458851d
commit d104135025
2 changed files with 15 additions and 11 deletions

View File

@@ -1,8 +1,8 @@
import asyncio import asyncio
import atexit import atexit
import socket
import threading import threading
import httpx
import structlog import structlog
import uvicorn import uvicorn
@@ -15,14 +15,14 @@ _server: uvicorn.Server | None = None
_server_thread: threading.Thread | None = None _server_thread: threading.Thread | None = None
def _is_port_in_use(port: int) -> bool: async def _is_server_running(port: int) -> bool:
"""Check if a port is already in use.""" """Check if the server is running by making an HTTP request."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try:
try: async with httpx.AsyncClient(timeout=1.0) as client:
s.bind(("localhost", port)) await client.get(f"http://localhost:{port}")
return False
except OSError:
return True return True
except (httpx.ConnectError, httpx.TimeoutException):
return False
def _cleanup_on_exit() -> None: def _cleanup_on_exit() -> None:
@@ -49,7 +49,7 @@ async def _wait_for_server(port: int, timeout: float = 10.0, interval: float = 0
"""Wait for the server to become available on the specified port.""" """Wait for the server to become available on the specified port."""
start_time = asyncio.get_event_loop().time() start_time = asyncio.get_event_loop().time()
while asyncio.get_event_loop().time() - start_time < timeout: while asyncio.get_event_loop().time() - start_time < timeout:
if _is_port_in_use(port): if await _is_server_running(port):
return True return True
await asyncio.sleep(interval) await asyncio.sleep(interval)
return False return False
@@ -66,7 +66,7 @@ async def ensure_local_server_running() -> None:
port = settings.PORT port = settings.PORT
# Check if server is already running # Check if server is already running
if _is_port_in_use(port): if await _is_server_running(port):
LOG.info(f"Local Skyvern server already running on port {port}") LOG.info(f"Local Skyvern server already running on port {port}")
return return
@@ -86,7 +86,7 @@ async def ensure_local_server_running() -> None:
app=app, app=app,
host="127.0.0.1", host="127.0.0.1",
port=port, port=port,
log_level="info", log_level="error",
reload=False, reload=False,
access_log=False, access_log=False,
) )

View File

@@ -308,6 +308,7 @@ class SkyvernBrowserPage:
return selector return selector
except Exception as e: except Exception as e:
error_to_raise = e error_to_raise = e
selector = None
# if the original selector doesn't work, try to click the element with the ai generated selector # if the original selector doesn't work, try to click the element with the ai generated selector
if prompt: if prompt:
@@ -522,6 +523,8 @@ class SkyvernBrowserPage:
return value return value
except Exception as e: except Exception as e:
error_to_raise = e error_to_raise = e
selector = None
if prompt: if prompt:
return await self._ai.ai_select_option( return await self._ai.ai_select_option(
selector=selector or "", selector=selector or "",
@@ -749,6 +752,7 @@ class SkyvernBrowserPage:
return value return value
except Exception as e: except Exception as e:
error_to_raise = e error_to_raise = e
selector = None
if intention: if intention:
return await self._ai.ai_input_text( return await self._ai.ai_input_text(