tools.headless_browser module
Headless Firefox browser automation via Playwright.
Provides navigation, interaction, content extraction, waiting, screenshots, JavaScript execution, cookie management, network interception, session persistence, download handling, and console/dialog tools – all exposed through the v3 multi-tool format.
- class tools.headless_browser.HeadlessBrowserManager[source]
Bases:
objectProcess-wide owner of the shared headless Firefox instance.
Lazily launches a single Playwright Firefox browser and multiplexes it across named contexts (each with its own page, console-log buffer, response-pattern list, intercepted responses, downloads, and dialog handler), so every browser tool and other consumers share one warm browser rather than spawning their own. It tracks usage/error counts and self-heals: too many errors or a disconnected browser trigger a restart. A module-level singleton
_bmis the instance everything uses, and it is imported directly bytools/web_scraper.pyandbot/voice/puter.pyto reuse the same browser.- default_context_id
The default context id.
- is_initialized
The is initialized.
- last_used
The last used.
- usage_count
The usage count.
- error_count
The error count.
- max_errors_before_restart
The max errors before restart.
- idle_timeout
The idle timeout.
- _lock
The lock.
- default_viewport
The default viewport.
- default_user_agent
The default user agent.
- __init__()[source]
Set up manager bookkeeping without launching a browser.
Initializes all per-instance state (context/page maps, console-log, interception, download, dialog, and persistent-session registries, the async lock, default viewport/user-agent, and error/usage counters) to empty defaults. The actual Playwright/Firefox launch is deferred to
initializeso the module can be imported cheaply.Called once at import time to build the module-level
_bmsingleton.
- async initialize()[source]
Launch the Firefox browser and create the default context.
Starts Playwright and launches a headless Firefox instance (with autoplay and desktop-notification preferences disabled), then creates the
defaultcontext so a page is ready to use. Guarded by the async lock and idempotent: if an initialized, live browser already exists it returns immediately. On failure it cleans up and reportsFalse.Called by
ensure_readyandrestartin this module, and bybrowser_create_persistent_contextwhen no Playwright instance exists yet.- Returns:
Trueif the browser is ready,Falseif Playwright is missing or the launch failed.- Return type:
- async ensure_ready()[source]
Guarantee a live browser, restarting or initializing as needed.
The health gate every page/context accessor calls first. It restarts the browser if the error count has crossed
max_errors_before_restart, initializes it if not yet started, and restarts it if the existing browser has lost its connection; on success it refresheslast_used.Called by
ensure_proxied_context,get_page,get_context,create_new_context,browser_emulate_device, andbrowser_set_geolocationin this module.- Returns:
Truewhen a connected browser is available,Falseotherwise.- Return type:
- async restart()[source]
Tear down and relaunch the browser from scratch.
Recovery path used after repeated errors or a lost connection: it runs
cleanupto release all pages/contexts/browser/Playwright resources, theninitializeto launch a fresh browser and default context.Called by
ensure_readyin this module and exposed through thebrowser_restarttool handler.- Returns:
Trueif the relaunch succeeded,Falseotherwise.- Return type:
- async cleanup()[source]
Close all pages, contexts, the browser, and Playwright.
Best-effort teardown that swallows per-resource errors while closing every open page and context, the browser, and the Playwright driver, clearing the page/context maps and marking the manager uninitialized. Holds the async lock so it cannot race a concurrent initialize.
Called by
restartin this module; safe to invoke during shutdown.
- async resolve_context_id(context_id, proxy)[source]
Choose the context to use, preferring a proxy-derived one.
Resolves the effective browser context for a request: when
proxyis set it routes to the stable SOCKS-backed context fromensure_proxied_context(the proxy deliberately overrides any explicitcontext_id); otherwise it falls back tocontext_idor the default context. This is why setting a proxy is enough to isolate a session.Called by
_ctx_or_errin this module (which all proxy-aware browser tools route through).
- async ensure_proxied_context(proxy_url)[source]
Return (creating if needed) the context bound to a SOCKS proxy.
Validates the proxy with
assert_safe_socks_proxy_url, derives a stable context id via_proxy_derived_context_id, ensures the browser is ready, and lazily creates a proxied context the first time that proxy is seen so subsequent requests through the same proxy reuse one context.Called by
resolve_context_idin this module.- Parameters:
proxy_url (
str) – The SOCKS proxy URL to route the context through.- Returns:
The context id bound to this proxy.
- Return type:
- Raises:
ValueError – If the proxy URL fails validation.
RuntimeError – If the browser could not be made ready.
- async get_page(context_id=None)[source]
Return the active page for a context, creating it if absent.
The workhorse accessor nearly every browser handler calls to obtain a page to drive. It ensures the browser is ready, creates the context (or just a new page within an existing context) when one does not yet exist, and bumps
usage_count/last_used.Called by most
browser_*handlers in this module (navigation, interaction, content, waiting, screenshots, JS, etc.).- Parameters:
context_id (
str) – Context to fetch the page from; defaults to the default context.- Returns:
The Playwright page for the context.
- Raises:
RuntimeError – If the browser is not available.
- async get_context(context_id=None)[source]
Return the browser context for an id, creating it if absent.
Ensures the browser is ready and lazily creates the named context when it does not already exist, then returns it. Used by the cookie tools, which operate at the context level rather than on a page.
Called by
browser_get_cookies,browser_set_cookies, andbrowser_clear_cookiesin this module.- Parameters:
context_id (
str) – Context to fetch; defaults to the default context.- Returns:
The Playwright browser context.
- Raises:
RuntimeError – If the browser is not available.
- async create_new_context(context_id=None, **options)[source]
Create a brand-new isolated context and return its id.
Ensures the browser is ready, generates a random
context_<hex>id when none is given, and delegates to_create_contextwith the supplied options (device, geolocation, proxy, etc.). Used to spin up incognito-like or specially-configured contexts.Called by
browser_emulate_device,browser_set_geolocation, andbrowser_new_contextin this module.- Parameters:
context_id (
str) – Desired context id; auto-generated when omitted.**options – Context options forwarded to
_create_context.
- Returns:
The id of the newly created context.
- Return type:
- Raises:
RuntimeError – If the browser is not available.
- async close_context(context_id)[source]
Close a context’s page and context, ignoring errors.
Best-effort teardown of a single context: closes its page and the context itself (swallowing close errors) and removes both from the manager’s maps, freeing the resources held by that browsing session.
Called by
browser_close_contextin this module.- Parameters:
context_id (
str) – The context to close.
- record_error()[source]
Increment the failure counter that drives auto-restart.
Bumps
error_count; once it reachesmax_errors_before_restart, the nextensure_readycall recycles the browser. Handlers call this on unexpected Playwright failures so a flaky browser self-heals.Called from the exception paths of most
browser_*handlers in this module.
Navigate the headless browser to a URL.
Backs the
browser_navigatetool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec) and validating the URL and wait state (_validate_url), it resolves the context (_ctx_or_err), grabs the page (_bm.get_page), and drivespage.goto, returning the final URL, title, and HTTP status. Playwright errors are recorded via_bm.record_errorso a flaky browser self-heals.Dispatched by the tool runner in
tools/__init__.pyfor the registeredbrowser_navigatetool (tool_def.handler(**arguments, ctx=ctx)); also invoked directly intests/test_unsandboxed_exec_gating.py.- Parameters:
url (
str) – URL to navigate to.wait_until (
str) – When navigation is considered complete (load,domcontentloaded,networkidle, orcommit).timeout (
int) – Navigation timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL; routes through a proxied context.ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope; on success carries the resulting url, title, status,
okflag, and context id; on failure an error message.- Return type:
- async tools.headless_browser.browser_go_back(timeout=30000, context_id=None, proxy=None)[source]
Navigate back in the browser history.
Backs the
browser_go_backtool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.go_back, returning the resulting url, title, and status. Requires no extra privilege.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_go_backtool; there are no direct internal callers.
- async tools.headless_browser.browser_go_forward(timeout=30000, context_id=None, proxy=None)[source]
Navigate forward in the browser history.
Backs the
browser_go_forwardtool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.go_forward, returning the resulting url, title, and status. Requires no extra privilege.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_go_forwardtool; there are no direct internal callers.
- async tools.headless_browser.browser_reload(wait_until='load', timeout=30000, context_id=None, proxy=None, ctx=None)[source]
Reload the current page.
Backs the
browser_reloadtool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.reload, returning the resulting url, title, and status.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments, ctx=ctx)for the registeredbrowser_reloadtool; there are no direct internal callers.- Parameters:
wait_until (
str) – When the reload is considered complete.timeout (
int) – Timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope with the resulting url, title, and status, or an error.
- Return type:
- async tools.headless_browser.browser_close_context(context_id=None, proxy=None, ctx=None)[source]
Close a browser context (window), except the default one.
Backs the
browser_close_contexttool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec) and resolving the context (_ctx_or_err), it refuses to close the default context and otherwise delegates to_bm.close_contextto free that browsing session’s page and context.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments, ctx=ctx)for the registeredbrowser_close_contexttool; there are no direct internal callers.- Parameters:
context_id (
str) – Context to close (optional ifproxyis set).proxy (
str) – Optional SOCKS proxy URL (same one used to create it).ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope confirming closure, or an error (including refusal to close the default context).
- Return type:
- async tools.headless_browser.browser_click(selector, button='left', click_count=1, timeout=30000, context_id=None, proxy=None, ctx=None)[source]
Click an element on the page.
Backs the
browser_clicktool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec) and validating the mouse button, it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.clickwith the requested button and click count.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments, ctx=ctx)for the registeredbrowser_clicktool; there are no direct internal callers.- Parameters:
selector (
str) – Element selector (CSS, XPath,text=...).button (
str) – Mouse button:left,right, ormiddle.click_count (
int) – Number of clicks (e.g. 2 for double-click).timeout (
int) – Element wait timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope confirming the click, or an error (e.g. element not found within the timeout).
- Return type:
- async tools.headless_browser.browser_type(selector, text, delay=0, clear_first=False, timeout=30000, context_id=None, proxy=None, ctx=None)[source]
Type text into an input element, simulating keystrokes.
Backs the
browser_typetool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), optionally clears the field first, then drivespage.typewith the requested per-keydelayso the input looks human-typed (useful for fields that react to keystrokes).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments, ctx=ctx)for the registeredbrowser_typetool; there are no direct internal callers.- Parameters:
selector (
str) – Target input element selector.text (
str) – Text to type.delay (
int) – Delay between keystrokes in milliseconds.clear_first (
bool) – Whether to clear the field before typing.timeout (
int) – Element wait timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope confirming the typed text length, or an error.
- Return type:
- async tools.headless_browser.browser_fill(selector, value, timeout=30000, context_id=None, proxy=None, ctx=None)[source]
Fill an input field instantly (no per-key typing).
Backs the
browser_filltool. After requiringUNSANDBOXED_EXEC(_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.fill, which sets the value in one shot. Faster thanbrowser_typewhen keystroke simulation is unnecessary.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments, ctx=ctx)for the registeredbrowser_filltool; there are no direct internal callers.- Parameters:
selector (
str) – Target input element selector.value (
str) – Value to set.timeout (
int) – Element wait timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.ctx (ToolContext) – Tool execution context; required for the privilege check.
- Returns:
JSON envelope confirming the filled value length, or an error.
- Return type:
- async tools.headless_browser.browser_fill_form(fields, timeout=30000, context_id=None, proxy=None)[source]
Fill several form fields in one call.
Backs the
browser_fill_formtool. Parsesfields(a JSON object mapping selectors to values), resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.fillfor each entry, accumulating per-field successes and errors so a single bad selector does not abort the rest. Note this handler is not privilege-gated.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_fill_formtool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope;
successwhen all fields filled,partialwith a filled/errors breakdown when some failed, or an error.- Return type:
- async tools.headless_browser.browser_select_option(selector, value=None, label=None, index=None, timeout=30000, context_id=None, proxy=None)[source]
Select an option in a dropdown/
<select>element.Backs the
browser_select_optiontool. Requires at least one ofvalue/label/index, resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.select_optionwith whichever matchers were supplied, returning the selected values.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_select_optiontool; there are no direct internal callers.- Parameters:
selector (
str) – The<select>element selector.value (
str) – Option value to select.label (
str) – Option visible label to select.index (
int) – Option index to select.timeout (
int) – Element wait timeout in milliseconds.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.
- Returns:
JSON envelope with the selected values, or an error.
- Return type:
- async tools.headless_browser.browser_press_key(key, selector=None, timeout=30000, context_id=None, proxy=None)[source]
Press a keyboard key, optionally focused on an element.
Backs the
browser_press_keytool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and either presses the key on a specific element (page.press) when a selector is given or on the page keyboard (page.keyboard.press) otherwise. Supports chords likeControl+a.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_press_keytool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the key press, or an error.
- Return type:
- async tools.headless_browser.browser_hover(selector, timeout=30000, context_id=None, proxy=None)[source]
Hover the mouse over an element.
Backs the
browser_hovertool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and callspage.hoverto move the pointer over the element, useful for triggering hover menus or tooltips.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_hovertool; there are no direct internal callers.
- async tools.headless_browser.browser_scroll(direction='down', amount=500, selector=None, context_id=None, proxy=None)[source]
Scroll the page or a specific element by a pixel amount.
Backs the
browser_scrolltool. Validates the direction, resolves the context (_ctx_or_err), gets the page (_bm.get_page), translates the direction/amount into dx/dy, and runs apage.evaluatethat scrolls either the matched element or the window.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_scrolltool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the scroll, or an error.
- Return type:
- async tools.headless_browser.browser_get_content(format='text', selector=None, context_id=None, proxy=None)[source]
Extract page content as HTML, plain text, or markdown.
Backs the
browser_get_contenttool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then reads either the whole page or a selected element. Formarkdownit converts the HTML throughhtml2text(when available) and collapses blank runs; all output is bounded by_truncate_contentso a huge page cannot overflow the tool result.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_contenttool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the (possibly truncated) content, its length, the page url, and title, or an error.
- Return type:
- async tools.headless_browser.browser_get_text(selector, timeout=30000, context_id=None, proxy=None)[source]
Get the inner text of a single element.
Backs the
browser_get_texttool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then returnspage.inner_textfor the matched selector, waiting up to the timeout for it to appear.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_texttool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the element text, or an error (e.g. not found).
- Return type:
- async tools.headless_browser.browser_get_attribute(selector, attribute, timeout=30000, context_id=None, proxy=None)[source]
Read a named attribute from an element.
Backs the
browser_get_attributetool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then returnspage.get_attributefor the selector/attribute pair (e.g.href,src,class).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_attributetool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the attribute value (
Noneif absent), or an error.- Return type:
- async tools.headless_browser.browser_get_page_info(context_id=None, proxy=None)[source]
Report the current page’s URL, title, and viewport.
Backs the
browser_get_page_infotool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and returns lightweight page metadata without extracting content.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_page_infotool; there are no direct internal callers.
- async tools.headless_browser.browser_query_selector_all(selector, attributes=None, timeout=30000, context_id=None, proxy=None)[source]
Find all elements matching a selector and return their data.
Backs the
browser_query_selector_alltool. Optionally parsesattributes(a JSON array of attribute names), resolves the context (_ctx_or_err) and page (_bm.get_page), runspage.query_selector_all, and for each match collects its inner text plus any requested attributes. Results are capped at 100 elements.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_query_selector_alltool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the match count and the per-element data (first 100), or an error.
- Return type:
- async tools.headless_browser.browser_wait_for_selector(selector, state='visible', timeout=30000, context_id=None, proxy=None)[source]
Wait for an element to reach a given state.
Backs the
browser_wait_for_selectortool. Validates the requested state, resolves the context (_ctx_or_err) and page (_bm.get_page), and callspage.wait_for_selectorto block until the element is visible, hidden, attached, or detached (or the timeout elapses).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_wait_for_selectortool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the state was reached, or a timeout/error.
- Return type:
- async tools.headless_browser.browser_wait_for_load_state(state='load', timeout=30000, context_id=None, proxy=None)[source]
Wait for the page to reach a load lifecycle state.
Backs the
browser_wait_for_load_statetool. Validates the state, resolves the context (_ctx_or_err) and page (_bm.get_page), and callspage.wait_for_load_stateto block until the page is loaded, DOM-content loaded, or network-idle.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_wait_for_load_statetool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the load state and url, or a timeout/error.
- Return type:
- async tools.headless_browser.browser_wait_for_url(url_pattern, timeout=30000, context_id=None, proxy=None)[source]
Wait until the page URL matches a pattern.
Backs the
browser_wait_for_urltool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and callspage.wait_for_url; a pattern anchored with^/$is compiled as a regex, otherwise it is treated as a glob substring. Useful for awaiting redirects after a click or login.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_wait_for_urltool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the matched url, or a timeout/error.
- Return type:
- async tools.headless_browser.browser_wait(milliseconds, context_id=None, proxy=None)[source]
Sleep for a fixed number of milliseconds.
Backs the
browser_waittool. A simpleasyncio.sleep(bounded to 0-60000 ms) for pacing automation between steps; it does not touch a page or context.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_waittool; there are no direct internal callers.
- async tools.headless_browser.browser_screenshot(full_page=True, selector=None, filename=None, context_id=None, proxy=None)[source]
Capture a screenshot to a file under the output directory.
Backs the registered
browser_screenshottool (this definition shadows the earlier base64 variant). Resolves the context (_ctx_or_err) and page (_bm.get_page), then writes a PNG toOUTPUT_DIR(auto-named with a timestamp when no filename is given) for either a selected element or the full/visible page, returning the saved path.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_screenshottool; there are no direct internal callers.- Parameters:
full_page (
bool) – Whether to capture the full scrollable page.selector (
str) – Optional element to screenshot instead of the page.filename (
str) – Optional base filename (.pngis appended).context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.
- Returns:
JSON envelope with the saved file path and metadata, or an error.
- Return type:
- async tools.headless_browser.browser_pdf(filename=None, format='A4', landscape=False, print_background=True, context_id=None, proxy=None)[source]
Render the current page to a PDF file.
Backs the
browser_pdftool. Validates the paper format, resolves the context (_ctx_or_err) and page (_bm.get_page), then writes a PDF toOUTPUT_DIR(auto-named with a timestamp when no filename is given) viapage.pdf, honoring orientation and background-printing options.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_pdftool; there are no direct internal callers.- Parameters:
filename (
str) – Optional base filename (.pdfis appended).format (
str) – Paper size:A4,Letter,Legal,Tabloid,A3, orA5.landscape (
bool) – Whether to use landscape orientation.print_background (
bool) – Whether to print background graphics.context_id (
str) – Browser context to use (ignored whenproxyis set).proxy (
str) – Optional SOCKS proxy URL.
- Returns:
JSON envelope with the saved PDF path and metadata, or an error.
- Return type:
- async tools.headless_browser.browser_evaluate(expression, context_id=None, proxy=None)[source]
Evaluate a JavaScript expression and return its result.
Backs the
browser_evaluatetool. Resolves the context (_ctx_or_err) and page (_bm.get_page), runspage.evaluate(expression), and serializes the return value (coercing non-JSON types viadefault=str), truncating the payload pastMAX_CONTENT_LENGTH.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_evaluatetool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the evaluated result (and its type), truncated when too large, or an error.
- Return type:
- async tools.headless_browser.browser_execute(script, context_id=None, proxy=None)[source]
Execute a JavaScript snippet for its side effects.
Backs the
browser_executetool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and runspage.evaluate(script)without returning the result, for scripts run purely for their effect on the page.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_executetool; there are no direct internal callers.
- async tools.headless_browser.browser_get_cookies(urls=None, context_id=None, proxy=None)[source]
List the context’s cookies with values redacted.
Backs the
browser_get_cookiestool. Optionally parsesurls(a JSON array to filter by), fetches cookies from the context (_bm.get_context, thencontext.cookies), and returns sanitized metadata (name, domain, path, flags, and value length) without exposing the secret cookie values.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_cookiestool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the sanitized cookie list and count, or an error.
- Return type:
- async tools.headless_browser.browser_set_cookies(cookies, context_id=None, proxy=None)[source]
Add cookies to the browser context.
Backs the
browser_set_cookiestool. Parsescookies(a JSON array of cookie objects), validates that each has a name/value and aurlordomain, then adds them via_bm.get_contextpluscontext.add_cookies. Lets automation pre-seed an authenticated session.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_set_cookiestool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the count and names set, or a validation error.
- Return type:
- async tools.headless_browser.browser_clear_cookies(context_id=None, proxy=None)[source]
Clear all cookies in the browser context.
Backs the
browser_clear_cookiestool. Fetches the context (_bm.get_context) and callscontext.clear_cookiesto drop every cookie, effectively logging the session out.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_clear_cookiestool; there are no direct internal callers.
- async tools.headless_browser.browser_emulate_device(device_name, context_id=None, proxy=None)[source]
Create a context emulating a named mobile device or tablet.
Backs the
browser_emulate_devicetool. Ensures the browser is ready (_bm.ensure_ready), looks the device up in Playwright’s device registry, and spins up a new context configured for it via_bm.create_new_context(passingdevice), returning the new context id and the device viewport.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_emulate_devicetool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the new context id and device info, or an error (e.g. unknown device, with a sample of valid names).
- Return type:
- async tools.headless_browser.browser_set_geolocation(latitude, longitude, accuracy=100, context_id=None, proxy=None)[source]
Create a context with a spoofed geolocation.
Backs the
browser_set_geolocationtool. Validates the latitude/longitude ranges, ensures the browser is ready (_bm.ensure_ready), and creates a new context with the geolocation (and the geolocation permission granted) via_bm.create_new_context, so pages that readnavigator.geolocationsee the supplied coordinates.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_set_geolocationtool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the new context id and coordinates, or an error.
- Return type:
- async tools.headless_browser.browser_set_viewport(width, height, context_id=None, proxy=None)[source]
Resize the page viewport.
Backs the
browser_set_viewporttool. Validates the dimensions (100-4000 px each), resolves the context (_ctx_or_err) and page (_bm.get_page), and callspage.set_viewport_sizeto change the rendered window size.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_set_viewporttool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the new size, or an error.
- Return type:
- async tools.headless_browser.browser_block_resources(resource_types, context_id=None, proxy=None)[source]
Block selected resource types from loading.
Backs the
browser_block_resourcestool. Parses and validatesresource_types(a JSON array), resolves the context (_ctx_or_err) and page (_bm.get_page), and installs apage.routehandler that aborts requests of the blocked types and continues the rest, speeding up loads and saving bandwidth (e.g. blocking images and fonts).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_block_resourcestool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope listing the blocked types, or a validation error.
- Return type:
- async tools.headless_browser.browser_get_status()[source]
Report the browser manager’s runtime status.
Backs the
browser_get_statustool. Reads diagnostic fields off the shared_bmsingleton (whether Playwright is available, init/connection state, usage and error counts, and the list of active contexts/pages) without touching any page.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_statustool; there are no direct internal callers.- Returns:
JSON envelope with the manager status snapshot.
- Return type:
- async tools.headless_browser.browser_restart()[source]
Restart the shared browser instance.
Backs the
browser_restarttool. Delegates to_bm.restartto tear down and relaunch Firefox, used to recover from a wedged or misbehaving browser.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_restarttool; there are no direct internal callers.- Returns:
JSON envelope reporting success or failure of the restart.
- Return type:
- async tools.headless_browser.browser_new_context(context_id=None, incognito=True, proxy=None)[source]
Create a new isolated browser context (incognito-like window).
Backs the
browser_new_contexttool. Optionally validates a SOCKS proxy (assert_safe_socks_proxy_url) and creates a fresh context via_bm.create_new_context, returning its id. Each context has its own cookies, storage, and cache, so concurrent isolated sessions are possible.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_new_contexttool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the new context id, or an error.
- Return type:
- async tools.headless_browser.browser_enable_response_interception(url_patterns, context_id=None, proxy=None)[source]
Start capturing network responses matching URL patterns.
Backs the
browser_enable_response_interceptiontool. Parsesurl_patterns(a JSON array of globs), resolves the context (_ctx_or_err), ensures a page exists (_bm.get_page), and appends the patterns to this context’s_bm.response_patterns. Theon_responsehandler installed by_setup_page_handlersthen stores matching responses in_bm.intercepted_responsesfor later retrieval.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_enable_response_interceptiontool; there are no direct internal callers.
- async tools.headless_browser.browser_wait_for_response(url_pattern, timeout=30000, context_id=None, proxy=None)[source]
Wait for one network response matching a pattern and return its body.
Backs the
browser_wait_for_responsetool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then usespage.expect_responseto block until a response URL matches the glob, decoding the body (JSON via_json_loads_network_body, text inline, large bodies summarized). Unlike interception, this captures a single awaited response inline.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_wait_for_responsetool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the response url, status, headers, body, and method, or a timeout/error.
- Return type:
- async tools.headless_browser.browser_get_intercepted_responses(context_id=None, proxy=None, url_filter=None)[source]
List captured network responses as lightweight summaries.
Backs the
browser_get_intercepted_responsestool. Reads from_bm.intercepted_responses, optionally narrowing to a context (_ctx_or_err) and/or a URL glob, and returns one summary per response (id, url, status, method, timestamp, and whether a body was captured) so the caller can then fetch a specific body viabrowser_get_response_body.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_intercepted_responsestool; there are no direct internal callers.
- async tools.headless_browser.browser_get_response_body(response_id)[source]
Return the full stored record for one intercepted response.
Backs the
browser_get_response_bodytool. Looks the id up in_bm.intercepted_responsesand returns the complete captured record (headers and decoded body included), the companion tobrowser_get_intercepted_responseswhich only returns summaries.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_response_bodytool; there are no direct internal callers.
- async tools.headless_browser.browser_clear_intercepted_responses(context_id=None, proxy=None)[source]
Drop stored intercepted responses, optionally scoped to a context.
Backs the
browser_clear_intercepted_responsestool. When a context (or proxy) is given it resolves it (_ctx_or_err) and removes only that context’s entries from_bm.intercepted_responses; otherwise it clears the whole capture buffer, freeing memory.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_clear_intercepted_responsestool; there are no direct internal callers.
- async tools.headless_browser.browser_create_persistent_context(session_name, context_id=None, proxy=None)[source]
Create a context whose cookies and storage persist on disk.
Backs the
browser_create_persistent_contexttool. Rejects a duplicate context id, ensures Playwright is initialized, optionally validates a SOCKS proxy, then calls_bm._create_persistent_contextto launch a context rooted at aSESSIONS_DIRprofile so a logged-in session can be reused across runs.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_create_persistent_contexttool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the context id, sanitized session name, and session path, or an error.
- Return type:
- async tools.headless_browser.browser_list_sessions()[source]
List saved persistent browser sessions on disk.
Backs the
browser_list_sessionstool. WalksSESSIONS_DIRfor saved profile directories, computes each one’s on-disk size, and notes whether a live context (from_bm.persistent_contexts) is currently bound to it.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_list_sessionstool; there are no direct internal callers.- Returns:
JSON envelope listing each session’s name, path, size, and active context (if any), or an error.
- Return type:
- async tools.headless_browser.browser_delete_session(session_name)[source]
Delete a saved persistent session directory from disk.
Backs the
browser_delete_sessiontool. Resolves the sanitized session path underSESSIONS_DIR, refuses to delete a session still bound to a live context (checked against_bm.persistent_contexts), and otherwise removes the directory tree off-thread viaasyncio.to_thread(shutil.rmtree).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_delete_sessiontool; there are no direct internal callers.
- async tools.headless_browser.browser_wait_for_download(timeout=30000, context_id=None, proxy=None)[source]
Wait for the next file download in a context to complete.
Backs the
browser_wait_for_downloadtool. Resolves the context (_ctx_or_err), ensures a page exists (_bm.get_page), and registers a future in_bm.pending_downloadsthat theon_downloadhandler resolves once the file is saved toDOWNLOADS_DIR; the future is awaited with a timeout.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_wait_for_downloadtool; there are no direct internal callers.
- async tools.headless_browser.browser_get_downloads(context_id=None, proxy=None, state=None)[source]
List captured downloads, optionally filtered.
Backs the
browser_get_downloadstool. Reads_bm.downloads, optionally narrowing to a context (_ctx_or_err) and/or a state, and returns the matching download records (id, url, filename, saved path, state, size).Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_downloadstool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the count and matching download records, or an error.
- Return type:
- async tools.headless_browser.browser_save_download(download_id, filename=None)[source]
Return a download’s info, optionally renaming the saved file.
Backs the
browser_save_downloadtool. Looks the id up in_bm.downloads; when afilenameis given it renames the file on disk (withinDOWNLOADS_DIR, sanitizing the name) and updates the record. Returns the (possibly updated) download info.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_save_downloadtool; there are no direct internal callers.
- async tools.headless_browser.browser_get_console_logs(context_id=None, proxy=None, level=None, limit=100)[source]
Return captured browser console messages for a context.
Backs the
browser_get_console_logstool. Resolves the context (_ctx_or_err) and reads its buffer from_bm.console_logs(populated by theon_consolepage handler), optionally filtering by log level and capping to the most recentlimitentries.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_console_logstool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope with the matching console logs and count, or an error.
- Return type:
- async tools.headless_browser.browser_clear_console_logs(context_id=None, proxy=None)[source]
Clear a context’s captured console logs.
Backs the
browser_clear_console_logstool. Resolves the context (_ctx_or_err) and empties its_bm.console_logsbuffer, reporting how many entries were dropped.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_clear_console_logstool; there are no direct internal callers.
- async tools.headless_browser.browser_handle_dialog(action, prompt_text=None, context_id=None, proxy=None)[source]
Set how JavaScript dialogs are auto-handled for a context.
Backs the
browser_handle_dialogtool. Validates the action, resolves the context (_ctx_or_err), and stores the policy in_bm.dialog_handlersso theon_dialogpage handler accepts, dismisses, or auto-accepts future alerts/confirms/prompts; it also echoes back the last seen dialog from_bm.pending_dialogs.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_handle_dialogtool; there are no direct internal callers.- Parameters:
- Returns:
JSON envelope confirming the policy and the last dialog seen, or a validation error.
- Return type:
- async tools.headless_browser.browser_get_pending_dialog(context_id=None, proxy=None)[source]
Report the last JavaScript dialog seen in a context.
Backs the
browser_get_pending_dialogtool. Resolves the context (_ctx_or_err) and returns its entry from_bm.pending_dialogs(recorded by theon_dialogpage handler), so the caller can inspect the type, message, and how it was handled.Dispatched by the tool runner in
tools/__init__.py, which callstool_def.handler(**arguments)for the registeredbrowser_get_pending_dialogtool; there are no direct internal callers.