@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 267 lines 9.83 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 { Blueprint } from "./Blueprint.ts";
5 import type { DataAutomationLibrary } from "./DataAutomationLibrary.ts";
6 import type { DataAutomationProject } from "./DataAutomationProject.ts";
7
8 /**
9 * Shared scaffolding for Bedrock Data Automation HTTP 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 two
13 * builders below. Everything except the operation and the IAM action list is
14 * boilerplate.
15 */
16
17 /**
18 * Build the impl Effect for a project-scoped runtime operation (the sync and
19 * async invoke APIs). The runtime callable injects the bound
20 * {@link DataAutomationProject}'s ARN + stage as `dataAutomationConfiguration`
21 * and the deploy-time half grants `actions` on the project ARN plus the
22 * account's data automation profiles in every region — cross-region
23 * inference profiles (e.g. `us.data-automation-v1`) fan requests out to
24 * sibling regions, so the profile grant cannot be scoped to one region.
25 */
26 export const makeBdaProjectHttpBinding = <
27 I extends {
28 dataAutomationConfiguration?: {
29 dataAutomationProjectArn: string;
30 stage?: string;
31 };
32 },
33 A,
34 E,
35 R,
36 >(options: {
37 /** Fully-qualified binding tag, e.g. `AWS.BedrockDataAutomation.InvokeDataAutomationAsync`. */
38 tag: string;
39 /** The distilled operation; `dataAutomationConfiguration` is injected from the project. */
40 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
41 /** IAM actions granted on the project ARN + the account's profiles. */
42 actions: readonly string[];
43 }) =>
44 Effect.gen(function* () {
45 const bind = yield* AWSBindings.Bind;
46 const op = yield* AWSBindings.withCredentials(options.operation);
47
48 return Effect.fn(function* (project: DataAutomationProject) {
49 const projectArn = yield* project.projectArn;
50 const projectStage = yield* project.projectStage;
51 if (!globalThis.__ALCHEMY_RUNTIME__) {
52 yield* bind`${options.tag}(${project})`({
53 policyStatements: [
54 {
55 Effect: "Allow",
56 Action: [...options.actions],
57 Resource: [
58 Output.interpolate`${project.projectArn}`,
59 // Cross-region inference profiles live in every region of
60 // the geography (us./eu./apac.), so the profile resource
61 // cannot be pinned to the deploy region.
62 "arn:aws:bedrock:*:*:data-automation-profile/*",
63 ],
64 },
65 ],
66 });
67 }
68 return Effect.fn(`${options.tag}(${project.LogicalId})`)(function* (
69 request: Omit<I, "dataAutomationConfiguration">,
70 ) {
71 return yield* op({
72 ...request,
73 dataAutomationConfiguration: {
74 dataAutomationProjectArn: yield* projectArn,
75 stage: yield* projectStage,
76 },
77 } as I);
78 });
79 });
80 });
81
82 /**
83 * Build the impl Effect for a library-scoped runtime operation (entity reads
84 * and ingestion-job APIs). The runtime callable injects the bound
85 * {@link DataAutomationLibrary}'s ARN as `libraryArn` and the deploy-time
86 * half grants `actions` on the library ARN.
87 */
88 export const makeBdaLibraryHttpBinding = <I extends { libraryArn: string }, A, E, R>(options: {
89 /** Fully-qualified binding tag, e.g. `AWS.BedrockDataAutomation.ListDataAutomationLibraryEntities`. */
90 tag: string;
91 /** The distilled operation; `libraryArn` is injected from the library. */
92 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
93 /** IAM actions granted on the library ARN (+ `additionalResources`). */
94 actions: readonly string[];
95 /**
96 * Extra resource ARNs the actions authorize against — the ingestion-job
97 * APIs check `…:data-automation-library-ingestion-job/{id}` (minted at
98 * runtime), not the library ARN.
99 */
100 additionalResources?: readonly string[];
101 }) =>
102 Effect.gen(function* () {
103 const bind = yield* AWSBindings.Bind;
104 const op = yield* AWSBindings.withCredentials(options.operation);
105
106 return Effect.fn(function* (library: DataAutomationLibrary) {
107 const libraryArn = yield* library.libraryArn;
108 if (!globalThis.__ALCHEMY_RUNTIME__) {
109 yield* bind`${options.tag}(${library})`({
110 policyStatements: [
111 {
112 Effect: "Allow",
113 Action: [...options.actions],
114 Resource: [
115 Output.interpolate`${library.libraryArn}`,
116 ...(options.additionalResources ?? []),
117 ],
118 },
119 ],
120 });
121 }
122 return Effect.fn(`${options.tag}(${library.LogicalId})`)(function* (
123 request: Omit<I, "libraryArn">,
124 ) {
125 return yield* op({
126 ...request,
127 libraryArn: yield* libraryArn,
128 } as I);
129 });
130 });
131 });
132
133 /**
134 * Build the impl Effect for a blueprint-scoped runtime operation (blueprint
135 * optimization). The runtime callable injects the bound {@link Blueprint}'s
136 * ARN + stage as `blueprint` and the deploy-time half grants `actions` on the
137 * blueprint ARN plus the account's data automation profiles in every region
138 * (cross-region inference profiles fan out to sibling regions).
139 */
140 export const makeBdaBlueprintOptimizationHttpBinding = <
141 I extends { blueprint?: { blueprintArn: string; stage?: string } },
142 A,
143 E,
144 R,
145 >(options: {
146 /** Fully-qualified binding tag, e.g. `AWS.BedrockDataAutomation.InvokeBlueprintOptimizationAsync`. */
147 tag: string;
148 /** The distilled operation; `blueprint` is injected from the blueprint. */
149 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
150 /** IAM actions granted on the blueprint ARN + the account's profiles. */
151 actions: readonly string[];
152 }) =>
153 Effect.gen(function* () {
154 const bind = yield* AWSBindings.Bind;
155 const op = yield* AWSBindings.withCredentials(options.operation);
156
157 return Effect.fn(function* (blueprint: Blueprint) {
158 const blueprintArn = yield* blueprint.blueprintArn;
159 const blueprintStage = yield* blueprint.blueprintStage;
160 if (!globalThis.__ALCHEMY_RUNTIME__) {
161 yield* bind`${options.tag}(${blueprint})`({
162 policyStatements: [
163 {
164 Effect: "Allow",
165 Action: [...options.actions],
166 Resource: [
167 Output.interpolate`${blueprint.blueprintArn}`,
168 "arn:aws:bedrock:*:*:data-automation-profile/*",
169 // The invoke authorizes against the invocation resource
170 // (minted at runtime), not the blueprint ARN.
171 "arn:aws:bedrock:*:*:blueprint-optimization-invocation/*",
172 ],
173 },
174 ],
175 });
176 }
177 return Effect.fn(`${options.tag}(${blueprint.LogicalId})`)(function* (
178 request: Omit<I, "blueprint">,
179 ) {
180 return yield* op({
181 ...request,
182 blueprint: {
183 blueprintArn: yield* blueprintArn,
184 stage: yield* blueprintStage,
185 },
186 } as I);
187 });
188 });
189 });
190
191 /**
192 * Build the impl Effect for a blueprint-scoped management operation (version
193 * snapshots and stage copies). The runtime callable injects the bound
194 * {@link Blueprint}'s ARN as `blueprintArn` and the deploy-time half grants
195 * `actions` on the blueprint ARN.
196 */
197 export const makeBdaBlueprintHttpBinding = <I extends { blueprintArn: string }, A, E, R>(options: {
198 /** Fully-qualified binding tag, e.g. `AWS.BedrockDataAutomation.CreateBlueprintVersion`. */
199 tag: string;
200 /** The distilled operation; `blueprintArn` is injected from the blueprint. */
201 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
202 /** IAM actions granted on the blueprint ARN. */
203 actions: readonly string[];
204 }) =>
205 Effect.gen(function* () {
206 const bind = yield* AWSBindings.Bind;
207 const op = yield* AWSBindings.withCredentials(options.operation);
208
209 return Effect.fn(function* (blueprint: Blueprint) {
210 const blueprintArn = yield* blueprint.blueprintArn;
211 if (!globalThis.__ALCHEMY_RUNTIME__) {
212 yield* bind`${options.tag}(${blueprint})`({
213 policyStatements: [
214 {
215 Effect: "Allow",
216 Action: [...options.actions],
217 Resource: [Output.interpolate`${blueprint.blueprintArn}`],
218 },
219 ],
220 });
221 }
222 return Effect.fn(`${options.tag}(${blueprint.LogicalId})`)(function* (
223 request: Omit<I, "blueprintArn">,
224 ) {
225 return yield* op({
226 ...request,
227 blueprintArn: yield* blueprintArn,
228 } as I);
229 });
230 });
231 });
232
233 /**
234 * Build the impl Effect for an account-level runtime operation (job status
235 * polling). The deploy-time half grants `actions` on `*` — invocation ARNs
236 * (`…:data-automation-invocation/{id}`) are minted at runtime and unknowable
237 * at deploy time.
238 */
239 export const makeBdaAccountHttpBinding = <I, A, E, R>(options: {
240 /** Fully-qualified binding tag, e.g. `AWS.BedrockDataAutomation.GetDataAutomationStatus`. */
241 tag: string;
242 /** The distilled operation, invoked with the caller's request as-is. */
243 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
244 /** IAM actions granted on `*`. */
245 actions: readonly string[];
246 }) =>
247 Effect.gen(function* () {
248 const bind = yield* AWSBindings.Bind;
249 const op = yield* AWSBindings.withCredentials(options.operation);
250
251 return Effect.fn(function* () {
252 if (!globalThis.__ALCHEMY_RUNTIME__) {
253 yield* bind`${options.tag}()`({
254 policyStatements: [
255 {
256 Effect: "Allow",
257 Action: [...options.actions],
258 Resource: ["*"],
259 },
260 ],
261 });
262 }
263 return Effect.fn(options.tag)(function* (request: I) {
264 return yield* op(request);
265 });
266 });
267 });