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 Effect from "effect/Effect"; |
| 4 | import { Gateway } from "./src/AiGateway.ts"; |
| 5 | import Api from "./src/Api.ts"; |
| 6 | import { Bucket } from "./src/Bucket.ts"; |
| 7 | import SandboxLive from "./src/Sandbox.ts"; |
| 8 | import SecondaryApiLive, { SecondaryApi } from "./src/SecondaryApi.ts"; |
| 9 | import WorkerTagLive, { WorkerTag } from "./src/WorkerTag.ts"; |
| 10 | |
| 11 | // Demo Action — runs at deploy time when its input (the resolved deployed |
| 12 | // URL) changes. Logs the new URL and returns a tiny manifest used as the |
| 13 | // stack output. Re-deploys with no changes skip the body. |
| 14 | const AnnounceDeploy = Alchemy.Action("AnnounceDeploy", (input: { url: string; bucket: string }) => |
| 15 | Effect.gen(function* () { |
| 16 | yield* Effect.log(`Deployed ${input.url} (bucket: ${input.bucket})`); |
| 17 | return { deployedAt: new Date().toISOString(), url: input.url }; |
| 18 | }), |
| 19 | ); |
| 20 | |
| 21 | export default Alchemy.Stack( |
| 22 | "CloudflareWorkerExample", |
| 23 | { |
| 24 | providers: Cloudflare.providers(), |
| 25 | state: Cloudflare.state(), |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const api = yield* Api; |
| 29 | const bucket = yield* Bucket; |
| 30 | const gateway = yield* Gateway; |
| 31 | const workerTag = yield* WorkerTag; |
| 32 | // Two Workers binding the same Agent DO triggers the regression where |
| 33 | // a single Container DO namespace appears in multiple bindings on the |
| 34 | // Sandbox ContainerApplication. See SecondaryApi.ts for details. |
| 35 | const secondaryApi = yield* SecondaryApi; |
| 36 | // The Queue consumer is wired automatically by |
| 37 | // `Cloudflare.Queues.consumeQueueMessages(Queue, handler)` inside src/Api.ts — |
| 38 | // no explicit `Cloudflare.Queues.Consumer(...)` is needed here. |
| 39 | |
| 40 | const announcement = yield* AnnounceDeploy({ |
| 41 | url: api.url.as<string>(), |
| 42 | bucket: bucket.bucketName, |
| 43 | }); |
| 44 | |
| 45 | return { |
| 46 | url: api.url.as<string>(), |
| 47 | bucket: bucket.bucketName, |
| 48 | gatewayId: gateway.gatewayId, |
| 49 | workerTagUrl: workerTag.url.as<string>(), |
| 50 | secondaryApiUrl: secondaryApi.url.as<string>(), |
| 51 | deployedAt: announcement.deployedAt, |
| 52 | }; |
| 53 | }).pipe(Effect.provide([WorkerTagLive, SecondaryApiLive, SandboxLive])), |
| 54 | ); |