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 * 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 { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | fingerprint, |
| 16 | hasAlchemyLabelMap, |
| 17 | lastSegment, |
| 18 | normalizeLocation, |
| 19 | parseResourceName, |
| 20 | specifiedEquals, |
| 21 | toPhysicalRfc1035, |
| 22 | userLabels, |
| 23 | } from "./helpers.ts"; |
| 24 | import { listLocations } from "./names.ts"; |
| 25 | import { settlePendingCreates, waitForOperation } from "./operations.ts"; |
| 26 | import type { EncryptionSpec } from "./shared.ts"; |
| 27 | |
| 28 | const MAX_NAME_LENGTH = 63; |
| 29 | |
| 30 | export type ClientConnectionConfig = { |
| 31 | /** Online prediction request timeout (e.g. `"5s"`). */ |
| 32 | inferenceTimeout?: string; |
| 33 | }; |
| 34 | |
| 35 | export type PredictRequestResponseLoggingConfig = { |
| 36 | /** Enable request/response logging. */ |
| 37 | enabled?: boolean; |
| 38 | /** Sampling rate in `(0, 1]`. */ |
| 39 | samplingRate?: number; |
| 40 | /** BigQuery destination for logs. */ |
| 41 | bigqueryDestination?: { outputUri?: string }; |
| 42 | }; |
| 43 | |
| 44 | export type PrivateServiceConnectConfig = { |
| 45 | /** Expose the endpoint via Private Service Connect. */ |
| 46 | enablePrivateServiceConnect?: boolean; |
| 47 | /** Projects allowed to create forwarding rules. */ |
| 48 | projectAllowlist?: string[]; |
| 49 | }; |
| 50 | |
| 51 | export type GenAiAdvancedFeaturesConfig = { |
| 52 | /** Enable native RAG on GenAI endpoints. */ |
| 53 | ragConfig?: { enableRag?: boolean }; |
| 54 | }; |
| 55 | |
| 56 | export type GdcConfig = { |
| 57 | /** Google Distributed Cloud zone for this endpoint. Immutable. */ |
| 58 | zone?: string; |
| 59 | }; |
| 60 | |
| 61 | export type EndpointProps = { |
| 62 | /** |
| 63 | * Endpoint id (the `{endpoint}` segment of |
| 64 | * `projects/{project}/locations/{location}/endpoints/{endpoint}`). |
| 65 | * If omitted, a unique name is generated. Immutable — changing it |
| 66 | * replaces the endpoint. |
| 67 | */ |
| 68 | endpointId?: string; |
| 69 | /** |
| 70 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 71 | * replaces the endpoint. |
| 72 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 73 | */ |
| 74 | location?: string; |
| 75 | /** |
| 76 | * Display name. Defaults to the endpoint id when omitted. |
| 77 | */ |
| 78 | displayName?: string; |
| 79 | /** |
| 80 | * Human-readable description. |
| 81 | */ |
| 82 | description?: string; |
| 83 | /** |
| 84 | * User labels. Alchemy ownership labels are merged in automatically. |
| 85 | */ |
| 86 | labels?: Record<string, string>; |
| 87 | /** |
| 88 | * VPC network to peer |
| 89 | * (`projects/{project}/global/networks/{network}`). Immutable. |
| 90 | */ |
| 91 | network?: string; |
| 92 | /** |
| 93 | * Customer-managed encryption. Immutable — changing it replaces the |
| 94 | * endpoint. |
| 95 | */ |
| 96 | encryptionSpec?: EncryptionSpec; |
| 97 | /** |
| 98 | * Serve this endpoint through a dedicated DNS. |
| 99 | * @default false |
| 100 | */ |
| 101 | dedicatedEndpointEnabled?: boolean; |
| 102 | /** |
| 103 | * Traffic split from deployed-model id to percentage. Values must sum |
| 104 | * to 100, or the map must be empty. |
| 105 | */ |
| 106 | trafficSplit?: Record<string, number>; |
| 107 | /** |
| 108 | * Online-prediction client settings (inference timeout). |
| 109 | */ |
| 110 | clientConnectionConfig?: ClientConnectionConfig; |
| 111 | /** |
| 112 | * Request/response logging for online prediction. |
| 113 | */ |
| 114 | predictRequestResponseLoggingConfig?: PredictRequestResponseLoggingConfig; |
| 115 | /** |
| 116 | * Private Service Connect. Mutually exclusive with `network`. |
| 117 | */ |
| 118 | privateServiceConnectConfig?: PrivateServiceConnectConfig; |
| 119 | /** |
| 120 | * Deprecated PSC flag. Prefer `privateServiceConnectConfig`. Immutable. |
| 121 | */ |
| 122 | enablePrivateServiceConnect?: boolean; |
| 123 | /** |
| 124 | * GenAI advanced features (native RAG). |
| 125 | */ |
| 126 | genAiAdvancedFeaturesConfig?: GenAiAdvancedFeaturesConfig; |
| 127 | /** |
| 128 | * Google Distributed Cloud zone. Immutable. |
| 129 | */ |
| 130 | gdcConfig?: GdcConfig; |
| 131 | }; |
| 132 | |
| 133 | export type Endpoint = Resource< |
| 134 | "GCP.AIPlatform.Endpoint", |
| 135 | EndpointProps, |
| 136 | { |
| 137 | /** Full resource name `projects/{project}/locations/{location}/endpoints/{endpoint}`. */ |
| 138 | name: string; |
| 139 | /** Endpoint id (last path segment). */ |
| 140 | endpointId: string; |
| 141 | /** Project id. */ |
| 142 | project: string; |
| 143 | /** Location id. */ |
| 144 | location: string; |
| 145 | /** Display name. */ |
| 146 | displayName: string | undefined; |
| 147 | /** Description. */ |
| 148 | description: string | undefined; |
| 149 | /** User labels (Alchemy ownership labels stripped). */ |
| 150 | labels: Record<string, string>; |
| 151 | /** Peered VPC network, if any. */ |
| 152 | network: string | undefined; |
| 153 | /** Customer-managed encryption key, if any. */ |
| 154 | encryptionSpec: EncryptionSpec | undefined; |
| 155 | /** Whether a dedicated DNS is enabled. */ |
| 156 | dedicatedEndpointEnabled: boolean; |
| 157 | /** Dedicated endpoint DNS, if provisioned. */ |
| 158 | dedicatedEndpointDns: string | undefined; |
| 159 | /** Traffic split map. */ |
| 160 | trafficSplit: Record<string, number>; |
| 161 | /** Client connection config. */ |
| 162 | clientConnectionConfig: ClientConnectionConfig | undefined; |
| 163 | /** Request/response logging config. */ |
| 164 | predictRequestResponseLoggingConfig: PredictRequestResponseLoggingConfig | undefined; |
| 165 | /** Private Service Connect config. */ |
| 166 | privateServiceConnectConfig: PrivateServiceConnectConfig | undefined; |
| 167 | /** Deprecated PSC flag. */ |
| 168 | enablePrivateServiceConnect: boolean; |
| 169 | /** GenAI advanced features. */ |
| 170 | genAiAdvancedFeaturesConfig: GenAiAdvancedFeaturesConfig | undefined; |
| 171 | /** GDC config. */ |
| 172 | gdcConfig: GdcConfig | undefined; |
| 173 | /** Deployed model ids. */ |
| 174 | deployedModelIds: string[]; |
| 175 | /** Associated model-deployment monitoring job, if any. */ |
| 176 | modelDeploymentMonitoringJob: string | undefined; |
| 177 | /** RFC3339 creation timestamp. */ |
| 178 | createTime: string | undefined; |
| 179 | /** RFC3339 last-update timestamp. */ |
| 180 | updateTime: string | undefined; |
| 181 | /** Server etag. */ |
| 182 | etag: string | undefined; |
| 183 | }, |
| 184 | never, |
| 185 | Providers |
| 186 | >; |
| 187 | |
| 188 | /** |
| 189 | * A Vertex AI Endpoint for online prediction. |
| 190 | * |
| 191 | * Name, location, network, encryption, and GDC zone are identity — |
| 192 | * changing them replaces the endpoint. Display name, description, labels, |
| 193 | * traffic split, dedicated-endpoint flag, client connection, logging, and |
| 194 | * GenAI features update in place. |
| 195 | * |
| 196 | * ### Creating an Endpoint |
| 197 | * **Example:** Generated name |
| 198 | * ```typescript |
| 199 | * const endpoint = yield* GCP.AIPlatform.Endpoint("Predictor", {}); |
| 200 | * ``` |
| 201 | * |
| 202 | * **Example:** Named endpoint with labels |
| 203 | * ```typescript |
| 204 | * const endpoint = yield* GCP.AIPlatform.Endpoint("Predictor", { |
| 205 | * endpointId: "orders-predictor", |
| 206 | * displayName: "orders", |
| 207 | * labels: { env: "prod" }, |
| 208 | * }); |
| 209 | * ``` |
| 210 | * |
| 211 | * ### Updating an Endpoint |
| 212 | * **Example:** Change display name and labels |
| 213 | * ```typescript |
| 214 | * // Same logical id, changed props: the engine updates it in place. |
| 215 | * const endpoint = yield* GCP.AIPlatform.Endpoint("Predictor", { |
| 216 | * displayName: "orders-v2", |
| 217 | * labels: { env: "prod", role: "predict" }, |
| 218 | * }); |
| 219 | * ``` |
| 220 | * |
| 221 | * @resource |
| 222 | * @category AIPlatform |
| 223 | */ |
| 224 | export const Endpoint = Resource<Endpoint>("GCP.AIPlatform.Endpoint"); |
| 225 | |
| 226 | export class EndpointNotResolved extends Data.TaggedError("GCP.AIPlatform.EndpointNotResolved")<{ |
| 227 | resourceName: string; |
| 228 | }> {} |
| 229 | |
| 230 | export class EndpointStillExists extends Data.TaggedError("GCP.AIPlatform.EndpointStillExists")<{ |
| 231 | resourceName: string; |
| 232 | }> {} |
| 233 | |
| 234 | const resourceName = (project: string, location: string, endpointId: string) => |
| 235 | `projects/${project}/locations/${location}/endpoints/${endpointId}`; |
| 236 | |
| 237 | const trafficSplitOf = ( |
| 238 | split: Record<string, number | undefined> | null | undefined, |
| 239 | ): Record<string, number> => |
| 240 | Object.fromEntries( |
| 241 | Object.entries(split ?? {}).flatMap(([key, value]) => |
| 242 | typeof value === "number" ? [[key, value] as const] : [], |
| 243 | ), |
| 244 | ); |
| 245 | |
| 246 | const toEncryption = ( |
| 247 | spec: aiplatform.GoogleCloudAiplatformV1EncryptionSpec | EncryptionSpec | undefined, |
| 248 | ): EncryptionSpec | undefined => { |
| 249 | const kmsKeyName = spec?.kmsKeyName; |
| 250 | if (kmsKeyName === undefined || kmsKeyName.length === 0) return undefined; |
| 251 | return { kmsKeyName }; |
| 252 | }; |
| 253 | |
| 254 | const toClientConnection = ( |
| 255 | config: |
| 256 | | aiplatform.GoogleCloudAiplatformV1ClientConnectionConfig |
| 257 | | ClientConnectionConfig |
| 258 | | undefined, |
| 259 | ): ClientConnectionConfig | undefined => { |
| 260 | if (config?.inferenceTimeout === undefined) return undefined; |
| 261 | return { inferenceTimeout: config.inferenceTimeout }; |
| 262 | }; |
| 263 | |
| 264 | const toLogging = ( |
| 265 | config: |
| 266 | | aiplatform.GoogleCloudAiplatformV1PredictRequestResponseLoggingConfig |
| 267 | | PredictRequestResponseLoggingConfig |
| 268 | | undefined, |
| 269 | ): PredictRequestResponseLoggingConfig | undefined => { |
| 270 | if (config === undefined) return undefined; |
| 271 | return { |
| 272 | enabled: config.enabled, |
| 273 | samplingRate: config.samplingRate, |
| 274 | bigqueryDestination: config.bigqueryDestination |
| 275 | ? { outputUri: config.bigqueryDestination.outputUri } |
| 276 | : undefined, |
| 277 | }; |
| 278 | }; |
| 279 | |
| 280 | const toPsc = ( |
| 281 | config: |
| 282 | | aiplatform.GoogleCloudAiplatformV1PrivateServiceConnectConfig |
| 283 | | PrivateServiceConnectConfig |
| 284 | | undefined, |
| 285 | ): PrivateServiceConnectConfig | undefined => { |
| 286 | if (config === undefined) return undefined; |
| 287 | return { |
| 288 | enablePrivateServiceConnect: config.enablePrivateServiceConnect === true, |
| 289 | projectAllowlist: config.projectAllowlist, |
| 290 | }; |
| 291 | }; |
| 292 | |
| 293 | const toGenAi = ( |
| 294 | config: |
| 295 | | aiplatform.GoogleCloudAiplatformV1GenAiAdvancedFeaturesConfig |
| 296 | | GenAiAdvancedFeaturesConfig |
| 297 | | undefined, |
| 298 | ): GenAiAdvancedFeaturesConfig | undefined => { |
| 299 | if (config === undefined) return undefined; |
| 300 | return { |
| 301 | ragConfig: |
| 302 | config.ragConfig === undefined |
| 303 | ? undefined |
| 304 | : { enableRag: config.ragConfig.enableRag === true }, |
| 305 | }; |
| 306 | }; |
| 307 | |
| 308 | const toGdc = ( |
| 309 | config: aiplatform.GoogleCloudAiplatformV1GdcConfig | GdcConfig | undefined, |
| 310 | ): GdcConfig | undefined => { |
| 311 | const zone = config?.zone; |
| 312 | if (zone === undefined || zone.length === 0) return undefined; |
| 313 | return { zone }; |
| 314 | }; |
| 315 | |
| 316 | const toAttrs = (endpoint: aiplatform.GoogleCloudAiplatformV1Endpoint, project: string) => { |
| 317 | const name = endpoint.name ?? ""; |
| 318 | const parsed = parseResourceName(name, "endpoints"); |
| 319 | return { |
| 320 | name, |
| 321 | endpointId: parsed.id, |
| 322 | project: parsed.project || project, |
| 323 | location: parsed.location, |
| 324 | displayName: endpoint.displayName, |
| 325 | description: endpoint.description, |
| 326 | labels: userLabels(endpoint.labels), |
| 327 | network: endpoint.network, |
| 328 | encryptionSpec: toEncryption(endpoint.encryptionSpec), |
| 329 | dedicatedEndpointEnabled: endpoint.dedicatedEndpointEnabled === true, |
| 330 | dedicatedEndpointDns: endpoint.dedicatedEndpointDns, |
| 331 | trafficSplit: trafficSplitOf(endpoint.trafficSplit), |
| 332 | clientConnectionConfig: toClientConnection(endpoint.clientConnectionConfig), |
| 333 | predictRequestResponseLoggingConfig: toLogging(endpoint.predictRequestResponseLoggingConfig), |
| 334 | privateServiceConnectConfig: toPsc(endpoint.privateServiceConnectConfig), |
| 335 | enablePrivateServiceConnect: endpoint.enablePrivateServiceConnect === true, |
| 336 | genAiAdvancedFeaturesConfig: toGenAi(endpoint.genAiAdvancedFeaturesConfig), |
| 337 | gdcConfig: toGdc(endpoint.gdcConfig), |
| 338 | deployedModelIds: (endpoint.deployedModels ?? []).flatMap((model) => |
| 339 | model.id ? [model.id] : [], |
| 340 | ), |
| 341 | modelDeploymentMonitoringJob: endpoint.modelDeploymentMonitoringJob, |
| 342 | createTime: endpoint.createTime, |
| 343 | updateTime: endpoint.updateTime, |
| 344 | etag: endpoint.etag, |
| 345 | }; |
| 346 | }; |
| 347 | |
| 348 | const getByName = (name: string) => |
| 349 | aiplatform |
| 350 | .getProjectsLocationsEndpoints({ name }) |
| 351 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 352 | |
| 353 | /** |
| 354 | * Look an endpoint up by name. An endpoint is invisible while its create |
| 355 | * operation is pending (minutes on a fresh project), so on a miss wait for |
| 356 | * any pending create of this endpoint id and look again — otherwise an |
| 357 | * interrupted create's recovery read / delete reports it gone and leaks it. |
| 358 | */ |
| 359 | const getSettled = (name: string) => |
| 360 | Effect.gen(function* () { |
| 361 | const existing = yield* getByName(name); |
| 362 | if (existing !== undefined) return existing; |
| 363 | const parts = name.split("/"); |
| 364 | const settled = yield* settlePendingCreates({ |
| 365 | parent: parts.slice(0, 4).join("/"), |
| 366 | collection: "endpoints", |
| 367 | resourceId: parts[5], |
| 368 | }); |
| 369 | return settled ? yield* getByName(name) : undefined; |
| 370 | }); |
| 371 | |
| 372 | const waitUntilExists = (name: string) => |
| 373 | getByName(name).pipe( |
| 374 | Effect.flatMap((endpoint) => |
| 375 | endpoint |
| 376 | ? Effect.succeed(endpoint) |
| 377 | : Effect.fail(new EndpointNotResolved({ resourceName: name })), |
| 378 | ), |
| 379 | Effect.retry({ |
| 380 | while: (error) => error._tag === "GCP.AIPlatform.EndpointNotResolved", |
| 381 | times: 8, |
| 382 | schedule: Schedule.spaced("1 second"), |
| 383 | }), |
| 384 | ); |
| 385 | |
| 386 | const waitUntilGone = (name: string) => |
| 387 | getByName(name).pipe( |
| 388 | Effect.flatMap((endpoint) => |
| 389 | endpoint === undefined |
| 390 | ? Effect.void |
| 391 | : Effect.fail(new EndpointStillExists({ resourceName: name })), |
| 392 | ), |
| 393 | Effect.retry({ |
| 394 | while: (error) => error._tag === "GCP.AIPlatform.EndpointStillExists", |
| 395 | times: 10, |
| 396 | schedule: Schedule.spaced("2 seconds"), |
| 397 | }), |
| 398 | ); |
| 399 | |
| 400 | const toCreateBody = ( |
| 401 | news: EndpointProps, |
| 402 | endpointId: string, |
| 403 | desiredLabels: Record<string, string>, |
| 404 | ): aiplatform.GoogleCloudAiplatformV1Endpoint => ({ |
| 405 | displayName: news.displayName ?? endpointId, |
| 406 | description: news.description, |
| 407 | labels: desiredLabels, |
| 408 | network: news.network, |
| 409 | encryptionSpec: news.encryptionSpec, |
| 410 | dedicatedEndpointEnabled: news.dedicatedEndpointEnabled === true ? true : undefined, |
| 411 | trafficSplit: news.trafficSplit, |
| 412 | clientConnectionConfig: news.clientConnectionConfig, |
| 413 | predictRequestResponseLoggingConfig: news.predictRequestResponseLoggingConfig, |
| 414 | privateServiceConnectConfig: news.privateServiceConnectConfig, |
| 415 | enablePrivateServiceConnect: news.enablePrivateServiceConnect === true ? true : undefined, |
| 416 | genAiAdvancedFeaturesConfig: news.genAiAdvancedFeaturesConfig, |
| 417 | gdcConfig: news.gdcConfig, |
| 418 | }); |
| 419 | |
| 420 | export const EndpointProvider = () => |
| 421 | Provider.succeed(Endpoint, { |
| 422 | stables: ["name", "endpointId", "project", "location", "createTime"], |
| 423 | |
| 424 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 425 | const env = yield* GcpEnvironment.current; |
| 426 | if (!isResolved(news)) return undefined; |
| 427 | const previousId = olds?.endpointId ?? output?.endpointId; |
| 428 | const nextId = news.endpointId ? lastSegment(news.endpointId) : previousId; |
| 429 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 430 | const nextLocation = normalizeLocation(news.location ?? output?.location, env.region); |
| 431 | const previousNetwork = olds?.network ?? output?.network ?? ""; |
| 432 | const nextNetwork = news.network ?? previousNetwork; |
| 433 | const previousKey = |
| 434 | olds?.encryptionSpec?.kmsKeyName ?? output?.encryptionSpec?.kmsKeyName ?? ""; |
| 435 | const nextKey = news.encryptionSpec?.kmsKeyName ?? previousKey; |
| 436 | const previousGdc = olds?.gdcConfig?.zone ?? output?.gdcConfig?.zone ?? ""; |
| 437 | const nextGdc = news.gdcConfig?.zone ?? previousGdc; |
| 438 | const previousPsc = |
| 439 | olds?.enablePrivateServiceConnect ?? output?.enablePrivateServiceConnect ?? false; |
| 440 | const nextPsc = news.enablePrivateServiceConnect ?? previousPsc; |
| 441 | const replace = |
| 442 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 443 | previousLocation !== nextLocation || |
| 444 | previousNetwork !== nextNetwork || |
| 445 | previousKey !== nextKey || |
| 446 | previousGdc !== nextGdc || |
| 447 | previousPsc !== nextPsc; |
| 448 | if (!replace) return undefined; |
| 449 | return { |
| 450 | action: "replace" as const, |
| 451 | deleteFirst: |
| 452 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 453 | }; |
| 454 | }), |
| 455 | |
| 456 | read: Effect.fn(function* ({ id, olds, output }) { |
| 457 | const env = yield* GcpEnvironment.current; |
| 458 | const endpointId = yield* toPhysicalRfc1035( |
| 459 | id, |
| 460 | olds?.endpointId, |
| 461 | output?.endpointId, |
| 462 | MAX_NAME_LENGTH, |
| 463 | ); |
| 464 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 465 | const name = output?.name ?? resourceName(env.project, location, endpointId); |
| 466 | const existing = yield* getSettled(name); |
| 467 | if (existing === undefined) return undefined; |
| 468 | const attrs = toAttrs(existing, env.project); |
| 469 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 470 | }), |
| 471 | |
| 472 | list: () => |
| 473 | Effect.gen(function* () { |
| 474 | const env = yield* GcpEnvironment.current; |
| 475 | return yield* Stream.fromIterable(listLocations(env.region)).pipe( |
| 476 | Stream.flatMap((location) => |
| 477 | aiplatform.listProjectsLocationsEndpoints.pages({ |
| 478 | parent: `projects/${env.project}/locations/${location}`, |
| 479 | pageSize: 100, |
| 480 | }), |
| 481 | ), |
| 482 | Stream.flatMap((page) => Stream.fromIterable(page.endpoints ?? [])), |
| 483 | Stream.filter((endpoint) => hasAlchemyLabelMap(endpoint.labels)), |
| 484 | Stream.map((endpoint) => toAttrs(endpoint, env.project)), |
| 485 | Stream.runCollect, |
| 486 | Effect.map((chunk) => Array.from(chunk)), |
| 487 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 488 | ); |
| 489 | }), |
| 490 | |
| 491 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 492 | const env = yield* GcpEnvironment.current; |
| 493 | const endpointId = yield* toPhysicalRfc1035( |
| 494 | id, |
| 495 | news.endpointId, |
| 496 | output?.endpointId, |
| 497 | MAX_NAME_LENGTH, |
| 498 | ); |
| 499 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 500 | const name = resourceName(env.project, location, endpointId); |
| 501 | const desiredLabels = { |
| 502 | ...toLabels(news.labels), |
| 503 | ...(yield* createInternalLabels(id)), |
| 504 | }; |
| 505 | const displayName = news.displayName ?? endpointId; |
| 506 | |
| 507 | let current = yield* getSettled(output?.name ?? name); |
| 508 | |
| 509 | if (current === undefined) { |
| 510 | const created = yield* aiplatform |
| 511 | .createProjectsLocationsEndpoints({ |
| 512 | parent: `projects/${env.project}/locations/${location}`, |
| 513 | endpointId, |
| 514 | body: toCreateBody(news, endpointId, desiredLabels), |
| 515 | }) |
| 516 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 517 | if (created !== undefined) { |
| 518 | yield* waitForOperation(created); |
| 519 | } |
| 520 | current = yield* waitUntilExists(name); |
| 521 | } |
| 522 | |
| 523 | if (current === undefined) { |
| 524 | return yield* new EndpointNotResolved({ resourceName: name }); |
| 525 | } |
| 526 | |
| 527 | const observedLabels = tagRecord(current.labels); |
| 528 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 529 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 530 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 531 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 532 | const dedicatedChanged = |
| 533 | (current.dedicatedEndpointEnabled === true) !== (news.dedicatedEndpointEnabled === true); |
| 534 | const trafficChanged = |
| 535 | news.trafficSplit !== undefined && |
| 536 | fingerprint(trafficSplitOf(current.trafficSplit)) !== fingerprint(news.trafficSplit); |
| 537 | const clientChanged = |
| 538 | news.clientConnectionConfig !== undefined && |
| 539 | !specifiedEquals( |
| 540 | news.clientConnectionConfig, |
| 541 | toClientConnection(current.clientConnectionConfig), |
| 542 | ); |
| 543 | const loggingChanged = |
| 544 | news.predictRequestResponseLoggingConfig !== undefined && |
| 545 | !specifiedEquals( |
| 546 | news.predictRequestResponseLoggingConfig, |
| 547 | toLogging(current.predictRequestResponseLoggingConfig), |
| 548 | ); |
| 549 | const pscChanged = |
| 550 | news.privateServiceConnectConfig !== undefined && |
| 551 | !specifiedEquals( |
| 552 | news.privateServiceConnectConfig, |
| 553 | toPsc(current.privateServiceConnectConfig), |
| 554 | ); |
| 555 | const genAiChanged = |
| 556 | news.genAiAdvancedFeaturesConfig !== undefined && |
| 557 | !specifiedEquals( |
| 558 | news.genAiAdvancedFeaturesConfig, |
| 559 | toGenAi(current.genAiAdvancedFeaturesConfig), |
| 560 | ); |
| 561 | |
| 562 | if ( |
| 563 | labelsChanged || |
| 564 | displayNameChanged || |
| 565 | descriptionChanged || |
| 566 | dedicatedChanged || |
| 567 | trafficChanged || |
| 568 | clientChanged || |
| 569 | loggingChanged || |
| 570 | pscChanged || |
| 571 | genAiChanged |
| 572 | ) { |
| 573 | const updateMask = [ |
| 574 | labelsChanged ? "labels" : undefined, |
| 575 | displayNameChanged ? "display_name" : undefined, |
| 576 | descriptionChanged ? "description" : undefined, |
| 577 | dedicatedChanged ? "dedicated_endpoint_enabled" : undefined, |
| 578 | trafficChanged ? "traffic_split" : undefined, |
| 579 | clientChanged ? "client_connection_config" : undefined, |
| 580 | loggingChanged ? "predict_request_response_logging_config" : undefined, |
| 581 | pscChanged ? "private_service_connect_config" : undefined, |
| 582 | genAiChanged ? "gen_ai_advanced_features_config" : undefined, |
| 583 | ].filter((field): field is string => field !== undefined); |
| 584 | |
| 585 | current = yield* aiplatform |
| 586 | .patchProjectsLocationsEndpoints({ |
| 587 | name, |
| 588 | updateMask: updateMask.join(","), |
| 589 | body: { |
| 590 | name, |
| 591 | labels: desiredLabels, |
| 592 | displayName, |
| 593 | description: news.description, |
| 594 | dedicatedEndpointEnabled: news.dedicatedEndpointEnabled === true, |
| 595 | trafficSplit: news.trafficSplit, |
| 596 | clientConnectionConfig: news.clientConnectionConfig, |
| 597 | predictRequestResponseLoggingConfig: news.predictRequestResponseLoggingConfig, |
| 598 | privateServiceConnectConfig: news.privateServiceConnectConfig, |
| 599 | genAiAdvancedFeaturesConfig: news.genAiAdvancedFeaturesConfig, |
| 600 | }, |
| 601 | }) |
| 602 | .pipe( |
| 603 | Effect.retry({ |
| 604 | while: (error) => error._tag === "Conflict", |
| 605 | times: 8, |
| 606 | schedule: Schedule.spaced("5 seconds"), |
| 607 | }), |
| 608 | ); |
| 609 | } |
| 610 | |
| 611 | return toAttrs(current, env.project); |
| 612 | }), |
| 613 | |
| 614 | delete: Effect.fn(function* ({ output }) { |
| 615 | // Settle a still-pending create first: deleting an endpoint whose |
| 616 | // create hasn't finished reports NotFound and would leak it. |
| 617 | if ((yield* getSettled(output.name)) === undefined) return; |
| 618 | const operation = yield* aiplatform |
| 619 | .deleteProjectsLocationsEndpoints({ name: output.name }) |
| 620 | .pipe( |
| 621 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 622 | Effect.retry({ |
| 623 | while: (error) => error._tag === "Conflict", |
| 624 | times: 8, |
| 625 | schedule: Schedule.spaced("5 seconds"), |
| 626 | }), |
| 627 | ); |
| 628 | if (operation !== undefined) { |
| 629 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 630 | } |
| 631 | yield* waitUntilGone(output.name); |
| 632 | }), |
| 633 | }); |