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

main
ts 397 lines 13 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 GcsSource = {
29 /** GCS URIs of input files. Wildcards allowed. */
30 uris?: string[];
31 };
32
33 export type GcsDestination = {
34 /** GCS URI prefix of the output directory. */
35 outputUriPrefix?: string;
36 };
37
38 export type BigQuerySource = {
39 /** BigQuery table URI, for example `bq://project.dataset.table`. */
40 inputUri?: string;
41 };
42
43 export type BigQueryDestination = {
44 /** BigQuery project, dataset, or table URI (`bq://...`). */
45 outputUri?: string;
46 };
47
48 export type BatchPredictionInputConfig = {
49 /** Instance format; must be one of the Model's supported input formats. */
50 instancesFormat?: string;
51 /** GCS input. */
52 gcsSource?: GcsSource;
53 /** BigQuery input. */
54 bigquerySource?: BigQuerySource;
55 /** Vertex multimodal dataset input. */
56 vertexMultimodalDatasetSource?: { datasetName?: string };
57 };
58
59 export type BatchPredictionOutputConfig = {
60 /** Predictions format; must be one of the Model's supported output formats. */
61 predictionsFormat?: string;
62 /** GCS output directory. */
63 gcsDestination?: GcsDestination;
64 /** BigQuery output. */
65 bigqueryDestination?: BigQueryDestination;
66 };
67
68 export type BatchDedicatedResources = {
69 /** Max machine replicas. Defaults to 10. */
70 maxReplicaCount?: number;
71 /** Starting replica count. */
72 startingReplicaCount?: number;
73 /** Single-machine spec. */
74 machineSpec?: {
75 machineType?: string;
76 acceleratorType?: string;
77 acceleratorCount?: number;
78 };
79 };
80
81 export type BatchPredictionJobProps = {
82 /**
83 * Region. Immutable — changing it replaces the job.
84 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
85 */
86 location?: string;
87 /**
88 * User-facing display name. Defaults to a generated id. Immutable after
89 * create — BatchPredictionJob has no update API.
90 */
91 displayName?: string;
92 /**
93 * Model resource that produces predictions
94 * (`projects/{project}/locations/{location}/models/{model}` or a
95 * publisher model). Exactly one of `model` or `endpoint` should be set.
96 */
97 model?: string;
98 /**
99 * Input instances. Required.
100 */
101 inputConfig: BatchPredictionInputConfig;
102 /**
103 * Output predictions location. Required.
104 */
105 outputConfig: BatchPredictionOutputConfig;
106 /**
107 * Dedicated machine resources for the prediction workers.
108 */
109 dedicatedResources?: BatchDedicatedResources;
110 /**
111 * Model parameters forwarded to the predictor.
112 */
113 modelParameters?: unknown;
114 /**
115 * Generate explanations with the predictions.
116 */
117 generateExplanation?: boolean;
118 /**
119 * Disable container logging.
120 */
121 disableContainerLogging?: boolean;
122 /**
123 * Runtime service account email.
124 */
125 serviceAccount?: string;
126 /**
127 * User labels. Alchemy ownership labels are merged in automatically.
128 */
129 labels?: Record<string, string>;
130 /**
131 * Customer-managed encryption.
132 */
133 encryptionSpec?: EncryptionSpec;
134 };
135
136 export type BatchPredictionJob = Resource<
137 "GCP.AIPlatform.BatchPredictionJob",
138 BatchPredictionJobProps,
139 {
140 /** Full resource name `.../batchPredictionJobs/{job}`. */
141 name: string;
142 /** Job id (last path segment). */
143 batchPredictionJobId: string;
144 /** Project id. */
145 project: string;
146 /** Location id. */
147 location: string;
148 /** Display name. */
149 displayName: string | undefined;
150 /** Model resource name. */
151 model: string | undefined;
152 /** Model version id. */
153 modelVersionId: string | undefined;
154 /** Detailed job state. */
155 state: string | undefined;
156 /** User labels (Alchemy ownership labels stripped). */
157 labels: Record<string, string>;
158 /** GCS output directory, if any. */
159 gcsOutputDirectory: string | undefined;
160 /** BigQuery output dataset, if any. */
161 bigqueryOutputDataset: string | undefined;
162 /** Customer-managed KMS key, if any. */
163 kmsKeyName: string | undefined;
164 /** RFC3339 creation timestamp. */
165 createTime: string | undefined;
166 /** RFC3339 start timestamp. */
167 startTime: string | undefined;
168 /** RFC3339 end timestamp. */
169 endTime: string | undefined;
170 /** Failure/cancel status, if any. */
171 error: { code?: number; message?: string } | undefined;
172 },
173 never,
174 Providers
175 >;
176
177 /**
178 * A Vertex AI BatchPredictionJob — offline predictions over many
179 * instances.
180 *
181 * Creating a job starts it immediately. There is no update API, so
182 * reconcile is observe-ensure. Delete can only run after the job
183 * finishes; Alchemy cancels first. Alchemy ownership labels are merged
184 * into `labels` so `list` / nuke can find the job.
185 *
186 * ### Creating a Batch Prediction Job
187 * **Example:** JSONL in, JSONL out
188 * ```typescript
189 * const job = yield* GCP.AIPlatform.BatchPredictionJob("Nightly", {
190 * displayName: "nightly-scores",
191 * model: "projects/my-project/locations/us-central1/models/my-model",
192 * inputConfig: {
193 * instancesFormat: "jsonl",
194 * gcsSource: { uris: ["gs://bucket/input.jsonl"] },
195 * },
196 * outputConfig: {
197 * predictionsFormat: "jsonl",
198 * gcsDestination: { outputUriPrefix: "gs://bucket/out/" },
199 * },
200 * });
201 * ```
202 *
203 * @resource
204 * @category AIPlatform
205 */
206 export const BatchPredictionJob = Resource<BatchPredictionJob>("GCP.AIPlatform.BatchPredictionJob");
207
208 export class BatchPredictionJobNotResolved extends Data.TaggedError(
209 "GCP.AIPlatform.BatchPredictionJobNotResolved",
210 )<{
211 resourceName: string;
212 }> {}
213
214 const toId = (id: string, existing?: string) =>
215 Effect.gen(function* () {
216 if (existing !== undefined) return existing;
217 return rfc1035(
218 yield* createPhysicalName({
219 id,
220 maxLength: 63,
221 lowercase: true,
222 }),
223 );
224 });
225
226 const toAttrs = (job: aiplatform.GoogleCloudAiplatformV1BatchPredictionJob, project: string) => {
227 const name = job.name ?? "";
228 const parsed = parseResourceName(name, "batchPredictionJobs");
229 return {
230 name,
231 batchPredictionJobId: parsed.id,
232 project: parsed.project || project,
233 location: parsed.location,
234 displayName: job.displayName,
235 model: job.model,
236 modelVersionId: job.modelVersionId,
237 state: job.state,
238 labels: userLabels(job.labels),
239 gcsOutputDirectory: job.outputInfo?.gcsOutputDirectory,
240 bigqueryOutputDataset: job.outputInfo?.bigqueryOutputDataset,
241 kmsKeyName: job.encryptionSpec?.kmsKeyName,
242 createTime: job.createTime,
243 startTime: job.startTime,
244 endTime: job.endTime,
245 error: job.error ? { code: job.error.code, message: job.error.message } : undefined,
246 };
247 };
248
249 const getByName = (name: string) =>
250 aiplatform
251 .getProjectsLocationsBatchPredictionJobs({ name })
252 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
253
254 const listJobs = (project: string, region: string) => {
255 const collect = (parent: string) =>
256 aiplatform.listProjectsLocationsBatchPredictionJobs.pages({ parent, pageSize: 100 }).pipe(
257 Stream.flatMap((page) => Stream.fromIterable(page.batchPredictionJobs ?? [])),
258 Stream.filter((job) => hasAlchemyLabelKeys(job.labels)),
259 Stream.runCollect,
260 Effect.map((chunk) => Array.from(chunk)),
261 );
262 const fallback = Effect.forEach(listLocations(region), (location) =>
263 collect(`projects/${project}/locations/${location}`),
264 ).pipe(Effect.map((pages) => pages.flat()));
265 // Vertex AI has no `locations/-` wildcard; scan known locations.
266 return fallback.pipe(Effect.catchTag("NotFound", () => Effect.succeed([])));
267 };
268
269 const findOwned = (id: string, project: string, region: string, hinted?: string) =>
270 Effect.gen(function* () {
271 if (hinted !== undefined && hinted.length > 0) {
272 const existing = yield* getByName(hinted);
273 if (existing !== undefined) return existing;
274 }
275 const jobs = yield* listJobs(project, region);
276 for (const job of jobs) {
277 if (yield* hasAlchemyLabels(id, tagRecord(job.labels))) return job;
278 }
279 return undefined as aiplatform.GoogleCloudAiplatformV1BatchPredictionJob | undefined;
280 });
281
282 const cancelAndDelete = (name: string) =>
283 Effect.gen(function* () {
284 const existing = yield* getByName(name);
285 if (existing === undefined) return;
286 if (!isJobTerminal(existing.state)) {
287 yield* aiplatform
288 .cancelProjectsLocationsBatchPredictionJobs({ name })
289 .pipe(Effect.catchTag("NotFound", () => Effect.void));
290 yield* getByName(name).pipe(
291 Effect.filterOrFail(
292 (job) => job === undefined || isJobTerminal(job.state),
293 () => new BatchPredictionJobNotResolved({ resourceName: name }),
294 ),
295 Effect.retry({
296 while: (error) => error._tag === "GCP.AIPlatform.BatchPredictionJobNotResolved",
297 times: 8,
298 schedule: Schedule.spaced("4 seconds"),
299 }),
300 Effect.catchTag("GCP.AIPlatform.BatchPredictionJobNotResolved", () => Effect.void),
301 );
302 }
303 const operation = yield* aiplatform.deleteProjectsLocationsBatchPredictionJobs({ name }).pipe(
304 Effect.retry({
305 while: (error) => error._tag === "Conflict",
306 times: 8,
307 schedule: Schedule.spaced("3 seconds"),
308 }),
309 Effect.catchTags({
310 NotFound: () => Effect.undefined,
311 Conflict: () => Effect.undefined,
312 }),
313 );
314 if (operation !== undefined) {
315 yield* waitForOperation(operation, { notFoundOk: true });
316 }
317 });
318
319 export const BatchPredictionJobProvider = () =>
320 Provider.succeed(BatchPredictionJob, {
321 stables: ["name", "batchPredictionJobId", "project", "location", "createTime"],
322
323 diff: Effect.fn(function* ({ news, olds, output }) {
324 const env = yield* GcpEnvironment.current;
325 if (!isResolved(news)) return undefined;
326 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
327 const nextLocation = normalizeLocation(
328 news.location ?? olds?.location ?? output?.location,
329 env.region,
330 );
331 if (previousLocation !== nextLocation) {
332 return { action: "replace" as const, deleteFirst: false };
333 }
334 return undefined;
335 }),
336
337 read: Effect.fn(function* ({ id, output }) {
338 const env = yield* GcpEnvironment.current;
339 const existing = yield* findOwned(id, env.project, env.region, output?.name);
340 if (existing === undefined) return undefined;
341 const attrs = toAttrs(existing, env.project);
342 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
343 }),
344
345 list: () =>
346 Effect.gen(function* () {
347 const env = yield* GcpEnvironment.current;
348 const jobs = yield* listJobs(env.project, env.region);
349 return jobs.map((job) => toAttrs(job, env.project));
350 }),
351
352 reconcile: Effect.fn(function* ({ id, news, output }) {
353 const env = yield* GcpEnvironment.current;
354 const location = normalizeLocation(news.location ?? output?.location, env.region);
355 const displayName = news.displayName ?? (yield* toId(id, output?.batchPredictionJobId));
356 const desiredLabels = {
357 ...toLabels(news.labels),
358 ...(yield* createInternalLabels(id)),
359 };
360
361 let current = yield* findOwned(id, env.project, env.region, output?.name);
362
363 if (current === undefined) {
364 const created = yield* aiplatform
365 .createProjectsLocationsBatchPredictionJobs({
366 parent: parentOf(env.project, location),
367 body: {
368 displayName,
369 model: news.model,
370 inputConfig: news.inputConfig,
371 outputConfig: news.outputConfig,
372 dedicatedResources: news.dedicatedResources,
373 modelParameters: news.modelParameters,
374 generateExplanation: news.generateExplanation,
375 disableContainerLogging: news.disableContainerLogging,
376 serviceAccount: news.serviceAccount,
377 labels: desiredLabels,
378 encryptionSpec: news.encryptionSpec,
379 },
380 })
381 .pipe(Effect.catchTag("Conflict", () => findOwned(id, env.project, env.region)));
382 current = created ?? undefined;
383 }
384
385 if (current === undefined) {
386 return yield* new BatchPredictionJobNotResolved({
387 resourceName: output?.name ?? parentOf(env.project, location),
388 });
389 }
390
391 return toAttrs(current, env.project);
392 }),
393
394 delete: Effect.fn(function* ({ output }) {
395 yield* cancelAndDelete(output.name);
396 }),
397 });