@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 145 lines 5.12 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { ServiceLevelObjective } from "./ServiceLevelObjective.ts";
5
6 /**
7 * Shared scaffolding for Application Signals HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
11 * below. Everything except the operation, the IAM action list, and (for
12 * SLO-scoped operations) the injected identifier is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for an account-level operation (the service
17 * discovery, audit, and change-event read APIs). The deploy-time half
18 * grants `actions` on `*` because the Application Signals discovery
19 * actions do not support resource-level scoping.
20 */
21 export const makeApplicationSignalsAccountHttpBinding = <I, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.ApplicationSignals.ListServices`. */
23 tag: string;
24 /** The distilled operation, invoked with the caller's request as-is. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on `*`. */
27 actions: readonly string[];
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 const op = yield* AWSBindings.withCredentials(options.operation);
32
33 return Effect.fn(function* () {
34 if (!globalThis.__ALCHEMY_RUNTIME__) {
35 yield* bind`${options.tag}()`({
36 policyStatements: [
37 {
38 Effect: "Allow",
39 Action: [...options.actions],
40 Resource: ["*"],
41 },
42 ],
43 });
44 }
45 return Effect.fn(options.tag)(function* (request: I) {
46 return yield* op(request);
47 });
48 });
49 });
50
51 /**
52 * Build the impl Effect for an SLO-scoped operation taking a single `Id`
53 * (`GetServiceLevelObjective`, `ListServiceLevelObjectiveExclusionWindows`):
54 * the runtime callable injects the bound SLO's ARN as `Id` and the
55 * deploy-time half grants `actions` on the SLO ARN.
56 */
57 export const makeSloIdHttpBinding = <I extends { Id: string }, A, E, R>(options: {
58 /** Fully-qualified binding tag. */
59 tag: string;
60 /** The distilled operation; `Id` is injected from the SLO. */
61 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
62 /** IAM actions granted on the SLO ARN. */
63 actions: readonly string[];
64 }) =>
65 Effect.gen(function* () {
66 const bind = yield* AWSBindings.Bind;
67 const op = yield* AWSBindings.withCredentials(options.operation);
68
69 return Effect.fn(function* (slo: ServiceLevelObjective) {
70 const SloArn = yield* slo.sloArn;
71 if (!globalThis.__ALCHEMY_RUNTIME__) {
72 yield* bind`${options.tag}(${slo})`({
73 policyStatements: [
74 {
75 Effect: "Allow",
76 Action: [...options.actions],
77 Resource: [Output.interpolate`${slo.sloArn}`],
78 },
79 ],
80 });
81 }
82 return Effect.fn(`${options.tag}(${slo.LogicalId})`)(function* (request?: Omit<I, "Id">) {
83 return yield* op({
84 ...request,
85 Id: yield* SloArn,
86 } as I);
87 });
88 });
89 });
90
91 /**
92 * Build the impl Effect for a batch SLO operation taking `SloIds`
93 * (`BatchGetServiceLevelObjectiveBudgetReport`,
94 * `BatchUpdateExclusionWindows`), narrowed to the single bound SLO: the
95 * runtime callable injects `SloIds: [sloArn]` and the deploy-time half
96 * grants `actions` on the SLO ARN.
97 */
98 export const makeSloBatchHttpBinding = <I extends { SloIds: string[] }, A, E, R>(options: {
99 /** Fully-qualified binding tag. */
100 tag: string;
101 /** The distilled operation; `SloIds` is injected from the SLO. */
102 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
103 /** IAM actions granted on the SLO ARN. */
104 actions: readonly string[];
105 /**
106 * Additional IAM actions granted on `*` — dependent permissions the
107 * operation exercises with the caller's credentials (e.g. budget reports
108 * read the SLI metric via `cloudwatch:GetMetricData`).
109 */
110 accountActions?: readonly string[];
111 }) =>
112 Effect.gen(function* () {
113 const bind = yield* AWSBindings.Bind;
114 const op = yield* AWSBindings.withCredentials(options.operation);
115
116 return Effect.fn(function* (slo: ServiceLevelObjective) {
117 const SloArn = yield* slo.sloArn;
118 if (!globalThis.__ALCHEMY_RUNTIME__) {
119 yield* bind`${options.tag}(${slo})`({
120 policyStatements: [
121 {
122 Effect: "Allow",
123 Action: [...options.actions],
124 Resource: [Output.interpolate`${slo.sloArn}`],
125 },
126 ...(options.accountActions?.length
127 ? [
128 {
129 Effect: "Allow" as const,
130 Action: [...options.accountActions],
131 Resource: ["*"],
132 },
133 ]
134 : []),
135 ],
136 });
137 }
138 return Effect.fn(`${options.tag}(${slo.LogicalId})`)(function* (request: Omit<I, "SloIds">) {
139 return yield* op({
140 ...request,
141 SloIds: [yield* SloArn],
142 } as I);
143 });
144 });
145 });