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 Context from "effect/Context"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import type * as Rpc from "effect/rpc/Rpc"; |
| 5 | import * as RpcClient from "effect/rpc/RpcClient"; |
| 6 | import type { RpcClientError } from "effect/rpc/RpcClientError"; |
| 7 | import type * as RpcGroup from "effect/rpc/RpcGroup"; |
| 8 | import * as RpcSerialization from "effect/rpc/RpcSerialization"; |
| 9 | import * as Socket from "effect/socket/Socket"; |
| 10 | |
| 11 | export interface LayerOptions { |
| 12 | /** Options passed to Effect's WebSocket transport. */ |
| 13 | readonly socket?: Parameters<typeof Socket.layerWebSocket>[1]; |
| 14 | /** Options passed to Effect's socket RPC protocol. */ |
| 15 | readonly protocol?: Parameters<typeof RpcClient.layerProtocolSocket>[0]; |
| 16 | /** Options passed to the grouped Effect RPC client. */ |
| 17 | readonly client?: Omit<NonNullable<Parameters<typeof RpcClient.make>[1]>, "flatten">; |
| 18 | /** |
| 19 | * Serialization matching the server. Alchemy's RpcDurableObject uses JSON. |
| 20 | * @default RpcSerialization.json |
| 21 | */ |
| 22 | readonly serialization?: RpcSerialization.RpcSerialization["Service"]; |
| 23 | } |
| 24 | |
| 25 | type ProtocolOptions = NonNullable<LayerOptions["protocol"]>; |
| 26 | |
| 27 | /** |
| 28 | * Effect's pinger drops the connection when no frame arrived within |
| 29 | * `pingTimeout` of a ping tick, and `pingTimeout` defaults to the interval: |
| 30 | * a Pong that returns faster than the timer's own drift (any local server) |
| 31 | * then misses the deadline by a few milliseconds. Allow two intervals. |
| 32 | */ |
| 33 | const withPingTimeout = (protocol: LayerOptions["protocol"]): ProtocolOptions => ({ |
| 34 | ...protocol, |
| 35 | pingTimeout: |
| 36 | protocol?.pingTimeout ?? |
| 37 | Duration.times(Duration.fromInputUnsafe(protocol?.pingInterval ?? "5 seconds"), 2), |
| 38 | }); |
| 39 | |
| 40 | /** |
| 41 | * Provide a typed RPC client and its WebSocket connection for the Layer's lifetime. |
| 42 | * |
| 43 | * Import from `@alchemy.run/cloudflare/Workers/RpcWebSocketClient` in browsers to avoid |
| 44 | * Cloudflare server modules. Provide `Socket.layerWebSocketConstructorGlobal` |
| 45 | * in a browser, or the corresponding constructor Layer for another platform. |
| 46 | * RPC client middleware remains an explicit dependency. |
| 47 | * |
| 48 | * Provide this Layer around the application or session that shares the client. |
| 49 | * Releasing the Layer closes the connection on success, failure, or interruption. |
| 50 | * Ordinary calls need no caller Scope; streaming calls using `{ asQueue: true }` |
| 51 | * still require a Scope for the queue consumer. Inside a Cloudflare Worker, |
| 52 | * provide this Layer per request, not in the isolate-scoped initializer. |
| 53 | * |
| 54 | * ### Browser Client |
| 55 | * **Example:** Provide a client for the application's program |
| 56 | * ```typescript |
| 57 | * import * as RpcWebSocketClient from "@alchemy.run/cloudflare/Workers/RpcWebSocketClient"; |
| 58 | * import { Context, Effect, Layer } from "effect"; |
| 59 | * import type * as RpcClient from "effect/rpc/RpcClient"; |
| 60 | * import type { RpcClientError } from "effect/rpc/RpcClientError"; |
| 61 | * import * as Socket from "effect/socket/Socket"; |
| 62 | * import { CounterRpcs } from "./rpcs.ts"; |
| 63 | * |
| 64 | * class CounterClient extends Context.Service< |
| 65 | * CounterClient, |
| 66 | * RpcClient.FromGroup<typeof CounterRpcs, RpcClientError> |
| 67 | * >()("CounterClient") {} |
| 68 | * |
| 69 | * const ClientLive = RpcWebSocketClient.layer( |
| 70 | * CounterClient, |
| 71 | * CounterRpcs, |
| 72 | * "wss://example.com/counters/alice", |
| 73 | * ).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal)); |
| 74 | * |
| 75 | * const program = Effect.gen(function* () { |
| 76 | * const counter = yield* CounterClient; |
| 77 | * return yield* counter.increment(); |
| 78 | * }).pipe(Effect.provide(ClientLive)); |
| 79 | * ``` |
| 80 | * |
| 81 | * @layer |
| 82 | * @provides service |
| 83 | * @product Workers |
| 84 | * @category Workers & Compute |
| 85 | */ |
| 86 | export const layer = <I, Rpcs extends Rpc.Any>( |
| 87 | service: Context.Key<I, RpcClient.RpcClient<Rpcs, RpcClientError>>, |
| 88 | group: RpcGroup.RpcGroup<Rpcs>, |
| 89 | url: Parameters<typeof Socket.layerWebSocket>[0], |
| 90 | options: LayerOptions = {}, |
| 91 | ): Layer.Layer<I, never, Socket.WebSocketConstructor | Rpc.MiddlewareClient<Rpcs>> => |
| 92 | Layer.effect(service, RpcClient.make(group, options.client)).pipe( |
| 93 | Layer.provide( |
| 94 | RpcClient.layerProtocolSocket(withPingTimeout(options.protocol)).pipe( |
| 95 | Layer.provide( |
| 96 | Layer.mergeAll( |
| 97 | Socket.layerWebSocket(url, options.socket), |
| 98 | Layer.succeed( |
| 99 | RpcSerialization.RpcSerialization, |
| 100 | options.serialization ?? RpcSerialization.json, |
| 101 | ), |
| 102 | ), |
| 103 | ), |
| 104 | ), |
| 105 | ), |
| 106 | ); |