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

main
ts 120 lines 4.29 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { PolicyStatement } from "../IAM/Policy.ts";
4 import { Region } from "../Region.ts";
5 import type { ReportDefinition } from "./ReportDefinition.ts";
6
7 /**
8 * Shared HTTP scaffolding for the AWS Cost and Usage Report (`cur`) runtime
9 * bindings.
10 *
11 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
12 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
13 * below.
14 *
15 * The CUR control plane is a global service hosted only in `us-east-1`, so
16 * every operation is resolved with the distilled Region pinned there —
17 * exactly like the {@link ReportDefinition} provider. The distilled Region
18 * service value is `Effect<RegionName>`, not a raw string, hence
19 * `Effect.succeed`.
20 */
21 const CUR_REGION = "us-east-1" as const;
22
23 const pinCur = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
24 effect.pipe(Effect.provideService(Region, Effect.succeed(CUR_REGION)));
25
26 /**
27 * Build the impl Effect for an account-level CUR operation.
28 * `cur:DescribeReportDefinitions` enumerates every report definition in the
29 * account, so the grant is on `Resource: ["*"]`.
30 */
31 export const makeCurHttpBinding = <I extends object, A, E, R>(options: {
32 /**
33 * Short capability name used in the binding sid and runtime span, e.g.
34 * `"DescribeReportDefinitions"`.
35 */
36 capability: string;
37 /** IAM actions granted on `Resource: ["*"]`. */
38 iamActions: readonly string[];
39 /** The distilled operation implementing the capability. */
40 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* options.operation.pipe(pinCur, AWSBindings.withCredentials);
45
46 return Effect.fn(function* () {
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 const policyStatements: PolicyStatement[] = [
49 {
50 Effect: "Allow",
51 Action: [...options.iamActions],
52 Resource: ["*"],
53 },
54 ];
55 yield* bind`AWS.CostAndUsageReport.${options.capability}()`({
56 policyStatements,
57 });
58 }
59 return Effect.fn(`AWS.CostAndUsageReport.${options.capability}`)(function* (request?: I) {
60 // The region must also be pinned at the call site: the yield-time
61 // snapshot is only a fallback — the calling fiber's ambient Region
62 // (the host Function's own region) wins over it.
63 return yield* pinCur(op((request ?? {}) as I));
64 });
65 });
66 });
67
68 /**
69 * Build the impl Effect for an operation scoped to one
70 * {@link ReportDefinition}. The runtime callable injects the bound report's
71 * name as the request's `ReportName`; the deploy-time half grants
72 * `iamActions` on the report definition's ARN
73 * (`arn:aws:cur:us-east-1:{account}:definition/{name}`).
74 */
75 export const makeReportDefinitionHttpBinding = <
76 I extends { ReportName?: string },
77 A,
78 E,
79 R,
80 >(options: {
81 /**
82 * Short capability name used in the binding sid and runtime span, e.g.
83 * `"ListTagsForResource"`.
84 */
85 capability: string;
86 /** IAM actions granted on the report definition's ARN. */
87 iamActions: readonly string[];
88 /** The distilled operation; `ReportName` is injected. */
89 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
90 }) =>
91 Effect.gen(function* () {
92 const bind = yield* AWSBindings.Bind;
93 const op = yield* options.operation.pipe(pinCur, AWSBindings.withCredentials);
94
95 return Effect.fn(function* (report: ReportDefinition) {
96 const ReportName = yield* report.reportName;
97 if (!globalThis.__ALCHEMY_RUNTIME__) {
98 yield* bind`AWS.CostAndUsageReport.${options.capability}(${report})`({
99 policyStatements: [
100 {
101 Effect: "Allow",
102 Action: [...options.iamActions],
103 Resource: [report.reportArn],
104 },
105 ],
106 });
107 }
108 return Effect.fn(`AWS.CostAndUsageReport.${options.capability}(${report.LogicalId})`)(
109 function* (request?: Omit<I, "ReportName">) {
110 // Call-site region pin — see makeCurHttpBinding above.
111 return yield* pinCur(
112 op({
113 ...request,
114 ReportName: yield* ReportName,
115 } as I),
116 );
117 },
118 );
119 });
120 });