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

main
ts 77 lines 2.74 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { PolicyStatement } from "../IAM/Policy.ts";
4
5 /**
6 * Shared HTTP scaffolding for the Amazon Forecast runtime bindings.
7 *
8 * The runtime-targeted Forecast objects (dataset import jobs, predictors,
9 * forecasts) are created *at runtime* with caller-chosen names, so their ARNs
10 * are unknowable at deploy time — every binding is account-level and grants
11 * its action(s) on `Resource: ["*"]`. The only variation between bindings is
12 * the distilled operation, the IAM action, and whether the operation passes
13 * a data-access role to the service (`CreateDatasetImportJob` /
14 * `CreateAutoPredictor`, which additionally need `iam:PassRole` conditioned
15 * to `forecast.amazonaws.com`).
16 *
17 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
18 * a `Layer.effect(Cap, makeForecastHttpBinding({ … }))`.
19 */
20 export const makeForecastHttpBinding = <I, A, E, R>(options: {
21 /**
22 * Short capability name used in the binding sid and runtime span, e.g.
23 * `"QueryForecast"`.
24 */
25 capability: string;
26 /**
27 * IAM actions granted on `Resource: ["*"]` (the target ARNs are created at
28 * runtime and unknowable at deploy time).
29 */
30 iamActions: readonly string[];
31 /**
32 * The distilled operation implementing the capability.
33 */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /**
36 * Grant `iam:PassRole` (conditioned to `forecast.amazonaws.com`) so the
37 * function can hand the service the role it assumes to read S3 training
38 * data or use a customer KMS key. Set on the `Create*` bindings that accept
39 * a `RoleArn`.
40 */
41 passRole?: boolean;
42 }) =>
43 Effect.gen(function* () {
44 const bind = yield* AWSBindings.Bind;
45 const op = yield* AWSBindings.withCredentials(options.operation);
46
47 return Effect.fn(function* () {
48 if (!globalThis.__ALCHEMY_RUNTIME__) {
49 const policyStatements: PolicyStatement[] = [
50 {
51 Effect: "Allow",
52 Action: [...options.iamActions],
53 // Runtime-created Forecast ARNs are unknowable at deploy time.
54 Resource: ["*"],
55 },
56 ];
57 if (options.passRole) {
58 policyStatements.push({
59 Effect: "Allow",
60 Action: ["iam:PassRole"],
61 Resource: ["*"],
62 Condition: {
63 StringEquals: {
64 "iam:PassedToService": "forecast.amazonaws.com",
65 },
66 },
67 });
68 }
69 yield* bind`AWS.Forecast.${options.capability}()`({
70 policyStatements,
71 });
72 }
73 return Effect.fn(`AWS.Forecast.${options.capability}`)(function* (request: I) {
74 return yield* op(request);
75 });
76 });
77 });