Add PR A CLI browser command parity with MCP (#4789)
This commit is contained in:
@@ -3,17 +3,26 @@ from __future__ import annotations
|
||||
import os
|
||||
from contextvars import ContextVar
|
||||
|
||||
import structlog
|
||||
|
||||
from skyvern.client import SkyvernEnvironment
|
||||
from skyvern.config import settings
|
||||
from skyvern.library.skyvern import Skyvern
|
||||
|
||||
_skyvern_instance: ContextVar[Skyvern | None] = ContextVar("skyvern_instance", default=None)
|
||||
_global_skyvern_instance: Skyvern | None = None
|
||||
LOG = structlog.get_logger(__name__)
|
||||
|
||||
|
||||
def get_skyvern() -> Skyvern:
|
||||
"""Get or create a Skyvern client instance."""
|
||||
global _global_skyvern_instance
|
||||
|
||||
instance = _skyvern_instance.get()
|
||||
if instance is None:
|
||||
instance = _global_skyvern_instance
|
||||
if instance is not None:
|
||||
_skyvern_instance.set(instance)
|
||||
return instance
|
||||
|
||||
api_key = settings.SKYVERN_API_KEY or os.environ.get("SKYVERN_API_KEY")
|
||||
@@ -28,5 +37,28 @@ def get_skyvern() -> Skyvern:
|
||||
else:
|
||||
instance = Skyvern.local()
|
||||
|
||||
_global_skyvern_instance = instance
|
||||
_skyvern_instance.set(instance)
|
||||
return instance
|
||||
|
||||
|
||||
async def close_skyvern() -> None:
|
||||
"""Close active Skyvern client(s) and release Playwright resources."""
|
||||
global _global_skyvern_instance
|
||||
|
||||
instances: list[Skyvern] = []
|
||||
seen: set[int] = set()
|
||||
for candidate in (_skyvern_instance.get(), _global_skyvern_instance):
|
||||
if candidate is None or id(candidate) in seen:
|
||||
continue
|
||||
seen.add(id(candidate))
|
||||
instances.append(candidate)
|
||||
|
||||
for instance in instances:
|
||||
try:
|
||||
await instance.aclose()
|
||||
except Exception:
|
||||
LOG.warning("Failed to close Skyvern client", exc_info=True)
|
||||
|
||||
_skyvern_instance.set(None)
|
||||
_global_skyvern_instance = None
|
||||
|
||||
Reference in New Issue
Block a user