Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix/browser-version adapt (#3638)

Puzhen Zhang177cbaa157a9
diff --git a/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts b/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts
index 8827628a0..99070f400 100644
--- a/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts
+++ b/camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts
@@ -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);
@@ -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
@@ -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}`;
@@ -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');
@@ -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
@@ -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) {
@@ -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;
@@ -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() !== '') {
@@ -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() !== '') {
@@ -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;
@@ -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() !== '') {
@@ -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;
@@ -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() !== '') {
@@ -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}`;
@@ -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}`;