Skip to content

Commit c20cb16

Browse files
committed
refactor and patch litellm newer version dependency
1 parent 4251741 commit c20cb16

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies = [
1919
"rich>=13.7.0",
2020
"openai>=1.83.0,<1.99.6",
2121
"anthropic>=0.51.0",
22-
"litellm>=1.72.0",
22+
"litellm>=1.72.0,<1.75.0",
2323
"nest-asyncio>=1.6.0",
2424
]
2525
[[project.authors]]

stagehand/main.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .logging import StagehandLogger, default_log_handler
3030
from .metrics import StagehandFunctionName, StagehandMetrics
3131
from .page import StagehandPage
32-
from .utils import make_serializable
32+
from .utils import get_download_path, make_serializable
3333

3434
load_dotenv()
3535

@@ -512,27 +512,6 @@ async def init(self):
512512
)
513513
self._playwright_page = self._page._page
514514

515-
# Set up download behavior via CDP
516-
try:
517-
# Create CDP session for the page
518-
cdp_session = await self._context.new_cdp_session(
519-
self._playwright_page
520-
)
521-
# Enable download behavior
522-
await cdp_session.send(
523-
"Browser.setDownloadBehavior",
524-
{
525-
"behavior": "allow",
526-
"downloadPath": "downloads",
527-
"eventsEnabled": True,
528-
},
529-
)
530-
self.logger.debug("Set up CDP download behavior")
531-
except Exception as e:
532-
self.logger.warning(
533-
f"Failed to set up CDP download behavior: {str(e)}"
534-
)
535-
536515
except Exception:
537516
await self.close()
538517
raise
@@ -561,6 +540,23 @@ async def init(self):
561540
# Should not happen due to __init__ validation
562541
raise RuntimeError(f"Invalid env value: {self.env}")
563542

543+
# Set up download behavior via CDP
544+
try:
545+
# Create CDP session for the page
546+
cdp_session = await self._context.new_cdp_session(self._playwright_page)
547+
# Enable download behavior
548+
await cdp_session.send(
549+
"Browser.setDownloadBehavior",
550+
{
551+
"behavior": "allow",
552+
"downloadPath": get_download_path(self),
553+
"eventsEnabled": True,
554+
},
555+
)
556+
self.logger.debug("Set up CDP download behavior")
557+
except Exception as e:
558+
self.logger.warning(f"Failed to set up CDP download behavior: {str(e)}")
559+
564560
self._initialized = True
565561

566562
def agent(self, **kwargs) -> Agent:

stagehand/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import os
23
from typing import Any, Union, get_args, get_origin
34

45
from pydantic import AnyUrl, BaseModel, Field, HttpUrl, create_model
@@ -530,3 +531,15 @@ def make_serializable(obj):
530531
elif isinstance(obj, dict):
531532
return {key: make_serializable(value) for key, value in obj.items()}
532533
return obj
534+
535+
536+
def get_download_path(stagehand):
537+
if stagehand.env == "BROWSERBASE":
538+
return "downloads"
539+
else:
540+
if stagehand.local_browser_launch_options.get("downloadPath"):
541+
return stagehand.local_browser_launch_options["downloadPath"]
542+
else:
543+
path = os.path.join(os.getcwd(), "downloads")
544+
os.makedirs(path, exist_ok=True)
545+
return path

0 commit comments

Comments
 (0)