Skip to content

expose a11y #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ async function example() {
/**
* Add your code here!
*/
// delete before pushing to main
const tree = await stagehand.page.getAccessibilityTree();
console.log(tree);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally what we log/write to a file is tree.simplified (the txt version passed into the llm)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i kinda like both? allows the user to play with it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, the whole tree is kinda overkill but with this structure both are returned

await stagehand.close();
}

Expand Down
16 changes: 14 additions & 2 deletions lib/StagehandPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import { StagehandObserveHandler } from "./handlers/observeHandler";
import { ActOptions, ActResult, GotoOptions, Stagehand } from "./index";
import { LLMClient } from "./llm/LLMClient";
import { StagehandContext } from "./StagehandContext";
import { EnhancedContext } from "../types/context";
import { EnhancedContext, TreeResult } from "../types/context";
import { clearOverlays } from "./utils";
import { getAccessibilityTree } from "./a11y/utils";

const BROWSERBASE_REGION_DOMAIN = {
"us-west-2": "wss://connect.usw2.browserbase.com",
Expand Down Expand Up @@ -58,6 +59,7 @@ export class StagehandPage {
(prop === ("act" as keyof Page) ||
prop === ("extract" as keyof Page) ||
prop === ("observe" as keyof Page) ||
prop === ("getAccessibilityTree" as keyof Page) ||
prop === ("on" as keyof Page))
) {
return () => {
Expand Down Expand Up @@ -248,6 +250,12 @@ export class StagehandPage {
};
}

if (prop === "getAccessibilityTree") {
return async () => {
return await this.getAccessibilityTree();
};
}

// Handle goto specially
if (prop === "goto") {
return async (url: string, options: GotoOptions) => {
Expand Down Expand Up @@ -679,7 +687,7 @@ export class StagehandPage {

async observe(
instructionOrOptions?: string | ObserveOptions,
): Promise<ObserveResult[]> {
): Promise<TreeResult | ObserveResult[]> {
if (!this.observeHandler) {
throw new Error("Observe handler not initialized");
}
Expand Down Expand Up @@ -829,4 +837,8 @@ export class StagehandPage {
async disableCDP(domain: string): Promise<void> {
await this.sendCDP(`${domain}.disable`, {});
}

async getAccessibilityTree(): Promise<TreeResult> {
return getAccessibilityTree(this, this.stagehand.logger);
}
}
3 changes: 3 additions & 0 deletions lib/a11y/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export async function buildHierarchicalTree(

/**
* Retrieves the full accessibility tree via CDP and transforms it into a hierarchical structure.
*
* DO NOT USE THIS FUNCTION DIRECTLY.
* Instead, use `StagehandPage.getAccessibilityTree()`
*/
export async function getAccessibilityTree(
page: StagehandPage,
Expand Down
14 changes: 8 additions & 6 deletions lib/handlers/observeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { observe } from "../inference";
import { LLMClient } from "../llm/LLMClient";
import { StagehandPage } from "../StagehandPage";
import { generateId, drawObserveOverlay } from "../utils";
import {
getAccessibilityTree,
getXPathByResolvedObjectId,
} from "../a11y/utils";
import { getXPathByResolvedObjectId } from "../a11y/utils";
import { AccessibilityNode } from "../../types/context";

export class StagehandObserveHandler {
Expand Down Expand Up @@ -67,7 +64,12 @@ export class StagehandObserveHandler {
drawOverlay?: boolean;
}) {
if (!instruction) {
instruction = `Find elements that can be used for any future actions in the page. These may be navigation links, related pages, section/subsection links, buttons, or other interactive elements. Be comprehensive: if there are multiple elements that may be relevant for future actions, return all of them.`;
this.logger({
category: "observation",
message: "No instruction provided, returning hybrid tree",
level: 1,
});
return await this.stagehandPage.getAccessibilityTree();
}

this.logger({
Expand All @@ -88,7 +90,7 @@ export class StagehandObserveHandler {
const useAccessibilityTree = !onlyVisible;
if (useAccessibilityTree) {
await this.stagehandPage._waitForSettledDom();
const tree = await getAccessibilityTree(this.stagehandPage, this.logger);
const tree = await this.stagehandPage.getAccessibilityTree();
this.logger({
category: "observation",
message: "Getting accessibility tree data",
Expand Down
8 changes: 7 additions & 1 deletion types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ObserveOptions,
ObserveResult,
} from "./stagehand";
import { TreeResult as TreeResultType } from "./context";

export const defaultExtractSchema = z.object({
extraction: z.string(),
Expand All @@ -21,6 +22,9 @@ export const pageTextSchema = z.object({
page_text: z.string(),
});

// Need to re-export TreeResult to make it into dist/types
export type TreeResult = TreeResultType;

export interface Page extends Omit<PlaywrightPage, "on"> {
act(action: string): Promise<ActResult>;
act(options: ActOptions): Promise<ActResult>;
Expand All @@ -34,13 +38,15 @@ export interface Page extends Omit<PlaywrightPage, "on"> {
): Promise<ExtractResult<T>>;
extract(): Promise<ExtractResult<typeof pageTextSchema>>;

observe(): Promise<ObserveResult[]>;
observe(): Promise<TreeResult>;
observe(instruction: string): Promise<ObserveResult[]>;
observe(options?: ObserveOptions): Promise<ObserveResult[]>;

on: {
(event: "popup", listener: (page: Page) => unknown): Page;
} & PlaywrightPage["on"];

getAccessibilityTree(): Promise<TreeResult>;
}

// Empty type for now, but will be used in the future
Expand Down
Loading