The best way to control Claude Code, Codex and more from any computer or phone

main
ts 137 lines 4.86 KB
Raw
1 import * as DynamoDB from "@alchemy.run/aws/DynamoDB";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as S3 from "@alchemy.run/aws/S3";
4 import * as SQS from "@alchemy.run/aws/SQS";
5 import * as Config from "effect/Config";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import * as Stream from "effect/Stream";
11 import { LAMBDA_MARKER } from "./lambda/marker.ts";
12
13 /**
14 * The AWS half of the stack: one Lambda owning an S3 bucket, a DynamoDB
15 * table, and an SQS queue it also consumes. Under `alchemy dev` all four
16 * land in the floci emulator and the function URL is served on
17 * `*.lambda-url.us-east-1.localhost:4566`.
18 *
19 * The stack imports this module (`main: import.meta.url`), so editing it —
20 * or `./lambda/marker.ts` — takes the full-restart reload path and the dev
21 * provider hot-swaps the function's code inside the emulator.
22 *
23 * `EchoWorker`'s sibling `ApiWorker` reaches this function URL from local
24 * workerd, which is the cross-cloud hop the stress suite pins.
25 *
26 * Routes:
27 * - `GET /` → marker + a `Config`-provided env var
28 * - `GET /s3` → PutObject/GetObject roundtrip
29 * - `GET /dynamo` → PutItem/GetItem roundtrip
30 * - `POST /queue/send` → SendMessage over the binding
31 * - `GET /queue/messages` → what the queue consumer recorded
32 */
33 export default class ApiFunction extends Lambda.Function<ApiFunction>()(
34 "ApiFunction",
35 {
36 main: import.meta.url,
37 functionUrl: true,
38 memorySize: 512,
39 env: { LAMBDA_VARIABLE: "lambda-variable-v1" },
40 },
41 Effect.gen(function* () {
42 const bucket = yield* S3.Bucket("StressBucket", { forceDestroy: true });
43 const table = yield* DynamoDB.Table("StressTable", {
44 partitionKey: "id",
45 attributes: { id: "S" },
46 });
47 const queue = yield* SQS.Queue("StressQueue");
48
49 const getObject = yield* S3.GetObject(bucket);
50 const putObject = yield* S3.PutObject(bucket);
51 const getItem = yield* DynamoDB.GetItem(table);
52 const putItem = yield* DynamoDB.PutItem(table);
53 const sendMessage = yield* SQS.SendMessage(queue);
54 // <<LAMBDA_BINDINGS>>
55 // <</LAMBDA_BINDINGS>>
56
57 // Produce → deliver → consume, so the suite can prove the event-source
58 // glue survives every reload it inflicts on the stack.
59 yield* SQS.consumeQueueMessages(
60 queue,
61 Effect.fn(function* (event) {
62 for (const record of event.Records) {
63 const parsed = JSON.parse(record.body) as { id: string };
64 yield* putItem({
65 Item: { id: { S: `msg:${parsed.id}` }, body: { S: record.body } },
66 });
67 }
68 }),
69 );
70
71 return {
72 fetch: Effect.gen(function* () {
73 const request = yield* HttpServerRequest;
74 const url = new URL(request.originalUrl);
75
76 if (url.pathname === "/") {
77 const variable = yield* Config.String("LAMBDA_VARIABLE");
78 return yield* HttpServerResponse.json({
79 marker: LAMBDA_MARKER,
80 variable,
81 });
82 }
83
84 if (url.pathname === "/s3") {
85 yield* putObject({ Key: "hello.txt", Body: "hello from s3" });
86 const object = yield* getObject({ Key: "hello.txt" });
87 const text = yield* (
88 object.Body?.pipe(Stream.decodeText, Stream.mkString) ?? Effect.succeed("")
89 );
90 return yield* HttpServerResponse.json({ text });
91 }
92
93 if (url.pathname === "/dynamo") {
94 yield* putItem({
95 Item: { id: { S: "hello" }, content: { S: "hello from dynamo" } },
96 });
97 const item = yield* getItem({ Key: { id: { S: "hello" } } });
98 return yield* HttpServerResponse.json({
99 text: item.Item?.content?.S ?? null,
100 });
101 }
102
103 if (url.pathname === "/queue/send" && request.method === "POST") {
104 const body = yield* request.text;
105 yield* sendMessage({ MessageBody: body });
106 return yield* HttpServerResponse.json({ sent: true });
107 }
108
109 if (url.pathname === "/queue/messages") {
110 const id = url.searchParams.get("id") ?? "";
111 const item = yield* getItem({ Key: { id: { S: `msg:${id}` } } });
112 return yield* HttpServerResponse.json({
113 body: item.Item?.body?.S ?? null,
114 });
115 }
116
117 // <<LAMBDA_ROUTES>>
118 // <</LAMBDA_ROUTES>>
119
120 return HttpServerResponse.text("Not found", { status: 404 });
121 }).pipe(Effect.orDie),
122 };
123 }).pipe(
124 Effect.provide(
125 Layer.mergeAll(
126 Lambda.QueueEventSource,
127 S3.GetObjectHttp,
128 S3.PutObjectHttp,
129 DynamoDB.GetItemHttp,
130 DynamoDB.PutItemHttp,
131 SQS.SendMessageHttp,
132 // <<LAMBDA_LAYERS>>
133 // <</LAMBDA_LAYERS>>
134 ),
135 ),
136 ),
137 ) {}