-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathapi.py
More file actions
266 lines (227 loc) · 10 KB
/
api.py
File metadata and controls
266 lines (227 loc) · 10 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import json
from datetime import datetime
from importlib.metadata import PackageNotFoundError, version
from typing import Any
from .metrics import StagehandMetrics
from .utils import convert_dict_keys_to_camel_case
__all__ = ["_create_session", "_execute", "_get_replay_metrics"]
async def _create_session(self):
"""
Create a new session by calling /sessions/start on the server.
Depends on browserbase_api_key, browserbase_project_id, and model_api_key.
"""
if not self.browserbase_api_key:
raise ValueError("browserbase_api_key is required to create a session.")
if not self.browserbase_project_id:
raise ValueError("browserbase_project_id is required to create a session.")
if not self.model_api_key:
raise ValueError("model_api_key is required to create a session.")
browserbase_session_create_params = (
convert_dict_keys_to_camel_case(self.browserbase_session_create_params)
if self.browserbase_session_create_params
else None
)
payload = {
"modelName": self.model_name,
"verbose": 2 if self.verbose == 3 else self.verbose,
"domSettleTimeoutMs": self.dom_settle_timeout_ms,
"browserbaseSessionID": self.session_id,
"browserbaseSessionCreateParams": (
browserbase_session_create_params
if browserbase_session_create_params
else {
"browserSettings": {
"blockAds": True,
"viewport": {
"width": 1024,
"height": 768,
},
},
}
),
}
# Add the new parameters if they have values
if hasattr(self, "self_heal") and self.self_heal is not None:
payload["selfHeal"] = self.self_heal
if (
hasattr(self, "wait_for_captcha_solves")
and self.wait_for_captcha_solves is not None
):
payload["waitForCaptchaSolves"] = self.wait_for_captcha_solves
if hasattr(self, "act_timeout_ms") and self.act_timeout_ms is not None:
payload["actTimeoutMs"] = self.act_timeout_ms
if hasattr(self, "system_prompt") and self.system_prompt:
payload["systemPrompt"] = self.system_prompt
if hasattr(self, "model_client_options") and self.model_client_options:
payload["modelClientOptions"] = self.model_client_options
def get_version(package_str):
try:
result = version(package_str)
except PackageNotFoundError:
self.logger.error(package_str + " not installed")
result = None
return result
headers = {
"x-bb-api-key": self.browserbase_api_key,
"x-bb-project-id": self.browserbase_project_id,
"x-model-api-key": self.model_api_key,
"Content-Type": "application/json",
"x-sent-at": datetime.now().isoformat(),
"x-language": "python",
"x-sdk-version": get_version("stagehand"),
}
# async with self._client:
resp = await self._client.post(
f"{self.api_url}/sessions/start",
json=payload,
headers=headers,
)
if resp.status_code != 200:
raise RuntimeError(f"Failed to create session: {resp.text}")
data = resp.json()
self.logger.debug(f"Session created: {data}")
if not data.get("success") or "sessionId" not in data.get("data", {}):
raise RuntimeError(f"Invalid response format: {resp.text}")
self.session_id = data["data"]["sessionId"]
async def _execute(self, method: str, payload: dict[str, Any]) -> Any:
"""
Internal helper to call /sessions/{session_id}/{method} with the given method and payload.
Streams line-by-line, returning the 'result' from the final message (if any).
"""
headers = {
"x-bb-api-key": self.browserbase_api_key,
"x-bb-project-id": self.browserbase_project_id,
"Content-Type": "application/json",
"Connection": "keep-alive",
"x-sent-at": datetime.now().isoformat(),
# Always enable streaming for better log handling
"x-stream-response": "true",
}
if self.model_api_key:
headers["x-model-api-key"] = self.model_api_key
# Convert snake_case keys to camelCase for the API
modified_payload = convert_dict_keys_to_camel_case(payload)
# async with self._client:
try:
# Always use streaming for consistent log handling
async with self._client.stream(
"POST",
f"{self.api_url}/sessions/{self.session_id}/{method}",
json=modified_payload,
headers=headers,
) as response:
if response.status_code != 200:
error_text = await response.aread()
error_message = error_text.decode("utf-8")
self.logger.error(
f"[HTTP ERROR] Status {response.status_code}: {error_message}"
)
raise RuntimeError(
f"Request failed with status {response.status_code}: {error_message}"
)
result = None
async for line in response.aiter_lines():
# Skip empty lines
if not line.strip():
continue
try:
# Handle SSE-style messages that start with "data: "
if line.startswith("data: "):
line = line[len("data: ") :]
message = json.loads(line)
# Handle different message types
msg_type = message.get("type")
if msg_type == "system":
status = message.get("data", {}).get("status")
if status == "error":
error_msg = message.get("data", {}).get(
"error", "Unknown error"
)
self.logger.error(f"[ERROR] {error_msg}")
raise RuntimeError(f"Server returned error: {error_msg}")
elif status == "finished":
result = message.get("data", {}).get("result")
elif msg_type == "log":
# Process log message using _handle_log
await self._handle_log(message)
else:
# Log any other message types
self.logger.debug(f"[UNKNOWN] Message type: {msg_type}")
except json.JSONDecodeError:
self.logger.error(f"Could not parse line as JSON: {line}")
# Return the final result
return result
except Exception as e:
self.logger.error(f"[EXCEPTION] {str(e)}")
raise
async def _get_replay_metrics(self):
"""
Fetch replay metrics from the API and parse them into StagehandMetrics.
"""
if not self.session_id:
raise ValueError("session_id is required to fetch metrics.")
headers = {
"x-bb-api-key": self.browserbase_api_key,
"x-bb-project-id": self.browserbase_project_id,
"Content-Type": "application/json",
}
try:
response = await self._client.get(
f"{self.api_url}/sessions/{self.session_id}/replay",
headers=headers,
)
if response.status_code != 200:
error_text = (
await response.aread() if hasattr(response, "aread") else response.text
)
self.logger.error(
f"[HTTP ERROR] Failed to fetch metrics. Status {response.status_code}: {error_text}"
)
raise RuntimeError(
f"Failed to fetch metrics with status {response.status_code}: {error_text}"
)
data = response.json()
if not data.get("success"):
raise RuntimeError(
f"Failed to fetch metrics: {data.get('error', 'Unknown error')}"
)
# Parse the API data into StagehandMetrics format
api_data = data.get("data", {})
metrics = StagehandMetrics()
# Parse pages and their actions
pages = api_data.get("pages", [])
for page in pages:
actions = page.get("actions", [])
for action in actions:
# Get method name and token usage
method = action.get("method", "").lower()
token_usage = action.get("tokenUsage", {})
if token_usage:
input_tokens = token_usage.get("inputTokens", 0)
output_tokens = token_usage.get("outputTokens", 0)
time_ms = token_usage.get("timeMs", 0)
# Map method to metrics fields
if method == "act":
metrics.act_prompt_tokens += input_tokens
metrics.act_completion_tokens += output_tokens
metrics.act_inference_time_ms += time_ms
elif method == "extract":
metrics.extract_prompt_tokens += input_tokens
metrics.extract_completion_tokens += output_tokens
metrics.extract_inference_time_ms += time_ms
elif method == "observe":
metrics.observe_prompt_tokens += input_tokens
metrics.observe_completion_tokens += output_tokens
metrics.observe_inference_time_ms += time_ms
elif method == "agent":
metrics.agent_prompt_tokens += input_tokens
metrics.agent_completion_tokens += output_tokens
metrics.agent_inference_time_ms += time_ms
# Always update totals for any method with token usage
metrics.total_prompt_tokens += input_tokens
metrics.total_completion_tokens += output_tokens
metrics.total_inference_time_ms += time_ms
return metrics
except Exception as e:
self.logger.error(f"[EXCEPTION] Error fetching replay metrics: {str(e)}")
raise