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

main
ts 276 lines 10.1 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 PersonalizeDatasetNotReady extends Data.TaggedError("PersonalizeDatasetNotReady")<{
16 readonly message: string;
17 readonly cause?: unknown;
18 }> {}
19
20 export interface DatasetProps {
21 /**
22 * Name of the dataset. If omitted, a unique name is generated from the app,
23 * stage, and logical ID. Changing the name replaces the dataset.
24 */
25 name?: string;
26 /**
27 * ARN of the schema that describes this dataset's fields. Immutable —
28 * changing it replaces the dataset.
29 */
30 schemaArn: string;
31 /**
32 * ARN of the dataset group this dataset belongs to. Immutable — changing it
33 * replaces the dataset.
34 */
35 datasetGroupArn: string;
36 /**
37 * The type of dataset: `Interactions`, `Items`, `Users`, `Actions`, or
38 * `Action_Interactions`. Immutable — changing it replaces the dataset.
39 */
40 datasetType: string;
41 /**
42 * User-defined tags for the dataset.
43 */
44 tags?: Record<string, string>;
45 }
46
47 export interface Dataset extends Resource<
48 "AWS.Personalize.Dataset",
49 DatasetProps,
50 {
51 /**
52 * ARN of the dataset.
53 */
54 datasetArn: string;
55 /**
56 * Name of the dataset.
57 */
58 name: string;
59 /**
60 * Dataset type (`Interactions`, `Items`, `Users`, or `Actions`).
61 */
62 datasetType: string;
63 /**
64 * Dataset status (e.g. `ACTIVE`, `CREATE PENDING`).
65 */
66 status: string;
67 /**
68 * ARN of the schema the dataset conforms to.
69 */
70 schemaArn: string;
71 /**
72 * ARN of the dataset group the dataset belongs to.
73 */
74 datasetGroupArn: string;
75 },
76 never,
77 Providers
78 > {}
79
80 /**
81 * An Amazon Personalize dataset — a typed collection (Interactions, Items,
82 * Users, …) inside a dataset group, backed by a schema. Creating the dataset
83 * is a cheap metadata operation; bulk imports and training happen through
84 * separate import jobs and solutions.
85 *
86 * ### Creating a Dataset
87 * **Example:** Interactions Dataset
88 * ```typescript
89 * const dataset = yield* Personalize.Dataset("Interactions", {
90 * schemaArn: schema.schemaArn,
91 * datasetGroupArn: group.datasetGroupArn,
92 * datasetType: "Interactions",
93 * });
94 * ```
95 *
96 * @resource
97 */
98 export const Dataset = Resource<Dataset>("AWS.Personalize.Dataset");
99
100 export const DatasetProvider = () =>
101 Provider.effect(
102 Dataset,
103 Effect.gen(function* () {
104 const createName = Effect.fn(function* (id: string, props: DatasetProps) {
105 return props.name ?? (yield* createPhysicalName({ id, maxLength: 63 }));
106 });
107
108 const describe = Effect.fn(function* (datasetArn: string) {
109 const response = yield* personalize
110 .describeDataset({ datasetArn })
111 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
112 return response?.dataset;
113 });
114
115 const waitActive = Effect.fn(function* (datasetArn: string) {
116 const dataset = yield* describe(datasetArn).pipe(
117 Effect.repeat({
118 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(40)]),
119 until: (d) => d?.status === "ACTIVE" || (d?.status ?? "").includes("FAILED"),
120 }),
121 );
122 if (dataset?.status !== "ACTIVE") {
123 return yield* new PersonalizeDatasetNotReady({
124 message: `Personalize dataset ${datasetArn} did not become ACTIVE (status: ${dataset?.status})`,
125 });
126 }
127 return dataset;
128 });
129
130 /** Find an existing dataset's ARN by name within its dataset group. */
131 const findArnByName = Effect.fn(function* (name: string, datasetGroupArn: string) {
132 const pages = yield* personalize.listDatasets
133 .pages({ datasetGroupArn })
134 .pipe(Stream.runCollect);
135 return Array.from(pages)
136 .flatMap((page) => page.datasets ?? [])
137 .find((summary) => summary.name === name)?.datasetArn;
138 });
139
140 const toAttrs = (dataset: personalize.Dataset) => ({
141 datasetArn: dataset.datasetArn!,
142 name: dataset.name!,
143 datasetType: dataset.datasetType!,
144 status: dataset.status!,
145 schemaArn: dataset.schemaArn!,
146 datasetGroupArn: dataset.datasetGroupArn!,
147 });
148
149 return {
150 stables: ["datasetArn", "name"],
151
152 diff: Effect.fn(function* ({ id, olds, news }) {
153 if (!isResolved(news)) return undefined;
154 const oldName = yield* createName(id, olds);
155 const newName = yield* createName(id, news);
156 if (
157 oldName !== newName ||
158 (olds.schemaArn ?? undefined) !== (news.schemaArn ?? undefined) ||
159 (olds.datasetGroupArn ?? undefined) !== (news.datasetGroupArn ?? undefined) ||
160 (olds.datasetType ?? undefined) !== (news.datasetType ?? undefined)
161 ) {
162 return { action: "replace" } as const;
163 }
164 }),
165
166 read: Effect.fn(function* ({ id, output }) {
167 if (!output?.datasetArn) return undefined;
168 const dataset = yield* describe(output.datasetArn);
169 if (dataset === undefined) return undefined;
170 const attrs = toAttrs(dataset);
171 const tags = yield* readPersonalizeTags(dataset.datasetArn!);
172 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
173 }),
174
175 reconcile: Effect.fn(function* ({ id, news, output, session }) {
176 const name = yield* createName(id, news);
177 const internalTags = yield* createInternalTags(id);
178 const desiredTags = { ...internalTags, ...news.tags };
179
180 let dataset =
181 output?.datasetArn !== undefined ? yield* describe(output.datasetArn) : undefined;
182
183 // Ensure — create if missing. A crashed prior run may have left a
184 // same-named dataset behind with no persisted state — adopt it by
185 // name and converge.
186 if (dataset === undefined) {
187 const arn = yield* personalize
188 .createDataset({
189 name,
190 schemaArn: news.schemaArn,
191 datasetGroupArn: news.datasetGroupArn,
192 datasetType: news.datasetType,
193 tags: Object.entries(desiredTags).map(([tagKey, tagValue]) => ({
194 tagKey,
195 tagValue,
196 })),
197 })
198 .pipe(
199 Effect.map((created) => created.datasetArn!),
200 Effect.catchTag("ResourceAlreadyExistsException", (error) =>
201 findArnByName(name, news.datasetGroupArn).pipe(
202 Effect.flatMap((existing) =>
203 existing === undefined ? Effect.fail(error) : Effect.succeed(existing),
204 ),
205 ),
206 ),
207 );
208 dataset = yield* waitActive(arn);
209 }
210
211 // Sync tags — diff against OBSERVED cloud tags (no-op after a fresh
212 // create; converges an adopted leftover).
213 yield* syncPersonalizeTags(dataset.datasetArn!, desiredTags);
214
215 yield* session.note(dataset.datasetArn!);
216 return toAttrs(dataset);
217 }),
218
219 delete: Effect.fn(function* ({ output }) {
220 yield* personalize.deleteDataset({ datasetArn: output.datasetArn }).pipe(
221 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
222 Effect.retry({
223 while: (e) => e._tag === "ResourceInUseException",
224 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
225 }),
226 );
227 // Deletion is asynchronous (DELETE PENDING) — wait until the dataset
228 // is actually gone so the schema and dataset group it references can
229 // delete without exhausting their ResourceInUse retries.
230 const remaining = yield* describe(output.datasetArn).pipe(
231 Effect.repeat({
232 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
233 until: (dataset): boolean => dataset === undefined,
234 }),
235 );
236 if (remaining !== undefined) {
237 return yield* new PersonalizeDatasetNotReady({
238 message: `Personalize dataset ${output.datasetArn} was not deleted (status: ${remaining.status})`,
239 });
240 }
241 }),
242
243 // Datasets are keyed by their parent dataset group (ListDatasets
244 // requires a datasetGroupArn), so account-wide enumeration walks the
245 // dataset groups first and lists each group's datasets. Without this,
246 // orphaned datasets are invisible to nuke and permanently block both
247 // their schema and their dataset group from deleting.
248 list: () =>
249 personalize.listDatasetGroups.pages({}).pipe(
250 Stream.runCollect,
251 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.datasetGroups ?? [])),
252 Effect.flatMap(
253 Effect.forEach(
254 (group) =>
255 personalize.listDatasets.pages({ datasetGroupArn: group.datasetGroupArn }).pipe(
256 Stream.runCollect,
257 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.datasets ?? [])),
258 ),
259 { concurrency: 4 },
260 ),
261 ),
262 Effect.map((groups) => groups.flat()),
263 Effect.flatMap(
264 Effect.forEach(
265 (summary) =>
266 describe(summary.datasetArn!).pipe(
267 Effect.map((d) => (d ? toAttrs(d) : undefined)),
268 ),
269 { concurrency: 4 },
270 ),
271 ),
272 Effect.map((items) => items.filter((item) => item !== undefined)),
273 ),
274 };
275 }),
276 );