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 logging from "@distilled.cloud/gcp/logging_v2"; |
| 2 | import { unownedUnlessPrevious } 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 * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | createOwnership, |
| 14 | encodeDescription, |
| 15 | hasOwnershipMarker, |
| 16 | lastSegment, |
| 17 | ownedBy, |
| 18 | parseDescription, |
| 19 | parseLoggingName, |
| 20 | toLinkId, |
| 21 | } from "./internal.ts"; |
| 22 | |
| 23 | export type FolderBucketsLinkProps = { |
| 24 | /** |
| 25 | * Full resource name of the parent analytics-enabled log bucket |
| 26 | * (`{parent}/locations/{location}/buckets/{bucket}`). A bucket may |
| 27 | * contain only one link. Immutable — changing it replaces the link |
| 28 | * (delete first). |
| 29 | */ |
| 30 | bucketName: string; |
| 31 | /** |
| 32 | * Link id (also the BigQuery dataset id). If omitted, a unique name is |
| 33 | * generated. Up to 100 characters: letters, digits, and underscores. |
| 34 | * Immutable — changing it replaces the link (delete first). |
| 35 | */ |
| 36 | linkId?: string; |
| 37 | /** |
| 38 | * Human-readable description. Links have no labels field, so Alchemy |
| 39 | * ownership is stored in a `[alchemy …]` prefix and stripped from |
| 40 | * attributes. Changing the description replaces the link. |
| 41 | */ |
| 42 | description?: string; |
| 43 | }; |
| 44 | |
| 45 | export type FolderBucketsLink = Resource< |
| 46 | "GCP.Logging.FolderBucketsLink", |
| 47 | FolderBucketsLinkProps, |
| 48 | { |
| 49 | /** Full resource name `{bucket}/links/{linkId}`. */ |
| 50 | name: string; |
| 51 | /** Link id (last path segment). */ |
| 52 | linkId: string; |
| 53 | /** Parent bucket resource name. */ |
| 54 | bucketName: string; |
| 55 | /** User description with the Alchemy ownership prefix stripped. */ |
| 56 | description: string | undefined; |
| 57 | /** BigQuery dataset resource name, if created. */ |
| 58 | bigqueryDatasetId: string | undefined; |
| 59 | /** Link lifecycle (`ACTIVE`, `CREATING`, …). */ |
| 60 | lifecycleState: string | undefined; |
| 61 | /** RFC3339 creation timestamp. */ |
| 62 | createTime: string | undefined; |
| 63 | }, |
| 64 | never, |
| 65 | Providers |
| 66 | >; |
| 67 | |
| 68 | /** |
| 69 | * A BigQuery linked dataset for an analytics-enabled folder log bucket. |
| 70 | * |
| 71 | * Create is asynchronous and also provisions a BigQuery dataset whose id |
| 72 | * matches `linkId`. A bucket may currently contain only one link. Links |
| 73 | * have no labels field, so Alchemy stamps ownership into the description |
| 74 | * for `list` / nuke. There is no update API — changing `linkId`, |
| 75 | * `bucketName`, or `description` replaces the link (delete first). |
| 76 | * |
| 77 | * ### Creating a Folder Bucket Link |
| 78 | * **Example:** Link an analytics bucket to BigQuery |
| 79 | * ```typescript |
| 80 | * const bucket = yield* GCP.Logging.FolderBucket("Analytics", { |
| 81 | * analyticsEnabled: true, |
| 82 | * }); |
| 83 | * const link = yield* GCP.Logging.FolderBucketsLink("Bq", { |
| 84 | * bucketName: bucket.name, |
| 85 | * description: "log analytics dataset", |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * @resource |
| 90 | * @category Logging |
| 91 | */ |
| 92 | export const FolderBucketsLink = Resource<FolderBucketsLink>("GCP.Logging.FolderBucketsLink"); |
| 93 | |
| 94 | export class FolderBucketsLinkNotResolved extends Data.TaggedError( |
| 95 | "GCP.Logging.FolderBucketsLinkNotResolved", |
| 96 | )<{ |
| 97 | resourceName: string; |
| 98 | }> {} |
| 99 | |
| 100 | export class FolderBucketsLinkFailed extends Data.TaggedError( |
| 101 | "GCP.Logging.FolderBucketsLinkFailed", |
| 102 | )<{ |
| 103 | resourceName: string; |
| 104 | state: string | undefined; |
| 105 | }> {} |
| 106 | |
| 107 | const resourceName = (bucketName: string, linkId: string) => `${bucketName}/links/${linkId}`; |
| 108 | |
| 109 | const isDeleted = (link: logging.Link | undefined): link is undefined => |
| 110 | link === undefined || link.lifecycleState === "DELETE_REQUESTED"; |
| 111 | |
| 112 | const isPending = (state: string | undefined) => state === "CREATING" || state === "UPDATING"; |
| 113 | |
| 114 | const toAttrs = (link: logging.Link, bucketName: string) => { |
| 115 | const parsed = parseLoggingName(link.name ?? ""); |
| 116 | const linkId = parsed.linkId ?? lastSegment(link.name ?? ""); |
| 117 | const description = parseDescription(link.description); |
| 118 | return { |
| 119 | name: link.name ?? resourceName(bucketName, linkId), |
| 120 | linkId, |
| 121 | bucketName, |
| 122 | description: description.description, |
| 123 | bigqueryDatasetId: link.bigqueryDataset?.datasetId, |
| 124 | lifecycleState: link.lifecycleState, |
| 125 | createTime: link.createTime, |
| 126 | }; |
| 127 | }; |
| 128 | |
| 129 | const getByName = (name: string) => |
| 130 | logging |
| 131 | .getFoldersLocationsBucketsLinks({ name }) |
| 132 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 133 | |
| 134 | const waitUntilActive = (name: string) => |
| 135 | Effect.gen(function* () { |
| 136 | const link = yield* getByName(name); |
| 137 | if (link === undefined || isDeleted(link)) { |
| 138 | return yield* new FolderBucketsLinkNotResolved({ resourceName: name }); |
| 139 | } |
| 140 | if (link.lifecycleState === "FAILED") { |
| 141 | return yield* new FolderBucketsLinkFailed({ |
| 142 | resourceName: name, |
| 143 | state: link.lifecycleState, |
| 144 | }); |
| 145 | } |
| 146 | if (isPending(link.lifecycleState)) { |
| 147 | return yield* new FolderBucketsLinkNotResolved({ resourceName: name }); |
| 148 | } |
| 149 | return link; |
| 150 | }).pipe( |
| 151 | Effect.retry({ |
| 152 | while: (error) => error._tag === "GCP.Logging.FolderBucketsLinkNotResolved", |
| 153 | times: 10, |
| 154 | schedule: Schedule.spaced("5 seconds"), |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | export const FolderBucketsLinkProvider = () => |
| 159 | Provider.succeed(FolderBucketsLink, { |
| 160 | nuke: { |
| 161 | dependsOn: ["GCP.Logging.LogBucket"], |
| 162 | }, |
| 163 | stables: ["name", "linkId", "bucketName", "createTime"], |
| 164 | |
| 165 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 166 | if (!isResolved(news)) return undefined; |
| 167 | const previousId = olds?.linkId ?? output?.linkId; |
| 168 | const idChanged = |
| 169 | previousId !== undefined && news.linkId !== undefined && news.linkId !== previousId; |
| 170 | const previousBucket = olds?.bucketName ?? output?.bucketName; |
| 171 | const bucketChanged = previousBucket !== undefined && news.bucketName !== previousBucket; |
| 172 | const previousDescription = olds?.description ?? output?.description; |
| 173 | const descriptionChanged = |
| 174 | news.description !== undefined && |
| 175 | previousDescription !== undefined && |
| 176 | news.description !== previousDescription; |
| 177 | if (!idChanged && !bucketChanged && !descriptionChanged) { |
| 178 | return undefined; |
| 179 | } |
| 180 | return { action: "replace" as const, deleteFirst: true }; |
| 181 | }), |
| 182 | |
| 183 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 184 | const linkId = yield* toLinkId(id, olds?.linkId, output?.linkId); |
| 185 | const bucketName = olds?.bucketName ?? output?.bucketName; |
| 186 | if (bucketName === undefined) return undefined; |
| 187 | const name = output?.name ?? resourceName(bucketName, linkId); |
| 188 | const existing = yield* getByName(name); |
| 189 | if (isDeleted(existing)) return undefined; |
| 190 | const attrs = toAttrs(existing, bucketName); |
| 191 | const { labels } = parseDescription(existing.description); |
| 192 | return (yield* ownedBy(id, labels)) ? attrs : unownedUnlessPrevious(attrs, previous, "name"); |
| 193 | }), |
| 194 | |
| 195 | list: () => |
| 196 | Effect.gen(function* () { |
| 197 | const env = yield* GcpEnvironment.current; |
| 198 | const buckets = yield* logging.listFoldersLocationsBuckets |
| 199 | .pages({ |
| 200 | parent: `projects/${env.project}/locations/-`, |
| 201 | pageSize: 1000, |
| 202 | }) |
| 203 | .pipe( |
| 204 | Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])), |
| 205 | Stream.filter((bucket) => bucket.analyticsEnabled === true), |
| 206 | Stream.runCollect, |
| 207 | Effect.map((chunk) => Array.from(chunk)), |
| 208 | ); |
| 209 | const links: ReturnType<typeof toAttrs>[] = []; |
| 210 | for (const bucket of buckets) { |
| 211 | if (bucket.name === undefined) continue; |
| 212 | const listed = yield* logging.listFoldersLocationsBucketsLinks |
| 213 | .pages({ |
| 214 | parent: bucket.name, |
| 215 | pageSize: 1000, |
| 216 | }) |
| 217 | .pipe( |
| 218 | Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])), |
| 219 | Stream.filter((link) => !isDeleted(link) && hasOwnershipMarker(link.description)), |
| 220 | Stream.map((link) => toAttrs(link, bucket.name ?? "")), |
| 221 | Stream.runCollect, |
| 222 | Effect.map((chunk) => Array.from(chunk)), |
| 223 | Effect.catchTag("NotFound", () => Effect.succeed([] as ReturnType<typeof toAttrs>[])), |
| 224 | ); |
| 225 | links.push(...listed); |
| 226 | } |
| 227 | return links; |
| 228 | }), |
| 229 | |
| 230 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 231 | const linkId = yield* toLinkId(id, news.linkId, output?.linkId); |
| 232 | const name = resourceName(news.bucketName, linkId); |
| 233 | const ownership = yield* createOwnership(id); |
| 234 | const desiredDescription = encodeDescription(ownership, news.description); |
| 235 | |
| 236 | let current = yield* getByName(output?.name ?? name); |
| 237 | |
| 238 | if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") { |
| 239 | current = undefined; |
| 240 | } |
| 241 | |
| 242 | if (current === undefined) { |
| 243 | yield* logging |
| 244 | .createFoldersLocationsBucketsLinks({ |
| 245 | parent: news.bucketName, |
| 246 | linkId, |
| 247 | body: { description: desiredDescription }, |
| 248 | }) |
| 249 | .pipe( |
| 250 | Effect.catchTag("Conflict", () => Effect.succeed<logging.Operation>({ done: true })), |
| 251 | Effect.asVoid, |
| 252 | ); |
| 253 | current = yield* waitUntilActive(name); |
| 254 | } |
| 255 | |
| 256 | if (isDeleted(current)) { |
| 257 | return yield* new FolderBucketsLinkNotResolved({ resourceName: name }); |
| 258 | } |
| 259 | |
| 260 | if (isPending(current.lifecycleState)) { |
| 261 | current = yield* waitUntilActive(current.name ?? name); |
| 262 | } |
| 263 | |
| 264 | return toAttrs(current, news.bucketName); |
| 265 | }), |
| 266 | |
| 267 | delete: Effect.fn(function* ({ output }) { |
| 268 | if (output.lifecycleState === "DELETE_REQUESTED") return; |
| 269 | yield* logging.deleteFoldersLocationsBucketsLinks({ name: output.name }).pipe( |
| 270 | Effect.catchTag("NotFound", () => Effect.void), |
| 271 | Effect.asVoid, |
| 272 | ); |
| 273 | yield* getByName(output.name).pipe( |
| 274 | Effect.flatMap((link) => |
| 275 | isDeleted(link) |
| 276 | ? Effect.void |
| 277 | : Effect.fail(new FolderBucketsLinkNotResolved({ resourceName: output.name })), |
| 278 | ), |
| 279 | Effect.retry({ |
| 280 | while: (error) => error._tag === "GCP.Logging.FolderBucketsLinkNotResolved", |
| 281 | times: 10, |
| 282 | schedule: Schedule.spaced("3 seconds"), |
| 283 | }), |
| 284 | ); |
| 285 | }), |
| 286 | }); |