@browserbasehq/orca 3.0.0-preview.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.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +165 -0
  3. package/dist/index.d.ts +1611 -0
  4. package/dist/index.js +28681 -0
  5. package/dist/lib/api.d.ts +23 -0
  6. package/dist/lib/dom/build/scriptContent.d.ts +1 -0
  7. package/dist/lib/inference.d.ts +71 -0
  8. package/dist/lib/inferenceLogUtils.d.ts +12 -0
  9. package/dist/lib/logger.d.ts +54 -0
  10. package/dist/lib/prompt.d.ts +12 -0
  11. package/dist/lib/utils.d.ts +65 -0
  12. package/dist/lib/v3/agent/AgentClient.d.ts +18 -0
  13. package/dist/lib/v3/agent/AgentProvider.d.ts +18 -0
  14. package/dist/lib/v3/agent/AnthropicCUAClient.d.ts +55 -0
  15. package/dist/lib/v3/agent/OpenAICUAClient.d.ts +64 -0
  16. package/dist/lib/v3/agent/StagehandAgent.d.ts +15 -0
  17. package/dist/lib/v3/agent/tools/index.d.ts +229 -0
  18. package/dist/lib/v3/agent/tools/v3-act.d.ts +29 -0
  19. package/dist/lib/v3/agent/tools/v3-ariaTree.d.ts +11 -0
  20. package/dist/lib/v3/agent/tools/v3-close.d.ts +24 -0
  21. package/dist/lib/v3/agent/tools/v3-extract.d.ts +38 -0
  22. package/dist/lib/v3/agent/tools/v3-fillform.d.ts +37 -0
  23. package/dist/lib/v3/agent/tools/v3-goto.d.ts +29 -0
  24. package/dist/lib/v3/agent/tools/v3-navback.d.ts +17 -0
  25. package/dist/lib/v3/agent/tools/v3-screenshot.d.ts +13 -0
  26. package/dist/lib/v3/agent/tools/v3-scroll.d.ts +23 -0
  27. package/dist/lib/v3/agent/tools/v3-wait.d.ts +19 -0
  28. package/dist/lib/v3/agent/utils/cuaKeyMapping.d.ts +10 -0
  29. package/dist/lib/v3/agent/utils/imageCompression.d.ts +18 -0
  30. package/dist/lib/v3/agent/utils/messageProcessing.d.ts +13 -0
  31. package/dist/lib/v3/dom/build/scriptV3Content.d.ts +1 -0
  32. package/dist/lib/v3/dom/genDomScripts.d.ts +1 -0
  33. package/dist/lib/v3/dom/index.d.ts +1 -0
  34. package/dist/lib/v3/dom/piercer.entry.d.ts +1 -0
  35. package/dist/lib/v3/dom/piercer.runtime.d.ts +25 -0
  36. package/dist/lib/v3/handlers/actHandler.d.ts +18 -0
  37. package/dist/lib/v3/handlers/extractHandler.d.ts +29 -0
  38. package/dist/lib/v3/handlers/handlerUtils/actHandlerUtils.d.ts +18 -0
  39. package/dist/lib/v3/handlers/observeHandler.d.ts +15 -0
  40. package/dist/lib/v3/handlers/v3AgentHandler.d.ts +17 -0
  41. package/dist/lib/v3/handlers/v3CuaAgentHandler.d.ts +26 -0
  42. package/dist/lib/v3/index.d.ts +10 -0
  43. package/dist/lib/v3/launch/browserbase.d.ts +8 -0
  44. package/dist/lib/v3/launch/local.d.ts +13 -0
  45. package/dist/lib/v3/llm/AnthropicClient.d.ts +16 -0
  46. package/dist/lib/v3/llm/CerebrasClient.d.ts +17 -0
  47. package/dist/lib/v3/llm/GoogleClient.d.ts +19 -0
  48. package/dist/lib/v3/llm/GroqClient.d.ts +17 -0
  49. package/dist/lib/v3/llm/LLMClient.d.ts +99 -0
  50. package/dist/lib/v3/llm/LLMProvider.d.ts +10 -0
  51. package/dist/lib/v3/llm/OpenAIClient.d.ts +15 -0
  52. package/dist/lib/v3/llm/aisdk.d.ts +15 -0
  53. package/dist/lib/v3/logger.d.ts +48 -0
  54. package/dist/lib/v3/mcp/connection.d.ts +11 -0
  55. package/dist/lib/v3/mcp/utils.d.ts +3 -0
  56. package/dist/lib/v3/tests/default-page-tracking.spec.d.ts +1 -0
  57. package/dist/lib/v3/tests/perform-understudy-method.spec.d.ts +1 -0
  58. package/dist/lib/v3/tests/shadow-iframe.spec.d.ts +1 -0
  59. package/dist/lib/v3/tests/timeouts.spec.d.ts +1 -0
  60. package/dist/lib/v3/tests/v3.config.d.ts +4 -0
  61. package/dist/lib/v3/tests/v3.playwright.config.d.ts +2 -0
  62. package/dist/lib/v3/tests/xpath-for-location-deep.spec.d.ts +1 -0
  63. package/dist/lib/v3/types/act.d.ts +10 -0
  64. package/dist/lib/v3/types/agent.d.ts +132 -0
  65. package/dist/lib/v3/types/api.d.ts +40 -0
  66. package/dist/lib/v3/types/cache.d.ts +71 -0
  67. package/dist/lib/v3/types/context.d.ts +2 -0
  68. package/dist/lib/v3/types/evals.d.ts +71 -0
  69. package/dist/lib/v3/types/evaluator.d.ts +40 -0
  70. package/dist/lib/v3/types/llm.d.ts +11 -0
  71. package/dist/lib/v3/types/log.d.ts +23 -0
  72. package/dist/lib/v3/types/model.d.ts +20 -0
  73. package/dist/lib/v3/types/playwright.d.ts +6 -0
  74. package/dist/lib/v3/types/stagehand.d.ts +113 -0
  75. package/dist/lib/v3/types/stagehandApiErrors.d.ts +18 -0
  76. package/dist/lib/v3/types/stagehandErrors.d.ts +104 -0
  77. package/dist/lib/v3/types.d.ts +176 -0
  78. package/dist/lib/v3/understudy/a11y/snapshot.d.ts +71 -0
  79. package/dist/lib/v3/understudy/cdp.d.ts +58 -0
  80. package/dist/lib/v3/understudy/context.d.ts +120 -0
  81. package/dist/lib/v3/understudy/deepLocator.d.ts +69 -0
  82. package/dist/lib/v3/understudy/executionContextRegistry.d.ts +15 -0
  83. package/dist/lib/v3/understudy/frame.d.ts +63 -0
  84. package/dist/lib/v3/understudy/frameLocator.d.ts +46 -0
  85. package/dist/lib/v3/understudy/frameRegistry.d.ts +100 -0
  86. package/dist/lib/v3/understudy/locator.d.ts +196 -0
  87. package/dist/lib/v3/understudy/page.d.ts +241 -0
  88. package/dist/lib/v3/understudy/piercer.d.ts +4 -0
  89. package/dist/lib/v3/v3.d.ts +156 -0
  90. package/dist/lib/version.d.ts +5 -0
  91. package/package.json +130 -0
@@ -0,0 +1,113 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/dist/esm/client";
2
+ import { ToolSet } from "ai";
3
+ import { z } from "zod/v3";
4
+ import { AgentProviderType } from "./agent";
5
+ import { ModelConfiguration } from "./model";
6
+ export interface ActOptions {
7
+ action: string;
8
+ model?: ModelConfiguration;
9
+ variables?: Record<string, string>;
10
+ domSettleTimeoutMs?: number;
11
+ timeoutMs?: number;
12
+ iframes?: boolean;
13
+ frameId?: string;
14
+ }
15
+ export interface ActResult {
16
+ success: boolean;
17
+ message: string;
18
+ actionDescription: string;
19
+ actions: Action[];
20
+ }
21
+ export interface ExtractOptions<T extends z.AnyZodObject> {
22
+ instruction?: string;
23
+ schema?: T;
24
+ model?: ModelConfiguration;
25
+ domSettleTimeoutMs?: number;
26
+ /**
27
+ * @deprecated The `useTextExtract` parameter has no effect in this version of Stagehand and will be removed in later versions.
28
+ */
29
+ useTextExtract?: boolean;
30
+ selector?: string;
31
+ iframes?: boolean;
32
+ frameId?: string;
33
+ }
34
+ export type ExtractResult<T extends z.AnyZodObject> = z.infer<T>;
35
+ export interface ObserveOptions {
36
+ instruction?: string;
37
+ model?: ModelConfiguration;
38
+ domSettleTimeoutMs?: number;
39
+ returnAction?: boolean;
40
+ selector?: string;
41
+ /**
42
+ * @deprecated The `onlyVisible` parameter has no effect in this version of Stagehand and will be removed in later versions.
43
+ */
44
+ onlyVisible?: boolean;
45
+ drawOverlay?: boolean;
46
+ iframes?: boolean;
47
+ frameId?: string;
48
+ }
49
+ export interface Action {
50
+ selector: string;
51
+ description: string;
52
+ backendNodeId?: number;
53
+ method?: string;
54
+ arguments?: string[];
55
+ }
56
+ /**
57
+ * Configuration for agent functionality
58
+ */
59
+ export interface AgentConfig {
60
+ /**
61
+ * The provider to use for agent functionality
62
+ */
63
+ provider?: AgentProviderType;
64
+ /**
65
+ * The model to use for agent functionality
66
+ */
67
+ model?: string;
68
+ /**
69
+ * The model to use for tool execution (observe/act calls within agent tools).
70
+ * If not specified, inherits from the main model configuration.
71
+ * Format: "provider/model" (e.g., "openai/gpt-4o-mini", "google/gemini-2.0-flash-exp")
72
+ */
73
+ executionModel?: string;
74
+ /**
75
+ * Custom instructions to provide to the agent
76
+ */
77
+ instructions?: string;
78
+ /**
79
+ * Additional options to pass to the agent client
80
+ */
81
+ options?: Record<string, unknown>;
82
+ /**
83
+ * MCP integrations - Array of Client objects
84
+ */
85
+ integrations?: (Client | string)[];
86
+ /**
87
+ * Tools passed to the agent client
88
+ */
89
+ tools?: ToolSet;
90
+ }
91
+ export interface HistoryEntry {
92
+ method: "act" | "extract" | "observe" | "navigate";
93
+ parameters: unknown;
94
+ result: unknown;
95
+ timestamp: string;
96
+ }
97
+ /**
98
+ * Represents a path through a Zod schema from the root object down to a
99
+ * particular field. The `segments` array describes the chain of keys/indices.
100
+ *
101
+ * - **String** segments indicate object property names.
102
+ * - **Number** segments indicate array indices.
103
+ *
104
+ * For example, `["users", 0, "homepage"]` might describe reaching
105
+ * the `homepage` field in `schema.users[0].homepage`.
106
+ */
107
+ export interface ZodPathSegments {
108
+ /**
109
+ * The ordered list of keys/indices leading from the schema root
110
+ * to the targeted field.
111
+ */
112
+ segments: Array<string | number>;
113
+ }
@@ -0,0 +1,18 @@
1
+ export declare class StagehandAPIError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class StagehandAPIUnauthorizedError extends StagehandAPIError {
5
+ constructor(message?: string);
6
+ }
7
+ export declare class StagehandHttpError extends StagehandAPIError {
8
+ constructor(message: string);
9
+ }
10
+ export declare class StagehandServerError extends StagehandAPIError {
11
+ constructor(message: string);
12
+ }
13
+ export declare class StagehandResponseBodyError extends StagehandAPIError {
14
+ constructor();
15
+ }
16
+ export declare class StagehandResponseParseError extends StagehandAPIError {
17
+ constructor(message: string);
18
+ }
@@ -0,0 +1,104 @@
1
+ import { ZodError } from "zod/v3";
2
+ export declare class StagehandError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ export declare class StagehandDefaultError extends StagehandError {
6
+ constructor(error?: unknown);
7
+ }
8
+ export declare class StagehandEnvironmentError extends StagehandError {
9
+ constructor(currentEnvironment: string, requiredEnvironment: string, feature: string);
10
+ }
11
+ export declare class MissingEnvironmentVariableError extends StagehandError {
12
+ constructor(missingEnvironmentVariable: string, feature: string);
13
+ }
14
+ export declare class UnsupportedModelError extends StagehandError {
15
+ constructor(supportedModels: string[], feature?: string);
16
+ }
17
+ export declare class UnsupportedModelProviderError extends StagehandError {
18
+ constructor(supportedProviders: string[], feature?: string);
19
+ }
20
+ export declare class UnsupportedAISDKModelProviderError extends StagehandError {
21
+ constructor(provider: string, supportedProviders: string[]);
22
+ }
23
+ export declare class InvalidAISDKModelFormatError extends StagehandError {
24
+ constructor(modelName: string);
25
+ }
26
+ export declare class StagehandNotInitializedError extends StagehandError {
27
+ constructor(prop: string);
28
+ }
29
+ export declare class BrowserbaseSessionNotFoundError extends StagehandError {
30
+ constructor();
31
+ }
32
+ export declare class CaptchaTimeoutError extends StagehandError {
33
+ constructor();
34
+ }
35
+ export declare class MissingLLMConfigurationError extends StagehandError {
36
+ constructor();
37
+ }
38
+ export declare class HandlerNotInitializedError extends StagehandError {
39
+ constructor(handlerType: string);
40
+ }
41
+ export declare class StagehandInvalidArgumentError extends StagehandError {
42
+ constructor(message: string);
43
+ }
44
+ export declare class StagehandElementNotFoundError extends StagehandError {
45
+ constructor(xpaths: string[]);
46
+ }
47
+ export declare class AgentScreenshotProviderError extends StagehandError {
48
+ constructor(message: string);
49
+ }
50
+ export declare class StagehandMissingArgumentError extends StagehandError {
51
+ constructor(message: string);
52
+ }
53
+ export declare class CreateChatCompletionResponseError extends StagehandError {
54
+ constructor(message: string);
55
+ }
56
+ export declare class StagehandEvalError extends StagehandError {
57
+ constructor(message: string);
58
+ }
59
+ export declare class StagehandDomProcessError extends StagehandError {
60
+ constructor(message: string);
61
+ }
62
+ export declare class StagehandClickError extends StagehandError {
63
+ constructor(message: string, selector: string);
64
+ }
65
+ export declare class LLMResponseError extends StagehandError {
66
+ constructor(primitive: string, message: string);
67
+ }
68
+ export declare class StagehandIframeError extends StagehandError {
69
+ constructor(frameUrl: string, message: string);
70
+ }
71
+ export declare class ContentFrameNotFoundError extends StagehandError {
72
+ constructor(selector: string);
73
+ }
74
+ export declare class XPathResolutionError extends StagehandError {
75
+ constructor(xpath: string);
76
+ }
77
+ export declare class ExperimentalApiConflictError extends StagehandError {
78
+ constructor();
79
+ }
80
+ export declare class ExperimentalNotConfiguredError extends StagehandError {
81
+ constructor(featureName: string);
82
+ }
83
+ export declare class ZodSchemaValidationError extends Error {
84
+ readonly received: unknown;
85
+ readonly issues: ReturnType<ZodError["format"]>;
86
+ constructor(received: unknown, issues: ReturnType<ZodError["format"]>);
87
+ }
88
+ export declare class StagehandInitError extends StagehandError {
89
+ constructor(message: string);
90
+ }
91
+ export declare class MCPConnectionError extends StagehandError {
92
+ readonly serverUrl: string;
93
+ readonly originalError: unknown;
94
+ constructor(serverUrl: string, originalError: unknown);
95
+ }
96
+ export declare class StagehandShadowRootMissingError extends StagehandError {
97
+ constructor(detail?: string);
98
+ }
99
+ export declare class StagehandShadowSegmentEmptyError extends StagehandError {
100
+ constructor();
101
+ }
102
+ export declare class StagehandShadowSegmentNotFoundError extends StagehandError {
103
+ constructor(segment: string, hint?: string);
104
+ }
@@ -0,0 +1,176 @@
1
+ import { LaunchedChrome } from "chrome-launcher";
2
+ import Browserbase from "@browserbasehq/sdk";
3
+ import { Page } from "./understudy/page";
4
+ import { ModelConfiguration } from "../v3/types/model";
5
+ import { LLMClient } from "./llm/LLMClient";
6
+ import { z } from "zod/v3";
7
+ import type { ZodTypeAny } from "zod/v3";
8
+ import type { LogLine } from "../v3/types/log";
9
+ export type V3Env = "LOCAL" | "BROWSERBASE";
10
+ /** Local launch options for V3 (chrome-launcher + CDP).
11
+ * Matches v2 shape where feasible; unsupported fields are accepted but ignored.
12
+ */
13
+ export interface LocalBrowserLaunchOptions {
14
+ args?: string[];
15
+ executablePath?: string;
16
+ userDataDir?: string;
17
+ preserveUserDataDir?: boolean;
18
+ headless?: boolean;
19
+ devtools?: boolean;
20
+ chromiumSandbox?: boolean;
21
+ ignoreDefaultArgs?: boolean | string[];
22
+ proxy?: {
23
+ server: string;
24
+ bypass?: string;
25
+ username?: string;
26
+ password?: string;
27
+ };
28
+ locale?: string;
29
+ viewport?: {
30
+ width: number;
31
+ height: number;
32
+ };
33
+ deviceScaleFactor?: number;
34
+ hasTouch?: boolean;
35
+ ignoreHTTPSErrors?: boolean;
36
+ cdpUrl?: string;
37
+ connectTimeoutMs?: number;
38
+ downloadsPath?: string;
39
+ acceptDownloads?: boolean;
40
+ }
41
+ /** Constructor options for V3 */
42
+ export interface V3Options {
43
+ env: V3Env;
44
+ apiKey?: string;
45
+ projectId?: string;
46
+ /**
47
+ * Optional: fine-tune Browserbase session creation or resume an existing session.
48
+ */
49
+ browserbaseSessionCreateParams?: Omit<Browserbase.Sessions.SessionCreateParams, "projectId"> & {
50
+ projectId?: string;
51
+ };
52
+ browserbaseSessionID?: string;
53
+ localBrowserLaunchOptions?: LocalBrowserLaunchOptions;
54
+ model?: ModelConfiguration;
55
+ llmClient?: LLMClient;
56
+ systemPrompt?: string;
57
+ logInferenceToFile?: boolean;
58
+ experimental?: boolean;
59
+ verbose?: 0 | 1 | 2;
60
+ selfHeal?: boolean;
61
+ /** Disable pino logging backend (useful for tests or minimal environments). */
62
+ disablePino?: boolean;
63
+ /** Optional external logger hook for integrating with host apps. */
64
+ logger?: (line: LogLine) => void;
65
+ /** Show a visual cursor overlay that follows our mouse events. */
66
+ includeCursor?: boolean;
67
+ /** Directory used to persist cached actions for act(). */
68
+ cacheDir?: string;
69
+ domSettleTimeout?: number;
70
+ }
71
+ /** Narrow shape we rely on from Browserbase session creation */
72
+ export interface BrowserbaseSession {
73
+ id: string;
74
+ connectUrl: string;
75
+ }
76
+ /** Narrow shape we rely on from /json/version */
77
+ export interface JsonVersionResponse {
78
+ webSocketDebuggerUrl: string;
79
+ }
80
+ export type InitState = {
81
+ kind: "UNINITIALIZED";
82
+ } | {
83
+ kind: "LOCAL";
84
+ chrome: LaunchedChrome;
85
+ ws: string;
86
+ userDataDir?: string;
87
+ createdTempProfile?: boolean;
88
+ preserveUserDataDir?: boolean;
89
+ } | {
90
+ kind: "BROWSERBASE";
91
+ bb: Browserbase;
92
+ sessionId: string;
93
+ ws: string;
94
+ };
95
+ export type PlaywrightPage = import("playwright-core").Page;
96
+ export type PatchrightPage = import("patchright-core").Page;
97
+ export type PuppeteerPage = import("puppeteer-core").Page;
98
+ export type AnyPage = PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
99
+ export interface ActOptions {
100
+ model?: ModelConfiguration;
101
+ variables?: Record<string, string>;
102
+ timeout?: number;
103
+ page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
104
+ }
105
+ export interface ActHandlerParams {
106
+ instruction: string;
107
+ model?: ModelConfiguration;
108
+ variables?: Record<string, string>;
109
+ timeout?: number;
110
+ page: Page;
111
+ }
112
+ export interface ExtractOptions {
113
+ model?: ModelConfiguration;
114
+ timeout?: number;
115
+ selector?: string;
116
+ page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
117
+ }
118
+ export interface ExtractHandlerParams<T extends ZodTypeAny> {
119
+ instruction?: string;
120
+ schema?: T;
121
+ model?: ModelConfiguration;
122
+ timeout?: number;
123
+ selector?: string;
124
+ page: Page;
125
+ }
126
+ export declare const defaultExtractSchema: z.ZodObject<{
127
+ extraction: z.ZodString;
128
+ }, "strip", z.ZodTypeAny, {
129
+ extraction?: string;
130
+ }, {
131
+ extraction?: string;
132
+ }>;
133
+ export declare const pageTextSchema: z.ZodObject<{
134
+ pageText: z.ZodString;
135
+ }, "strip", z.ZodTypeAny, {
136
+ pageText?: string;
137
+ }, {
138
+ pageText?: string;
139
+ }>;
140
+ export interface ObserveOptions {
141
+ model?: ModelConfiguration;
142
+ timeout?: number;
143
+ selector?: string;
144
+ page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;
145
+ }
146
+ export interface ObserveHandlerParams {
147
+ instruction?: string;
148
+ model?: ModelConfiguration;
149
+ timeout?: number;
150
+ selector?: string;
151
+ page: Page;
152
+ }
153
+ export type LoadState = "load" | "domcontentloaded" | "networkidle";
154
+ export interface V3Metrics {
155
+ actPromptTokens: number;
156
+ actCompletionTokens: number;
157
+ actInferenceTimeMs: number;
158
+ extractPromptTokens: number;
159
+ extractCompletionTokens: number;
160
+ extractInferenceTimeMs: number;
161
+ observePromptTokens: number;
162
+ observeCompletionTokens: number;
163
+ observeInferenceTimeMs: number;
164
+ agentPromptTokens: number;
165
+ agentCompletionTokens: number;
166
+ agentInferenceTimeMs: number;
167
+ totalPromptTokens: number;
168
+ totalCompletionTokens: number;
169
+ totalInferenceTimeMs: number;
170
+ }
171
+ export declare enum V3FunctionName {
172
+ ACT = "ACT",
173
+ EXTRACT = "EXTRACT",
174
+ OBSERVE = "OBSERVE",
175
+ AGENT = "AGENT"
176
+ }
@@ -0,0 +1,71 @@
1
+ import { Page } from "../page";
2
+ /**
3
+ * a11y/snapshot
4
+ *
5
+ * Purpose:
6
+ * Build a **hybrid DOM + Accessibility** snapshot for a V3 `Page`, suitable for
7
+ * act/extract/observe handlers. The snapshot includes:
8
+ * - A merged, human-readable outline (Accessibility tree) for the page
9
+ * - EncodedId → XPath map (DOM)
10
+ * - EncodedId → URL map (from AX properties)
11
+ *
12
+ * Design highlights:
13
+ * - EncodedId is frame-aware and compact: `${frameOrdinal}-${backendNodeId}` where
14
+ * the frame ordinal is provided by `Page.getOrdinal(frameId)`.
15
+ * - Each frame (main, same-process iframe, OOPIF) is processed against **its owning session**,
16
+ * so DOM and A11y ownership are correct for that document.
17
+ * - We compute an **absolute iframe XPath prefix** for each child frame by asking
18
+ * the **parent session** for the `<iframe>` owner node (via `DOM.getFrameOwner`).
19
+ * - No global inventory imports; we rely solely on Page’s registry-backed APIs.
20
+ */
21
+ export type SnapshotOptions = {
22
+ /**
23
+ * Filter the snapshot to a specific element/subtree using a selector that can cross iframes.
24
+ * Supports:
25
+ * - XPath: strings starting with 'xpath=' or '/'
26
+ * - CSS with iframe hops via '>>': e.g., 'section iframe >> #email'
27
+ */
28
+ focusSelector?: string;
29
+ /** Pierce shadow DOM in DOM.getDocument (default: true). */
30
+ pierceShadow?: boolean;
31
+ /** Experimental behaviours flag. */
32
+ experimental?: boolean;
33
+ };
34
+ export type HybridSnapshot = {
35
+ /** Merged/stitched outline across frames. */
36
+ combinedTree: string;
37
+ /** EncodedId (ordinal-backendId) → absolute XPath (across iframes). */
38
+ combinedXpathMap: Record<string, string>;
39
+ /** EncodedId → URL (from AX properties). */
40
+ combinedUrlMap: Record<string, string>;
41
+ /** Per-frame details for debugging/consumers. */
42
+ perFrame?: Array<{
43
+ frameId: string;
44
+ outline: string;
45
+ xpathMap: Record<string, string>;
46
+ urlMap: Record<string, string>;
47
+ }>;
48
+ };
49
+ /**
50
+ * Resolve deepest node for a page coordinate and compute its absolute XPath across frames.
51
+ * More efficient than building a full hybrid snapshot when only a single node’s XPath is needed.
52
+ */
53
+ export declare function resolveXpathForLocation(page: Page, x: number, y: number): Promise<{
54
+ frameId: string;
55
+ backendNodeId: number;
56
+ absoluteXPath: string;
57
+ } | null>;
58
+ /**
59
+ * Compute the absolute XPath for the currently focused element.
60
+ * - Detects which frame has focus via document.hasFocus().
61
+ * - Finds the deepest activeElement (dives into shadow DOM).
62
+ * - Builds an absolute, cross-frame XPath by prefixing iframe hosts.
63
+ */
64
+ export declare function computeActiveElementXpath(page: Page): Promise<string | null>;
65
+ export declare function captureHybridSnapshot(page: Page, options?: SnapshotOptions): Promise<HybridSnapshot>;
66
+ /**
67
+ * Return the lines that appear in `nextTree` but not in `prevTree`.
68
+ * Comparison is done line-by-line, ignoring leading whitespace in both trees.
69
+ * The returned block is re-indented so the minimal indent becomes column 0.
70
+ */
71
+ export declare function diffCombinedTrees(prevTree: string, nextTree: string): string;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * CDP transport & session multiplexer
3
+ *
4
+ * Owns the browser WebSocket and multiplexes flattened Target sessions.
5
+ * Tracks inflight CDP calls, routes responses to the right session, and forwards events.
6
+ *
7
+ * This does not interpret Page/DOM/Runtime semantics — callers own that logic.
8
+ */
9
+ export interface CDPSessionLike {
10
+ send<R = unknown>(method: string, params?: object): Promise<R>;
11
+ on<P = unknown>(event: string, handler: (params: P) => void): void;
12
+ off<P = unknown>(event: string, handler: (params: P) => void): void;
13
+ close(): Promise<void>;
14
+ readonly id: string | null;
15
+ }
16
+ type EventHandler = (params: unknown) => void;
17
+ export declare class CdpConnection implements CDPSessionLike {
18
+ private ws;
19
+ private nextId;
20
+ private inflight;
21
+ private eventHandlers;
22
+ private sessions;
23
+ readonly id: string | null;
24
+ private transportCloseHandlers;
25
+ onTransportClosed(handler: (why: string) => void): void;
26
+ offTransportClosed(handler: (why: string) => void): void;
27
+ private emitTransportClosed;
28
+ private constructor();
29
+ static connect(wsUrl: string): Promise<CdpConnection>;
30
+ enableAutoAttach(): Promise<void>;
31
+ send<R = unknown>(method: string, params?: object): Promise<R>;
32
+ on<P = unknown>(event: string, handler: (params: P) => void): void;
33
+ off<P = unknown>(event: string, handler: (params: P) => void): void;
34
+ close(): Promise<void>;
35
+ getSession(sessionId: string): CdpSession | undefined;
36
+ attachToTarget(targetId: string): Promise<CdpSession>;
37
+ getTargets(): Promise<Array<{
38
+ targetId: string;
39
+ type: string;
40
+ url: string;
41
+ }>>;
42
+ private onMessage;
43
+ _sendViaSession<R = unknown>(sessionId: string, method: string, params?: object): Promise<R>;
44
+ _onSessionEvent(sessionId: string, event: string, handler: EventHandler): void;
45
+ _offSessionEvent(sessionId: string, event: string, handler: EventHandler): void;
46
+ _dispatchToSession(sessionId: string, event: string, params: unknown): void;
47
+ }
48
+ export declare class CdpSession implements CDPSessionLike {
49
+ private readonly root;
50
+ readonly id: string;
51
+ constructor(root: CdpConnection, id: string);
52
+ send<R = unknown>(method: string, params?: object): Promise<R>;
53
+ on<P = unknown>(event: string, handler: (params: P) => void): void;
54
+ off<P = unknown>(event: string, handler: (params: P) => void): void;
55
+ close(): Promise<void>;
56
+ dispatch(event: string, params: unknown): void;
57
+ }
58
+ export {};
@@ -0,0 +1,120 @@
1
+ import type { Protocol } from "devtools-protocol";
2
+ import { CdpConnection } from "./cdp";
3
+ import { Page } from "./page";
4
+ /**
5
+ * V3Context
6
+ *
7
+ * Owns the root CDP connection and wires Target/Page events into Page.
8
+ * Maintains one Page per top-level target, adopts OOPIF child sessions into the owner Page,
9
+ * and tracks target→page and (root) frame→target mappings for lookups.
10
+ *
11
+ * IMPORTANT: FrameId → session ownership is managed inside Page (via its FrameRegistry).
12
+ * Context never “guesses” owners; it simply forwards events (with the emitting session)
13
+ * so Page can record the correct owner at event time.
14
+ */
15
+ export declare class V3Context {
16
+ readonly conn: CdpConnection;
17
+ private readonly includeCursor;
18
+ private readonly env;
19
+ private constructor();
20
+ private readonly _piercerInstalled;
21
+ private _lastPopupSignalAt;
22
+ private sessionKey;
23
+ private readonly _sessionInit;
24
+ private pagesByTarget;
25
+ private mainFrameToTarget;
26
+ private sessionOwnerPage;
27
+ private frameOwnerPage;
28
+ private pendingOopifByMainFrame;
29
+ private createdAtByTarget;
30
+ private typeByTarget;
31
+ private _pageOrder;
32
+ /**
33
+ * Create a Context for a given CDP websocket URL and bootstrap target wiring.
34
+ */
35
+ static create(wsUrl: string, opts?: {
36
+ includeCursor?: boolean;
37
+ env?: "LOCAL" | "BROWSERBASE";
38
+ }): Promise<V3Context>;
39
+ /**
40
+ * Wait until at least one top-level Page has been created and registered.
41
+ * We poll internal maps that bootstrap/onAttachedToTarget populate.
42
+ */
43
+ private waitForFirstTopLevelPage;
44
+ private ensurePiercer;
45
+ /** Mark a page target as the most-recent one (active). */
46
+ private _pushActive;
47
+ /** Remove a page target from the recency list (used on close). */
48
+ private _removeFromOrder;
49
+ /** Return the current active Page (most-recent page that still exists). */
50
+ activePage(): Page | undefined;
51
+ /**
52
+ * Return top-level `Page`s (oldest → newest). OOPIF targets are not included.
53
+ */
54
+ pages(): Page[];
55
+ /**
56
+ * Resolve an owning `Page` by the **top-level main frame id**.
57
+ * Note: child (OOPIF) roots are intentionally not present in this mapping.
58
+ */
59
+ resolvePageByMainFrameId(frameId: string): Page | undefined;
60
+ /**
61
+ * Serialize the full frame tree for a given top-level main frame id.
62
+ */
63
+ getFullFrameTreeByMainFrameId(rootMainFrameId: string): Promise<Protocol.Page.FrameTree>;
64
+ /**
65
+ * Create a new top-level page (tab) with the given URL and return its Page object.
66
+ * Waits until the target is attached and registered.
67
+ */
68
+ newPage(url?: string): Promise<Page>;
69
+ /**
70
+ * Close CDP and clear all mappings. Best-effort cleanup.
71
+ */
72
+ close(): Promise<void>;
73
+ /**
74
+ * Bootstrap target lifecycle:
75
+ * - Attach to existing targets.
76
+ * - Attach on `Target.targetCreated` (fallback for OOPIFs).
77
+ * - Handle auto-attach events.
78
+ * - Clean up on detach/destroy.
79
+ */
80
+ private bootstrap;
81
+ /**
82
+ * Handle a newly attached target (top-level or potential OOPIF):
83
+ * - Enable Page domain and lifecycle events.
84
+ * - If top-level → create Page, wire listeners, resume.
85
+ * - Else → probe child root frame id via `Page.getFrameTree` and adopt immediately
86
+ * if the parent is known; otherwise stage until parent `frameAttached`.
87
+ * - Resume the target only after listeners are wired.
88
+ */
89
+ private onAttachedToTarget;
90
+ /**
91
+ * Detach handler:
92
+ * - Remove child session ownership and prune its subtree.
93
+ * - If a top-level target, cleanup its `Page` and mappings.
94
+ * - Drop any staged child for this session.
95
+ */
96
+ private onDetachedFromTarget;
97
+ /**
98
+ * Cleanup a top-level Page by target id, removing its root and staged children.
99
+ */
100
+ private cleanupByTarget;
101
+ /**
102
+ * Wire Page-domain frame events for a session into the owning Page & mappings.
103
+ * We forward the *emitting session* with every event so Page can stamp ownership precisely.
104
+ */
105
+ private installFrameEventBridges;
106
+ /**
107
+ * Register that a session belongs to a Page (used by event routing).
108
+ */
109
+ private wireSessionToOwnerPage;
110
+ /**
111
+ * Utility: reverse-lookup the top-level target id that owns a given Page.
112
+ */
113
+ private findTargetIdByPage;
114
+ private _notePopupSignal;
115
+ /**
116
+ * Await the current active page, waiting briefly if a popup/open was just triggered.
117
+ * Normal path returns immediately; popup path waits up to timeoutMs for the new page.
118
+ */
119
+ awaitActivePage(timeoutMs?: number): Promise<Page>;
120
+ }