The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as S3 from "@alchemy.run/aws/S3"; |
| 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 | |
| 9 | export class EffectWebFunction extends Lambda.WebFunction<Lambda.WebFunction>()( |
| 10 | "EffectWebFunction", |
| 11 | ) {} |
| 12 | |
| 13 | export default EffectWebFunction.make( |
| 14 | { main: import.meta.url, authType: "ApplicationManaged" }, |
| 15 | Effect.gen(function* () { |
| 16 | const bucket = yield* S3.Bucket("WebFunctionBucket", { forceDestroy: true }); |
| 17 | const putObject = yield* S3.PutObject(bucket); |
| 18 | const getObject = yield* S3.GetObject(bucket); |
| 19 | |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const request = yield* HttpServerRequest; |
| 23 | const url = new URL(request.originalUrl, "http://localhost"); |
| 24 | |
| 25 | if (url.pathname === "/stream") { |
| 26 | // Three chunks 600 ms apart: a buffered bridge would deliver them |
| 27 | // all at once after ~1.2 s. |
| 28 | return HttpServerResponse.stream( |
| 29 | Stream.make("one\n", "two\n", "three\n").pipe( |
| 30 | Stream.mapEffect((chunk, i) => |
| 31 | i === 0 ? Effect.succeed(chunk) : Effect.as(Effect.sleep("600 millis"), chunk), |
| 32 | ), |
| 33 | Stream.encodeText, |
| 34 | ), |
| 35 | { contentType: "text/plain" }, |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | if (url.pathname === "/s3") { |
| 40 | const key = url.searchParams.get("key") ?? "web-function.txt"; |
| 41 | yield* putObject({ Key: key, Body: `written by ${process.env.ALCHEMY_STAGE}` }); |
| 42 | const object = yield* getObject({ Key: key }); |
| 43 | const body = yield* Stream.mkString(Stream.decodeText(object.Body!)); |
| 44 | return yield* HttpServerResponse.json({ key, body }); |
| 45 | } |
| 46 | |
| 47 | if (url.pathname === "/runtime") { |
| 48 | return yield* HttpServerResponse.json({ |
| 49 | endpoint: process.env.AWS_LAMBDA_HTTP_ENDPOINT, |
| 50 | requestId: request.headers["lambda-web-request-id"], |
| 51 | hasCredentials: process.env.AWS_ACCESS_KEY_ID !== undefined, |
| 52 | region: process.env.AWS_REGION, |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | return HttpServerResponse.text("hello from effect"); |
| 57 | }).pipe(Effect.orDie), |
| 58 | }; |
| 59 | }).pipe(Effect.provide(Layer.mergeAll(S3.PutObjectHttp, S3.GetObjectHttp))), |
| 60 | ); |