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 * as Context from "effect/Context"; |
| 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 * as Stream from "effect/Stream"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "iot-event-source-handler.ts"); |
| 11 | |
| 12 | // The MQTT topic the rule subscribes to and the fixture publishes to. |
| 13 | export const TOPIC = "alchemy/iot/eventsource"; |
| 14 | |
| 15 | // End-to-end IoT TopicRuleEventSource fixture. The Lambda: |
| 16 | // 1. Publishes MQTT messages to `TOPIC` via the IoT Publish binding |
| 17 | // (POST /publish). |
| 18 | // 2. Consumes messages routed back to it by the IoT topic rule |
| 19 | // (`consumeTopicMessages`) and forwards each into a result SQS queue via |
| 20 | // QueueSink, so the test can observe delivery out-of-band. |
| 21 | export class IoTEventSourceFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 22 | "IoTEventSourceFunction", |
| 23 | ) {} |
| 24 | |
| 25 | export class ResultQueue extends Context.Service<ResultQueue, { result: AWS.SQS.Queue }>()( |
| 26 | "IoTResultQueue", |
| 27 | ) {} |
| 28 | |
| 29 | export const ResultQueueLive = Layer.effect( |
| 30 | ResultQueue, |
| 31 | Effect.gen(function* () { |
| 32 | const result = yield* AWS.SQS.Queue("IoTEventSourceResultQueue"); |
| 33 | return { result }; |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | export default IoTEventSourceFunction.make( |
| 38 | { |
| 39 | main, |
| 40 | functionUrl: true, |
| 41 | }, |
| 42 | Effect.gen(function* () { |
| 43 | const { result } = yield* ResultQueue; |
| 44 | const sink = yield* AWS.SQS.QueueSink(result); |
| 45 | const publish = yield* AWS.IoT.Publish(TOPIC); |
| 46 | |
| 47 | yield* AWS.IoT.consumeTopicMessages(TOPIC, (messages) => |
| 48 | messages.pipe( |
| 49 | Stream.map((message) => ({ MessageBody: JSON.stringify(message) })), |
| 50 | Stream.run(sink), |
| 51 | Effect.orDie, |
| 52 | ), |
| 53 | ); |
| 54 | |
| 55 | const resultQueueUrl = yield* result.queueUrl; |
| 56 | |
| 57 | return { |
| 58 | fetch: Effect.gen(function* () { |
| 59 | const request = yield* HttpServerRequest; |
| 60 | const pathname = new URL(request.originalUrl).pathname; |
| 61 | |
| 62 | if (request.method === "GET" && pathname === "/ready") { |
| 63 | return yield* HttpServerResponse.json({ |
| 64 | ok: true, |
| 65 | resultQueueUrl: yield* resultQueueUrl, |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | if (request.method === "POST" && pathname === "/publish") { |
| 70 | const body = (yield* request.json) as { marker: string }; |
| 71 | yield* publish({ |
| 72 | topic: TOPIC, |
| 73 | payload: JSON.stringify({ marker: body.marker }), |
| 74 | }); |
| 75 | return yield* HttpServerResponse.json({ ok: true }); |
| 76 | } |
| 77 | |
| 78 | return yield* HttpServerResponse.json( |
| 79 | { error: "Not found", method: request.method, pathname }, |
| 80 | { status: 404 }, |
| 81 | ); |
| 82 | }).pipe(Effect.orDie), |
| 83 | }; |
| 84 | }).pipe( |
| 85 | Effect.provide( |
| 86 | Layer.provideMerge( |
| 87 | Layer.mergeAll(AWS.Lambda.TopicRuleEventSource, AWS.SQS.QueueSinkHttp, AWS.IoT.PublishHttp), |
| 88 | Layer.mergeAll(AWS.SQS.SendMessageBatchHttp, ResultQueueLive), |
| 89 | ), |
| 90 | ), |
| 91 | ), |
| 92 | ); |