The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createHash } from "node:crypto"; |
| 2 | import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1"; |
| 3 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 4 | import { isResolved } from "alchemy/Diff"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { createInternalLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { listLocations } from "./names.ts"; |
| 15 | import { lastSegment, locationOf, locationParent } from "./ownership.ts"; |
| 16 | |
| 17 | export type StudyMetricSpec = { |
| 18 | /** Metric id. Must be unique among metrics. */ |
| 19 | metricId: string; |
| 20 | /** Optimization goal. */ |
| 21 | goal: "MAXIMIZE" | "MINIMIZE" | (string & {}); |
| 22 | }; |
| 23 | |
| 24 | export type StudyParameterSpec = { |
| 25 | /** Parameter id. Must be unique among parameters. */ |
| 26 | parameterId: string; |
| 27 | /** Inclusive range for a `DOUBLE` parameter. */ |
| 28 | doubleValueSpec?: { |
| 29 | minValue?: number; |
| 30 | maxValue?: number; |
| 31 | defaultValue?: number; |
| 32 | }; |
| 33 | /** Inclusive range for an `INTEGER` parameter. */ |
| 34 | integerValueSpec?: { |
| 35 | minValue?: string; |
| 36 | maxValue?: string; |
| 37 | defaultValue?: string; |
| 38 | }; |
| 39 | /** Discrete feasible values. */ |
| 40 | discreteValueSpec?: { values?: number[]; defaultValue?: number }; |
| 41 | /** Categorical feasible values. */ |
| 42 | categoricalValueSpec?: { values?: string[]; defaultValue?: string }; |
| 43 | /** Scaling. Leave unset for categorical parameters. */ |
| 44 | scaleType?: "UNIT_LINEAR_SCALE" | "UNIT_LOG_SCALE" | "UNIT_REVERSE_LOG_SCALE" | (string & {}); |
| 45 | }; |
| 46 | |
| 47 | export type StudySpec = { |
| 48 | /** Metrics to optimize. */ |
| 49 | metrics: StudyMetricSpec[]; |
| 50 | /** Parameters to tune. */ |
| 51 | parameters: StudyParameterSpec[]; |
| 52 | /** Search algorithm. */ |
| 53 | algorithm?: "ALGORITHM_UNSPECIFIED" | "GRID_SEARCH" | "RANDOM_SEARCH" | (string & {}); |
| 54 | /** Which measurement to use when a trial reports several. */ |
| 55 | measurementSelectionType?: "LAST_MEASUREMENT" | "BEST_MEASUREMENT" | (string & {}); |
| 56 | }; |
| 57 | |
| 58 | export type StudyProps = { |
| 59 | /** |
| 60 | * Region (`us-central1`, …). Immutable — changing it replaces the Study. |
| 61 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 62 | */ |
| 63 | location?: string; |
| 64 | /** |
| 65 | * User-facing display name: letters, digits, and underscores, starting |
| 66 | * with a letter. Vertex AI Studies have no labels field, so Alchemy |
| 67 | * appends an `_alchemy_{hash}` ownership suffix (stripped from |
| 68 | * attributes). Display name is the lookup key. |
| 69 | * @default "study" |
| 70 | */ |
| 71 | displayName?: string; |
| 72 | /** |
| 73 | * Study configuration. Immutable — changing it replaces the Study. |
| 74 | */ |
| 75 | studySpec: StudySpec; |
| 76 | }; |
| 77 | |
| 78 | export type Study = Resource< |
| 79 | "GCP.AIPlatform.Study", |
| 80 | StudyProps, |
| 81 | { |
| 82 | /** Full resource name `projects/{project}/locations/{location}/studies/{study}`. */ |
| 83 | name: string; |
| 84 | /** Study id (last path segment). */ |
| 85 | studyId: string; |
| 86 | /** Region id. */ |
| 87 | location: string; |
| 88 | /** Project id. */ |
| 89 | project: string; |
| 90 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 91 | displayName: string | undefined; |
| 92 | /** Study configuration. */ |
| 93 | studySpec: aiplatform.GoogleCloudAiplatformV1StudySpec | undefined; |
| 94 | /** Server-reported state. */ |
| 95 | state: string | undefined; |
| 96 | /** Why the study is inactive, if set. */ |
| 97 | inactiveReason: string | undefined; |
| 98 | /** RFC3339 creation timestamp. */ |
| 99 | createTime: string | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | >; |
| 104 | |
| 105 | /** |
| 106 | * A Vertex AI Vizier Study for hyperparameter search. |
| 107 | * |
| 108 | * Studies have no labels field and no update RPC — Alchemy stamps an |
| 109 | * ownership hash onto the display name. Location and study spec are immutable. |
| 110 | * |
| 111 | * ### Creating a Study |
| 112 | * **Example:** Maximize accuracy over a double parameter |
| 113 | * ```typescript |
| 114 | * const study = yield* GCP.AIPlatform.Study("Tune", { |
| 115 | * studySpec: { |
| 116 | * metrics: [{ metricId: "accuracy", goal: "MAXIMIZE" }], |
| 117 | * parameters: [ |
| 118 | * { |
| 119 | * parameterId: "learning_rate", |
| 120 | * doubleValueSpec: { minValue: 0.001, maxValue: 0.1 }, |
| 121 | * }, |
| 122 | * ], |
| 123 | * algorithm: "RANDOM_SEARCH", |
| 124 | * }, |
| 125 | * }); |
| 126 | * ``` |
| 127 | * |
| 128 | * @resource |
| 129 | * @category AIPlatform |
| 130 | */ |
| 131 | export const Study = Resource<Study>("GCP.AIPlatform.Study"); |
| 132 | |
| 133 | export class StudyNotResolved extends Data.TaggedError("GCP.AIPlatform.StudyNotResolved")<{ |
| 134 | resourceName: string; |
| 135 | }> {} |
| 136 | |
| 137 | const toStudySpec = (spec: StudySpec): aiplatform.GoogleCloudAiplatformV1StudySpec => ({ |
| 138 | metrics: spec.metrics.map((metric) => ({ |
| 139 | metricId: metric.metricId, |
| 140 | goal: metric.goal, |
| 141 | })), |
| 142 | parameters: spec.parameters.map((parameter) => ({ |
| 143 | parameterId: parameter.parameterId, |
| 144 | doubleValueSpec: parameter.doubleValueSpec, |
| 145 | integerValueSpec: parameter.integerValueSpec, |
| 146 | discreteValueSpec: parameter.discreteValueSpec, |
| 147 | categoricalValueSpec: parameter.categoricalValueSpec, |
| 148 | scaleType: parameter.scaleType, |
| 149 | })), |
| 150 | algorithm: spec.algorithm, |
| 151 | measurementSelectionType: spec.measurementSelectionType, |
| 152 | }); |
| 153 | |
| 154 | const specKey = (spec: StudySpec) => |
| 155 | JSON.stringify({ |
| 156 | metrics: spec.metrics, |
| 157 | parameters: spec.parameters, |
| 158 | algorithm: spec.algorithm ?? "", |
| 159 | measurementSelectionType: spec.measurementSelectionType ?? "", |
| 160 | }); |
| 161 | |
| 162 | /** |
| 163 | * Study display names allow only `[A-Za-z0-9_]`, so ownership is a |
| 164 | * `_alchemy_{hash}` suffix over the stack, stage, and logical id rather |
| 165 | * than the bracketed marker other Vertex AI resources use. |
| 166 | */ |
| 167 | const OWNER_SEPARATOR = "_alchemy_"; |
| 168 | const DEFAULT_DISPLAY_NAME = "study"; |
| 169 | |
| 170 | const ownerSuffix = (id: string) => |
| 171 | createInternalLabels(id).pipe( |
| 172 | Effect.flatMap((labels) => |
| 173 | Effect.sync( |
| 174 | () => |
| 175 | OWNER_SEPARATOR + |
| 176 | createHash("sha256").update(JSON.stringify(labels)).digest("hex").slice(0, 20), |
| 177 | ), |
| 178 | ), |
| 179 | ); |
| 180 | |
| 181 | const encodeDisplayName = (suffix: string, displayName: string | undefined) => |
| 182 | `${displayName ?? DEFAULT_DISPLAY_NAME}${suffix}`; |
| 183 | |
| 184 | const parseDisplayName = (displayName: string | undefined) => { |
| 185 | const at = displayName?.lastIndexOf(OWNER_SEPARATOR) ?? -1; |
| 186 | return displayName !== undefined && at > 0 ? displayName.slice(0, at) : displayName; |
| 187 | }; |
| 188 | |
| 189 | /** Whether a study's display name carries an Alchemy ownership suffix. */ |
| 190 | export const hasStudyOwnership = (displayName: string | undefined) => |
| 191 | (displayName?.lastIndexOf(OWNER_SEPARATOR) ?? -1) > 0; |
| 192 | |
| 193 | const ownedByAlchemy = (id: string, displayName: string | undefined) => |
| 194 | ownerSuffix(id).pipe(Effect.map((suffix) => displayName?.endsWith(suffix) === true)); |
| 195 | |
| 196 | const toAttrs = (study: aiplatform.GoogleCloudAiplatformV1Study, project: string) => { |
| 197 | const name = study.name ?? ""; |
| 198 | const parsed = { text: parseDisplayName(study.displayName) }; |
| 199 | return { |
| 200 | name, |
| 201 | studyId: lastSegment(name), |
| 202 | location: locationOf(name), |
| 203 | project, |
| 204 | displayName: parsed.text, |
| 205 | studySpec: study.studySpec, |
| 206 | state: study.state, |
| 207 | inactiveReason: study.inactiveReason, |
| 208 | createTime: study.createTime, |
| 209 | }; |
| 210 | }; |
| 211 | |
| 212 | const getByName = (name: string) => |
| 213 | name.length === 0 |
| 214 | ? Effect.undefined |
| 215 | : aiplatform |
| 216 | .getProjectsLocationsStudies({ name }) |
| 217 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 218 | |
| 219 | // `lookupProjectsLocationsStudies` answers a missing display name with a |
| 220 | // 500 ("There was an issue when sending 'LookupStudyRequest'"), so scan |
| 221 | // the listing instead. |
| 222 | const lookupByDisplayName = (parent: string, displayName: string) => |
| 223 | aiplatform.listProjectsLocationsStudies.pages({ parent, pageSize: 100 }).pipe( |
| 224 | Stream.flatMap((page) => Stream.fromIterable(page.studies ?? [])), |
| 225 | Stream.filter((study) => study.displayName === displayName), |
| 226 | Stream.runHead, |
| 227 | Effect.map(Option.getOrUndefined), |
| 228 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 229 | ); |
| 230 | |
| 231 | const listAt = (parent: string, project: string) => |
| 232 | aiplatform.listProjectsLocationsStudies.pages({ parent, pageSize: 100 }).pipe( |
| 233 | Stream.flatMap((page) => Stream.fromIterable(page.studies ?? [])), |
| 234 | Stream.filter((study) => hasStudyOwnership(study.displayName)), |
| 235 | Stream.map((study) => toAttrs(study, project)), |
| 236 | Stream.runCollect, |
| 237 | Effect.map((chunk) => Array.from(chunk)), |
| 238 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 239 | ); |
| 240 | |
| 241 | export const StudyProvider = () => |
| 242 | Provider.succeed(Study, { |
| 243 | stables: ["name", "studyId", "location", "project", "createTime"], |
| 244 | |
| 245 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 246 | if (!isResolved(news)) return undefined; |
| 247 | const previousLocation = olds?.location ?? output?.location; |
| 248 | const nextLocation = news.location ?? previousLocation; |
| 249 | if (previousLocation !== undefined && previousLocation !== nextLocation) { |
| 250 | return { action: "replace" as const, deleteFirst: false }; |
| 251 | } |
| 252 | if (olds?.studySpec !== undefined) { |
| 253 | if (specKey(olds.studySpec) !== specKey(news.studySpec)) { |
| 254 | return { action: "replace" as const, deleteFirst: false }; |
| 255 | } |
| 256 | } |
| 257 | return undefined; |
| 258 | }), |
| 259 | |
| 260 | read: Effect.fn(function* ({ id, olds, output }) { |
| 261 | const env = yield* GcpEnvironment.current; |
| 262 | const existing = yield* getByName(output?.name ?? ""); |
| 263 | if (existing !== undefined) { |
| 264 | const attrs = toAttrs(existing, env.project); |
| 265 | return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); |
| 266 | } |
| 267 | const location = olds?.location ?? output?.location ?? env.region; |
| 268 | const parent = locationParent(env.project, location); |
| 269 | const displayName = encodeDisplayName(yield* ownerSuffix(id), olds?.displayName); |
| 270 | const lookedUp = yield* lookupByDisplayName(parent, displayName); |
| 271 | if (lookedUp === undefined) return undefined; |
| 272 | const attrs = toAttrs(lookedUp, env.project); |
| 273 | return (yield* ownedByAlchemy(id, lookedUp.displayName)) ? attrs : Unowned(attrs); |
| 274 | }), |
| 275 | |
| 276 | list: () => |
| 277 | Effect.gen(function* () { |
| 278 | const env = yield* GcpEnvironment.current; |
| 279 | return (yield* Effect.forEach(listLocations(env.region), (location) => |
| 280 | listAt(locationParent(env.project, location), env.project), |
| 281 | )).flat(); |
| 282 | }), |
| 283 | |
| 284 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 285 | const env = yield* GcpEnvironment.current; |
| 286 | const location = news.location ?? output?.location ?? env.region; |
| 287 | const parent = locationParent(env.project, location); |
| 288 | const displayName = encodeDisplayName(yield* ownerSuffix(id), news.displayName); |
| 289 | const studySpec = toStudySpec(news.studySpec); |
| 290 | |
| 291 | let current = yield* getByName(output?.name ?? ""); |
| 292 | if (current === undefined) { |
| 293 | current = yield* lookupByDisplayName(parent, displayName); |
| 294 | } |
| 295 | |
| 296 | if (current === undefined) { |
| 297 | const created = yield* aiplatform |
| 298 | .createProjectsLocationsStudies({ |
| 299 | parent, |
| 300 | body: { |
| 301 | displayName, |
| 302 | studySpec, |
| 303 | }, |
| 304 | }) |
| 305 | .pipe(Effect.catchTag("Conflict", () => lookupByDisplayName(parent, displayName))); |
| 306 | current = created ?? undefined; |
| 307 | } |
| 308 | |
| 309 | if (current === undefined) { |
| 310 | return yield* new StudyNotResolved({ |
| 311 | resourceName: output?.name ?? `${parent}/studies/-`, |
| 312 | }); |
| 313 | } |
| 314 | |
| 315 | return toAttrs(current, env.project); |
| 316 | }), |
| 317 | |
| 318 | delete: Effect.fn(function* ({ output }) { |
| 319 | yield* aiplatform |
| 320 | .deleteProjectsLocationsStudies({ name: output.name }) |
| 321 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 322 | }), |
| 323 | }); |