The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { declareDurableObjectApplication } from "@alchemy.run/cloudflare/Containers/DurableObjectApplication"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import type { NativeAsyncObject, NativeAsyncReviewer } from "./async-worker.ts"; |
| 5 | import { NativeImages } from "./images.ts"; |
| 6 | import { NativeLint, NativeShell, NativeTools } from "./object.ts"; |
| 7 | import { NativeWorker } from "./worker.ts"; |
| 8 | |
| 9 | export { NativeImages } from "./images.ts"; |
| 10 | |
| 11 | /** Shared by both async classes, keyed `AsyncShell` in `ctx.container.images`. */ |
| 12 | export const AsyncShell = Cloudflare.Container( |
| 13 | "AsyncShell", |
| 14 | Effect.map(NativeImages, ({ shell }) => ({ |
| 15 | schedulingPolicy: "durable_object" as const, |
| 16 | image: shell, |
| 17 | })), |
| 18 | ); |
| 19 | |
| 20 | /** `NativeAsyncObject`'s second image, keyed `AsyncTools`. */ |
| 21 | export const AsyncTools = Cloudflare.Container( |
| 22 | "AsyncTools", |
| 23 | Effect.map(NativeImages, ({ tools }) => ({ |
| 24 | schedulingPolicy: "durable_object" as const, |
| 25 | image: tools, |
| 26 | })), |
| 27 | ); |
| 28 | |
| 29 | /** `NativeAsyncReviewer`'s second image, keyed `AsyncLint`. */ |
| 30 | export const AsyncLint = Cloudflare.Container("AsyncLint", { |
| 31 | schedulingPolicy: "durable_object", |
| 32 | image: "alpine:3.19", |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | * Two classes on one async Worker, each with its own container application. |
| 37 | * `tools: undefined` removes `AsyncTools` from `NativeAsyncObject`. |
| 38 | */ |
| 39 | export const NativeAsyncWorker = Cloudflare.Worker( |
| 40 | "NativeAsyncWorker", |
| 41 | Effect.map(NativeImages, (images) => ({ |
| 42 | main: `${import.meta.dirname}/async-worker.ts`, |
| 43 | env: { |
| 44 | SANDBOX: Cloudflare.DurableObject<NativeAsyncObject>("NativeAsyncObject", { |
| 45 | containers: images.tools === undefined ? [AsyncShell] : [AsyncShell, AsyncTools], |
| 46 | }), |
| 47 | REVIEWER: Cloudflare.DurableObject<NativeAsyncReviewer>("NativeAsyncReviewer", { |
| 48 | containers: [AsyncShell, AsyncLint], |
| 49 | }), |
| 50 | IMAGE_REVISION: JSON.stringify(images), |
| 51 | }, |
| 52 | })), |
| 53 | ); |
| 54 | |
| 55 | export const nativeStack = Effect.gen(function* () { |
| 56 | const worker = yield* NativeWorker; |
| 57 | const asyncWorker = yield* NativeAsyncWorker; |
| 58 | return { |
| 59 | worker, |
| 60 | asyncWorker, |
| 61 | shell: yield* NativeShell.Application, |
| 62 | tools: yield* NativeTools.Application, |
| 63 | lint: yield* NativeLint.Application, |
| 64 | asyncShell: yield* AsyncShell.Application, |
| 65 | // Each class's one container application, shared by its Containers. |
| 66 | application: yield* declareDurableObjectApplication(worker, "NativeObject"), |
| 67 | reviewerApplication: yield* declareDurableObjectApplication(worker, "NativeReviewer"), |
| 68 | asyncApplication: yield* declareDurableObjectApplication(asyncWorker, "NativeAsyncObject"), |
| 69 | asyncReviewerApplication: yield* declareDurableObjectApplication( |
| 70 | asyncWorker, |
| 71 | "NativeAsyncReviewer", |
| 72 | ), |
| 73 | }; |
| 74 | }); |
| 75 | |
| 76 | /** The removed async form: a Durable Object-managed Container directly in `env`. */ |
| 77 | export const directEnvStack = Effect.gen(function* () { |
| 78 | return yield* Cloudflare.Worker("DirectEnvWorker", { |
| 79 | main: `${import.meta.dirname}/async-worker.ts`, |
| 80 | env: { SANDBOX: AsyncShell, IMAGE_REVISION: "direct" }, |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | /** |
| 85 | * An async Worker that gains its second container-backed class, |
| 86 | * `NativeAsyncReviewer`, on a later deploy (`withReviewer`). |
| 87 | */ |
| 88 | export const growingStack = (withReviewer: boolean) => |
| 89 | Effect.gen(function* () { |
| 90 | const worker = yield* Cloudflare.Worker("GrowingAsyncWorker", { |
| 91 | main: `${import.meta.dirname}/async-worker.ts`, |
| 92 | env: { |
| 93 | SANDBOX: Cloudflare.DurableObject<NativeAsyncObject>("NativeAsyncObject", { |
| 94 | containers: [AsyncShell], |
| 95 | }), |
| 96 | ...(withReviewer |
| 97 | ? { |
| 98 | REVIEWER: Cloudflare.DurableObject<NativeAsyncReviewer>("NativeAsyncReviewer", { |
| 99 | containers: [AsyncShell, AsyncLint], |
| 100 | }), |
| 101 | } |
| 102 | : {}), |
| 103 | IMAGE_REVISION: "growing", |
| 104 | }, |
| 105 | }); |
| 106 | return { |
| 107 | worker, |
| 108 | reviewerApplication: withReviewer |
| 109 | ? yield* declareDurableObjectApplication(worker, "NativeAsyncReviewer") |
| 110 | : undefined, |
| 111 | }; |
| 112 | }); |