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

main
ts 179 lines 6.37 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 import type { Dataset } from "./Dataset.ts";
5 import type { EventTracker } from "./EventTracker.ts";
6
7 /**
8 * Shared HTTP scaffolding for the Amazon Personalize runtime bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
12 * builders below. Everything except the distilled operation, the identifier
13 * injection, and the IAM action list is boilerplate:
14 *
15 * - dataset-scoped operations (`PutItems`, `PutUsers`, `PutActions`) inject
16 * the bound {@link Dataset}'s ARN as `datasetArn` and grant on it,
17 * - event-tracker-scoped operations (`PutEvents`, `PutActionInteractions`)
18 * inject the bound {@link EventTracker}'s `trackingId`; per the service
19 * authorization reference these actions support no resource types, so the
20 * grant is on `Resource: ["*"]`,
21 * - account-level operations (the `personalize-runtime` recommenders and the
22 * MLOps retraining loop) address campaigns / solutions / import jobs whose
23 * ARNs are created at runtime, so they grant on `Resource: ["*"]`.
24 */
25
26 /**
27 * Build the impl Effect for a dataset-scoped Personalize operation: the
28 * runtime callable injects the bound {@link Dataset}'s ARN as `datasetArn`
29 * and the deploy-time half grants `actions` on the dataset ARN.
30 */
31 export const makePersonalizeDatasetHttpBinding = <
32 I extends { datasetArn: string },
33 A,
34 E,
35 R,
36 >(options: {
37 /** Fully-qualified binding tag, e.g. `AWS.Personalize.PutItems`. */
38 tag: string;
39 /** The distilled operation; `datasetArn` is injected from the dataset. */
40 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
41 /** IAM actions granted on the dataset ARN. */
42 actions: readonly string[];
43 }) =>
44 Effect.gen(function* () {
45 const bind = yield* AWSBindings.Bind;
46 const op = yield* AWSBindings.withCredentials(options.operation);
47
48 return Effect.fn(function* (dataset: Dataset) {
49 const datasetArn = yield* dataset.datasetArn;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${dataset})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: [dataset.datasetArn],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${dataset.LogicalId})`)(function* (
62 request: Omit<I, "datasetArn">,
63 ) {
64 return yield* op({
65 ...request,
66 datasetArn: yield* datasetArn,
67 } as I);
68 });
69 });
70 });
71
72 /**
73 * Build the impl Effect for an event-tracker-scoped Personalize operation:
74 * the runtime callable injects the bound {@link EventTracker}'s tracking ID
75 * as `trackingId`. The deploy-time half grants `actions` on `Resource: ["*"]`
76 * — `personalize:PutEvents` / `personalize:PutActionInteractions` support no
77 * resource types (a tracker-ARN-scoped grant answers AccessDeniedException).
78 */
79 export const makePersonalizeEventTrackerHttpBinding = <
80 I extends { trackingId: string },
81 A,
82 E,
83 R,
84 >(options: {
85 /** Fully-qualified binding tag, e.g. `AWS.Personalize.PutEvents`. */
86 tag: string;
87 /** The distilled operation; `trackingId` is injected from the tracker. */
88 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
89 /** IAM actions granted on the event tracker ARN. */
90 actions: readonly string[];
91 }) =>
92 Effect.gen(function* () {
93 const bind = yield* AWSBindings.Bind;
94 const op = yield* AWSBindings.withCredentials(options.operation);
95
96 return Effect.fn(function* (tracker: EventTracker) {
97 const trackingId = yield* tracker.trackingId;
98 if (!globalThis.__ALCHEMY_RUNTIME__) {
99 yield* bind`${options.tag}(${tracker})`({
100 policyStatements: [
101 {
102 Effect: "Allow",
103 Action: [...options.actions],
104 // The event-ingestion actions support no resource types.
105 Resource: ["*"],
106 },
107 ],
108 });
109 }
110 return Effect.fn(`${options.tag}(${tracker.LogicalId})`)(function* (
111 request: Omit<I, "trackingId">,
112 ) {
113 return yield* op({
114 ...request,
115 trackingId: yield* trackingId,
116 } as I);
117 });
118 });
119 });
120
121 /**
122 * Build the impl Effect for an account-level Personalize operation (the
123 * `personalize-runtime` recommendation calls and the MLOps retraining loop):
124 * campaigns, solutions, and import jobs are addressed by ARNs the caller
125 * supplies at runtime, so the deploy-time half grants `actions` on
126 * `Resource: ["*"]`.
127 */
128 export const makePersonalizeAccountHttpBinding = <I, A, E, R>(options: {
129 /** Fully-qualified binding tag, e.g. `AWS.Personalize.GetRecommendations`. */
130 tag: string;
131 /** The distilled operation implementing the capability. */
132 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
133 /**
134 * IAM actions granted on `Resource: ["*"]` (the target ARNs are chosen by
135 * the caller at runtime).
136 */
137 actions: readonly string[];
138 /**
139 * Grant `iam:PassRole` (conditioned to `personalize.amazonaws.com`) so the
140 * function can hand Personalize the role it assumes to read S3 training
141 * data. Set on `CreateDatasetImportJob`, which accepts a `roleArn`.
142 */
143 passRole?: boolean;
144 }) =>
145 Effect.gen(function* () {
146 const bind = yield* AWSBindings.Bind;
147 const op = yield* AWSBindings.withCredentials(options.operation);
148
149 return Effect.fn(function* () {
150 if (!globalThis.__ALCHEMY_RUNTIME__) {
151 const policyStatements: PolicyStatement[] = [
152 {
153 Effect: "Allow",
154 Action: [...options.actions],
155 // Campaign/solution/import-job ARNs are chosen at runtime.
156 Resource: ["*"],
157 },
158 ];
159 if (options.passRole) {
160 policyStatements.push({
161 Effect: "Allow",
162 Action: ["iam:PassRole"],
163 Resource: ["*"],
164 Condition: {
165 StringEquals: {
166 "iam:PassedToService": "personalize.amazonaws.com",
167 },
168 },
169 });
170 }
171 yield* bind`${options.tag}()`({
172 policyStatements,
173 });
174 }
175 return Effect.fn(options.tag)(function* (request: I) {
176 return yield* op(request);
177 });
178 });
179 });