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

main
ts 176 lines 6.88 KB
Raw
1 import type { Output } from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { TaggableResource } from "./binding-common.ts";
5 import { sortByLogicalId } from "./common.ts";
6
7 /**
8 * Shared scaffolding for CloudWatch HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in
11 * this service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over
12 * one of the three builders below. Everything except the operation, the IAM
13 * action, and the injected identifier(s) is boilerplate:
14 *
15 * - {@link makeCloudWatchAccountHttpBinding} — account-level operations
16 * (`PutMetricData`, `GetMetricData`, `List*`, account-wide `Describe*`).
17 * CloudWatch metric-data and account-wide describe/list actions do not
18 * support resource-level permissions, so the deploy-time half grants
19 * `actions` on `*`.
20 * - {@link makeCloudWatchResourceHttpBinding} — operations scoped to one
21 * bound resource (`GetDashboard`, `SetAlarmState`, `GetMetricStream`, …).
22 * The runtime callable injects the resource's identifier under
23 * `requestKey`; the deploy-time half grants `actions` on the resource ARN.
24 * - {@link makeCloudWatchResourceSetHttpBinding} — batch toggles over a
25 * variadic set of bound resources (`EnableAlarmActions`,
26 * `DisableInsightRules`, `StartMetricStreams`, …). The runtime callable
27 * takes no request and injects the sorted resource names under `namesKey`;
28 * the deploy-time half grants `action` on every resource ARN.
29 *
30 * Genuinely-different bindings stay bespoke: `DescribeAlarms` (computes
31 * `AlarmTypes` from the bound alarm set and accepts a filter request) and
32 * `MetricSink` (a batching sink over the `PutMetricData` capability).
33 */
34
35 /**
36 * Build the impl Effect for an account-level CloudWatch operation. The
37 * runtime callable passes the caller's request through unchanged; the
38 * deploy-time half grants `actions` on `*` (these CloudWatch actions do not
39 * support resource-level permissions).
40 */
41 export const makeCloudWatchAccountHttpBinding = <I, A, E, R>(options: {
42 /** Fully-qualified binding tag, e.g. `AWS.CloudWatch.GetMetricData`. */
43 tag: string;
44 /** The distilled operation, invoked with the caller's request as-is. */
45 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
46 /** IAM actions granted on `*`. */
47 actions: readonly string[];
48 }) =>
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const op = yield* AWSBindings.withCredentials(options.operation);
52
53 return Effect.fn(function* () {
54 if (!globalThis.__ALCHEMY_RUNTIME__) {
55 yield* bind`${options.tag}()`({
56 policyStatements: [
57 {
58 Effect: "Allow",
59 Action: [...options.actions],
60 Resource: ["*"],
61 },
62 ],
63 });
64 }
65 return Effect.fn(options.tag)(function* (request?: I) {
66 return yield* op((request ?? {}) as I);
67 });
68 });
69 });
70
71 /**
72 * Build the impl Effect for an operation scoped to a single bound CloudWatch
73 * resource. The runtime callable injects the resolved `identifier` under
74 * `requestKey`; the deploy-time half grants `actions` on `resourceArn`.
75 */
76 export const makeCloudWatchResourceHttpBinding = <
77 Res extends TaggableResource,
78 K extends string,
79 I,
80 A,
81 E,
82 R,
83 >(options: {
84 /** Fully-qualified binding tag, e.g. `AWS.CloudWatch.GetDashboard`. */
85 tag: string;
86 /** The distilled operation; the identifier is injected from the resource. */
87 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
88 /** IAM actions granted on the resource ARN. */
89 actions: readonly string[];
90 /** Request field the resolved identifier is injected under. */
91 requestKey: K;
92 /** Resolve the injected identifier from the bound resource. */
93 identifier: (resource: Res) => Output<string, never>;
94 /** Resolve the IAM resource the actions are granted on. */
95 resourceArn: (resource: Res) => Output<string, never>;
96 }) =>
97 Effect.gen(function* () {
98 const bind = yield* AWSBindings.Bind;
99 const op = yield* AWSBindings.withCredentials(options.operation);
100
101 return Effect.fn(function* (resource: Res) {
102 const identifier = yield* options.identifier(resource);
103 if (!globalThis.__ALCHEMY_RUNTIME__) {
104 yield* bind`${options.tag}(${resource})`({
105 policyStatements: [
106 {
107 Effect: "Allow",
108 Action: [...options.actions],
109 Resource: [options.resourceArn(resource)],
110 },
111 ],
112 });
113 }
114 return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (request?: Omit<I, K>) {
115 const input: Record<string, unknown> = { ...request };
116 input[options.requestKey] = yield* identifier;
117 return yield* op(input as unknown as I);
118 });
119 });
120 });
121
122 /**
123 * Build the impl Effect for a batch toggle over a variadic set of bound
124 * CloudWatch resources. The runtime callable takes no request and injects
125 * the resources' names (sorted by Logical ID for a deterministic binding
126 * identity) under `namesKey`; the deploy-time half grants `action` on every
127 * resource ARN.
128 */
129 export const makeCloudWatchResourceSetHttpBinding = <
130 Res extends TaggableResource,
131 K extends string,
132 I,
133 A,
134 E,
135 R,
136 >(options: {
137 /** Fully-qualified binding tag, e.g. `AWS.CloudWatch.EnableAlarmActions`. */
138 tag: string;
139 /** The distilled operation; the sorted names array is injected. */
140 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
141 /** IAM action granted on every bound resource ARN. */
142 action: string;
143 /** Request field the resolved names array is injected under. */
144 namesKey: K;
145 /** Resolve a resource's name (the injected identifier). */
146 name: (resource: Res) => Output<string, never>;
147 /** Resolve a resource's ARN (the IAM resource). */
148 arn: (resource: Res) => Output<string, never>;
149 }) =>
150 Effect.gen(function* () {
151 const bind = yield* AWSBindings.Bind;
152 const op = yield* AWSBindings.withCredentials(options.operation);
153
154 return Effect.fn(function* (...resources: [Res, ...Res[]]) {
155 const sorted = sortByLogicalId(resources) as [Res, ...Res[]];
156 const names = yield* Effect.forEach(sorted, (resource) => options.name(resource).asEffect());
157 if (!globalThis.__ALCHEMY_RUNTIME__) {
158 yield* bind`${options.tag}(${sorted})`({
159 policyStatements: [
160 {
161 Effect: "Allow",
162 Action: [options.action],
163 Resource: sorted.map((resource) => options.arn(resource)),
164 },
165 ],
166 });
167 }
168 return Effect.fn(
169 `${options.tag}(${sorted.map((resource) => resource.LogicalId).join(", ")})`,
170 )(function* () {
171 const input: Record<string, unknown> = {};
172 input[options.namesKey] = yield* Effect.forEach(names, (name) => name);
173 return yield* op(input as unknown as I);
174 });
175 });
176 });