@revoengine/cli 1.0.39 → 2.0.0
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/CHANGELOG.md +14 -0
- package/README.md +7 -3
- package/dist/src/argument-contract.js +1 -1
- package/dist/src/automation-contract.generated.d.ts +6 -30
- package/dist/src/automation-contract.generated.js +6 -30
- package/dist/src/commands/component.js +16 -0
- package/dist/src/commands/terminal-browser-human-input.d.ts +28 -0
- package/dist/src/commands/terminal-browser-human-input.js +56 -7
- package/dist/src/commands/terminal-browser-live-protocol.d.ts +9 -1
- package/dist/src/commands/terminal-browser-live-protocol.js +16 -3
- package/dist/src/commands/terminal-browser-live-transport.d.ts +2 -0
- package/dist/src/commands/terminal-browser-live-transport.js +21 -5
- package/dist/src/commands/terminal-browser-live.d.ts +18 -1
- package/dist/src/commands/terminal-browser-live.js +190 -14
- package/dist/src/commands/terminal-browser-observation.d.ts +1 -1
- package/dist/src/commands/terminal-browser-observation.js +10 -1
- package/dist/src/commands/terminal-browser-page-input.d.ts +1 -0
- package/dist/src/commands/terminal-browser-page-input.js +20 -0
- package/dist/src/commands/terminal-browser-profile-sync.d.ts +5 -1
- package/dist/src/commands/terminal-browser-profile-sync.js +13 -2
- package/dist/src/commands/terminal-browser-profile-sync.protocol.d.ts +2 -0
- package/dist/src/commands/terminal-browser.d.ts +17 -0
- package/dist/src/commands/terminal-browser.js +310 -104
- package/dist/src/commands/terminal.js +104 -24
- package/dist/src/endpoint-emulator.js +9 -10
- package/dist/src/project.js +1 -0
- package/dist/src/request-contract.d.ts +2 -0
- package/dist/src/request-contract.js +11 -1
- package/dist/src/types.d.ts +0 -3
- package/dist/src/ui.js +1 -1
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { canUseOtherBrowserTab } from "./terminal-browser-live-protocol.js";
|
|
1
2
|
import fs from 'node:fs';
|
|
2
3
|
import { readPublicWebPage } from "./terminal-public-web.js";
|
|
3
4
|
import path from 'node:path';
|
|
@@ -11,8 +12,10 @@ import { applyPageInteraction, clearSecretInputs, releasePageInput, describeFocu
|
|
|
11
12
|
const MAX_BROWSER_SESSIONS = 4;
|
|
12
13
|
const BROWSER_SESSION_IDLE_MS = 60 * 60_000;
|
|
13
14
|
const MAX_BROWSER_TABS = 16;
|
|
15
|
+
const MAX_SHARED_TABS = 32;
|
|
14
16
|
const browserSessions = new Map();
|
|
15
|
-
const creatingSessions = new
|
|
17
|
+
const creatingSessions = new Map();
|
|
18
|
+
const sharedContexts = new Map();
|
|
16
19
|
const sessionQueues = new Map();
|
|
17
20
|
const FORM_ACTIONS = new Set(['click', 'fill', 'check', 'select', 'press']);
|
|
18
21
|
const INTERACTIVE_ACTIONS = new Set([...FORM_ACTIONS, 'scroll', 'interact']);
|
|
@@ -140,6 +143,15 @@ async function closeSession(id, options = {}) {
|
|
|
140
143
|
await session.live?.settled();
|
|
141
144
|
if (session.networkLeaseTimer)
|
|
142
145
|
clearTimeout(session.networkLeaseTimer);
|
|
146
|
+
if (session.shared) {
|
|
147
|
+
try {
|
|
148
|
+
await Promise.all([...session.tabs.values()].map(page => page.close().catch(() => undefined)));
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
await releaseSharedMember(id, session.shared, session.sharedMember);
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
143
155
|
try {
|
|
144
156
|
await session.browser.close().catch(() => undefined);
|
|
145
157
|
}
|
|
@@ -148,9 +160,32 @@ async function closeSession(id, options = {}) {
|
|
|
148
160
|
await session.authProfile?.release();
|
|
149
161
|
}
|
|
150
162
|
}
|
|
163
|
+
async function releaseSharedMember(id, group, member) {
|
|
164
|
+
if (!group.members.delete(id))
|
|
165
|
+
return;
|
|
166
|
+
if (!group.members.size) {
|
|
167
|
+
// Dispose the lease before yielding so a late command cannot recreate this generation.
|
|
168
|
+
sharedContexts.delete(member.contextId);
|
|
169
|
+
const released = group.profile.lease.release();
|
|
170
|
+
const resources = await group.resources.catch(() => undefined);
|
|
171
|
+
await resources?.browser.close().catch(() => undefined);
|
|
172
|
+
await released;
|
|
173
|
+
}
|
|
174
|
+
await member.releaseMember();
|
|
175
|
+
}
|
|
151
176
|
async function checkpointProfile(session) {
|
|
152
177
|
if (!session.authProfile)
|
|
153
178
|
return;
|
|
179
|
+
if (session.shared) {
|
|
180
|
+
const group = session.shared;
|
|
181
|
+
const save = group.snapshots.then(async () => {
|
|
182
|
+
if (!session.sharedMember?.authorized())
|
|
183
|
+
throw new Error('browser_profile_lease_expired');
|
|
184
|
+
await group.profile.lease.save(await session.context.storageState({ indexedDB: true }));
|
|
185
|
+
});
|
|
186
|
+
group.snapshots = save.catch(() => undefined);
|
|
187
|
+
return save;
|
|
188
|
+
}
|
|
154
189
|
session.snapshotting = true;
|
|
155
190
|
try {
|
|
156
191
|
await session.authProfile.save(await session.context.storageState({ indexedDB: true }));
|
|
@@ -224,7 +259,7 @@ export async function reconcileBrowserSessions(networkMode, policyGrantRevision)
|
|
|
224
259
|
if (session.threadScope && policyGrantRevision !== undefined
|
|
225
260
|
&& session.threadScope.policyGrantRevision !== policyGrantRevision)
|
|
226
261
|
return true;
|
|
227
|
-
if (!permanent && !session.threadScope
|
|
262
|
+
if (!permanent && !session.threadScope && !session.shared
|
|
228
263
|
&& (session.networkAccessExpiresAt === null || session.networkAccessExpiresAt <= now))
|
|
229
264
|
return true;
|
|
230
265
|
setNetworkLease(id, session, session.networkAccessExpiresAt);
|
|
@@ -242,50 +277,46 @@ export async function closeThreadBrowserSession(browserSessionId) {
|
|
|
242
277
|
await closeSession(browserSessionId);
|
|
243
278
|
}
|
|
244
279
|
export function browserSessionIds() { return [...browserSessions.keys()]; }
|
|
280
|
+
/** Local runner display. Authentication itself is known only to the website. */
|
|
281
|
+
export function browserRuntimeInventory() {
|
|
282
|
+
const contexts = new Set();
|
|
283
|
+
const sessions = [...browserSessions].flatMap(([id, session]) => {
|
|
284
|
+
if (!session.browser.isConnected())
|
|
285
|
+
return [];
|
|
286
|
+
const tabs = [...session.tabs.values()].filter(page => !page.isClosed()).length;
|
|
287
|
+
if (!tabs)
|
|
288
|
+
return [];
|
|
289
|
+
contexts.add(session.context);
|
|
290
|
+
const name = session.centralProfile?.name || session.profileScope?.name;
|
|
291
|
+
return [{ id: id.slice(0, 8), tabs, mode: session.control.mode,
|
|
292
|
+
profile: name?.replace(/[\p{C}\p{Zl}\p{Zp}]/gu, '').slice(0, 64) || null,
|
|
293
|
+
restored: !!session.authProfile?.state }];
|
|
294
|
+
});
|
|
295
|
+
return { browsers: contexts.size, tabs: sessions.reduce((total, session) => total + session.tabs, 0), sessions };
|
|
296
|
+
}
|
|
245
297
|
export function hasBrowserSessions() { return browserSessions.size > 0; }
|
|
246
298
|
/** Revocation closes only this central context and never checkpoints its authentication. */
|
|
247
299
|
export async function revokeBrowserProfileSession(browserSessionId, profileId) {
|
|
248
300
|
if (browserSessions.get(browserSessionId)?.centralProfile?.id === profileId)
|
|
249
301
|
await closeSession(browserSessionId);
|
|
250
302
|
}
|
|
251
|
-
async function
|
|
252
|
-
await
|
|
253
|
-
if (browserSessions.size + creatingSessions.size >= MAX_BROWSER_SESSIONS)
|
|
254
|
-
throw new Error('browser_session_limit');
|
|
255
|
-
creatingSessions.add(id);
|
|
256
|
-
let profileLease;
|
|
257
|
-
try {
|
|
258
|
-
profileLease = centralProfile?.lease ?? (authProfile ? await authProfile.store.openAsync(authProfile.scope) : undefined);
|
|
259
|
-
}
|
|
260
|
-
catch (error) {
|
|
261
|
-
creatingSessions.delete(id);
|
|
262
|
-
throw error;
|
|
263
|
-
}
|
|
264
|
-
const epoch = sessionEpoch;
|
|
265
|
-
const { chromium } = await import('playwright-core').catch(async (error) => {
|
|
266
|
-
await profileLease?.release();
|
|
267
|
-
creatingSessions.delete(id);
|
|
268
|
-
throw error;
|
|
269
|
-
});
|
|
303
|
+
async function createBrowserContext(localOnly, width, height, owner, profileLease) {
|
|
304
|
+
const { chromium } = await import('playwright-core');
|
|
270
305
|
const browser = await chromium.launch({ channel: 'chromium', headless: true, ignoreDefaultArgs: ['--hide-scrollbars'],
|
|
271
306
|
args: localOnly ? ['--disable-background-networking',
|
|
272
307
|
'--host-resolver-rules=MAP localhost 127.0.0.1'] : [] }).catch(error => {
|
|
273
308
|
if (error instanceof Error && error.message.includes("Executable doesn't exist at"))
|
|
274
309
|
throw new Error('browser_binary_unavailable');
|
|
275
310
|
throw error;
|
|
276
|
-
})
|
|
277
|
-
const onAbort = () => { void browser.close(); };
|
|
278
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
311
|
+
});
|
|
279
312
|
try {
|
|
280
|
-
if (signal?.aborted)
|
|
281
|
-
throw new Error('browser_cancelled');
|
|
282
313
|
const context = await browser.newContext({ viewport: { width, height }, acceptDownloads: false,
|
|
283
314
|
serviceWorkers: localOnly ? 'block' : 'allow' });
|
|
284
315
|
const geometryBinding = 'revoGeometry_' + randomUUID().replaceAll('-', '');
|
|
285
316
|
await context.exposeBinding(geometryBinding, ({ page }) => {
|
|
286
|
-
const session =
|
|
317
|
+
const session = owner(page);
|
|
287
318
|
if (session && session.tabs.size && [...session.tabs.values()].includes(page))
|
|
288
|
-
session.observations.invalidate();
|
|
319
|
+
session.observations.invalidate(page);
|
|
289
320
|
});
|
|
290
321
|
await context.addInitScript(({ binding }) => {
|
|
291
322
|
const invalidate = () => { void Reflect.get(globalThis, binding)().catch(() => undefined); };
|
|
@@ -306,7 +337,12 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
306
337
|
await context.route('**/*', async (route) => {
|
|
307
338
|
try {
|
|
308
339
|
(localOnly ? localBrowserUrl : browserUrl)(route.request().url());
|
|
309
|
-
|
|
340
|
+
let page;
|
|
341
|
+
try {
|
|
342
|
+
page = route.request().frame().page();
|
|
343
|
+
}
|
|
344
|
+
catch { /* An initial popup request may lack a frame. */ }
|
|
345
|
+
const live = page && owner(page)?.live;
|
|
310
346
|
// Do not evaluate the page while holding a navigation route: evaluation can
|
|
311
347
|
// wait for that very navigation. Fence capture, then let the request continue.
|
|
312
348
|
if (route.request().isNavigationRequest() && browserAuthPage(route.request().url()) && live && !live.private)
|
|
@@ -330,14 +366,79 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
330
366
|
socket.close();
|
|
331
367
|
}
|
|
332
368
|
});
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
369
|
+
const state = profileLease?.state;
|
|
370
|
+
if (state)
|
|
371
|
+
await context.setStorageState(state);
|
|
372
|
+
return { browser, context };
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
await browser.close().catch(() => undefined);
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
async function createSession(id, url, localOnly, width, height, signal, onNavigationAttempt, actionTimeoutMs, authProfile, centralProfile, recording) {
|
|
380
|
+
await pruneIdleSessions();
|
|
381
|
+
const contextId = centralProfile?.shared?.contextId ?? id;
|
|
382
|
+
const contexts = new Set([...creatingSessions.values(),
|
|
383
|
+
...[...browserSessions].map(([sessionId, session]) => session.sharedMember?.contextId ?? sessionId)]);
|
|
384
|
+
if (!contexts.has(contextId) && contexts.size >= MAX_BROWSER_SESSIONS)
|
|
385
|
+
throw new Error('browser_session_limit');
|
|
386
|
+
creatingSessions.set(id, contextId);
|
|
387
|
+
let profileLease;
|
|
388
|
+
try {
|
|
389
|
+
profileLease = centralProfile?.lease ?? (authProfile ? await authProfile.store.openAsync(authProfile.scope) : undefined);
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
creatingSessions.delete(id);
|
|
393
|
+
throw error;
|
|
394
|
+
}
|
|
395
|
+
const epoch = sessionEpoch;
|
|
396
|
+
const member = centralProfile?.shared;
|
|
397
|
+
let group;
|
|
398
|
+
let resources;
|
|
399
|
+
let openingPage;
|
|
400
|
+
const onAbort = () => {
|
|
401
|
+
if (group)
|
|
402
|
+
void openingPage?.close().catch(() => undefined);
|
|
403
|
+
else
|
|
404
|
+
void resources?.browser.close().catch(() => undefined);
|
|
405
|
+
};
|
|
406
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
407
|
+
try {
|
|
408
|
+
if (signal?.aborted || member && !member.authorized())
|
|
409
|
+
throw new Error('browser_cancelled');
|
|
410
|
+
if (member) {
|
|
411
|
+
group = sharedContexts.get(member.contextId);
|
|
412
|
+
if (group && (group.profile.lease !== profileLease || group.localOnly !== localOnly))
|
|
413
|
+
throw new Error('browser_profile_scope_changed');
|
|
414
|
+
if (!group) {
|
|
415
|
+
const members = new Set();
|
|
416
|
+
group = { members, profile: centralProfile, localOnly, snapshots: Promise.resolve(),
|
|
417
|
+
resources: createBrowserContext(localOnly, width, height, page => {
|
|
418
|
+
for (const memberId of members) {
|
|
419
|
+
const session = browserSessions.get(memberId);
|
|
420
|
+
if (session && [...session.tabs.values()].includes(page))
|
|
421
|
+
return session;
|
|
422
|
+
}
|
|
423
|
+
return undefined;
|
|
424
|
+
}, profileLease) };
|
|
425
|
+
sharedContexts.set(member.contextId, group);
|
|
426
|
+
}
|
|
427
|
+
group.members.add(id);
|
|
428
|
+
resources = await group.resources;
|
|
429
|
+
}
|
|
430
|
+
else
|
|
431
|
+
resources = await createBrowserContext(localOnly, width, height, () => browserSessions.get(id), profileLease);
|
|
432
|
+
const { browser, context } = resources;
|
|
433
|
+
if (signal?.aborted || member && !member.authorized())
|
|
434
|
+
throw new Error('browser_cancelled');
|
|
435
|
+
const page = openingPage = await context.newPage();
|
|
336
436
|
if (localOnly && !await page.evaluate(() => ['RTCPeerConnection', 'webkitRTCPeerConnection',
|
|
337
437
|
'WebTransport', 'Worker', 'SharedWorker'].every(name => typeof Reflect.get(globalThis, name) === 'undefined'
|
|
338
438
|
&& Object.getOwnPropertyDescriptor(globalThis, name)?.configurable === false)))
|
|
339
439
|
throw new Error('browser_local_network_guard_unavailable');
|
|
340
440
|
const session = { browser, context, page, localOnly, tabs: new Map(),
|
|
441
|
+
...(group && member ? { shared: group, sharedMember: member } : {}),
|
|
341
442
|
observations: new BrowserObservations(), control: { epoch: 0, mode: 'observe', private: false, connected: false },
|
|
342
443
|
...(profileLease ? { authProfile: profileLease, profileScopeId: centralProfile?.id ?? browserProfileScopeId(authProfile.scope),
|
|
343
444
|
...(centralProfile ? { centralProfile: { id: centralProfile.id, name: centralProfile.name } } : { profileScope: authProfile.scope }) } : {}),
|
|
@@ -348,17 +449,32 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
348
449
|
session.workspace = session.owner.workspace;
|
|
349
450
|
session.policyGrantRevision = recording.policyGrantRevision;
|
|
350
451
|
setNetworkLease(id, session, recording.networkAccessExpiresAt);
|
|
351
|
-
browserSessions.set(id, session);
|
|
352
452
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
453
|
+
if (recording || group)
|
|
454
|
+
browserSessions.set(id, session);
|
|
455
|
+
if (!registerTab(session, page))
|
|
456
|
+
throw new Error('browser_tab_limit');
|
|
457
|
+
if (!group)
|
|
458
|
+
context.on('page', opened => {
|
|
459
|
+
if (session.snapshotting)
|
|
460
|
+
return;
|
|
461
|
+
if (session.control.tabId) {
|
|
462
|
+
const heldPage = session.tabs.get(session.control.tabId);
|
|
463
|
+
void opened.opener().then(opener => {
|
|
464
|
+
if (!opener || opener === heldPage || ![...session.tabs.values()].includes(opener))
|
|
465
|
+
return opened.close();
|
|
466
|
+
if (registerTab(session, opened))
|
|
467
|
+
session.page = opened;
|
|
468
|
+
}).catch(() => undefined);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
if (registerTab(session, opened))
|
|
472
|
+
session.page = opened;
|
|
473
|
+
});
|
|
360
474
|
const initialPopup = page.waitForEvent('popup', { timeout: 5_000 })
|
|
361
475
|
.then(async (opened) => {
|
|
476
|
+
if (group && ![...session.tabs.values()].includes(opened))
|
|
477
|
+
return;
|
|
362
478
|
session.page = opened;
|
|
363
479
|
await opened.waitForLoadState('domcontentloaded', { timeout: 5_000 }).catch(() => undefined);
|
|
364
480
|
}).catch(() => undefined);
|
|
@@ -366,6 +482,8 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
366
482
|
await attachBrowserLive({ ...recording.options, sessionOwned: true });
|
|
367
483
|
if (browserAuthPage(url))
|
|
368
484
|
await session.live?.pauseRecording();
|
|
485
|
+
if (signal?.aborted || member && !member.authorized())
|
|
486
|
+
throw new Error('browser_cancelled');
|
|
369
487
|
onNavigationAttempt?.();
|
|
370
488
|
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: actionTimeoutMs ?? 30_000 });
|
|
371
489
|
(localOnly ? localBrowserUrl : browserUrl)(page.url());
|
|
@@ -385,11 +503,18 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
385
503
|
return session;
|
|
386
504
|
}
|
|
387
505
|
catch (error) {
|
|
388
|
-
if (browserSessions.
|
|
506
|
+
if (browserSessions.has(id))
|
|
389
507
|
await closeSession(id);
|
|
508
|
+
else if (group && member) {
|
|
509
|
+
await openingPage?.close().catch(() => undefined);
|
|
510
|
+
await releaseSharedMember(id, group, member);
|
|
511
|
+
}
|
|
390
512
|
else {
|
|
391
|
-
await browser.close().catch(() => undefined);
|
|
392
|
-
|
|
513
|
+
await resources?.browser.close().catch(() => undefined);
|
|
514
|
+
if (member)
|
|
515
|
+
await member.releaseMember();
|
|
516
|
+
else
|
|
517
|
+
await profileLease?.release();
|
|
393
518
|
}
|
|
394
519
|
throw error;
|
|
395
520
|
}
|
|
@@ -401,14 +526,24 @@ async function createSession(id, url, localOnly, width, height, signal, onNaviga
|
|
|
401
526
|
function registerTab(session, page) {
|
|
402
527
|
if ([...session.tabs.values()].includes(page))
|
|
403
528
|
return false;
|
|
404
|
-
|
|
405
|
-
|
|
529
|
+
const sharedTabCount = session.shared
|
|
530
|
+
? [...session.shared.members].reduce((count, id) => count + (browserSessions.get(id)?.tabs.size ?? 0), 0)
|
|
531
|
+
: 0;
|
|
532
|
+
if (session.tabs.size >= MAX_BROWSER_TABS || sharedTabCount >= MAX_SHARED_TABS) {
|
|
533
|
+
void page.close().catch(() => undefined);
|
|
406
534
|
return false;
|
|
407
535
|
}
|
|
408
536
|
const id = randomUUID();
|
|
409
537
|
session.tabs.set(id, page);
|
|
538
|
+
if (session.shared)
|
|
539
|
+
page.on('popup', opened => {
|
|
540
|
+
if (page.isClosed() || session.control.tabId === id || session.sharedMember?.authorized() !== true)
|
|
541
|
+
void opened.close().catch(() => undefined);
|
|
542
|
+
else if (registerTab(session, opened))
|
|
543
|
+
session.page = opened;
|
|
544
|
+
});
|
|
410
545
|
page.on('framenavigated', frame => {
|
|
411
|
-
session.observations.invalidate();
|
|
546
|
+
session.observations.invalidate(page);
|
|
412
547
|
if (frame === page.mainFrame() && browserAuthPage(page.url()))
|
|
413
548
|
void session.live?.pauseRecording().catch(() => session.live?.block());
|
|
414
549
|
});
|
|
@@ -424,26 +559,46 @@ function registerTab(session, page) {
|
|
|
424
559
|
});
|
|
425
560
|
return true;
|
|
426
561
|
}
|
|
427
|
-
async function
|
|
562
|
+
async function openTab(session) {
|
|
563
|
+
if (session.tabs.size >= MAX_BROWSER_TABS)
|
|
564
|
+
throw new Error('browser_tab_limit');
|
|
565
|
+
const page = await session.context.newPage();
|
|
566
|
+
if (session.shared && !registerTab(session, page))
|
|
567
|
+
throw new Error('browser_tab_limit');
|
|
568
|
+
session.page = page;
|
|
569
|
+
return page;
|
|
570
|
+
}
|
|
571
|
+
async function closeTab(id, session, page) {
|
|
572
|
+
if (session.tabs.size === 1) {
|
|
573
|
+
await checkpointProfile(session).catch(() => undefined);
|
|
574
|
+
await closeSession(id);
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
await page.close();
|
|
578
|
+
session.observations.invalidate();
|
|
579
|
+
await checkpointSession(id, session);
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
async function describeSession(id, session, sessionCreated, selectedPage = session?.page) {
|
|
428
583
|
const tabs = [];
|
|
429
584
|
for (const [tabId, page] of session?.tabs || []) {
|
|
430
585
|
if (page.isClosed())
|
|
431
586
|
continue;
|
|
432
587
|
const rawUrl = page.url();
|
|
433
588
|
tabs.push({ id: tabId, title: clipTerminalText(await page.title().catch(() => ''), 200),
|
|
434
|
-
url: rawUrl === 'about:blank' ? rawUrl : safePageUrl(rawUrl), popup: !!await page.opener(), active:
|
|
589
|
+
url: rawUrl === 'about:blank' ? rawUrl : safePageUrl(rawUrl), popup: !!await page.opener(), active: selectedPage === page });
|
|
435
590
|
}
|
|
436
591
|
const active = tabs.find(tab => tab.active);
|
|
437
592
|
return { browserSessionId: id, activeTabId: active?.id || null,
|
|
438
593
|
tabs, pageUrl: active?.url || '', sessionCreated,
|
|
439
|
-
...(active && session ? { browserObservation: session.observations.fresh(
|
|
594
|
+
...(active && session ? { browserObservation: session.observations.fresh(selectedPage, active.id) } : {}),
|
|
440
595
|
...(session?.centralProfile ? { browserProfile: { ...session.centralProfile, persistent: true, default: false } }
|
|
441
596
|
: session?.profileScope ? { browserProfile: { id: session.profileScopeId, name: session.profileScope.name, persistent: true, default: false } } : {}) };
|
|
442
597
|
}
|
|
443
598
|
function activePage(session) {
|
|
444
599
|
if (!session.page.isClosed())
|
|
445
600
|
return session.page;
|
|
446
|
-
const remaining = session.
|
|
601
|
+
const remaining = [...session.tabs.values()].filter(page => !page.isClosed());
|
|
447
602
|
if (!remaining.length)
|
|
448
603
|
throw new Error('browser_session_missing');
|
|
449
604
|
session.page = remaining[remaining.length - 1];
|
|
@@ -536,11 +691,25 @@ export function writePrivateScreenshot(file, bytes) {
|
|
|
536
691
|
}
|
|
537
692
|
async function runForSession(input) {
|
|
538
693
|
const existing = browserSessions.get(input.browserSessionId);
|
|
539
|
-
|
|
540
|
-
|
|
694
|
+
const assertModelAccess = (session) => {
|
|
695
|
+
if (input.signal?.aborted)
|
|
696
|
+
throw new Error('browser_cancelled');
|
|
697
|
+
if (session && session.control.mode !== 'observe'
|
|
698
|
+
&& (!canUseOtherBrowserTab(session.control.tabId, input.tabId, input.action)
|
|
699
|
+
|| session.localOnly !== (input.localOnly === true) || input.networkAccessOnce
|
|
700
|
+
|| !session.browser.isConnected()
|
|
701
|
+
|| session.networkAccessExpiresAt !== null && session.networkAccessExpiresAt <= Date.now()
|
|
702
|
+
|| session.networkAccessExpiresAt !== (input.networkAccessExpiresAt ?? null)))
|
|
703
|
+
throw new Error('browser_human_control_active');
|
|
704
|
+
};
|
|
705
|
+
assertModelAccess(existing);
|
|
706
|
+
const parallelTab = !!existing && existing.control.mode !== 'observe';
|
|
541
707
|
if (input.signal?.aborted)
|
|
542
708
|
throw new Error('browser_cancelled');
|
|
543
709
|
validateBrowserActionInput(input);
|
|
710
|
+
const shared = input.centralProfile?.shared;
|
|
711
|
+
if (shared && (!shared.authorized() || input.networkAccessOnce || input.networkAccessExpiresAt != null))
|
|
712
|
+
throw new Error('browser_profile_lease_expired');
|
|
544
713
|
const url = input.localOnly && !['tabs', 'close', 'close_tab'].includes(input.action) ? localBrowserUrl(input.url) : browserUrl(input.url);
|
|
545
714
|
let width = input.width ?? existing?.page.viewportSize()?.width ?? input.recording?.options.browserSettings?.width ?? 1440;
|
|
546
715
|
let height = input.height ?? existing?.page.viewportSize()?.height ?? input.recording?.options.browserSettings?.height ?? 900;
|
|
@@ -552,7 +721,7 @@ async function runForSession(input) {
|
|
|
552
721
|
if (session?.centralProfile && !['tabs', 'close', 'close_tab'].includes(input.action)
|
|
553
722
|
&& session.localOnly !== (input.localOnly === true)) {
|
|
554
723
|
// A new network boundary requires fresh navigation, never a replayed click/form action.
|
|
555
|
-
if (input.action !== 'navigate' || input.tabId)
|
|
724
|
+
if (session.shared || input.action !== 'navigate' || input.tabId)
|
|
556
725
|
throw new Error('browser_profile_navigation_required');
|
|
557
726
|
await checkpointSession(input.browserSessionId, session);
|
|
558
727
|
const viewport = session.page.viewportSize();
|
|
@@ -562,8 +731,8 @@ async function runForSession(input) {
|
|
|
562
731
|
await closeSession(input.browserSessionId, { retainProfileLease: true });
|
|
563
732
|
session = undefined;
|
|
564
733
|
}
|
|
565
|
-
if (session && (!['tabs', 'close', 'close_tab'].includes(input.action) && session.localOnly !== (input.localOnly === true) || !session.browser.isConnected()
|
|
566
|
-
|| session.
|
|
734
|
+
if (!parallelTab && session && (!['tabs', 'close', 'close_tab'].includes(input.action) && session.localOnly !== (input.localOnly === true) || !session.browser.isConnected()
|
|
735
|
+
|| [...session.tabs.values()].every(page => page.isClosed())
|
|
567
736
|
|| Date.now() - session.lastUsedAt >= BROWSER_SESSION_IDLE_MS)) {
|
|
568
737
|
await closeSession(input.browserSessionId);
|
|
569
738
|
session = undefined;
|
|
@@ -592,10 +761,10 @@ async function runForSession(input) {
|
|
|
592
761
|
session.owner = owner;
|
|
593
762
|
}
|
|
594
763
|
// Inventory and cleanup cannot renew, remove or widen an existing network lease.
|
|
595
|
-
if (!['tabs', 'close_tab'].includes(input.action)) {
|
|
764
|
+
if (!parallelTab && !['tabs', 'close_tab'].includes(input.action)) {
|
|
596
765
|
session.workspace = input.agentTerminalWorkspaceId ? input.workspace : undefined;
|
|
597
766
|
session.policyGrantRevision = input.policyGrantRevision;
|
|
598
|
-
session.threadScope = input.networkAccessForThread === true ? { actorUserId: input.actorUserId,
|
|
767
|
+
session.threadScope = !session.shared && input.networkAccessForThread === true ? { actorUserId: input.actorUserId,
|
|
599
768
|
threadId: input.threadId, ...(input.agentTerminalWorkspaceId ? { agentTerminalWorkspaceId: input.agentTerminalWorkspaceId } : {}), policyGrantRevision: input.policyGrantRevision } : undefined;
|
|
600
769
|
try {
|
|
601
770
|
setNetworkLease(input.browserSessionId, session, input.networkAccessExpiresAt ?? null);
|
|
@@ -606,12 +775,14 @@ async function runForSession(input) {
|
|
|
606
775
|
}
|
|
607
776
|
}
|
|
608
777
|
if (input.signal?.aborted) {
|
|
609
|
-
|
|
778
|
+
if (!parallelTab)
|
|
779
|
+
await closeSession(input.browserSessionId);
|
|
610
780
|
throw new Error('browser_cancelled');
|
|
611
781
|
}
|
|
612
|
-
if (input.recording && session.live?.sessionOwned)
|
|
782
|
+
if (!parallelTab && input.recording && session.live?.sessionOwned)
|
|
613
783
|
session.live.refreshRecordingAuthority(input.recording.options);
|
|
614
|
-
const onAbort = () => {
|
|
784
|
+
const onAbort = () => { if (!parallelTab && !session.control.tabId)
|
|
785
|
+
void closeSession(input.browserSessionId); };
|
|
615
786
|
input.signal?.addEventListener('abort', onAbort, { once: true });
|
|
616
787
|
session.active = true;
|
|
617
788
|
try {
|
|
@@ -621,7 +792,7 @@ async function runForSession(input) {
|
|
|
621
792
|
const selected = session.tabs.get(input.tabId);
|
|
622
793
|
if (!selected || selected.isClosed())
|
|
623
794
|
throw new Error('browser_tab_missing');
|
|
624
|
-
if (session.page !== selected)
|
|
795
|
+
if (session.page !== selected && !parallelTab)
|
|
625
796
|
session.observations.invalidate();
|
|
626
797
|
session.page = selected;
|
|
627
798
|
}
|
|
@@ -629,41 +800,32 @@ async function runForSession(input) {
|
|
|
629
800
|
return { dom: '', ...await describeSession(input.browserSessionId, session, created) };
|
|
630
801
|
if (input.action === 'close_tab') {
|
|
631
802
|
input.onActionAttempt?.();
|
|
632
|
-
await activePage(session)
|
|
633
|
-
|
|
634
|
-
if (!remaining.length) {
|
|
635
|
-
await closeSession(input.browserSessionId);
|
|
636
|
-
return { dom: '', ...await describeSession(input.browserSessionId, undefined, false) };
|
|
637
|
-
}
|
|
638
|
-
session.page = remaining[remaining.length - 1];
|
|
639
|
-
await checkpointSession(input.browserSessionId, session);
|
|
640
|
-
return { dom: '', ...await describeSession(input.browserSessionId, session, false) };
|
|
803
|
+
const closed = await closeTab(input.browserSessionId, session, activePage(session));
|
|
804
|
+
return { dom: '', ...await describeSession(input.browserSessionId, closed ? undefined : session, false) };
|
|
641
805
|
}
|
|
642
806
|
if (['navigate', 'new_tab'].includes(input.action) && browserAuthPage(url))
|
|
643
807
|
await session.live?.pauseRecording();
|
|
644
808
|
if (input.action === 'new_tab' && !created) {
|
|
645
|
-
|
|
646
|
-
throw new Error('browser_tab_limit');
|
|
647
|
-
session.page = await session.context.newPage();
|
|
809
|
+
const opened = await openTab(session);
|
|
648
810
|
input.onActionAttempt?.();
|
|
649
|
-
await
|
|
811
|
+
await opened.goto(url, { waitUntil: 'domcontentloaded', timeout: input.actionTimeoutMs ?? 30_000 });
|
|
650
812
|
}
|
|
651
813
|
let page = activePage(session);
|
|
652
|
-
|
|
814
|
+
if (!session.shared && !parallelTab)
|
|
815
|
+
await page.bringToFront();
|
|
653
816
|
if (!['interact', 'dom'].includes(input.action) || input.width != null || input.height != null) {
|
|
654
817
|
const old = page.viewportSize();
|
|
655
818
|
if (old?.width !== width || old?.height !== height)
|
|
656
|
-
session.observations.invalidate();
|
|
819
|
+
session.observations.invalidate(page);
|
|
657
820
|
await page.setViewportSize({ width, height });
|
|
658
821
|
}
|
|
659
822
|
if (input.action === 'navigate' && !created) {
|
|
660
|
-
|
|
661
|
-
throw new Error('browser_human_control_active');
|
|
823
|
+
assertModelAccess(session);
|
|
662
824
|
input.onActionAttempt?.();
|
|
663
825
|
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: input.actionTimeoutMs ?? 30_000 });
|
|
664
826
|
}
|
|
665
827
|
(input.localOnly ? localBrowserUrl : browserUrl)(page.url());
|
|
666
|
-
if (session.live && await pageHasSecretFocus(page))
|
|
828
|
+
if (!parallelTab && session.live && await pageHasSecretFocus(page))
|
|
667
829
|
await session.live.pauseRecording();
|
|
668
830
|
const actionPage = page;
|
|
669
831
|
let target = input.selector ? page.locator(input.selector) : page.locator('body');
|
|
@@ -673,11 +835,10 @@ async function runForSession(input) {
|
|
|
673
835
|
throw new Error('browser_tab_missing');
|
|
674
836
|
const observation = session.observations.require(input.interaction.observationId, page, tabId);
|
|
675
837
|
await applyPageInteraction(page, input.interaction, observation, false, () => {
|
|
676
|
-
|
|
677
|
-
throw new Error('browser_human_control_active');
|
|
838
|
+
assertModelAccess(session);
|
|
678
839
|
input.onActionAttempt?.();
|
|
679
840
|
});
|
|
680
|
-
session.observations.invalidate();
|
|
841
|
+
session.observations.invalidate(page);
|
|
681
842
|
page = activePage(session);
|
|
682
843
|
}
|
|
683
844
|
else if (FORM_ACTIONS.has(input.action)) {
|
|
@@ -700,13 +861,12 @@ async function runForSession(input) {
|
|
|
700
861
|
? 'browser_selector_missing' : 'browser_selector_ambiguous');
|
|
701
862
|
if (input.action === 'fill')
|
|
702
863
|
await guardSecretFill(page, input.selector);
|
|
703
|
-
|
|
704
|
-
throw new Error('browser_human_control_active');
|
|
864
|
+
assertModelAccess(session);
|
|
705
865
|
const popup = input.action === 'click' || input.action === 'press'
|
|
706
866
|
? actionPage.waitForEvent('popup', { timeout: 750 }).catch(() => undefined)
|
|
707
867
|
: Promise.resolve(undefined);
|
|
708
868
|
input.onActionAttempt?.();
|
|
709
|
-
session.observations.invalidate();
|
|
869
|
+
session.observations.invalidate(page);
|
|
710
870
|
if (input.action === 'click') {
|
|
711
871
|
await target.click({ timeout: input.actionTimeoutMs ?? 10_000 });
|
|
712
872
|
}
|
|
@@ -723,7 +883,7 @@ async function runForSession(input) {
|
|
|
723
883
|
else
|
|
724
884
|
await target.press(input.key, { timeout: input.actionTimeoutMs ?? 10_000 });
|
|
725
885
|
const opened = await popup;
|
|
726
|
-
if (opened && !opened.isClosed())
|
|
886
|
+
if (opened && !opened.isClosed() && (!session.shared || [...session.tabs.values()].includes(opened)))
|
|
727
887
|
session.page = opened;
|
|
728
888
|
page = activePage(session);
|
|
729
889
|
if (page !== actionPage)
|
|
@@ -733,16 +893,15 @@ async function runForSession(input) {
|
|
|
733
893
|
else if (input.action === 'scroll') {
|
|
734
894
|
input.onActionAttempt?.();
|
|
735
895
|
await page.mouse.wheel(0, input.deltaY);
|
|
736
|
-
session.observations.invalidate();
|
|
896
|
+
session.observations.invalidate(page);
|
|
737
897
|
}
|
|
738
898
|
else if (input.action === 'wait_for') {
|
|
739
899
|
await target.waitFor({ state: input.state, timeout: Math.min(input.waitTimeoutMs, input.actionTimeoutMs ?? input.waitTimeoutMs) });
|
|
740
900
|
}
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
throw new Error('browser_human_control_active');
|
|
901
|
+
assertModelAccess(session);
|
|
902
|
+
if (!parallelTab)
|
|
903
|
+
await checkpointSession(input.browserSessionId, session);
|
|
904
|
+
assertModelAccess(session);
|
|
746
905
|
if (activePage(session) !== page) {
|
|
747
906
|
page = activePage(session);
|
|
748
907
|
(input.localOnly ? localBrowserUrl : browserUrl)(page.url());
|
|
@@ -753,10 +912,12 @@ async function runForSession(input) {
|
|
|
753
912
|
const bodyText = clipTerminalText(await domTarget.innerText({ timeout: input.actionTimeoutMs ?? 10_000 }), 25_000);
|
|
754
913
|
const dom = clipTerminalText(bodyText + await semanticControls(page), 40_000);
|
|
755
914
|
const pageUrl = safePageUrl(page.url());
|
|
756
|
-
|
|
915
|
+
if (!parallelTab)
|
|
916
|
+
await session.live?.resumeRecordingIfPublic();
|
|
757
917
|
const inventory = await describeSession(input.browserSessionId, session, created);
|
|
758
|
-
if (session.control.
|
|
759
|
-
|
|
918
|
+
if (session.control.tabId)
|
|
919
|
+
inventory.tabs = inventory.tabs.map(tab => tab.id === session.control.tabId ? { ...tab, title: '', url: '' } : tab);
|
|
920
|
+
assertModelAccess(session);
|
|
760
921
|
if (input.action !== 'screenshot')
|
|
761
922
|
return { dom, ...inventory, pageUrl };
|
|
762
923
|
const root = fs.realpathSync(input.workspace);
|
|
@@ -792,8 +953,7 @@ async function runForSession(input) {
|
|
|
792
953
|
}
|
|
793
954
|
if (bytes.length > 30 * 1024 * 1024)
|
|
794
955
|
throw new Error('browser_screenshot_too_large');
|
|
795
|
-
|
|
796
|
-
throw new Error('browser_human_control_active');
|
|
956
|
+
assertModelAccess(session);
|
|
797
957
|
writePrivateScreenshot(screenshotPath, bytes);
|
|
798
958
|
inventory.browserObservation = session.observations.fresh(page, inventory.activeTabId);
|
|
799
959
|
const imageWidth = bytes.readUInt32BE(16);
|
|
@@ -825,8 +985,9 @@ export async function runBrowserAction(input) {
|
|
|
825
985
|
return { dom, pageUrl: safePageUrl(JSON.parse(dom).url), browserSessionId: input.browserSessionId,
|
|
826
986
|
activeTabId: null, tabs: [], sessionCreated: false };
|
|
827
987
|
}
|
|
828
|
-
|
|
829
|
-
|
|
988
|
+
const existing = browserSessions.get(input.browserSessionId);
|
|
989
|
+
if (existing && existing.control.mode !== 'observe'
|
|
990
|
+
&& !canUseOtherBrowserTab(existing.control.tabId, input.tabId, input.action))
|
|
830
991
|
throw new Error('browser_human_control_active');
|
|
831
992
|
const previous = sessionQueues.get(input.browserSessionId) || Promise.resolve();
|
|
832
993
|
const task = previous.catch(() => undefined).then(() => runForSession(input));
|
|
@@ -931,6 +1092,7 @@ export async function attachBrowserLive(options) {
|
|
|
931
1092
|
if (options.initialControl.mode !== 'observe') {
|
|
932
1093
|
session.control.mode = 'blocked';
|
|
933
1094
|
session.control.private = true;
|
|
1095
|
+
session.control.tabId = options.initialControl.tabId;
|
|
934
1096
|
}
|
|
935
1097
|
// A reconnect never auto-releases an existing local human/blocked fence.
|
|
936
1098
|
}
|
|
@@ -946,10 +1108,19 @@ export async function attachBrowserLive(options) {
|
|
|
946
1108
|
await previous.settled();
|
|
947
1109
|
const authorized = () => browserSessions.get(options.browserSessionId) === session && session.browser.isConnected()
|
|
948
1110
|
&& (session.networkAccessExpiresAt === null || session.networkAccessExpiresAt > Date.now());
|
|
1111
|
+
const viewerPage = () => {
|
|
1112
|
+
if (!session.control.tabId)
|
|
1113
|
+
return activePage(session);
|
|
1114
|
+
const page = session.tabs.get(session.control.tabId);
|
|
1115
|
+
if (!page || page.isClosed())
|
|
1116
|
+
throw new Error('browser_tab_missing');
|
|
1117
|
+
return page;
|
|
1118
|
+
};
|
|
1119
|
+
let viewerInput = Promise.resolve(undefined);
|
|
949
1120
|
const handle = new BrowserLiveHandle(options, {
|
|
950
1121
|
control: session.control, observations: session.observations,
|
|
951
1122
|
active: () => {
|
|
952
|
-
const page =
|
|
1123
|
+
const page = viewerPage();
|
|
953
1124
|
const tabId = [...session.tabs].find(([, current]) => current === page)?.[0];
|
|
954
1125
|
if (!tabId)
|
|
955
1126
|
throw new Error('browser_tab_missing');
|
|
@@ -957,7 +1128,17 @@ export async function attachBrowserLive(options) {
|
|
|
957
1128
|
},
|
|
958
1129
|
pages: () => [...session.tabs.values()].filter(page => !page.isClosed()),
|
|
959
1130
|
authorized, fullNetwork: () => authorized() && !session.localOnly,
|
|
960
|
-
queue: async (work) => {
|
|
1131
|
+
queue: async (work, tabInput = false) => {
|
|
1132
|
+
if (tabInput && session.control.tabId) {
|
|
1133
|
+
const task = viewerInput.catch(() => undefined).then(() => {
|
|
1134
|
+
if (!authorized())
|
|
1135
|
+
throw new Error('browser_live_scope_expired');
|
|
1136
|
+
session.lastUsedAt = Date.now();
|
|
1137
|
+
return work();
|
|
1138
|
+
});
|
|
1139
|
+
viewerInput = task;
|
|
1140
|
+
return task;
|
|
1141
|
+
}
|
|
961
1142
|
const previous = sessionQueues.get(options.browserSessionId) || Promise.resolve();
|
|
962
1143
|
const task = previous.catch(() => undefined).then(async () => {
|
|
963
1144
|
if (!authorized())
|
|
@@ -981,8 +1162,9 @@ export async function attachBrowserLive(options) {
|
|
|
981
1162
|
}
|
|
982
1163
|
},
|
|
983
1164
|
state: async () => {
|
|
984
|
-
const
|
|
985
|
-
|
|
1165
|
+
const page = viewerPage();
|
|
1166
|
+
const focusId = await describeFocusedField(page);
|
|
1167
|
+
return { ...await describeSession(options.browserSessionId, session, false, page), focusEditable: !!focusId, focusId };
|
|
986
1168
|
},
|
|
987
1169
|
selectTab: async (tabId) => {
|
|
988
1170
|
const page = session.tabs.get(tabId);
|
|
@@ -992,8 +1174,32 @@ export async function attachBrowserLive(options) {
|
|
|
992
1174
|
session.page = page;
|
|
993
1175
|
await page.bringToFront();
|
|
994
1176
|
},
|
|
1177
|
+
newTab: async () => {
|
|
1178
|
+
const current = new URL(activePage(session).url());
|
|
1179
|
+
const url = (session.localOnly ? localBrowserUrl : browserUrl)(current.origin + current.pathname);
|
|
1180
|
+
const opened = await openTab(session);
|
|
1181
|
+
try {
|
|
1182
|
+
await opened.goto(url, { waitUntil: 'domcontentloaded', timeout: 30_000 });
|
|
1183
|
+
await checkpointSession(options.browserSessionId, session);
|
|
1184
|
+
}
|
|
1185
|
+
catch (error) {
|
|
1186
|
+
await opened.close().catch(() => undefined);
|
|
1187
|
+
throw error;
|
|
1188
|
+
}
|
|
1189
|
+
},
|
|
1190
|
+
closeTab: async (tabId) => {
|
|
1191
|
+
const page = session.tabs.get(tabId);
|
|
1192
|
+
if (!page || page.isClosed())
|
|
1193
|
+
throw new Error('browser_tab_missing');
|
|
1194
|
+
return closeTab(options.browserSessionId, session, page);
|
|
1195
|
+
},
|
|
1196
|
+
closeSession: async () => {
|
|
1197
|
+
await checkpointProfile(session).catch(() => undefined);
|
|
1198
|
+
await closeSession(options.browserSessionId);
|
|
1199
|
+
},
|
|
995
1200
|
release: async () => {
|
|
996
|
-
|
|
1201
|
+
const pages = session.control.tabId ? [viewerPage()] : session.tabs.values();
|
|
1202
|
+
for (const page of pages)
|
|
997
1203
|
if (!page.isClosed()) {
|
|
998
1204
|
await releasePageInput(page);
|
|
999
1205
|
await clearSecretInputs(page);
|