@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 118 lines 4.4 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 { Role } from "../IAM/Role.ts";
5
6 /**
7 * Shared scaffolding for AWS Transcribe 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: Amazon Transcribe's batch APIs have no resource-level IAM, so
13 * every grant is on `*`. The role-bound operations (`StartCallAnalyticsJob`,
14 * `StartMedicalScribeJob`, `CreateLanguageModel`) additionally inject the
15 * bound data-access role and a scoped `iam:PassRole` grant.
16 */
17
18 /**
19 * Build the impl Effect for a Transcribe operation with no bound resource
20 * (start/get/list/delete jobs, vocabulary/filter/category lifecycle, tags).
21 * The deploy-time half grants `actions` on `*` — Transcribe batch IAM
22 * actions do not support resource-level scoping.
23 */
24 export const makeTranscribeHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.Transcribe.GetTranscriptionJob`. */
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* () {
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}()`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(options.tag)(function* (request?: I) {
49 return yield* op((request ?? {}) as I);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for a role-bound Transcribe operation
56 * (`StartCallAnalyticsJob`, `StartMedicalScribeJob`, `CreateLanguageModel`):
57 * the binding is constructed with the **data-access role** (the IAM role
58 * Amazon Transcribe assumes to read your Amazon S3 media/training data and
59 * write results; its trust policy must allow `transcribe.amazonaws.com`).
60 * The role's ARN is injected as `DataAccessRoleArn` on every runtime request
61 * and the deploy-time half grants `actions` on `*` plus `iam:PassRole` on
62 * the role — without the PassRole grant, the operation fails only at runtime
63 * with an AccessDenied.
64 */
65 export const makeTranscribeRoleHttpBinding = <
66 I extends { DataAccessRoleArn?: string },
67 A,
68 E,
69 R,
70 >(options: {
71 /** Fully-qualified binding tag, e.g. `AWS.Transcribe.StartMedicalScribeJob`. */
72 tag: string;
73 /** The distilled operation; `DataAccessRoleArn` is injected from the role. */
74 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
75 /** IAM actions granted on `*`. */
76 actions: readonly string[];
77 }) =>
78 Effect.gen(function* () {
79 const bind = yield* AWSBindings.Bind;
80 const op = yield* AWSBindings.withCredentials(options.operation);
81
82 return Effect.fn(function* <Rl extends Role>(dataAccessRole: Rl) {
83 const RoleArn = yield* dataAccessRole.roleArn;
84 if (!globalThis.__ALCHEMY_RUNTIME__) {
85 yield* bind`${options.tag}(${dataAccessRole})`({
86 policyStatements: [
87 {
88 Effect: "Allow",
89 Action: [...options.actions],
90 Resource: ["*"],
91 },
92 // CRITICAL: without iam:PassRole on the data-access role, the
93 // operation fails only at runtime with an AccessDenied.
94 {
95 Effect: "Allow",
96 Action: ["iam:PassRole"],
97 Resource: [Output.interpolate`${dataAccessRole.roleArn}`],
98 Condition: {
99 StringEquals: {
100 "iam:PassedToService": "transcribe.amazonaws.com",
101 },
102 },
103 },
104 ],
105 });
106 }
107 return Effect.fn(`${options.tag}(${dataAccessRole.LogicalId})`)(function* (
108 request: Omit<I, "DataAccessRoleArn"> & {
109 DataAccessRoleArn?: string;
110 },
111 ) {
112 return yield* op({
113 ...request,
114 DataAccessRoleArn: request.DataAccessRoleArn ?? (yield* RoleArn),
115 } as I);
116 });
117 });
118 });