Skip to content

Commit 177cbaa

Browse files
fix/browser-version adapt (#3638)
Co-authored-by: Wendong-Fan <[email protected]>
1 parent 38eb3e3 commit 177cbaa

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export class HybridBrowserSession {
2222
this.logLimit = this.configLoader.getBrowserConfig().consoleLogLimit || 1000;
2323
}
2424

25+
/**
26+
* Compatibility wrapper for _snapshotForAI() API
27+
* Handles both old (string return) and new (object with .full) versions
28+
*/
29+
private async getSnapshot(page: Page): Promise<string> {
30+
const result = await (page as any)._snapshotForAI();
31+
// Version compatibility: if result is object (v1.57.0+), use .full property
32+
// If result is string (v1.56.x and earlier), return directly
33+
return typeof result === 'string' ? result : result.full;
34+
}
35+
2536
private registerNewPage(tabId: string, page: Page): void {
2637
// Register page and logs with tabId
2738
this.pages.set(tabId, page);
@@ -442,7 +453,7 @@ export class HybridBrowserSession {
442453
try {
443454
// Use _snapshotForAI() to properly update _lastAriaSnapshot
444455
const snapshotStart = Date.now();
445-
const snapshotText = await (page as any)._snapshotForAI();
456+
const snapshotText = await this.getSnapshot(page);
446457
const snapshotTime = Date.now() - snapshotStart;
447458

448459
// Extract refs from the snapshot text
@@ -559,7 +570,7 @@ export class HybridBrowserSession {
559570

560571
try {
561572
// Ensure we have the latest snapshot and mapping
562-
await (page as any)._snapshotForAI();
573+
await this.getSnapshot(page);
563574

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

674685
if (shouldCheckDiff && snapshotBefore) {
675686
await page.waitForTimeout(300);
676-
const snapshotAfter = await (page as any)._snapshotForAI();
687+
const snapshotAfter = await this.getSnapshot(page);
677688
let diffSnapshot = this.getSnapshotDiff(snapshotBefore, snapshotAfter, ['option', 'menuitem']);
678689

679690
// For combobox, find the new ref based on aria-label and prepend to diffSnapshot
@@ -795,7 +806,7 @@ export class HybridBrowserSession {
795806
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 }> {
796807
try {
797808
// Ensure we have the latest snapshot
798-
await (page as any)._snapshotForAI();
809+
await this.getSnapshot(page);
799810

800811
// Handle multiple inputs if provided
801812
if (inputs && inputs.length > 0) {
@@ -871,7 +882,7 @@ export class HybridBrowserSession {
871882
}
872883

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)