The best way to control Claude Code, Codex and more from any computer or phone
| 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 | } from "./internal.ts"; |
| 24 | import { listLocations } from "./names.ts"; |
| 25 | |
| 26 | export type TuningJobProps = { |
| 27 | /** |
| 28 | * Vertex AI location. Immutable — changing it replaces the job. |
| 29 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 30 | */ |
| 31 | location?: string; |
| 32 | /** |
| 33 | * Description of the tuning job. |
| 34 | */ |
| 35 | description?: string; |
| 36 | /** |
| 37 | * Display name of the tuned model (max 128 Unicode characters). |
| 38 | */ |
| 39 | tunedModelDisplayName?: string; |
| 40 | /** |
| 41 | * Base model being tuned (for example `gemini-2.0-flash-001`). |
| 42 | */ |
| 43 | baseModel?: string; |
| 44 | /** |
| 45 | * Supervised fine-tuning spec. Required unless `preferenceOptimizationSpec` |
| 46 | * or `preTunedModel` is set. |
| 47 | */ |
| 48 | supervisedTuningSpec?: aiplatform.GoogleCloudAiplatformV1SupervisedTuningSpec; |
| 49 | /** |
| 50 | * Preference-optimization spec. |
| 51 | */ |
| 52 | preferenceOptimizationSpec?: aiplatform.GoogleCloudAiplatformV1PreferenceOptimizationSpec; |
| 53 | /** |
| 54 | * Pre-tuned model for continuous tuning. |
| 55 | */ |
| 56 | preTunedModel?: aiplatform.GoogleCloudAiplatformV1PreTunedModel; |
| 57 | /** |
| 58 | * Runtime service account email. |
| 59 | */ |
| 60 | serviceAccount?: string; |
| 61 | /** |
| 62 | * User labels. Alchemy ownership labels are merged in automatically. |
| 63 | */ |
| 64 | labels?: Record<string, string>; |
| 65 | /** |
| 66 | * Customer-managed encryption key. Immutable. |
| 67 | */ |
| 68 | encryptionSpec?: aiplatform.GoogleCloudAiplatformV1EncryptionSpec; |
| 69 | }; |
| 70 | |
| 71 | export type TuningJob = Resource< |
| 72 | "GCP.AIPlatform.TuningJob", |
| 73 | TuningJobProps, |
| 74 | { |
| 75 | /** Full resource name. */ |
| 76 | name: string; |
| 77 | /** Job id (last path segment). */ |
| 78 | tuningJobId: string; |
| 79 | /** Project id. */ |
| 80 | project: string; |
| 81 | /** Location. */ |
| 82 | location: string; |
| 83 | /** Description. */ |
| 84 | description: string | undefined; |
| 85 | /** Tuned-model display name. */ |
| 86 | tunedModelDisplayName: string | undefined; |
| 87 | /** Base model. */ |
| 88 | baseModel: string | undefined; |
| 89 | /** User labels (Alchemy ownership labels stripped). */ |
| 90 | labels: Record<string, string>; |
| 91 | /** Job state. */ |
| 92 | state: string | undefined; |
| 93 | /** RFC3339 creation timestamp. */ |
| 94 | createTime: string | undefined; |
| 95 | /** RFC3339 start timestamp. */ |
| 96 | startTime: string | undefined; |
| 97 | /** RFC3339 end timestamp. */ |
| 98 | endTime: string | undefined; |
| 99 | /** RFC3339 last-update timestamp. */ |
| 100 | updateTime: string | undefined; |
| 101 | /** Failure/cancel status, if any. */ |
| 102 | error: { code?: number; message?: string } | undefined; |
| 103 | }, |
| 104 | never, |
| 105 | Providers |
| 106 | >; |
| 107 | |
| 108 | /** |
| 109 | * A Vertex AI TuningJob (supervised fine-tune / preference optimization). |
| 110 | * |
| 111 | * Creating a TuningJob starts it immediately. There is no update API, so |
| 112 | * reconcile is observe-ensure (create if missing). There is no delete RPC — |
| 113 | * destroy cancels the job. Alchemy ownership labels are merged into `labels` |
| 114 | * so `list` / nuke can find it. |
| 115 | * |
| 116 | * ### Creating a Tuning Job |
| 117 | * **Example:** Supervised fine-tune |
| 118 | * ```typescript |
| 119 | * const job = yield* GCP.AIPlatform.TuningJob("Tune", { |
| 120 | * baseModel: "gemini-2.0-flash-001", |
| 121 | * supervisedTuningSpec: { |
| 122 | * trainingDatasetUri: "gs://bucket/train.jsonl", |
| 123 | * }, |
| 124 | * }); |
| 125 | * ``` |
| 126 | * |
| 127 | * @resource |
| 128 | * @category AIPlatform |
| 129 | */ |
| 130 | export const TuningJob = Resource<TuningJob>("GCP.AIPlatform.TuningJob"); |
| 131 | |
| 132 | export class TuningJobNotResolved extends Data.TaggedError("GCP.AIPlatform.TuningJobNotResolved")<{ |
| 133 | resourceName: string; |
| 134 | }> {} |
| 135 | |
| 136 | const toDisplayName = (id: string, existing?: string) => |
| 137 | Effect.gen(function* () { |
| 138 | if (existing !== undefined) return existing; |
| 139 | return rfc1035( |
| 140 | yield* createPhysicalName({ |
| 141 | id, |
| 142 | maxLength: 63, |
| 143 | lowercase: true, |
| 144 | }), |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | const toAttrs = (job: aiplatform.GoogleCloudAiplatformV1TuningJob, project: string) => { |
| 149 | const name = job.name ?? ""; |
| 150 | const parsed = parseResourceName(name, "tuningJobs"); |
| 151 | return { |
| 152 | name, |
| 153 | tuningJobId: parsed.id, |
| 154 | project: parsed.project || project, |
| 155 | location: parsed.location, |
| 156 | description: job.description, |
| 157 | tunedModelDisplayName: job.tunedModelDisplayName, |
| 158 | baseModel: job.baseModel, |
| 159 | labels: userLabels(job.labels), |
| 160 | state: typeof job.state === "string" ? job.state : undefined, |
| 161 | createTime: job.createTime, |
| 162 | startTime: job.startTime, |
| 163 | endTime: job.endTime, |
| 164 | updateTime: job.updateTime, |
| 165 | error: job.error ? { code: job.error.code, message: job.error.message } : undefined, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const getByName = (name: string) => |
| 170 | aiplatform |
| 171 | .getProjectsLocationsTuningJobs({ name }) |
| 172 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 173 | |
| 174 | const listJobs = (project: string, region: string) => { |
| 175 | const collect = (parent: string) => |
| 176 | aiplatform.listProjectsLocationsTuningJobs.pages({ parent, pageSize: 100 }).pipe( |
| 177 | Stream.flatMap((page) => Stream.fromIterable(page.tuningJobs ?? [])), |
| 178 | Stream.filter((job) => hasAlchemyLabelKeys(job.labels)), |
| 179 | Stream.runCollect, |
| 180 | Effect.map((chunk) => Array.from(chunk)), |
| 181 | ); |
| 182 | const fallback = Effect.forEach(listLocations(region), (location) => |
| 183 | collect(`projects/${project}/locations/${location}`), |
| 184 | ).pipe(Effect.map((pages) => pages.flat())); |
| 185 | // Vertex AI has no `locations/-` wildcard; scan known locations. |
| 186 | return fallback.pipe(Effect.catchTag("NotFound", () => Effect.succeed([]))); |
| 187 | }; |
| 188 | |
| 189 | const findOwned = (id: string, project: string, region: string, hinted?: string) => |
| 190 | Effect.gen(function* () { |
| 191 | if (hinted !== undefined && hinted.length > 0) { |
| 192 | const existing = yield* getByName(hinted); |
| 193 | if (existing !== undefined) return existing; |
| 194 | } |
| 195 | const jobs = yield* listJobs(project, region); |
| 196 | for (const job of jobs) { |
| 197 | if (yield* hasAlchemyLabels(id, tagRecord(job.labels))) return job; |
| 198 | } |
| 199 | return undefined as aiplatform.GoogleCloudAiplatformV1TuningJob | undefined; |
| 200 | }); |
| 201 | |
| 202 | const cancelJob = (name: string) => |
| 203 | Effect.gen(function* () { |
| 204 | const existing = yield* getByName(name); |
| 205 | if (existing === undefined) return; |
| 206 | if (!isJobTerminal(typeof existing.state === "string" ? existing.state : undefined)) { |
| 207 | yield* aiplatform |
| 208 | .cancelProjectsLocationsTuningJobs({ name }) |
| 209 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 210 | yield* getByName(name).pipe( |
| 211 | Effect.filterOrFail( |
| 212 | (job) => |
| 213 | job === undefined || |
| 214 | isJobTerminal(typeof job.state === "string" ? job.state : undefined), |
| 215 | () => new TuningJobNotResolved({ resourceName: name }), |
| 216 | ), |
| 217 | Effect.retry({ |
| 218 | while: (error) => error._tag === "GCP.AIPlatform.TuningJobNotResolved", |
| 219 | times: 8, |
| 220 | schedule: Schedule.spaced("4 seconds"), |
| 221 | }), |
| 222 | Effect.catchTag("GCP.AIPlatform.TuningJobNotResolved", () => Effect.void), |
| 223 | ); |
| 224 | } |
| 225 | }); |
| 226 | |
| 227 | export const TuningJobProvider = () => |
| 228 | Provider.succeed(TuningJob, { |
| 229 | stables: ["name", "tuningJobId", "project", "location", "createTime"], |
| 230 | |
| 231 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 232 | const env = yield* GcpEnvironment.current; |
| 233 | if (!isResolved(news)) return undefined; |
| 234 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 235 | const nextLocation = normalizeLocation( |
| 236 | news.location ?? olds?.location ?? output?.location, |
| 237 | env.region, |
| 238 | ); |
| 239 | if (previousLocation !== nextLocation) { |
| 240 | return { action: "replace" as const, deleteFirst: false }; |
| 241 | } |
| 242 | return undefined; |
| 243 | }), |
| 244 | |
| 245 | read: Effect.fn(function* ({ id, output }) { |
| 246 | const env = yield* GcpEnvironment.current; |
| 247 | const existing = yield* findOwned(id, env.project, env.region, output?.name); |
| 248 | if (existing === undefined) return undefined; |
| 249 | const attrs = toAttrs(existing, env.project); |
| 250 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 251 | }), |
| 252 | |
| 253 | list: () => |
| 254 | Effect.gen(function* () { |
| 255 | const env = yield* GcpEnvironment.current; |
| 256 | const jobs = yield* listJobs(env.project, env.region); |
| 257 | // Tuning jobs have no delete API — `delete` cancels. A terminal job |
| 258 | // holds no resources and can never be removed, so it is not a |
| 259 | // reclaimable leftover (nuke / census would report it forever). |
| 260 | return jobs |
| 261 | .filter((job) => !isJobTerminal(typeof job.state === "string" ? job.state : undefined)) |
| 262 | .map((job) => toAttrs(job, env.project)); |
| 263 | }), |
| 264 | |
| 265 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 266 | const env = yield* GcpEnvironment.current; |
| 267 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 268 | const tunedModelDisplayName = |
| 269 | news.tunedModelDisplayName ?? (yield* toDisplayName(id, output?.tuningJobId)); |
| 270 | const desiredLabels = { |
| 271 | ...toLabels(news.labels), |
| 272 | ...(yield* createInternalLabels(id)), |
| 273 | }; |
| 274 | |
| 275 | let current = yield* findOwned(id, env.project, env.region, output?.name); |
| 276 | |
| 277 | if (current === undefined) { |
| 278 | const created = yield* aiplatform |
| 279 | .createProjectsLocationsTuningJobs({ |
| 280 | parent: parentOf(env.project, location), |
| 281 | body: { |
| 282 | description: news.description, |
| 283 | tunedModelDisplayName, |
| 284 | baseModel: news.baseModel, |
| 285 | supervisedTuningSpec: news.supervisedTuningSpec, |
| 286 | preferenceOptimizationSpec: news.preferenceOptimizationSpec, |
| 287 | preTunedModel: news.preTunedModel, |
| 288 | serviceAccount: news.serviceAccount, |
| 289 | labels: desiredLabels, |
| 290 | encryptionSpec: news.encryptionSpec, |
| 291 | }, |
| 292 | }) |
| 293 | .pipe(Effect.catchTag("Conflict", () => findOwned(id, env.project, env.region))); |
| 294 | current = created ?? undefined; |
| 295 | } |
| 296 | |
| 297 | if (current === undefined) { |
| 298 | return yield* new TuningJobNotResolved({ |
| 299 | resourceName: output?.name ?? parentOf(env.project, location), |
| 300 | }); |
| 301 | } |
| 302 | |
| 303 | return toAttrs(current, env.project); |
| 304 | }), |
| 305 | |
| 306 | delete: Effect.fn(function* ({ output }) { |
| 307 | yield* cancelJob(output.name); |
| 308 | }), |
| 309 | }); |