The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ces from "@distilled.cloud/gcp/ces_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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | collectPages, |
| 14 | encodeOwnership, |
| 15 | expandApp, |
| 16 | forEachApp, |
| 17 | hasOwnershipMarker, |
| 18 | normalizeLocation, |
| 19 | ownedByAlchemy, |
| 20 | parseOwnership, |
| 21 | parseResourceName, |
| 22 | replaceOnIdentity, |
| 23 | retryTransient, |
| 24 | toPhysicalId, |
| 25 | waitUntilGone, |
| 26 | } from "./internal.ts"; |
| 27 | |
| 28 | export type AppsVersionProps = { |
| 29 | /** |
| 30 | * Parent CES app. Full name |
| 31 | * `projects/{project}/locations/{location}/apps/{app}` or the app id |
| 32 | * (combined with `location`). Immutable — changing it replaces the |
| 33 | * version. |
| 34 | */ |
| 35 | app: string; |
| 36 | /** |
| 37 | * Region used when `app` is a bare id. |
| 38 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 39 | */ |
| 40 | location?: string; |
| 41 | /** |
| 42 | * App version id. If omitted, a unique name is generated. Immutable — |
| 43 | * changing it replaces the version. |
| 44 | */ |
| 45 | appVersionId?: string; |
| 46 | /** |
| 47 | * Human-readable name. Alchemy falls back to the generated version |
| 48 | * id. |
| 49 | */ |
| 50 | displayName?: string; |
| 51 | /** |
| 52 | * Snapshot description. Versions have no labels field and no update |
| 53 | * RPC, so Alchemy stamps ownership into this field at create time. |
| 54 | * Later description edits are ignored. |
| 55 | */ |
| 56 | description?: string; |
| 57 | }; |
| 58 | |
| 59 | export type AppsVersion = Resource< |
| 60 | "GCP.CES.AppsVersion", |
| 61 | AppsVersionProps, |
| 62 | { |
| 63 | /** Full resource name `.../apps/{app}/versions/{version}`. */ |
| 64 | name: string; |
| 65 | /** Version id (last path segment). */ |
| 66 | appVersionId: string; |
| 67 | /** Parent app resource name. */ |
| 68 | app: string; |
| 69 | /** Location id. */ |
| 70 | location: string; |
| 71 | /** Project id. */ |
| 72 | project: string; |
| 73 | /** Display name. */ |
| 74 | displayName: string | undefined; |
| 75 | /** User description with the Alchemy ownership prefix stripped. */ |
| 76 | description: string | undefined; |
| 77 | /** Email of the user who created the version. */ |
| 78 | creator: string | undefined; |
| 79 | /** RFC3339 creation timestamp. */ |
| 80 | createTime: string | undefined; |
| 81 | /** Server-assigned etag. */ |
| 82 | etag: string | undefined; |
| 83 | }, |
| 84 | never, |
| 85 | Providers |
| 86 | >; |
| 87 | |
| 88 | /** |
| 89 | * A Customer Engagement Suite app version — an immutable snapshot of an |
| 90 | * app at a point in time. |
| 91 | * |
| 92 | * Versions have no labels field and no patch RPC. Alchemy stamps |
| 93 | * ownership into `description` so `list` / nuke can find them. Reconcile |
| 94 | * is observe-ensure: if the snapshot is missing it is created; later |
| 95 | * description edits are ignored. |
| 96 | * |
| 97 | * ### Creating an App Version |
| 98 | * **Example:** Snapshot |
| 99 | * ```typescript |
| 100 | * const version = yield* GCP.CES.AppsVersion("v1", { |
| 101 | * app: app.name, |
| 102 | * displayName: "v1", |
| 103 | * description: "initial", |
| 104 | * }); |
| 105 | * ``` |
| 106 | * |
| 107 | * @resource |
| 108 | * @category CES |
| 109 | */ |
| 110 | export const AppsVersion = Resource<AppsVersion>("GCP.CES.AppsVersion"); |
| 111 | |
| 112 | export class AppsVersionNotResolved extends Data.TaggedError("GCP.CES.AppsVersionNotResolved")<{ |
| 113 | resourceName: string; |
| 114 | }> {} |
| 115 | |
| 116 | const resourceName = (app: string, appVersionId: string) => `${app}/versions/${appVersionId}`; |
| 117 | |
| 118 | const toAttrs = (version: ces.AppVersion, project: string, region: string, appHint?: string) => { |
| 119 | const name = version.name ?? ""; |
| 120 | const parsed = parseResourceName(name, "versions", region); |
| 121 | return { |
| 122 | name, |
| 123 | appVersionId: parsed.id, |
| 124 | app: name.includes("/versions/") ? parsed.app : (appHint ?? parsed.parent), |
| 125 | location: parsed.location, |
| 126 | project: parsed.project || project, |
| 127 | displayName: version.displayName, |
| 128 | description: parseOwnership(version.description).text, |
| 129 | creator: version.creator, |
| 130 | createTime: version.createTime, |
| 131 | etag: version.etag, |
| 132 | }; |
| 133 | }; |
| 134 | |
| 135 | const getByName = (name: string) => |
| 136 | name.length === 0 |
| 137 | ? Effect.undefined |
| 138 | : ces |
| 139 | .getProjectsLocationsAppsVersions({ name }) |
| 140 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 141 | |
| 142 | const listAt = (parent: string, project: string, region: string) => |
| 143 | collectPages( |
| 144 | ces.listProjectsLocationsAppsVersions.pages({ parent, pageSize: 100 }), |
| 145 | (page) => page.appVersions, |
| 146 | ).pipe( |
| 147 | Effect.map((versions) => |
| 148 | versions |
| 149 | .filter((version) => hasOwnershipMarker(version.description)) |
| 150 | .map((version) => toAttrs(version, project, region, parent)), |
| 151 | ), |
| 152 | ); |
| 153 | |
| 154 | export const AppsVersionProvider = () => |
| 155 | Provider.succeed(AppsVersion, { |
| 156 | stables: ["name", "appVersionId", "app", "location", "project", "createTime", "creator"], |
| 157 | |
| 158 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 159 | if (!isResolved(news)) return undefined; |
| 160 | return replaceOnIdentity({ |
| 161 | previousId: olds?.appVersionId ?? output?.appVersionId, |
| 162 | nextId: news.appVersionId, |
| 163 | previousParent: olds?.app ?? output?.app, |
| 164 | nextParent: news.app, |
| 165 | }); |
| 166 | }), |
| 167 | |
| 168 | read: Effect.fn(function* ({ id, olds, output }) { |
| 169 | const env = yield* GcpEnvironment.current; |
| 170 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 171 | const app = olds?.app ? expandApp(olds.app, env.project, location) : output?.app; |
| 172 | const appVersionId = yield* toPhysicalId(id, olds?.appVersionId, output?.appVersionId); |
| 173 | const name = output?.name ?? (app !== undefined ? resourceName(app, appVersionId) : ""); |
| 174 | const existing = yield* getByName(name); |
| 175 | if (existing === undefined) return undefined; |
| 176 | const attrs = toAttrs(existing, env.project, env.region, app); |
| 177 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 178 | }), |
| 179 | |
| 180 | list: () => |
| 181 | Effect.gen(function* () { |
| 182 | const env = yield* GcpEnvironment.current; |
| 183 | return yield* forEachApp( |
| 184 | env.project, |
| 185 | (parent) => listAt(parent, env.project, env.region), |
| 186 | env.region, |
| 187 | ); |
| 188 | }), |
| 189 | |
| 190 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 191 | const env = yield* GcpEnvironment.current; |
| 192 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 193 | const app = expandApp(news.app, env.project, location); |
| 194 | const appVersionId = yield* toPhysicalId(id, news.appVersionId, output?.appVersionId); |
| 195 | const name = output?.name ?? resourceName(app, appVersionId); |
| 196 | const ownership = yield* createInternalLabels(id); |
| 197 | const description = encodeOwnership(ownership, news.description); |
| 198 | const displayName = news.displayName ?? appVersionId; |
| 199 | |
| 200 | let current = yield* getByName(name); |
| 201 | |
| 202 | if (current === undefined) { |
| 203 | const created = yield* retryTransient( |
| 204 | ces.createProjectsLocationsAppsVersions({ |
| 205 | parent: app, |
| 206 | appVersionId, |
| 207 | body: { |
| 208 | displayName, |
| 209 | description, |
| 210 | }, |
| 211 | }), |
| 212 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 213 | current = created ?? undefined; |
| 214 | } |
| 215 | |
| 216 | if (current === undefined) { |
| 217 | return yield* new AppsVersionNotResolved({ resourceName: name }); |
| 218 | } |
| 219 | |
| 220 | return toAttrs(current, env.project, env.region, app); |
| 221 | }), |
| 222 | |
| 223 | delete: Effect.fn(function* ({ output }) { |
| 224 | if (!output.name) return; |
| 225 | yield* retryTransient(ces.deleteProjectsLocationsAppsVersions({ name: output.name })).pipe( |
| 226 | // A deployment deleted just before still references the version. |
| 227 | Effect.retry({ |
| 228 | while: (error) => error._tag === "AppVersionInUse", |
| 229 | schedule: Schedule.spaced("3 seconds"), |
| 230 | times: 20, |
| 231 | }), |
| 232 | Effect.catchTag("NotFound", () => Effect.void), |
| 233 | ); |
| 234 | yield* waitUntilGone(getByName(output.name)); |
| 235 | }), |
| 236 | }); |