The best way to control Claude Code, Codex and more from any computer or phone

main
ts 67 lines 2.53 KB
Raw
1 import { Docker, DockerEnvironment } from "@alchemy.run/docker/Docker";
2 import { Stage } from "alchemy/Stage";
3 import * as Effect from "effect/Effect";
4 import * as Schema from "effect/Schema";
5
6 /** Run `effect` with extra environment on every Docker CLI call it makes. */
7 export const withDockerEnv =
8 (env: Record<string, string>) =>
9 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
10 Effect.flatMap(DockerEnvironment, (current) =>
11 Effect.provideService(effect, DockerEnvironment, { ...current, ...env }),
12 );
13
14 export const withBuilder =
15 (prefix: string, options?: { attestations?: boolean }) =>
16 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
17 Effect.gen(function* () {
18 const name = `${prefix}-${yield* Stage}`;
19 const docker = yield* Docker;
20 return yield* Effect.acquireUseRelease(
21 // A killed run cannot release its builder. Remove any leftover first,
22 // so every invocation starts from an empty cache under the same name.
23 docker
24 .run(["buildx", "rm", "--force", name])
25 .pipe(
26 Effect.ignore,
27 Effect.andThen(
28 docker.run(["buildx", "create", "--name", name, "--driver", "docker-container"]),
29 ),
30 ),
31 () =>
32 docker.run(["buildx", "inspect", "--bootstrap"]).pipe(
33 Effect.andThen(effect),
34 withDockerEnv({
35 BUILDX_BUILDER: name,
36 BUILDX_NO_DEFAULT_ATTESTATIONS: String(options?.attestations === false),
37 }),
38 ),
39 () => docker.run(["buildx", "rm", "--force", name]).pipe(Effect.orDie),
40 );
41 });
42
43 /**
44 * Whether the installed Buildx can export straight to a registry
45 * (`buildx build --push` authenticating via DOCKER_AUTH_CONFIG, 0.26+).
46 * Older plugins make the provider `--load` the image and `docker push` it.
47 */
48 export const supportsRegistryExport = Effect.gen(function* () {
49 const docker = yield* Docker;
50 const version = yield* docker.run(["buildx", "version"]);
51 const match = /buildx v?(\d+)\.(\d+)\./.exec(version.stdout);
52 if (!match) return false;
53 return Number(match[1]) >= 1 || Number(match[2]) >= 26;
54 });
55
56 const decodeBuild = Schema.Struct({ ref: Schema.String, status: Schema.String }).pipe(
57 Schema.fromJsonString,
58 Schema.decodeEffect,
59 );
60
61 export const buildHistory = Effect.gen(function* () {
62 const docker = yield* Docker;
63 const history = yield* docker.run(["buildx", "history", "ls", "--format", "json"]);
64 return yield* Effect.forEach(history.stdout.split("\n").filter(Boolean), (line) =>
65 decodeBuild(line),
66 );
67 });