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