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

main
ts 75 lines 2.67 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Rbin from "@alchemy.run/aws/Rbin";
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 path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class RbinTestFunction extends Lambda.Function<Lambda.Function>()("RbinTestFunction") {}
13
14 export default RbinTestFunction.make(
15 {
16 main,
17 functionUrl: true,
18 timeout: Duration.seconds(30),
19 },
20 Effect.gen(function* () {
21 // The retention rule the rule-scoped binding is bound to. Tag-level and
22 // targeting a tag no snapshot carries, so it never retains anything.
23 const rule = yield* Rbin.Rule("BindingRule", {
24 resourceType: "EBS_SNAPSHOT",
25 retentionPeriod: "7 days",
26 description: "alchemy rbin bindings fixture rule",
27 resourceTags: [{ key: "AlchemyRbinBindings", value: "true" }],
28 });
29
30 const getRule = yield* Rbin.GetRule(rule);
31 const listRules = yield* Rbin.ListRules();
32
33 const bound = { getRule, listRules };
34
35 return {
36 fetch: Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 const url = new URL(request.originalUrl);
39 const pathname = url.pathname;
40
41 if (request.method === "GET" && pathname === "/bindings") {
42 return yield* HttpServerResponse.json({
43 bound: Object.keys(bound),
44 });
45 }
46
47 // Rule-scoped read: the Identifier is injected from the binding.
48 if (request.method === "GET" && pathname === "/rule") {
49 const detail = yield* getRule();
50 return yield* HttpServerResponse.json({
51 identifier: detail.Identifier,
52 resourceType: detail.ResourceType,
53 retentionDays: detail.RetentionPeriod?.RetentionPeriodValue,
54 description: detail.Description,
55 });
56 }
57
58 // Account-level list, scoped to the fixture rule's resource type.
59 if (request.method === "GET" && pathname === "/rules") {
60 const { Rules } = yield* listRules({
61 ResourceType: "EBS_SNAPSHOT",
62 });
63 return yield* HttpServerResponse.json({
64 ids: (Rules ?? []).map((summary) => summary.Identifier),
65 });
66 }
67
68 return yield* HttpServerResponse.json(
69 { error: "Not found", method: request.method, pathname },
70 { status: 404 },
71 );
72 }).pipe(Effect.orDie),
73 };
74 }).pipe(Effect.provide(Layer.mergeAll(Rbin.GetRuleHttp, Rbin.ListRulesHttp))),
75 );