The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | |
| 10 | export class FirehoseApiFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 11 | "FirehoseApiFunction", |
| 12 | ) {} |
| 13 | |
| 14 | export class BucketAndDeliveryStream extends Context.Service< |
| 15 | BucketAndDeliveryStream, |
| 16 | { |
| 17 | bucket: AWS.S3.Bucket; |
| 18 | deliveryStream: AWS.Firehose.DeliveryStream; |
| 19 | } |
| 20 | >()("BucketAndDeliveryStream") {} |
| 21 | |
| 22 | export const BucketAndDeliveryStreamLive = Layer.effect( |
| 23 | BucketAndDeliveryStream, |
| 24 | Effect.gen(function* () { |
| 25 | const bucket = yield* AWS.S3.Bucket("FirehoseFixtureBucket", { |
| 26 | forceDestroy: true, |
| 27 | }); |
| 28 | |
| 29 | const deliveryStream = yield* AWS.Firehose.DeliveryStream("FixtureDeliveryStream", { |
| 30 | destination: { |
| 31 | bucketArn: bucket.bucketArn, |
| 32 | prefix: "records/", |
| 33 | // Direct S3 supports zero buffering. Keep the binding fixture at |
| 34 | // zero so end-to-end delivery can be proven inside the normal test |
| 35 | // budget instead of imposing Firehose's one-minute flush delay. |
| 36 | bufferingInterval: "0 seconds", |
| 37 | bufferingSizeInMBs: 1, |
| 38 | }, |
| 39 | tags: { fixture: "firehose-bindings" }, |
| 40 | }); |
| 41 | |
| 42 | return { bucket, deliveryStream }; |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | export const FirehoseApiFunctionLive = FirehoseApiFunction.make( |
| 47 | { |
| 48 | main: import.meta.url, |
| 49 | functionUrl: true, |
| 50 | timeout: Duration.seconds(30), |
| 51 | }, |
| 52 | Effect.gen(function* () { |
| 53 | const { deliveryStream } = yield* BucketAndDeliveryStream; |
| 54 | |
| 55 | const putRecord = yield* AWS.Firehose.PutRecord(deliveryStream); |
| 56 | const putRecordBatch = yield* AWS.Firehose.PutRecordBatch(deliveryStream); |
| 57 | const sink = yield* AWS.Firehose.DeliveryStreamSink(deliveryStream); |
| 58 | const listDeliveryStreams = yield* AWS.Firehose.ListDeliveryStreams(); |
| 59 | |
| 60 | return { |
| 61 | fetch: Effect.gen(function* () { |
| 62 | const request = yield* HttpServerRequest; |
| 63 | const url = new URL(request.originalUrl); |
| 64 | const pathname = url.pathname; |
| 65 | |
| 66 | if (request.method === "GET" && pathname === "/ready") { |
| 67 | return yield* HttpServerResponse.json({ ok: true }); |
| 68 | } |
| 69 | |
| 70 | if (request.method === "POST" && pathname === "/put-record") { |
| 71 | const body = (yield* request.json) as { data: string }; |
| 72 | return yield* HttpServerResponse.json( |
| 73 | yield* putRecord({ |
| 74 | Record: { Data: new TextEncoder().encode(`${body.data}\n`) }, |
| 75 | }), |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | if (request.method === "POST" && pathname === "/put-record-batch") { |
| 80 | const body = (yield* request.json) as { records: string[] }; |
| 81 | return yield* HttpServerResponse.json( |
| 82 | yield* putRecordBatch({ |
| 83 | Records: body.records.map((data) => ({ |
| 84 | Data: new TextEncoder().encode(`${data}\n`), |
| 85 | })), |
| 86 | }), |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | if (request.method === "GET" && pathname === "/list-streams") { |
| 91 | return yield* HttpServerResponse.json(yield* listDeliveryStreams({ Limit: 100 })); |
| 92 | } |
| 93 | |
| 94 | if (request.method === "POST" && pathname === "/sink") { |
| 95 | const body = (yield* request.json) as { records: string[] }; |
| 96 | // Sinks are request-scoped: Stream.run drains fully (including the |
| 97 | // engine's bounded partial-failure retries) before the handler |
| 98 | // responds. |
| 99 | yield* Stream.fromIterable(body.records).pipe( |
| 100 | Stream.map((data) => ({ |
| 101 | Data: new TextEncoder().encode(`${data}\n`), |
| 102 | })), |
| 103 | Stream.run(sink), |
| 104 | ); |
| 105 | return yield* HttpServerResponse.json({ |
| 106 | ok: true, |
| 107 | count: body.records.length, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | return yield* HttpServerResponse.json( |
| 112 | { error: "Not found", method: request.method, pathname }, |
| 113 | { status: 404 }, |
| 114 | ); |
| 115 | }).pipe(Effect.orDie), |
| 116 | }; |
| 117 | }).pipe( |
| 118 | Effect.provide( |
| 119 | Layer.provideMerge( |
| 120 | // The sink layer consumes the PutRecordBatch binding, so the op |
| 121 | // layers are provided *into* the sink group (and merged out for the |
| 122 | // fetch routes that call them directly). |
| 123 | Layer.mergeAll(AWS.Firehose.DeliveryStreamSinkHttp, BucketAndDeliveryStreamLive), |
| 124 | Layer.mergeAll( |
| 125 | AWS.Firehose.PutRecordHttp, |
| 126 | AWS.Firehose.PutRecordBatchHttp, |
| 127 | AWS.Firehose.ListDeliveryStreamsHttp, |
| 128 | ), |
| 129 | ), |
| 130 | ), |
| 131 | ), |
| 132 | // Re-merge so the deploying Stack can `yield* BucketAndDeliveryStream` and |
| 133 | // expose the stream/bucket names as deploy-time outputs. Reusing the same |
| 134 | // `BucketAndDeliveryStreamLive` reference keeps it a single shared pair. |
| 135 | ).pipe(Layer.provideMerge(BucketAndDeliveryStreamLive)); |
| 136 | |
| 137 | export default FirehoseApiFunctionLive; |