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, 04:45 UTC
| 1 | import * as aoss from "@distilled.cloud/aws/opensearchserverless"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { recordToTagList, retryWhileConflict, tagsToRecord } from "./internal.ts"; |
| 12 | |
| 13 | export interface CollectionGroupCapacityLimits { |
| 14 | /** |
| 15 | * Maximum indexing capacity, in OpenSearch Compute Units (OCUs), shared by |
| 16 | * the group's collections. |
| 17 | */ |
| 18 | maxIndexingCapacityInOCU?: number; |
| 19 | /** |
| 20 | * Maximum search capacity, in OCUs, shared by the group's collections. |
| 21 | */ |
| 22 | maxSearchCapacityInOCU?: number; |
| 23 | /** |
| 24 | * Minimum indexing capacity, in OCUs, reserved for the group. |
| 25 | */ |
| 26 | minIndexingCapacityInOCU?: number; |
| 27 | /** |
| 28 | * Minimum search capacity, in OCUs, reserved for the group. |
| 29 | */ |
| 30 | minSearchCapacityInOCU?: number; |
| 31 | } |
| 32 | |
| 33 | export interface CollectionGroupProps { |
| 34 | /** |
| 35 | * Name of the collection group (3-32 characters, lowercase; must start with |
| 36 | * a lowercase letter). Changing the name replaces the group. |
| 37 | * @default a generated physical name |
| 38 | */ |
| 39 | groupName?: string; |
| 40 | /** |
| 41 | * Whether collections in the group deploy redundant standby replicas. Set |
| 42 | * at creation time; changing it replaces the group. |
| 43 | * @default "ENABLED" |
| 44 | */ |
| 45 | standbyReplicas?: "ENABLED" | "DISABLED"; |
| 46 | /** |
| 47 | * A human-readable description of the collection group. |
| 48 | */ |
| 49 | description?: string; |
| 50 | /** |
| 51 | * OCU capacity limits shared by the collections in the group. |
| 52 | */ |
| 53 | capacityLimits?: CollectionGroupCapacityLimits; |
| 54 | /** |
| 55 | * Tags to apply to the collection group. Merged with the internal Alchemy |
| 56 | * tags. |
| 57 | */ |
| 58 | tags?: Record<string, string>; |
| 59 | } |
| 60 | |
| 61 | export interface CollectionGroup extends Resource< |
| 62 | "AWS.OpenSearchServerless.CollectionGroup", |
| 63 | CollectionGroupProps, |
| 64 | { |
| 65 | /** |
| 66 | * Unique identifier of the collection group. |
| 67 | */ |
| 68 | collectionGroupId: string; |
| 69 | /** |
| 70 | * Name of the collection group. |
| 71 | */ |
| 72 | collectionGroupName: string; |
| 73 | /** |
| 74 | * ARN of the collection group. |
| 75 | */ |
| 76 | collectionGroupArn: string; |
| 77 | /** |
| 78 | * Whether standby replicas are enabled for the group's collections. |
| 79 | */ |
| 80 | standbyReplicas?: string; |
| 81 | /** |
| 82 | * Number of collections currently in the group. |
| 83 | */ |
| 84 | numberOfCollections?: number; |
| 85 | /** |
| 86 | * The capacity generation of the group. |
| 87 | */ |
| 88 | generation?: string; |
| 89 | }, |
| 90 | {}, |
| 91 | Providers |
| 92 | > {} |
| 93 | |
| 94 | /** |
| 95 | * An Amazon OpenSearch Serverless collection group. Collection groups manage |
| 96 | * OpenSearch Compute Units (OCUs) at a group level — multiple collections |
| 97 | * share the group's capacity limits instead of each collection scaling |
| 98 | * independently. |
| 99 | * |
| 100 | * ### Creating Collection Groups |
| 101 | * **Example:** Capacity-Bounded Collection Group |
| 102 | * ```typescript |
| 103 | * import * as AWS from "@alchemy.run/aws"; |
| 104 | * |
| 105 | * const group = yield* AWS.OpenSearchServerless.CollectionGroup("Group", { |
| 106 | * groupName: "analytics", |
| 107 | * standbyReplicas: "DISABLED", |
| 108 | * capacityLimits: { |
| 109 | * maxIndexingCapacityInOCU: 4, |
| 110 | * maxSearchCapacityInOCU: 4, |
| 111 | * }, |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * @resource |
| 116 | */ |
| 117 | export const CollectionGroup = Resource<CollectionGroup>( |
| 118 | "AWS.OpenSearchServerless.CollectionGroup", |
| 119 | ); |
| 120 | |
| 121 | export const CollectionGroupProvider = () => |
| 122 | Provider.effect( |
| 123 | CollectionGroup, |
| 124 | Effect.gen(function* () { |
| 125 | const createName = Effect.fn(function* ( |
| 126 | id: string, |
| 127 | props: { groupName?: string | undefined }, |
| 128 | ) { |
| 129 | return ( |
| 130 | props.groupName ?? (yield* createPhysicalName({ id, maxLength: 32, lowercase: true })) |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | const toAttributes = (detail: aoss.CollectionGroupDetail) => ({ |
| 135 | collectionGroupId: detail.id!, |
| 136 | collectionGroupName: detail.name!, |
| 137 | collectionGroupArn: detail.arn!, |
| 138 | standbyReplicas: detail.standbyReplicas, |
| 139 | numberOfCollections: detail.numberOfCollections, |
| 140 | generation: detail.generation, |
| 141 | }); |
| 142 | |
| 143 | // batchGetCollectionGroup reports a missing group in |
| 144 | // collectionGroupErrorDetails instead of failing. |
| 145 | const observeByName = Effect.fn(function* (name: string) { |
| 146 | const response = yield* aoss.batchGetCollectionGroup({ |
| 147 | names: [name], |
| 148 | }); |
| 149 | return response.collectionGroupDetails?.[0]; |
| 150 | }); |
| 151 | |
| 152 | const capacityDrift = ( |
| 153 | desired: CollectionGroupCapacityLimits | undefined, |
| 154 | observed: aoss.CollectionGroupCapacityLimits | undefined, |
| 155 | ): boolean => |
| 156 | desired !== undefined && |
| 157 | (desired.maxIndexingCapacityInOCU !== observed?.maxIndexingCapacityInOCU || |
| 158 | desired.maxSearchCapacityInOCU !== observed?.maxSearchCapacityInOCU || |
| 159 | desired.minIndexingCapacityInOCU !== observed?.minIndexingCapacityInOCU || |
| 160 | desired.minSearchCapacityInOCU !== observed?.minSearchCapacityInOCU); |
| 161 | |
| 162 | // batchGetCollectionGroup does not return the group's tags — read them |
| 163 | // via listTagsForResource (the authoritative tag store). |
| 164 | const observeTags = Effect.fn(function* (arn: string) { |
| 165 | return yield* aoss |
| 166 | .listTagsForResource({ resourceArn: arn }) |
| 167 | .pipe(Effect.map((r) => tagsToRecord(r.tags))); |
| 168 | }); |
| 169 | |
| 170 | const syncTags = Effect.fn(function* ( |
| 171 | arn: string, |
| 172 | observed: Record<string, string>, |
| 173 | desired: Record<string, string>, |
| 174 | ) { |
| 175 | const { upsert, removed } = diffTags(observed, desired); |
| 176 | if (upsert.length > 0) { |
| 177 | yield* aoss.tagResource({ |
| 178 | resourceArn: arn, |
| 179 | tags: recordToTagList(Object.fromEntries(upsert.map((t) => [t.Key, t.Value]))), |
| 180 | }); |
| 181 | } |
| 182 | if (removed.length > 0) { |
| 183 | yield* aoss.untagResource({ resourceArn: arn, tagKeys: removed }); |
| 184 | } |
| 185 | }); |
| 186 | |
| 187 | return CollectionGroup.Provider.of({ |
| 188 | stables: ["collectionGroupId", "collectionGroupName", "collectionGroupArn"], |
| 189 | |
| 190 | list: () => |
| 191 | Effect.gen(function* () { |
| 192 | const pages = yield* aoss.listCollectionGroups.pages({}).pipe(Stream.runCollect); |
| 193 | return Array.from(pages) |
| 194 | .flatMap((page) => page.collectionGroupSummaries ?? []) |
| 195 | .filter((s) => s.id !== undefined && s.name !== undefined && s.arn !== undefined) |
| 196 | .map((s) => ({ |
| 197 | collectionGroupId: s.id!, |
| 198 | collectionGroupName: s.name!, |
| 199 | collectionGroupArn: s.arn!, |
| 200 | generation: s.generation, |
| 201 | })); |
| 202 | }), |
| 203 | |
| 204 | read: Effect.fn(function* ({ id, olds, output }) { |
| 205 | const name = output?.collectionGroupName ?? (yield* createName(id, olds ?? {})); |
| 206 | const detail = yield* observeByName(name); |
| 207 | if (detail?.id === undefined || detail.arn === undefined) { |
| 208 | return undefined; |
| 209 | } |
| 210 | const attrs = toAttributes(detail); |
| 211 | const tags = yield* observeTags(detail.arn).pipe(Effect.orElseSucceed(() => ({}))); |
| 212 | return (yield* hasAlchemyTags( |
| 213 | id, |
| 214 | Object.entries(tags).map(([Key, Value]) => ({ Key, Value })), |
| 215 | )) |
| 216 | ? attrs |
| 217 | : Unowned(attrs); |
| 218 | }), |
| 219 | |
| 220 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 221 | if (!isResolved(news)) return undefined; |
| 222 | const oldName = yield* createName(id, olds); |
| 223 | const newName = yield* createName(id, news); |
| 224 | if (oldName !== newName) { |
| 225 | return { action: "replace" } as const; |
| 226 | } |
| 227 | if ( |
| 228 | olds.standbyReplicas !== undefined && |
| 229 | news.standbyReplicas !== undefined && |
| 230 | olds.standbyReplicas !== news.standbyReplicas |
| 231 | ) { |
| 232 | return { action: "replace" } as const; |
| 233 | } |
| 234 | // description/capacityLimits/tags fall through to update |
| 235 | }), |
| 236 | |
| 237 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 238 | const name = output?.collectionGroupName ?? (yield* createName(id, news)); |
| 239 | const internalTags = yield* createInternalTags(id); |
| 240 | const desiredTags = { ...news.tags, ...internalTags }; |
| 241 | |
| 242 | // 1. OBSERVE — cloud state is authoritative |
| 243 | let detail = yield* observeByName(name); |
| 244 | |
| 245 | // 2. ENSURE — create if missing; tolerate a concurrent create race |
| 246 | if (detail === undefined) { |
| 247 | const created = yield* aoss |
| 248 | .createCollectionGroup({ |
| 249 | name, |
| 250 | standbyReplicas: news.standbyReplicas ?? "ENABLED", |
| 251 | description: news.description, |
| 252 | capacityLimits: news.capacityLimits, |
| 253 | tags: recordToTagList(desiredTags), |
| 254 | }) |
| 255 | .pipe( |
| 256 | Effect.map((r) => r.createCollectionGroupDetail), |
| 257 | Effect.catchTag("ConflictException", () => Effect.undefined), |
| 258 | ); |
| 259 | detail = created !== undefined ? { ...created } : yield* observeByName(name); |
| 260 | } else { |
| 261 | // 3. SYNC — description/capacityLimits when observed drifts |
| 262 | const descriptionDrift = |
| 263 | news.description !== undefined && news.description !== detail.description; |
| 264 | const limitsDrift = capacityDrift(news.capacityLimits, detail.capacityLimits); |
| 265 | if (descriptionDrift || limitsDrift) { |
| 266 | yield* aoss.updateCollectionGroup({ |
| 267 | id: detail.id!, |
| 268 | description: descriptionDrift ? news.description : undefined, |
| 269 | capacityLimits: limitsDrift ? news.capacityLimits : undefined, |
| 270 | }); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (detail?.id === undefined || detail.arn === undefined) { |
| 275 | return yield* aoss.ResourceNotFoundException.make({ |
| 276 | message: `collection group ${name} not visible after reconcile`, |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | // 3b. SYNC TAGS — diff against observed cloud tags (read via |
| 281 | // listTagsForResource on every path: adoption may bring foreign |
| 282 | // tags, and a create race may have dropped ours). |
| 283 | yield* syncTags(detail.arn, yield* observeTags(detail.arn), desiredTags); |
| 284 | |
| 285 | yield* session.note(detail.id); |
| 286 | return toAttributes(detail); |
| 287 | }), |
| 288 | |
| 289 | delete: Effect.fn(function* ({ output }) { |
| 290 | // A group with collections still in it (or tearing down) surfaces |
| 291 | // ConflictException — retry through the teardown window. |
| 292 | yield* retryWhileConflict( |
| 293 | aoss.deleteCollectionGroup({ id: output.collectionGroupId }), |
| 294 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 295 | }), |
| 296 | }); |
| 297 | }), |
| 298 | ); |