@myrialabs/clopen 0.4.22 → 0.4.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/backend/chat/stream-manager.ts +41 -13
  2. package/backend/mcp/internal/servers/browser-automation/actions/console.ts +3 -0
  3. package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +3 -0
  4. package/backend/mcp/internal/servers/browser-automation/actions/navigation.ts +5 -0
  5. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +25 -6
  6. package/backend/mcp/internal/servers/browser-automation/actions/types.ts +26 -0
  7. package/backend/mcp/internal/servers/browser-automation/context.ts +43 -19
  8. package/backend/mcp/internal/servers/browser-automation/description.ts +2 -1
  9. package/backend/mcp/internal/servers/browser-automation/format.ts +6 -0
  10. package/backend/mcp/internal/servers/browser-automation/runner.ts +62 -33
  11. package/backend/mcp/internal/servers/browser-automation/schema.ts +1 -1
  12. package/backend/module-shadowing.test.ts +135 -0
  13. package/backend/preview/browser/browser-host-bridge.ts +8 -2
  14. package/backend/preview/browser/browser-interaction-handler.ts +70 -0
  15. package/backend/preview/browser/browser-mcp-control.ts +247 -13
  16. package/backend/preview/browser/browser-preview-service.ts +146 -8
  17. package/backend/preview/browser/browser-video-capture.ts +551 -247
  18. package/backend/preview/browser/scripts/host-bridge.ts +61 -89
  19. package/backend/preview/browser/scripts/video-stream.ts +32 -0
  20. package/backend/preview/browser/types.ts +22 -0
  21. package/backend/preview/index.ts +6 -1
  22. package/backend/ws/preview/browser/interact.ts +16 -2
  23. package/backend/ws/preview/browser/native-ui.ts +22 -1
  24. package/backend/ws/preview/browser/tab-info.ts +20 -2
  25. package/backend/ws/preview/browser/webcodecs.ts +6 -1
  26. package/backend/ws/preview/index.ts +22 -0
  27. package/dist/assets/{index-DH_GO0Lf.js → index-CNDHpafl.js} +1 -1
  28. package/dist/assets/{index-igSQ3JaM.js → index-NF77n8kP.js} +1 -1
  29. package/dist/assets/{main-D-d8SFfW.js → main-BJ0PUVTW.js} +381 -376
  30. package/dist/assets/main-DSirC670.css +1 -0
  31. package/dist/index.html +2 -2
  32. package/package.json +1 -1
  33. package/dist/assets/main-BD3-qPfT.css +0 -1
@@ -56,6 +56,9 @@ export class BrowserPreviewService extends EventEmitter {
56
56
  // Store context menu info for later action execution
57
57
  private contextMenus = new Map<string, BrowserContextMenuInfo>();
58
58
 
59
+ /** Tabs whose page is showing something full screen — see applyFullscreenState. */
60
+ private fullscreenTabs = new Set<string>();
61
+
59
62
  // Project ID for isolation (REQUIRED)
60
63
  private projectId: string;
61
64
 
@@ -105,6 +108,13 @@ export class BrowserPreviewService extends EventEmitter {
105
108
  this.navigationTracker.on('navigation', async (data) => {
106
109
  this.emit('preview:browser-navigation', data);
107
110
 
111
+ // A new document cannot still be full screen: the element that was
112
+ // went with the old one. Left set, the viewer would keep offering an
113
+ // exit for a state that no longer exists.
114
+ if (this.fullscreenTabs.delete(data.sessionId)) {
115
+ this.emit('preview:browser-fullscreen-state', { tabId: data.sessionId, active: false });
116
+ }
117
+
108
118
  // After navigation completes, restart video streaming for the tab
109
119
  // This re-injects the peer script and restarts CDP screencast
110
120
  const { sessionId } = data;
@@ -115,14 +125,21 @@ export class BrowserPreviewService extends EventEmitter {
115
125
  if (this.videoCapture.isStreaming(sessionId)) {
116
126
  const tab = this.getTab(sessionId);
117
127
  if (tab) {
118
- // Restart streaming immediately — page is already navigated
128
+ // Restart streaming immediately — page is already navigated.
129
+ // A failure here is logged rather than swallowed: it used to
130
+ // leave the session marked active over a page that could no
131
+ // longer encode, and the only visible symptom was a preview
132
+ // stuck loading. handleNavigation now clears `isActive` on
133
+ // failure so the next handshake rebuilds from scratch.
119
134
  try {
120
135
  const success = await this.videoCapture.handleNavigation(sessionId, tab);
121
136
  if (success) {
122
137
  this.emit('preview:browser-navigation-streaming-ready', { sessionId });
138
+ } else {
139
+ debug.warn('preview', `Streaming restore failed after navigation on ${sessionId}`);
123
140
  }
124
141
  } catch (error) {
125
- // Silently fail - frontend will request refresh if needed
142
+ debug.warn('preview', `Streaming restore threw after navigation on ${sessionId}:`, error);
126
143
  }
127
144
  }
128
145
  }
@@ -152,6 +169,15 @@ export class BrowserPreviewService extends EventEmitter {
152
169
  this.emit('preview:browser-tab-opened', data);
153
170
  });
154
171
  this.tabManager.on('preview:browser-tab-closed', (data) => {
172
+ // Hooked to the event rather than to closeTab(): the tab manager
173
+ // closes tabs on its own too (browser disconnect, pool teardown, a
174
+ // window the page closed), and those paths never pass through the
175
+ // service. The page is gone either way, and with it the bindings and
176
+ // on-new-document scripts this bookkeeping describes.
177
+ if (data?.tabId) {
178
+ this.videoCapture.disposeTab(data.tabId);
179
+ this.fullscreenTabs.delete(data.tabId);
180
+ }
155
181
  this.emit('preview:browser-tab-closed', data);
156
182
  });
157
183
  this.tabManager.on('preview:browser-tab-switched', (data) => {
@@ -199,6 +225,10 @@ export class BrowserPreviewService extends EventEmitter {
199
225
  void this.restoreEmulatedViewport(data.tabId, data.requestId);
200
226
  return;
201
227
  }
228
+ if (data.kind === 'fullscreen-state') {
229
+ this.applyFullscreenState(data.tabId, data.requestId, data.payload);
230
+ return;
231
+ }
202
232
  this.emit('preview:browser-host-request', data);
203
233
  });
204
234
  this.hostBridge.on('download', (data) => {
@@ -679,6 +709,68 @@ export class BrowserPreviewService extends EventEmitter {
679
709
  }
680
710
  }
681
711
 
712
+ /**
713
+ * Record that a tab's page went full screen (or left it) and tell viewers.
714
+ *
715
+ * The state matters to the viewer because the way out cannot live only in
716
+ * the page: the hint the shim draws is a DOM node the page is free to
717
+ * destroy, and a fullscreen Chrome granted from its own C++ leaves no hint
718
+ * at all. Knowing lets the viewer render an exit control of its own, which
719
+ * nothing in the page can reach.
720
+ */
721
+ private applyFullscreenState(tabId: string, requestId: string, payload: unknown): void {
722
+ const active = !!(payload as { active?: boolean } | null)?.active;
723
+
724
+ if (active) this.fullscreenTabs.add(tabId);
725
+ else this.fullscreenTabs.delete(tabId);
726
+
727
+ debug.log('preview', `🖥️ Tab ${tabId} full screen: ${active}`);
728
+ this.emit('preview:browser-fullscreen-state', { tabId, active });
729
+ this.hostBridge.respond(requestId, { ok: true });
730
+ }
731
+
732
+ /** Whether a tab's page currently has something full screen. */
733
+ isPageFullscreen(tabId: string): boolean {
734
+ return this.fullscreenTabs.has(tabId);
735
+ }
736
+
737
+ /**
738
+ * Force a tab out of full screen, from outside the page.
739
+ *
740
+ * Every frame is asked, not just the main one: a video in an embed goes
741
+ * full screen inside its own document, and that frame is the only one that
742
+ * holds the state. The emulated viewport is re-asserted afterwards because
743
+ * a fullscreen Chrome granted itself collapses the composited surface and
744
+ * leaves it collapsed — the zoomed, cropped preview that looked like the
745
+ * fullscreen had never ended.
746
+ */
747
+ async exitPageFullscreen(tabId: string): Promise<boolean> {
748
+ const tab = this.getTab(tabId);
749
+ if (!tab?.page || tab.page.isClosed()) return false;
750
+
751
+ await Promise.all(
752
+ tab.page.frames().map((frame) =>
753
+ frame
754
+ .evaluate(() => {
755
+ (window as any).__clopenFullscreen?.forceExit();
756
+ })
757
+ .catch(() => {
758
+ // Detached or navigating — nothing of ours is left in it.
759
+ })
760
+ )
761
+ );
762
+
763
+ this.fullscreenTabs.delete(tabId);
764
+ this.emit('preview:browser-fullscreen-state', { tabId, active: false });
765
+
766
+ const { width, height } = getViewportDimensions(tab.deviceSize, tab.rotation);
767
+ const restored = await this.videoCapture.updateViewport(tabId, tab, width, height);
768
+ if (!restored) await tab.page.setViewport({ width, height }).catch(() => {});
769
+
770
+ debug.log('preview', `🖥️ Forced tab ${tabId} out of full screen`);
771
+ return true;
772
+ }
773
+
682
774
  async updateWebCodecsViewport(tabId: string, width: number, height: number): Promise<boolean> {
683
775
  const tab = this.getTab(tabId);
684
776
  if (!tab) {
@@ -1056,6 +1148,10 @@ class BrowserPreviewServiceManager {
1056
1148
  ws.emit.project(projectId, 'preview:browser-viewport-changed', { ...data, projectId });
1057
1149
  });
1058
1150
 
1151
+ service.on('preview:browser-fullscreen-state', (data) => {
1152
+ ws.emit.project(projectId, 'preview:browser-fullscreen-state', { ...data, projectId });
1153
+ });
1154
+
1059
1155
  // Forward live tab metadata (title, favicon, back/forward availability)
1060
1156
  service.on('preview:browser-tab-meta', (data) => {
1061
1157
  ws.emit.project(projectId, 'preview:browser-tab-meta', { ...data, projectId });
@@ -1202,26 +1298,68 @@ class BrowserPreviewServiceManager {
1202
1298
  });
1203
1299
  });
1204
1300
 
1301
+ browserMcpControl.on('control-focus', (data) => {
1302
+ ws.emit.project(data.projectId, 'preview:browser-mcp-control-focus', {
1303
+ browserTabId: data.browserTabId,
1304
+ projectId: data.projectId,
1305
+ timestamp: data.timestamp
1306
+ });
1307
+ });
1308
+
1309
+ // Cursor events go to the owning project when it is known. Tab ids repeat
1310
+ // across projects, so a broadcast could draw the agent's pointer over a
1311
+ // same-numbered tab somewhere the agent has never been. The broadcast is
1312
+ // kept only for the case where ownership can't be resolved.
1205
1313
  browserMcpControl.on('cursor-position', (data) => {
1206
- emitToActiveProjects('preview:browser-mcp-cursor-position', {
1314
+ const payload = {
1207
1315
  sessionId: data.tabId,
1208
1316
  x: data.x,
1209
1317
  y: data.y,
1210
1318
  pressed: data.pressed ?? false,
1211
1319
  timestamp: data.timestamp,
1212
- source: 'mcp'
1213
- });
1320
+ source: 'mcp' as const
1321
+ };
1322
+
1323
+ if (data.projectId) {
1324
+ ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-position', payload);
1325
+ return;
1326
+ }
1327
+ emitToActiveProjects('preview:browser-mcp-cursor-position', payload);
1214
1328
  });
1215
1329
 
1216
1330
  browserMcpControl.on('cursor-click', (data) => {
1217
- emitToActiveProjects('preview:browser-mcp-cursor-click', {
1331
+ const payload = {
1218
1332
  sessionId: data.tabId,
1219
1333
  x: data.x,
1220
1334
  y: data.y,
1221
1335
  button: data.button ?? 'left',
1222
1336
  timestamp: data.timestamp,
1223
- source: 'mcp'
1224
- });
1337
+ source: 'mcp' as const
1338
+ };
1339
+
1340
+ if (data.projectId) {
1341
+ ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-click', payload);
1342
+ return;
1343
+ }
1344
+ emitToActiveProjects('preview:browser-mcp-cursor-click', payload);
1345
+ });
1346
+
1347
+ // What the agent is doing, for the caption beside its cursor. Same
1348
+ // project-scoping rule as the cursor itself, and the same fallback: an
1349
+ // event nobody can attribute is better broadcast than dropped, since the
1350
+ // alternative is a pointer left describing an action that has finished.
1351
+ browserMcpControl.on('activity', (data) => {
1352
+ const payload = {
1353
+ sessionId: data.tabId,
1354
+ label: data.label,
1355
+ timestamp: data.timestamp
1356
+ };
1357
+
1358
+ if (data.projectId) {
1359
+ ws.emit.project(data.projectId, 'preview:browser-mcp-activity', payload);
1360
+ return;
1361
+ }
1362
+ emitToActiveProjects('preview:browser-mcp-activity', payload);
1225
1363
  });
1226
1364
 
1227
1365
  browserMcpControl.on('test-completed', (data) => {