The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as DynamoDB from "@alchemy.run/aws/DynamoDB"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as S3 from "@alchemy.run/aws/S3"; |
| 4 | import * as SQS from "@alchemy.run/aws/SQS"; |
| 5 | import * as RemovalPolicy from "alchemy/RemovalPolicy"; |
| 6 | import { Stack } from "alchemy/Stack"; |
| 7 | import * as Console from "effect/Console"; |
| 8 | import * as Context from "effect/Context"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import type { Job } from "./Job.ts"; |
| 14 | |
| 15 | export class PutJobError extends Data.TaggedError("PutJobError")<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export class GetJobError extends Data.TaggedError("GetJobError")<{ |
| 21 | readonly message: string; |
| 22 | readonly cause?: unknown; |
| 23 | }> {} |
| 24 | |
| 25 | export class JobStorage extends Context.Service< |
| 26 | JobStorage, |
| 27 | { |
| 28 | putJob(job: Job): Effect.Effect<Job, PutJobError>; |
| 29 | getJob(jobId: string): Effect.Effect<Job | undefined, GetJobError>; |
| 30 | } |
| 31 | >()("JobStorage") {} |
| 32 | |
| 33 | export const JobStorageDynamoDB = Layer.provideMerge( |
| 34 | Layer.effect( |
| 35 | JobStorage, |
| 36 | Effect.gen(function* () { |
| 37 | const stack = yield* Stack; |
| 38 | const table = yield* DynamoDB.Table("JobsTable", { |
| 39 | partitionKey: "id", |
| 40 | attributes: { |
| 41 | id: "S", |
| 42 | }, |
| 43 | }); |
| 44 | const queue = yield* SQS.Queue("JobsQueue").pipe( |
| 45 | RemovalPolicy.retain(stack.stage === "prod"), |
| 46 | ); |
| 47 | |
| 48 | const getItem = yield* DynamoDB.GetItem(table); |
| 49 | const putItem = yield* DynamoDB.PutItem(table); |
| 50 | const sink = yield* SQS.QueueSink(queue); |
| 51 | |
| 52 | yield* DynamoDB.consumeTableChanges( |
| 53 | table, |
| 54 | { |
| 55 | streamViewType: "NEW_AND_OLD_IMAGES", |
| 56 | startingPosition: "LATEST", |
| 57 | batchSize: 10, |
| 58 | }, |
| 59 | Effect.fn(function* (event) { |
| 60 | yield* Stream.fromIterable(event.Records).pipe( |
| 61 | Stream.map((record) => ({ |
| 62 | MessageBody: JSON.stringify({ |
| 63 | eventName: record.eventName, |
| 64 | keys: record.dynamodb.Keys, |
| 65 | newImage: record.dynamodb.NewImage, |
| 66 | oldImage: record.dynamodb.OldImage, |
| 67 | }), |
| 68 | })), |
| 69 | Stream.run(sink), |
| 70 | ); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | const putJob = (job: Job) => |
| 75 | putItem({ |
| 76 | Item: { |
| 77 | id: { S: job.id }, |
| 78 | content: { S: job.content }, |
| 79 | }, |
| 80 | }).pipe( |
| 81 | Effect.map(() => job), |
| 82 | Effect.tapError(Console.log), |
| 83 | Effect.catchCause((cause) => |
| 84 | Effect.fail( |
| 85 | new PutJobError({ |
| 86 | message: `Failed to store job "${job.id}": ${cause}`, |
| 87 | cause, |
| 88 | }), |
| 89 | ), |
| 90 | ), |
| 91 | ); |
| 92 | |
| 93 | const getJob = (jobId: string) => |
| 94 | getItem({ |
| 95 | Key: { |
| 96 | id: { S: jobId }, |
| 97 | }, |
| 98 | }).pipe( |
| 99 | Effect.flatMap((item) => |
| 100 | item.Item |
| 101 | ? Effect.try({ |
| 102 | try: () => |
| 103 | ({ |
| 104 | id: item.Item?.id?.S ?? jobId, |
| 105 | content: item.Item?.content?.S ?? "", |
| 106 | }) as Job, |
| 107 | catch: (cause) => |
| 108 | new GetJobError({ |
| 109 | message: `Failed to parse job "${jobId}": ${cause}`, |
| 110 | cause, |
| 111 | }), |
| 112 | }) |
| 113 | : Effect.succeed(undefined), |
| 114 | ), |
| 115 | Effect.tapError(Console.log), |
| 116 | Effect.catchCause((cause) => |
| 117 | Effect.fail( |
| 118 | new GetJobError({ |
| 119 | message: `Failed to load job "${jobId}": ${cause}`, |
| 120 | cause, |
| 121 | }), |
| 122 | ), |
| 123 | ), |
| 124 | ); |
| 125 | |
| 126 | return JobStorage.of({ |
| 127 | putJob, |
| 128 | getJob, |
| 129 | }); |
| 130 | }), |
| 131 | ), |
| 132 | Layer.mergeAll(Lambda.TableEventSource, SQS.QueueSinkHttp).pipe( |
| 133 | Layer.provideMerge( |
| 134 | Layer.mergeAll(DynamoDB.GetItemHttp, DynamoDB.PutItemHttp, SQS.SendMessageBatchHttp), |
| 135 | ), |
| 136 | ), |
| 137 | ); |
| 138 | |
| 139 | export const JobStorageS3 = Layer.provideMerge( |
| 140 | Layer.effect( |
| 141 | JobStorage, |
| 142 | Effect.gen(function* () { |
| 143 | const stack = yield* Stack; |
| 144 | const bucket = yield* S3.Bucket("JobsBucket"); |
| 145 | const queue = yield* SQS.Queue("JobsQueue").pipe( |
| 146 | RemovalPolicy.retain(stack.stage === "prod"), |
| 147 | ); |
| 148 | |
| 149 | const getObject = yield* S3.GetObject(bucket); |
| 150 | const putObject = yield* S3.PutObject(bucket); |
| 151 | const sink = yield* SQS.QueueSink(queue); |
| 152 | |
| 153 | const putJob = (job: Job) => |
| 154 | putObject({ |
| 155 | Key: job.id, |
| 156 | Body: JSON.stringify(job), |
| 157 | }).pipe( |
| 158 | Effect.map(() => job), |
| 159 | Effect.tapError(Console.log), |
| 160 | Effect.catchCause((cause) => |
| 161 | Effect.fail( |
| 162 | new PutJobError({ |
| 163 | message: `Failed to store job "${job.id}": ${cause}`, |
| 164 | cause, |
| 165 | }), |
| 166 | ), |
| 167 | ), |
| 168 | ); |
| 169 | |
| 170 | const getJob = (jobId: string) => |
| 171 | getObject({ |
| 172 | Key: jobId, |
| 173 | }).pipe( |
| 174 | Effect.catchTag("NoSuchKey", () => Effect.succeed(undefined)), |
| 175 | Effect.flatMap( |
| 176 | (item) => |
| 177 | item?.Body?.pipe( |
| 178 | Stream.decodeText, |
| 179 | Stream.mkString, |
| 180 | Effect.flatMap((body) => |
| 181 | Effect.try({ |
| 182 | try: () => JSON.parse(body) as Job, |
| 183 | catch: (cause) => |
| 184 | new GetJobError({ |
| 185 | message: `Failed to parse job "${jobId}": ${cause}`, |
| 186 | cause, |
| 187 | }), |
| 188 | }), |
| 189 | ), |
| 190 | ) ?? Effect.succeed(undefined), |
| 191 | ), |
| 192 | Effect.tapError(Console.log), |
| 193 | Effect.catchCause((cause) => |
| 194 | Effect.fail( |
| 195 | new GetJobError({ |
| 196 | message: `Failed to load job "${jobId}": ${cause}`, |
| 197 | cause, |
| 198 | }), |
| 199 | ), |
| 200 | ), |
| 201 | ); |
| 202 | |
| 203 | yield* S3.consumeBucketEvents(bucket, (stream) => |
| 204 | stream.pipe( |
| 205 | Stream.flatMap((item) => Stream.fromEffect(getJob(item.key).pipe(Effect.orDie))), |
| 206 | Stream.filter((job): job is Job => job !== undefined), |
| 207 | Stream.map((job) => ({ MessageBody: JSON.stringify(job) })), |
| 208 | Stream.run(sink), |
| 209 | Effect.orDie, |
| 210 | ), |
| 211 | ); |
| 212 | |
| 213 | return JobStorage.of({ |
| 214 | putJob, |
| 215 | getJob, |
| 216 | }); |
| 217 | }), |
| 218 | ), |
| 219 | Layer.mergeAll(Lambda.BucketEventSource, SQS.QueueSinkHttp).pipe( |
| 220 | Layer.provideMerge( |
| 221 | Layer.mergeAll(S3.GetObjectHttp, S3.PutObjectHttp, SQS.SendMessageBatchHttp), |
| 222 | ), |
| 223 | ), |
| 224 | ); |