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 emr from "@distilled.cloud/aws/emr"; |
| 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 Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | |
| 14 | export class StudioUnexpectedResponse extends Data.TaggedError("StudioUnexpectedResponse")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export interface StudioProps { |
| 20 | /** |
| 21 | * Descriptive name of the Studio. If omitted, a deterministic physical |
| 22 | * name is generated. Updateable in place. |
| 23 | */ |
| 24 | studioName?: string; |
| 25 | /** |
| 26 | * Authentication mode — `IAM` or `SSO` (IAM Identity Center). Changing the |
| 27 | * mode replaces the Studio. |
| 28 | * @default "IAM" |
| 29 | */ |
| 30 | authMode?: "IAM" | "SSO"; |
| 31 | /** |
| 32 | * VPC the Studio workspaces and notebooks connect to. Changing the VPC |
| 33 | * replaces the Studio. |
| 34 | */ |
| 35 | vpcId: string; |
| 36 | /** |
| 37 | * Subnets of the VPC the Studio can use (at most 5). Updateable in place |
| 38 | * (subnets can be added, not removed). |
| 39 | */ |
| 40 | subnetIds: string[]; |
| 41 | /** |
| 42 | * ARN of the IAM service role the Studio assumes, trusting |
| 43 | * `elasticmapreduce.amazonaws.com`. Changing the role replaces the Studio. |
| 44 | */ |
| 45 | serviceRole: string; |
| 46 | /** |
| 47 | * ARN of the IAM user role Studio users assume (`SSO` auth mode only). |
| 48 | * Changing the role replaces the Studio. |
| 49 | */ |
| 50 | userRole?: string; |
| 51 | /** |
| 52 | * Security group for the Studio workspace (must allow outbound TCP 18888 |
| 53 | * to the engine security group and outbound HTTPS 443). Changing it |
| 54 | * replaces the Studio. |
| 55 | */ |
| 56 | workspaceSecurityGroupId: string; |
| 57 | /** |
| 58 | * Security group for the Studio engine (must allow inbound TCP 18888 from |
| 59 | * the workspace security group). Changing it replaces the Studio. |
| 60 | */ |
| 61 | engineSecurityGroupId: string; |
| 62 | /** |
| 63 | * S3 location that backs up Studio workspaces and notebooks, e.g. |
| 64 | * `"s3://my-bucket/studio/"`. Updateable in place. |
| 65 | */ |
| 66 | defaultS3Location: string; |
| 67 | /** |
| 68 | * Description of the Studio. Updateable in place. |
| 69 | */ |
| 70 | description?: string; |
| 71 | /** |
| 72 | * Authentication endpoint of your identity provider (IAM federation only). |
| 73 | * Changing it replaces the Studio. |
| 74 | */ |
| 75 | idpAuthUrl?: string; |
| 76 | /** |
| 77 | * Name the IdP uses for its RelayState parameter (IAM federation only). |
| 78 | * Changing it replaces the Studio. |
| 79 | */ |
| 80 | idpRelayStateParameterName?: string; |
| 81 | /** |
| 82 | * Whether trusted identity propagation is enabled for the Studio. |
| 83 | * Changing it replaces the Studio. |
| 84 | */ |
| 85 | trustedIdentityPropagationEnabled?: boolean; |
| 86 | /** |
| 87 | * Whether IAM Identity Center user assignment is `REQUIRED` or `OPTIONAL` |
| 88 | * (SSO auth mode only). Changing it replaces the Studio. |
| 89 | */ |
| 90 | idcUserAssignment?: "REQUIRED" | "OPTIONAL"; |
| 91 | /** |
| 92 | * ARN of the IAM Identity Center instance (SSO auth mode only). Changing |
| 93 | * it replaces the Studio. |
| 94 | */ |
| 95 | idcInstanceArn?: string; |
| 96 | /** |
| 97 | * KMS key ARN used to encrypt Studio workspace and notebook files. |
| 98 | * Changing it replaces the Studio. |
| 99 | */ |
| 100 | encryptionKeyArn?: string; |
| 101 | /** |
| 102 | * User-defined tags for the Studio. |
| 103 | */ |
| 104 | tags?: Record<string, string>; |
| 105 | } |
| 106 | |
| 107 | export interface Studio extends Resource< |
| 108 | "AWS.EMR.Studio", |
| 109 | StudioProps, |
| 110 | { |
| 111 | /** The ID of the Studio (e.g. `es-0123456789ABCDEFGHIJKLMNOP`). */ |
| 112 | studioId: string; |
| 113 | /** The ARN of the Studio. */ |
| 114 | studioArn: string; |
| 115 | /** The name of the Studio. */ |
| 116 | studioName: string; |
| 117 | /** The unique access URL of the Studio. */ |
| 118 | url: string | undefined; |
| 119 | /** The tags applied to the Studio. */ |
| 120 | tags: Record<string, string>; |
| 121 | }, |
| 122 | never, |
| 123 | Providers |
| 124 | > {} |
| 125 | |
| 126 | /** |
| 127 | * An Amazon EMR Studio — a web-based IDE for notebooks and interactive |
| 128 | * workloads that attaches to EMR clusters. |
| 129 | * |
| 130 | * A Studio itself is free; you pay for the clusters it attaches to. Each |
| 131 | * Studio needs a VPC with subnets, a workspace and an engine security group, |
| 132 | * an IAM service role, and an S3 backup location. |
| 133 | * ### Creating a Studio |
| 134 | * **Example:** IAM-Authenticated Studio |
| 135 | * ```typescript |
| 136 | * const studio = yield* Studio("Notebooks", { |
| 137 | * authMode: "IAM", |
| 138 | * vpcId: vpc.vpcId, |
| 139 | * subnetIds: [subnetA.subnetId, subnetB.subnetId], |
| 140 | * serviceRole: serviceRole.roleArn, |
| 141 | * workspaceSecurityGroupId: workspaceSg.groupId, |
| 142 | * engineSecurityGroupId: engineSg.groupId, |
| 143 | * defaultS3Location: Output.interpolate`s3://${bucket.bucketName}/studio/`, |
| 144 | * }); |
| 145 | * ``` |
| 146 | * |
| 147 | * **Example:** Studio with Description and Tags |
| 148 | * ```typescript |
| 149 | * const studio = yield* Studio("Notebooks", { |
| 150 | * authMode: "IAM", |
| 151 | * vpcId: vpc.vpcId, |
| 152 | * subnetIds: [subnetA.subnetId], |
| 153 | * serviceRole: serviceRole.roleArn, |
| 154 | * workspaceSecurityGroupId: workspaceSg.groupId, |
| 155 | * engineSecurityGroupId: engineSg.groupId, |
| 156 | * defaultS3Location: Output.interpolate`s3://${bucket.bucketName}/studio/`, |
| 157 | * description: "Data-science notebooks", |
| 158 | * tags: { team: "analytics" }, |
| 159 | * }); |
| 160 | * ``` |
| 161 | * |
| 162 | * @resource |
| 163 | */ |
| 164 | export const Studio = Resource<Studio>("AWS.EMR.Studio"); |
| 165 | |
| 166 | const toTagRecord = ( |
| 167 | tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined, |
| 168 | ): Record<string, string> => |
| 169 | Object.fromEntries( |
| 170 | (tags ?? []) |
| 171 | .filter( |
| 172 | (tag): tag is { Key: string; Value: string } => |
| 173 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 174 | ) |
| 175 | .map((tag) => [tag.Key, tag.Value]), |
| 176 | ); |
| 177 | |
| 178 | const sameStringSet = ( |
| 179 | a: readonly string[] | undefined, |
| 180 | b: readonly string[] | undefined, |
| 181 | ): boolean => { |
| 182 | const left = [...(a ?? [])].sort(); |
| 183 | const right = [...(b ?? [])].sort(); |
| 184 | return left.length === right.length && left.every((v, i) => v === right[i]); |
| 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * A freshly created IAM service role (or its inline policy) takes a few |
| 189 | * seconds to propagate (IAM eventual consistency); CreateStudio validates |
| 190 | * both assumability and S3-location access at create time and surfaces that |
| 191 | * window as the typed StudioServiceRoleNotAssumable / |
| 192 | * StudioServiceRoleMissingS3Access tags. Bounded retry (~40s max) — a |
| 193 | * genuinely misconfigured role still fails, just slower. |
| 194 | * |
| 195 | * NOTE: keep this helper at module scope with an EXPLICIT return annotation — |
| 196 | * an inlined `Effect.retry` in provider lifecycle code leaves `Retry.Return`'s |
| 197 | * conditional type unresolved in the provider's inferred layer type, which |
| 198 | * TypeScript's declaration emit widens to an `unknown` R. |
| 199 | */ |
| 200 | const retryWhileRolePropagates = <A, E extends { _tag: string }, R>( |
| 201 | self: Effect.Effect<A, E, R>, |
| 202 | ): Effect.Effect<A, E, R> => |
| 203 | Effect.retry(self, { |
| 204 | while: (e) => |
| 205 | e._tag === "StudioServiceRoleNotAssumable" || e._tag === "StudioServiceRoleMissingS3Access", |
| 206 | schedule: Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(10)]), |
| 207 | }); |
| 208 | |
| 209 | export const StudioProvider = () => |
| 210 | Provider.effect( |
| 211 | Studio, |
| 212 | Effect.gen(function* () { |
| 213 | const toName = (id: string, props: Partial<StudioProps>) => |
| 214 | props.studioName |
| 215 | ? Effect.succeed(props.studioName) |
| 216 | : createPhysicalName({ id, maxLength: 80 }); |
| 217 | |
| 218 | const readStudio = Effect.fn(function* (studioId: string) { |
| 219 | const response = yield* emr |
| 220 | .describeStudio({ StudioId: studioId }) |
| 221 | .pipe(Effect.catchTag("StudioNotFound", () => Effect.undefined)); |
| 222 | return response?.Studio; |
| 223 | }); |
| 224 | |
| 225 | // Studio names are not unique; the deterministic physical name is. |
| 226 | // Used when state was lost and we only know the derived name. |
| 227 | const findStudioByName = Effect.fn(function* (name: string) { |
| 228 | const matches = yield* emr.listStudios.items({}).pipe( |
| 229 | Stream.filter((summary) => summary.Name === name), |
| 230 | Stream.take(1), |
| 231 | Stream.runCollect, |
| 232 | ); |
| 233 | const summary = Array.from(matches)[0]; |
| 234 | return summary?.StudioId ? yield* readStudio(summary.StudioId) : undefined; |
| 235 | }); |
| 236 | |
| 237 | const toAttrs = Effect.fn(function* (studio: emr.Studio) { |
| 238 | if (!studio.StudioId || !studio.StudioArn || !studio.Name) { |
| 239 | return yield* new StudioUnexpectedResponse({ |
| 240 | message: `EMR Studio '${studio.StudioId}' is missing its ARN`, |
| 241 | }); |
| 242 | } |
| 243 | return { |
| 244 | studioId: studio.StudioId, |
| 245 | studioArn: studio.StudioArn, |
| 246 | studioName: studio.Name, |
| 247 | url: studio.Url, |
| 248 | tags: toTagRecord(studio.Tags), |
| 249 | }; |
| 250 | }); |
| 251 | |
| 252 | return { |
| 253 | stables: ["studioId", "studioArn"], |
| 254 | |
| 255 | diff: Effect.fn(function* ({ olds, news }) { |
| 256 | if (!isResolved(news)) return undefined; |
| 257 | const n = news; |
| 258 | const o = olds; |
| 259 | if (n === undefined || o === undefined) return undefined; |
| 260 | // Create-only properties force a replacement. (Name, description, |
| 261 | // subnets, and the S3 location update in place.) |
| 262 | if ((n.authMode ?? "IAM") !== (o.authMode ?? "IAM")) { |
| 263 | return { action: "replace" } as const; |
| 264 | } |
| 265 | if (n.vpcId !== o.vpcId) { |
| 266 | return { action: "replace" } as const; |
| 267 | } |
| 268 | if (n.serviceRole !== o.serviceRole) { |
| 269 | return { action: "replace" } as const; |
| 270 | } |
| 271 | if ((n.userRole ?? undefined) !== (o.userRole ?? undefined)) { |
| 272 | return { action: "replace" } as const; |
| 273 | } |
| 274 | if (n.workspaceSecurityGroupId !== o.workspaceSecurityGroupId) { |
| 275 | return { action: "replace" } as const; |
| 276 | } |
| 277 | if (n.engineSecurityGroupId !== o.engineSecurityGroupId) { |
| 278 | return { action: "replace" } as const; |
| 279 | } |
| 280 | if ((n.idpAuthUrl ?? undefined) !== (o.idpAuthUrl ?? undefined)) { |
| 281 | return { action: "replace" } as const; |
| 282 | } |
| 283 | if ( |
| 284 | (n.idpRelayStateParameterName ?? undefined) !== |
| 285 | (o.idpRelayStateParameterName ?? undefined) |
| 286 | ) { |
| 287 | return { action: "replace" } as const; |
| 288 | } |
| 289 | if ( |
| 290 | (n.trustedIdentityPropagationEnabled ?? undefined) !== |
| 291 | (o.trustedIdentityPropagationEnabled ?? undefined) |
| 292 | ) { |
| 293 | return { action: "replace" } as const; |
| 294 | } |
| 295 | if ((n.idcUserAssignment ?? undefined) !== (o.idcUserAssignment ?? undefined)) { |
| 296 | return { action: "replace" } as const; |
| 297 | } |
| 298 | if ((n.idcInstanceArn ?? undefined) !== (o.idcInstanceArn ?? undefined)) { |
| 299 | return { action: "replace" } as const; |
| 300 | } |
| 301 | if ((n.encryptionKeyArn ?? undefined) !== (o.encryptionKeyArn ?? undefined)) { |
| 302 | return { action: "replace" } as const; |
| 303 | } |
| 304 | }), |
| 305 | |
| 306 | read: Effect.fn(function* ({ id, olds, output }) { |
| 307 | const studio = output?.studioId |
| 308 | ? yield* readStudio(output.studioId) |
| 309 | : yield* findStudioByName(yield* toName(id, olds ?? {})); |
| 310 | if (!studio) return undefined; |
| 311 | const attrs = yield* toAttrs(studio); |
| 312 | return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs); |
| 313 | }), |
| 314 | |
| 315 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 316 | const props = news!; |
| 317 | const name = yield* toName(id, props); |
| 318 | const internalTags = yield* createInternalTags(id); |
| 319 | const desiredTags = { ...internalTags, ...props.tags }; |
| 320 | |
| 321 | // 1. Observe — cloud state is authoritative; output is only an id |
| 322 | // cache. |
| 323 | let observed = output?.studioId |
| 324 | ? yield* readStudio(output.studioId) |
| 325 | : yield* findStudioByName(name); |
| 326 | |
| 327 | // 2. Ensure — create if missing. Create errors (invalid security |
| 328 | // groups, role, subnets) propagate directly. |
| 329 | if (observed === undefined) { |
| 330 | const created = yield* retryWhileRolePropagates( |
| 331 | emr.createStudio({ |
| 332 | Name: name, |
| 333 | AuthMode: props.authMode ?? "IAM", |
| 334 | VpcId: props.vpcId, |
| 335 | SubnetIds: props.subnetIds, |
| 336 | ServiceRole: props.serviceRole, |
| 337 | UserRole: props.userRole, |
| 338 | WorkspaceSecurityGroupId: props.workspaceSecurityGroupId, |
| 339 | EngineSecurityGroupId: props.engineSecurityGroupId, |
| 340 | DefaultS3Location: props.defaultS3Location, |
| 341 | Description: props.description, |
| 342 | IdpAuthUrl: props.idpAuthUrl, |
| 343 | IdpRelayStateParameterName: props.idpRelayStateParameterName, |
| 344 | TrustedIdentityPropagationEnabled: props.trustedIdentityPropagationEnabled, |
| 345 | IdcUserAssignment: props.idcUserAssignment, |
| 346 | IdcInstanceArn: props.idcInstanceArn, |
| 347 | EncryptionKeyArn: props.encryptionKeyArn, |
| 348 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 349 | Key, |
| 350 | Value, |
| 351 | })), |
| 352 | }), |
| 353 | ); |
| 354 | if (!created.StudioId) { |
| 355 | return yield* new StudioUnexpectedResponse({ |
| 356 | message: `CreateStudio for '${name}' returned no StudioId`, |
| 357 | }); |
| 358 | } |
| 359 | observed = yield* readStudio(created.StudioId); |
| 360 | if (observed === undefined) { |
| 361 | return yield* new StudioUnexpectedResponse({ |
| 362 | message: `EMR Studio '${created.StudioId}' not visible after create`, |
| 363 | }); |
| 364 | } |
| 365 | } |
| 366 | const studioId = observed.StudioId!; |
| 367 | |
| 368 | // 3. Sync — compute the update delta from OBSERVED state. |
| 369 | const update: emr.UpdateStudioInput = { StudioId: studioId }; |
| 370 | let mutated = false; |
| 371 | if (observed.Name !== name) { |
| 372 | update.Name = name; |
| 373 | mutated = true; |
| 374 | } |
| 375 | if (props.description !== undefined && props.description !== observed.Description) { |
| 376 | update.Description = props.description; |
| 377 | mutated = true; |
| 378 | } |
| 379 | if (!sameStringSet(props.subnetIds, observed.SubnetIds)) { |
| 380 | update.SubnetIds = props.subnetIds; |
| 381 | mutated = true; |
| 382 | } |
| 383 | if (props.defaultS3Location !== observed.DefaultS3Location) { |
| 384 | update.DefaultS3Location = props.defaultS3Location; |
| 385 | mutated = true; |
| 386 | } |
| 387 | if (mutated) { |
| 388 | yield* emr.updateStudio(update); |
| 389 | } |
| 390 | |
| 391 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 392 | const observedTags = toTagRecord(observed.Tags); |
| 393 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 394 | if (upsert.length > 0) { |
| 395 | yield* emr.addTags({ ResourceId: studioId, Tags: upsert }); |
| 396 | } |
| 397 | if (removed.length > 0) { |
| 398 | yield* emr.removeTags({ ResourceId: studioId, TagKeys: removed }); |
| 399 | } |
| 400 | |
| 401 | // 4. Return fresh attributes. |
| 402 | const final = yield* readStudio(studioId); |
| 403 | yield* session.note(studioId); |
| 404 | return yield* toAttrs(final ?? observed); |
| 405 | }), |
| 406 | |
| 407 | delete: Effect.fn(function* ({ output }) { |
| 408 | yield* emr |
| 409 | .deleteStudio({ StudioId: output.studioId }) |
| 410 | .pipe(Effect.catchTag("StudioNotFound", () => Effect.void)); |
| 411 | }), |
| 412 | |
| 413 | list: () => |
| 414 | emr.listStudios.items({}).pipe( |
| 415 | Stream.runCollect, |
| 416 | Effect.map((chunk) => |
| 417 | Array.from(chunk).flatMap((summary) => (summary.StudioId ? [summary.StudioId] : [])), |
| 418 | ), |
| 419 | Effect.flatMap( |
| 420 | Effect.forEach((studioId) => readStudio(studioId), { |
| 421 | concurrency: 4, |
| 422 | }), |
| 423 | ), |
| 424 | Effect.map((studios) => |
| 425 | studios.filter((studio): studio is emr.Studio => studio !== undefined), |
| 426 | ), |
| 427 | Effect.flatMap(Effect.forEach((studio) => toAttrs(studio), { concurrency: 4 })), |
| 428 | ), |
| 429 | }; |
| 430 | }), |
| 431 | ); |