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

main
ts 323 lines 11.3 KB
Raw
1 import * as databrew from "@distilled.cloud/aws/databrew";
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 Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { AWSEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 canonicalJson,
14 cleanMap,
15 databrewArn,
16 fetchObservedTags,
17 retryWhileConflict,
18 syncTags,
19 } from "./internal.ts";
20
21 /** A single transformation step in a DataBrew recipe. */
22 export interface RecipeStep {
23 /** The transformation to apply. */
24 action: {
25 /**
26 * The DataBrew operation name, e.g. `UPPER_CASE`, `REMOVE_VALUES`,
27 * `RENAME`. See the DataBrew recipe-action reference for the full list.
28 */
29 operation: string;
30 /** Operation parameters, e.g. `{ sourceColumn: "name" }`. */
31 parameters?: Record<string, string>;
32 };
33 /** Conditions that must hold for the step to apply to a row. */
34 conditionExpressions?: {
35 /** The condition name, e.g. `LESS_THAN`, `IS_MISSING`. */
36 condition: string;
37 /** The value to compare against (JSON-encoded for lists). */
38 value?: string;
39 /** The column the condition applies to. */
40 targetColumn: string;
41 }[];
42 }
43
44 export interface RecipeProps {
45 /**
46 * Name of the recipe. If omitted, a unique name is generated. Changing
47 * the name replaces the recipe.
48 * @default a generated physical name
49 */
50 recipeName?: string;
51 /**
52 * A description of the recipe.
53 */
54 description?: string;
55 /**
56 * The ordered transformation steps applied to the data.
57 */
58 steps: RecipeStep[];
59 /**
60 * Publish a new numbered version whenever the working steps change (and
61 * once on create). Recipe jobs consume the latest *published* version by
62 * default, so set this when the recipe feeds a `DataBrew.Job`.
63 * @default false
64 */
65 publish?: boolean;
66 /**
67 * Tags to apply to the recipe. Merged with internal Alchemy tags.
68 */
69 tags?: Record<string, string>;
70 }
71
72 export interface Recipe extends Resource<
73 "AWS.DataBrew.Recipe",
74 RecipeProps,
75 {
76 /** Name of the recipe. */
77 recipeName: string;
78 /** ARN of the recipe. */
79 recipeArn: string;
80 /** Latest published version (e.g. `"1.0"`), or `"LATEST_WORKING"` if never published. */
81 recipeVersion: string;
82 },
83 {},
84 Providers
85 > {}
86
87 /**
88 * An AWS Glue DataBrew recipe — an ordered list of data-transformation steps
89 * (rename, filter, case conversion, etc.). Edits modify the `LATEST_WORKING`
90 * version; setting `publish: true` snapshots numbered versions that recipe
91 * jobs consume.
92 * ### Creating Recipes
93 * **Example:** Simple Transform Recipe
94 * ```typescript
95 * import * as AWS from "@alchemy.run/aws";
96 *
97 * const recipe = yield* AWS.DataBrew.Recipe("Clean", {
98 * description: "normalize customer names",
99 * steps: [
100 * {
101 * action: {
102 * operation: "UPPER_CASE",
103 * parameters: { sourceColumn: "name" },
104 * },
105 * },
106 * ],
107 * });
108 * ```
109 *
110 * **Example:** Published Recipe for Jobs
111 * ```typescript
112 * // publish: true snapshots a numbered version (1.0, 2.0, ...) whenever the
113 * // steps change — recipe jobs run the latest published version by default.
114 * const recipe = yield* AWS.DataBrew.Recipe("Clean", {
115 * publish: true,
116 * steps: [
117 * {
118 * action: {
119 * operation: "REMOVE_VALUES",
120 * parameters: { sourceColumn: "email" },
121 * },
122 * conditionExpressions: [
123 * { condition: "IS_MISSING", targetColumn: "email" },
124 * ],
125 * },
126 * ],
127 * });
128 * ```
129 *
130 * @resource
131 */
132 export const Recipe = Resource<Recipe>("AWS.DataBrew.Recipe");
133
134 export const buildSteps = (steps: RecipeStep[]) =>
135 steps.map((step) => ({
136 Action: {
137 Operation: step.action.operation,
138 Parameters: step.action.parameters,
139 },
140 ConditionExpressions: step.conditionExpressions?.map((expr) => ({
141 Condition: expr.condition,
142 Value: expr.value,
143 TargetColumn: expr.targetColumn,
144 })),
145 }));
146
147 /** Numeric-aware max over published version strings like `"2.0"`. */
148 const latestPublished = (versions: (string | undefined)[]) => {
149 const numeric = versions
150 .filter((v): v is string => v !== undefined && !v.startsWith("LATEST"))
151 .sort((a, b) => Number.parseFloat(a) - Number.parseFloat(b));
152 return numeric.at(-1);
153 };
154
155 export const RecipeProvider = () =>
156 Provider.effect(
157 Recipe,
158 Effect.gen(function* () {
159 const createName = Effect.fn(function* (
160 id: string,
161 props: { recipeName?: string | undefined },
162 ) {
163 return props.recipeName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
164 });
165
166 const observeWorking = Effect.fn(function* (name: string) {
167 return yield* databrew
168 .describeRecipe({ Name: name, RecipeVersion: "LATEST_WORKING" })
169 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
170 });
171
172 const publishedVersions = Effect.fn(function* (name: string) {
173 const pages = yield* databrew.listRecipeVersions
174 .pages({ Name: name })
175 .pipe(Stream.runCollect);
176 return Array.from(pages)
177 .flatMap((page) => page.Recipes ?? [])
178 .map((r) => r.RecipeVersion);
179 });
180
181 return Recipe.Provider.of({
182 stables: ["recipeName", "recipeArn"],
183
184 list: () =>
185 Effect.gen(function* () {
186 const { accountId, region } = yield* AWSEnvironment.current;
187 // LATEST_WORKING enumerates every recipe (the default listing
188 // only returns recipes with a published version).
189 const workingPages = yield* databrew.listRecipes
190 .pages({ RecipeVersion: "LATEST_WORKING" })
191 .pipe(Stream.runCollect);
192 const publishedPages = yield* databrew.listRecipes.pages({}).pipe(Stream.runCollect);
193 const published = new Map(
194 Array.from(publishedPages)
195 .flatMap((page) => page.Recipes ?? [])
196 .map((r) => [r.Name, r.RecipeVersion]),
197 );
198 return Array.from(workingPages)
199 .flatMap((page) => page.Recipes ?? [])
200 .map((r) => ({
201 recipeName: r.Name,
202 recipeArn: r.ResourceArn ?? databrewArn(region, accountId, "recipe", r.Name),
203 recipeVersion: published.get(r.Name) ?? "LATEST_WORKING",
204 }));
205 }),
206
207 read: Effect.fn(function* ({ id, olds, output }) {
208 const { accountId, region } = yield* AWSEnvironment.current;
209 const name = output?.recipeName ?? (yield* createName(id, olds ?? {}));
210 const recipe = yield* observeWorking(name);
211 if (recipe === undefined) return undefined;
212 const arn = recipe.ResourceArn ?? databrewArn(region, accountId, "recipe", name);
213 const versions = yield* publishedVersions(name);
214 const attrs = {
215 recipeName: name,
216 recipeArn: arn,
217 recipeVersion: latestPublished(versions) ?? "LATEST_WORKING",
218 };
219 const tags = cleanMap(recipe.Tags);
220 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
221 }),
222
223 diff: Effect.fn(function* ({ id, news, olds }) {
224 if (!isResolved(news)) return undefined;
225 const oldName = yield* createName(id, olds);
226 const newName = yield* createName(id, news);
227 if (oldName !== newName) return { action: "replace" } as const;
228 // steps/description are UpdateRecipe-able; publish is a sync step
229 }),
230
231 reconcile: Effect.fn(function* ({ id, news, output, session }) {
232 const { accountId, region } = yield* AWSEnvironment.current;
233 const name = output?.recipeName ?? (yield* createName(id, news));
234 const internalTags = yield* createInternalTags(id);
235 const desiredTags = { ...news.tags, ...internalTags };
236 const desiredSteps = buildSteps(news.steps);
237
238 // 1. OBSERVE the working copy
239 let recipe = yield* observeWorking(name);
240 let changed = false;
241
242 // 2. ENSURE
243 if (recipe === undefined) {
244 yield* databrew
245 .createRecipe({
246 Name: name,
247 Description: news.description,
248 Steps: desiredSteps,
249 Tags: desiredTags,
250 })
251 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
252 changed = true;
253 recipe = yield* observeWorking(name);
254 } else if (
255 canonicalJson(recipe.Steps ?? []) !== canonicalJson(desiredSteps) ||
256 (recipe.Description ?? undefined) !== news.description
257 ) {
258 // 3. SYNC the working copy only when observed differs
259 yield* databrew.updateRecipe({
260 Name: name,
261 Description: news.description,
262 Steps: desiredSteps,
263 });
264 changed = true;
265 }
266
267 // 3b. SYNC published versions — publish when the working copy
268 // changed, or when publishing is desired but nothing is published.
269 let versions = yield* publishedVersions(name);
270 if (news.publish === true && (changed || versions.length === 0)) {
271 yield* databrew.publishRecipe({
272 Name: name,
273 Description: news.description,
274 });
275 versions = yield* publishedVersions(name);
276 }
277
278 const arn = recipe?.ResourceArn ?? databrewArn(region, accountId, "recipe", name);
279
280 // 3c. SYNC TAGS against observed cloud tags
281 const observedTags = yield* fetchObservedTags(arn);
282 yield* syncTags(arn, observedTags, desiredTags);
283
284 yield* session.note(name);
285 return {
286 recipeName: name,
287 recipeArn: arn,
288 recipeVersion: latestPublished(versions) ?? "LATEST_WORKING",
289 };
290 }),
291
292 delete: Effect.fn(function* ({ output }) {
293 const name = output.recipeName;
294 // A recipe is deleted by deleting all of its versions: published
295 // versions first (batched), then LATEST_WORKING.
296 const versions = yield* databrew.listRecipeVersions.pages({ Name: name }).pipe(
297 Stream.runCollect,
298 Effect.map((pages) =>
299 Array.from(pages)
300 .flatMap((page) => page.Recipes ?? [])
301 .map((r) => r.RecipeVersion)
302 .filter((v): v is string => v !== undefined),
303 ),
304 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([] as string[])),
305 );
306 if (versions.length > 0) {
307 yield* retryWhileConflict(
308 databrew.batchDeleteRecipeVersion({
309 Name: name,
310 RecipeVersions: versions,
311 }),
312 ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
313 }
314 yield* retryWhileConflict(
315 databrew.deleteRecipeVersion({
316 Name: name,
317 RecipeVersion: "LATEST_WORKING",
318 }),
319 ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
320 }),
321 });
322 }),
323 );