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

main
ts 113 lines 4.14 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import * as HttpClientRequest from "effect/http/HttpClientRequest";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as Option from "effect/Option";
6 import * as Redacted from "effect/Redacted";
7 import * as Result from "effect/Result";
8
9 /**
10 * A sibling resource whose output is threaded into the container's `env`,
11 * proving that a pre-built image (no Effect bundling, no `.make()`) receives
12 * environment variables that resolve from other resources in the stack.
13 */
14 export const EnvBucket = Cloudflare.R2.Bucket("RemoteContainerEnvBucket", {
15 forceDestroy: true,
16 });
17
18 /** Plain-text env value the container is expected to echo back verbatim. */
19 export const DEMO_PLAIN = "hello-from-env";
20 /** `Redacted` env value — encrypted in state, plain inside the container. */
21 export const DEMO_SECRET = "sh-hh-its-a-secret";
22
23 export class RemoteContainer extends Cloudflare.Container<RemoteContainer>()(
24 "RemoteContainer",
25 Effect.gen(function* () {
26 const bucket = yield* EnvBucket;
27 return {
28 image: "mendhak/http-https-echo:latest",
29 vcpu: 1,
30 memoryMib: 4096,
31 observability: { logs: { enabled: true } },
32 env: {
33 // Tells the echo image to include `process.env` in its JSON response.
34 ECHO_INCLUDE_ENV_VARS: "1",
35 DEMO_PLAIN,
36 DEMO_SECRET: Redacted.make(DEMO_SECRET),
37 DEMO_BUCKET: bucket.bucketName,
38 },
39 };
40 }),
41 ) {}
42
43 /**
44 * Durable Object that binds and starts the {@link RemoteContainer} and
45 * proxies an HTTP request to the echo server running on port 8080 inside it.
46 */
47 export class RemoteContainerObject extends Cloudflare.DurableObject<RemoteContainerObject>()(
48 "RemoteContainerObject",
49 Effect.gen(function* () {
50 const container = yield* RemoteContainer;
51
52 return Effect.gen(function* () {
53 // Starting is idempotent: a no-op once the container is running.
54 const start = container.start({ enableInternet: true });
55 const { fetch } = yield* container.getTcpPort(8080);
56
57 return {
58 hello: () =>
59 Effect.gen(function* () {
60 yield* start;
61 const response = yield* fetch(HttpClientRequest.get("http://container/"));
62 return yield* response.text;
63 }),
64 // The proxy pattern from #1334: forward the incoming request to the
65 // container verbatim. In production the incoming web Request carries
66 // an https:// URL, which workerd's container ports reject — the
67 // runtime must downgrade the scheme on the container hop.
68 fetch: Effect.gen(function* () {
69 const request = yield* HttpServerRequest;
70 yield* start;
71 return yield* fetch(request);
72 }),
73 };
74 });
75 }),
76 ) {}
77
78 /**
79 * The echo image capped at one instance, so a second Durable Object's start
80 * finds no container instance to run on.
81 */
82 export class CappedContainer extends Cloudflare.Container<CappedContainer>()("CappedContainer", {
83 image: "mendhak/http-https-echo:latest",
84 maxInstances: 1,
85 }) {}
86
87 /**
88 * Starts the {@link CappedContainer} and reports how it ends within a short
89 * window: the error tag `start` or `monitor` failed with, or `running`.
90 */
91 export class CappedContainerObject extends Cloudflare.DurableObject<CappedContainerObject>()(
92 "CappedContainerObject",
93 Effect.gen(function* () {
94 const container = yield* CappedContainer;
95
96 return Effect.succeed({
97 start: Effect.fn("start")(function* () {
98 const started = yield* container.start({ enableInternet: true }).pipe(Effect.result);
99 if (Result.isFailure(started)) return { phase: "start", tag: started.failure._tag };
100 const monitored = yield* container
101 .monitor()
102 .pipe(Effect.timeoutOption("20 seconds"), Effect.result);
103 if (Result.isFailure(monitored))
104 return {
105 phase: "monitor",
106 tag: monitored.failure._tag,
107 detail: String(monitored.failure.cause ?? monitored.failure.message),
108 };
109 return { phase: "monitor", tag: Option.isNone(monitored.success) ? "running" : "exited" };
110 }),
111 });
112 }),
113 ) {}