agent-zero/plugins/_browser/helpers/selector.py
Alessandro d47207dfd7
Some checks are pending
Build And Publish Docker Images / plan (push) Waiting to run
Build And Publish Docker Images / build (push) Blocked by required conditions
Refine host browser routing and settings copy
Store and surface host-browser preparation and CDP endpoint metadata from A0 CLI.

Let Browser runtime prepare candidate CLIs before the first action, and keep host-required errors more actionable.

Simplify Host Browser settings language and document the Chrome remote-debugging consent flow.
2026-05-08 06:37:32 +02:00

66 lines
2.4 KiB
Python

from __future__ import annotations
from typing import Any
from plugins._browser.helpers.config import RUNTIME_BACKEND_KEY, get_browser_config
from plugins._browser.helpers.runtime import get_runtime as get_container_runtime
async def get_tool_runtime(agent: Any):
context_id = str(agent.context.id)
config = get_browser_config(agent=agent)
backend = str(config.get(RUNTIME_BACKEND_KEY) or "container").strip()
if backend == "container":
return await get_container_runtime(context_id)
sid = _select_host_browser_candidate_sid(context_id)
if sid:
from plugins._browser.helpers.connector_runtime import ConnectorBrowserRuntime
return ConnectorBrowserRuntime(context_id, agent)
if backend == "host_required":
detail = _host_browser_status_detail(context_id)
raise RuntimeError(
"Browser host_required mode is enabled, but no subscribed A0 CLI currently "
"advertises host-browser support"
+ (f": {detail}" if detail else ".")
)
return await get_container_runtime(context_id)
def _select_host_browser_target_sid(context_id: str) -> str | None:
try:
from plugins._a0_connector.helpers.ws_runtime import select_host_browser_target_sid
except ImportError:
return None
return select_host_browser_target_sid(context_id)
def _select_host_browser_candidate_sid(context_id: str) -> str | None:
try:
from plugins._a0_connector.helpers.ws_runtime import select_host_browser_candidate_sid
except ImportError:
return _select_host_browser_target_sid(context_id)
return select_host_browser_candidate_sid(context_id)
def _host_browser_status_detail(context_id: str) -> str:
try:
from plugins._a0_connector.helpers.ws_runtime import host_browser_metadata_for_context
except ImportError:
return ""
statuses = host_browser_metadata_for_context(context_id)
if not statuses:
return "open A0 CLI and connect it to this Agent Zero chat."
parts = []
for status in statuses:
parts.append(
f"sid={status.get('sid')} supported={status.get('supported')} "
f"can_prepare={status.get('can_prepare')} enabled={status.get('enabled')} "
f"status={status.get('status') or 'unknown'} "
f"reason={status.get('support_reason') or 'none'}"
)
return "; ".join(parts)