The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as frauddetector from "@distilled.cloud/aws/frauddetector"; |
| 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 type { Providers } from "../Providers.ts"; |
| 11 | import { readFraudDetectorTags, syncFraudDetectorTags } from "./internal.ts"; |
| 12 | |
| 13 | export interface LabelProps { |
| 14 | /** |
| 15 | * Name of the label. If omitted, a unique lowercase name is generated from |
| 16 | * the app, stage, and logical ID. Changing the name replaces the label. |
| 17 | */ |
| 18 | name?: string; |
| 19 | /** |
| 20 | * Human-readable description. This is an in-place update. |
| 21 | */ |
| 22 | description?: string; |
| 23 | /** |
| 24 | * User-defined tags for the label. |
| 25 | */ |
| 26 | tags?: Record<string, string>; |
| 27 | } |
| 28 | |
| 29 | export interface Label extends Resource< |
| 30 | "AWS.FraudDetector.Label", |
| 31 | LabelProps, |
| 32 | { |
| 33 | /** The name of the label. */ |
| 34 | name: string; |
| 35 | /** The ARN of the label. */ |
| 36 | arn: string; |
| 37 | }, |
| 38 | never, |
| 39 | Providers |
| 40 | > {} |
| 41 | |
| 42 | /** |
| 43 | * An Amazon Fraud Detector label — a classification (e.g. `fraud`, `legit`) |
| 44 | * used to tag stored events for supervised model training. Event types |
| 45 | * reference labels; they are cheap metadata objects. |
| 46 | * |
| 47 | * ### Creating a Label |
| 48 | * **Example:** Fraud and Legit Labels |
| 49 | * ```typescript |
| 50 | * const fraud = yield* FraudDetector.Label("fraud", { |
| 51 | * description: "confirmed fraudulent event", |
| 52 | * }); |
| 53 | * const legit = yield* FraudDetector.Label("legit", {}); |
| 54 | * ``` |
| 55 | * |
| 56 | * @resource |
| 57 | */ |
| 58 | export const Label = Resource<Label>("AWS.FraudDetector.Label"); |
| 59 | |
| 60 | export const LabelProvider = () => |
| 61 | Provider.effect( |
| 62 | Label, |
| 63 | Effect.gen(function* () { |
| 64 | const createName = Effect.fn(function* (id: string, props: LabelProps) { |
| 65 | return props.name ?? (yield* createPhysicalName({ id, maxLength: 64, lowercase: true })); |
| 66 | }); |
| 67 | |
| 68 | /** Look a label up by name; typed not-found → undefined. */ |
| 69 | const get = Effect.fn(function* (name: string) { |
| 70 | const response = yield* frauddetector |
| 71 | .getLabels({ name }) |
| 72 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 73 | return response?.labels?.[0]; |
| 74 | }); |
| 75 | |
| 76 | const toAttrs = (label: frauddetector.Label) => ({ |
| 77 | name: label.name!, |
| 78 | arn: label.arn!, |
| 79 | }); |
| 80 | |
| 81 | return { |
| 82 | stables: ["name", "arn"], |
| 83 | |
| 84 | diff: Effect.fn(function* ({ id, olds = {}, news }) { |
| 85 | if (!isResolved(news)) return undefined; |
| 86 | const oldName = yield* createName(id, olds); |
| 87 | const newName = yield* createName(id, news); |
| 88 | if (oldName !== newName) { |
| 89 | return { action: "replace" } as const; |
| 90 | } |
| 91 | }), |
| 92 | |
| 93 | read: Effect.fn(function* ({ id, olds, output }) { |
| 94 | const name = output?.name ?? (yield* createName(id, olds ?? {})); |
| 95 | const label = yield* get(name); |
| 96 | if (label === undefined) return undefined; |
| 97 | const attrs = toAttrs(label); |
| 98 | const tags = yield* readFraudDetectorTags(label.arn!); |
| 99 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 100 | }), |
| 101 | |
| 102 | reconcile: Effect.fn(function* ({ id, news = {}, session }) { |
| 103 | const name = yield* createName(id, news); |
| 104 | const internalTags = yield* createInternalTags(id); |
| 105 | const desiredTags = { ...internalTags, ...news.tags }; |
| 106 | |
| 107 | // putLabel is an idempotent upsert — call it to converge whether |
| 108 | // creating or updating the description. |
| 109 | yield* frauddetector.putLabel({ |
| 110 | name, |
| 111 | description: news.description, |
| 112 | }); |
| 113 | |
| 114 | const label = yield* get(name); |
| 115 | // Sync tags — diff against OBSERVED cloud tags. |
| 116 | yield* syncFraudDetectorTags(label!.arn!, desiredTags); |
| 117 | |
| 118 | yield* session.note(name); |
| 119 | return toAttrs(label!); |
| 120 | }), |
| 121 | |
| 122 | delete: Effect.fn(function* ({ output }) { |
| 123 | yield* frauddetector.deleteLabel({ name: output.name }).pipe( |
| 124 | // Deleting an already-removed label is a no-op for us; Fraud |
| 125 | // Detector surfaces a missing label as a validation error. |
| 126 | Effect.catchTag("ValidationException", () => Effect.void), |
| 127 | ); |
| 128 | }), |
| 129 | |
| 130 | list: () => |
| 131 | frauddetector.getLabels.pages({}).pipe( |
| 132 | Stream.runCollect, |
| 133 | Effect.map((chunk) => |
| 134 | Array.from(chunk).flatMap((page) => (page.labels ?? []).map(toAttrs)), |
| 135 | ), |
| 136 | ), |
| 137 | }; |
| 138 | }), |
| 139 | ); |