The best way to control Claude Code, Codex and more from any computer or phone

main
ts 155 lines 5.66 KB
Raw
1 import * as location from "@distilled.cloud/aws/location";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as EffectStream from "effect/Stream";
7 import type { Providers } from "../Providers.ts";
8
9 export interface TrackerConsumerProps {
10 /**
11 * Name of the tracker whose device position updates are consumed.
12 * Changing it replaces the association.
13 */
14 trackerName: string;
15 /**
16 * ARN of the geofence collection that consumes the tracker's position
17 * updates (evaluating them against its geofences). Changing it replaces
18 * the association.
19 */
20 consumerArn: string;
21 }
22
23 export interface TrackerConsumer extends Resource<
24 "AWS.Location.TrackerConsumer",
25 TrackerConsumerProps,
26 {
27 /** Name of the tracker. */
28 trackerName: string;
29 /** ARN of the consuming geofence collection. */
30 consumerArn: string;
31 },
32 never,
33 Providers
34 > {}
35
36 /**
37 * Links an Amazon Location {@link Tracker} to a {@link GeofenceCollection}
38 * so every device position uploaded to the tracker is automatically
39 * evaluated against the collection's geofences, emitting ENTER/EXIT events
40 * (delivered to EventBridge — see `consumeTrackerEvents`).
41 *
42 * The association is existence-only: both properties are immutable and any
43 * change replaces it.
44 *
45 * ### Linking Trackers to Geofence Collections
46 * **Example:** Evaluate Tracker Positions Against a Collection
47 * ```typescript
48 * import * as Location from "@alchemy.run/aws/Location";
49 *
50 * const tracker = yield* Location.Tracker("Fleet", {
51 * eventBridgeEnabled: true,
52 * });
53 * const fences = yield* Location.GeofenceCollection("Zones", {});
54 *
55 * const link = yield* Location.TrackerConsumer("FleetZones", {
56 * trackerName: tracker.trackerName,
57 * consumerArn: fences.collectionArn,
58 * });
59 * ```
60 *
61 * @resource
62 */
63 export const TrackerConsumer = Resource<TrackerConsumer>("AWS.Location.TrackerConsumer");
64
65 export const TrackerConsumerProvider = () =>
66 Provider.effect(
67 TrackerConsumer,
68 Effect.gen(function* () {
69 /** Whether `consumerArn` is currently linked to `trackerName`. */
70 const observeAssociation = Effect.fn(function* (trackerName: string, consumerArn: string) {
71 const consumerArns = yield* location.listTrackerConsumers
72 .pages({ TrackerName: trackerName })
73 .pipe(
74 EffectStream.runCollect,
75 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.ConsumerArns)),
76 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([] as string[])),
77 );
78 return consumerArns.includes(consumerArn);
79 });
80
81 return {
82 stables: ["trackerName", "consumerArn"],
83 list: () =>
84 Effect.gen(function* () {
85 const trackerNames = yield* location.listTrackers.pages({}).pipe(
86 EffectStream.runCollect,
87 Effect.map((chunk) =>
88 Array.from(chunk).flatMap((page) =>
89 (page.Entries ?? []).map((entry) => entry.TrackerName),
90 ),
91 ),
92 );
93 const associations = yield* Effect.forEach(
94 trackerNames,
95 (trackerName) =>
96 location.listTrackerConsumers.pages({ TrackerName: trackerName }).pipe(
97 EffectStream.runCollect,
98 Effect.map((chunk) =>
99 Array.from(chunk).flatMap((page) =>
100 page.ConsumerArns.map((consumerArn) => ({
101 trackerName,
102 consumerArn,
103 })),
104 ),
105 ),
106 // Tracker deleted between the two list calls.
107 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([])),
108 ),
109 { concurrency: 5 },
110 );
111 return associations.flat();
112 }),
113 read: Effect.fn(function* ({ olds, output }) {
114 const trackerName = output?.trackerName ?? olds?.trackerName;
115 const consumerArn = output?.consumerArn ?? olds?.consumerArn;
116 if (!trackerName || !consumerArn) return undefined;
117 const exists = yield* observeAssociation(trackerName, consumerArn);
118 return exists ? { trackerName, consumerArn } : undefined;
119 }),
120 diff: Effect.fn(function* ({ news, olds }) {
121 if (!isResolved(news)) return;
122 if (olds.trackerName !== news.trackerName || olds.consumerArn !== news.consumerArn) {
123 return { action: "replace" } as const;
124 }
125 }),
126 reconcile: Effect.fn(function* ({ news, session }) {
127 const exists = yield* observeAssociation(news.trackerName, news.consumerArn);
128 if (!exists) {
129 yield* location
130 .associateTrackerConsumer({
131 TrackerName: news.trackerName,
132 ConsumerArn: news.consumerArn,
133 })
134 .pipe(
135 // Racing reconciles: the association already exists.
136 Effect.catchTag("ConflictException", () => Effect.void),
137 );
138 }
139 yield* session.note(`${news.trackerName} -> ${news.consumerArn}`);
140 return {
141 trackerName: news.trackerName,
142 consumerArn: news.consumerArn,
143 };
144 }),
145 delete: Effect.fn(function* ({ output }) {
146 yield* location
147 .disassociateTrackerConsumer({
148 TrackerName: output.trackerName,
149 ConsumerArn: output.consumerArn,
150 })
151 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
152 }),
153 };
154 }),
155 );