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

main
ts 209 lines 7.26 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 SequenceStoreProps {
14 /**
15 * A name for the sequence 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 sequence 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 * An S3 location that is used as the destination for read sets whose
37 * upload failed. Changing this replaces the store.
38 */
39 fallbackLocation?: string;
40 /**
41 * The ETag algorithm family to use for ingested read sets. Changing this
42 * replaces the store. Valid values are `MD5up` and `SHA256up`.
43 */
44 eTagAlgorithmFamily?: "MD5up" | "SHA256up";
45 /**
46 * The tags keys to propagate to the S3 objects associated with read sets
47 * in this store. Changing this replaces the store.
48 */
49 propagatedSetLevelTags?: string[];
50 /**
51 * Tags to apply to the sequence store. Merged with internal Alchemy tags.
52 */
53 tags?: Record<string, string>;
54 }
55
56 export interface SequenceStore extends Resource<
57 "AWS.Omics.SequenceStore",
58 SequenceStoreProps,
59 {
60 /**
61 * ID of the sequence store.
62 */
63 sequenceStoreId: string;
64 /**
65 * ARN of the sequence store.
66 */
67 sequenceStoreArn: string;
68 /**
69 * Name of the sequence store.
70 */
71 name: string;
72 /**
73 * When the sequence store was created (ISO-8601).
74 */
75 creationTime: string;
76 },
77 never,
78 Providers
79 > {}
80
81 /**
82 * An Amazon HealthOmics sequence store — a container for genomics read sets
83 * (FASTQ, BAM, CRAM).
84 *
85 * A sequence store name is auto-generated from the app, stage, and logical ID
86 * unless you provide one. HealthOmics offers no update-store API, so any
87 * change to an immutable property (name, description, encryption, fallback
88 * location, ETag algorithm) replaces the store. A store can only be deleted
89 * once it contains no read sets.
90 * ### Creating a Sequence Store
91 * **Example:** Basic Sequence Store
92 * ```typescript
93 * import * as Omics from "@alchemy.run/aws/Omics";
94 *
95 * const store = yield* Omics.SequenceStore("Reads");
96 * ```
97 *
98 * **Example:** Sequence Store with Fallback Location
99 * ```typescript
100 * const store = yield* Omics.SequenceStore("Reads", {
101 * name: "sample-reads",
102 * fallbackLocation: "s3://my-bucket/omics-fallback/",
103 * eTagAlgorithmFamily: "SHA256up",
104 * });
105 * ```
106 *
107 * ### Encryption
108 * **Example:** Customer-managed KMS key
109 * ```typescript
110 * const store = yield* Omics.SequenceStore("Reads", {
111 * sseConfig: {
112 * type: "KMS",
113 * keyArn: "arn:aws:kms:us-east-1:123456789012:key/abc-123",
114 * },
115 * });
116 * ```
117 *
118 * @resource
119 */
120 export const SequenceStore = Resource<SequenceStore>("AWS.Omics.SequenceStore");
121
122 export const SequenceStoreProvider = () =>
123 Provider.effect(
124 SequenceStore,
125 Effect.gen(function* () {
126 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
127 return props.name ?? (yield* createPhysicalName({ id, maxLength: 96 }));
128 });
129
130 const toAttrs = (store: { id: string; arn: string; name?: string; creationTime: Date }) => ({
131 sequenceStoreId: store.id,
132 sequenceStoreArn: store.arn,
133 name: store.name ?? "",
134 creationTime: store.creationTime.toISOString(),
135 });
136
137 return SequenceStore.Provider.of({
138 stables: ["sequenceStoreId", "sequenceStoreArn", "name", "creationTime"],
139 list: () =>
140 omics.listSequenceStores.items({}).pipe(
141 Stream.map(toAttrs),
142 Stream.runCollect,
143 Effect.map((chunk) => Array.from(chunk)),
144 ),
145 read: Effect.fn(function* ({ id, output }) {
146 if (output?.sequenceStoreId === undefined) return undefined;
147 const found = yield* omics
148 .getSequenceStore({ id: output.sequenceStoreId })
149 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
150 if (found === undefined) return undefined;
151 const attrs = toAttrs(found);
152 const tags = yield* fetchOmicsTags(found.arn);
153 return (yield* hasAlchemyTags(id, 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 ((prev.description ?? "") !== (news.description ?? "")) {
162 return { action: "replace" } as const;
163 }
164 if (
165 prev.sseConfig?.type !== news.sseConfig?.type ||
166 prev.sseConfig?.keyArn !== news.sseConfig?.keyArn ||
167 (prev.fallbackLocation ?? "") !== (news.fallbackLocation ?? "") ||
168 (prev.eTagAlgorithmFamily ?? "") !== (news.eTagAlgorithmFamily ?? "")
169 ) {
170 return { action: "replace" } as const;
171 }
172 }),
173 reconcile: Effect.fn(function* ({ id, news, output, session }) {
174 const name = output?.name ?? (yield* createName(id, news));
175 const internalTags = yield* createInternalTags(id);
176 const desiredTags = { ...news.tags, ...internalTags };
177
178 let store =
179 output?.sequenceStoreId === undefined
180 ? undefined
181 : yield* omics
182 .getSequenceStore({ id: output.sequenceStoreId })
183 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
184
185 if (store === undefined) {
186 store = yield* omics.createSequenceStore({
187 name,
188 description: news.description,
189 sseConfig: news.sseConfig,
190 fallbackLocation: news.fallbackLocation,
191 eTagAlgorithmFamily: news.eTagAlgorithmFamily,
192 propagatedSetLevelTags: news.propagatedSetLevelTags,
193 tags: desiredTags,
194 });
195 }
196
197 yield* syncOmicsTags(store.arn, desiredTags);
198
199 yield* session.note(store.id);
200 return toAttrs(store);
201 }),
202 delete: Effect.fn(function* ({ output }) {
203 yield* omics
204 .deleteSequenceStore({ id: output.sequenceStoreId })
205 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
206 }),
207 });
208 }),
209 );