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

main
ts 159 lines 5.41 KB
Raw
1 import * as iot from "@distilled.cloud/aws/iot";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 export interface ThingProps {
11 /**
12 * Name of the thing. Must be unique in your account and region and may only
13 * contain letters, numbers, colons, underscores, and hyphens.
14 * If omitted, a unique name is generated. Changing it replaces the thing.
15 */
16 thingName?: string;
17
18 /**
19 * Name of the {@link ThingType} to associate with this thing.
20 */
21 thingTypeName?: string;
22
23 /**
24 * A set of string attributes (key/value pairs) to store on the thing.
25 */
26 attributes?: Record<string, string>;
27 }
28
29 export interface Thing extends Resource<
30 "AWS.IoT.Thing",
31 ThingProps,
32 {
33 /** The name of the thing. */
34 thingName: string;
35 /** The ARN of the thing. */
36 thingArn: string;
37 },
38 never,
39 Providers
40 > {}
41
42 /**
43 * An AWS IoT Thing — the cloud representation of a physical device.
44 *
45 * ### Creating a Thing
46 * **Example:** Basic Thing
47 * ```typescript
48 * const thing = yield* Thing("sensor", {});
49 * ```
50 *
51 * **Example:** Thing with Attributes
52 * ```typescript
53 * const thing = yield* Thing("sensor", {
54 * thingName: "temperature-sensor-01",
55 * attributes: { location: "warehouse-a", model: "acme-t1000" },
56 * });
57 * ```
58 *
59 * @resource
60 */
61 export const Thing = Resource<Thing>("AWS.IoT.Thing");
62
63 export const ThingProvider = () =>
64 Provider.effect(
65 Thing,
66 Effect.gen(function* () {
67 const createName = Effect.fn(function* (id: string, props: ThingProps) {
68 return props.thingName ?? (yield* createPhysicalName({ id }));
69 });
70
71 return Thing.Provider.of({
72 stables: ["thingName", "thingArn"],
73 list: () =>
74 iot.listThings.pages({}).pipe(
75 Stream.runCollect,
76 Effect.map((chunk) =>
77 Array.from(chunk).flatMap((page) =>
78 (page.things ?? [])
79 .filter((t) => t.thingName != null && t.thingArn != null)
80 .map((t) => ({
81 thingName: t.thingName!,
82 thingArn: t.thingArn!,
83 })),
84 ),
85 ),
86 ),
87 read: Effect.fn(function* ({ id, olds, output }) {
88 const thingName = output?.thingName ?? (yield* createName(id, olds ?? {}));
89 const found = yield* iot
90 .describeThing({ thingName })
91 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
92 if (!found) return undefined;
93 return {
94 thingName,
95 thingArn: found.thingArn!,
96 };
97 }),
98 diff: Effect.fn(function* ({ id, news = {}, olds = {} }) {
99 if (!isResolved(news)) return undefined;
100 const oldName = yield* createName(id, olds);
101 const newName = yield* createName(id, news);
102 if (oldName !== newName) return { action: "replace" } as const;
103 return undefined;
104 }),
105 reconcile: Effect.fn(function* ({ id, news = {}, output, session }) {
106 const thingName = output?.thingName ?? (yield* createName(id, news));
107
108 // 1. OBSERVE — cloud is authoritative; output is only an id cache.
109 let live = yield* iot
110 .describeThing({ thingName })
111 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
112
113 // 2. ENSURE — create if missing; tolerate an AlreadyExists race.
114 if (live === undefined) {
115 yield* iot
116 .createThing({
117 thingName,
118 thingTypeName: news.thingTypeName,
119 attributePayload: news.attributes ? { attributes: news.attributes } : undefined,
120 })
121 .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void));
122 live = yield* iot.describeThing({ thingName });
123 } else {
124 // 3. SYNC — converge attributes + thing type to desired.
125 const desiredAttrs = news.attributes ?? {};
126 const observedAttrs = live.attributes ?? {};
127 const attrsChanged = JSON.stringify(desiredAttrs) !== JSON.stringify(observedAttrs);
128 const typeChanged =
129 (news.thingTypeName ?? undefined) !== (live.thingTypeName ?? undefined);
130 if (attrsChanged || typeChanged) {
131 yield* iot.updateThing({
132 thingName,
133 thingTypeName: news.thingTypeName,
134 removeThingType:
135 news.thingTypeName === undefined && live.thingTypeName !== undefined
136 ? true
137 : undefined,
138 attributePayload: attrsChanged
139 ? { attributes: desiredAttrs, merge: false }
140 : undefined,
141 });
142 live = yield* iot.describeThing({ thingName });
143 }
144 }
145
146 yield* session.note(thingName);
147 return {
148 thingName,
149 thingArn: live.thingArn!,
150 };
151 }),
152 delete: Effect.fn(function* ({ output }) {
153 yield* iot
154 .deleteThing({ thingName: output.thingName })
155 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
156 }),
157 });
158 }),
159 );