The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Namespace from "alchemy/Namespace"; |
| 2 | import type lambda from "aws-lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import { processBatch, traceparentLink } from "../internal/EventSourceBatch.ts"; |
| 7 | import type { Queue } from "../SQS/Queue.ts"; |
| 8 | import { |
| 9 | QueueEventSource as SQSQueueEventSource, |
| 10 | type QueueEventSourceProps, |
| 11 | type QueueEventSourceService, |
| 12 | type QueueMessagesHandler, |
| 13 | type SQSBatchResponse, |
| 14 | type SQSEvent, |
| 15 | } from "../SQS/QueueEventSource.ts"; |
| 16 | import { EventSourceMapping } from "./EventSourceMapping.ts"; |
| 17 | import * as Lambda from "./Function.ts"; |
| 18 | |
| 19 | export const isSQSEvent = (event: any): event is lambda.SQSEvent => |
| 20 | Array.isArray(event?.Records) && |
| 21 | event.Records.length > 0 && |
| 22 | event.Records[0].eventSource === "aws:sqs"; |
| 23 | |
| 24 | /** @binding */ |
| 25 | export const QueueEventSource = Layer.effect( |
| 26 | SQSQueueEventSource, |
| 27 | Effect.gen(function* () { |
| 28 | const host = yield* Lambda.eventSourceHost("AWS.Lambda.QueueEventSource"); |
| 29 | const Mapping = yield* EventSourceMapping; |
| 30 | |
| 31 | return Effect.fn(function* <E = never, Req = never>( |
| 32 | queue: Queue, |
| 33 | props: QueueEventSourceProps, |
| 34 | process: QueueMessagesHandler<E, Req>, |
| 35 | ) { |
| 36 | // Deploy-time: grant IAM and create the event-source mapping. Skipped once |
| 37 | // running inside the deployed Function (the global guard), where the only |
| 38 | // work is registering the runtime handler below. Namespaced under the host |
| 39 | // so the mapping's logical identity matches the previous Binding.Policy. |
| 40 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 41 | yield* Namespace.push( |
| 42 | host.LogicalId, |
| 43 | Effect.gen(function* () { |
| 44 | yield* host.bind`Allow(${host}, AWS.Lambda.QueueEventSource(${queue}))`({ |
| 45 | policyStatements: [ |
| 46 | { |
| 47 | Effect: "Allow", |
| 48 | Action: ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"], |
| 49 | Resource: [queue.queueArn], |
| 50 | }, |
| 51 | ], |
| 52 | }); |
| 53 | |
| 54 | yield* Mapping(`${queue.LogicalId}-EventSource`, { |
| 55 | functionName: Lambda.qualifiedNameOf(host), |
| 56 | eventSourceArn: queue.queueArn, |
| 57 | batchSize: props.batchSize, |
| 58 | maximumBatchingWindow: props.maximumBatchingWindow, |
| 59 | enabled: true, |
| 60 | }); |
| 61 | }), |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | yield* host.listen( |
| 66 | Effect.gen(function* () { |
| 67 | return (event: any) => { |
| 68 | if (isSQSEvent(event)) { |
| 69 | // A failure fails the invocation, so Lambda retries the whole batch. |
| 70 | return processQueueBatch(event, process).pipe(Effect.orDie); |
| 71 | } |
| 72 | }; |
| 73 | }), |
| 74 | ); |
| 75 | }) as QueueEventSourceService; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | /** |
| 80 | * Run one SQS batch through the user's handler inside a CONSUMER span and |
| 81 | * normalize its result into Lambda's partial-batch response. A failure |
| 82 | * propagates so the invocation errors and Lambda retries the whole batch. |
| 83 | */ |
| 84 | const processQueueBatch = <E, Req>( |
| 85 | event: SQSEvent, |
| 86 | process: QueueMessagesHandler<E, Req>, |
| 87 | ): Effect.Effect<SQSBatchResponse, E, Req> => |
| 88 | process(event).pipe( |
| 89 | Effect.map((response) => response ?? { batchItemFailures: [] }), |
| 90 | processBatch({ |
| 91 | system: "aws_sqs", |
| 92 | destination: event.Records[0]?.eventSourceARN.split(":")[5] ?? "unknown", |
| 93 | count: event.Records.length, |
| 94 | links: event.Records.flatMap((record) => |
| 95 | Option.toArray(traceparentLink(record.messageAttributes?.traceparent?.stringValue)), |
| 96 | ), |
| 97 | }), |
| 98 | ); |