Skip to content

Agent Zero AI framework

Send browser helper source to host connector

Alessandroc020f1af2888
diff --git a/plugins/_a0_connector/helpers/ws_runtime.py b/plugins/_a0_connector/helpers/ws_runtime.py
index f311dd3bd..e56b71aab 100644
--- a/plugins/_a0_connector/helpers/ws_runtime.py
+++ b/plugins/_a0_connector/helpers/ws_runtime.py
@@ -69,6 +69,7 @@ class HostBrowserMetadata:
profile_label: str
profile_path: str
cdp_endpoint: str
+ content_helper_sha256: str
features: tuple[str, ...]
support_reason: str
updated_at: float
@@ -371,6 +372,7 @@ def store_sid_host_browser_metadata(sid: str, payload: dict[str, Any]) -> HostBr
profile_label=str(payload.get("profile_label", "") or "").strip(),
profile_path=str(payload.get("profile_path", "") or "").strip(),
cdp_endpoint=str(payload.get("cdp_endpoint", "") or "").strip(),
+ content_helper_sha256=str(payload.get("content_helper_sha256", "") or "").strip().lower(),
features=features,
support_reason=support_reason,
updated_at=time.time(),
@@ -418,6 +420,7 @@ def host_browser_metadata_for_sid(sid: str) -> dict[str, Any] | None:
"profile_label": metadata.profile_label,
"profile_path": metadata.profile_path,
"cdp_endpoint": metadata.cdp_endpoint,
+ "content_helper_sha256": metadata.content_helper_sha256,
"features": list(metadata.features),
"support_reason": metadata.support_reason,
"updated_at": metadata.updated_at,
@@ -484,6 +487,7 @@ def all_host_browser_metadata() -> list[dict[str, Any]]:
"browser_family": metadata.browser_family,
"profile_label": metadata.profile_label,
"profile_path": metadata.profile_path,
+ "content_helper_sha256": metadata.content_helper_sha256,
"features": list(metadata.features),
"support_reason": metadata.support_reason,
"updated_at": metadata.updated_at,
diff --git a/plugins/_browser/helpers/connector_runtime.py b/plugins/_browser/helpers/connector_runtime.py
index 3cd5baa98..97a691c69 100644
--- a/plugins/_browser/helpers/connector_runtime.py
+++ b/plugins/_browser/helpers/connector_runtime.py
@@ -2,7 +2,9 @@ from __future__ import annotations
import asyncio
import base64
+import hashlib
import uuid
+from functools import lru_cache
from pathlib import Path
from typing import Any
from urllib.parse import urlparse
@@ -40,6 +42,7 @@ from plugins._browser.helpers.config import (
BROWSER_OP_EVENT = "connector_browser_op"
BROWSER_OP_TIMEOUT = 120.0
HOST_BROWSER_SCREENSHOT_DIR = ("tmp", "browser", "host-screenshots")
+CONTENT_HELPER_PATH = Path(__file__).resolve().parents[1] / "assets" / "browser-page-content.js"
MAX_ARTIFACT_SIZE_BYTES = 25 * 1024 * 1024
BASE64_DECODE_CHARS_PER_CHUNK = 64 * 1024
_LOCAL_PROVIDERS = {"ollama", "lm_studio"}
@@ -167,15 +170,26 @@ class ConnectorBrowserRuntime:
if self._needs_prepare(sid, payload):
await self._send_browser_op(
sid,
- {
- "op_id": str(uuid.uuid4()),
- "context_id": self.context_id,
- "action": "ensure",
- },
+ self._with_content_helper(
+ sid,
+ {
+ "op_id": str(uuid.uuid4()),
+ "context_id": self.context_id,
+ "action": "ensure",
+ },
+ ),
)
sid = self._select_sid() or sid
- return await self._send_browser_op(sid, payload)
+ return await self._send_browser_op(sid, self._with_content_helper(sid, payload))
+
+ def _with_content_helper(self, sid: str, payload: dict[str, Any]) -> dict[str, Any]:
+ metadata = host_browser_metadata_for_sid(sid) or {}
+ if str(metadata.get("content_helper_sha256") or "").strip().lower() == _content_helper_sha256():
+ return payload
+ payload = dict(payload)
+ payload["content_helper"] = _content_helper_payload()
+ return payload
async def _send_browser_op(self, sid: str, payload: dict[str, Any]) -> Any:
op_id = str(payload["op_id"])
@@ -335,6 +349,24 @@ class ConnectorBrowserRuntime:
return "; ".join(parts)
+@lru_cache(maxsize=1)
+def _content_helper_payload() -> dict[str, str]:
+ try:
+ source = CONTENT_HELPER_PATH.read_text(encoding="utf-8")
+ except OSError as exc:
+ raise RuntimeError(
+ f"Host-browser content helper could not be read from {CONTENT_HELPER_PATH}: {exc}"
+ ) from exc
+ return {
+ "source": source,
+ "sha256": hashlib.sha256(source.encode("utf-8")).hexdigest(),
+ }
+
+
+def _content_helper_sha256() -> str:
+ return _content_helper_payload()["sha256"]
+
+
def _agent_uses_local_chat_model(agent: Any) -> bool:
try:
from plugins._model_config.helpers import model_config
diff --git a/tests/test_host_browser_connector.py b/tests/test_host_browser_connector.py
index 7819ca38b..cad22f3b8 100644
--- a/tests/test_host_browser_connector.py
+++ b/tests/test_host_browser_connector.py
@@ -36,6 +36,7 @@ def test_host_browser_metadata_selection_is_context_scoped():
"status": "ready",
"browser_family": "chrome",
"profile_label": "Default",
+ "content_helper_sha256": "abc123",
"features": ["open", "content"],
},
)
@@ -44,6 +45,7 @@ def test_host_browser_metadata_selection_is_context_scoped():
rows = ws_runtime.host_browser_metadata_for_context(context_id)
assert rows[0]["browser_family"] == "chrome"
assert rows[0]["enabled"] is True
+ assert rows[0]["content_helper_sha256"] == "abc123"
finally:
ws_runtime.unregister_sid(sid)
@@ -323,6 +325,8 @@ def test_connector_runtime_ensures_preparable_host_browser_before_action(monkeyp
assert result == {"id": 1, "state": {"runtime": "host"}}
assert [payload["action"] for payload in emitted] == ["ensure", "open"]
+ assert "__spaceBrowserPageContent__" in emitted[0]["content_helper"]["source"]
+ assert emitted[0]["content_helper"]["sha256"]
finally:
ws_runtime.unregister_sid(sid)