@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 308 lines 10.2 KB
Raw
1 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
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 { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import { GcpEnvironment } from "../Environment.ts";
13 import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
14 import type { Providers } from "../Providers.ts";
15 import {
16 hasAlchemyLabelKeys,
17 isJobTerminal,
18 normalizeLocation,
19 parentOf,
20 parseResourceName,
21 rfc1035,
22 userLabels,
23 waitForOperation,
24 } from "./internal.ts";
25 import { listLocations } from "./names.ts";
26 import type { EncryptionSpec } from "./shared.ts";
27
28 export type CustomJobSpec = aiplatform.GoogleCloudAiplatformV1CustomJobSpec;
29
30 export type CustomJobProps = {
31 /**
32 * Region. Immutable — changing it replaces the job.
33 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
34 */
35 location?: string;
36 /**
37 * User-facing display name (max 128 characters). Defaults to a generated
38 * id. Immutable after create — CustomJob has no update API.
39 */
40 displayName?: string;
41 /**
42 * Worker pools, container/python spec, networking, and scheduling.
43 * Required. Immutable after create.
44 */
45 jobSpec: CustomJobSpec;
46 /**
47 * User labels. Alchemy ownership labels are merged in automatically.
48 * Set at create; CustomJob cannot patch labels.
49 */
50 labels?: Record<string, string>;
51 /**
52 * Customer-managed encryption. Immutable after create.
53 */
54 encryptionSpec?: EncryptionSpec;
55 };
56
57 export type CustomJob = Resource<
58 "GCP.AIPlatform.CustomJob",
59 CustomJobProps,
60 {
61 /** Full resource name `.../customJobs/{custom_job}`. */
62 name: string;
63 /** Custom job id (last path segment). */
64 customJobId: string;
65 /** Project id. */
66 project: string;
67 /** Location id. */
68 location: string;
69 /** Display name. */
70 displayName: string | undefined;
71 /** Job spec as stored by Vertex. */
72 jobSpec: CustomJobSpec | undefined;
73 /** User labels (Alchemy ownership labels stripped). */
74 labels: Record<string, string>;
75 /** Detailed job state. */
76 state: string | undefined;
77 /** Customer-managed KMS key, if any. */
78 kmsKeyName: string | undefined;
79 /** RFC3339 creation timestamp. */
80 createTime: string | undefined;
81 /** RFC3339 start timestamp. */
82 startTime: string | undefined;
83 /** RFC3339 end timestamp. */
84 endTime: string | undefined;
85 /** RFC3339 last-update timestamp. */
86 updateTime: string | undefined;
87 /** Failure/cancel status, if any. */
88 error: { code?: number; message?: string } | undefined;
89 },
90 never,
91 Providers
92 >;
93
94 /**
95 * A Vertex AI CustomJob — a container or Python training workload.
96 *
97 * Creating a CustomJob starts it immediately. There is no update API, so
98 * reconcile is observe-ensure (create if missing). Delete cancels a
99 * running job, then deletes it. Alchemy ownership labels are merged into
100 * `labels` so `list` / nuke can find the job.
101 *
102 * ### Creating a Custom Job
103 * **Example:** Single-worker container job
104 * ```typescript
105 * const job = yield* GCP.AIPlatform.CustomJob("Train", {
106 * displayName: "echo-train",
107 * jobSpec: {
108 * workerPoolSpecs: [
109 * {
110 * machineSpec: { machineType: "n1-standard-4" },
111 * replicaCount: "1",
112 * containerSpec: {
113 * imageUri: "gcr.io/google-samples/hello-app:1.0",
114 * command: ["echo"],
115 * args: ["ok"],
116 * },
117 * },
118 * ],
119 * scheduling: { timeout: "600s" },
120 * },
121 * labels: { env: "test" },
122 * });
123 * ```
124 *
125 * @resource
126 * @category AIPlatform
127 */
128 export const CustomJob = Resource<CustomJob>("GCP.AIPlatform.CustomJob");
129
130 export class CustomJobNotResolved extends Data.TaggedError("GCP.AIPlatform.CustomJobNotResolved")<{
131 resourceName: string;
132 }> {}
133
134 const toId = (id: string, existing?: string) =>
135 Effect.gen(function* () {
136 if (existing !== undefined) return existing;
137 return rfc1035(
138 yield* createPhysicalName({
139 id,
140 maxLength: 63,
141 lowercase: true,
142 }),
143 );
144 });
145
146 const toAttrs = (job: aiplatform.GoogleCloudAiplatformV1CustomJob, project: string) => {
147 const name = job.name ?? "";
148 const parsed = parseResourceName(name, "customJobs");
149 return {
150 name,
151 customJobId: parsed.id,
152 project: parsed.project || project,
153 location: parsed.location,
154 displayName: job.displayName,
155 jobSpec: job.jobSpec,
156 labels: userLabels(job.labels),
157 state: job.state,
158 kmsKeyName: job.encryptionSpec?.kmsKeyName,
159 createTime: job.createTime,
160 startTime: job.startTime,
161 endTime: job.endTime,
162 updateTime: job.updateTime,
163 error: job.error ? { code: job.error.code, message: job.error.message } : undefined,
164 };
165 };
166
167 const getByName = (name: string) =>
168 aiplatform
169 .getProjectsLocationsCustomJobs({ name })
170 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
171
172 const listJobs = (project: string, region: string) => {
173 const collect = (parent: string) =>
174 aiplatform.listProjectsLocationsCustomJobs.pages({ parent, pageSize: 100 }).pipe(
175 Stream.flatMap((page) => Stream.fromIterable(page.customJobs ?? [])),
176 Stream.filter((job) => hasAlchemyLabelKeys(job.labels)),
177 Stream.runCollect,
178 Effect.map((chunk) => Array.from(chunk)),
179 );
180 const fallback = Effect.forEach(listLocations(region), (location) =>
181 collect(`projects/${project}/locations/${location}`),
182 ).pipe(Effect.map((pages) => pages.flat()));
183 // Vertex AI has no `locations/-` wildcard; scan known locations.
184 return fallback.pipe(Effect.catchTag("NotFound", () => Effect.succeed([])));
185 };
186
187 const findOwned = (id: string, project: string, region: string, hinted?: string) =>
188 Effect.gen(function* () {
189 if (hinted !== undefined && hinted.length > 0) {
190 const existing = yield* getByName(hinted);
191 if (existing !== undefined) return existing;
192 }
193 const jobs = yield* listJobs(project, region);
194 for (const job of jobs) {
195 if (yield* hasAlchemyLabels(id, tagRecord(job.labels))) return job;
196 }
197 return undefined as aiplatform.GoogleCloudAiplatformV1CustomJob | undefined;
198 });
199
200 const cancelAndDelete = (name: string) =>
201 Effect.gen(function* () {
202 const existing = yield* getByName(name);
203 if (existing === undefined) return;
204 if (!isJobTerminal(existing.state)) {
205 yield* aiplatform
206 .cancelProjectsLocationsCustomJobs({ name })
207 .pipe(Effect.catchTag("NotFound", () => Effect.void));
208 yield* getByName(name).pipe(
209 Effect.filterOrFail(
210 (job) => job === undefined || isJobTerminal(job.state),
211 () => new CustomJobNotResolved({ resourceName: name }),
212 ),
213 Effect.retry({
214 while: (error) => error._tag === "GCP.AIPlatform.CustomJobNotResolved",
215 times: 8,
216 schedule: Schedule.spaced("4 seconds"),
217 }),
218 Effect.catchTag("GCP.AIPlatform.CustomJobNotResolved", () => Effect.void),
219 );
220 }
221 const operation = yield* aiplatform.deleteProjectsLocationsCustomJobs({ name }).pipe(
222 Effect.retry({
223 while: (error) => error._tag === "Conflict",
224 times: 8,
225 schedule: Schedule.spaced("3 seconds"),
226 }),
227 Effect.catchTags({
228 NotFound: () => Effect.undefined,
229 Conflict: () => Effect.undefined,
230 }),
231 );
232 if (operation !== undefined) {
233 yield* waitForOperation(operation, { notFoundOk: true });
234 }
235 });
236
237 export const CustomJobProvider = () =>
238 Provider.succeed(CustomJob, {
239 stables: ["name", "customJobId", "project", "location", "createTime"],
240
241 diff: Effect.fn(function* ({ news, olds, output }) {
242 const env = yield* GcpEnvironment.current;
243 if (!isResolved(news)) return undefined;
244 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
245 const nextLocation = normalizeLocation(
246 news.location ?? olds?.location ?? output?.location,
247 env.region,
248 );
249 if (previousLocation !== nextLocation) {
250 return { action: "replace" as const, deleteFirst: false };
251 }
252 return undefined;
253 }),
254
255 read: Effect.fn(function* ({ id, output }) {
256 const env = yield* GcpEnvironment.current;
257 const existing = yield* findOwned(id, env.project, env.region, output?.name);
258 if (existing === undefined) return undefined;
259 const attrs = toAttrs(existing, env.project);
260 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
261 }),
262
263 list: () =>
264 Effect.gen(function* () {
265 const env = yield* GcpEnvironment.current;
266 const jobs = yield* listJobs(env.project, env.region);
267 return jobs.map((job) => toAttrs(job, env.project));
268 }),
269
270 reconcile: Effect.fn(function* ({ id, news, output }) {
271 const env = yield* GcpEnvironment.current;
272 const location = normalizeLocation(news.location ?? output?.location, env.region);
273 const displayName = news.displayName ?? (yield* toId(id, output?.customJobId));
274 const desiredLabels = {
275 ...toLabels(news.labels),
276 ...(yield* createInternalLabels(id)),
277 };
278
279 let current = yield* findOwned(id, env.project, env.region, output?.name);
280
281 if (current === undefined) {
282 const created = yield* aiplatform
283 .createProjectsLocationsCustomJobs({
284 parent: parentOf(env.project, location),
285 body: {
286 displayName,
287 jobSpec: news.jobSpec,
288 labels: desiredLabels,
289 encryptionSpec: news.encryptionSpec,
290 },
291 })
292 .pipe(Effect.catchTag("Conflict", () => findOwned(id, env.project, env.region)));
293 current = created ?? undefined;
294 }
295
296 if (current === undefined) {
297 return yield* new CustomJobNotResolved({
298 resourceName: output?.name ?? parentOf(env.project, location),
299 });
300 }
301
302 return toAttrs(current, env.project);
303 }),
304
305 delete: Effect.fn(function* ({ output }) {
306 yield* cancelAndDelete(output.name);
307 }),
308 });