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

main
ts 440 lines 15.6 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 { hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import {
15 alchemyIdFilter,
16 createInternalLabels,
17 hasAlchemyPrefix,
18 labelsDiffer,
19 listLocations,
20 locationOf,
21 lastSegment,
22 normalizeLocation,
23 projectOf,
24 stableJson,
25 toDisplayName,
26 toLabels,
27 userLabels,
28 } from "./names.ts";
29 import { waitForOperation } from "./operations.ts";
30
31 export type ModelDeploymentMonitoringJobProps = {
32 /**
33 * Vertex AI location. Immutable — changing it replaces the job.
34 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
35 */
36 location?: string;
37 /**
38 * Display name (max 128 Unicode characters). Generated from the stack,
39 * stage, and logical id when omitted.
40 */
41 displayName?: string;
42 /**
43 * User labels. Alchemy ownership labels are merged in automatically.
44 */
45 labels?: Record<string, string>;
46 /**
47 * Endpoint resource name
48 * `projects/{project}/locations/{location}/endpoints/{endpoint}`.
49 * Immutable.
50 */
51 endpoint: string;
52 /**
53 * Sample strategy for logging prediction requests.
54 */
55 loggingSamplingStrategy: aiplatform.GoogleCloudAiplatformV1SamplingStrategy;
56 /**
57 * Schedule config for running the monitoring job.
58 */
59 modelDeploymentMonitoringScheduleConfig: aiplatform.GoogleCloudAiplatformV1ModelDeploymentMonitoringScheduleConfig;
60 /**
61 * Per-DeployedModel monitoring objectives.
62 */
63 modelDeploymentMonitoringObjectiveConfigs: aiplatform.GoogleCloudAiplatformV1ModelDeploymentMonitoringObjectiveConfigList;
64 /**
65 * Alert config for model monitoring.
66 */
67 modelMonitoringAlertConfig?: aiplatform.GoogleCloudAiplatformV1ModelMonitoringAlertConfig;
68 /**
69 * YAML schema URI describing a single predict instance.
70 */
71 predictInstanceSchemaUri?: string;
72 /**
73 * YAML schema URI describing a TFDV analysis instance.
74 */
75 analysisInstanceSchemaUri?: string;
76 /**
77 * Sample predict instance used instead of `predictInstanceSchemaUri`.
78 */
79 samplePredictInstance?: unknown;
80 /**
81 * GCS folder for stats anomalies.
82 */
83 statsAnomaliesBaseDirectory?: aiplatform.GoogleCloudAiplatformV1GcsDestination;
84 /**
85 * TTL of BigQuery log tables (duration string).
86 */
87 logTtl?: string;
88 /**
89 * Send monitoring pipeline logs to Cloud Logging.
90 * @default false
91 */
92 enableMonitoringPipelineLogs?: boolean;
93 /**
94 * Customer-managed encryption key. Immutable.
95 */
96 encryptionSpec?: aiplatform.GoogleCloudAiplatformV1EncryptionSpec;
97 };
98
99 export type ModelDeploymentMonitoringJob = Resource<
100 "GCP.AIPlatform.ModelDeploymentMonitoringJob",
101 ModelDeploymentMonitoringJobProps,
102 {
103 /** Full resource name. */
104 name: string;
105 /** Job id (last path segment). */
106 modelDeploymentMonitoringJobId: string;
107 /** Project id. */
108 project: string;
109 /** Location id. */
110 location: string;
111 /** Display name. */
112 displayName: string | undefined;
113 /** User labels (Alchemy ownership labels stripped). */
114 labels: Record<string, string>;
115 /** Monitored endpoint resource name. */
116 endpoint: string | undefined;
117 /** Job state. */
118 state: string | undefined;
119 /** Schedule state while running. */
120 scheduleState: string | undefined;
121 /** Whether pipeline logs are enabled. */
122 enableMonitoringPipelineLogs: boolean;
123 /** RFC3339 creation timestamp. */
124 createTime: string | undefined;
125 /** RFC3339 last-update timestamp. */
126 updateTime: string | undefined;
127 /** RFC3339 next schedule time. */
128 nextScheduleTime: string | undefined;
129 },
130 never,
131 Providers
132 >;
133
134 /**
135 * A Vertex AI ModelDeploymentMonitoringJob that periodically scores a
136 * deployed model for skew and drift.
137 *
138 * Changing `location` or `endpoint` replaces the job. Display name,
139 * labels, schedule, sampling, alerts, and objective configs update in
140 * place (long-running). Delete pauses the job first.
141 *
142 * ### Creating a ModelDeploymentMonitoringJob
143 * **Example:** Hourly sampling
144 * ```typescript
145 * const job = yield* GCP.AIPlatform.ModelDeploymentMonitoringJob("Watch", {
146 * endpoint: endpoint.name,
147 * loggingSamplingStrategy: { randomSampleConfig: { sampleRate: 0.1 } },
148 * modelDeploymentMonitoringScheduleConfig: { monitorInterval: "3600s" },
149 * modelDeploymentMonitoringObjectiveConfigs: [{
150 * deployedModelId: deployed.id,
151 * objectiveConfig: {
152 * predictionDriftDetectionConfig: {},
153 * },
154 * }],
155 * });
156 * ```
157 *
158 * @resource
159 * @category AIPlatform
160 */
161 export const ModelDeploymentMonitoringJob = Resource<ModelDeploymentMonitoringJob>(
162 "GCP.AIPlatform.ModelDeploymentMonitoringJob",
163 );
164
165 export class ModelDeploymentMonitoringJobNotResolved extends Data.TaggedError(
166 "GCP.AIPlatform.ModelDeploymentMonitoringJobNotResolved",
167 )<{
168 resourceName: string;
169 }> {}
170
171 export class ModelDeploymentMonitoringJobStillExists extends Data.TaggedError(
172 "GCP.AIPlatform.ModelDeploymentMonitoringJobStillExists",
173 )<{
174 resourceName: string;
175 }> {}
176
177 const toAttrs = (
178 job: aiplatform.GoogleCloudAiplatformV1ModelDeploymentMonitoringJob,
179 project: string,
180 ) => {
181 const name = job.name ?? "";
182 return {
183 name,
184 modelDeploymentMonitoringJobId: lastSegment(name),
185 project: projectOf(name, project),
186 location: locationOf(name),
187 displayName: job.displayName,
188 labels: userLabels(job.labels),
189 endpoint: job.endpoint,
190 state: job.state,
191 scheduleState: job.scheduleState,
192 enableMonitoringPipelineLogs: job.enableMonitoringPipelineLogs === true,
193 createTime: job.createTime,
194 updateTime: job.updateTime,
195 nextScheduleTime: job.nextScheduleTime,
196 };
197 };
198
199 const getByName = (name: string) =>
200 aiplatform
201 .getProjectsLocationsModelDeploymentMonitoringJobs({ name })
202 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
203
204 const listPage = (parent: string, filter?: string) =>
205 aiplatform.listProjectsLocationsModelDeploymentMonitoringJobs
206 .pages({ parent, pageSize: 100, filter })
207 .pipe(
208 Stream.runCollect,
209 Effect.map((pages) =>
210 Array.from(pages).flatMap((page) => page.modelDeploymentMonitoringJobs ?? []),
211 ),
212 Effect.catchTag("NotFound", () =>
213 Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1ModelDeploymentMonitoringJob[]),
214 ),
215 );
216
217 const findOwned = (project: string, location: string, labels: Record<string, string>) =>
218 listPage(`projects/${project}/locations/${location}`, alchemyIdFilter(labels)).pipe(
219 Effect.map((items) => items.find((item) => hasAlchemyPrefix(item.labels)) ?? undefined),
220 );
221
222 const waitUntilExists = (name: string) =>
223 getByName(name).pipe(
224 Effect.filterOrFail(
225 (job): job is aiplatform.GoogleCloudAiplatformV1ModelDeploymentMonitoringJob =>
226 job !== undefined,
227 () => new ModelDeploymentMonitoringJobNotResolved({ resourceName: name }),
228 ),
229 Effect.retry({
230 while: (error) => error._tag === "GCP.AIPlatform.ModelDeploymentMonitoringJobNotResolved",
231 times: 8,
232 schedule: Schedule.spaced("2 seconds"),
233 }),
234 );
235
236 const waitUntilGone = (name: string) =>
237 getByName(name).pipe(
238 Effect.filterOrFail(
239 (job) => job === undefined,
240 () => new ModelDeploymentMonitoringJobStillExists({ resourceName: name }),
241 ),
242 Effect.asVoid,
243 Effect.retry({
244 while: (error) => error._tag === "GCP.AIPlatform.ModelDeploymentMonitoringJobStillExists",
245 times: 10,
246 schedule: Schedule.spaced("2 seconds"),
247 }),
248 );
249
250 export const ModelDeploymentMonitoringJobProvider = () =>
251 Provider.succeed(ModelDeploymentMonitoringJob, {
252 stables: [
253 "name",
254 "modelDeploymentMonitoringJobId",
255 "project",
256 "location",
257 "endpoint",
258 "createTime",
259 ],
260
261 diff: Effect.fn(function* ({ news, olds, output }) {
262 const env = yield* GcpEnvironment.current;
263 if (!isResolved(news)) return undefined;
264 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
265 const nextLocation = normalizeLocation(
266 news.location ?? olds?.location ?? output?.location,
267 env.region,
268 );
269 const previousEndpoint = olds?.endpoint ?? output?.endpoint;
270 const nextEndpoint = news.endpoint ?? previousEndpoint;
271 if (
272 previousLocation !== nextLocation ||
273 (previousEndpoint !== undefined &&
274 nextEndpoint !== undefined &&
275 nextEndpoint !== previousEndpoint)
276 ) {
277 return { action: "replace" as const, deleteFirst: false };
278 }
279 return undefined;
280 }),
281
282 read: Effect.fn(function* ({ id, olds, output }) {
283 const env = yield* GcpEnvironment.current;
284 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
285 const ownership = yield* createInternalLabels(id);
286 const existing =
287 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
288 (yield* findOwned(env.project, location, ownership));
289 if (existing === undefined) return undefined;
290 const attrs = toAttrs(existing, env.project);
291 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
292 }),
293
294 list: () =>
295 Effect.gen(function* () {
296 const env = yield* GcpEnvironment.current;
297 const pages = yield* Effect.forEach(
298 listLocations(env.region),
299 (location) => listPage(`projects/${env.project}/locations/${location}`),
300 { concurrency: 4 },
301 );
302 return pages
303 .flat()
304 .filter((job) => hasAlchemyPrefix(job.labels))
305 .map((job) => toAttrs(job, env.project));
306 }),
307
308 reconcile: Effect.fn(function* ({ id, news, output }) {
309 const env = yield* GcpEnvironment.current;
310 const location = normalizeLocation(news.location ?? output?.location, env.region);
311 const displayName = yield* toDisplayName(id, news.displayName, output?.displayName);
312 const desiredLabels = {
313 ...toLabels(news.labels),
314 ...(yield* createInternalLabels(id)),
315 };
316 const enableLogs = news.enableMonitoringPipelineLogs === true;
317
318 let current =
319 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
320 (yield* findOwned(env.project, location, desiredLabels));
321
322 if (current === undefined) {
323 const created = yield* aiplatform
324 .createProjectsLocationsModelDeploymentMonitoringJobs({
325 parent: `projects/${env.project}/locations/${location}`,
326 body: {
327 displayName,
328 labels: desiredLabels,
329 endpoint: news.endpoint,
330 loggingSamplingStrategy: news.loggingSamplingStrategy,
331 modelDeploymentMonitoringScheduleConfig: news.modelDeploymentMonitoringScheduleConfig,
332 modelDeploymentMonitoringObjectiveConfigs:
333 news.modelDeploymentMonitoringObjectiveConfigs,
334 modelMonitoringAlertConfig: news.modelMonitoringAlertConfig,
335 predictInstanceSchemaUri: news.predictInstanceSchemaUri,
336 analysisInstanceSchemaUri: news.analysisInstanceSchemaUri,
337 samplePredictInstance: news.samplePredictInstance,
338 statsAnomaliesBaseDirectory: news.statsAnomaliesBaseDirectory,
339 logTtl: news.logTtl,
340 enableMonitoringPipelineLogs: enableLogs,
341 encryptionSpec: news.encryptionSpec,
342 },
343 })
344 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
345 current = created ?? (yield* findOwned(env.project, location, desiredLabels));
346 }
347
348 if (current === undefined || current.name === undefined) {
349 return yield* new ModelDeploymentMonitoringJobNotResolved({
350 resourceName: output?.name ?? `${location}/modelDeploymentMonitoringJobs`,
351 });
352 }
353
354 const name = current.name;
355 const displayChanged = (current.displayName ?? "") !== displayName;
356 const labelsChanged = labelsDiffer(current.labels, desiredLabels);
357 const logsChanged = (current.enableMonitoringPipelineLogs === true) !== enableLogs;
358 const samplingChanged =
359 stableJson(current.loggingSamplingStrategy) !== stableJson(news.loggingSamplingStrategy);
360 const scheduleChanged =
361 stableJson(current.modelDeploymentMonitoringScheduleConfig) !==
362 stableJson(news.modelDeploymentMonitoringScheduleConfig);
363 const objectivesChanged =
364 stableJson(current.modelDeploymentMonitoringObjectiveConfigs) !==
365 stableJson(news.modelDeploymentMonitoringObjectiveConfigs);
366 const alertChanged =
367 news.modelMonitoringAlertConfig !== undefined &&
368 stableJson(current.modelMonitoringAlertConfig) !==
369 stableJson(news.modelMonitoringAlertConfig);
370 const logTtlChanged = (current.logTtl ?? "") !== (news.logTtl ?? "");
371
372 if (
373 displayChanged ||
374 labelsChanged ||
375 logsChanged ||
376 samplingChanged ||
377 scheduleChanged ||
378 objectivesChanged ||
379 alertChanged ||
380 logTtlChanged
381 ) {
382 const operation = yield* aiplatform.patchProjectsLocationsModelDeploymentMonitoringJobs({
383 name,
384 updateMask: [
385 displayChanged ? "display_name" : undefined,
386 labelsChanged ? "labels" : undefined,
387 logsChanged ? "enable_monitoring_pipeline_logs" : undefined,
388 samplingChanged ? "logging_sampling_strategy" : undefined,
389 scheduleChanged ? "model_deployment_monitoring_schedule_config" : undefined,
390 objectivesChanged ? "model_deployment_monitoring_objective_configs" : undefined,
391 alertChanged ? "model_monitoring_alert_config" : undefined,
392 logTtlChanged ? "log_ttl" : undefined,
393 ]
394 .filter((field): field is string => field !== undefined)
395 .join(","),
396 body: {
397 name,
398 displayName,
399 labels: desiredLabels,
400 enableMonitoringPipelineLogs: enableLogs,
401 loggingSamplingStrategy: news.loggingSamplingStrategy,
402 modelDeploymentMonitoringScheduleConfig: news.modelDeploymentMonitoringScheduleConfig,
403 modelDeploymentMonitoringObjectiveConfigs:
404 news.modelDeploymentMonitoringObjectiveConfigs,
405 modelMonitoringAlertConfig: news.modelMonitoringAlertConfig,
406 logTtl: news.logTtl,
407 },
408 });
409 yield* waitForOperation(operation);
410 current = yield* waitUntilExists(name);
411 }
412
413 return toAttrs(current, env.project);
414 }),
415
416 delete: Effect.fn(function* ({ output }) {
417 yield* aiplatform
418 .pauseProjectsLocationsModelDeploymentMonitoringJobs({
419 name: output.name,
420 body: {},
421 })
422 .pipe(Effect.catchTag(["NotFound", "BadRequest", "Conflict"], () => Effect.void));
423 const operation = yield* aiplatform
424 .deleteProjectsLocationsModelDeploymentMonitoringJobs({
425 name: output.name,
426 })
427 .pipe(
428 Effect.retry({
429 while: (error) => error._tag === "Conflict",
430 times: 8,
431 schedule: Schedule.spaced("2 seconds"),
432 }),
433 Effect.catchTag("NotFound", () => Effect.undefined),
434 );
435 if (operation !== undefined) {
436 yield* waitForOperation(operation, { notFoundOk: true });
437 }
438 yield* waitUntilGone(output.name);
439 }),
440 });