-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbrowser_use_callback_demo.py
More file actions
92 lines (63 loc) · 2.36 KB
/
Copy pathbrowser_use_callback_demo.py
File metadata and controls
92 lines (63 loc) · 2.36 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
"""Run the Browser Use adapter without installing Browser Use.
This uses a fake Browser Use-shaped agent so the example is deterministic:
python examples/browser_use_callback_demo.py
Then open the BrowserTrace UI:
browsertrace
"""
from __future__ import annotations
import asyncio
import base64
import os
from browsertrace import Tracer
from browsertrace.integrations.browser_use import attach_tracer
PNG_BYTES = b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
class DemoBrowserState:
def __init__(self, url: str):
self.url = url
self.screenshot = base64.b64encode(PNG_BYTES).decode("ascii")
class DemoAction:
def __init__(self, payload: dict):
self._payload = payload
def model_dump(self, exclude_none: bool = True) -> dict:
return self._payload
class DemoCurrentState:
def __init__(self, thought: str):
self.thought = thought
class DemoAgentOutput:
def __init__(self, thought: str, action: dict):
self.current_state = DemoCurrentState(thought)
self.action = [DemoAction(action)]
class DemoBrowserUseAgent:
def __init__(self):
self._new_step_callback = None
def register_new_step_callback(self, callback):
self._new_step_callback = callback
async def run(self) -> None:
if self._new_step_callback is None:
raise RuntimeError("step callback was not registered")
await self._new_step_callback(
DemoBrowserState("https://example.com/search"),
DemoAgentOutput(
"search for the project",
{"search_google": {"query": "BrowserTrace"}},
),
0,
)
await self._new_step_callback(
DemoBrowserState("https://example.com/results"),
DemoAgentOutput(
"open the first useful result",
{"click": {"selector": "#result-1"}},
),
1,
)
async def main() -> None:
tracer = Tracer(home=os.environ.get("BROWSERTRACE_HOME"))
agent = DemoBrowserUseAgent()
with attach_tracer(agent, tracer, name="demo: browser-use callback flow") as bt_run:
await agent.run()
print(f"BrowserTrace run id: {bt_run.run.id}")
print("Recorded Browser Use-shaped callback steps: search_google, click")
print("Open the local UI with: browsertrace")
if __name__ == "__main__":
asyncio.run(main())