The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as location from "@distilled.cloud/aws/location"; |
| 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, diffTags, hasAlchemyTags, type Tags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as EffectStream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { toTagRecord } from "./internal.ts"; |
| 13 | |
| 14 | export class TrackerUnexpectedResponse extends Data.TaggedError("TrackerUnexpectedResponse")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export type PositionFiltering = "TimeBased" | "DistanceBased" | "AccuracyBased"; |
| 20 | |
| 21 | export interface TrackerProps { |
| 22 | /** |
| 23 | * Name of the tracker. Immutable — changing it replaces the tracker. |
| 24 | * @default ${app}-${stage}-${id} |
| 25 | */ |
| 26 | trackerName?: string; |
| 27 | /** |
| 28 | * KMS key ID used to encrypt the tracker's position data. Immutable — |
| 29 | * changing it replaces the tracker. |
| 30 | */ |
| 31 | kmsKeyId?: string; |
| 32 | /** |
| 33 | * The position filtering method applied to device updates. |
| 34 | * @default "TimeBased" |
| 35 | */ |
| 36 | positionFiltering?: PositionFiltering; |
| 37 | /** |
| 38 | * Whether to publish device position updates to EventBridge. |
| 39 | * @default false |
| 40 | */ |
| 41 | eventBridgeEnabled?: boolean; |
| 42 | /** |
| 43 | * Optional description of the tracker resource. |
| 44 | */ |
| 45 | description?: string; |
| 46 | /** |
| 47 | * Tags to associate with the tracker. |
| 48 | */ |
| 49 | tags?: Record<string, string>; |
| 50 | } |
| 51 | |
| 52 | export interface Tracker extends Resource< |
| 53 | "AWS.Location.Tracker", |
| 54 | TrackerProps, |
| 55 | { |
| 56 | /** Physical name of the tracker. */ |
| 57 | trackerName: string; |
| 58 | /** ARN of the tracker. */ |
| 59 | trackerArn: string; |
| 60 | /** KMS key ID backing the tracker, if configured. */ |
| 61 | kmsKeyId: string | undefined; |
| 62 | /** Position filtering method applied to updates. */ |
| 63 | positionFiltering: string | undefined; |
| 64 | /** Whether device updates are published to EventBridge. */ |
| 65 | eventBridgeEnabled: boolean | undefined; |
| 66 | /** Description of the tracker. */ |
| 67 | description: string | undefined; |
| 68 | /** Tags currently associated with the tracker. */ |
| 69 | tags: Record<string, string>; |
| 70 | }, |
| 71 | never, |
| 72 | Providers |
| 73 | > {} |
| 74 | |
| 75 | /** |
| 76 | * An Amazon Location Service tracker. A tracker records device positions and |
| 77 | * evaluates them against linked geofence collections. The KMS key is |
| 78 | * immutable; position filtering, EventBridge publishing, and the description |
| 79 | * can be updated in place. |
| 80 | * |
| 81 | * ### Creating Trackers |
| 82 | * **Example:** Basic Tracker |
| 83 | * ```typescript |
| 84 | * import * as Location from "@alchemy.run/aws/Location"; |
| 85 | * |
| 86 | * const tracker = yield* Location.Tracker("Devices", {}); |
| 87 | * ``` |
| 88 | * |
| 89 | * **Example:** Distance-Filtered Tracker with EventBridge |
| 90 | * ```typescript |
| 91 | * const tracker = yield* Location.Tracker("Fleet", { |
| 92 | * positionFiltering: "DistanceBased", |
| 93 | * eventBridgeEnabled: true, |
| 94 | * }); |
| 95 | * ``` |
| 96 | * |
| 97 | * @resource |
| 98 | */ |
| 99 | export const Tracker = Resource<Tracker>("AWS.Location.Tracker"); |
| 100 | |
| 101 | const createTrackerName = (id: string, props: { trackerName?: string | undefined }) => |
| 102 | Effect.gen(function* () { |
| 103 | if (props.trackerName) return props.trackerName; |
| 104 | return yield* createPhysicalName({ id, maxLength: 100 }); |
| 105 | }); |
| 106 | |
| 107 | const readTracker = Effect.fn(function* (trackerName: string) { |
| 108 | const found = yield* location |
| 109 | .describeTracker({ TrackerName: trackerName }) |
| 110 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 111 | if (!found) return undefined; |
| 112 | return { |
| 113 | trackerName: found.TrackerName, |
| 114 | trackerArn: found.TrackerArn, |
| 115 | kmsKeyId: found.KmsKeyId, |
| 116 | positionFiltering: found.PositionFiltering, |
| 117 | eventBridgeEnabled: found.EventBridgeEnabled, |
| 118 | description: found.Description ? found.Description : undefined, |
| 119 | tags: toTagRecord(found.Tags), |
| 120 | } satisfies Tracker["Attributes"]; |
| 121 | }); |
| 122 | |
| 123 | export const TrackerProvider = () => |
| 124 | Provider.effect( |
| 125 | Tracker, |
| 126 | Effect.gen(function* () { |
| 127 | return { |
| 128 | stables: ["trackerName", "trackerArn"], |
| 129 | list: () => |
| 130 | Effect.gen(function* () { |
| 131 | const names = yield* location.listTrackers.pages({}).pipe( |
| 132 | EffectStream.runCollect, |
| 133 | Effect.map((chunk) => |
| 134 | Array.from(chunk).flatMap((page) => |
| 135 | (page.Entries ?? []).map((entry) => entry.TrackerName), |
| 136 | ), |
| 137 | ), |
| 138 | ); |
| 139 | const hydrated = yield* Effect.forEach(names, (name) => readTracker(name), { |
| 140 | concurrency: 10, |
| 141 | }); |
| 142 | return hydrated.filter((attrs): attrs is Tracker["Attributes"] => attrs !== undefined); |
| 143 | }), |
| 144 | read: Effect.fn(function* ({ id, olds, output }) { |
| 145 | const trackerName = output?.trackerName ?? (yield* createTrackerName(id, olds ?? {})); |
| 146 | const state = yield* readTracker(trackerName); |
| 147 | if (!state) return undefined; |
| 148 | return (yield* hasAlchemyTags(id, state.tags as Tags)) ? state : Unowned(state); |
| 149 | }), |
| 150 | diff: Effect.fn(function* ({ id, news = {}, olds = {} }) { |
| 151 | if (!isResolved(news)) return; |
| 152 | const oldName = yield* createTrackerName(id, olds); |
| 153 | const newName = yield* createTrackerName(id, news); |
| 154 | if (oldName !== newName || olds.kmsKeyId !== news.kmsKeyId) { |
| 155 | return { action: "replace" } as const; |
| 156 | } |
| 157 | }), |
| 158 | reconcile: Effect.fn(function* ({ id, news = {}, output, session }) { |
| 159 | const trackerName = output?.trackerName ?? (yield* createTrackerName(id, news)); |
| 160 | const internalTags = yield* createInternalTags(id); |
| 161 | const desiredTags = { ...internalTags, ...news.tags }; |
| 162 | |
| 163 | let state = yield* readTracker(trackerName); |
| 164 | |
| 165 | if (state === undefined) { |
| 166 | yield* location |
| 167 | .createTracker({ |
| 168 | TrackerName: trackerName, |
| 169 | KmsKeyId: news.kmsKeyId, |
| 170 | PositionFiltering: news.positionFiltering, |
| 171 | EventBridgeEnabled: news.eventBridgeEnabled, |
| 172 | Description: news.description, |
| 173 | Tags: desiredTags, |
| 174 | }) |
| 175 | .pipe(Effect.catchTag("ConflictException", () => Effect.void)); |
| 176 | state = yield* readTracker(trackerName); |
| 177 | if (state === undefined) { |
| 178 | return yield* new TrackerUnexpectedResponse({ |
| 179 | message: `failed to read created tracker ${trackerName}`, |
| 180 | }); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | const desiredFiltering = news.positionFiltering ?? "TimeBased"; |
| 185 | const desiredEventBridge = news.eventBridgeEnabled ?? false; |
| 186 | if ( |
| 187 | state.description !== (news.description ?? undefined) || |
| 188 | state.positionFiltering !== desiredFiltering || |
| 189 | (state.eventBridgeEnabled ?? false) !== desiredEventBridge |
| 190 | ) { |
| 191 | yield* location.updateTracker({ |
| 192 | TrackerName: trackerName, |
| 193 | Description: news.description, |
| 194 | PositionFiltering: news.positionFiltering, |
| 195 | EventBridgeEnabled: news.eventBridgeEnabled, |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | const { removed, upsert } = diffTags(state.tags, desiredTags); |
| 200 | if (removed.length > 0) { |
| 201 | yield* location.untagResource({ |
| 202 | ResourceArn: state.trackerArn, |
| 203 | TagKeys: removed, |
| 204 | }); |
| 205 | } |
| 206 | if (upsert.length > 0) { |
| 207 | yield* location.tagResource({ |
| 208 | ResourceArn: state.trackerArn, |
| 209 | Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | yield* session.note(state.trackerArn); |
| 214 | |
| 215 | const final = yield* readTracker(trackerName); |
| 216 | if (!final) { |
| 217 | return yield* new TrackerUnexpectedResponse({ |
| 218 | message: `failed to read reconciled tracker ${trackerName}`, |
| 219 | }); |
| 220 | } |
| 221 | return final; |
| 222 | }), |
| 223 | delete: Effect.fn(function* ({ output }) { |
| 224 | yield* location |
| 225 | .deleteTracker({ TrackerName: output.trackerName }) |
| 226 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 227 | }), |
| 228 | }; |
| 229 | }), |
| 230 | ); |