The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type * as HttpApi from "effect/http-api/HttpApi"; |
| 3 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 4 | import * as HttpApiClient from "effect/http-api/HttpApiClient"; |
| 5 | import type * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 6 | import * as Etag from "effect/http/Etag"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpPlatform from "effect/http/HttpPlatform"; |
| 9 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 10 | import type * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import { |
| 13 | type Rpc, |
| 14 | RpcClient, |
| 15 | type RpcGroup, |
| 16 | RpcSchema, |
| 17 | RpcSerialization, |
| 18 | RpcServer, |
| 19 | } from "effect/rpc"; |
| 20 | import type * as RpcClientError from "effect/rpc/RpcClientError"; |
| 21 | import * as Scope from "effect/Scope"; |
| 22 | import * as Semaphore from "effect/Semaphore"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | |
| 25 | /** |
| 26 | * Serve an Effect RPC group as a Cloud Run service's or container's |
| 27 | * `fetch`. Uses NDJSON, the serialization the `schema` clients of |
| 28 | * {@link bindContainer} and {@link InvokeService} speak, so value and |
| 29 | * streaming procedures both work. |
| 30 | * |
| 31 | * ### Serving Effect RPC |
| 32 | * **Example:** A container that serves an RpcGroup |
| 33 | * ```typescript |
| 34 | * export default class Tasks extends GCP.Run.Container<Tasks>()( |
| 35 | * "Tasks", |
| 36 | * { main: import.meta.url, port: 3000 }, |
| 37 | * Effect.gen(function* () { |
| 38 | * return { |
| 39 | * fetch: GCP.Run.ServeRpc(TaskRpcs, TaskRpcs.toLayer({ |
| 40 | * getTask: ({ id }) => Effect.succeed(new Task({ id, title: "write docs" })), |
| 41 | * })), |
| 42 | * }; |
| 43 | * }), |
| 44 | * ) {} |
| 45 | * ``` |
| 46 | * |
| 47 | * @primitive |
| 48 | * @category Run |
| 49 | */ |
| 50 | /** |
| 51 | * Build `server` once, on the first request, in a scope that lives as long |
| 52 | * as the process (a Cloud Run instance), and hand every request to it. |
| 53 | */ |
| 54 | const servedOnce = <E, R, RIn>( |
| 55 | server: Effect.Effect< |
| 56 | Effect.Effect<HttpServerResponse.HttpServerResponse, E, RIn>, |
| 57 | never, |
| 58 | R | Scope.Scope |
| 59 | >, |
| 60 | ): Effect.Effect<HttpServerResponse.HttpServerResponse, E, RIn | Exclude<R, Scope.Scope>> => { |
| 61 | let built: Effect.Effect<HttpServerResponse.HttpServerResponse, E, RIn> | undefined; |
| 62 | const lock = Semaphore.makeUnsafe(1); |
| 63 | const build = lock.withPermits(1)( |
| 64 | Effect.suspend(() => |
| 65 | built !== undefined |
| 66 | ? Effect.succeed(built) |
| 67 | : server.pipe( |
| 68 | Scope.provide(Scope.makeUnsafe()), |
| 69 | Effect.tap((handler) => Effect.sync(() => (built = handler))), |
| 70 | ), |
| 71 | ), |
| 72 | ); |
| 73 | return Effect.flatMap( |
| 74 | Effect.suspend(() => (built !== undefined ? Effect.succeed(built) : build)), |
| 75 | (handler) => handler, |
| 76 | ); |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * Serve an Effect RPC group as a Cloud Run service's or container's |
| 81 | * `fetch`. Uses NDJSON, the serialization the `schema` clients of |
| 82 | * {@link bindContainer} and {@link InvokeService} speak, so value and |
| 83 | * streaming procedures both work. |
| 84 | * |
| 85 | * ### Serving Effect RPC |
| 86 | * **Example:** A container that serves an RpcGroup |
| 87 | * ```typescript |
| 88 | * export default class Tasks extends GCP.Run.Container<Tasks>()( |
| 89 | * "Tasks", |
| 90 | * { main: import.meta.url, port: 3000 }, |
| 91 | * Effect.gen(function* () { |
| 92 | * return { |
| 93 | * fetch: GCP.Run.ServeRpc(TaskRpcs, TaskRpcs.toLayer({ |
| 94 | * getTask: ({ id }) => Effect.succeed(new Task({ id, title: "write docs" })), |
| 95 | * })), |
| 96 | * }; |
| 97 | * }), |
| 98 | * ) {} |
| 99 | * ``` |
| 100 | * |
| 101 | * @primitive |
| 102 | * @category Run |
| 103 | */ |
| 104 | export const ServeRpc = <Rpcs extends Rpc.Any, R>( |
| 105 | group: RpcGroup.RpcGroup<Rpcs>, |
| 106 | handlers: Layer.Layer<Rpc.ToHandler<Rpcs>, never, R>, |
| 107 | ) => |
| 108 | servedOnce( |
| 109 | RpcServer.toHttpEffect(group).pipe( |
| 110 | Effect.provide(Layer.mergeAll(handlers, RpcSerialization.layerNdjson)), |
| 111 | ), |
| 112 | ); |
| 113 | |
| 114 | /** |
| 115 | * Serve an Effect HttpApi as a Cloud Run service's or container's |
| 116 | * `fetch`: the api plus the Layers of its implemented groups. |
| 117 | * |
| 118 | * ### Serving an HttpApi |
| 119 | * **Example:** A service that serves an HttpApi |
| 120 | * ```typescript |
| 121 | * export default class TaskHttp extends GCP.Run.Service<TaskHttp>()( |
| 122 | * "TaskHttp", |
| 123 | * { main: import.meta.url }, |
| 124 | * Effect.gen(function* () { |
| 125 | * const tasks = HttpApiBuilder.group(TaskApi, "Tasks", (handlers) => |
| 126 | * handlers.handle("getTask", ({ params }) => |
| 127 | * Effect.succeed(new Task({ id: params.id, title: "write docs" })), |
| 128 | * ), |
| 129 | * ); |
| 130 | * return { fetch: GCP.Run.ServeHttpApi(TaskApi, tasks) }; |
| 131 | * }), |
| 132 | * ) {} |
| 133 | * ``` |
| 134 | * |
| 135 | * @primitive |
| 136 | * @category Run |
| 137 | */ |
| 138 | export const ServeHttpApi = <Id extends string, Groups extends HttpApiGroup.Constraint, R>( |
| 139 | api: HttpApi.HttpApi<Id, Groups>, |
| 140 | groups: Layer.Layer<HttpApiGroup.ToService<Id, Groups>, never, R>, |
| 141 | ) => |
| 142 | api.pipe( |
| 143 | HttpApiBuilder.layer, |
| 144 | Layer.provide(groups), |
| 145 | Layer.provide(Layer.mergeAll(HttpPlatform.layer, Etag.layer)), |
| 146 | HttpRouter.toHttpEffect, |
| 147 | servedOnce, |
| 148 | ); |
| 149 | |
| 150 | /** |
| 151 | * A typed Effect RPC client over `http`. Each call builds its own |
| 152 | * `RpcClient` in a scope that lives as long as the call (or the stream, |
| 153 | * for streaming procedures), so no I/O outlives a request. |
| 154 | */ |
| 155 | export const makeRpcGroupClient = <Rpcs extends Rpc.Any>( |
| 156 | group: RpcGroup.RpcGroup<Rpcs>, |
| 157 | http: HttpClient.HttpClient, |
| 158 | ): RpcClient.RpcClient<Rpcs, RpcClientError.RpcClientError> => { |
| 159 | const protocol = // `http` already resolves relative URLs against the target; an empty |
| 160 | // base keeps the protocol from prefixing them again. |
| 161 | RpcClient.layerProtocolHttp({ url: "" }).pipe( |
| 162 | Layer.provide(RpcSerialization.layerNdjson), |
| 163 | Layer.provide(Layer.succeed(HttpClient.HttpClient, http)), |
| 164 | ); |
| 165 | const client = RpcClient.make(group).pipe(Effect.provide(protocol)); |
| 166 | const call = (method: string, args: unknown[]) => |
| 167 | Effect.map(client, (built) => |
| 168 | (built as unknown as Record<string, (...a: unknown[]) => unknown>)[method]!(...args), |
| 169 | ); |
| 170 | return new Proxy( |
| 171 | {}, |
| 172 | { |
| 173 | get: (_target, method) => { |
| 174 | if (typeof method !== "string") return undefined; |
| 175 | const rpc = group.requests.get(method) as Rpc.AnyWithProps | undefined; |
| 176 | if (rpc === undefined) return undefined; |
| 177 | return RpcSchema.isStreamSchema(rpc.successSchema) |
| 178 | ? (...args: unknown[]) => |
| 179 | Stream.unwrap( |
| 180 | call(method, args) as Effect.Effect< |
| 181 | Stream.Stream<unknown, unknown>, |
| 182 | never, |
| 183 | Scope.Scope |
| 184 | >, |
| 185 | ) |
| 186 | : (...args: unknown[]) => |
| 187 | Effect.scoped( |
| 188 | Effect.flatMap( |
| 189 | call(method, args) as Effect.Effect< |
| 190 | Effect.Effect<unknown, unknown>, |
| 191 | never, |
| 192 | Scope.Scope |
| 193 | >, |
| 194 | (effect) => effect, |
| 195 | ), |
| 196 | ); |
| 197 | }, |
| 198 | }, |
| 199 | ) as RpcClient.RpcClient<Rpcs, RpcClientError.RpcClientError>; |
| 200 | }; |
| 201 | |
| 202 | /** A typed Effect HttpApi client whose requests go through `http`. */ |
| 203 | export const makeHttpApiClient = <Id extends string, Groups extends HttpApiGroup.Constraint, E>( |
| 204 | api: HttpApi.HttpApi<Id, Groups>, |
| 205 | http: HttpClient.HttpClient.With<E>, |
| 206 | ) => |
| 207 | HttpApiClient.make(api).pipe( |
| 208 | Effect.provideService(HttpClient.HttpClient, http as unknown as HttpClient.HttpClient), |
| 209 | ) as unknown as Effect.Effect<HttpApiClient.Client<Groups, E>>; |