The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ServerHost } from "alchemy/Runtime/Process"; |
| 2 | import { toWireSeconds } from "alchemy/Util/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Exit from "effect/Exit"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import { processBatch, traceparentLink } from "../internal/EventSourceBatch.ts"; |
| 8 | import * as SQS from "../SQS/index.ts"; |
| 9 | |
| 10 | export const SQSQueueEventSource = Layer.effect( |
| 11 | SQS.QueueEventSource, |
| 12 | Effect.gen(function* () { |
| 13 | const { run } = yield* ServerHost; |
| 14 | |
| 15 | const ReceiveMessage = yield* SQS.ReceiveMessage; |
| 16 | const DeleteMessageBatch = yield* SQS.DeleteMessageBatch; |
| 17 | |
| 18 | // Cast below: the service type erases `process`'s requirements, so every |
| 19 | // event source implementation casts its client. |
| 20 | return Effect.fn(function* <E = never, Req = never>( |
| 21 | queue: SQS.Queue, |
| 22 | props: SQS.MessagesProps, |
| 23 | process: SQS.QueueMessagesHandler<E, Req>, |
| 24 | ) { |
| 25 | const QueueArn = yield* queue.queueArn; |
| 26 | |
| 27 | const receiveMessage = yield* ReceiveMessage(queue); |
| 28 | const deleteMessageBatch = yield* DeleteMessageBatch(queue); |
| 29 | |
| 30 | yield* run( |
| 31 | Effect.forever( |
| 32 | Effect.gen(function* () { |
| 33 | const queueArn = yield* QueueArn; |
| 34 | const region = queueArn.split(":")[3]!; |
| 35 | const result = yield* receiveMessage({ |
| 36 | MaxNumberOfMessages: props.maxNumberOfMessages ?? props.batchSize ?? 10, |
| 37 | MessageAttributeNames: ["All"], |
| 38 | MessageSystemAttributeNames: ["All"], |
| 39 | WaitTimeSeconds: |
| 40 | toWireSeconds(props.waitTime) ?? toWireSeconds(props.maximumBatchingWindow), |
| 41 | }); |
| 42 | |
| 43 | const messages = result.Messages ?? []; |
| 44 | if (messages.length === 0) return; |
| 45 | |
| 46 | const records: SQS.SQSRecord[] = messages.map((msg) => ({ |
| 47 | messageId: msg.MessageId!, |
| 48 | receiptHandle: msg.ReceiptHandle!, |
| 49 | body: msg.Body!, |
| 50 | attributes: { |
| 51 | ApproximateReceiveCount: msg.Attributes?.ApproximateReceiveCount!, |
| 52 | SentTimestamp: msg.Attributes?.SentTimestamp!, |
| 53 | SenderId: msg.Attributes?.SenderId!, |
| 54 | ApproximateFirstReceiveTimestamp: msg.Attributes?.ApproximateFirstReceiveTimestamp!, |
| 55 | ...(msg.Attributes?.MessageGroupId |
| 56 | ? { MessageGroupId: msg.Attributes.MessageGroupId } |
| 57 | : {}), |
| 58 | ...(msg.Attributes?.MessageDeduplicationId |
| 59 | ? { MessageDeduplicationId: msg.Attributes.MessageDeduplicationId } |
| 60 | : {}), |
| 61 | ...(msg.Attributes?.SequenceNumber |
| 62 | ? { SequenceNumber: msg.Attributes.SequenceNumber } |
| 63 | : {}), |
| 64 | ...(msg.Attributes?.AWSTraceHeader |
| 65 | ? { AWSTraceHeader: msg.Attributes.AWSTraceHeader } |
| 66 | : {}), |
| 67 | }, |
| 68 | messageAttributes: Object.fromEntries( |
| 69 | Object.entries(msg.MessageAttributes ?? {}).map(([name, value]) => [ |
| 70 | name, |
| 71 | { |
| 72 | dataType: value?.DataType ?? "String", |
| 73 | stringValue: value?.StringValue, |
| 74 | }, |
| 75 | ]), |
| 76 | ), |
| 77 | md5OfBody: msg.MD5OfBody!, |
| 78 | eventSource: "aws:sqs", |
| 79 | eventSourceARN: queueArn, |
| 80 | awsRegion: region, |
| 81 | })); |
| 82 | |
| 83 | // Same contract as the Lambda event source: the handler's |
| 84 | // `batchItemFailures` stay on the queue (visible again after the |
| 85 | // visibility timeout); a failure leaves the whole batch. |
| 86 | const response = yield* process({ Records: records }).pipe( |
| 87 | Effect.map((response) => response ?? { batchItemFailures: [] }), |
| 88 | processBatch({ |
| 89 | system: "aws_sqs", |
| 90 | destination: queueArn.split(":")[5]!, |
| 91 | count: records.length, |
| 92 | links: records.flatMap((record) => |
| 93 | Option.toArray( |
| 94 | traceparentLink(record.messageAttributes.traceparent?.stringValue), |
| 95 | ), |
| 96 | ), |
| 97 | }), |
| 98 | Effect.exit, |
| 99 | ); |
| 100 | if (Exit.isFailure(response)) return; |
| 101 | |
| 102 | const failed = new Set( |
| 103 | response.value.batchItemFailures.map((failure) => failure.itemIdentifier), |
| 104 | ); |
| 105 | const succeeded = records.filter((record) => !failed.has(record.messageId)); |
| 106 | if (succeeded.length === 0) return; |
| 107 | |
| 108 | yield* deleteMessageBatch({ |
| 109 | Entries: succeeded.map((record) => ({ |
| 110 | Id: record.messageId, |
| 111 | ReceiptHandle: record.receiptHandle, |
| 112 | })), |
| 113 | }); |
| 114 | }), |
| 115 | ).pipe(Effect.orDie), |
| 116 | ); |
| 117 | }) as SQS.QueueEventSourceService; |
| 118 | }), |
| 119 | ); |