The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Process bootstrap for `GCP.Run.Service`. The generated entry imports |
| 3 | * this module and the user's `main`, nothing else — see |
| 4 | * `alchemy/Runtime/Bootstrap` for why. |
| 5 | * |
| 6 | * Runtime credentials come from the Cloud Run metadata server (the |
| 7 | * service's runtime service account), so HTTP bindings call GCP with |
| 8 | * exactly the IAM the deploy granted — never the deploy-time token. |
| 9 | */ |
| 10 | import { BunServices } from "@effect/platform-bun"; |
| 11 | import { BunHttpServer } from "alchemy/Http"; |
| 12 | import { reifyBoundConfigProvider } from "alchemy/Runtime"; |
| 13 | import { |
| 14 | entrypointLayer, |
| 15 | resolveProgram, |
| 16 | runProcess, |
| 17 | stackFromEnv, |
| 18 | } from "alchemy/Runtime/Bootstrap"; |
| 19 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 20 | import * as Effect from "effect/Effect"; |
| 21 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 22 | import * as Layer from "effect/Layer"; |
| 23 | import * as Logger from "effect/Logger"; |
| 24 | import { fromMetadataServer } from "../MetadataCredentials.ts"; |
| 25 | |
| 26 | /** Serve the bundled program with a Bun HTTP server on the injected `PORT`. */ |
| 27 | export const bootstrap = (entrypoint: unknown): Promise<void> => { |
| 28 | // Cloud Run sets `PORT` only on the ingress container; a sidecar |
| 29 | // `GCP.Run.Container` gets its port from `ALCHEMY_HTTP_PORT`. |
| 30 | if (process.env.ALCHEMY_HTTP_PORT && !process.env.PORT) { |
| 31 | process.env.PORT = process.env.ALCHEMY_HTTP_PORT; |
| 32 | } |
| 33 | const platform = Layer.mergeAll( |
| 34 | BunServices.layer, |
| 35 | FetchHttpClient.layer, |
| 36 | Logger.layer([Logger.consolePretty()]), |
| 37 | ); |
| 38 | |
| 39 | const program = resolveProgram("program", { telemetry: true }).pipe( |
| 40 | Effect.provide( |
| 41 | entrypointLayer(entrypoint).pipe( |
| 42 | Layer.provideMerge(stackFromEnv), |
| 43 | Layer.provideMerge(fromMetadataServer()), |
| 44 | Layer.provideMerge(BunHttpServer({ hostname: "0.0.0.0" })), |
| 45 | Layer.provideMerge(platform), |
| 46 | Layer.provideMerge( |
| 47 | Layer.succeed( |
| 48 | ConfigProvider.ConfigProvider, |
| 49 | reifyBoundConfigProvider(ConfigProvider.fromEnv(), process.env), |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | ), |
| 54 | Effect.scoped, |
| 55 | ); |
| 56 | |
| 57 | return runProcess("Cloud Run service", program); |
| 58 | }; |