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 pubsublite from "@distilled.cloud/gcp/pubsublite_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 Effect from "effect/Effect"; |
| 7 | import { GcpEnvironment } from "../Environment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | DEFAULT_PARTITION_COUNT, |
| 11 | DEFAULT_PER_PARTITION_BYTES, |
| 12 | DEFAULT_PUBLISH_MIB, |
| 13 | DEFAULT_SUBSCRIBE_MIB, |
| 14 | DEFAULT_ZONE, |
| 15 | ResourceNotResolved, |
| 16 | countOf, |
| 17 | expandName, |
| 18 | fieldMask, |
| 19 | getTopic, |
| 20 | hasOwnershipMarker, |
| 21 | ignoreMissing, |
| 22 | listOwnedTopics, |
| 23 | normalizeLocation, |
| 24 | ownedByAlchemy, |
| 25 | parentOf, |
| 26 | parseName, |
| 27 | partitionBody, |
| 28 | partitionKey, |
| 29 | regionOf, |
| 30 | replaceOnIdentity, |
| 31 | resourceName, |
| 32 | retentionBody, |
| 33 | retentionKey, |
| 34 | retryInUse, |
| 35 | sameRef, |
| 36 | toResourceId, |
| 37 | waitUntilGone, |
| 38 | } from "./internal.ts"; |
| 39 | |
| 40 | export type TopicPartitionCapacity = { |
| 41 | /** |
| 42 | * Publish throughput per partition in MiB/s. Must be between 4 and 16. |
| 43 | * @default 4 |
| 44 | */ |
| 45 | publishMibPerSec?: number; |
| 46 | /** |
| 47 | * Subscribe throughput per partition in MiB/s. Must be between 4 and 32. |
| 48 | * @default 4 |
| 49 | */ |
| 50 | subscribeMibPerSec?: number; |
| 51 | }; |
| 52 | |
| 53 | export type TopicPartitionConfig = { |
| 54 | /** |
| 55 | * Number of partitions. Must be at least 1. After create, the count |
| 56 | * can increase but not decrease — shrinking replaces the topic. |
| 57 | * @default 1 |
| 58 | */ |
| 59 | count?: number | string; |
| 60 | /** |
| 61 | * Per-partition throughput. Required unless the topic uses a |
| 62 | * reservation that supplies capacity. |
| 63 | */ |
| 64 | capacity?: TopicPartitionCapacity; |
| 65 | }; |
| 66 | |
| 67 | export type TopicRetentionConfig = { |
| 68 | /** |
| 69 | * Provisioned storage per partition, in bytes. The API minimum is |
| 70 | * 30 GiB (`32212254720`). Older messages are dropped when a partition |
| 71 | * exceeds this size, regardless of `period`. |
| 72 | * @default "32212254720" |
| 73 | */ |
| 74 | perPartitionBytes?: string; |
| 75 | /** |
| 76 | * How long a published message is retained (e.g. `"86400s"`). If |
| 77 | * omitted, messages are retained until `perPartitionBytes` is |
| 78 | * exhausted. |
| 79 | */ |
| 80 | period?: string; |
| 81 | }; |
| 82 | |
| 83 | export type TopicReservationConfig = { |
| 84 | /** |
| 85 | * Reservation that supplies this topic's throughput, as a full |
| 86 | * resource name or reservation id. Regional topics require a |
| 87 | * reservation in the same region. |
| 88 | */ |
| 89 | throughputReservation?: string; |
| 90 | }; |
| 91 | |
| 92 | export type AdminTopicProps = { |
| 93 | /** |
| 94 | * Topic id (the `{topic}` segment of |
| 95 | * `projects/{project}/locations/{location}/topics/{topic}`). If |
| 96 | * omitted, a unique name is generated from the stack, stage, and |
| 97 | * logical id. Pub/Sub Lite has no labels field, so Alchemy stamps |
| 98 | * ownership into a `+alc.{stack}.{stage}.{id}` suffix. Immutable — |
| 99 | * changing it replaces the topic. |
| 100 | */ |
| 101 | topicId?: string; |
| 102 | /** |
| 103 | * Zone (`us-central1-a`) or region (`us-central1`). Zonal topics can |
| 104 | * provision their own partition capacity. Regional topics must attach |
| 105 | * a reservation. Immutable — changing it replaces the topic. |
| 106 | * @default "us-central1-a" |
| 107 | */ |
| 108 | location?: string; |
| 109 | /** |
| 110 | * Partition count and per-partition throughput. |
| 111 | */ |
| 112 | partitionConfig?: TopicPartitionConfig; |
| 113 | /** |
| 114 | * Message retention. `perPartitionBytes` defaults to 30 GiB. |
| 115 | */ |
| 116 | retentionConfig?: TopicRetentionConfig; |
| 117 | /** |
| 118 | * Optional reservation that this topic draws throughput from. |
| 119 | */ |
| 120 | reservationConfig?: TopicReservationConfig; |
| 121 | }; |
| 122 | |
| 123 | export type AdminTopic = Resource< |
| 124 | "GCP.PubSubLite.AdminTopic", |
| 125 | AdminTopicProps, |
| 126 | { |
| 127 | /** Full resource name `projects/{project}/locations/{location}/topics/{topic}`. */ |
| 128 | name: string; |
| 129 | /** Topic id (last path segment, including the Alchemy ownership suffix). */ |
| 130 | topicId: string; |
| 131 | /** Project id. */ |
| 132 | project: string; |
| 133 | /** Zone or region id. */ |
| 134 | location: string; |
| 135 | /** Partition configuration currently applied. */ |
| 136 | partitionConfig: pubsublite.PartitionConfig | undefined; |
| 137 | /** Retention configuration currently applied. */ |
| 138 | retentionConfig: pubsublite.RetentionConfig | undefined; |
| 139 | /** Reservation configuration currently applied. */ |
| 140 | reservationConfig: pubsublite.ReservationConfig | undefined; |
| 141 | }, |
| 142 | never, |
| 143 | Providers |
| 144 | >; |
| 145 | |
| 146 | /** |
| 147 | * A Pub/Sub Lite topic — a zonal or regional partitioned log. |
| 148 | * |
| 149 | * Pub/Sub Lite topics have no labels. Alchemy stamps |
| 150 | * `alchemy-stack` / `alchemy-stage` / `alchemy-id` into the topic id so |
| 151 | * `list` / `pnpm nuke:gcp` can find them. `topicId` and `location` |
| 152 | * replace the topic. Partition count can only increase in place; |
| 153 | * shrinking it replaces the topic. Capacity, retention, and reservation |
| 154 | * attachment update in place. |
| 155 | * |
| 156 | * ### Creating a Topic |
| 157 | * **Example:** Zonal topic with generated name |
| 158 | * ```typescript |
| 159 | * const topic = yield* GCP.PubSubLite.AdminTopic("Events", {}); |
| 160 | * ``` |
| 161 | * |
| 162 | * **Example:** Explicit capacity and retention |
| 163 | * ```typescript |
| 164 | * const topic = yield* GCP.PubSubLite.AdminTopic("Events", { |
| 165 | * location: "us-central1-a", |
| 166 | * partitionConfig: { |
| 167 | * count: 1, |
| 168 | * capacity: { publishMibPerSec: 4, subscribeMibPerSec: 8 }, |
| 169 | * }, |
| 170 | * retentionConfig: { perPartitionBytes: "32212254720", period: "86400s" }, |
| 171 | * }); |
| 172 | * ``` |
| 173 | * |
| 174 | * ### Using a Reservation |
| 175 | * **Example:** Regional topic attached to a reservation |
| 176 | * ```typescript |
| 177 | * const reservation = yield* GCP.PubSubLite.AdminReservation("Capacity", { |
| 178 | * location: "us-central1", |
| 179 | * throughputCapacity: "4", |
| 180 | * }); |
| 181 | * const topic = yield* GCP.PubSubLite.AdminTopic("Events", { |
| 182 | * location: "us-central1", |
| 183 | * reservationConfig: { throughputReservation: reservation.name }, |
| 184 | * }); |
| 185 | * ``` |
| 186 | * |
| 187 | * @resource |
| 188 | * @category PubSubLite |
| 189 | */ |
| 190 | export const AdminTopic = Resource<AdminTopic>("GCP.PubSubLite.AdminTopic"); |
| 191 | |
| 192 | const COLLECTION = "reservations"; |
| 193 | const TOPIC_COLLECTION = "topics"; |
| 194 | |
| 195 | const desiredPartition = (news: AdminTopicProps): pubsublite.PartitionConfig => |
| 196 | partitionBody({ |
| 197 | count: news.partitionConfig?.count ?? DEFAULT_PARTITION_COUNT, |
| 198 | capacity: { |
| 199 | publishMibPerSec: news.partitionConfig?.capacity?.publishMibPerSec ?? DEFAULT_PUBLISH_MIB, |
| 200 | subscribeMibPerSec: |
| 201 | news.partitionConfig?.capacity?.subscribeMibPerSec ?? DEFAULT_SUBSCRIBE_MIB, |
| 202 | }, |
| 203 | }); |
| 204 | |
| 205 | const desiredRetention = (news: AdminTopicProps): pubsublite.RetentionConfig => |
| 206 | retentionBody({ |
| 207 | perPartitionBytes: news.retentionConfig?.perPartitionBytes ?? DEFAULT_PER_PARTITION_BYTES, |
| 208 | period: news.retentionConfig?.period, |
| 209 | }); |
| 210 | |
| 211 | const desiredReservation = ( |
| 212 | news: AdminTopicProps, |
| 213 | project: string, |
| 214 | location: string, |
| 215 | ): pubsublite.ReservationConfig | undefined => { |
| 216 | const reservation = news.reservationConfig?.throughputReservation; |
| 217 | if (reservation === undefined || reservation.length === 0) { |
| 218 | return news.reservationConfig === undefined ? undefined : { throughputReservation: undefined }; |
| 219 | } |
| 220 | return { |
| 221 | throughputReservation: expandName(reservation, project, regionOf(location), COLLECTION), |
| 222 | }; |
| 223 | }; |
| 224 | |
| 225 | const toAttrs = (topic: pubsublite.Topic, project: string): AdminTopic["Attributes"] => { |
| 226 | const name = topic.name ?? ""; |
| 227 | const parsed = parseName(name, TOPIC_COLLECTION); |
| 228 | return { |
| 229 | name, |
| 230 | topicId: parsed.id, |
| 231 | project: parsed.project || project, |
| 232 | location: parsed.location || DEFAULT_ZONE, |
| 233 | partitionConfig: topic.partitionConfig, |
| 234 | retentionConfig: topic.retentionConfig, |
| 235 | reservationConfig: topic.reservationConfig, |
| 236 | }; |
| 237 | }; |
| 238 | |
| 239 | export const AdminTopicProvider = () => |
| 240 | Provider.succeed(AdminTopic, { |
| 241 | stables: ["name", "topicId", "project", "location"], |
| 242 | |
| 243 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 244 | if (!isResolved(news)) return undefined; |
| 245 | const previousCount = countOf(olds?.partitionConfig?.count ?? output?.partitionConfig?.count); |
| 246 | const nextCount = countOf( |
| 247 | news.partitionConfig?.count ?? |
| 248 | olds?.partitionConfig?.count ?? |
| 249 | output?.partitionConfig?.count, |
| 250 | ); |
| 251 | return replaceOnIdentity({ |
| 252 | previousId: olds?.topicId ?? output?.topicId, |
| 253 | nextId: news.topicId ?? olds?.topicId ?? output?.topicId, |
| 254 | previousLocation: normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE), |
| 255 | nextLocation: normalizeLocation( |
| 256 | news.location ?? olds?.location ?? output?.location, |
| 257 | DEFAULT_ZONE, |
| 258 | ), |
| 259 | extra: nextCount < previousCount, |
| 260 | }); |
| 261 | }), |
| 262 | |
| 263 | read: Effect.fn(function* ({ id, olds, output }) { |
| 264 | const env = yield* GcpEnvironment.current; |
| 265 | const topicId = yield* toResourceId(id, olds?.topicId, output?.topicId); |
| 266 | const location = normalizeLocation(olds?.location ?? output?.location, DEFAULT_ZONE); |
| 267 | const name = output?.name ?? resourceName(env.project, location, TOPIC_COLLECTION, topicId); |
| 268 | const existing = yield* getTopic(name); |
| 269 | if (existing === undefined) return undefined; |
| 270 | const attrs = toAttrs(existing, env.project); |
| 271 | const owned = yield* ownedByAlchemy(id, attrs.topicId); |
| 272 | if (owned) return attrs; |
| 273 | return hasOwnershipMarker(attrs.topicId) ? undefined : Unowned(attrs); |
| 274 | }), |
| 275 | |
| 276 | list: () => |
| 277 | Effect.gen(function* () { |
| 278 | const env = yield* GcpEnvironment.current; |
| 279 | const items = yield* listOwnedTopics(); |
| 280 | return items.map((item) => toAttrs(item, env.project)); |
| 281 | }), |
| 282 | |
| 283 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 284 | const env = yield* GcpEnvironment.current; |
| 285 | const topicId = yield* toResourceId(id, news.topicId, output?.topicId); |
| 286 | const location = normalizeLocation(news.location ?? output?.location, DEFAULT_ZONE); |
| 287 | const name = resourceName(env.project, location, TOPIC_COLLECTION, topicId); |
| 288 | const partitionConfig = desiredPartition(news); |
| 289 | const retentionConfig = desiredRetention(news); |
| 290 | const reservationConfig = desiredReservation(news, env.project, location); |
| 291 | |
| 292 | let current = yield* getTopic(output?.name ?? name); |
| 293 | |
| 294 | if (current === undefined) { |
| 295 | const body: pubsublite.Topic = { |
| 296 | partitionConfig, |
| 297 | retentionConfig, |
| 298 | }; |
| 299 | if (reservationConfig?.throughputReservation) { |
| 300 | body.reservationConfig = reservationConfig; |
| 301 | } |
| 302 | const created = yield* pubsublite |
| 303 | .createAdminProjectsLocationsTopics({ |
| 304 | parent: parentOf(env.project, location), |
| 305 | topicId, |
| 306 | body, |
| 307 | }) |
| 308 | .pipe(Effect.catchTag("Conflict", () => getTopic(name))); |
| 309 | current = created ?? undefined; |
| 310 | } |
| 311 | |
| 312 | if (current === undefined) { |
| 313 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 314 | } |
| 315 | |
| 316 | const observedName = current.name ?? name; |
| 317 | const reservationChanged = |
| 318 | reservationConfig !== undefined && |
| 319 | !sameRef( |
| 320 | current.reservationConfig?.throughputReservation, |
| 321 | reservationConfig.throughputReservation, |
| 322 | ); |
| 323 | const mask = fieldMask([ |
| 324 | partitionKey(current.partitionConfig) !== partitionKey(partitionConfig) && |
| 325 | "partitionConfig", |
| 326 | retentionKey(current.retentionConfig) !== retentionKey(retentionConfig) && |
| 327 | "retentionConfig", |
| 328 | reservationChanged && "reservationConfig", |
| 329 | ]); |
| 330 | if (mask.length > 0) { |
| 331 | const body: pubsublite.Topic = { |
| 332 | name: observedName, |
| 333 | partitionConfig, |
| 334 | retentionConfig, |
| 335 | }; |
| 336 | if (reservationConfig !== undefined) { |
| 337 | body.reservationConfig = reservationConfig; |
| 338 | } |
| 339 | current = yield* pubsublite.patchAdminProjectsLocationsTopics({ |
| 340 | name: observedName, |
| 341 | updateMask: mask, |
| 342 | body, |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | return toAttrs(current, env.project); |
| 347 | }), |
| 348 | |
| 349 | delete: Effect.fn(function* ({ output }) { |
| 350 | yield* pubsublite |
| 351 | .deleteAdminProjectsLocationsTopics({ |
| 352 | name: output.name, |
| 353 | }) |
| 354 | .pipe(ignoreMissing, retryInUse); |
| 355 | yield* waitUntilGone(getTopic(output.name)); |
| 356 | }), |
| 357 | }); |