@myrialabs/clopen 0.4.23 → 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.
- package/backend/chat/stream-manager.ts +41 -13
- package/backend/mcp/internal/servers/browser-automation/actions/console.ts +3 -0
- package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +3 -0
- package/backend/mcp/internal/servers/browser-automation/actions/navigation.ts +5 -0
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +8 -0
- package/backend/mcp/internal/servers/browser-automation/actions/types.ts +10 -0
- package/backend/mcp/internal/servers/browser-automation/context.ts +6 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +1 -1
- package/backend/mcp/internal/servers/browser-automation/runner.ts +21 -1
- package/backend/mcp/internal/servers/browser-automation/schema.ts +1 -1
- package/backend/module-shadowing.test.ts +135 -0
- package/backend/preview/browser/browser-host-bridge.ts +8 -2
- package/backend/preview/browser/browser-interaction-handler.ts +70 -0
- package/backend/preview/browser/browser-mcp-control.ts +247 -13
- package/backend/preview/browser/browser-preview-service.ts +146 -8
- package/backend/preview/browser/browser-video-capture.ts +551 -247
- package/backend/preview/browser/scripts/host-bridge.ts +61 -89
- package/backend/preview/browser/scripts/video-stream.ts +32 -0
- package/backend/preview/browser/types.ts +22 -0
- package/backend/preview/index.ts +6 -1
- package/backend/ws/preview/browser/interact.ts +16 -2
- package/backend/ws/preview/browser/native-ui.ts +22 -1
- package/backend/ws/preview/browser/tab-info.ts +20 -2
- package/backend/ws/preview/browser/webcodecs.ts +6 -1
- package/backend/ws/preview/index.ts +22 -0
- package/dist/assets/{index-DH_GO0Lf.js → index-CNDHpafl.js} +1 -1
- package/dist/assets/{index-igSQ3JaM.js → index-NF77n8kP.js} +1 -1
- package/dist/assets/{main-D-d8SFfW.js → main-BJ0PUVTW.js} +381 -376
- package/dist/assets/main-DSirC670.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/dist/assets/main-BD3-qPfT.css +0 -1
|
@@ -41,8 +41,32 @@ export interface McpControlEvent {
|
|
|
41
41
|
timestamp: number;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Which of a project's locked tabs the agent is working on right now.
|
|
46
|
+
*
|
|
47
|
+
* A session accumulates tabs, and a lock says only "hands off". With several
|
|
48
|
+
* locked at once that leaves the user unable to tell where the agent actually
|
|
49
|
+
* is — they can watch any tab freely, but not know which one to watch.
|
|
50
|
+
* `browserTabId` is null when the project has no focused tab any more.
|
|
51
|
+
*/
|
|
52
|
+
export interface McpFocusEvent {
|
|
53
|
+
browserTabId: string | null;
|
|
54
|
+
projectId: string;
|
|
55
|
+
chatSessionId?: string;
|
|
56
|
+
timestamp: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Answers "does this chat session still have a stream running?". */
|
|
60
|
+
export type SessionLivenessProbe = (chatSessionId: string) => boolean;
|
|
61
|
+
|
|
44
62
|
export interface McpCursorEvent {
|
|
45
63
|
tabId: string;
|
|
64
|
+
/**
|
|
65
|
+
* Owning project. Tab ids repeat across projects (`tab-1` exists in every
|
|
66
|
+
* one), so a cursor broadcast to all of them could be drawn over a
|
|
67
|
+
* same-numbered tab in a project the agent has never touched.
|
|
68
|
+
*/
|
|
69
|
+
projectId?: string;
|
|
46
70
|
x: number;
|
|
47
71
|
y: number;
|
|
48
72
|
/** Mouse button held down — the difference between a move and a drag. */
|
|
@@ -53,6 +77,7 @@ export interface McpCursorEvent {
|
|
|
53
77
|
|
|
54
78
|
export interface McpClickEvent {
|
|
55
79
|
tabId: string;
|
|
80
|
+
projectId?: string;
|
|
56
81
|
x: number;
|
|
57
82
|
y: number;
|
|
58
83
|
button?: 'left' | 'right' | 'middle';
|
|
@@ -60,6 +85,24 @@ export interface McpClickEvent {
|
|
|
60
85
|
source: 'mcp';
|
|
61
86
|
}
|
|
62
87
|
|
|
88
|
+
/**
|
|
89
|
+
* What the agent is doing on a tab, in the user's words.
|
|
90
|
+
*
|
|
91
|
+
* The pointer alone says "something else is driving this" and nothing more,
|
|
92
|
+
* which is the wrong amount of information for a run that can hold a tab for
|
|
93
|
+
* minutes: a page changing on its own with a nameless cursor on it reads as a
|
|
94
|
+
* glitch. `label` is a two-or-three-word gloss of the action about to run —
|
|
95
|
+
* "Typing", "Clicking", "Navigating" — and rides next to the cursor.
|
|
96
|
+
*
|
|
97
|
+
* `null` means the agent still holds the tab but is between actions.
|
|
98
|
+
*/
|
|
99
|
+
export interface McpActivityEvent {
|
|
100
|
+
tabId: string;
|
|
101
|
+
projectId?: string;
|
|
102
|
+
label: string | null;
|
|
103
|
+
timestamp: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
63
106
|
export class BrowserMcpControl extends EventEmitter {
|
|
64
107
|
/** Tab → ownership info (which chat session controls it) */
|
|
65
108
|
private tabOwnership = new Map<string, TabOwnershipInfo>();
|
|
@@ -67,6 +110,23 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
67
110
|
/** Chat session → set of tab IDs it controls */
|
|
68
111
|
private sessionTabs = new Map<string, Set<string>>();
|
|
69
112
|
|
|
113
|
+
/** Project → the tab its controlling agent is pointed at right now. */
|
|
114
|
+
private focusedTabs = new Map<string, string>();
|
|
115
|
+
|
|
116
|
+
/** Tab → what the agent is doing on it, for the cursor's caption. */
|
|
117
|
+
private tabActivity = new Map<string, string>();
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether the stream that owns a lock is still running.
|
|
121
|
+
*
|
|
122
|
+
* Registered by the chat stream manager. Without it a tool call that lands
|
|
123
|
+
* after its stream ended — the remote-MCP transports resolve their session
|
|
124
|
+
* from "most recent active stream", so a late call can name a session that
|
|
125
|
+
* has already been released — filed a lock under an owner nobody would ever
|
|
126
|
+
* release, and the tab stayed locked until the server restarted.
|
|
127
|
+
*/
|
|
128
|
+
private livenessProbe: SessionLivenessProbe | null = null;
|
|
129
|
+
|
|
70
130
|
// Pending tab requests (keyed by request type + timestamp)
|
|
71
131
|
private pendingTabRequests = new Map<string, PendingTabRequest>();
|
|
72
132
|
private requestCounter = 0;
|
|
@@ -93,6 +153,47 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
93
153
|
debug.log('mcp', '🔗 Browser MCP Control initialized with event-based tab tracking');
|
|
94
154
|
}
|
|
95
155
|
|
|
156
|
+
/**
|
|
157
|
+
* Teach this manager which chat sessions are still streaming.
|
|
158
|
+
*
|
|
159
|
+
* Called once by the chat stream manager. Absent, every session reads as
|
|
160
|
+
* live — the previous behaviour, which is the safe default for anything
|
|
161
|
+
* that constructs this without a chat layer (tests, tooling).
|
|
162
|
+
*/
|
|
163
|
+
setSessionLivenessProbe(probe: SessionLivenessProbe): void {
|
|
164
|
+
this.livenessProbe = probe;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Whether a chat session is still streaming (true when no probe is set). */
|
|
168
|
+
isSessionLive(chatSessionId: string): boolean {
|
|
169
|
+
return this.livenessProbe ? this.livenessProbe(chatSessionId) : true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Release every tab whose owning session has stopped streaming.
|
|
174
|
+
*
|
|
175
|
+
* The belt to `releaseSession`'s braces. Release is driven by the stream's
|
|
176
|
+
* terminal events, so anything that never reaches one — a lock acquired
|
|
177
|
+
* during the teardown window, an interrupt that took an unusual path —
|
|
178
|
+
* leaves ownership behind with nothing to collect it. Sweeping is cheap
|
|
179
|
+
* (the map holds a handful of entries) and makes a stuck lock recover on
|
|
180
|
+
* the next stream event or tab query instead of at the next restart.
|
|
181
|
+
*/
|
|
182
|
+
releaseOrphans(): number {
|
|
183
|
+
if (!this.livenessProbe) return 0;
|
|
184
|
+
|
|
185
|
+
const orphans = [...this.tabOwnership.entries()]
|
|
186
|
+
.filter(([, info]) => !this.isSessionLive(info.chatSessionId))
|
|
187
|
+
.map(([tabId]) => tabId);
|
|
188
|
+
|
|
189
|
+
for (const tabId of orphans) {
|
|
190
|
+
debug.warn('mcp', `🧹 Releasing orphaned tab ${tabId} — its chat session is no longer streaming`);
|
|
191
|
+
this.releaseTab(tabId);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return orphans.length;
|
|
195
|
+
}
|
|
196
|
+
|
|
96
197
|
/**
|
|
97
198
|
* Handle tab destroyed event
|
|
98
199
|
* Auto-release control for the destroyed tab only
|
|
@@ -224,6 +325,11 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
224
325
|
sessionSet.delete(browserTabId);
|
|
225
326
|
sessionSet.add(browserTabId);
|
|
226
327
|
debug.log('mcp', `🔀 Promoted tab ${browserTabId} to end of session ${chatSessionId.slice(0, 8)} set`);
|
|
328
|
+
|
|
329
|
+
// The agent just changed where it is working; the focus marker is
|
|
330
|
+
// what tells the user that, so it moves with the promotion.
|
|
331
|
+
const projectId = this.tabOwnership.get(browserTabId)?.projectId;
|
|
332
|
+
if (projectId) this.focusTab(browserTabId, chatSessionId, projectId);
|
|
227
333
|
}
|
|
228
334
|
}
|
|
229
335
|
|
|
@@ -235,12 +341,21 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
235
341
|
* - If the tab is free → acquire and add to session's controlled set
|
|
236
342
|
*/
|
|
237
343
|
acquireControl(browserTabId: string, chatSessionId: string, projectId: string): boolean {
|
|
344
|
+
// A cancelled stream can still have a tool call in flight. Letting it
|
|
345
|
+
// take the lock re-locks a tab that was just handed back, and files the
|
|
346
|
+
// lock under an owner whose release already ran.
|
|
347
|
+
if (!this.isSessionLive(chatSessionId)) {
|
|
348
|
+
debug.warn('mcp', `❌ Session ${chatSessionId.slice(0, 8)} is no longer streaming, refusing control of ${browserTabId}`);
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
|
|
238
352
|
// Check existing ownership
|
|
239
353
|
const existingOwner = this.tabOwnership.get(browserTabId);
|
|
240
354
|
|
|
241
355
|
if (existingOwner) {
|
|
242
356
|
// Same session already owns it → idempotent success
|
|
243
357
|
if (existingOwner.chatSessionId === chatSessionId) {
|
|
358
|
+
this.focusTab(browserTabId, chatSessionId, projectId);
|
|
244
359
|
return true;
|
|
245
360
|
}
|
|
246
361
|
// Different session owns it → denied
|
|
@@ -266,11 +381,65 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
266
381
|
|
|
267
382
|
// Emit control start event to frontend
|
|
268
383
|
this.emitControlStart(browserTabId, chatSessionId, projectId);
|
|
384
|
+
this.focusTab(browserTabId, chatSessionId, projectId);
|
|
269
385
|
|
|
270
386
|
debug.log('mcp', `🎮 Session ${chatSessionId.slice(0, 8)} acquired tab: ${browserTabId} (total: ${sessionSet.size} tabs)`);
|
|
271
387
|
return true;
|
|
272
388
|
}
|
|
273
389
|
|
|
390
|
+
// ============================================================================
|
|
391
|
+
// Focus
|
|
392
|
+
// ============================================================================
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Mark the tab the agent is acting on. Idempotent and quiet when unchanged —
|
|
396
|
+
* it is called on every action, and only a change is worth telling the UI.
|
|
397
|
+
*/
|
|
398
|
+
focusTab(browserTabId: string, chatSessionId: string, projectId: string): void {
|
|
399
|
+
if (this.focusedTabs.get(projectId) === browserTabId) return;
|
|
400
|
+
|
|
401
|
+
this.focusedTabs.set(projectId, browserTabId);
|
|
402
|
+
this.emitFocus(browserTabId, projectId, chatSessionId);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** The tab a project's agent is currently pointed at, if any. */
|
|
406
|
+
getFocusedTab(projectId: string): string | null {
|
|
407
|
+
return this.focusedTabs.get(projectId) ?? null;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Re-derive a project's focus after ownership changed.
|
|
412
|
+
*
|
|
413
|
+
* Falls back to another tab the *same* session still holds, so a batch that
|
|
414
|
+
* closed the tab it was working on lands the marker where the work went
|
|
415
|
+
* rather than nowhere — and never onto a tab a different session owns,
|
|
416
|
+
* which would point the user at the wrong agent. Emits nothing when the
|
|
417
|
+
* focused tab is still held, so releasing several tabs at once produces one
|
|
418
|
+
* update rather than a burst.
|
|
419
|
+
*/
|
|
420
|
+
private refreshFocus(projectId: string | undefined): void {
|
|
421
|
+
if (!projectId) return;
|
|
422
|
+
|
|
423
|
+
const focused = this.focusedTabs.get(projectId);
|
|
424
|
+
if (!focused) return;
|
|
425
|
+
|
|
426
|
+
const owner = this.tabOwnership.get(focused);
|
|
427
|
+
if (owner && owner.projectId === projectId) return; // Still held — nothing to say.
|
|
428
|
+
|
|
429
|
+
const successor = [...this.tabOwnership.entries()]
|
|
430
|
+
.reverse()
|
|
431
|
+
.find(([, info]) => info.projectId === projectId);
|
|
432
|
+
|
|
433
|
+
if (successor) {
|
|
434
|
+
this.focusedTabs.set(projectId, successor[0]);
|
|
435
|
+
this.emitFocus(successor[0], projectId, successor[1].chatSessionId);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
this.focusedTabs.delete(projectId);
|
|
440
|
+
this.emitFocus(null, projectId);
|
|
441
|
+
}
|
|
442
|
+
|
|
274
443
|
// ============================================================================
|
|
275
444
|
// Control Release
|
|
276
445
|
// ============================================================================
|
|
@@ -295,8 +464,13 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
295
464
|
}
|
|
296
465
|
}
|
|
297
466
|
|
|
467
|
+
// Before the control-end, so the caption cannot outlive the cursor it
|
|
468
|
+
// belongs to on a viewer that processes the two in order.
|
|
469
|
+
this.emitActivity(browserTabId, null, ownership.projectId);
|
|
470
|
+
|
|
298
471
|
// Emit control end event to frontend
|
|
299
472
|
this.emitControlEnd(browserTabId, ownership.projectId);
|
|
473
|
+
this.refreshFocus(ownership.projectId);
|
|
300
474
|
|
|
301
475
|
debug.log('mcp', `🎮 Released tab: ${browserTabId} (was owned by session ${ownership.chatSessionId.slice(0, 8)})`);
|
|
302
476
|
}
|
|
@@ -307,23 +481,31 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
307
481
|
*/
|
|
308
482
|
releaseSession(chatSessionId: string): void {
|
|
309
483
|
const sessionSet = this.sessionTabs.get(chatSessionId);
|
|
310
|
-
|
|
311
|
-
this.sessionTabs.delete(chatSessionId);
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
484
|
+
this.sessionTabs.delete(chatSessionId);
|
|
314
485
|
|
|
315
|
-
const tabIds = Array.from(sessionSet);
|
|
316
|
-
|
|
486
|
+
const tabIds = Array.from(sessionSet ?? []);
|
|
487
|
+
if (tabIds.length > 0) {
|
|
488
|
+
debug.log('mcp', `🎮 Releasing ${tabIds.length} tabs for session ${chatSessionId.slice(0, 8)}`);
|
|
489
|
+
|
|
490
|
+
const projects = new Set<string>();
|
|
491
|
+
for (const tabId of tabIds) {
|
|
492
|
+
const ownership = this.tabOwnership.get(tabId);
|
|
493
|
+
this.tabOwnership.delete(tabId);
|
|
494
|
+
this.emitActivity(tabId, null, ownership?.projectId);
|
|
495
|
+
this.emitControlEnd(tabId, ownership?.projectId);
|
|
496
|
+
if (ownership?.projectId) projects.add(ownership.projectId);
|
|
497
|
+
}
|
|
317
498
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
this.
|
|
321
|
-
this.emitControlEnd(tabId, ownership?.projectId);
|
|
322
|
-
}
|
|
499
|
+
// After every deletion, so the successor search sees the final state
|
|
500
|
+
// rather than tabs that are about to go too.
|
|
501
|
+
for (const projectId of projects) this.refreshFocus(projectId);
|
|
323
502
|
|
|
324
|
-
|
|
503
|
+
debug.log('mcp', `🎮 Session ${chatSessionId.slice(0, 8)} fully released`);
|
|
504
|
+
}
|
|
325
505
|
|
|
326
|
-
|
|
506
|
+
// A tool call that acquired during this session's teardown is filed
|
|
507
|
+
// under an owner that is already gone; nothing else would collect it.
|
|
508
|
+
this.releaseOrphans();
|
|
327
509
|
}
|
|
328
510
|
|
|
329
511
|
/**
|
|
@@ -344,11 +526,18 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
344
526
|
forceReleaseAll(): void {
|
|
345
527
|
// Emit control-end for all controlled tabs
|
|
346
528
|
for (const [tabId, info] of this.tabOwnership) {
|
|
529
|
+
this.emitActivity(tabId, null, info.projectId);
|
|
347
530
|
this.emitControlEnd(tabId, info.projectId);
|
|
348
531
|
}
|
|
349
532
|
|
|
533
|
+
for (const projectId of this.focusedTabs.keys()) {
|
|
534
|
+
this.emitFocus(null, projectId);
|
|
535
|
+
}
|
|
536
|
+
|
|
350
537
|
this.tabOwnership.clear();
|
|
351
538
|
this.sessionTabs.clear();
|
|
539
|
+
this.focusedTabs.clear();
|
|
540
|
+
this.tabActivity.clear();
|
|
352
541
|
|
|
353
542
|
debug.log('mcp', '🧹 Force released all MCP control');
|
|
354
543
|
}
|
|
@@ -363,6 +552,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
363
552
|
emitCursorPosition(tabId: string, x: number, y: number, pressed = false): void {
|
|
364
553
|
const event: McpCursorEvent = {
|
|
365
554
|
tabId,
|
|
555
|
+
projectId: this.tabOwnership.get(tabId)?.projectId,
|
|
366
556
|
x,
|
|
367
557
|
y,
|
|
368
558
|
pressed,
|
|
@@ -379,6 +569,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
379
569
|
emitCursorClick(tabId: string, x: number, y: number, button: 'left' | 'right' | 'middle' = 'left'): void {
|
|
380
570
|
const event: McpClickEvent = {
|
|
381
571
|
tabId,
|
|
572
|
+
projectId: this.tabOwnership.get(tabId)?.projectId,
|
|
382
573
|
x,
|
|
383
574
|
y,
|
|
384
575
|
button,
|
|
@@ -389,6 +580,36 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
389
580
|
this.emit('cursor-click', event);
|
|
390
581
|
}
|
|
391
582
|
|
|
583
|
+
/**
|
|
584
|
+
* Say what the agent is doing on a tab.
|
|
585
|
+
*
|
|
586
|
+
* Repeats are dropped: a batch of eight `type` actions would otherwise
|
|
587
|
+
* relabel the cursor eight times with the same word, and every one of those
|
|
588
|
+
* is a re-render on every viewer.
|
|
589
|
+
*/
|
|
590
|
+
emitActivity(tabId: string, label: string | null, projectId?: string): void {
|
|
591
|
+
if ((this.tabActivity.get(tabId) ?? null) === label) return;
|
|
592
|
+
|
|
593
|
+
if (label === null) this.tabActivity.delete(tabId);
|
|
594
|
+
else this.tabActivity.set(tabId, label);
|
|
595
|
+
|
|
596
|
+
const event: McpActivityEvent = {
|
|
597
|
+
tabId,
|
|
598
|
+
// Explicit on release, where ownership has already been dropped and
|
|
599
|
+
// there is nothing left to look the project up in.
|
|
600
|
+
projectId: projectId ?? this.tabOwnership.get(tabId)?.projectId,
|
|
601
|
+
label,
|
|
602
|
+
timestamp: Date.now()
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
this.emit('activity', event);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** What the agent last said it was doing on a tab, for a late-joining viewer. */
|
|
609
|
+
getActivity(tabId: string): string | null {
|
|
610
|
+
return this.tabActivity.get(tabId) ?? null;
|
|
611
|
+
}
|
|
612
|
+
|
|
392
613
|
/**
|
|
393
614
|
* Emit test completed event (hide virtual cursor)
|
|
394
615
|
*/
|
|
@@ -430,6 +651,19 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
430
651
|
|
|
431
652
|
debug.log('mcp', `📢 Emitted mcp:control-end for tab: ${browserTabId}`);
|
|
432
653
|
}
|
|
654
|
+
|
|
655
|
+
private emitFocus(browserTabId: string | null, projectId: string, chatSessionId?: string): void {
|
|
656
|
+
const event: McpFocusEvent = {
|
|
657
|
+
browserTabId,
|
|
658
|
+
projectId,
|
|
659
|
+
chatSessionId,
|
|
660
|
+
timestamp: Date.now()
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
this.emit('control-focus', event);
|
|
664
|
+
|
|
665
|
+
debug.log('mcp', `📢 Emitted mcp:control-focus for ${projectId}: ${browserTabId ?? 'none'}`);
|
|
666
|
+
}
|
|
433
667
|
}
|
|
434
668
|
|
|
435
669
|
// Singleton instance
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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) => {
|