browser-use 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +761 -0
  3. package/dist/agent/cloud-events.d.ts +264 -0
  4. package/dist/agent/cloud-events.js +318 -0
  5. package/dist/agent/gif.d.ts +15 -0
  6. package/dist/agent/gif.js +215 -0
  7. package/dist/agent/index.d.ts +8 -0
  8. package/dist/agent/index.js +8 -0
  9. package/dist/agent/message-manager/service.d.ts +30 -0
  10. package/dist/agent/message-manager/service.js +208 -0
  11. package/dist/agent/message-manager/utils.d.ts +2 -0
  12. package/dist/agent/message-manager/utils.js +41 -0
  13. package/dist/agent/message-manager/views.d.ts +26 -0
  14. package/dist/agent/message-manager/views.js +73 -0
  15. package/dist/agent/prompts.d.ts +52 -0
  16. package/dist/agent/prompts.js +259 -0
  17. package/dist/agent/service.d.ts +290 -0
  18. package/dist/agent/service.js +2200 -0
  19. package/dist/agent/views.d.ts +741 -0
  20. package/dist/agent/views.js +537 -0
  21. package/dist/browser/browser.d.ts +7 -0
  22. package/dist/browser/browser.js +5 -0
  23. package/dist/browser/context.d.ts +8 -0
  24. package/dist/browser/context.js +4 -0
  25. package/dist/browser/dvd-screensaver.d.ts +101 -0
  26. package/dist/browser/dvd-screensaver.js +270 -0
  27. package/dist/browser/extensions.d.ts +63 -0
  28. package/dist/browser/extensions.js +359 -0
  29. package/dist/browser/index.d.ts +10 -0
  30. package/dist/browser/index.js +9 -0
  31. package/dist/browser/playwright-manager.d.ts +47 -0
  32. package/dist/browser/playwright-manager.js +146 -0
  33. package/dist/browser/profile.d.ts +196 -0
  34. package/dist/browser/profile.js +815 -0
  35. package/dist/browser/session.d.ts +505 -0
  36. package/dist/browser/session.js +3409 -0
  37. package/dist/browser/types.d.ts +1184 -0
  38. package/dist/browser/types.js +1 -0
  39. package/dist/browser/utils.d.ts +1 -0
  40. package/dist/browser/utils.js +19 -0
  41. package/dist/browser/views.d.ts +78 -0
  42. package/dist/browser/views.js +72 -0
  43. package/dist/cli.d.ts +2 -0
  44. package/dist/cli.js +44 -0
  45. package/dist/config.d.ts +108 -0
  46. package/dist/config.js +430 -0
  47. package/dist/controller/index.d.ts +3 -0
  48. package/dist/controller/index.js +3 -0
  49. package/dist/controller/registry/index.d.ts +2 -0
  50. package/dist/controller/registry/index.js +2 -0
  51. package/dist/controller/registry/service.d.ts +45 -0
  52. package/dist/controller/registry/service.js +184 -0
  53. package/dist/controller/registry/views.d.ts +55 -0
  54. package/dist/controller/registry/views.js +174 -0
  55. package/dist/controller/service.d.ts +49 -0
  56. package/dist/controller/service.js +1176 -0
  57. package/dist/controller/views.d.ts +241 -0
  58. package/dist/controller/views.js +88 -0
  59. package/dist/dom/clickable-element-processor/service.d.ts +11 -0
  60. package/dist/dom/clickable-element-processor/service.js +60 -0
  61. package/dist/dom/dom_tree/index.js +1400 -0
  62. package/dist/dom/history-tree-processor/service.d.ts +14 -0
  63. package/dist/dom/history-tree-processor/service.js +75 -0
  64. package/dist/dom/history-tree-processor/view.d.ts +54 -0
  65. package/dist/dom/history-tree-processor/view.js +56 -0
  66. package/dist/dom/playground/extraction.d.ts +19 -0
  67. package/dist/dom/playground/extraction.js +187 -0
  68. package/dist/dom/playground/process-dom.d.ts +1 -0
  69. package/dist/dom/playground/process-dom.js +5 -0
  70. package/dist/dom/playground/test-accessibility.d.ts +44 -0
  71. package/dist/dom/playground/test-accessibility.js +111 -0
  72. package/dist/dom/service.d.ts +19 -0
  73. package/dist/dom/service.js +227 -0
  74. package/dist/dom/utils.d.ts +1 -0
  75. package/dist/dom/utils.js +6 -0
  76. package/dist/dom/views.d.ts +61 -0
  77. package/dist/dom/views.js +247 -0
  78. package/dist/event-bus.d.ts +11 -0
  79. package/dist/event-bus.js +19 -0
  80. package/dist/exceptions.d.ts +10 -0
  81. package/dist/exceptions.js +22 -0
  82. package/dist/filesystem/file-system.d.ts +68 -0
  83. package/dist/filesystem/file-system.js +412 -0
  84. package/dist/filesystem/index.d.ts +1 -0
  85. package/dist/filesystem/index.js +1 -0
  86. package/dist/index.d.ts +31 -0
  87. package/dist/index.js +33 -0
  88. package/dist/integrations/gmail/actions.d.ts +12 -0
  89. package/dist/integrations/gmail/actions.js +113 -0
  90. package/dist/integrations/gmail/index.d.ts +2 -0
  91. package/dist/integrations/gmail/index.js +2 -0
  92. package/dist/integrations/gmail/service.d.ts +61 -0
  93. package/dist/integrations/gmail/service.js +260 -0
  94. package/dist/llm/anthropic/chat.d.ts +28 -0
  95. package/dist/llm/anthropic/chat.js +126 -0
  96. package/dist/llm/anthropic/index.d.ts +2 -0
  97. package/dist/llm/anthropic/index.js +2 -0
  98. package/dist/llm/anthropic/serializer.d.ts +68 -0
  99. package/dist/llm/anthropic/serializer.js +285 -0
  100. package/dist/llm/aws/chat-anthropic.d.ts +61 -0
  101. package/dist/llm/aws/chat-anthropic.js +176 -0
  102. package/dist/llm/aws/chat-bedrock.d.ts +15 -0
  103. package/dist/llm/aws/chat-bedrock.js +80 -0
  104. package/dist/llm/aws/index.d.ts +3 -0
  105. package/dist/llm/aws/index.js +3 -0
  106. package/dist/llm/aws/serializer.d.ts +5 -0
  107. package/dist/llm/aws/serializer.js +68 -0
  108. package/dist/llm/azure/chat.d.ts +15 -0
  109. package/dist/llm/azure/chat.js +83 -0
  110. package/dist/llm/azure/index.d.ts +1 -0
  111. package/dist/llm/azure/index.js +1 -0
  112. package/dist/llm/base.d.ts +16 -0
  113. package/dist/llm/base.js +1 -0
  114. package/dist/llm/deepseek/chat.d.ts +15 -0
  115. package/dist/llm/deepseek/chat.js +51 -0
  116. package/dist/llm/deepseek/index.d.ts +2 -0
  117. package/dist/llm/deepseek/index.js +2 -0
  118. package/dist/llm/deepseek/serializer.d.ts +6 -0
  119. package/dist/llm/deepseek/serializer.js +57 -0
  120. package/dist/llm/exceptions.d.ts +10 -0
  121. package/dist/llm/exceptions.js +18 -0
  122. package/dist/llm/google/chat.d.ts +20 -0
  123. package/dist/llm/google/chat.js +144 -0
  124. package/dist/llm/google/index.d.ts +2 -0
  125. package/dist/llm/google/index.js +2 -0
  126. package/dist/llm/google/serializer.d.ts +6 -0
  127. package/dist/llm/google/serializer.js +64 -0
  128. package/dist/llm/groq/chat.d.ts +15 -0
  129. package/dist/llm/groq/chat.js +52 -0
  130. package/dist/llm/groq/index.d.ts +3 -0
  131. package/dist/llm/groq/index.js +3 -0
  132. package/dist/llm/groq/parser.d.ts +32 -0
  133. package/dist/llm/groq/parser.js +189 -0
  134. package/dist/llm/groq/serializer.d.ts +6 -0
  135. package/dist/llm/groq/serializer.js +56 -0
  136. package/dist/llm/messages.d.ts +77 -0
  137. package/dist/llm/messages.js +157 -0
  138. package/dist/llm/ollama/chat.d.ts +15 -0
  139. package/dist/llm/ollama/chat.js +77 -0
  140. package/dist/llm/ollama/index.d.ts +2 -0
  141. package/dist/llm/ollama/index.js +2 -0
  142. package/dist/llm/ollama/serializer.d.ts +6 -0
  143. package/dist/llm/ollama/serializer.js +53 -0
  144. package/dist/llm/openai/chat.d.ts +38 -0
  145. package/dist/llm/openai/chat.js +174 -0
  146. package/dist/llm/openai/index.d.ts +3 -0
  147. package/dist/llm/openai/index.js +3 -0
  148. package/dist/llm/openai/like.d.ts +17 -0
  149. package/dist/llm/openai/like.js +19 -0
  150. package/dist/llm/openai/serializer.d.ts +6 -0
  151. package/dist/llm/openai/serializer.js +57 -0
  152. package/dist/llm/openrouter/chat.d.ts +15 -0
  153. package/dist/llm/openrouter/chat.js +74 -0
  154. package/dist/llm/openrouter/index.d.ts +2 -0
  155. package/dist/llm/openrouter/index.js +2 -0
  156. package/dist/llm/openrouter/serializer.d.ts +3 -0
  157. package/dist/llm/openrouter/serializer.js +3 -0
  158. package/dist/llm/schema.d.ts +6 -0
  159. package/dist/llm/schema.js +77 -0
  160. package/dist/llm/views.d.ts +15 -0
  161. package/dist/llm/views.js +12 -0
  162. package/dist/logging-config.d.ts +25 -0
  163. package/dist/logging-config.js +89 -0
  164. package/dist/mcp/client.d.ts +142 -0
  165. package/dist/mcp/client.js +638 -0
  166. package/dist/mcp/controller.d.ts +6 -0
  167. package/dist/mcp/controller.js +38 -0
  168. package/dist/mcp/index.d.ts +3 -0
  169. package/dist/mcp/index.js +3 -0
  170. package/dist/mcp/server.d.ts +134 -0
  171. package/dist/mcp/server.js +759 -0
  172. package/dist/observability-decorators.d.ts +158 -0
  173. package/dist/observability-decorators.js +286 -0
  174. package/dist/observability.d.ts +23 -0
  175. package/dist/observability.js +58 -0
  176. package/dist/screenshots/index.d.ts +1 -0
  177. package/dist/screenshots/index.js +1 -0
  178. package/dist/screenshots/service.d.ts +6 -0
  179. package/dist/screenshots/service.js +28 -0
  180. package/dist/sync/auth.d.ts +27 -0
  181. package/dist/sync/auth.js +205 -0
  182. package/dist/sync/index.d.ts +2 -0
  183. package/dist/sync/index.js +2 -0
  184. package/dist/sync/service.d.ts +21 -0
  185. package/dist/sync/service.js +146 -0
  186. package/dist/telemetry/index.d.ts +2 -0
  187. package/dist/telemetry/index.js +2 -0
  188. package/dist/telemetry/service.d.ts +12 -0
  189. package/dist/telemetry/service.js +85 -0
  190. package/dist/telemetry/views.d.ts +112 -0
  191. package/dist/telemetry/views.js +112 -0
  192. package/dist/tokens/index.d.ts +2 -0
  193. package/dist/tokens/index.js +2 -0
  194. package/dist/tokens/service.d.ts +35 -0
  195. package/dist/tokens/service.js +423 -0
  196. package/dist/tokens/views.d.ts +58 -0
  197. package/dist/tokens/views.js +1 -0
  198. package/dist/utils.d.ts +128 -0
  199. package/dist/utils.js +529 -0
  200. package/package.json +94 -5
@@ -0,0 +1,537 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { ActionModel } from '../controller/registry/views.js';
4
+ import { BrowserStateHistory } from '../browser/views.js';
5
+ import { HistoryTreeProcessor } from '../dom/history-tree-processor/service.js';
6
+ import { MessageManagerState } from './message-manager/views.js';
7
+ // Re-export ActionModel for agent/service.ts
8
+ export { ActionModel };
9
+ const parseStructuredOutput = (schema, value) => {
10
+ if (!schema) {
11
+ return null;
12
+ }
13
+ if (schema.parse) {
14
+ return schema.parse(value);
15
+ }
16
+ if (schema.model_validate_json) {
17
+ return schema.model_validate_json(value);
18
+ }
19
+ return null;
20
+ };
21
+ export class ActionResult {
22
+ is_done;
23
+ success;
24
+ error;
25
+ attachments;
26
+ long_term_memory;
27
+ extracted_content;
28
+ include_extracted_content_only_once;
29
+ include_in_memory;
30
+ constructor(init = {}) {
31
+ this.is_done = init.is_done ?? false;
32
+ this.success = init.success ?? null;
33
+ this.error = init.error ?? null;
34
+ this.attachments = init.attachments ?? null;
35
+ this.long_term_memory = init.long_term_memory ?? null;
36
+ this.extracted_content = init.extracted_content ?? null;
37
+ this.include_extracted_content_only_once =
38
+ init.include_extracted_content_only_once ?? false;
39
+ this.include_in_memory = init.include_in_memory ?? false;
40
+ this.validate();
41
+ }
42
+ validate() {
43
+ if (this.success === true && this.is_done !== true) {
44
+ throw new Error('success=True can only be set when is_done=True. For regular actions that succeed, leave success as None. Use success=False only for actions that fail.');
45
+ }
46
+ }
47
+ toJSON() {
48
+ return {
49
+ is_done: this.is_done,
50
+ success: this.success,
51
+ error: this.error,
52
+ attachments: this.attachments,
53
+ long_term_memory: this.long_term_memory,
54
+ extracted_content: this.extracted_content,
55
+ include_extracted_content_only_once: this.include_extracted_content_only_once,
56
+ include_in_memory: this.include_in_memory,
57
+ };
58
+ }
59
+ model_dump() {
60
+ return this.toJSON();
61
+ }
62
+ model_dump_json() {
63
+ return JSON.stringify(this.toJSON());
64
+ }
65
+ }
66
+ export const defaultAgentSettings = () => ({
67
+ session_attachment_mode: 'copy',
68
+ use_vision: true,
69
+ vision_detail_level: 'auto',
70
+ use_vision_for_planner: false,
71
+ save_conversation_path: null,
72
+ save_conversation_path_encoding: 'utf-8',
73
+ max_failures: 3,
74
+ retry_delay: 10,
75
+ validate_output: false,
76
+ generate_gif: false,
77
+ override_system_message: null,
78
+ extend_system_message: null,
79
+ include_attributes: [
80
+ 'title',
81
+ 'type',
82
+ 'name',
83
+ 'role',
84
+ 'tabindex',
85
+ 'aria-label',
86
+ 'placeholder',
87
+ 'value',
88
+ 'alt',
89
+ 'aria-expanded',
90
+ ],
91
+ max_actions_per_step: 10,
92
+ use_thinking: true,
93
+ flash_mode: false,
94
+ max_history_items: null,
95
+ page_extraction_llm: null,
96
+ planner_llm: null,
97
+ planner_interval: 1,
98
+ is_planner_reasoning: false,
99
+ extend_planner_system_message: null,
100
+ calculate_cost: false,
101
+ include_tool_call_examples: false,
102
+ llm_timeout: 60,
103
+ step_timeout: 180,
104
+ });
105
+ export class AgentState {
106
+ agent_id;
107
+ n_steps;
108
+ consecutive_failures;
109
+ last_result;
110
+ last_plan;
111
+ last_model_output;
112
+ paused;
113
+ stopped;
114
+ message_manager_state;
115
+ file_system_state;
116
+ constructor(init) {
117
+ this.agent_id = init?.agent_id ?? '';
118
+ this.n_steps = init?.n_steps ?? 1;
119
+ this.consecutive_failures = init?.consecutive_failures ?? 0;
120
+ this.last_result = init?.last_result ?? null;
121
+ this.last_plan = init?.last_plan ?? null;
122
+ this.last_model_output = init?.last_model_output ?? null;
123
+ this.paused = init?.paused ?? false;
124
+ this.stopped = init?.stopped ?? false;
125
+ if (init?.message_manager_state instanceof MessageManagerState) {
126
+ this.message_manager_state = init.message_manager_state;
127
+ }
128
+ else if (init?.message_manager_state) {
129
+ this.message_manager_state = Object.assign(new MessageManagerState(), init.message_manager_state);
130
+ }
131
+ else {
132
+ this.message_manager_state = new MessageManagerState();
133
+ }
134
+ this.file_system_state = init?.file_system_state ?? null;
135
+ }
136
+ model_dump() {
137
+ return {
138
+ agent_id: this.agent_id,
139
+ n_steps: this.n_steps,
140
+ consecutive_failures: this.consecutive_failures,
141
+ last_result: this.last_result?.map((result) => result.model_dump()) ?? null,
142
+ last_plan: this.last_plan,
143
+ last_model_output: this.last_model_output?.model_dump() ?? null,
144
+ paused: this.paused,
145
+ stopped: this.stopped,
146
+ message_manager_state: JSON.parse(JSON.stringify(this.message_manager_state)),
147
+ file_system_state: this.file_system_state,
148
+ };
149
+ }
150
+ toJSON() {
151
+ return this.model_dump();
152
+ }
153
+ }
154
+ export class AgentStepInfo {
155
+ step_number;
156
+ max_steps;
157
+ constructor(step_number, max_steps) {
158
+ this.step_number = step_number;
159
+ this.max_steps = max_steps;
160
+ }
161
+ is_last_step() {
162
+ return this.step_number >= this.max_steps - 1;
163
+ }
164
+ }
165
+ export class StepMetadata {
166
+ step_start_time;
167
+ step_end_time;
168
+ step_number;
169
+ constructor(step_start_time, step_end_time, step_number) {
170
+ this.step_start_time = step_start_time;
171
+ this.step_end_time = step_end_time;
172
+ this.step_number = step_number;
173
+ }
174
+ get duration_seconds() {
175
+ return this.step_end_time - this.step_start_time;
176
+ }
177
+ }
178
+ export class AgentOutput {
179
+ thinking;
180
+ evaluation_previous_goal;
181
+ memory;
182
+ next_goal;
183
+ action;
184
+ constructor(init) {
185
+ this.thinking = init?.thinking ?? null;
186
+ this.evaluation_previous_goal = init?.evaluation_previous_goal ?? null;
187
+ this.memory = init?.memory ?? null;
188
+ this.next_goal = init?.next_goal ?? null;
189
+ this.action = (init?.action ?? []).map((entry) => entry instanceof ActionModel ? entry : new ActionModel(entry));
190
+ }
191
+ get current_state() {
192
+ return {
193
+ thinking: this.thinking,
194
+ evaluation_previous_goal: this.evaluation_previous_goal ?? '',
195
+ memory: this.memory ?? '',
196
+ next_goal: this.next_goal ?? '',
197
+ };
198
+ }
199
+ model_dump() {
200
+ return {
201
+ thinking: this.thinking,
202
+ evaluation_previous_goal: this.evaluation_previous_goal,
203
+ memory: this.memory,
204
+ next_goal: this.next_goal,
205
+ action: this.action.map((action) => action.model_dump?.() ?? action),
206
+ };
207
+ }
208
+ model_dump_json() {
209
+ return JSON.stringify(this.model_dump());
210
+ }
211
+ toJSON() {
212
+ return this.model_dump();
213
+ }
214
+ static fromJSON(data) {
215
+ if (!data) {
216
+ return new AgentOutput();
217
+ }
218
+ const actions = Array.isArray(data.action)
219
+ ? data.action.map((item) => new ActionModel(item))
220
+ : [];
221
+ return new AgentOutput({
222
+ thinking: data.thinking ?? null,
223
+ evaluation_previous_goal: data.evaluation_previous_goal ?? null,
224
+ memory: data.memory ?? null,
225
+ next_goal: data.next_goal ?? null,
226
+ action: actions,
227
+ });
228
+ }
229
+ static type_with_custom_actions(custom_actions) {
230
+ const CustomActionModel = custom_actions;
231
+ return class AgentOutputWithCustomActions extends AgentOutput {
232
+ constructor(init) {
233
+ super(init);
234
+ this.action = (init?.action ?? []).map((entry) => entry instanceof CustomActionModel
235
+ ? entry
236
+ : new CustomActionModel(entry?.model_dump?.() ?? entry));
237
+ }
238
+ };
239
+ }
240
+ static type_with_custom_actions_no_thinking(custom_actions) {
241
+ const BaseModel = AgentOutput.type_with_custom_actions(custom_actions);
242
+ return class AgentOutputWithoutThinking extends BaseModel {
243
+ constructor(init) {
244
+ super(init);
245
+ this.thinking = null;
246
+ }
247
+ };
248
+ }
249
+ static type_with_custom_actions_flash_mode(custom_actions) {
250
+ const BaseModel = AgentOutput.type_with_custom_actions(custom_actions);
251
+ return class AgentOutputFlashMode extends BaseModel {
252
+ constructor(init) {
253
+ super(init);
254
+ this.thinking = null;
255
+ this.evaluation_previous_goal = null;
256
+ this.next_goal = null;
257
+ }
258
+ };
259
+ }
260
+ }
261
+ export class AgentHistory {
262
+ model_output;
263
+ result;
264
+ state;
265
+ metadata;
266
+ constructor(model_output, result, state, metadata = null) {
267
+ this.model_output = model_output;
268
+ this.result = result;
269
+ this.state = state;
270
+ this.metadata = metadata;
271
+ }
272
+ static get_interacted_element(model_output, selector_map) {
273
+ const elements = [];
274
+ for (const action of model_output.action) {
275
+ const index = typeof action.get_index === 'function' ? action.get_index() : null;
276
+ if (index != null && selector_map[index]) {
277
+ const node = selector_map[index];
278
+ elements.push(HistoryTreeProcessor.convert_dom_element_to_history_element(node));
279
+ }
280
+ else {
281
+ elements.push(null);
282
+ }
283
+ }
284
+ return elements;
285
+ }
286
+ toJSON() {
287
+ return {
288
+ model_output: this.model_output?.toJSON() ?? null,
289
+ result: this.result.map((r) => r.toJSON()),
290
+ state: this.state.to_dict(),
291
+ metadata: this.metadata
292
+ ? {
293
+ step_start_time: this.metadata.step_start_time,
294
+ step_end_time: this.metadata.step_end_time,
295
+ step_number: this.metadata.step_number,
296
+ }
297
+ : null,
298
+ };
299
+ }
300
+ }
301
+ export class AgentHistoryList {
302
+ history;
303
+ usage;
304
+ _output_model_schema = null;
305
+ constructor(history = [], usage = null) {
306
+ this.history = history;
307
+ this.usage = usage ?? null;
308
+ }
309
+ total_duration_seconds() {
310
+ return this.history.reduce((sum, item) => sum + (item.metadata?.duration_seconds ?? 0), 0);
311
+ }
312
+ add_item(history_item) {
313
+ this.history.push(history_item);
314
+ }
315
+ last_action() {
316
+ if (!this.history.length) {
317
+ return null;
318
+ }
319
+ const last = this.history[this.history.length - 1];
320
+ if (!last.model_output || !last.model_output.action.length) {
321
+ return null;
322
+ }
323
+ const action = last.model_output.action[last.model_output.action.length - 1];
324
+ if (typeof action?.model_dump === 'function') {
325
+ return action.model_dump();
326
+ }
327
+ return action;
328
+ }
329
+ errors() {
330
+ return this.history.map((historyItem) => {
331
+ const error = historyItem.result.find((result) => result.error);
332
+ return error?.error ?? null;
333
+ });
334
+ }
335
+ final_result() {
336
+ if (!this.history.length) {
337
+ return null;
338
+ }
339
+ const last = this.history[this.history.length - 1];
340
+ const result = last.result[last.result.length - 1];
341
+ return result?.extracted_content ?? null;
342
+ }
343
+ is_done() {
344
+ if (!this.history.length) {
345
+ return false;
346
+ }
347
+ const last = this.history[this.history.length - 1];
348
+ const result = last.result[last.result.length - 1];
349
+ return result?.is_done === true;
350
+ }
351
+ is_successful() {
352
+ if (!this.history.length) {
353
+ return null;
354
+ }
355
+ const last = this.history[this.history.length - 1];
356
+ const result = last.result[last.result.length - 1];
357
+ if (result?.is_done) {
358
+ return result.success ?? null;
359
+ }
360
+ return null;
361
+ }
362
+ has_errors() {
363
+ return this.errors().some((error) => error != null);
364
+ }
365
+ urls() {
366
+ return this.history.map((item) => item.state.url ?? null);
367
+ }
368
+ screenshot_paths(n_last = null, return_none_if_not_screenshot = true) {
369
+ if (n_last === 0) {
370
+ return [];
371
+ }
372
+ const items = n_last == null ? this.history : this.history.slice(-n_last);
373
+ return items
374
+ .map((item) => item.state.screenshot_path ?? null)
375
+ .filter((pathValue) => return_none_if_not_screenshot || pathValue !== null);
376
+ }
377
+ screenshots(n_last = null, return_none_if_not_screenshot = true) {
378
+ if (n_last === 0) {
379
+ return [];
380
+ }
381
+ const items = n_last == null ? this.history : this.history.slice(-n_last);
382
+ const screenshots = [];
383
+ for (const item of items) {
384
+ const screenshot = item.state.get_screenshot();
385
+ if (screenshot) {
386
+ screenshots.push(screenshot);
387
+ }
388
+ else if (return_none_if_not_screenshot) {
389
+ screenshots.push(null);
390
+ }
391
+ }
392
+ return screenshots;
393
+ }
394
+ action_names() {
395
+ const names = [];
396
+ for (const action of this.model_actions()) {
397
+ const [name] = Object.keys(action);
398
+ if (name) {
399
+ names.push(name);
400
+ }
401
+ }
402
+ return names;
403
+ }
404
+ model_thoughts() {
405
+ return this.history
406
+ .filter((item) => item.model_output)
407
+ .map((item) => item.model_output.current_state);
408
+ }
409
+ model_outputs() {
410
+ return (this.history
411
+ .filter((item) => item.model_output)
412
+ .map((item) => item.model_output) ?? []);
413
+ }
414
+ model_actions() {
415
+ const outputs = [];
416
+ for (const item of this.history) {
417
+ if (!item.model_output) {
418
+ continue;
419
+ }
420
+ const interacted = item.state.interacted_element ?? [];
421
+ for (let index = 0; index < item.model_output.action.length; index += 1) {
422
+ const action = item.model_output.action[index];
423
+ const interactedElement = interacted[index] ?? null;
424
+ const payload = typeof action?.model_dump === 'function'
425
+ ? action.model_dump()
426
+ : action;
427
+ if (payload && typeof payload === 'object' && interactedElement) {
428
+ payload.interacted_element =
429
+ interactedElement;
430
+ }
431
+ else if (payload && typeof payload === 'object') {
432
+ payload.interacted_element =
433
+ interactedElement;
434
+ }
435
+ outputs.push(payload);
436
+ }
437
+ }
438
+ return outputs;
439
+ }
440
+ action_history() {
441
+ const history = [];
442
+ for (const item of this.history) {
443
+ const stepActions = [];
444
+ if (item.model_output) {
445
+ const interacted = item.state.interacted_element ?? [];
446
+ for (let index = 0; index < item.model_output.action.length; index += 1) {
447
+ const action = item.model_output.action[index];
448
+ const interactedElement = interacted[index] ?? null;
449
+ const result = item.result[index];
450
+ const payload = typeof action?.model_dump === 'function'
451
+ ? action.model_dump()
452
+ : action;
453
+ const enriched = payload && typeof payload === 'object'
454
+ ? { ...payload }
455
+ : { action: payload };
456
+ enriched.interacted_element = interactedElement;
457
+ enriched.result = result?.long_term_memory ?? null;
458
+ stepActions.push(enriched);
459
+ }
460
+ }
461
+ history.push(stepActions);
462
+ }
463
+ return history;
464
+ }
465
+ action_results() {
466
+ return this.history.flatMap((item) => item.result);
467
+ }
468
+ extracted_content() {
469
+ return this.history.flatMap((item) => item.result.map((result) => result.extracted_content).filter(Boolean));
470
+ }
471
+ model_actions_filtered(include = []) {
472
+ if (!include.length) {
473
+ return this.model_actions();
474
+ }
475
+ return this.model_actions().filter((action) => {
476
+ const [name] = Object.keys(action);
477
+ return include.includes(name);
478
+ });
479
+ }
480
+ number_of_steps() {
481
+ return this.history.length;
482
+ }
483
+ get structured_output() {
484
+ const final_result = this.final_result();
485
+ if (!final_result || !this._output_model_schema) {
486
+ return null;
487
+ }
488
+ return parseStructuredOutput(this._output_model_schema, final_result);
489
+ }
490
+ save_to_file(filepath) {
491
+ const dir = path.dirname(filepath);
492
+ fs.mkdirSync(dir, { recursive: true });
493
+ fs.writeFileSync(filepath, JSON.stringify(this.toJSON(), null, 2), 'utf-8');
494
+ }
495
+ static load_from_file(filepath, outputModel) {
496
+ const content = fs.readFileSync(filepath, 'utf-8');
497
+ const payload = JSON.parse(content);
498
+ const historyItems = (payload.history ?? []).map((entry) => {
499
+ const modelOutput = entry.model_output
500
+ ? outputModel.fromJSON(entry.model_output)
501
+ : null;
502
+ const result = (entry.result ?? []).map((item) => new ActionResult(item));
503
+ const state = new BrowserStateHistory(entry.state?.url ?? '', entry.state?.title ?? '', entry.state?.tabs ?? [], entry.state?.interacted_element ?? [], entry.state?.screenshot_path ?? null);
504
+ const metadata = entry.metadata
505
+ ? new StepMetadata(entry.metadata.step_start_time, entry.metadata.step_end_time, entry.metadata.step_number)
506
+ : null;
507
+ return new AgentHistory(modelOutput, result, state, metadata);
508
+ });
509
+ return new AgentHistoryList(historyItems);
510
+ }
511
+ toJSON() {
512
+ return {
513
+ history: this.history.map((item) => item.toJSON()),
514
+ usage: this.usage,
515
+ };
516
+ }
517
+ model_dump() {
518
+ return this.toJSON();
519
+ }
520
+ }
521
+ export class AgentError extends Error {
522
+ static VALIDATION_ERROR = 'Invalid model output format. Please follow the correct schema.';
523
+ static RATE_LIMIT_ERROR = 'Rate limit reached. Waiting before retry.';
524
+ static NO_VALID_ACTION = 'No valid action found';
525
+ static format_error(error, include_trace = false) {
526
+ if (error?.name === 'ValidationError') {
527
+ return `${AgentError.VALIDATION_ERROR}\nDetails: ${error.message}`;
528
+ }
529
+ if (error.name === 'RateLimitError') {
530
+ return AgentError.RATE_LIMIT_ERROR;
531
+ }
532
+ if (include_trace && error?.stack) {
533
+ return `${error.message}\nStacktrace:\n${error.stack}`;
534
+ }
535
+ return error.message;
536
+ }
537
+ }
@@ -0,0 +1,7 @@
1
+ import { BrowserProfile } from './profile.js';
2
+ import { BrowserSession } from './session.js';
3
+ export type BrowserConfig = BrowserProfile;
4
+ export type BrowserContextConfig = BrowserProfile;
5
+ export declare const Browser: typeof BrowserSession;
6
+ export { BrowserProfile };
7
+ export { BrowserSession };
@@ -0,0 +1,5 @@
1
+ import { BrowserProfile } from './profile.js';
2
+ import { BrowserSession } from './session.js';
3
+ export const Browser = BrowserSession;
4
+ export { BrowserProfile };
5
+ export { BrowserSession };
@@ -0,0 +1,8 @@
1
+ import { BrowserProfile } from './profile.js';
2
+ import { BrowserSession } from './session.js';
3
+ export type Browser = BrowserSession;
4
+ export type BrowserConfig = BrowserProfile;
5
+ export type BrowserContext = BrowserSession;
6
+ export type BrowserContextConfig = BrowserProfile;
7
+ export { BrowserProfile };
8
+ export { BrowserSession };
@@ -0,0 +1,4 @@
1
+ import { BrowserProfile } from './profile.js';
2
+ import { BrowserSession } from './session.js';
3
+ export { BrowserProfile };
4
+ export { BrowserSession };
@@ -0,0 +1,101 @@
1
+ /**
2
+ * DVD Screensaver Loading Animation
3
+ *
4
+ * Displays a fun DVD logo bouncing animation while waiting for browser operations.
5
+ * Inspired by the classic DVD screensaver.
6
+ */
7
+ /**
8
+ * DVD Screensaver Animation Controller
9
+ */
10
+ export declare class DVDScreensaver {
11
+ private isRunning;
12
+ private intervalId;
13
+ private width;
14
+ private height;
15
+ private x;
16
+ private y;
17
+ private dx;
18
+ private dy;
19
+ private logoWidth;
20
+ private logoHeight;
21
+ private colors;
22
+ private currentColorIndex;
23
+ private cornerHits;
24
+ private frameCount;
25
+ private message;
26
+ constructor(message?: string);
27
+ /**
28
+ * Start the animation
29
+ */
30
+ start(fps?: number): void;
31
+ /**
32
+ * Stop the animation
33
+ */
34
+ stop(): void;
35
+ /**
36
+ * Update logo position
37
+ */
38
+ private update;
39
+ /**
40
+ * Change logo color
41
+ */
42
+ private changeColor;
43
+ /**
44
+ * Render the current frame
45
+ */
46
+ private render;
47
+ /**
48
+ * Get character for logo at specific position
49
+ */
50
+ private getLogoChar;
51
+ /**
52
+ * Clear screen
53
+ */
54
+ private clear;
55
+ }
56
+ /**
57
+ * Show DVD screensaver loading animation
58
+ * Returns a function to stop the animation
59
+ *
60
+ * @param message - Message to display
61
+ * @param fps - Frames per second (default: 10)
62
+ * @returns Function to stop the animation
63
+ *
64
+ * @example
65
+ * const stopAnimation = showDVDScreensaver('Loading browser...');
66
+ * await someAsyncOperation();
67
+ * stopAnimation();
68
+ */
69
+ export declare function showDVDScreensaver(message?: string, fps?: number): () => void;
70
+ /**
71
+ * Run an async operation with DVD screensaver animation
72
+ *
73
+ * @param operation - Async operation to run
74
+ * @param message - Message to display
75
+ * @returns Result of the operation
76
+ *
77
+ * @example
78
+ * const result = await withDVDScreensaver(
79
+ * async () => await longRunningOperation(),
80
+ * 'Processing...'
81
+ * );
82
+ */
83
+ export declare function withDVDScreensaver<T>(operation: () => Promise<T>, message?: string): Promise<T>;
84
+ /**
85
+ * Simple spinner animation (alternative to DVD screensaver)
86
+ */
87
+ export declare class SpinnerAnimation {
88
+ private isRunning;
89
+ private intervalId;
90
+ private frames;
91
+ private currentFrame;
92
+ private message;
93
+ constructor(message?: string);
94
+ start(fps?: number): void;
95
+ stop(): void;
96
+ private render;
97
+ }
98
+ /**
99
+ * Show simple spinner animation
100
+ */
101
+ export declare function showSpinner(message?: string, fps?: number): () => void;