@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 413 lines 15 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 * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import {
13 createInternalLabels,
14 hasAlchemyLabels,
15 stripInternalLabels,
16 toLabels,
17 } from "../Labels.ts";
18 import type { Providers } from "../Providers.ts";
19 import {
20 compact,
21 normalizeLocation,
22 parentOf,
23 parseName,
24 toPhysicalId,
25 listLocations,
26 } from "./names.ts";
27 import { waitForOperation } from "./operations.ts";
28
29 export type TrainingPipelineEncryptionSpec = aiplatform.GoogleCloudAiplatformV1EncryptionSpec;
30 export type TrainingPipelineInputDataConfig = aiplatform.GoogleCloudAiplatformV1InputDataConfig;
31 export type TrainingPipelineModel = aiplatform.GoogleCloudAiplatformV1Model;
32
33 export type TrainingPipelineProps = {
34 /**
35 * Training pipeline id (the `{training_pipeline}` segment of
36 * `projects/{project}/locations/{location}/trainingPipelines/{training_pipeline}`).
37 * Vertex assigns this on create. Pass the value from a previous deploy
38 * to target the same pipeline. Immutable — changing it replaces the
39 * pipeline.
40 */
41 trainingPipelineId?: string;
42 /**
43 * Vertex AI location (`us-central1`, `us-east1`, …). Immutable —
44 * changing it replaces the pipeline. `US-CENTRAL1` is accepted and
45 * normalized to `us-central1`.
46 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
47 */
48 location?: string;
49 /**
50 * User-defined display name. Required by Vertex. If omitted, a unique
51 * name is generated from the stack, stage, and logical id.
52 */
53 displayName?: string;
54 /**
55 * Cloud Storage URI of the training-task YAML
56 * (`gs://google-cloud-aiplatform/schema/trainingjob/definition/…`).
57 * Immutable — changing it replaces the pipeline.
58 */
59 trainingTaskDefinition: string;
60 /**
61 * Training-task inputs as specified by `trainingTaskDefinition`.
62 * Immutable — changing it replaces the pipeline.
63 */
64 trainingTaskInputs?: unknown;
65 /**
66 * Vertex Dataset split and export configuration used by the training
67 * task.
68 */
69 inputDataConfig?: TrainingPipelineInputDataConfig;
70 /**
71 * Model uploaded when the pipeline succeeds.
72 */
73 modelToUpload?: TrainingPipelineModel;
74 /**
75 * Existing model to version instead of uploading a new model.
76 */
77 parentModel?: string;
78 /**
79 * Id of the uploaded model (last path segment). Letters, digits,
80 * underscores, and hyphens; cannot start with a digit or hyphen.
81 */
82 modelId?: string;
83 /**
84 * Customer-managed encryption key for the pipeline (and the uploaded
85 * model when `modelToUpload` is omitted).
86 */
87 encryptionSpec?: TrainingPipelineEncryptionSpec;
88 /**
89 * User labels. Alchemy ownership labels are merged in automatically.
90 */
91 labels?: Record<string, string>;
92 };
93
94 export type TrainingPipeline = Resource<
95 "GCP.AIPlatform.TrainingPipeline",
96 TrainingPipelineProps,
97 {
98 /** Full resource name `projects/{project}/locations/{location}/trainingPipelines/{training_pipeline}`. */
99 name: string;
100 /** Training pipeline id (last path segment). */
101 trainingPipelineId: string;
102 /** Location id (`us-central1`, …). */
103 location: string;
104 /** Project id. */
105 project: string;
106 /** User-defined display name. */
107 displayName: string | undefined;
108 /** Training-task YAML URI. */
109 trainingTaskDefinition: string | undefined;
110 /** Training-task inputs. */
111 trainingTaskInputs: unknown;
112 /** Training-task metadata populated while the pipeline runs. */
113 trainingTaskMetadata: unknown;
114 /** Dataset split and export configuration. */
115 inputDataConfig: TrainingPipelineInputDataConfig | undefined;
116 /** Model uploaded by the pipeline, if any. */
117 modelToUpload: TrainingPipelineModel | undefined;
118 /** Parent model versioned by this pipeline. */
119 parentModel: string | undefined;
120 /** Uploaded model id. */
121 modelId: string | undefined;
122 /** CMEK spec. */
123 encryptionSpec: TrainingPipelineEncryptionSpec | undefined;
124 /** User labels (Alchemy ownership labels stripped). */
125 labels: Record<string, string>;
126 /** Pipeline state (`PIPELINE_STATE_RUNNING`, …). */
127 state: string | undefined;
128 /** Error when the pipeline failed or was cancelled. */
129 error: aiplatform.GoogleRpcStatus | undefined;
130 /** RFC3339 creation timestamp. */
131 createTime: string | undefined;
132 /** RFC3339 start timestamp. */
133 startTime: string | undefined;
134 /** RFC3339 end timestamp. */
135 endTime: string | undefined;
136 /** RFC3339 last-update timestamp. */
137 updateTime: string | undefined;
138 },
139 never,
140 Providers
141 >;
142
143 /**
144 * A Vertex AI TrainingPipeline that runs a training task and optionally
145 * uploads a Model.
146 *
147 * Creating a TrainingPipeline starts it immediately. Vertex assigns the
148 * resource id; there is no update RPC — changing `location` or
149 * `trainingTaskDefinition` replaces the pipeline. Labels brand the
150 * resource for `list` / nuke.
151 *
152 * ### Creating a TrainingPipeline
153 * **Example:** Custom training task
154 * ```typescript
155 * const pipeline = yield* GCP.AIPlatform.TrainingPipeline("Train", {
156 * location: "us-central1",
157 * displayName: "custom-train",
158 * trainingTaskDefinition:
159 * "gs://google-cloud-aiplatform/schema/trainingjob/definition/custom_task_1.0.0.yaml",
160 * trainingTaskInputs: {
161 * workerPoolSpecs: [
162 * {
163 * machineSpec: { machineType: "n1-standard-4" },
164 * replicaCount: "1",
165 * containerSpec: { imageUri: "gcr.io/my-project/trainer:latest" },
166 * },
167 * ],
168 * },
169 * labels: { env: "dev" },
170 * });
171 * ```
172 *
173 * @resource
174 * @category AIPlatform
175 */
176 export const TrainingPipeline = Resource<TrainingPipeline>("GCP.AIPlatform.TrainingPipeline");
177
178 export class TrainingPipelineNotResolved extends Data.TaggedError(
179 "GCP.AIPlatform.TrainingPipelineNotResolved",
180 )<{
181 resourceName: string;
182 }> {}
183
184 export class TrainingPipelineStillExists extends Data.TaggedError(
185 "GCP.AIPlatform.TrainingPipelineStillExists",
186 )<{
187 resourceName: string;
188 }> {}
189
190 const resourceName = (project: string, location: string, trainingPipelineId: string) =>
191 `projects/${project}/locations/${location}/trainingPipelines/${trainingPipelineId}`;
192
193 const userLabels = (
194 labels: Record<string, string | undefined> | null | undefined,
195 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
196
197 const toAttrs = (pipeline: aiplatform.GoogleCloudAiplatformV1TrainingPipeline, project: string) => {
198 const name = pipeline.name ?? "";
199 const parsed = parseName(name, "trainingPipelines");
200 return {
201 name,
202 trainingPipelineId: parsed.resourceId,
203 location: parsed.location,
204 project: parsed.project || project,
205 displayName: pipeline.displayName,
206 trainingTaskDefinition: pipeline.trainingTaskDefinition,
207 trainingTaskInputs: pipeline.trainingTaskInputs,
208 trainingTaskMetadata: pipeline.trainingTaskMetadata,
209 inputDataConfig: pipeline.inputDataConfig,
210 modelToUpload: pipeline.modelToUpload,
211 parentModel: pipeline.parentModel,
212 modelId: pipeline.modelId,
213 encryptionSpec: pipeline.encryptionSpec,
214 labels: userLabels(pipeline.labels),
215 state: pipeline.state,
216 error: pipeline.error,
217 createTime: pipeline.createTime,
218 startTime: pipeline.startTime,
219 endTime: pipeline.endTime,
220 updateTime: pipeline.updateTime,
221 };
222 };
223
224 const getByName = (name: string) =>
225 aiplatform
226 .getProjectsLocationsTrainingPipelines({ name })
227 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
228
229 const listAt = (parent: string) =>
230 aiplatform.listProjectsLocationsTrainingPipelines.pages({ parent, pageSize: 100 }).pipe(
231 Stream.flatMap((page) => Stream.fromIterable(page.trainingPipelines ?? [])),
232 Stream.take(500),
233 Stream.runCollect,
234 Effect.map((chunk) => Array.from(chunk)),
235 Effect.catchTag("NotFound", () => Effect.succeed([])),
236 );
237
238 const findOwned = (id: string, parent: string) =>
239 Effect.gen(function* () {
240 const expected = yield* createInternalLabels(id);
241 const pipelines = yield* listAt(parent);
242 return pipelines.find((pipeline) =>
243 Object.entries(expected).every(([key, value]) => (pipeline.labels ?? {})[key] === value),
244 );
245 });
246
247 const waitUntilGone = (name: string) =>
248 getByName(name).pipe(
249 Effect.flatMap((pipeline) =>
250 pipeline === undefined
251 ? Effect.void
252 : Effect.fail(new TrainingPipelineStillExists({ resourceName: name })),
253 ),
254 Effect.retry({
255 while: (error) => error._tag === "GCP.AIPlatform.TrainingPipelineStillExists",
256 times: 30,
257 schedule: Schedule.spaced("5 seconds"),
258 }),
259 );
260
261 const toBody = (
262 news: TrainingPipelineProps,
263 displayName: string,
264 labels: Record<string, string>,
265 ): aiplatform.GoogleCloudAiplatformV1TrainingPipeline =>
266 compact({
267 displayName,
268 trainingTaskDefinition: news.trainingTaskDefinition,
269 trainingTaskInputs: news.trainingTaskInputs,
270 inputDataConfig: news.inputDataConfig,
271 modelToUpload: news.modelToUpload,
272 parentModel: news.parentModel,
273 modelId: news.modelId,
274 encryptionSpec: news.encryptionSpec,
275 labels,
276 });
277
278 export const TrainingPipelineProvider = () =>
279 Provider.succeed(TrainingPipeline, {
280 stables: ["name", "trainingPipelineId", "location", "project", "createTime"],
281
282 diff: Effect.fn(function* ({ news, olds, output }) {
283 const env = yield* GcpEnvironment.current;
284 if (!isResolved(news)) return undefined;
285 const previousId = olds?.trainingPipelineId ?? output?.trainingPipelineId;
286 const nextId = news.trainingPipelineId ?? previousId;
287 const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId;
288 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
289 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
290 const definitionChanged =
291 (olds?.trainingTaskDefinition ?? output?.trainingTaskDefinition) !== undefined &&
292 news.trainingTaskDefinition !==
293 (olds?.trainingTaskDefinition ?? output?.trainingTaskDefinition);
294 if (idChanged || previousLocation !== nextLocation || definitionChanged) {
295 return { action: "replace" as const, deleteFirst: false };
296 }
297 return undefined;
298 }),
299
300 read: Effect.fn(function* ({ id, olds, output }) {
301 const env = yield* GcpEnvironment.current;
302 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
303 // Pipeline ids are server-assigned numbers, so only a recorded name
304 // or a caller-supplied id can be fetched directly.
305 const trainingPipelineId = olds?.trainingPipelineId ?? output?.trainingPipelineId;
306 const name =
307 output?.name ??
308 (trainingPipelineId !== undefined
309 ? resourceName(env.project, location, trainingPipelineId)
310 : undefined);
311 const existing =
312 (name !== undefined ? yield* getByName(name) : undefined) ??
313 (yield* findOwned(id, parentOf(env.project, location)));
314 if (existing === undefined) return undefined;
315 const attrs = toAttrs(existing, env.project);
316 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
317 }),
318
319 list: () =>
320 Effect.gen(function* () {
321 const env = yield* GcpEnvironment.current;
322 const pipelines = (yield* Effect.forEach(listLocations(env.region), (location) =>
323 listAt(parentOf(env.project, location)),
324 )).flat();
325 return pipelines
326 .filter((pipeline) =>
327 Object.keys(pipeline.labels ?? {}).some((key) => key.startsWith("alchemy-")),
328 )
329 .map((pipeline) => toAttrs(pipeline, env.project));
330 }),
331
332 reconcile: Effect.fn(function* ({ id, news, output }) {
333 const env = yield* GcpEnvironment.current;
334 const location = normalizeLocation(news.location ?? output?.location, env.region);
335 const parent = parentOf(env.project, location);
336 const desiredLabels = {
337 ...toLabels(news.labels),
338 ...(yield* createInternalLabels(id)),
339 };
340 const displayName =
341 news.displayName ??
342 (yield* toPhysicalId(id, news.trainingPipelineId, output?.trainingPipelineId));
343
344 let current =
345 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
346 (yield* findOwned(id, parent));
347
348 if (current === undefined) {
349 const created = yield* aiplatform
350 .createProjectsLocationsTrainingPipelines({
351 parent,
352 body: toBody(news, displayName, desiredLabels),
353 })
354 .pipe(Effect.catchTag("Conflict", () => findOwned(id, parent)));
355 current = created ?? undefined;
356 }
357
358 if (current === undefined) {
359 return yield* new TrainingPipelineNotResolved({
360 resourceName: output?.name ?? parent,
361 });
362 }
363
364 return toAttrs(current, env.project);
365 }),
366
367 delete: Effect.fn(function* ({ output }) {
368 const name = output.name;
369 // Custom training runs in a backing CustomJob that outlives the
370 // pipeline; remember it so it can be deleted too.
371 const metadata = (yield* getByName(name))?.trainingTaskMetadata;
372 const backingJob =
373 metadata !== null && typeof metadata === "object" && "backingCustomJob" in metadata
374 ? (metadata as { backingCustomJob?: unknown }).backingCustomJob
375 : undefined;
376 yield* aiplatform.cancelProjectsLocationsTrainingPipelines({ name, body: {} }).pipe(
377 Effect.catchTags({
378 NotFound: () => Effect.void,
379 BadRequest: () => Effect.void,
380 Conflict: () => Effect.void,
381 }),
382 );
383 // Delete is rejected until the cancellation reaches a terminal state,
384 // which can take a couple of minutes.
385 const operation = yield* aiplatform.deleteProjectsLocationsTrainingPipelines({ name }).pipe(
386 Effect.retry({
387 while: (error) => error._tag === "Conflict" || error._tag === "BadRequest",
388 times: 24,
389 schedule: Schedule.spaced("10 seconds"),
390 }),
391 Effect.catchTag("NotFound", () => Effect.undefined),
392 );
393 if (operation !== undefined) {
394 yield* waitForOperation(operation, { notFoundOk: true });
395 }
396 yield* waitUntilGone(name);
397 if (typeof backingJob === "string" && backingJob.length > 0) {
398 const jobDelete = yield* aiplatform
399 .deleteProjectsLocationsCustomJobs({ name: backingJob })
400 .pipe(
401 Effect.retry({
402 while: (error) => error._tag === "BadRequest",
403 times: 8,
404 schedule: Schedule.spaced("3 seconds"),
405 }),
406 Effect.catchTag("NotFound", () => Effect.undefined),
407 );
408 if (jobDelete !== undefined) {
409 yield* waitForOperation(jobDelete, { notFoundOk: true });
410 }
411 }
412 }),
413 });