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 { Role } from "../IAM/Role.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AWS Translate HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 10 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two |
| 11 | * builders below. Everything except the operation and the IAM action list is |
| 12 | * boilerplate. The bindings take terminology/parallel-data/job identifiers |
| 13 | * at runtime (not as bound resources), so grants are on `Resource: ["*"]`. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Build the impl Effect for a Translate operation with no bound resource |
| 18 | * (real-time translation, terminology/parallel-data reads, job |
| 19 | * describe/list/stop). |
| 20 | */ |
| 21 | export const makeTranslateHttpBinding = <I, A, E, R>(options: { |
| 22 | /** Fully-qualified binding tag, e.g. `AWS.Translate.TranslateText`. */ |
| 23 | tag: string; |
| 24 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 25 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 26 | /** IAM actions granted on `*`. */ |
| 27 | actions: readonly string[]; |
| 28 | }) => |
| 29 | Effect.gen(function* () { |
| 30 | const bind = yield* AWSBindings.Bind; |
| 31 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 32 | |
| 33 | return Effect.fn(function* () { |
| 34 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 35 | yield* bind`${options.tag}()`({ |
| 36 | policyStatements: [ |
| 37 | { |
| 38 | Effect: "Allow", |
| 39 | Action: [...options.actions], |
| 40 | Resource: ["*"], |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | return Effect.fn(options.tag)(function* (request?: I) { |
| 46 | return yield* op((request ?? {}) as I); |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | /** |
| 52 | * Build the impl Effect for `StartTextTranslationJob`: the binding is |
| 53 | * constructed with the **data-access role** (the IAM role Amazon Translate |
| 54 | * assumes to read input documents from S3 and write results; its trust |
| 55 | * policy must allow `translate.amazonaws.com`). The role's ARN is injected |
| 56 | * as `DataAccessRoleArn` on every runtime request, and the deploy-time half |
| 57 | * grants `actions` on `*` plus `iam:PassRole` on the role — without the |
| 58 | * PassRole grant, the start call fails only at runtime with an AccessDenied. |
| 59 | * `ClientToken` is optional on the runtime request (the AWS SDK also |
| 60 | * auto-generates it) — a UUID is generated per call when omitted. |
| 61 | */ |
| 62 | export const makeTranslateStartJobHttpBinding = < |
| 63 | I extends { DataAccessRoleArn: string; ClientToken: string }, |
| 64 | A, |
| 65 | E, |
| 66 | R, |
| 67 | >(options: { |
| 68 | /** Fully-qualified binding tag, e.g. `AWS.Translate.StartTextTranslationJob`. */ |
| 69 | tag: string; |
| 70 | /** The distilled operation; `DataAccessRoleArn` is injected from the role. */ |
| 71 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 72 | /** IAM actions granted on `*`. */ |
| 73 | actions: readonly string[]; |
| 74 | }) => |
| 75 | Effect.gen(function* () { |
| 76 | const bind = yield* AWSBindings.Bind; |
| 77 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 78 | |
| 79 | return Effect.fn(function* <Rl extends Role>(dataAccessRole: Rl) { |
| 80 | const RoleArn = yield* dataAccessRole.roleArn; |
| 81 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 82 | yield* bind`${options.tag}(${dataAccessRole})`({ |
| 83 | policyStatements: [ |
| 84 | { |
| 85 | Effect: "Allow", |
| 86 | Action: [...options.actions], |
| 87 | Resource: ["*"], |
| 88 | }, |
| 89 | // CRITICAL: without iam:PassRole on the data-access role, the |
| 90 | // start call fails only at runtime with an AccessDenied. |
| 91 | { |
| 92 | Effect: "Allow", |
| 93 | Action: ["iam:PassRole"], |
| 94 | Resource: [Output.interpolate`${dataAccessRole.roleArn}`], |
| 95 | Condition: { |
| 96 | StringEquals: { |
| 97 | "iam:PassedToService": "translate.amazonaws.com", |
| 98 | }, |
| 99 | }, |
| 100 | }, |
| 101 | ], |
| 102 | }); |
| 103 | } |
| 104 | return Effect.fn(`${options.tag}(${dataAccessRole.LogicalId})`)(function* ( |
| 105 | request: Omit<I, "DataAccessRoleArn" | "ClientToken"> & { |
| 106 | DataAccessRoleArn?: string; |
| 107 | ClientToken?: string; |
| 108 | }, |
| 109 | ) { |
| 110 | return yield* op({ |
| 111 | ...request, |
| 112 | DataAccessRoleArn: request.DataAccessRoleArn ?? (yield* RoleArn), |
| 113 | ClientToken: request.ClientToken ?? (yield* Effect.sync(() => crypto.randomUUID())), |
| 114 | } as I); |
| 115 | }); |
| 116 | }); |
| 117 | }); |