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

main
ts 142 lines 4.58 KB
Raw
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 /**
11 * Shared infrastructure for the BusSink fixture:
12 * - a custom event bus the sink publishes to
13 * - an SQS queue routed from the bus (via `toQueue`) so the test can observe
14 * delivered events out-of-band via `sqs.receiveMessage`.
15 */
16 export class BusSinkInfra extends Context.Service<
17 BusSinkInfra,
18 {
19 bus: AWS.EventBridge.EventBus;
20 queue: AWS.SQS.Queue;
21 }
22 >()("EventBridgeBusSinkInfra") {}
23
24 export const BusSinkInfraLive = Layer.effect(
25 BusSinkInfra,
26 Effect.gen(function* () {
27 const bus = yield* AWS.EventBridge.EventBus("BusSinkBus", {
28 name: "alchemy-test-eb-bus-sink",
29 });
30 // The delivery policy is a plain prop (not a `toQueue` binding) so the
31 // Queue never depends on the Rule — Queue<->Rule would otherwise form a
32 // cycle neither resource can precreate out of. A queue policy only ever
33 // governs the queue it is attached to, so `Resource: "*"` is safely
34 // scoped for this fixture.
35 const queue = yield* AWS.SQS.Queue("BusSinkQueue", {
36 policy: {
37 Version: "2012-10-17",
38 Statement: [
39 {
40 Effect: "Allow",
41 Principal: { Service: "events.amazonaws.com" },
42 Action: "sqs:SendMessage",
43 Resource: "*",
44 },
45 ],
46 },
47 });
48 // Route every sink-published event to the queue for out-of-band
49 // observation.
50 yield* AWS.EventBridge.Rule("BusSinkRoute", {
51 eventBusName: bus.eventBusName,
52 eventPattern: { source: ["alchemy.test.bussink"] },
53 targets: [{ Id: "BusSinkQueueTarget", Arn: queue.queueArn as any }],
54 });
55 return { bus, queue };
56 }),
57 );
58
59 export class BusSinkFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
60 "BusSinkFunction",
61 ) {}
62
63 export const BusSinkFunctionLive = BusSinkFunction.make(
64 {
65 main: import.meta.url,
66 functionUrl: true,
67 // The sink's bounded partial-failure retry can sleep up to ~6s, which
68 // exceeds Lambda's 3s default timeout (see PATTERNS §7).
69 timeout: Duration.seconds(30),
70 },
71 Effect.gen(function* () {
72 const { bus, queue } = yield* BusSinkInfra;
73 const sink = yield* AWS.EventBridge.BusSink(bus);
74 const queueUrl = yield* queue.queueUrl;
75
76 return {
77 fetch: Effect.gen(function* () {
78 const request = yield* HttpServerRequest;
79 const pathname = new URL(request.originalUrl).pathname;
80
81 if (request.method === "GET" && pathname === "/ready") {
82 return yield* HttpServerResponse.json({
83 ok: true,
84 queueUrl: yield* queueUrl,
85 });
86 }
87
88 if (request.method === "POST" && pathname === "/sink") {
89 const body = (yield* request.json) as {
90 markers: string[];
91 includeMalformed?: boolean;
92 };
93
94 const entries: AWS.EventBridge.BusSinkEntry[] = body.markers.map((marker) => ({
95 Source: "alchemy.test.bussink",
96 DetailType: "BusSinkEvent",
97 Detail: JSON.stringify({ marker }),
98 }));
99 if (body.includeMalformed) {
100 // Detail must be valid JSON. EventBridge rejects this entry
101 // per-entry (ErrorCode: MalformedDetail) without failing the
102 // batch — the sink must drop it and deliver the rest.
103 entries.splice(1, 0, {
104 Source: "alchemy.test.bussink",
105 DetailType: "BusSinkEvent",
106 Detail: "{ this is not json",
107 });
108 }
109
110 yield* Stream.fromIterable(entries).pipe(Stream.run(sink));
111
112 return yield* HttpServerResponse.json({
113 ok: true,
114 count: entries.length,
115 });
116 }
117
118 return yield* HttpServerResponse.json(
119 { error: "Not found", method: request.method, pathname },
120 { status: 404 },
121 );
122 }).pipe(
123 Effect.catch((error) =>
124 Effect.succeed(
125 HttpServerResponse.text(`Internal server error: ${String(error)}`, {
126 status: 500,
127 }),
128 ),
129 ),
130 ),
131 };
132 }).pipe(
133 Effect.provide(
134 Layer.provideMerge(
135 Layer.mergeAll(BusSinkInfraLive, AWS.EventBridge.BusSinkHttp),
136 Layer.mergeAll(AWS.EventBridge.PutEventsHttp),
137 ),
138 ),
139 ),
140 );
141
142 export default BusSinkFunctionLive;