@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 64 lines 2.25 KB
Raw
1 import {
2 entrypointLayer,
3 resolveProgram,
4 runProcess,
5 stackConstant,
6 } from "alchemy/Runtime/Bootstrap";
7 /**
8 * Process bootstrap core for `AWS.Lambda.MicrovmImage` images. The runtime
9 * (`bun` | `node`) is chosen at deploy time, and each is an optional peer,
10 * so the platform-specific layers are supplied by the thin entry modules
11 * {@link ./MicrovmBun.ts} / {@link ./MicrovmNode.ts} rather than imported
12 * here. The generated entry imports one of those and the user's `main`,
13 * nothing else — see `alchemy/Runtime/Bootstrap` for why.
14 */
15 import * as Effect from "effect/Effect";
16 import * as FetchHttpClient from "effect/http/FetchHttpClient";
17 import * as Layer from "effect/Layer";
18 import * as Logger from "effect/Logger";
19 import { MinimumLogLevel } from "effect/References";
20
21 export interface MicrovmBootstrapOptions {
22 /** Port to serve on when the VM does not inject `PORT`. */
23 readonly port: number;
24 /** Stack identity baked in at deploy time. */
25 readonly stack: { readonly name: string; readonly stage: string };
26 }
27
28 export interface MicrovmRuntime {
29 /** `BunServices.layer` / `NodeServices.layer`. */
30 readonly services: Layer.Layer<any>;
31 /** `BunHttpServer()` / `NodeHttpServer()`; both listen on `PORT`. */
32 readonly httpServer: Layer.Layer<any, any, any>;
33 }
34
35 /** Serve the bundled in-VM program on the runtime's HTTP server. */
36 export const bootstrapMicrovm = (
37 runtime: MicrovmRuntime,
38 entrypoint: unknown,
39 options: MicrovmBootstrapOptions,
40 ): Promise<void> => {
41 // The HTTP server listens on `PORT`; default it to the image's port when
42 // the VM does not inject one.
43 process.env.PORT ??= String(options.port);
44
45 const platform = Layer.mergeAll(
46 runtime.services,
47 FetchHttpClient.layer,
48 Logger.layer([Logger.consolePretty()]),
49 );
50
51 const program = resolveProgram("default", { telemetry: true }).pipe(
52 Effect.provide(
53 entrypointLayer(entrypoint).pipe(
54 Layer.provideMerge(stackConstant(options.stack.name, options.stack.stage)),
55 Layer.provideMerge(runtime.httpServer),
56 Layer.provideMerge(platform),
57 Layer.provideMerge(Layer.succeed(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info")),
58 ),
59 ),
60 Effect.scoped,
61 );
62
63 return runProcess(`MicroVM (port ${process.env.PORT})`, program);
64 };