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

main
ts 89 lines 3.29 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { InvestigationGroup } from "./InvestigationGroup.ts";
4
5 /**
6 * Shared scaffolding for the CloudWatch investigations (AIOps) HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
10 * below. Everything except the operation, the IAM action list, and (for
11 * group-scoped ops) how the group's ARN maps onto the request is boilerplate.
12 */
13
14 /**
15 * Build the impl Effect for an AIOps operation scoped to an
16 * {@link InvestigationGroup}: the deploy-time half grants `actions` on the
17 * bound group's ARN, and the runtime half injects the group's ARN into the
18 * request via `input`. All group-scoped AIOps read requests consist solely of
19 * the group identifier, so the runtime callable takes no arguments.
20 */
21 export const makeAIOpsGroupHttpBinding = <I, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.AIOps.GetInvestigationGroup`. */
23 tag: string;
24 /** The distilled operation. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on the investigation group ARN. */
27 actions: readonly string[];
28 /** Map the bound group's ARN onto the operation's request shape. */
29 input: (groupArn: string) => I;
30 }) =>
31 Effect.gen(function* () {
32 const bind = yield* AWSBindings.Bind;
33 const op = yield* AWSBindings.withCredentials(options.operation);
34
35 return Effect.fn(function* (group: InvestigationGroup) {
36 const Arn = yield* group.arn;
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}(${group})`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: [group.arn],
44 },
45 ],
46 });
47 }
48 return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* () {
49 const arn = yield* Arn;
50 return yield* op(options.input(arn));
51 });
52 });
53 });
54
55 /**
56 * Build the impl Effect for an account-level AIOps operation (enumerating
57 * the Region's investigation groups). The deploy-time half grants `actions`
58 * on `*` — `aiops:ListInvestigationGroups` is a list action that is not
59 * scoped to a single group resource.
60 */
61 export const makeAIOpsAccountHttpBinding = <I, A, E, R>(options: {
62 /** Fully-qualified binding tag, e.g. `AWS.AIOps.ListInvestigationGroups`. */
63 tag: string;
64 /** The distilled operation, invoked with the caller's request as-is. */
65 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
66 /** IAM actions granted on `*`. */
67 actions: readonly string[];
68 }) =>
69 Effect.gen(function* () {
70 const bind = yield* AWSBindings.Bind;
71 const op = yield* AWSBindings.withCredentials(options.operation);
72
73 return Effect.fn(function* () {
74 if (!globalThis.__ALCHEMY_RUNTIME__) {
75 yield* bind`${options.tag}()`({
76 policyStatements: [
77 {
78 Effect: "Allow",
79 Action: [...options.actions],
80 Resource: ["*"],
81 },
82 ],
83 });
84 }
85 return Effect.fn(options.tag)(function* (request?: I) {
86 return yield* op((request ?? {}) as I);
87 });
88 });
89 });