browser-use 0.2.0 → 0.3.0
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.
- package/README.md +295 -686
- package/dist/actor/element.d.ts +19 -0
- package/dist/actor/element.js +46 -0
- package/dist/actor/index.d.ts +4 -0
- package/dist/actor/index.js +4 -0
- package/dist/actor/mouse.d.ts +19 -0
- package/dist/actor/mouse.js +39 -0
- package/dist/actor/page.d.ts +29 -0
- package/dist/actor/page.js +88 -0
- package/dist/actor/utils.d.ts +4 -0
- package/dist/actor/utils.js +35 -0
- package/dist/agent/cloud-events.d.ts +18 -0
- package/dist/agent/cloud-events.js +65 -2
- package/dist/agent/gif.d.ts +1 -0
- package/dist/agent/gif.js +24 -2
- package/dist/agent/judge.d.ts +17 -0
- package/dist/agent/judge.js +197 -0
- package/dist/agent/message-manager/service.d.ts +12 -4
- package/dist/agent/message-manager/service.js +205 -39
- package/dist/agent/message-manager/utils.js +0 -1
- package/dist/agent/message-manager/views.d.ts +4 -0
- package/dist/agent/message-manager/views.js +11 -7
- package/dist/agent/prompts.d.ts +24 -3
- package/dist/agent/prompts.js +274 -59
- package/dist/agent/service.d.ts +99 -41
- package/dist/agent/service.js +2266 -472
- package/dist/agent/variable-detector.d.ts +12 -0
- package/dist/agent/variable-detector.js +211 -0
- package/dist/agent/views.d.ts +237 -18
- package/dist/agent/views.js +446 -33
- package/dist/browser/cloud/cloud.d.ts +20 -0
- package/dist/browser/cloud/cloud.js +129 -0
- package/dist/browser/cloud/index.d.ts +2 -0
- package/dist/browser/cloud/index.js +2 -0
- package/dist/browser/cloud/views.d.ts +41 -0
- package/dist/browser/cloud/views.js +35 -0
- package/dist/browser/events.d.ts +345 -0
- package/dist/browser/events.js +566 -0
- package/dist/browser/extensions.js +17 -17
- package/dist/browser/index.d.ts +4 -0
- package/dist/browser/index.js +4 -0
- package/dist/browser/profile.d.ts +8 -2
- package/dist/browser/profile.js +79 -12
- package/dist/browser/session-manager.d.ts +85 -0
- package/dist/browser/session-manager.js +208 -0
- package/dist/browser/session.d.ts +100 -8
- package/dist/browser/session.js +1097 -58
- package/dist/browser/types.d.ts +0 -2
- package/dist/browser/views.d.ts +39 -0
- package/dist/browser/views.js +32 -0
- package/dist/browser/watchdogs/aboutblank-watchdog.d.ts +12 -0
- package/dist/browser/watchdogs/aboutblank-watchdog.js +131 -0
- package/dist/browser/watchdogs/base.d.ts +21 -0
- package/dist/browser/watchdogs/base.js +81 -0
- package/dist/browser/watchdogs/cdp-session-watchdog.d.ts +14 -0
- package/dist/browser/watchdogs/cdp-session-watchdog.js +177 -0
- package/dist/browser/watchdogs/crash-watchdog.d.ts +38 -0
- package/dist/browser/watchdogs/crash-watchdog.js +296 -0
- package/dist/browser/watchdogs/default-action-watchdog.d.ts +49 -0
- package/dist/browser/watchdogs/default-action-watchdog.js +212 -0
- package/dist/browser/watchdogs/dom-watchdog.d.ts +8 -0
- package/dist/browser/watchdogs/dom-watchdog.js +31 -0
- package/dist/browser/watchdogs/downloads-watchdog.d.ts +77 -0
- package/dist/browser/watchdogs/downloads-watchdog.js +409 -0
- package/dist/browser/watchdogs/har-recording-watchdog.d.ts +19 -0
- package/dist/browser/watchdogs/har-recording-watchdog.js +317 -0
- package/dist/browser/watchdogs/index.d.ts +15 -0
- package/dist/browser/watchdogs/index.js +15 -0
- package/dist/browser/watchdogs/local-browser-watchdog.d.ts +10 -0
- package/dist/browser/watchdogs/local-browser-watchdog.js +32 -0
- package/dist/browser/watchdogs/permissions-watchdog.d.ts +8 -0
- package/dist/browser/watchdogs/permissions-watchdog.js +73 -0
- package/dist/browser/watchdogs/popups-watchdog.d.ts +13 -0
- package/dist/browser/watchdogs/popups-watchdog.js +77 -0
- package/dist/browser/watchdogs/recording-watchdog.d.ts +27 -0
- package/dist/browser/watchdogs/recording-watchdog.js +249 -0
- package/dist/browser/watchdogs/screenshot-watchdog.d.ts +6 -0
- package/dist/browser/watchdogs/screenshot-watchdog.js +13 -0
- package/dist/browser/watchdogs/security-watchdog.d.ts +10 -0
- package/dist/browser/watchdogs/security-watchdog.js +84 -0
- package/dist/browser/watchdogs/storage-state-watchdog.d.ts +24 -0
- package/dist/browser/watchdogs/storage-state-watchdog.js +288 -0
- package/dist/cli.d.ts +7 -2
- package/dist/cli.js +182 -25
- package/dist/code-use/formatting.d.ts +3 -0
- package/dist/code-use/formatting.js +18 -0
- package/dist/code-use/index.d.ts +6 -0
- package/dist/code-use/index.js +6 -0
- package/dist/code-use/namespace.d.ts +5 -0
- package/dist/code-use/namespace.js +81 -0
- package/dist/code-use/notebook-export.d.ts +3 -0
- package/dist/code-use/notebook-export.js +56 -0
- package/dist/code-use/service.d.ts +24 -0
- package/dist/code-use/service.js +104 -0
- package/dist/code-use/utils.d.ts +4 -0
- package/dist/code-use/utils.js +98 -0
- package/dist/code-use/views.d.ts +108 -0
- package/dist/code-use/views.js +165 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.js +69 -3
- package/dist/controller/registry/service.d.ts +10 -1
- package/dist/controller/registry/service.js +266 -10
- package/dist/controller/registry/views.d.ts +4 -1
- package/dist/controller/registry/views.js +25 -2
- package/dist/controller/service.d.ts +10 -1
- package/dist/controller/service.js +1807 -268
- package/dist/controller/views.d.ts +78 -155
- package/dist/controller/views.js +61 -12
- package/dist/dom/history-tree-processor/service.d.ts +5 -0
- package/dist/dom/history-tree-processor/service.js +169 -14
- package/dist/dom/history-tree-processor/view.d.ts +7 -1
- package/dist/dom/history-tree-processor/view.js +10 -1
- package/dist/dom/markdown-extractor.d.ts +37 -0
- package/dist/dom/markdown-extractor.js +345 -0
- package/dist/dom/service.d.ts +3 -1
- package/dist/dom/service.js +76 -0
- package/dist/dom/views.d.ts +1 -0
- package/dist/dom/views.js +45 -0
- package/dist/event-bus.d.ts +107 -7
- package/dist/event-bus.js +313 -10
- package/dist/exceptions.d.ts +0 -3
- package/dist/exceptions.js +0 -7
- package/dist/filesystem/file-system.d.ts +18 -0
- package/dist/filesystem/file-system.js +503 -42
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/integrations/gmail/actions.d.ts +3 -3
- package/dist/integrations/gmail/actions.js +4 -4
- package/dist/llm/anthropic/chat.d.ts +18 -1
- package/dist/llm/anthropic/chat.js +123 -55
- package/dist/llm/anthropic/serializer.d.ts +2 -0
- package/dist/llm/anthropic/serializer.js +81 -9
- package/dist/llm/aws/chat-anthropic.d.ts +17 -0
- package/dist/llm/aws/chat-anthropic.js +126 -26
- package/dist/llm/aws/chat-bedrock.d.ts +28 -1
- package/dist/llm/aws/chat-bedrock.js +161 -34
- package/dist/llm/aws/serializer.d.ts +13 -1
- package/dist/llm/aws/serializer.js +56 -17
- package/dist/llm/azure/chat.d.ts +53 -2
- package/dist/llm/azure/chat.js +366 -54
- package/dist/llm/base.d.ts +2 -0
- package/dist/llm/browser-use/chat.d.ts +40 -0
- package/dist/llm/browser-use/chat.js +305 -0
- package/dist/llm/browser-use/index.d.ts +1 -0
- package/dist/llm/browser-use/index.js +1 -0
- package/dist/llm/cerebras/chat.d.ts +39 -0
- package/dist/llm/cerebras/chat.js +178 -0
- package/dist/llm/cerebras/index.d.ts +2 -0
- package/dist/llm/cerebras/index.js +2 -0
- package/dist/llm/cerebras/serializer.d.ts +7 -0
- package/dist/llm/cerebras/serializer.js +82 -0
- package/dist/llm/deepseek/chat.d.ts +19 -2
- package/dist/llm/deepseek/chat.js +138 -25
- package/dist/llm/google/chat.d.ts +46 -2
- package/dist/llm/google/chat.js +267 -64
- package/dist/llm/google/serializer.d.ts +9 -1
- package/dist/llm/google/serializer.js +141 -34
- package/dist/llm/groq/chat.d.ts +21 -2
- package/dist/llm/groq/chat.js +125 -26
- package/dist/llm/groq/parser.js +3 -1
- package/dist/llm/mistral/chat.d.ts +43 -0
- package/dist/llm/mistral/chat.js +154 -0
- package/dist/llm/mistral/index.d.ts +2 -0
- package/dist/llm/mistral/index.js +2 -0
- package/dist/llm/mistral/schema.d.ts +8 -0
- package/dist/llm/mistral/schema.js +27 -0
- package/dist/llm/models.d.ts +2 -0
- package/dist/llm/models.js +317 -0
- package/dist/llm/ollama/chat.d.ts +13 -1
- package/dist/llm/ollama/chat.js +110 -19
- package/dist/llm/ollama/serializer.d.ts +1 -0
- package/dist/llm/ollama/serializer.js +34 -12
- package/dist/llm/openai/chat.d.ts +16 -0
- package/dist/llm/openai/chat.js +94 -44
- package/dist/llm/openai/like.d.ts +5 -3
- package/dist/llm/openai/like.js +7 -3
- package/dist/llm/openai/responses-serializer.d.ts +18 -0
- package/dist/llm/openai/responses-serializer.js +72 -0
- package/dist/llm/openrouter/chat.d.ts +28 -2
- package/dist/llm/openrouter/chat.js +115 -29
- package/dist/llm/schema.d.ts +11 -1
- package/dist/llm/schema.js +81 -1
- package/dist/llm/vercel/chat.d.ts +50 -0
- package/dist/llm/vercel/chat.js +276 -0
- package/dist/llm/vercel/index.d.ts +1 -0
- package/dist/llm/vercel/index.js +1 -0
- package/dist/llm/vercel/serializer.d.ts +5 -0
- package/dist/llm/vercel/serializer.js +7 -0
- package/dist/llm/views.d.ts +2 -1
- package/dist/llm/views.js +3 -1
- package/dist/logging-config.d.ts +2 -0
- package/dist/logging-config.js +82 -29
- package/dist/mcp/client.d.ts +10 -5
- package/dist/mcp/client.js +14 -9
- package/dist/mcp/controller.d.ts +42 -3
- package/dist/mcp/controller.js +56 -31
- package/dist/mcp/server.d.ts +14 -0
- package/dist/mcp/server.js +255 -52
- package/dist/observability.js +10 -4
- package/dist/sandbox/index.d.ts +2 -0
- package/dist/sandbox/index.js +2 -0
- package/dist/sandbox/sandbox.d.ts +19 -0
- package/dist/sandbox/sandbox.js +140 -0
- package/dist/sandbox/views.d.ts +67 -0
- package/dist/sandbox/views.js +121 -0
- package/dist/skill-cli/index.d.ts +3 -0
- package/dist/skill-cli/index.js +3 -0
- package/dist/skill-cli/protocol.d.ts +30 -0
- package/dist/skill-cli/protocol.js +48 -0
- package/dist/skill-cli/server.d.ts +11 -0
- package/dist/skill-cli/server.js +85 -0
- package/dist/skill-cli/sessions.d.ts +24 -0
- package/dist/skill-cli/sessions.js +47 -0
- package/dist/skills/index.d.ts +3 -0
- package/dist/skills/index.js +3 -0
- package/dist/skills/service.d.ts +27 -0
- package/dist/skills/service.js +266 -0
- package/dist/skills/utils.d.ts +6 -0
- package/dist/skills/utils.js +53 -0
- package/dist/skills/views.d.ts +40 -0
- package/dist/skills/views.js +10 -0
- package/dist/sync/auth.js +8 -3
- package/dist/sync/service.d.ts +6 -6
- package/dist/sync/service.js +54 -89
- package/dist/telemetry/views.d.ts +20 -6
- package/dist/telemetry/views.js +23 -5
- package/dist/tokens/custom-pricing.d.ts +2 -0
- package/dist/tokens/custom-pricing.js +22 -0
- package/dist/tokens/index.d.ts +2 -0
- package/dist/tokens/index.js +2 -0
- package/dist/tokens/mappings.d.ts +1 -0
- package/dist/tokens/mappings.js +3 -0
- package/dist/tokens/service.js +27 -8
- package/dist/tools/extraction/index.d.ts +2 -0
- package/dist/tools/extraction/index.js +2 -0
- package/dist/tools/extraction/schema-utils.d.ts +6 -0
- package/dist/tools/extraction/schema-utils.js +237 -0
- package/dist/tools/extraction/views.d.ts +7 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.js +5 -0
- package/dist/tools/registry/index.d.ts +2 -0
- package/dist/tools/registry/index.js +2 -0
- package/dist/tools/registry/service.d.ts +1 -0
- package/dist/tools/registry/service.js +1 -0
- package/dist/tools/registry/views.d.ts +1 -0
- package/dist/tools/registry/views.js +1 -0
- package/dist/tools/service.d.ts +2 -0
- package/dist/tools/service.js +1 -0
- package/dist/tools/utils.d.ts +2 -0
- package/dist/tools/utils.js +57 -0
- package/dist/tools/views.d.ts +1 -0
- package/dist/tools/views.js +1 -0
- package/dist/utils.d.ts +10 -1
- package/dist/utils.js +70 -3
- package/package.json +87 -26
- package/dist/dom/playground/process-dom.js +0 -5
- package/dist/dom/playground/test-accessibility.d.ts +0 -44
- package/dist/dom/playground/test-accessibility.js +0 -111
- /package/dist/{dom/playground/process-dom.d.ts → tools/extraction/views.js} +0 -0
package/dist/event-bus.d.ts
CHANGED
|
@@ -1,11 +1,111 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface EventPayload {
|
|
2
|
+
event_type?: string;
|
|
3
|
+
event_id?: string;
|
|
4
|
+
event_parent_id?: string | null;
|
|
5
|
+
event_timeout?: number | null;
|
|
6
|
+
event_created_at?: Date;
|
|
7
|
+
event_result?: unknown;
|
|
8
|
+
event_error?: unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface EventBusEventInit<TResult = unknown> {
|
|
11
|
+
event_id?: string;
|
|
12
|
+
event_parent_id?: string | null;
|
|
13
|
+
event_timeout?: number | null;
|
|
14
|
+
event_created_at?: Date;
|
|
15
|
+
event_result?: TResult | null;
|
|
16
|
+
event_error?: unknown;
|
|
17
|
+
}
|
|
18
|
+
export declare class EventBusEvent<TResult = unknown> implements EventPayload {
|
|
19
|
+
event_type: string;
|
|
20
|
+
event_id: string;
|
|
21
|
+
event_parent_id: string | null;
|
|
22
|
+
event_timeout: number | null;
|
|
23
|
+
event_created_at: Date;
|
|
24
|
+
event_result: TResult | null;
|
|
25
|
+
event_error: unknown;
|
|
26
|
+
constructor(event_type: string, init?: EventBusEventInit<TResult>);
|
|
27
|
+
}
|
|
28
|
+
export type EventTypeReference<TEvent extends EventPayload = EventPayload> = string | (new (...args: any[]) => TEvent);
|
|
29
|
+
export type EventHandler<TEvent extends EventPayload = EventPayload, TResult = unknown> = (event: TEvent) => TResult | Promise<TResult>;
|
|
30
|
+
export interface EventSubscriptionOptions {
|
|
31
|
+
once?: boolean;
|
|
32
|
+
handler_id?: string;
|
|
33
|
+
allow_duplicate?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export interface EventDispatchOptions {
|
|
36
|
+
throw_on_error?: boolean;
|
|
37
|
+
timeout_ms?: number | null;
|
|
38
|
+
parallel_handlers?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export interface EventBusOptions {
|
|
41
|
+
event_history_limit?: number;
|
|
42
|
+
throw_on_error_by_default?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface EventHandlerExecutionResult {
|
|
45
|
+
handler_id: string;
|
|
46
|
+
event_type: string;
|
|
47
|
+
status: 'fulfilled' | 'rejected' | 'timed_out';
|
|
48
|
+
started_at: Date;
|
|
49
|
+
completed_at: Date;
|
|
50
|
+
duration_ms: number;
|
|
51
|
+
result?: unknown;
|
|
52
|
+
error?: unknown;
|
|
53
|
+
}
|
|
54
|
+
export interface EventDispatchResult<TEvent extends EventPayload = EventPayload> {
|
|
55
|
+
event: TEvent;
|
|
56
|
+
event_id: string;
|
|
57
|
+
event_type: string;
|
|
58
|
+
event_parent_id: string | null;
|
|
59
|
+
event_timeout: number | null;
|
|
60
|
+
started_at: Date;
|
|
61
|
+
completed_at: Date;
|
|
62
|
+
duration_ms: number;
|
|
63
|
+
status: 'pending' | 'fulfilled' | 'rejected' | 'timed_out';
|
|
64
|
+
handler_results: EventHandlerExecutionResult[];
|
|
65
|
+
errors: unknown[];
|
|
66
|
+
}
|
|
67
|
+
export declare class EventHandlerTimeoutError extends Error {
|
|
68
|
+
event_type: string;
|
|
69
|
+
handler_id: string;
|
|
70
|
+
timeout_ms: number;
|
|
71
|
+
constructor(event_type: string, handler_id: string, timeout_ms: number);
|
|
72
|
+
}
|
|
73
|
+
export declare class EventDispatchError extends Error {
|
|
74
|
+
dispatch_result: EventDispatchResult<EventPayload>;
|
|
75
|
+
constructor(dispatch_result: EventDispatchResult<EventPayload>);
|
|
76
|
+
}
|
|
77
|
+
interface EventHandlerRegistration {
|
|
78
|
+
event_type: string;
|
|
79
|
+
handler: EventHandler<any, any>;
|
|
80
|
+
handler_id: string;
|
|
81
|
+
once: boolean;
|
|
82
|
+
}
|
|
2
83
|
export declare class EventBus {
|
|
3
84
|
readonly name: string;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
85
|
+
readonly handlers: Map<string, EventHandlerRegistration[]>;
|
|
86
|
+
readonly event_history: Map<string, EventDispatchResult<EventPayload>>;
|
|
87
|
+
private readonly history_limit;
|
|
88
|
+
private readonly throw_on_error_by_default;
|
|
89
|
+
private readonly dispatch_context;
|
|
90
|
+
constructor(name: string, options?: EventBusOptions);
|
|
91
|
+
on<TEvent extends EventPayload = EventPayload, TResult = unknown>(event_type_ref: EventTypeReference<TEvent>, handler: EventHandler<TEvent, TResult>, options?: EventSubscriptionOptions): () => void;
|
|
92
|
+
once<TEvent extends EventPayload = EventPayload, TResult = unknown>(event_type_ref: EventTypeReference<TEvent>, handler: EventHandler<TEvent, TResult>, options?: Omit<EventSubscriptionOptions, 'once'>): () => void;
|
|
93
|
+
off<TEvent extends EventPayload = EventPayload>(event_type_ref: EventTypeReference<TEvent>, handler_or_id?: EventHandler<TEvent, unknown> | string): void;
|
|
94
|
+
dispatch<TEvent extends EventPayload>(event: TEvent, options?: EventDispatchOptions): Promise<EventDispatchResult<TEvent>>;
|
|
95
|
+
dispatch_or_throw<TEvent extends EventPayload>(event: TEvent, options?: Omit<EventDispatchOptions, 'throw_on_error'>): Promise<EventDispatchResult<TEvent>>;
|
|
96
|
+
getHandlers<TEvent extends EventPayload = EventPayload>(event_type_ref: EventTypeReference<TEvent>): EventHandlerRegistration[];
|
|
10
97
|
stop(): Promise<void>;
|
|
98
|
+
private resolveEventType;
|
|
99
|
+
private resolveEventTypeFromRef;
|
|
100
|
+
private resolveHandlerId;
|
|
101
|
+
private resolveEventId;
|
|
102
|
+
private resolveParentEventId;
|
|
103
|
+
private resolveTimeoutMs;
|
|
104
|
+
private assignEventMetadata;
|
|
105
|
+
private assignEventResult;
|
|
106
|
+
private assignEventError;
|
|
107
|
+
private safeAssign;
|
|
108
|
+
private withTimeout;
|
|
109
|
+
private pruneHistory;
|
|
11
110
|
}
|
|
111
|
+
export {};
|
package/dist/event-bus.js
CHANGED
|
@@ -1,19 +1,322 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
export class EventBusEvent {
|
|
4
|
+
event_type;
|
|
5
|
+
event_id;
|
|
6
|
+
event_parent_id;
|
|
7
|
+
event_timeout;
|
|
8
|
+
event_created_at;
|
|
9
|
+
event_result;
|
|
10
|
+
event_error;
|
|
11
|
+
constructor(event_type, init = {}) {
|
|
12
|
+
this.event_type = event_type;
|
|
13
|
+
this.event_id = init.event_id ?? randomUUID();
|
|
14
|
+
this.event_parent_id = init.event_parent_id ?? null;
|
|
15
|
+
this.event_timeout = init.event_timeout ?? null;
|
|
16
|
+
this.event_created_at = init.event_created_at ?? new Date();
|
|
17
|
+
this.event_result = init.event_result ?? null;
|
|
18
|
+
this.event_error = init.event_error ?? null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class EventHandlerTimeoutError extends Error {
|
|
22
|
+
event_type;
|
|
23
|
+
handler_id;
|
|
24
|
+
timeout_ms;
|
|
25
|
+
constructor(event_type, handler_id, timeout_ms) {
|
|
26
|
+
super(`Handler ${handler_id} timed out after ${timeout_ms}ms for ${event_type}`);
|
|
27
|
+
this.name = 'EventHandlerTimeoutError';
|
|
28
|
+
this.event_type = event_type;
|
|
29
|
+
this.handler_id = handler_id;
|
|
30
|
+
this.timeout_ms = timeout_ms;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class EventDispatchError extends Error {
|
|
34
|
+
dispatch_result;
|
|
35
|
+
constructor(dispatch_result) {
|
|
36
|
+
super(`Event ${dispatch_result.event_type}#${dispatch_result.event_id} failed with ${dispatch_result.errors.length} error(s)`);
|
|
37
|
+
this.name = 'EventDispatchError';
|
|
38
|
+
this.dispatch_result = dispatch_result;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
2
41
|
export class EventBus {
|
|
3
42
|
name;
|
|
4
|
-
|
|
5
|
-
|
|
43
|
+
handlers = new Map();
|
|
44
|
+
event_history = new Map();
|
|
45
|
+
history_limit;
|
|
46
|
+
throw_on_error_by_default;
|
|
47
|
+
dispatch_context = new AsyncLocalStorage();
|
|
48
|
+
constructor(name, options = {}) {
|
|
6
49
|
this.name = name;
|
|
50
|
+
this.history_limit = options.event_history_limit ?? 500;
|
|
51
|
+
this.throw_on_error_by_default = options.throw_on_error_by_default ?? false;
|
|
7
52
|
}
|
|
8
|
-
on(
|
|
9
|
-
this.
|
|
53
|
+
on(event_type_ref, handler, options = {}) {
|
|
54
|
+
const event_type = this.resolveEventTypeFromRef(event_type_ref);
|
|
55
|
+
const handler_id = options.handler_id ??
|
|
56
|
+
this.resolveHandlerId(event_type, handler);
|
|
57
|
+
const registrations = this.handlers.get(event_type) ?? [];
|
|
58
|
+
if (!options.allow_duplicate) {
|
|
59
|
+
const hasDuplicate = registrations.some((existing) => existing.handler === handler || existing.handler_id === handler_id);
|
|
60
|
+
if (hasDuplicate) {
|
|
61
|
+
throw new Error(`Duplicate handler registration for ${event_type} (${handler_id})`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const registration = {
|
|
65
|
+
event_type,
|
|
66
|
+
handler: handler,
|
|
67
|
+
handler_id,
|
|
68
|
+
once: options.once ?? false,
|
|
69
|
+
};
|
|
70
|
+
registrations.push(registration);
|
|
71
|
+
this.handlers.set(event_type, registrations);
|
|
72
|
+
return () => {
|
|
73
|
+
this.off(event_type_ref, handler_id);
|
|
74
|
+
};
|
|
10
75
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
76
|
+
once(event_type_ref, handler, options = {}) {
|
|
77
|
+
return this.on(event_type_ref, handler, { ...options, once: true });
|
|
78
|
+
}
|
|
79
|
+
off(event_type_ref, handler_or_id) {
|
|
80
|
+
const event_type = this.resolveEventTypeFromRef(event_type_ref);
|
|
81
|
+
const registrations = this.handlers.get(event_type);
|
|
82
|
+
if (!registrations || registrations.length === 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (!handler_or_id) {
|
|
86
|
+
this.handlers.delete(event_type);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const next = registrations.filter((entry) => {
|
|
90
|
+
if (typeof handler_or_id === 'string') {
|
|
91
|
+
return entry.handler_id !== handler_or_id;
|
|
92
|
+
}
|
|
93
|
+
return entry.handler !== handler_or_id;
|
|
94
|
+
});
|
|
95
|
+
if (next.length) {
|
|
96
|
+
this.handlers.set(event_type, next);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.handlers.delete(event_type);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async dispatch(event, options = {}) {
|
|
103
|
+
const event_type = this.resolveEventType(event) ?? this.resolveEventTypeFromRef('event');
|
|
104
|
+
const event_id = this.resolveEventId(event);
|
|
105
|
+
const event_parent_id = this.resolveParentEventId(event);
|
|
106
|
+
const timeout_ms = this.resolveTimeoutMs(event, options.timeout_ms);
|
|
107
|
+
const timeout_seconds = timeout_ms == null ? null : timeout_ms / 1000;
|
|
108
|
+
this.assignEventMetadata(event, {
|
|
109
|
+
event_type,
|
|
110
|
+
event_id,
|
|
111
|
+
event_parent_id,
|
|
112
|
+
event_timeout: timeout_seconds,
|
|
113
|
+
});
|
|
114
|
+
const started_at = new Date();
|
|
115
|
+
const registrations = [
|
|
116
|
+
...(this.handlers.get(event_type) ?? []),
|
|
117
|
+
...(event_type === '*' ? [] : (this.handlers.get('*') ?? [])),
|
|
118
|
+
];
|
|
119
|
+
const dispatch_result = {
|
|
120
|
+
event,
|
|
121
|
+
event_id,
|
|
122
|
+
event_type,
|
|
123
|
+
event_parent_id,
|
|
124
|
+
event_timeout: timeout_seconds,
|
|
125
|
+
started_at,
|
|
126
|
+
completed_at: started_at,
|
|
127
|
+
duration_ms: 0,
|
|
128
|
+
status: 'pending',
|
|
129
|
+
handler_results: [],
|
|
130
|
+
errors: [],
|
|
131
|
+
};
|
|
132
|
+
this.event_history.set(event_id, dispatch_result);
|
|
133
|
+
this.pruneHistory();
|
|
134
|
+
const runHandler = async (registration) => {
|
|
135
|
+
const handler_started_at = new Date();
|
|
136
|
+
const safeTimeoutMs = timeout_ms ?? undefined;
|
|
137
|
+
let handler_status = 'fulfilled';
|
|
138
|
+
let handler_result;
|
|
139
|
+
let handler_error;
|
|
140
|
+
try {
|
|
141
|
+
const execution = this.dispatch_context.run({ event_id }, () => Promise.resolve(registration.handler(event)));
|
|
142
|
+
handler_result =
|
|
143
|
+
safeTimeoutMs == null
|
|
144
|
+
? await execution
|
|
145
|
+
: await this.withTimeout(execution, safeTimeoutMs, event_type, registration.handler_id);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
handler_error = error;
|
|
149
|
+
handler_status =
|
|
150
|
+
error instanceof EventHandlerTimeoutError ? 'timed_out' : 'rejected';
|
|
151
|
+
dispatch_result.errors.push(error);
|
|
152
|
+
}
|
|
153
|
+
const handler_completed_at = new Date();
|
|
154
|
+
const handler_execution_result = {
|
|
155
|
+
handler_id: registration.handler_id,
|
|
156
|
+
event_type: registration.event_type,
|
|
157
|
+
status: handler_status,
|
|
158
|
+
started_at: handler_started_at,
|
|
159
|
+
completed_at: handler_completed_at,
|
|
160
|
+
duration_ms: handler_completed_at.getTime() - handler_started_at.getTime(),
|
|
161
|
+
};
|
|
162
|
+
if (handler_result !== undefined) {
|
|
163
|
+
handler_execution_result.result = handler_result;
|
|
164
|
+
}
|
|
165
|
+
if (handler_error !== undefined) {
|
|
166
|
+
handler_execution_result.error = handler_error;
|
|
167
|
+
}
|
|
168
|
+
dispatch_result.handler_results.push(handler_execution_result);
|
|
169
|
+
if (registration.once) {
|
|
170
|
+
this.off(registration.event_type, registration.handler_id);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
if (options.parallel_handlers) {
|
|
174
|
+
await Promise.all(registrations.map((registration) => runHandler(registration)));
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
for (const registration of registrations) {
|
|
178
|
+
await runHandler(registration);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const completed_at = new Date();
|
|
182
|
+
dispatch_result.completed_at = completed_at;
|
|
183
|
+
dispatch_result.duration_ms = completed_at.getTime() - started_at.getTime();
|
|
184
|
+
if (dispatch_result.errors.length > 0) {
|
|
185
|
+
const hasTimeout = dispatch_result.handler_results.some((result) => result.status === 'timed_out');
|
|
186
|
+
dispatch_result.status = hasTimeout ? 'timed_out' : 'rejected';
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
dispatch_result.status = 'fulfilled';
|
|
190
|
+
}
|
|
191
|
+
this.assignEventResult(event, dispatch_result.handler_results);
|
|
192
|
+
if (dispatch_result.errors.length > 0) {
|
|
193
|
+
this.assignEventError(event, dispatch_result.errors[0] ?? null);
|
|
194
|
+
}
|
|
195
|
+
const throw_on_error = options.throw_on_error ?? this.throw_on_error_by_default;
|
|
196
|
+
if (throw_on_error && dispatch_result.errors.length > 0) {
|
|
197
|
+
throw new EventDispatchError(dispatch_result);
|
|
198
|
+
}
|
|
199
|
+
return dispatch_result;
|
|
200
|
+
}
|
|
201
|
+
async dispatch_or_throw(event, options = {}) {
|
|
202
|
+
return this.dispatch(event, { ...options, throw_on_error: true });
|
|
203
|
+
}
|
|
204
|
+
getHandlers(event_type_ref) {
|
|
205
|
+
const event_type = this.resolveEventTypeFromRef(event_type_ref);
|
|
206
|
+
return [...(this.handlers.get(event_type) ?? [])];
|
|
15
207
|
}
|
|
16
208
|
async stop() {
|
|
17
|
-
this.
|
|
209
|
+
this.handlers.clear();
|
|
210
|
+
this.event_history.clear();
|
|
211
|
+
}
|
|
212
|
+
resolveEventType(event) {
|
|
213
|
+
const event_type = event.event_type ??
|
|
214
|
+
event.constructor?.name ??
|
|
215
|
+
null;
|
|
216
|
+
return event_type && event_type.length > 0 ? event_type : null;
|
|
217
|
+
}
|
|
218
|
+
resolveEventTypeFromRef(event_type_ref) {
|
|
219
|
+
if (typeof event_type_ref === 'string') {
|
|
220
|
+
return event_type_ref;
|
|
221
|
+
}
|
|
222
|
+
return event_type_ref.name;
|
|
223
|
+
}
|
|
224
|
+
resolveHandlerId(event_type, handler) {
|
|
225
|
+
const suffix = typeof handler.name === 'string' && handler.name.length > 0
|
|
226
|
+
? handler.name
|
|
227
|
+
: `handler_${randomUUID().slice(0, 8)}`;
|
|
228
|
+
return `${event_type}:${suffix}`;
|
|
229
|
+
}
|
|
230
|
+
resolveEventId(event) {
|
|
231
|
+
if (event.event_id && event.event_id.length > 0) {
|
|
232
|
+
return event.event_id;
|
|
233
|
+
}
|
|
234
|
+
return randomUUID();
|
|
235
|
+
}
|
|
236
|
+
resolveParentEventId(event) {
|
|
237
|
+
if (typeof event.event_parent_id === 'string' &&
|
|
238
|
+
event.event_parent_id.length > 0) {
|
|
239
|
+
return event.event_parent_id;
|
|
240
|
+
}
|
|
241
|
+
return this.dispatch_context.getStore()?.event_id ?? null;
|
|
242
|
+
}
|
|
243
|
+
resolveTimeoutMs(event, dispatch_timeout_ms) {
|
|
244
|
+
if (dispatch_timeout_ms !== undefined) {
|
|
245
|
+
return dispatch_timeout_ms;
|
|
246
|
+
}
|
|
247
|
+
if (event.event_timeout == null) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
if (!Number.isFinite(event.event_timeout)) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
if (event.event_timeout < 0) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
return event.event_timeout * 1000;
|
|
257
|
+
}
|
|
258
|
+
assignEventMetadata(event, metadata) {
|
|
259
|
+
this.safeAssign(event, 'event_type', metadata.event_type);
|
|
260
|
+
this.safeAssign(event, 'event_id', metadata.event_id);
|
|
261
|
+
this.safeAssign(event, 'event_parent_id', metadata.event_parent_id);
|
|
262
|
+
if (event.event_timeout === undefined) {
|
|
263
|
+
this.safeAssign(event, 'event_timeout', metadata.event_timeout);
|
|
264
|
+
}
|
|
265
|
+
if (event.event_created_at === undefined) {
|
|
266
|
+
this.safeAssign(event, 'event_created_at', new Date());
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
assignEventResult(event, handler_results) {
|
|
270
|
+
const first_defined_result = handler_results.find((result) => result.status === 'fulfilled' &&
|
|
271
|
+
Object.prototype.hasOwnProperty.call(result, 'result'));
|
|
272
|
+
if (!first_defined_result) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.safeAssign(event, 'event_result', first_defined_result.result);
|
|
276
|
+
}
|
|
277
|
+
assignEventError(event, error) {
|
|
278
|
+
this.safeAssign(event, 'event_error', error);
|
|
279
|
+
}
|
|
280
|
+
safeAssign(event, key, value) {
|
|
281
|
+
try {
|
|
282
|
+
event[key] = value;
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
// Read-only event objects should still be dispatchable.
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async withTimeout(promise, timeout_ms, event_type, handler_id) {
|
|
289
|
+
if (timeout_ms <= 0) {
|
|
290
|
+
throw new EventHandlerTimeoutError(event_type, handler_id, timeout_ms);
|
|
291
|
+
}
|
|
292
|
+
let timeout_handle = null;
|
|
293
|
+
try {
|
|
294
|
+
return await Promise.race([
|
|
295
|
+
promise,
|
|
296
|
+
new Promise((_, reject) => {
|
|
297
|
+
timeout_handle = setTimeout(() => {
|
|
298
|
+
reject(new EventHandlerTimeoutError(event_type, handler_id, timeout_ms));
|
|
299
|
+
}, timeout_ms);
|
|
300
|
+
}),
|
|
301
|
+
]);
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
if (timeout_handle) {
|
|
305
|
+
clearTimeout(timeout_handle);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
pruneHistory() {
|
|
310
|
+
if (this.history_limit <= 0) {
|
|
311
|
+
this.event_history.clear();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
while (this.event_history.size > this.history_limit) {
|
|
315
|
+
const firstKey = this.event_history.keys().next().value;
|
|
316
|
+
if (!firstKey) {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
this.event_history.delete(firstKey);
|
|
320
|
+
}
|
|
18
321
|
}
|
|
19
322
|
}
|
package/dist/exceptions.d.ts
CHANGED
package/dist/exceptions.js
CHANGED
|
@@ -20,10 +20,3 @@ export class URLNotAllowedError extends Error {
|
|
|
20
20
|
this.name = 'URLNotAllowedError';
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
export class InsecureSensitiveDataError extends Error {
|
|
24
|
-
constructor() {
|
|
25
|
-
super('Sensitive data requires BrowserProfile.allowed_domains to be configured. ' +
|
|
26
|
-
'Set allowed_domains explicitly or pass allow_insecure_sensitive_data=true.');
|
|
27
|
-
this.name = 'InsecureSensitiveDataError';
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
export declare function extractPdfText(buffer: Buffer): Promise<{
|
|
2
|
+
text: string;
|
|
3
|
+
totalPages: number;
|
|
4
|
+
}>;
|
|
5
|
+
export declare function extractPdfTextByPage(buffer: Buffer): Promise<{
|
|
6
|
+
numPages: number;
|
|
7
|
+
pageTexts: string[];
|
|
8
|
+
totalChars: number;
|
|
9
|
+
}>;
|
|
1
10
|
export declare const INVALID_FILENAME_ERROR_MESSAGE = "Error: Invalid filename format. Must be alphanumeric with supported extension.";
|
|
2
11
|
export declare const DEFAULT_FILE_SYSTEM_PATH = "browseruse_agent_data";
|
|
3
12
|
export declare class FileSystemError extends Error {
|
|
@@ -45,6 +54,8 @@ export declare class FileSystem {
|
|
|
45
54
|
constructor(baseDir: string, createDefaultFiles?: boolean);
|
|
46
55
|
private createDefaultFiles;
|
|
47
56
|
private isValidFilename;
|
|
57
|
+
static sanitize_filename(fileName: string): string;
|
|
58
|
+
private resolveFilename;
|
|
48
59
|
private parseFilename;
|
|
49
60
|
private getFileClass;
|
|
50
61
|
private instantiateFile;
|
|
@@ -53,6 +64,13 @@ export declare class FileSystem {
|
|
|
53
64
|
get_file(filename: string): BaseFile | null;
|
|
54
65
|
list_files(): string[];
|
|
55
66
|
display_file(filename: string): string | null;
|
|
67
|
+
read_file_structured(filename: string, externalFile?: boolean): Promise<{
|
|
68
|
+
message: string;
|
|
69
|
+
images: Array<{
|
|
70
|
+
name: string;
|
|
71
|
+
data: string;
|
|
72
|
+
}> | null;
|
|
73
|
+
}>;
|
|
56
74
|
read_file(filename: string, externalFile?: boolean): Promise<string>;
|
|
57
75
|
write_file(filename: string, content: string): Promise<string>;
|
|
58
76
|
append_file(filename: string, content: string): Promise<string>;
|