Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
import shutil
import time

# Ensure /a0 is on sys.path so plugin helpers can be imported regardless of CWD
_a0_root = "/a0"
if _a0_root not in sys.path:
sys.path.insert(0, _a0_root)


try:
from usr.plugins.camofox_browser.helpers.config import normalize_headless_mode
from usr.plugins.camofox_browser.helpers.cli import resolve_camofox_command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
</div>
</div>
<div class="camofox-body" x-show="!$store.camofox.panelMinimized">
<template x-if="$store.camofox.vncUrl">
<iframe :src="$store.camofox.vncUrl" class="camofox-vnc-frame"
allow="clipboard-read; clipboard-write"
sandbox="allow-scripts allow-same-origin allow-forms"></iframe>
</template>
<template x-if="!$store.camofox.vncUrl">
<iframe x-show="$store.camofox.vncUrl"
:src="$store.camofox.vncUrl || 'about:blank'"
class="camofox-vnc-frame"
allow="clipboard-read; clipboard-write"
sandbox="allow-scripts allow-same-origin allow-forms"></iframe>
<template x-if="!$store.camofox.vncUrl && !$store.camofox._rawVncUrl">
<div class="camofox-placeholder">
<span class="material-symbols-outlined" style="font-size:2rem;opacity:0.3;">desktop_access_disabled</span>
<div style="margin-top:8px;">No browser visible — agent is in headless mode</div>
Expand Down
49 changes: 34 additions & 15 deletions helpers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

_STATE_FILE = "/tmp/camofox_plugin_state.json"
_BROWSING_ACTIVE_TTL_SECONDS = 120
_VNC_IDLE_TTL_SECONDS = 10
_VNC_IDLE_TTL_SECONDS = 3600 # 1 hour — was 10s which caused iframe flicker; keepalive hack no longer needed


def _read() -> dict:
Expand Down Expand Up @@ -106,28 +106,47 @@ def _normalize_entry(entry: dict) -> dict:
is_vnc_idle = vnc_ts and vnc_age > _VNC_IDLE_TTL_SECONDS
if normalized.get("browsing") and not normalized.get("blocked") and is_browsing_stale:
normalized["browsing"] = False
# Clear idle VNC URL after timeout, but preserve the user-selected
# display_mode — that reflects intent and should only change via an
# explicit toggle.
if (
not normalized.get("browsing")
and not normalized.get("blocked")
and normalized.get("vnc_url")
and is_vnc_idle
):
normalized["vnc_url"] = ""
# NOTE: Do NOT auto-clear vnc_url based on idle time. The URL reflects
# whether the camofox server has VNC running; only an explicit clear_vnc()
# call (made when the server actually stops VNC) should remove it.
# Time-based clearing caused the iframe to vanish after ~1h even while
# the VNC stack was alive and the user was simply not actively browsing.
_update_last_activity(normalized)
return normalized


def get(user_id: str = "") -> dict:
"""Get state for a userId, or the most recently active if empty."""
"""Get state for a userId, or the most recently active if empty.

If an explicit user_id is requested but that user has no active VNC URL
and is not browsing, fall back to any other user that IS active. This
prevents the WebUI from sticking to a stale userId binding after the
actual browsing session moved to a different userId (e.g. WebUI bound
to 'a0-default' while the agent tool runs as 'a0-agent-0').
"""
data = {uid: _normalize_entry(entry) for uid, entry in _read().items()}

def _is_active(entry: dict) -> bool:
return bool(
entry.get("browsing")
or entry.get("vnc_url")
or entry.get("display_mode", "headless") != "headless"
)

if user_id and user_id in data:
return {**data[user_id], "_userId": user_id}
# Find any active entry
entry = data[user_id]
if _is_active(entry):
return {**entry, "_userId": user_id}
# Requested user is idle — try to find any active entry instead
for uid, other in data.items():
if uid != user_id and _is_active(other):
return {**other, "_userId": uid}
# No active entry anywhere — return the requested user as-is
return {**entry, "_userId": user_id}

# No explicit user — find any active entry
for uid, entry in data.items():
if entry.get("browsing") or entry.get("vnc_url") or entry.get("display_mode", "headless") != "headless":
if _is_active(entry):
return {**entry, "_userId": uid}
# Return most recent entry
if data:
Expand Down
7 changes: 4 additions & 3 deletions helpers/viewer_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def viewer_state_for_request(state: dict, request=None, server_url: str = "") ->
result["vnc_url_raw"] = raw_vnc_url
result["vnc_url"] = normalized_vnc_url
result["vnc_url_rewritten"] = normalized_vnc_url != raw_vnc_url
vnc_ts = result.get("vnc_ts") or result.get("ts")
if vnc_ts:
result["vnc_session_key"] = str(int(float(vnc_ts) * 1000))
# vnc_session_key identifies the upstream session, not write recency.
# Hashing the raw URL means the key changes only when the actual session
# changes (different upstream token), not on every state write.
result["vnc_session_key"] = hashlib.md5(raw_vnc_url.encode()).hexdigest()[:16]
return result
9 changes: 9 additions & 0 deletions webui/camofox-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,21 @@ export const store = createStore("camofox", {
},

async showBrowser() {
// If panel already has a live URL, just reveal it
if (this.vncUrl) {
this.panelVisible = true;
this.panelMinimized = false;
return;
}
if (!this.connected) return;
// If server already reports a visible mode (virtual/headed) and we just
// haven't received the URL via poll yet, force-poll instead of toggling.
if (this.displayMode === "virtual" || this.displayMode === "headed" || this._rawVncUrl) {
this.panelVisible = true;
this.panelMinimized = false;
await this._pollVnc();
return;
}

try {
const res = await fetchApi(`${API_BASE}/camofox_vnc`, {
Expand Down