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 { Workflow } from "./Workflow.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for the Amazon MWAA Serverless HTTP bindings. |
| 8 | * |
| 9 | * Every MWAA Serverless data-plane operation is addressed by a |
| 10 | * `WorkflowArn`, so every binding is scoped to a bound {@link Workflow}: |
| 11 | * the deploy-time half grants `actions` on the workflow's ARN (plus the |
| 12 | * `{workflowArn}/*` wildcard covering run- and task-scoped sub-resources), |
| 13 | * and the runtime half injects the workflow's ARN into every request. |
| 14 | * |
| 15 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 16 | * a `Layer.effect(Cap, makeMwaaServerlessHttpBinding({ … }))`. |
| 17 | */ |
| 18 | export const makeMwaaServerlessHttpBinding = <I extends { WorkflowArn: string }, A, E, R>(options: { |
| 19 | /** Fully-qualified binding tag, e.g. `AWS.MWAAServerless.StartWorkflowRun`. */ |
| 20 | tag: string; |
| 21 | /** The distilled operation. */ |
| 22 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 23 | /** IAM actions granted on the workflow ARN (+ `{arn}/*`). */ |
| 24 | actions: readonly string[]; |
| 25 | }) => |
| 26 | Effect.gen(function* () { |
| 27 | const bind = yield* AWSBindings.Bind; |
| 28 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 29 | |
| 30 | return Effect.fn(function* (workflow: Workflow) { |
| 31 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 32 | const WorkflowArn = yield* workflow.workflowArn; |
| 33 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 34 | yield* bind`${options.tag}(${workflow})`({ |
| 35 | policyStatements: [ |
| 36 | { |
| 37 | Effect: "Allow", |
| 38 | Action: [...options.actions], |
| 39 | Resource: [ |
| 40 | workflow.workflowArn, |
| 41 | // run/task-scoped operations authorize against |
| 42 | // sub-resources below the workflow ARN |
| 43 | Output.interpolate`${workflow.workflowArn}/*`, |
| 44 | ], |
| 45 | }, |
| 46 | ], |
| 47 | }); |
| 48 | } |
| 49 | return Effect.fn(`${options.tag}(${workflow.LogicalId})`)(function* ( |
| 50 | request?: Omit<I, "WorkflowArn">, |
| 51 | ) { |
| 52 | return yield* op({ |
| 53 | ...request, |
| 54 | WorkflowArn: yield* WorkflowArn, |
| 55 | } as I); |
| 56 | }); |
| 57 | }); |
| 58 | }); |