@capybara-hq/plugin-browser 0.1.0-rc.7 → 0.1.0-rc.8
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/.capybara-plugin/plugin.json +3 -3
- package/README.md +59 -10
- package/dist/index.mjs +86 -38
- package/package.json +2 -2
- package/ui/panel.js +149 -41
- package/ui/service/daemon.mjs +1231 -307
package/ui/service/daemon.mjs
CHANGED
|
@@ -69,6 +69,25 @@ var ServiceMessageDecoder = class {
|
|
|
69
69
|
return out;
|
|
70
70
|
}
|
|
71
71
|
};
|
|
72
|
+
function parseViewerContext(body) {
|
|
73
|
+
let msg;
|
|
74
|
+
try {
|
|
75
|
+
const text = typeof body === "string" ? body : new TextDecoder("utf-8", { fatal: true }).decode(body);
|
|
76
|
+
msg = JSON.parse(text);
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (typeof msg !== "object" || msg === null || Array.isArray(msg))
|
|
81
|
+
return null;
|
|
82
|
+
const { kind, conversationId, rootConversationId } = msg;
|
|
83
|
+
if (kind !== "viewer")
|
|
84
|
+
return null;
|
|
85
|
+
if (typeof conversationId !== "string" || conversationId === "")
|
|
86
|
+
return null;
|
|
87
|
+
if (typeof rootConversationId !== "string" || rootConversationId === "")
|
|
88
|
+
return null;
|
|
89
|
+
return { kind: "viewer", conversationId, rootConversationId };
|
|
90
|
+
}
|
|
72
91
|
|
|
73
92
|
// daemon/main.ts
|
|
74
93
|
import { chmodSync, existsSync as existsSync2, mkdirSync as mkdirSync2, unlinkSync as unlinkSync2 } from "node:fs";
|
|
@@ -159,6 +178,10 @@ function encodeFrame(f) {
|
|
|
159
178
|
var DEFAULT_FRAME_MAX_BYTES = 256 * 1024;
|
|
160
179
|
var HANDOFF_MAX_CHARS = 4096;
|
|
161
180
|
var MAX_TABS = 10;
|
|
181
|
+
var MAX_TABS_PER_CONVERSATION = 6;
|
|
182
|
+
var CONVERSATION_IDLE_MS = 15 * 6e4;
|
|
183
|
+
var EVICT_MIN_IDLE_MS = 5 * 6e4;
|
|
184
|
+
var NO_CONVERSATION_MESSAGE = "the request names no conversation";
|
|
162
185
|
var HANDOFF_KEEPALIVE_MS = 24 * 60 * 6e4;
|
|
163
186
|
var DOWNLOAD_BLOCKED_PREFIX = "The browser could not be downloaded into the device: its network settings block ";
|
|
164
187
|
var REPLY_TOO_LARGE_MESSAGE = "The reply was too large to send; ask for less (a smaller maxBytes).";
|
|
@@ -259,27 +282,35 @@ var systemClock = {
|
|
|
259
282
|
schedule: (fn, ms) => setTimeout(fn, ms).unref(),
|
|
260
283
|
cancel: (h) => clearTimeout(h)
|
|
261
284
|
};
|
|
285
|
+
var HANDOFF_PARK = "handoff";
|
|
286
|
+
var HANDOFF_PARK_KEY = "";
|
|
287
|
+
var parkId = (kind, key2) => `${kind}\0${key2}`;
|
|
262
288
|
var DaemonState = class {
|
|
263
289
|
clock;
|
|
264
290
|
idleMs;
|
|
265
291
|
graceMs;
|
|
292
|
+
conversationIdleMs;
|
|
266
293
|
onIdle;
|
|
294
|
+
onConversationIdle;
|
|
267
295
|
listeners = /* @__PURE__ */ new Set();
|
|
296
|
+
conversations = /* @__PURE__ */ new Map();
|
|
268
297
|
viewers = 0;
|
|
269
298
|
inflight = 0;
|
|
270
299
|
since;
|
|
271
|
-
|
|
300
|
+
/** Conversations the running takeover concerns (told on their next ok reply after it ends). */
|
|
301
|
+
concerned = /* @__PURE__ */ new Set();
|
|
302
|
+
/** Hand back notes not yet told: conversation id to when the takeover began. */
|
|
303
|
+
notes = /* @__PURE__ */ new Map();
|
|
272
304
|
idleTimer;
|
|
273
305
|
graceTimer;
|
|
274
306
|
disposed = false;
|
|
275
|
-
pendingHandoff;
|
|
276
|
-
/** When the first notice of the pending handoff arrived (the keepalive counts from it). */
|
|
277
|
-
handoffSince;
|
|
278
307
|
constructor(opts) {
|
|
279
308
|
this.clock = opts.clock ?? systemClock;
|
|
280
309
|
this.idleMs = opts.idleMs ?? IDLE_STOP_MS;
|
|
281
310
|
this.graceMs = opts.graceMs ?? TAKEOVER_GRACE_MS;
|
|
311
|
+
this.conversationIdleMs = opts.conversationIdleMs ?? CONVERSATION_IDLE_MS;
|
|
282
312
|
this.onIdle = opts.onIdle;
|
|
313
|
+
this.onConversationIdle = opts.onConversationIdle ?? (() => void 0);
|
|
283
314
|
this.armIdle();
|
|
284
315
|
}
|
|
285
316
|
get control() {
|
|
@@ -295,66 +326,218 @@ var DaemonState = class {
|
|
|
295
326
|
get requestsInFlight() {
|
|
296
327
|
return this.inflight;
|
|
297
328
|
}
|
|
298
|
-
/** Subscribes to control changes; returns the unsubscribe function. */
|
|
329
|
+
/** Subscribes to control, handoff and park changes; returns the unsubscribe function. */
|
|
299
330
|
onChange(fn) {
|
|
300
331
|
this.listeners.add(fn);
|
|
301
332
|
return () => this.listeners.delete(fn);
|
|
302
333
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
334
|
+
// ---- conversations
|
|
335
|
+
/** The conversations the daemon keeps state for (tests, the session file). */
|
|
336
|
+
get conversationIds() {
|
|
337
|
+
return [...this.conversations.keys()];
|
|
338
|
+
}
|
|
339
|
+
/** Whether the daemon keeps state for the conversation. */
|
|
340
|
+
known(conversationId) {
|
|
341
|
+
return this.conversations.has(conversationId);
|
|
342
|
+
}
|
|
343
|
+
/** The root of a conversation's tree, when the daemon knows the conversation. */
|
|
344
|
+
rootOf(conversationId) {
|
|
345
|
+
return this.conversations.get(conversationId)?.root;
|
|
346
|
+
}
|
|
347
|
+
entry(conversationId, root) {
|
|
348
|
+
let e = this.conversations.get(conversationId);
|
|
349
|
+
if (e === void 0) {
|
|
350
|
+
e = {
|
|
351
|
+
root,
|
|
352
|
+
inflight: 0,
|
|
353
|
+
lastActiveAt: this.clock.now(),
|
|
354
|
+
pins: 0,
|
|
355
|
+
parks: /* @__PURE__ */ new Map(),
|
|
356
|
+
handoff: void 0,
|
|
357
|
+
idleTimer: void 0
|
|
358
|
+
};
|
|
359
|
+
this.conversations.set(conversationId, e);
|
|
360
|
+
}
|
|
361
|
+
return e;
|
|
306
362
|
}
|
|
307
|
-
/**
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
363
|
+
/**
|
|
364
|
+
* Records activity of a conversation that is not a request (it gained a tab: a restore, an
|
|
365
|
+
* adopt, a tab the person opened): its idle countdowns start again.
|
|
366
|
+
*/
|
|
367
|
+
seen(conversationId, root) {
|
|
368
|
+
const e = this.entry(conversationId, root);
|
|
369
|
+
e.lastActiveAt = this.clock.now();
|
|
370
|
+
this.armConversation(conversationId, e);
|
|
371
|
+
}
|
|
372
|
+
/** A request of the conversation arrived (it is in use until {@link requestFinished}). */
|
|
373
|
+
requestArrived(conversationId, root) {
|
|
374
|
+
const e = this.entry(conversationId, root);
|
|
375
|
+
e.inflight++;
|
|
376
|
+
e.lastActiveAt = this.clock.now();
|
|
377
|
+
this.armConversation(conversationId, e);
|
|
378
|
+
}
|
|
379
|
+
/** The conversation's request got its reply. */
|
|
380
|
+
requestFinished(conversationId) {
|
|
381
|
+
const e = this.conversations.get(conversationId);
|
|
382
|
+
if (e === void 0) return;
|
|
383
|
+
e.inflight = Math.max(0, e.inflight - 1);
|
|
384
|
+
e.lastActiveAt = this.clock.now();
|
|
385
|
+
this.armConversation(conversationId, e);
|
|
386
|
+
}
|
|
387
|
+
/** A viewer pins the conversation (its own, or the owner of the tab it shows). */
|
|
388
|
+
pin(conversationId, root) {
|
|
389
|
+
const e = this.entry(conversationId, root);
|
|
390
|
+
e.pins++;
|
|
391
|
+
this.armConversation(conversationId, e);
|
|
392
|
+
}
|
|
393
|
+
unpin(conversationId) {
|
|
394
|
+
const e = this.conversations.get(conversationId);
|
|
395
|
+
if (e === void 0) return;
|
|
396
|
+
e.pins = Math.max(0, e.pins - 1);
|
|
397
|
+
e.lastActiveAt = this.clock.now();
|
|
398
|
+
this.armConversation(conversationId, e);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Whether the conversation is in use: a request in flight or a conversation park. Adopt of its
|
|
402
|
+
* tabs is refused meanwhile, and the cap eviction skips it.
|
|
403
|
+
*/
|
|
404
|
+
busy(conversationId) {
|
|
405
|
+
const e = this.conversations.get(conversationId);
|
|
406
|
+
if (e === void 0) return false;
|
|
407
|
+
if (e.inflight > 0) return true;
|
|
408
|
+
for (const p of e.parks.values()) if (p.tabId === void 0) return true;
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
/** Whether a viewer pins the conversation. */
|
|
412
|
+
pinned(conversationId) {
|
|
413
|
+
return (this.conversations.get(conversationId)?.pins ?? 0) > 0;
|
|
414
|
+
}
|
|
415
|
+
/** How long the conversation has been without activity (unknown: forever). */
|
|
416
|
+
idleFor(conversationId) {
|
|
417
|
+
const e = this.conversations.get(conversationId);
|
|
418
|
+
return e === void 0 ? Number.POSITIVE_INFINITY : this.clock.now() - e.lastActiveAt;
|
|
419
|
+
}
|
|
420
|
+
/** The last activity of the conversation (unknown: never). */
|
|
421
|
+
lastActiveAt(conversationId) {
|
|
422
|
+
return this.conversations.get(conversationId)?.lastActiveAt ?? Number.NEGATIVE_INFINITY;
|
|
423
|
+
}
|
|
424
|
+
// ---- parks
|
|
425
|
+
/** Starts (or renews the spec of) a park; a park of the same kind and key keeps its start. */
|
|
426
|
+
park(conversationId, root, spec) {
|
|
427
|
+
const e = this.entry(conversationId, root);
|
|
428
|
+
const id = parkId(spec.kind, spec.key);
|
|
429
|
+
const since = e.parks.get(id)?.since ?? this.clock.now();
|
|
430
|
+
e.parks.set(id, { ...spec, since });
|
|
431
|
+
this.armConversation(conversationId, e);
|
|
432
|
+
this.armIdle();
|
|
433
|
+
this.emit();
|
|
434
|
+
}
|
|
435
|
+
/** Ends a park; an unknown one changes nothing. */
|
|
436
|
+
unpark(conversationId, kind, key2) {
|
|
437
|
+
const e = this.conversations.get(conversationId);
|
|
438
|
+
if (e === void 0 || !e.parks.delete(parkId(kind, key2))) return;
|
|
439
|
+
e.lastActiveAt = this.clock.now();
|
|
440
|
+
this.armConversation(conversationId, e);
|
|
441
|
+
this.armIdle();
|
|
312
442
|
this.emit();
|
|
443
|
+
}
|
|
444
|
+
/** The conversation's parks. */
|
|
445
|
+
parksOf(conversationId) {
|
|
446
|
+
return [...this.conversations.get(conversationId)?.parks.values() ?? []];
|
|
447
|
+
}
|
|
448
|
+
/** Whether a tab park pins the tab. */
|
|
449
|
+
tabParked(tabId) {
|
|
450
|
+
for (const e of this.conversations.values())
|
|
451
|
+
for (const p of e.parks.values()) if (p.tabId === tabId) return true;
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
/** Drops the tab parks of a tab that closed. */
|
|
455
|
+
tabClosed(tabId) {
|
|
456
|
+
for (const [conv, e] of this.conversations) {
|
|
457
|
+
let changed = false;
|
|
458
|
+
for (const [id, p] of e.parks) if (p.tabId === tabId) changed = e.parks.delete(id) || changed;
|
|
459
|
+
if (changed) this.armConversation(conv, e);
|
|
460
|
+
}
|
|
313
461
|
this.armIdle();
|
|
314
462
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
463
|
+
// ---- handoffs (a park of kind HANDOFF_PARK)
|
|
464
|
+
/** The tree's or every pending handoff, oldest first. */
|
|
465
|
+
handoffs(root) {
|
|
466
|
+
const out = [];
|
|
467
|
+
for (const [conversationId, e] of this.conversations) {
|
|
468
|
+
if (e.handoff === void 0 || root !== void 0 && e.root !== root) continue;
|
|
469
|
+
const since = e.parks.get(parkId(HANDOFF_PARK, HANDOFF_PARK_KEY))?.since ?? 0;
|
|
470
|
+
out.push({ conversationId, rootConversationId: e.root, ...e.handoff, since });
|
|
471
|
+
}
|
|
472
|
+
return out.sort((a, b) => a.since - b.since);
|
|
473
|
+
}
|
|
474
|
+
/** The conversation's pending handoff, if any. */
|
|
475
|
+
handoffOf(conversationId) {
|
|
476
|
+
return this.conversations.get(conversationId)?.handoff;
|
|
477
|
+
}
|
|
478
|
+
/** Records the conversation's handoff question; it replaces an earlier one of the conversation. */
|
|
479
|
+
noticeHandoff(conversationId, root, callId, message) {
|
|
480
|
+
const m = message.length <= HANDOFF_MAX_CHARS ? message : `${message.slice(0, HANDOFF_MAX_CHARS - 1)}\u2026`;
|
|
481
|
+
const e = this.entry(conversationId, root);
|
|
482
|
+
e.handoff = { callId, message: m };
|
|
483
|
+
this.park(conversationId, root, { kind: HANDOFF_PARK, key: HANDOFF_PARK_KEY, persist: true });
|
|
484
|
+
this.emit();
|
|
485
|
+
}
|
|
486
|
+
/** Clears the conversation's pending handoff when it is `callId`'s; a stale clear changes nothing. */
|
|
487
|
+
clearHandoff(conversationId, callId) {
|
|
488
|
+
if (this.conversations.get(conversationId)?.handoff?.callId !== callId) return;
|
|
489
|
+
this.endHandoff(conversationId);
|
|
319
490
|
}
|
|
320
491
|
/**
|
|
321
|
-
* Drops the pending handoff whatever its callId: the next
|
|
322
|
-
* moved on (an aborted run never sends handoff_clear).
|
|
323
|
-
*
|
|
324
|
-
* conversations and the browser is one.
|
|
492
|
+
* Drops the conversation's pending handoff whatever its callId: the conversation's next tool
|
|
493
|
+
* request means its run moved on (an aborted run never sends handoff_clear). Other
|
|
494
|
+
* conversations' handoffs stay.
|
|
325
495
|
*/
|
|
326
|
-
dropHandoff() {
|
|
327
|
-
if (this.
|
|
328
|
-
this.endHandoff();
|
|
329
|
-
}
|
|
330
|
-
endHandoff() {
|
|
331
|
-
|
|
332
|
-
|
|
496
|
+
dropHandoff(conversationId) {
|
|
497
|
+
if (this.conversations.get(conversationId)?.handoff === void 0) return;
|
|
498
|
+
this.endHandoff(conversationId);
|
|
499
|
+
}
|
|
500
|
+
endHandoff(conversationId) {
|
|
501
|
+
const e = this.conversations.get(conversationId);
|
|
502
|
+
if (e === void 0) return;
|
|
503
|
+
e.handoff = void 0;
|
|
504
|
+
this.unpark(conversationId, HANDOFF_PARK, HANDOFF_PARK_KEY);
|
|
333
505
|
this.emit();
|
|
334
|
-
this.armIdle();
|
|
335
506
|
}
|
|
336
|
-
takeover
|
|
507
|
+
// ---- takeover
|
|
508
|
+
/**
|
|
509
|
+
* The person takes control of the browser (every conversation's agent is refused meanwhile).
|
|
510
|
+
* `owners`: the conversations owning a tab now, told on hand back.
|
|
511
|
+
*/
|
|
512
|
+
takeover(owners = []) {
|
|
337
513
|
if (this.since !== void 0) return;
|
|
338
514
|
this.since = this.clock.now();
|
|
339
|
-
this.
|
|
515
|
+
this.concerned = new Set(owners);
|
|
340
516
|
this.emit();
|
|
341
517
|
}
|
|
518
|
+
/** The person used a tab of `conversationId` during the takeover: it is told on hand back. */
|
|
519
|
+
concern(conversationId) {
|
|
520
|
+
if (this.since === void 0 || conversationId === void 0) return;
|
|
521
|
+
this.concerned.add(conversationId);
|
|
522
|
+
}
|
|
342
523
|
handback() {
|
|
343
524
|
if (this.since === void 0) return;
|
|
344
|
-
this.
|
|
525
|
+
for (const c of this.concerned) this.notes.set(c, this.since);
|
|
526
|
+
this.concerned = /* @__PURE__ */ new Set();
|
|
345
527
|
this.since = void 0;
|
|
346
528
|
this.clearGrace();
|
|
347
529
|
this.emit();
|
|
348
530
|
}
|
|
349
531
|
/**
|
|
350
|
-
* When the last takeover began (ISO), once:
|
|
351
|
-
* the agent learns the person drove the browser.
|
|
532
|
+
* When the last takeover that concerned the conversation began (ISO), once: its first ok reply
|
|
533
|
+
* after the hand back carries it so its agent learns the person drove the browser.
|
|
352
534
|
*/
|
|
353
|
-
takeHandbackNote() {
|
|
354
|
-
const n = this.
|
|
355
|
-
this.
|
|
535
|
+
takeHandbackNote(conversationId) {
|
|
536
|
+
const n = this.notes.get(conversationId);
|
|
537
|
+
this.notes.delete(conversationId);
|
|
356
538
|
return n === void 0 ? void 0 : new Date(n).toISOString();
|
|
357
539
|
}
|
|
540
|
+
// ---- viewers and requests (the daemon's own idle stop)
|
|
358
541
|
viewerOpened() {
|
|
359
542
|
this.viewers++;
|
|
360
543
|
this.clearGrace();
|
|
@@ -384,6 +567,10 @@ var DaemonState = class {
|
|
|
384
567
|
this.clearGrace();
|
|
385
568
|
if (this.idleTimer !== void 0) this.clock.cancel(this.idleTimer);
|
|
386
569
|
this.idleTimer = void 0;
|
|
570
|
+
for (const e of this.conversations.values()) {
|
|
571
|
+
if (e.idleTimer !== void 0) this.clock.cancel(e.idleTimer);
|
|
572
|
+
e.idleTimer = void 0;
|
|
573
|
+
}
|
|
387
574
|
this.listeners.clear();
|
|
388
575
|
}
|
|
389
576
|
emit() {
|
|
@@ -394,15 +581,49 @@ var DaemonState = class {
|
|
|
394
581
|
this.graceTimer = void 0;
|
|
395
582
|
}
|
|
396
583
|
/**
|
|
397
|
-
* (Re)starts the idle countdown when
|
|
398
|
-
*
|
|
399
|
-
|
|
584
|
+
* (Re)starts the conversation's idle countdown when it is not in use (no request in flight, no
|
|
585
|
+
* viewer pin, no conversation park); stops it otherwise.
|
|
586
|
+
*/
|
|
587
|
+
armConversation(conversationId, e) {
|
|
588
|
+
if (e.idleTimer !== void 0) this.clock.cancel(e.idleTimer);
|
|
589
|
+
e.idleTimer = void 0;
|
|
590
|
+
if (this.disposed || e.pins > 0 || this.busy(conversationId)) return;
|
|
591
|
+
const wait = Math.max(0, e.lastActiveAt + this.conversationIdleMs - this.clock.now());
|
|
592
|
+
e.idleTimer = this.clock.schedule(() => {
|
|
593
|
+
e.idleTimer = void 0;
|
|
594
|
+
this.idleClose(conversationId, e);
|
|
595
|
+
}, wait);
|
|
596
|
+
}
|
|
597
|
+
/** Closes an idle conversation's tabs and deletes what the daemon keeps for it. */
|
|
598
|
+
idleClose(conversationId, e) {
|
|
599
|
+
if (this.conversations.get(conversationId) !== e) return;
|
|
600
|
+
try {
|
|
601
|
+
this.onConversationIdle(conversationId);
|
|
602
|
+
} finally {
|
|
603
|
+
this.notes.delete(conversationId);
|
|
604
|
+
this.concerned.delete(conversationId);
|
|
605
|
+
if (e.parks.size === 0) this.conversations.delete(conversationId);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
/** When the oldest pending park began, if any. */
|
|
609
|
+
oldestPark() {
|
|
610
|
+
let oldest;
|
|
611
|
+
for (const e of this.conversations.values())
|
|
612
|
+
for (const p of e.parks.values())
|
|
613
|
+
if (oldest === void 0 || p.since < oldest) oldest = p.since;
|
|
614
|
+
return oldest;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* (Re)starts the idle countdown when nothing is in use; stops it otherwise. Pending parks keep
|
|
618
|
+
* the daemon running until {@link HANDOFF_KEEPALIVE_MS} after the oldest one began (the person
|
|
619
|
+
* is still to answer a handoff); the device's idle suspension may still stop the device sooner.
|
|
400
620
|
*/
|
|
401
621
|
armIdle() {
|
|
402
622
|
if (this.idleTimer !== void 0) this.clock.cancel(this.idleTimer);
|
|
403
623
|
this.idleTimer = void 0;
|
|
404
624
|
if (this.disposed || this.viewers > 0 || this.inflight > 0) return;
|
|
405
|
-
const
|
|
625
|
+
const oldest = this.oldestPark();
|
|
626
|
+
const keepalive = oldest === void 0 ? 0 : oldest + HANDOFF_KEEPALIVE_MS - this.clock.now();
|
|
406
627
|
this.idleTimer = this.clock.schedule(
|
|
407
628
|
() => {
|
|
408
629
|
this.idleTimer = void 0;
|
|
@@ -548,6 +769,7 @@ function within(real, root) {
|
|
|
548
769
|
var sameFile = (a, b) => a.dev === b.dev && a.ino === b.ino;
|
|
549
770
|
var REF_RE = /^(f\d+)?e\d+$/;
|
|
550
771
|
var PROXY_FAILURE_RE = /ERR_TUNNEL_CONNECTION_FAILED|ERR_PROXY_CONNECTION_FAILED/;
|
|
772
|
+
var ERROR_PAGE_INTERRUPT_RE = /is interrupted by another navigation to "chrome-error:/;
|
|
551
773
|
var ERROR_PAGE_WAIT_MS = 2e3;
|
|
552
774
|
var isTimeout = (e) => e instanceof Error && e.name === "TimeoutError";
|
|
553
775
|
var NET_ERROR_MEANINGS = {
|
|
@@ -777,6 +999,41 @@ function normalizeUrl(raw) {
|
|
|
777
999
|
return url.href;
|
|
778
1000
|
}
|
|
779
1001
|
var firstLine = (e) => (e instanceof Error ? e.message : String(e)).split("\n")[0]?.replace(/^[a-zA-Z.]+: /, "") ?? "";
|
|
1002
|
+
var NO_USAGE = {
|
|
1003
|
+
busy: () => false,
|
|
1004
|
+
pinned: () => false,
|
|
1005
|
+
idleFor: () => Number.POSITIVE_INFINITY,
|
|
1006
|
+
lastActiveAt: () => Number.NEGATIVE_INFINITY,
|
|
1007
|
+
tabParked: () => false,
|
|
1008
|
+
tabClosed: () => void 0,
|
|
1009
|
+
seen: () => void 0,
|
|
1010
|
+
known: () => true
|
|
1011
|
+
};
|
|
1012
|
+
function isStartBlank(tab) {
|
|
1013
|
+
return tab.owner === void 0 && tab.url === "about:blank";
|
|
1014
|
+
}
|
|
1015
|
+
var OTHER_TITLE_CHARS = 120;
|
|
1016
|
+
var notYourTab = (id) => `Tab ${id} belongs to another conversation; call browser_tabs with action adopt and tabId ${id} first if the user asked you to use it.`;
|
|
1017
|
+
var tabInUse = (id) => `Tab ${id} is in use by another conversation right now; retry shortly or open your own tab.`;
|
|
1018
|
+
var tabMoved = (id, title) => `Tab ${id} (${title}) was moved to another conversation.`;
|
|
1019
|
+
var tabEvicted = (id, title) => `Tab ${id} (${title}) was closed to make room for another conversation.`;
|
|
1020
|
+
var CONVERSATION_CAP_MESSAGE = `This conversation has ${MAX_TABS_PER_CONVERSATION} tabs open, the most one conversation keeps; close one first.`;
|
|
1021
|
+
var deviceCapMessage = (own) => own > 0 ? `The browser has ${MAX_TABS} tabs open, the most it keeps (${own} ${own === 1 ? "is" : "are"} this conversation's); close one of yours first.` : `Other conversations hold all ${MAX_TABS} tabs of the browser; retry later or ask the user to close one in the browser panel.`;
|
|
1022
|
+
function bareUrl(raw) {
|
|
1023
|
+
try {
|
|
1024
|
+
const u = new URL(raw);
|
|
1025
|
+
u.search = "";
|
|
1026
|
+
u.hash = "";
|
|
1027
|
+
return u.href;
|
|
1028
|
+
} catch {
|
|
1029
|
+
return raw.replace(/[?#].*$/s, "");
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
function isActing(req) {
|
|
1033
|
+
if (req.op === "handoff_notice" || req.op === "handoff_clear") return false;
|
|
1034
|
+
if (req.op === "tabs") return req.action !== "list" && req.action !== "adopt";
|
|
1035
|
+
return true;
|
|
1036
|
+
}
|
|
780
1037
|
var BrowserSession = class {
|
|
781
1038
|
ctx;
|
|
782
1039
|
proxy;
|
|
@@ -814,14 +1071,36 @@ var BrowserSession = class {
|
|
|
814
1071
|
collectors = /* @__PURE__ */ new Set();
|
|
815
1072
|
probes = /* @__PURE__ */ new Map();
|
|
816
1073
|
reservedDownloads = /* @__PURE__ */ new Set();
|
|
817
|
-
|
|
1074
|
+
/** Notes of a conversation's pages that arrived with none of its requests in flight. */
|
|
1075
|
+
unreported = /* @__PURE__ */ new Map();
|
|
1076
|
+
/** Device notes (no page: the browser build) for the next reply of any conversation. */
|
|
1077
|
+
unreportedAll = [];
|
|
818
1078
|
nextId = 1;
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
/**
|
|
822
|
-
|
|
823
|
-
/** The
|
|
824
|
-
|
|
1079
|
+
/** Each owned tab's owner (an unowned tab has no entry). */
|
|
1080
|
+
owners = /* @__PURE__ */ new Map();
|
|
1081
|
+
/** Each conversation's active tab. */
|
|
1082
|
+
activeOf = /* @__PURE__ */ new Map();
|
|
1083
|
+
/** The action queues, by {@link queueKey}. */
|
|
1084
|
+
queues = /* @__PURE__ */ new Map();
|
|
1085
|
+
/** A conversation's first tab being claimed or opened (shared by its callers meanwhile). */
|
|
1086
|
+
claiming = /* @__PURE__ */ new Map();
|
|
1087
|
+
/** Tab allocations (claim, open, eviction) one at a time across conversations. */
|
|
1088
|
+
allocChain = Promise.resolve();
|
|
1089
|
+
/** Tabs being opened by the session (not yet tracked): they count toward the device cap. */
|
|
1090
|
+
reserved = 0;
|
|
1091
|
+
/**
|
|
1092
|
+
* Tabs of an idle-closed conversation whose close is under way: no conversation owns, claims,
|
|
1093
|
+
* adopts or lists them any more.
|
|
1094
|
+
*/
|
|
1095
|
+
closing = /* @__PURE__ */ new Set();
|
|
1096
|
+
/** When an agent request last acted on each tab (the eviction's least recently used tab). */
|
|
1097
|
+
tabUsedAt = /* @__PURE__ */ new Map();
|
|
1098
|
+
/** Order of the conversations' last acting requests (a viewer's default shown tab). */
|
|
1099
|
+
actedSeq = /* @__PURE__ */ new Map();
|
|
1100
|
+
actSeq = 0;
|
|
1101
|
+
usage;
|
|
1102
|
+
/** Each tab's latest agent click; `seq` grows with every click (viewers flash only new ones). */
|
|
1103
|
+
clicks = /* @__PURE__ */ new Map();
|
|
825
1104
|
clickSeq = 0;
|
|
826
1105
|
constructor(ctx, opts) {
|
|
827
1106
|
this.ctx = ctx;
|
|
@@ -832,20 +1111,20 @@ var BrowserSession = class {
|
|
|
832
1111
|
this.downloadWaitMs = opts.downloadWaitMs ?? DOWNLOAD_WAIT_MS;
|
|
833
1112
|
this.clock = opts.clock ?? systemClock;
|
|
834
1113
|
this.navigationTimeoutMs = opts.navigationTimeoutMs ?? NAVIGATION_TIMEOUT_MS;
|
|
1114
|
+
this.usage = opts.usage ?? NO_USAGE;
|
|
835
1115
|
ctx.setDefaultTimeout(ACTION_TIMEOUT_MS);
|
|
836
1116
|
ctx.setDefaultNavigationTimeout(this.navigationTimeoutMs);
|
|
837
1117
|
ctx.on("page", (p) => {
|
|
838
|
-
if (
|
|
839
|
-
p.close().catch(() => void 0);
|
|
840
|
-
this.note(
|
|
841
|
-
`The page opened a popup, but the browser keeps at most ${MAX_TABS} tabs, so the popup was closed; close a tab and retry if it was needed.`
|
|
842
|
-
);
|
|
843
|
-
return;
|
|
844
|
-
}
|
|
1118
|
+
if (this.ids.has(p)) return;
|
|
845
1119
|
this.track(p);
|
|
846
|
-
this.active = this.ids.get(p);
|
|
847
|
-
this.emit({ type: "active" });
|
|
848
1120
|
this.emit({ type: "state" });
|
|
1121
|
+
queueMicrotask(() => {
|
|
1122
|
+
void this.allocate(async () => {
|
|
1123
|
+
const id = this.ids.get(p);
|
|
1124
|
+
if (id === void 0 || this.owners.has(id) || p.isClosed()) return;
|
|
1125
|
+
if (this.pages.size > MAX_TABS) await p.close().catch(() => void 0);
|
|
1126
|
+
});
|
|
1127
|
+
});
|
|
849
1128
|
});
|
|
850
1129
|
for (const p of ctx.pages()) this.track(p);
|
|
851
1130
|
}
|
|
@@ -856,55 +1135,288 @@ var BrowserSession = class {
|
|
|
856
1135
|
this.listeners.add(fn);
|
|
857
1136
|
return () => this.listeners.delete(fn);
|
|
858
1137
|
}
|
|
859
|
-
/**
|
|
860
|
-
* The active tab, opening a blank one when none is left. Callers that find no tab at the same
|
|
861
|
-
* time (two quick closes of the last tabs, a viewer's frame and a request) share that one
|
|
862
|
-
* opening, so the last close leaves exactly one blank tab.
|
|
863
|
-
*/
|
|
864
|
-
async activePage() {
|
|
865
|
-
const cur = this.active === void 0 ? void 0 : this.pages.get(this.active);
|
|
866
|
-
if (cur !== void 0 && !cur.isClosed()) return cur;
|
|
867
|
-
const first = [...this.pages.values()].find((p) => !p.isClosed());
|
|
868
|
-
if (first !== void 0) {
|
|
869
|
-
this.active = this.ids.get(first);
|
|
870
|
-
return first;
|
|
871
|
-
}
|
|
872
|
-
this.opening ??= this.ctx.newPage().then(
|
|
873
|
-
(p) => {
|
|
874
|
-
this.opening = void 0;
|
|
875
|
-
this.track(p);
|
|
876
|
-
this.active = this.ids.get(p);
|
|
877
|
-
this.emit({ type: "active" });
|
|
878
|
-
this.emit({ type: "state" });
|
|
879
|
-
return p;
|
|
880
|
-
},
|
|
881
|
-
(e) => {
|
|
882
|
-
this.opening = void 0;
|
|
883
|
-
throw e;
|
|
884
|
-
}
|
|
885
|
-
);
|
|
886
|
-
return this.opening;
|
|
887
|
-
}
|
|
888
1138
|
tabIdOf(page) {
|
|
889
1139
|
return this.ids.get(page) ?? "";
|
|
890
1140
|
}
|
|
1141
|
+
/** The live page of a tab, if it is open (and not closing). */
|
|
1142
|
+
pageById(tabId) {
|
|
1143
|
+
const page = tabId === void 0 ? void 0 : this.pages.get(tabId);
|
|
1144
|
+
return page === void 0 || page.isClosed() || this.closing.has(tabId) ? void 0 : page;
|
|
1145
|
+
}
|
|
1146
|
+
/** The open tabs that are not closing, in tab order. */
|
|
1147
|
+
live() {
|
|
1148
|
+
return [...this.pages.entries()].filter(([id, p]) => !p.isClosed() && !this.closing.has(id));
|
|
1149
|
+
}
|
|
1150
|
+
/** A tab's owner, if it has one. */
|
|
1151
|
+
ownerOf(tabId) {
|
|
1152
|
+
return this.owners.get(tabId);
|
|
1153
|
+
}
|
|
1154
|
+
/** The conversations owning a tab now. */
|
|
1155
|
+
owningConversations() {
|
|
1156
|
+
return [...new Set([...this.owners.values()].map((o) => o.owner))];
|
|
1157
|
+
}
|
|
1158
|
+
/** The conversation's tabs in tab order. */
|
|
1159
|
+
tabsOf(conversationId) {
|
|
1160
|
+
return this.live().map(([id]) => id).filter((id) => this.owners.get(id)?.owner === conversationId);
|
|
1161
|
+
}
|
|
1162
|
+
/** How many tabs the conversation owns. */
|
|
1163
|
+
countOf(conversationId) {
|
|
1164
|
+
return this.tabsOf(conversationId).length;
|
|
1165
|
+
}
|
|
1166
|
+
/** The conversation's active tab id, if it has a live one. */
|
|
1167
|
+
activeTabOf(conversationId) {
|
|
1168
|
+
const id = this.activeOf.get(conversationId);
|
|
1169
|
+
return this.pageById(id) === void 0 ? void 0 : id;
|
|
1170
|
+
}
|
|
1171
|
+
/** The conversation's active page, if it has one (never opens a tab). */
|
|
1172
|
+
currentPage(conversationId) {
|
|
1173
|
+
return this.pageById(this.activeOf.get(conversationId));
|
|
1174
|
+
}
|
|
1175
|
+
/** The order of the conversation's last acting request (higher: later; 0: never). */
|
|
1176
|
+
actedOrder(conversationId) {
|
|
1177
|
+
return this.actedSeq.get(conversationId) ?? 0;
|
|
1178
|
+
}
|
|
1179
|
+
/** The tab's latest agent click, if any. */
|
|
1180
|
+
lastClickOf(tabId) {
|
|
1181
|
+
return tabId === void 0 ? void 0 : this.clicks.get(tabId);
|
|
1182
|
+
}
|
|
1183
|
+
/** Whether the tab is loading a document. */
|
|
1184
|
+
isLoading(tabId) {
|
|
1185
|
+
return tabId !== void 0 && this.loadingTabs.has(tabId);
|
|
1186
|
+
}
|
|
891
1187
|
tabs() {
|
|
892
|
-
return
|
|
1188
|
+
return this.live().map(([id, p]) => {
|
|
893
1189
|
const blocker = this.blockers.get(id);
|
|
894
1190
|
const icon = this.favicons.get(id)?.data;
|
|
1191
|
+
const o = this.owners.get(id);
|
|
895
1192
|
return {
|
|
896
1193
|
id,
|
|
897
1194
|
title: clip(this.titles.get(id) ?? "", MAX_TITLE_CHARS),
|
|
898
1195
|
url: clip(p.url(), MAX_URL_CHARS),
|
|
899
|
-
active:
|
|
1196
|
+
active: o !== void 0 && this.activeOf.get(o.owner) === id,
|
|
1197
|
+
...o === void 0 ? {} : { owner: o.owner, root: o.root },
|
|
900
1198
|
...blocker === void 0 ? {} : { blocker },
|
|
901
1199
|
...icon === void 0 || icon === null ? {} : { favicon: icon }
|
|
902
1200
|
};
|
|
903
1201
|
});
|
|
904
1202
|
}
|
|
905
|
-
/**
|
|
906
|
-
|
|
907
|
-
|
|
1203
|
+
/** Makes a tab the conversation's active tab (it must own it). */
|
|
1204
|
+
setActive(conversationId, tabId) {
|
|
1205
|
+
if (this.owners.get(tabId)?.owner !== conversationId) return;
|
|
1206
|
+
if (this.activeOf.get(conversationId) === tabId) return;
|
|
1207
|
+
this.activeOf.set(conversationId, tabId);
|
|
1208
|
+
this.emit({ type: "active", conversationId });
|
|
1209
|
+
this.emit({ type: "state" });
|
|
1210
|
+
}
|
|
1211
|
+
/** Gives a tab to a conversation (owner and root); the previous owner's active moves on. */
|
|
1212
|
+
assign(tabId, to) {
|
|
1213
|
+
const before = this.owners.get(tabId);
|
|
1214
|
+
this.owners.set(tabId, to);
|
|
1215
|
+
if (before !== void 0 && before.owner !== to.owner) this.activeMovedOff(before.owner, tabId);
|
|
1216
|
+
return before;
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* A conversation lost its tab `tabId` (closed or moved): when it was its active tab, the active
|
|
1220
|
+
* tab becomes its most recently opened remaining tab, or none.
|
|
1221
|
+
*/
|
|
1222
|
+
activeMovedOff(conversationId, tabId) {
|
|
1223
|
+
if (this.activeOf.get(conversationId) !== tabId) return;
|
|
1224
|
+
const next = this.tabsOf(conversationId).filter((id) => id !== tabId).at(-1);
|
|
1225
|
+
if (next === void 0) this.activeOf.delete(conversationId);
|
|
1226
|
+
else this.activeOf.set(conversationId, next);
|
|
1227
|
+
this.emit({ type: "active", conversationId });
|
|
1228
|
+
}
|
|
1229
|
+
/** Runs a tab allocation (claim, open, eviction) one at a time across conversations. */
|
|
1230
|
+
allocate(fn) {
|
|
1231
|
+
const run = this.allocChain.then(fn, fn);
|
|
1232
|
+
this.allocChain = run.catch(() => void 0);
|
|
1233
|
+
return run;
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* The page an acting request of the conversation acts on: its active tab; with no live tab, an
|
|
1237
|
+
* unowned tab it claims, else a new tab (subject to the caps). Callers that find no tab at the
|
|
1238
|
+
* same time share one claim, so the conversation gets exactly one tab.
|
|
1239
|
+
*/
|
|
1240
|
+
async pageFor(conversationId, root) {
|
|
1241
|
+
const cur = this.currentPage(conversationId);
|
|
1242
|
+
if (cur !== void 0) return cur;
|
|
1243
|
+
const own = this.tabsOf(conversationId).at(-1);
|
|
1244
|
+
if (own !== void 0) {
|
|
1245
|
+
this.setActive(conversationId, own);
|
|
1246
|
+
const page = this.pageById(own);
|
|
1247
|
+
if (page !== void 0) return page;
|
|
1248
|
+
}
|
|
1249
|
+
let claim = this.claiming.get(conversationId);
|
|
1250
|
+
if (claim === void 0) {
|
|
1251
|
+
claim = this.allocate(async () => {
|
|
1252
|
+
const now = this.currentPage(conversationId) ?? this.pageById(this.tabsOf(conversationId).at(-1));
|
|
1253
|
+
if (now !== void 0) {
|
|
1254
|
+
this.setActive(conversationId, this.tabIdOf(now));
|
|
1255
|
+
return now;
|
|
1256
|
+
}
|
|
1257
|
+
const unowned = this.live().find(
|
|
1258
|
+
([id]) => !this.owners.has(id) && !this.usage.tabParked(id)
|
|
1259
|
+
);
|
|
1260
|
+
if (unowned !== void 0) {
|
|
1261
|
+
const [id, page] = unowned;
|
|
1262
|
+
this.assign(id, { owner: conversationId, root });
|
|
1263
|
+
this.activeOf.set(conversationId, id);
|
|
1264
|
+
this.emit({ type: "active", conversationId });
|
|
1265
|
+
this.emit({ type: "state" });
|
|
1266
|
+
return page;
|
|
1267
|
+
}
|
|
1268
|
+
return this.openLocked(conversationId, root);
|
|
1269
|
+
}).finally(() => {
|
|
1270
|
+
this.claiming.delete(conversationId);
|
|
1271
|
+
});
|
|
1272
|
+
this.claiming.set(conversationId, claim);
|
|
1273
|
+
}
|
|
1274
|
+
return claim;
|
|
1275
|
+
}
|
|
1276
|
+
/** Opens a new tab for the conversation (the caps apply) and makes it its active tab. */
|
|
1277
|
+
openTab(conversationId, root) {
|
|
1278
|
+
return this.allocate(() => this.openLocked(conversationId, root));
|
|
1279
|
+
}
|
|
1280
|
+
/** {@link openTab} inside the allocation chain. */
|
|
1281
|
+
async openLocked(conversationId, root) {
|
|
1282
|
+
const own = this.countOf(conversationId);
|
|
1283
|
+
if (own >= MAX_TABS_PER_CONVERSATION) throw new ActionError("failed", CONVERSATION_CAP_MESSAGE);
|
|
1284
|
+
if (this.pages.size + this.reserved >= MAX_TABS && !await this.evict(conversationId))
|
|
1285
|
+
throw new ActionError("failed", deviceCapMessage(own));
|
|
1286
|
+
this.reserved++;
|
|
1287
|
+
let page;
|
|
1288
|
+
try {
|
|
1289
|
+
page = await this.ctx.newPage();
|
|
1290
|
+
} finally {
|
|
1291
|
+
this.reserved--;
|
|
1292
|
+
}
|
|
1293
|
+
this.track(page);
|
|
1294
|
+
const id = this.tabIdOf(page);
|
|
1295
|
+
this.assign(id, { owner: conversationId, root });
|
|
1296
|
+
this.activeOf.set(conversationId, id);
|
|
1297
|
+
this.emit({ type: "active", conversationId });
|
|
1298
|
+
this.emit({ type: "state" });
|
|
1299
|
+
return page;
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1302
|
+
* Closes one tab to make room at the device cap: an unowned tab first, else a tab of the least
|
|
1303
|
+
* recently active conversation that is evictable (not in use, not pinned by a viewer, idle for
|
|
1304
|
+
* at least EVICT_MIN_IDLE_MS), its least recently used non-active tab before its active one; tabs
|
|
1305
|
+
* a park pins are skipped. Its owner is told. False when no tab qualifies.
|
|
1306
|
+
*/
|
|
1307
|
+
async evict(forConversation) {
|
|
1308
|
+
const live = this.live().filter(([id]) => !this.usage.tabParked(id));
|
|
1309
|
+
const unowned = live.find(([id]) => !this.owners.has(id));
|
|
1310
|
+
if (unowned !== void 0) {
|
|
1311
|
+
await unowned[1].close().catch(() => void 0);
|
|
1312
|
+
return true;
|
|
1313
|
+
}
|
|
1314
|
+
const owners = /* @__PURE__ */ new Set();
|
|
1315
|
+
for (const [id] of live) {
|
|
1316
|
+
const o = this.owners.get(id)?.owner;
|
|
1317
|
+
if (o !== void 0 && o !== forConversation && !this.usage.busy(o) && !this.usage.pinned(o) && this.usage.idleFor(o) >= EVICT_MIN_IDLE_MS)
|
|
1318
|
+
owners.add(o);
|
|
1319
|
+
}
|
|
1320
|
+
const victim = [...owners].sort(
|
|
1321
|
+
(a, b) => this.usage.lastActiveAt(a) - this.usage.lastActiveAt(b)
|
|
1322
|
+
)[0];
|
|
1323
|
+
if (victim === void 0) return false;
|
|
1324
|
+
const active = this.activeOf.get(victim);
|
|
1325
|
+
const used = (id) => this.tabUsedAt.get(id) ?? 0;
|
|
1326
|
+
const [tabId, page] = live.filter(([id]) => this.owners.get(id)?.owner === victim).sort(
|
|
1327
|
+
([a], [b]) => (a === active ? 1 : 0) - (b === active ? 1 : 0) || used(a) - used(b)
|
|
1328
|
+
)[0] ?? [];
|
|
1329
|
+
if (tabId === void 0 || page === void 0) return false;
|
|
1330
|
+
const title = clip(this.titles.get(tabId) ?? "", OTHER_TITLE_CHARS) || "no title";
|
|
1331
|
+
await page.close().catch(() => void 0);
|
|
1332
|
+
this.noteTo(victim, tabEvicted(tabId, title));
|
|
1333
|
+
return true;
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Moves a tab to the conversation and makes it its active tab (the tool's and a viewer's adopt).
|
|
1337
|
+
* `in_use`: its owner has a request in flight or a park (or a park pins the tab); `cap`: the
|
|
1338
|
+
* conversation already owns MAX_TABS_PER_CONVERSATION tabs. The previous owner is told.
|
|
1339
|
+
*/
|
|
1340
|
+
adopt(conversationId, root, tabId) {
|
|
1341
|
+
this.tabById(tabId);
|
|
1342
|
+
const before = this.owners.get(tabId);
|
|
1343
|
+
if (before?.owner === conversationId) {
|
|
1344
|
+
this.setActive(conversationId, tabId);
|
|
1345
|
+
return "ok";
|
|
1346
|
+
}
|
|
1347
|
+
if (before !== void 0 && this.usage.busy(before.owner) || this.usage.tabParked(tabId))
|
|
1348
|
+
return "in_use";
|
|
1349
|
+
if (this.countOf(conversationId) >= MAX_TABS_PER_CONVERSATION) return "cap";
|
|
1350
|
+
this.assign(tabId, { owner: conversationId, root });
|
|
1351
|
+
this.activeOf.set(conversationId, tabId);
|
|
1352
|
+
this.usage.seen(conversationId, root);
|
|
1353
|
+
if (before !== void 0) {
|
|
1354
|
+
const title = clip(this.titles.get(tabId) ?? "", OTHER_TITLE_CHARS) || "no title";
|
|
1355
|
+
this.noteTo(before.owner, tabMoved(tabId, title));
|
|
1356
|
+
}
|
|
1357
|
+
this.emit({ type: "active", conversationId });
|
|
1358
|
+
this.emit({ type: "state" });
|
|
1359
|
+
return "ok";
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Opens a restored tab for its owner (the first claims the start's blank tab) and starts its
|
|
1363
|
+
* navigation without waiting for it.
|
|
1364
|
+
*/
|
|
1365
|
+
async restoreTab(owner, url) {
|
|
1366
|
+
const page = await this.allocate(async () => {
|
|
1367
|
+
const blank = this.live().find(
|
|
1368
|
+
([id2, p]) => isStartBlank({ owner: this.owners.get(id2)?.owner, url: p.url() })
|
|
1369
|
+
);
|
|
1370
|
+
if (blank !== void 0) {
|
|
1371
|
+
this.assign(blank[0], owner);
|
|
1372
|
+
return blank[1];
|
|
1373
|
+
}
|
|
1374
|
+
this.reserved++;
|
|
1375
|
+
try {
|
|
1376
|
+
const p = await this.ctx.newPage();
|
|
1377
|
+
this.track(p);
|
|
1378
|
+
this.assign(this.tabIdOf(p), owner);
|
|
1379
|
+
return p;
|
|
1380
|
+
} finally {
|
|
1381
|
+
this.reserved--;
|
|
1382
|
+
}
|
|
1383
|
+
});
|
|
1384
|
+
const id = this.tabIdOf(page);
|
|
1385
|
+
if (!this.activeOf.has(owner.owner)) this.activeOf.set(owner.owner, id);
|
|
1386
|
+
this.usage.seen(owner.owner, owner.root);
|
|
1387
|
+
this.emit({ type: "active", conversationId: owner.owner });
|
|
1388
|
+
this.emit({ type: "state" });
|
|
1389
|
+
let target;
|
|
1390
|
+
try {
|
|
1391
|
+
target = normalizeUrl(url);
|
|
1392
|
+
} catch {
|
|
1393
|
+
}
|
|
1394
|
+
if (target !== void 0) void this.viewerNav(page, { url: target });
|
|
1395
|
+
return id;
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Closes an idle conversation's tabs (except those a park pins) and forgets what the session
|
|
1399
|
+
* keeps for it.
|
|
1400
|
+
*/
|
|
1401
|
+
async closeConversation(conversationId) {
|
|
1402
|
+
const ids = this.tabsOf(conversationId).filter((id) => !this.usage.tabParked(id));
|
|
1403
|
+
for (const id of ids) {
|
|
1404
|
+
this.closing.add(id);
|
|
1405
|
+
this.owners.delete(id);
|
|
1406
|
+
}
|
|
1407
|
+
this.activeOf.delete(conversationId);
|
|
1408
|
+
this.queues.delete(this.queueKey(conversationId));
|
|
1409
|
+
this.unreported.delete(conversationId);
|
|
1410
|
+
this.claiming.delete(conversationId);
|
|
1411
|
+
this.actedSeq.delete(conversationId);
|
|
1412
|
+
await Promise.all(
|
|
1413
|
+
ids.map(
|
|
1414
|
+
(id) => this.pages.get(id)?.close().catch(() => {
|
|
1415
|
+
this.closing.delete(id);
|
|
1416
|
+
})
|
|
1417
|
+
)
|
|
1418
|
+
);
|
|
1419
|
+
this.emit({ type: "state" });
|
|
908
1420
|
}
|
|
909
1421
|
emit(e) {
|
|
910
1422
|
for (const fn of [...this.listeners]) {
|
|
@@ -963,38 +1475,75 @@ var BrowserSession = class {
|
|
|
963
1475
|
this.emit({ type: "state" });
|
|
964
1476
|
}
|
|
965
1477
|
if (this.proxy !== void 0 && PROXY_FAILURE_RE.test(err))
|
|
966
|
-
this.checkBlocked(r.url(), this.pageToOpen(page, r));
|
|
1478
|
+
this.checkBlocked(page, r.url(), this.pageToOpen(page, r));
|
|
967
1479
|
});
|
|
968
1480
|
page.on("response", (r) => {
|
|
969
1481
|
if (r.request().isNavigationRequest() && r.frame() === page.mainFrame())
|
|
970
1482
|
this.docStatus.set(id, r.status());
|
|
971
1483
|
if (this.proxy !== void 0 && r.status() === 403 && r.url().startsWith("http:")) {
|
|
972
|
-
this.checkBlocked(r.url(), this.pageToOpen(page, r.request()));
|
|
1484
|
+
this.checkBlocked(page, r.url(), this.pageToOpen(page, r.request()));
|
|
973
1485
|
}
|
|
974
1486
|
});
|
|
975
|
-
page.on("dialog", (d) => this.onDialog(d));
|
|
976
|
-
page.on("download", (d) => this.onDownload(d));
|
|
1487
|
+
page.on("dialog", (d) => this.onDialog(page, d));
|
|
1488
|
+
page.on("download", (d) => this.onDownload(page, d));
|
|
977
1489
|
page.on("filechooser", () => {
|
|
978
|
-
|
|
979
|
-
this.
|
|
1490
|
+
const o = this.owners.get(id);
|
|
1491
|
+
this.noteTo(
|
|
1492
|
+
o?.owner,
|
|
1493
|
+
"The page opened a file chooser; use the upload tool with the file input's ref."
|
|
1494
|
+
);
|
|
1495
|
+
if (o !== void 0) this.emit({ type: "dialog", kind: "file", message: "", root: o.root });
|
|
980
1496
|
});
|
|
1497
|
+
page.on("popup", (p) => this.onPopup(page, p));
|
|
981
1498
|
page.on("close", () => {
|
|
982
1499
|
this.pendingNavs.delete(page);
|
|
983
1500
|
this.pages.delete(id);
|
|
1501
|
+
this.closing.delete(id);
|
|
984
1502
|
this.titles.delete(id);
|
|
985
1503
|
this.loadingTabs.delete(id);
|
|
986
1504
|
this.blockers.delete(id);
|
|
987
1505
|
this.favicons.delete(id);
|
|
988
1506
|
this.docStatus.delete(id);
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
1507
|
+
this.tabUsedAt.delete(id);
|
|
1508
|
+
this.clicks.delete(id);
|
|
1509
|
+
const o = this.owners.get(id);
|
|
1510
|
+
this.owners.delete(id);
|
|
1511
|
+
if (o !== void 0) this.activeMovedOff(o.owner, id);
|
|
1512
|
+
this.usage.tabClosed(id);
|
|
994
1513
|
this.emit({ type: "state" });
|
|
995
1514
|
});
|
|
996
1515
|
}
|
|
997
|
-
|
|
1516
|
+
/**
|
|
1517
|
+
* A page opened a popup: it belongs to the opener's owner and becomes its active tab, as in a
|
|
1518
|
+
* desktop browser (an unowned opener leaves it unowned). Past the conversation's cap it is closed
|
|
1519
|
+
* at once; past the device cap a tab is evicted for it, else it is closed; the owner is told.
|
|
1520
|
+
*/
|
|
1521
|
+
onPopup(opener, popup) {
|
|
1522
|
+
this.track(popup);
|
|
1523
|
+
const id = this.tabIdOf(popup);
|
|
1524
|
+
const o = this.owners.get(this.tabIdOf(opener));
|
|
1525
|
+
if (o === void 0 || this.owners.has(id) || popup.isClosed()) return;
|
|
1526
|
+
const closeIt = (why) => {
|
|
1527
|
+
popup.close().catch(() => void 0);
|
|
1528
|
+
this.noteTo(
|
|
1529
|
+
o.owner,
|
|
1530
|
+
`The page opened a popup, but ${why}, so the popup was closed; close a tab and retry if it was needed.`
|
|
1531
|
+
);
|
|
1532
|
+
};
|
|
1533
|
+
if (this.countOf(o.owner) >= MAX_TABS_PER_CONVERSATION) {
|
|
1534
|
+
closeIt(`this conversation keeps at most ${MAX_TABS_PER_CONVERSATION} tabs`);
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
this.assign(id, o);
|
|
1538
|
+
this.activeOf.set(o.owner, id);
|
|
1539
|
+
this.emit({ type: "active", conversationId: o.owner });
|
|
1540
|
+
this.emit({ type: "state" });
|
|
1541
|
+
void this.allocate(async () => {
|
|
1542
|
+
if (popup.isClosed() || this.pages.size <= MAX_TABS) return;
|
|
1543
|
+
if (!await this.evict(o.owner)) closeIt(`the browser keeps at most ${MAX_TABS} tabs`);
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
onDialog(page, d) {
|
|
998
1547
|
const type = d.type();
|
|
999
1548
|
if (type === "beforeunload") {
|
|
1000
1549
|
d.accept().catch(() => void 0);
|
|
@@ -1004,24 +1553,50 @@ var BrowserSession = class {
|
|
|
1004
1553
|
const message = clip(d.message(), MAX_DIALOG_CHARS);
|
|
1005
1554
|
const settle = kind === "alert" ? d.accept() : d.dismiss();
|
|
1006
1555
|
settle.catch(() => void 0);
|
|
1007
|
-
this.
|
|
1556
|
+
const o = this.owners.get(this.tabIdOf(page));
|
|
1557
|
+
this.noteTo(
|
|
1558
|
+
o?.owner,
|
|
1008
1559
|
`The page showed ${kind === "alert" ? "an alert" : `a ${kind}`} dialog: ${JSON.stringify(message)} (${kind === "alert" ? "closed" : "dismissed"}).`
|
|
1009
1560
|
);
|
|
1010
|
-
this.emit({ type: "dialog", kind, message });
|
|
1561
|
+
if (o !== void 0) this.emit({ type: "dialog", kind, message, root: o.root });
|
|
1011
1562
|
}
|
|
1012
1563
|
/**
|
|
1013
|
-
*
|
|
1014
|
-
*
|
|
1564
|
+
* A device note (not about a page: the browser build that runs): the replies of the requests in
|
|
1565
|
+
* flight carry it, else the next reply of any conversation.
|
|
1015
1566
|
*/
|
|
1016
1567
|
note(text) {
|
|
1017
1568
|
const n = clip(text, MAX_DIALOG_CHARS + 200);
|
|
1018
1569
|
if (this.collectors.size === 0) {
|
|
1019
|
-
this.
|
|
1020
|
-
if (this.
|
|
1570
|
+
this.unreportedAll.push(n);
|
|
1571
|
+
if (this.unreportedAll.length > MAX_UNREPORTED) this.unreportedAll.shift();
|
|
1021
1572
|
return;
|
|
1022
1573
|
}
|
|
1023
1574
|
for (const c of this.collectors) c.notes.push(n);
|
|
1024
1575
|
}
|
|
1576
|
+
/**
|
|
1577
|
+
* A note for a conversation (about one of its pages): its requests in flight carry it, else its
|
|
1578
|
+
* next reply. A note of an unowned page (`undefined`) reaches nobody.
|
|
1579
|
+
*/
|
|
1580
|
+
noteTo(conversationId, text) {
|
|
1581
|
+
if (conversationId === void 0) return;
|
|
1582
|
+
if (this.countOf(conversationId) === 0 && !this.usage.known(conversationId)) return;
|
|
1583
|
+
const n = clip(text, MAX_DIALOG_CHARS + 200);
|
|
1584
|
+
let delivered = false;
|
|
1585
|
+
for (const c of this.collectors) {
|
|
1586
|
+
if (c.conv !== conversationId) continue;
|
|
1587
|
+
c.notes.push(n);
|
|
1588
|
+
delivered = true;
|
|
1589
|
+
}
|
|
1590
|
+
if (delivered) return;
|
|
1591
|
+
const list = this.unreported.get(conversationId) ?? [];
|
|
1592
|
+
list.push(n);
|
|
1593
|
+
if (list.length > MAX_UNREPORTED) list.shift();
|
|
1594
|
+
this.unreported.set(conversationId, list);
|
|
1595
|
+
}
|
|
1596
|
+
/** The owner of a page, if it has one. */
|
|
1597
|
+
ownerOfPage(page) {
|
|
1598
|
+
return page === null || page === void 0 ? void 0 : this.owners.get(this.tabIdOf(page));
|
|
1599
|
+
}
|
|
1025
1600
|
/**
|
|
1026
1601
|
* Saves a download into `<workspace>/downloads` under its suggested name (sanitized, numbered
|
|
1027
1602
|
* when taken). A request in flight waits for it at most `downloadWaitMs`, then says it is still
|
|
@@ -1029,12 +1604,14 @@ var BrowserSession = class {
|
|
|
1029
1604
|
* cancelled when together they pass `downloadMaxBytes`, so a page cannot fill the disk.
|
|
1030
1605
|
* Without a workspace the download is cancelled.
|
|
1031
1606
|
*/
|
|
1032
|
-
onDownload(d) {
|
|
1607
|
+
onDownload(page, d) {
|
|
1033
1608
|
const shown = JSON.stringify(clip(d.suggestedFilename(), 300));
|
|
1034
1609
|
const workspace = this.workspace;
|
|
1610
|
+
const owner = this.ownerOfPage(page)?.owner;
|
|
1611
|
+
const note = (text) => this.noteTo(owner, text);
|
|
1035
1612
|
if (workspace === void 0) {
|
|
1036
1613
|
d.cancel().catch(() => void 0);
|
|
1037
|
-
|
|
1614
|
+
note(
|
|
1038
1615
|
`The page started a download (${shown}), which was cancelled: the device has no workspace.`
|
|
1039
1616
|
);
|
|
1040
1617
|
return;
|
|
@@ -1047,20 +1624,20 @@ var BrowserSession = class {
|
|
|
1047
1624
|
try {
|
|
1048
1625
|
const failure = await d.failure();
|
|
1049
1626
|
if (failure !== null) {
|
|
1050
|
-
if (failure !== "canceled")
|
|
1627
|
+
if (failure !== "canceled") note(`The download of ${shown} failed: ${failure}.`);
|
|
1051
1628
|
return;
|
|
1052
1629
|
}
|
|
1053
1630
|
const tmp = await d.path();
|
|
1054
1631
|
if (statSync(tmp).size > this.downloadMaxBytes) {
|
|
1055
|
-
|
|
1632
|
+
note(
|
|
1056
1633
|
`The download of ${shown} was not saved: it is larger than ${this.downloadMaxBytes} bytes.`
|
|
1057
1634
|
);
|
|
1058
1635
|
return;
|
|
1059
1636
|
}
|
|
1060
1637
|
target = this.saveDownload(workspace, safeFileName(d.suggestedFilename()), tmp);
|
|
1061
|
-
|
|
1638
|
+
note(`Downloaded ${shown} to ${target}.`);
|
|
1062
1639
|
} catch (e) {
|
|
1063
|
-
|
|
1640
|
+
note(
|
|
1064
1641
|
`The download of ${shown} was not saved: ${e instanceof ActionError ? e.message : firstLine(e)}`
|
|
1065
1642
|
);
|
|
1066
1643
|
} finally {
|
|
@@ -1070,7 +1647,7 @@ var BrowserSession = class {
|
|
|
1070
1647
|
entry.finished = true;
|
|
1071
1648
|
}
|
|
1072
1649
|
})();
|
|
1073
|
-
for (const c of this.collectors) c.downloads.push(entry);
|
|
1650
|
+
for (const c of this.collectors) if (c.conv === owner) c.downloads.push(entry);
|
|
1074
1651
|
}
|
|
1075
1652
|
/**
|
|
1076
1653
|
* Copies a finished download from Playwright's private file into `<workspace>/downloads` and
|
|
@@ -1169,7 +1746,8 @@ var BrowserSession = class {
|
|
|
1169
1746
|
if (bytes > this.downloadMaxBytes) {
|
|
1170
1747
|
for (const f of names) this.cancelledTmp.add(f);
|
|
1171
1748
|
for (const d of this.activeDownloads) {
|
|
1172
|
-
this.
|
|
1749
|
+
this.noteTo(
|
|
1750
|
+
this.ownerOfPage(d.page())?.owner,
|
|
1173
1751
|
`The download of ${JSON.stringify(clip(d.suggestedFilename(), 300))} was cancelled: downloads in progress passed ${this.downloadMaxBytes} bytes.`
|
|
1174
1752
|
);
|
|
1175
1753
|
d.cancel().catch(() => void 0);
|
|
@@ -1255,8 +1833,9 @@ var BrowserSession = class {
|
|
|
1255
1833
|
* Probes the proxy for the URL's host and reports it when refused; the viewers' `blocked`
|
|
1256
1834
|
* message carries `pageUrl` (default: the URL itself), the page to open once allowed.
|
|
1257
1835
|
*/
|
|
1258
|
-
checkBlocked(rawUrl, pageUrl) {
|
|
1836
|
+
checkBlocked(page, rawUrl, pageUrl) {
|
|
1259
1837
|
const proxy = this.proxy;
|
|
1838
|
+
const owner = this.ownerOfPage(page);
|
|
1260
1839
|
if (proxy === void 0) return Promise.resolve(false);
|
|
1261
1840
|
let url;
|
|
1262
1841
|
try {
|
|
@@ -1266,19 +1845,20 @@ var BrowserSession = class {
|
|
|
1266
1845
|
}
|
|
1267
1846
|
const host = url.hostname;
|
|
1268
1847
|
const port = Number(url.port || (url.protocol === "https:" ? 443 : 80));
|
|
1269
|
-
|
|
1848
|
+
const key2 = `${owner?.owner ?? ""}\0${host}`;
|
|
1849
|
+
let probe = this.probes.get(key2);
|
|
1270
1850
|
if (probe === void 0) {
|
|
1271
1851
|
probe = proxyRefuses(proxy, host, port).then((refused) => {
|
|
1272
|
-
this.probes.delete(
|
|
1273
|
-
if (refused) {
|
|
1274
|
-
for (const c of this.collectors) c.blocked.add(host);
|
|
1275
|
-
this.emit({ type: "blocked", host, url: pageUrl ?? url.href });
|
|
1852
|
+
this.probes.delete(key2);
|
|
1853
|
+
if (refused && owner !== void 0) {
|
|
1854
|
+
for (const c of this.collectors) if (c.conv === owner.owner) c.blocked.add(host);
|
|
1855
|
+
this.emit({ type: "blocked", host, url: pageUrl ?? url.href, root: owner.root });
|
|
1276
1856
|
}
|
|
1277
1857
|
return refused;
|
|
1278
1858
|
});
|
|
1279
|
-
this.probes.set(
|
|
1859
|
+
this.probes.set(key2, probe);
|
|
1280
1860
|
}
|
|
1281
|
-
for (const c of this.collectors) c.pending.push(probe);
|
|
1861
|
+
for (const c of this.collectors) if (c.conv === owner?.owner) c.pending.push(probe);
|
|
1282
1862
|
return probe;
|
|
1283
1863
|
}
|
|
1284
1864
|
/** `value`, or `fallback` when it takes longer than {@link PAGE_QUERY_MS} (never rejects). */
|
|
@@ -1295,24 +1875,50 @@ var BrowserSession = class {
|
|
|
1295
1875
|
this.clock.cancel(timer);
|
|
1296
1876
|
}
|
|
1297
1877
|
}
|
|
1298
|
-
/**
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1878
|
+
/**
|
|
1879
|
+
* The action queue a conversation's requests run in: one per conversation, so one
|
|
1880
|
+
* conversation's slow navigation does not stall another (ruling 7). Falling back to one
|
|
1881
|
+
* device-wide queue is returning a constant here.
|
|
1882
|
+
*/
|
|
1883
|
+
queueKey(conversationId) {
|
|
1884
|
+
return conversationId;
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* Runs `fn` one at a time with the conversation's other actions (`wait`, the tab list and adopt
|
|
1888
|
+
* run beside them).
|
|
1889
|
+
*/
|
|
1890
|
+
serial(conversationId, fn) {
|
|
1891
|
+
const key2 = this.queueKey(conversationId);
|
|
1892
|
+
const run = (this.queues.get(key2) ?? Promise.resolve()).then(fn, fn);
|
|
1893
|
+
const tail = run.catch(() => void 0);
|
|
1894
|
+
this.queues.set(key2, tail);
|
|
1895
|
+
void tail.then(() => {
|
|
1896
|
+
if (this.queues.get(key2) === tail) this.queues.delete(key2);
|
|
1897
|
+
});
|
|
1302
1898
|
return run;
|
|
1303
1899
|
}
|
|
1304
1900
|
/**
|
|
1305
|
-
* Runs a tool request; never throws. `takenOver` is checked when a queued
|
|
1306
|
-
* queue: a takeover that began while it waited refuses it. An action already
|
|
1307
|
-
* finish (stopping it halfway would leave the page in an unknown state).
|
|
1901
|
+
* Runs a tool request of its conversation; never throws. `takenOver` is checked when a queued
|
|
1902
|
+
* request leaves the queue: a takeover that began while it waited refuses it. An action already
|
|
1903
|
+
* running is left to finish (stopping it halfway would leave the page in an unknown state).
|
|
1308
1904
|
*/
|
|
1309
1905
|
async run(req, opts = {}) {
|
|
1310
|
-
const
|
|
1311
|
-
const
|
|
1906
|
+
const conv = req.conversationId;
|
|
1907
|
+
const lateOwn = this.unreported.get(conv) ?? [];
|
|
1908
|
+
this.unreported.delete(conv);
|
|
1909
|
+
const lateAll = this.unreportedAll.splice(0);
|
|
1910
|
+
const c = {
|
|
1911
|
+
conv,
|
|
1912
|
+
blocked: /* @__PURE__ */ new Set(),
|
|
1913
|
+
notes: [...lateAll, ...lateOwn],
|
|
1914
|
+
pending: [],
|
|
1915
|
+
downloads: []
|
|
1916
|
+
};
|
|
1312
1917
|
this.collectors.add(c);
|
|
1313
1918
|
let reply;
|
|
1314
1919
|
try {
|
|
1315
|
-
const
|
|
1920
|
+
const beside = req.op === "wait" || req.op === "tabs" && (req.action === "list" || req.action === "adopt");
|
|
1921
|
+
const out = await (beside ? this.exec(req) : this.serial(conv, () => {
|
|
1316
1922
|
if (opts.takenOver?.() === true)
|
|
1317
1923
|
throw new ActionError("taken_over", TAKEN_OVER_MESSAGE);
|
|
1318
1924
|
return this.exec(req);
|
|
@@ -1320,8 +1926,12 @@ var BrowserSession = class {
|
|
|
1320
1926
|
await this.settleProbes(c);
|
|
1321
1927
|
await this.settleDownloads(c);
|
|
1322
1928
|
this.collectors.delete(c);
|
|
1323
|
-
const page =
|
|
1324
|
-
|
|
1929
|
+
const page = this.currentPage(conv);
|
|
1930
|
+
if (page !== void 0 && isActing(req)) {
|
|
1931
|
+
this.tabUsedAt.set(this.tabIdOf(page), this.clock.now());
|
|
1932
|
+
this.actedSeq.set(conv, ++this.actSeq);
|
|
1933
|
+
}
|
|
1934
|
+
const verifying = page === void 0 ? false : await this.checkVerification(page, this.tabIdOf(page));
|
|
1325
1935
|
const text = [
|
|
1326
1936
|
...verifying ? [VERIFICATION_NOTE] : [],
|
|
1327
1937
|
...c.notes,
|
|
@@ -1329,7 +1939,7 @@ var BrowserSession = class {
|
|
|
1329
1939
|
].join("\n");
|
|
1330
1940
|
reply = {
|
|
1331
1941
|
ok: true,
|
|
1332
|
-
page: await this.pageInfo(page),
|
|
1942
|
+
...page === void 0 ? {} : { page: await this.pageInfo(page) },
|
|
1333
1943
|
blockedHosts: [...c.blocked],
|
|
1334
1944
|
...text === "" ? {} : { text },
|
|
1335
1945
|
...out.image === void 0 ? {} : { image: out.image }
|
|
@@ -1346,7 +1956,9 @@ var BrowserSession = class {
|
|
|
1346
1956
|
message = `The device's network settings block ${[...c.blocked].join(", ")}.`;
|
|
1347
1957
|
}
|
|
1348
1958
|
reply = { ok: false, error: code, message, blockedHosts: [...c.blocked] };
|
|
1349
|
-
|
|
1959
|
+
if (lateOwn.length > 0)
|
|
1960
|
+
this.unreported.set(conv, [...lateOwn, ...this.unreported.get(conv) ?? []]);
|
|
1961
|
+
this.unreportedAll.unshift(...lateAll);
|
|
1350
1962
|
} finally {
|
|
1351
1963
|
this.collectors.delete(c);
|
|
1352
1964
|
}
|
|
@@ -1502,8 +2114,8 @@ var BrowserSession = class {
|
|
|
1502
2114
|
);
|
|
1503
2115
|
}
|
|
1504
2116
|
}
|
|
1505
|
-
markClick(x, y) {
|
|
1506
|
-
this.
|
|
2117
|
+
markClick(page, x, y) {
|
|
2118
|
+
this.clicks.set(this.tabIdOf(page), { x, y, seq: ++this.clickSeq });
|
|
1507
2119
|
}
|
|
1508
2120
|
async goto(page, url) {
|
|
1509
2121
|
let res;
|
|
@@ -1512,7 +2124,7 @@ var BrowserSession = class {
|
|
|
1512
2124
|
const deadline = Date.now() + this.navigationTimeoutMs;
|
|
1513
2125
|
const watch = new NavWatch(page, url, () => this.pendingNavs.get(page));
|
|
1514
2126
|
try {
|
|
1515
|
-
res = await
|
|
2127
|
+
res = await this.gotoPastLateErrorPage(page, url, watch, deadline);
|
|
1516
2128
|
} catch (e) {
|
|
1517
2129
|
const msg = firstLine(e);
|
|
1518
2130
|
if (msg.includes("Download is starting")) {
|
|
@@ -1543,7 +2155,7 @@ var BrowserSession = class {
|
|
|
1543
2155
|
} finally {
|
|
1544
2156
|
watch.dispose();
|
|
1545
2157
|
}
|
|
1546
|
-
if (res?.status() === 403 && await this.checkBlocked(res.url())) {
|
|
2158
|
+
if (res?.status() === 403 && await this.checkBlocked(page, res.url())) {
|
|
1547
2159
|
throw new ActionError(
|
|
1548
2160
|
"blocked",
|
|
1549
2161
|
`The device's network settings block ${new URL(res.url()).hostname}.`
|
|
@@ -1551,6 +2163,27 @@ var BrowserSession = class {
|
|
|
1551
2163
|
}
|
|
1552
2164
|
await page.waitForLoadState("load", { timeout: LOAD_GRACE_MS }).catch(() => void 0);
|
|
1553
2165
|
}
|
|
2166
|
+
/**
|
|
2167
|
+
* `page.goto`, past the late error page of the tab's previous failed or stopped navigation:
|
|
2168
|
+
* Chromium can commit that page after Playwright reported the failure (the Linux full build),
|
|
2169
|
+
* and its commit interrupts a navigation started meanwhile. Then this navigation is followed
|
|
2170
|
+
* when it went on in Chromium, and otherwise started once more (the error page is in now),
|
|
2171
|
+
* within the navigation's own deadline.
|
|
2172
|
+
*/
|
|
2173
|
+
async gotoPastLateErrorPage(page, url, watch, deadline) {
|
|
2174
|
+
try {
|
|
2175
|
+
return await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
2176
|
+
} catch (e) {
|
|
2177
|
+
if (!ERROR_PAGE_INTERRUPT_RE.test(firstLine(e))) throw e;
|
|
2178
|
+
const out = await watch.follow(deadline);
|
|
2179
|
+
if (out.kind === "loaded") return out.response;
|
|
2180
|
+
if (out.kind === "failed") throw new Error(`${out.error} at ${out.url}`);
|
|
2181
|
+
return page.goto(url, {
|
|
2182
|
+
waitUntil: "domcontentloaded",
|
|
2183
|
+
timeout: Math.max(1, deadline - Date.now())
|
|
2184
|
+
});
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
1554
2187
|
/**
|
|
1555
2188
|
* The error of a navigation that failed with Chromium's net error `error`: a host the proxy
|
|
1556
2189
|
* refuses is reported as blocked (`run` turns `original` into the blocked reply); any other net
|
|
@@ -1561,7 +2194,7 @@ var BrowserSession = class {
|
|
|
1561
2194
|
if (net === void 0 || net === "ERR_ABORTED") return original;
|
|
1562
2195
|
await this.errorPageCommitted(page);
|
|
1563
2196
|
if (PROXY_FAILURE_RE.test(error)) {
|
|
1564
|
-
if (await this.checkBlocked(url)) return original;
|
|
2197
|
+
if (await this.checkBlocked(page, url)) return original;
|
|
1565
2198
|
const detail = await this.networkDetail(url);
|
|
1566
2199
|
return new ActionError(
|
|
1567
2200
|
"unreachable",
|
|
@@ -1647,9 +2280,12 @@ var BrowserSession = class {
|
|
|
1647
2280
|
}
|
|
1648
2281
|
}
|
|
1649
2282
|
async exec(req) {
|
|
2283
|
+
const conv = req.conversationId;
|
|
2284
|
+
const root = req.rootConversationId;
|
|
2285
|
+
const activePage = () => this.pageFor(conv, root);
|
|
1650
2286
|
switch (req.op) {
|
|
1651
2287
|
case "navigate": {
|
|
1652
|
-
const page = await
|
|
2288
|
+
const page = await activePage();
|
|
1653
2289
|
if (req.url !== void 0) {
|
|
1654
2290
|
await this.goto(page, normalizeUrl(req.url));
|
|
1655
2291
|
} else if (req.action === "back" || req.action === "forward") {
|
|
@@ -1678,40 +2314,40 @@ var BrowserSession = class {
|
|
|
1678
2314
|
case "tabs":
|
|
1679
2315
|
return this.tabsOp(req);
|
|
1680
2316
|
case "snapshot":
|
|
1681
|
-
return { text: await takeSnapshot(await
|
|
2317
|
+
return { text: await takeSnapshot(await activePage()) };
|
|
1682
2318
|
case "click":
|
|
1683
2319
|
case "hover": {
|
|
1684
|
-
const page = await
|
|
2320
|
+
const page = await activePage();
|
|
1685
2321
|
const loc = await this.locate(page, req.ref);
|
|
1686
2322
|
if (req.op === "hover") {
|
|
1687
2323
|
await loc.hover();
|
|
1688
2324
|
return {};
|
|
1689
2325
|
}
|
|
1690
2326
|
const at = await this.centerOf(loc);
|
|
1691
|
-
if (at !== void 0) this.markClick(at.x, at.y);
|
|
2327
|
+
if (at !== void 0) this.markClick(page, at.x, at.y);
|
|
1692
2328
|
await this.settled(page, () => loc.click());
|
|
1693
2329
|
return {};
|
|
1694
2330
|
}
|
|
1695
2331
|
case "type": {
|
|
1696
|
-
const page = await
|
|
2332
|
+
const page = await activePage();
|
|
1697
2333
|
const loc = await this.locate(page, req.ref);
|
|
1698
2334
|
await loc.fill(req.text);
|
|
1699
2335
|
if (req.submit === true) await this.settled(page, () => loc.press("Enter"));
|
|
1700
2336
|
return {};
|
|
1701
2337
|
}
|
|
1702
2338
|
case "select": {
|
|
1703
|
-
const page = await
|
|
2339
|
+
const page = await activePage();
|
|
1704
2340
|
const loc = await this.locate(page, req.ref);
|
|
1705
2341
|
const chosen = await this.settled(page, () => loc.selectOption(req.values));
|
|
1706
2342
|
return { text: `Selected ${JSON.stringify(chosen)}.` };
|
|
1707
2343
|
}
|
|
1708
2344
|
case "press": {
|
|
1709
|
-
const page = await
|
|
2345
|
+
const page = await activePage();
|
|
1710
2346
|
await this.settled(page, () => page.keyboard.press(req.key));
|
|
1711
2347
|
return {};
|
|
1712
2348
|
}
|
|
1713
2349
|
case "scroll": {
|
|
1714
|
-
const page = await
|
|
2350
|
+
const page = await activePage();
|
|
1715
2351
|
if (req.ref !== void 0) {
|
|
1716
2352
|
const loc = await this.locate(page, req.ref);
|
|
1717
2353
|
await loc.scrollIntoViewIfNeeded();
|
|
@@ -1725,7 +2361,7 @@ var BrowserSession = class {
|
|
|
1725
2361
|
return {};
|
|
1726
2362
|
}
|
|
1727
2363
|
case "upload": {
|
|
1728
|
-
const page = await
|
|
2364
|
+
const page = await activePage();
|
|
1729
2365
|
const file = this.readUpload(req.path);
|
|
1730
2366
|
const loc = await this.locate(page, req.ref);
|
|
1731
2367
|
await loc.setInputFiles(file);
|
|
@@ -1733,17 +2369,17 @@ var BrowserSession = class {
|
|
|
1733
2369
|
}
|
|
1734
2370
|
case "click_at":
|
|
1735
2371
|
case "type_at": {
|
|
1736
|
-
const page = await
|
|
2372
|
+
const page = await activePage();
|
|
1737
2373
|
this.checkPoint(req.x, req.y);
|
|
1738
|
-
this.markClick(Math.round(req.x), Math.round(req.y));
|
|
2374
|
+
this.markClick(page, Math.round(req.x), Math.round(req.y));
|
|
1739
2375
|
await this.settled(page, () => page.mouse.click(req.x, req.y));
|
|
1740
2376
|
if (req.op === "type_at") await page.keyboard.type(req.text);
|
|
1741
2377
|
return {};
|
|
1742
2378
|
}
|
|
1743
2379
|
case "wait":
|
|
1744
|
-
return this.waitOp(req);
|
|
2380
|
+
return this.waitOp(req, await activePage());
|
|
1745
2381
|
case "screenshot": {
|
|
1746
|
-
const page = await
|
|
2382
|
+
const page = await activePage();
|
|
1747
2383
|
const maxBytes = Math.min(MAX_SCREENSHOT_BYTES, Math.floor(req.maxBytes));
|
|
1748
2384
|
if (!(maxBytes > 0))
|
|
1749
2385
|
throw new ActionError("failed", "screenshot needs a positive maxBytes.");
|
|
@@ -1774,56 +2410,79 @@ var BrowserSession = class {
|
|
|
1774
2410
|
);
|
|
1775
2411
|
}
|
|
1776
2412
|
async tabsOp(req) {
|
|
2413
|
+
const conv = req.conversationId;
|
|
2414
|
+
const root = req.rootConversationId;
|
|
1777
2415
|
switch (req.action) {
|
|
1778
|
-
case "list":
|
|
1779
|
-
|
|
1780
|
-
const lines = await Promise.all(
|
|
1781
|
-
[...this.pages.entries()].map(async ([id, p]) => {
|
|
1782
|
-
const info = await this.pageInfo(p);
|
|
1783
|
-
return `${id}${id === this.active ? " (active)" : ""}: ${info.title || "(no title)"} ${info.url}`;
|
|
1784
|
-
})
|
|
1785
|
-
);
|
|
1786
|
-
return { text: lines.join("\n") };
|
|
1787
|
-
}
|
|
2416
|
+
case "list":
|
|
2417
|
+
return { text: await this.listText(conv, root) };
|
|
1788
2418
|
case "open": {
|
|
1789
|
-
const page = await this.openTab();
|
|
2419
|
+
const page = this.countOf(conv) === 0 ? await this.pageFor(conv, root) : await this.openTab(conv, root);
|
|
1790
2420
|
if (req.url !== void 0) await this.goto(page, normalizeUrl(req.url));
|
|
1791
2421
|
return {};
|
|
1792
2422
|
}
|
|
1793
2423
|
case "switch": {
|
|
1794
|
-
|
|
1795
|
-
this.
|
|
1796
|
-
await page.bringToFront().catch(() => void 0);
|
|
1797
|
-
this.emit({ type: "active" });
|
|
2424
|
+
this.ownTab(conv, req.tabId);
|
|
2425
|
+
this.setActive(conv, req.tabId);
|
|
1798
2426
|
return {};
|
|
1799
2427
|
}
|
|
1800
2428
|
case "close": {
|
|
1801
|
-
const page = this.
|
|
2429
|
+
const page = this.ownTab(conv, req.tabId);
|
|
1802
2430
|
await page.close();
|
|
1803
|
-
|
|
2431
|
+
return {};
|
|
2432
|
+
}
|
|
2433
|
+
case "adopt": {
|
|
2434
|
+
const id = req.tabId;
|
|
2435
|
+
const result = this.adopt(conv, root, this.tabIdOf(this.tabById(id)));
|
|
2436
|
+
if (result === "in_use") throw new ActionError("failed", tabInUse(id ?? ""));
|
|
2437
|
+
if (result === "cap") throw new ActionError("failed", CONVERSATION_CAP_MESSAGE);
|
|
1804
2438
|
return {};
|
|
1805
2439
|
}
|
|
1806
2440
|
}
|
|
1807
|
-
throw new ActionError("failed", "tabs needs an action: list, open, switch or
|
|
2441
|
+
throw new ActionError("failed", "tabs needs an action: list, open, switch, close or adopt.");
|
|
1808
2442
|
}
|
|
1809
|
-
/**
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
`The browser has ${MAX_TABS} tabs open, the most it keeps; close one first.`
|
|
1815
|
-
);
|
|
1816
|
-
}
|
|
1817
|
-
const page = await this.ctx.newPage();
|
|
1818
|
-
this.track(page);
|
|
1819
|
-
this.active = this.ids.get(page);
|
|
1820
|
-
this.emit({ type: "active" });
|
|
1821
|
-
this.emit({ type: "state" });
|
|
2443
|
+
/** The conversation's own tab `tabId`; another conversation's is refused with what to do. */
|
|
2444
|
+
ownTab(conversationId, tabId) {
|
|
2445
|
+
const page = this.tabById(tabId);
|
|
2446
|
+
if (this.owners.get(tabId)?.owner !== conversationId)
|
|
2447
|
+
throw new ActionError("failed", notYourTab(tabId));
|
|
1822
2448
|
return page;
|
|
1823
2449
|
}
|
|
2450
|
+
/**
|
|
2451
|
+
* The tab list of a conversation: its own tabs, then its tree's other tabs, then other trees'
|
|
2452
|
+
* tabs (unowned ones too, but not a blank start tab), with ids, titles clipped to
|
|
2453
|
+
* OTHER_TITLE_CHARS and URLs without query or fragment outside its own; no conversation ids.
|
|
2454
|
+
*/
|
|
2455
|
+
async listText(conversationId, root) {
|
|
2456
|
+
const own = [];
|
|
2457
|
+
const tree = [];
|
|
2458
|
+
const device = [];
|
|
2459
|
+
for (const [id, p] of this.live()) {
|
|
2460
|
+
const o = this.owners.get(id);
|
|
2461
|
+
if (o?.owner === conversationId) {
|
|
2462
|
+
const info = await this.pageInfo(p);
|
|
2463
|
+
const active = this.activeOf.get(conversationId) === id ? " (active)" : "";
|
|
2464
|
+
own.push(`${id}${active}: ${info.title || "(no title)"} ${info.url}`);
|
|
2465
|
+
continue;
|
|
2466
|
+
}
|
|
2467
|
+
if (isStartBlank({ owner: o?.owner, url: p.url() })) continue;
|
|
2468
|
+
const title = clip(this.titles.get(id) ?? "", OTHER_TITLE_CHARS) || "(no title)";
|
|
2469
|
+
const line = `${id}: ${title} ${bareUrl(clip(p.url(), MAX_URL_CHARS))}`;
|
|
2470
|
+
if (o !== void 0 && o.root === root) tree.push(line);
|
|
2471
|
+
else device.push(line);
|
|
2472
|
+
}
|
|
2473
|
+
const lines = [...own];
|
|
2474
|
+
if (tree.length > 0) lines.push("Tabs of other conversations in this task:", ...tree);
|
|
2475
|
+
if (device.length > 0) lines.push("Tabs of other conversations on this device:", ...device);
|
|
2476
|
+
if (tree.length + device.length > 0)
|
|
2477
|
+
lines.push(
|
|
2478
|
+
"To use another conversation's tab, call browser_tabs with action adopt and its tabId."
|
|
2479
|
+
);
|
|
2480
|
+
if (lines.length === 0) return "No browser tabs are open.";
|
|
2481
|
+
return lines.join("\n");
|
|
2482
|
+
}
|
|
1824
2483
|
tabById(tabId) {
|
|
1825
|
-
const page =
|
|
1826
|
-
if (page === void 0
|
|
2484
|
+
const page = this.pageById(tabId);
|
|
2485
|
+
if (page === void 0) {
|
|
1827
2486
|
throw new ActionError(
|
|
1828
2487
|
"failed",
|
|
1829
2488
|
`No tab ${JSON.stringify(tabId ?? "")}; list the tabs to see their ids.`
|
|
@@ -1831,18 +2490,13 @@ var BrowserSession = class {
|
|
|
1831
2490
|
}
|
|
1832
2491
|
return page;
|
|
1833
2492
|
}
|
|
1834
|
-
/**
|
|
1835
|
-
async
|
|
1836
|
-
|
|
1837
|
-
this.active = tabId;
|
|
1838
|
-
await page.bringToFront().catch(() => void 0);
|
|
1839
|
-
this.emit({ type: "active" });
|
|
1840
|
-
this.emit({ type: "state" });
|
|
2493
|
+
/** Brings a tab to the front of the window (only a viewer in control decides what is front). */
|
|
2494
|
+
async bringToFront(tabId) {
|
|
2495
|
+
await this.pageById(tabId)?.bringToFront().catch(() => void 0);
|
|
1841
2496
|
}
|
|
1842
|
-
/** Navigates
|
|
1843
|
-
async viewerNav(nav) {
|
|
2497
|
+
/** Navigates a tab for a viewer (address bar, back, forward, reload); never throws. */
|
|
2498
|
+
async viewerNav(page, nav) {
|
|
1844
2499
|
try {
|
|
1845
|
-
const page = await this.activePage();
|
|
1846
2500
|
if (nav.url !== void 0) await this.goto(page, normalizeUrl(nav.url));
|
|
1847
2501
|
else if (nav.action === "back") await page.goBack();
|
|
1848
2502
|
else if (nav.action === "forward") await page.goForward();
|
|
@@ -1850,10 +2504,9 @@ var BrowserSession = class {
|
|
|
1850
2504
|
} catch {
|
|
1851
2505
|
}
|
|
1852
2506
|
}
|
|
1853
|
-
async waitOp(req) {
|
|
2507
|
+
async waitOp(req, page) {
|
|
1854
2508
|
const asked = Number(req.timeoutMs);
|
|
1855
2509
|
const timeout = Number.isFinite(asked) ? Math.max(0, Math.min(MAX_WAIT_MS, Math.floor(asked))) : DEFAULT_WAIT_MS;
|
|
1856
|
-
const page = await this.activePage();
|
|
1857
2510
|
if (req.text !== void 0) {
|
|
1858
2511
|
await page.getByText(req.text).first().waitFor({ state: "visible", timeout }).catch((e) => {
|
|
1859
2512
|
throw e instanceof Error && e.name === "TimeoutError" ? new ActionError(
|
|
@@ -2245,6 +2898,8 @@ async function restoreCookies(ctx, stateDir) {
|
|
|
2245
2898
|
import { readFileSync as readFileSync3, renameSync as renameSync2, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
|
|
2246
2899
|
import { join as join4 } from "node:path";
|
|
2247
2900
|
var RESTORE_MAX_AGE_MS = 10 * 6e4;
|
|
2901
|
+
var SESSION_FILE_VERSION = 2;
|
|
2902
|
+
var isId = (v) => typeof v === "string" && v !== "";
|
|
2248
2903
|
var SessionFile = class {
|
|
2249
2904
|
file;
|
|
2250
2905
|
clock;
|
|
@@ -2254,7 +2909,7 @@ var SessionFile = class {
|
|
|
2254
2909
|
this.file = join4(stateDir, "session.json");
|
|
2255
2910
|
this.clock = clock;
|
|
2256
2911
|
}
|
|
2257
|
-
/** The saved session when it is recent and well formed; undefined otherwise. */
|
|
2912
|
+
/** The saved session when it is recent, of this format and well formed; undefined otherwise. */
|
|
2258
2913
|
read() {
|
|
2259
2914
|
let raw;
|
|
2260
2915
|
try {
|
|
@@ -2263,42 +2918,67 @@ var SessionFile = class {
|
|
|
2263
2918
|
return void 0;
|
|
2264
2919
|
}
|
|
2265
2920
|
const s = raw;
|
|
2266
|
-
if (typeof s !== "object" || s === null || typeof s.savedAt !== "number" || this.clock.now() - s.savedAt > RESTORE_MAX_AGE_MS || !Array.isArray(s.tabs))
|
|
2921
|
+
if (typeof s !== "object" || s === null || s.version !== SESSION_FILE_VERSION || typeof s.savedAt !== "number" || this.clock.now() - s.savedAt > RESTORE_MAX_AGE_MS || !Array.isArray(s.tabs) || !Array.isArray(s.conversations))
|
|
2267
2922
|
return void 0;
|
|
2268
2923
|
const tabs = s.tabs.filter(
|
|
2269
|
-
(
|
|
2270
|
-
);
|
|
2271
|
-
const
|
|
2272
|
-
const
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2924
|
+
(t) => typeof t === "object" && t !== null && typeof t.url === "string" && /^https?:\/\//i.test(t.url) && isId(t.owner) && isId(t.root)
|
|
2925
|
+
).slice(0, MAX_TABS).map((t) => ({ url: t.url, owner: t.owner, root: t.root }));
|
|
2926
|
+
const conversations = [];
|
|
2927
|
+
for (const c of s.conversations) {
|
|
2928
|
+
if (typeof c !== "object" || c === null) continue;
|
|
2929
|
+
const x = c;
|
|
2930
|
+
if (!isId(x.id) || !isId(x.root)) continue;
|
|
2931
|
+
const h = x.handoff;
|
|
2932
|
+
const handoff = typeof h === "object" && h !== null && isId(h.callId) && typeof h.message === "string" ? { callId: h.callId, message: h.message.slice(0, HANDOFF_MAX_CHARS) } : void 0;
|
|
2933
|
+
const parks = Array.isArray(x.parks) ? x.parks.filter(
|
|
2934
|
+
(p) => typeof p === "object" && p !== null && isId(p.kind) && typeof p.key === "string"
|
|
2935
|
+
).map((p) => ({ kind: p.kind, key: p.key })) : [];
|
|
2936
|
+
conversations.push({
|
|
2937
|
+
id: x.id,
|
|
2938
|
+
root: x.root,
|
|
2939
|
+
...typeof x.active === "number" ? { active: x.active } : {},
|
|
2940
|
+
...handoff === void 0 ? {} : { handoff },
|
|
2941
|
+
...parks.length === 0 ? {} : { parks }
|
|
2942
|
+
});
|
|
2943
|
+
}
|
|
2944
|
+
return { version: SESSION_FILE_VERSION, savedAt: s.savedAt, tabs, conversations };
|
|
2279
2945
|
}
|
|
2280
2946
|
/**
|
|
2281
|
-
* Writes the session soon (coalesced): the tabs
|
|
2282
|
-
* the write runs.
|
|
2947
|
+
* Writes the session soon (coalesced): the tabs with their owners, and per conversation its
|
|
2948
|
+
* active tab, pending handoff and persistent parks, all read when the write runs.
|
|
2283
2949
|
*/
|
|
2284
|
-
save(session,
|
|
2950
|
+
save(session, state) {
|
|
2285
2951
|
if (this.queued || this.forgotten) return;
|
|
2286
2952
|
this.queued = true;
|
|
2287
2953
|
queueMicrotask(() => {
|
|
2288
2954
|
this.queued = false;
|
|
2289
2955
|
if (this.forgotten) return;
|
|
2290
|
-
const
|
|
2291
|
-
|
|
2292
|
-
const kept = all.filter((t) => /^https?:\/\//i.test(t.url));
|
|
2293
|
-
const active = Math.max(
|
|
2294
|
-
0,
|
|
2295
|
-
kept.findIndex((t) => t.active)
|
|
2956
|
+
const kept = session.tabs().filter(
|
|
2957
|
+
(t) => t.owner !== void 0 && t.root !== void 0 && /^https?:\/\//i.test(t.url)
|
|
2296
2958
|
);
|
|
2959
|
+
const tabs = kept.map((t) => ({ url: t.url, owner: t.owner, root: t.root }));
|
|
2960
|
+
const ids = /* @__PURE__ */ new Set([...kept.map((t) => t.owner), ...state.conversationIds]);
|
|
2961
|
+
const conversations = [];
|
|
2962
|
+
for (const id of ids) {
|
|
2963
|
+
const root = kept.find((t) => t.owner === id)?.root ?? state.rootOf(id);
|
|
2964
|
+
if (root === void 0) continue;
|
|
2965
|
+
const active = kept.findIndex((t) => t.owner === id && t.active);
|
|
2966
|
+
const handoff = state.handoffOf(id);
|
|
2967
|
+
const parks = state.parksOf(id).filter((p) => p.persist === true && p.tabId === void 0 && p.kind !== HANDOFF_PARK).map((p) => ({ kind: p.kind, key: p.key }));
|
|
2968
|
+
if (active < 0 && handoff === void 0 && parks.length === 0) continue;
|
|
2969
|
+
conversations.push({
|
|
2970
|
+
id,
|
|
2971
|
+
root,
|
|
2972
|
+
...active < 0 ? {} : { active },
|
|
2973
|
+
...handoff === void 0 ? {} : { handoff },
|
|
2974
|
+
...parks.length === 0 ? {} : { parks }
|
|
2975
|
+
});
|
|
2976
|
+
}
|
|
2297
2977
|
const body = {
|
|
2978
|
+
version: SESSION_FILE_VERSION,
|
|
2298
2979
|
savedAt: this.clock.now(),
|
|
2299
|
-
tabs
|
|
2300
|
-
|
|
2301
|
-
...handoff === void 0 ? {} : { handoff }
|
|
2980
|
+
tabs,
|
|
2981
|
+
conversations
|
|
2302
2982
|
};
|
|
2303
2983
|
try {
|
|
2304
2984
|
writeFileSync3(`${this.file}.tmp`, JSON.stringify(body), { mode: 384 });
|
|
@@ -2312,23 +2992,32 @@ var SessionFile = class {
|
|
|
2312
2992
|
this.forgotten = true;
|
|
2313
2993
|
rmSync2(this.file, { force: true });
|
|
2314
2994
|
}
|
|
2995
|
+
/** Gives the conversations their pending handoffs and persistent parks back. */
|
|
2996
|
+
restoreState(state, saved) {
|
|
2997
|
+
for (const c of saved.conversations) {
|
|
2998
|
+
if (c.handoff !== void 0)
|
|
2999
|
+
state.noticeHandoff(c.id, c.root, c.handoff.callId, c.handoff.message);
|
|
3000
|
+
for (const p of c.parks ?? []) state.park(c.id, c.root, { ...p, persist: true });
|
|
3001
|
+
}
|
|
3002
|
+
}
|
|
2315
3003
|
/**
|
|
2316
|
-
* Opens the saved tabs in a browser that has only its first blank tab
|
|
2317
|
-
*
|
|
3004
|
+
* Opens the saved tabs, each for its owner, in a browser that has only its first blank tab, and
|
|
3005
|
+
* gives each conversation its active tab back. The navigations are started, not awaited: the
|
|
3006
|
+
* first request after a restart does not wait for every page to load.
|
|
2318
3007
|
*/
|
|
2319
3008
|
async restoreTabs(session, saved) {
|
|
2320
|
-
if (saved.tabs.length === 0) return;
|
|
2321
3009
|
const ids = [];
|
|
2322
|
-
for (const
|
|
3010
|
+
for (const t of saved.tabs) {
|
|
2323
3011
|
try {
|
|
2324
|
-
|
|
2325
|
-
ids.push(session.tabIdOf(page));
|
|
2326
|
-
void session.viewerNav({ url: normalizeUrl(url) });
|
|
3012
|
+
ids.push(await session.restoreTab({ owner: t.owner, root: t.root }, t.url));
|
|
2327
3013
|
} catch {
|
|
3014
|
+
ids.push(void 0);
|
|
2328
3015
|
}
|
|
2329
3016
|
}
|
|
2330
|
-
const
|
|
2331
|
-
|
|
3017
|
+
for (const c of saved.conversations) {
|
|
3018
|
+
const id = c.active === void 0 ? void 0 : ids[c.active];
|
|
3019
|
+
if (id !== void 0) session.setActive(c.id, id);
|
|
3020
|
+
}
|
|
2332
3021
|
}
|
|
2333
3022
|
};
|
|
2334
3023
|
|
|
@@ -2630,6 +3319,19 @@ var clampDim = (n, max) => typeof n === "number" && Number.isFinite(n) ? Math.ma
|
|
|
2630
3319
|
var ViewerSession = class {
|
|
2631
3320
|
browser;
|
|
2632
3321
|
state;
|
|
3322
|
+
/** The viewer's conversation and the root of its tree (from the kernel's viewer context). */
|
|
3323
|
+
conv;
|
|
3324
|
+
root;
|
|
3325
|
+
/** The tab the person (or a new handoff) chose to show; cleared when it leaves the tree. */
|
|
3326
|
+
choice;
|
|
3327
|
+
/** The tab shown now (computed with every state). */
|
|
3328
|
+
shown;
|
|
3329
|
+
/** The other conversation the viewer pins because it shows one of its tabs. */
|
|
3330
|
+
pinnedOwner;
|
|
3331
|
+
/** Group keys of the tree's other conversations, in order of first appearance. */
|
|
3332
|
+
groups = /* @__PURE__ */ new Map();
|
|
3333
|
+
/** The tree's handoffs seen so far (a new one moves the shown tab to its conversation). */
|
|
3334
|
+
seenHandoffs = /* @__PURE__ */ new Set();
|
|
2633
3335
|
out;
|
|
2634
3336
|
clock;
|
|
2635
3337
|
frameMaxBytes;
|
|
@@ -2691,6 +3393,9 @@ var ViewerSession = class {
|
|
|
2691
3393
|
constructor(opts) {
|
|
2692
3394
|
this.browser = opts.browser;
|
|
2693
3395
|
this.state = opts.state;
|
|
3396
|
+
this.conv = opts.conversationId;
|
|
3397
|
+
this.root = opts.rootConversationId;
|
|
3398
|
+
this.state.pin(this.conv, this.root);
|
|
2694
3399
|
this.out = opts.transport;
|
|
2695
3400
|
this.clock = opts.clock;
|
|
2696
3401
|
this.frameMaxBytes = opts.frameMaxBytes;
|
|
@@ -2711,8 +3416,11 @@ var ViewerSession = class {
|
|
|
2711
3416
|
this.browser.on((e) => this.onSession(e)),
|
|
2712
3417
|
this.state.onChange(() => this.onControl())
|
|
2713
3418
|
);
|
|
3419
|
+
const pending = this.state.handoffs(this.root);
|
|
3420
|
+
this.seenHandoffs = new Set(pending.map(handoffKey));
|
|
3421
|
+
const newest = pending.at(-1);
|
|
3422
|
+
if (newest !== void 0) this.choice = this.browser.activeTabOf(newest.conversationId);
|
|
2714
3423
|
this.sendState();
|
|
2715
|
-
void this.serial(() => this.attach());
|
|
2716
3424
|
this.ping();
|
|
2717
3425
|
return;
|
|
2718
3426
|
}
|
|
@@ -2738,7 +3446,7 @@ var ViewerSession = class {
|
|
|
2738
3446
|
return;
|
|
2739
3447
|
}
|
|
2740
3448
|
case "takeover":
|
|
2741
|
-
this.state.takeover();
|
|
3449
|
+
this.state.takeover(this.browser.owningConversations());
|
|
2742
3450
|
return;
|
|
2743
3451
|
case "handback":
|
|
2744
3452
|
this.inputGen++;
|
|
@@ -2752,22 +3460,38 @@ var ViewerSession = class {
|
|
|
2752
3460
|
this.input(() => this.key(m));
|
|
2753
3461
|
return;
|
|
2754
3462
|
case "nav":
|
|
2755
|
-
this.input(
|
|
2756
|
-
|
|
3463
|
+
this.input(async () => {
|
|
3464
|
+
let page = this.shownPage();
|
|
3465
|
+
if (page === void 0) {
|
|
3466
|
+
if (typeof m.url !== "string") return;
|
|
3467
|
+
page = await this.openOwnTab();
|
|
3468
|
+
if (page === void 0) return;
|
|
3469
|
+
}
|
|
3470
|
+
this.concern(this.browser.tabIdOf(page));
|
|
3471
|
+
await this.browser.viewerNav(page, {
|
|
2757
3472
|
...typeof m.url === "string" ? { url: m.url } : {},
|
|
2758
3473
|
...m.action === void 0 ? {} : { action: m.action }
|
|
2759
|
-
})
|
|
2760
|
-
);
|
|
3474
|
+
});
|
|
3475
|
+
});
|
|
3476
|
+
return;
|
|
3477
|
+
case "show":
|
|
3478
|
+
if (typeof m.tabId === "string" && this.inTree(m.tabId)) {
|
|
3479
|
+
this.choice = m.tabId;
|
|
3480
|
+
this.queueState();
|
|
3481
|
+
}
|
|
2761
3482
|
return;
|
|
2762
3483
|
case "tab":
|
|
3484
|
+
if (m.action === "adopt") {
|
|
3485
|
+
this.adopt(m.tabId);
|
|
3486
|
+
return;
|
|
3487
|
+
}
|
|
2763
3488
|
this.input(async () => {
|
|
2764
3489
|
try {
|
|
2765
|
-
if (m.action === "open") await this.
|
|
2766
|
-
else if (m.action === "switch" && m.tabId !== void 0)
|
|
2767
|
-
|
|
2768
|
-
|
|
3490
|
+
if (m.action === "open") await this.openOwnTab();
|
|
3491
|
+
else if (m.action === "switch" && m.tabId !== void 0) this.switchTo(m.tabId);
|
|
3492
|
+
else if (m.action === "close" && m.tabId !== void 0 && this.inTree(m.tabId)) {
|
|
3493
|
+
this.concern(m.tabId);
|
|
2769
3494
|
await this.browser.tabById(m.tabId).close();
|
|
2770
|
-
await this.browser.activePage();
|
|
2771
3495
|
}
|
|
2772
3496
|
} catch {
|
|
2773
3497
|
}
|
|
@@ -2775,6 +3499,69 @@ var ViewerSession = class {
|
|
|
2775
3499
|
return;
|
|
2776
3500
|
}
|
|
2777
3501
|
}
|
|
3502
|
+
/** Whether a tab belongs to the viewer's tree. */
|
|
3503
|
+
inTree(tabId) {
|
|
3504
|
+
return this.browser.ownerOf(tabId)?.root === this.root && this.browser.pageById(tabId) !== void 0;
|
|
3505
|
+
}
|
|
3506
|
+
/** The shown tab's page, if the tree has one. */
|
|
3507
|
+
shownPage() {
|
|
3508
|
+
return this.browser.pageById(this.shown);
|
|
3509
|
+
}
|
|
3510
|
+
/** The person used a tab during the takeover: its owner gets the hand back note. */
|
|
3511
|
+
concern(tabId) {
|
|
3512
|
+
if (tabId === void 0) return;
|
|
3513
|
+
this.state.concern(this.browser.ownerOf(tabId)?.owner);
|
|
3514
|
+
}
|
|
3515
|
+
/**
|
|
3516
|
+
* A tab the person opens: owned by the viewer's conversation, made its active tab and shown
|
|
3517
|
+
* (the caps apply; a refusal opens nothing).
|
|
3518
|
+
*/
|
|
3519
|
+
async openOwnTab() {
|
|
3520
|
+
try {
|
|
3521
|
+
const page = this.browser.countOf(this.conv) === 0 ? await this.browser.pageFor(this.conv, this.root) : await this.browser.openTab(this.conv, this.root);
|
|
3522
|
+
this.state.concern(this.conv);
|
|
3523
|
+
this.choice = void 0;
|
|
3524
|
+
this.queueState();
|
|
3525
|
+
return page;
|
|
3526
|
+
} catch {
|
|
3527
|
+
return void 0;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
/**
|
|
3531
|
+
* Shows a tab of the tree and brings it to the front (a viewer in control decides what is
|
|
3532
|
+
* front). A tab of the viewer's own conversation also becomes its active tab (the person chose
|
|
3533
|
+
* it); another conversation's tab changes only the shown tab, never that agent's active tab.
|
|
3534
|
+
*/
|
|
3535
|
+
switchTo(tabId) {
|
|
3536
|
+
if (!this.inTree(tabId)) return;
|
|
3537
|
+
if (this.browser.ownerOf(tabId)?.owner === this.conv) {
|
|
3538
|
+
this.browser.setActive(this.conv, tabId);
|
|
3539
|
+
this.choice = void 0;
|
|
3540
|
+
} else this.choice = tabId;
|
|
3541
|
+
void this.browser.bringToFront(tabId);
|
|
3542
|
+
this.queueState();
|
|
3543
|
+
}
|
|
3544
|
+
/**
|
|
3545
|
+
* Moves another tree's tab (or an unowned one) to the viewer's conversation, as the tool's adopt;
|
|
3546
|
+
* a tab of the viewer's own tree is ignored (inside the tree the person drives tabs without
|
|
3547
|
+
* moving them). A tab whose owner is in use answers the `tab_in_use` notice.
|
|
3548
|
+
*/
|
|
3549
|
+
adopt(tabId) {
|
|
3550
|
+
if (typeof tabId !== "string" || this.browser.pageById(tabId) === void 0) return;
|
|
3551
|
+
if (this.browser.ownerOf(tabId)?.root === this.root) return;
|
|
3552
|
+
let result;
|
|
3553
|
+
try {
|
|
3554
|
+
result = this.browser.adopt(this.conv, this.root, tabId);
|
|
3555
|
+
} catch {
|
|
3556
|
+
return;
|
|
3557
|
+
}
|
|
3558
|
+
if (result === "in_use") this.send({ t: "notice", code: "tab_in_use" });
|
|
3559
|
+
else if (result === "cap") this.send({ t: "notice", code: "tab_cap" });
|
|
3560
|
+
else if (result === "ok") {
|
|
3561
|
+
this.choice = void 0;
|
|
3562
|
+
this.queueState();
|
|
3563
|
+
}
|
|
3564
|
+
}
|
|
2778
3565
|
/**
|
|
2779
3566
|
* Runs viewer input in arrival order, apart from the screencast chain, and only while the person
|
|
2780
3567
|
* holds control: input from a viewer in the watch state (the agent drives) is dropped, both when
|
|
@@ -2801,7 +3588,8 @@ var ViewerSession = class {
|
|
|
2801
3588
|
const slot = { ...p };
|
|
2802
3589
|
this.input(async () => {
|
|
2803
3590
|
if (this.moveSlot === slot) this.moveSlot = void 0;
|
|
2804
|
-
const page =
|
|
3591
|
+
const page = this.shownPage();
|
|
3592
|
+
if (page === void 0) return;
|
|
2805
3593
|
try {
|
|
2806
3594
|
await page.mouse.move(slot.x, slot.y);
|
|
2807
3595
|
} catch {
|
|
@@ -2860,11 +3648,14 @@ var ViewerSession = class {
|
|
|
2860
3648
|
get driving() {
|
|
2861
3649
|
return !this.closed && this.state.control === "user";
|
|
2862
3650
|
}
|
|
2863
|
-
/** Ends the viewer: stops its screencast and its subscriptions. */
|
|
3651
|
+
/** Ends the viewer: stops its screencast and its subscriptions, and lets go of its pins. */
|
|
2864
3652
|
async close() {
|
|
2865
3653
|
if (this.closed) return;
|
|
2866
3654
|
this.closed = true;
|
|
2867
3655
|
this.inputGen++;
|
|
3656
|
+
this.state.unpin(this.conv);
|
|
3657
|
+
if (this.pinnedOwner !== void 0) this.state.unpin(this.pinnedOwner);
|
|
3658
|
+
this.pinnedOwner = void 0;
|
|
2868
3659
|
await this.releaseHeld();
|
|
2869
3660
|
for (const u of this.unsubscribe.splice(0)) u();
|
|
2870
3661
|
for (const t of [this.pumpTimer, this.pingTimer, this.encodeTimer])
|
|
@@ -2884,8 +3675,18 @@ var ViewerSession = class {
|
|
|
2884
3675
|
this.send({ t: "ping", id });
|
|
2885
3676
|
this.pingTimer = this.clock.schedule(() => this.ping(), VIEWER_PING_MS);
|
|
2886
3677
|
}
|
|
2887
|
-
/**
|
|
3678
|
+
/**
|
|
3679
|
+
* Control or handoffs changed: a new state; input of the old control dropped; a new handoff of
|
|
3680
|
+
* the tree shows its conversation's active tab.
|
|
3681
|
+
*/
|
|
2888
3682
|
onControl() {
|
|
3683
|
+
const pending = this.state.handoffs(this.root);
|
|
3684
|
+
const fresh = pending.filter((h) => !this.seenHandoffs.has(handoffKey(h))).at(-1);
|
|
3685
|
+
this.seenHandoffs = new Set(pending.map(handoffKey));
|
|
3686
|
+
if (fresh !== void 0) {
|
|
3687
|
+
const tab = this.browser.activeTabOf(fresh.conversationId);
|
|
3688
|
+
if (tab !== void 0) this.choice = tab;
|
|
3689
|
+
}
|
|
2889
3690
|
const control = this.state.control;
|
|
2890
3691
|
if (control !== this.lastControl) {
|
|
2891
3692
|
this.lastControl = control;
|
|
@@ -2917,7 +3718,6 @@ var ViewerSession = class {
|
|
|
2917
3718
|
const plain = { ...m, tabs: m.tabs.map(({ favicon: _icon, ...t }) => t) };
|
|
2918
3719
|
if (fits(plain)) return plain;
|
|
2919
3720
|
m = plain;
|
|
2920
|
-
const handoff = m.handoff === void 0 ? {} : { handoff: { ...m.handoff, message: clip(m.handoff.message, 200) } };
|
|
2921
3721
|
small = {
|
|
2922
3722
|
...m,
|
|
2923
3723
|
tabs: m.tabs.map((t) => ({
|
|
@@ -2926,7 +3726,12 @@ var ViewerSession = class {
|
|
|
2926
3726
|
url: clip(t.url, 200),
|
|
2927
3727
|
...t.blocker === void 0 ? {} : { blocker: { ...t.blocker, url: clip(t.blocker.url, 200) } }
|
|
2928
3728
|
})),
|
|
2929
|
-
|
|
3729
|
+
otherTabs: m.otherTabs.map((t) => ({
|
|
3730
|
+
...t,
|
|
3731
|
+
title: clip(t.title, 200),
|
|
3732
|
+
url: clip(t.url, 200)
|
|
3733
|
+
})),
|
|
3734
|
+
handoffs: m.handoffs.map((h) => ({ ...h, message: clip(h.message, 200) }))
|
|
2930
3735
|
};
|
|
2931
3736
|
} else if (m.t === "dialog") {
|
|
2932
3737
|
small = { ...m, message: clip(m.message, 200) };
|
|
@@ -2953,14 +3758,12 @@ var ViewerSession = class {
|
|
|
2953
3758
|
onSession(e) {
|
|
2954
3759
|
if (e.type === "state") this.queueState();
|
|
2955
3760
|
else if (e.type === "active") {
|
|
3761
|
+
if (e.conversationId === this.conv) this.choice = void 0;
|
|
3762
|
+
else if (this.shown !== void 0 && this.browser.ownerOf(this.shown)?.owner === e.conversationId)
|
|
3763
|
+
this.choice = this.browser.activeTabOf(e.conversationId);
|
|
2956
3764
|
this.queueState();
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
if (page === this.page) return;
|
|
2960
|
-
await this.detach();
|
|
2961
|
-
await this.attach();
|
|
2962
|
-
});
|
|
2963
|
-
} else if (e.type === "blocked") this.send({ t: "blocked", host: e.host, url: e.url });
|
|
3765
|
+
} else if (e.root !== this.root) return;
|
|
3766
|
+
else if (e.type === "blocked") this.send({ t: "blocked", host: e.host, url: e.url });
|
|
2964
3767
|
else this.send({ t: "dialog", kind: e.kind, message: e.message });
|
|
2965
3768
|
}
|
|
2966
3769
|
/** Coalesces bursts of changes into one state message. */
|
|
@@ -2972,18 +3775,88 @@ var ViewerSession = class {
|
|
|
2972
3775
|
this.sendState();
|
|
2973
3776
|
});
|
|
2974
3777
|
}
|
|
3778
|
+
/**
|
|
3779
|
+
* The tab to show: the chosen one while it is in the tree; else the viewer's conversation's
|
|
3780
|
+
* active tab; else the active tab of the tree's conversation that acted last; else the tree's
|
|
3781
|
+
* first tab; none when the tree owns no tab.
|
|
3782
|
+
*/
|
|
3783
|
+
computeShown(tree) {
|
|
3784
|
+
if (this.choice !== void 0 && tree.some((t) => t.id === this.choice)) return this.choice;
|
|
3785
|
+
this.choice = void 0;
|
|
3786
|
+
const own = this.browser.activeTabOf(this.conv);
|
|
3787
|
+
if (own !== void 0) return own;
|
|
3788
|
+
let best;
|
|
3789
|
+
for (const owner of new Set(tree.map((t) => t.owner))) {
|
|
3790
|
+
if (owner === void 0) continue;
|
|
3791
|
+
const tab = this.browser.activeTabOf(owner);
|
|
3792
|
+
const order = this.browser.actedOrder(owner);
|
|
3793
|
+
if (tab !== void 0 && (best === void 0 || order > best.order)) best = { tab, order };
|
|
3794
|
+
}
|
|
3795
|
+
return best?.tab ?? tree[0]?.id;
|
|
3796
|
+
}
|
|
3797
|
+
/** Pins the owner of the shown tab (besides the viewer's own conversation). */
|
|
3798
|
+
pinShownOwner() {
|
|
3799
|
+
const owner = this.shown === void 0 ? void 0 : this.browser.ownerOf(this.shown);
|
|
3800
|
+
const next = owner === void 0 || owner.owner === this.conv ? void 0 : owner.owner;
|
|
3801
|
+
if (next === this.pinnedOwner) return;
|
|
3802
|
+
if (next !== void 0 && owner !== void 0) this.state.pin(next, owner.root);
|
|
3803
|
+
if (this.pinnedOwner !== void 0) this.state.unpin(this.pinnedOwner);
|
|
3804
|
+
this.pinnedOwner = next;
|
|
3805
|
+
}
|
|
3806
|
+
groupOf(owner) {
|
|
3807
|
+
if (owner === this.conv || owner === void 0) return "own";
|
|
3808
|
+
let g = this.groups.get(owner);
|
|
3809
|
+
if (g === void 0) {
|
|
3810
|
+
g = `sub${this.groups.size + 1}`;
|
|
3811
|
+
this.groups.set(owner, g);
|
|
3812
|
+
}
|
|
3813
|
+
return g;
|
|
3814
|
+
}
|
|
2975
3815
|
sendState() {
|
|
2976
|
-
|
|
2977
|
-
const
|
|
2978
|
-
const
|
|
3816
|
+
if (this.closed) return;
|
|
3817
|
+
const all = this.browser.tabs();
|
|
3818
|
+
const tree = all.filter((t) => t.root === this.root);
|
|
3819
|
+
this.shown = this.computeShown(tree);
|
|
3820
|
+
this.pinShownOwner();
|
|
3821
|
+
if (this.browser.pageById(this.shown) !== this.page) {
|
|
3822
|
+
void this.serial(async () => {
|
|
3823
|
+
if (this.browser.pageById(this.shown) === this.page) return;
|
|
3824
|
+
await this.detach();
|
|
3825
|
+
await this.attach();
|
|
3826
|
+
});
|
|
3827
|
+
}
|
|
3828
|
+
const order = /* @__PURE__ */ new Map([["own", 0]]);
|
|
3829
|
+
const tabs = tree.map((t) => {
|
|
3830
|
+
const group = this.groupOf(t.owner);
|
|
3831
|
+
if (!order.has(group)) order.set(group, order.size);
|
|
3832
|
+
return {
|
|
3833
|
+
id: t.id,
|
|
3834
|
+
title: t.title,
|
|
3835
|
+
url: t.url,
|
|
3836
|
+
active: t.id === this.shown,
|
|
3837
|
+
group,
|
|
3838
|
+
...t.blocker === void 0 ? {} : { blocker: t.blocker },
|
|
3839
|
+
...t.favicon === void 0 ? {} : { favicon: t.favicon }
|
|
3840
|
+
};
|
|
3841
|
+
});
|
|
3842
|
+
tabs.sort((a, b) => (order.get(a.group) ?? 0) - (order.get(b.group) ?? 0));
|
|
3843
|
+
const shownTab = tree.find((t) => t.id === this.shown);
|
|
3844
|
+
const last = this.browser.lastClickOf(this.shown);
|
|
2979
3845
|
this.send({
|
|
2980
3846
|
t: "state",
|
|
2981
3847
|
tabs,
|
|
2982
|
-
|
|
2983
|
-
|
|
3848
|
+
...this.shown === void 0 ? {} : { shown: this.shown },
|
|
3849
|
+
otherTabs: all.filter((t) => t.root !== this.root && !isStartBlank(t)).map((t) => ({ id: t.id, title: t.title, url: t.url })),
|
|
3850
|
+
loading: this.browser.isLoading(this.shown),
|
|
3851
|
+
secure: shownTab?.url.startsWith("https:") ?? false,
|
|
2984
3852
|
control: this.state.control,
|
|
2985
3853
|
...last === void 0 ? {} : { lastClick: last },
|
|
2986
|
-
|
|
3854
|
+
handoffs: this.state.handoffs(this.root).map((h) => ({
|
|
3855
|
+
callId: h.callId,
|
|
3856
|
+
conversationId: h.conversationId,
|
|
3857
|
+
message: h.message,
|
|
3858
|
+
tabIds: this.browser.tabsOf(h.conversationId)
|
|
3859
|
+
}))
|
|
2987
3860
|
});
|
|
2988
3861
|
}
|
|
2989
3862
|
get scale() {
|
|
@@ -2995,7 +3868,8 @@ var ViewerSession = class {
|
|
|
2995
3868
|
}
|
|
2996
3869
|
async attach() {
|
|
2997
3870
|
if (this.closed) return;
|
|
2998
|
-
const page =
|
|
3871
|
+
const page = this.shownPage();
|
|
3872
|
+
if (page === void 0) return;
|
|
2999
3873
|
const cdp = await this.browser.context.newCDPSession(page);
|
|
3000
3874
|
this.page = page;
|
|
3001
3875
|
this.cdp = cdp;
|
|
@@ -3125,9 +3999,10 @@ var ViewerSession = class {
|
|
|
3125
3999
|
this.scheduleEncodeAck();
|
|
3126
4000
|
}
|
|
3127
4001
|
async mouse(m) {
|
|
3128
|
-
const page =
|
|
4002
|
+
const page = this.shownPage();
|
|
3129
4003
|
const p = point(m);
|
|
3130
|
-
if (p === null) return;
|
|
4004
|
+
if (p === null || page === void 0) return;
|
|
4005
|
+
this.concern(this.browser.tabIdOf(page));
|
|
3131
4006
|
const { x, y } = p;
|
|
3132
4007
|
const button = m.button === "right" ? "right" : "left";
|
|
3133
4008
|
try {
|
|
@@ -3150,7 +4025,9 @@ var ViewerSession = class {
|
|
|
3150
4025
|
if (m.kind !== "down") this.wantCursor(x, y);
|
|
3151
4026
|
}
|
|
3152
4027
|
async key(m) {
|
|
3153
|
-
const page =
|
|
4028
|
+
const page = this.shownPage();
|
|
4029
|
+
if (page === void 0) return;
|
|
4030
|
+
this.concern(this.browser.tabIdOf(page));
|
|
3154
4031
|
try {
|
|
3155
4032
|
if (m.kind === "char") {
|
|
3156
4033
|
const text = m.text ?? m.key;
|
|
@@ -3168,6 +4045,7 @@ var ViewerSession = class {
|
|
|
3168
4045
|
}
|
|
3169
4046
|
}
|
|
3170
4047
|
};
|
|
4048
|
+
var handoffKey = (h) => `${h.conversationId}\0${h.callId}`;
|
|
3171
4049
|
function point(m) {
|
|
3172
4050
|
const x = Number(m.x);
|
|
3173
4051
|
const y = Number(m.y);
|
|
@@ -3252,16 +4130,20 @@ async function startDaemon(opts) {
|
|
|
3252
4130
|
let closing = false;
|
|
3253
4131
|
const sessionFile = new SessionFile(opts.stateDir, clock);
|
|
3254
4132
|
const saved = sessionFile.read();
|
|
4133
|
+
let live;
|
|
3255
4134
|
const state = new DaemonState({
|
|
3256
4135
|
clock,
|
|
3257
4136
|
...opts.idleMs === void 0 ? {} : { idleMs: opts.idleMs },
|
|
4137
|
+
...opts.conversationIdleMs === void 0 ? {} : { conversationIdleMs: opts.conversationIdleMs },
|
|
3258
4138
|
onIdle: () => {
|
|
3259
4139
|
sessionFile.forget();
|
|
3260
4140
|
opts.onIdle?.();
|
|
4141
|
+
},
|
|
4142
|
+
onConversationIdle: (conversationId) => {
|
|
4143
|
+
void live?.closeConversation(conversationId);
|
|
3261
4144
|
}
|
|
3262
4145
|
});
|
|
3263
|
-
if (saved
|
|
3264
|
-
state.noticeHandoff(saved.handoff.callId, saved.handoff.message);
|
|
4146
|
+
if (saved !== void 0) sessionFile.restoreState(state, saved);
|
|
3265
4147
|
const launch = opts.launch ?? launchChromium;
|
|
3266
4148
|
const closeGraceMs = opts.browserCloseGraceMs ?? BROWSER_CLOSE_GRACE_MS;
|
|
3267
4149
|
let current;
|
|
@@ -3299,6 +4181,7 @@ async function startDaemon(opts) {
|
|
|
3299
4181
|
current = starting;
|
|
3300
4182
|
starting.catch(() => {
|
|
3301
4183
|
if (current === starting) current = void 0;
|
|
4184
|
+
live = void 0;
|
|
3302
4185
|
if (running?.launch === starting) running = void 0;
|
|
3303
4186
|
});
|
|
3304
4187
|
return starting;
|
|
@@ -3316,6 +4199,7 @@ async function startDaemon(opts) {
|
|
|
3316
4199
|
const session = new BrowserSession(ctx, {
|
|
3317
4200
|
proxy: plan.proxy,
|
|
3318
4201
|
clock,
|
|
4202
|
+
usage: state,
|
|
3319
4203
|
workspace: workspace === void 0 || workspace === "" ? void 0 : workspace,
|
|
3320
4204
|
downloadsTmp: plan.downloadsTmp,
|
|
3321
4205
|
downloadMaxBytes: downloadMaxBytes(opts.env),
|
|
@@ -3332,10 +4216,11 @@ async function startDaemon(opts) {
|
|
|
3332
4216
|
if (saved !== void 0) await sessionFile.restoreTabs(session, saved);
|
|
3333
4217
|
}
|
|
3334
4218
|
if (!closing) {
|
|
3335
|
-
const save = () => sessionFile.save(session,
|
|
4219
|
+
const save = () => sessionFile.save(session, state);
|
|
3336
4220
|
session.on(save);
|
|
3337
4221
|
state.onChange(save);
|
|
3338
4222
|
}
|
|
4223
|
+
if (!failed()) live = session;
|
|
3339
4224
|
return session;
|
|
3340
4225
|
}
|
|
3341
4226
|
}
|
|
@@ -3400,11 +4285,12 @@ async function startDaemon(opts) {
|
|
|
3400
4285
|
async function handoffOp(req) {
|
|
3401
4286
|
if (typeof req.callId !== "string" || req.callId === "")
|
|
3402
4287
|
return fail("The handoff needs a callId.");
|
|
4288
|
+
const conv = req.conversationId;
|
|
3403
4289
|
if (req.op === "handoff_notice") {
|
|
3404
4290
|
if (typeof req.message !== "string") return fail("The handoff notice needs a message.");
|
|
3405
|
-
state.noticeHandoff(req.callId, req.message);
|
|
4291
|
+
state.noticeHandoff(conv, req.rootConversationId, req.callId, req.message);
|
|
3406
4292
|
} else {
|
|
3407
|
-
state.clearHandoff(req.callId);
|
|
4293
|
+
state.clearHandoff(conv, req.callId);
|
|
3408
4294
|
if (req.handback === true) state.handback();
|
|
3409
4295
|
}
|
|
3410
4296
|
let session;
|
|
@@ -3413,15 +4299,38 @@ async function startDaemon(opts) {
|
|
|
3413
4299
|
} catch (e) {
|
|
3414
4300
|
return launchFailure(e);
|
|
3415
4301
|
}
|
|
3416
|
-
|
|
4302
|
+
const page = session.currentPage(conv);
|
|
4303
|
+
return {
|
|
4304
|
+
ok: true,
|
|
4305
|
+
...page === void 0 ? {} : { page: await session.pageInfo(page) },
|
|
4306
|
+
blockedHosts: []
|
|
4307
|
+
};
|
|
3417
4308
|
}
|
|
4309
|
+
const namesConversation = (raw) => {
|
|
4310
|
+
const r = raw;
|
|
4311
|
+
return typeof r.conversationId === "string" && r.conversationId !== "" && typeof r.rootConversationId === "string" && r.rootConversationId !== "";
|
|
4312
|
+
};
|
|
3418
4313
|
async function handleRequest(raw) {
|
|
3419
4314
|
if (typeof raw !== "object" || raw === null || !REQUEST_OPS.has(String(raw.op))) {
|
|
3420
4315
|
return fail("Unknown browser request.");
|
|
3421
4316
|
}
|
|
4317
|
+
if (!namesConversation(raw)) return fail(NO_CONVERSATION_MESSAGE);
|
|
3422
4318
|
const req = raw;
|
|
4319
|
+
const conv = req.conversationId;
|
|
4320
|
+
state.requestArrived(conv, req.rootConversationId);
|
|
4321
|
+
let reply;
|
|
4322
|
+
try {
|
|
4323
|
+
reply = await conversationRequest(req);
|
|
4324
|
+
} catch (e) {
|
|
4325
|
+
reply = fail(e instanceof ActionError ? e.message : String(e));
|
|
4326
|
+
} finally {
|
|
4327
|
+
state.requestFinished(conv);
|
|
4328
|
+
}
|
|
4329
|
+
return { ...reply, tabs: { open: live?.countOf(conv) ?? 0, max: MAX_TABS_PER_CONVERSATION } };
|
|
4330
|
+
}
|
|
4331
|
+
async function conversationRequest(req) {
|
|
3423
4332
|
if (req.op === "handoff_notice" || req.op === "handoff_clear") return handoffOp(req);
|
|
3424
|
-
state.dropHandoff();
|
|
4333
|
+
state.dropHandoff(req.conversationId);
|
|
3425
4334
|
if (state.control === "user" && req.op !== "wait") {
|
|
3426
4335
|
return { ok: false, error: "taken_over", message: TAKEN_OVER_MESSAGE, blockedHosts: [] };
|
|
3427
4336
|
}
|
|
@@ -3433,7 +4342,7 @@ async function startDaemon(opts) {
|
|
|
3433
4342
|
}
|
|
3434
4343
|
const reply = await session.run(req, { takenOver: () => state.control === "user" });
|
|
3435
4344
|
if (reply.ok) {
|
|
3436
|
-
const since = state.takeHandbackNote();
|
|
4345
|
+
const since = state.takeHandbackNote(req.conversationId);
|
|
3437
4346
|
if (since !== void 0) reply.userControlledSince = since;
|
|
3438
4347
|
}
|
|
3439
4348
|
return reply;
|
|
@@ -3443,6 +4352,7 @@ async function startDaemon(opts) {
|
|
|
3443
4352
|
sockets.add(sock);
|
|
3444
4353
|
const decoder = new ServiceMessageDecoder();
|
|
3445
4354
|
let kind;
|
|
4355
|
+
let context;
|
|
3446
4356
|
let handled = false;
|
|
3447
4357
|
let viewer;
|
|
3448
4358
|
let opened = false;
|
|
@@ -3465,37 +4375,51 @@ async function startDaemon(opts) {
|
|
|
3465
4375
|
if (kind === void 0) {
|
|
3466
4376
|
const first = parse(raw);
|
|
3467
4377
|
if (first?.kind === "request") kind = "request";
|
|
3468
|
-
else if (first?.kind === "stream")
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
4378
|
+
else if (first?.kind === "stream") kind = "stream";
|
|
4379
|
+
else sock.destroy();
|
|
4380
|
+
return;
|
|
4381
|
+
}
|
|
4382
|
+
if (kind === "stream" && context === void 0) {
|
|
4383
|
+
const ctx = parseViewerContext(raw);
|
|
4384
|
+
if (ctx === null) {
|
|
4385
|
+
sock.destroy();
|
|
4386
|
+
return;
|
|
4387
|
+
}
|
|
4388
|
+
context = {
|
|
4389
|
+
conversationId: ctx.conversationId,
|
|
4390
|
+
rootConversationId: ctx.rootConversationId
|
|
4391
|
+
};
|
|
4392
|
+
const viewerContext = context;
|
|
4393
|
+
opened = true;
|
|
4394
|
+
state.viewerOpened();
|
|
4395
|
+
browser().then(
|
|
4396
|
+
(session) => {
|
|
4397
|
+
if (sock.destroyed) return;
|
|
4398
|
+
viewer = new ViewerSession({
|
|
4399
|
+
browser: session,
|
|
4400
|
+
state,
|
|
4401
|
+
conversationId: viewerContext.conversationId,
|
|
4402
|
+
rootConversationId: viewerContext.rootConversationId,
|
|
4403
|
+
clock,
|
|
4404
|
+
frameMaxBytes: frameCap,
|
|
4405
|
+
transport: {
|
|
4406
|
+
send: (m) => {
|
|
4407
|
+
let dropped = false;
|
|
4408
|
+
const ok = write(sock, m, () => {
|
|
4409
|
+
dropped = true;
|
|
4410
|
+
});
|
|
4411
|
+
return ok || dropped || sock.destroyed;
|
|
4412
|
+
},
|
|
4413
|
+
sendFrame: (frame) => {
|
|
4414
|
+
if (sock.destroyed) return true;
|
|
4415
|
+
return sock.write(encodeServiceMessage(frame));
|
|
4416
|
+
},
|
|
4417
|
+
whenDrained: () => new Promise((r) => sock.once("drain", () => r()))
|
|
4418
|
+
}
|
|
4419
|
+
});
|
|
4420
|
+
},
|
|
4421
|
+
() => sock.destroy()
|
|
4422
|
+
);
|
|
3499
4423
|
return;
|
|
3500
4424
|
}
|
|
3501
4425
|
if (kind === "request") {
|