@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 401 lines 15.7 KB
Raw
1 import * as personalize from "@distilled.cloud/aws/personalize";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import type { Providers } from "../Providers.ts";
13 import { readPersonalizeTags, syncPersonalizeTags } from "./internal.ts";
14
15 export class PersonalizeDatasetGroupNotReady extends Data.TaggedError(
16 "PersonalizeDatasetGroupNotReady",
17 )<{
18 readonly message: string;
19 readonly cause?: unknown;
20 }> {}
21
22 export interface DatasetGroupProps {
23 /**
24 * Name of the dataset group. If omitted, a unique name is generated from the
25 * app, stage, and logical ID. Changing the name replaces the group.
26 */
27 name?: string;
28 /**
29 * ARN of an IAM role Personalize assumes to access a customer-managed KMS
30 * key. Immutable — changing it replaces the group.
31 */
32 roleArn?: string;
33 /**
34 * ARN of a customer-managed KMS key used to encrypt the datasets. Immutable
35 * — changing it replaces the group.
36 */
37 kmsKeyArn?: string;
38 /**
39 * The domain of a domain dataset group (`ECOMMERCE` or `VIDEO_ON_DEMAND`).
40 * Omit for a custom dataset group. Immutable — changing it replaces the
41 * group.
42 */
43 domain?: string;
44 /**
45 * User-defined tags for the dataset group.
46 */
47 tags?: Record<string, string>;
48 }
49
50 export interface DatasetGroup extends Resource<
51 "AWS.Personalize.DatasetGroup",
52 DatasetGroupProps,
53 {
54 /**
55 * ARN of the dataset group.
56 */
57 datasetGroupArn: string;
58 /**
59 * Name of the dataset group.
60 */
61 name: string;
62 /**
63 * Dataset group status (e.g. `ACTIVE`, `CREATE PENDING`).
64 */
65 status: string;
66 /**
67 * Domain of the dataset group (`ECOMMERCE` or `VIDEO_ON_DEMAND`) when
68 * it is a domain dataset group.
69 */
70 domain: string | undefined;
71 },
72 never,
73 Providers
74 > {}
75
76 /**
77 * An Amazon Personalize dataset group — the top-level container that holds the
78 * datasets, solutions, and campaigns for a single use case. Creating a dataset
79 * group is cheap and fast; the expensive training work lives in solutions and
80 * campaigns provisioned separately.
81 *
82 * ### Creating a Dataset Group
83 * **Example:** Custom Dataset Group
84 * ```typescript
85 * const group = yield* Personalize.DatasetGroup("Recommendations", {});
86 * ```
87 *
88 * **Example:** Domain Dataset Group with Encryption
89 * ```typescript
90 * const group = yield* Personalize.DatasetGroup("Storefront", {
91 * domain: "ECOMMERCE",
92 * roleArn: role.roleArn,
93 * kmsKeyArn: key.keyArn,
94 * tags: { team: "growth" },
95 * });
96 * ```
97 *
98 * @resource
99 */
100 export const DatasetGroup = Resource<DatasetGroup>("AWS.Personalize.DatasetGroup");
101
102 export const DatasetGroupProvider = () =>
103 Provider.effect(
104 DatasetGroup,
105 Effect.gen(function* () {
106 const createName = Effect.fn(function* (id: string, props: DatasetGroupProps) {
107 return props.name ?? (yield* createPhysicalName({ id, maxLength: 63 }));
108 });
109
110 const describe = Effect.fn(function* (datasetGroupArn: string) {
111 const response = yield* personalize
112 .describeDatasetGroup({ datasetGroupArn })
113 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
114 return response?.datasetGroup;
115 });
116
117 /** Poll until the group reaches ACTIVE; fail fast on CREATE FAILED. */
118 const waitActive = Effect.fn(function* (datasetGroupArn: string) {
119 const group = yield* describe(datasetGroupArn).pipe(
120 Effect.repeat({
121 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(40)]),
122 until: (g) => g?.status === "ACTIVE" || (g?.status ?? "").includes("FAILED"),
123 }),
124 );
125 if (group?.status !== "ACTIVE") {
126 return yield* new PersonalizeDatasetGroupNotReady({
127 message: `Personalize dataset group ${datasetGroupArn} did not become ACTIVE (status: ${group?.status}, reason: ${group?.failureReason})`,
128 });
129 }
130 return group;
131 });
132
133 /** Find an existing dataset group's ARN by its (deterministic) name. */
134 const findArnByName = Effect.fn(function* (name: string) {
135 const pages = yield* personalize.listDatasetGroups.pages({}).pipe(Stream.runCollect);
136 return Array.from(pages)
137 .flatMap((page) => page.datasetGroups ?? [])
138 .find((summary) => summary.name === name)?.datasetGroupArn;
139 });
140
141 const toAttrs = (group: personalize.DatasetGroup) => ({
142 datasetGroupArn: group.datasetGroupArn!,
143 name: group.name!,
144 status: group.status!,
145 domain: group.domain,
146 });
147
148 /** Bounded retry schedule for ResourceInUse while async deletes drain. */
149 const inUseRetry = {
150 while: (e: { _tag: string }) => e._tag === "ResourceInUseException",
151 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
152 };
153
154 const listChildren = Effect.fn(function* (datasetGroupArn: string) {
155 const [trackers, filters, solutions, datasets] = yield* Effect.all(
156 [
157 personalize.listEventTrackers.pages({ datasetGroupArn }).pipe(
158 Stream.runCollect,
159 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.eventTrackers ?? [])),
160 ),
161 personalize.listFilters.pages({ datasetGroupArn }).pipe(
162 Stream.runCollect,
163 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Filters ?? [])),
164 ),
165 personalize.listSolutions.pages({ datasetGroupArn }).pipe(
166 Stream.runCollect,
167 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.solutions ?? [])),
168 ),
169 personalize.listDatasets.pages({ datasetGroupArn }).pipe(
170 Stream.runCollect,
171 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.datasets ?? [])),
172 ),
173 ],
174 { concurrency: 4 },
175 );
176 return { trackers, filters, solutions, datasets };
177 });
178
179 /**
180 * DeleteDatasetGroup requires all children (event trackers, filters,
181 * campaigns, solutions, datasets) to be deleted first. Children created
182 * out-of-band (e.g. via CreateSolution/CreateCampaign runtime bindings)
183 * are not tracked in state, so the group drains any that remain. Each
184 * child delete is idempotent (NotFound tolerated) and asynchronous, so
185 * after issuing the deletes we poll (bounded) until the group is empty.
186 */
187 const drainChildren = Effect.fn(function* (datasetGroupArn: string) {
188 const { trackers, filters, solutions, datasets } = yield* listChildren(datasetGroupArn);
189
190 yield* Effect.forEach(
191 trackers,
192 (t) =>
193 personalize.deleteEventTracker({ eventTrackerArn: t.eventTrackerArn! }).pipe(
194 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
195 Effect.retry(inUseRetry),
196 ),
197 { concurrency: 4 },
198 );
199
200 yield* Effect.forEach(
201 filters,
202 (f) =>
203 personalize.deleteFilter({ filterArn: f.filterArn! }).pipe(
204 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
205 Effect.retry(inUseRetry),
206 ),
207 { concurrency: 4 },
208 );
209
210 // Campaigns block their solution's deletion, so drain them per
211 // solution before deleting the solution.
212 yield* Effect.forEach(
213 solutions,
214 Effect.fn(function* (s) {
215 const campaigns = yield* personalize.listCampaigns
216 .pages({ solutionArn: s.solutionArn })
217 .pipe(
218 Stream.runCollect,
219 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.campaigns ?? [])),
220 );
221 yield* Effect.forEach(
222 campaigns,
223 (c) =>
224 personalize.deleteCampaign({ campaignArn: c.campaignArn! }).pipe(
225 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
226 Effect.retry(inUseRetry),
227 ),
228 { concurrency: 4 },
229 );
230 yield* personalize.deleteSolution({ solutionArn: s.solutionArn! }).pipe(
231 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
232 // Campaign deletion is async — retry while it drains.
233 Effect.retry(inUseRetry),
234 );
235 }),
236 { concurrency: 2 },
237 );
238
239 yield* Effect.forEach(
240 datasets,
241 (d) =>
242 personalize.deleteDataset({ datasetArn: d.datasetArn! }).pipe(
243 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
244 Effect.retry(inUseRetry),
245 ),
246 { concurrency: 4 },
247 );
248
249 // All child deletes are asynchronous — wait (bounded) until the group
250 // reports no remaining children so DeleteDatasetGroup succeeds.
251 const remaining = yield* listChildren(datasetGroupArn).pipe(
252 Effect.repeat({
253 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
254 until: (children): boolean =>
255 children.trackers.length === 0 &&
256 children.filters.length === 0 &&
257 children.solutions.length === 0 &&
258 children.datasets.length === 0,
259 }),
260 );
261 if (
262 remaining.trackers.length +
263 remaining.filters.length +
264 remaining.solutions.length +
265 remaining.datasets.length >
266 0
267 ) {
268 return yield* new PersonalizeDatasetGroupNotReady({
269 message: `Personalize dataset group ${datasetGroupArn} still has children after drain (trackers: ${remaining.trackers.length}, filters: ${remaining.filters.length}, solutions: ${remaining.solutions.length}, datasets: ${remaining.datasets.length})`,
270 });
271 }
272 });
273
274 return {
275 stables: ["datasetGroupArn", "name"],
276
277 diff: Effect.fn(function* ({ id, olds = {}, news }) {
278 if (!isResolved(news)) return undefined;
279 const oldName = yield* createName(id, olds);
280 const newName = yield* createName(id, news);
281 if (
282 oldName !== newName ||
283 (olds.roleArn ?? undefined) !== (news.roleArn ?? undefined) ||
284 (olds.kmsKeyArn ?? undefined) !== (news.kmsKeyArn ?? undefined) ||
285 (olds.domain ?? undefined) !== (news.domain ?? undefined)
286 ) {
287 return { action: "replace" } as const;
288 }
289 }),
290
291 read: Effect.fn(function* ({ id, output }) {
292 if (!output?.datasetGroupArn) return undefined;
293 const group = yield* describe(output.datasetGroupArn);
294 if (group === undefined) return undefined;
295 const attrs = toAttrs(group);
296 const tags = yield* readPersonalizeTags(group.datasetGroupArn!);
297 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
298 }),
299
300 reconcile: Effect.fn(function* ({ id, news = {}, output, session }) {
301 const name = yield* createName(id, news);
302 const internalTags = yield* createInternalTags(id);
303 const desiredTags = { ...internalTags, ...news.tags };
304
305 // 1. Observe — cloud state is authoritative; output is an ARN cache.
306 let group =
307 output?.datasetGroupArn !== undefined
308 ? yield* describe(output.datasetGroupArn)
309 : undefined;
310
311 // 2. Ensure — create if missing, then wait for ACTIVE. A crashed
312 // prior run may have left a same-named group behind with no
313 // persisted state — adopt it by name and converge.
314 if (group === undefined) {
315 const arn = yield* personalize
316 .createDatasetGroup({
317 name,
318 roleArn: news.roleArn,
319 kmsKeyArn: news.kmsKeyArn,
320 domain: news.domain,
321 tags: Object.entries(desiredTags).map(([tagKey, tagValue]) => ({
322 tagKey,
323 tagValue,
324 })),
325 })
326 .pipe(
327 Effect.map((created) => created.datasetGroupArn!),
328 Effect.catchTag("ResourceAlreadyExistsException", (error) =>
329 findArnByName(name).pipe(
330 Effect.flatMap((existing) =>
331 existing === undefined ? Effect.fail(error) : Effect.succeed(existing),
332 ),
333 ),
334 ),
335 );
336 group = yield* waitActive(arn);
337 }
338
339 // 3. Sync tags — diff against OBSERVED cloud tags (no-op after a
340 // fresh create; converges an adopted leftover).
341 yield* syncPersonalizeTags(group.datasetGroupArn!, desiredTags);
342
343 yield* session.note(group.datasetGroupArn!);
344 return toAttrs(group);
345 }),
346
347 delete: Effect.fn(function* ({ output }) {
348 // DeleteDatasetGroup requires every child — event trackers, filters,
349 // campaigns, solutions, and datasets — to be deleted first, and each
350 // child delete is itself asynchronous. Deletion ordering can't
351 // guarantee children-before-parent (bindings/tests create children
352 // out-of-band that are not tracked in state), so drain the group's
353 // remaining children before deleting the group itself. Every step is
354 // idempotent and NotFound-tolerant.
355 const group = yield* describe(output.datasetGroupArn);
356 if (group !== undefined) {
357 yield* drainChildren(output.datasetGroupArn);
358 }
359 yield* personalize.deleteDatasetGroup({ datasetGroupArn: output.datasetGroupArn }).pipe(
360 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
361 // Tolerate the eventual-consistency window while the drained
362 // children finish their own asynchronous deletions.
363 Effect.retry({
364 while: (e) => e._tag === "ResourceInUseException",
365 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
366 }),
367 );
368 // Deletion is asynchronous (DELETE IN_PROGRESS) — wait until the
369 // group is actually gone so the run leaves no lingering resources
370 // (the group's auto-created event schema is removed with it).
371 const remaining = yield* describe(output.datasetGroupArn).pipe(
372 Effect.repeat({
373 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
374 until: (group): boolean => group === undefined,
375 }),
376 );
377 if (remaining !== undefined) {
378 return yield* new PersonalizeDatasetGroupNotReady({
379 message: `Personalize dataset group ${output.datasetGroupArn} was not deleted (status: ${remaining.status})`,
380 });
381 }
382 }),
383
384 list: () =>
385 personalize.listDatasetGroups.pages({}).pipe(
386 Stream.runCollect,
387 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.datasetGroups ?? [])),
388 Effect.flatMap(
389 Effect.forEach(
390 (summary) =>
391 describe(summary.datasetGroupArn!).pipe(
392 Effect.map((g) => (g ? toAttrs(g) : undefined)),
393 ),
394 { concurrency: 4 },
395 ),
396 ),
397 Effect.map((items) => items.filter((item) => item !== undefined)),
398 ),
399 };
400 }),
401 );