@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 57 lines 2.09 KB
Raw
1 import * as notifications from "@distilled.cloud/aws/notifications";
2 import { createInternalTags, diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import { Region } from "../Region.ts";
5
6 /**
7 * AWS User Notifications is managed from a single control-plane region.
8 * Every management API call must target `us-east-1` regardless of the
9 * ambient stack region (other regions reject with "API ...Activity cannot
10 * be called in us-west-2. Use us-east-1 instead"), so we pin the region on
11 * every distilled operation.
12 */
13 const US_EAST_1 = "us-east-1";
14 export const pinNotificationsRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
15 effect.pipe(Effect.provideService(Region, Effect.succeed(US_EAST_1)));
16
17 /**
18 * Read the observed tags on a User Notifications resource, tolerating a
19 * not-yet-visible resource (returns `{}`).
20 */
21 export const readNotificationsTags = Effect.fn(function* (arn: string) {
22 return yield* pinNotificationsRegion(
23 notifications.listTagsForResource({ arn }).pipe(
24 Effect.map((r) => (r.tags ?? {}) as Record<string, string>),
25 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], () =>
26 Effect.succeed({} as Record<string, string>),
27 ),
28 ),
29 );
30 });
31
32 /**
33 * Converge the tags on a User Notifications resource to the desired user
34 * tags merged with the internal Alchemy ownership tags, diffing against
35 * OBSERVED cloud tags so adoption converges.
36 */
37 export const syncNotificationsTags = Effect.fn(function* (
38 arn: string,
39 id: string,
40 userTags: Record<string, string> | undefined,
41 ) {
42 const internalTags = yield* createInternalTags(id);
43 const desired = { ...userTags, ...internalTags };
44 const observed = yield* readNotificationsTags(arn);
45 const { upsert, removed } = diffTags(observed, desired);
46 if (upsert.length > 0) {
47 yield* pinNotificationsRegion(
48 notifications.tagResource({
49 arn,
50 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
51 }),
52 );
53 }
54 if (removed.length > 0) {
55 yield* pinNotificationsRegion(notifications.untagResource({ arn, tagKeys: removed }));
56 }
57 });