@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 713 lines 28.2 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - Names download files on the shared disk.
2 /** The manager owns the single debugger attachment; the relay shares it. */
3 import {
4 DesktopBrowserCommand,
5 DesktopBrowserEvent,
6 type DesktopBrowserEvent as DesktopBrowserEventType,
7 type PreviewAutomationLoadError,
8 } from "@t3tools/contracts";
9 import * as Context from "effect/Context";
10 import * as NodePath from "node:path";
11 import * as Effect from "effect/Effect";
12 import * as Fiber from "effect/Fiber";
13 import * as Layer from "effect/Layer";
14 import * as Option from "effect/Option";
15 import * as PubSub from "effect/PubSub";
16 import * as Schema from "effect/Schema";
17 import * as Stream from "effect/Stream";
18
19 import { createCdpRelayConnection, type CdpRelayConnection } from "./CdpRelay.ts";
20 import { sendPageCommand } from "./GuestInput.ts";
21
22 const encodeEvent = Schema.encodeSync(Schema.fromJsonString(DesktopBrowserEvent));
23 const decodeCommand = Schema.decodeUnknownOption(Schema.fromJsonString(DesktopBrowserCommand));
24 const lineEncoder = new TextEncoder();
25 const AGENT_INPUT_COMMAND =
26 /"method":"(?:Input\.|Page\.navigate"|Runtime\.(?:evaluate|callFunctionOn)")/;
27 /** The same commands as above, by method, for the relay's sends. */
28 const AGENT_INPUT_METHOD = /^(?:Input\.|Page\.navigate$|Runtime\.(?:evaluate|callFunctionOn)$)/;
29 const AGENT_INPUT_WINDOW_MS = 5_000;
30 /** A viewer's pointer travelling over the stream activates nothing on the page. */
31 const POINTER_TRAVEL = new Set<unknown>(["mouseMoved", "mouseWheel"]);
32 const FOCUS_IDLE = "5 seconds";
33 /** How long a screenshot waits for the client to draw its guest at full detail. */
34 const CAPTURE_DRAW_WAIT = "1 second";
35 /** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
36 const FIRST_PAGE_TIMEOUT = "5 seconds";
37 const ERR_ABORTED = -3;
38
39 export interface DesktopBrowserTabKey {
40 readonly threadId: string;
41 readonly tabId: string;
42 }
43
44 export interface DesktopBrowserTabDebugger {
45 readonly webContents: Electron.WebContents;
46 readonly debugger: Electron.Debugger;
47 /** Runs after the server clears the tab's cookies, so state kept beside them goes too. */
48 readonly cookiesCleared?: () => Promise<void>;
49 }
50
51 const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`;
52
53 interface AttachedTab {
54 readonly key: DesktopBrowserTabKey;
55 readonly debuggee: DesktopBrowserTabDebugger;
56 relay: CdpRelayConnection | null;
57 screenshotTurn: object | null;
58 /** Each keeps the guest painting while whoever queued it still waits, and focused while `held`. */
59 readonly waitingScreenshots: Array<{
60 readonly start: () => void;
61 readonly live: () => boolean;
62 held: boolean;
63 }>;
64 downloadDirectory: string | null;
65 pendingDownloadGuid: string | null;
66 agentInputAt: number;
67 /** When the agent last drove the page with no person's input through a viewer since. */
68 agentLedAt: number;
69 focusEmulated: boolean;
70 focusChange: Promise<void> | null;
71 focusUses: number;
72 /** Each pending command, by the extra session waiting on it; the main connection's have none. */
73 readonly focusPending: Map<object, string | undefined>;
74 /** Screenshots Chromium was sent and has not answered; each keeps its wake lock until then. */
75 readonly capturing: Set<object>;
76 /** Whether the client keeps the guest painting, as published on `painting`. */
77 painting: boolean;
78 /**
79 * Driven by the agent since its thread's last turn ended or the guest last throttled. A
80 * turn's start can reach us after its first command, so this holds focus once it does.
81 */
82 agentUsed: boolean;
83 focusReleasing: boolean;
84 /** The sessions sharing the guest's one Chromium screencast; it stops with the last of them. */
85 readonly screencasts: Map<string | undefined, { started: boolean; pending: number }>;
86 readonly onMessage: (
87 event: Electron.Event,
88 method: string,
89 params: unknown,
90 sessionId: string,
91 ) => void;
92 readonly onNewDocument: () => void;
93 /** Stops waiting for the first page; set until the tab is announced. */
94 stopWaiting: (() => void) | null;
95 }
96
97 export class DesktopBrowserHost extends Context.Service<
98 DesktopBrowserHost,
99 {
100 readonly events: Stream.Stream<Uint8Array>;
101 readonly handleCommandLine: (line: string) => Effect.Effect<void>;
102 readonly attach: (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => void;
103 readonly detach: (key: DesktopBrowserTabKey) => void;
104 readonly loadFailed: (key: DesktopBrowserTabKey, failure: PreviewAutomationLoadError) => void;
105 readonly setShown: (key: DesktopBrowserTabKey, shown: boolean | undefined) => void;
106 /** Whether the client draws the guest at full detail while it is captured. */
107 readonly setCaptureDrawn: (key: DesktopBrowserTabKey, drawn: boolean | undefined) => void;
108 readonly humanStartedDownload: (source: Electron.WebContents) => boolean;
109 /** Whether what the page does now follows the agent's input, not a viewer's. */
110 readonly agentLedPage: (source: Electron.WebContents) => boolean;
111 readonly placeDownload: (source: Electron.WebContents, item: Electron.DownloadItem) => boolean;
112 readonly pointers: Stream.Stream<{
113 readonly key: DesktopBrowserTabKey;
114 readonly phase: "move" | "click";
115 readonly x: number;
116 readonly y: number;
117 }>;
118 /** Whether a guest needs its frames painted while the panel hides it. */
119 readonly painting: Stream.Stream<{
120 readonly key: DesktopBrowserTabKey;
121 readonly active: boolean;
122 }>;
123 }
124 >()("@t3tools/desktop/preview/DesktopBrowserHost") {}
125
126 export const make = Effect.gen(function* () {
127 const outbox = yield* PubSub.unbounded<DesktopBrowserEventType>();
128 const pointers = yield* PubSub.sliding<{
129 readonly key: DesktopBrowserTabKey;
130 readonly phase: "move" | "click";
131 readonly x: number;
132 readonly y: number;
133 }>(16);
134 const painting = yield* PubSub.unbounded<{
135 readonly key: DesktopBrowserTabKey;
136 readonly active: boolean;
137 }>();
138 const runFork = Effect.runForkWith(yield* Effect.context<never>());
139 const tabs = new Map<string, AttachedTab>();
140 /** Threads whose agent turn runs, by the server's account. */
141 const agentTurns = new Set<string>();
142 // Kept apart from attachment: the renderer can show a webview before its debugger attaches,
143 // and the server forgets `shown` on detach, for example while DevTools holds the debugger.
144 const shownTabs = new Map<string, boolean>();
145 const drawnTabs = new Set<string>();
146 const drawWaiters = new Map<string, Set<() => void>>();
147 const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event));
148
149 const nextScreenshot = (tab: AttachedTab) => {
150 tab.screenshotTurn = null;
151 tab.waitingScreenshots.shift()?.start();
152 releaseFocusWhenIdle(tab);
153 syncPainting(tab);
154 };
155
156 /**
157 * Chromium below 157 crashes the whole app when a guest's renderer exits with a
158 * screenshot copy in flight (crbug.com/567321049). Overlapping screenshots make that
159 * far likelier, and one the server gave up on stays pending in Chromium, so each guest
160 * gets one at a time. One whose relay or session has since gone is dropped unsent.
161 */
162 const takeScreenshot = (
163 tab: AttachedTab,
164 relay: CdpRelayConnection,
165 ownerLive: () => boolean,
166 /** Sends only while `current` holds; holds focus only while `held`. */
167 capture: (current: () => boolean, held: boolean) => Promise<unknown>,
168 ) =>
169 new Promise<unknown>((resolve, reject) => {
170 const live = () => tab.relay === relay && ownerLive();
171 const waiting = {
172 start: () => {
173 if (!live()) {
174 reject(new Error("The preview connection that asked for this screenshot closed."));
175 nextScreenshot(tab);
176 return;
177 }
178 const turn = {};
179 tab.screenshotTurn = turn;
180 capture(() => tab.screenshotTurn === turn && live(), waiting.held)
181 .finally(() => {
182 if (tab.screenshotTurn === turn) nextScreenshot(tab);
183 })
184 .then(resolve, reject);
185 },
186 live,
187 held: true,
188 };
189 if (tab.screenshotTurn === null) waiting.start();
190 else {
191 // The one ahead may be abandoned mid-capture; it needs frames to finish and free its turn.
192 tab.waitingScreenshots.push(waiting);
193 setFocusEmulation(tab, true);
194 syncPainting(tab);
195 }
196 });
197
198 /**
199 * Chromium counts a focus-emulated guest as captured, so a guest the panel hides would
200 * never throttle. Without it, a hidden guest paints no frames and focus() fires no focus
201 * events, so the guest is emulated focused while the server drives it or streams it.
202 */
203 const setFocusEmulation = (tab: AttachedTab, enabled: boolean) => {
204 if (tab.focusEmulated === enabled) return;
205 tab.focusEmulated = enabled;
206 // A guest that refuses it, or a debugger already gone, behaves as unfocused.
207 const change: Promise<void> = tab.debuggee.debugger
208 .sendCommand("Emulation.setFocusEmulationEnabled", { enabled })
209 .then(
210 () => undefined,
211 () => undefined,
212 )
213 .finally(() => {
214 if (tab.focusChange === change) tab.focusChange = null;
215 });
216 tab.focusChange = change;
217 };
218
219 const waitingHolds = (tab: AttachedTab) =>
220 tab.waitingScreenshots.some((waiting) => waiting.held && waiting.live());
221
222 const releaseFocusWhenIdle = (tab: AttachedTab) => {
223 if (tab.focusReleasing) return;
224 tab.focusReleasing = true;
225 runFork(
226 Effect.gen(function* () {
227 let uses: number;
228 do {
229 uses = tab.focusUses;
230 yield* Effect.sleep(FOCUS_IDLE);
231 } while (uses !== tab.focusUses);
232 tab.focusReleasing = false;
233 // The last pending command to settle starts another idle wait.
234 if (tab.focusPending.size > 0 || tab.screencasts.size > 0 || waitingHolds(tab)) {
235 return;
236 }
237 // The agent pauses to think between steps; its tab stays focused until the turn ends.
238 if (tab.agentUsed && agentTurns.has(tab.key.threadId)) return;
239 tab.agentUsed = false;
240 if (tabs.get(keyOf(tab.key)) === tab) setFocusEmulation(tab, false);
241 }),
242 );
243 };
244
245 /**
246 * Electron composites no offscreen guest, so its screenshot or screencast waits for a frame
247 * that never comes. The client paints the guest behind the app while one is pending; the
248 * capture then finishes on its own, and the guest goes back offscreen once none is.
249 */
250 const syncPainting = (tab: AttachedTab) => {
251 const active =
252 tabs.get(keyOf(tab.key)) === tab &&
253 (tab.screencasts.size > 0 ||
254 tab.capturing.size > 0 ||
255 tab.waitingScreenshots.some((waiting) => waiting.live()));
256 if (tab.painting === active) return;
257 tab.painting = active;
258 runFork(PubSub.publish(painting, { key: tab.key, active }));
259 };
260
261 /** A command may wait on the page's next frame, so the guest stays focused until it settles. */
262 const holdFocus = <A>(tab: AttachedTab, owner: string | undefined, work: () => Promise<A>) => {
263 const pending = {};
264 tab.focusPending.set(pending, owner);
265 tab.focusUses += 1;
266 setFocusEmulation(tab, true);
267 releaseFocusWhenIdle(tab);
268 return work().finally(() => {
269 if (!tab.focusPending.delete(pending)) return;
270 tab.focusUses += 1;
271 releaseFocusWhenIdle(tab);
272 });
273 };
274
275 /**
276 * A scaled guest rasters at its on-screen size until the client draws it at full detail,
277 * and a screenshot taken before that copies the blurred frame. A client that cannot draw
278 * (a hidden window, say) only delays the capture.
279 */
280 const whenDrawn = (tab: AttachedTab) =>
281 new Promise<void>((resolve) => {
282 const id = keyOf(tab.key);
283 const waiters = drawWaiters.get(id) ?? new Set();
284 drawWaiters.set(id, waiters);
285 const done = () => {
286 waiters.delete(done);
287 if (waiters.size === 0 && drawWaiters.get(id) === waiters) drawWaiters.delete(id);
288 resolve();
289 };
290 waiters.add(done);
291 runFork(Effect.sleep(CAPTURE_DRAW_WAIT).pipe(Effect.andThen(Effect.sync(done))));
292 });
293
294 /** Chromium answers a capture, and frees its wake lock, only once the guest paints a frame. */
295 const paintUntilAnswered = <A>(tab: AttachedTab, send: () => Promise<A>) => {
296 const sent = {};
297 tab.capturing.add(sent);
298 syncPainting(tab);
299 // Sent at once when already drawn, so it keeps its place among the commands after it.
300 return (drawnTabs.has(keyOf(tab.key)) ? send() : whenDrawn(tab).then(send)).finally(() => {
301 tab.capturing.delete(sent);
302 syncPainting(tab);
303 });
304 };
305
306 /**
307 * Chromium can leave a command unanswered, so the screenshot guard's release points
308 * also end every focus hold: a released relay has no one waiting, and a new document
309 * answers nothing pending from the old one. A released relay's screencast has no viewer.
310 */
311 const dropFocusHolds = (tab: AttachedTab) => {
312 tab.focusPending.clear();
313 releaseFocusWhenIdle(tab);
314 syncPainting(tab);
315 };
316
317 const dropRelay = (tab: AttachedTab) => {
318 tab.relay = null;
319 tab.screencasts.clear();
320 tab.agentUsed = false;
321 dropFocusHolds(tab);
322 };
323
324 /** An owner that leaves without stopping would otherwise keep the screencast running unseen. */
325 const leaveScreencast = (tab: AttachedTab, owner: string | undefined) => {
326 if (!tab.screencasts.delete(owner) || tab.screencasts.size > 0) return;
327 void tab.debuggee.debugger.sendCommand("Page.stopScreencast").catch(() => {});
328 };
329
330 /** A detached session, such as a closed viewer, waits on nothing it sent. */
331 const dropSessionHolds = (tab: AttachedTab, owner: string) => {
332 leaveScreencast(tab, owner);
333 for (const [pending, holder] of tab.focusPending) {
334 if (holder === owner) tab.focusPending.delete(pending);
335 }
336 releaseFocusWhenIdle(tab);
337 syncPainting(tab);
338 };
339
340 /**
341 * A command the server stopped waiting on, such as a timed-out screenshot or evaluation,
342 * may never settle; the turn's end ends every focus hold. The agent also sends on its own extra
343 * session, so this cannot spare other sessions; a viewer's screencast still holds.
344 */
345 const endAgentTurn = (threadId: string) => {
346 agentTurns.delete(threadId);
347 for (const tab of tabs.values()) {
348 if (tab.key.threadId !== threadId) continue;
349 tab.agentUsed = false;
350 // Still sent in turn, so the queue keeps moving, but no longer kept focused.
351 for (const waiting of tab.waitingScreenshots) waiting.held = false;
352 dropFocusHolds(tab);
353 }
354 };
355
356 const relayFor = (tab: AttachedTab) => {
357 if (tab.relay) return tab.relay;
358 const { webContents, debugger: debuggee, cookiesCleared } = tab.debuggee;
359 const send = async (method: string, params: Record<string, unknown>, sessionId?: string) => {
360 const result = await sendPageCommand(webContents, debuggee, method, params, sessionId);
361 if (method === "Network.clearBrowserCookies") await cookiesCleared?.();
362 return result;
363 };
364 const detachedOwners = new Set<string>();
365 /** Viewers and recordings stream; the agent's own sessions never do. */
366 const streamingOwners = new Set<string>();
367 const relay: CdpRelayConnection = createCdpRelayConnection(
368 {
369 send: (method, params, sessionId, owner) => {
370 // A child session's screencast is not the page's, such as a late one on a detached session.
371 const stream =
372 sessionId === undefined && method === "Page.startScreencast"
373 ? (tab.screencasts.get(owner) ?? { started: false, pending: 0 })
374 : undefined;
375 if (stream) {
376 stream.pending += 1;
377 tab.screencasts.set(owner, stream);
378 if (owner !== undefined) streamingOwners.add(owner);
379 syncPainting(tab);
380 } else if (sessionId === undefined && method === "Page.stopScreencast") {
381 tab.screencasts.delete(owner);
382 syncPainting(tab);
383 if (tab.screencasts.size > 0) return Promise.resolve({});
384 }
385 // A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
386 if (AGENT_INPUT_METHOD.test(method)) {
387 if (owner === undefined || !streamingOwners.has(owner)) {
388 tab.agentUsed = true;
389 tab.agentLedAt = performance.now();
390 } else if (method.startsWith("Input.") && !POINTER_TRAVEL.has(params["type"])) {
391 tab.agentLedAt = Number.NEGATIVE_INFINITY;
392 }
393 }
394 // Waits for focus emulation; insertText and other non-CDP input would not queue behind it.
395 const forward = (current?: () => boolean) => {
396 const change = tab.focusChange;
397 const stale = () => tab.relay !== relay || (current !== undefined && !current());
398 const sendUnlessStale = () =>
399 stale()
400 ? Promise.reject(new Error("The page this screenshot was for has moved on."))
401 : send(method, params, sessionId);
402 const run = () =>
403 current === undefined || stale()
404 ? sendUnlessStale()
405 : paintUntilAnswered(tab, sendUnlessStale);
406 return change === null ? run() : change.then(run);
407 };
408 return method === "Page.captureScreenshot"
409 ? takeScreenshot(
410 tab,
411 relay,
412 () => owner === undefined || !detachedOwners.has(owner),
413 (current, held) =>
414 held ? holdFocus(tab, owner, () => forward(current)) : forward(current),
415 )
416 : method === "Page.startScreencast"
417 ? holdFocus(tab, owner, forward).then(
418 (result) => {
419 if (stream) {
420 stream.pending -= 1;
421 stream.started = true;
422 // A detach's stop can overtake a start still waiting on focus; nobody owns it now.
423 if (tab.screencasts.get(owner) !== stream && tab.screencasts.size === 0) {
424 void debuggee.sendCommand("Page.stopScreencast").catch(() => {});
425 }
426 }
427 return result;
428 },
429 (cause: unknown) => {
430 if (stream) stream.pending -= 1;
431 // A viewer can outlive a refused start; its running or pending stream still holds it.
432 if (
433 stream &&
434 !stream.started &&
435 stream.pending === 0 &&
436 tab.relay === relay &&
437 tab.screencasts.get(owner) === stream
438 ) {
439 leaveScreencast(tab, owner);
440 }
441 releaseFocusWhenIdle(tab);
442 syncPainting(tab);
443 throw cause;
444 },
445 )
446 : holdFocus(tab, owner, forward);
447 },
448 sessionDetached: (owner) => {
449 detachedOwners.add(owner);
450 dropSessionHolds(tab, owner);
451 },
452 receivesScreencast: (owner) => tab.screencasts.has(owner),
453 ackScreencastFrame: (frameId) =>
454 void debuggee
455 .sendCommand("Page.screencastFrameAck", { sessionId: frameId })
456 .catch(() => {}),
457 targetId: () =>
458 debuggee
459 .sendCommand("Target.getTargetInfo")
460 .then((result: { targetInfo: { targetId: string } }) => result.targetInfo.targetId),
461 url: () => webContents.getURL(),
462 title: () => webContents.getTitle(),
463 userAgent: () => webContents.getUserAgent(),
464 setDownloadDirectory: (directory) => {
465 tab.downloadDirectory = directory;
466 },
467 },
468 (message) => {
469 if (tab.relay === relay && tabs.get(keyOf(tab.key)) === tab) {
470 emit({ type: "cdp", ...tab.key, message });
471 }
472 },
473 // Announcing the tab again would reconnect Playwright, and the page could
474 // replay the same oversized event, so the tab stays detached.
475 () => {
476 if (tab.relay !== relay || tabs.get(keyOf(tab.key)) !== tab) return;
477 // Disabling auto-attach detaches child sessions and releases their debugger pauses.
478 void debuggee
479 .sendCommand("Target.setAutoAttach", {
480 autoAttach: false,
481 waitForDebuggerOnStart: false,
482 flatten: true,
483 })
484 .catch(() => {});
485 if (tab.screencasts.size > 0) {
486 void debuggee.sendCommand("Page.stopScreencast").catch(() => {});
487 }
488 setFocusEmulation(tab, false);
489 detach(tab.key);
490 },
491 );
492 tab.relay = relay;
493 return relay;
494 };
495
496 // CDP supplies the path before this runs; otherwise Electron opens a Save dialog.
497 const placeDownload = (source: Electron.WebContents, item: Electron.DownloadItem) => {
498 const tab = [...tabs.values()].find(
499 (candidate) => candidate.debuggee.webContents === source && candidate.downloadDirectory,
500 );
501 if (!tab?.downloadDirectory || !tab.pendingDownloadGuid) return false;
502 item.setSavePath(NodePath.join(tab.downloadDirectory, tab.pendingDownloadGuid));
503 tab.pendingDownloadGuid = null;
504 return true;
505 };
506
507 const detach = (key: DesktopBrowserTabKey) => {
508 const id = keyOf(key);
509 const tab = tabs.get(id);
510 if (!tab) return;
511 tabs.delete(id);
512 const announced = tab.stopWaiting === null;
513 tab.stopWaiting?.();
514 tab.relay = null;
515 tab.debuggee.debugger.off("message", tab.onMessage);
516 tab.debuggee.webContents.off("did-navigate", tab.onNewDocument);
517 syncPainting(tab);
518 if (announced) emit({ type: "detached", ...key });
519 };
520
521 const attach = (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => {
522 const id = keyOf(key);
523 if (tabs.get(id)?.debuggee.webContents === debuggee.webContents) return;
524 detach(key);
525 const tab: AttachedTab = {
526 key,
527 debuggee,
528 relay: null,
529 screenshotTurn: null,
530 waitingScreenshots: [],
531 downloadDirectory: null,
532 pendingDownloadGuid: null,
533 agentInputAt: Number.NEGATIVE_INFINITY,
534 agentLedAt: Number.NEGATIVE_INFINITY,
535 focusEmulated: false,
536 focusChange: null,
537 focusUses: 0,
538 focusPending: new Map(),
539 capturing: new Set(),
540 painting: false,
541 agentUsed: false,
542 focusReleasing: false,
543 screencasts: new Map(),
544 onMessage: (_event, method, params, sessionId) => {
545 if (method === "Page.downloadWillBegin") {
546 const guid = (params as { guid?: unknown } | undefined)?.guid;
547 tab.pendingDownloadGuid = typeof guid === "string" ? guid : null;
548 }
549 tab.relay?.event(method, params, sessionId);
550 },
551 // A screenshot pending when the page loads a new document, a reload included, can
552 // stay unanswered. A crashed page lands here once it reloads, well after its renderer died.
553 onNewDocument: () => {
554 // Before the next screenshot starts, so its own holds survive.
555 tab.capturing.clear();
556 dropFocusHolds(tab);
557 if (tab.screenshotTurn !== null) nextScreenshot(tab);
558 },
559 stopWaiting: null,
560 };
561 tabs.set(id, tab);
562 debuggee.debugger.on("message", tab.onMessage);
563 debuggee.webContents.on("did-navigate", tab.onNewDocument);
564 const announce = () => {
565 emit({ type: "attached", ...key });
566 const shown = shownTabs.get(id);
567 if (shown !== undefined) emit({ type: "shown", ...key, shown });
568 };
569 if (!debuggee.webContents.isLoadingMainFrame()) {
570 announce();
571 return;
572 }
573 // Electron does not pause a guest for the debugger, so Playwright connecting during its first
574 // commit builds its utility world in the discarded document and every wait on it hangs.
575 const { webContents } = debuggee;
576 // ERR_ABORTED is a newer navigation replacing this one, whose commit is still to come.
577 const onFailLoad = (
578 _event: Electron.Event,
579 code: number,
580 _description: string,
581 _url: string,
582 isMainFrame: boolean,
583 ) => {
584 if (isMainFrame && code !== ERR_ABORTED) ready();
585 };
586 const ready = () => {
587 if (tab.stopWaiting === null) return;
588 // The manager's cleanup for a destroyed guest can still be queued behind another tab's attach.
589 if (webContents.isDestroyed()) return detach(key);
590 tab.stopWaiting();
591 announce();
592 };
593 const timeout = runFork(
594 Effect.sleep(FIRST_PAGE_TIMEOUT).pipe(Effect.andThen(Effect.sync(ready))),
595 );
596 tab.stopWaiting = () => {
597 tab.stopWaiting = null;
598 webContents.off("dom-ready", ready);
599 webContents.off("did-stop-loading", ready);
600 webContents.off("did-fail-load", onFailLoad);
601 runFork(Fiber.interrupt(timeout));
602 };
603 webContents.on("dom-ready", ready);
604 webContents.on("did-stop-loading", ready);
605 webContents.on("did-fail-load", onFailLoad);
606 };
607
608 const setShown = (key: DesktopBrowserTabKey, shown: boolean | undefined) => {
609 const id = keyOf(key);
610 if (shown === undefined) {
611 shownTabs.delete(id);
612 return;
613 }
614 if (shownTabs.get(id) === shown) return;
615 shownTabs.set(id, shown);
616 if (tabs.get(id)?.stopWaiting === null) emit({ type: "shown", ...key, shown });
617 };
618
619 const setCaptureDrawn = (key: DesktopBrowserTabKey, drawn: boolean | undefined) => {
620 const id = keyOf(key);
621 if (!drawn) {
622 drawnTabs.delete(id);
623 return;
624 }
625 drawnTabs.add(id);
626 for (const done of drawWaiters.get(id) ?? []) done();
627 drawWaiters.delete(id);
628 };
629
630 const loadFailed: DesktopBrowserHost["Service"]["loadFailed"] = (key, failure) => {
631 if (tabs.has(keyOf(key))) emit({ type: "loadFailed", ...key, ...failure });
632 };
633
634 const handleCommandLine = (line: string) =>
635 Effect.sync(() => {
636 const command = decodeCommand(line);
637 if (Option.isNone(command)) return;
638 if (command.value.type === "turn") {
639 if (command.value.active) agentTurns.add(command.value.threadId);
640 else endAgentTurn(command.value.threadId);
641 return;
642 }
643 const tab = tabs.get(keyOf(command.value));
644 if (!tab) return;
645 if (command.value.type === "pointer") {
646 const { threadId, tabId, phase, x, y } = command.value;
647 runFork(PubSub.publish(pointers, { key: { threadId, tabId }, phase, x, y }));
648 return;
649 }
650 if (command.value.type === "release") {
651 dropRelay(tab);
652 return;
653 }
654 if (AGENT_INPUT_COMMAND.test(command.value.message)) tab.agentInputAt = performance.now();
655 relayFor(tab).receive(command.value.message);
656 });
657
658 const humanStartedDownload = (source: Electron.WebContents) => {
659 const tab = [...tabs.values()].find((candidate) => candidate.debuggee.webContents === source);
660 return tab !== undefined && performance.now() - tab.agentInputAt > AGENT_INPUT_WINDOW_MS;
661 };
662
663 const agentLedPage = (source: Electron.WebContents) => {
664 const tab = [...tabs.values()].find((candidate) => candidate.debuggee.webContents === source);
665 return tab !== undefined && performance.now() - tab.agentLedAt <= AGENT_INPUT_WINDOW_MS;
666 };
667
668 const announceAll = Effect.suspend(() => {
669 // A new backend: the old one's turns ended without telling us.
670 agentTurns.clear();
671 return Effect.forEach(
672 [...tabs.values()],
673 (tab) => {
674 dropRelay(tab);
675 // Announced once its first page is up.
676 if (tab.stopWaiting !== null) return Effect.void;
677 const shown = shownTabs.get(keyOf(tab.key));
678 return PubSub.publish(outbox, { type: "attached", ...tab.key }).pipe(
679 Effect.andThen(
680 shown !== undefined
681 ? PubSub.publish(outbox, { type: "shown", ...tab.key, shown })
682 : Effect.void,
683 ),
684 );
685 },
686 { discard: true },
687 );
688 });
689
690 return DesktopBrowserHost.of({
691 pointers: Stream.fromPubSub(pointers),
692 painting: Stream.fromPubSub(painting),
693 // Subscribes before announcing, so no attach falls between the two.
694 events: Stream.unwrap(
695 Effect.gen(function* () {
696 const subscription = yield* PubSub.subscribe(outbox);
697 yield* announceAll;
698 return Stream.fromSubscription(subscription);
699 }),
700 ).pipe(Stream.map((event) => lineEncoder.encode(`${encodeEvent(event)}\n`))),
701 handleCommandLine,
702 attach,
703 detach,
704 loadFailed,
705 setShown,
706 setCaptureDrawn,
707 placeDownload,
708 humanStartedDownload,
709 agentLedPage,
710 });
711 });
712
713 export const layer = Layer.effect(DesktopBrowserHost, make);