@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 258 lines 9.23 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 // Annotation/variant store names must match `[a-z]([a-z0-9_]){0,254}` — the
15 // generated 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 AnnotationStoreProps {
23 /**
24 * A name for the annotation store. If omitted, a unique name is generated
25 * from the app, stage, and logical ID. Must be lowercase, start with a
26 * letter, and contain only `[a-z0-9_]`. Changing the name replaces the
27 * store.
28 */
29 name?: string;
30 /**
31 * A description for the annotation store. Mutable.
32 */
33 description?: string;
34 /**
35 * The annotation file format of the store. Immutable — changing it replaces
36 * the store.
37 */
38 storeFormat: "GFF" | "TSV" | "VCF";
39 /**
40 * The genome reference for the store, as a reference ARN. Required for
41 * `GFF` and `VCF` stores. Immutable — changing it replaces the store.
42 */
43 reference?: {
44 /** ARN of a reference in a HealthOmics reference store. */
45 referenceArn: string;
46 };
47 /**
48 * File parsing options for `TSV` stores. Immutable.
49 */
50 storeOptions?: {
51 /** Parsing options for TSV-format annotation files. */
52 tsvStoreOptions: {
53 /** The store's annotation type (e.g. `GENERIC`, `CHR_POS_REF_ALT`). */
54 annotationType?: string;
55 /** Maps TSV format fields to the store's header columns. */
56 formatToHeader?: Record<string, string>;
57 /** Column-name-to-type entries describing the store's schema. */
58 schema?: Record<string, string>[];
59 };
60 };
61 /**
62 * Server-side encryption (SSE) settings. Defaults to an AWS-owned key.
63 * Immutable.
64 */
65 sseConfig?: {
66 /** Encryption type. `KMS` uses a customer-managed key. */
67 type: "KMS";
68 /** ARN of the customer-managed KMS key. Required when `type` is `KMS`. */
69 keyArn?: string;
70 };
71 /**
72 * Tags to apply to the annotation store. Merged with internal Alchemy tags.
73 */
74 tags?: Record<string, string>;
75 }
76
77 export interface AnnotationStore extends Resource<
78 "AWS.Omics.AnnotationStore",
79 AnnotationStoreProps,
80 {
81 /**
82 * ID of the annotation store.
83 */
84 annotationStoreId: string;
85 /**
86 * ARN of the annotation store.
87 */
88 annotationStoreArn: string;
89 /**
90 * Name of the annotation store.
91 */
92 name: string;
93 /**
94 * Store status (e.g. `ACTIVE`, `CREATING`, `UPDATING`, `FAILED`).
95 */
96 status: string;
97 },
98 never,
99 Providers
100 > {}
101
102 /**
103 * An Amazon HealthOmics annotation store — a container for genome annotation
104 * data (GFF, TSV, or VCF) aligned to a reference genome.
105 *
106 * An annotation store name is auto-generated from the app, stage, and logical
107 * ID unless you provide one. The `storeFormat`, `reference`, `storeOptions`,
108 * and `sseConfig` are immutable — changing any of them replaces the store.
109 * `description` is updated in place.
110 * ### Creating an Annotation Store
111 * **Example:** VCF Annotation Store
112 * ```typescript
113 * import * as Omics from "@alchemy.run/aws/Omics";
114 *
115 * const store = yield* Omics.AnnotationStore("Annotations", {
116 * storeFormat: "VCF",
117 * reference: {
118 * referenceArn: "arn:aws:omics:us-east-1:123456789012:referenceStore/1234567890/reference/0987654321",
119 * },
120 * });
121 * ```
122 *
123 * **Example:** TSV Annotation Store
124 * ```typescript
125 * const store = yield* Omics.AnnotationStore("Annotations", {
126 * storeFormat: "TSV",
127 * storeOptions: {
128 * tsvStoreOptions: { annotationType: "GENERIC" },
129 * },
130 * });
131 * ```
132 *
133 * @resource
134 */
135 export const AnnotationStore = Resource<AnnotationStore>("AWS.Omics.AnnotationStore");
136
137 export const AnnotationStoreProvider = () =>
138 Provider.effect(
139 AnnotationStore,
140 Effect.gen(function* () {
141 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
142 return props.name ?? toStoreName(yield* createPhysicalName({ id, maxLength: 96 }));
143 });
144
145 const waitUntilActive = Effect.fn(function* (name: string) {
146 const final = yield* omics.getAnnotationStore({ name }).pipe(
147 Effect.repeat({
148 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(23)]),
149 until: (s) => s.status === "ACTIVE" || s.status === "FAILED",
150 }),
151 );
152 if (final.status === "FAILED") {
153 return yield* omics.ValidationException.make({
154 message: `Annotation store ${name} failed: ${final.statusMessage ?? "unknown"}`,
155 });
156 }
157 return final;
158 });
159
160 return AnnotationStore.Provider.of({
161 stables: ["annotationStoreId", "annotationStoreArn", "name"],
162 list: () =>
163 omics.listAnnotationStores.items({}).pipe(
164 Stream.map((item) => ({
165 annotationStoreId: item.id!,
166 annotationStoreArn: item.storeArn!,
167 name: item.name ?? "",
168 status: item.status ?? "",
169 })),
170 Stream.runCollect,
171 Effect.map((chunk) => Array.from(chunk)),
172 ),
173 read: Effect.fn(function* ({ id, olds, output }) {
174 const name = output?.name ?? (yield* createName(id, olds ?? {}));
175 const found = yield* omics
176 .getAnnotationStore({ name })
177 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
178 if (found === undefined) return undefined;
179 const attrs = {
180 annotationStoreId: found.id,
181 annotationStoreArn: found.storeArn,
182 name: found.name,
183 status: found.status,
184 };
185 return (yield* hasAlchemyTags(id, tagRecord(found.tags))) ? attrs : Unowned(attrs);
186 }),
187 diff: Effect.fn(function* ({ id, news, olds }) {
188 if (!isResolved(news)) return undefined;
189 const prev = olds ?? {};
190 const oldName = yield* createName(id, prev);
191 const newName = yield* createName(id, news);
192 if (oldName !== newName) return { action: "replace" } as const;
193 if (
194 (prev.storeFormat ?? "") !== (news.storeFormat ?? "") ||
195 (prev.reference?.referenceArn ?? "") !== (news.reference?.referenceArn ?? "") ||
196 prev.sseConfig?.type !== news.sseConfig?.type ||
197 prev.sseConfig?.keyArn !== news.sseConfig?.keyArn
198 ) {
199 return { action: "replace" } as const;
200 }
201 }),
202 reconcile: Effect.fn(function* ({ id, news, output, session }) {
203 const name = output?.name ?? (yield* createName(id, news));
204 const internalTags = yield* createInternalTags(id);
205 const desiredTags = { ...news.tags, ...internalTags };
206
207 // OBSERVE — annotation stores are addressable by name.
208 let store = yield* omics
209 .getAnnotationStore({ name })
210 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
211
212 // ENSURE — create if missing, tolerating a create race, then wait
213 // for the async provisioning to reach a terminal state.
214 if (store === undefined) {
215 yield* omics
216 .createAnnotationStore({
217 name,
218 description: news.description,
219 storeFormat: news.storeFormat,
220 reference: news.reference,
221 storeOptions: news.storeOptions,
222 sseConfig: news.sseConfig,
223 tags: desiredTags,
224 })
225 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
226 store = yield* waitUntilActive(name);
227 } else if (store.status === "CREATING") {
228 store = yield* waitUntilActive(name);
229 }
230
231 // SYNC — description is the only mutable field.
232 if (news.description !== undefined && news.description !== store.description) {
233 yield* omics.updateAnnotationStore({
234 name,
235 description: news.description,
236 });
237 store = yield* omics.getAnnotationStore({ name });
238 }
239
240 // SYNC TAGS — diff against observed cloud tags so adoption converges.
241 yield* syncOmicsTags(store.storeArn, desiredTags);
242
243 yield* session.note(store.id);
244 return {
245 annotationStoreId: store.id,
246 annotationStoreArn: store.storeArn,
247 name: store.name,
248 status: store.status,
249 };
250 }),
251 delete: Effect.fn(function* ({ output }) {
252 yield* omics
253 .deleteAnnotationStore({ name: output.name, force: true })
254 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
255 }),
256 });
257 }),
258 );