Skip to content

Commit 01010d8

Browse files
committed
stash
1 parent 2bcffcd commit 01010d8

File tree

12 files changed

+857
-8
lines changed

12 files changed

+857
-8
lines changed

.fernignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
.vscode/
44

55
src/browser_use/client.py
6-
src/browser_use/wrapper/
6+
src/browser_use/lib/
7+
src/browser_use/wrapper/
8+
examples/

examples/async_retrieve.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use import AsyncBrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = AsyncBrowserUse()
12+
13+
14+
# Regular Task
15+
async def retrieve_regular_task() -> None:
16+
"""
17+
Retrieves a regular task and waits for it to finish.
18+
"""
19+
20+
print("Retrieving regular task...")
21+
22+
regular_task = await client.tasks.create_task(
23+
task="""
24+
Find top 10 Hacker News articles and return the title and url.
25+
""",
26+
llm="gemini-2.5-flash",
27+
)
28+
29+
print(f"Regular Task ID: {regular_task.id}")
30+
31+
while True:
32+
regular_status = await client.tasks.get_task(regular_task.id)
33+
print(f"Regular Task Status: {regular_status.status}")
34+
if regular_status.status == "finished":
35+
print(f"Regular Task Output: {regular_status.output}")
36+
break
37+
38+
await asyncio.sleep(1)
39+
40+
print("Done")
41+
42+
43+
async def retrieve_structured_task() -> None:
44+
"""
45+
Retrieves a structured task and waits for it to finish.
46+
"""
47+
48+
print("Retrieving structured task...")
49+
50+
# Structured Output
51+
class HackerNewsPost(BaseModel):
52+
title: str
53+
url: str
54+
55+
class SearchResult(BaseModel):
56+
posts: List[HackerNewsPost]
57+
58+
structured_task = await client.tasks.create_task(
59+
task="""
60+
Find top 10 Hacker News articles and return the title and url.
61+
""",
62+
llm="gpt-4.1",
63+
schema=SearchResult,
64+
)
65+
66+
print(f"Structured Task ID: {structured_task.id}")
67+
68+
while True:
69+
structured_status = await client.tasks.retrieve(task_id=structured_task.id, schema=SearchResult)
70+
print(f"Structured Task Status: {structured_status.status}")
71+
72+
if structured_status.status == "finished":
73+
if structured_status.parsed_output is None:
74+
print("Structured Task No output")
75+
else:
76+
for post in structured_status.parsed_output.posts:
77+
print(f" - {post.title} - {post.url}")
78+
79+
break
80+
81+
await asyncio.sleep(1)
82+
83+
print("Done")
84+
85+
86+
# Main
87+
88+
89+
async def main() -> None:
90+
await asyncio.gather(
91+
#
92+
retrieve_regular_task(),
93+
retrieve_structured_task(),
94+
)
95+
96+
97+
asyncio.run(main())

examples/async_run.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use import AsyncBrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = AsyncBrowserUse()
12+
13+
14+
# Regular Task
15+
async def run_regular_task() -> None:
16+
task = await client.tasks.create_task(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
""",
20+
llm="gemini-2.5-flash",
21+
)
22+
23+
print(f"Regular Task ID: {task.id}")
24+
25+
result = await task.complete()
26+
27+
print(f"Regular Task Output: {result.output}")
28+
29+
print("Done")
30+
31+
32+
# Structured Output
33+
async def run_structured_task() -> None:
34+
class HackerNewsPost(BaseModel):
35+
title: str
36+
url: str
37+
38+
class SearchResult(BaseModel):
39+
posts: List[HackerNewsPost]
40+
41+
task = await client.tasks.create_task(
42+
task="""
43+
Find top 10 Hacker News articles and return the title and url.
44+
""",
45+
llm="gpt-4.1",
46+
structured_output_json=SearchResult,
47+
)
48+
49+
print(f"Structured Task ID: {task.id}")
50+
51+
result = await task.complete()
52+
53+
if result.parsed is not None:
54+
print("Structured Task Output:")
55+
for post in result.parsed.posts:
56+
print(f" - {post.title} - {post.url}")
57+
58+
print("Structured Task Done")
59+
60+
61+
async def main() -> None:
62+
await asyncio.gather(
63+
#
64+
run_regular_task(),
65+
run_structured_task(),
66+
)
67+
68+
69+
asyncio.run(main())

examples/async_stream.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use import AsyncBrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = AsyncBrowserUse()
12+
13+
14+
# Regular Task
15+
async def stream_regular_task() -> None:
16+
regular_task = await client.tasks.create(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
""",
20+
llm="gemini-2.5-flash",
21+
)
22+
23+
print(f"Regular Task ID: {regular_task.id}")
24+
25+
async for res in client.tasks.stream(regular_task.id):
26+
print(f"Regular Task Status: {res.status}")
27+
28+
if len(res.steps) > 0:
29+
last_step = res.steps[-1]
30+
print(f"Regular Task Step: {last_step.url} ({last_step.next_goal})")
31+
for action in last_step.actions:
32+
print(f" - Regular Task Action: {action}")
33+
34+
print("Regular Task Done")
35+
36+
37+
# Structured Output
38+
async def stream_structured_task() -> None:
39+
class HackerNewsPost(BaseModel):
40+
title: str
41+
url: str
42+
43+
class SearchResult(BaseModel):
44+
posts: List[HackerNewsPost]
45+
46+
structured_task = await client.tasks.create(
47+
task="""
48+
Find top 10 Hacker News articles and return the title and url.
49+
""",
50+
llm="gpt-4.1",
51+
structured_output_json=SearchResult,
52+
)
53+
54+
print(f"Structured Task ID: {structured_task.id}")
55+
56+
async for res in client.tasks.stream(structured_task.id, structured_output_json=SearchResult):
57+
print(f"Structured Task Status: {res.status}")
58+
59+
if res.status == "finished":
60+
if res.parsed_output is None:
61+
print("Structured Task No output")
62+
else:
63+
for post in res.parsed_output.posts:
64+
print(f" - Structured Task Post: {post.title} - {post.url}")
65+
break
66+
67+
print("Structured Task Done")
68+
69+
70+
# Main
71+
72+
73+
async def main() -> None:
74+
await asyncio.gather(
75+
#
76+
stream_regular_task(),
77+
stream_structured_task(),
78+
)
79+
80+
81+
asyncio.run(main())

examples/retrieve.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import time
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use import BrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = BrowserUse()
12+
13+
14+
# Regular Task
15+
def retrieve_regular_task() -> None:
16+
"""
17+
Retrieves a regular task and waits for it to finish.
18+
"""
19+
20+
print("Retrieving regular task...")
21+
22+
regular_task = client.tasks.create_task(
23+
task="""
24+
Find top 10 Hacker News articles and return the title and url.
25+
""",
26+
llm="gemini-2.5-flash",
27+
)
28+
29+
print(f"Task ID: {regular_task.id}")
30+
31+
while True:
32+
regular_status = client.tasks.get_task(regular_task.id)
33+
print(regular_status.status)
34+
if regular_status.status == "finished":
35+
print(regular_status.output)
36+
break
37+
38+
time.sleep(1)
39+
40+
print("Done")
41+
42+
43+
retrieve_regular_task()
44+
45+
46+
def retrieve_structured_task() -> None:
47+
"""
48+
Retrieves a structured task and waits for it to finish.
49+
"""
50+
51+
print("Retrieving structured task...")
52+
53+
# Structured Output
54+
class HackerNewsPost(BaseModel):
55+
title: str
56+
url: str
57+
58+
class SearchResult(BaseModel):
59+
posts: List[HackerNewsPost]
60+
61+
structured_task = client.tasks.create_task(
62+
task="""
63+
Find top 10 Hacker News articles and return the title and url.
64+
""",
65+
llm="gpt-4.1",
66+
schema=SearchResult,
67+
)
68+
69+
print(f"Task ID: {structured_task.id}")
70+
71+
while True:
72+
structured_status = client.tasks.get_task(task_id=structured_task.id, schema=SearchResult)
73+
print(structured_status.status)
74+
75+
if structured_status.status == "finished":
76+
if structured_status.parsed is None:
77+
print("No output")
78+
else:
79+
for post in structured_status.parsed.posts:
80+
print(f" - {post.title} - {post.url}")
81+
82+
break
83+
84+
time.sleep(1)
85+
86+
print("Done")
87+
88+
89+
retrieve_structured_task()

0 commit comments

Comments
 (0)