-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
129 lines (104 loc) · 4.68 KB
/
agent.py
File metadata and controls
129 lines (104 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# agent.py — written for browser-use 0.1.40
#
# Every risky call is wrapped defensively so we never crash
# on a missing method or renamed attribute.
from langchain_openai import ChatOpenAI
from browser_use import Agent, Browser, BrowserConfig
import config
def create_llm():
llm = ChatOpenAI(
model=config.OPENROUTER_MODEL,
openai_api_key=config.OPENROUTER_API_KEY,
openai_api_base=config.OPENROUTER_BASE_URL,
temperature=0.0,
model_kwargs={
"extra_headers": {
"HTTP-Referer": "https://my-automation-project",
"X-Title": "Simple Automation",
}
},
)
return llm
def create_browser(headless=False):
# BrowserConfig only accepts headless in 0.1.40.
# We do NOT pass slow_mo or any other args — those were added later.
config_obj = BrowserConfig(headless=headless)
browser = Browser(config=config_obj)
return browser
# Rules appended to every task.
# In 0.1.40 there is no extend_system_message / system_prompt parameter
# on Agent, so we just attach our rules to the task text itself.
EXTRA_INSTRUCTIONS = """
Rules for you to follow at all times:
1. Work like a human. Click, type, scroll naturally.
2. Never ask for CSS selectors, XPath, or locators. Figure it out from the page.
3. Before submitting any form, check every field.
Fill all required fields (marked with *, 'required', or aria-required).
4. If a required field is not in the task, use a sensible placeholder value.
5. Always verify the outcome after each action.
6. At the end, clearly state what you did and what the result was.
"""
async def run_task(task: str, headless: bool = False) -> str:
print(f"\n Starting task: {task}")
print("-" * 60)
llm = create_llm()
print("AI model ready.")
browser = create_browser(headless=headless)
print("Browser ready.")
# Combine task + rules into one string.
full_task = task + EXTRA_INSTRUCTIONS
# ── Agent init ────────────────────────────────────────────────────────────
# We only pass parameters that exist in 0.1.40.
# Removed: extend_system_message, system_prompt, max_failures (added later)
# Kept: task, llm, browser, max_actions_per_step
agent = Agent(
task=full_task,
llm=llm,
browser=browser,
max_actions_per_step=5,
)
print("Agent is running...\n")
# ── Run ───────────────────────────────────────────────────────────────────
history = await agent.run(max_steps=25)
# ── Extract result ────────────────────────────────────────────────────────
# history.final_result() exists in 0.1.40 but may return None.
# We fall back gracefully through several options so we always
# return something useful to the caller.
result = None
# Option 1: final_result() method (exists in 0.1.40)
if result is None and hasattr(history, "final_result"):
try:
fr = history.final_result()
if fr:
result = str(fr)
except Exception:
pass
# Option 2: .result attribute (some versions use this)
if result is None and hasattr(history, "result"):
try:
if history.result:
result = str(history.result)
except Exception:
pass
# Option 3: last item in history list (always available)
if result is None and hasattr(history, "history") and history.history:
try:
last = history.history[-1]
# Each history item has a 'result' list of ActionResult objects
if hasattr(last, "result") and last.result:
last_result = last.result[-1]
if hasattr(last_result, "extracted_content") and last_result.extracted_content:
result = str(last_result.extracted_content)
except Exception:
pass
# Option 4: absolute fallback
if result is None:
result = "Task finished but no summary was returned."
# ── Close browser ─────────────────────────────────────────────────────────
try:
await browser.close()
except Exception:
pass # already closed — that is fine
print("\n" + "-" * 60)
print(f"Done. Result: {result}")
return result