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

main
ts 182 lines 6.46 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { AnomalyMonitor } from "./AnomalyMonitor.ts";
4 import { pinCe } from "./common.ts";
5 import type { CostCategory } from "./CostCategory.ts";
6
7 /**
8 * Shared HTTP scaffolding for the Cost Explorer runtime bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
12 * below. Everything except the operation, the IAM action, and (for the
13 * scoped builders) the injected ARN is boilerplate.
14 *
15 * Cost Explorer is a global service served exclusively from the `us-east-1`
16 * endpoint, so every operation is resolved with the Region pinned via
17 * {@link pinCe} — exactly like the resource providers in `common.ts`.
18 *
19 * Per the `ce` service authorization reference, most query actions support
20 * no resource types, so the account-level builder grants on
21 * `Resource: ["*"]`. `ce:GetAnomalies` authorizes on the `anomalymonitor`
22 * resource type, so the anomaly-monitor builder grants on the bound
23 * monitor's ARN.
24 */
25
26 /**
27 * Build the impl Effect for an account-level Cost Explorer operation (the
28 * cost/usage queries, forecasts, recommendations, and cost allocation tag
29 * operations — none of which are resource-scoped).
30 */
31 export const makeCostExplorerHttpBinding = <
32 I extends object,
33 A,
34 // `{ _tag: string }` bound so `pinCe` (whose throttle retry inspects
35 // `_tag`) applies without collapsing the inferred error union.
36 E extends { _tag: string },
37 R,
38 >(options: {
39 /**
40 * Short capability name used in the binding sid and runtime span, e.g.
41 * `"GetCostAndUsage"`.
42 */
43 capability: string;
44 /** IAM actions granted on `Resource: ["*"]`. */
45 iamActions: readonly string[];
46 /** The distilled operation implementing the capability. */
47 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
48 }) =>
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const op = yield* options.operation.pipe(pinCe, AWSBindings.withCredentials);
52
53 return Effect.fn(function* () {
54 if (!globalThis.__ALCHEMY_RUNTIME__) {
55 yield* bind`AWS.CostExplorer.${options.capability}()`({
56 policyStatements: [
57 {
58 Effect: "Allow",
59 Action: [...options.iamActions],
60 Resource: ["*"],
61 },
62 ],
63 });
64 }
65 return Effect.fn(`AWS.CostExplorer.${options.capability}`)(function* (request?: I) {
66 // The region must also be pinned at the call site: the yield-time
67 // snapshot is only a fallback — the calling fiber's ambient Region
68 // (the host Function's own region) wins over it.
69 return yield* pinCe(op((request ?? {}) as I));
70 });
71 });
72 });
73
74 /**
75 * Build the impl Effect for an operation scoped to one
76 * {@link AnomalyMonitor}. The runtime callable injects the bound monitor's
77 * ARN as the request's `MonitorArn`; the deploy-time half grants
78 * `iamActions` on the monitor's ARN.
79 */
80 export const makeAnomalyMonitorHttpBinding = <
81 I extends { MonitorArn?: string },
82 A,
83 // See makeCostExplorerHttpBinding.
84 E extends { _tag: string },
85 R,
86 >(options: {
87 /**
88 * Short capability name used in the binding sid and runtime span, e.g.
89 * `"GetAnomalies"`.
90 */
91 capability: string;
92 /** IAM actions granted on the monitor ARN. */
93 iamActions: readonly string[];
94 /** The distilled operation; `MonitorArn` is injected. */
95 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
96 }) =>
97 Effect.gen(function* () {
98 const bind = yield* AWSBindings.Bind;
99 const op = yield* options.operation.pipe(pinCe, AWSBindings.withCredentials);
100
101 return Effect.fn(function* (monitor: AnomalyMonitor) {
102 const MonitorArn = yield* monitor.monitorArn;
103 if (!globalThis.__ALCHEMY_RUNTIME__) {
104 yield* bind`AWS.CostExplorer.${options.capability}(${monitor})`({
105 policyStatements: [
106 {
107 Effect: "Allow",
108 Action: [...options.iamActions],
109 Resource: [monitor.monitorArn],
110 },
111 ],
112 });
113 }
114 return Effect.fn(`AWS.CostExplorer.${options.capability}(${monitor.LogicalId})`)(function* (
115 request: Omit<I, "MonitorArn">,
116 ) {
117 // Call-site region pin — see makeCostExplorerHttpBinding above.
118 return yield* pinCe(
119 op({
120 ...request,
121 MonitorArn: yield* MonitorArn,
122 } as I),
123 );
124 });
125 });
126 });
127
128 /**
129 * Build the impl Effect for an operation bound to one {@link CostCategory}.
130 * The runtime callable injects the bound category's ARN as the request's
131 * `CostCategoryArn`. The deploy-time half grants `iamActions` on
132 * `Resource: ["*"]` — per the `ce` service authorization reference,
133 * `ce:ListCostCategoryResourceAssociations` supports no resource types, so
134 * a grant scoped to the category ARN would never match.
135 */
136 export const makeCostCategoryHttpBinding = <
137 I extends { CostCategoryArn?: string },
138 A,
139 // See makeCostExplorerHttpBinding.
140 E extends { _tag: string },
141 R,
142 >(options: {
143 /**
144 * Short capability name used in the binding sid and runtime span, e.g.
145 * `"ListCostCategoryResourceAssociations"`.
146 */
147 capability: string;
148 /** IAM actions granted on `Resource: ["*"]` (no resource types). */
149 iamActions: readonly string[];
150 /** The distilled operation; `CostCategoryArn` is injected. */
151 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
152 }) =>
153 Effect.gen(function* () {
154 const bind = yield* AWSBindings.Bind;
155 const op = yield* options.operation.pipe(pinCe, AWSBindings.withCredentials);
156
157 return Effect.fn(function* (category: CostCategory) {
158 const CostCategoryArn = yield* category.costCategoryArn;
159 if (!globalThis.__ALCHEMY_RUNTIME__) {
160 yield* bind`AWS.CostExplorer.${options.capability}(${category})`({
161 policyStatements: [
162 {
163 Effect: "Allow",
164 Action: [...options.iamActions],
165 Resource: ["*"],
166 },
167 ],
168 });
169 }
170 return Effect.fn(`AWS.CostExplorer.${options.capability}(${category.LogicalId})`)(function* (
171 request?: Omit<I, "CostCategoryArn">,
172 ) {
173 // Call-site region pin — see makeCostExplorerHttpBinding above.
174 return yield* pinCe(
175 op({
176 ...request,
177 CostCategoryArn: yield* CostCategoryArn,
178 } as I),
179 );
180 });
181 });
182 });