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
| 1 | import type * as cf from "@cloudflare/workers-types"; |
| 2 | import type { HttpEffect } from "alchemy/Http"; |
| 3 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpBody from "effect/http/HttpBody"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Latch from "effect/Latch"; |
| 9 | import * as Queue from "effect/Queue"; |
| 10 | import * as RpcMessage from "effect/rpc/RpcMessage"; |
| 11 | import * as RpcSerialization from "effect/rpc/RpcSerialization"; |
| 12 | import * as RpcServer from "effect/rpc/RpcServer"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import { DurableObjectState } from "./DurableObjectState.ts"; |
| 15 | import { fromWebSocket, type WebSocket } from "./WebSocket.ts"; |
| 16 | |
| 17 | const socketTag = "alchemy:rpc"; |
| 18 | const attachmentKey = "__alchemyRpcWebSocket"; |
| 19 | const restartReason = "Durable Object RPC activation reset"; |
| 20 | const Metadata = Schema.Struct({ |
| 21 | version: Schema.Literal(1), |
| 22 | clientId: Schema.Number, |
| 23 | pending: Schema.Boolean, |
| 24 | serialization: Schema.String, |
| 25 | }); |
| 26 | const isAttachment = Schema.is(Schema.Struct({ [attachmentKey]: Metadata })); |
| 27 | |
| 28 | interface Connection { |
| 29 | readonly id: number; |
| 30 | readonly socket: WebSocket; |
| 31 | readonly parser: RpcSerialization.Parser; |
| 32 | readonly pending: Set<string | number>; |
| 33 | readonly idle: Latch.Latch; |
| 34 | } |
| 35 | |
| 36 | export interface Transport { |
| 37 | readonly protocol: RpcServer.Protocol["Service"]; |
| 38 | readonly fetch: HttpEffect; |
| 39 | readonly webSocketMessage: ( |
| 40 | socket: WebSocket, |
| 41 | message: string | ArrayBuffer, |
| 42 | ) => Effect.Effect<void>; |
| 43 | readonly webSocketClose: (socket: WebSocket) => Effect.Effect<void>; |
| 44 | readonly webSocketError: (socket: WebSocket, error: unknown) => Effect.Effect<void>; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Internal, per-activation transport. Provide `protocol` to `RpcServer.layer` |
| 49 | * in an instance-owned scope, not the first fetch or WebSocket event's scope. |
| 50 | * This transport exclusively owns the object's WebSocket auto-response pair. |
| 51 | * Binary serializers use waking Ping/Pong messages instead of auto-response. |
| 52 | * Serializers must emit each response immediately; buffered output fails closed. |
| 53 | */ |
| 54 | export const make: Effect.Effect< |
| 55 | Transport, |
| 56 | never, |
| 57 | DurableObjectState | RuntimeContext | RpcSerialization.RpcSerialization |
| 58 | > = Effect.gen(function* () { |
| 59 | const state = yield* DurableObjectState; |
| 60 | const runtime = yield* RuntimeContext; |
| 61 | const serialization = yield* RpcSerialization.RpcSerialization; |
| 62 | const native = globalThis as unknown as { |
| 63 | WebSocketPair: typeof cf.WebSocketPair; |
| 64 | WebSocketRequestResponsePair: typeof cf.WebSocketRequestResponsePair; |
| 65 | Response: typeof cf.Response; |
| 66 | }; |
| 67 | const inRuntime = <A, E>(effect: Effect.Effect<A, E, RuntimeContext>) => |
| 68 | Effect.provideService(effect, RuntimeContext, runtime); |
| 69 | const disconnects = yield* Queue.make<number>(); |
| 70 | const ready = yield* Latch.make(); |
| 71 | const bySocket = new Map<cf.WebSocket, Connection>(); |
| 72 | const byId = new Map<number, Connection>(); |
| 73 | const closed = new WeakSet<cf.WebSocket>(); |
| 74 | let nextId = 0; |
| 75 | let restoring = true; |
| 76 | let stopped = false; |
| 77 | let receive: Parameters<RpcServer.Protocol["Service"]["run"]>[0] | undefined; |
| 78 | |
| 79 | const heartbeat = yield* Effect.sync(() => { |
| 80 | const parser = serialization.makeUnsafe(); |
| 81 | const ping = parser.encode(RpcMessage.constPing); |
| 82 | const pong = parser.encode(RpcMessage.constPong); |
| 83 | return typeof ping === "string" && typeof pong === "string" |
| 84 | ? new native.WebSocketRequestResponsePair(ping, pong) |
| 85 | : undefined; |
| 86 | }); |
| 87 | const existing = yield* inRuntime(state.getWebSocketAutoResponse()); |
| 88 | if ( |
| 89 | existing !== null && |
| 90 | (heartbeat === undefined || |
| 91 | existing.request !== heartbeat.request || |
| 92 | existing.response !== heartbeat.response) |
| 93 | ) { |
| 94 | return yield* Effect.die( |
| 95 | new Error("RPC WebSockets require exclusive ownership of auto-response"), |
| 96 | ); |
| 97 | } |
| 98 | let heartbeatEnabled = existing !== null; |
| 99 | |
| 100 | const syncHeartbeat = Effect.suspend(() => { |
| 101 | const enabled = |
| 102 | !restoring && |
| 103 | !stopped && |
| 104 | heartbeat !== undefined && |
| 105 | byId.size > 0 && |
| 106 | !Array.from(byId.values()).some((connection) => connection.pending.size > 0); |
| 107 | if (enabled === heartbeatEnabled) return Effect.void; |
| 108 | return inRuntime(state.setWebSocketAutoResponse(enabled ? heartbeat : undefined)).pipe( |
| 109 | Effect.tap(() => |
| 110 | Effect.sync(() => { |
| 111 | heartbeatEnabled = enabled; |
| 112 | }), |
| 113 | ), |
| 114 | Effect.catchCause(() => inRuntime(state.abort("Unable to update RPC WebSocket heartbeat"))), |
| 115 | ); |
| 116 | }); |
| 117 | yield* syncHeartbeat; |
| 118 | |
| 119 | const persist = (connection: Connection) => |
| 120 | Effect.sync(() => { |
| 121 | const previous = connection.socket.deserializeAttachment<unknown>(); |
| 122 | if ( |
| 123 | previous !== null && |
| 124 | previous !== undefined && |
| 125 | (typeof previous !== "object" || Array.isArray(previous)) |
| 126 | ) { |
| 127 | throw new Error("Invalid RPC WebSocket attachment"); |
| 128 | } |
| 129 | connection.socket.serializeAttachment({ |
| 130 | ...previous, |
| 131 | [attachmentKey]: { |
| 132 | version: 1, |
| 133 | clientId: connection.id, |
| 134 | pending: connection.pending.size > 0, |
| 135 | serialization: serialization.contentType, |
| 136 | } satisfies typeof Metadata.Type, |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | const unregister = (socket: WebSocket) => |
| 141 | Effect.gen(function* () { |
| 142 | yield* Effect.sync(() => { |
| 143 | closed.add(socket.ws); |
| 144 | const connection = bySocket.get(socket.ws); |
| 145 | if (connection === undefined) return; |
| 146 | bySocket.delete(socket.ws); |
| 147 | byId.delete(connection.id); |
| 148 | connection.pending.clear(); |
| 149 | connection.idle.openUnsafe(); |
| 150 | Queue.offerUnsafe(disconnects, connection.id); |
| 151 | }); |
| 152 | yield* syncHeartbeat; |
| 153 | }); |
| 154 | |
| 155 | const close = (socket: WebSocket, code = 1012, reason = restartReason) => |
| 156 | socket.close(code, reason).pipe( |
| 157 | Effect.catchCause(() => inRuntime(state.abort("Unable to close RPC WebSocket"))), |
| 158 | Effect.ensuring(unregister(socket)), |
| 159 | Effect.uninterruptible, |
| 160 | ); |
| 161 | |
| 162 | const register = (socket: WebSocket, id: number): Connection => { |
| 163 | const connection: Connection = { |
| 164 | id, |
| 165 | socket, |
| 166 | parser: serialization.makeUnsafe(), |
| 167 | pending: new Set(), |
| 168 | idle: Latch.makeUnsafe(true), |
| 169 | }; |
| 170 | bySocket.set(socket.ws, connection); |
| 171 | byId.set(id, connection); |
| 172 | nextId = Math.max(nextId, id + 1); |
| 173 | return connection; |
| 174 | }; |
| 175 | |
| 176 | for (const socket of yield* inRuntime(state.getWebSockets(socketTag))) { |
| 177 | yield* Effect.gen(function* () { |
| 178 | const attachment = yield* Effect.sync(() => socket.deserializeAttachment<unknown>()); |
| 179 | if (!isAttachment(attachment)) return yield* close(socket); |
| 180 | const metadata = attachment[attachmentKey]; |
| 181 | if ( |
| 182 | metadata.pending || |
| 183 | metadata.serialization !== serialization.contentType || |
| 184 | !Number.isSafeInteger(metadata.clientId) || |
| 185 | metadata.clientId < 0 || |
| 186 | byId.has(metadata.clientId) |
| 187 | ) { |
| 188 | return yield* close(socket); |
| 189 | } |
| 190 | const connection = yield* Effect.sync(() => register(socket, metadata.clientId)); |
| 191 | yield* persist(connection); |
| 192 | }).pipe(Effect.catchCause(() => close(socket))); |
| 193 | } |
| 194 | restoring = false; |
| 195 | yield* syncHeartbeat; |
| 196 | |
| 197 | const send = (connection: Connection, response: RpcMessage.FromServerEncoded) => |
| 198 | Effect.gen(function* () { |
| 199 | if (!byId.has(connection.id)) return; |
| 200 | const encoded = yield* Effect.sync(() => connection.parser.encode(response)); |
| 201 | // Buffered responses cannot be recovered after hibernation. |
| 202 | if (encoded === undefined) return yield* close(connection.socket); |
| 203 | yield* connection.socket.send(encoded); |
| 204 | if (response._tag === "Exit") { |
| 205 | connection.pending.delete(response.requestId); |
| 206 | yield* persist(connection); |
| 207 | if (connection.pending.size === 0) yield* connection.idle.open; |
| 208 | yield* syncHeartbeat; |
| 209 | } |
| 210 | if (response._tag === "Defect" || response._tag === "ClientProtocolError") { |
| 211 | yield* close(connection.socket); |
| 212 | } |
| 213 | }).pipe( |
| 214 | Effect.catchCause(() => close(connection.socket)), |
| 215 | Effect.uninterruptible, |
| 216 | ); |
| 217 | |
| 218 | const protocol = RpcServer.Protocol.of({ |
| 219 | run: (write) => |
| 220 | Effect.acquireUseRelease( |
| 221 | Effect.sync(() => { |
| 222 | if (receive !== undefined || stopped) { |
| 223 | throw new Error("RPC WebSocket protocol is already running or stopped"); |
| 224 | } |
| 225 | receive = write; |
| 226 | }), |
| 227 | () => ready.open.pipe(Effect.andThen(Effect.never)), |
| 228 | () => |
| 229 | Effect.gen(function* () { |
| 230 | stopped = true; |
| 231 | receive = undefined; |
| 232 | yield* ready.open; |
| 233 | for (const connection of Array.from(byId.values())) { |
| 234 | yield* close(connection.socket); |
| 235 | } |
| 236 | yield* syncHeartbeat; |
| 237 | }), |
| 238 | ), |
| 239 | disconnects, |
| 240 | send: (id, response) => |
| 241 | Effect.suspend(() => { |
| 242 | const connection = byId.get(id); |
| 243 | return connection === undefined ? Effect.void : send(connection, response); |
| 244 | }), |
| 245 | end: (id) => |
| 246 | Effect.suspend(() => { |
| 247 | const connection = byId.get(id); |
| 248 | return connection === undefined ? Effect.void : close(connection.socket, 1000, ""); |
| 249 | }), |
| 250 | clientIds: Effect.sync(() => new Set(byId.keys())), |
| 251 | initialMessage: Effect.succeedNone, |
| 252 | supportsAck: true, |
| 253 | supportsTransferables: false, |
| 254 | supportsSpanPropagation: true, |
| 255 | supportsNotifications: true, |
| 256 | codecFor: serialization.codecFor, |
| 257 | }); |
| 258 | |
| 259 | const fetch: HttpEffect = Effect.gen(function* () { |
| 260 | const request = yield* HttpServerRequest; |
| 261 | if (request.method !== "GET" || request.headers.upgrade?.toLowerCase() !== "websocket") { |
| 262 | return HttpServerResponse.empty({ |
| 263 | status: 426, |
| 264 | headers: { Upgrade: "websocket" }, |
| 265 | }); |
| 266 | } |
| 267 | yield* ready.await; |
| 268 | if (stopped) return HttpServerResponse.empty({ status: 503 }); |
| 269 | const pair = yield* Effect.sync(() => new native.WebSocketPair()); |
| 270 | const socket = fromWebSocket(pair[1]); |
| 271 | let accepted = false; |
| 272 | return yield* Effect.gen(function* () { |
| 273 | yield* inRuntime(state.acceptWebSocket(socket, [socketTag])); |
| 274 | accepted = true; |
| 275 | const connection = yield* Effect.sync(() => register(socket, nextId)); |
| 276 | yield* persist(connection); |
| 277 | yield* syncHeartbeat; |
| 278 | return yield* Effect.sync(() => |
| 279 | HttpServerResponse.setBody( |
| 280 | HttpServerResponse.empty({ status: 101 }), |
| 281 | HttpBody.raw(new native.Response(null, { status: 101, webSocket: pair[0] })), |
| 282 | ), |
| 283 | ); |
| 284 | }).pipe( |
| 285 | Effect.catchCause(() => |
| 286 | (accepted ? close(socket) : Effect.void).pipe( |
| 287 | Effect.as(HttpServerResponse.empty({ status: 503 })), |
| 288 | ), |
| 289 | ), |
| 290 | Effect.uninterruptible, |
| 291 | ); |
| 292 | }); |
| 293 | |
| 294 | const webSocketMessage = (socket: WebSocket, message: string | ArrayBuffer) => |
| 295 | Effect.gen(function* () { |
| 296 | if (closed.has(socket.ws)) return; |
| 297 | yield* ready.await; |
| 298 | const connection = bySocket.get(socket.ws); |
| 299 | if (connection === undefined || receive === undefined) return yield* close(socket); |
| 300 | const messages = yield* Effect.sync(() => |
| 301 | connection.parser.decode(typeof message === "string" ? message : new Uint8Array(message)), |
| 302 | ); |
| 303 | // The serializer owns the envelopes; RpcServer validates payloads and schemas. |
| 304 | const requests = messages as ReadonlyArray<RpcMessage.FromClientEncoded>; |
| 305 | let hasRequests = false; |
| 306 | for (const request of requests) { |
| 307 | if (request._tag !== "Request") continue; |
| 308 | if ( |
| 309 | (typeof request.id !== "string" && |
| 310 | !(typeof request.id === "number" && Number.isFinite(request.id))) || |
| 311 | connection.pending.has(request.id) |
| 312 | ) { |
| 313 | return yield* close(socket); |
| 314 | } |
| 315 | connection.pending.add(request.id); |
| 316 | hasRequests = true; |
| 317 | } |
| 318 | if (hasRequests) { |
| 319 | yield* connection.idle.close; |
| 320 | // Mark the entire batch before dispatching even its first request. |
| 321 | yield* persist(connection); |
| 322 | yield* syncHeartbeat; |
| 323 | yield* inRuntime(state.waitUntil(connection.idle.await)); |
| 324 | } |
| 325 | for (const request of requests) { |
| 326 | if (!byId.has(connection.id)) return; |
| 327 | // Direct dispatch preserves this event's context; RpcServer creates each RPC scope. |
| 328 | yield* receive(connection.id, request); |
| 329 | } |
| 330 | }).pipe(Effect.catchCause(() => close(socket))); |
| 331 | |
| 332 | return { |
| 333 | protocol, |
| 334 | fetch, |
| 335 | webSocketMessage, |
| 336 | webSocketClose: (socket) => close(socket, 1000, ""), |
| 337 | webSocketError: (socket) => close(socket), |
| 338 | } satisfies Transport; |
| 339 | }); |