MCPcopy Index your code
hub / github.com/agent0ai/agent-zero / connectViewer

Function connectViewer

plugins/_browser/webui/browser-store.js:937–1019  ·  view source on GitHub ↗
(options = {})

Source from the content-addressed store, hash-verified

935 },
936
937 async connectViewer(options = {}) {
938 let contextId = "";
939 const requestedBrowserId = this.normalizeBrowserId(options.browserId ?? this.activeBrowserId);
940 const requestedContextId = this.normalizeContextId(
941 options.contextId
942 ?? options.context_id
943 ?? this.contextIdForBrowserId(requestedBrowserId)
944 ?? this.activeBrowserContextId
945 ?? this.contextId
946 );
947 try {
948 contextId = requestedContextId || await this.ensureContextId();
949 } catch (error) {
950 this.connected = false;
951 this.switchingBrowserId = null;
952 this._surfaceSwitching = false;
953 throw error;
954 }
955 if (!contextId) {
956 this.connected = false;
957 this.error = "Could not create a chat for Browser.";
958 this.switchingBrowserId = null;
959 this._surfaceSwitching = false;
960 return;
961 }
962 const previousContextId = this.normalizeContextId(this.contextId);
963 if (previousContextId && previousContextId !== contextId) {
964 try {
965 await websocket.emit("browser_viewer_unsubscribe", { context_id: previousContextId });
966 } catch {}
967 }
968 this.contextId = contextId;
969 const sequence = this._connectSequence + 1;
970 const viewerToken = makeViewerToken();
971 this._connectSequence = sequence;
972 this._viewerToken = viewerToken;
973 this.error = "";
974 await this._bindSocketEvents();
975 if (sequence !== this._connectSequence || viewerToken !== this._viewerToken) {
976 return;
977 }
978 const initialViewport = options.initialViewport || this.currentViewportSize();
979 let response;
980 try {
981 response = await websocket.request(
982 "browser_viewer_subscribe",
983 {
984 context_id: contextId,
985 browser_id: requestedBrowserId,
986 viewer_id: viewerToken,
987 create_browser: Boolean(options.createBrowser || options.create_browser),
988 viewer_transport: this.requestedViewerTransport(),
989 viewport_width: initialViewport?.width,
990 viewport_height: initialViewport?.height,
991 },
992 {
993 timeoutMs: this.browserInstallExpected
994 ? BROWSER_FIRST_INSTALL_TIMEOUT_MS

Callers

nothing calls this directly

Calls 4

requestMethod · 0.80
makeViewerTokenFunction · 0.70
firstOkFunction · 0.70
emitMethod · 0.45

Tested by

no test coverage detected