Skip to content

Commit 6f72281

Browse files
shubh24miguelg719
andauthored
Fixing downloads behavior for use_api=false (#183)
* passing setDownloadBehavior in env=LOCAL setup * updated download logic to work with use_api=false * black formatting * fixed downloads for use_api=false * fixed the ruff error * refactor and patch litellm newer version dependency --------- Co-authored-by: miguel <miguelg71921@gmail.com>
1 parent edc57ac commit 6f72281

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stagehand": patch
3+
---
4+
5+
Fixing downloads behavior for use_api=false

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: 20 additions & 1 deletion
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

@@ -515,6 +515,7 @@ async def init(self):
515515
self.logger,
516516
)
517517
self._playwright_page = self._page._page
518+
518519
except Exception:
519520
await self.close()
520521
raise
@@ -535,13 +536,31 @@ async def init(self):
535536
self.logger,
536537
)
537538
self._playwright_page = self._page._page
539+
538540
except Exception:
539541
await self.close()
540542
raise
541543
else:
542544
# Should not happen due to __init__ validation
543545
raise RuntimeError(f"Invalid env value: {self.env}")
544546

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

547566
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)