The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { HttpEffect } from "alchemy/Http"; |
| 2 | import * as Http from "alchemy/Http"; |
| 3 | import { serveRpc } from "alchemy/Rpc"; |
| 4 | import { createHostRuntimeContext, type HostRuntimeContext } from "alchemy/Runtime/Process"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | |
| 9 | /** |
| 10 | * Claims an inbound HTTP request for an event source (a Pub/Sub push |
| 11 | * delivery, a Cloud Scheduler call, …). Return `undefined` to pass the |
| 12 | * request on to the next listener and finally the user's `fetch`. |
| 13 | */ |
| 14 | export type GcpHttpListener = ( |
| 15 | request: HttpServerRequest, |
| 16 | ) => Effect.Effect<HttpServerResponse.HttpServerResponse, never, any> | undefined; |
| 17 | |
| 18 | /** |
| 19 | * Runtime context of an HTTP-serving GCP host (`GCP.Run.Service`, |
| 20 | * `GCP.CloudFunctions.Function`). Extends the process host context with |
| 21 | * `listen`, the counterpart of `host.listen` on a Cloudflare Worker or an |
| 22 | * AWS Lambda: event sources register listeners that see every request |
| 23 | * before the user's `fetch` handler. |
| 24 | */ |
| 25 | export interface GcpHostRuntimeContext extends HostRuntimeContext { |
| 26 | listen: (listener: GcpHttpListener) => Effect.Effect<void>; |
| 27 | } |
| 28 | |
| 29 | export const isGcpListenHost = ( |
| 30 | value: unknown, |
| 31 | ): value is GcpHostRuntimeContext & { LogicalId: string; Type: string } => |
| 32 | typeof value === "object" && |
| 33 | value !== null && |
| 34 | typeof (value as { listen?: unknown }).listen === "function" && |
| 35 | typeof (value as { serve?: unknown }).serve === "function"; |
| 36 | |
| 37 | const notFound = Effect.succeed(HttpServerResponse.text("Not Found", { status: 404 })); |
| 38 | |
| 39 | /** Env var a host's impl sets when it returns RPC methods. */ |
| 40 | export const RPC_METHODS_ENV = "ALCHEMY_RPC_METHODS"; |
| 41 | |
| 42 | /** Methods on an impl shape that are served as RPC. */ |
| 43 | export const rpcMethodsOf = (shape: Record<string, unknown> | undefined) => |
| 44 | Object.fromEntries( |
| 45 | Object.entries(shape ?? {}).filter( |
| 46 | ([key, value]) => key !== "fetch" && key !== "run" && typeof value === "function", |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | export const createGcpHostRuntimeContext = |
| 51 | ( |
| 52 | type: string, |
| 53 | options?: { |
| 54 | /** |
| 55 | * Serve the impl's methods on `/__rpc__/*`. `true` for hosts no |
| 56 | * request reaches from outside the instance (sidecar containers). |
| 57 | * `"private"` also records {@link RPC_METHODS_ENV} in the host's env |
| 58 | * so its provider can refuse to deploy the methods on a public host. |
| 59 | */ |
| 60 | rpc?: boolean | "private"; |
| 61 | }, |
| 62 | ) => |
| 63 | (id: string): GcpHostRuntimeContext => { |
| 64 | const base = createHostRuntimeContext(type)(id); |
| 65 | const listeners: GcpHttpListener[] = []; |
| 66 | let served = false; |
| 67 | |
| 68 | // Listeners are read per request, so it does not matter whether an |
| 69 | // event source registers before or after the user's `fetch` is served. |
| 70 | const route = <Req>( |
| 71 | handler: HttpEffect<Req> | Effect.Effect<HttpEffect<Req>>, |
| 72 | ): HttpEffect<Req> => |
| 73 | Effect.gen(function* () { |
| 74 | const request = yield* HttpServerRequest; |
| 75 | for (const listener of listeners) { |
| 76 | const claimed = listener(request); |
| 77 | if (claimed !== undefined) return yield* claimed; |
| 78 | } |
| 79 | return yield* Http.safeHttpEffect(handler); |
| 80 | }) as HttpEffect<Req>; |
| 81 | |
| 82 | return { |
| 83 | ...base, |
| 84 | listen: (listener) => |
| 85 | Effect.sync(() => { |
| 86 | listeners.push(listener); |
| 87 | }), |
| 88 | serve: ((handler, serveOptions) => { |
| 89 | served = true; |
| 90 | const routed = route(handler as HttpEffect<any>); |
| 91 | const methods = options?.rpc ? rpcMethodsOf(serveOptions?.shape) : {}; |
| 92 | if (options?.rpc === "private" && Object.keys(methods).length > 0) { |
| 93 | base.env[RPC_METHODS_ENV] = "1"; |
| 94 | } |
| 95 | return base.serve( |
| 96 | Object.keys(methods).length > 0 ? serveRpc(methods, routed) : routed, |
| 97 | serveOptions, |
| 98 | ); |
| 99 | }) as HostRuntimeContext["serve"], |
| 100 | // A host whose only job is consuming events has no `fetch`, so the |
| 101 | // Platform never calls `serve`; start the server for the listeners. |
| 102 | exports: Effect.gen(function* () { |
| 103 | if (!served && listeners.length > 0) { |
| 104 | served = true; |
| 105 | yield* base.serve(route(notFound)); |
| 106 | } |
| 107 | return yield* base.exports; |
| 108 | }), |
| 109 | }; |
| 110 | }; |