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

main
ts 122 lines 4.3 KB
Raw
1 import * as AIOps from "@alchemy.run/aws/AIOps";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class AIOpsTestFunction extends Lambda.Function<Lambda.Function>()("AIOpsTestFunction") {}
14
15 export default AIOpsTestFunction.make(
16 {
17 main,
18 functionUrl: true,
19 timeout: Duration.seconds(30),
20 },
21 Effect.gen(function* () {
22 // The one-per-Region investigation group the group-scoped bindings are
23 // bound to. The role is what CloudWatch investigations assumes to read
24 // telemetry — required at create time.
25 const role = yield* Role("AIOpsBindingRole", {
26 assumeRolePolicyDocument: {
27 Version: "2012-10-17",
28 Statement: [
29 {
30 Effect: "Allow",
31 Principal: { Service: "aiops.amazonaws.com" },
32 Action: ["sts:AssumeRole"],
33 },
34 ],
35 },
36 managedPolicyArns: ["arn:aws:iam::aws:policy/AIOpsAssistantPolicy"],
37 });
38 const group = yield* AIOps.InvestigationGroup("BindingGroup", {
39 roleArn: role.roleArn,
40 retention: "7 days",
41 tags: { Purpose: "bindings-test" },
42 });
43
44 const getInvestigationGroup = yield* AIOps.GetInvestigationGroup(group);
45 const getInvestigationGroupPolicy = yield* AIOps.GetInvestigationGroupPolicy(group);
46 const listTagsForResource = yield* AIOps.ListTagsForResource(group);
47 const listInvestigationGroups = yield* AIOps.ListInvestigationGroups();
48
49 const bound = {
50 getInvestigationGroup,
51 getInvestigationGroupPolicy,
52 listTagsForResource,
53 listInvestigationGroups,
54 };
55
56 return {
57 fetch: Effect.gen(function* () {
58 const request = yield* HttpServerRequest;
59 const url = new URL(request.originalUrl);
60 const pathname = url.pathname;
61
62 if (request.method === "GET" && pathname === "/bindings") {
63 return yield* HttpServerResponse.json({
64 bound: Object.keys(bound),
65 });
66 }
67
68 // Group-scoped read: the group ARN is injected from the binding.
69 if (request.method === "GET" && pathname === "/group") {
70 const detail = yield* getInvestigationGroup();
71 return yield* HttpServerResponse.json({
72 name: detail.name,
73 arn: detail.arn,
74 retentionInDays: detail.retentionInDays,
75 });
76 }
77
78 // Group-scoped policy read: no policy is attached to the fixture's
79 // group, so the typed ResourceNotFoundException proves the grant and
80 // ARN injection end-to-end.
81 if (request.method === "GET" && pathname === "/policy") {
82 const policy = yield* getInvestigationGroupPolicy().pipe(
83 Effect.map((r) => r.policy),
84 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
85 );
86 return yield* HttpServerResponse.json({
87 hasPolicy: policy !== undefined,
88 });
89 }
90
91 // Group-scoped tag read.
92 if (request.method === "GET" && pathname === "/tags") {
93 const { tags } = yield* listTagsForResource();
94 return yield* HttpServerResponse.json({ tags: tags ?? {} });
95 }
96
97 // Account-level list: at most one group exists per Region, and it is
98 // the fixture's own group.
99 if (request.method === "GET" && pathname === "/groups") {
100 const { investigationGroups } = yield* listInvestigationGroups();
101 return yield* HttpServerResponse.json({
102 groupArns: (investigationGroups ?? []).map((g) => g.arn),
103 });
104 }
105
106 return yield* HttpServerResponse.json(
107 { error: "Not found", method: request.method, pathname },
108 { status: 404 },
109 );
110 }).pipe(Effect.orDie),
111 };
112 }).pipe(
113 Effect.provide(
114 Layer.mergeAll(
115 AIOps.GetInvestigationGroupHttp,
116 AIOps.GetInvestigationGroupPolicyHttp,
117 AIOps.ListTagsForResourceHttp,
118 AIOps.ListInvestigationGroupsHttp,
119 ),
120 ),
121 ),
122 );