The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Activity } from "./Activity.ts"; |
| 5 | import type { StateMachine } from "./StateMachine.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS Step Functions HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this |
| 11 | * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of |
| 12 | * the builders below. Everything except the operation, the IAM action list, |
| 13 | * and the injected/derived identifier is boilerplate. Genuinely-different |
| 14 | * bindings (none currently — `StartSyncExecution` folds its bespoke |
| 15 | * `sync-states` endpoint into the `operation` effect it passes) stay |
| 16 | * bespoke. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * Execution ARNs replace the `:stateMachine:` segment with `:execution:` |
| 21 | * and append the execution name — grant on the machine's execution ARN |
| 22 | * pattern. |
| 23 | */ |
| 24 | export const executionArnPattern = (stateMachine: StateMachine) => |
| 25 | Output.map( |
| 26 | stateMachine.stateMachineArn, |
| 27 | (arn) => `${arn.replace(":stateMachine:", ":execution:")}:*`, |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Map Run ARNs replace the `:stateMachine:` segment with `:mapRun:` and |
| 32 | * append `/{executionName}:{uuid}` — grant on the machine's Map Run ARN |
| 33 | * pattern. |
| 34 | */ |
| 35 | export const mapRunArnPattern = (stateMachine: StateMachine) => |
| 36 | Output.map( |
| 37 | stateMachine.stateMachineArn, |
| 38 | (arn) => `${arn.replace(":stateMachine:", ":mapRun:")}/*`, |
| 39 | ); |
| 40 | |
| 41 | const Grant = Effect.gen(function* () { |
| 42 | const bind = yield* AWSBindings.Bind; |
| 43 | return Effect.fn(function* ( |
| 44 | tag: string, |
| 45 | scope: { LogicalId: string } | string, |
| 46 | actions: readonly string[], |
| 47 | resources: readonly (string | Output.Output<string, any>)[], |
| 48 | ) { |
| 49 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 50 | yield* bind`${tag}(${scope})`({ |
| 51 | policyStatements: [ |
| 52 | { |
| 53 | Effect: "Allow", |
| 54 | Action: [...actions], |
| 55 | Resource: [...resources], |
| 56 | }, |
| 57 | ], |
| 58 | }); |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | /** |
| 64 | * Build the impl Effect for a state-machine-scoped operation whose request |
| 65 | * carries the machine identity as `stateMachineArn` (`StartExecution`, |
| 66 | * `ListExecutions`, …): the runtime callable injects the bound |
| 67 | * {@link StateMachine}'s ARN and the deploy-time half grants `actions` on |
| 68 | * it. |
| 69 | */ |
| 70 | export const makeStateMachineArnHttpBinding = < |
| 71 | I extends { stateMachineArn?: string }, |
| 72 | A, |
| 73 | E, |
| 74 | R, |
| 75 | >(options: { |
| 76 | /** Fully-qualified binding tag, e.g. `AWS.StepFunctions.StartExecution`. */ |
| 77 | tag: string; |
| 78 | /** The distilled operation; the machine ARN is injected as `stateMachineArn`. */ |
| 79 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 80 | /** IAM actions granted on the state machine ARN. */ |
| 81 | actions: readonly string[]; |
| 82 | }) => |
| 83 | Effect.gen(function* () { |
| 84 | const grant = yield* Grant; |
| 85 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 86 | |
| 87 | return Effect.fn(function* (stateMachine: StateMachine) { |
| 88 | const StateMachineArn = yield* stateMachine.stateMachineArn; |
| 89 | yield* grant(options.tag, stateMachine, options.actions, [stateMachine.stateMachineArn]); |
| 90 | return Effect.fn(`${options.tag}(${stateMachine.LogicalId})`)(function* ( |
| 91 | request?: Omit<I, "stateMachineArn">, |
| 92 | ) { |
| 93 | return yield* op({ |
| 94 | ...request, |
| 95 | stateMachineArn: yield* StateMachineArn, |
| 96 | } as I); |
| 97 | }); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | /** |
| 102 | * Build the impl Effect for an operation addressed by an execution or Map |
| 103 | * Run ARN the caller already holds (`DescribeExecution`, `StopExecution`, |
| 104 | * `GetExecutionHistory`, `RedriveExecution`, `DescribeMapRun`, …): the |
| 105 | * runtime callable passes the request through unchanged while the |
| 106 | * deploy-time half grants `actions` on the bound {@link StateMachine}'s |
| 107 | * execution (or Map Run) ARN pattern. |
| 108 | */ |
| 109 | export const makeExecutionScopedHttpBinding = <I, A, E, R>(options: { |
| 110 | /** Fully-qualified binding tag, e.g. `AWS.StepFunctions.StopExecution`. */ |
| 111 | tag: string; |
| 112 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 113 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 114 | /** IAM actions granted on the derived ARN pattern. */ |
| 115 | actions: readonly string[]; |
| 116 | /** |
| 117 | * Which derived ARN pattern the grant targets. |
| 118 | * @default "execution" |
| 119 | */ |
| 120 | scope?: "execution" | "mapRun"; |
| 121 | }) => |
| 122 | Effect.gen(function* () { |
| 123 | const grant = yield* Grant; |
| 124 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 125 | |
| 126 | return Effect.fn(function* (stateMachine: StateMachine) { |
| 127 | yield* grant(options.tag, stateMachine, options.actions, [ |
| 128 | options.scope === "mapRun" |
| 129 | ? mapRunArnPattern(stateMachine) |
| 130 | : executionArnPattern(stateMachine), |
| 131 | ]); |
| 132 | return Effect.fn(`${options.tag}(${stateMachine.LogicalId})`)(function* (request: I) { |
| 133 | return yield* op(request); |
| 134 | }); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | /** |
| 139 | * Build the impl Effect for an activity-scoped operation whose request |
| 140 | * carries the activity identity as `activityArn` (`GetActivityTask`): the |
| 141 | * runtime callable injects the bound {@link Activity}'s ARN and the |
| 142 | * deploy-time half grants `actions` on it. |
| 143 | */ |
| 144 | export const makeActivityArnHttpBinding = <I extends { activityArn?: string }, A, E, R>(options: { |
| 145 | /** Fully-qualified binding tag, e.g. `AWS.StepFunctions.GetActivityTask`. */ |
| 146 | tag: string; |
| 147 | /** The distilled operation; the activity ARN is injected as `activityArn`. */ |
| 148 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 149 | /** IAM actions granted on the activity ARN. */ |
| 150 | actions: readonly string[]; |
| 151 | }) => |
| 152 | Effect.gen(function* () { |
| 153 | const grant = yield* Grant; |
| 154 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 155 | |
| 156 | return Effect.fn(function* (activity: Activity) { |
| 157 | const ActivityArn = yield* activity.activityArn; |
| 158 | yield* grant(options.tag, activity, options.actions, [activity.activityArn]); |
| 159 | return Effect.fn(`${options.tag}(${activity.LogicalId})`)(function* ( |
| 160 | request?: Omit<I, "activityArn">, |
| 161 | ) { |
| 162 | return yield* op({ |
| 163 | ...request, |
| 164 | activityArn: yield* ActivityArn, |
| 165 | } as I); |
| 166 | }); |
| 167 | }); |
| 168 | }); |
| 169 | |
| 170 | /** |
| 171 | * Build the impl Effect for a task-token callback operation |
| 172 | * (`SendTaskSuccess`/`SendTaskFailure`/`SendTaskHeartbeat`): task tokens |
| 173 | * from `.waitForTaskToken` service integrations carry no IAM resource, so |
| 174 | * the grant is `*` unless an {@link Activity} is bound (only Activity tasks |
| 175 | * support resource-level scoping). The request passes through unchanged. |
| 176 | */ |
| 177 | export const makeTaskCallbackHttpBinding = <I, A, E, R>(options: { |
| 178 | /** Fully-qualified binding tag, e.g. `AWS.StepFunctions.SendTaskSuccess`. */ |
| 179 | tag: string; |
| 180 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 181 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 182 | /** IAM actions granted on the activity ARN (or `*`). */ |
| 183 | actions: readonly string[]; |
| 184 | }) => |
| 185 | Effect.gen(function* () { |
| 186 | const grant = yield* Grant; |
| 187 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 188 | |
| 189 | return Effect.fn(function* (activity?: Activity) { |
| 190 | yield* grant(options.tag, activity ?? "*", options.actions, [ |
| 191 | activity ? Output.interpolate`${activity.activityArn}` : "*", |
| 192 | ]); |
| 193 | return Effect.fn(`${options.tag}(${activity?.LogicalId})`)(function* (request: I) { |
| 194 | return yield* op(request); |
| 195 | }); |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | /** |
| 200 | * Build the impl Effect for a zero-arity, service-scoped Step Functions |
| 201 | * binding (`TestState`, `ValidateStateMachineDefinition`): registers the |
| 202 | * service-scoped IAM policy statement on the host (deploy time) and passes |
| 203 | * the request through unchanged (runtime). |
| 204 | */ |
| 205 | export const makeSfnServiceHttpBinding = <I, A, E, R>(options: { |
| 206 | /** Fully-qualified binding tag, e.g. `AWS.StepFunctions.TestState`. */ |
| 207 | tag: string; |
| 208 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 209 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 210 | /** IAM actions the operation needs (service-scoped, `Resource: ["*"]`). */ |
| 211 | actions: readonly string[]; |
| 212 | }) => |
| 213 | Effect.gen(function* () { |
| 214 | const bind = yield* AWSBindings.Bind; |
| 215 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 216 | |
| 217 | return Effect.fn(function* () { |
| 218 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 219 | yield* bind`${options.tag}()`({ |
| 220 | policyStatements: [ |
| 221 | { |
| 222 | Effect: "Allow", |
| 223 | Action: [...options.actions], |
| 224 | Resource: ["*"], |
| 225 | }, |
| 226 | ], |
| 227 | }); |
| 228 | } |
| 229 | return Effect.fn(options.tag)(function* (request: I) { |
| 230 | return yield* op(request); |
| 231 | }); |
| 232 | }); |
| 233 | }); |