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

main
ts 147 lines 4.53 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
9 // Bindings fixture: a standalone SQS→SQS pipe (no Lambda target, so no
10 // circularity) plus a Lambda that exercises the four Pipes runtime bindings
11 // against it — DescribePipe / ListPipes / StopPipe / StartPipe.
12 export class PipesBindingsFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
13 "PipesBindingsFunction",
14 ) {}
15
16 export class BoundPipe extends Context.Service<BoundPipe, { pipe: AWS.Pipes.Pipe }>()(
17 "BoundPipe",
18 ) {}
19
20 export const BoundPipeLive = Layer.effect(
21 BoundPipe,
22 Effect.gen(function* () {
23 const source = yield* AWS.SQS.Queue("BindingsSourceQueue");
24 const target = yield* AWS.SQS.Queue("BindingsTargetQueue");
25 const role = yield* AWS.IAM.Role("BindingsPipeRole", {
26 assumeRolePolicyDocument: {
27 Version: "2012-10-17",
28 Statement: [
29 {
30 Effect: "Allow",
31 Principal: { Service: "pipes.amazonaws.com" },
32 Action: ["sts:AssumeRole"],
33 },
34 ],
35 },
36 inlinePolicies: {
37 PipeAccess: {
38 Version: "2012-10-17",
39 Statement: [
40 {
41 Effect: "Allow",
42 Action: ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"],
43 Resource: [source.queueArn],
44 },
45 {
46 Effect: "Allow",
47 Action: ["sqs:SendMessage"],
48 Resource: [target.queueArn],
49 },
50 ],
51 },
52 },
53 });
54 const pipe = yield* AWS.Pipes.Pipe("BindingsPipe", {
55 source: source.queueArn,
56 target: target.queueArn,
57 roleArn: role.roleArn,
58 sourceParameters: {
59 SqsQueueParameters: { BatchSize: 1 },
60 },
61 });
62 return { pipe };
63 }),
64 );
65
66 export default PipesBindingsFunction.make(
67 {
68 main: import.meta.url,
69 functionUrl: true,
70 timeout: Duration.seconds(30),
71 },
72 Effect.gen(function* () {
73 const { pipe } = yield* BoundPipe;
74
75 const describePipe = yield* AWS.Pipes.DescribePipe(pipe);
76 const listPipes = yield* AWS.Pipes.ListPipes();
77 const stopPipe = yield* AWS.Pipes.StopPipe(pipe);
78 const startPipe = yield* AWS.Pipes.StartPipe(pipe);
79
80 const bound = { describePipe, listPipes, stopPipe, startPipe };
81
82 const pipeName = yield* pipe.pipeName;
83
84 return {
85 fetch: Effect.gen(function* () {
86 const request = yield* HttpServerRequest;
87 const url = new URL(request.originalUrl);
88 const pathname = url.pathname;
89
90 if (request.method === "GET" && pathname === "/bindings") {
91 return yield* HttpServerResponse.json({
92 bound: Object.keys(bound),
93 });
94 }
95
96 if (request.method === "GET" && pathname === "/describe") {
97 const described = yield* describePipe();
98 return yield* HttpServerResponse.json({
99 name: described.Name,
100 currentState: described.CurrentState,
101 desiredState: described.DesiredState,
102 });
103 }
104
105 if (request.method === "GET" && pathname === "/list") {
106 const { Pipes } = yield* listPipes({
107 NamePrefix: yield* pipeName,
108 });
109 return yield* HttpServerResponse.json({
110 names: (Pipes ?? []).flatMap((p) => (p.Name !== undefined ? [p.Name] : [])),
111 });
112 }
113
114 if (request.method === "GET" && pathname === "/stop") {
115 const response = yield* stopPipe();
116 return yield* HttpServerResponse.json({
117 desiredState: response.DesiredState,
118 currentState: response.CurrentState,
119 });
120 }
121
122 if (request.method === "GET" && pathname === "/start") {
123 const response = yield* startPipe();
124 return yield* HttpServerResponse.json({
125 desiredState: response.DesiredState,
126 currentState: response.CurrentState,
127 });
128 }
129
130 return yield* HttpServerResponse.json(
131 { error: "Not found", method: request.method, pathname },
132 { status: 404 },
133 );
134 }).pipe(Effect.orDie),
135 };
136 }).pipe(
137 Effect.provide(
138 Layer.mergeAll(
139 AWS.Pipes.DescribePipeHttp,
140 AWS.Pipes.ListPipesHttp,
141 AWS.Pipes.StopPipeHttp,
142 AWS.Pipes.StartPipeHttp,
143 BoundPipeLive,
144 ),
145 ),
146 ),
147 );