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

main
ts 193 lines 6.77 KB
Raw
1 import * as omics from "@distilled.cloud/aws/omics";
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, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11 import { fetchOmicsTags, syncOmicsTags } from "./internal.ts";
12
13 export interface ReferenceStoreProps {
14 /**
15 * A name for the reference store. If omitted, a unique name is generated
16 * from the app, stage, and logical ID. Changing the name replaces the
17 * store. Must match `[\w -.]+` and be 1-127 characters.
18 */
19 name?: string;
20 /**
21 * A description for the reference store. Changing the description replaces
22 * the store (HealthOmics has no update-store API).
23 */
24 description?: string;
25 /**
26 * Server-side encryption (SSE) settings. Defaults to an AWS-owned key.
27 * Changing this replaces the store.
28 */
29 sseConfig?: {
30 /** Encryption type. `KMS` uses a customer-managed key. */
31 type: "KMS";
32 /** ARN of the customer-managed KMS key. Required when `type` is `KMS`. */
33 keyArn?: string;
34 };
35 /**
36 * Tags to apply to the reference store. Merged with internal Alchemy tags.
37 */
38 tags?: Record<string, string>;
39 }
40
41 export interface ReferenceStore extends Resource<
42 "AWS.Omics.ReferenceStore",
43 ReferenceStoreProps,
44 {
45 /**
46 * ID of the reference store.
47 */
48 referenceStoreId: string;
49 /**
50 * ARN of the reference store.
51 */
52 referenceStoreArn: string;
53 /**
54 * Name of the reference store.
55 */
56 name: string;
57 /**
58 * When the reference store was created (ISO-8601).
59 */
60 creationTime: string;
61 },
62 never,
63 Providers
64 > {}
65
66 /**
67 * An Amazon HealthOmics reference store — a container for reference genomes
68 * that read sets are aligned against.
69 *
70 * A reference store name is auto-generated from the app, stage, and logical
71 * ID unless you provide one. HealthOmics offers no update-store API, so any
72 * change to `name`, `description`, or `sseConfig` replaces the store. A store
73 * can only be deleted once it contains no reference genomes.
74 * ### Creating a Reference Store
75 * **Example:** Basic Reference Store
76 * ```typescript
77 * import * as Omics from "@alchemy.run/aws/Omics";
78 *
79 * const store = yield* Omics.ReferenceStore("References");
80 * ```
81 *
82 * **Example:** Named Reference Store with Description
83 * ```typescript
84 * const store = yield* Omics.ReferenceStore("References", {
85 * name: "human-references",
86 * description: "GRCh38 reference genomes",
87 * });
88 * ```
89 *
90 * ### Encryption
91 * **Example:** Customer-managed KMS key
92 * ```typescript
93 * const store = yield* Omics.ReferenceStore("References", {
94 * sseConfig: {
95 * type: "KMS",
96 * keyArn: "arn:aws:kms:us-east-1:123456789012:key/abc-123",
97 * },
98 * });
99 * ```
100 *
101 * @resource
102 */
103 export const ReferenceStore = Resource<ReferenceStore>("AWS.Omics.ReferenceStore");
104
105 export const ReferenceStoreProvider = () =>
106 Provider.effect(
107 ReferenceStore,
108 Effect.gen(function* () {
109 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
110 return props.name ?? (yield* createPhysicalName({ id, maxLength: 96 }));
111 });
112
113 const toAttrs = (store: { id: string; arn: string; name?: string; creationTime: Date }) => ({
114 referenceStoreId: store.id,
115 referenceStoreArn: store.arn,
116 name: store.name ?? "",
117 creationTime: store.creationTime.toISOString(),
118 });
119
120 return ReferenceStore.Provider.of({
121 stables: ["referenceStoreId", "referenceStoreArn", "name", "creationTime"],
122 list: () =>
123 omics.listReferenceStores.items({}).pipe(
124 Stream.map(toAttrs),
125 Stream.runCollect,
126 Effect.map((chunk) => Array.from(chunk)),
127 ),
128 read: Effect.fn(function* ({ id, output }) {
129 if (output?.referenceStoreId === undefined) return undefined;
130 const found = yield* omics
131 .getReferenceStore({ id: output.referenceStoreId })
132 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
133 if (found === undefined) return undefined;
134 const attrs = toAttrs(found);
135 const tags = yield* fetchOmicsTags(found.arn);
136 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
137 }),
138 diff: Effect.fn(function* ({ id, news, olds }) {
139 if (!isResolved(news)) return undefined;
140 const prev = olds ?? {};
141 const oldName = yield* createName(id, prev);
142 const newName = yield* createName(id, news);
143 if (oldName !== newName) return { action: "replace" } as const;
144 if ((prev.description ?? "") !== (news.description ?? "")) {
145 return { action: "replace" } as const;
146 }
147 if (
148 prev.sseConfig?.type !== news.sseConfig?.type ||
149 prev.sseConfig?.keyArn !== news.sseConfig?.keyArn
150 ) {
151 return { action: "replace" } as const;
152 }
153 }),
154 reconcile: Effect.fn(function* ({ id, news, output, session }) {
155 const name = output?.name ?? (yield* createName(id, news));
156 const internalTags = yield* createInternalTags(id);
157 const desiredTags = { ...news.tags, ...internalTags };
158
159 // OBSERVE — the store id is a server-generated cache; if the store
160 // was deleted out-of-band, getReferenceStore returns NotFound and we
161 // recreate.
162 let store =
163 output?.referenceStoreId === undefined
164 ? undefined
165 : yield* omics
166 .getReferenceStore({ id: output.referenceStoreId })
167 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
168
169 // ENSURE — create if missing. The store name is not a unique key, so
170 // there is no AlreadyExists race to tolerate here.
171 if (store === undefined) {
172 store = yield* omics.createReferenceStore({
173 name,
174 description: news.description,
175 sseConfig: news.sseConfig,
176 tags: desiredTags,
177 });
178 }
179
180 // SYNC TAGS — diff against observed cloud tags so adoption converges.
181 yield* syncOmicsTags(store.arn, desiredTags);
182
183 yield* session.note(store.id);
184 return toAttrs(store);
185 }),
186 delete: Effect.fn(function* ({ output }) {
187 yield* omics
188 .deleteReferenceStore({ id: output.referenceStoreId })
189 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
190 }),
191 });
192 }),
193 );