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 | /** |
| 2 | * Process bootstrap for `Fly.Service` and `Fly.Sprite` (a Node process |
| 3 | * serving the bundled program). The generated entry imports this module |
| 4 | * and the user's `main`, nothing else — see `alchemy/Runtime/Bootstrap` for why. |
| 5 | */ |
| 6 | import { NodeServices } from "@effect/platform-node"; |
| 7 | import { NodeHttpServer } from "alchemy/Http"; |
| 8 | import { reifyBoundConfigProvider } from "alchemy/Runtime"; |
| 9 | import { |
| 10 | entrypointLayer, |
| 11 | resolveProgram, |
| 12 | runProcess, |
| 13 | stackFromEnv, |
| 14 | } from "alchemy/Runtime/Bootstrap"; |
| 15 | import { ManagedHttpShutdown } from "alchemy/Runtime/ManagedHttpShutdown"; |
| 16 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Logger from "effect/Logger"; |
| 21 | import * as Option from "effect/Option"; |
| 22 | import * as Scope from "effect/Scope"; |
| 23 | |
| 24 | /** |
| 25 | * Resolve the bundled program (the runners registered via `host.run` / |
| 26 | * serve) and run it with a Node HTTP server bound to `PORT`, so the |
| 27 | * returned `{ fetch }` handler is actually served and `host.run` loops |
| 28 | * stay alive. |
| 29 | */ |
| 30 | export const bootstrap = (entrypoint: unknown): Promise<void> => { |
| 31 | const platform = Layer.mergeAll( |
| 32 | NodeServices.layer, |
| 33 | FetchHttpClient.layer, |
| 34 | Logger.layer([Logger.consolePretty()]), |
| 35 | ); |
| 36 | |
| 37 | const services = entrypointLayer(entrypoint).pipe( |
| 38 | Layer.provideMerge(stackFromEnv), |
| 39 | Layer.provideMerge(NodeHttpServer({ hostname: "0.0.0.0" })), |
| 40 | Layer.provideMerge(platform), |
| 41 | Layer.provideMerge( |
| 42 | Layer.succeed( |
| 43 | ConfigProvider.ConfigProvider, |
| 44 | reifyBoundConfigProvider(ConfigProvider.fromEnv(), process.env), |
| 45 | ), |
| 46 | ), |
| 47 | ); |
| 48 | const program = Effect.gen(function* () { |
| 49 | const managed = yield* Effect.serviceOption(ManagedHttpShutdown); |
| 50 | if (Option.isNone(managed)) { |
| 51 | return yield* resolveProgram("program").pipe(Effect.provide(services), Effect.scoped); |
| 52 | } |
| 53 | // Initializer dependencies outlive both run finalizers and HTTP responses. |
| 54 | const context = yield* Layer.buildWithScope(services, managed.value.dependencies); |
| 55 | return yield* resolveProgram("program").pipe( |
| 56 | Effect.provideContext(context), |
| 57 | Scope.provide(managed.value.dependencies), |
| 58 | ); |
| 59 | }); |
| 60 | |
| 61 | const timeout = process.env.ALCHEMY_FLY_SHUTDOWN_TIMEOUT_MS; |
| 62 | return runProcess("Fly service", program, { |
| 63 | managedHttpShutdownTimeoutMs: |
| 64 | timeout === undefined ? undefined : /^\d+$/.test(timeout) ? Number(timeout) : NaN, |
| 65 | }); |
| 66 | }; |