@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, 06:00 UTC

main
ts 139 lines 4.66 KB
Raw
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 OutcomeProps {
14 /**
15 * Name of the outcome. If omitted, a unique lowercase name is generated from
16 * the app, stage, and logical ID. Changing the name replaces the outcome.
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 outcome.
25 */
26 tags?: Record<string, string>;
27 }
28
29 export interface Outcome extends Resource<
30 "AWS.FraudDetector.Outcome",
31 OutcomeProps,
32 {
33 /** The name of the outcome. */
34 name: string;
35 /** The ARN of the outcome. */
36 arn: string;
37 },
38 never,
39 Providers
40 > {}
41
42 /**
43 * An Amazon Fraud Detector outcome — the result a rule produces when it matches
44 * (e.g. `approve`, `review`, `block`). Detector rules reference outcomes; they
45 * are cheap metadata objects.
46 *
47 * ### Creating an Outcome
48 * **Example:** Approve and Review Outcomes
49 * ```typescript
50 * const approve = yield* FraudDetector.Outcome("approve", {
51 * description: "let the transaction through",
52 * });
53 * const review = yield* FraudDetector.Outcome("review", {});
54 * ```
55 *
56 * @resource
57 */
58 export const Outcome = Resource<Outcome>("AWS.FraudDetector.Outcome");
59
60 export const OutcomeProvider = () =>
61 Provider.effect(
62 Outcome,
63 Effect.gen(function* () {
64 const createName = Effect.fn(function* (id: string, props: OutcomeProps) {
65 return props.name ?? (yield* createPhysicalName({ id, maxLength: 64, lowercase: true }));
66 });
67
68 /** Look an outcome up by name; typed not-found → undefined. */
69 const get = Effect.fn(function* (name: string) {
70 const response = yield* frauddetector
71 .getOutcomes({ name })
72 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
73 return response?.outcomes?.[0];
74 });
75
76 const toAttrs = (outcome: frauddetector.Outcome) => ({
77 name: outcome.name!,
78 arn: outcome.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 outcome = yield* get(name);
96 if (outcome === undefined) return undefined;
97 const attrs = toAttrs(outcome);
98 const tags = yield* readFraudDetectorTags(outcome.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 // putOutcome is an idempotent upsert — call it to converge whether
108 // creating or updating the description.
109 yield* frauddetector.putOutcome({
110 name,
111 description: news.description,
112 });
113
114 const outcome = yield* get(name);
115 // Sync tags — diff against OBSERVED cloud tags.
116 yield* syncFraudDetectorTags(outcome!.arn!, desiredTags);
117
118 yield* session.note(name);
119 return toAttrs(outcome!);
120 }),
121
122 delete: Effect.fn(function* ({ output }) {
123 yield* frauddetector.deleteOutcome({ name: output.name }).pipe(
124 // Deleting an already-removed outcome is a no-op for us; Fraud
125 // Detector surfaces a missing outcome as a validation error.
126 Effect.catchTag("ValidationException", () => Effect.void),
127 );
128 }),
129
130 list: () =>
131 frauddetector.getOutcomes.pages({}).pipe(
132 Stream.runCollect,
133 Effect.map((chunk) =>
134 Array.from(chunk).flatMap((page) => (page.outcomes ?? []).map(toAttrs)),
135 ),
136 ),
137 };
138 }),
139 );