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

main
ts 218 lines 7.59 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, tagRecord } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import { syncOmicsTags } from "./internal.ts";
13
14 // Variant store names must match `[a-z]([a-z0-9_]){0,254}` — the generated
15 // physical name (hyphens, mixed case) is coerced to fit.
16 const toStoreName = (raw: string) => {
17 const sanitized = raw.toLowerCase().replace(/[^a-z0-9_]/g, "_");
18 const prefixed = /^[a-z]/.test(sanitized) ? sanitized : `a${sanitized}`;
19 return prefixed.slice(0, 255);
20 };
21
22 export interface VariantStoreProps {
23 /**
24 * A name for the variant store. If omitted, a unique name is generated from
25 * the app, stage, and logical ID. Must be lowercase, start with a letter,
26 * and contain only `[a-z0-9_]`. Changing the name replaces the store.
27 */
28 name?: string;
29 /**
30 * A description for the variant store. Mutable.
31 */
32 description?: string;
33 /**
34 * The genome reference for the store, as a reference ARN. Required.
35 * Immutable — changing it replaces the store.
36 */
37 reference: {
38 /** ARN of a reference in a HealthOmics reference store. */
39 referenceArn: string;
40 };
41 /**
42 * Server-side encryption (SSE) settings. Defaults to an AWS-owned key.
43 * Immutable.
44 */
45 sseConfig?: {
46 /** Encryption type. `KMS` uses a customer-managed key. */
47 type: "KMS";
48 /** ARN of the customer-managed KMS key. Required when `type` is `KMS`. */
49 keyArn?: string;
50 };
51 /**
52 * Tags to apply to the variant store. Merged with internal Alchemy tags.
53 */
54 tags?: Record<string, string>;
55 }
56
57 export interface VariantStore extends Resource<
58 "AWS.Omics.VariantStore",
59 VariantStoreProps,
60 {
61 /**
62 * ID of the variant store.
63 */
64 variantStoreId: string;
65 /**
66 * ARN of the variant store.
67 */
68 variantStoreArn: string;
69 /**
70 * Name of the variant store.
71 */
72 name: string;
73 /**
74 * Store status (e.g. `ACTIVE`, `CREATING`, `UPDATING`, `FAILED`).
75 */
76 status: string;
77 },
78 never,
79 Providers
80 > {}
81
82 /**
83 * An Amazon HealthOmics variant store — a container for genomic variant data
84 * (VCF) aligned to a reference genome.
85 *
86 * A variant store name is auto-generated from the app, stage, and logical ID
87 * unless you provide one. The `reference` and `sseConfig` are immutable —
88 * changing either replaces the store. `description` is updated in place.
89 * ### Creating a Variant Store
90 * **Example:** Basic Variant Store
91 * ```typescript
92 * import * as Omics from "@alchemy.run/aws/Omics";
93 *
94 * const store = yield* Omics.VariantStore("Variants", {
95 * reference: {
96 * referenceArn: "arn:aws:omics:us-east-1:123456789012:referenceStore/1234567890/reference/0987654321",
97 * },
98 * });
99 * ```
100 *
101 * @resource
102 */
103 export const VariantStore = Resource<VariantStore>("AWS.Omics.VariantStore");
104
105 export const VariantStoreProvider = () =>
106 Provider.effect(
107 VariantStore,
108 Effect.gen(function* () {
109 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
110 return props.name ?? toStoreName(yield* createPhysicalName({ id, maxLength: 96 }));
111 });
112
113 const waitUntilActive = Effect.fn(function* (name: string) {
114 const final = yield* omics.getVariantStore({ name }).pipe(
115 Effect.repeat({
116 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(23)]),
117 until: (s) => s.status === "ACTIVE" || s.status === "FAILED",
118 }),
119 );
120 if (final.status === "FAILED") {
121 return yield* omics.ValidationException.make({
122 message: `Variant store ${name} failed: ${final.statusMessage ?? "unknown"}`,
123 });
124 }
125 return final;
126 });
127
128 return VariantStore.Provider.of({
129 stables: ["variantStoreId", "variantStoreArn", "name"],
130 list: () =>
131 omics.listVariantStores.items({}).pipe(
132 Stream.map((item) => ({
133 variantStoreId: item.id!,
134 variantStoreArn: item.storeArn!,
135 name: item.name ?? "",
136 status: item.status ?? "",
137 })),
138 Stream.runCollect,
139 Effect.map((chunk) => Array.from(chunk)),
140 ),
141 read: Effect.fn(function* ({ id, olds, output }) {
142 const name = output?.name ?? (yield* createName(id, olds ?? {}));
143 const found = yield* omics
144 .getVariantStore({ name })
145 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
146 if (found === undefined) return undefined;
147 const attrs = {
148 variantStoreId: found.id,
149 variantStoreArn: found.storeArn,
150 name: found.name,
151 status: found.status,
152 };
153 return (yield* hasAlchemyTags(id, tagRecord(found.tags))) ? attrs : Unowned(attrs);
154 }),
155 diff: Effect.fn(function* ({ id, news, olds }) {
156 if (!isResolved(news)) return undefined;
157 const prev = olds ?? {};
158 const oldName = yield* createName(id, prev);
159 const newName = yield* createName(id, news);
160 if (oldName !== newName) return { action: "replace" } as const;
161 if (
162 (prev.reference?.referenceArn ?? "") !== (news.reference?.referenceArn ?? "") ||
163 prev.sseConfig?.type !== news.sseConfig?.type ||
164 prev.sseConfig?.keyArn !== news.sseConfig?.keyArn
165 ) {
166 return { action: "replace" } as const;
167 }
168 }),
169 reconcile: Effect.fn(function* ({ id, news, output, session }) {
170 const name = output?.name ?? (yield* createName(id, news));
171 const internalTags = yield* createInternalTags(id);
172 const desiredTags = { ...news.tags, ...internalTags };
173
174 let store = yield* omics
175 .getVariantStore({ name })
176 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
177
178 if (store === undefined) {
179 yield* omics
180 .createVariantStore({
181 name,
182 description: news.description,
183 reference: news.reference,
184 sseConfig: news.sseConfig,
185 tags: desiredTags,
186 })
187 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
188 store = yield* waitUntilActive(name);
189 } else if (store.status === "CREATING") {
190 store = yield* waitUntilActive(name);
191 }
192
193 if (news.description !== undefined && news.description !== store.description) {
194 yield* omics.updateVariantStore({
195 name,
196 description: news.description,
197 });
198 store = yield* omics.getVariantStore({ name });
199 }
200
201 yield* syncOmicsTags(store.storeArn, desiredTags);
202
203 yield* session.note(store.id);
204 return {
205 variantStoreId: store.id,
206 variantStoreArn: store.storeArn,
207 name: store.name,
208 status: store.status,
209 };
210 }),
211 delete: Effect.fn(function* ({ output }) {
212 yield* omics
213 .deleteVariantStore({ name: output.name, force: true })
214 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
215 }),
216 });
217 }),
218 );