The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as sitewise from "@distilled.cloud/aws/iotsitewise"; |
| 2 | import { diffTags } from "alchemy/Tags"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | /** |
| 6 | * Fetch the observed tags on an IoT SiteWise resource ARN as a plain |
| 7 | * string map (SiteWise's TagMap allows `undefined` values on the wire). |
| 8 | */ |
| 9 | export const fetchSiteWiseTags = Effect.fn(function* (arn: string) { |
| 10 | const response = yield* sitewise |
| 11 | .listTagsForResource({ resourceArn: arn }) |
| 12 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 13 | const tags: Record<string, string> = {}; |
| 14 | for (const [key, value] of Object.entries(response?.tags ?? {})) { |
| 15 | if (value !== undefined) tags[key] = value; |
| 16 | } |
| 17 | return tags; |
| 18 | }); |
| 19 | |
| 20 | /** |
| 21 | * Converge the tags on a SiteWise resource ARN from the observed cloud |
| 22 | * tags to the desired map (untag removed keys, upsert added/changed). |
| 23 | */ |
| 24 | export const syncSiteWiseTags = Effect.fn(function* ( |
| 25 | arn: string, |
| 26 | observed: Record<string, string>, |
| 27 | desired: Record<string, string>, |
| 28 | ) { |
| 29 | const { removed, upsert } = diffTags(observed, desired); |
| 30 | if (removed.length > 0) { |
| 31 | yield* sitewise.untagResource({ resourceArn: arn, tagKeys: removed }); |
| 32 | } |
| 33 | if (upsert.length > 0) { |
| 34 | yield* sitewise.tagResource({ |
| 35 | resourceArn: arn, |
| 36 | tags: Object.fromEntries(upsert.map((tag) => [tag.Key, tag.Value])), |
| 37 | }); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | /** |
| 42 | * Deep-project `observed` onto the key structure of `desired`: for every |
| 43 | * key path present in `desired`, take the observed value. Keys the caller |
| 44 | * never specified (server-side defaults like `processingConfig` or |
| 45 | * generated `path` segments) are dropped so they don't read as drift. |
| 46 | */ |
| 47 | const projectLike = (desired: unknown, observed: unknown): unknown => { |
| 48 | if (Array.isArray(desired)) { |
| 49 | if (!Array.isArray(observed)) return observed; |
| 50 | return desired.map((item, index) => projectLike(item, observed[index])); |
| 51 | } |
| 52 | if (desired !== null && typeof desired === "object") { |
| 53 | if (observed === null || typeof observed !== "object" || Array.isArray(observed)) { |
| 54 | return observed; |
| 55 | } |
| 56 | const out: Record<string, unknown> = {}; |
| 57 | for (const [key, value] of Object.entries(desired as Record<string, unknown>)) { |
| 58 | if (value === undefined) continue; |
| 59 | out[key] = projectLike(value, (observed as Record<string, unknown>)[key]); |
| 60 | } |
| 61 | return out; |
| 62 | } |
| 63 | return observed; |
| 64 | }; |
| 65 | |
| 66 | const stripUndefined = (value: unknown): unknown => |
| 67 | value === undefined ? undefined : JSON.parse(JSON.stringify(value)); |
| 68 | |
| 69 | /** |
| 70 | * Structural equality between a desired shape and the observed cloud |
| 71 | * state, ignoring observed-only keys (server defaults, generated ids the |
| 72 | * caller didn't pin, etc.). |
| 73 | */ |
| 74 | export const matchesDesired = (desired: unknown, observed: unknown): boolean => |
| 75 | JSON.stringify(stripUndefined(desired)) === |
| 76 | JSON.stringify(stripUndefined(projectLike(desired, observed))); |