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

main
ts 385 lines 12.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 createInternalLabels,
16 hasAlchemyPrefix,
17 labelsDiffer,
18 listLocations,
19 locationOf,
20 lastSegment,
21 projectOf,
22 stableJson,
23 toDisplayName,
24 toLabels,
25 toResourceId,
26 userLabels,
27 } from "./names.ts";
28 import { waitForOperation } from "./operations.ts";
29
30 const DEFAULT_SCHEMA_TITLE = "system.Artifact";
31
32 export type MetadataStoresArtifactProps = {
33 /**
34 * Parent MetadataStore resource name
35 * `projects/{project}/locations/{location}/metadataStores/{metadatastore}`.
36 * Immutable — changing it replaces the artifact.
37 */
38 metadataStore: string;
39 /**
40 * Artifact id. If omitted, a unique id is generated. Must be 4-128
41 * characters of `a-z` and `-`. Immutable.
42 */
43 artifactId?: string;
44 /**
45 * Display name (max 128 Unicode characters).
46 */
47 displayName?: string;
48 /**
49 * Human-readable description.
50 */
51 description?: string;
52 /**
53 * User labels. Alchemy ownership labels are merged in automatically.
54 */
55 labels?: Record<string, string>;
56 /**
57 * Uniform resource identifier of the artifact file.
58 */
59 uri?: string;
60 /**
61 * Artifact lifecycle state.
62 */
63 state?: aiplatform.GoogleCloudAiplatformV1ArtifactStateEnum | (string & {});
64 /**
65 * Schema title registered in the MetadataStore. Vertex AI rejects
66 * entries without one.
67 * @default "system.Artifact"
68 */
69 schemaTitle?: string;
70 /**
71 * Schema version registered in the MetadataStore.
72 */
73 schemaVersion?: string;
74 /**
75 * Arbitrary metadata properties.
76 */
77 metadata?: aiplatform.DocumentMap;
78 };
79
80 export type MetadataStoresArtifact = Resource<
81 "GCP.AIPlatform.MetadataStoresArtifact",
82 MetadataStoresArtifactProps,
83 {
84 /** Full resource name. */
85 name: string;
86 /** Artifact id (last path segment). */
87 artifactId: string;
88 /** Parent MetadataStore resource name. */
89 metadataStore: string;
90 /** Project id. */
91 project: string;
92 /** Location id. */
93 location: string;
94 /** Display name. */
95 displayName: string | undefined;
96 /** User-provided description. */
97 description: string | undefined;
98 /** User labels (Alchemy ownership labels stripped). */
99 labels: Record<string, string>;
100 /** Artifact URI. */
101 uri: string | undefined;
102 /** Artifact state. */
103 state: string | undefined;
104 /** Schema title. */
105 schemaTitle: string | undefined;
106 /** Schema version. */
107 schemaVersion: string | undefined;
108 /** RFC3339 creation timestamp. */
109 createTime: string | undefined;
110 /** RFC3339 last-update timestamp. */
111 updateTime: string | undefined;
112 },
113 never,
114 Providers
115 >;
116
117 /**
118 * A Vertex ML Metadata Artifact associated with a MetadataStore.
119 *
120 * Changing `metadataStore` or `artifactId` replaces the artifact. Display
121 * name, description, labels, URI, state, schema, and metadata update in
122 * place.
123 *
124 * ### Creating an Artifact
125 * **Example:** Generated id
126 * ```typescript
127 * const artifact = yield* GCP.AIPlatform.MetadataStoresArtifact("Model", {
128 * metadataStore: store.name,
129 * displayName: "trained-model",
130 * uri: "gs://bucket/model",
131 * labels: { env: "prod" },
132 * });
133 * ```
134 *
135 * @resource
136 * @category AIPlatform
137 */
138 export const MetadataStoresArtifact = Resource<MetadataStoresArtifact>(
139 "GCP.AIPlatform.MetadataStoresArtifact",
140 );
141
142 export class MetadataStoresArtifactNotResolved extends Data.TaggedError(
143 "GCP.AIPlatform.MetadataStoresArtifactNotResolved",
144 )<{
145 resourceName: string;
146 }> {}
147
148 export class MetadataStoresArtifactStillExists extends Data.TaggedError(
149 "GCP.AIPlatform.MetadataStoresArtifactStillExists",
150 )<{
151 resourceName: string;
152 }> {}
153
154 const parentOf = (name: string) => {
155 const at = name.lastIndexOf("/artifacts/");
156 return at >= 0 ? name.slice(0, at) : name;
157 };
158
159 const resourceName = (metadataStore: string, artifactId: string) =>
160 `${metadataStore}/artifacts/${artifactId}`;
161
162 const toAttrs = (artifact: aiplatform.GoogleCloudAiplatformV1Artifact, project: string) => {
163 const name = artifact.name ?? "";
164 return {
165 name,
166 artifactId: lastSegment(name),
167 metadataStore: parentOf(name),
168 project: projectOf(name, project),
169 location: locationOf(name),
170 displayName: artifact.displayName,
171 description: artifact.description,
172 labels: userLabels(artifact.labels),
173 uri: artifact.uri,
174 state: artifact.state,
175 schemaTitle: artifact.schemaTitle,
176 schemaVersion: artifact.schemaVersion,
177 createTime: artifact.createTime,
178 updateTime: artifact.updateTime,
179 };
180 };
181
182 const getByName = (name: string) =>
183 aiplatform
184 .getProjectsLocationsMetadataStoresArtifacts({ name })
185 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
186
187 const listArtifacts = (parent: string) =>
188 aiplatform.listProjectsLocationsMetadataStoresArtifacts.pages({ parent, pageSize: 100 }).pipe(
189 Stream.runCollect,
190 Effect.map((pages) => Array.from(pages).flatMap((page) => page.artifacts ?? [])),
191 Effect.catchTag("NotFound", () =>
192 Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1Artifact[]),
193 ),
194 );
195
196 const listStores = (parent: string) =>
197 aiplatform.listProjectsLocationsMetadataStores.pages({ parent, pageSize: 100 }).pipe(
198 Stream.runCollect,
199 Effect.map((pages) => Array.from(pages).flatMap((page) => page.metadataStores ?? [])),
200 Effect.catchTag("NotFound", () =>
201 Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1MetadataStore[]),
202 ),
203 );
204
205 const waitUntilGone = (name: string) =>
206 getByName(name).pipe(
207 Effect.filterOrFail(
208 (artifact) => artifact === undefined,
209 () => new MetadataStoresArtifactStillExists({ resourceName: name }),
210 ),
211 Effect.asVoid,
212 Effect.retry({
213 while: (error) => error._tag === "GCP.AIPlatform.MetadataStoresArtifactStillExists",
214 times: 10,
215 schedule: Schedule.spaced("1 second"),
216 }),
217 );
218
219 export const MetadataStoresArtifactProvider = () =>
220 Provider.succeed(MetadataStoresArtifact, {
221 stables: ["name", "artifactId", "metadataStore", "project", "location", "createTime"],
222
223 diff: Effect.fn(function* ({ news, olds, output }) {
224 if (!isResolved(news)) return undefined;
225 const previousParent = olds?.metadataStore ?? output?.metadataStore;
226 const nextParent = news.metadataStore ?? previousParent;
227 const previousId = olds?.artifactId ?? output?.artifactId;
228 const nextId = news.artifactId ?? previousId;
229 if (
230 (previousParent !== undefined &&
231 nextParent !== undefined &&
232 nextParent !== previousParent) ||
233 (previousId !== undefined && nextId !== undefined && nextId !== previousId)
234 ) {
235 return {
236 action: "replace" as const,
237 deleteFirst:
238 previousParent === nextParent && previousId !== undefined && nextId === previousId,
239 };
240 }
241 return undefined;
242 }),
243
244 read: Effect.fn(function* ({ id, olds, output }) {
245 const env = yield* GcpEnvironment.current;
246 const artifactId = yield* toResourceId(id, olds?.artifactId, output?.artifactId);
247 const metadataStore = olds?.metadataStore ?? output?.metadataStore;
248 const name =
249 output?.name ??
250 (metadataStore !== undefined ? resourceName(metadataStore, artifactId) : "");
251 if (name.length === 0) return undefined;
252 const existing = yield* getByName(name);
253 if (existing === undefined) return undefined;
254 const attrs = toAttrs(existing, env.project);
255 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
256 }),
257
258 list: () =>
259 Effect.gen(function* () {
260 const env = yield* GcpEnvironment.current;
261 const stores = yield* Effect.forEach(
262 listLocations(env.region),
263 (location) => listStores(`projects/${env.project}/locations/${location}`),
264 { concurrency: 4 },
265 );
266 const artifacts = yield* Effect.forEach(
267 stores.flat().filter((store) => store.name !== undefined),
268 (store) => listArtifacts(store.name!),
269 { concurrency: 4 },
270 );
271 return artifacts
272 .flat()
273 .filter((artifact) => hasAlchemyPrefix(artifact.labels))
274 .map((artifact) => toAttrs(artifact, env.project));
275 }),
276
277 reconcile: Effect.fn(function* ({ id, news, output }) {
278 const env = yield* GcpEnvironment.current;
279 const artifactId = yield* toResourceId(id, news.artifactId, output?.artifactId);
280 const metadataStore = news.metadataStore;
281 const name = resourceName(metadataStore, artifactId);
282 const displayName = yield* toDisplayName(id, news.displayName, output?.displayName);
283 const desiredLabels = {
284 ...toLabels(news.labels),
285 ...(yield* createInternalLabels(id)),
286 };
287
288 let current = yield* getByName(output?.name ?? name);
289
290 if (current === undefined) {
291 const created = yield* aiplatform
292 .createProjectsLocationsMetadataStoresArtifacts({
293 parent: metadataStore,
294 artifactId,
295 body: {
296 displayName,
297 description: news.description,
298 labels: desiredLabels,
299 uri: news.uri,
300 state: news.state,
301 schemaTitle: news.schemaTitle ?? DEFAULT_SCHEMA_TITLE,
302 schemaVersion: news.schemaVersion,
303 metadata: news.metadata,
304 },
305 })
306 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
307 current = created ?? undefined;
308 }
309
310 if (current === undefined) {
311 return yield* new MetadataStoresArtifactNotResolved({ resourceName: name });
312 }
313
314 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
315 const displayChanged = (current.displayName ?? "") !== displayName;
316 const labelsChanged = labelsDiffer(current.labels, desiredLabels);
317 const uriChanged = (current.uri ?? "") !== (news.uri ?? "");
318 const stateChanged = (current.state ?? "") !== (news.state ?? "");
319 const schemaTitleChanged =
320 (current.schemaTitle ?? "") !== (news.schemaTitle ?? DEFAULT_SCHEMA_TITLE);
321 const schemaVersionChanged = (current.schemaVersion ?? "") !== (news.schemaVersion ?? "");
322 const metadataChanged =
323 news.metadata !== undefined && stableJson(current.metadata) !== stableJson(news.metadata);
324
325 if (
326 descriptionChanged ||
327 displayChanged ||
328 labelsChanged ||
329 uriChanged ||
330 stateChanged ||
331 schemaTitleChanged ||
332 schemaVersionChanged ||
333 metadataChanged
334 ) {
335 current = yield* aiplatform.patchProjectsLocationsMetadataStoresArtifacts({
336 name,
337 updateMask: [
338 descriptionChanged ? "description" : undefined,
339 displayChanged ? "display_name" : undefined,
340 labelsChanged ? "labels" : undefined,
341 uriChanged ? "uri" : undefined,
342 stateChanged ? "state" : undefined,
343 schemaTitleChanged ? "schema_title" : undefined,
344 schemaVersionChanged ? "schema_version" : undefined,
345 metadataChanged ? "metadata" : undefined,
346 ]
347 .filter((field): field is string => field !== undefined)
348 .join(","),
349 body: {
350 name,
351 displayName,
352 description: news.description,
353 labels: desiredLabels,
354 uri: news.uri,
355 state: news.state,
356 schemaTitle: news.schemaTitle ?? DEFAULT_SCHEMA_TITLE,
357 schemaVersion: news.schemaVersion,
358 metadata: news.metadata,
359 etag: current.etag,
360 },
361 });
362 }
363
364 return toAttrs(current, env.project);
365 }),
366
367 delete: Effect.fn(function* ({ output }) {
368 const operation = yield* aiplatform
369 .deleteProjectsLocationsMetadataStoresArtifacts({
370 name: output.name,
371 })
372 .pipe(
373 Effect.retry({
374 while: (error) => error._tag === "Conflict",
375 times: 8,
376 schedule: Schedule.spaced("2 seconds"),
377 }),
378 Effect.catchTag("NotFound", () => Effect.undefined),
379 );
380 if (operation !== undefined) {
381 yield* waitForOperation(operation, { notFoundOk: true });
382 }
383 yield* waitUntilGone(output.name);
384 }),
385 });