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

main
ts 454 lines 16 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 * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import { listLocations } from "./names.ts";
13 import { waitForOperation } from "./operations.ts";
14 import {
15 encodeOwnershipLine,
16 hasOwnershipMarker,
17 lastSegment,
18 locationOf,
19 locationParent,
20 ownedByAlchemy,
21 parseOwnership,
22 } from "./ownership.ts";
23
24 export type PipelineJobRequest = {
25 /**
26 * Location parent for created pipeline jobs
27 * (`projects/{project}/locations/{location}`). Defaults to the
28 * Schedule's location.
29 */
30 parent?: string;
31 /** Optional pipeline job id. */
32 pipelineJobId?: string;
33 /** Pipeline job to create on each run. */
34 pipelineJob?: {
35 displayName?: string;
36 templateUri?: string;
37 pipelineSpec?: Record<string, unknown>;
38 serviceAccount?: string;
39 network?: string;
40 labels?: Record<string, string>;
41 runtimeConfig?: {
42 gcsOutputDirectory?: string;
43 parameterValues?: Record<string, unknown>;
44 };
45 };
46 };
47
48 export type NotebookExecutionJobRequest = {
49 parent?: string;
50 notebookExecutionJobId?: string;
51 notebookExecutionJob?: Record<string, unknown>;
52 };
53
54 export type ScheduleProps = {
55 /**
56 * Region (`us-central1`, …). Immutable — changing it replaces the
57 * Schedule.
58 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
59 */
60 location?: string;
61 /**
62 * User-facing display name. Vertex AI Schedules have no labels field,
63 * so Alchemy ownership is stored in a `[alchemy …]` prefix.
64 */
65 displayName?: string;
66 /**
67 * Cron schedule. Prefix with `CRON_TZ=${IANA_TIME_ZONE}` to set a
68 * timezone (for example `CRON_TZ=UTC 0 0 * * *`).
69 */
70 cron: string;
71 /**
72 * Maximum number of runs that can be started concurrently.
73 * @default "1"
74 */
75 maxConcurrentRunCount?: string;
76 /**
77 * Maximum number of active (non-terminal) pipeline runs. Only applies
78 * to `createPipelineJobRequest`.
79 */
80 maxConcurrentActiveRunCount?: string;
81 /**
82 * Maximum number of runs before the schedule completes.
83 */
84 maxRunCount?: string;
85 /**
86 * RFC3339 timestamp after which the first run may be scheduled.
87 */
88 startTime?: string;
89 /**
90 * RFC3339 timestamp after which no new runs are scheduled.
91 */
92 endTime?: string;
93 /**
94 * Queue new runs when `maxConcurrentRunCount` is reached instead of
95 * skipping them.
96 * @default false
97 */
98 allowQueueing?: boolean;
99 /**
100 * Pipeline job created on each tick. Exactly one of
101 * `createPipelineJobRequest` or `createNotebookExecutionJobRequest`
102 * must be set.
103 */
104 createPipelineJobRequest?: PipelineJobRequest;
105 /**
106 * Notebook execution job created on each tick.
107 */
108 createNotebookExecutionJobRequest?: NotebookExecutionJobRequest;
109 /**
110 * When true, the schedule exists but does not start new runs (`PAUSED`).
111 * Synced via `schedules.pause` / `schedules.resume`.
112 * @default false
113 */
114 paused?: boolean;
115 };
116
117 export type Schedule = Resource<
118 "GCP.AIPlatform.Schedule",
119 ScheduleProps,
120 {
121 /** Full resource name `projects/{project}/locations/{location}/schedules/{schedule}`. */
122 name: string;
123 /** Schedule id (last path segment). */
124 scheduleId: string;
125 /** Region id. */
126 location: string;
127 /** Project id. */
128 project: string;
129 /** User display name with the Alchemy ownership prefix stripped. */
130 displayName: string | undefined;
131 /** Cron expression. */
132 cron: string | undefined;
133 /** Server-reported state. */
134 state: string | undefined;
135 /** Whether the schedule is paused. */
136 paused: boolean;
137 /** Maximum concurrent starts. */
138 maxConcurrentRunCount: string | undefined;
139 /** Maximum concurrent active pipeline runs. */
140 maxConcurrentActiveRunCount: string | undefined;
141 /** Maximum run count. */
142 maxRunCount: string | undefined;
143 /** RFC3339 start bound. */
144 startTime: string | undefined;
145 /** RFC3339 end bound. */
146 endTime: string | undefined;
147 /** Whether new runs may queue. */
148 allowQueueing: boolean;
149 /** Number of runs started so far. */
150 startedRunCount: string | undefined;
151 /** Next scheduled run (RFC3339). */
152 nextRunTime: string | undefined;
153 /** RFC3339 creation timestamp. */
154 createTime: string | undefined;
155 /** RFC3339 last-update timestamp. */
156 updateTime: string | undefined;
157 },
158 never,
159 Providers
160 >;
161
162 /**
163 * A Vertex AI Schedule that periodically starts pipeline or notebook jobs.
164 *
165 * Schedules have no labels field — Alchemy stamps ownership into the
166 * display name. Location is immutable. Cron, run limits, and pause state
167 * update in place.
168 *
169 * ### Creating a Schedule
170 * **Example:** Daily paused pipeline schedule
171 * ```typescript
172 * const schedule = yield* GCP.AIPlatform.Schedule("Nightly", {
173 * cron: "CRON_TZ=UTC 0 8 * * *",
174 * paused: true,
175 * maxRunCount: "1",
176 * createPipelineJobRequest: {
177 * pipelineJob: {
178 * displayName: "nightly",
179 * templateUri: "https://us-kfp.pkg.dev/ml-pipeline/google-cloud-registry/hello-world/latest",
180 * },
181 * },
182 * });
183 * ```
184 *
185 * @resource
186 * @category AIPlatform
187 */
188 export const Schedule = Resource<Schedule>("GCP.AIPlatform.Schedule");
189
190 export class ScheduleNotResolved extends Data.TaggedError("GCP.AIPlatform.ScheduleNotResolved")<{
191 resourceName: string;
192 }> {}
193
194 const toAttrs = (schedule: aiplatform.GoogleCloudAiplatformV1Schedule, project: string) => {
195 const name = schedule.name ?? "";
196 const parsed = parseOwnership(schedule.displayName);
197 return {
198 name,
199 scheduleId: lastSegment(name),
200 location: locationOf(name),
201 project,
202 displayName: parsed.text,
203 cron: schedule.cron,
204 state: schedule.state,
205 paused: schedule.state === "PAUSED",
206 maxConcurrentRunCount: schedule.maxConcurrentRunCount,
207 maxConcurrentActiveRunCount: schedule.maxConcurrentActiveRunCount,
208 maxRunCount: schedule.maxRunCount,
209 startTime: schedule.startTime,
210 endTime: schedule.endTime,
211 allowQueueing: schedule.allowQueueing === true,
212 startedRunCount: schedule.startedRunCount,
213 nextRunTime: schedule.nextRunTime,
214 createTime: schedule.createTime,
215 updateTime: schedule.updateTime,
216 };
217 };
218
219 const getByName = (name: string) =>
220 name.length === 0
221 ? Effect.undefined
222 : aiplatform
223 .getProjectsLocationsSchedules({ name })
224 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
225
226 const listAt = (parent: string, project: string) =>
227 aiplatform.listProjectsLocationsSchedules.pages({ parent, pageSize: 100 }).pipe(
228 Stream.flatMap((page) => Stream.fromIterable(page.schedules ?? [])),
229 Stream.filter((schedule) => hasOwnershipMarker(schedule.displayName)),
230 Stream.map((schedule) => toAttrs(schedule, project)),
231 Stream.runCollect,
232 Effect.map((chunk) => Array.from(chunk)),
233 Effect.catchTag("NotFound", () => Effect.succeed([])),
234 );
235
236 const toPipelineRequest = (
237 request: PipelineJobRequest | undefined,
238 parent: string,
239 ): aiplatform.GoogleCloudAiplatformV1CreatePipelineJobRequest | undefined => {
240 if (request === undefined) return undefined;
241 const job = request.pipelineJob;
242 return {
243 parent: request.parent ?? parent,
244 pipelineJobId: request.pipelineJobId,
245 pipelineJob: job
246 ? {
247 displayName: job.displayName,
248 templateUri: job.templateUri,
249 pipelineSpec: job.pipelineSpec,
250 serviceAccount: job.serviceAccount,
251 network: job.network,
252 labels: job.labels,
253 runtimeConfig: job.runtimeConfig,
254 }
255 : undefined,
256 };
257 };
258
259 const toNotebookRequest = (
260 request: NotebookExecutionJobRequest | undefined,
261 ): aiplatform.GoogleCloudAiplatformV1CreateNotebookExecutionJobRequest | undefined => {
262 if (request === undefined) return undefined;
263 return {
264 parent: request.parent,
265 notebookExecutionJobId: request.notebookExecutionJobId,
266 notebookExecutionJob:
267 request.notebookExecutionJob as aiplatform.GoogleCloudAiplatformV1NotebookExecutionJob,
268 };
269 };
270
271 export const ScheduleProvider = () =>
272 Provider.succeed(Schedule, {
273 stables: ["name", "scheduleId", "location", "project", "createTime"],
274
275 diff: Effect.fn(function* ({ news, olds, output }) {
276 if (!isResolved(news)) return undefined;
277 const previousLocation = olds?.location ?? output?.location;
278 const nextLocation = news.location ?? previousLocation;
279 if (previousLocation !== undefined && previousLocation !== nextLocation) {
280 return { action: "replace" as const, deleteFirst: false };
281 }
282 return undefined;
283 }),
284
285 read: Effect.fn(function* ({ id, olds, output }) {
286 const env = yield* GcpEnvironment.current;
287 const existing = yield* getByName(output?.name ?? "");
288 if (existing !== undefined) {
289 const attrs = toAttrs(existing, env.project);
290 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
291 }
292 const location = olds?.location ?? output?.location ?? env.region;
293 const parent = locationParent(env.project, location);
294 const ownership = yield* createInternalLabels(id);
295 const displayName = encodeOwnershipLine(ownership, olds?.displayName);
296 const found = yield* aiplatform.listProjectsLocationsSchedules
297 .pages({ parent, pageSize: 100 })
298 .pipe(
299 Stream.flatMap((page) => Stream.fromIterable(page.schedules ?? [])),
300 Stream.filter((schedule) => schedule.displayName === displayName),
301 Stream.runHead,
302 Effect.map((option) => (option._tag === "Some" ? option.value : undefined)),
303 Effect.catchTag("NotFound", () => Effect.undefined),
304 );
305 if (found === undefined) return undefined;
306 const attrs = toAttrs(found, env.project);
307 return (yield* ownedByAlchemy(id, found.displayName)) ? attrs : Unowned(attrs);
308 }),
309
310 list: () =>
311 Effect.gen(function* () {
312 const env = yield* GcpEnvironment.current;
313 return (yield* Effect.forEach(listLocations(env.region), (location) =>
314 listAt(locationParent(env.project, location), env.project),
315 )).flat();
316 }),
317
318 reconcile: Effect.fn(function* ({ id, news, output }) {
319 const env = yield* GcpEnvironment.current;
320 const location = news.location ?? output?.location ?? env.region;
321 const parent = locationParent(env.project, location);
322 const ownership = yield* createInternalLabels(id);
323 const displayName = encodeOwnershipLine(ownership, news.displayName);
324 const maxConcurrentRunCount = news.maxConcurrentRunCount ?? "1";
325 const desiredPaused = news.paused === true;
326 const pipelineRequest = toPipelineRequest(news.createPipelineJobRequest, parent);
327 const notebookRequest = toNotebookRequest(news.createNotebookExecutionJobRequest);
328
329 let current = yield* getByName(output?.name ?? "");
330 if (current === undefined) {
331 current = yield* aiplatform.listProjectsLocationsSchedules
332 .pages({ parent, pageSize: 100 })
333 .pipe(
334 Stream.flatMap((page) => Stream.fromIterable(page.schedules ?? [])),
335 Stream.filter((schedule) => schedule.displayName === displayName),
336 Stream.runHead,
337 Effect.map((option) => (option._tag === "Some" ? option.value : undefined)),
338 Effect.catchTag("NotFound", () => Effect.undefined),
339 );
340 }
341
342 if (current === undefined) {
343 const created = yield* aiplatform
344 .createProjectsLocationsSchedules({
345 parent,
346 body: {
347 displayName,
348 cron: news.cron,
349 maxConcurrentRunCount,
350 maxConcurrentActiveRunCount: news.maxConcurrentActiveRunCount,
351 maxRunCount: news.maxRunCount,
352 startTime: news.startTime,
353 endTime: news.endTime,
354 allowQueueing: news.allowQueueing === true ? true : undefined,
355 createPipelineJobRequest: pipelineRequest,
356 createNotebookExecutionJobRequest: notebookRequest,
357 },
358 })
359 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
360 current = created ?? undefined;
361 if (current === undefined) {
362 current = yield* aiplatform.listProjectsLocationsSchedules
363 .pages({ parent, pageSize: 100 })
364 .pipe(
365 Stream.flatMap((page) => Stream.fromIterable(page.schedules ?? [])),
366 Stream.filter((schedule) => schedule.displayName === displayName),
367 Stream.runHead,
368 Effect.map((option) => (option._tag === "Some" ? option.value : undefined)),
369 Effect.catchTag("NotFound", () => Effect.undefined),
370 );
371 }
372 }
373
374 if (current === undefined) {
375 return yield* new ScheduleNotResolved({
376 resourceName: output?.name ?? `${parent}/schedules/-`,
377 });
378 }
379
380 const name = current.name ?? "";
381 const cronChanged = (current.cron ?? "") !== news.cron;
382 const displayChanged = (current.displayName ?? "") !== displayName;
383 const concurrentChanged = (current.maxConcurrentRunCount ?? "") !== maxConcurrentRunCount;
384 const activeChanged =
385 (current.maxConcurrentActiveRunCount ?? "") !== (news.maxConcurrentActiveRunCount ?? "");
386 const maxRunChanged = (current.maxRunCount ?? "") !== (news.maxRunCount ?? "");
387 const startChanged = (current.startTime ?? "") !== (news.startTime ?? "");
388 const endChanged = (current.endTime ?? "") !== (news.endTime ?? "");
389 const queueChanged = (current.allowQueueing === true) !== (news.allowQueueing === true);
390
391 if (
392 cronChanged ||
393 displayChanged ||
394 concurrentChanged ||
395 activeChanged ||
396 maxRunChanged ||
397 startChanged ||
398 endChanged ||
399 queueChanged
400 ) {
401 current = yield* aiplatform.patchProjectsLocationsSchedules({
402 name,
403 updateMask: [
404 cronChanged ? "cron" : undefined,
405 displayChanged ? "display_name" : undefined,
406 concurrentChanged ? "max_concurrent_run_count" : undefined,
407 activeChanged ? "max_concurrent_active_run_count" : undefined,
408 maxRunChanged ? "max_run_count" : undefined,
409 startChanged ? "start_time" : undefined,
410 endChanged ? "end_time" : undefined,
411 queueChanged ? "allow_queueing" : undefined,
412 ]
413 .filter((field): field is string => field !== undefined)
414 .join(","),
415 body: {
416 name,
417 displayName,
418 cron: news.cron,
419 maxConcurrentRunCount,
420 maxConcurrentActiveRunCount: news.maxConcurrentActiveRunCount,
421 maxRunCount: news.maxRunCount,
422 startTime: news.startTime,
423 endTime: news.endTime,
424 allowQueueing: news.allowQueueing === true,
425 },
426 });
427 }
428
429 const isPaused = current.state === "PAUSED";
430 if (desiredPaused && !isPaused) {
431 yield* aiplatform.pauseProjectsLocationsSchedules({ name });
432 current = (yield* getByName(name)) ?? current;
433 } else if (!desiredPaused && isPaused) {
434 yield* aiplatform.resumeProjectsLocationsSchedules({
435 name,
436 body: { catchUp: false },
437 });
438 current = (yield* getByName(name)) ?? current;
439 }
440
441 return toAttrs(current, env.project);
442 }),
443
444 delete: Effect.fn(function* ({ output }) {
445 const existing = yield* getByName(output.name);
446 if (existing === undefined) return;
447 const operation = yield* aiplatform
448 .deleteProjectsLocationsSchedules({ name: output.name })
449 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
450 if (operation !== undefined) {
451 yield* waitForOperation(operation, { notFoundOk: true });
452 }
453 }),
454 });