@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 375 lines 13 KB
Raw
1 import { Credentials } from "@distilled.cloud/gcp/Credentials";
2 import type * as Bundle from "alchemy/Bundle/Bundle";
3 import type { PackageInstall } from "alchemy/Bundle/InstalledPackages";
4 import { isResolved } from "alchemy/Diff";
5 import { createPhysicalName } from "alchemy/PhysicalName";
6 import { Platform, type Main, type PlatformProps } from "alchemy/Platform";
7 import * as Provider from "alchemy/Provider";
8 import { Resource, type ResourceBinding } from "alchemy/Resource";
9 import { type ServerHost } from "alchemy/Runtime/Process";
10 import type { ExtraFile } from "alchemy/Util/extraFiles";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import { destroyHostImageRepository, makeImageSource } from "../ArtifactRegistry/ImageSource.ts";
14 import { GcpEnvironment } from "../Environment.ts";
15 import type { GcpHostBinding, GcpIamGrant } from "../Host.ts";
16 import { createGcpHostRuntimeContext, type GcpHostRuntimeContext } from "../HostContext.ts";
17 import { alchemyRuntimeEnv, makeGcpBootstrap, mergeContainerEnv } from "../HostRuntime.ts";
18 import type { Providers } from "../Providers.ts";
19 import type { ContainerSpec, Probe } from "./Service.ts";
20
21 const DEFAULT_PORT = 3000;
22 const MAX_NAME_LENGTH = 49;
23
24 /** Env var a sidecar's program listens on (Cloud Run sets `PORT` only on the ingress container). */
25 export const CONTAINER_PORT_ENV = "ALCHEMY_HTTP_PORT";
26
27 /**
28 * A container a host binds as a sidecar: its resolved spec, the IAM its
29 * bindings need on the revision's service account, and the sidecars it
30 * binds in turn.
31 */
32 export type BoundContainer = {
33 spec: ContainerSpec;
34 iam: GcpIamGrant[];
35 sidecars: BoundContainer[];
36 };
37
38 export type ContainerProps = PlatformProps &
39 Omit<ContainerSpec, "image" | "env" | "ports"> & {
40 /**
41 * Container image. Required unless `main` is set, in which case
42 * Alchemy builds and pushes the image.
43 */
44 image?: string;
45 /**
46 * Port the container listens on. A `main` program serves HTTP and
47 * bound RPC on it; bound callers reach it on `localhost`.
48 * @default 3000
49 */
50 port?: number;
51 /**
52 * Region of the Artifact Registry repository a `main` image is pushed
53 * to.
54 * @default the stack's GCP region
55 */
56 location?: string;
57 /**
58 * Module entrypoint for an Effect-native container (typically
59 * `import.meta.url`). Alchemy bundles the program and builds its image.
60 */
61 main?: string;
62 /**
63 * Named export to load from `main`.
64 * @default "default"
65 */
66 handler?: string;
67 /**
68 * Environment variables. Merged after binding-injected `env`.
69 */
70 env?: Record<string, any>;
71 /** Bundler configuration for `main`. */
72 build?: Bundle.BundleConfig & { install?: PackageInstall };
73 /** Host files or directories baked into the image next to `main`. */
74 extraFiles?: ReadonlyArray<ExtraFile>;
75 };
76
77 export type Container = Resource<
78 "GCP.Run.Container",
79 ContainerProps,
80 {
81 /** Container name in the revision (DNS_LABEL). */
82 name: string;
83 /** Image the container runs. */
84 image: string;
85 /** Port the container listens on. */
86 port: number | undefined;
87 /** The container's spec, as a host adds it to its revision. */
88 spec: ContainerSpec;
89 /** IAM roles the container's bindings need on the revision's service account. */
90 iam: GcpIamGrant[];
91 /** Sidecars this container binds, which join the same revision. */
92 sidecars: BoundContainer[];
93 /** Project id. */
94 project: string;
95 /** Region of the image repository. */
96 location: string;
97 /** Artifact Registry repository a `main` image is pushed to. */
98 repository: string | undefined;
99 /** Hash of the bundled `main` program (Effect-native only). */
100 codeHash: string | undefined;
101 },
102 GcpHostBinding,
103 Providers
104 >;
105
106 export type ContainerRuntimeContext = GcpHostRuntimeContext;
107 export type ContainerServices = Credentials | GcpEnvironment | ServerHost;
108 export type ContainerShape = Main<ContainerServices>;
109
110 /**
111 * A container that runs next to a Cloud Run service's main container, in
112 * the same instance. Bind it from the service with
113 * {@link bindContainer} (typed RPC and `fetch`) or {@link bindPort} (a raw
114 * port): binding adds it to the service's revision and holds the service
115 * back until the container passes its startup probe.
116 *
117 * Give it `main` and an Effect implementation to run your own program —
118 * Alchemy bundles it into its own image — or just an `image`.
119 *
120 * ### Declaring a Container
121 * **Example:** An Effect-native container with RPC methods
122 * ```typescript
123 * export default class Worker extends GCP.Run.Container<Worker>()(
124 * "Worker",
125 * {
126 * main: import.meta.url,
127 * port: 3000,
128 * startupProbe: { httpGet: { path: "/health" } },
129 * },
130 * Effect.gen(function* () {
131 * return {
132 * greet: (name: string) => Effect.succeed(`hello ${name}`),
133 * fetch: Effect.succeed(HttpServerResponse.text("ok")),
134 * };
135 * }),
136 * ) {}
137 * ```
138 *
139 * **Example:** An image
140 * ```typescript
141 * export const Cache = GCP.Run.Container("Cache", {
142 * image: "docker.io/library/redis:7",
143 * port: 6379,
144 * startupProbe: { tcpSocket: {} },
145 * });
146 * ```
147 *
148 * ### Using bindings
149 * **Example:** Bindings inside a Container
150 * ```typescript
151 * export default class Worker extends GCP.Run.Container<Worker>()(
152 * "Worker",
153 * { main: import.meta.url, port: 3000 },
154 * Effect.gen(function* () {
155 * const publish = yield* GCP.PubSub.Publish(Tweets);
156 * return {
157 * post: (text: string) =>
158 * publish({ body: { messages: [{ data: btoa(text) }] } }),
159 * };
160 * }).pipe(Effect.provide(GCP.PubSub.PublishHttp)),
161 * ) {}
162 * ```
163 * The binding's env lands on this container. Its IAM roles go to the
164 * service account of the revision the container joins.
165 *
166 * @resource
167 * @category Run
168 */
169 export const Container: Platform<
170 Container,
171 ContainerServices,
172 ContainerShape,
173 ContainerRuntimeContext
174 > = Platform("GCP.Run.Container", {
175 createRuntimeContext: createGcpHostRuntimeContext("GCP.Run.Container", {
176 rpc: true,
177 }) as (id: string) => ContainerRuntimeContext,
178 });
179
180 /** A Container declares neither `image` nor `main`. */
181 export class ContainerImageMissing extends Data.TaggedError("GCP.Run.ContainerImageMissing")<{
182 id: string;
183 }> {
184 override get message() {
185 return `GCP.Run.Container ${this.id} needs an image or main`;
186 }
187 }
188
189 const lastSegment = (value: string) => {
190 const parts = value.replace(/\/+$/, "").split("/");
191 return parts[parts.length - 1] || value;
192 };
193
194 const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
195 lastSegment(location ?? defaultLocation).toLowerCase();
196
197 const dnsLabel = (name: string, fallback: string): string => {
198 let next = name
199 .toLowerCase()
200 .replace(/[^a-z0-9-]/g, "-")
201 .replace(/-+/g, "-")
202 .replace(/^-+|-+$/g, "");
203 if (!/^[a-z]/.test(next)) next = `c${next}`;
204 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
205 return next.length > 0 ? next : fallback;
206 };
207
208 /** Point a probe that names no port at the container's port; sidecars expose no `ports`. */
209 const withPort = (probe: Probe | undefined, port: number | undefined): Probe | undefined => {
210 if (probe === undefined || port === undefined) return probe;
211 return {
212 ...probe,
213 httpGet: probe.httpGet && { ...probe.httpGet, port: probe.httpGet.port ?? port },
214 tcpSocket: probe.tcpSocket && { ...probe.tcpSocket, port: probe.tcpSocket.port ?? port },
215 grpc: probe.grpc && { ...probe.grpc, port: probe.grpc.port ?? port },
216 };
217 };
218
219 const sourceOf = (news: ContainerProps, main: string) => ({
220 main,
221 handler: news.handler ?? "default",
222 build: news.build,
223 extraFiles: news.extraFiles,
224 install: news.build?.install,
225 });
226
227 const bootstrapFor = (news: ContainerProps) =>
228 makeGcpBootstrap("CloudRun", news.handler ?? "default");
229
230 const repositoryFor = Effect.fn(function* (id: string) {
231 const name = yield* createPhysicalName({ id, maxLength: MAX_NAME_LENGTH, lowercase: true });
232 return `${dnsLabel(name, "container")}-src`;
233 });
234
235 /** Containers bound by this container's own bindings, flattened. */
236 export const boundContainersOf = (
237 bindings: readonly ResourceBinding<GcpHostBinding>[],
238 ): BoundContainer[] =>
239 flattenContainers(bindings.flatMap((binding) => binding.data?.containers ?? []));
240
241 /** These containers and every sidecar they bind, once each by name. */
242 export const flattenContainers = (containers: readonly BoundContainer[]): BoundContainer[] => {
243 const seen = new Map<string, BoundContainer>();
244 const visit = (container: BoundContainer) => {
245 const name = container.spec.name ?? "";
246 if (seen.has(name)) return;
247 seen.set(name, container);
248 for (const sidecar of container.sidecars ?? []) visit(sidecar);
249 };
250 for (const container of containers) visit(container);
251 return [...seen.values()];
252 };
253
254 /**
255 * A `GCP.Run.Container` listed in a revision template reaches the
256 * provider as its attributes (the engine resolves resource references in
257 * props).
258 */
259 export const isContainerAttributes = (value: unknown): value is Container["Attributes"] =>
260 typeof value === "object" &&
261 value !== null &&
262 typeof (value as { spec?: unknown }).spec === "object" &&
263 Array.isArray((value as { iam?: unknown }).iam) &&
264 Array.isArray((value as { sidecars?: unknown }).sidecars);
265
266 /** Names of the containers bound directly by these bindings. */
267 export const directContainerNames = (bindings: readonly ResourceBinding<GcpHostBinding>[]) => [
268 ...new Set(
269 bindings.flatMap((binding) =>
270 (binding.data?.containers ?? []).map((container) => container.spec.name ?? ""),
271 ),
272 ),
273 ];
274
275 export const ContainerProvider = () =>
276 Provider.succeed(Container, {
277 stables: ["name", "project", "location", "repository"],
278
279 diff: Effect.fn(function* ({ news, output }) {
280 if (!isResolved(news)) return undefined;
281 // A code-only change leaves the props untouched; hash the bundle.
282 if (output !== undefined && news.main !== undefined) {
283 const images = yield* makeImageSource;
284 const hash = yield* images.hash({
285 source: sourceOf(news, news.main),
286 port: news.port ?? DEFAULT_PORT,
287 isExternal: news.isExternal,
288 bootstrap: bootstrapFor(news),
289 });
290 if (hash !== undefined && hash !== output.codeHash) {
291 return { action: "update" as const };
292 }
293 }
294 return undefined;
295 }),
296
297 read: Effect.fn(function* ({ output }) {
298 return output;
299 }),
300
301 reconcile: Effect.fn(function* ({ id, news, output, bindings, session }) {
302 const env = yield* GcpEnvironment.current;
303 const location = normalizeLocation(news.location ?? output?.location, env.region);
304 const name = output?.name ?? dnsLabel(news.name ?? id, "container");
305 const hostBindings = bindings as ResourceBinding<GcpHostBinding>[];
306 const port = news.port ?? (news.main !== undefined ? DEFAULT_PORT : undefined);
307
308 let image = news.image;
309 let codeHash: string | undefined;
310 let repository: string | undefined;
311 const runtimeEnv: Record<string, unknown> = {};
312 if (news.main !== undefined) {
313 repository = output?.repository ?? (yield* repositoryFor(id));
314 const images = yield* makeImageSource;
315 const resolved = yield* images.resolve({
316 id,
317 source: sourceOf(news, news.main),
318 repositoryName: repository,
319 location,
320 port: port ?? DEFAULT_PORT,
321 isExternal: news.isExternal,
322 bootstrap: bootstrapFor(news),
323 session,
324 });
325 image = resolved.imageUri;
326 codeHash = resolved.codeHash;
327 Object.assign(runtimeEnv, yield* alchemyRuntimeEnv, {
328 [CONTAINER_PORT_ENV]: String(port ?? DEFAULT_PORT),
329 });
330 }
331 if (image === undefined) {
332 return yield* new ContainerImageMissing({ id });
333 }
334
335 const bindingEnv = Object.assign(
336 {},
337 ...hostBindings.map((binding) => binding.data?.env ?? {}),
338 ) as Record<string, unknown>;
339 const dependsOn = [
340 ...new Set([...(news.dependsOn ?? []), ...directContainerNames(hostBindings)]),
341 ];
342 const spec: ContainerSpec = {
343 name,
344 command: news.command,
345 args: news.args,
346 workingDir: news.workingDir,
347 resources: news.resources,
348 volumeMounts: news.volumeMounts,
349 image,
350 env: mergeContainerEnv(undefined, bindingEnv, runtimeEnv, news.env),
351 startupProbe: withPort(news.startupProbe, port),
352 livenessProbe: withPort(news.livenessProbe, port),
353 dependsOn: dependsOn.length > 0 ? dependsOn : undefined,
354 };
355
356 return {
357 name,
358 image,
359 port,
360 spec,
361 iam: hostBindings.flatMap((binding) => (binding.data?.iam ?? []) as GcpIamGrant[]),
362 sidecars: boundContainersOf(hostBindings),
363 project: env.project,
364 location,
365 repository,
366 codeHash,
367 };
368 }),
369
370 delete: Effect.fn(function* ({ id, output }) {
371 if (output.repository !== undefined) {
372 yield* destroyHostImageRepository(id, output, output.repository);
373 }
374 }),
375 });