@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, 04:45 UTC

main
ts 290 lines 11.8 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - Bridges Playwright's WebSocket transport to the desktop's fds.
2 /** Playwright needs a CDP WebSocket; the secret loopback endpoint relays desktop debugger frames over bootstrap fds. */
3 import * as NodeStream from "@effect/platform-node/NodeStream";
4 import * as NodeSocketServer from "@effect/platform-node/NodeSocketServer";
5 import {
6 DesktopBrowserCommand,
7 DesktopBrowserEvent,
8 type DesktopBrowserCommand as DesktopBrowserCommandType,
9 type PreviewAutomationLoadError,
10 } from "@t3tools/contracts";
11 import * as Context from "effect/Context";
12 import * as Duration from "effect/Duration";
13 import * as Effect from "effect/Effect";
14 import * as Layer from "effect/Layer";
15 import * as Option from "effect/Option";
16 import * as PubSub from "effect/PubSub";
17 import * as Queue from "effect/Queue";
18 import * as Schema from "effect/Schema";
19 import * as Scope from "effect/Scope";
20 import * as Semaphore from "effect/Semaphore";
21 import * as Stream from "effect/Stream";
22 import * as Ndjson from "effect/encoding/Ndjson";
23 import * as NodeCrypto from "node:crypto";
24 import * as NodeNet from "node:net";
25
26 import * as ServerConfig from "../config.ts";
27 import { writeAllToFileDescriptor } from "../resourceTelemetry/DesktopTelemetryReceiver.ts";
28
29 const decodeEvent = Schema.decodeUnknownEffect(DesktopBrowserEvent);
30 const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(DesktopBrowserCommand));
31
32 export interface DesktopTabKey {
33 readonly threadId: string;
34 readonly tabId: string;
35 }
36
37 const keyOf = ({ threadId, tabId }: DesktopTabKey) => `${threadId}\u0000${tabId}`;
38
39 export class DesktopBrowserChannel extends Context.Service<
40 DesktopBrowserChannel,
41 {
42 readonly available: boolean;
43 /** Subscribe before checking so concurrent attaches cannot be missed. */
44 readonly awaitAttached: (key: DesktopTabKey, timeout: Duration.Input) => Effect.Effect<boolean>;
45 readonly detached: Stream.Stream<DesktopTabKey>;
46 readonly attached: Stream.Stream<DesktopTabKey>;
47 readonly isAttached: (key: DesktopTabKey) => Effect.Effect<boolean>;
48 /** Whether a desktop window draws the tab on screen, which no streamed viewer counts. */
49 readonly isShown: (key: DesktopTabKey) => boolean;
50 /** Resolves true once the tab is shown, or false on timeout. */
51 readonly awaitShown: (key: DesktopTabKey, timeout: Duration.Input) => Effect.Effect<boolean>;
52 /** A load failure the desktop saw, possibly before the server connected to the tab. */
53 readonly loadFailure: (key: DesktopTabKey) => PreviewAutomationLoadError | undefined;
54 readonly endpoint: (key: DesktopTabKey) => Effect.Effect<string, never, Scope.Scope>;
55 readonly pointer: (
56 key: DesktopTabKey,
57 pointer: { readonly phase: "move" | "click"; readonly x: number; readonly y: number },
58 ) => Effect.Effect<void>;
59 /** Never fails. A failed write means a broken channel; the next backend starts with no turns. */
60 readonly turn: (threadId: string, active: boolean) => Effect.Effect<void>;
61 }
62 >()("t3/preview/DesktopBrowserChannel") {}
63
64 const make = Effect.gen(function* () {
65 const config = yield* ServerConfig.ServerConfig;
66 const inputFd = config.desktopBrowserFd;
67 const controlFd = config.desktopBrowserControlFd;
68 const changes = yield* PubSub.unbounded<{ key: DesktopTabKey; attached: boolean }>();
69 const attachedTabs = new Set<string>();
70 // Missing reports protect desktop tabs until the renderer explicitly marks them hidden.
71 const shownTabs = new Map<string, boolean>();
72 const shownReports = yield* PubSub.unbounded<string>();
73 const loadFailures = new Map<string, PreviewAutomationLoadError>();
74 const inbound = new Map<string, Queue.Queue<string>>();
75 const writeLock = yield* Semaphore.make(1);
76
77 if (inputFd === undefined || controlFd === undefined) {
78 return DesktopBrowserChannel.of({
79 available: false,
80 awaitAttached: Effect.fn("DesktopBrowserChannel.awaitAttached")(function () {
81 return Effect.succeed(false);
82 }),
83 detached: Stream.empty,
84 attached: Stream.empty,
85 isAttached: Effect.fn("DesktopBrowserChannel.isAttached")(function () {
86 return Effect.succeed(false);
87 }),
88 endpoint: Effect.fn("DesktopBrowserChannel.endpoint")(function () {
89 return Effect.die("No desktop app is attached to this server.");
90 }),
91 turn: Effect.fn("DesktopBrowserChannel.turn")(function () {
92 return Effect.void;
93 }),
94 pointer: Effect.fn("DesktopBrowserChannel.pointer")(function () {
95 return Effect.void;
96 }),
97 isShown: () => false,
98 awaitShown: Effect.fn("DesktopBrowserChannel.awaitShown")(function () {
99 return Effect.succeed(false);
100 }),
101 loadFailure: () => undefined,
102 });
103 }
104
105 const command = (message: DesktopBrowserCommandType) =>
106 writeLock.withPermits(1)(
107 encodeCommand(message).pipe(
108 Effect.flatMap((line) => writeAllToFileDescriptor(controlFd, Buffer.from(`${line}\n`))),
109 Effect.catchCause((cause) =>
110 Effect.logWarning("desktop browser command failed", { cause }),
111 ),
112 ),
113 );
114
115 // Use cancellable socket reads; filesystem pipe reads keep Node alive after scope shutdown.
116 const readable = yield* Effect.acquireRelease(
117 Effect.sync(() => new NodeNet.Socket({ fd: inputFd, readable: true, writable: false })),
118 (stream) => Effect.sync(() => stream.destroy()),
119 );
120 yield* NodeStream.fromReadable<Uint8Array, Error>({
121 evaluate: () => readable,
122 closeOnDone: true,
123 onError: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
124 }).pipe(
125 Stream.pipeThroughChannel(Ndjson.decode({ ignoreEmptyLines: true })),
126 Stream.mapEffect((value) => decodeEvent(value).pipe(Effect.option)),
127 Stream.runForEach((decoded) => {
128 if (Option.isNone(decoded)) return Effect.void;
129 const event = decoded.value;
130 const key = { threadId: event.threadId, tabId: event.tabId };
131 const id = keyOf(key);
132 switch (event.type) {
133 case "cdp": {
134 const queue = inbound.get(id);
135 return queue ? Queue.offer(queue, event.message).pipe(Effect.asVoid) : Effect.void;
136 }
137 case "attached":
138 attachedTabs.add(id);
139 return PubSub.publish(changes, { key, attached: true });
140 case "loadFailed":
141 loadFailures.set(id, {
142 url: event.url.slice(0, 2048),
143 code: event.code,
144 description: event.description,
145 });
146 return Effect.void;
147 case "detached": {
148 attachedTabs.delete(id);
149 shownTabs.delete(id);
150 loadFailures.delete(id);
151 const queue = inbound.get(id);
152 return (queue ? Queue.shutdown(queue) : Effect.void).pipe(
153 Effect.andThen(PubSub.publish(changes, { key, attached: false })),
154 );
155 }
156 case "shown":
157 shownTabs.set(id, event.shown);
158 return event.shown ? PubSub.publish(shownReports, id) : Effect.void;
159 }
160 }),
161 Effect.catchCause((cause) => Effect.logWarning("desktop browser channel stopped", { cause })),
162 Effect.ensuring(
163 Effect.sync(() => {
164 shownTabs.clear();
165 loadFailures.clear();
166 }),
167 ),
168 Effect.forkScoped,
169 );
170
171 const endpoint = Effect.fn("DesktopBrowserChannel.endpoint")(function* (key: DesktopTabKey) {
172 const id = keyOf(key);
173 const secret = NodeCrypto.randomBytes(24).toString("base64url");
174 const server = yield* NodeSocketServer.makeWebSocket({
175 host: "127.0.0.1",
176 port: 0,
177 path: `/${secret}`,
178 }).pipe(Effect.orDie);
179 const queue = yield* Queue.unbounded<string>();
180 inbound.set(id, queue);
181 // A detach before this registration shut down no queue, so check again.
182 if (!attachedTabs.has(id)) {
183 inbound.delete(id);
184 return yield* Effect.die("The desktop tab detached before the server connected.");
185 }
186 yield* Effect.addFinalizer(() =>
187 Effect.gen(function* () {
188 if (inbound.get(id) === queue) inbound.delete(id);
189 yield* Queue.shutdown(queue);
190 yield* command({ type: "release", ...key });
191 }),
192 );
193 // The relay serves one Playwright connection; a second would see the first's sessions.
194 let connected = false;
195 yield* server
196 .run((socket) =>
197 Effect.gen(function* () {
198 if (connected) return;
199 connected = true;
200 const writer = yield* socket.writer;
201 const reader = yield* socket.reader;
202 // A detach shuts the queue down. Ending here closes the socket, so Playwright's
203 // pending commands fail instead of waiting forever for the tab to answer.
204 const toPlaywright = Stream.fromQueue(queue).pipe(
205 Stream.runForEach((message) => writer.write(message)),
206 Effect.exit,
207 );
208 const decoder = new TextDecoder();
209 const toDesktop = reader.pull.pipe(
210 Effect.flatMap((frames) =>
211 Effect.forEach(
212 frames,
213 (frame) =>
214 command({
215 type: "cdp",
216 ...key,
217 message: typeof frame === "string" ? frame : decoder.decode(frame),
218 }),
219 { discard: true },
220 ),
221 ),
222 Effect.forever,
223 );
224 return yield* Effect.raceFirst(toPlaywright, toDesktop);
225 }).pipe(Effect.scoped, Effect.ignore),
226 )
227 .pipe(Effect.forkScoped);
228 const address = server.address;
229 if (address._tag !== "InetAddressV4") return yield* Effect.die("Unexpected relay address.");
230 return `ws://127.0.0.1:${address.port}/${secret}`;
231 });
232
233 const isShown = (key: DesktopTabKey) => shownTabs.get(keyOf(key)) ?? true;
234
235 return DesktopBrowserChannel.of({
236 available: true,
237 awaitAttached: Effect.fn("DesktopBrowserChannel.awaitAttached")(function (key, timeout) {
238 return Effect.scoped(
239 Effect.gen(function* () {
240 const subscription = yield* PubSub.subscribe(changes);
241 if (attachedTabs.has(keyOf(key))) return true;
242 return yield* Stream.fromSubscription(subscription).pipe(
243 Stream.filter((change) => change.attached && keyOf(change.key) === keyOf(key)),
244 Stream.runHead,
245 Effect.map(Option.isSome),
246 Effect.timeoutOption(timeout),
247 Effect.map((result) => Option.getOrElse(result, () => false)),
248 );
249 }),
250 );
251 }),
252 detached: Stream.fromPubSub(changes).pipe(
253 Stream.filter((change) => !change.attached),
254 Stream.map((change) => change.key),
255 ),
256 attached: Stream.fromPubSub(changes).pipe(
257 Stream.filter((change) => change.attached),
258 Stream.map((change) => change.key),
259 ),
260 isAttached: Effect.fn("DesktopBrowserChannel.isAttached")(function (key) {
261 return Effect.sync(() => attachedTabs.has(keyOf(key)));
262 }),
263 isShown,
264 awaitShown: Effect.fn("DesktopBrowserChannel.awaitShown")(function (key, timeout) {
265 return Effect.scoped(
266 Effect.gen(function* () {
267 const subscription = yield* PubSub.subscribe(shownReports);
268 if (isShown(key)) return true;
269 return yield* Stream.fromSubscription(subscription).pipe(
270 Stream.filter((id) => id === keyOf(key)),
271 Stream.runHead,
272 Effect.map(Option.isSome),
273 Effect.timeoutOption(timeout),
274 Effect.map((result) => Option.getOrElse(result, () => false)),
275 );
276 }),
277 );
278 }),
279 loadFailure: (key) => loadFailures.get(keyOf(key)),
280 endpoint,
281 turn: Effect.fn("DesktopBrowserChannel.turn")(function (threadId, active) {
282 return command({ type: "turn", threadId, active });
283 }),
284 pointer: Effect.fn("DesktopBrowserChannel.pointer")(function (key, pointer) {
285 return command({ type: "pointer", ...key, ...pointer });
286 }),
287 });
288 });
289
290 export const layer = Layer.effect(DesktopBrowserChannel, make);