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 * as Alchemy from "alchemy"; |
| 3 | import * as Config from "effect/Config"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import type { Counter, QueueMessages } from "./src/AsyncWorker.ts"; |
| 7 | import EffectWorker from "./src/EffectWorker.ts"; |
| 8 | import HyperdriveWorker from "./src/HyperdriveWorker.ts"; |
| 9 | import { SandboxLive } from "./src/SandboxContainer.ts"; |
| 10 | |
| 11 | export type AsyncWorkerEnv = Cloudflare.InferEnv<ReturnType<typeof AsyncWorker>>; |
| 12 | |
| 13 | /** |
| 14 | * Value the Secrets Store secret is seeded with. The integ test asserts the |
| 15 | * exact value round-trips through the `secrets_store_secret` binding. |
| 16 | */ |
| 17 | export const SECRETS_STORE_VALUE = "store-secret-abc123"; |
| 18 | |
| 19 | /** |
| 20 | * Hyperdrive needs a reachable Postgres origin even in local dev (the local |
| 21 | * provider is a passthrough to the `dev` origin, not a SQL simulator). Set |
| 22 | * e.g. `HYPERDRIVE_DEV_URL=postgres://user:pass@localhost:5432/postgres` to |
| 23 | * include the HyperdriveWorker in the stack; without it the resource is |
| 24 | * omitted and the integ test skips. |
| 25 | */ |
| 26 | export const HYPERDRIVE_DEV_URL = process.env.HYPERDRIVE_DEV_URL; |
| 27 | |
| 28 | /** |
| 29 | * Tail consumer: listed in AsyncWorker's `tailConsumers`, it receives a trace |
| 30 | * batch for every AsyncWorker invocation and records them into a KV |
| 31 | * namespace, exposed over `GET /events`. |
| 32 | */ |
| 33 | const TailWorker = Effect.gen(function* () { |
| 34 | const events = yield* Cloudflare.KV.Namespace("TailEvents"); |
| 35 | return yield* Cloudflare.Worker("TailWorker", { |
| 36 | main: "./src/TailWorker.ts", |
| 37 | env: { |
| 38 | EVENTS: events, |
| 39 | }, |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | const AsyncWorker = (deps: { tailWorker: Cloudflare.Worker; liveKv: Cloudflare.KV.Namespace }) => |
| 44 | Effect.gen(function* () { |
| 45 | const queue = yield* Cloudflare.Queues.Queue("AsyncWorkerQueue"); |
| 46 | const bucket = yield* Cloudflare.R2.Bucket("AsyncWorkerBucket", { |
| 47 | forceDestroy: true, |
| 48 | }); |
| 49 | const db = yield* Cloudflare.D1.Database("AsyncWorkerDB", { |
| 50 | // Applied on deploy — including local dev, where they run against the |
| 51 | // local D1 simulator through an ephemeral workerd gateway. |
| 52 | migrations: "./migrations", |
| 53 | }); |
| 54 | const worker = yield* Cloudflare.Worker("AsyncWorker", { |
| 55 | main: "./src/AsyncWorker.ts", |
| 56 | assets: { |
| 57 | directory: "./assets", |
| 58 | runWorkerFirst: true, |
| 59 | }, |
| 60 | // Every invocation of this worker delivers a trace batch (console |
| 61 | // logs, outcome) to the tail worker. |
| 62 | tailConsumers: [deps.tailWorker], |
| 63 | env: { |
| 64 | COUNTER: Cloudflare.DurableObject<Counter>("Counter", { |
| 65 | className: "Counter", |
| 66 | }), |
| 67 | QUEUE: queue, |
| 68 | BUCKET: bucket, |
| 69 | DB: db, |
| 70 | MESSAGES: Cloudflare.DurableObject<QueueMessages>("QueueMessages", { |
| 71 | className: "QueueMessages", |
| 72 | }), |
| 73 | MY_VARIABLE: "my-variable-abc123", |
| 74 | MY_SECRET: Config.Redacted("MY_SECRET").pipe( |
| 75 | Config.withDefault(Redacted.make("my-secret-abc123")), |
| 76 | ), |
| 77 | // The worker's own URL, injected as a plain-text binding (`self_url`). |
| 78 | PUBLIC_URL: Cloudflare.Worker.URL, |
| 79 | // Rate limit binding — a Worker-only binding with no backing cloud |
| 80 | // resource; fully emulated (real throttling) in local dev. |
| 81 | THROTTLE: Cloudflare.RateLimit("THROTTLE", { |
| 82 | namespaceId: 1001, |
| 83 | simple: { limit: 2, period: 10 }, |
| 84 | }), |
| 85 | // Version metadata — locally stubbed with a random `id`. |
| 86 | CF_VERSION_METADATA: Cloudflare.Workers.VersionMetadata(), |
| 87 | // Service binding to the Effect-native worker (worker → worker call). |
| 88 | SERVICE: EffectWorker, |
| 89 | // Hybrid: this KV namespace opted out of local emulation via |
| 90 | // `Alchemy.remote()` — the local worker proxies to the REAL namespace. |
| 91 | LIVE_KV: deps.liveKv, |
| 92 | }, |
| 93 | }); |
| 94 | yield* Cloudflare.Queues.Consumer("Consumer", { |
| 95 | queueId: queue.queueId, |
| 96 | scriptName: worker.workerName, |
| 97 | }); |
| 98 | return worker; |
| 99 | }); |
| 100 | |
| 101 | /** |
| 102 | * Media & messaging worker: Browser Rendering, Images, Stream, Secrets |
| 103 | * Store, and Email (send + inbound handler) — all served by local |
| 104 | * simulators under `alchemy dev`. `IMAGES_REMOTE` demonstrates the per- |
| 105 | * binding hybrid escape hatch: piping the binding through |
| 106 | * `Alchemy.remote()` proxies just that binding to the real Images service |
| 107 | * while the worker stays local. |
| 108 | */ |
| 109 | const MediaWorker = Effect.gen(function* () { |
| 110 | const store = yield* Cloudflare.SecretsStore.Store("Secrets"); |
| 111 | const apiKey = yield* Cloudflare.SecretsStore.Secret("ApiKey", { |
| 112 | store, |
| 113 | value: Redacted.make(SECRETS_STORE_VALUE), |
| 114 | }); |
| 115 | const email = yield* Cloudflare.Email.SendEmail("Notifications", { |
| 116 | allowedDestinationAddresses: ["allowed@example.com"], |
| 117 | }); |
| 118 | const worker = yield* Cloudflare.Worker("MediaWorker", { |
| 119 | main: "./src/MediaWorker.ts", |
| 120 | env: { |
| 121 | BROWSER: Cloudflare.Browser("BROWSER"), |
| 122 | IMAGES: Cloudflare.Images.Images("IMAGES"), |
| 123 | IMAGES_REMOTE: Cloudflare.Images.Images("IMAGES_REMOTE").pipe(Alchemy.remote()), |
| 124 | STREAM: Cloudflare.Stream.Stream("STREAM"), |
| 125 | EMAIL: email, |
| 126 | API_KEY: apiKey, |
| 127 | }, |
| 128 | }); |
| 129 | return { worker, store, apiKey }; |
| 130 | }); |
| 131 | |
| 132 | /** |
| 133 | * Inbound email worker: exposes an `email()` handler driven locally via |
| 134 | * `POST /cdn-cgi/handler/email`. Kept on its own worker — see the note in |
| 135 | * src/InboxWorker.ts for why it must not share a worker with images/stream |
| 136 | * bindings in local dev. |
| 137 | */ |
| 138 | const InboxWorker = Effect.gen(function* () { |
| 139 | const inbox = yield* Cloudflare.KV.Namespace("Inbox"); |
| 140 | return yield* Cloudflare.Worker("InboxWorker", { |
| 141 | main: "./src/InboxWorker.ts", |
| 142 | env: { |
| 143 | INBOX: inbox, |
| 144 | }, |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | export default Alchemy.Stack( |
| 149 | "CloudflareDev", |
| 150 | { |
| 151 | providers: Cloudflare.providers(), |
| 152 | state: Cloudflare.state(), |
| 153 | }, |
| 154 | Effect.gen(function* () { |
| 155 | const tailWorker = yield* TailWorker; |
| 156 | const liveKv = yield* Cloudflare.KV.Namespace("LiveKV").pipe(Alchemy.remote()); |
| 157 | const asyncWorker = yield* AsyncWorker({ tailWorker, liveKv }); |
| 158 | const effectWorker = yield* EffectWorker; |
| 159 | const media = yield* MediaWorker; |
| 160 | const inboxWorker = yield* InboxWorker; |
| 161 | const hyperdrive = HYPERDRIVE_DEV_URL ? yield* HyperdriveWorker : undefined; |
| 162 | |
| 163 | return { |
| 164 | asyncWorker: asyncWorker.url, |
| 165 | effectWorker: effectWorker.url, |
| 166 | mediaWorker: media.worker.url, |
| 167 | tailWorker: tailWorker.url, |
| 168 | inboxWorker: inboxWorker.url, |
| 169 | // `dev:`-prefixed ids mean "locally emulated"; LiveKV must NOT carry |
| 170 | // one (it's a real cloud namespace, even during dev). |
| 171 | liveKvNamespaceId: liveKv.namespaceId, |
| 172 | secretsStoreId: media.store.storeId, |
| 173 | secretsSecretId: media.apiKey.secretId, |
| 174 | ...(hyperdrive ? { hyperdrive: hyperdrive.url } : {}), |
| 175 | }; |
| 176 | }).pipe(Effect.provide(SandboxLive)), |
| 177 | ); |