Skip to content

Commit 265ccc6

Browse files
committed
kwargs agent execute
1 parent b602ac9 commit 265ccc6

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

examples/agent_example.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
async def main():
3737
# Build a unified configuration object for Stagehand
3838
config = StagehandConfig(
39-
env="BROWSERBASE",
40-
# env="LOCAL",
39+
# env="BROWSERBASE",
40+
env="LOCAL",
4141
api_key=os.getenv("BROWSERBASE_API_KEY"),
4242
project_id=os.getenv("BROWSERBASE_PROJECT_ID"),
4343
model_name="gpt-4o",
@@ -58,13 +58,6 @@ async def main():
5858
console.print(
5959
f"🌐 [white]View your live browser:[/] [url]https://www.browserbase.com/sessions/{stagehand.session_id}[/]"
6060
)
61-
62-
# Define the task for the agent
63-
execute_options = AgentExecuteOptions(
64-
instruction="Play a game of 2048",
65-
max_steps=20,
66-
auto_screenshot=True,
67-
)
6861

6962
console.print("\n▶️ [highlight] Navigating[/] to Google")
7063
await stagehand.page.goto("https://google.com/")
@@ -76,7 +69,11 @@ async def main():
7669
instructions="You are a helpful web navigation assistant that helps users find information. You are currently on the following page: google.com. Do not ask follow up questions, the user will trust your judgement.",
7770
options={"apiKey": os.getenv("MODEL_API_KEY")}
7871
)
79-
agent_result = await agent.execute(execute_options)
72+
agent_result = await agent.execute(
73+
instruction="Play a game of 2048",
74+
max_steps=20,
75+
auto_screenshot=True,
76+
)
8077

8178
console.print("📊 [info]Agent execution result:[/]")
8279
console.print(f"✅ Success: [bold]{'Yes' if agent_result.success else 'No'}[/]")

stagehand/agent/agent.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,40 @@ def _get_client(self) -> AgentClient:
8787
)
8888

8989
async def execute(
90-
self, options_or_instruction: Union[AgentExecuteOptions, str]
90+
self,
91+
options_or_instruction: Union[AgentExecuteOptions, str, dict, None] = None,
92+
**kwargs,
9193
) -> AgentResult:
9294
options: Optional[AgentExecuteOptions] = None
93-
instruction: str
95+
options_dict = {}
9496

95-
if isinstance(options_or_instruction, str):
96-
instruction = options_or_instruction
97-
options = AgentExecuteOptions(instruction=instruction)
97+
if isinstance(options_or_instruction, AgentExecuteOptions):
98+
options_dict = options_or_instruction.model_dump()
9899
elif isinstance(options_or_instruction, dict):
99-
options = AgentExecuteOptions(**options_or_instruction)
100-
instruction = options.instruction
101-
else:
102-
options = options_or_instruction
103-
instruction = options.instruction
100+
options_dict = options_or_instruction.copy()
101+
elif isinstance(options_or_instruction, str):
102+
options_dict["instruction"] = options_or_instruction
103+
104+
options_dict.update(kwargs)
105+
106+
try:
107+
options = AgentExecuteOptions(**options_dict)
108+
except Exception as e:
109+
self.logger.error(f"Invalid agent execute options: {e}")
110+
raise
111+
112+
if not options.instruction:
113+
self.logger.error("No instruction provided for agent execution.")
114+
return AgentResult(
115+
message="No instruction provided.",
116+
completed=True,
117+
actions=[],
118+
usage={},
119+
)
104120

105-
if self.stagehand.env == "LOCAL":
106-
if not instruction:
107-
self.logger.error("No instruction provided for agent execution.")
108-
return AgentResult(
109-
message="No instruction provided.",
110-
completed=True,
111-
actions=[],
112-
usage={},
113-
)
121+
instruction = options.instruction
114122

123+
if self.stagehand.env == "LOCAL":
115124
self.logger.info(
116125
f"Agent starting execution for instruction: '{instruction}'",
117126
category="agent",

0 commit comments

Comments
 (0)