Skip to content
Merged
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
39 changes: 25 additions & 14 deletions camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export class HybridBrowserSession {
this.logLimit = this.configLoader.getBrowserConfig().consoleLogLimit || 1000;
}

/**
* Compatibility wrapper for _snapshotForAI() API
* Handles both old (string return) and new (object with .full) versions
*/
private async getSnapshot(page: Page): Promise<string> {
const result = await (page as any)._snapshotForAI();
// Version compatibility: if result is object (v1.57.0+), use .full property
// If result is string (v1.56.x and earlier), return directly
return typeof result === 'string' ? result : result.full;
}

private registerNewPage(tabId: string, page: Page): void {
// Register page and logs with tabId
this.pages.set(tabId, page);
Expand Down Expand Up @@ -442,7 +453,7 @@ export class HybridBrowserSession {
try {
// Use _snapshotForAI() to properly update _lastAriaSnapshot
const snapshotStart = Date.now();
const snapshotText = await (page as any)._snapshotForAI();
const snapshotText = await this.getSnapshot(page);
const snapshotTime = Date.now() - snapshotStart;

// Extract refs from the snapshot text
Expand Down Expand Up @@ -559,7 +570,7 @@ export class HybridBrowserSession {

try {
// Ensure we have the latest snapshot and mapping
await (page as any)._snapshotForAI();
await this.getSnapshot(page);

// Use Playwright's aria-ref selector engine
const selector = `aria-ref=${ref}`;
Expand All @@ -581,7 +592,7 @@ export class HybridBrowserSession {
let snapshotBefore: string | null = null;
let comboboxAriaLabel: string | null = null;
if (shouldCheckDiff) {
snapshotBefore = await (page as any)._snapshotForAI();
snapshotBefore = await this.getSnapshot(page);
// Capture aria-label for combobox to find it again after click (ref may change)
if (isCombobox) {
comboboxAriaLabel = await element.getAttribute('aria-label');
Expand Down Expand Up @@ -673,7 +684,7 @@ export class HybridBrowserSession {

if (shouldCheckDiff && snapshotBefore) {
await page.waitForTimeout(300);
const snapshotAfter = await (page as any)._snapshotForAI();
const snapshotAfter = await this.getSnapshot(page);
let diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotAfter, ['option', 'menuitem']);

// For combobox, find the new ref based on aria-label and prepend to diffSnapshot
Expand Down Expand Up @@ -795,7 +806,7 @@ export class HybridBrowserSession {
private async performType(page: Page, ref: string | undefined, text: string | undefined, inputs?: Array<{ ref: string; text: string }>): Promise<{ success: boolean; error?: string; details?: Record<string, any>; diffSnapshot?: string }> {
try {
// Ensure we have the latest snapshot
await (page as any)._snapshotForAI();
await this.getSnapshot(page);

// Handle multiple inputs if provided
if (inputs && inputs.length > 0) {
Expand Down Expand Up @@ -871,7 +882,7 @@ export class HybridBrowserSession {
}

// Get snapshot before action to record existing elements
const snapshotBefore = await (page as any)._snapshotForAI();
const snapshotBefore = await this.getSnapshot(page);
const existingRefs = new Set<string>();
const refPattern = /\[ref=([^\]]+)\]/g;
let match;
Expand Down Expand Up @@ -929,7 +940,7 @@ export class HybridBrowserSession {
// If this element might show dropdown, wait and check for new elements
if (shouldCheckDiff) {
await page.waitForTimeout(300);
const snapshotAfter = await (page as any)._snapshotForAI();
const snapshotAfter = await this.getSnapshot(page);
const diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotAfter, ['option', 'menuitem']);

if (diffSnapshot && diffSnapshot.trim() !== '') {
Expand Down Expand Up @@ -982,7 +993,7 @@ export class HybridBrowserSession {
// If element might show dropdown, check for new elements
if (shouldCheckDiff) {
await page.waitForTimeout(300);
const snapshotFinal = await (page as any)._snapshotForAI();
const snapshotFinal = await this.getSnapshot(page);
const diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotFinal, ['option', 'menuitem']);

if (diffSnapshot && diffSnapshot.trim() !== '') {
Expand All @@ -1000,7 +1011,7 @@ export class HybridBrowserSession {
console.log(`Looking for new elements that appeared after action...`);

// Get snapshot after action to find new elements
const snapshotAfter = await (page as any)._snapshotForAI();
const snapshotAfter = await this.getSnapshot(page);
const newRefs = new Set<string>();
const afterRefPattern = /\[ref=([^\]]+)\]/g;
let afterMatch;
Expand Down Expand Up @@ -1047,7 +1058,7 @@ export class HybridBrowserSession {
// If element might show dropdown, check for new elements
if (shouldCheckDiff) {
await page.waitForTimeout(300);
const snapshotFinal = await (page as any)._snapshotForAI();
const snapshotFinal = await this.getSnapshot(page);
const diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotFinal, ['option', 'menuitem']);

if (diffSnapshot && diffSnapshot.trim() !== '') {
Expand Down Expand Up @@ -1077,7 +1088,7 @@ export class HybridBrowserSession {
console.log(`Looking for new elements that appeared after clicking readonly element...`);

// Get snapshot after action to find new elements
const snapshotAfter = await (page as any)._snapshotForAI();
const snapshotAfter = await this.getSnapshot(page);
const newRefs = new Set<string>();
const afterRefPattern = /\[ref=([^\]]+)\]/g;
let afterMatch;
Expand Down Expand Up @@ -1124,7 +1135,7 @@ export class HybridBrowserSession {
// If element might show dropdown, check for new elements
if (shouldCheckDiff) {
await page.waitForTimeout(300);
const snapshotFinal = await (page as any)._snapshotForAI();
const snapshotFinal = await this.getSnapshot(page);
const diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotFinal, ['option', 'menuitem']);

if (diffSnapshot && diffSnapshot.trim() !== '') {
Expand Down Expand Up @@ -1158,7 +1169,7 @@ export class HybridBrowserSession {
private async performSelect(page: Page, ref: string, value: string): Promise<{ success: boolean; error?: string }> {
try {
// Ensure we have the latest snapshot
await (page as any)._snapshotForAI();
await this.getSnapshot(page);

// Use Playwright's aria-ref selector
const selector = `aria-ref=${ref}`;
Expand Down Expand Up @@ -1219,7 +1230,7 @@ export class HybridBrowserSession {
private async performMouseDrag(page: Page, fromRef: string, toRef: string): Promise<{ success: boolean; error?: string }> {
try {
// Ensure we have the latest snapshot
await (page as any)._snapshotForAI();
await this.getSnapshot(page);

// Get elements using Playwright's aria-ref selector
const fromSelector = `aria-ref=${fromRef}`;
Expand Down
Loading