The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Stack } from "alchemy/Stack"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import { JobNotifications, JobNotificationsSNS, NotifyJobError } from "./JobNotifications.ts"; |
| 8 | import { GetJobError, JobStorage, JobStorageDynamoDB, PutJobError } from "./JobStorage.ts"; |
| 9 | |
| 10 | export default class JobFunction extends AWS.Lambda.Function<JobFunction>()( |
| 11 | "JobFunction", |
| 12 | Stack.useSync((stack) => ({ |
| 13 | main: import.meta.url, |
| 14 | memorySize: stack.stage === "prod" ? 1024 : 512, |
| 15 | functionUrl: true, |
| 16 | })), |
| 17 | Effect.gen(function* () { |
| 18 | const jobStorage = yield* JobStorage; |
| 19 | const notifications = yield* JobNotifications; |
| 20 | |
| 21 | return { |
| 22 | fetch: Effect.gen(function* () { |
| 23 | const request = yield* HttpServerRequest; |
| 24 | const url = new URL(request.originalUrl); |
| 25 | |
| 26 | if (request.method === "GET" && url.pathname === "/") { |
| 27 | const jobId = url.searchParams.get("jobId"); |
| 28 | if (!jobId) { |
| 29 | return HttpServerResponse.text("Job ID is required", { status: 400 }); |
| 30 | } |
| 31 | |
| 32 | const job = yield* jobStorage |
| 33 | .getJob(jobId) |
| 34 | .pipe(Effect.match({ onFailure: (error) => error, onSuccess: (job) => job })); |
| 35 | |
| 36 | if (job instanceof GetJobError) { |
| 37 | return HttpServerResponse.text(job.message, { status: 500 }); |
| 38 | } |
| 39 | |
| 40 | if (!job) { |
| 41 | return HttpServerResponse.text("Job not found", { status: 404 }); |
| 42 | } |
| 43 | |
| 44 | return yield* HttpServerResponse.json(job); |
| 45 | } |
| 46 | |
| 47 | if (request.method === "POST" && url.pathname === "/") { |
| 48 | const content = yield* request.text; |
| 49 | if (!content) { |
| 50 | return HttpServerResponse.text("Job content is required", { status: 400 }); |
| 51 | } |
| 52 | |
| 53 | const job = yield* jobStorage |
| 54 | .putJob({ id: crypto.randomUUID(), content }) |
| 55 | .pipe(Effect.match({ onFailure: (error) => error, onSuccess: (job) => job })); |
| 56 | |
| 57 | if (job instanceof PutJobError) { |
| 58 | return HttpServerResponse.text(job.message, { status: 500 }); |
| 59 | } |
| 60 | |
| 61 | const notificationResult = yield* notifications |
| 62 | .notifyJobCreated(job) |
| 63 | .pipe(Effect.match({ onFailure: (error) => error, onSuccess: () => undefined })); |
| 64 | |
| 65 | if (notificationResult instanceof NotifyJobError) { |
| 66 | return HttpServerResponse.text(notificationResult.message, { status: 500 }); |
| 67 | } |
| 68 | |
| 69 | return yield* HttpServerResponse.json({ jobId: job.id }, { status: 201 }); |
| 70 | } |
| 71 | |
| 72 | return HttpServerResponse.text("Not found", { status: 404 }); |
| 73 | }), |
| 74 | }; |
| 75 | }).pipe( |
| 76 | Effect.provide( |
| 77 | Layer.mergeAll( |
| 78 | // Services go here |
| 79 | JobStorageDynamoDB, |
| 80 | JobNotificationsSNS, |
| 81 | // JobStorageS3, |
| 82 | ), |
| 83 | ), |
| 84 | ), |
| 85 | ) {} |