Skip to content

Agent Zero AI framework

Create chat context for browser launches

Alessandroad76578c475e
diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.js
index a848a4313..b7db50928 100644
--- a/plugins/_browser/webui/browser-store.js
+++ b/plugins/_browser/webui/browser-store.js
@@ -96,6 +96,7 @@ const model = {
_surfaceOpenedAt: 0,
_connectSequence: 0,
_viewerToken: "",
+ _contextCreatePromise: null,
extensionMenuOpen: false,
extensionInstallUrl: "",
extensionActionLoading: false,
@@ -204,6 +205,47 @@ const model = {
return globalThis.getContext?.() || urlContext || selectedChat || "";
},
+ async ensureContextId() {
+ const existingContextId = String(this.resolveContextId() || "").trim();
+ if (existingContextId) {
+ this.contextId = existingContextId;
+ return existingContextId;
+ }
+
+ if (!this._contextCreatePromise) {
+ this._contextCreatePromise = this.createChatContextForBrowser();
+ }
+
+ try {
+ this.contextId = await this._contextCreatePromise;
+ return this.contextId;
+ } finally {
+ this._contextCreatePromise = null;
+ }
+ },
+
+ async createChatContextForBrowser() {
+ const response = await callJsonApi("/chat_create", {
+ current_context: this.resolveContextId() || "",
+ });
+ const selectedContextId = String(this.resolveContextId() || "").trim();
+ if (selectedContextId) return selectedContextId;
+
+ const contextId = String(response?.ctxid || "").trim();
+ if (!response?.ok || !contextId) {
+ throw new Error(response?.error || "Could not create a chat for Browser.");
+ }
+
+ if (typeof globalThis.setContext === "function") {
+ globalThis.setContext(contextId);
+ } else {
+ const chatsStore = globalThis.Alpine?.store?.("chats");
+ chatsStore?.setSelected?.(contextId);
+ }
+
+ return contextId;
+ },
+
async openExtensionsSettings() {
if (!pluginSettingsStore?.openConfig) {
this.error = "Browser settings are unavailable.";
@@ -386,8 +428,8 @@ const model = {
} else {
this.setupCanvasSurface(element);
}
- this.contextId = this.resolveContextId();
try {
+ await this.ensureContextId();
await this.refreshStatus();
const viewport = await this.waitForSurfaceViewport();
this.resetRenderedFrameIfViewportChanged(viewport, requestedBrowserId);
@@ -475,9 +517,18 @@ const model = {
},
async connectViewer(options = {}) {
- if (!this.contextId) {
+ let contextId = "";
+ try {
+ contextId = await this.ensureContextId();
+ } catch (error) {
+ this.connected = false;
+ this.switchingBrowserId = null;
+ this._surfaceSwitching = false;
+ throw error;
+ }
+ if (!contextId) {
this.connected = false;
- this.error = "No active chat context is selected.";
+ this.error = "Could not create a chat for Browser.";
this.switchingBrowserId = null;
this._surfaceSwitching = false;
return;
@@ -638,6 +689,7 @@ const model = {
this.commandInFlight = true;
const previousActiveBrowserId = this.activeBrowserId;
try {
+ await this.ensureContextId();
const response = await websocket.request(
"browser_viewer_command",
{
diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.py
index fea453201..bcbec0f67 100644
--- a/tests/test_browser_agent_regressions.py
+++ b/tests/test_browser_agent_regressions.py
@@ -347,6 +347,19 @@ def test_browser_viewer_allows_slow_extension_startup():
assert "Installing Chromium for the first Browser run" in js
+def test_browser_viewer_creates_chat_when_no_context_is_selected():
+ js = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "browser-store.js").read_text(
+ encoding="utf-8"
+ )
+
+ assert "async ensureContextId()" in js
+ assert "async createChatContextForBrowser()" in js
+ assert 'callJsonApi("/chat_create"' in js
+ assert "globalThis.setContext(contextId)" in js
+ assert "await this.ensureContextId();" in js
+ assert "No active chat context is selected." not in js
+
+
def test_browser_canvas_startup_waits_for_raw_viewport_settle():
js = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "browser-store.js").read_text(
encoding="utf-8"