Skip to content

Conversation

@subway-jack
Copy link
Collaborator

Added a BaseBrowser that supports asynchronous

Description

Describe your changes in detail (optional if the linked issue already contains a detailed description of the changes).

Checklist

Go over all the following points, and put an x in all the boxes that apply.

  • I have read the CONTRIBUTION guide (required)
  • I have linked this PR to an issue using the Development section on the right sidebar or by adding Fixes #issue-number in the PR description (required)
  • I have checked if any dependencies need to be added or updated in pyproject.toml and uv lock
  • I have updated the tests accordingly (required for a bug fix or a new feature)
  • I have updated the documentation if needed:
  • I have added examples if this is a new feature

If you are unsure about any of these, don't hesitate to ask. We are here to help!

Added a BaseBrowser that supports asynchronous
@subway-jack subway-jack self-assigned this Mar 17, 2025
@subway-jack subway-jack changed the title Update browser_toolkit.py browserToolkit-supports-async Mar 17, 2025
Modified the asynchronous tool function name
To ensure that when determining whether a function is an asynchronous function, the original function is obtained instead of the packaged version
Support async function
print("Initializing browser...")
await browser.async_init()

# Test async_visit_page and async_wait_for_load (using GitHub as the test page)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The pre-commit failure is due to an E501 error, indicating that some lines are too long. Please shorten the lines to fit within the character limit. Also, run pre-commit run --all-files before committing and pushing your code to automatically fix formatting and import sorting issues.

Copy link
Collaborator Author

@subway-jack subway-jack Mar 22, 2025

Choose a reason for hiding this comment

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

OK, I got it, thanks for reminding me!

Comment on lines 116 to 132
==========================================================================
Initializing browser...
Testing async_visit_page and async_wait_for_load:
Current page URL: https://github.com/camel-ai/camel
Testing async_click_id:
Click complete
Testing async_click_blank_area:
Successfully clicked the blank area
Testing async_get_screenshot:
Screenshot taken successfully, saved at: tmp/camel_0317152711.png
Testing async_capture_full_page_screenshots:
Full page screenshot file paths: ['tmp/camel_0317152711.png', 'tmp/camel_0317152712.png', 'tmp/camel_0317152713.png', 'tmp/camel_0317152713.png', 'tmp/camel_0317152714.png', 'tmp/camel_0317152715.png', 'tmp/camel_0317152715.png', 'tmp/camel_0317152716.png', 'tmp/camel_0317152716.png', 'tmp/camel_0317152717.png', 'tmp/camel_0317152718.png', 'tmp/camel_0317152718.png', 'tmp/camel_0317152719.png', 'tmp/camel_0317152720.png', 'tmp/camel_0317152720.png', 'tmp/camel_0317152721.png', 'tmp/camel_0317152721.png', 'tmp/camel_0317152722.png', 'tmp/camel_0317152723.png', 'tmp/camel_0317152723.png', 'tmp/camel_0317152724.png', 'tmp/camel_0317152724.png']
Copy link
Collaborator

Choose a reason for hiding this comment

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

are these expected output logs from running the test functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep

Fix "browse url" error when executing asynchronous tools: 'playwright' object has no attribute 'start'
@Wendong-Fan Wendong-Fan marked this pull request as draft March 22, 2025 08:04
@Wendong-Fan
Copy link
Member

hey @subway-jack , thanks for the contribution! I turned the status of this PR to draft, feel free to open this once it's ready for review~

@subway-jack subway-jack marked this pull request as ready for review March 24, 2025 02:50
@subway-jack subway-jack requested a review from fengju0213 April 25, 2025 06:41
@subway-jack subway-jack linked an issue May 10, 2025 that may be closed by this pull request
2 tasks
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks @subway-jack left some comments below

pages.
"""

class AsyncBaseBrowser:
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe we can add an arg named async_mode in BaseBrowser,and no need to add a new class,add asynchronous methods based on the original functions


def init(self) -> None:
r"""Initialize the browser asynchronously."""
return self.async_init()
Copy link
Collaborator

Choose a reason for hiding this comment

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

will there be any issues if a synchronous method is used to call an asynchronous function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Screenshot 2025-05-11 at 14 09 48 This should be the previous version. Now I just added the @retry_on_error()

Comment on lines 564 to 578
fixed_part = f"_{timestamp}.png"
# Get the absolute path of the cache directory (ensure it ends with a separator)
base_path = os.path.join(os.path.abspath(self.cache_dir), "")
file_path = os.path.join(self.cache_dir, f"{url_name}{fixed_part}")

# If the generated file path exceeds the limit, truncate url_name accordingly
if len(file_path) > MAX_PATH_LENGTH:
allowed_name_length = (
MAX_PATH_LENGTH - len(base_path) - len(fixed_part)
)
url_name = url_name[:allowed_name_length]
file_path = os.path.join(
self.cache_dir, f"{url_name}{fixed_part}"
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this handled this way because there will be problems on Windows if the path exceeds a certain length?

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

thanks @subway-jack 's contribution! Left some comments below


MAX_PATH_LENGTH = 260

AVAILABLE_ACTIONS_PROMPT = """
Copy link
Member

Choose a reason for hiding this comment

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

for the duplicated components involving the browser toolkit file, could we move them all into a browser_toolkit_commons.py file to reduce duplication? We can created another issue & PR to refactor this later

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I’ve opened a follow-up issue to extract all duplicated browser-toolkit code into a shared commons module: #2380. We’ll track the refactor there and land a separate PR so this one can stay focused.

@subway-jack subway-jack requested a review from Wendong-Fan May 11, 2025 08:02
Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

thanks @subway-jack !

@Wendong-Fan Wendong-Fan merged commit 7bd1b55 into master May 11, 2025
6 of 7 checks passed
@Wendong-Fan Wendong-Fan deleted the BrowserToolkit-add-async-mode branch May 11, 2025 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Feature Request] Browser toolkit support async mode [Feature Request] BrowserToolkit add timeout setting

6 participants