@myrialabs/clopen 0.4.24 → 0.4.26
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/engine/README.md +18 -0
- package/backend/engine/adapters/claude/message-converter.test.ts +36 -0
- package/backend/engine/adapters/claude/message-converter.ts +40 -4
- package/backend/engine/adapters/claude/stream.ts +68 -3
- package/backend/engine/adapters/claude/workflow-transcript.test.ts +65 -0
- package/backend/engine/adapters/claude/workflow-transcript.ts +248 -0
- package/backend/engine/adapters/coordination-tool-mapping.test.ts +17 -0
- package/backend/engine/adapters/copilot/message-converter.ts +20 -1
- package/backend/engine/adapters/opencode/message-converter.test.ts +80 -0
- package/backend/engine/adapters/opencode/message-converter.ts +32 -0
- package/backend/engine/adapters/opencode/stream.ts +29 -23
- package/backend/engine/adapters/qwen/message-converter.ts +5 -8
- package/backend/engine/docs/adapter-contract.md +29 -1
- package/backend/engine/docs/adding-an-engine.md +8 -1
- package/backend/engine/docs/frontend-and-chat.md +32 -1
- package/backend/engine/docs/lessons-learned.md +49 -2
- package/backend/engine/docs/quick-reference.md +2 -0
- package/backend/preview/browser/browser-interaction-handler.ts +25 -38
- package/backend/preview/browser/browser-mcp-control.ts +130 -54
- package/backend/preview/browser/browser-preview-service.ts +43 -62
- package/backend/preview/browser/browser-tab-manager.ts +25 -1
- package/backend/ws/preview/browser/console.ts +12 -10
- package/backend/ws/preview/browser/interact.ts +24 -8
- package/backend/ws/preview/browser/native-ui.ts +13 -9
- package/backend/ws/preview/browser/tab-info.ts +31 -10
- package/backend/ws/preview/index.ts +7 -9
- package/backend/ws/snapshot/content.ts +85 -0
- package/backend/ws/snapshot/index.ts +4 -1
- package/dist/assets/{index-NF77n8kP.js → index-B6QGUN03.js} +1 -1
- package/dist/assets/{index-CNDHpafl.js → index-DYa-GDsb.js} +1 -1
- package/dist/assets/main-Dh-MTGNR.css +1 -0
- package/dist/assets/main-Dznkfxsd.js +627 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/unified/index.ts +7 -0
- package/shared/types/unified/tool.ts +57 -2
- package/dist/assets/main-BJ0PUVTW.js +0 -621
- package/dist/assets/main-DSirC670.css +0 -1
|
@@ -42,15 +42,20 @@ export interface McpControlEvent {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Whether an agent is acting on this tab right now.
|
|
46
46
|
*
|
|
47
47
|
* A session accumulates tabs, and a lock says only "hands off". With several
|
|
48
48
|
* locked at once that leaves the user unable to tell where the agent actually
|
|
49
49
|
* is — they can watch any tab freely, but not know which one to watch.
|
|
50
|
-
*
|
|
50
|
+
*
|
|
51
|
+
* Stated per tab rather than as "the project's focused tab" because two runs
|
|
52
|
+
* can be going at once in one project, and each has a tab of its own to point
|
|
53
|
+
* at. `focused: false` retracts an earlier mark, so a viewer never has to infer
|
|
54
|
+
* that one tab losing focus means some other tab gained it.
|
|
51
55
|
*/
|
|
52
56
|
export interface McpFocusEvent {
|
|
53
|
-
browserTabId: string
|
|
57
|
+
browserTabId: string;
|
|
58
|
+
focused: boolean;
|
|
54
59
|
projectId: string;
|
|
55
60
|
chatSessionId?: string;
|
|
56
61
|
timestamp: number;
|
|
@@ -110,12 +115,33 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
110
115
|
/** Chat session → set of tab IDs it controls */
|
|
111
116
|
private sessionTabs = new Map<string, Set<string>>();
|
|
112
117
|
|
|
113
|
-
/**
|
|
118
|
+
/**
|
|
119
|
+
* Chat session → the tab that session's agent is pointed at right now.
|
|
120
|
+
*
|
|
121
|
+
* Keyed by session rather than by project because a project can have more
|
|
122
|
+
* than one run going at once, and a single per-project marker made those two
|
|
123
|
+
* agents fight over it — the pointer flickered between their tabs and named
|
|
124
|
+
* neither. A tab can only be held by one session (`acquireControl` refuses
|
|
125
|
+
* the second), so "is this tab focused" still has exactly one answer, and no
|
|
126
|
+
* viewer has to know which chat is open to get it.
|
|
127
|
+
*/
|
|
114
128
|
private focusedTabs = new Map<string, string>();
|
|
115
129
|
|
|
116
130
|
/** Tab → what the agent is doing on it, for the cursor's caption. */
|
|
117
131
|
private tabActivity = new Map<string, string>();
|
|
118
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Tab → the project it was last owned by, kept after the lock is gone.
|
|
135
|
+
*
|
|
136
|
+
* Ownership is the only place a tab's project was recorded, so anything
|
|
137
|
+
* emitted after a release — the tail of a batch that was mid-action when the
|
|
138
|
+
* user pressed stop — had no project to address and fell back to a broadcast
|
|
139
|
+
* across every open project. On a shared server that draws one project's
|
|
140
|
+
* agent onto another project's preview. Remembering the last owner means an
|
|
141
|
+
* event can always be addressed, and the broadcast path could be deleted.
|
|
142
|
+
*/
|
|
143
|
+
private lastProjectByTab = new Map<string, string>();
|
|
144
|
+
|
|
119
145
|
/**
|
|
120
146
|
* Whether the stream that owns a lock is still running.
|
|
121
147
|
*
|
|
@@ -200,7 +226,10 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
200
226
|
*/
|
|
201
227
|
private handleTabDestroyed(tabId: string): void {
|
|
202
228
|
const ownership = this.tabOwnership.get(tabId);
|
|
203
|
-
if (!ownership)
|
|
229
|
+
if (!ownership) {
|
|
230
|
+
this.forgetTab(tabId);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
204
233
|
|
|
205
234
|
// Validate project to prevent cross-project collisions
|
|
206
235
|
const serviceProjectId = this.previewService?.getProjectId();
|
|
@@ -208,6 +237,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
208
237
|
|
|
209
238
|
debug.warn('mcp', `⚠️ Controlled tab ${tabId} was destroyed - auto-releasing from session ${ownership.chatSessionId}`);
|
|
210
239
|
this.releaseTab(tabId);
|
|
240
|
+
this.forgetTab(tabId);
|
|
211
241
|
}
|
|
212
242
|
|
|
213
243
|
/**
|
|
@@ -349,6 +379,15 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
349
379
|
return false;
|
|
350
380
|
}
|
|
351
381
|
|
|
382
|
+
// Harvest locks whose owner stopped streaming before reading ownership
|
|
383
|
+
// below. A lock left behind by an interrupt is indistinguishable here
|
|
384
|
+
// from a live one held by someone else, so without this the next run
|
|
385
|
+
// would be refused with "controlled by another chat session" — for a
|
|
386
|
+
// session that no longer exists — and nothing short of a restart would
|
|
387
|
+
// clear it. The other two sweep points only run on a release or a tab
|
|
388
|
+
// list, neither of which a fresh run is guaranteed to reach first.
|
|
389
|
+
this.releaseOrphans();
|
|
390
|
+
|
|
352
391
|
// Check existing ownership
|
|
353
392
|
const existingOwner = this.tabOwnership.get(browserTabId);
|
|
354
393
|
|
|
@@ -370,6 +409,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
370
409
|
projectId,
|
|
371
410
|
acquiredAt: now
|
|
372
411
|
});
|
|
412
|
+
this.lastProjectByTab.set(browserTabId, projectId);
|
|
373
413
|
|
|
374
414
|
// Add to session's tab set
|
|
375
415
|
let sessionSet = this.sessionTabs.get(chatSessionId);
|
|
@@ -392,52 +432,69 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
392
432
|
// ============================================================================
|
|
393
433
|
|
|
394
434
|
/**
|
|
395
|
-
* Mark the tab
|
|
396
|
-
* it is called on every action, and only a change is worth
|
|
435
|
+
* Mark the tab this session's agent is acting on. Idempotent and quiet when
|
|
436
|
+
* unchanged — it is called on every action, and only a change is worth
|
|
437
|
+
* telling the UI.
|
|
397
438
|
*/
|
|
398
439
|
focusTab(browserTabId: string, chatSessionId: string, projectId: string): void {
|
|
399
|
-
if (this.focusedTabs.get(
|
|
440
|
+
if (this.focusedTabs.get(chatSessionId) === browserTabId) return;
|
|
441
|
+
|
|
442
|
+
const previous = this.focusedTabs.get(chatSessionId);
|
|
443
|
+
this.focusedTabs.set(chatSessionId, browserTabId);
|
|
400
444
|
|
|
401
|
-
|
|
402
|
-
|
|
445
|
+
// The tab it moved off is no longer focused, and the viewer watching
|
|
446
|
+
// *that* tab needs to hear so — otherwise its pointer stays drawn on a
|
|
447
|
+
// tab the agent has left. Only announced while the session still holds
|
|
448
|
+
// it; a release emits its own end.
|
|
449
|
+
if (previous && this.tabOwnership.has(previous)) {
|
|
450
|
+
this.emitFocus(previous, false, projectId, chatSessionId);
|
|
451
|
+
}
|
|
452
|
+
this.emitFocus(browserTabId, true, projectId, chatSessionId);
|
|
403
453
|
}
|
|
404
454
|
|
|
405
|
-
/**
|
|
406
|
-
|
|
407
|
-
|
|
455
|
+
/**
|
|
456
|
+
* Whether an agent is acting on this tab right now.
|
|
457
|
+
*
|
|
458
|
+
* A tab is held by at most one session, so this needs no session argument
|
|
459
|
+
* and no project argument: there is only ever one answer, which is what
|
|
460
|
+
* lets a viewer decide whether to draw the pointer without knowing which
|
|
461
|
+
* chat is open in front of it.
|
|
462
|
+
*/
|
|
463
|
+
isTabFocused(browserTabId: string): boolean {
|
|
464
|
+
const owner = this.tabOwnership.get(browserTabId);
|
|
465
|
+
if (!owner) return false;
|
|
466
|
+
return this.focusedTabs.get(owner.chatSessionId) === browserTabId;
|
|
408
467
|
}
|
|
409
468
|
|
|
410
469
|
/**
|
|
411
|
-
* Re-derive a
|
|
470
|
+
* Re-derive a session's focus after ownership changed.
|
|
412
471
|
*
|
|
413
|
-
* Falls back to another tab the
|
|
472
|
+
* Falls back to another tab the same session still holds, so a batch that
|
|
414
473
|
* closed the tab it was working on lands the marker where the work went
|
|
415
|
-
* rather than nowhere
|
|
416
|
-
*
|
|
417
|
-
* focused tab is still held, so releasing several tabs at once produces one
|
|
418
|
-
* update rather than a burst.
|
|
474
|
+
* rather than nowhere. Emits nothing when the focused tab is still held, so
|
|
475
|
+
* releasing several tabs at once produces one update rather than a burst.
|
|
419
476
|
*/
|
|
420
|
-
private refreshFocus(projectId: string | undefined): void {
|
|
421
|
-
if (!
|
|
477
|
+
private refreshFocus(chatSessionId: string | undefined, projectId: string | undefined): void {
|
|
478
|
+
if (!chatSessionId) return;
|
|
422
479
|
|
|
423
|
-
const focused = this.focusedTabs.get(
|
|
480
|
+
const focused = this.focusedTabs.get(chatSessionId);
|
|
424
481
|
if (!focused) return;
|
|
425
482
|
|
|
426
|
-
|
|
427
|
-
if (owner && owner.projectId === projectId) return; // Still held — nothing to say.
|
|
483
|
+
if (this.tabOwnership.has(focused)) return; // Still held — nothing to say.
|
|
428
484
|
|
|
429
485
|
const successor = [...this.tabOwnership.entries()]
|
|
430
486
|
.reverse()
|
|
431
|
-
.find(([, info]) => info.
|
|
487
|
+
.find(([, info]) => info.chatSessionId === chatSessionId);
|
|
488
|
+
|
|
489
|
+
this.emitFocus(focused, false, projectId, chatSessionId);
|
|
432
490
|
|
|
433
491
|
if (successor) {
|
|
434
|
-
this.focusedTabs.set(
|
|
435
|
-
this.emitFocus(successor[0],
|
|
492
|
+
this.focusedTabs.set(chatSessionId, successor[0]);
|
|
493
|
+
this.emitFocus(successor[0], true, successor[1].projectId, chatSessionId);
|
|
436
494
|
return;
|
|
437
495
|
}
|
|
438
496
|
|
|
439
|
-
this.focusedTabs.delete(
|
|
440
|
-
this.emitFocus(null, projectId);
|
|
497
|
+
this.focusedTabs.delete(chatSessionId);
|
|
441
498
|
}
|
|
442
499
|
|
|
443
500
|
// ============================================================================
|
|
@@ -470,7 +527,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
470
527
|
|
|
471
528
|
// Emit control end event to frontend
|
|
472
529
|
this.emitControlEnd(browserTabId, ownership.projectId);
|
|
473
|
-
this.refreshFocus(ownership.projectId);
|
|
530
|
+
this.refreshFocus(ownership.chatSessionId, ownership.projectId);
|
|
474
531
|
|
|
475
532
|
debug.log('mcp', `🎮 Released tab: ${browserTabId} (was owned by session ${ownership.chatSessionId.slice(0, 8)})`);
|
|
476
533
|
}
|
|
@@ -487,18 +544,18 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
487
544
|
if (tabIds.length > 0) {
|
|
488
545
|
debug.log('mcp', `🎮 Releasing ${tabIds.length} tabs for session ${chatSessionId.slice(0, 8)}`);
|
|
489
546
|
|
|
490
|
-
|
|
547
|
+
let projectId: string | undefined;
|
|
491
548
|
for (const tabId of tabIds) {
|
|
492
549
|
const ownership = this.tabOwnership.get(tabId);
|
|
493
550
|
this.tabOwnership.delete(tabId);
|
|
494
551
|
this.emitActivity(tabId, null, ownership?.projectId);
|
|
495
552
|
this.emitControlEnd(tabId, ownership?.projectId);
|
|
496
|
-
|
|
553
|
+
projectId ??= ownership?.projectId;
|
|
497
554
|
}
|
|
498
555
|
|
|
499
556
|
// After every deletion, so the successor search sees the final state
|
|
500
557
|
// rather than tabs that are about to go too.
|
|
501
|
-
|
|
558
|
+
this.refreshFocus(chatSessionId, projectId);
|
|
502
559
|
|
|
503
560
|
debug.log('mcp', `🎮 Session ${chatSessionId.slice(0, 8)} fully released`);
|
|
504
561
|
}
|
|
@@ -524,20 +581,21 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
524
581
|
* Force release all control (for cleanup)
|
|
525
582
|
*/
|
|
526
583
|
forceReleaseAll(): void {
|
|
584
|
+
for (const tabId of this.focusedTabs.values()) {
|
|
585
|
+
this.emitFocus(tabId, false, this.tabOwnership.get(tabId)?.projectId);
|
|
586
|
+
}
|
|
587
|
+
|
|
527
588
|
// Emit control-end for all controlled tabs
|
|
528
589
|
for (const [tabId, info] of this.tabOwnership) {
|
|
529
590
|
this.emitActivity(tabId, null, info.projectId);
|
|
530
591
|
this.emitControlEnd(tabId, info.projectId);
|
|
531
592
|
}
|
|
532
593
|
|
|
533
|
-
for (const projectId of this.focusedTabs.keys()) {
|
|
534
|
-
this.emitFocus(null, projectId);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
594
|
this.tabOwnership.clear();
|
|
538
595
|
this.sessionTabs.clear();
|
|
539
596
|
this.focusedTabs.clear();
|
|
540
597
|
this.tabActivity.clear();
|
|
598
|
+
this.lastProjectByTab.clear();
|
|
541
599
|
|
|
542
600
|
debug.log('mcp', '🧹 Force released all MCP control');
|
|
543
601
|
}
|
|
@@ -547,12 +605,22 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
547
605
|
// ============================================================================
|
|
548
606
|
|
|
549
607
|
/**
|
|
550
|
-
* Emit cursor position event with MCP source
|
|
608
|
+
* Emit cursor position event with MCP source.
|
|
609
|
+
*
|
|
610
|
+
* Silent once the tab has been handed back. An action is only interruptible
|
|
611
|
+
* between steps, so pressing stop mid-glide leaves a few hundred
|
|
612
|
+
* milliseconds of pointer updates still to come for a tab nobody controls
|
|
613
|
+
* any more — and a viewer that drew them would show the agent moving after
|
|
614
|
+
* it had been told to stop. Dropping them here means the wire agrees with
|
|
615
|
+
* the lock, rather than relying on every viewer to ignore them.
|
|
551
616
|
*/
|
|
552
617
|
emitCursorPosition(tabId: string, x: number, y: number, pressed = false): void {
|
|
618
|
+
const ownership = this.tabOwnership.get(tabId);
|
|
619
|
+
if (!ownership) return;
|
|
620
|
+
|
|
553
621
|
const event: McpCursorEvent = {
|
|
554
622
|
tabId,
|
|
555
|
-
projectId:
|
|
623
|
+
projectId: ownership.projectId,
|
|
556
624
|
x,
|
|
557
625
|
y,
|
|
558
626
|
pressed,
|
|
@@ -564,12 +632,16 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
564
632
|
}
|
|
565
633
|
|
|
566
634
|
/**
|
|
567
|
-
* Emit cursor click event with MCP source
|
|
635
|
+
* Emit cursor click event with MCP source. Silent after release — see
|
|
636
|
+
* `emitCursorPosition`.
|
|
568
637
|
*/
|
|
569
638
|
emitCursorClick(tabId: string, x: number, y: number, button: 'left' | 'right' | 'middle' = 'left'): void {
|
|
639
|
+
const ownership = this.tabOwnership.get(tabId);
|
|
640
|
+
if (!ownership) return;
|
|
641
|
+
|
|
570
642
|
const event: McpClickEvent = {
|
|
571
643
|
tabId,
|
|
572
|
-
projectId:
|
|
644
|
+
projectId: ownership.projectId,
|
|
573
645
|
x,
|
|
574
646
|
y,
|
|
575
647
|
button,
|
|
@@ -597,7 +669,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
597
669
|
tabId,
|
|
598
670
|
// Explicit on release, where ownership has already been dropped and
|
|
599
671
|
// there is nothing left to look the project up in.
|
|
600
|
-
projectId: projectId ?? this.tabOwnership.get(tabId)?.projectId,
|
|
672
|
+
projectId: projectId ?? this.tabOwnership.get(tabId)?.projectId ?? this.lastProjectByTab.get(tabId),
|
|
601
673
|
label,
|
|
602
674
|
timestamp: Date.now()
|
|
603
675
|
};
|
|
@@ -610,15 +682,10 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
610
682
|
return this.tabActivity.get(tabId) ?? null;
|
|
611
683
|
}
|
|
612
684
|
|
|
613
|
-
/**
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
this.emit('test-completed', {
|
|
618
|
-
tabId,
|
|
619
|
-
timestamp: Date.now(),
|
|
620
|
-
source: 'mcp'
|
|
621
|
-
});
|
|
685
|
+
/** Forget everything remembered about a tab that no longer exists. */
|
|
686
|
+
forgetTab(browserTabId: string): void {
|
|
687
|
+
this.lastProjectByTab.delete(browserTabId);
|
|
688
|
+
this.tabActivity.delete(browserTabId);
|
|
622
689
|
}
|
|
623
690
|
|
|
624
691
|
// ============================================================================
|
|
@@ -652,17 +719,26 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
652
719
|
debug.log('mcp', `📢 Emitted mcp:control-end for tab: ${browserTabId}`);
|
|
653
720
|
}
|
|
654
721
|
|
|
655
|
-
private emitFocus(
|
|
722
|
+
private emitFocus(
|
|
723
|
+
browserTabId: string,
|
|
724
|
+
focused: boolean,
|
|
725
|
+
projectId: string | undefined,
|
|
726
|
+
chatSessionId?: string
|
|
727
|
+
): void {
|
|
728
|
+
const owningProject = projectId ?? this.lastProjectByTab.get(browserTabId);
|
|
729
|
+
if (!owningProject) return;
|
|
730
|
+
|
|
656
731
|
const event: McpFocusEvent = {
|
|
657
732
|
browserTabId,
|
|
658
|
-
|
|
733
|
+
focused,
|
|
734
|
+
projectId: owningProject,
|
|
659
735
|
chatSessionId,
|
|
660
736
|
timestamp: Date.now()
|
|
661
737
|
};
|
|
662
738
|
|
|
663
739
|
this.emit('control-focus', event);
|
|
664
740
|
|
|
665
|
-
debug.log('mcp', `📢 Emitted mcp:control-focus
|
|
741
|
+
debug.log('mcp', `📢 Emitted mcp:control-focus ${focused ? 'on' : 'off'} for ${browserTabId}`);
|
|
666
742
|
}
|
|
667
743
|
}
|
|
668
744
|
|
|
@@ -99,10 +99,6 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
99
99
|
// with the `source` stamp the client contract requires. Forwarding them
|
|
100
100
|
// from here as well delivered every cursor update twice, once without
|
|
101
101
|
// that field.
|
|
102
|
-
this.interactionHandler.on('test-completed', (data) => {
|
|
103
|
-
this.emit('preview:browser-mcp-test-completed', data);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
102
|
// Forward navigation events and handle video streaming restart
|
|
107
103
|
// Only full navigations (framenavigated) need streaming restart
|
|
108
104
|
this.navigationTracker.on('navigation', async (data) => {
|
|
@@ -507,12 +503,31 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
507
503
|
}
|
|
508
504
|
|
|
509
505
|
/**
|
|
510
|
-
*
|
|
506
|
+
* Make a tab the project's active one.
|
|
507
|
+
*
|
|
508
|
+
* "Active" is a single value shared by everyone in the project, so this is
|
|
509
|
+
* for the agent (`switch_tab`, `open_tab`) and for tab creation — decisions
|
|
510
|
+
* that genuinely belong to the project. A viewer merely looking at a tab
|
|
511
|
+
* must use `noteTabViewed` instead: with two people watching, letting the
|
|
512
|
+
* act of looking reassign this made each of them move the other's target.
|
|
511
513
|
*/
|
|
512
514
|
switchTab(tabId: string): boolean {
|
|
513
515
|
return this.tabManager.setActiveTab(tabId);
|
|
514
516
|
}
|
|
515
517
|
|
|
518
|
+
/**
|
|
519
|
+
* Note that some viewer is now watching this tab.
|
|
520
|
+
*
|
|
521
|
+
* Keeps the tab out of the idle-cleanup sweep without touching who the
|
|
522
|
+
* project considers active. Returns false if the tab is gone, which is how
|
|
523
|
+
* a viewer learns its tab strip is stale.
|
|
524
|
+
*/
|
|
525
|
+
noteTabViewed(tabId: string): boolean {
|
|
526
|
+
if (!this.tabManager.getTab(tabId)) return false;
|
|
527
|
+
this.tabManager.markTabActivity(tabId);
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
|
|
516
531
|
/**
|
|
517
532
|
* Get a tab by ID
|
|
518
533
|
*/
|
|
@@ -874,6 +889,11 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
874
889
|
* caller can report "3 of 7 ran" instead of claiming a success the user
|
|
875
890
|
* interrupted.
|
|
876
891
|
*/
|
|
892
|
+
/** Where the agent's pointer stands on a tab, for a viewer building state. */
|
|
893
|
+
getMcpCursorPosition(tabId: string): { x: number; y: number } | null {
|
|
894
|
+
return this.interactionHandler.getCursorPosition(tabId);
|
|
895
|
+
}
|
|
896
|
+
|
|
877
897
|
async performAutonomousActions(
|
|
878
898
|
tabId: string,
|
|
879
899
|
actions: BrowserAutonomousAction[],
|
|
@@ -1181,12 +1201,6 @@ class BrowserPreviewServiceManager {
|
|
|
1181
1201
|
ws.emit.project(projectId, 'preview:browser-console-clear', data);
|
|
1182
1202
|
});
|
|
1183
1203
|
|
|
1184
|
-
// Forward MCP events. Cursor position/click come from the singleton
|
|
1185
|
-
// instead (setupMcpControlForwarding) — see setupEventForwarding().
|
|
1186
|
-
service.on('preview:browser-mcp-test-completed', (data) => {
|
|
1187
|
-
ws.emit.project(projectId, 'preview:browser-mcp-test-completed', data);
|
|
1188
|
-
});
|
|
1189
|
-
|
|
1190
1204
|
// Forward dialog events
|
|
1191
1205
|
service.on('preview:browser-dialog', (data) => {
|
|
1192
1206
|
ws.emit.project(projectId, 'preview:browser-dialog', data);
|
|
@@ -1257,23 +1271,19 @@ class BrowserPreviewServiceManager {
|
|
|
1257
1271
|
*
|
|
1258
1272
|
* Registered exactly once for the whole manager. The emitter is a singleton
|
|
1259
1273
|
* shared across all projects, so attaching these listeners per-project would
|
|
1260
|
-
* accumulate them without bound (MaxListenersExceededWarning).
|
|
1261
|
-
*
|
|
1262
|
-
*
|
|
1274
|
+
* accumulate them without bound (MaxListenersExceededWarning).
|
|
1275
|
+
*
|
|
1276
|
+
* Every event here is addressed to exactly one project room. Tab ids repeat
|
|
1277
|
+
* across projects (`tab-1` exists in all of them), so an unaddressed event
|
|
1278
|
+
* has no safe destination: broadcasting it draws one project's agent onto a
|
|
1279
|
+
* same-numbered tab in another project, which on a shared server means onto
|
|
1280
|
+
* a colleague's screen. `browserMcpControl` remembers a tab's last owning
|
|
1281
|
+
* project precisely so this never has to guess.
|
|
1263
1282
|
*/
|
|
1264
1283
|
private setupMcpControlForwarding(): void {
|
|
1265
1284
|
if (this.mcpForwardingSetup) return;
|
|
1266
1285
|
this.mcpForwardingSetup = true;
|
|
1267
1286
|
|
|
1268
|
-
const emitToActiveProjects = <K extends Parameters<typeof ws.emit.project>[1]>(
|
|
1269
|
-
event: K,
|
|
1270
|
-
payload: Parameters<typeof ws.emit.project<K>>[2]
|
|
1271
|
-
) => {
|
|
1272
|
-
for (const projectId of this.services.keys()) {
|
|
1273
|
-
ws.emit.project(projectId, event, payload);
|
|
1274
|
-
}
|
|
1275
|
-
};
|
|
1276
|
-
|
|
1277
1287
|
// Control events are project-scoped: emit ONLY to the owning project's room
|
|
1278
1288
|
// and stamp projectId. Tab IDs (tab-N) repeat across projects, so a broadcast
|
|
1279
1289
|
// would let project A's control event mismark a same-numbered tab in project B.
|
|
@@ -1301,72 +1311,43 @@ class BrowserPreviewServiceManager {
|
|
|
1301
1311
|
browserMcpControl.on('control-focus', (data) => {
|
|
1302
1312
|
ws.emit.project(data.projectId, 'preview:browser-mcp-control-focus', {
|
|
1303
1313
|
browserTabId: data.browserTabId,
|
|
1314
|
+
focused: data.focused,
|
|
1304
1315
|
projectId: data.projectId,
|
|
1305
1316
|
timestamp: data.timestamp
|
|
1306
1317
|
});
|
|
1307
1318
|
});
|
|
1308
1319
|
|
|
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.
|
|
1313
1320
|
browserMcpControl.on('cursor-position', (data) => {
|
|
1314
|
-
|
|
1321
|
+
if (!data.projectId) return;
|
|
1322
|
+
ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-position', {
|
|
1315
1323
|
sessionId: data.tabId,
|
|
1316
1324
|
x: data.x,
|
|
1317
1325
|
y: data.y,
|
|
1318
1326
|
pressed: data.pressed ?? false,
|
|
1319
1327
|
timestamp: data.timestamp,
|
|
1320
1328
|
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);
|
|
1329
|
+
});
|
|
1328
1330
|
});
|
|
1329
1331
|
|
|
1330
1332
|
browserMcpControl.on('cursor-click', (data) => {
|
|
1331
|
-
|
|
1333
|
+
if (!data.projectId) return;
|
|
1334
|
+
ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-click', {
|
|
1332
1335
|
sessionId: data.tabId,
|
|
1333
1336
|
x: data.x,
|
|
1334
1337
|
y: data.y,
|
|
1335
1338
|
button: data.button ?? 'left',
|
|
1336
1339
|
timestamp: data.timestamp,
|
|
1337
1340
|
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);
|
|
1341
|
+
});
|
|
1345
1342
|
});
|
|
1346
1343
|
|
|
1347
|
-
// What the agent is doing, for the caption beside its cursor.
|
|
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.
|
|
1344
|
+
// What the agent is doing, for the caption beside its cursor.
|
|
1351
1345
|
browserMcpControl.on('activity', (data) => {
|
|
1352
|
-
|
|
1346
|
+
if (!data.projectId) return;
|
|
1347
|
+
ws.emit.project(data.projectId, 'preview:browser-mcp-activity', {
|
|
1353
1348
|
sessionId: data.tabId,
|
|
1354
1349
|
label: data.label,
|
|
1355
1350
|
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);
|
|
1363
|
-
});
|
|
1364
|
-
|
|
1365
|
-
browserMcpControl.on('test-completed', (data) => {
|
|
1366
|
-
emitToActiveProjects('preview:browser-mcp-test-completed', {
|
|
1367
|
-
sessionId: data.tabId,
|
|
1368
|
-
timestamp: data.timestamp,
|
|
1369
|
-
source: 'mcp'
|
|
1370
1351
|
});
|
|
1371
1352
|
});
|
|
1372
1353
|
}
|
|
@@ -746,16 +746,40 @@ export class BrowserTabManager extends EventEmitter {
|
|
|
746
746
|
return false;
|
|
747
747
|
}
|
|
748
748
|
|
|
749
|
+
/**
|
|
750
|
+
* Defensive cleanup before a URL reaches CDP.
|
|
751
|
+
*
|
|
752
|
+
* `Page.navigate` rejects with a bare "Cannot navigate to invalid URL" for
|
|
753
|
+
* strings that look completely valid to a person — the usual cause is a
|
|
754
|
+
* copy-paste artefact CDP does not tolerate: leading/trailing whitespace,
|
|
755
|
+
* a stray newline, or zero-width/invisible Unicode that a real address
|
|
756
|
+
* bar strips silently but Puppeteer never sees. `URL` is the same class of
|
|
757
|
+
* parser Chromium uses internally, so whatever it rejects would have
|
|
758
|
+
* failed identically inside the browser — surfacing that here, with the
|
|
759
|
+
* exact string, turns a dead end into something explainable instead of
|
|
760
|
+
* three pointless retries against a string that was never going anywhere.
|
|
761
|
+
*/
|
|
762
|
+
private sanitizeNavigationUrl(url: string): string {
|
|
763
|
+
const cleaned = url.replace(/[\u200B-\u200F\u202A-\u202E\uFEFF]/g, '').trim();
|
|
764
|
+
try {
|
|
765
|
+
new URL(cleaned);
|
|
766
|
+
} catch {
|
|
767
|
+
throw new Error(`Invalid URL: "${cleaned}"`);
|
|
768
|
+
}
|
|
769
|
+
return cleaned;
|
|
770
|
+
}
|
|
771
|
+
|
|
749
772
|
/**
|
|
750
773
|
* Navigate with retry, including Cloudflare auto-pass detection and CAPTCHA popup dismissal.
|
|
751
774
|
*/
|
|
752
775
|
private async navigateWithRetry(page: Page, url: string): Promise<string> {
|
|
776
|
+
const cleanUrl = this.sanitizeNavigationUrl(url);
|
|
753
777
|
let retries = 3;
|
|
754
778
|
let actualUrl = '';
|
|
755
779
|
|
|
756
780
|
while (retries > 0) {
|
|
757
781
|
try {
|
|
758
|
-
await page.goto(
|
|
782
|
+
await page.goto(cleanUrl, {
|
|
759
783
|
waitUntil: 'domcontentloaded',
|
|
760
784
|
timeout: 30000
|
|
761
785
|
});
|
|
@@ -11,24 +11,24 @@ import { requireBrowserTabAccess } from '../access';
|
|
|
11
11
|
export const consolePreviewHandler = createRouter()
|
|
12
12
|
// Get console logs
|
|
13
13
|
.http('preview:browser-console-get', {
|
|
14
|
-
data: t.Object({}),
|
|
14
|
+
data: t.Object({ tabId: t.Optional(t.String()) }),
|
|
15
15
|
response: t.Object({
|
|
16
16
|
logs: t.Any()
|
|
17
17
|
})
|
|
18
|
-
}, async ({ conn }) => {
|
|
19
|
-
const { previewService, tab } = requireBrowserTabAccess(conn);
|
|
18
|
+
}, async ({ data, conn }) => {
|
|
19
|
+
const { previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
20
20
|
const consoleLogs = previewService.getConsoleLogs(tab.id);
|
|
21
21
|
return { logs: consoleLogs };
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
// Clear console logs
|
|
25
25
|
.http('preview:browser-console-clear', {
|
|
26
|
-
data: t.Object({}),
|
|
26
|
+
data: t.Object({ tabId: t.Optional(t.String()) }),
|
|
27
27
|
response: t.Object({
|
|
28
28
|
message: t.String()
|
|
29
29
|
})
|
|
30
|
-
}, async ({ conn }) => {
|
|
31
|
-
const { previewService, tab } = requireBrowserTabAccess(conn);
|
|
30
|
+
}, async ({ data, conn }) => {
|
|
31
|
+
const { previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
32
32
|
|
|
33
33
|
const success = previewService.clearConsoleLogs(tab.id);
|
|
34
34
|
|
|
@@ -42,13 +42,14 @@ export const consolePreviewHandler = createRouter()
|
|
|
42
42
|
// Execute console command
|
|
43
43
|
.http('preview:browser-console-execute', {
|
|
44
44
|
data: t.Object({
|
|
45
|
-
command: t.String({ minLength: 1 })
|
|
45
|
+
command: t.String({ minLength: 1 }),
|
|
46
|
+
tabId: t.Optional(t.String())
|
|
46
47
|
}),
|
|
47
48
|
response: t.Object({
|
|
48
49
|
result: t.Any()
|
|
49
50
|
})
|
|
50
51
|
}, async ({ data, conn }) => {
|
|
51
|
-
const { previewService, tab } = requireBrowserTabAccess(conn);
|
|
52
|
+
const { previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
52
53
|
|
|
53
54
|
const result = await previewService.executeConsoleCommand(tab.id, data.command);
|
|
54
55
|
return { result };
|
|
@@ -57,14 +58,15 @@ export const consolePreviewHandler = createRouter()
|
|
|
57
58
|
// Toggle console logging
|
|
58
59
|
.http('preview:browser-console-toggle', {
|
|
59
60
|
data: t.Object({
|
|
60
|
-
enabled: t.Boolean()
|
|
61
|
+
enabled: t.Boolean(),
|
|
62
|
+
tabId: t.Optional(t.String())
|
|
61
63
|
}),
|
|
62
64
|
response: t.Object({
|
|
63
65
|
enabled: t.Boolean(),
|
|
64
66
|
message: t.String()
|
|
65
67
|
})
|
|
66
68
|
}, async ({ data, conn }) => {
|
|
67
|
-
const { previewService, tab } = requireBrowserTabAccess(conn);
|
|
69
|
+
const { previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
68
70
|
|
|
69
71
|
const success = previewService.toggleConsoleLogging(tab.id, data.enabled);
|
|
70
72
|
|