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 apim from "@distilled.cloud/gcp/apim_v1alpha"; |
| 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 Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | ResourceFailed, |
| 12 | ResourceNotReady, |
| 13 | ResourceNotResolved, |
| 14 | expandObservationSource, |
| 15 | hasAlchemyId, |
| 16 | listAtLocation, |
| 17 | collectPages, |
| 18 | normalizeLocation, |
| 19 | parentOf, |
| 20 | parseName, |
| 21 | replaceOnIdentity, |
| 22 | resourceName, |
| 23 | sameStringList, |
| 24 | toPhysicalId, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | waitUntilReady, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | const COLLECTION = "observationJobs"; |
| 32 | const CREATED = new Set(["DISABLED", "ENABLED"]); |
| 33 | const ENABLED = new Set(["ENABLED"]); |
| 34 | const DISABLED = new Set(["DISABLED"]); |
| 35 | |
| 36 | export type ObservationJobProps = { |
| 37 | /** |
| 38 | * Observation job id (the `{observation_job}` segment of |
| 39 | * `projects/{project}/locations/{location}/observationJobs/{observation_job}`). |
| 40 | * Must be 4-63 lowercase letters, digits, or hyphens. If omitted, a |
| 41 | * unique `alch-` prefixed name is generated. Immutable — changing it |
| 42 | * replaces the job. |
| 43 | */ |
| 44 | observationJobId?: string; |
| 45 | /** |
| 46 | * Region of the job (`us-central1`, …). Immutable — changing it |
| 47 | * replaces the job. |
| 48 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 49 | */ |
| 50 | location?: string; |
| 51 | /** |
| 52 | * Observation source names |
| 53 | * (`projects/{project}/locations/{location}/observationSources/{source}`) |
| 54 | * or bare source ids in the same location. Sources must be the same |
| 55 | * kind. Immutable — changing them replaces the job. |
| 56 | */ |
| 57 | sources?: string[]; |
| 58 | /** |
| 59 | * When true, the job is enabled and collects observations. Synced in |
| 60 | * place via enable/disable. Jobs are created disabled. |
| 61 | * @default false |
| 62 | */ |
| 63 | enabled?: boolean; |
| 64 | }; |
| 65 | |
| 66 | export type ObservationJob = Resource< |
| 67 | "GCP.Apim.ObservationJob", |
| 68 | ObservationJobProps, |
| 69 | { |
| 70 | /** Full resource name. */ |
| 71 | name: string; |
| 72 | /** Observation job id (last path segment). */ |
| 73 | observationJobId: string; |
| 74 | /** Project id. */ |
| 75 | project: string; |
| 76 | /** Location id. */ |
| 77 | location: string; |
| 78 | /** Observation source resource names. */ |
| 79 | sources: string[]; |
| 80 | /** Whether the job is enabled. */ |
| 81 | enabled: boolean; |
| 82 | /** Server-reported state. */ |
| 83 | state: string | undefined; |
| 84 | /** RFC3339 creation timestamp. */ |
| 85 | createTime: string | undefined; |
| 86 | /** RFC3339 last-update timestamp. */ |
| 87 | updateTime: string | undefined; |
| 88 | }, |
| 89 | never, |
| 90 | Providers |
| 91 | >; |
| 92 | |
| 93 | /** |
| 94 | * An API Observation job — configuration that collects data about APIs |
| 95 | * from attached observation sources. Jobs have no labels, description, |
| 96 | * or patch API; Alchemy stamps ownership into generated ids (`alch-`) |
| 97 | * so `list` / nuke can find them. Identity, location, and sources |
| 98 | * replace the resource. `enabled` is synced in place. |
| 99 | * |
| 100 | * ### Creating an Observation Job |
| 101 | * **Example:** Disabled job with a source |
| 102 | * ```typescript |
| 103 | * const job = yield* GCP.Apim.ObservationJob("Shadow", { |
| 104 | * sources: [source.name], |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * **Example:** Enable collection |
| 109 | * ```typescript |
| 110 | * const job = yield* GCP.Apim.ObservationJob("Shadow", { |
| 111 | * sources: [source.name], |
| 112 | * enabled: true, |
| 113 | * }); |
| 114 | * ``` |
| 115 | * |
| 116 | * @resource |
| 117 | * @category Apim |
| 118 | */ |
| 119 | export const ObservationJob = Resource<ObservationJob>("GCP.Apim.ObservationJob"); |
| 120 | |
| 121 | const toAttrs = (location: string, item: apim.ObservationJob, project: string) => { |
| 122 | const name = item.name ?? ""; |
| 123 | const parsed = parseName(name, COLLECTION); |
| 124 | const state = item.state; |
| 125 | return { |
| 126 | name, |
| 127 | observationJobId: parsed.id, |
| 128 | project: parsed.project || project, |
| 129 | location: parsed.location || location, |
| 130 | sources: [...(item.sources ?? [])], |
| 131 | enabled: (state ?? "").toUpperCase() === "ENABLED", |
| 132 | state, |
| 133 | createTime: item.createTime, |
| 134 | updateTime: item.updateTime, |
| 135 | }; |
| 136 | }; |
| 137 | |
| 138 | const getByName = (name: string) => |
| 139 | name.length === 0 |
| 140 | ? Effect.undefined |
| 141 | : apim |
| 142 | .getProjectsLocationsObservationJobs({ name }) |
| 143 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 144 | |
| 145 | const listOwned = (project: string, region: string) => |
| 146 | listAtLocation(project, region, (parent) => |
| 147 | collectPages( |
| 148 | apim.listProjectsLocationsObservationJobs.pages({ |
| 149 | parent, |
| 150 | pageSize: 1000, |
| 151 | }), |
| 152 | (page) => page.observationJobs, |
| 153 | ), |
| 154 | ).pipe( |
| 155 | Effect.map((items) => |
| 156 | items.filter((item) => hasAlchemyId(parseName(item.name ?? "", COLLECTION).id)), |
| 157 | ), |
| 158 | ); |
| 159 | |
| 160 | const runAndWait = (operation: apim.Operation | undefined) => |
| 161 | Effect.gen(function* () { |
| 162 | if (operation !== undefined) { |
| 163 | yield* waitForOperation(operation); |
| 164 | } |
| 165 | }); |
| 166 | |
| 167 | export const ObservationJobProvider = () => |
| 168 | Provider.succeed(ObservationJob, { |
| 169 | stables: ["name", "observationJobId", "project", "location", "createTime"], |
| 170 | |
| 171 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 172 | if (!isResolved(news)) return undefined; |
| 173 | const env = yield* GcpEnvironment.current; |
| 174 | const previousSources = olds?.sources ?? output?.sources; |
| 175 | return replaceOnIdentity({ |
| 176 | previousId: olds?.observationJobId ?? output?.observationJobId, |
| 177 | nextId: news.observationJobId ?? olds?.observationJobId ?? output?.observationJobId, |
| 178 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 179 | nextLocation: normalizeLocation( |
| 180 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 181 | ), |
| 182 | extra: |
| 183 | previousSources !== undefined && |
| 184 | news.sources !== undefined && |
| 185 | !sameStringList(previousSources, news.sources), |
| 186 | }); |
| 187 | }), |
| 188 | |
| 189 | read: Effect.fn(function* ({ id, olds, output }) { |
| 190 | const env = yield* GcpEnvironment.current; |
| 191 | const observationJobId = yield* toPhysicalId( |
| 192 | id, |
| 193 | olds?.observationJobId, |
| 194 | output?.observationJobId, |
| 195 | "job", |
| 196 | ); |
| 197 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 198 | const name = |
| 199 | output?.name ?? resourceName(env.project, location, COLLECTION, observationJobId); |
| 200 | const existing = yield* getByName(name); |
| 201 | if (existing === undefined) return undefined; |
| 202 | const attrs = toAttrs(location, existing, env.project); |
| 203 | if (output !== undefined || hasAlchemyId(attrs.observationJobId)) { |
| 204 | return attrs; |
| 205 | } |
| 206 | return Unowned(attrs); |
| 207 | }), |
| 208 | |
| 209 | list: () => |
| 210 | Effect.gen(function* () { |
| 211 | const env = yield* GcpEnvironment.current; |
| 212 | const items = yield* listOwned(env.project, env.region); |
| 213 | return items.map((item) => toAttrs(env.region, item, env.project)); |
| 214 | }), |
| 215 | |
| 216 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 217 | const env = yield* GcpEnvironment.current; |
| 218 | const observationJobId = yield* toPhysicalId( |
| 219 | id, |
| 220 | news.observationJobId, |
| 221 | output?.observationJobId, |
| 222 | "job", |
| 223 | ); |
| 224 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 225 | const name = resourceName(env.project, location, COLLECTION, observationJobId); |
| 226 | const sources = (news.sources ?? []).map((source) => |
| 227 | expandObservationSource(source, env.project, location), |
| 228 | ); |
| 229 | const wantEnabled = news.enabled === true; |
| 230 | |
| 231 | let current = yield* getByName(output?.name ?? name); |
| 232 | |
| 233 | if (current === undefined) { |
| 234 | const created = yield* apim |
| 235 | .createProjectsLocationsObservationJobs({ |
| 236 | parent: parentOf(env.project, location), |
| 237 | observationJobId, |
| 238 | body: { |
| 239 | sources: sources.length > 0 ? sources : undefined, |
| 240 | }, |
| 241 | }) |
| 242 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 243 | if (created !== undefined) { |
| 244 | yield* waitForOperation(created); |
| 245 | } |
| 246 | current = yield* waitUntilExists(getByName(name), name); |
| 247 | } |
| 248 | |
| 249 | if (current === undefined) { |
| 250 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 251 | } |
| 252 | |
| 253 | const resource = current.name ?? name; |
| 254 | current = yield* waitUntilReady(getByName(resource), resource, (item) => item.state, CREATED); |
| 255 | |
| 256 | const state = (current.state ?? "").toUpperCase(); |
| 257 | if (wantEnabled && state !== "ENABLED") { |
| 258 | const operation = yield* apim |
| 259 | .enableProjectsLocationsObservationJobs({ |
| 260 | name: resource, |
| 261 | body: {}, |
| 262 | }) |
| 263 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 264 | yield* runAndWait(operation); |
| 265 | current = yield* waitUntilReady( |
| 266 | getByName(resource), |
| 267 | resource, |
| 268 | (item) => item.state, |
| 269 | ENABLED, |
| 270 | ); |
| 271 | } else if (!wantEnabled && state === "ENABLED") { |
| 272 | const operation = yield* apim |
| 273 | .disableProjectsLocationsObservationJobs({ |
| 274 | name: resource, |
| 275 | body: {}, |
| 276 | }) |
| 277 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 278 | yield* runAndWait(operation); |
| 279 | current = yield* waitUntilReady( |
| 280 | getByName(resource), |
| 281 | resource, |
| 282 | (item) => item.state, |
| 283 | DISABLED, |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | return toAttrs(location, current, env.project); |
| 288 | }), |
| 289 | |
| 290 | delete: Effect.fn(function* ({ output }) { |
| 291 | const existing = yield* getByName(output.name); |
| 292 | const state = (existing?.state ?? "").toUpperCase(); |
| 293 | if (existing !== undefined && (state === "ENABLED" || state === "ENABLING")) { |
| 294 | const disable = yield* apim |
| 295 | .disableProjectsLocationsObservationJobs({ |
| 296 | name: output.name, |
| 297 | body: {}, |
| 298 | }) |
| 299 | .pipe(Effect.catchTag(["NotFound", "Conflict"], () => Effect.undefined)); |
| 300 | if (disable !== undefined) { |
| 301 | yield* waitForOperation(disable, { notFoundOk: true }); |
| 302 | } |
| 303 | yield* waitUntilReady( |
| 304 | getByName(output.name), |
| 305 | output.name, |
| 306 | (item) => item.state, |
| 307 | DISABLED, |
| 308 | ).pipe( |
| 309 | Effect.catchIf( |
| 310 | (error) => |
| 311 | error instanceof ResourceNotResolved || |
| 312 | error instanceof ResourceNotReady || |
| 313 | error instanceof ResourceFailed, |
| 314 | () => Effect.void, |
| 315 | ), |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | const operation = yield* apim |
| 320 | .deleteProjectsLocationsObservationJobs({ name: output.name }) |
| 321 | .pipe( |
| 322 | Effect.retry({ |
| 323 | while: (error) => error._tag === "Conflict", |
| 324 | times: 8, |
| 325 | schedule: Schedule.spaced("2 seconds"), |
| 326 | }), |
| 327 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 328 | ); |
| 329 | if (operation !== undefined) { |
| 330 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 331 | } |
| 332 | yield* waitUntilGone(getByName(output.name), output.name); |
| 333 | }), |
| 334 | }); |