@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, 06:00 UTC

main
ts 288 lines 10.2 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 Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import { fetchOmicsTags, syncOmicsTags } from "./internal.ts";
13
14 /**
15 * Coerce a definition zip that survived engine plan/state serialization back
16 * into a `Uint8Array`. The engine round-trips a `Uint8Array` prop as an
17 * index-keyed plain object (`{"0":80,"1":75,...}`), so a provider that carries
18 * binary members must rebuild the array before encoding the request.
19 */
20 const coerceBytes = (value: unknown): Uint8Array | undefined => {
21 if (value === undefined) return undefined;
22 if (value instanceof Uint8Array) return value;
23 if (Array.isArray(value)) return Uint8Array.from(value as number[]);
24 if (typeof value === "object" && value !== null) {
25 const entries = Object.entries(value as Record<string, number>)
26 .map(([k, v]) => [Number(k), v] as const)
27 .sort((a, b) => a[0] - b[0]);
28 return Uint8Array.from(entries.map(([, v]) => v));
29 }
30 return undefined;
31 };
32
33 export interface WorkflowProps {
34 /**
35 * A name for the workflow. If omitted, a unique name is generated from the
36 * app, stage, and logical ID. Mutable.
37 */
38 name?: string;
39 /**
40 * A description for the workflow. Mutable.
41 */
42 description?: string;
43 /**
44 * The workflow engine. Immutable — changing it replaces the workflow.
45 * @default "WDL"
46 */
47 engine?: "WDL" | "NEXTFLOW" | "CWL";
48 /**
49 * The definition of the workflow, as a zip archive of the workflow source
50 * files. Provided as raw bytes. Mutually exclusive with `definitionUri`.
51 * Immutable — changing it replaces the workflow.
52 */
53 definitionZip?: Uint8Array;
54 /**
55 * The S3 URI of a zip archive containing the workflow definition. Mutually
56 * exclusive with `definitionZip`. Immutable — changing it replaces the
57 * workflow.
58 */
59 definitionUri?: string;
60 /**
61 * The path of the main definition file for the workflow within the zip
62 * archive. Immutable.
63 */
64 main?: string;
65 /**
66 * A parameter template describing the workflow's input parameters.
67 * Immutable.
68 */
69 parameterTemplate?: Record<string, { description?: string; optional?: boolean }>;
70 /**
71 * The default static storage capacity (in gibibytes) for runs that use this
72 * workflow. Mutable.
73 */
74 storageCapacity?: number;
75 /**
76 * The storage type for runs that use this workflow. Mutable.
77 * @default "DYNAMIC"
78 */
79 storageType?: "STATIC" | "DYNAMIC";
80 /**
81 * The computational accelerators used by the workflow. Immutable.
82 */
83 accelerators?: "GPU";
84 /**
85 * Tags to apply to the workflow. Merged with internal Alchemy tags.
86 */
87 tags?: Record<string, string>;
88 }
89
90 export interface Workflow extends Resource<
91 "AWS.Omics.Workflow",
92 WorkflowProps,
93 {
94 /**
95 * ID of the workflow.
96 */
97 workflowId: string;
98 /**
99 * ARN of the workflow.
100 */
101 workflowArn: string;
102 /**
103 * Name of the workflow.
104 */
105 name: string;
106 /**
107 * Workflow status (e.g. `ACTIVE`, `CREATING`, `UPDATING`, `FAILED`).
108 */
109 status: string;
110 },
111 never,
112 Providers
113 > {}
114
115 /**
116 * An Amazon HealthOmics private workflow — a bioinformatics workflow (WDL,
117 * Nextflow, or CWL) that is executed as one or more runs.
118 *
119 * A workflow name is auto-generated from the app, stage, and logical ID
120 * unless you provide one. The workflow definition (`engine`, `definitionZip`,
121 * `definitionUri`, `main`, `parameterTemplate`, `accelerators`) is immutable —
122 * changing any of it replaces the workflow. `name`, `description`,
123 * `storageCapacity`, and `storageType` are updated in place.
124 * ### Creating a Workflow
125 * **Example:** Workflow from an inline definition zip
126 * ```typescript
127 * import * as Omics from "@alchemy.run/aws/Omics";
128 *
129 * const workflow = yield* Omics.Workflow("Hello", {
130 * engine: "WDL",
131 * main: "main.wdl",
132 * definitionZip: myZipBytes,
133 * });
134 * ```
135 *
136 * **Example:** Workflow from an S3-hosted definition
137 * ```typescript
138 * const workflow = yield* Omics.Workflow("Hello", {
139 * engine: "NEXTFLOW",
140 * definitionUri: "s3://my-bucket/workflows/hello.zip",
141 * });
142 * ```
143 *
144 * @resource
145 */
146 export const Workflow = Resource<Workflow>("AWS.Omics.Workflow");
147
148 export const WorkflowProvider = () =>
149 Provider.effect(
150 Workflow,
151 Effect.gen(function* () {
152 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
153 return props.name ?? (yield* createPhysicalName({ id, maxLength: 96 }));
154 });
155
156 // A freshly created workflow is CREATING until its definition is
157 // validated; wait until it reaches a terminal state before returning so
158 // downstream runs (and deletes) don't race the async provisioning.
159 const waitUntilReady = Effect.fn(function* (workflowId: string) {
160 const final = yield* omics.getWorkflow({ id: workflowId }).pipe(
161 Effect.repeat({
162 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(11)]),
163 until: (w) => w.status === "ACTIVE" || w.status === "FAILED",
164 }),
165 );
166 if (final.status === "FAILED") {
167 return yield* omics.ValidationException.make({
168 message: `Workflow ${workflowId} failed: ${final.statusMessage ?? "unknown"}`,
169 });
170 }
171 return final;
172 });
173
174 return Workflow.Provider.of({
175 stables: ["workflowId", "workflowArn"],
176 list: () =>
177 omics.listWorkflows.items({}).pipe(
178 Stream.map((item) => ({
179 workflowId: item.id!,
180 workflowArn: item.arn!,
181 name: item.name ?? "",
182 status: item.status ?? "",
183 })),
184 Stream.runCollect,
185 Effect.map((chunk) => Array.from(chunk)),
186 ),
187 read: Effect.fn(function* ({ id, output }) {
188 if (output?.workflowId === undefined) return undefined;
189 const found = yield* omics
190 .getWorkflow({ id: output.workflowId })
191 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
192 if (found === undefined || found.id === undefined) return undefined;
193 const attrs = {
194 workflowId: found.id,
195 workflowArn: found.arn!,
196 name: found.name ?? "",
197 status: found.status ?? "",
198 };
199 const tags = yield* fetchOmicsTags(found.arn!);
200 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
201 }),
202 diff: Effect.fn(function* ({ news, olds }) {
203 if (!isResolved(news)) return undefined;
204 const prev = olds ?? {};
205 if (
206 (prev.engine ?? "WDL") !== (news.engine ?? "WDL") ||
207 (prev.definitionUri ?? "") !== (news.definitionUri ?? "") ||
208 (prev.main ?? "") !== (news.main ?? "") ||
209 (prev.accelerators ?? "") !== (news.accelerators ?? "")
210 ) {
211 return { action: "replace" } as const;
212 }
213 }),
214 reconcile: Effect.fn(function* ({ id, news, output, session }) {
215 const name = output?.name ?? (yield* createName(id, news));
216 const internalTags = yield* createInternalTags(id);
217 const desiredTags = { ...news.tags, ...internalTags };
218
219 let workflow =
220 output?.workflowId === undefined
221 ? undefined
222 : yield* omics
223 .getWorkflow({ id: output.workflowId })
224 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
225
226 if (workflow === undefined || workflow.id === undefined) {
227 const created = yield* omics.createWorkflow({
228 name,
229 description: news.description,
230 engine: news.engine ?? "WDL",
231 definitionZip: coerceBytes(news.definitionZip),
232 definitionUri: news.definitionUri,
233 main: news.main,
234 parameterTemplate: news.parameterTemplate,
235 storageCapacity: news.storageCapacity,
236 storageType: news.storageType,
237 accelerators: news.accelerators,
238 requestId: `alchemy-${id}`,
239 tags: desiredTags,
240 });
241 yield* waitUntilReady(created.id!);
242 workflow = yield* omics.getWorkflow({ id: created.id! });
243 } else {
244 const patch: {
245 name?: string;
246 description?: string;
247 storageType?: string;
248 storageCapacity?: number;
249 } = {};
250 if (news.name !== undefined && news.name !== workflow.name) {
251 patch.name = news.name;
252 }
253 if (news.description !== undefined && news.description !== workflow.description) {
254 patch.description = news.description;
255 }
256 if (news.storageType !== undefined && news.storageType !== workflow.storageType) {
257 patch.storageType = news.storageType;
258 }
259 if (
260 news.storageCapacity !== undefined &&
261 news.storageCapacity !== workflow.storageCapacity
262 ) {
263 patch.storageCapacity = news.storageCapacity;
264 }
265 if (Object.keys(patch).length > 0) {
266 yield* omics.updateWorkflow({ id: workflow.id, ...patch });
267 workflow = yield* omics.getWorkflow({ id: workflow.id });
268 }
269 }
270
271 yield* syncOmicsTags(workflow.arn!, desiredTags);
272
273 yield* session.note(workflow.id!);
274 return {
275 workflowId: workflow.id!,
276 workflowArn: workflow.arn!,
277 name: workflow.name ?? name,
278 status: workflow.status ?? "",
279 };
280 }),
281 delete: Effect.fn(function* ({ output }) {
282 yield* omics
283 .deleteWorkflow({ id: output.workflowId })
284 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
285 }),
286 });
287 }),
288 );