@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 106 lines 4 KB
Raw
1 /**
2 * Bootstrap for an Effect `AWS.Lambda.WebFunction` (zip, Node runtime). The
3 * generated entry imports this module and the user's `main` — see
4 * `alchemy/Runtime/Bootstrap` for why the entry is a shim.
5 *
6 * Unlike an Event Function, a Web Function is a plain HTTP server: the
7 * bundled program's `fetch` handler is served by Effect's Node HTTP server
8 * on the address Lambda passes in `AWS_LAMBDA_HTTP_ENDPOINT`. One execution
9 * environment serves many requests concurrently, and response bodies stream
10 * straight to the client.
11 */
12 import * as Credentials from "@distilled.cloud/aws/Credentials";
13 import * as Endpoint from "@distilled.cloud/aws/Endpoint";
14 import * as Region from "@distilled.cloud/aws/Region";
15 import { NodeServices } from "@effect/platform-node";
16 import { NodeHttpServer } from "alchemy/Http";
17 import { reifyBoundConfigProvider } from "alchemy/Runtime";
18 import {
19 entrypointLayer,
20 resolveProgram,
21 runProcess,
22 stackFromEnv,
23 } from "alchemy/Runtime/Bootstrap";
24 import { ManagedHttpShutdown } from "alchemy/Runtime/ManagedHttpShutdown";
25 import * as ConfigProvider from "effect/ConfigProvider";
26 import * as Effect from "effect/Effect";
27 import * as FetchHttpClient from "effect/http/FetchHttpClient";
28 import * as Layer from "effect/Layer";
29 import * as Logger from "effect/Logger";
30 import * as Option from "effect/Option";
31 import { MinimumLogLevel } from "effect/References";
32 import * as Scope from "effect/Scope";
33
34 /**
35 * The `host:port` Lambda routes requests to, or `127.0.0.1:3000` when
36 * `AWS_LAMBDA_HTTP_ENDPOINT` is unset (local runs).
37 */
38 const lambdaHttpEndpoint = () => {
39 const value = process.env.AWS_LAMBDA_HTTP_ENDPOINT;
40 const separator = value?.lastIndexOf(":") ?? -1;
41 if (value === undefined || separator <= 0) {
42 return { host: "127.0.0.1", port: 3000 };
43 }
44 return {
45 host: value.slice(0, separator).replace(/^\[(.*)\]$/, "$1"),
46 port: Number(value.slice(separator + 1)),
47 };
48 };
49
50 /**
51 * Lambda sends `SIGTERM` and terminates the environment 1 second later.
52 * Managed shutdown stops accepting, drains in-flight responses, closes the
53 * instance scope, and exits inside that window.
54 */
55 const SHUTDOWN_BUDGET_MS = 1000;
56
57 /**
58 * Resolve the bundled program (the `fetch` handler registered via `serve`)
59 * and run it with a Node HTTP server bound to `AWS_LAMBDA_HTTP_ENDPOINT`.
60 */
61 export const bootstrap = (entrypoint: unknown): Promise<void> => {
62 const { host, port } = lambdaHttpEndpoint();
63
64 const platform = Layer.mergeAll(
65 NodeServices.layer,
66 FetchHttpClient.layer,
67 Logger.layer([Logger.consolePretty()]),
68 );
69
70 const services = entrypointLayer(entrypoint).pipe(
71 Layer.provideMerge(stackFromEnv),
72 Layer.provideMerge(Credentials.fromEnv()),
73 Layer.provideMerge(Region.fromEnv()),
74 Layer.provideMerge(Endpoint.fromEnv()),
75 Layer.provideMerge(NodeHttpServer({ hostname: host, port })),
76 Layer.provideMerge(platform),
77 Layer.provideMerge(
78 Layer.succeed(
79 ConfigProvider.ConfigProvider,
80 // Auto-bound `Config` values arrive in the env as Redacted markers;
81 // reify them so a `Config` re-read inside a handler decodes the raw
82 // source value.
83 reifyBoundConfigProvider(ConfigProvider.fromEnv(), process.env),
84 ),
85 ),
86 Layer.provideMerge(Layer.succeed(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info")),
87 );
88
89 const program = Effect.gen(function* () {
90 const managed = yield* Effect.serviceOption(ManagedHttpShutdown);
91 if (Option.isNone(managed)) {
92 return yield* resolveProgram("program").pipe(Effect.provide(services), Effect.scoped);
93 }
94 // Instance services outlive both HTTP responses and run finalizers;
95 // they close last, inside the SIGTERM window.
96 const context = yield* Layer.buildWithScope(services, managed.value.dependencies);
97 return yield* resolveProgram("program").pipe(
98 Effect.provideContext(context),
99 Scope.provide(managed.value.dependencies),
100 );
101 });
102
103 return runProcess("Lambda web function", program, {
104 managedHttpShutdownTimeoutMs: SHUTDOWN_BUDGET_MS,
105 });
106 };