@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 212 lines 7.39 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 { toWireMinutes } from "alchemy/Util/Duration";
9 import type * as Duration from "effect/Duration";
10 import * as Effect from "effect/Effect";
11 import * as Stream from "effect/Stream";
12 import type { Providers } from "../Providers.ts";
13 import { fetchOmicsTags, syncOmicsTags } from "./internal.ts";
14
15 export interface RunGroupProps {
16 /**
17 * A name for the run group. If omitted, a unique name is generated from the
18 * app, stage, and logical ID. Mutable.
19 */
20 name?: string;
21 /**
22 * The maximum number of vCPUs that can run concurrently across all active
23 * runs in the group. Mutable.
24 */
25 maxCpus?: number;
26 /**
27 * The maximum number of concurrent runs for the group. Mutable.
28 */
29 maxRuns?: number;
30 /**
31 * The maximum time for each run in the group, e.g. `"10 hours"` or
32 * `Duration.minutes(600)`. Sent to the API as whole minutes (a bare
33 * number is milliseconds). Mutable.
34 */
35 maxDuration?: Duration.Input;
36 /**
37 * The maximum number of GPUs that can run concurrently across all active
38 * runs in the group. Mutable.
39 */
40 maxGpus?: number;
41 /**
42 * Tags to apply to the run group. Merged with internal Alchemy tags.
43 */
44 tags?: Record<string, string>;
45 }
46
47 export interface RunGroup extends Resource<
48 "AWS.Omics.RunGroup",
49 RunGroupProps,
50 {
51 /**
52 * ID of the run group.
53 */
54 runGroupId: string;
55 /**
56 * ARN of the run group.
57 */
58 runGroupArn: string;
59 /**
60 * Name of the run group.
61 */
62 name: string;
63 },
64 never,
65 Providers
66 > {}
67
68 /**
69 * An Amazon HealthOmics run group — a resource-limit envelope for workflow
70 * runs. A run group caps the vCPUs, GPUs, concurrent runs, and per-run
71 * duration for the workflow runs assigned to it.
72 *
73 * A run group name is auto-generated from the app, stage, and logical ID
74 * unless you provide one. All limits are mutable in place.
75 * ### Creating a Run Group
76 * **Example:** Basic Run Group
77 * ```typescript
78 * import * as Omics from "@alchemy.run/aws/Omics";
79 *
80 * const group = yield* Omics.RunGroup("Batch");
81 * ```
82 *
83 * **Example:** Run Group with Limits
84 * ```typescript
85 * const group = yield* Omics.RunGroup("Batch", {
86 * name: "nightly-batch",
87 * maxCpus: 100,
88 * maxRuns: 10,
89 * maxDuration: "10 hours",
90 * });
91 * ```
92 *
93 * @resource
94 */
95 export const RunGroup = Resource<RunGroup>("AWS.Omics.RunGroup");
96
97 export const RunGroupProvider = () =>
98 Provider.effect(
99 RunGroup,
100 Effect.gen(function* () {
101 const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) {
102 return props.name ?? (yield* createPhysicalName({ id, maxLength: 96 }));
103 });
104
105 return RunGroup.Provider.of({
106 stables: ["runGroupId", "runGroupArn"],
107 list: () =>
108 omics.listRunGroups.items({}).pipe(
109 Stream.map((item) => ({
110 runGroupId: item.id!,
111 runGroupArn: item.arn!,
112 name: item.name ?? "",
113 })),
114 Stream.runCollect,
115 Effect.map((chunk) => Array.from(chunk)),
116 ),
117 read: Effect.fn(function* ({ id, output }) {
118 if (output?.runGroupId === undefined) return undefined;
119 const found = yield* omics
120 .getRunGroup({ id: output.runGroupId })
121 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
122 if (found === undefined || found.id === undefined) return undefined;
123 const attrs = {
124 runGroupId: found.id,
125 runGroupArn: found.arn!,
126 name: found.name ?? "",
127 };
128 const tags = yield* fetchOmicsTags(found.arn!);
129 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
130 }),
131 // No replacement path: every prop (name + limits) is mutable via
132 // updateRunGroup. Return undefined to run the default update path.
133 diff: Effect.fn(function* ({ news }) {
134 if (!isResolved(news)) return undefined;
135 }),
136 reconcile: Effect.fn(function* ({ id, news, output, session }) {
137 const name = output?.name ?? (yield* createName(id, news));
138 const maxDurationMinutes = toWireMinutes(news.maxDuration);
139 const internalTags = yield* createInternalTags(id);
140 const desiredTags = { ...news.tags, ...internalTags };
141
142 // OBSERVE — the run group id is a server-generated cache.
143 let group =
144 output?.runGroupId === undefined
145 ? undefined
146 : yield* omics
147 .getRunGroup({ id: output.runGroupId })
148 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
149
150 // ENSURE — create if missing. `requestId` is a client idempotency
151 // token; a stable value derived from the logical id keeps a retried
152 // create from provisioning a duplicate group.
153 if (group === undefined || group.id === undefined) {
154 const created = yield* omics.createRunGroup({
155 name,
156 maxCpus: news.maxCpus,
157 maxRuns: news.maxRuns,
158 maxDuration: maxDurationMinutes,
159 maxGpus: news.maxGpus,
160 requestId: `alchemy-${id}`,
161 tags: desiredTags,
162 });
163 group = yield* omics.getRunGroup({ id: created.id! });
164 } else {
165 // SYNC — apply the delta of mutable settings. Observed cloud state
166 // is the diff baseline.
167 const patch: {
168 name?: string;
169 maxCpus?: number;
170 maxRuns?: number;
171 maxDuration?: number;
172 maxGpus?: number;
173 } = {};
174 if (news.name !== undefined && news.name !== group.name) {
175 patch.name = news.name;
176 }
177 if (news.maxCpus !== undefined && news.maxCpus !== group.maxCpus) {
178 patch.maxCpus = news.maxCpus;
179 }
180 if (news.maxRuns !== undefined && news.maxRuns !== group.maxRuns) {
181 patch.maxRuns = news.maxRuns;
182 }
183 if (maxDurationMinutes !== undefined && maxDurationMinutes !== group.maxDuration) {
184 patch.maxDuration = maxDurationMinutes;
185 }
186 if (news.maxGpus !== undefined && news.maxGpus !== group.maxGpus) {
187 patch.maxGpus = news.maxGpus;
188 }
189 if (Object.keys(patch).length > 0) {
190 yield* omics.updateRunGroup({ id: group.id, ...patch });
191 group = yield* omics.getRunGroup({ id: group.id });
192 }
193 }
194
195 // SYNC TAGS — diff against observed cloud tags so adoption converges.
196 yield* syncOmicsTags(group.arn!, desiredTags);
197
198 yield* session.note(group.id!);
199 return {
200 runGroupId: group.id!,
201 runGroupArn: group.arn!,
202 name: group.name ?? name,
203 };
204 }),
205 delete: Effect.fn(function* ({ output }) {
206 yield* omics
207 .deleteRunGroup({ id: output.runGroupId })
208 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
209 }),
210 });
211 }),
212 );