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

main
ts 97 lines 3.69 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 { JobQueue } from "./JobQueue.ts";
5
6 /**
7 * Shared scaffolding for AWS Batch HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every queue-anchored `{Op}Http.ts` in this
10 * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of
11 * the two builders below. Everything except the operation, the IAM action
12 * list, and (for queue-scoped operations) the injected `jobQueue` is
13 * boilerplate. `SubmitJobHttp` binds TWO resources (queue + job definition)
14 * and injects both, so it stays bespoke.
15 */
16
17 /**
18 * Build the impl Effect for a job-level operation (describe/terminate/cancel
19 * submitted jobs). The bound {@link JobQueue} anchors the binding's identity;
20 * the deploy-time half grants `actions` on `*` because job ARNs
21 * (`arn:…:job/{jobId}`) are only known at runtime and `batch:DescribeJobs` /
22 * `batch:ListJobs` have no resource-level IAM at all.
23 */
24 export const makeBatchJobHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.Batch.DescribeJobs`. */
26 tag: string;
27 /** The distilled operation, invoked with the caller's request as-is. */
28 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
29 /** IAM actions granted on `*`. */
30 actions: readonly string[];
31 }) =>
32 Effect.gen(function* () {
33 const bind = yield* AWSBindings.Bind;
34 const op = yield* AWSBindings.withCredentials(options.operation);
35
36 return Effect.fn(function* (queue: JobQueue) {
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}(${queue})`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(`${options.tag}(${queue.LogicalId})`)(function* (request: I) {
49 return yield* op(request);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for a queue-scoped operation: the runtime callable
56 * injects the bound {@link JobQueue}'s ARN as `jobQueue` and the deploy-time
57 * half grants `actions` on the queue ARN (or `*` when the action has no
58 * resource-level IAM).
59 */
60 export const makeBatchQueueHttpBinding = <I extends { jobQueue?: string }, A, E, R>(options: {
61 /** Fully-qualified binding tag, e.g. `AWS.Batch.ListJobs`. */
62 tag: string;
63 /** The distilled operation; `jobQueue` is injected from the bound queue. */
64 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
65 /** IAM actions granted on the queue ARN (or `*` — see `wildcardIam`). */
66 actions: readonly string[];
67 /** Grant on `*` instead of the queue ARN (actions without resource-level IAM). */
68 wildcardIam?: boolean;
69 }) =>
70 Effect.gen(function* () {
71 const bind = yield* AWSBindings.Bind;
72 const op = yield* AWSBindings.withCredentials(options.operation);
73
74 return Effect.fn(function* (queue: JobQueue) {
75 // Outputs yield a DEFERRED effect — resolve again per invocation below.
76 const JobQueueArn = yield* queue.jobQueueArn;
77 if (!globalThis.__ALCHEMY_RUNTIME__) {
78 yield* bind`${options.tag}(${queue})`({
79 policyStatements: [
80 {
81 Effect: "Allow",
82 Action: [...options.actions],
83 Resource: options.wildcardIam ? ["*"] : [Output.interpolate`${queue.jobQueueArn}`],
84 },
85 ],
86 });
87 }
88 return Effect.fn(`${options.tag}(${queue.LogicalId})`)(function* (
89 request?: Omit<I, "jobQueue">,
90 ) {
91 return yield* op({
92 ...request,
93 jobQueue: yield* JobQueueArn,
94 } as I);
95 });
96 });
97 });