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 * as Effect from "effect/Effect"; |
| 3 | import * as Stream from "effect/Stream"; |
| 4 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 5 | |
| 6 | export const resourceNameFromOperation = ( |
| 7 | operation: aiplatform.GoogleLongrunningOperation, |
| 8 | ): string | undefined => { |
| 9 | const response = operation.response; |
| 10 | const responseName = response?.name; |
| 11 | if (typeof responseName === "string" && responseName.length > 0) { |
| 12 | return responseName; |
| 13 | } |
| 14 | const metadata = operation.metadata; |
| 15 | const target = metadata?.target; |
| 16 | if (typeof target === "string" && target.length > 0) { |
| 17 | return target; |
| 18 | } |
| 19 | return undefined; |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Wait for a Vertex AI long-running operation and return the finished |
| 24 | * operation (so callers can read its `response`). Index deploys and |
| 25 | * feature-store provisioning take up to ~30 minutes. |
| 26 | * |
| 27 | * An operation that failed with ALREADY_EXISTS (6) counts as done (a lost |
| 28 | * create race). With `notFoundOk`, NOT_FOUND (5) — or an operation record |
| 29 | * that is gone — counts as done too (a lost delete race). |
| 30 | */ |
| 31 | export const waitForOperation = ( |
| 32 | operation: aiplatform.GoogleLongrunningOperation, |
| 33 | options?: { |
| 34 | notFoundOk?: boolean; |
| 35 | alreadyExistsOk?: boolean; |
| 36 | /** |
| 37 | * Operation getter for a service that the v1 API does not serve, such as |
| 38 | * AgentService (v1beta1 only). Defaults to v1 `getProjectsLocationsOperations`. |
| 39 | */ |
| 40 | get?: (name: string) => ReturnType<typeof aiplatform.getProjectsLocationsOperations>; |
| 41 | }, |
| 42 | ) => |
| 43 | Effect.gen(function* () { |
| 44 | const get = |
| 45 | options?.get ?? ((name: string) => aiplatform.getProjectsLocationsOperations({ name })); |
| 46 | const finished = yield* waitForGcpOperation(operation, get, { |
| 47 | budget: "30 minutes", |
| 48 | }).pipe( |
| 49 | Effect.as(true), |
| 50 | Effect.catchTags({ |
| 51 | "GCP.OperationFailed": (error) => |
| 52 | error.code === 6 || (options?.notFoundOk === true && error.code === 5) |
| 53 | ? Effect.succeed(false) |
| 54 | : Effect.fail(error), |
| 55 | NotFound: (error) => |
| 56 | options?.notFoundOk === true ? Effect.succeed(false) : Effect.fail(error), |
| 57 | }), |
| 58 | ); |
| 59 | if (!finished || operation.done === true || !operation.name) { |
| 60 | return operation; |
| 61 | } |
| 62 | return yield* get(operation.name); |
| 63 | }); |
| 64 | |
| 65 | /** |
| 66 | * Vertex AI hides a resource from `get` / `list` until its create |
| 67 | * operation finishes. On a freshly provisioned project that took 4–9 |
| 68 | * minutes for endpoints, indexes, index endpoints and deployment |
| 69 | * resource pools, so a deploy interrupted in that window leaves a |
| 70 | * resource that a plain lookup cannot see — the recovery `read` / |
| 71 | * `delete` would report it gone and leak it once the create completes. |
| 72 | * |
| 73 | * `settlePendingCreates` waits for every unfinished create operation on |
| 74 | * `{location}/{collection}` (optionally only `{collection}/{resourceId}`) |
| 75 | * and reports whether any were found, so callers re-run their lookup |
| 76 | * afterwards. It only waits — it never acts on resources it can't |
| 77 | * attribute — so it is safe for server-assigned ids, where the pending |
| 78 | * operation carries no ownership labels. |
| 79 | */ |
| 80 | export const settlePendingCreates = (options: { |
| 81 | /** `projects/{project}/locations/{location}` */ |
| 82 | parent: string; |
| 83 | /** Collection segment, e.g. `indexes`, `endpoints`. */ |
| 84 | collection: string; |
| 85 | /** Restrict to one resource id (deterministic-id resources). */ |
| 86 | resourceId?: string; |
| 87 | }) => |
| 88 | Effect.gen(function* () { |
| 89 | const prefix = |
| 90 | options.resourceId !== undefined |
| 91 | ? `${options.parent}/${options.collection}/${options.resourceId}/operations/` |
| 92 | : `${options.parent}/${options.collection}/`; |
| 93 | // Operation names use the project NUMBER while callers usually hold |
| 94 | // the project ID, so match on the location-relative suffix. |
| 95 | const relative = prefix.slice(prefix.indexOf("/locations/")); |
| 96 | const pending = yield* aiplatform.listProjectsLocationsOperations |
| 97 | .pages({ name: options.parent, filter: "done=false", pageSize: 100 }) |
| 98 | .pipe( |
| 99 | Stream.flatMap((page) => Stream.fromIterable(page.operations ?? [])), |
| 100 | Stream.filter((operation) => { |
| 101 | const name = operation.name ?? ""; |
| 102 | const rawType = operation.metadata?.["@type"]; |
| 103 | const type = typeof rawType === "string" ? rawType : ""; |
| 104 | return ( |
| 105 | operation.done !== true && |
| 106 | name.includes(relative) && |
| 107 | /\.Create[A-Za-z]*OperationMetadata$/.test(type) |
| 108 | ); |
| 109 | }), |
| 110 | Stream.runCollect, |
| 111 | Effect.map((chunk) => Array.from(chunk)), |
| 112 | Effect.catchTag("NotFound", () => |
| 113 | Effect.succeed([] as aiplatform.GoogleLongrunningOperation[]), |
| 114 | ), |
| 115 | ); |
| 116 | if (pending.length === 0) return false; |
| 117 | yield* Effect.logInfo( |
| 118 | `Waiting for ${pending.length} pending Vertex AI create operation(s) on ${options.collection} before looking up the resource`, |
| 119 | ); |
| 120 | yield* Effect.forEach( |
| 121 | pending, |
| 122 | (operation) => |
| 123 | waitForOperation(operation, { notFoundOk: true }).pipe( |
| 124 | // A failed create produced nothing to recover. |
| 125 | Effect.catchTag("GCP.OperationFailed", () => Effect.void), |
| 126 | Effect.asVoid, |
| 127 | ), |
| 128 | { concurrency: 4, discard: true }, |
| 129 | ); |
| 130 | return true; |
| 131 | }); |