@myrialabs/clopen 0.4.23 → 0.4.25
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/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/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 +95 -38
- package/backend/preview/browser/browser-mcp-control.ts +332 -22
- package/backend/preview/browser/browser-preview-service.ts +152 -33
- package/backend/preview/browser/browser-tab-manager.ts +25 -1
- 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/console.ts +12 -10
- package/backend/ws/preview/browser/interact.ts +40 -10
- package/backend/ws/preview/browser/native-ui.ts +35 -10
- package/backend/ws/preview/browser/tab-info.ts +46 -7
- package/backend/ws/preview/browser/webcodecs.ts +6 -1
- package/backend/ws/preview/index.ts +23 -3
- package/dist/assets/{index-DH_GO0Lf.js → index-C4PD7B-x.js} +1 -1
- package/dist/assets/{index-igSQ3JaM.js → index-KBGloYDk.js} +1 -1
- package/dist/assets/{main-D-d8SFfW.js → main-B0AA-88X.js} +364 -358
- package/dist/assets/main-OsjA5Ab5.css +1 -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-BD3-qPfT.css +0 -1
|
@@ -41,8 +41,37 @@ export interface McpControlEvent {
|
|
|
41
41
|
timestamp: number;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Whether an agent is acting on this tab 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
|
+
*
|
|
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.
|
|
55
|
+
*/
|
|
56
|
+
export interface McpFocusEvent {
|
|
57
|
+
browserTabId: string;
|
|
58
|
+
focused: boolean;
|
|
59
|
+
projectId: string;
|
|
60
|
+
chatSessionId?: string;
|
|
61
|
+
timestamp: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Answers "does this chat session still have a stream running?". */
|
|
65
|
+
export type SessionLivenessProbe = (chatSessionId: string) => boolean;
|
|
66
|
+
|
|
44
67
|
export interface McpCursorEvent {
|
|
45
68
|
tabId: string;
|
|
69
|
+
/**
|
|
70
|
+
* Owning project. Tab ids repeat across projects (`tab-1` exists in every
|
|
71
|
+
* one), so a cursor broadcast to all of them could be drawn over a
|
|
72
|
+
* same-numbered tab in a project the agent has never touched.
|
|
73
|
+
*/
|
|
74
|
+
projectId?: string;
|
|
46
75
|
x: number;
|
|
47
76
|
y: number;
|
|
48
77
|
/** Mouse button held down — the difference between a move and a drag. */
|
|
@@ -53,6 +82,7 @@ export interface McpCursorEvent {
|
|
|
53
82
|
|
|
54
83
|
export interface McpClickEvent {
|
|
55
84
|
tabId: string;
|
|
85
|
+
projectId?: string;
|
|
56
86
|
x: number;
|
|
57
87
|
y: number;
|
|
58
88
|
button?: 'left' | 'right' | 'middle';
|
|
@@ -60,6 +90,24 @@ export interface McpClickEvent {
|
|
|
60
90
|
source: 'mcp';
|
|
61
91
|
}
|
|
62
92
|
|
|
93
|
+
/**
|
|
94
|
+
* What the agent is doing on a tab, in the user's words.
|
|
95
|
+
*
|
|
96
|
+
* The pointer alone says "something else is driving this" and nothing more,
|
|
97
|
+
* which is the wrong amount of information for a run that can hold a tab for
|
|
98
|
+
* minutes: a page changing on its own with a nameless cursor on it reads as a
|
|
99
|
+
* glitch. `label` is a two-or-three-word gloss of the action about to run —
|
|
100
|
+
* "Typing", "Clicking", "Navigating" — and rides next to the cursor.
|
|
101
|
+
*
|
|
102
|
+
* `null` means the agent still holds the tab but is between actions.
|
|
103
|
+
*/
|
|
104
|
+
export interface McpActivityEvent {
|
|
105
|
+
tabId: string;
|
|
106
|
+
projectId?: string;
|
|
107
|
+
label: string | null;
|
|
108
|
+
timestamp: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
63
111
|
export class BrowserMcpControl extends EventEmitter {
|
|
64
112
|
/** Tab → ownership info (which chat session controls it) */
|
|
65
113
|
private tabOwnership = new Map<string, TabOwnershipInfo>();
|
|
@@ -67,6 +115,44 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
67
115
|
/** Chat session → set of tab IDs it controls */
|
|
68
116
|
private sessionTabs = new Map<string, Set<string>>();
|
|
69
117
|
|
|
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
|
+
*/
|
|
128
|
+
private focusedTabs = new Map<string, string>();
|
|
129
|
+
|
|
130
|
+
/** Tab → what the agent is doing on it, for the cursor's caption. */
|
|
131
|
+
private tabActivity = new Map<string, string>();
|
|
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
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Whether the stream that owns a lock is still running.
|
|
147
|
+
*
|
|
148
|
+
* Registered by the chat stream manager. Without it a tool call that lands
|
|
149
|
+
* after its stream ended — the remote-MCP transports resolve their session
|
|
150
|
+
* from "most recent active stream", so a late call can name a session that
|
|
151
|
+
* has already been released — filed a lock under an owner nobody would ever
|
|
152
|
+
* release, and the tab stayed locked until the server restarted.
|
|
153
|
+
*/
|
|
154
|
+
private livenessProbe: SessionLivenessProbe | null = null;
|
|
155
|
+
|
|
70
156
|
// Pending tab requests (keyed by request type + timestamp)
|
|
71
157
|
private pendingTabRequests = new Map<string, PendingTabRequest>();
|
|
72
158
|
private requestCounter = 0;
|
|
@@ -93,13 +179,57 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
93
179
|
debug.log('mcp', '🔗 Browser MCP Control initialized with event-based tab tracking');
|
|
94
180
|
}
|
|
95
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Teach this manager which chat sessions are still streaming.
|
|
184
|
+
*
|
|
185
|
+
* Called once by the chat stream manager. Absent, every session reads as
|
|
186
|
+
* live — the previous behaviour, which is the safe default for anything
|
|
187
|
+
* that constructs this without a chat layer (tests, tooling).
|
|
188
|
+
*/
|
|
189
|
+
setSessionLivenessProbe(probe: SessionLivenessProbe): void {
|
|
190
|
+
this.livenessProbe = probe;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Whether a chat session is still streaming (true when no probe is set). */
|
|
194
|
+
isSessionLive(chatSessionId: string): boolean {
|
|
195
|
+
return this.livenessProbe ? this.livenessProbe(chatSessionId) : true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Release every tab whose owning session has stopped streaming.
|
|
200
|
+
*
|
|
201
|
+
* The belt to `releaseSession`'s braces. Release is driven by the stream's
|
|
202
|
+
* terminal events, so anything that never reaches one — a lock acquired
|
|
203
|
+
* during the teardown window, an interrupt that took an unusual path —
|
|
204
|
+
* leaves ownership behind with nothing to collect it. Sweeping is cheap
|
|
205
|
+
* (the map holds a handful of entries) and makes a stuck lock recover on
|
|
206
|
+
* the next stream event or tab query instead of at the next restart.
|
|
207
|
+
*/
|
|
208
|
+
releaseOrphans(): number {
|
|
209
|
+
if (!this.livenessProbe) return 0;
|
|
210
|
+
|
|
211
|
+
const orphans = [...this.tabOwnership.entries()]
|
|
212
|
+
.filter(([, info]) => !this.isSessionLive(info.chatSessionId))
|
|
213
|
+
.map(([tabId]) => tabId);
|
|
214
|
+
|
|
215
|
+
for (const tabId of orphans) {
|
|
216
|
+
debug.warn('mcp', `🧹 Releasing orphaned tab ${tabId} — its chat session is no longer streaming`);
|
|
217
|
+
this.releaseTab(tabId);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return orphans.length;
|
|
221
|
+
}
|
|
222
|
+
|
|
96
223
|
/**
|
|
97
224
|
* Handle tab destroyed event
|
|
98
225
|
* Auto-release control for the destroyed tab only
|
|
99
226
|
*/
|
|
100
227
|
private handleTabDestroyed(tabId: string): void {
|
|
101
228
|
const ownership = this.tabOwnership.get(tabId);
|
|
102
|
-
if (!ownership)
|
|
229
|
+
if (!ownership) {
|
|
230
|
+
this.forgetTab(tabId);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
103
233
|
|
|
104
234
|
// Validate project to prevent cross-project collisions
|
|
105
235
|
const serviceProjectId = this.previewService?.getProjectId();
|
|
@@ -107,6 +237,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
107
237
|
|
|
108
238
|
debug.warn('mcp', `⚠️ Controlled tab ${tabId} was destroyed - auto-releasing from session ${ownership.chatSessionId}`);
|
|
109
239
|
this.releaseTab(tabId);
|
|
240
|
+
this.forgetTab(tabId);
|
|
110
241
|
}
|
|
111
242
|
|
|
112
243
|
/**
|
|
@@ -224,6 +355,11 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
224
355
|
sessionSet.delete(browserTabId);
|
|
225
356
|
sessionSet.add(browserTabId);
|
|
226
357
|
debug.log('mcp', `🔀 Promoted tab ${browserTabId} to end of session ${chatSessionId.slice(0, 8)} set`);
|
|
358
|
+
|
|
359
|
+
// The agent just changed where it is working; the focus marker is
|
|
360
|
+
// what tells the user that, so it moves with the promotion.
|
|
361
|
+
const projectId = this.tabOwnership.get(browserTabId)?.projectId;
|
|
362
|
+
if (projectId) this.focusTab(browserTabId, chatSessionId, projectId);
|
|
227
363
|
}
|
|
228
364
|
}
|
|
229
365
|
|
|
@@ -235,12 +371,30 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
235
371
|
* - If the tab is free → acquire and add to session's controlled set
|
|
236
372
|
*/
|
|
237
373
|
acquireControl(browserTabId: string, chatSessionId: string, projectId: string): boolean {
|
|
374
|
+
// A cancelled stream can still have a tool call in flight. Letting it
|
|
375
|
+
// take the lock re-locks a tab that was just handed back, and files the
|
|
376
|
+
// lock under an owner whose release already ran.
|
|
377
|
+
if (!this.isSessionLive(chatSessionId)) {
|
|
378
|
+
debug.warn('mcp', `❌ Session ${chatSessionId.slice(0, 8)} is no longer streaming, refusing control of ${browserTabId}`);
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
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
|
+
|
|
238
391
|
// Check existing ownership
|
|
239
392
|
const existingOwner = this.tabOwnership.get(browserTabId);
|
|
240
393
|
|
|
241
394
|
if (existingOwner) {
|
|
242
395
|
// Same session already owns it → idempotent success
|
|
243
396
|
if (existingOwner.chatSessionId === chatSessionId) {
|
|
397
|
+
this.focusTab(browserTabId, chatSessionId, projectId);
|
|
244
398
|
return true;
|
|
245
399
|
}
|
|
246
400
|
// Different session owns it → denied
|
|
@@ -255,6 +409,7 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
255
409
|
projectId,
|
|
256
410
|
acquiredAt: now
|
|
257
411
|
});
|
|
412
|
+
this.lastProjectByTab.set(browserTabId, projectId);
|
|
258
413
|
|
|
259
414
|
// Add to session's tab set
|
|
260
415
|
let sessionSet = this.sessionTabs.get(chatSessionId);
|
|
@@ -266,11 +421,82 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
266
421
|
|
|
267
422
|
// Emit control start event to frontend
|
|
268
423
|
this.emitControlStart(browserTabId, chatSessionId, projectId);
|
|
424
|
+
this.focusTab(browserTabId, chatSessionId, projectId);
|
|
269
425
|
|
|
270
426
|
debug.log('mcp', `🎮 Session ${chatSessionId.slice(0, 8)} acquired tab: ${browserTabId} (total: ${sessionSet.size} tabs)`);
|
|
271
427
|
return true;
|
|
272
428
|
}
|
|
273
429
|
|
|
430
|
+
// ============================================================================
|
|
431
|
+
// Focus
|
|
432
|
+
// ============================================================================
|
|
433
|
+
|
|
434
|
+
/**
|
|
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.
|
|
438
|
+
*/
|
|
439
|
+
focusTab(browserTabId: string, chatSessionId: string, projectId: string): void {
|
|
440
|
+
if (this.focusedTabs.get(chatSessionId) === browserTabId) return;
|
|
441
|
+
|
|
442
|
+
const previous = this.focusedTabs.get(chatSessionId);
|
|
443
|
+
this.focusedTabs.set(chatSessionId, browserTabId);
|
|
444
|
+
|
|
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);
|
|
453
|
+
}
|
|
454
|
+
|
|
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;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Re-derive a session's focus after ownership changed.
|
|
471
|
+
*
|
|
472
|
+
* Falls back to another tab the same session still holds, so a batch that
|
|
473
|
+
* closed the tab it was working on lands the marker where the work went
|
|
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.
|
|
476
|
+
*/
|
|
477
|
+
private refreshFocus(chatSessionId: string | undefined, projectId: string | undefined): void {
|
|
478
|
+
if (!chatSessionId) return;
|
|
479
|
+
|
|
480
|
+
const focused = this.focusedTabs.get(chatSessionId);
|
|
481
|
+
if (!focused) return;
|
|
482
|
+
|
|
483
|
+
if (this.tabOwnership.has(focused)) return; // Still held — nothing to say.
|
|
484
|
+
|
|
485
|
+
const successor = [...this.tabOwnership.entries()]
|
|
486
|
+
.reverse()
|
|
487
|
+
.find(([, info]) => info.chatSessionId === chatSessionId);
|
|
488
|
+
|
|
489
|
+
this.emitFocus(focused, false, projectId, chatSessionId);
|
|
490
|
+
|
|
491
|
+
if (successor) {
|
|
492
|
+
this.focusedTabs.set(chatSessionId, successor[0]);
|
|
493
|
+
this.emitFocus(successor[0], true, successor[1].projectId, chatSessionId);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
this.focusedTabs.delete(chatSessionId);
|
|
498
|
+
}
|
|
499
|
+
|
|
274
500
|
// ============================================================================
|
|
275
501
|
// Control Release
|
|
276
502
|
// ============================================================================
|
|
@@ -295,8 +521,13 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
295
521
|
}
|
|
296
522
|
}
|
|
297
523
|
|
|
524
|
+
// Before the control-end, so the caption cannot outlive the cursor it
|
|
525
|
+
// belongs to on a viewer that processes the two in order.
|
|
526
|
+
this.emitActivity(browserTabId, null, ownership.projectId);
|
|
527
|
+
|
|
298
528
|
// Emit control end event to frontend
|
|
299
529
|
this.emitControlEnd(browserTabId, ownership.projectId);
|
|
530
|
+
this.refreshFocus(ownership.chatSessionId, ownership.projectId);
|
|
300
531
|
|
|
301
532
|
debug.log('mcp', `🎮 Released tab: ${browserTabId} (was owned by session ${ownership.chatSessionId.slice(0, 8)})`);
|
|
302
533
|
}
|
|
@@ -307,23 +538,31 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
307
538
|
*/
|
|
308
539
|
releaseSession(chatSessionId: string): void {
|
|
309
540
|
const sessionSet = this.sessionTabs.get(chatSessionId);
|
|
310
|
-
|
|
311
|
-
this.sessionTabs.delete(chatSessionId);
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
541
|
+
this.sessionTabs.delete(chatSessionId);
|
|
314
542
|
|
|
315
|
-
const tabIds = Array.from(sessionSet);
|
|
316
|
-
|
|
543
|
+
const tabIds = Array.from(sessionSet ?? []);
|
|
544
|
+
if (tabIds.length > 0) {
|
|
545
|
+
debug.log('mcp', `🎮 Releasing ${tabIds.length} tabs for session ${chatSessionId.slice(0, 8)}`);
|
|
546
|
+
|
|
547
|
+
let projectId: string | undefined;
|
|
548
|
+
for (const tabId of tabIds) {
|
|
549
|
+
const ownership = this.tabOwnership.get(tabId);
|
|
550
|
+
this.tabOwnership.delete(tabId);
|
|
551
|
+
this.emitActivity(tabId, null, ownership?.projectId);
|
|
552
|
+
this.emitControlEnd(tabId, ownership?.projectId);
|
|
553
|
+
projectId ??= ownership?.projectId;
|
|
554
|
+
}
|
|
317
555
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
this.
|
|
321
|
-
this.emitControlEnd(tabId, ownership?.projectId);
|
|
322
|
-
}
|
|
556
|
+
// After every deletion, so the successor search sees the final state
|
|
557
|
+
// rather than tabs that are about to go too.
|
|
558
|
+
this.refreshFocus(chatSessionId, projectId);
|
|
323
559
|
|
|
324
|
-
|
|
560
|
+
debug.log('mcp', `🎮 Session ${chatSessionId.slice(0, 8)} fully released`);
|
|
561
|
+
}
|
|
325
562
|
|
|
326
|
-
|
|
563
|
+
// A tool call that acquired during this session's teardown is filed
|
|
564
|
+
// under an owner that is already gone; nothing else would collect it.
|
|
565
|
+
this.releaseOrphans();
|
|
327
566
|
}
|
|
328
567
|
|
|
329
568
|
/**
|
|
@@ -342,13 +581,21 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
342
581
|
* Force release all control (for cleanup)
|
|
343
582
|
*/
|
|
344
583
|
forceReleaseAll(): void {
|
|
584
|
+
for (const tabId of this.focusedTabs.values()) {
|
|
585
|
+
this.emitFocus(tabId, false, this.tabOwnership.get(tabId)?.projectId);
|
|
586
|
+
}
|
|
587
|
+
|
|
345
588
|
// Emit control-end for all controlled tabs
|
|
346
589
|
for (const [tabId, info] of this.tabOwnership) {
|
|
590
|
+
this.emitActivity(tabId, null, info.projectId);
|
|
347
591
|
this.emitControlEnd(tabId, info.projectId);
|
|
348
592
|
}
|
|
349
593
|
|
|
350
594
|
this.tabOwnership.clear();
|
|
351
595
|
this.sessionTabs.clear();
|
|
596
|
+
this.focusedTabs.clear();
|
|
597
|
+
this.tabActivity.clear();
|
|
598
|
+
this.lastProjectByTab.clear();
|
|
352
599
|
|
|
353
600
|
debug.log('mcp', '🧹 Force released all MCP control');
|
|
354
601
|
}
|
|
@@ -358,11 +605,22 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
358
605
|
// ============================================================================
|
|
359
606
|
|
|
360
607
|
/**
|
|
361
|
-
* 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.
|
|
362
616
|
*/
|
|
363
617
|
emitCursorPosition(tabId: string, x: number, y: number, pressed = false): void {
|
|
618
|
+
const ownership = this.tabOwnership.get(tabId);
|
|
619
|
+
if (!ownership) return;
|
|
620
|
+
|
|
364
621
|
const event: McpCursorEvent = {
|
|
365
622
|
tabId,
|
|
623
|
+
projectId: ownership.projectId,
|
|
366
624
|
x,
|
|
367
625
|
y,
|
|
368
626
|
pressed,
|
|
@@ -374,11 +632,16 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
374
632
|
}
|
|
375
633
|
|
|
376
634
|
/**
|
|
377
|
-
* Emit cursor click event with MCP source
|
|
635
|
+
* Emit cursor click event with MCP source. Silent after release — see
|
|
636
|
+
* `emitCursorPosition`.
|
|
378
637
|
*/
|
|
379
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
|
+
|
|
380
642
|
const event: McpClickEvent = {
|
|
381
643
|
tabId,
|
|
644
|
+
projectId: ownership.projectId,
|
|
382
645
|
x,
|
|
383
646
|
y,
|
|
384
647
|
button,
|
|
@@ -390,14 +653,39 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
390
653
|
}
|
|
391
654
|
|
|
392
655
|
/**
|
|
393
|
-
*
|
|
656
|
+
* Say what the agent is doing on a tab.
|
|
657
|
+
*
|
|
658
|
+
* Repeats are dropped: a batch of eight `type` actions would otherwise
|
|
659
|
+
* relabel the cursor eight times with the same word, and every one of those
|
|
660
|
+
* is a re-render on every viewer.
|
|
394
661
|
*/
|
|
395
|
-
|
|
396
|
-
this.
|
|
662
|
+
emitActivity(tabId: string, label: string | null, projectId?: string): void {
|
|
663
|
+
if ((this.tabActivity.get(tabId) ?? null) === label) return;
|
|
664
|
+
|
|
665
|
+
if (label === null) this.tabActivity.delete(tabId);
|
|
666
|
+
else this.tabActivity.set(tabId, label);
|
|
667
|
+
|
|
668
|
+
const event: McpActivityEvent = {
|
|
397
669
|
tabId,
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
670
|
+
// Explicit on release, where ownership has already been dropped and
|
|
671
|
+
// there is nothing left to look the project up in.
|
|
672
|
+
projectId: projectId ?? this.tabOwnership.get(tabId)?.projectId ?? this.lastProjectByTab.get(tabId),
|
|
673
|
+
label,
|
|
674
|
+
timestamp: Date.now()
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
this.emit('activity', event);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** What the agent last said it was doing on a tab, for a late-joining viewer. */
|
|
681
|
+
getActivity(tabId: string): string | null {
|
|
682
|
+
return this.tabActivity.get(tabId) ?? null;
|
|
683
|
+
}
|
|
684
|
+
|
|
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);
|
|
401
689
|
}
|
|
402
690
|
|
|
403
691
|
// ============================================================================
|
|
@@ -430,6 +718,28 @@ export class BrowserMcpControl extends EventEmitter {
|
|
|
430
718
|
|
|
431
719
|
debug.log('mcp', `📢 Emitted mcp:control-end for tab: ${browserTabId}`);
|
|
432
720
|
}
|
|
721
|
+
|
|
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
|
+
|
|
731
|
+
const event: McpFocusEvent = {
|
|
732
|
+
browserTabId,
|
|
733
|
+
focused,
|
|
734
|
+
projectId: owningProject,
|
|
735
|
+
chatSessionId,
|
|
736
|
+
timestamp: Date.now()
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
this.emit('control-focus', event);
|
|
740
|
+
|
|
741
|
+
debug.log('mcp', `📢 Emitted mcp:control-focus ${focused ? 'on' : 'off'} for ${browserTabId}`);
|
|
742
|
+
}
|
|
433
743
|
}
|
|
434
744
|
|
|
435
745
|
// Singleton instance
|