@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 101 lines 3.61 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 { ImagePipeline } from "./ImagePipeline.ts";
5
6 /**
7 * Shared scaffolding for the EC2 Image Builder HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
10 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
11 * below. Everything except the operation and the IAM action list is
12 * boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for an Image Builder operation scoped to a bound
17 * {@link ImagePipeline}: the runtime callable injects the pipeline's ARN as
18 * `imagePipelineArn` and the deploy-time half grants `actions` on the
19 * pipeline's ARN.
20 *
21 * `clientToken` (an idempotency token) is auto-generated by distilled when
22 * absent, so it is omitted from the runtime request shape.
23 */
24 export const makeImageBuilderPipelineHttpBinding = <
25 I extends { imagePipelineArn?: string },
26 A,
27 E,
28 R,
29 >(options: {
30 /** Fully-qualified binding tag, e.g. `AWS.ImageBuilder.GetImagePipeline`. */
31 tag: string;
32 /** The distilled operation; `imagePipelineArn` is injected. */
33 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
34 /** IAM actions granted on the pipeline ARN. */
35 actions: readonly string[];
36 }) =>
37 Effect.gen(function* () {
38 const bind = yield* AWSBindings.Bind;
39 const op = yield* AWSBindings.withCredentials(options.operation);
40
41 return Effect.fn(function* (pipeline: ImagePipeline) {
42 // Outputs yield a DEFERRED effect — resolve again per invocation below.
43 const PipelineArn = yield* pipeline.imagePipelineArn;
44 if (!globalThis.__ALCHEMY_RUNTIME__) {
45 yield* bind`${options.tag}(${pipeline})`({
46 policyStatements: [
47 {
48 Effect: "Allow",
49 Action: [...options.actions],
50 Resource: [Output.interpolate`${pipeline.imagePipelineArn}`],
51 },
52 ],
53 });
54 }
55 return Effect.fn(`${options.tag}(${pipeline.LogicalId})`)(function* (
56 request?: Omit<I, "imagePipelineArn" | "clientToken">,
57 ) {
58 return yield* op({
59 ...request,
60 imagePipelineArn: yield* PipelineArn,
61 } as I);
62 });
63 });
64 });
65
66 /**
67 * Build the impl Effect for an account-level Image Builder operation (image
68 * build-version reads and control, account image listings). Image build
69 * versions are created dynamically by pipeline runs — their ARNs are not
70 * known at deploy time — so the deploy-time half grants `actions` on `*` and
71 * the runtime callable passes the caller's request through as-is.
72 */
73 export const makeImageBuilderAccountHttpBinding = <I, A, E, R>(options: {
74 /** Fully-qualified binding tag, e.g. `AWS.ImageBuilder.GetImage`. */
75 tag: string;
76 /** The distilled operation, invoked with the caller's request as-is. */
77 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
78 /** IAM actions granted on `*`. */
79 actions: readonly string[];
80 }) =>
81 Effect.gen(function* () {
82 const bind = yield* AWSBindings.Bind;
83 const op = yield* AWSBindings.withCredentials(options.operation);
84
85 return Effect.fn(function* () {
86 if (!globalThis.__ALCHEMY_RUNTIME__) {
87 yield* bind`${options.tag}()`({
88 policyStatements: [
89 {
90 Effect: "Allow",
91 Action: [...options.actions],
92 Resource: ["*"],
93 },
94 ],
95 });
96 }
97 return Effect.fn(options.tag)(function* (request?: Omit<I, "clientToken">) {
98 return yield* op((request ?? {}) as I);
99 });
100 });
101 });