@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 86 lines 3.33 KB
Raw
1 import { reifyBoundConfigProvider } from "alchemy/Runtime";
2 import {
3 entrypointLayer,
4 resolveProgram,
5 runProcess,
6 stackConstant,
7 } from "alchemy/Runtime/Bootstrap";
8 /**
9 * Process bootstrap core for `Cloudflare.Container` images. The runtime
10 * (`bun` | `node`) is chosen at deploy time, and each is an optional peer,
11 * so the platform-specific layers are supplied by the thin entry modules
12 * {@link ./ContainerBun.ts} / {@link ./ContainerNode.ts}
13 * rather than imported here. The generated entry imports one of those and
14 * the user's `main`, nothing else — see `alchemy/Runtime/Bootstrap` for why.
15 */
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 { MinimumLogLevel } from "effect/References";
22 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
23
24 export interface ContainerBootstrapOptions {
25 /** Stack identity baked in at deploy time. */
26 readonly stack: { readonly name: string; readonly stage: string };
27 }
28
29 export interface ContainerRuntime {
30 /** `BunServices.layer` / `NodeServices.layer`. */
31 readonly services: Layer.Layer<any>;
32 /** `BunHttpServer()` / `NodeHttpServer()`. */
33 readonly httpServer: Layer.Layer<any, any, any>;
34 }
35
36 /** Serve the bundled container program on the runtime's HTTP server. */
37 export const bootstrapContainer = (
38 runtime: ContainerRuntime,
39 entrypoint: unknown,
40 options: ContainerBootstrapOptions,
41 ): Promise<void> => {
42 const platform = Layer.mergeAll(
43 runtime.services,
44 FetchHttpClient.layer,
45 // TODO(sam): wire this up to telemetry more directly
46 Logger.layer([Logger.consolePretty()]),
47 );
48
49 const program = resolveProgram("default", { telemetry: true }).pipe(
50 Effect.provide(
51 entrypointLayer(entrypoint).pipe(
52 Layer.provideMerge(stackConstant(options.stack.name, options.stack.stage)),
53 Layer.provideMerge(runtime.httpServer),
54 // Capability bindings that talk to Cloudflare's HTTP API from inside
55 // the container (e.g. R2/KV/Queue `*Http` bindings) resolve their
56 // account via `CloudflareEnvironment` at runtime, exactly like the
57 // Worker bridge does (the service value is an `Effect` of the
58 // resolved credentials). The per-operation account/token are read
59 // from the container's env (the bound token outputs), so an absent
60 // account id here is harmless.
61 Layer.provideMerge(
62 Layer.succeed(
63 CloudflareEnvironment,
64 Effect.succeed({
65 account: process.env.ALCHEMY_CLOUDFLARE_ACCOUNT_ID,
66 }) as any,
67 ),
68 ),
69 Layer.provideMerge(platform),
70 Layer.provideMerge(
71 Layer.succeed(
72 ConfigProvider.ConfigProvider,
73 // Auto-bound `Config` values arrive in the env as
74 // `{"_tag":"Redacted","value":...}` markers; reify them so a
75 // `Config` re-read inside a handler decodes the raw source value.
76 reifyBoundConfigProvider(ConfigProvider.fromEnv(), process.env),
77 ),
78 ),
79 Layer.provideMerge(Layer.succeed(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info")),
80 ),
81 ),
82 Effect.scoped,
83 );
84
85 return runProcess("Container", program);
86 };