@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 86 lines 3 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 * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7
8 // End-to-end Pipe fixture: an EventBridge Pipe (created at deploy time via
9 // the `AWS.Pipes.from(...).toLambda(...)` builder, which synthesizes the
10 // pipes.amazonaws.com execution role) delivers batches from a source SQS
11 // queue to this Lambda target. The runtime listener forwards each record
12 // body into a sink queue so the test can observe delivery out-of-band
13 // (mirrors test/AWS/SQS/event-source-handler.ts).
14 export class PipeTargetFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
15 "PipeTargetFunction",
16 ) {}
17
18 export class PipeQueues extends Context.Service<
19 PipeQueues,
20 {
21 source: AWS.SQS.Queue;
22 sink: AWS.SQS.Queue;
23 }
24 >()("PipeQueues") {}
25
26 export const PipeQueuesLive = Layer.effect(
27 PipeQueues,
28 Effect.gen(function* () {
29 const source = yield* AWS.SQS.Queue("PipeSourceQueue");
30 const sink = yield* AWS.SQS.Queue("PipeSinkQueue");
31 return { source, sink };
32 }),
33 );
34
35 // EventBridge Pipes invokes a Lambda target with the raw batch: a JSON
36 // array of source records (NOT the `{ Records: [...] }` Lambda event-source
37 // envelope). SQS records carry `eventSource: "aws:sqs"`.
38 const isPipeSqsBatch = (event: unknown): event is Array<{ body: string; messageId: string }> =>
39 Array.isArray(event) &&
40 event.length > 0 &&
41 (event[0] as { eventSource?: string })?.eventSource === "aws:sqs";
42
43 export default PipeTargetFunction.make(
44 {
45 main: import.meta.url,
46 functionUrl: true,
47 timeout: Duration.seconds(30),
48 },
49 Effect.gen(function* () {
50 const { source, sink } = yield* PipeQueues;
51 const sendMessage = yield* AWS.SQS.SendMessage(sink);
52 const host = yield* AWS.Lambda.Function;
53
54 // Deploy-time: wire the pipe. Skipped at runtime — the deployed Lambda
55 // re-executes this init effect but must not touch the deploy plane.
56 if (!globalThis.__ALCHEMY_RUNTIME__) {
57 yield* AWS.Pipes.from(source, { batchSize: 1 }).toLambda(host);
58 }
59
60 // Runtime: forward every pipe-delivered record body into the sink queue.
61 yield* host.listen(
62 Effect.gen(function* () {
63 return (event: unknown) => {
64 if (isPipeSqsBatch(event)) {
65 return Effect.forEach(event, (record) => sendMessage({ MessageBody: record.body }), {
66 discard: true,
67 }).pipe(Effect.orDie);
68 }
69 };
70 }),
71 );
72
73 const sourceQueueUrl = yield* source.queueUrl;
74 const sinkQueueUrl = yield* sink.queueUrl;
75
76 return {
77 fetch: Effect.gen(function* () {
78 return yield* HttpServerResponse.json({
79 ok: true,
80 sourceQueueUrl: yield* sourceQueueUrl,
81 sinkQueueUrl: yield* sinkQueueUrl,
82 });
83 }).pipe(Effect.orDie),
84 };
85 }).pipe(Effect.provide(Layer.mergeAll(AWS.SQS.SendMessageHttp, PipeQueuesLive))),
86 );